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.

i2cSlave.v 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. //////////////////////////////////////////////////////////////////////
  2. //// ////
  3. //// i2cSlave.v ////
  4. //// ////
  5. //// This file is part of the i2cSlave opencores effort.
  6. //// <http://www.opencores.org/cores//> ////
  7. //// ////
  8. //// Module Description: ////
  9. //// You will need to modify this file to implement your
  10. //// interface.
  11. //// ////
  12. //// To Do: ////
  13. ////
  14. //// ////
  15. //// Author(s): ////
  16. //// - Steve Fielding, sfielding@base2designs.com ////
  17. //// ////
  18. //////////////////////////////////////////////////////////////////////
  19. //// ////
  20. //// Copyright (C) 2008 Steve Fielding and OPENCORES.ORG ////
  21. //// ////
  22. //// This source file may be used and distributed without ////
  23. //// restriction provided that this copyright statement is not ////
  24. //// removed from the file and that any derivative work contains ////
  25. //// the original copyright notice and the associated disclaimer. ////
  26. //// ////
  27. //// This source file is free software; you can redistribute it ////
  28. //// and/or modify it under the terms of the GNU Lesser General ////
  29. //// Public License as published by the Free Software Foundation; ////
  30. //// either version 2.1 of the License, or (at your option) any ////
  31. //// later version. ////
  32. //// ////
  33. //// This source is distributed in the hope that it will be ////
  34. //// useful, but WITHOUT ANY WARRANTY; without even the implied ////
  35. //// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR ////
  36. //// PURPOSE. See the GNU Lesser General Public License for more ////
  37. //// details. ////
  38. //// ////
  39. //// You should have received a copy of the GNU Lesser General ////
  40. //// Public License along with this source; if not, download it ////
  41. //// from <http://www.opencores.org/lgpl.shtml> ////
  42. //// ////
  43. //////////////////////////////////////////////////////////////////////
  44. //
  45. `include "i2cSlave_define.v"
  46. module i2cSlave (
  47. clk,
  48. rst,
  49. sda,
  50. scl,
  51. tb_readEn,
  52. tb_writeEn,
  53. tb_addr,
  54. tb_dataIn,
  55. tb_dataOut
  56. );
  57. input clk;
  58. input rst;
  59. inout sda;
  60. input scl;
  61. // tb interface
  62. input tb_readEn;
  63. input tb_writeEn;
  64. input [15:0] tb_addr;
  65. input [15:0] tb_dataIn;
  66. output [15:0] tb_dataOut;
  67. // local wires and regs
  68. reg sdaDeb;
  69. reg sclDeb;
  70. reg [`DEB_I2C_LEN-1:0] sdaPipe;
  71. reg [`DEB_I2C_LEN-1:0] sclPipe;
  72. reg [`SCL_DEL_LEN-1:0] sclDelayed;
  73. reg [`SDA_DEL_LEN-1:0] sdaDelayed;
  74. reg [1:0] startStopDetState;
  75. wire clearStartStopDet;
  76. wire sdaOut;
  77. wire sdaIn;
  78. wire [15:0] regAddr;
  79. wire [7:0] dataToRegIF;
  80. wire writeEn;
  81. wire [7:0] dataFromRegIF;
  82. reg [1:0] rstPipe;
  83. wire rstSyncToClk;
  84. reg startEdgeDet;
  85. assign sda = (sdaOut == 1'b0) ? 1'b0 : 1'bz;
  86. assign sdaIn = sda;
  87. // sync rst rsing edge to clk
  88. always @(posedge clk) begin
  89. if (rst == 1'b1)
  90. rstPipe <= 2'b11;
  91. else
  92. rstPipe <= {rstPipe[0], 1'b0};
  93. end
  94. assign rstSyncToClk = rstPipe[1];
  95. // debounce sda and scl
  96. always @(posedge clk) begin
  97. if (rstSyncToClk == 1'b1) begin
  98. sdaPipe <= {`DEB_I2C_LEN{1'b1}};
  99. sdaDeb <= 1'b1;
  100. sclPipe <= {`DEB_I2C_LEN{1'b1}};
  101. sclDeb <= 1'b1;
  102. end
  103. else begin
  104. sdaPipe <= {sdaPipe[`DEB_I2C_LEN-2:0], sdaIn};
  105. sclPipe <= {sclPipe[`DEB_I2C_LEN-2:0], scl};
  106. if (&sclPipe[`DEB_I2C_LEN-1:1] == 1'b1)
  107. sclDeb <= 1'b1;
  108. else if (|sclPipe[`DEB_I2C_LEN-1:1] == 1'b0)
  109. sclDeb <= 1'b0;
  110. if (&sdaPipe[`DEB_I2C_LEN-1:1] == 1'b1)
  111. sdaDeb <= 1'b1;
  112. else if (|sdaPipe[`DEB_I2C_LEN-1:1] == 1'b0)
  113. sdaDeb <= 1'b0;
  114. end
  115. end
  116. // delay scl and sda
  117. // sclDelayed is used as a delayed sampling clock
  118. // sdaDelayed is only used for start stop detection
  119. // Because sda hold time from scl falling is 0nS
  120. // sda must be delayed with respect to scl to avoid incorrect
  121. // detection of start/stop at scl falling edge.
  122. always @(posedge clk) begin
  123. if (rstSyncToClk == 1'b1) begin
  124. sclDelayed <= {`SCL_DEL_LEN{1'b1}};
  125. sdaDelayed <= {`SDA_DEL_LEN{1'b1}};
  126. end
  127. else begin
  128. sclDelayed <= {sclDelayed[`SCL_DEL_LEN-2:0], sclDeb};
  129. sdaDelayed <= {sdaDelayed[`SDA_DEL_LEN-2:0], sdaDeb};
  130. end
  131. end
  132. // start stop detection
  133. always @(posedge clk) begin
  134. if (rstSyncToClk == 1'b1) begin
  135. startStopDetState <= `NULL_DET;
  136. startEdgeDet <= 1'b0;
  137. end
  138. else begin
  139. if (sclDeb == 1'b1 && sdaDelayed[`SDA_DEL_LEN-2] == 1'b0 && sdaDelayed[`SDA_DEL_LEN-1] == 1'b1)
  140. startEdgeDet <= 1'b1;
  141. else
  142. startEdgeDet <= 1'b0;
  143. if (clearStartStopDet == 1'b1)
  144. startStopDetState <= `NULL_DET;
  145. else if (sclDeb == 1'b1) begin
  146. if (sdaDelayed[`SDA_DEL_LEN-2] == 1'b1 && sdaDelayed[`SDA_DEL_LEN-1] == 1'b0)
  147. startStopDetState <= `STOP_DET;
  148. else if (sdaDelayed[`SDA_DEL_LEN-2] == 1'b0 && sdaDelayed[`SDA_DEL_LEN-1] == 1'b1)
  149. startStopDetState <= `START_DET;
  150. end
  151. end
  152. end
  153. registerInterface u_registerInterface(
  154. .clk(clk),
  155. .addr(regAddr),
  156. .dataIn(dataToRegIF),
  157. .writeEn(writeEn),
  158. .dataOut(dataFromRegIF),
  159. .tb_readEn(tb_readEn),
  160. .tb_writeEn(tb_writeEn),
  161. .tb_addr(tb_addr),
  162. .tb_dataIn(tb_dataIn),
  163. .tb_dataOut(tb_dataOut)
  164. );
  165. serialInterface u_serialInterface (
  166. .clk(clk),
  167. .rst(rstSyncToClk | startEdgeDet),
  168. .dataIn(dataFromRegIF),
  169. .dataOut(dataToRegIF),
  170. .writeEn(writeEn),
  171. .regAddr(regAddr),
  172. .scl(sclDelayed[`SCL_DEL_LEN-1]),
  173. .sdaIn(sdaDeb),
  174. .sdaOut(sdaOut),
  175. .startStopDetState(startStopDetState),
  176. .clearStartStopDet(clearStartStopDet)
  177. );
  178. endmodule