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

Generated on Fri Feb 28 2020 16:27:03 for gem5 by doxygen 1.8.13