sc_fifo.hh (12853:e23d6f09069a) sc_fifo.hh (13123:c86a8a2bc851)
1/*
2 * Copyright 2018 Google, Inc.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met: redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer;
8 * redistributions in binary form must reproduce the above copyright

--- 16 unchanged lines hidden (view full) ---

25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *
27 * Authors: Gabe Black
28 */
29
30#ifndef __SYSTEMC_EXT_CHANNEL_SC_FIFO_HH__
31#define __SYSTEMC_EXT_CHANNEL_SC_FIFO_HH__
32
1/*
2 * Copyright 2018 Google, Inc.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met: redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer;
8 * redistributions in binary form must reproduce the above copyright

--- 16 unchanged lines hidden (view full) ---

25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *
27 * Authors: Gabe Black
28 */
29
30#ifndef __SYSTEMC_EXT_CHANNEL_SC_FIFO_HH__
31#define __SYSTEMC_EXT_CHANNEL_SC_FIFO_HH__
32
33#include <list>
34
33#include "../core/sc_module.hh" // for sc_gen_unique_name
34#include "../core/sc_prim.hh"
35#include "sc_fifo_in_if.hh"
36#include "sc_fifo_out_if.hh"
37#include "warn_unimpl.hh"
38
39namespace sc_core
40{

--- 4 unchanged lines hidden (view full) ---

45template <class T>
46class sc_fifo : public sc_fifo_in_if<T>,
47 public sc_fifo_out_if<T>,
48 public sc_prim_channel
49{
50 public:
51 explicit sc_fifo(int size=16) :
52 sc_fifo_in_if<T>(), sc_fifo_out_if<T>(),
35#include "../core/sc_module.hh" // for sc_gen_unique_name
36#include "../core/sc_prim.hh"
37#include "sc_fifo_in_if.hh"
38#include "sc_fifo_out_if.hh"
39#include "warn_unimpl.hh"
40
41namespace sc_core
42{

--- 4 unchanged lines hidden (view full) ---

47template <class T>
48class sc_fifo : public sc_fifo_in_if<T>,
49 public sc_fifo_out_if<T>,
50 public sc_prim_channel
51{
52 public:
53 explicit sc_fifo(int size=16) :
54 sc_fifo_in_if<T>(), sc_fifo_out_if<T>(),
53 sc_prim_channel(sc_gen_unique_name("fifo"))
55 sc_prim_channel(sc_gen_unique_name("fifo")),
56 _size(size), _readsHappened(false)
54 {}
55 explicit sc_fifo(const char *name, int size=16) :
56 sc_fifo_in_if<T>(), sc_fifo_out_if<T>(),
57 {}
58 explicit sc_fifo(const char *name, int size=16) :
59 sc_fifo_in_if<T>(), sc_fifo_out_if<T>(),
57 sc_prim_channel(name)
60 sc_prim_channel(name), _size(size), _readsHappened(false)
58 {}
59 virtual ~sc_fifo() {}
60
61 virtual void
62 register_port(sc_port_base &, const char *)
63 {
64 sc_channel_warn_unimpl(__PRETTY_FUNCTION__);
65 }
66
67 virtual void
61 {}
62 virtual ~sc_fifo() {}
63
64 virtual void
65 register_port(sc_port_base &, const char *)
66 {
67 sc_channel_warn_unimpl(__PRETTY_FUNCTION__);
68 }
69
70 virtual void
68 read(T &)
71 read(T &t)
69 {
72 {
70 sc_channel_warn_unimpl(__PRETTY_FUNCTION__);
73 while (num_available() == 0)
74 sc_core::wait(_dataWriteEvent);
75 _readsHappened = true;
76 t = _entries.front();
77 _entries.pop_front();
78 request_update();
71 }
72 virtual T
73 read()
74 {
79 }
80 virtual T
81 read()
82 {
75 sc_channel_warn_unimpl(__PRETTY_FUNCTION__);
76 return *(T *)nullptr;
83 T t;
84 read(t);
85 return t;
77 }
78 virtual bool
86 }
87 virtual bool
79 nb_read(T &)
88 nb_read(T &t)
80 {
89 {
81 sc_channel_warn_unimpl(__PRETTY_FUNCTION__);
82 return false;
90 if (num_available()) {
91 read(t);
92 return true;
93 } else {
94 return false;
95 }
83 }
96 }
84 operator T()
85 {
86 sc_channel_warn_unimpl(__PRETTY_FUNCTION__);
87 return *(T *)nullptr;
88 }
97 operator T() { return read(); }
89
90 virtual void
98
99 virtual void
91 write(const T &)
100 write(const T &t)
92 {
101 {
93 sc_channel_warn_unimpl(__PRETTY_FUNCTION__);
102 while (num_free() == 0)
103 sc_core::wait(_dataReadEvent);
104 _pending.emplace_back(t);
105 request_update();
94 }
95 virtual bool
106 }
107 virtual bool
96 nb_write(const T&)
108 nb_write(const T &t)
97 {
109 {
98 sc_channel_warn_unimpl(__PRETTY_FUNCTION__);
99 return false;
110 if (num_free()) {
111 write(t);
112 return true;
113 } else {
114 return false;
115 }
100 }
101 sc_fifo<T> &
116 }
117 sc_fifo<T> &
102 operator = (const T &)
118 operator = (const T &t)
103 {
119 {
104 sc_channel_warn_unimpl(__PRETTY_FUNCTION__);
120 write(t);
105 return *this;
106 }
107
108 virtual const sc_event &
109 data_written_event() const
110 {
121 return *this;
122 }
123
124 virtual const sc_event &
125 data_written_event() const
126 {
111 sc_channel_warn_unimpl(__PRETTY_FUNCTION__);
112 return *(const sc_event *)nullptr;
127 return _dataWriteEvent;
113 }
114 virtual const sc_event &
115 data_read_event() const
116 {
128 }
129 virtual const sc_event &
130 data_read_event() const
131 {
117 sc_channel_warn_unimpl(__PRETTY_FUNCTION__);
118 return *(const sc_event *)nullptr;
132 return _dataReadEvent;
119 }
120
133 }
134
135 virtual int num_available() const { return _entries.size(); }
121 virtual int
136 virtual int
122 num_available() const
123 {
124 sc_channel_warn_unimpl(__PRETTY_FUNCTION__);
125 return 0;
126 }
127 virtual int
128 num_free() const
129 {
137 num_free() const
138 {
130 sc_channel_warn_unimpl(__PRETTY_FUNCTION__);
131 return 0;
139 return _size - _entries.size() - _pending.size();
132 }
133
134 virtual void
140 }
141
142 virtual void
135 print(std::ostream & =std::cout) const
143 print(std::ostream &os=std::cout) const
136 {
144 {
137 sc_channel_warn_unimpl(__PRETTY_FUNCTION__);
145 for (typename ::std::list<T>::iterator pos = _entries.begin();
146 pos != _entries.end(); pos++) {
147 os << *pos << ::std::endl;
148 }
138 }
139 virtual void
140 dump(std::ostream & =std::cout) const
141 {
142 sc_channel_warn_unimpl(__PRETTY_FUNCTION__);
143 }
144 virtual const char *kind() const { return "sc_fifo"; }
145
146 protected:
147 virtual void
148 update()
149 {
149 }
150 virtual void
151 dump(std::ostream & =std::cout) const
152 {
153 sc_channel_warn_unimpl(__PRETTY_FUNCTION__);
154 }
155 virtual const char *kind() const { return "sc_fifo"; }
156
157 protected:
158 virtual void
159 update()
160 {
150 sc_channel_warn_unimpl(__PRETTY_FUNCTION__);
161 if (!_pending.empty()) {
162 _dataWriteEvent.notify(SC_ZERO_TIME);
163 _entries.insert(_entries.end(), _pending.begin(), _pending.end());
164 _pending.clear();
165 }
166 if (_readsHappened) {
167 _readsHappened = false;
168 _dataReadEvent.notify(SC_ZERO_TIME);
169 }
151 }
152
153 private:
154 // Disabled
155 sc_fifo(const sc_fifo<T> &) :
156 sc_fifo_in_if<T>(), sc_fifo_in_if<T>(), sc_prim_channel()
157 {}
158 sc_fifo &operator = (const sc_fifo<T> &) { return *this; }
170 }
171
172 private:
173 // Disabled
174 sc_fifo(const sc_fifo<T> &) :
175 sc_fifo_in_if<T>(), sc_fifo_in_if<T>(), sc_prim_channel()
176 {}
177 sc_fifo &operator = (const sc_fifo<T> &) { return *this; }
178
179 sc_event _dataReadEvent;
180 sc_event _dataWriteEvent;
181
182 int _size;
183 mutable std::list<T> _entries;
184 mutable std::list<T> _pending;
185 bool _readsHappened;
159};
160
161template <class T>
162inline std::ostream &
163operator << (std::ostream &os, const sc_fifo<T> &)
164{
165 sc_channel_warn_unimpl(__PRETTY_FUNCTION__);
166 return os;
167}
168
169} // namespace sc_core
170
171#endif //__SYSTEMC_EXT_CHANNEL_SC_FIFO_HH__
186};
187
188template <class T>
189inline std::ostream &
190operator << (std::ostream &os, const sc_fifo<T> &)
191{
192 sc_channel_warn_unimpl(__PRETTY_FUNCTION__);
193 return os;
194}
195
196} // namespace sc_core
197
198#endif //__SYSTEMC_EXT_CHANNEL_SC_FIFO_HH__