1 /*****************************************************************************
3 The following code is derived, directly or indirectly, from the SystemC
4 source code Copyright (c) 1996-2004 by all Contributors.
7 The contents of this file are subject to the restrictions and limitations
8 set forth in the SystemC Open Source License Version 2.4 (the "License");
9 You may not use this file except in compliance with such restrictions and
10 limitations. You may obtain instructions on how to receive a copy of the
11 License at http://www.systemc.org/. Software distributed by Contributors
12 under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF
13 ANY KIND, either express or implied. See the License for the specific
14 language governing rights and limitations under the License.
16 *****************************************************************************/
18 /*****************************************************************************
20 simple_fifo.cpp -- Simple SystemC 2.0 producer/consumer example.
22 From "An Introduction to System Level Modeling in
23 SystemC 2.0". By Stuart Swan, Cadence Design Systems.
24 Available at www.systemc.org
26 Original Author: Stuart Swan, Cadence Design Systems, 2001-06-18
28 *****************************************************************************/
30 /*****************************************************************************
32 MODIFICATION LOG - modifiers, enter your name, affiliation, date and
33 changes you are making here.
35 Name, Affiliation, Date:
36 Description of Modification:
38 *****************************************************************************/
40 #ifndef _SIMPLE_FIFO_H_
41 #define _SIMPLE_FIFO_H_
45 class write_if : virtual public sc_interface
48 virtual void write(char) = 0;
49 virtual void reset() = 0;
50 virtual int wtest(int) = 0;
53 class read_if : virtual public sc_interface
56 virtual void read(char &) = 0;
57 virtual int num_available() = 0;
58 virtual int rtest(int) = 0;
61 class fifo : public sc_channel, public write_if, public read_if
64 fifo(sc_module_name name, int size)
65 : sc_channel(name), num_elements(0), first(0), max(size) {
66 data = new char[size];
70 if (num_elements == max)
73 data[(first + num_elements) % max] = c;
79 if (num_elements == 0)
84 first = (first + 1) % max;
88 int rtest(int size) { return (size <= num_elements) ? 1 : 0; }
89 int wtest(int size) { return (size <= max - num_elements) ? 1 : 0; }
92 void reset() { num_elements = first = 0; }
94 int num_available() { return num_elements;}
99 int num_elements, first;
100 sc_event write_event, read_event;
107 class producer : public sc_module
110 sc_port<write_if> out;
112 SC_HAS_PROCESS(producer);
114 producer(sc_module_name name) : sc_module(name)
122 "Visit www.systemc.org and see what SystemC can do for you today!\n";
129 class consumer : public sc_module
134 SC_HAS_PROCESS(consumer);
136 consumer(sc_module_name name) : sc_module(name)
144 cout << endl << endl;
150 if (in->num_available() == 1)
151 cout << "<1>" << flush;
152 if (in->num_available() == 9)
153 cout << "<9>" << flush;
158 class top : public sc_module
165 top(sc_module_name name) : sc_module(name)
167 fifo_inst = new fifo("Fifo1");
169 prod_inst = new producer("Producer1");
170 prod_inst->out(*fifo_inst);
172 cons_inst = new consumer("Consumer1");
173 cons_inst->in(*fifo_inst);
177 int sc_main (int argc , char *argv[]) {