quic/qbox
Loading...
Searching...
No Matches
fss_utils.h
1/*
2 * Copyright (c) 2025 Qualcomm Innovation Center, Inc. All Rights Reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#ifndef __GS_FSS_UTILS_H__
8#define __GS_FSS_UTILS_H__
9
10#include <systemc>
11#include <tlm>
12#include <cstddef>
13#include <new>
14#include <cassert>
15#include <type_traits>
16#include <utility>
17#include "fss_interfaces.h"
18
19namespace gs {
20
29template <class PARENT_TYPE, class MEMBER_TYPE>
31{
32 const PARENT_TYPE* const parent = 0;
33 auto member_offset = reinterpret_cast<ptrdiff_t>(reinterpret_cast<const uint8_t*>(&(parent->*member_ptr)) -
34 reinterpret_cast<const uint8_t*>(parent));
35 return std::launder(
36 reinterpret_cast<PARENT_TYPE*>(reinterpret_cast<size_t>(member) - static_cast<size_t>(member_offset)));
37}
38
39void die_null_ptr(const std::string& fn_name, const std::string& ptr_name)
40{
41 std::cerr << "Error: fss_utils.h -> Function: " << fn_name << " => pointer: " << ptr_name << "is null!"
42 << std::endl;
43 exit(EXIT_FAILURE);
44}
45
46template <typename INTERFACE>
48{
49public:
50 virtual INTERFACE* get_if_handle() const = 0;
51
52 virtual fssString get_if_name() const = 0;
53
54 virtual void bind(fss_binder_if<INTERFACE>& other)
55 {
56 bind_if(other.get_if_name(), other.get_if_handle());
57 other.bind_if(get_if_name(), get_if_handle());
58 }
59
60 virtual ~fss_binder_if() {}
61
62protected:
63 virtual void bind_if(fssString if_name, INTERFACE* if_handle) = 0;
64};
65
66template <typename T>
68
69template <typename T>
71
72template <typename IF>
73class fss_binder_base : public sc_core::sc_object, public fss_binder_if<IF>
74{
75public:
77 : sc_core::sc_object(obj_name), m_if_name(if_name), m_bind_handle(bind_handle)
78 {
79 if (!bind_handle) {
80 die_null_ptr("fss_binder_base", "bind_handle");
81 }
82 if (!if_name) {
83 die_null_ptr("fss_binder_base", "if_name");
84 }
86 if constexpr (std::is_same_v<IF, fssEventsIf>) {
87 get_if = bind_handle->getEventsIfHandle;
88 } else if constexpr (std::is_same_v<IF, fssTimeSyncIf>) {
89 get_if = bind_handle->getTimeSyncIfHandle;
90 } else if constexpr (std::is_same_v<IF, fssControlIf>) {
91 get_if = bind_handle->getControlIfHandle;
92 } else {
93 get_if = bind_handle->getBusIfHandle;
94 }
95 if (!get_if) {
96 die_null_ptr("fss_binder_base", "get_if");
97 }
98 m_if_handle = get_if(bind_handle, if_name);
99 if (!m_if_handle) {
100 die_null_ptr("fss_binder_base", "m_if_handle");
101 }
102 }
103
104 fss_binder_base(fssString obj_name, fssString _if_name, IF* if_handle)
105 : sc_core::sc_object(obj_name), m_if_name(_if_name), m_if_handle(if_handle)
106 {
107 if (!if_handle) {
108 die_null_ptr("fss_binder_base", "if_handle");
109 }
110 if (!_if_name) {
111 die_null_ptr("fss_binder_base", "_if_name");
112 }
113 m_bind_handle = nullptr;
114 }
115
116 IF* get_if_handle() const override { return m_if_handle; }
117
118 fssString get_if_name() const override { return m_if_name; }
119
120 virtual ~fss_binder_base() {}
121
122protected:
123 void bind_if(fssString if_name, IF* if_handle) override
124 {
125 if (!if_name) {
126 die_null_ptr("bind_if", "if_name");
127 }
128 if (!if_handle) {
129 die_null_ptr("bind_if", "if_handle");
130 }
131 other_if_name = if_name;
132 other_if_handle = if_handle;
133 if (m_bind_handle) {
135 if constexpr (std::is_same_v<IF, fssEventsIf>) {
136 bind_if_if = m_bind_handle->bindEventsIf;
137 } else if constexpr (std::is_same_v<IF, fssTimeSyncIf>) {
138 bind_if_if = m_bind_handle->bindTimeSyncIf;
139 } else if constexpr (std::is_same_v<IF, fssControlIf>) {
140 bind_if_if = m_bind_handle->bindControlIf;
141 } else {
142 bind_if_if = m_bind_handle->bindBusIf;
143 }
144 if (!bind_if_if) {
145 die_null_ptr("bind_if", "bind_if_if");
146 }
147 bind_if_if(m_bind_handle, if_name, if_handle);
148 }
149 }
150
151protected:
152 fssString m_if_name;
153 fssBindIFHandle m_bind_handle;
154 IF* m_if_handle;
155 IF* other_if_handle;
156 fssString other_if_name;
157};
158
159class fss_events_binder : public fss_binder_base<fssEventsIf>
160{
161public:
164 {
165 }
166
169 {
170 }
171
172 void notify(fssUint64 event)
173 {
174 if (!other_if_handle) {
175 die_null_ptr("bind_if", "other_if_handle");
176 }
177 if (!other_if_handle->handleEvents) {
178 die_null_ptr("bind_if", "other_if_handle->handleEvents");
179 }
180 other_if_handle->handleEvents(other_if_handle, event);
181 }
182
183 virtual ~fss_events_binder() {}
184};
185
186class fss_time_sync_binder : public fss_binder_base<fssTimeSyncIf>
187{
188public:
191 {
192 }
193
196 {
197 }
198
199 void update_time(const TimeWindow& window)
200 {
201 if (!other_if_handle) {
202 die_null_ptr("update_time", "other_if_handle");
203 }
204 if (!other_if_handle->updateTimeWindow) {
205 die_null_ptr("update_time", "other_if_handle->updateTimeWindow");
206 }
207 other_if_handle->updateTimeWindow(other_if_handle, window);
208 }
209
210 virtual ~fss_time_sync_binder() {}
211};
212
213class fss_data_binder : public fss_binder_base<fssBusIf>
214{
215public:
218 {
219 }
220
223 {
224 }
225
226 fssBusIfItemSize get_number()
227 {
228 if (!other_if_handle) {
229 die_null_ptr("get_number", "other_if_handle");
230 }
231 if (!other_if_handle->getNumber) {
232 die_null_ptr("get_number", "other_if_handle->getNumber");
233 }
234 return other_if_handle->getNumber(other_if_handle);
235 }
237 {
238 if (!other_if_handle) {
239 die_null_ptr("get_name", "other_if_handle");
240 }
241 if (!other_if_handle->getName) {
242 die_null_ptr("get_name", "other_if_handle->getName");
243 }
244 return other_if_handle->getName(other_if_handle, _index);
245 }
247 {
248 if (!other_if_handle) {
249 die_null_ptr("get_size", "other_if_handle");
250 }
251 if (!other_if_handle->getSize) {
252 die_null_ptr("get_size", "other_if_handle->getSize");
253 }
254 return other_if_handle->getSize(other_if_handle, _index);
255 }
257 {
258 if (!other_if_handle) {
259 die_null_ptr("get_index", "other_if_handle");
260 }
261 if (!other_if_handle->getIndex) {
262 die_null_ptr("get_index", "other_if_handle->getIndex");
263 }
264 return other_if_handle->getIndex(other_if_handle, _name);
265 }
267 {
268 if (!other_if_handle) {
269 die_null_ptr("add_item", "other_if_handle");
270 }
271 if (!other_if_handle->addItem) {
272 die_null_ptr("add_item", "other_if_handle->addItem");
273 }
274 return other_if_handle->addItem(other_if_handle, _name, _size);
275 }
277 {
278 if (!other_if_handle) {
279 die_null_ptr("get_item", "other_if_handle");
280 }
281 if (!other_if_handle->getItem) {
282 die_null_ptr("get_item", "other_if_handle->getItem");
283 }
284 return other_if_handle->getItem(other_if_handle, _index);
285 }
287 {
288 if (!other_if_handle) {
289 die_null_ptr("set_item", "other_if_handle");
290 }
291 if (!other_if_handle->setItem) {
292 die_null_ptr("set_item", "other_if_handle->setItem");
293 }
294 other_if_handle->setItem(other_if_handle, _index, _data);
295 }
296
297 void transmit()
298 {
299 if (!other_if_handle) {
300 die_null_ptr("transmit", "other_if_handle");
301 }
302 if (!other_if_handle->transmit) {
303 die_null_ptr("transmit", "other_if_handle->transmit");
304 }
305 other_if_handle->transmit(other_if_handle);
306 }
307
308 virtual ~fss_data_binder() {}
309};
310
311class fss_control_binder : public fss_binder_base<fssControlIf>
312{
313public:
316 {
317 }
318
321 {
322 }
323
324 fssString do_command(fssString cmd)
325 {
326 if (!other_if_handle) {
327 die_null_ptr("do_command", "other_if_handle");
328 }
329 if (!other_if_handle->doCommand) {
330 die_null_ptr("do_command", "other_if_handle->doCommand");
331 }
332 return other_if_handle->doCommand(other_if_handle, cmd);
333 }
334
335 virtual ~fss_control_binder() {}
336};
337
338} // namespace gs
339
340#endif
Definition target.h:160
Definition fss_utils.h:74
Definition fss_utils.h:48
Definition fss_utils.h:312
Definition fss_utils.h:214
Definition fss_utils.h:160
Definition fss_utils.h:187
Tool which reads a Lua configuration file and sets parameters.
Definition biflow.cc:10
PARENT_TYPE * container_of(const MEMBER_TYPE *member, const MEMBER_TYPE PARENT_TYPE::*member_ptr)
Definition fss_utils.h:30
Time window struct declaration 'from' and 'to' members are 64 floating point types to represent time ...
Definition fss_interfaces.h:63
Bind interface encapsulates the functions to bind different interfaces.
Definition fss_interfaces.h:368
Bus Interface encapsulates the data which can be transmitted on a bus.
Definition fss_interfaces.h:321
Control interface encapsulates the function to inject and query parameters.
Definition fss_interfaces.h:360
Events interface encapsulates the function to handle simulation events.
Definition fss_interfaces.h:344
Time synchronization interface encapsulates the function to handle time window update.
Definition fss_interfaces.h:352