A SystemC productivity library for virtual platform development utilizing SCV and TLM2.0 https://www.minres.com/#opensource

tlm_target.h 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*******************************************************************************
  2. * Copyright 2016, 2018 MINRES Technologies GmbH
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *******************************************************************************/
  16. #ifndef _SYSC_TLM_TARGET_H_
  17. #define _SYSC_TLM_TARGET_H_
  18. #include "resource_access_if.h"
  19. #include "scv4tlm/tlm_rec_target_socket.h"
  20. #include "target_mixin.h"
  21. #include "util/range_lut.h"
  22. #include "utilities.h"
  23. #include <array>
  24. namespace scc {
  25. /**
  26. * a simple access-width based bus interface (no DMI support)
  27. */
  28. struct addr_range {
  29. uint64_t base, size;
  30. };
  31. template <unsigned int BUSWIDTH = 32> class tlm_target {
  32. public:
  33. using this_type = tlm_target<BUSWIDTH>;
  34. /**
  35. * the constructor
  36. *
  37. * @param clock
  38. */
  39. tlm_target(sc_core::sc_time &clock);
  40. /**
  41. * the socket
  42. */
  43. scc::target_mixin<scv4tlm::tlm_rec_target_socket<BUSWIDTH>> socket;
  44. /**
  45. * the blocking transport callback
  46. *
  47. * @param
  48. * @param
  49. */
  50. void b_tranport_cb(tlm::tlm_generic_payload &, sc_core::sc_time &);
  51. /**
  52. * the debug transport callback
  53. *
  54. * @param
  55. * @return
  56. */
  57. unsigned int tranport_dbg_cb(tlm::tlm_generic_payload &);
  58. /**
  59. * add a resource to this target at a certain address within the socket address range
  60. *
  61. * @param rai the resource to add
  62. * @param base_addr the offset of the resource (from address 0)
  63. */
  64. void addResource(resource_access_if &rai, uint64_t base_addr) {
  65. socket_map.addEntry(std::make_pair(&rai, base_addr), base_addr, rai.size());
  66. }
  67. /**
  68. * add an indexed resource to this target at a certain address within the socket address range
  69. * @param irai the resource to add
  70. * @param base_addr the offset of the resource (from address 0)
  71. */
  72. void addResource(indexed_resource_access_if &irai, uint64_t base_addr) {
  73. for (size_t idx = 0; idx < irai.size(); ++idx) {
  74. socket_map.addEntry(std::make_pair(&irai[idx], base_addr), base_addr, irai[idx].size());
  75. base_addr += irai[idx].size();
  76. }
  77. }
  78. private:
  79. sc_core::sc_time &clk;
  80. protected:
  81. util::range_lut<std::pair<resource_access_if *, uint64_t>> socket_map;
  82. };
  83. /**
  84. * helper structure to define a address range for a socket
  85. */
  86. template <unsigned BUSWIDTH = 32> struct target_memory_map_entry {
  87. tlm::tlm_target_socket<BUSWIDTH> &target;
  88. sc_dt::uint64 start;
  89. sc_dt::uint64 size;
  90. };
  91. /**
  92. * helper structure to define a named address range
  93. */
  94. template <unsigned BUSWIDTH = 32> struct target_name_map_entry {
  95. std::string name;
  96. sc_dt::uint64 start;
  97. sc_dt::uint64 size;
  98. };
  99. } /* namespace scc */
  100. template <unsigned int BUSWIDTH>
  101. inline scc::tlm_target<BUSWIDTH>::tlm_target(sc_core::sc_time &clock)
  102. : socket("socket")
  103. , clk(clock)
  104. , socket_map(std::make_pair(nullptr, 0)) {
  105. socket.register_b_transport(
  106. [=](tlm::tlm_generic_payload &gp, sc_core::sc_time &delay) -> void { this->b_tranport_cb(gp, delay); });
  107. socket.register_transport_dbg([=](tlm::tlm_generic_payload &gp) -> unsigned { return this->tranport_dbg_cb(gp); });
  108. }
  109. template <unsigned int BUSWIDTH>
  110. void scc::tlm_target<BUSWIDTH>::b_tranport_cb(tlm::tlm_generic_payload &gp, sc_core::sc_time &delay) {
  111. resource_access_if *ra = nullptr;
  112. uint64_t base = 0;
  113. std::tie(ra, base) = socket_map.getEntry(gp.get_address());
  114. if (ra) {
  115. gp.set_response_status(tlm::TLM_BURST_ERROR_RESPONSE);
  116. if (gp.get_data_length() <= ra->size()) {
  117. gp.set_response_status(tlm::TLM_BYTE_ENABLE_ERROR_RESPONSE);
  118. if (gp.get_byte_enable_ptr() == nullptr) {
  119. gp.set_response_status(tlm::TLM_GENERIC_ERROR_RESPONSE);
  120. if (gp.get_data_length() == gp.get_streaming_width()) {
  121. if (gp.get_command() == tlm::TLM_READ_COMMAND) {
  122. if (ra->read(gp.get_data_ptr(), gp.get_data_length(), (gp.get_address() - base), delay))
  123. gp.set_response_status(tlm::TLM_OK_RESPONSE);
  124. } else if (gp.get_command() == tlm::TLM_WRITE_COMMAND) {
  125. if (ra->write(gp.get_data_ptr(), gp.get_data_length(), (gp.get_address() - base), delay))
  126. gp.set_response_status(tlm::TLM_OK_RESPONSE);
  127. } else {
  128. gp.set_response_status(tlm::TLM_COMMAND_ERROR_RESPONSE);
  129. }
  130. }
  131. }
  132. }
  133. } else {
  134. gp.set_response_status(tlm::TLM_ADDRESS_ERROR_RESPONSE);
  135. }
  136. delay += clk;
  137. }
  138. template <unsigned int BUSWIDTH> unsigned int scc::tlm_target<BUSWIDTH>::tranport_dbg_cb(tlm::tlm_generic_payload &gp) {
  139. resource_access_if *ra = nullptr;
  140. uint64_t base = 0;
  141. std::tie(ra, base) = socket_map.getEntry(gp.get_address());
  142. if (ra) {
  143. if (gp.get_data_length() == ra->size() && gp.get_byte_enable_ptr() == nullptr &&
  144. gp.get_data_length() == gp.get_streaming_width()) {
  145. if (gp.get_command() == tlm::TLM_READ_COMMAND) {
  146. if (ra->read_dbg(gp.get_data_ptr(), gp.get_data_length(), (gp.get_address() - base) / ra->size()))
  147. return gp.get_data_length();
  148. } else if (gp.get_command() == tlm::TLM_READ_COMMAND) {
  149. if (ra->write_dbg(gp.get_data_ptr(), gp.get_data_length(), (gp.get_address() - base) / ra->size()))
  150. return gp.get_data_length();
  151. }
  152. }
  153. }
  154. return 0;
  155. }
  156. #endif /* _SYSC_TLM_TARGET_H_ */