1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
|
/*****************************************************************************
Licensed to Accellera Systems Initiative Inc. (Accellera) under one or
more contributor license agreements. See the NOTICE file distributed
with this work for additional information regarding copyright ownership.
Accellera licenses this file to you under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with the
License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied. See the License for the specific language governing
permissions and limitations under the License.
*****************************************************************************/
/*****************************************************************************
sc_fifo.h -- The sc_fifo<T> primitive channel class.
Original Author: Martin Janssen, Synopsys, Inc., 2001-05-21
CHANGE LOG IS AT THE END OF THE FILE
*****************************************************************************/
#ifndef SC_FIFO_H
#define SC_FIFO_H
#include "sysc/communication/sc_communication_ids.h"
#include "sysc/communication/sc_prim_channel.h"
#include "sysc/communication/sc_fifo_ifs.h"
#include "sysc/kernel/sc_event.h"
#include "sysc/kernel/sc_simcontext.h"
#include "sysc/tracing/sc_trace.h"
#include <typeinfo>
namespace sc_core {
// ----------------------------------------------------------------------------
// CLASS : sc_fifo<T>
//
// The sc_fifo<T> primitive channel class.
// ----------------------------------------------------------------------------
template <class T>
class sc_fifo
: public sc_fifo_in_if<T>,
public sc_fifo_out_if<T>,
public sc_prim_channel
{
public:
// constructors
explicit sc_fifo( int size_ = 16 )
: sc_prim_channel( sc_gen_unique_name( "fifo" ) ),
m_data_read_event(
(std::string(SC_KERNEL_EVENT_PREFIX)+"_read_event").c_str()),
m_data_written_event(
(std::string(SC_KERNEL_EVENT_PREFIX)+"_write_event").c_str())
{ init( size_ ); }
explicit sc_fifo( const char* name_, int size_ = 16 )
: sc_prim_channel( name_ ),
m_data_read_event(
(std::string(SC_KERNEL_EVENT_PREFIX)+"_read_event").c_str()),
m_data_written_event(
(std::string(SC_KERNEL_EVENT_PREFIX)+"_write_event").c_str())
{ init( size_ ); }
// destructor
virtual ~sc_fifo()
{ delete [] m_buf; }
// interface methods
virtual void register_port( sc_port_base&, const char* );
// blocking read
virtual void read( T& );
virtual T read();
// non-blocking read
virtual bool nb_read( T& );
// get the number of available samples
virtual int num_available() const
{ return ( m_num_readable - m_num_read ); }
// get the data written event
virtual const sc_event& data_written_event() const
{ return m_data_written_event; }
// blocking write
virtual void write( const T& );
// non-blocking write
virtual bool nb_write( const T& );
// get the number of free spaces
virtual int num_free() const
{ return ( m_size - m_num_readable - m_num_written ); }
// get the data read event
virtual const sc_event& data_read_event() const
{ return m_data_read_event; }
// other methods
operator T ()
{ return read(); }
sc_fifo<T>& operator = ( const T& a )
{ write( a ); return *this; }
void trace( sc_trace_file* tf ) const;
virtual void print( ::std::ostream& = ::std::cout ) const;
virtual void dump( ::std::ostream& = ::std::cout ) const;
virtual const char* kind() const
{ return "sc_fifo"; }
protected:
virtual void update();
// support methods
void init( int );
void buf_init( int );
bool buf_write( const T& );
bool buf_read( T& );
protected:
int m_size; // size of the buffer
T* m_buf; // the buffer
int m_free; // number of free spaces
int m_ri; // index of next read
int m_wi; // index of next write
sc_port_base* m_reader; // used for static design rule checking
sc_port_base* m_writer; // used for static design rule checking
int m_num_readable; // #samples readable
int m_num_read; // #samples read during this delta cycle
int m_num_written; // #samples written during this delta cycle
sc_event m_data_read_event;
sc_event m_data_written_event;
private:
// disabled
sc_fifo( const sc_fifo<T>& );
sc_fifo& operator = ( const sc_fifo<T>& );
};
// IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
template <class T>
inline
void
sc_fifo<T>::register_port( sc_port_base& port_,
const char* if_typename_ )
{
std::string nm( if_typename_ );
if( nm == typeid( sc_fifo_in_if<T> ).name() ||
nm == typeid( sc_fifo_blocking_in_if<T> ).name()
) {
// only one reader can be connected
if( m_reader != 0 ) {
SC_REPORT_ERROR( SC_ID_MORE_THAN_ONE_FIFO_READER_, 0 );
}
m_reader = &port_;
} else if( nm == typeid( sc_fifo_out_if<T> ).name() ||
nm == typeid( sc_fifo_blocking_out_if<T> ).name()
) {
// only one writer can be connected
if( m_writer != 0 ) {
SC_REPORT_ERROR( SC_ID_MORE_THAN_ONE_FIFO_WRITER_, 0 );
}
m_writer = &port_;
}
else
{
SC_REPORT_ERROR( SC_ID_BIND_IF_TO_PORT_,
"sc_fifo<T> port not recognized" );
}
}
// blocking read
template <class T>
inline
void
sc_fifo<T>::read( T& val_ )
{
while( num_available() == 0 ) {
sc_core::wait( m_data_written_event );
}
m_num_read ++;
buf_read( val_ );
request_update();
}
template <class T>
inline
T
sc_fifo<T>::read()
{
T tmp;
read( tmp );
return tmp;
}
// non-blocking read
template <class T>
inline
bool
sc_fifo<T>::nb_read( T& val_ )
{
if( num_available() == 0 ) {
return false;
}
m_num_read ++;
buf_read( val_ );
request_update();
return true;
}
// blocking write
template <class T>
inline
void
sc_fifo<T>::write( const T& val_ )
{
while( num_free() == 0 ) {
sc_core::wait( m_data_read_event );
}
m_num_written ++;
buf_write( val_ );
request_update();
}
// non-blocking write
template <class T>
inline
bool
sc_fifo<T>::nb_write( const T& val_ )
{
if( num_free() == 0 ) {
return false;
}
m_num_written ++;
buf_write( val_ );
request_update();
return true;
}
template <class T>
inline
void
sc_fifo<T>::trace( sc_trace_file* tf ) const
{
#if defined(DEBUG_SYSTEMC)
char buf[32];
std::string nm = name();
for( int i = 0; i < m_size; ++ i ) {
std::sprintf( buf, "_%d", i );
sc_trace( tf, m_buf[i], nm + buf );
}
#endif
}
template <class T>
inline
void
sc_fifo<T>::print( ::std::ostream& os ) const
{
if( m_free != m_size ) {
int i = m_ri;
do {
os << m_buf[i] << ::std::endl;
i = ( i + 1 ) % m_size;
} while( i != m_wi );
}
}
template <class T>
inline
void
sc_fifo<T>::dump( ::std::ostream& os ) const
{
os << "name = " << name() << ::std::endl;
if( m_free != m_size ) {
int i = m_ri;
int j = 0;
do {
os << "value[" << i << "] = " << m_buf[i] << ::std::endl;
i = ( i + 1 ) % m_size;
j ++;
} while( i != m_wi );
}
}
template <class T>
inline
void
sc_fifo<T>::update()
{
if( m_num_read > 0 ) {
m_data_read_event.notify(SC_ZERO_TIME);
}
if( m_num_written > 0 ) {
m_data_written_event.notify(SC_ZERO_TIME);
}
m_num_readable = m_size - m_free;
m_num_read = 0;
m_num_written = 0;
}
// support methods
template <class T>
inline
void
sc_fifo<T>::init( int size_ )
{
buf_init( size_ );
m_reader = 0;
m_writer = 0;
m_num_readable = 0;
m_num_read = 0;
m_num_written = 0;
}
template <class T>
inline
void
sc_fifo<T>::buf_init( int size_ )
{
if( size_ <= 0 ) {
SC_REPORT_ERROR( SC_ID_INVALID_FIFO_SIZE_, 0 );
}
m_size = size_;
m_buf = new T[m_size];
m_free = m_size;
m_ri = 0;
m_wi = 0;
}
template <class T>
inline
bool
sc_fifo<T>::buf_write( const T& val_ )
{
if( m_free == 0 ) {
return false;
}
m_buf[m_wi] = val_;
m_wi = ( m_wi + 1 ) % m_size;
m_free --;
return true;
}
template <class T>
inline
bool
sc_fifo<T>::buf_read( T& val_ )
{
if( m_free == m_size ) {
return false;
}
val_ = m_buf[m_ri];
m_buf[m_ri] = T(); // clear entry for boost::shared_ptr, et al.
m_ri = ( m_ri + 1 ) % m_size;
m_free ++;
return true;
}
// ----------------------------------------------------------------------------
template <class T>
inline
::std::ostream&
operator << ( ::std::ostream& os, const sc_fifo<T>& a )
{
a.print( os );
return os;
}
} // namespace sc_core
//$Log: sc_fifo.h,v $
//Revision 1.6 2011/08/26 20:45:40 acg
// Andy Goodrich: moved the modification log to the end of the file to
// eliminate source line number skew when check-ins are done.
//
//Revision 1.5 2011/03/23 16:17:22 acg
// Andy Goodrich: hide the sc_events that are kernel related.
//
//Revision 1.4 2011/02/18 20:23:45 acg
// Andy Goodrich: Copyright update.
//
//Revision 1.3 2009/10/14 19:05:40 acg
// Andy Goodrich: added check for blocking interfaces in addition to the
// combined blocking/nonblocking interface.
//
//Revision 1.2 2009/05/22 16:06:24 acg
// Andy Goodrich: process control updates.
//
//Revision 1.1.1.1 2006/12/15 20:20:04 acg
//SystemC 2.3
//
//Revision 1.4 2006/01/24 20:46:31 acg
//Andy Goodrich: changes to eliminate use of deprecated features. For instance,
//using notify(SC_ZERO_TIME) in place of notify_delayed().
//
//Revision 1.3 2006/01/13 20:41:59 acg
//Andy Goodrich: Changes to add port registration to the things that are
//checked when SC_NO_WRITE_CHECK is not defined.
//
//Revision 1.2 2006/01/03 23:18:26 acg
//Changed copyright to include 2006.
//
//Revision 1.1.1.1 2005/12/19 23:16:43 acg
//First check in of SystemC 2.1 into its own archive.
//
//Revision 1.12 2005/09/15 23:01:51 acg
//Added std:: prefix to appropriate methods and types to get around
//issues with the Edison Front End.
//
//Revision 1.11 2005/06/10 22:43:55 acg
//Added CVS change log annotation.
//
#endif
// Taf!
|