spi_rtl.cpp 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*******************************************************************************
  2. * Copyright (C) 2018 MINRES Technologies GmbH
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * 3. Neither the name of the copyright holder nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  23. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29. * POSSIBILITY OF SUCH DAMAGE.
  30. *
  31. *******************************************************************************/
  32. #include "scc/signal_initiator_mixin.h"
  33. #include "scc/time2tick.h"
  34. #include "sysc/SiFive/spi.h"
  35. #include "sysc/rtl/tl_uh_bfm.h"
  36. #include <VTLSPI.h>
  37. #include <scc/utilities.h>
  38. #include <tlm.h>
  39. #include <tlm/tlm_signal_conv.h>
  40. namespace sysc {
  41. namespace spi_impl {
  42. using namespace sc_core;
  43. class rtl : public spi {
  44. public:
  45. SC_HAS_PROCESS(beh);// NOLINT
  46. rtl(sc_module_name nm);
  47. ~rtl() override;
  48. // void trace( sc_trace_file* tf ) const override {
  49. // }
  50. private:
  51. sc_signal<bool> clock;
  52. sc_signal<uint32_t> a_bits_address;
  53. sc_signal<uint32_t> a_bits_data;
  54. sc_signal<bool> a_ready;
  55. sc_signal<bool> a_valid;
  56. sc_signal<uint32_t> a_bits_opcode;
  57. sc_signal<uint32_t> a_bits_param;
  58. sc_signal<uint32_t> a_bits_size;
  59. sc_signal<uint32_t> a_bits_source;
  60. sc_signal<uint32_t> a_bits_mask;
  61. sc_signal<bool> a_bits_corrupt;
  62. sc_signal<uint32_t> d_bits_data;
  63. sc_signal<bool> d_ready;
  64. sc_signal<bool> d_valid;
  65. sc_signal<uint32_t> d_bits_opcode;
  66. sc_signal<uint32_t> d_bits_size;
  67. sc_signal<uint32_t> d_bits_source;
  68. sc_signal<bool> sck;
  69. sc_signal<bool> dq_0_i;
  70. sc_signal<bool> dq_0_o;
  71. sc_signal<bool> dq_0_oe;
  72. sc_signal<bool> dq_1_i;
  73. sc_signal<bool> dq_1_o;
  74. sc_signal<bool> dq_1_oe;
  75. sc_signal<bool> dq_2_i;
  76. sc_signal<bool> dq_3_i;
  77. sc_signal<bool> cs_0;
  78. sc_signal<bool> cs_2;
  79. sc_signal<bool> cs_3;
  80. VTLSPI i_vtlspi;
  81. tl_uh_bfm i_tlbfm;
  82. scc::time2tick i_time2tick;
  83. tlm::sc_signal2tlm_signal<bool> i_sck_conv;
  84. tlm::sc_signal2tlm_signal<bool> i_mosi_conv;
  85. tlm::tlm_signal2sc_signal<bool> i_miso_conv;
  86. tlm::sc_signal2tlm_signal<bool> i_scs0_conv, i_scs2_conv, i_scs3_conv;
  87. scc::tlm_signal_bool_opt_out scs_1;
  88. };
  89. rtl::rtl(sc_module_name nm)
  90. : spi(nm)
  91. , NAMED(a_bits_address)
  92. , NAMED(a_bits_data)
  93. , NAMED(a_ready)
  94. , NAMED(a_valid)
  95. , NAMED(a_bits_opcode)
  96. , NAMED(a_bits_param)
  97. , NAMED(a_bits_size)
  98. , NAMED(a_bits_source)
  99. , NAMED(a_bits_mask)
  100. , NAMED(a_bits_corrupt)
  101. , NAMED(d_bits_data)
  102. , NAMED(d_ready)
  103. , NAMED(d_valid)
  104. , NAMED(d_bits_opcode)
  105. , NAMED(d_bits_size)
  106. , NAMED(d_bits_source)
  107. , NAMED(sck)
  108. , NAMED(dq_0_i)
  109. , NAMED(dq_0_o)
  110. , NAMED(dq_0_oe)
  111. , NAMED(dq_1_i)
  112. , NAMED(dq_1_o)
  113. , NAMED(dq_1_oe)
  114. , NAMED(dq_2_i)
  115. , NAMED(dq_3_i)
  116. , NAMED(cs_0)
  117. , NAMED(cs_2)
  118. , NAMED(cs_3)
  119. , NAMED(i_vtlspi)
  120. , NAMED(i_tlbfm, 0x10024000)
  121. , NAMED(i_time2tick)
  122. , NAMED(i_sck_conv)
  123. , NAMED(i_mosi_conv)
  124. , NAMED(i_miso_conv)
  125. , NAMED(i_scs0_conv)
  126. , NAMED(i_scs2_conv)
  127. , NAMED(i_scs3_conv)
  128. , NAMED(scs_1) {
  129. i_vtlspi.clock(clock);
  130. i_vtlspi.reset(rst_i);
  131. i_vtlspi.auto_int_xing_out_sync_0(irq_o);
  132. i_vtlspi.auto_control_xing_in_a_ready(a_ready);
  133. i_vtlspi.auto_control_xing_in_a_valid(a_valid);
  134. i_vtlspi.auto_control_xing_in_a_bits_address(a_bits_address);
  135. i_vtlspi.auto_control_xing_in_a_bits_data(a_bits_data);
  136. i_vtlspi.auto_control_xing_in_a_bits_opcode(a_bits_opcode);
  137. i_vtlspi.auto_control_xing_in_a_bits_param(a_bits_param);
  138. i_vtlspi.auto_control_xing_in_a_bits_size(a_bits_size);
  139. i_vtlspi.auto_control_xing_in_a_bits_source(a_bits_source);
  140. i_vtlspi.auto_control_xing_in_a_bits_mask(a_bits_mask);
  141. i_vtlspi.auto_control_xing_in_a_bits_corrupt(a_bits_corrupt);
  142. i_vtlspi.auto_control_xing_in_d_ready(d_ready);
  143. i_vtlspi.auto_control_xing_in_d_valid(d_valid);
  144. i_vtlspi.auto_control_xing_in_d_bits_data(d_bits_data);
  145. i_vtlspi.auto_control_xing_in_d_bits_opcode(d_bits_opcode);
  146. i_vtlspi.auto_control_xing_in_d_bits_size(d_bits_size);
  147. i_vtlspi.auto_control_xing_in_d_bits_source(d_bits_source);
  148. i_vtlspi.auto_io_out_sck(sck);
  149. i_vtlspi.auto_io_out_dq_0_i(dq_0_i);
  150. i_vtlspi.auto_io_out_dq_0_o(dq_0_o);
  151. i_vtlspi.auto_io_out_dq_0_oe(dq_0_oe);
  152. i_vtlspi.auto_io_out_dq_1_i(dq_1_i);
  153. i_vtlspi.auto_io_out_dq_1_o(dq_1_o);
  154. i_vtlspi.auto_io_out_dq_1_oe(dq_1_oe);
  155. i_vtlspi.auto_io_out_dq_2_i(dq_2_i);
  156. i_vtlspi.auto_io_out_dq_3_i(dq_3_i);
  157. i_vtlspi.auto_io_out_cs_0(cs_0);
  158. i_vtlspi.auto_io_out_cs_2(cs_2);
  159. i_vtlspi.auto_io_out_cs_3(cs_3);
  160. i_tlbfm.clock(clock);
  161. i_tlbfm.reset(rst_i);
  162. spi::socket(i_tlbfm.socket);
  163. i_tlbfm.a_ready(a_ready);
  164. i_tlbfm.a_valid(a_valid);
  165. i_tlbfm.a_bits_address(a_bits_address);
  166. i_tlbfm.a_bits_data(a_bits_data);
  167. i_tlbfm.d_bits_data(d_bits_data);
  168. i_tlbfm.a_bits_opcode(a_bits_opcode);
  169. i_tlbfm.a_bits_param(a_bits_param);
  170. i_tlbfm.a_bits_size(a_bits_size);
  171. i_tlbfm.a_bits_source(a_bits_source);
  172. i_tlbfm.a_bits_mask(a_bits_mask);
  173. i_tlbfm.a_bits_corrupt(a_bits_corrupt);
  174. i_tlbfm.d_ready(d_ready);
  175. i_tlbfm.d_valid(d_valid);
  176. i_tlbfm.d_bits_opcode(d_bits_opcode);
  177. i_tlbfm.d_bits_size(d_bits_size);
  178. i_tlbfm.d_bits_source(d_bits_source);
  179. i_time2tick.clk_i(clk_i);
  180. i_time2tick.clk_o(clock);
  181. i_sck_conv.s_i(sck);
  182. i_sck_conv.t_o(sck_o);
  183. i_mosi_conv.s_i(dq_0_o);
  184. i_mosi_conv.t_o(mosi_o);
  185. miso_i(i_miso_conv.t_i);
  186. i_miso_conv.s_o(dq_0_i);
  187. i_scs0_conv.s_i(cs_0);
  188. i_scs0_conv.t_o(scs_o[0]);
  189. i_scs2_conv.s_i(cs_2);
  190. i_scs2_conv.t_o(scs_o[2]);
  191. scs_1(scs_o[1]); // dummy to drive port
  192. i_scs3_conv.s_i(cs_3);
  193. i_scs3_conv.t_o(scs_o[3]);
  194. }
  195. rtl::~rtl() = default;
  196. }
  197. template <> std::unique_ptr<spi> spi::create<sysc::spi_impl::rtl>(sc_core::sc_module_name nm) {
  198. auto *res = new sysc::spi_impl::rtl(nm);
  199. return std::unique_ptr<spi>(res);
  200. }
  201. } /* namespace sysc */