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_
44 using sc_core::sc_interface;
45 using sc_core::sc_channel;
46 using sc_core::sc_event;
47 using sc_core::sc_module_name;
49 class write_if : virtual public sc_interface
52 virtual void write(char) = 0;
53 virtual void reset() = 0;
54 virtual int wtest(int) = 0;
57 class read_if : virtual public sc_interface
60 virtual void read(char &) = 0;
61 virtual int num_available() = 0;
62 virtual int rtest(int) = 0;
65 class fifo : public sc_channel, public write_if, public read_if
68 fifo(sc_module_name name, int size)
69 : sc_channel(name), num_elements(0), first(0), max(size) {
70 data = new char[size];
74 if (num_elements == max)
77 data[(first + num_elements) % max] = c;
83 if (num_elements == 0)
88 first = (first + 1) % max;
92 int rtest(int size) { return (size <= num_elements) ? 1 : 0; }
93 int wtest(int size) { return (size <= max - num_elements) ? 1 : 0; }
96 void reset() { num_elements = first = 0; }
98 int num_available() { return num_elements;}
103 int num_elements, first;
104 sc_event write_event, read_event;
111 class producer : public sc_module
114 sc_port<write_if> out;
116 SC_HAS_PROCESS(producer);
118 producer(sc_module_name name) : sc_module(name)
126 "Visit www.systemc.org and see what SystemC can do for you today!\n";
133 class consumer : public sc_module
138 SC_HAS_PROCESS(consumer);
140 consumer(sc_module_name name) : sc_module(name)
148 cout << endl << endl;
154 if (in->num_available() == 1)
155 cout << "<1>" << flush;
156 if (in->num_available() == 9)
157 cout << "<9>" << flush;
162 class top : public sc_module
169 top(sc_module_name name) : sc_module(name)
171 fifo_inst = new fifo("Fifo1");
173 prod_inst = new producer("Producer1");
174 prod_inst->out(*fifo_inst);
176 cons_inst = new consumer("Consumer1");
177 cons_inst->in(*fifo_inst);
181 int sc_main (int argc , char *argv[]) {