scc 2026.07
SystemC components library
base.cpp
1/*
2 * Copyright 2020 - 2022 Arteris IP
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.axi_util.cpp
15 */
16
17#ifndef SC_INCLUDE_DYNAMIC_PROCESSES
18#define SC_INCLUDE_DYNAMIC_PROCESSES
19#endif
20
21#include "base.h"
22#include "protocol_fsm.h"
23#include <scc/report.h>
24#include <scc/utilities.h>
25#include <systemc>
26#include <tlm/scc/tlm_id.h>
27#include <tlm/scc/tlm_mm.h>
28
29using namespace sc_core;
30using namespace tlm;
31namespace axi {
32namespace fsm {
33
34fsm_handle::fsm_handle()
35: fsm{new AxiProtocolFsm()} {
36 fsm->initiate();
37}
38
39fsm_handle::~fsm_handle() {
40 fsm->terminate();
41 delete fsm;
42}
43
44base::base(size_t transfer_width, bool coherent, protocol_time_point_e wr_start)
45: transfer_width_in_bytes(transfer_width / 8)
46, wr_start(wr_start)
47, coherent(coherent) {
48 assert(wr_start == RequestPhaseBeg || wr_start == WValidE);
49 idle_fsm.clear();
50 active_fsm.clear();
51 sc_core::sc_spawn_options opts;
52 opts.dont_initialize();
53 opts.spawn_method();
54 opts.set_sensitivity(&fsm_event_queue.event());
55 sc_core::sc_spawn(sc_bind(&base::process_fsm_event, this), sc_core::sc_gen_unique_name("fsm_event_method"), &opts);
56 fsm_clk_queue.set_avail_cb([this]() {
57 if(fsm_clk_queue_hndl.valid())
58 fsm_clk_queue_hndl.enable();
59 });
60 fsm_clk_queue.set_empty_cb([this]() {
61 if(fsm_clk_queue_hndl.valid())
62 fsm_clk_queue_hndl.disable();
63 });
64}
65
67 auto it = active_fsm.find(gp);
68 if(!gp || it == active_fsm.end()) {
69 if(gp) {
70 SCCTRACE(instance_name) << "creating fsm for trans " << *gp;
71 } else {
72 SCCTRACE(instance_name) << "creating fsm for new transaction";
73 }
74 if(idle_fsm.empty()) {
75 auto fsm_hndl = create_fsm_handle();
76 setup_callbacks(fsm_hndl);
77 idle_fsm.push_back(fsm_hndl);
78 allocated_fsm.emplace_back(fsm_hndl);
79 }
80 auto fsm_hndl = idle_fsm.front();
81 idle_fsm.pop_front();
82 fsm_hndl->reset();
83 if(gp != nullptr) {
84 fsm_hndl->trans = gp;
85 } else {
86 fsm_hndl->trans =
88 }
89 active_fsm.insert(std::make_pair(fsm_hndl->trans.get(), fsm_hndl));
90 fsm_hndl->start = sc_time_stamp();
91 return fsm_hndl;
92 } else {
93 it->second->start = sc_time_stamp();
94 return it->second;
95 }
96}
97
99 while(auto e = fsm_event_queue.get_next()) {
100 auto entry = e.get();
101 if(std::get<2>(entry))
102 schedule(std::get<0>(entry), std::get<1>(entry), 0);
103 else
104 react(std::get<0>(entry), std::get<1>(entry));
105 }
106}
107
109 if(!fsm_clk_queue_hndl.valid())
110 fsm_clk_queue_hndl = sc_process_handle(sc_get_current_process_handle());
111 if(fsm_clk_queue.avail())
112 while(fsm_clk_queue.avail()) {
113 auto entry = fsm_clk_queue.front();
114 if(std::get<2>(entry) == 0) {
115 SCCTRACE(instance_name) << "processing event " << evt2str(std::get<0>(entry)) << " of trans " << *std::get<1>(entry);
116 react(std::get<0>(entry), std::get<1>(entry));
117 } else {
118 std::get<2>(entry) -= 1;
119 fsm_clk_queue.push_back(entry);
120 }
121 fsm_clk_queue.pop_front();
122 }
123 else
124 // fall asleep if there is nothing to process
125 fsm_clk_queue_hndl.disable();
126}
127
128void base::react(protocol_time_point_e event, axi::fsm::fsm_handle* fsm_hndl) {
129 SCCTRACE(instance_name) << "in react() base has coherent =" << coherent << " with event " << evt2str(event);
130 fsm_hndl->state = event;
131 switch(event) {
132 case WValidE:
133 fsm_hndl->fsm->process_event(WReq());
134 return;
135 case WReadyE:
136 case RequestPhaseBeg:
137 if(is_burst(*fsm_hndl->trans) && fsm_hndl->trans->is_write() && !is_dataless(fsm_hndl->trans->get_extension<axi::ace_extension>()))
138 fsm_hndl->fsm->process_event(BegPartReq());
139 else
140 fsm_hndl->fsm->process_event(BegReq());
141 return;
142 case BegPartReqE:
143 fsm_hndl->fsm->process_event(BegPartReq());
144 return;
145 case EndPartReqE:
146 fsm_hndl->fsm->process_event(EndPartReq());
147 return;
148 case BegReqE:
149 fsm_hndl->fsm->process_event(BegReq());
150 return;
151 case EndReqE:
152 fsm_hndl->fsm->process_event(EndReq());
153 return;
154 case BegPartRespE:
155 fsm_hndl->fsm->process_event(BegPartResp());
156 return;
157 case EndPartRespE:
158 fsm_hndl->fsm->process_event(EndPartResp());
159 return;
160 case BegRespE:
161 fsm_hndl->fsm->process_event(BegResp());
162 return;
163 case EndRespE:
164 SCCTRACE(instance_name) << "in EndResp of base() with coherent =" << coherent;
165 if(!coherent || fsm_hndl->is_snoop) {
166 SCCTRACE(instance_name) << "freeing fsm for trans " << *fsm_hndl->trans;
167 fsm_hndl->fsm->process_event(EndResp());
168 active_fsm.erase(fsm_hndl->trans.get());
169 fsm_hndl->trans = nullptr;
170 idle_fsm.push_back(fsm_hndl);
171 finish_evt.notify();
172 } else {
173 fsm_hndl->fsm->process_event(EndRespNoAck());
174 }
175 return;
176 case Ack:
177 SCCTRACE(instance_name) << "freeing fsm for trans " << *fsm_hndl->trans;
178 fsm_hndl->fsm->process_event(AckRecv());
179 active_fsm.erase(fsm_hndl->trans.get());
180 fsm_hndl->trans = nullptr;
181 idle_fsm.push_back(fsm_hndl);
182 finish_evt.notify();
183 return;
184 default:
185 SCCFATAL(instance_name) << "No valid protocol time point";
186 }
187}
188
189tlm_sync_enum base::nb_fw(payload_type& trans, phase_type const& phase, sc_time& t) {
190 SCCTRACE(instance_name) << "base::nb_fw " << phase << " with delay = " << t << " of trans " << trans;
191 if(phase == BEGIN_PARTIAL_REQ || phase == BEGIN_REQ) { // read/write
192 auto fsm_hndl = find_or_create(&trans);
193 if(!trans.is_read()) {
194 protocol_time_point_e evt = axi::fsm::RequestPhaseBeg;
195 if(fsm_hndl->beat_count == 0 && wr_start != RequestPhaseBeg)
196 evt = wr_start;
197 else
198 evt = phase == BEGIN_PARTIAL_REQ ? BegPartReqE : BegReqE;
199 if(t == SC_ZERO_TIME) {
200 react(evt, &trans);
201 } else {
202 schedule(evt, &trans, t);
203 }
204 } else {
205 if(t == SC_ZERO_TIME) {
206 react(BegReqE, &trans);
207 } else
208 schedule(BegReqE, &trans, t);
209 }
210 } else if(phase == END_PARTIAL_RESP || phase == END_RESP) {
211 if(t == SC_ZERO_TIME) {
212 react(phase == END_RESP ? EndRespE : EndPartRespE, &trans);
213 } else
214 schedule(phase == END_RESP ? EndRespE : EndPartRespE, &trans, t);
215 } else if(phase == END_REQ) { // snoop access resp
216 if(t == SC_ZERO_TIME) {
217 react(EndReqE, &trans);
218 } else
219 schedule(EndReqE, &trans, t);
220 } else if(phase == BEGIN_PARTIAL_RESP || phase == BEGIN_RESP) { // snoop access resp
221 if(t == SC_ZERO_TIME) {
222 react(phase == BEGIN_RESP ? BegRespE : BegPartRespE, &trans);
223 } else
224 schedule(phase == BEGIN_RESP ? BegRespE : BegPartRespE, &trans, t);
225 } else if(phase == axi::ACK) {
226 if(t == SC_ZERO_TIME) {
227 react(Ack, &trans);
228 } else
229 schedule(Ack, &trans, t);
230 }
231 return TLM_ACCEPTED;
232}
233
234tlm_sync_enum base::nb_bw(payload_type& trans, phase_type const& phase, sc_time& t) {
235 SCCTRACE(instance_name) << "base::nb_bw " << phase << " of trans " << trans;
236 if(phase == END_PARTIAL_REQ || phase == END_REQ) { // read/write
237 if(t == SC_ZERO_TIME) {
238 react(phase == END_REQ ? EndReqE : EndPartReqE, &trans);
239 } else
240 schedule(phase == END_REQ ? EndReqE : EndPartReqE, &trans, t);
241 } else if(phase == BEGIN_PARTIAL_RESP || phase == BEGIN_RESP) { // read/write response
242 if(t == SC_ZERO_TIME) {
243 react(phase == BEGIN_RESP ? BegRespE : BegPartRespE, &trans);
244 } else
245 schedule(phase == BEGIN_RESP ? BegRespE : BegPartRespE, &trans, t);
246 } else if(phase == BEGIN_REQ) { // snoop read
247 auto fsm_hndl = find_or_create(&trans, true);
248 fsm_hndl->is_snoop = true;
249 if(t == SC_ZERO_TIME) {
250 react(BegReqE, &trans);
251 } else
252 schedule(BegReqE, &trans, t);
253 } else if(phase == END_PARTIAL_RESP || phase == END_RESP) { // snoop read response
254 if(t == SC_ZERO_TIME) {
255 react(phase == END_RESP ? EndRespE : EndPartRespE, &trans);
256 } else
257 schedule(phase == END_RESP ? EndRespE : EndPartRespE, &trans, t);
258 }
259 return TLM_ACCEPTED;
260}
261
262} // namespace fsm
263} // namespace axi
payload_type * allocate()
get a plain tlm_payload_type without extensions
Definition tlm_mm.h:218
T * get() const noexcept
Return the stored pointer.
TLM2.0 components modeling AHB.
bool is_burst(const axi::axi_protocol_types::tlm_payload_type &trans)
Definition axi_tlm.h:1136
SystemC TLM.
Definition dmi_mgr.h:19
void react(axi::fsm::protocol_time_point_e event, tlm::scc::tlm_gp_shared_ptr &trans)
triggers the FSM with event and given transaction
Definition base.h:127
tlm::tlm_sync_enum nb_fw(payload_type &trans, phase_type const &phase, sc_core::sc_time &t)
triggers the FSM based on TLM phases in the forward path. Should be called from np_transport_fw of th...
Definition base.cpp:189
virtual axi::fsm::fsm_handle * create_fsm_handle()=0
function to create a fsm_handle. Needs to be implemented by the derived class
virtual void setup_callbacks(axi::fsm::fsm_handle *)=0
this function is called to add the callbacks to the fsm handle during creation. Needs to be implement...
base(size_t transfer_width, bool coherent=false, axi::fsm::protocol_time_point_e wr_start=axi::fsm::RequestPhaseBeg)
the constructor
Definition base.cpp:44
void schedule(axi::fsm::protocol_time_point_e e, tlm::scc::tlm_gp_shared_ptr &gp, unsigned cycles)
processes the fsm_sched_queue and propagates events to fsm_clk_queue. Should be registered as falling...
Definition base.h:103
void process_fsm_clk_queue()
processes the fsm_clk_queue and triggers the FSM accordingly. Should be registered as rising-edge clo...
Definition base.cpp:108
tlm::tlm_sync_enum nb_bw(payload_type &trans, phase_type const &phase, sc_core::sc_time &t)
triggers the FSM based on TLM phases in the backward path. Should be called from np_transport_bw of t...
Definition base.cpp:234
axi::fsm::fsm_handle * find_or_create(payload_type *gp=nullptr, bool ace=false)
retrieve the FSM handle based on the transaction passed. If non exist one will be created
Definition base.cpp:66
void process_fsm_event()
processes the fsm_event_queue and triggers FSM aligned
Definition base.cpp:98
axi::axi_protocol_types::tlm_payload_type payload_type
aliases used in the class
Definition base.h:42
tlm::scc::tlm_gp_shared_ptr trans
pointer to the associated AXITLM payload
Definition types.h:61
size_t beat_count
beat count of this transaction
Definition types.h:63
AxiProtocolFsm *const fsm
pointer to the FSM
Definition types.h:59
bool is_snoop
indicator if this is a snoop access
Definition types.h:65
static tlm_mm & get()
accessor function of the singleton
Definition tlm_mm.h:338