Home > SytemC Tutorials > SystemC Tutorial: Hardware Modeling with C++

SystemC Tutorial: Hardware Modeling with C++

Forewords

C++ implements Object-Orientation on the C language. For most Hardware Engineers, the principles of Object-Orientation seem fairly remote from the creation of Hardware components. However ironically, Object-Orientation was created from design techniques used in Hardware designs. Data abstraction is the central aspect of Object-Orientation which incidentally, is found in everyday hardware designs through the use of publicly visible “ports” and private “internal signals”. Furthermore, the principle of “composition” used in C++ for creating hierarchical design is almost identical to component instantiation found in hardware designs. The coming sections will introduce the basics of C++ by looking at the creation of an hardware component.

The Class

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++ ; }

int do_read() { return 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; commonly, a class declaration will also contain specialised 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 mean for setting a default value to any data member.
A destructor is used to perform clean-up operations before an instance of the ADT become out of scope. Pragmatically, the destructor is used for closing previously opened files or de-allocating dynamically allocated memory.
An example of an ADT with constructors and a destructor is as follows:

class counter

{

int value;

public:

void do_reset() { value = 0 ; }

void do_count_up() { value++ ; }

int do_read() { return value; }

counter() {

cout << “This a simple constructor” << endl;

value = 10;

}

counter(int arg): value(arg) {

cout << “This a more interesting constructor” << endl;

}

~counter() {

cout << “Destroying a counter object” << endl;}

};

 

In our example all access functions are made visible to the outside world through the use of the “public” keyword. However, not all functions have to be made public; they can be held hidden from the rest of the world through the use of the “private” keyword.

The Object

An object is an instance of an ADT. Any number of instances can be made of a given ADT; each of these instances are initialised individually.
An example of objects instantiation and messages passing is as follows:

void main() {

 

counter first_counter;

counter second_counter(55);

 

// Message passing

first_counter.do_reset();

for (int i=0; i < 10; i++) { first_counter.do_count_up(); }

second_counter.do_count_up();

 

first_counter.do_reset();

second_counter.do_reset();

}

 

In this example two instances of the counter ADT are created. The “first_counter” instance uses the default constructor not requiring any arguments. The “second_counter” instance uses a more sophisticated constructor form that passes an argument.

Messages are send to individual objects via the ‘dot’ notation which is similar to what is used for “struct” data structures.

 

The building of more complex objects can be achieved through the composition mechanism. This mechanism is akin to component instantiation in Hardware design.

The following example illustrates the principle of hierarchical design through composition.

 

class modulo_counter

{

counter internal_counter;

int terminal_count;

public:

void do_reset() { internal_counter.do_reset(); }

void do_count_up() {

if ( internal_counter.do_read() < terminal_count ) {

internal_counter.do_count_up();

} else { internal_counter.do_reset(); }

}

int do_read() { return internal_counter.do_read(); }

modulo_counter(int tc):

terminal_count(tc),

internal_counter(0) {

cout << “A new modulo_counter instance” << endl; }

};

 

In this example a new ADT “modulo_counter” is created form an instance of a counter ADT called “internal_counter”. An additional data member “terminal_count” was added to provide the modulo value for this new ADT. The access function “do_count_up” is customised to take in considerations of the modulo counter specifications. As it can be observed, most actions are delegated to the “internal_counter” instance via message passing. It is interesting to notice how the constructor function is used to initialise both the “terminal_count” value and the instantiated object “internal_counter”. The argument “tc” is used to set the value of “terminal_count” and the value “0” is passed to the constructor of the “internal_counter” object.

Inheritance

Along with composition, C++ provides a sophisticated mechanism for reusing code called: inheritance. With inheritance designers are able to create new ADTs from existing ones by accessing all public elements of a parent class into a child class. This principle can be more appropriate than composition since it usually requires less effort to achieve the same goal. However inheritance does not replace composition but complements it. The following example illustrates the creation of a modulo counter ADT from an existing counter ADT whilst using inheritance.

class modulo_counter : public counter {

int terminal_count;

public:

void do_count_up() {

if ( do_read() < terminal_count ) {

counter::do_count_up(); }

else { do_reset(); }

}

modulo_counter(int tc): terminal_count(tc), counter(0) {

cout << “A new modulo_counter instance” << endl; }

};

 

In this example a new access function called “do_count_up” is created. This overrides the inherited function from the parent class “counter”. The remaining public access functions (do_reset, do_read) found in the parent class are now available to the child class: “modulo_counter”. As it can be observed the creation of the “modulo_counter” class is greatly simplified through the use of inheritance. Nevertheless, the “do_read” and “counter::do_count_up” functions had to be used in the newly created “do_count_up” function to access the private data members inside the parent class. Although this is a valid solution, C++ offers a more pragmatic alternative, consisting in using a protected encapsulation in place of a private one. Unlike private members, protected members are inherited along with public ones when a parent class is derived, however, protected data members remain hidden from the outside world in the same way as private members do. A new implementation of the counter and modulo_counter class using protected data members is as follows:

class counter

{

protected:

int value;

public:

void do_reset() { value = 0 ; }

void do_count_up() { value++ ; }

int do_read() { return value; }

counter() {

cout << “This a simple constructor” << endl;

value = 10;

}

counter(int arg): value(arg) {

cout << “This a more interesting constructor” << endl;

}

~counter() { cout << “Destroying a counter object”

<< endl;}

};

 

class modulo_counter : public counter {

protected:

int terminal_count;

public:

void do_count_up() {

if ( value < terminal_count ) { value++; }

else { value = 0; }

}

modulo_counter(int tc): terminal_count(tc), counter(0) {

cout << “A new modulo_counter instance” << endl; }

};

 

In this last example the data member “value” is directly accessed from within the “modulo_counter” class. The use of the protected encapsulation has simplified the creation of the derived class by making data members from the parent class accessible.
The principle of single inheritance can be extended to allow a child class to be built from multiple parents. This principle is referred as multiple inheritance. Multiple inheritance enables designers to rapidly create new sophisticated classes from a multitude of existing ADTs. The use of multiple inheritance commonly requires careful planning since it can create numerous undesirable side effects. An example of multiple inheritance is as follows:

class reg {

protected:

int value;

public:

void do_reset() { value = 0; }

int do_read() { return value; }

void do_write(int arg) { value = arg; }

reg(): value(0) {}

};

 

class up_counter: virtual public reg {

public:

void do_count_up() { value++; }

};

 

class down_counter: virtual public reg {

public:

void do_count_down() { value–; }

};

 

class up_down_counter: public up_counter, public down_counter { };

 

In this example the “up_down_counter” ADT is created from two parent classes: “up_counter” and “down_counter”. The “up_down_counter” does not require any code since it inherits all of its implementation from its parent classes. It is important to point out that both the “up_counter” and “down_conter” are inheriting virtually the register class. The virtual inheritance is used here to prevent multiple declarations of the “value”, “do_reset”, “do_read”, “do_write” inside the “up_down_counter” since this ADT inherits those attributes twice, though both the “up_counter” and “down_counter” inheritance.

Template Class

The template mechanism is used for creating more versatile ADTs. Templates can be used for variable types or values. An example of a simple template class is as follows:

template<int min, int max> class barrel_counter {

private:

int value;

public:

void do_reset() { value = min; }

void do_count_up() {

if (value< max ) { value++; }

else {value = min; }

}

int do_read() { return value; }

barrel_counter(): value(min) { }

};

 

 

int main(int argc, char *argv[])

{

barrel_counter<10, 50> first_counter;

barrel_counter<0, 10> second_counter;

 

for (int i=0; i < 60; i++) {

first_counter.do_count_up();

second_counter.do_count_up();

cout << first_counter.do_read() << endl;

cout << second_counter.do_read() << endl;

}

return 0;

}

 

This counter implementation defines two templates “min” and “max”; these variables are then used inside the class to define the boundaries of the barrel counter. This example uses templates to set variable values; alternatively templates can be used to set a variable type. An example of templates used for variable types is as follows:

template<class T> class adder {

private:

T result;

public:

T add( T a, T b ) {

result = a + b ;

return result ;

}

};

 

int main(int argc, char *argv[]) {

adder<int> integer_adder;

adder<float> float_adder;

 

int int_result = integer_adder.add(3, 5);

float float_result = float_adder.add(6.7, 10.2);

cout << int_result << endl;

cout <<float_result << endl;

return 0;

}

 

This example uses a variable type “T” used throughout the class to provide a versatile ADT implementation. At a later stage two objects of type adder are declared however, both adders use different types for its operations.

Summary

As we illustrated in this section, Hardware components can be modeled in C++ and to some extent the mechanisms used are similar to those used in HDLs. Additionally C++ provides inheritance as a way to complement the composition mechanism and promotes design reuse.
Nevertheless, C++ does not provide for concurrency which is an essential aspect of systems modeling. Furthermore, timing and propagation delays cannot easily expressed in C++. The SystemC library provides additional mechanisms such as processes and dedicated data types to tackle C++ modeling deficiencies.

The pdf file and the source files for this tutorial can be found HERE.

Et Voila !

David Cabanis

Advertisements
Categories: SytemC Tutorials
  1. January 9, 2009 at 5:13 pm

    Mon cher David!

    Superb exposition! I hear the comforting
    ring of clear thinking. I have just
    yesterday begun my SystemC studies, with
    a bet-the-farm project around my serdes
    invention. Thank you millefois for your
    postings. They are my first food.

    A sharp concern in my proof-of-concept is
    whether I can implement differential
    comparators at physical ports of the FPGA.
    I have registered for the AMS portion of
    the 1666 group in hopes of getting a
    definitive answer. I would much rather have
    the diff comps on the same chip, since this
    project has do demonstrate state-of-the-art
    speed, combining phase modulation and
    waggledance modulation for 12 bits per
    cycle over 4 wires. I’m shooting for
    1.2 Terabyte/second, feeding 32 channels
    of 40 GbE through a 4-wire concentrator.

    Best regards,
    DZ

  2. January 9, 2009 at 5:19 pm

    Ooops! I mean Terabit/sec, not TByte/sec…
    DZ

  3. James Bower
    September 6, 2009 at 11:29 pm

    Great blog, reading it through RSS feed as well

  4. September 6, 2009 at 11:30 pm

    Sorry for the double post, just wanted to let you know that i think something is wrong with the RSS feed of your blog and you might want to check it out, thanks

  5. June 10, 2010 at 6:08 am

    nice post, thanks 🙂

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: