99class port_fnct :
public tlm_utils::simple_target_socket<port_fnct, DEFAULT_TLM_BUSWIDTH>
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;
109 std::function<
unsigned int(tlm::tlm_generic_payload&)> transport_dbg_func;
111 std::string my_name()
113 std::string n(name());
114 n = n.substr(0, n.length() - strlen(
"_target_socket"));
115 n = n.substr(n.find_last_of(
".") + 1);
120 unsigned int transport_dbg(tlm::tlm_generic_payload& txn)
122 if (transport_dbg_func) {
123 return transport_dbg_func(txn);
128 void b_transport(tlm::tlm_generic_payload& txn, sc_core::sc_time& delay)
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);
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);
152 capture_txn_pre(txn);
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);
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);
176 m_in_callback =
false;
179 txn.set_dmi_allowed(
false);
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;
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)")
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);
212 void register_transport_dbg_func(std::function<
unsigned int(tlm::tlm_generic_payload&)> fn)
214 transport_dbg_func = fn;
218 std::string parent(std::string name) {
return name.substr(0, name.find_last_of(
'.')); }
226class proxy_data_array
228 scp::scp_logger_cache& SCP_LOGGER_NAME();
229 TYPE* m_dmi =
nullptr;
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);
253 uint8_t* dmi_bytes_at(uint64_t idx) {
return reinterpret_cast<uint8_t*
>(m_dmi) + p_stride.get_value() * idx; }
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;
265 tlm_utils::simple_initiator_socket<proxy_data_array, DEFAULT_TLM_BUSWIDTH> initiator_socket;
267 void get(TYPE* dst, uint64_t idx = 0, uint64_t length = 1)
269 sc_assert(idx + length <= p_number);
270 const uint64_t stride = p_stride.get_value();
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();
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();
307 (
"Got value (transport strided) : [{:#x}]", fmt::join(std::vector<TYPE>(&dst[0], &dst[length]),
","));
311 void set(TYPE* src, uint64_t idx = 0, uint64_t length = 1,
bool use_mask =
true)
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>());
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));
321 for (uint64_t i = 0; i < length; i++) {
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));
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();
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]);
352 curr_data.resize(
sizeof(TYPE));
353 get(
reinterpret_cast<TYPE*
>(curr_data.data()), idx + i, 1);
355 write_with_mask(&tmp,
reinterpret_cast<TYPE*
>(curr_data.data()), 1);
356 data_ptr = curr_data.data();
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();
371 (
"Set value (transport strided) : [{:#x}]", fmt::join(std::vector<TYPE>(&src[0], &src[length]),
","));
375 void write_with_mask(TYPE* src, TYPE* dst, uint64_t length)
377 write_with_mask(src, dst, length, p_mask.get_value());
380 void write_with_mask(TYPE* src, TYPE* dst, uint64_t length, TYPE mask)
383 SCP_FATAL(())(
"write_with_mask(): src pointer is NULL");
386 SCP_FATAL(())(
"write_with_mask(): dst pointer is NULL");
388 std::vector<TYPE> mask_to_apply(length, mask);
389 std::vector<TYPE> new_and_mask(length, 0);
390 std::vector<TYPE> old_and_not_mask(length, 0);
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); });
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; });
400 TYPE& operator[](
int idx)
405 SCP_FATAL(())(
"Operator[] can only be used for DMI'able registers");
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";
414 return *
reinterpret_cast<TYPE*
>(dmi_bytes_at(
static_cast<uint64_t
>(idx)));
421 memcpy(dst,
reinterpret_cast<uint8_t*
>(m_dmi) + byte_offset,
sizeof(TYPE));
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();
440 void set_bytes(TYPE* src, uint64_t byte_offset,
bool use_mask =
true)
442 const TYPE mask = mask_for_byte_offset(byte_offset);
443 const bool full_mask = (mask == gs_full_mask<TYPE>());
445 if (!use_mask || full_mask) {
446 memcpy(
reinterpret_cast<uint8_t*
>(m_dmi) + byte_offset, src,
sizeof(TYPE));
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));
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);
461 curr_data.resize(
sizeof(TYPE));
462 get_bytes(
reinterpret_cast<TYPE*
>(curr_data.data()), byte_offset);
464 write_with_mask(&tmp,
reinterpret_cast<TYPE*
>(curr_data.data()), 1, mask);
465 data_ptr = curr_data.data();
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();
481 void invalidate_direct_mem_ptr(sc_dt::uint64 start, sc_dt::uint64 end) { m_dmi =
nullptr; }
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(); }
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")
507 initiator_socket.register_invalidate_direct_mem_ptr(
this,
508 &gs::proxy_data_array<TYPE>::invalidate_direct_mem_ptr);
555class gs_register :
public port_fnct,
public proxy_data<TYPE>
557 SCP_LOGGER((),
"register");
560 std::string m_regname;
562 std::vector<unsigned char> captured_txn_data;
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>())
570 , port_fnct(_name, path)
571 , proxy_data<TYPE>(SCP_LOGGER_NAME(), _name, path, offset, number, mask)
573 std::string n(name());
574 SCP_LOGGER_NAME().features[0] = parent(n) +
"." + _name;
575 n = n.substr(0, n.length() - strlen(
"_target_socket"));
576 SCP_TRACE((), n)(
"constructor : {} attching in {}", _name, path);
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));
587 if (txn.get_command() == tlm::TLM_WRITE_COMMAND) {
589 memcpy(&tmp, data,
sizeof(TYPE));
590 proxy_data<TYPE>::set(tmp);
591 return static_cast<unsigned int>(
sizeof(TYPE));
593 return static_cast<unsigned int>(0);
596 void operator=(TYPE value) { proxy_data<TYPE>::set(value); }
597 operator TYPE() {
return proxy_data<TYPE>::get(); }
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)
604 if (other == 0) SCP_FATAL(())(
"Trying to divide a register value by 0!");
605 proxy_data<TYPE>::set(proxy_data<TYPE>::get() / other);
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); }
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)
618 return proxy_data_array<TYPE>::set(src, idx, length, use_mask);
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)
628 SCP_TRACE(()) <<
"Set Mask to 0x" << std::hex << mask;
629 proxy_data<TYPE>::p_mask = mask;
631 TYPE get_mask()
const {
return proxy_data<TYPE>::p_mask.get_value(); }
632 void capture_txn_pre(tlm::tlm_generic_payload& txn)
override
634 if ((proxy_data<TYPE>::p_mask.get_value() == gs_full_mask<TYPE>()) ||
635 (txn.get_command() != tlm::tlm_command::TLM_WRITE_COMMAND)) {
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();
644 if (proxy_data<TYPE>::p_relative_addresses.get_value()) {
645 idx = txn_addr / stride;
647 idx = (txn_addr - proxy_data<TYPE>::p_offset.get_value()) / stride;
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));
653 void handle_mask_post(tlm::tlm_generic_payload& txn)
override
655 if ((proxy_data<TYPE>::p_mask.get_value() == gs_full_mask<TYPE>()) ||
656 (txn.get_command() != tlm::tlm_command::TLM_WRITE_COMMAND)) {
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();
665 if (proxy_data<TYPE>::p_relative_addresses.get_value()) {
666 idx = txn_addr / stride;
668 idx = (txn_addr - proxy_data<TYPE>::p_offset.get_value()) / stride;
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);
958class gs_register<
gs_bank_of<ELEM>> :
public port_fnct,
public proxy_data_array<ELEM>
960 SCP_LOGGER((),
"register");
962 std::string m_regname;
964 std::vector<unsigned char> captured_txn_data;
965 std::unordered_map<uint64_t, ELEM> m_element_masks;
967 std::vector<uint64_t> m_dim_counts;
968 std::vector<uint64_t> m_dim_strides;
970 std::vector<uint64_t> m_row_major_div;
971 uint64_t m_total_count = 0;
972 uint64_t m_total_span = 0;
974 static uint64_t compute_total_span(
const std::vector<uint64_t>& counts,
const std::vector<uint64_t>& strides)
976 if (counts.empty() || counts.size() != strides.size())
return 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];
982 return span +
sizeof(ELEM);
985 static uint64_t compute_total_count(
const std::vector<uint64_t>& counts)
988 for (
auto v : counts) c *= v;
992 uint64_t flat_index_for_indices(
const uint64_t* indices,
size_t n)
const
994 if (n != m_dim_counts.size())
995 SCP_FATAL(())(
"bank {} indexed with {} dims, expected {}", m_regname, n, m_dim_counts.size());
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];
1005 bool try_flat_index_for_byte_offset(uint64_t byte_offset, uint64_t& flat)
const
1007 uint64_t remaining = byte_offset;
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];
1015 return remaining <
sizeof(ELEM);
1019 static std::string encode_vec(
const std::vector<uint64_t>& v)
1022 for (
size_t i = 0; i < v.size(); i++) {
1024 out += std::to_string(v[i]);
1029 void init_common(
const std::string& _name,
const std::string& path)
1031 std::string n(name());
1032 SCP_LOGGER_NAME().features[0] = parent(n) +
"." + _name;
1033 n = n.substr(0, n.length() - strlen(
"_target_socket"));
1035 m_total_count = compute_total_count(m_dim_counts);
1036 m_total_span = compute_total_span(m_dim_counts, m_dim_strides);
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];
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) {
1050 (
"bank {} dim {} stride 0x{:x} < inner span 0x{:x}: layout is ambiguous", _name, k, m_dim_strides[k],
1054 if (!m_dim_strides.empty() && m_dim_strides.back() <
sizeof(ELEM)) {
1056 (
"bank {} innermost stride 0x{:x} < sizeof(elem)={}", _name, m_dim_strides.back(), (uint64_t)
sizeof(ELEM));
1059 proxy_data_array<ELEM>::p_size = m_total_span;
1062 (
"bank constructor : {} dims={} total_count={} span=0x{:x} in {}", _name, m_dim_counts.size(), m_total_count,
1063 m_total_span, path);
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()
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) {
1076 memset(data, 0, txn.get_data_length());
1077 memcpy(data, &tmp,
sizeof(ELEM));
1078 return static_cast<unsigned int>(
sizeof(ELEM));
1080 if (txn.get_command() == tlm::TLM_WRITE_COMMAND) {
1082 memcpy(&tmp, data,
sizeof(ELEM));
1084 return static_cast<unsigned int>(
sizeof(ELEM));
1091 gs_register() =
delete;
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>())
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)))
1102 m_dim_counts = { count };
1103 m_dim_strides = { stride };
1104 init_common(_name, path);
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>())
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))
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);
1121 m_dim_counts = std::move(counts);
1122 m_dim_strides = std::move(strides);
1123 init_common(_name, path);
1128 if (i >= m_dim_counts[0])
1129 SCP_FATAL(())(
"bank {} dim 0 index {} exceeds count {}", m_regname, i, m_dim_counts[0]);
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)
1136 const uint64_t idx_arr[] = {
static_cast<uint64_t
>(indices)... };
1137 return build_cursor(idx_arr,
sizeof...(Idx));
1140 gs_register_bank_element<ELEM> at(std::initializer_list<uint64_t> indices)
1142 return build_cursor(indices.begin(), indices.size());
1144 gs_register_bank_element<ELEM> at(
const std::vector<uint64_t>& indices)
1146 return build_cursor(indices.data(), indices.size());
1150 gs_register_bank_element<ELEM> build_cursor(
const uint64_t* indices,
size_t n)
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());
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];
1160 return gs_register_bank_element<ELEM>(*
this, m_dim_strides, m_dim_counts, off, n);
1167 void get(ELEM* dst, uint64_t idx, uint64_t length)
1169 if (m_dim_counts.size() == 1) {
1170 proxy_data_array<ELEM>::get(dst, idx, length);
1172 for (uint64_t i = 0; i < length; i++)
1173 proxy_data_array<ELEM>::get_bytes(&dst[i], byte_offset_for_flat(idx + i));
1176 void set(ELEM* src, uint64_t idx, uint64_t length,
bool use_mask =
true)
1178 if (m_dim_counts.size() == 1 && m_element_masks.empty()) {
1179 proxy_data_array<ELEM>::set(src, idx, length, use_mask);
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);
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; }
1199 void set_element_mask(uint64_t flat_index, ELEM mask)
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;
1206 ELEM get_element_mask(uint64_t flat_index)
const
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);
1213 void clear_element_mask(uint64_t flat_index)
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);
1220 void set_element_mask(std::initializer_list<uint64_t> indices, ELEM mask)
1222 set_element_mask(flat_index_for_indices(indices.begin(), indices.size()), mask);
1225 ELEM get_element_mask(std::initializer_list<uint64_t> indices)
const
1227 return get_element_mask(flat_index_for_indices(indices.begin(), indices.size()));
1230 void clear_element_mask(std::initializer_list<uint64_t> indices)
1232 clear_element_mask(flat_index_for_indices(indices.begin(), indices.size()));
1235 void set_element_mask(
const std::vector<uint64_t>& indices, ELEM mask)
1237 set_element_mask(flat_index_for_indices(indices.data(), indices.size()), mask);
1240 ELEM get_element_mask(
const std::vector<uint64_t>& indices)
const
1242 return get_element_mask(flat_index_for_indices(indices.data(), indices.size()));
1245 void clear_element_mask(
const std::vector<uint64_t>& indices)
1247 clear_element_mask(flat_index_for_indices(indices.data(), indices.size()));
1251 bool has_element_masks()
const override {
return !m_element_masks.empty(); }
1253 ELEM mask_for_element(uint64_t flat_index)
const override
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;
1259 ELEM mask_for_byte_offset(uint64_t byte_offset)
const override
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);
1267 uint64_t byte_offset_for(
const uint64_t* indices,
size_t n)
const
1269 if (n != m_dim_strides.size())
1270 SCP_FATAL(())(
"bank {} indexed with {} dims, expected {}", m_regname, n, m_dim_strides.size());
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];
1280 uint64_t byte_offset_for_flat(uint64_t flat_idx)
const
1282 if (flat_idx >= m_total_count)
1283 SCP_FATAL(())(
"bank {} flat index {} exceeds total count {}", m_regname, flat_idx, m_total_count);
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];
1294 bool is_element_start(
const gs_bank_access& access)
const
1296 return access && byte_offset_for_flat(access.index) == access.byte_offset;
1299 uint64_t masked_transaction_element_count(
const tlm::tlm_generic_payload& txn,
const gs_bank_access& access)
const
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;
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;
1312 bool byte_offset_is_element(uint64_t byte_off)
const
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];
1320 return r <
sizeof(ELEM);
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);
1332 r.indices.assign(m_dim_strides.size(), 0);
1333 uint64_t rem = r.byte_offset;
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;
1341 r.aligned = rem <
sizeof(ELEM);
1344 for (
size_t k = 0; k < m_dim_strides.size(); k++) flat += r.indices[k] * m_row_major_div[k];
1349 void capture_txn_pre(tlm::tlm_generic_payload& txn)
override
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)) {
1355 auto a = decode_access(txn);
1356 uint64_t count = masked_transaction_element_count(txn, a);
1358 captured_txn_data.clear();
1359 txn.set_response_status(tlm::TLM_BURST_ERROR_RESPONSE);
1362 captured_txn_data.resize(count *
sizeof(ELEM));
1363 for (uint64_t i = 0; i < count; i++) {
1365 byte_offset_for_flat(a.index + i));
1369 void handle_mask_post(tlm::tlm_generic_payload& txn)
override
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)) {
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);
1381 unsigned char* txn_data_ptr = txn.get_data_ptr();
1382 for (uint64_t i = 0; i < count; i++) {
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));
1389 memcpy(txn_data_ptr + i *
sizeof(ELEM), &stored,
sizeof(ELEM));
1394 using port_fnct::parent;