scc  2022.4.0
SystemC components library
fifo_w_cb.h
1 /*******************************************************************************
2  * Copyright 2019 MINRES Technologies GmbH
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *******************************************************************************/
16 
17 #pragma once
18 
19 #include <deque>
20 #include <functional>
21 #include <sysc/communication/sc_prim_channel.h>
22 
28 namespace scc {
38 template <typename T> class fifo_w_cb : public sc_core::sc_prim_channel {
39 public:
40  fifo_w_cb()
41  : sc_core::sc_prim_channel(sc_core::sc_gen_unique_name("fifo_w_cb")) {}
42 
43  fifo_w_cb(const char* name)
44  : sc_core::sc_prim_channel(name) {}
45 
46  virtual ~fifo_w_cb(){};
47 
48  void push_back(T& t) {
49  in_queue.push_back(t);
50  request_update();
51  }
52  void push_back(const T& t) {
53  in_queue.push_back(t);
54  request_update();
55  }
56 
57  T& back() { return in_queue.back(); }
58  const T& back() const { return in_queue.back(); }
59 
60  void pop_front() {
61  out_queue.pop_front();
62  if(empty_cb && !out_queue.size())
63  empty_cb();
64  }
65 
66  T& front() { return out_queue.front(); }
67  const T& front() const { return out_queue.front(); }
68 
69  size_t avail() const { return out_queue.size(); }
70  bool empty() const { return out_queue.empty(); }
71 
72  void set_avail_cb(std::function<void(void)> f) { avail_cb = f; }
73  void set_empty_cb(std::function<void(void)> f) { empty_cb = f; }
74 
75  inline sc_core::sc_event const& data_written_event() const { return data_written_evt; }
76 
77 protected:
78  // the update method (does nothing by default)
79  virtual void update() {
80  if(in_queue.empty())
81  return;
82  out_queue.insert(out_queue.end(), in_queue.begin(), in_queue.end());
83  in_queue.clear();
84  if(avail_cb)
85  avail_cb();
86  data_written_evt.notify(sc_core::SC_ZERO_TIME);
87  }
88 
89  std::deque<T> in_queue{};
90  std::deque<T> out_queue{};
91  std::function<void(void)> avail_cb{};
92  std::function<void(void)> empty_cb{};
93  sc_core::sc_event data_written_evt{};
94 };
95 
96 } /* namespace scc */ // end of scc-sysc
fifo with callbacks
Definition: fifo_w_cb.h:38
SCC SystemC utilities.