qualcomm/qbox
Loading...
Searching...
No Matches
registers.h
1/*
2 * Copyright (c) 2022-2024 Qualcomm Innovation Center, Inc. All Rights Reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#ifndef GS_REGISTERS_H
8#define GS_REGISTERS_H
9
10#include <type_traits>
11
12#include <systemc>
13#include <cci_configuration>
14#include <tlm>
15#include <tlm_utils/simple_target_socket.h>
16#include <tlm_utils/simple_initiator_socket.h>
17#include <tlm_utils/multi_passthrough_target_socket.h>
18#include <scp/report.h>
19#include <cciutils.h>
20#include <tlm_sockets_buswidth.h>
21#include <vector>
22#include <algorithm>
23#include <limits>
24#include <bitset>
25#include <initializer_list>
26#include <optional>
27#include <string>
28#include <unordered_map>
29#include <utility>
30
31namespace gs {
32
33template <typename T>
34static T gs_full_mask()
35{
36 return static_cast<T>(std::bitset<std::numeric_limits<T>::digits>(0).flip().to_ullong());
37}
38
39template <typename T>
40static T gs_mask_for_field(uint32_t start, uint32_t length)
41{
42 constexpr uint32_t width = std::numeric_limits<T>::digits;
43 if (length == 0 || start >= width || length > width - start) {
44 sc_assert(false);
45 return 0;
46 }
47 if (length == width) return gs_full_mask<T>();
48 return static_cast<T>(gs_full_mask<T>() >> (width - length));
49}
50
52template <class ELEM = uint32_t>
53struct gs_bank_of {
54 using elem_type = ELEM;
55 static_assert(std::is_unsigned<ELEM>::value, "Bank element type must be unsigned");
56};
57
58template <class>
59struct is_gs_bank : std::false_type {
60};
61template <class E>
62struct is_gs_bank<gs_bank_of<E>> : std::true_type {
63};
64
67 uint64_t index; // flat row-major index
68 uint64_t byte_offset; // addr - base
69 std::vector<uint64_t> indices; // per-dim, outer to inner
70 bool aligned;
71 bool in_range;
72 explicit operator bool() const { return aligned && in_range; }
73};
74
79class tlm_fnct
80{
81public:
82 typedef std::function<void(tlm::tlm_generic_payload&, sc_core::sc_time&)> TLMFUNC;
83
84private:
85 TLMFUNC m_fnct;
86
87public:
88 tlm_fnct(tlm_fnct&) = delete;
89 tlm_fnct(TLMFUNC cb): m_fnct(cb) {}
90 void operator()(tlm::tlm_generic_payload& txn, sc_core::sc_time& delay) { m_fnct(txn, delay); }
91 virtual void dummy() {} // force polymorphism
92};
93
99class port_fnct : public tlm_utils::simple_target_socket<port_fnct, DEFAULT_TLM_BUSWIDTH>
100{
101 SCP_LOGGER((), "register");
102 std::vector<std::shared_ptr<tlm_fnct>> m_pre_read_fncts;
103 std::vector<std::shared_ptr<tlm_fnct>> m_pre_write_fncts;
104 std::vector<std::shared_ptr<tlm_fnct>> m_post_read_fncts;
105 std::vector<std::shared_ptr<tlm_fnct>> m_post_write_fncts;
106 cci::cci_param<bool> p_is_callback;
107 bool m_in_callback = false;
108
109 std::function<unsigned int(tlm::tlm_generic_payload&)> transport_dbg_func;
110
111 std::string my_name()
112 {
113 std::string n(name());
114 n = n.substr(0, n.length() - strlen("_target_socket")); // get rid of last part of string
115 n = n.substr(n.find_last_of(".") + 1); // take the last part.
116 return n;
117 }
118
119 /* to be implemented !!!!*/
120 unsigned int transport_dbg(tlm::tlm_generic_payload& txn)
121 {
122 if (transport_dbg_func) {
123 return transport_dbg_func(txn);
124 } else
125 return 0;
126 }
127
128 void b_transport(tlm::tlm_generic_payload& txn, sc_core::sc_time& delay)
129 {
130 if (m_pre_read_fncts.size() || m_pre_write_fncts.size() || m_post_read_fncts.size() ||
131 m_post_write_fncts.size()) {
132 if (!m_in_callback) {
133 m_in_callback = true;
134 switch (txn.get_response_status()) {
135 case tlm::TLM_INCOMPLETE_RESPONSE:
136 switch (txn.get_command()) {
137 case tlm::TLM_READ_COMMAND:
138 if (m_pre_read_fncts.size()) {
139 SCP_INFO(())("Pre-Read callbacks: {}", my_name());
140 for (auto& cb : m_pre_read_fncts) (*cb)(txn, delay);
141 }
142 break;
143 case tlm::TLM_WRITE_COMMAND:
144 if (m_pre_write_fncts.size()) {
145 SCP_INFO(())("Pre-Write callbacks: {}", my_name());
146 for (auto& cb : m_pre_write_fncts) (*cb)(txn, delay);
147 }
148 break;
149 default:
150 break;
151 }
152 capture_txn_pre(txn);
153 break;
154 case tlm::TLM_OK_RESPONSE:
155 handle_mask_post(txn);
156 switch (txn.get_command()) {
157 case tlm::TLM_READ_COMMAND:
158 if (m_post_read_fncts.size()) {
159 SCP_INFO(())("Post-Read callbacks: {}", my_name());
160 for (auto& cb : m_post_read_fncts) (*cb)(txn, delay);
161 }
162 break;
163 case tlm::TLM_WRITE_COMMAND:
164 if (m_post_write_fncts.size()) {
165 SCP_INFO(())("Post-Write callbacks: {}", my_name());
166 for (auto& cb : m_post_write_fncts) (*cb)(txn, delay);
167 }
168 break;
169 default:
170 break;
171 }
172 break;
173 default:
174 break;
175 }
176 m_in_callback = false;
177 }
178 }
179 txn.set_dmi_allowed(false);
180 }
181
182 /*template <typename T>
183 void is_a_do(std::shared_ptr<tlm_fnct> cb, std::function<void()> fn)
184 {
185 auto cbt = dynamic_cast<T*>(cb.get());
186 if (cbt) fn();
187 }*/
188
189public:
190 void pre_read(tlm_fnct::TLMFUNC cb) { m_pre_read_fncts.push_back(std::make_shared<tlm_fnct>(cb)); }
191 void pre_write(tlm_fnct::TLMFUNC cb) { m_pre_write_fncts.push_back(std::make_shared<tlm_fnct>(cb)); }
192 void post_read(tlm_fnct::TLMFUNC cb) { m_post_read_fncts.push_back(std::make_shared<tlm_fnct>(cb)); }
193 void post_write(tlm_fnct::TLMFUNC cb) { m_post_write_fncts.push_back(std::make_shared<tlm_fnct>(cb)); }
194 virtual void capture_txn_pre(tlm::tlm_generic_payload& txn) = 0;
195 virtual void handle_mask_post(tlm::tlm_generic_payload& txn) = 0;
196
197 port_fnct() = delete;
198 port_fnct(std::string name, std::string path_name)
199 : simple_target_socket<port_fnct, DEFAULT_TLM_BUSWIDTH>((name + "_target_socket").c_str())
200 , p_is_callback(path_name + ".target_socket.is_callback", true, "Is a callback (true)")
201 {
202 SCP_LOGGER_NAME().features[0] = parent(sc_core::sc_object::name()) + "." + name;
203 SCP_TRACE(())("Constructor");
204 p_is_callback = true;
205 p_is_callback.lock();
206 tlm_utils::simple_target_socket<port_fnct, DEFAULT_TLM_BUSWIDTH>::register_b_transport(this,
207 &port_fnct::b_transport);
208 tlm_utils::simple_target_socket<port_fnct, DEFAULT_TLM_BUSWIDTH>::register_transport_dbg(
209 this, &port_fnct::transport_dbg);
210 }
211
212 void register_transport_dbg_func(std::function<unsigned int(tlm::tlm_generic_payload&)> fn)
213 {
214 transport_dbg_func = fn;
215 }
216
217protected:
218 std::string parent(std::string name) { return name.substr(0, name.find_last_of('.')); }
219};
220
225template <class TYPE>
226class proxy_data_array
227{
228 scp::scp_logger_cache& SCP_LOGGER_NAME();
229 TYPE* m_dmi = nullptr;
230
231 void check_dmi()
232 {
233 tlm::tlm_generic_payload m_txn;
234 tlm::tlm_dmi m_dmi_data;
235 const uint64_t span = p_size.get_value();
236 m_txn.set_byte_enable_length(0);
237 m_txn.set_dmi_allowed(false);
238 m_txn.set_command(tlm::TLM_IGNORE_COMMAND);
239 m_txn.set_data_ptr(nullptr);
240 m_txn.set_address(p_offset);
241 m_txn.set_data_length(span);
242 m_txn.set_streaming_width(span);
243 m_txn.set_response_status(tlm::TLM_INCOMPLETE_RESPONSE);
244 if (initiator_socket->get_direct_mem_ptr(m_txn, m_dmi_data)) {
245 uint64_t start = m_dmi_data.get_start_address();
246 unsigned char* ptr = m_dmi_data.get_dmi_ptr();
247 ptr += (p_offset - start);
248 sc_assert(m_dmi_data.get_end_address() >= start + p_offset + span);
249 m_dmi = reinterpret_cast<TYPE*>(ptr);
250 }
251 }
252
253 uint8_t* dmi_bytes_at(uint64_t idx) { return reinterpret_cast<uint8_t*>(m_dmi) + p_stride.get_value() * idx; }
254
255public:
256 std::string m_path_name;
257 cci::cci_param<uint64_t> p_number;
258 cci::cci_param<uint64_t> p_offset;
259 cci::cci_param<uint64_t> p_size;
260 cci::cci_param<uint64_t> p_stride;
261 cci::cci_param<uint64_t> p_elem_size;
262 cci::cci_param<TYPE> p_mask;
263 cci::cci_param<bool> p_relative_addresses;
264
265 tlm_utils::simple_initiator_socket<proxy_data_array, DEFAULT_TLM_BUSWIDTH> initiator_socket;
266
267 void get(TYPE* dst, uint64_t idx = 0, uint64_t length = 1)
268 {
269 sc_assert(idx + length <= p_number);
270 const uint64_t stride = p_stride.get_value();
271 if (m_dmi) {
272 for (uint64_t i = 0; i < length; i++) memcpy(&dst[i], dmi_bytes_at(idx + i), sizeof(TYPE));
273 SCP_TRACE(())("Got value (DMI) : [{:#x}]", fmt::join(std::vector<TYPE>(&dst[0], &dst[length]), ","));
274 } else if (length == 1 || stride == sizeof(TYPE)) {
275 tlm::tlm_generic_payload m_txn;
276 sc_core::sc_time dummy;
277 m_txn.set_byte_enable_length(0);
278 m_txn.set_dmi_allowed(false);
279 m_txn.set_command(tlm::TLM_READ_COMMAND);
280 m_txn.set_data_ptr(reinterpret_cast<unsigned char*>(dst));
281 m_txn.set_address(p_offset + stride * idx);
282 m_txn.set_data_length(sizeof(TYPE) * length);
283 m_txn.set_streaming_width(sizeof(TYPE) * length);
284 m_txn.set_response_status(tlm::TLM_INCOMPLETE_RESPONSE);
285 initiator_socket->b_transport(m_txn, dummy);
286 sc_assert(m_txn.get_response_status() == tlm::TLM_OK_RESPONSE);
287 SCP_TRACE(())("Got value (transport) : [{:#x}]", fmt::join(std::vector<TYPE>(&dst[0], &dst[length]), ","));
288 if (m_txn.is_dmi_allowed()) check_dmi();
289 } else {
290 // gapped bank: one transaction per element
291 for (uint64_t i = 0; i < length; i++) {
292 tlm::tlm_generic_payload m_txn;
293 sc_core::sc_time dummy;
294 m_txn.set_byte_enable_length(0);
295 m_txn.set_dmi_allowed(false);
296 m_txn.set_command(tlm::TLM_READ_COMMAND);
297 m_txn.set_data_ptr(reinterpret_cast<unsigned char*>(&dst[i]));
298 m_txn.set_address(p_offset + stride * (idx + i));
299 m_txn.set_data_length(sizeof(TYPE));
300 m_txn.set_streaming_width(sizeof(TYPE));
301 m_txn.set_response_status(tlm::TLM_INCOMPLETE_RESPONSE);
302 initiator_socket->b_transport(m_txn, dummy);
303 sc_assert(m_txn.get_response_status() == tlm::TLM_OK_RESPONSE);
304 if (m_txn.is_dmi_allowed()) check_dmi();
305 }
306 SCP_TRACE(())
307 ("Got value (transport strided) : [{:#x}]", fmt::join(std::vector<TYPE>(&dst[0], &dst[length]), ","));
308 }
309 }
310
311 void set(TYPE* src, uint64_t idx = 0, uint64_t length = 1, bool use_mask = true)
312 {
313 sc_assert(idx + length <= p_number);
314 const uint64_t stride = p_stride.get_value();
315 const bool full_mask = (p_mask.get_value() == gs_full_mask<TYPE>());
316 if (m_dmi) {
317 SCP_TRACE(())("Set value (DMI) : [{:#x}]", fmt::join(std::vector<TYPE>(&src[0], &src[length]), ","));
318 if (!use_mask || full_mask) {
319 for (uint64_t i = 0; i < length; i++) memcpy(dmi_bytes_at(idx + i), &src[i], sizeof(TYPE));
320 } else {
321 for (uint64_t i = 0; i < length; i++) {
322 TYPE cur;
323 memcpy(&cur, dmi_bytes_at(idx + i), sizeof(TYPE));
324 write_with_mask(&src[i], &cur, 1);
325 memcpy(dmi_bytes_at(idx + i), &cur, sizeof(TYPE));
326 }
327 }
328 } else if ((length == 1 || stride == sizeof(TYPE)) && (full_mask || !use_mask)) {
329 tlm::tlm_generic_payload m_txn;
330 sc_core::sc_time dummy;
331 m_txn.set_data_ptr(reinterpret_cast<unsigned char*>(src));
332 m_txn.set_byte_enable_length(0);
333 m_txn.set_dmi_allowed(false);
334 m_txn.set_command(tlm::TLM_WRITE_COMMAND);
335 m_txn.set_address(p_offset + stride * idx);
336 m_txn.set_data_length(sizeof(TYPE) * length);
337 m_txn.set_streaming_width(sizeof(TYPE) * length);
338 m_txn.set_response_status(tlm::TLM_INCOMPLETE_RESPONSE);
339 SCP_TRACE(())("Set value (transport) : [{:#x}]", fmt::join(std::vector<TYPE>(&src[0], &src[length]), ","));
340 initiator_socket->b_transport(m_txn, dummy);
341 sc_assert(m_txn.get_response_status() == tlm::TLM_OK_RESPONSE);
342 if (m_txn.is_dmi_allowed()) check_dmi();
343 } else {
344 for (uint64_t i = 0; i < length; i++) {
345 tlm::tlm_generic_payload m_txn;
346 sc_core::sc_time dummy;
347 std::vector<unsigned char> curr_data;
348 unsigned char* data_ptr;
349 if (!use_mask || full_mask) {
350 data_ptr = reinterpret_cast<unsigned char*>(&src[i]);
351 } else {
352 curr_data.resize(sizeof(TYPE));
353 get(reinterpret_cast<TYPE*>(curr_data.data()), idx + i, 1);
354 TYPE tmp = src[i];
355 write_with_mask(&tmp, reinterpret_cast<TYPE*>(curr_data.data()), 1);
356 data_ptr = curr_data.data();
357 }
358 m_txn.set_data_ptr(data_ptr);
359 m_txn.set_byte_enable_length(0);
360 m_txn.set_dmi_allowed(false);
361 m_txn.set_command(tlm::TLM_WRITE_COMMAND);
362 m_txn.set_address(p_offset + stride * (idx + i));
363 m_txn.set_data_length(sizeof(TYPE));
364 m_txn.set_streaming_width(sizeof(TYPE));
365 m_txn.set_response_status(tlm::TLM_INCOMPLETE_RESPONSE);
366 initiator_socket->b_transport(m_txn, dummy);
367 sc_assert(m_txn.get_response_status() == tlm::TLM_OK_RESPONSE);
368 if (m_txn.is_dmi_allowed()) check_dmi();
369 }
370 SCP_TRACE(())
371 ("Set value (transport strided) : [{:#x}]", fmt::join(std::vector<TYPE>(&src[0], &src[length]), ","));
372 }
373 }
374
375 void write_with_mask(TYPE* src, TYPE* dst, uint64_t length)
376 {
377 write_with_mask(src, dst, length, p_mask.get_value());
378 }
379
380 void write_with_mask(TYPE* src, TYPE* dst, uint64_t length, TYPE mask)
381 {
382 if (!src) {
383 SCP_FATAL(())("write_with_mask(): src pointer is NULL");
384 }
385 if (!dst) {
386 SCP_FATAL(())("write_with_mask(): dst pointer is NULL");
387 }
388 std::vector<TYPE> mask_to_apply(length, mask);
389 std::vector<TYPE> new_and_mask(length, 0); // result of *src & mask
390 std::vector<TYPE> old_and_not_mask(length, 0); // result of *dmi & ~mask
391 std::transform(src, src + length, mask_to_apply.cbegin(), new_and_mask.begin(),
392 [](TYPE src_val, TYPE mask_val) { return src_val & mask_val; });
393 std::transform(dst, dst + length, mask_to_apply.cbegin(), old_and_not_mask.begin(),
394 [](TYPE curr_val, TYPE mask_val) { return curr_val & (~mask_val); });
395 std::transform(
396 new_and_mask.cbegin(), new_and_mask.cend(), old_and_not_mask.cbegin(), dst,
397 [](TYPE new_and_mask_val, TYPE old_and_not_mask_val) { return new_and_mask_val | old_and_not_mask_val; });
398 }
399
400 TYPE& operator[](int idx)
401 {
402 if (!m_dmi) {
403 check_dmi();
404 if (!m_dmi) {
405 SCP_FATAL(())("Operator[] can only be used for DMI'able registers");
406 }
407 }
408 SCP_TRACE(())("Access value (DMI) using operator [] at idx {}", idx);
409 if (p_mask.get_value() != gs_full_mask<TYPE>()) {
410 SCP_FATAL(()) << "operator[](): Register Mask: 0x" << std::hex << p_mask.get_value()
411 << " has readonly bits, please use set(TYPE* src, uint64_t idx, uint64_t length) and "
412 "get(TYPE* dst, uint64_t idx, uint64_t length) instead";
413 }
414 return *reinterpret_cast<TYPE*>(dmi_bytes_at(static_cast<uint64_t>(idx)));
415 }
416
418 void get_bytes(TYPE* dst, uint64_t byte_offset)
419 {
420 if (m_dmi) {
421 memcpy(dst, reinterpret_cast<uint8_t*>(m_dmi) + byte_offset, sizeof(TYPE));
422 } else {
423 tlm::tlm_generic_payload m_txn;
424 sc_core::sc_time dummy;
425 m_txn.set_byte_enable_length(0);
426 m_txn.set_dmi_allowed(false);
427 m_txn.set_command(tlm::TLM_READ_COMMAND);
428 m_txn.set_data_ptr(reinterpret_cast<unsigned char*>(dst));
429 m_txn.set_address(p_offset + byte_offset);
430 m_txn.set_data_length(sizeof(TYPE));
431 m_txn.set_streaming_width(sizeof(TYPE));
432 m_txn.set_response_status(tlm::TLM_INCOMPLETE_RESPONSE);
433 initiator_socket->b_transport(m_txn, dummy);
434 sc_assert(m_txn.get_response_status() == tlm::TLM_OK_RESPONSE);
435 if (m_txn.is_dmi_allowed()) check_dmi();
436 }
437 }
438
440 void set_bytes(TYPE* src, uint64_t byte_offset, bool use_mask = true)
441 {
442 const TYPE mask = mask_for_byte_offset(byte_offset);
443 const bool full_mask = (mask == gs_full_mask<TYPE>());
444 if (m_dmi) {
445 if (!use_mask || full_mask) {
446 memcpy(reinterpret_cast<uint8_t*>(m_dmi) + byte_offset, src, sizeof(TYPE));
447 } else {
448 TYPE cur;
449 memcpy(&cur, reinterpret_cast<uint8_t*>(m_dmi) + byte_offset, sizeof(TYPE));
450 write_with_mask(src, &cur, 1, mask);
451 memcpy(reinterpret_cast<uint8_t*>(m_dmi) + byte_offset, &cur, sizeof(TYPE));
452 }
453 } else {
454 tlm::tlm_generic_payload m_txn;
455 sc_core::sc_time dummy;
456 std::vector<unsigned char> curr_data;
457 unsigned char* data_ptr;
458 if (!use_mask || full_mask) {
459 data_ptr = reinterpret_cast<unsigned char*>(src);
460 } else {
461 curr_data.resize(sizeof(TYPE));
462 get_bytes(reinterpret_cast<TYPE*>(curr_data.data()), byte_offset);
463 TYPE tmp = *src;
464 write_with_mask(&tmp, reinterpret_cast<TYPE*>(curr_data.data()), 1, mask);
465 data_ptr = curr_data.data();
466 }
467 m_txn.set_data_ptr(data_ptr);
468 m_txn.set_byte_enable_length(0);
469 m_txn.set_dmi_allowed(false);
470 m_txn.set_command(tlm::TLM_WRITE_COMMAND);
471 m_txn.set_address(p_offset + byte_offset);
472 m_txn.set_data_length(sizeof(TYPE));
473 m_txn.set_streaming_width(sizeof(TYPE));
474 m_txn.set_response_status(tlm::TLM_INCOMPLETE_RESPONSE);
475 initiator_socket->b_transport(m_txn, dummy);
476 sc_assert(m_txn.get_response_status() == tlm::TLM_OK_RESPONSE);
477 if (m_txn.is_dmi_allowed()) check_dmi();
478 }
479 }
480
481 void invalidate_direct_mem_ptr(sc_dt::uint64 start, sc_dt::uint64 end) { m_dmi = nullptr; }
482
483protected:
484 virtual bool has_element_masks() const { return false; }
485 virtual TYPE mask_for_element(uint64_t index) const { return p_mask.get_value(); }
486 virtual TYPE mask_for_byte_offset(uint64_t byte_offset) const { return p_mask.get_value(); }
487
488public:
489 proxy_data_array(scp::scp_logger_cache& logger, std::string name, std::string path_name, uint64_t _offset = 0,
490 uint64_t number = 1, TYPE mask = gs_full_mask<TYPE>(), uint64_t stride = sizeof(TYPE),
491 uint64_t total_span_override = 0)
492 : SCP_LOGGER_NAME()(logger)
493 , m_path_name(path_name)
494 , p_number(path_name + ".number", number, "number of elements in this register")
495 , initiator_socket((name + "_initiator_socket").c_str())
496 , p_offset(path_name + ".target_socket.address", _offset, "Offset of this register")
497 , p_size(path_name + ".target_socket.size",
498 (total_span_override != 0 ? total_span_override
499 : (number == 0 ? 0 : stride * (number - 1) + sizeof(TYPE))),
500 "size of this register")
501 , p_stride(path_name + ".target_socket.stride", stride, "stride in bytes between elements")
502 , p_elem_size(path_name + ".target_socket.elem_size", sizeof(TYPE), "size in bytes of one element")
503 , p_mask(path_name + ".target_socket.mask", mask, " R/W mask, 0 means read only, and 1 means write/read")
504 , p_relative_addresses(path_name + ".target_socket.relative_addresses", true,
505 "allow relative_addresses for this register")
506 {
507 initiator_socket.register_invalidate_direct_mem_ptr(this,
508 &gs::proxy_data_array<TYPE>::invalidate_direct_mem_ptr);
509 }
510};
511
516template <class TYPE = uint32_t>
517class proxy_data : public proxy_data_array<TYPE>
518{
519 scp::scp_logger_cache& SCP_LOGGER_NAME();
520
521public:
522 TYPE get()
523 {
524 TYPE tmp;
525 proxy_data_array<TYPE>::get(&tmp);
526 return tmp;
527 }
528 operator TYPE() { return get(); }
529
530 void set(TYPE value) { proxy_data_array<TYPE>::set(&value); }
531 void operator=(TYPE value) { set(value); }
532
533 proxy_data(scp::scp_logger_cache& logger, std::string name, std::string path_name, uint64_t offset = 0,
534 uint64_t number = 1, uint64_t start = 0, uint64_t length = sizeof(TYPE) * 8,
535 TYPE mask = gs_full_mask<TYPE>())
536 : proxy_data_array<TYPE>(logger, name, path_name, offset, number, mask), SCP_LOGGER_NAME()(logger)
537 {
538 static_assert(std::is_unsigned<TYPE>::value, "Register types must be unsigned");
539 }
540};
541
542/* forward declaration */
543template <class TYPE = uint32_t>
544class gs_bitfield;
545template <class TYPE = uint32_t>
546class gs_field;
547template <class TYPE>
554template <class TYPE = uint32_t>
555class gs_register : public port_fnct, public proxy_data<TYPE>
556{
557 SCP_LOGGER((), "register");
558
559private:
560 std::string m_regname;
561 std::string m_path;
562 std::vector<unsigned char> captured_txn_data;
563
564public:
565 gs_register() = delete;
566 gs_register(std::string _name, std::string path = "", uint64_t offset = 0, uint64_t number = 1,
567 TYPE mask = gs_full_mask<TYPE>())
568 : m_regname(_name)
569 , m_path(path)
570 , port_fnct(_name, path)
571 , proxy_data<TYPE>(SCP_LOGGER_NAME(), _name, path, offset, number, mask)
572 {
573 std::string n(name());
574 SCP_LOGGER_NAME().features[0] = parent(n) + "." + _name;
575 n = n.substr(0, n.length() - strlen("_target_socket")); // get rid of last part of string
576 SCP_TRACE((), n)("constructor : {} attching in {}", _name, path);
577
578 register_transport_dbg_func([&](tlm::tlm_generic_payload& txn) {
579 if (txn.get_data_length() < sizeof(TYPE)) return static_cast<unsigned int>(0);
580 unsigned char* data = txn.get_data_ptr();
581 if (txn.get_command() == tlm::TLM_READ_COMMAND) {
582 TYPE tmp = proxy_data<TYPE>::get();
583 memset(data, 0, txn.get_data_length());
584 memcpy(data, &tmp, sizeof(TYPE));
585 return static_cast<unsigned int>(sizeof(TYPE));
586 }
587 if (txn.get_command() == tlm::TLM_WRITE_COMMAND) {
588 TYPE tmp;
589 memcpy(&tmp, data, sizeof(TYPE));
590 proxy_data<TYPE>::set(tmp);
591 return static_cast<unsigned int>(sizeof(TYPE));
592 }
593 return static_cast<unsigned int>(0);
594 });
595 }
596 void operator=(TYPE value) { proxy_data<TYPE>::set(value); }
597 operator TYPE() { return proxy_data<TYPE>::get(); }
598
599 void operator=(gs_register<TYPE>& other) { proxy_data<TYPE>::set(proxy_data<TYPE>::get()); }
600 void operator+=(TYPE other) { proxy_data<TYPE>::set(proxy_data<TYPE>::get() + other); }
601 void operator-=(TYPE other) { proxy_data<TYPE>::set(proxy_data<TYPE>::get() - other); }
602 void operator/=(TYPE other)
603 {
604 if (other == 0) SCP_FATAL(())("Trying to divide a register value by 0!");
605 proxy_data<TYPE>::set(proxy_data<TYPE>::get() / other);
606 }
607 void operator*=(TYPE other) { proxy_data<TYPE>::set(proxy_data<TYPE>::get() * other); }
608 void operator&=(TYPE other) { proxy_data<TYPE>::set(proxy_data<TYPE>::get() & other); }
609 void operator|=(TYPE other) { proxy_data<TYPE>::set(proxy_data<TYPE>::get() | other); }
610 void operator^=(TYPE other) { proxy_data<TYPE>::set(proxy_data<TYPE>::get() ^ other); }
611 void operator<<=(TYPE other) { proxy_data<TYPE>::set(proxy_data<TYPE>::get() << other); }
612 void operator>>=(TYPE other) { proxy_data<TYPE>::set(proxy_data<TYPE>::get() >> other); }
613
614 gs_register_element<TYPE> operator[](int idx);
615 void get(TYPE* dst, uint64_t idx, uint64_t length) { return proxy_data_array<TYPE>::get(dst, idx, length); }
616 void set(TYPE* src, uint64_t idx, uint64_t length, bool use_mask = true)
617 {
618 return proxy_data_array<TYPE>::set(src, idx, length, use_mask);
619 }
620
621 gs_bitfield<TYPE> operator[](gs_field<TYPE>& f) { return gs_bitfield<TYPE>(*this, f); }
622 std::string get_regname() const { return m_regname; }
623 std::string get_path() const { return m_path; }
624 uint64_t get_offset() const { return proxy_data<TYPE>::p_offset.get_value(); }
625 uint64_t get_size() const { return proxy_data<TYPE>::p_size.get_value(); }
626 void set_mask(TYPE mask) // mask is allowed to be set to implement e.g. write-once semantics
627 {
628 SCP_TRACE(()) << "Set Mask to 0x" << std::hex << mask;
629 proxy_data<TYPE>::p_mask = mask;
630 }
631 TYPE get_mask() const { return proxy_data<TYPE>::p_mask.get_value(); }
632 void capture_txn_pre(tlm::tlm_generic_payload& txn) override
633 {
634 if ((proxy_data<TYPE>::p_mask.get_value() == gs_full_mask<TYPE>()) ||
635 (txn.get_command() != tlm::tlm_command::TLM_WRITE_COMMAND)) {
636 return;
637 }
638 unsigned int txn_data_len = txn.get_data_length();
639 if ((txn_data_len == 0) || (txn_data_len % sizeof(TYPE)))
640 SCP_FATAL(()) << "capture_txn_pre(): txn data length should be n * sizeof(TYPE) where n >= 1";
641 uint64_t txn_addr = txn.get_address();
642 const uint64_t stride = proxy_data<TYPE>::p_stride.get_value();
643 uint64_t idx = 0;
644 if (proxy_data<TYPE>::p_relative_addresses.get_value()) {
645 idx = txn_addr / stride;
646 } else {
647 idx = (txn_addr - proxy_data<TYPE>::p_offset.get_value()) / stride;
648 }
649 captured_txn_data.clear();
650 captured_txn_data.resize(txn_data_len);
651 get(reinterpret_cast<TYPE*>(captured_txn_data.data()), idx, txn_data_len / sizeof(TYPE));
652 }
653 void handle_mask_post(tlm::tlm_generic_payload& txn) override
654 {
655 if ((proxy_data<TYPE>::p_mask.get_value() == gs_full_mask<TYPE>()) ||
656 (txn.get_command() != tlm::tlm_command::TLM_WRITE_COMMAND)) {
657 return;
658 }
659 unsigned int txn_data_len = txn.get_data_length();
660 if ((txn_data_len == 0) || (txn_data_len % sizeof(TYPE)))
661 SCP_FATAL(()) << "handle_mask_post(): txn data length should be n * sizeof(TYPE) where n >= 1";
662 uint64_t txn_addr = txn.get_address();
663 const uint64_t stride = proxy_data<TYPE>::p_stride.get_value();
664 uint64_t idx = 0;
665 if (proxy_data<TYPE>::p_relative_addresses.get_value()) {
666 idx = txn_addr / stride;
667 } else {
668 idx = (txn_addr - proxy_data<TYPE>::p_offset.get_value()) / stride;
669 }
670 unsigned char* txn_data_ptr = txn.get_data_ptr();
671 proxy_data<TYPE>::write_with_mask(reinterpret_cast<TYPE*>(txn_data_ptr),
672 reinterpret_cast<TYPE*>(captured_txn_data.data()),
673 txn_data_len / sizeof(TYPE));
674 set(reinterpret_cast<TYPE*>(captured_txn_data.data()), idx, txn_data_len / sizeof(TYPE), false);
675 memcpy(txn_data_ptr, captured_txn_data.data(), txn_data_len);
676 }
677};
678
683template <class TYPE>
684class gs_bitfield
685{
686 uint32_t start, length;
687 gs_register<TYPE>& m_reg;
688
689public:
690 gs_bitfield(gs_register<TYPE>& r, uint32_t s, uint32_t l): m_reg(r), start(s), length(l) {}
691
692 gs_bitfield(gs_register<TYPE>& r, gs_bitfield& f): m_reg(r), start(f.start), length(f.length) {}
693
694 void operator=(TYPE value)
695 {
696 TYPE field_mask = gs_mask_for_field<TYPE>(start, length);
697 sc_assert((value & ~field_mask) == 0);
698 TYPE mask = static_cast<TYPE>(field_mask << start);
699 m_reg &= static_cast<TYPE>(~mask);
700 m_reg |= static_cast<TYPE>((value & field_mask) << start);
701 }
702 operator TYPE()
703 {
704 TYPE field_mask = gs_mask_for_field<TYPE>(start, length);
705 return static_cast<TYPE>((m_reg >> start) & field_mask);
706 }
707};
708
714template <class TYPE>
716{
717 SCP_LOGGER();
718 std::string m_name;
719 uint32_t m_bit_start;
720 uint32_t m_bit_length;
721 std::optional<gs_bitfield<TYPE>> m_bitfield;
722
723 void require_bound() const
724 {
725 if (!m_bitfield) {
726 SCP_FATAL(())("Field {} is metadata-only and is not attached to a scalar register", m_name);
727 }
728 }
729
730public:
736 template <class REG>
737 gs_field(REG& reg, std::string name, uint32_t bit_start = 0, uint32_t bit_length = sizeof(TYPE) * 8)
738 : m_name(std::move(name)), m_bit_start(bit_start), m_bit_length(bit_length), m_bitfield(std::nullopt)
739 {
740 using REG_TYPE = std::remove_reference_t<REG>;
741 static_assert(std::is_same<REG_TYPE, gs_register<TYPE>>::value ||
742 std::is_same<REG_TYPE, gs_register<gs_bank_of<TYPE>>>::value,
743 "gs_field must be constructed with gs_register<T> or gs_register<gs_bank_of<T>>");
744 SCP_TRACE(())("gs_field constructor");
745 if (m_bit_length == 0) SCP_FATAL(())("Can't find bit length for {}", m_name);
746 if constexpr (std::is_same<REG_TYPE, gs_register<TYPE>>::value) {
747 m_bitfield.emplace(reg, m_bit_start, m_bit_length);
748 }
749 }
750
751 void operator=(TYPE value)
752 {
753 require_bound();
754 *m_bitfield = value;
755 }
756 void operator=(gs_field<TYPE>& value)
757 {
758 require_bound();
759 *m_bitfield = static_cast<TYPE>(value);
760 }
761
762 operator TYPE()
763 {
764 require_bound();
765 return static_cast<TYPE>(*m_bitfield);
766 }
767
768 operator gs::gs_bitfield<TYPE>&()
769 {
770 require_bound();
771 return *m_bitfield;
772 }
773
774 uint32_t bit_start() const { return m_bit_start; }
775 uint32_t bit_length() const { return m_bit_length; }
776};
777
778template <class TYPE>
779class gs_register_element
780{
781 TYPE& m_ref;
782
783 class field_proxy
784 {
785 TYPE& m_ref;
786 uint32_t m_start, m_length;
787
788 public:
789 field_proxy(TYPE& ref, uint32_t start, uint32_t length): m_ref(ref), m_start(start), m_length(length) {}
790 operator TYPE() const
791 {
792 TYPE field_mask = gs_mask_for_field<TYPE>(m_start, m_length);
793 return static_cast<TYPE>((m_ref >> m_start) & field_mask);
794 }
795 field_proxy& operator=(TYPE value)
796 {
797 TYPE field_mask = gs_mask_for_field<TYPE>(m_start, m_length);
798 TYPE mask = static_cast<TYPE>(field_mask << m_start);
799 m_ref = static_cast<TYPE>((m_ref & ~mask) | ((value & field_mask) << m_start));
800 return *this;
801 }
802 };
803
804public:
805 gs_register_element(TYPE& ref): m_ref(ref) {}
806 operator TYPE&() { return m_ref; }
807 operator TYPE() const { return m_ref; }
808 TYPE* operator&() { return &m_ref; }
809 gs_register_element& operator=(TYPE val)
810 {
811 m_ref = val;
812 return *this;
813 }
814 field_proxy operator[](gs_field<TYPE>& f) { return field_proxy(m_ref, f.bit_start(), f.bit_length()); }
815};
816
817template <class TYPE>
818gs_register_element<TYPE> gs_register<TYPE>::operator[](int idx)
819{
821}
822
828template <class ELEM>
829class gs_register_bank_element
830{
831 SCP_LOGGER();
833 const std::vector<uint64_t>* m_strides;
834 const std::vector<uint64_t>* m_counts;
835 uint64_t m_byte_off;
836 size_t m_next_dim;
837
838 class bit_proxy
839 {
840 gs_register_bank_element& m_elem;
841 uint32_t m_start, m_length;
842
843 public:
844 bit_proxy(gs_register_bank_element& e, uint32_t s, uint32_t l): m_elem(e), m_start(s), m_length(l) {}
845 operator ELEM() const
846 {
847 ELEM field_mask = gs_mask_for_field<ELEM>(m_start, m_length);
848 return static_cast<ELEM>((static_cast<ELEM>(m_elem) >> m_start) & field_mask);
849 }
850 bit_proxy& operator=(ELEM value)
851 {
852 ELEM cur = m_elem;
853 ELEM field_mask = gs_mask_for_field<ELEM>(m_start, m_length);
854 ELEM mask = static_cast<ELEM>(field_mask << m_start);
855 m_elem = static_cast<ELEM>((cur & ~mask) | ((value & field_mask) << m_start));
856 return *this;
857 }
858 };
859
860 void require_full(const char* op) const
861 {
862 if (!is_full()) {
863 SCP_FATAL(())
864 ("operation '{}' requires a fully-resolved bank element; {} of {} dims specified", op, m_next_dim,
865 m_strides->size());
866 }
867 }
868
869public:
870 gs_register_bank_element(proxy_data_array<ELEM>& bank, const std::vector<uint64_t>& strides,
871 const std::vector<uint64_t>& counts, uint64_t byte_off, size_t next_dim)
872 : m_bank(bank), m_strides(&strides), m_counts(&counts), m_byte_off(byte_off), m_next_dim(next_dim)
873 {
874 }
875
876 bool is_full() const { return m_next_dim >= m_strides->size(); }
877 uint64_t byte_offset() const { return m_byte_off; }
878 size_t dims_resolved() const { return m_next_dim; }
879 size_t dims_total() const { return m_strides->size(); }
880
881 gs_register_bank_element operator[](size_t idx)
882 {
883 if (is_full()) {
884 SCP_FATAL(())("operator[](size_t) on fully-resolved bank element ({} dims already consumed)", m_next_dim);
885 }
886 if (idx >= (*m_counts)[m_next_dim]) {
887 SCP_FATAL(())("bank index {} at dim {} exceeds count {}", idx, m_next_dim, (*m_counts)[m_next_dim]);
888 }
889 return gs_register_bank_element(m_bank, *m_strides, *m_counts, m_byte_off + idx * (*m_strides)[m_next_dim],
890 m_next_dim + 1);
891 }
892
893 operator ELEM() const
894 {
895 require_full("read");
896 ELEM v;
897 const_cast<proxy_data_array<ELEM>&>(m_bank).get_bytes(&v, m_byte_off);
898 return v;
899 }
900
901 gs_register_bank_element& operator=(ELEM value)
902 {
903 require_full("assign");
904 m_bank.set_bytes(&value, m_byte_off);
905 return *this;
906 }
907 gs_register_bank_element& operator=(const gs_register_bank_element& other)
908 {
909 require_full("assign");
910 ELEM v = static_cast<ELEM>(other);
911 m_bank.set_bytes(&v, m_byte_off);
912 return *this;
913 }
914
915#define GS_BANK_ELEM_RMW(op, name) \
916 gs_register_bank_element& operator op##=(ELEM rhs) \
917 { \
918 require_full(name); \
919 ELEM v = static_cast<ELEM>(*this) op rhs; \
920 m_bank.set_bytes(&v, m_byte_off); \
921 return *this; \
922 }
923 GS_BANK_ELEM_RMW(+, "+=")
924 GS_BANK_ELEM_RMW(-, "-=")
925 GS_BANK_ELEM_RMW(*, "*=")
926 GS_BANK_ELEM_RMW(&, "&=")
927 GS_BANK_ELEM_RMW(|, "|=")
928 GS_BANK_ELEM_RMW(^, "^=")
929 GS_BANK_ELEM_RMW(<<, "<<=")
930 GS_BANK_ELEM_RMW(>>, ">>=")
931#undef GS_BANK_ELEM_RMW
932
933 gs_register_bank_element& operator/=(ELEM rhs)
934 {
935 require_full("/=");
936 if (rhs == 0) SCP_FATAL(())("division by zero on bank element");
937 ELEM v = static_cast<ELEM>(*this) / rhs;
938 m_bank.set_bytes(&v, m_byte_off);
939 return *this;
940 }
941
944 {
945 require_full("[gs_field]");
946 return bit_proxy(*this, f.bit_start(), f.bit_length());
947 }
948};
949
957template <class ELEM>
958class gs_register<gs_bank_of<ELEM>> : public port_fnct, public proxy_data_array<ELEM>
959{
960 SCP_LOGGER((), "register");
961
962 std::string m_regname;
963 std::string m_path;
964 std::vector<unsigned char> captured_txn_data;
965 std::unordered_map<uint64_t, ELEM> m_element_masks;
966
967 std::vector<uint64_t> m_dim_counts;
968 std::vector<uint64_t> m_dim_strides;
969 // m_row_major_div[k] = prod(counts[k+1..]); used to unravel flat indices.
970 std::vector<uint64_t> m_row_major_div;
971 uint64_t m_total_count = 0;
972 uint64_t m_total_span = 0;
973
974 static uint64_t compute_total_span(const std::vector<uint64_t>& counts, const std::vector<uint64_t>& strides)
975 {
976 if (counts.empty() || counts.size() != strides.size()) return 0;
977 uint64_t span = 0;
978 for (size_t k = 0; k < counts.size(); k++) {
979 if (counts[k] == 0) return 0;
980 span += (counts[k] - 1) * strides[k];
981 }
982 return span + sizeof(ELEM);
983 }
984
985 static uint64_t compute_total_count(const std::vector<uint64_t>& counts)
986 {
987 uint64_t c = 1;
988 for (auto v : counts) c *= v;
989 return c;
990 }
991
992 uint64_t flat_index_for_indices(const uint64_t* indices, size_t n) const
993 {
994 if (n != m_dim_counts.size())
995 SCP_FATAL(())("bank {} indexed with {} dims, expected {}", m_regname, n, m_dim_counts.size());
996 uint64_t flat = 0;
997 for (size_t k = 0; k < n; k++) {
998 if (indices[k] >= m_dim_counts[k])
999 SCP_FATAL(())("bank {} index {} at dim {} exceeds count {}", m_regname, indices[k], k, m_dim_counts[k]);
1000 flat += indices[k] * m_row_major_div[k];
1001 }
1002 return flat;
1003 }
1004
1005 bool try_flat_index_for_byte_offset(uint64_t byte_offset, uint64_t& flat) const
1006 {
1007 uint64_t remaining = byte_offset;
1008 flat = 0;
1009 for (size_t k = 0; k < m_dim_strides.size(); k++) {
1010 uint64_t idx = remaining / m_dim_strides[k];
1011 remaining %= m_dim_strides[k];
1012 if (idx >= m_dim_counts[k]) return false;
1013 flat += idx * m_row_major_div[k];
1014 }
1015 return remaining < sizeof(ELEM);
1016 }
1017
1018 // Comma-separated decimal for CCI transport; reg_router parses via std::stoull.
1019 static std::string encode_vec(const std::vector<uint64_t>& v)
1020 {
1021 std::string out;
1022 for (size_t i = 0; i < v.size(); i++) {
1023 if (i) out += ',';
1024 out += std::to_string(v[i]);
1025 }
1026 return out;
1027 }
1028
1029 void init_common(const std::string& _name, const std::string& path)
1030 {
1031 std::string n(name());
1032 SCP_LOGGER_NAME().features[0] = parent(n) + "." + _name;
1033 n = n.substr(0, n.length() - strlen("_target_socket"));
1034
1035 m_total_count = compute_total_count(m_dim_counts);
1036 m_total_span = compute_total_span(m_dim_counts, m_dim_strides);
1037
1038 m_row_major_div.assign(m_dim_counts.size(), 1);
1039 for (size_t k = m_dim_counts.size(); k-- > 1;) m_row_major_div[k - 1] = m_row_major_div[k] * m_dim_counts[k];
1040
1041 // Each outer stride must cover the span of all inner dims; otherwise the
1042 // router's claim() cannot uniquely invert an address into per-dim indices.
1043 for (size_t k = 0; k + 1 < m_dim_strides.size(); k++) {
1044 uint64_t inner_span = 0;
1045 for (size_t j = k + 1; j < m_dim_strides.size(); j++)
1046 inner_span += (m_dim_counts[j] - 1) * m_dim_strides[j];
1047 inner_span += sizeof(ELEM);
1048 if (m_dim_strides[k] < inner_span) {
1049 SCP_FATAL(())
1050 ("bank {} dim {} stride 0x{:x} < inner span 0x{:x}: layout is ambiguous", _name, k, m_dim_strides[k],
1051 inner_span);
1052 }
1053 }
1054 if (!m_dim_strides.empty() && m_dim_strides.back() < sizeof(ELEM)) {
1055 SCP_FATAL(())
1056 ("bank {} innermost stride 0x{:x} < sizeof(elem)={}", _name, m_dim_strides.back(), (uint64_t)sizeof(ELEM));
1057 }
1058
1059 proxy_data_array<ELEM>::p_size = m_total_span;
1060
1061 SCP_TRACE((), n)
1062 ("bank constructor : {} dims={} total_count={} span=0x{:x} in {}", _name, m_dim_counts.size(), m_total_count,
1063 m_total_span, path);
1064
1065 register_transport_dbg_func([&](tlm::tlm_generic_payload& txn) -> unsigned int {
1066 if (txn.get_data_length() < sizeof(ELEM)) return 0u;
1067 uint64_t addr = txn.get_address();
1068 uint64_t byte_off = proxy_data_array<ELEM>::p_relative_addresses.get_value()
1069 ? addr
1070 : addr - proxy_data_array<ELEM>::p_offset.get_value();
1071 if (!byte_offset_is_element(byte_off)) return 0u;
1072 unsigned char* data = txn.get_data_ptr();
1073 if (txn.get_command() == tlm::TLM_READ_COMMAND) {
1074 ELEM tmp;
1075 proxy_data_array<ELEM>::get_bytes(&tmp, byte_off);
1076 memset(data, 0, txn.get_data_length());
1077 memcpy(data, &tmp, sizeof(ELEM));
1078 return static_cast<unsigned int>(sizeof(ELEM));
1079 }
1080 if (txn.get_command() == tlm::TLM_WRITE_COMMAND) {
1081 ELEM tmp;
1082 memcpy(&tmp, data, sizeof(ELEM));
1083 proxy_data_array<ELEM>::set_bytes(&tmp, byte_off);
1084 return static_cast<unsigned int>(sizeof(ELEM));
1085 }
1086 return 0u;
1087 });
1088 }
1089
1090public:
1091 gs_register() = delete;
1092
1094 gs_register(std::string _name, std::string path, uint64_t offset, uint64_t count, uint64_t stride = sizeof(ELEM),
1095 ELEM mask = gs_full_mask<ELEM>())
1096 : m_regname(_name)
1097 , m_path(path)
1098 , port_fnct(_name, path)
1099 , proxy_data_array<ELEM>(SCP_LOGGER_NAME(), _name, path, offset, count, mask, stride,
1100 (count == 0 ? 0 : stride * (count - 1) + sizeof(ELEM)))
1101 {
1102 m_dim_counts = { count };
1103 m_dim_strides = { stride };
1104 init_common(_name, path);
1105 }
1106
1108 gs_register(std::string _name, std::string path, uint64_t offset, std::vector<uint64_t> counts,
1109 std::vector<uint64_t> strides, ELEM mask = gs_full_mask<ELEM>())
1110 : m_regname(_name)
1111 , m_path(path)
1112 , port_fnct(_name, path)
1113 , proxy_data_array<ELEM>(SCP_LOGGER_NAME(), _name, path, offset, compute_total_count(counts), mask,
1114 (strides.empty() ? sizeof(ELEM) : strides.back()), compute_total_span(counts, strides))
1115 {
1116 if (counts.size() != strides.size() || counts.empty())
1117 SCP_FATAL(())("bank {} constructor: counts and strides must have equal, non-zero size", _name);
1118 for (uint64_t count : counts) {
1119 if (count == 0) SCP_FATAL(())("bank {} constructor: dimension counts must be non-zero", _name);
1120 }
1121 m_dim_counts = std::move(counts);
1122 m_dim_strides = std::move(strides);
1123 init_common(_name, path);
1124 }
1125
1126 gs_register_bank_element<ELEM> operator[](size_t i)
1127 {
1128 if (i >= m_dim_counts[0])
1129 SCP_FATAL(())("bank {} dim 0 index {} exceeds count {}", m_regname, i, m_dim_counts[0]);
1130 return gs_register_bank_element<ELEM>(*this, m_dim_strides, m_dim_counts, i * m_dim_strides[0], 1);
1131 }
1132
1133 template <class... Idx, typename = std::enable_if_t<(sizeof...(Idx) >= 1) && (std::is_integral<Idx>::value && ...)>>
1134 gs_register_bank_element<ELEM> operator()(Idx... indices)
1135 {
1136 const uint64_t idx_arr[] = { static_cast<uint64_t>(indices)... };
1137 return build_cursor(idx_arr, sizeof...(Idx));
1138 }
1139
1140 gs_register_bank_element<ELEM> at(std::initializer_list<uint64_t> indices)
1141 {
1142 return build_cursor(indices.begin(), indices.size());
1143 }
1144 gs_register_bank_element<ELEM> at(const std::vector<uint64_t>& indices)
1145 {
1146 return build_cursor(indices.data(), indices.size());
1147 }
1148
1149private:
1150 gs_register_bank_element<ELEM> build_cursor(const uint64_t* indices, size_t n)
1151 {
1152 if (n == 0 || n > m_dim_strides.size())
1153 SCP_FATAL(())("bank {} indexed with {} dims, expected 1..{}", m_regname, n, m_dim_strides.size());
1154 uint64_t off = 0;
1155 for (size_t k = 0; k < n; k++) {
1156 if (indices[k] >= m_dim_counts[k])
1157 SCP_FATAL(())("bank {} index {} at dim {} exceeds count {}", m_regname, indices[k], k, m_dim_counts[k]);
1158 off += indices[k] * m_dim_strides[k];
1159 }
1160 return gs_register_bank_element<ELEM>(*this, m_dim_strides, m_dim_counts, off, n);
1161 }
1162
1163public:
1164 // Bulk access by flat row-major index. For a 1D bank this uses the fast
1165 // contiguous path when stride == sizeof(ELEM); otherwise it loops per
1166 // element via the byte-offset path.
1167 void get(ELEM* dst, uint64_t idx, uint64_t length)
1168 {
1169 if (m_dim_counts.size() == 1) {
1170 proxy_data_array<ELEM>::get(dst, idx, length);
1171 } else {
1172 for (uint64_t i = 0; i < length; i++)
1173 proxy_data_array<ELEM>::get_bytes(&dst[i], byte_offset_for_flat(idx + i));
1174 }
1175 }
1176 void set(ELEM* src, uint64_t idx, uint64_t length, bool use_mask = true)
1177 {
1178 if (m_dim_counts.size() == 1 && m_element_masks.empty()) {
1179 proxy_data_array<ELEM>::set(src, idx, length, use_mask);
1180 } else {
1181 for (uint64_t i = 0; i < length; i++)
1182 proxy_data_array<ELEM>::set_bytes(&src[i], byte_offset_for_flat(idx + i), use_mask);
1183 }
1184 }
1185
1186 std::string get_regname() const { return m_regname; }
1187 std::string get_path() const { return m_path; }
1188 uint64_t get_offset() const { return proxy_data_array<ELEM>::p_offset.get_value(); }
1189 uint64_t get_size() const { return proxy_data_array<ELEM>::p_size.get_value(); }
1190 uint64_t get_total_count() const { return m_total_count; }
1191 size_t get_dims() const { return m_dim_counts.size(); }
1192 uint64_t get_dim_count(size_t k) const { return m_dim_counts.at(k); }
1193 uint64_t get_dim_stride(size_t k) const { return m_dim_strides.at(k); }
1194 const std::vector<uint64_t>& get_dim_counts() const { return m_dim_counts; }
1195 const std::vector<uint64_t>& get_dim_strides() const { return m_dim_strides; }
1196 ELEM get_mask() const { return proxy_data_array<ELEM>::p_mask.get_value(); }
1197 void set_mask(ELEM mask) { proxy_data_array<ELEM>::p_mask = mask; }
1198
1199 void set_element_mask(uint64_t flat_index, ELEM mask)
1200 {
1201 if (flat_index >= m_total_count)
1202 SCP_FATAL(())("bank {} flat index {} exceeds total count {}", m_regname, flat_index, m_total_count);
1203 m_element_masks[flat_index] = mask;
1204 }
1205
1206 ELEM get_element_mask(uint64_t flat_index) const
1207 {
1208 if (flat_index >= m_total_count)
1209 SCP_FATAL(())("bank {} flat index {} exceeds total count {}", m_regname, flat_index, m_total_count);
1210 return mask_for_element(flat_index);
1211 }
1212
1213 void clear_element_mask(uint64_t flat_index)
1214 {
1215 if (flat_index >= m_total_count)
1216 SCP_FATAL(())("bank {} flat index {} exceeds total count {}", m_regname, flat_index, m_total_count);
1217 m_element_masks.erase(flat_index);
1218 }
1219
1220 void set_element_mask(std::initializer_list<uint64_t> indices, ELEM mask)
1221 {
1222 set_element_mask(flat_index_for_indices(indices.begin(), indices.size()), mask);
1223 }
1224
1225 ELEM get_element_mask(std::initializer_list<uint64_t> indices) const
1226 {
1227 return get_element_mask(flat_index_for_indices(indices.begin(), indices.size()));
1228 }
1229
1230 void clear_element_mask(std::initializer_list<uint64_t> indices)
1231 {
1232 clear_element_mask(flat_index_for_indices(indices.begin(), indices.size()));
1233 }
1234
1235 void set_element_mask(const std::vector<uint64_t>& indices, ELEM mask)
1236 {
1237 set_element_mask(flat_index_for_indices(indices.data(), indices.size()), mask);
1238 }
1239
1240 ELEM get_element_mask(const std::vector<uint64_t>& indices) const
1241 {
1242 return get_element_mask(flat_index_for_indices(indices.data(), indices.size()));
1243 }
1244
1245 void clear_element_mask(const std::vector<uint64_t>& indices)
1246 {
1247 clear_element_mask(flat_index_for_indices(indices.data(), indices.size()));
1248 }
1249
1250protected:
1251 bool has_element_masks() const override { return !m_element_masks.empty(); }
1252
1253 ELEM mask_for_element(uint64_t flat_index) const override
1254 {
1255 auto it = m_element_masks.find(flat_index);
1256 return it == m_element_masks.end() ? proxy_data_array<ELEM>::p_mask.get_value() : it->second;
1257 }
1258
1259 ELEM mask_for_byte_offset(uint64_t byte_offset) const override
1260 {
1261 uint64_t flat_index = 0;
1262 if (!try_flat_index_for_byte_offset(byte_offset, flat_index)) return proxy_data_array<ELEM>::p_mask.get_value();
1263 return mask_for_element(flat_index);
1264 }
1265
1266public:
1267 uint64_t byte_offset_for(const uint64_t* indices, size_t n) const
1268 {
1269 if (n != m_dim_strides.size())
1270 SCP_FATAL(())("bank {} indexed with {} dims, expected {}", m_regname, n, m_dim_strides.size());
1271 uint64_t off = 0;
1272 for (size_t k = 0; k < n; k++) {
1273 if (indices[k] >= m_dim_counts[k])
1274 SCP_FATAL(())("bank {} index {} at dim {} exceeds count {}", m_regname, indices[k], k, m_dim_counts[k]);
1275 off += indices[k] * m_dim_strides[k];
1276 }
1277 return off;
1278 }
1279
1280 uint64_t byte_offset_for_flat(uint64_t flat_idx) const
1281 {
1282 if (flat_idx >= m_total_count)
1283 SCP_FATAL(())("bank {} flat index {} exceeds total count {}", m_regname, flat_idx, m_total_count);
1284 uint64_t off = 0;
1285 uint64_t remaining = flat_idx;
1286 for (size_t k = 0; k < m_dim_counts.size(); k++) {
1287 uint64_t idx_k = remaining / m_row_major_div[k];
1288 remaining = remaining % m_row_major_div[k];
1289 off += idx_k * m_dim_strides[k];
1290 }
1291 return off;
1292 }
1293
1294 bool is_element_start(const gs_bank_access& access) const
1295 {
1296 return access && byte_offset_for_flat(access.index) == access.byte_offset;
1297 }
1298
1299 uint64_t masked_transaction_element_count(const tlm::tlm_generic_payload& txn, const gs_bank_access& access) const
1300 {
1301 if (!is_element_start(access)) return 0;
1302 uint64_t length = txn.get_data_length();
1303 if (length == 0 || length % sizeof(ELEM) != 0) return 0;
1304
1305 uint64_t count = length / sizeof(ELEM);
1306 if (count == 1) return count;
1307 if (m_dim_counts.size() != 1 || m_dim_strides[0] != sizeof(ELEM)) return 0;
1308 if (access.index >= m_total_count || count > m_total_count - access.index) return 0;
1309 return count;
1310 }
1311
1312 bool byte_offset_is_element(uint64_t byte_off) const
1313 {
1314 uint64_t r = byte_off;
1315 for (size_t k = 0; k < m_dim_strides.size(); k++) {
1316 uint64_t idx = r / m_dim_strides[k];
1317 if (idx >= m_dim_counts[k]) return false;
1318 r = r % m_dim_strides[k];
1319 }
1320 return r < sizeof(ELEM);
1321 }
1322
1324 gs_bank_access decode_access(const tlm::tlm_generic_payload& txn) const
1325 {
1326 gs_bank_access r{};
1327 const uint64_t base = proxy_data_array<ELEM>::p_offset.get_value();
1328 const bool relative = proxy_data_array<ELEM>::p_relative_addresses.get_value();
1329 uint64_t addr = txn.get_address();
1330 r.byte_offset = relative ? addr : (addr - base);
1331
1332 r.indices.assign(m_dim_strides.size(), 0);
1333 uint64_t rem = r.byte_offset;
1334 r.in_range = true;
1335 for (size_t k = 0; k < m_dim_strides.size(); k++) {
1336 uint64_t idx_k = rem / m_dim_strides[k];
1337 rem = rem % m_dim_strides[k];
1338 r.indices[k] = idx_k;
1339 if (idx_k >= m_dim_counts[k]) r.in_range = false;
1340 }
1341 r.aligned = rem < sizeof(ELEM);
1342
1343 uint64_t flat = 0;
1344 for (size_t k = 0; k < m_dim_strides.size(); k++) flat += r.indices[k] * m_row_major_div[k];
1345 r.index = flat;
1346 return r;
1347 }
1348
1349 void capture_txn_pre(tlm::tlm_generic_payload& txn) override
1350 {
1351 if ((!has_element_masks() && proxy_data_array<ELEM>::p_mask.get_value() == gs_full_mask<ELEM>()) ||
1352 (txn.get_command() != tlm::tlm_command::TLM_WRITE_COMMAND)) {
1353 return;
1354 }
1355 auto a = decode_access(txn);
1356 uint64_t count = masked_transaction_element_count(txn, a);
1357 if (count == 0) {
1358 captured_txn_data.clear();
1359 txn.set_response_status(tlm::TLM_BURST_ERROR_RESPONSE);
1360 return;
1361 }
1362 captured_txn_data.resize(count * sizeof(ELEM));
1363 for (uint64_t i = 0; i < count; i++) {
1364 proxy_data_array<ELEM>::get_bytes(reinterpret_cast<ELEM*>(captured_txn_data.data()) + i,
1365 byte_offset_for_flat(a.index + i));
1366 }
1367 }
1368
1369 void handle_mask_post(tlm::tlm_generic_payload& txn) override
1370 {
1371 if ((!has_element_masks() && proxy_data_array<ELEM>::p_mask.get_value() == gs_full_mask<ELEM>()) ||
1372 (txn.get_command() != tlm::tlm_command::TLM_WRITE_COMMAND)) {
1373 return;
1374 }
1375 auto a = decode_access(txn);
1376 uint64_t count = masked_transaction_element_count(txn, a);
1377 if (count == 0 || captured_txn_data.size() < count * sizeof(ELEM)) {
1378 txn.set_response_status(tlm::TLM_BURST_ERROR_RESPONSE);
1379 return;
1380 }
1381 unsigned char* txn_data_ptr = txn.get_data_ptr();
1382 for (uint64_t i = 0; i < count; i++) {
1383 ELEM incoming;
1384 ELEM stored;
1385 memcpy(&incoming, txn_data_ptr + i * sizeof(ELEM), sizeof(ELEM));
1386 memcpy(&stored, captured_txn_data.data() + i * sizeof(ELEM), sizeof(ELEM));
1387 proxy_data_array<ELEM>::write_with_mask(&incoming, &stored, 1, get_element_mask(a.index + i));
1388 proxy_data_array<ELEM>::set_bytes(&stored, byte_offset_for_flat(a.index + i), false);
1389 memcpy(txn_data_ptr + i * sizeof(ELEM), &stored, sizeof(ELEM));
1390 }
1391 }
1392
1393protected:
1394 using port_fnct::parent;
1395};
1396
1397} // namespace gs
1398
1399#endif // GS_REGISTERS_H
Proxy for bitfield access to a register.
Definition registers.h:685
fields within registered encapsulated using a bitfield proxy. This field is constructed from a specif...
Definition registers.h:716
gs_field(REG &reg, std::string name, uint32_t bit_start=0, uint32_t bit_length=sizeof(TYPE) *8)
Definition registers.h:737
gs_register(std::string _name, std::string path, uint64_t offset, uint64_t count, uint64_t stride=sizeof(ELEM), ELEM mask=gs_full_mask< ELEM >())
Definition registers.h:1094
gs_register(std::string _name, std::string path, uint64_t offset, std::vector< uint64_t > counts, std::vector< uint64_t > strides, ELEM mask=gs_full_mask< ELEM >())
Definition registers.h:1108
gs_bank_access decode_access(const tlm::tlm_generic_payload &txn) const
Definition registers.h:1324
Definition registers.h:830
bit_proxy operator[](gs_field< ELEM > &f)
Definition registers.h:943
Definition registers.h:780
Class that encapsulates a 'register' that proxies it's data via a tlm interface, and uses callbacks (...
Definition registers.h:556
A proxy data class that stores it's value using a b_transport interface Data is passed by pointer,...
Definition registers.h:227
void set_bytes(TYPE *src, uint64_t byte_offset, bool use_mask=true)
Definition registers.h:440
void get_bytes(TYPE *dst, uint64_t byte_offset)
Definition registers.h:418
Tool which reads a Lua configuration file and sets parameters.
Definition biflow.cc:10
Definition registers.h:66
Definition registers.h:53
Definition registers.h:59