6 Fifo::Fifo(char* name, unsigned size = 18) {
\r
7 //std::cout << "Create Fifo." << std::endl;
\r
8 //except at the beginning, _head and _tail must never overlap,
\r
9 //otherwise one does not know whether the buffer is full or
\r
10 //empty. to have nevertheless a buffer with the given capacity,
\r
11 //a buffer with one more element is allocated.
\r
13 _buffer = new char[_size];
\r
17 _name = new char[strlen(name) + 1];
\r
18 strcpy(_name, name);
\r
25 //std::cout << "Delete Fifo." << std::endl;
\r
37 //std::cout << "Deleted Fifo." << std::endl;
\r
43 unsigned Fifo::read(void *destination, unsigned len) {
\r
44 char* buffer = (char*)destination;
\r
46 //std::cout << "Try to read " << len << " bytes from Fifo " << _name << "." << std::endl;
\r
49 while (read < len) {
\r
52 unsigned tocopy = (len - read <= _use ? len - read : _use);
\r
54 if ((unsigned)_tail + tocopy < _size) {
\r
55 memcpy(buffer, _buffer + (unsigned)_tail, tocopy);
\r
58 memcpy(buffer, _buffer + (unsigned)_tail, _size - (unsigned)_tail);
\r
59 memcpy(buffer + _size - (unsigned)_tail, _buffer, tocopy - _size + (unsigned)_tail);
\r
64 _tail = (_tail + tocopy) % _size;
\r
66 _readEvent.notify();
\r
69 /*while (read < len) {
\r
72 } else if ((len - read) < used()) {
\r
73 while (read < len) {
\r
74 unsigned tocopy = (len - read + _tail >= _size) ? _size - _tail : len - read;
\r
75 memcpy(buffer, _buffer + _tail, tocopy);
\r
76 _tail = (_tail + tocopy) % _size;
\r
80 _readEvent.notify();
\r
82 *buffer++ = *(_buffer + _tail % _size);
\r
83 _tail = (_tail + 1) % _size;
\r
85 _readEvent.notify();
\r
89 //std::cout << "Read " << read << " bytes from Fifo " << _name << "." << std::endl;
\r
96 unsigned Fifo::write(const void *source, unsigned len) {
\r
97 char* buffer = (char*)source;
\r
99 unsigned head = (_tail + _use) % _size;
\r
100 //std::cout << "Try to write " << len << " bytes to Fifo " << _name << std::endl;
\r
102 while (write < len) {
\r
103 while (unused() < len)
\r
105 unsigned tocopy = (len - write <= unused() ? len - write : unused());
\r
107 if (head + tocopy < _size) {
\r
108 memcpy(_buffer + head, buffer, tocopy);
\r
111 memcpy(_buffer + head, buffer, _size - head);
\r
112 memcpy(_buffer, buffer + _size - head, tocopy - _size + head);
\r
117 head = (head + tocopy) % _size;
\r
119 _writeEvent.notify();
\r
122 /* while (write < len) {
\r
123 if (unused() == 0) {
\r
125 } else if ((len - write) < unused()) {
\r
126 while (write < len) {
\r
127 unsigned tocopy = (len - write + _head >= _size) ? _size - _head : len - write;
\r
128 memcpy(_buffer + _head, buffer, tocopy);
\r
129 _head = (_head + tocopy) % _size;
\r
133 _writeEvent.notify();
\r
135 *(_buffer + (unsigned)(_head) % _size) = *buffer++;
\r
136 _head = (_head + 1) % _size;
\r
138 _writeEvent.notify();
\r
142 //std::cout << "Wrote " << write << " bytes to Fifo " << _name << "." << std::endl;
\r
149 unsigned Fifo::size() const {
\r
156 unsigned Fifo::unused() const {
\r
157 return (_size) - _use;
\r
163 unsigned Fifo::used() const {
\r
170 char* Fifo::getName() const {
\r
175 * Test the implementation
\r
178 class producer : public sc_module
\r
183 SC_HAS_PROCESS(producer);
\r
185 producer(sc_module_name name) : sc_module(name)
\r
193 "Visit www.systemc.org and see what SystemC can do for you today!\n";
\r
196 fifo->write((void*)str++, 4);
\r
201 class consumer : public sc_module
\r
206 SC_HAS_PROCESS(consumer);
\r
208 consumer(sc_module_name name) : sc_module(name)
\r
216 cout << endl << endl;
\r
220 cout << c << flush;
\r
222 if (fifo->used() == 1)
\r
223 cout << "<1>" << flush;
\r
224 if (fifo->used() == 9)
\r
225 cout << "<9>" << flush;
\r
230 class top : public sc_module
\r
234 producer *prod_inst;
\r
235 consumer *cons_inst;
\r
237 top(sc_module_name name) : sc_module(name)
\r
239 fifo_inst = new Fifo("myfifo", 10);
\r
241 prod_inst = new producer("producer");
\r
242 prod_inst->fifo = fifo_inst;
\r
244 cons_inst = new consumer("consumer");
\r
245 cons_inst->fifo = fifo_inst;
\r
249 int sc_main (int argc , char *argv[]) {
\r