b070b8bd0681a02b1f9db8ce9cb2161a5e152be3
[jump.git] / dol / src / dol / visitor / hdsd / lib / simple_fifo.h
1 /*****************************************************************************
2
3   The following code is derived, directly or indirectly, from the SystemC
4   source code Copyright (c) 1996-2004 by all Contributors.
5   All Rights reserved.
6
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.
15
16  *****************************************************************************/
17
18 /*****************************************************************************
19
20   simple_fifo.cpp -- Simple SystemC 2.0 producer/consumer example.
21
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
25
26   Original Author: Stuart Swan, Cadence Design Systems, 2001-06-18
27
28  *****************************************************************************/
29
30 /*****************************************************************************
31
32   MODIFICATION LOG - modifiers, enter your name, affiliation, date and
33   changes you are making here.
34
35       Name, Affiliation, Date:
36   Description of Modification:
37
38  *****************************************************************************/
39
40 #ifndef _SIMPLE_FIFO_H_
41 #define _SIMPLE_FIFO_H_
42
43 #include <systemc>
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;
48
49 class write_if : virtual public sc_interface
50 {
51    public:
52      virtual void write(char) = 0;
53      virtual void reset() = 0;
54      virtual int wtest(int) = 0;
55 };
56
57 class read_if : virtual public sc_interface
58 {
59    public:
60      virtual void read(char &) = 0;
61      virtual int num_available() = 0;
62      virtual int rtest(int) = 0;
63 };
64
65 class fifo : public sc_channel, public write_if, public read_if
66 {
67    public:
68      fifo(sc_module_name name, int size)
69          : sc_channel(name), num_elements(0), first(0), max(size) {
70          data = new char[size];
71      }
72
73      void write(char c) {
74        if (num_elements == max)
75          wait(read_event);
76
77        data[(first + num_elements) % max] = c;
78        ++ num_elements;
79        write_event.notify();
80      }
81
82      void read(char &c){
83        if (num_elements == 0)
84          wait(write_event);
85
86        c = data[first];
87        -- num_elements;
88        first = (first + 1) % max;
89        read_event.notify();
90      }
91
92      int rtest(int size) { return (size <= num_elements) ? 1 : 0; }
93      int wtest(int size) { return (size <= max - num_elements) ? 1 : 0; }
94
95     
96      void reset() { num_elements = first = 0; }
97
98      int num_available() { return num_elements;}
99
100    private:
101      int max;
102      char *data;
103      int num_elements, first;
104      sc_event write_event, read_event;
105 };
106
107
108 #endif
109
110 /*
111 class producer : public sc_module
112 {
113    public:
114      sc_port<write_if> out;
115
116      SC_HAS_PROCESS(producer);
117
118      producer(sc_module_name name) : sc_module(name)
119      {
120        SC_THREAD(main);
121      }
122
123      void main()
124      {
125        const char *str =
126          "Visit www.systemc.org and see what SystemC can do for you today!\n";
127
128        while (*str)
129          out->write(*str++);
130      }
131 };
132
133 class consumer : public sc_module
134 {
135    public:
136      sc_port<read_if> in;
137
138      SC_HAS_PROCESS(consumer);
139
140      consumer(sc_module_name name) : sc_module(name)
141      {
142        SC_THREAD(main);
143      }
144
145      void main()
146      {
147        char c;
148        cout << endl << endl;
149
150        while (true) {
151          in->read(c);
152          cout << c << flush;
153
154          if (in->num_available() == 1)
155            cout << "<1>" << flush;
156          if (in->num_available() == 9)
157            cout << "<9>" << flush;
158        }
159      }
160 };
161
162 class top : public sc_module
163 {
164    public:
165      fifo *fifo_inst;
166      producer *prod_inst;
167      consumer *cons_inst;
168
169      top(sc_module_name name) : sc_module(name)
170      {
171        fifo_inst = new fifo("Fifo1");
172
173        prod_inst = new producer("Producer1");
174        prod_inst->out(*fifo_inst);
175
176        cons_inst = new consumer("Consumer1");
177        cons_inst->in(*fifo_inst);
178      }
179 };
180
181 int sc_main (int argc , char *argv[]) {
182    top top1("Top1");
183    sc_start(-1);
184    return 0;
185 }
186 */