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.

serialInterface.v 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. //////////////////////////////////////////////////////////////////////
  2. //// ////
  3. //// serialInterface.v ////
  4. //// ////
  5. //// This file is part of the i2cSlave opencores effort.
  6. //// <http://www.opencores.org/cores//> ////
  7. //// ////
  8. //// Module Description: ////
  9. //// Perform all serial to parallel, and parallel
  10. //// to serial conversions. Perform device address matching
  11. //// Handle arbitrary length I2C reads terminated by NAK
  12. //// from host, and arbitrary length I2C writes terminated
  13. //// by STOP from host
  14. //// The second byte of a I2C write is always interpreted
  15. //// as a register address, and becomes the base register address
  16. //// for all read and write transactions.
  17. //// I2C WRITE: devAddr, regAddr, data[regAddr], data[regAddr+1], ..... data[regAddr+N]
  18. //// I2C READ: data[regAddr], data[regAddr+1], ..... data[regAddr+N]
  19. //// Note that when regAddR reaches 255 it will automatically wrap round to 0
  20. //// ////
  21. //// To Do: ////
  22. ////
  23. //// ////
  24. //// Author(s): ////
  25. //// - Steve Fielding, sfielding@base2designs.com ////
  26. //// ////
  27. //////////////////////////////////////////////////////////////////////
  28. //// ////
  29. //// Copyright (C) 2008 Steve Fielding and OPENCORES.ORG ////
  30. //// ////
  31. //// This source file may be used and distributed without ////
  32. //// restriction provided that this copyright statement is not ////
  33. //// removed from the file and that any derivative work contains ////
  34. //// the original copyright notice and the associated disclaimer. ////
  35. //// ////
  36. //// This source file is free software; you can redistribute it ////
  37. //// and/or modify it under the terms of the GNU Lesser General ////
  38. //// Public License as published by the Free Software Foundation; ////
  39. //// either version 2.1 of the License, or (at your option) any ////
  40. //// later version. ////
  41. //// ////
  42. //// This source is distributed in the hope that it will be ////
  43. //// useful, but WITHOUT ANY WARRANTY; without even the implied ////
  44. //// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR ////
  45. //// PURPOSE. See the GNU Lesser General Public License for more ////
  46. //// details. ////
  47. //// ////
  48. //// You should have received a copy of the GNU Lesser General ////
  49. //// Public License along with this source; if not, download it ////
  50. //// from <http://www.opencores.org/lgpl.shtml> ////
  51. //// ////
  52. //////////////////////////////////////////////////////////////////////
  53. //
  54. `include "timescale.v"
  55. `include "i2cSlave_define.v"
  56. module serialInterface (clearStartStopDet, clk, dataIn, dataOut, regAddr, rst, scl, sdaIn, sdaOut, startStopDetState, writeEn);
  57. input clk;
  58. input [7:0]dataIn;
  59. input rst;
  60. input scl;
  61. input sdaIn;
  62. input [1:0]startStopDetState;
  63. output clearStartStopDet;
  64. output [7:0]dataOut;
  65. output [15:0]regAddr;
  66. output sdaOut;
  67. output writeEn;
  68. reg clearStartStopDet, next_clearStartStopDet;
  69. wire clk;
  70. wire [7:0]dataIn;
  71. reg [7:0]dataOut, next_dataOut;
  72. reg [15:0]regAddr, next_regAddr;
  73. reg regAddr_hiByte = 1'b0; //high and low byte of regAddr
  74. wire rst;
  75. wire scl;
  76. wire sdaIn;
  77. reg sdaOut, next_sdaOut;
  78. wire [1:0]startStopDetState;
  79. reg writeEn, next_writeEn;
  80. // diagram signals declarations
  81. reg [2:0]bitCnt, next_bitCnt;
  82. reg [7:0]rxData, next_rxData;
  83. reg [1:0]streamSt, next_streamSt;
  84. reg [7:0]txData, next_txData;
  85. // BINARY ENCODED state machine: SISt
  86. // State codes definitions:
  87. `define START 4'b0000
  88. `define CHK_RD_WR 4'b0001
  89. `define READ_RD_LOOP 4'b0010
  90. `define READ_WT_HI 4'b0011
  91. `define READ_CHK_LOOP_FIN 4'b0100
  92. `define READ_WT_LO 4'b0101
  93. `define READ_WT_ACK 4'b0110
  94. `define WRITE_WT_LO 4'b0111
  95. `define WRITE_WT_HI 4'b1000
  96. `define WRITE_CHK_LOOP_FIN 4'b1001
  97. `define WRITE_LOOP_WT_LO 4'b1010
  98. `define WRITE_ST_LOOP 4'b1011
  99. `define WRITE_WT_LO2 4'b1100
  100. `define WRITE_WT_HI2 4'b1101
  101. `define WRITE_CLR_WR 4'b1110
  102. `define WRITE_CLR_ST_STOP 4'b1111
  103. reg [3:0]CurrState_SISt, NextState_SISt;
  104. // Diagram actions (continuous assignments allowed only: assign ...)
  105. // diagram ACTION
  106. // Machine: SISt
  107. // NextState logic (combinatorial)
  108. always @ (startStopDetState or streamSt or scl or txData or bitCnt or rxData or sdaIn or regAddr or dataIn or sdaOut or writeEn or dataOut or clearStartStopDet or CurrState_SISt)
  109. begin
  110. NextState_SISt <= CurrState_SISt;
  111. // Set default values for outputs and signals
  112. next_streamSt <= streamSt;
  113. next_txData <= txData;
  114. next_rxData <= rxData;
  115. next_sdaOut <= sdaOut;
  116. next_writeEn <= writeEn;
  117. next_dataOut <= dataOut;
  118. next_bitCnt <= bitCnt;
  119. next_clearStartStopDet <= clearStartStopDet;
  120. next_regAddr <= regAddr;
  121. case (CurrState_SISt) // synopsys parallel_case full_case
  122. `START:
  123. begin
  124. next_streamSt <= `STREAM_IDLE;
  125. next_txData <= 8'h00;
  126. next_rxData <= 8'h00;
  127. next_sdaOut <= 1'b1;
  128. next_writeEn <= 1'b0;
  129. next_dataOut <= 8'h00;
  130. next_bitCnt <= 3'b000;
  131. next_clearStartStopDet <= 1'b0;
  132. NextState_SISt <= `CHK_RD_WR;
  133. end
  134. `CHK_RD_WR:
  135. begin
  136. if (streamSt == `STREAM_READ)
  137. begin
  138. NextState_SISt <= `READ_RD_LOOP;
  139. next_txData <= dataIn;
  140. next_regAddr <= regAddr + 1'b1;
  141. next_bitCnt <= 3'b001;
  142. end
  143. else
  144. begin
  145. NextState_SISt <= `WRITE_WT_HI;
  146. next_rxData <= 8'h00;
  147. end
  148. end
  149. `READ_RD_LOOP:
  150. begin
  151. if (scl == 1'b0)
  152. begin
  153. NextState_SISt <= `READ_WT_HI;
  154. next_sdaOut <= txData [7];
  155. next_txData <= {txData [6:0], 1'b0};
  156. end
  157. end
  158. `READ_WT_HI:
  159. begin
  160. if (scl == 1'b1)
  161. begin
  162. NextState_SISt <= `READ_CHK_LOOP_FIN;
  163. end
  164. end
  165. `READ_CHK_LOOP_FIN:
  166. begin
  167. if (bitCnt == 3'b000)
  168. begin
  169. NextState_SISt <= `READ_WT_LO;
  170. end
  171. else
  172. begin
  173. NextState_SISt <= `READ_RD_LOOP;
  174. next_bitCnt <= bitCnt + 1'b1;
  175. end
  176. end
  177. `READ_WT_LO:
  178. begin
  179. if (scl == 1'b0)
  180. begin
  181. NextState_SISt <= `READ_WT_ACK;
  182. next_sdaOut <= 1'b1;
  183. end
  184. end
  185. `READ_WT_ACK:
  186. begin
  187. if (scl == 1'b1)
  188. begin
  189. NextState_SISt <= `CHK_RD_WR;
  190. if (sdaIn == `I2C_NAK)
  191. next_streamSt <= `STREAM_IDLE;
  192. end
  193. end
  194. `WRITE_WT_LO:
  195. begin
  196. if ((scl == 1'b0) && (startStopDetState == `STOP_DET ||
  197. (streamSt == `STREAM_IDLE && startStopDetState == `NULL_DET)))
  198. begin
  199. NextState_SISt <= `WRITE_CLR_ST_STOP;
  200. case (startStopDetState)
  201. `NULL_DET:
  202. next_bitCnt <= bitCnt + 1'b1;
  203. `START_DET: begin
  204. next_streamSt <= `STREAM_IDLE;
  205. next_rxData <= 8'h00;
  206. end
  207. default: ;
  208. endcase
  209. next_streamSt <= `STREAM_IDLE;
  210. next_clearStartStopDet <= 1'b1;
  211. end
  212. else if (scl == 1'b0)
  213. begin
  214. NextState_SISt <= `WRITE_ST_LOOP;
  215. case (startStopDetState)
  216. `NULL_DET:
  217. next_bitCnt <= bitCnt + 1'b1;
  218. `START_DET: begin
  219. next_streamSt <= `STREAM_IDLE;
  220. next_rxData <= 8'h00;
  221. end
  222. default: ;
  223. endcase
  224. end
  225. end
  226. `WRITE_WT_HI:
  227. begin
  228. if (scl == 1'b1)
  229. begin
  230. NextState_SISt <= `WRITE_WT_LO;
  231. next_rxData <= {rxData [6:0], sdaIn};
  232. next_bitCnt <= 3'b000;
  233. end
  234. end
  235. `WRITE_CHK_LOOP_FIN:
  236. begin
  237. if (bitCnt == 3'b111)
  238. begin
  239. NextState_SISt <= `WRITE_CLR_WR;
  240. next_sdaOut <= `I2C_ACK;
  241. case (streamSt)
  242. `STREAM_IDLE: begin
  243. if (rxData[7:1] == `I2C_ADDRESS && startStopDetState == `START_DET) begin
  244. if (rxData[0] == 1'b1)
  245. next_streamSt <= `STREAM_READ;
  246. else
  247. next_streamSt <= `STREAM_WRITE_ADDR;
  248. end
  249. else
  250. next_sdaOut <= `I2C_NAK;
  251. end
  252. `STREAM_WRITE_ADDR: begin
  253. if(regAddr_hiByte == 0) begin
  254. next_regAddr[15:8] <= rxData;
  255. regAddr_hiByte <= 1;
  256. end
  257. else begin
  258. next_streamSt <= `STREAM_WRITE_DATA;
  259. next_regAddr[7:0] <= rxData;
  260. regAddr_hiByte <= 0;
  261. end
  262. end
  263. `STREAM_WRITE_DATA: begin
  264. next_dataOut <= rxData;
  265. next_writeEn <= 1'b1;
  266. end
  267. default:
  268. next_streamSt <= streamSt;
  269. endcase
  270. end
  271. else
  272. begin
  273. NextState_SISt <= `WRITE_ST_LOOP;
  274. next_bitCnt <= bitCnt + 1'b1;
  275. end
  276. end
  277. `WRITE_LOOP_WT_LO:
  278. begin
  279. if (scl == 1'b0)
  280. begin
  281. NextState_SISt <= `WRITE_CHK_LOOP_FIN;
  282. end
  283. end
  284. `WRITE_ST_LOOP:
  285. begin
  286. if (scl == 1'b1)
  287. begin
  288. NextState_SISt <= `WRITE_LOOP_WT_LO;
  289. next_rxData <= {rxData [6:0], sdaIn};
  290. end
  291. end
  292. `WRITE_WT_LO2:
  293. begin
  294. if (scl == 1'b0)
  295. begin
  296. NextState_SISt <= `CHK_RD_WR;
  297. next_sdaOut <= 1'b1;
  298. end
  299. end
  300. `WRITE_WT_HI2:
  301. begin
  302. next_clearStartStopDet <= 1'b0;
  303. if (scl == 1'b1)
  304. begin
  305. NextState_SISt <= `WRITE_WT_LO2;
  306. end
  307. end
  308. `WRITE_CLR_WR:
  309. begin
  310. if (writeEn == 1'b1)
  311. next_regAddr <= regAddr + 1'b1;
  312. next_writeEn <= 1'b0;
  313. next_clearStartStopDet <= 1'b1;
  314. NextState_SISt <= `WRITE_WT_HI2;
  315. end
  316. `WRITE_CLR_ST_STOP:
  317. begin
  318. next_clearStartStopDet <= 1'b0;
  319. NextState_SISt <= `CHK_RD_WR;
  320. end
  321. endcase
  322. end
  323. // Current State Logic (sequential)
  324. always @ (posedge clk)
  325. begin
  326. if (rst == 1'b1)
  327. CurrState_SISt <= `START;
  328. else
  329. CurrState_SISt <= NextState_SISt;
  330. end
  331. // Registered outputs logic
  332. always @ (posedge clk)
  333. begin
  334. if (rst == 1'b1)
  335. begin
  336. sdaOut <= 1'b1;
  337. writeEn <= 1'b0;
  338. dataOut <= 8'h00;
  339. clearStartStopDet <= 1'b0;
  340. // regAddr <= // Initialization in the reset state or default value required!!
  341. streamSt <= `STREAM_IDLE;
  342. txData <= 8'h00;
  343. rxData <= 8'h00;
  344. bitCnt <= 3'b000;
  345. end
  346. else
  347. begin
  348. sdaOut <= next_sdaOut;
  349. writeEn <= next_writeEn;
  350. dataOut <= next_dataOut;
  351. clearStartStopDet <= next_clearStartStopDet;
  352. regAddr <= next_regAddr;
  353. streamSt <= next_streamSt;
  354. txData <= next_txData;
  355. rxData <= next_rxData;
  356. bitCnt <= next_bitCnt;
  357. end
  358. end
  359. endmodule