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

register.h 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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_REGISTER_H_
  17. #define _SYSC_REGISTER_H_
  18. #include <memory>
  19. #include "resetable.h"
  20. #include "resource_access_if.h"
  21. #include "util/delegate.h"
  22. #include "utilities.h"
  23. #include <functional>
  24. #include <limits>
  25. #include <sstream>
  26. namespace scc {
  27. namespace impl {
  28. /**
  29. * some template classes and functions to calculate mask values
  30. */
  31. template <typename T, bool = std::is_integral<T>::value> class helper {};
  32. template <typename T> class helper<T, true> {
  33. public:
  34. using Type = T;
  35. template <typename Type> constexpr Type get_max_uval() {
  36. return std::numeric_limits<Type>::is_signed ? -1 : std::numeric_limits<Type>::max();
  37. }
  38. };
  39. template <typename T> class helper<T, false> {
  40. public:
  41. using Type = typename T::StorageType;
  42. template <typename Type> constexpr Type get_max_uval() {
  43. return std::numeric_limits<Type>::is_signed ? -1 : std::numeric_limits<Type>::max();
  44. }
  45. };
  46. template <typename Type> constexpr Type get_max_uval() {
  47. return std::numeric_limits<Type>::is_signed ? -1 : std::numeric_limits<Type>::max();
  48. }
  49. /**
  50. * a simple register implementation taking a certain data type. The sc_register does not hold the value itself,
  51. * this needs to be provided. It only provides som resource access interface and handled callbacks for read and
  52. * write accesses
  53. */
  54. template <typename DATATYPE>
  55. class sc_register : public sc_core::sc_object, public resource_access_if, public traceable {
  56. public:
  57. using this_type = class sc_register<DATATYPE>;
  58. /**
  59. * the constructor
  60. *
  61. * @param nm the instance name
  62. * @param storage the storage data structure
  63. * @param reset_val the reset value
  64. * @param owner the owning object which needs to implement the resettable interface
  65. * @param rdmask the SW read mask
  66. * @param wrmask the SW write mask
  67. */
  68. sc_register(sc_core::sc_module_name nm, DATATYPE &storage, const DATATYPE reset_val, resetable &owner,
  69. DATATYPE rdmask = get_max_uval<DATATYPE>(), DATATYPE wrmask = get_max_uval<DATATYPE>())
  70. : sc_core::sc_object(nm)
  71. , res_val(reset_val)
  72. , rdmask(rdmask)
  73. , wrmask(wrmask)
  74. , storage(storage) {
  75. owner.register_resource(this);
  76. }
  77. /**
  78. * the destructor
  79. */
  80. ~sc_register() = default;
  81. /**
  82. *
  83. * @return
  84. */
  85. size_t size() const override { return sizeof(DATATYPE); }
  86. /**
  87. * reset the register
  88. */
  89. void reset() override {
  90. DATATYPE r(res_val);
  91. if (wr_cb) wr_cb(*this, r, sc_core::SC_ZERO_TIME);
  92. storage = r;
  93. }
  94. /**
  95. * write function from resource_access_if
  96. *
  97. * @param data data to write
  98. * @param length size of data to write
  99. * @param offset offset within register
  100. * @param d annotated delay if loosly-timed access
  101. * @return true if access is successful
  102. */
  103. bool write(const uint8_t *data, size_t length, uint64_t offset, sc_core::sc_time d) override {
  104. assert("Access out of range" && offset + length <= sizeof(DATATYPE));
  105. auto temp(storage);
  106. auto beg = reinterpret_cast<uint8_t *>(&temp) + offset;
  107. std::copy(data, data + length, beg);
  108. if (wr_cb) return wr_cb(*this, temp, d);
  109. storage = (temp & wrmask) | (storage & ~wrmask);
  110. return true;
  111. }
  112. /**
  113. * read function from resource_access_if
  114. *
  115. * @param data data buffer to read
  116. * @param length size of data to read
  117. * @param offset offset within register
  118. * @param d annotated delay if loosly-timed access
  119. * @return true if access is successful
  120. */
  121. bool read(uint8_t *data, size_t length, uint64_t offset, sc_core::sc_time d) const override {
  122. assert("Access out of range" && offset + length <= sizeof(DATATYPE));
  123. auto temp(storage);
  124. if (rd_cb) {
  125. if (!rd_cb(*this, temp, d)) return false;
  126. } else
  127. temp &= rdmask;
  128. auto beg = reinterpret_cast<uint8_t *>(&temp) + offset;
  129. std::copy(beg, beg + length, data);
  130. return true;
  131. }
  132. /**
  133. * debug write function from resource_access_if
  134. *
  135. * @param data data to write
  136. * @param length size of data to write
  137. * @param offset offset within register
  138. * @return true if access is successful
  139. */
  140. bool write_dbg(const uint8_t *data, size_t length, uint64_t offset) override {
  141. assert("Offset out of range" && offset == 0);
  142. if (length != sizeof(DATATYPE)) return false;
  143. storage = *reinterpret_cast<const DATATYPE *>(data);
  144. return true;
  145. }
  146. /**
  147. * debug read function from resource_access_if
  148. *
  149. * @param data data buffer to read
  150. * @param length size of data to read
  151. * @param offset offset within register
  152. * @return true if access is successful
  153. */
  154. bool read_dbg(uint8_t *data, size_t length, uint64_t offset) const override {
  155. assert("Offset out of range" && offset == 0);
  156. if (length != sizeof(DATATYPE)) return false;
  157. *reinterpret_cast<DATATYPE *>(data) = storage;
  158. return true;
  159. }
  160. /**
  161. * cast operator
  162. */
  163. operator DATATYPE() const { return storage; }
  164. /**
  165. * get the storage
  166. *
  167. * @return copy of the underlying datatype
  168. */
  169. DATATYPE get() const { return storage; }
  170. /**
  171. * put value to storage
  172. *
  173. * @param data the data to store
  174. */
  175. void put(DATATYPE data) const { storage = data; }
  176. /**
  177. * copy assignment
  178. *
  179. * @param other the data
  180. * @return
  181. */
  182. this_type &operator=(DATATYPE other) {
  183. storage = other;
  184. return *this;
  185. }
  186. /**
  187. * unary or
  188. *
  189. * @param other
  190. * @return
  191. */
  192. this_type &operator|=(DATATYPE other) {
  193. storage |= other;
  194. return *this;
  195. }
  196. /**
  197. * unary and
  198. *
  199. * @param other
  200. * @return
  201. */
  202. this_type &operator&=(DATATYPE other) {
  203. storage &= other;
  204. return *this;
  205. }
  206. /**
  207. * set the read callback triggered upon a read request without forwarding the annotated time
  208. * this is primary for backward compatibility
  209. *
  210. * @param read_cb
  211. */
  212. void set_read_cb(std::function<bool(const this_type &, DATATYPE &)> read_cb) {
  213. rd_cb = [read_cb](const this_type &reg, DATATYPE &data, sc_core::sc_time delay) { return read_cb(reg, data); };
  214. }
  215. /**
  216. * set the read callback triggered upon a read request
  217. *
  218. * @param read_cb
  219. */
  220. void set_read_cb(std::function<bool(const this_type &, DATATYPE &, sc_core::sc_time)> read_cb) { rd_cb = read_cb; }
  221. /**
  222. * set the write callback triggered upon a write request without forwarding the annotated time
  223. * this is primary for backward compatibility
  224. *
  225. * @param write_cb
  226. */
  227. void set_write_cb(std::function<bool(this_type &, DATATYPE &)> write_cb) {
  228. wr_cb = [write_cb](this_type &reg, DATATYPE &data, sc_core::sc_time delay) { return write_cb(reg, data); };
  229. }
  230. /**
  231. * set the write callback triggered upon a write request
  232. *
  233. * @param write_cb
  234. */
  235. void set_write_cb(std::function<bool(this_type &, DATATYPE &, sc_core::sc_time)> write_cb) { wr_cb = write_cb; }
  236. /**
  237. * trace the register value to the given trace file
  238. *
  239. * @param trf
  240. */
  241. void trace(sc_core::sc_trace_file *trf) const override { sc_trace(trf, storage, this->name()); }
  242. //! the reset value
  243. const DATATYPE res_val;
  244. //! the SW read mask
  245. const DATATYPE rdmask;
  246. //! the SW write mask
  247. const DATATYPE wrmask;
  248. private:
  249. DATATYPE &storage;
  250. std::function<bool(const this_type &, DATATYPE &, sc_core::sc_time)> rd_cb;
  251. std::function<bool(this_type &, DATATYPE &, sc_core::sc_time)> wr_cb;
  252. util::delegate<bool(const this_type &, DATATYPE &, sc_core::sc_time)> rd_dlgt;
  253. util::delegate<bool(this_type &, DATATYPE &, sc_core::sc_time)> wr_dlgt;
  254. };
  255. }
  256. //! import the implementation into the scc namespace
  257. template <typename DATATYPE> using sc_register = impl::sc_register<typename impl::helper<DATATYPE>::Type>;
  258. /**
  259. * an indexed register aka a register file of a certain type
  260. */
  261. template <typename DATATYPE, size_t SIZE, size_t START = 0>
  262. class sc_register_indexed : public indexed_resource_access_if {
  263. public:
  264. using BASE_DATA_TYPE = typename impl::helper<DATATYPE>::Type;
  265. using value_type = sc_register<DATATYPE>;
  266. using pointer = value_type *;
  267. /**
  268. * the constructor
  269. *
  270. * @param nm
  271. * @param storage
  272. * @param reset_val
  273. * @param owner
  274. * @param rdmask
  275. * @param wrmask
  276. */
  277. sc_register_indexed(sc_core::sc_module_name nm, std::array<DATATYPE, SIZE> &storage, const DATATYPE reset_val,
  278. resetable &owner,
  279. BASE_DATA_TYPE rdmask = std::numeric_limits<BASE_DATA_TYPE>::is_signed
  280. ? -1
  281. : std::numeric_limits<BASE_DATA_TYPE>::max(),
  282. BASE_DATA_TYPE wrmask = std::numeric_limits<BASE_DATA_TYPE>::is_signed
  283. ? -1
  284. : std::numeric_limits<BASE_DATA_TYPE>::max()) {
  285. _reg_field = reinterpret_cast<pointer>(malloc(SIZE * sizeof(value_type)));
  286. for (size_t idx = START; idx < (START + SIZE); ++idx) {
  287. std::stringstream ss;
  288. ss << nm << idx;
  289. new (_reg_field + idx)
  290. sc_register<DATATYPE>(sc_core::sc_module_name(ss.str().c_str()), storage[idx], reset_val, owner, rdmask, wrmask);
  291. }
  292. }
  293. /**
  294. * the destructor
  295. */
  296. ~sc_register_indexed() override {
  297. free(_reg_field);
  298. }
  299. /**
  300. * get the size of the register file
  301. *
  302. * @return the size
  303. */
  304. size_t size() override { return SIZE; };
  305. /**
  306. * set the read callback triggered upon a read request without forwarding the annotated time
  307. * this is primary for backward compatibility
  308. *
  309. * @param read_cb
  310. */
  311. void set_read_cb(std::function<bool(const sc_register<DATATYPE> &, DATATYPE &)> read_cb) {
  312. for (std::unique_ptr<sc_register<DATATYPE>> reg : *this) reg->add_read_cb(read_cb);
  313. }
  314. /**
  315. * set the read callback triggered upon a read request
  316. *
  317. * @param read_cb
  318. */
  319. void set_read_cb(std::function<bool(const sc_register<DATATYPE> &, DATATYPE &, sc_core::sc_time)> read_cb) {
  320. for (std::unique_ptr<sc_register<DATATYPE>> reg : *this) reg->add_read_cb(read_cb);
  321. }
  322. /**
  323. * set the write callback triggered upon a write request without forwarding the annotated time
  324. * this is primary for backward compatibility
  325. *
  326. * @param write_cb
  327. */
  328. void set_write_cb(std::function<bool(sc_register<DATATYPE> &, DATATYPE &)> write_cb) {
  329. for (std::unique_ptr<sc_register<DATATYPE>> reg : *this) reg->add_write_cb(write_cb);
  330. }
  331. /**
  332. * set the write callback triggered upon a write request
  333. *
  334. * @param write_cb
  335. */
  336. void set_write_cb(std::function<bool(sc_register<DATATYPE> &, DATATYPE &, sc_core::sc_time)> write_cb) {
  337. for (std::unique_ptr<sc_register<DATATYPE>> reg : *this) reg->add_write_cb(write_cb);
  338. }
  339. /**
  340. * Element access operator
  341. *
  342. * @param idx the index
  343. * @return the data reference at the index
  344. */
  345. reference operator[](size_t idx) noexcept override { return *(_reg_field + idx); }
  346. /**
  347. * const element access operator
  348. *
  349. * @param idx
  350. * @return the data reference at the index
  351. */
  352. const_reference operator[](size_t idx) const noexcept override { return *(_reg_field + idx); }
  353. /**
  354. * Element access operator
  355. *
  356. * @param idx the index
  357. * @return the data reference at the index
  358. */
  359. reference at(size_t idx) override {
  360. assert("access out of bound" && idx < SIZE);
  361. return *(_reg_field + idx);
  362. }
  363. /**
  364. * const element access operator
  365. *
  366. * @param idx
  367. * @return the data reference at the index
  368. */
  369. const_reference at(size_t idx) const override {
  370. assert("access out of bound" && idx < SIZE);
  371. return *(_reg_field + idx);
  372. }
  373. private:
  374. value_type *_reg_field;
  375. };
  376. /**
  377. * alias class to map template argument read an write mask to constructor arguments
  378. */
  379. template <typename DATATYPE, DATATYPE WRMASK = impl::get_max_uval<DATATYPE>(),
  380. DATATYPE RDMASK = impl::get_max_uval<DATATYPE>()>
  381. class sc_register_masked : public sc_register<DATATYPE> {
  382. public:
  383. sc_register_masked(sc_core::sc_module_name nm, DATATYPE &storage, const DATATYPE reset_val, resetable &owner)
  384. : sc_register<DATATYPE>(nm, storage, reset_val, owner, RDMASK, WRMASK) {}
  385. };
  386. }
  387. #endif /* _SYSC_REGISTER_H_ */