dol: initial dol commit
[jump.git] / dol / src / dol / visitor / systemC / 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.h>
44
45 class write_if : virtual public sc_interface
46 {
47    public:
48      virtual void write(char) = 0;
49      virtual void reset() = 0;
50      virtual int wtest(int) = 0;
51 };
52
53 class read_if : virtual public sc_interface
54 {
55    public:
56      virtual void read(char &) = 0;
57      virtual int num_available() = 0;
58      virtual int rtest(int) = 0;
59 };
60
61 class fifo : public sc_channel, public write_if, public read_if
62 {
63    public:
64      fifo(sc_module_name name, int size)
65          : sc_channel(name), num_elements(0), first(0), max(size) {
66          data = new char[size];
67      }
68
69      void write(char c) {
70        if (num_elements == max)
71          wait(read_event);
72
73        data[(first + num_elements) % max] = c;
74        ++ num_elements;
75        write_event.notify();
76      }
77
78      void read(char &c){
79        if (num_elements == 0)
80          wait(write_event);
81
82        c = data[first];
83        -- num_elements;
84        first = (first + 1) % max;
85        read_event.notify();
86      }
87
88      int rtest(int size) { return (size <= num_elements) ? 1 : 0; }
89      int wtest(int size) { return (size <= max - num_elements) ? 1 : 0; }
90
91     
92      void reset() { num_elements = first = 0; }
93
94      int num_available() { return num_elements;}
95
96    private:
97      int max;
98      char *data;
99      int num_elements, first;
100      sc_event write_event, read_event;
101 };
102
103
104 #endif
105
106 /*
107 class producer : public sc_module
108 {
109    public:
110      sc_port<write_if> out;
111
112      SC_HAS_PROCESS(producer);
113
114      producer(sc_module_name name) : sc_module(name)
115      {
116        SC_THREAD(main);
117      }
118
119      void main()
120      {
121        const char *str =
122          "Visit www.systemc.org and see what SystemC can do for you today!\n";
123
124        while (*str)
125          out->write(*str++);
126      }
127 };
128
129 class consumer : public sc_module
130 {
131    public:
132      sc_port<read_if> in;
133
134      SC_HAS_PROCESS(consumer);
135
136      consumer(sc_module_name name) : sc_module(name)
137      {
138        SC_THREAD(main);
139      }
140
141      void main()
142      {
143        char c;
144        cout << endl << endl;
145
146        while (true) {
147          in->read(c);
148          cout << c << flush;
149
150          if (in->num_available() == 1)
151            cout << "<1>" << flush;
152          if (in->num_available() == 9)
153            cout << "<9>" << flush;
154        }
155      }
156 };
157
158 class top : public sc_module
159 {
160    public:
161      fifo *fifo_inst;
162      producer *prod_inst;
163      consumer *cons_inst;
164
165      top(sc_module_name name) : sc_module(name)
166      {
167        fifo_inst = new fifo("Fifo1");
168
169        prod_inst = new producer("Producer1");
170        prod_inst->out(*fifo_inst);
171
172        cons_inst = new consumer("Consumer1");
173        cons_inst->in(*fifo_inst);
174      }
175 };
176
177 int sc_main (int argc , char *argv[]) {
178    top top1("Top1");
179    sc_start(-1);
180    return 0;
181 }
182 */