gem5 v24.0.0.0
Loading...
Searching...
No Matches
sc_fifo.hh
Go to the documentation of this file.
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
28#ifndef __SYSTEMC_EXT_CHANNEL_SC_FIFO_HH__
29#define __SYSTEMC_EXT_CHANNEL_SC_FIFO_HH__
30
31#include <list>
32#include <string>
33
34#include "../core/sc_module.hh" // for sc_gen_unique_name
35#include "../core/sc_prim.hh"
37#include "messages.hh"
38#include "sc_fifo_in_if.hh"
39#include "sc_fifo_out_if.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) :
55 sc_prim_channel(sc_gen_unique_name("fifo")),
56 _reader(NULL), _writer(NULL),
57 _size(size), _num_free(size), _num_available(0),
58 _readsHappened(false), _writesHappened(false)
59 {}
60 explicit sc_fifo(const char *name, int size=16) :
63 _reader(NULL), _writer(NULL),
64 _size(size), _num_free(size), _num_available(0),
65 _readsHappened(false), _writesHappened(false)
66 {}
67 virtual ~sc_fifo() {}
68
69 virtual void
70 register_port(sc_port_base &port, const char *iface_type_name)
71 {
72 std::string tn(iface_type_name);
73 if (tn == typeid(sc_fifo_in_if<T>).name() ||
74 tn == typeid(sc_fifo_blocking_in_if<T>).name()) {
75 if (_reader)
76 SC_REPORT_ERROR(SC_ID_MORE_THAN_ONE_FIFO_READER_, "");
77 _reader = &port;
78 } else if (tn == typeid(sc_fifo_out_if<T>).name() ||
79 tn == typeid(sc_fifo_blocking_out_if<T>).name()) {
80 if (_writer)
81 SC_REPORT_ERROR(SC_ID_MORE_THAN_ONE_FIFO_WRITER_, "");
82 _writer = &port;
83 } else {
84 SC_REPORT_ERROR(SC_ID_BIND_IF_TO_PORT_,
85 "sc_fifo<T> port not recognized");
86 }
87 }
88
89 virtual void
90 read(T &t)
91 {
92 while (num_available() == 0)
94 _readsHappened = true;
95 t = _entries.front();
96 _entries.pop_front();
99 }
100 virtual T
102 {
103 T t;
104 read(t);
105 return t;
106 }
107 virtual bool
109 {
110 if (num_available()) {
111 read(t);
112 return true;
113 } else {
114 return false;
115 }
116 }
117 operator T() { return read(); }
118
119 virtual void
120 write(const T &t)
121 {
122 while (num_free() == 0)
124 _writesHappened = true;
125 _entries.emplace_back(t);
126 _num_free--;
128 }
129 virtual bool
130 nb_write(const T &t)
131 {
132 if (num_free()) {
133 write(t);
134 return true;
135 } else {
136 return false;
137 }
138 }
139 sc_fifo<T> &
140 operator = (const T &t)
141 {
142 write(t);
143 return *this;
144 }
145
146 virtual const sc_event &
148 {
149 return _dataWriteEvent;
150 }
151 virtual const sc_event &
153 {
154 return _dataReadEvent;
155 }
156
157 virtual int num_available() const { return _num_available; }
158 virtual int num_free() const { return _num_free; }
159
160 virtual void
161 print(std::ostream &os=std::cout) const
162 {
163 for (typename ::std::list<T>::iterator pos = _entries.begin();
164 pos != _entries.end(); pos++) {
165 os << *pos << ::std::endl;
166 }
167 }
168 virtual void
169 dump(std::ostream &os=std::cout) const
170 {
171 os << "name = " << name() << std::endl;
172 int idx = 0;
173 for (typename ::std::list<T>::iterator pos = _entries.begin();
174 pos != _entries.end(); pos++) {
175 os << "value[" << idx++ << "] = " << *pos << ::std::endl;
176 }
177 }
178 virtual const char *kind() const { return "sc_fifo"; }
179
180 protected:
181 virtual void
183 {
184 _num_available = _entries.size();
186 if (_writesHappened) {
187 _writesHappened = false;
188 _dataWriteEvent.notify(SC_ZERO_TIME);
189 }
190 if (_readsHappened) {
191 _readsHappened = false;
192 _dataReadEvent.notify(SC_ZERO_TIME);
193 }
194 }
195
196 private:
197 // Disabled
200 sc_fifo &operator = (const sc_fifo<T> &) { return *this; }
201
204
207
208 int _size;
214};
215
216template <class T>
217inline std::ostream &
218operator << (std::ostream &os, const sc_fifo<T> &f)
219{
220 f.print(os);
221 return os;
222}
223
224} // namespace sc_core
225
226#endif //__SYSTEMC_EXT_CHANNEL_SC_FIFO_HH__
sc_port_base * _writer
Definition sc_fifo.hh:206
virtual int num_available() const
Definition sc_fifo.hh:157
virtual const sc_event & data_written_event() const
Definition sc_fifo.hh:147
sc_fifo< T > & operator=(const T &t)
Definition sc_fifo.hh:140
virtual int num_free() const
Definition sc_fifo.hh:158
virtual ~sc_fifo()
Definition sc_fifo.hh:67
virtual void print(std::ostream &os=std::cout) const
Definition sc_fifo.hh:161
sc_fifo(int size=16)
Definition sc_fifo.hh:53
virtual const sc_event & data_read_event() const
Definition sc_fifo.hh:152
sc_port_base * _reader
Definition sc_fifo.hh:205
sc_gem5::InternalScEvent _dataWriteEvent
Definition sc_fifo.hh:203
virtual void write(const T &t)
Definition sc_fifo.hh:120
virtual T read()
Definition sc_fifo.hh:101
virtual const char * kind() const
Definition sc_fifo.hh:178
virtual void dump(std::ostream &os=std::cout) const
Definition sc_fifo.hh:169
virtual bool nb_read(T &t)
Definition sc_fifo.hh:108
virtual bool nb_write(const T &t)
Definition sc_fifo.hh:130
sc_gem5::InternalScEvent _dataReadEvent
Definition sc_fifo.hh:202
virtual void register_port(sc_port_base &port, const char *iface_type_name)
Definition sc_fifo.hh:70
std::list< T > _entries
Definition sc_fifo.hh:211
sc_fifo(const sc_fifo< T > &)
Definition sc_fifo.hh:198
virtual void update()
Definition sc_fifo.hh:182
virtual void read(T &t)
Definition sc_fifo.hh:90
sc_fifo(const char *name, int size=16)
Definition sc_fifo.hh:60
const char * name() const
Definition sc_object.cc:44
STL list class.
Definition stl.hh:51
int f(int a, int b)
std::ostream & operator<<(std::ostream &os, sc_status s)
Definition sc_main.cc:178
void wait()
Definition sc_module.cc:653
#define SC_REPORT_ERROR(msg_type, msg)

Generated on Tue Jun 18 2024 16:24:06 for gem5 by doxygen 1.11.0