| Hardware Modelling with C++ Introduction This tutorial is an introduction to the use of C++ for describing hardware models. Essential constructs for the creation of SystemC models are also introduced. Note this is a deliberately simplified description of C++. Full details on the language, coding styles; design guidelines can be found on the Essential C++ for SystemC class. Tutorial The class in C++ is called an Abstract Data Type (ADT). It defines both data members and access functions (also called methods). Both data members and access functions are said to be private by default. In other words, data members and access functions are not visible from the outside world. This ADT mechanism is analogous to a package and package body in VHDL. The designer is responsible for making publicly available the essential set of access functions for manipulating an ADT.
The semantics for a C++ class declaration is as follows:
class counter
{ int value; public: void do_reset() { value = 0 ; } void do_count_up() { value++ ; }
In this example we see the declaration of an ADT called counter with a data member value and publicly available access functions: do_reset,do_count_up and do_read. Although this class declaration is complete, a class declaration will commonly also contain specialized functions such as constructors and a destructor.
When constructors are used, they provide initial values for the ADT’s data members. This mechanism is the only allowed means for setting a default value to any data member.
A destructor is used to perform clean-up operations before an instance of the ADT becomes out of scope. Pragmatically, the destructor is used for closing previously opened files or de-allocating dynamically allocated memory.
class counter { int value; public: void do_reset() { value = 0 ; } void do_count_up() { value++ ; } int do_read() { return value; } counter() { cout << "Simple constructor" << endl; value = 10; } counter(int arg): value(arg) { cout << "Interesting constructor" << endl; } ~counter() { cout << "Destroying a counter object" << endl; } Previous Tutorial | Next Tutorial |
