sc_fifo.hh (13123:c86a8a2bc851) sc_fifo.hh (13143:87d1097f49b0)
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
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution;
11 * neither the name of the copyright holders nor the names of its
12 * contributors may be used to endorse or promote products derived from
13 * this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
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
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{
43
44class sc_port_base;
45class sc_event;
46
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>(),
55 sc_prim_channel(sc_gen_unique_name("fifo")),
56 _size(size), _readsHappened(false)
57 {}
58 explicit sc_fifo(const char *name, int size=16) :
59 sc_fifo_in_if<T>(), sc_fifo_out_if<T>(),
60 sc_prim_channel(name), _size(size), _readsHappened(false)
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
71 read(T &t)
72 {
73 while (num_available() == 0)
74 sc_core::wait(_dataWriteEvent);
75 _readsHappened = true;
76 t = _entries.front();
77 _entries.pop_front();
78 request_update();
79 }
80 virtual T
81 read()
82 {
83 T t;
84 read(t);
85 return t;
86 }
87 virtual bool
88 nb_read(T &t)
89 {
90 if (num_available()) {
91 read(t);
92 return true;
93 } else {
94 return false;
95 }
96 }
97 operator T() { return read(); }
98
99 virtual void
100 write(const T &t)
101 {
102 while (num_free() == 0)
103 sc_core::wait(_dataReadEvent);
104 _pending.emplace_back(t);
105 request_update();
106 }
107 virtual bool
108 nb_write(const T &t)
109 {
110 if (num_free()) {
111 write(t);
112 return true;
113 } else {
114 return false;
115 }
116 }
117 sc_fifo<T> &
118 operator = (const T &t)
119 {
120 write(t);
121 return *this;
122 }
123
124 virtual const sc_event &
125 data_written_event() const
126 {
127 return _dataWriteEvent;
128 }
129 virtual const sc_event &
130 data_read_event() const
131 {
132 return _dataReadEvent;
133 }
134
135 virtual int num_available() const { return _entries.size(); }
136 virtual int
137 num_free() const
138 {
139 return _size - _entries.size() - _pending.size();
140 }
141
142 virtual void
143 print(std::ostream &os=std::cout) const
144 {
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
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution;
11 * neither the name of the copyright holders nor the names of its
12 * contributors may be used to endorse or promote products derived from
13 * this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
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
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{
43
44class sc_port_base;
45class sc_event;
46
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>(),
55 sc_prim_channel(sc_gen_unique_name("fifo")),
56 _size(size), _readsHappened(false)
57 {}
58 explicit sc_fifo(const char *name, int size=16) :
59 sc_fifo_in_if<T>(), sc_fifo_out_if<T>(),
60 sc_prim_channel(name), _size(size), _readsHappened(false)
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
71 read(T &t)
72 {
73 while (num_available() == 0)
74 sc_core::wait(_dataWriteEvent);
75 _readsHappened = true;
76 t = _entries.front();
77 _entries.pop_front();
78 request_update();
79 }
80 virtual T
81 read()
82 {
83 T t;
84 read(t);
85 return t;
86 }
87 virtual bool
88 nb_read(T &t)
89 {
90 if (num_available()) {
91 read(t);
92 return true;
93 } else {
94 return false;
95 }
96 }
97 operator T() { return read(); }
98
99 virtual void
100 write(const T &t)
101 {
102 while (num_free() == 0)
103 sc_core::wait(_dataReadEvent);
104 _pending.emplace_back(t);
105 request_update();
106 }
107 virtual bool
108 nb_write(const T &t)
109 {
110 if (num_free()) {
111 write(t);
112 return true;
113 } else {
114 return false;
115 }
116 }
117 sc_fifo<T> &
118 operator = (const T &t)
119 {
120 write(t);
121 return *this;
122 }
123
124 virtual const sc_event &
125 data_written_event() const
126 {
127 return _dataWriteEvent;
128 }
129 virtual const sc_event &
130 data_read_event() const
131 {
132 return _dataReadEvent;
133 }
134
135 virtual int num_available() const { return _entries.size(); }
136 virtual int
137 num_free() const
138 {
139 return _size - _entries.size() - _pending.size();
140 }
141
142 virtual void
143 print(std::ostream &os=std::cout) const
144 {
145 for (typename ::std::list<T>::iterator pos = _pending.begin();
146 pos != _pending.end(); pos++) {
147 os << *pos << ::std::endl;
148 }
145 for (typename ::std::list<T>::iterator pos = _entries.begin();
146 pos != _entries.end(); pos++) {
147 os << *pos << ::std::endl;
148 }
149 }
150 virtual void
149 for (typename ::std::list<T>::iterator pos = _entries.begin();
150 pos != _entries.end(); pos++) {
151 os << *pos << ::std::endl;
152 }
153 }
154 virtual void
151 dump(std::ostream & =std::cout) const
155 dump(std::ostream &os=std::cout) const
152 {
156 {
153 sc_channel_warn_unimpl(__PRETTY_FUNCTION__);
157 os << "name = " << name() << std::endl;
158 int idx = 0;
159 for (typename ::std::list<T>::iterator pos = _pending.begin();
160 pos != _pending.end(); pos++) {
161 os << "value[" << idx++ << "] = " << *pos << ::std::endl;
162 }
163 for (typename ::std::list<T>::iterator pos = _entries.begin();
164 pos != _entries.end(); pos++) {
165 os << "value[" << idx++ << "] = " << *pos << ::std::endl;
166 }
154 }
155 virtual const char *kind() const { return "sc_fifo"; }
156
157 protected:
158 virtual void
159 update()
160 {
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 }
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;
186};
187
188template <class T>
189inline std::ostream &
167 }
168 virtual const char *kind() const { return "sc_fifo"; }
169
170 protected:
171 virtual void
172 update()
173 {
174 if (!_pending.empty()) {
175 _dataWriteEvent.notify(SC_ZERO_TIME);
176 _entries.insert(_entries.end(), _pending.begin(), _pending.end());
177 _pending.clear();
178 }
179 if (_readsHappened) {
180 _readsHappened = false;
181 _dataReadEvent.notify(SC_ZERO_TIME);
182 }
183 }
184
185 private:
186 // Disabled
187 sc_fifo(const sc_fifo<T> &) :
188 sc_fifo_in_if<T>(), sc_fifo_in_if<T>(), sc_prim_channel()
189 {}
190 sc_fifo &operator = (const sc_fifo<T> &) { return *this; }
191
192 sc_event _dataReadEvent;
193 sc_event _dataWriteEvent;
194
195 int _size;
196 mutable std::list<T> _entries;
197 mutable std::list<T> _pending;
198 bool _readsHappened;
199};
200
201template <class T>
202inline std::ostream &
190operator << (std::ostream &os, const sc_fifo &)
203operator << (std::ostream &os, const sc_fifo<T> &f)
191{
204{
192 sc_channel_warn_unimpl(__PRETTY_FUNCTION__);
205 f.print(os);
193 return os;
194}
195
196} // namespace sc_core
197
198#endif //__SYSTEMC_EXT_CHANNEL_SC_FIFO_HH__
206 return os;
207}
208
209} // namespace sc_core
210
211#endif //__SYSTEMC_EXT_CHANNEL_SC_FIFO_HH__