gem5  v20.1.0.0
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"
36 #include "../utils/sc_report_handler.hh"
37 #include "messages.hh"
38 #include "sc_fifo_in_if.hh"
39 #include "sc_fifo_out_if.hh"
40 
41 namespace sc_core
42 {
43 
44 class sc_port_base;
45 class sc_event;
46 
47 template <class T>
48 class 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>(),
56  _size(size), _num_free(size), _num_available(0),
57  _readsHappened(false), _writesHappened(false),
58  _reader(NULL), _writer(NULL)
59  {}
60  explicit sc_fifo(const char *name, int size=16) :
61  sc_fifo_in_if<T>(), sc_fifo_out_if<T>(),
62  sc_prim_channel(name), _size(size), _num_free(size),
64  _reader(NULL), _writer(NULL)
65  {}
66  virtual ~sc_fifo() {}
67 
68  virtual void
69  register_port(sc_port_base &port, const char *iface_type_name)
70  {
71  std::string tn(iface_type_name);
72  if (tn == typeid(sc_fifo_in_if<T>).name() ||
73  tn == typeid(sc_fifo_blocking_in_if<T>).name()) {
74  if (_reader)
76  _reader = &port;
77  } else if (tn == typeid(sc_fifo_out_if<T>).name() ||
78  tn == typeid(sc_fifo_blocking_out_if<T>).name()) {
79  if (_writer)
81  _writer = &port;
82  } else {
84  "sc_fifo<T> port not recognized");
85  }
86  }
87 
88  virtual void
89  read(T &t)
90  {
91  while (num_available() == 0)
93  _readsHappened = true;
94  t = _entries.front();
95  _entries.pop_front();
98  }
99  virtual T
101  {
102  T t;
103  read(t);
104  return t;
105  }
106  virtual bool
107  nb_read(T &t)
108  {
109  if (num_available()) {
110  read(t);
111  return true;
112  } else {
113  return false;
114  }
115  }
116  operator T() { return read(); }
117 
118  virtual void
119  write(const T &t)
120  {
121  while (num_free() == 0)
123  _writesHappened = true;
124  _entries.emplace_back(t);
125  _num_free--;
126  request_update();
127  }
128  virtual bool
129  nb_write(const T &t)
130  {
131  if (num_free()) {
132  write(t);
133  return true;
134  } else {
135  return false;
136  }
137  }
138  sc_fifo<T> &
139  operator = (const T &t)
140  {
141  write(t);
142  return *this;
143  }
144 
145  virtual const sc_event &
147  {
148  return _dataWriteEvent;
149  }
150  virtual const sc_event &
152  {
153  return _dataReadEvent;
154  }
155 
156  virtual int num_available() const { return _num_available; }
157  virtual int num_free() const { return _num_free; }
158 
159  virtual void
160  print(std::ostream &os=std::cout) const
161  {
162  for (typename ::std::list<T>::iterator pos = _entries.begin();
163  pos != _entries.end(); pos++) {
164  os << *pos << ::std::endl;
165  }
166  }
167  virtual void
168  dump(std::ostream &os=std::cout) const
169  {
170  os << "name = " << name() << std::endl;
171  int idx = 0;
172  for (typename ::std::list<T>::iterator pos = _entries.begin();
173  pos != _entries.end(); pos++) {
174  os << "value[" << idx++ << "] = " << *pos << ::std::endl;
175  }
176  }
177  virtual const char *kind() const { return "sc_fifo"; }
178 
179  protected:
180  virtual void
182  {
183  _num_available = _entries.size();
185  if (_writesHappened) {
186  _writesHappened = false;
188  }
189  if (_readsHappened) {
190  _readsHappened = false;
192  }
193  }
194 
195  private:
196  // Disabled
198  {}
199  sc_fifo &operator = (const sc_fifo<T> &) { return *this; }
200 
203 
206 
207  int _size;
213 };
214 
215 template <class T>
216 inline std::ostream &
217 operator << (std::ostream &os, const sc_fifo<T> &f)
218 {
219  f.print(os);
220  return os;
221 }
222 
223 } // namespace sc_core
224 
225 #endif //__SYSTEMC_EXT_CHANNEL_SC_FIFO_HH__
sc_core::sc_port_base
Definition: sc_port.hh:74
sc_core::sc_fifo::print
virtual void print(std::ostream &os=std::cout) const
Definition: sc_fifo.hh:160
X86ISA::os
Bitfield< 17 > os
Definition: misc.hh:803
sc_core::sc_fifo::read
virtual void read(T &t)
Definition: sc_fifo.hh:89
sc_core::sc_fifo::sc_fifo
sc_fifo(const sc_fifo< T > &)
Definition: sc_fifo.hh:197
sc_core::sc_fifo::update
virtual void update()
Definition: sc_fifo.hh:181
sc_core::SC_ID_MORE_THAN_ONE_FIFO_WRITER_
const char SC_ID_MORE_THAN_ONE_FIFO_WRITER_[]
Definition: messages.cc:40
sc_core::sc_fifo::operator=
sc_fifo< T > & operator=(const T &t)
Definition: sc_fifo.hh:139
sc_core
Definition: messages.cc:31
sc_core::sc_fifo_out_if
Definition: sc_fifo_out_if.hh:54
sc_core::sc_fifo::sc_fifo
sc_fifo(int size=16)
Definition: sc_fifo.hh:53
sc_core::sc_fifo::kind
virtual const char * kind() const
Definition: sc_fifo.hh:177
sc_core::SC_ZERO_TIME
const sc_time SC_ZERO_TIME
Definition: sc_time.cc:290
sc_core::sc_fifo::_size
int _size
Definition: sc_fifo.hh:207
sc_core::sc_fifo::_writer
sc_port_base * _writer
Definition: sc_fifo.hh:205
sc_core::sc_fifo::data_read_event
virtual const sc_event & data_read_event() const
Definition: sc_fifo.hh:151
messages.hh
sc_core::sc_fifo::write
virtual void write(const T &t)
Definition: sc_fifo.hh:119
sc_core::sc_fifo::dump
virtual void dump(std::ostream &os=std::cout) const
Definition: sc_fifo.hh:168
sc_core::sc_fifo::~sc_fifo
virtual ~sc_fifo()
Definition: sc_fifo.hh:66
sc_core::sc_prim_channel::request_update
void request_update()
Definition: sc_prim.cc:70
sc_core::sc_fifo::_num_available
int _num_available
Definition: sc_fifo.hh:209
sc_core::sc_fifo::_reader
sc_port_base * _reader
Definition: sc_fifo.hh:204
sc_fifo_out_if.hh
sc_gem5::InternalScEvent
Definition: sc_event.hh:254
SC_REPORT_ERROR
#define SC_REPORT_ERROR(msg_type, msg)
Definition: sc_report_handler.hh:127
sc_core::sc_fifo_blocking_out_if
Definition: sc_fifo_out_if.hh:47
sc_core::sc_event
Definition: sc_event.hh:169
sc_core::operator<<
std::ostream & operator<<(std::ostream &os, sc_status s)
Definition: sc_main.cc:179
sc_core::sc_fifo::register_port
virtual void register_port(sc_port_base &port, const char *iface_type_name)
Definition: sc_fifo.hh:69
sc_core::sc_fifo::_readsHappened
bool _readsHappened
Definition: sc_fifo.hh:211
sc_core::sc_gen_unique_name
const char * sc_gen_unique_name(const char *seed)
Definition: sc_module.cc:820
sc_core::sc_fifo::sc_fifo
sc_fifo(const char *name, int size=16)
Definition: sc_fifo.hh:60
sc_fifo_in_if.hh
sc_core::sc_fifo_in_if
Definition: sc_fifo_in_if.hh:55
sc_core::SC_ID_MORE_THAN_ONE_FIFO_READER_
const char SC_ID_MORE_THAN_ONE_FIFO_READER_[]
Definition: messages.cc:38
sc_core::sc_fifo_blocking_in_if
Definition: sc_fifo_in_if.hh:47
sc_core::sc_event::notify
void notify()
Definition: sc_event.cc:337
sc_core::sc_fifo::_writesHappened
bool _writesHappened
Definition: sc_fifo.hh:212
sc_core::sc_fifo::nb_read
virtual bool nb_read(T &t)
Definition: sc_fifo.hh:107
sc_core::sc_fifo::_dataWriteEvent
sc_gem5::InternalScEvent _dataWriteEvent
Definition: sc_fifo.hh:202
sc_core::wait
void wait()
Definition: sc_module.cc:653
sc_core::sc_fifo::_entries
std::list< T > _entries
Definition: sc_fifo.hh:210
ArmISA::t
Bitfield< 5 > t
Definition: miscregs_types.hh:67
sc_core::sc_prim_channel
Definition: sc_prim.hh:50
sc_core::sc_object::name
const char * name() const
Definition: sc_object.cc:44
sc_core::sc_fifo::read
virtual T read()
Definition: sc_fifo.hh:100
sc_core::sc_fifo
Definition: sc_fifo.hh:48
sc_core::sc_fifo::data_written_event
virtual const sc_event & data_written_event() const
Definition: sc_fifo.hh:146
std::list
STL list class.
Definition: stl.hh:51
sc_core::sc_fifo::num_free
virtual int num_free() const
Definition: sc_fifo.hh:157
sc_core::SC_ID_BIND_IF_TO_PORT_
const char SC_ID_BIND_IF_TO_PORT_[]
Definition: messages.cc:44
sc_core::sc_fifo::_dataReadEvent
sc_gem5::InternalScEvent _dataReadEvent
Definition: sc_fifo.hh:201
sc_core::sc_fifo::_num_free
int _num_free
Definition: sc_fifo.hh:208
ArmISA::f
Bitfield< 6 > f
Definition: miscregs_types.hh:64
sc_core::sc_fifo::num_available
virtual int num_available() const
Definition: sc_fifo.hh:156
sc_core::sc_fifo::nb_write
virtual bool nb_write(const T &t)
Definition: sc_fifo.hh:129

Generated on Wed Sep 30 2020 14:02:15 for gem5 by doxygen 1.8.17