Verwendeter Programmcode in Studienarbeit für ESY1B zum Thema "Verifikation mit SystemVerilog und Python"
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

i2c_master_byte_ctrl.v 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /////////////////////////////////////////////////////////////////////
  2. //// ////
  3. //// WISHBONE rev.B2 compliant I2C Master byte-controller ////
  4. //// ////
  5. //// ////
  6. //// Author: Richard Herveille ////
  7. //// richard@asics.ws ////
  8. //// www.asics.ws ////
  9. //// ////
  10. //// Downloaded from: http://www.opencores.org/projects/i2c/ ////
  11. //// ////
  12. /////////////////////////////////////////////////////////////////////
  13. //// ////
  14. //// Copyright (C) 2001 Richard Herveille ////
  15. //// richard@asics.ws ////
  16. //// ////
  17. //// This source file may be used and distributed without ////
  18. //// restriction provided that this copyright statement is not ////
  19. //// removed from the file and that any derivative work contains ////
  20. //// the original copyright notice and the associated disclaimer.////
  21. //// ////
  22. //// THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY ////
  23. //// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED ////
  24. //// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS ////
  25. //// FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL THE AUTHOR ////
  26. //// OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, ////
  27. //// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES ////
  28. //// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE ////
  29. //// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR ////
  30. //// BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF ////
  31. //// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT ////
  32. //// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT ////
  33. //// OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE ////
  34. //// POSSIBILITY OF SUCH DAMAGE. ////
  35. //// ////
  36. /////////////////////////////////////////////////////////////////////
  37. // CVS Log
  38. //
  39. // $Id: i2c_master_byte_ctrl.v,v 1.1 2008-11-08 13:15:10 sfielding Exp $
  40. //
  41. // $Date: 2008-11-08 13:15:10 $
  42. // $Revision: 1.1 $
  43. // $Author: sfielding $
  44. // $Locker: $
  45. // $State: Exp $
  46. //
  47. // Change History:
  48. // $Log: not supported by cvs2svn $
  49. // Revision 1.7 2004/02/18 11:40:46 rherveille
  50. // Fixed a potential bug in the statemachine. During a 'stop' 2 cmd_ack signals were generated. Possibly canceling a new start command.
  51. //
  52. // Revision 1.6 2003/08/09 07:01:33 rherveille
  53. // Fixed a bug in the Arbitration Lost generation caused by delay on the (external) sda line.
  54. // Fixed a potential bug in the byte controller's host-acknowledge generation.
  55. //
  56. // Revision 1.5 2002/12/26 15:02:32 rherveille
  57. // Core is now a Multimaster I2C controller
  58. //
  59. // Revision 1.4 2002/11/30 22:24:40 rherveille
  60. // Cleaned up code
  61. //
  62. // Revision 1.3 2001/11/05 11:59:25 rherveille
  63. // Fixed wb_ack_o generation bug.
  64. // Fixed bug in the byte_controller statemachine.
  65. // Added headers.
  66. //
  67. // synopsys translate_off
  68. `include "timescale.v"
  69. // synopsys translate_on
  70. `include "i2c_master_defines.v"
  71. module i2c_master_byte_ctrl (
  72. clk, rst, nReset, ena, clk_cnt, start, stop, read, write, ack_in, din,
  73. cmd_ack, ack_out, dout, i2c_busy, i2c_al, scl_i, scl_o, scl_oen, sda_i, sda_o, sda_oen );
  74. //
  75. // inputs & outputs
  76. //
  77. input clk; // master clock
  78. input rst; // synchronous active high reset
  79. input nReset; // asynchronous active low reset
  80. input ena; // core enable signal
  81. input [15:0] clk_cnt; // 4x SCL
  82. // control inputs
  83. input start;
  84. input stop;
  85. input read;
  86. input write;
  87. input ack_in;
  88. input [7:0] din;
  89. // status outputs
  90. output cmd_ack;
  91. reg cmd_ack;
  92. output ack_out;
  93. reg ack_out;
  94. output i2c_busy;
  95. output i2c_al;
  96. output [7:0] dout;
  97. // I2C signals
  98. input scl_i;
  99. output scl_o;
  100. output scl_oen;
  101. input sda_i;
  102. output sda_o;
  103. output sda_oen;
  104. //
  105. // Variable declarations
  106. //
  107. // statemachine
  108. parameter [4:0] ST_IDLE = 5'b0_0000;
  109. parameter [4:0] ST_START = 5'b0_0001;
  110. parameter [4:0] ST_READ = 5'b0_0010;
  111. parameter [4:0] ST_WRITE = 5'b0_0100;
  112. parameter [4:0] ST_ACK = 5'b0_1000;
  113. parameter [4:0] ST_STOP = 5'b1_0000;
  114. // signals for bit_controller
  115. reg [3:0] core_cmd;
  116. reg core_txd;
  117. wire core_ack, core_rxd;
  118. // signals for shift register
  119. reg [7:0] sr; //8bit shift register
  120. reg shift, ld;
  121. // signals for state machine
  122. wire go;
  123. reg [2:0] dcnt;
  124. wire cnt_done;
  125. //
  126. // Module body
  127. //
  128. // hookup bit_controller
  129. i2c_master_bit_ctrl bit_controller (
  130. .clk ( clk ),
  131. .rst ( rst ),
  132. .nReset ( nReset ),
  133. .ena ( ena ),
  134. .clk_cnt ( clk_cnt ),
  135. .cmd ( core_cmd ),
  136. .cmd_ack ( core_ack ),
  137. .busy ( i2c_busy ),
  138. .al ( i2c_al ),
  139. .din ( core_txd ),
  140. .dout ( core_rxd ),
  141. .scl_i ( scl_i ),
  142. .scl_o ( scl_o ),
  143. .scl_oen ( scl_oen ),
  144. .sda_i ( sda_i ),
  145. .sda_o ( sda_o ),
  146. .sda_oen ( sda_oen )
  147. );
  148. // generate go-signal
  149. assign go = (read | write | stop) & ~cmd_ack;
  150. // assign dout output to shift-register
  151. assign dout = sr;
  152. // generate shift register
  153. always @(posedge clk or negedge nReset)
  154. if (!nReset)
  155. sr <= #1 8'h0;
  156. else if (rst)
  157. sr <= #1 8'h0;
  158. else if (ld)
  159. sr <= #1 din;
  160. else if (shift)
  161. sr <= #1 {sr[6:0], core_rxd};
  162. // generate counter
  163. always @(posedge clk or negedge nReset)
  164. if (!nReset)
  165. dcnt <= #1 3'h0;
  166. else if (rst)
  167. dcnt <= #1 3'h0;
  168. else if (ld)
  169. dcnt <= #1 3'h7;
  170. else if (shift)
  171. dcnt <= #1 dcnt - 3'h1;
  172. assign cnt_done = ~(|dcnt);
  173. //
  174. // state machine
  175. //
  176. reg [4:0] c_state; // synopsis enum_state
  177. always @(posedge clk or negedge nReset)
  178. if (!nReset)
  179. begin
  180. core_cmd <= #1 `I2C_CMD_NOP;
  181. core_txd <= #1 1'b0;
  182. shift <= #1 1'b0;
  183. ld <= #1 1'b0;
  184. cmd_ack <= #1 1'b0;
  185. c_state <= #1 ST_IDLE;
  186. ack_out <= #1 1'b0;
  187. end
  188. else if (rst | i2c_al)
  189. begin
  190. core_cmd <= #1 `I2C_CMD_NOP;
  191. core_txd <= #1 1'b0;
  192. shift <= #1 1'b0;
  193. ld <= #1 1'b0;
  194. cmd_ack <= #1 1'b0;
  195. c_state <= #1 ST_IDLE;
  196. ack_out <= #1 1'b0;
  197. end
  198. else
  199. begin
  200. // initially reset all signals
  201. core_txd <= #1 sr[7];
  202. shift <= #1 1'b0;
  203. ld <= #1 1'b0;
  204. cmd_ack <= #1 1'b0;
  205. case (c_state) // synopsys full_case parallel_case
  206. ST_IDLE:
  207. if (go)
  208. begin
  209. if (start)
  210. begin
  211. c_state <= #1 ST_START;
  212. core_cmd <= #1 `I2C_CMD_START;
  213. end
  214. else if (read)
  215. begin
  216. c_state <= #1 ST_READ;
  217. core_cmd <= #1 `I2C_CMD_READ;
  218. end
  219. else if (write)
  220. begin
  221. c_state <= #1 ST_WRITE;
  222. core_cmd <= #1 `I2C_CMD_WRITE;
  223. end
  224. else // stop
  225. begin
  226. c_state <= #1 ST_STOP;
  227. core_cmd <= #1 `I2C_CMD_STOP;
  228. end
  229. ld <= #1 1'b1;
  230. end
  231. ST_START:
  232. if (core_ack)
  233. begin
  234. if (read)
  235. begin
  236. c_state <= #1 ST_READ;
  237. core_cmd <= #1 `I2C_CMD_READ;
  238. end
  239. else
  240. begin
  241. c_state <= #1 ST_WRITE;
  242. core_cmd <= #1 `I2C_CMD_WRITE;
  243. end
  244. ld <= #1 1'b1;
  245. end
  246. ST_WRITE:
  247. if (core_ack)
  248. if (cnt_done)
  249. begin
  250. c_state <= #1 ST_ACK;
  251. core_cmd <= #1 `I2C_CMD_READ;
  252. end
  253. else
  254. begin
  255. c_state <= #1 ST_WRITE; // stay in same state
  256. core_cmd <= #1 `I2C_CMD_WRITE; // write next bit
  257. shift <= #1 1'b1;
  258. end
  259. ST_READ:
  260. if (core_ack)
  261. begin
  262. if (cnt_done)
  263. begin
  264. c_state <= #1 ST_ACK;
  265. core_cmd <= #1 `I2C_CMD_WRITE;
  266. end
  267. else
  268. begin
  269. c_state <= #1 ST_READ; // stay in same state
  270. core_cmd <= #1 `I2C_CMD_READ; // read next bit
  271. end
  272. shift <= #1 1'b1;
  273. core_txd <= #1 ack_in;
  274. end
  275. ST_ACK:
  276. if (core_ack)
  277. begin
  278. if (stop)
  279. begin
  280. c_state <= #1 ST_STOP;
  281. core_cmd <= #1 `I2C_CMD_STOP;
  282. end
  283. else
  284. begin
  285. c_state <= #1 ST_IDLE;
  286. core_cmd <= #1 `I2C_CMD_NOP;
  287. // generate command acknowledge signal
  288. cmd_ack <= #1 1'b1;
  289. end
  290. // assign ack_out output to bit_controller_rxd (contains last received bit)
  291. ack_out <= #1 core_rxd;
  292. core_txd <= #1 1'b1;
  293. end
  294. else
  295. core_txd <= #1 ack_in;
  296. ST_STOP:
  297. if (core_ack)
  298. begin
  299. c_state <= #1 ST_IDLE;
  300. core_cmd <= #1 `I2C_CMD_NOP;
  301. // generate command acknowledge signal
  302. cmd_ack <= #1 1'b1;
  303. end
  304. endcase
  305. end
  306. endmodule