scc 2025.09
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
28namespace scc {
38template <typename T> class fifo_w_cb : public sc_core::sc_prim_channel {
39public:
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 void push_back(T&& t) {
58 in_queue.emplace_back(std::move(t));
59 request_update();
60 }
61
62 T& back() { return in_queue.back(); }
63 const T& back() const { return in_queue.back(); }
64
65 void pop_front() {
66 out_queue.pop_front();
67 request_update();
68 if(empty_cb && !out_queue.size())
69 empty_cb();
70 }
71
72 T& front() { return out_queue.front(); }
73 const T& front() const { return out_queue.front(); }
74
75 T read() {
76 wait(data_written_evt);
77 auto val = front();
78 pop_front();
79 return val;
80 }
81
82 size_t avail() const { return out_queue.size(); }
83 bool empty() const { return out_queue.empty(); }
84
85 void set_avail_cb(std::function<void(void)> f) { avail_cb = f; }
86 void set_empty_cb(std::function<void(void)> f) { empty_cb = f; }
87
88 inline sc_core::sc_event const& data_written_event() const { return data_written_evt; }
89
90 inline unsigned num_avail() { return available; }
91 inline unsigned num_written() { return written; }
92
93protected:
94 // the update method (does nothing by default)
95 virtual void update() {
96 written = 0;
97 available = out_queue.size();
98 if(in_queue.empty())
99 return;
100 written = in_queue.size();
101 out_queue.insert(out_queue.end(), in_queue.begin(), in_queue.end());
102 in_queue.clear();
103 available = out_queue.size();
104 if(avail_cb)
105 avail_cb();
106 data_written_evt.notify(sc_core::SC_ZERO_TIME);
107 }
108
109 std::deque<T> in_queue{};
110 std::deque<T> out_queue{};
111 std::function<void(void)> avail_cb{};
112 std::function<void(void)> empty_cb{};
113 unsigned available = 0;
114 unsigned written = 0;
115 sc_core::sc_event data_written_evt{};
116};
117
118} /* namespace scc */ // end of scc-sysc
SCC TLM utilities.