Channels ▼


SystemC: Hardware-Oriented Constructs in C++

January, 2005: SystemC: Hardware-Oriented Constructs in C++

Listing 1

  The following code is derived, directly or indirectly, from the SystemC
  source code Copyright (c) 1996-2002 by all Contributors.
  All Rights reserved.
  The contents of this file are subject to the restrictions and limitations
  set forth in the SystemC Open Source License Version 2.3 (the "License");
  You may not use this file except in compliance with such restrictions and
  limitations. You may obtain instructions on how to receive a copy of the
  License at Software distributed by Contributors
  under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF
  ANY KIND, either express or implied. See the License for the specific
  language governing rights and limitations under the License.

simple_fifo.cpp -- Simple SystemC 2.0 producer/consumer example.

                     From "An Introduction to System Level Modeling in
                     SystemC 2.0". By Stuart Swan, Cadence Design Systems.
                     Available at

  Original Author: Stuart Swan, Cadence Design Systems, 2001-06-18

#include <systemc.h>

class write_if : virtual public sc_interface
     virtual void write(char) = 0;
     virtual void reset() = 0;
class read_if : virtual public sc_interface
     virtual void read(char &) = 0;
     virtual int num_available() = 0;
class fifo : public sc_channel, public write_if, public read_if
    fifo(sc_module_name name) : sc_channel(name), num_elements(0), first(0) {}
     void write(char c) {
       if (num_elements == max)
       data[(first + num_elements) % max] = c;
       ++ num_elements;
     void read(char &c){
       if (num_elements == 0)
       c = data[first];
       -- num_elements;
       first = (first + 1) % max;
     void reset() { num_elements = first = 0; }
     int num_available() { return num_elements;}
     enum e { max = 10 };
     char data[max];
     int num_elements, first;
     sc_event write_event, read_event;
class producer : public sc_module
     sc_port<write_if> out;
     producer(sc_module_name name) : sc_module(name)
     void main()
       const char *str =
         "Visit and see what SystemC can do for you 

       while (*str)
class consumer : public sc_module
     sc_port<read_if> in;
     consumer(sc_module_name name) : sc_module(name)
     void main()
       char c;
       cout << endl << endl;
       while (true) {
         cout << c << flush;
         if (in->num_available() == 1)
       cout << "<1>" << flush;
         if (in->num_available() == 9)
       cout << "<9>" << flush;
class top : public sc_module
     fifo *fifo_inst;
     producer *prod_inst;
     consumer *cons_inst;
     top(sc_module_name name) : sc_module(name)
       fifo_inst = new fifo("Fifo1");

       prod_inst = new producer("Producer1");

       cons_inst = new consumer("Consumer1");
int sc_main (int argc , char *argv[]) {
   top top1("Top1");
   return 0;

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.