24LC64.v 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. // *******************************************************************************************************
  2. // ** **
  3. // ** 24LC64.v - Microchip 24LC64 64K-BIT I2C SERIAL EEPROM (VCC = +2.5V TO +5.5V) **
  4. // ** **
  5. // *******************************************************************************************************
  6. // ** **
  7. // ** This information is distributed under license from Young Engineering. **
  8. // ** COPYRIGHT (c) 2009 YOUNG ENGINEERING **
  9. // ** ALL RIGHTS RESERVED **
  10. // ** **
  11. // ** **
  12. // ** Young Engineering provides design expertise for the digital world **
  13. // ** Started in 1990, Young Engineering offers products and services for your electronic design **
  14. // ** project. We have the expertise in PCB, FPGA, ASIC, firmware, and software design. **
  15. // ** From concept to prototype to production, we can help you. **
  16. // ** **
  17. // ** http://www.young-engineering.com/ **
  18. // ** **
  19. // *******************************************************************************************************
  20. // ** This information is provided to you for your convenience and use with Microchip products only. **
  21. // ** Microchip disclaims all liability arising from this information and its use. **
  22. // ** **
  23. // ** THIS INFORMATION IS PROVIDED "AS IS." MICROCHIP MAKES NO REPRESENTATION OR WARRANTIES OF **
  24. // ** ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO **
  25. // ** THE INFORMATION PROVIDED TO YOU, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, **
  26. // ** PERFORMANCE, MERCHANTABILITY, NON-INFRINGEMENT, OR FITNESS FOR PURPOSE. **
  27. // ** MICROCHIP IS NOT LIABLE, UNDER ANY CIRCUMSTANCES, FOR SPECIAL, INCIDENTAL OR CONSEQUENTIAL **
  28. // ** DAMAGES, FOR ANY REASON WHATSOEVER. **
  29. // ** **
  30. // ** It is your responsibility to ensure that your application meets with your specifications. **
  31. // ** **
  32. // *******************************************************************************************************
  33. // ** Revision : 1.4 **
  34. // ** Modified Date : 02/04/2009 **
  35. // ** Revision History: **
  36. // ** **
  37. // ** 10/01/2003: Initial design **
  38. // ** 07/19/2004: Fixed the timing checks and the open-drain modeling for SDA. **
  39. // ** 01/06/2006: Changed the legal information in the header **
  40. // ** 12/04/2006: Corrected timing checks to reference proper clock edges **
  41. // ** Added timing check for Tbuf (bus free time) **
  42. // ** Reduced memory blocks to single, monolithic array **
  43. // ** 02/04/2009: Added timing checks for tSU_WP and tHD_WP **
  44. // ** **
  45. // *******************************************************************************************************
  46. // ** TABLE OF CONTENTS **
  47. // *******************************************************************************************************
  48. // **---------------------------------------------------------------------------------------------------**
  49. // ** DECLARATIONS **
  50. // **---------------------------------------------------------------------------------------------------**
  51. // **---------------------------------------------------------------------------------------------------**
  52. // ** INITIALIZATION **
  53. // **---------------------------------------------------------------------------------------------------**
  54. // **---------------------------------------------------------------------------------------------------**
  55. // ** CORE LOGIC **
  56. // **---------------------------------------------------------------------------------------------------**
  57. // ** 1.01: START Bit Detection **
  58. // ** 1.02: STOP Bit Detection **
  59. // ** 1.03: Input Shift Register **
  60. // ** 1.04: Input Bit Counter **
  61. // ** 1.05: Control Byte Register **
  62. // ** 1.06: Byte Address Register **
  63. // ** 1.07: Write Data Buffer **
  64. // ** 1.08: Acknowledge Generator **
  65. // ** 1.09: Acknowledge Detect **
  66. // ** 1.10: Write Cycle Timer **
  67. // ** 1.11: Write Cycle Processor **
  68. // ** 1.12: Read Data Multiplexor **
  69. // ** 1.13: Read Data Processor **
  70. // ** 1.14: SDA Data I/O Buffer **
  71. // ** **
  72. // **---------------------------------------------------------------------------------------------------**
  73. // ** DEBUG LOGIC **
  74. // **---------------------------------------------------------------------------------------------------**
  75. // ** 2.01: Memory Data Bytes **
  76. // ** 2.02: Write Data Buffer **
  77. // ** **
  78. // **---------------------------------------------------------------------------------------------------**
  79. // ** TIMING CHECKS **
  80. // **---------------------------------------------------------------------------------------------------**
  81. // ** **
  82. // *******************************************************************************************************
  83. `timescale 1ns/10ps
  84. module M24LC64 (A0, A1, A2, WP, SDA, SCL, RESET);
  85. input A0; // chip select bit
  86. input A1; // chip select bit
  87. input A2; // chip select bit
  88. input WP; // write protect pin
  89. inout SDA; // serial data I/O
  90. input SCL; // serial data clock
  91. input RESET; // system reset
  92. // *******************************************************************************************************
  93. // ** DECLARATIONS **
  94. // *******************************************************************************************************
  95. reg SDA_DO; // serial data - output
  96. reg SDA_OE; // serial data - output enable
  97. wire SDA_DriveEnable; // serial data output enable
  98. reg SDA_DriveEnableDlyd; // serial data output enable - delayed
  99. wire [02:00] ChipAddress; // hardwired chip address
  100. reg [03:00] BitCounter; // serial bit counter
  101. reg START_Rcvd; // START bit received flag
  102. reg STOP_Rcvd; // STOP bit received flag
  103. reg CTRL_Rcvd; // control byte received flag
  104. reg ADHI_Rcvd; // byte address hi received flag
  105. reg ADLO_Rcvd; // byte address lo received flag
  106. reg MACK_Rcvd; // master acknowledge received flag
  107. reg WrCycle; // memory write cycle
  108. reg RdCycle; // memory read cycle
  109. reg [07:00] ShiftRegister; // input data shift register
  110. reg [07:00] ControlByte; // control byte register
  111. wire RdWrBit; // read/write control bit
  112. reg [12:00] StartAddress; // memory access starting address
  113. reg [04:00] PageAddress; // memory page address
  114. reg [07:00] WrDataByte [0:31]; // memory write data buffer
  115. wire [07:00] RdDataByte; // memory read data
  116. reg [15:00] WrCounter; // write buffer counter
  117. reg [04:00] WrPointer; // write buffer pointer
  118. reg [12:00] RdPointer; // read address pointer
  119. reg WriteActive; // memory write cycle active
  120. reg [07:00] MemoryBlock [0:8191]; // EEPROM data memory array
  121. integer LoopIndex; // iterative loop index
  122. integer tAA; // timing parameter
  123. integer tWC; // timing parameter
  124. // *******************************************************************************************************
  125. // ** INITIALIZATION **
  126. // *******************************************************************************************************
  127. //----------------------------
  128. //------写数据间隔改动----------
  129. initial tAA = 900; // SCL to SDA output delay
  130. initial tWC = 500; // memory write cycle time
  131. // initial tAA = 900; // SCL to SDA output delay
  132. // initial tWC = 5000000; // memory write cycle time
  133. initial begin
  134. SDA_DO = 0;
  135. SDA_OE = 0;
  136. end
  137. initial begin
  138. START_Rcvd = 0;
  139. STOP_Rcvd = 0;
  140. CTRL_Rcvd = 0;
  141. ADHI_Rcvd = 0;
  142. ADLO_Rcvd = 0;
  143. MACK_Rcvd = 0;
  144. end
  145. initial begin
  146. BitCounter = 0;
  147. ControlByte = 0;
  148. end
  149. initial begin
  150. WrCycle = 0;
  151. RdCycle = 0;
  152. WriteActive = 0;
  153. end
  154. assign ChipAddress = {A2,A1,A0};
  155. // *******************************************************************************************************
  156. // ** CORE LOGIC **
  157. // *******************************************************************************************************
  158. // -------------------------------------------------------------------------------------------------------
  159. // 1.01: START Bit Detection
  160. // -------------------------------------------------------------------------------------------------------
  161. always @(negedge SDA) begin
  162. if (SCL == 1) begin
  163. START_Rcvd <= 1;
  164. STOP_Rcvd <= 0;
  165. CTRL_Rcvd <= 0;
  166. ADHI_Rcvd <= 0;
  167. ADLO_Rcvd <= 0;
  168. MACK_Rcvd <= 0;
  169. WrCycle <= #1 0;
  170. RdCycle <= #1 0;
  171. BitCounter <= 0;
  172. end
  173. end
  174. // -------------------------------------------------------------------------------------------------------
  175. // 1.02: STOP Bit Detection
  176. // -------------------------------------------------------------------------------------------------------
  177. always @(posedge SDA) begin
  178. if (SCL == 1) begin
  179. START_Rcvd <= 0;
  180. STOP_Rcvd <= 1;
  181. CTRL_Rcvd <= 0;
  182. ADHI_Rcvd <= 0;
  183. ADLO_Rcvd <= 0;
  184. MACK_Rcvd <= 0;
  185. WrCycle <= #1 0;
  186. RdCycle <= #1 0;
  187. BitCounter <= 10;
  188. end
  189. end
  190. // -------------------------------------------------------------------------------------------------------
  191. // 1.03: Input Shift Register
  192. // -------------------------------------------------------------------------------------------------------
  193. always @(posedge SCL) begin
  194. ShiftRegister[00] <= SDA;
  195. ShiftRegister[01] <= ShiftRegister[00];
  196. ShiftRegister[02] <= ShiftRegister[01];
  197. ShiftRegister[03] <= ShiftRegister[02];
  198. ShiftRegister[04] <= ShiftRegister[03];
  199. ShiftRegister[05] <= ShiftRegister[04];
  200. ShiftRegister[06] <= ShiftRegister[05];
  201. ShiftRegister[07] <= ShiftRegister[06];
  202. end
  203. // -------------------------------------------------------------------------------------------------------
  204. // 1.04: Input Bit Counter
  205. // -------------------------------------------------------------------------------------------------------
  206. always @(posedge SCL) begin
  207. if (BitCounter < 10) BitCounter <= BitCounter + 1;
  208. end
  209. // -------------------------------------------------------------------------------------------------------
  210. // 1.05: Control Byte Register
  211. // -------------------------------------------------------------------------------------------------------
  212. always @(negedge SCL) begin
  213. if (START_Rcvd & (BitCounter == 8)) begin
  214. if (!WriteActive & (ShiftRegister[07:01] == {4'b1010,ChipAddress[02:00]})) begin
  215. if (ShiftRegister[00] == 0) WrCycle <= 1;
  216. if (ShiftRegister[00] == 1) RdCycle <= 1;
  217. ControlByte <= ShiftRegister[07:00];
  218. CTRL_Rcvd <= 1;
  219. end
  220. START_Rcvd <= 0;
  221. end
  222. end
  223. assign RdWrBit = ControlByte[00];
  224. // -------------------------------------------------------------------------------------------------------
  225. // 1.06: Byte Address Register
  226. // -------------------------------------------------------------------------------------------------------
  227. always @(negedge SCL) begin
  228. if (CTRL_Rcvd & (BitCounter == 8)) begin
  229. if (RdWrBit == 0) begin
  230. StartAddress[12:08] <= ShiftRegister[04:00];
  231. RdPointer[12:08] <= ShiftRegister[04:00];
  232. ADHI_Rcvd <= 1;
  233. end
  234. WrCounter <= 0;
  235. WrPointer <= 0;
  236. CTRL_Rcvd <= 0;
  237. end
  238. end
  239. always @(negedge SCL) begin
  240. if (ADHI_Rcvd & (BitCounter == 8)) begin
  241. if (RdWrBit == 0) begin
  242. StartAddress[07:00] <= ShiftRegister[07:00];
  243. RdPointer[07:00] <= ShiftRegister[07:00];
  244. ADLO_Rcvd <= 1;
  245. end
  246. WrCounter <= 0;
  247. WrPointer <= 0;
  248. ADHI_Rcvd <= 0;
  249. end
  250. end
  251. // -------------------------------------------------------------------------------------------------------
  252. // 1.07: Write Data Buffer
  253. // -------------------------------------------------------------------------------------------------------
  254. always @(negedge SCL) begin
  255. if (ADLO_Rcvd & (BitCounter == 8)) begin
  256. if (RdWrBit == 0) begin
  257. WrDataByte[WrPointer] <= ShiftRegister[07:00];
  258. WrCounter <= WrCounter + 1;
  259. WrPointer <= WrPointer + 1;
  260. end
  261. end
  262. end
  263. // -------------------------------------------------------------------------------------------------------
  264. // 1.08: Acknowledge Generator
  265. // -------------------------------------------------------------------------------------------------------
  266. always @(negedge SCL) begin
  267. if (!WriteActive) begin
  268. if (BitCounter == 8) begin
  269. if (WrCycle | (START_Rcvd & (ShiftRegister[07:01] == {4'b1010,ChipAddress[02:00]}))) begin
  270. SDA_DO <= 0;
  271. SDA_OE <= 1;
  272. end
  273. end
  274. if (BitCounter == 9) begin
  275. BitCounter <= 0;
  276. if (!RdCycle) begin
  277. SDA_DO <= 0;
  278. SDA_OE <= 0;
  279. end
  280. end
  281. end
  282. end
  283. // -------------------------------------------------------------------------------------------------------
  284. // 1.09: Acknowledge Detect
  285. // -------------------------------------------------------------------------------------------------------
  286. always @(posedge SCL) begin
  287. if (RdCycle & (BitCounter == 8)) begin
  288. if ((SDA == 0) & (SDA_OE == 0)) MACK_Rcvd <= 1;
  289. end
  290. end
  291. always @(negedge SCL) MACK_Rcvd <= 0;
  292. // -------------------------------------------------------------------------------------------------------
  293. // 1.10: Write Cycle Timer
  294. // -------------------------------------------------------------------------------------------------------
  295. always @(posedge STOP_Rcvd) begin
  296. if (WrCycle & (WP == 0) & (WrCounter > 0)) begin
  297. WriteActive = 1;
  298. #(tWC);
  299. WriteActive = 0;
  300. end
  301. end
  302. always @(posedge STOP_Rcvd) begin
  303. #(1.0);
  304. STOP_Rcvd = 0;
  305. end
  306. // -------------------------------------------------------------------------------------------------------
  307. // 1.11: Write Cycle Processor
  308. // -------------------------------------------------------------------------------------------------------
  309. always @(negedge WriteActive) begin
  310. for (LoopIndex = 0; LoopIndex < WrCounter; LoopIndex = LoopIndex + 1) begin
  311. PageAddress = StartAddress[04:00] + LoopIndex;
  312. MemoryBlock[{StartAddress[12:05],PageAddress[04:00]}] = WrDataByte[LoopIndex[04:00]];
  313. end
  314. end
  315. // -------------------------------------------------------------------------------------------------------
  316. // 1.12: Read Data Multiplexor
  317. // -------------------------------------------------------------------------------------------------------
  318. always @(negedge SCL) begin
  319. if (BitCounter == 8) begin
  320. if (WrCycle & ADLO_Rcvd) begin
  321. RdPointer <= StartAddress + WrPointer + 1;
  322. end
  323. if (RdCycle) begin
  324. RdPointer <= RdPointer + 1;
  325. end
  326. end
  327. end
  328. assign RdDataByte = MemoryBlock[RdPointer[12:00]];
  329. // -------------------------------------------------------------------------------------------------------
  330. // 1.13: Read Data Processor
  331. // -------------------------------------------------------------------------------------------------------
  332. always @(negedge SCL) begin
  333. if (RdCycle) begin
  334. if (BitCounter == 8) begin
  335. SDA_DO <= 0;
  336. SDA_OE <= 0;
  337. end
  338. else if (BitCounter == 9) begin
  339. SDA_DO <= RdDataByte[07];
  340. if (MACK_Rcvd) SDA_OE <= 1;
  341. end
  342. else begin
  343. SDA_DO <= RdDataByte[7-BitCounter];
  344. end
  345. end
  346. end
  347. // -------------------------------------------------------------------------------------------------------
  348. // 1.14: SDA Data I/O Buffer
  349. // -------------------------------------------------------------------------------------------------------
  350. bufif1 (SDA, 1'b0, SDA_DriveEnableDlyd);
  351. assign SDA_DriveEnable = !SDA_DO & SDA_OE;
  352. always @(SDA_DriveEnable) SDA_DriveEnableDlyd <= #(tAA) SDA_DriveEnable;
  353. // *******************************************************************************************************
  354. // ** DEBUG LOGIC **
  355. // *******************************************************************************************************
  356. // -------------------------------------------------------------------------------------------------------
  357. // 2.01: Memory Data Bytes
  358. // -------------------------------------------------------------------------------------------------------
  359. wire [07:00] MemoryByte_000 = MemoryBlock[00];
  360. wire [07:00] MemoryByte_001 = MemoryBlock[01];
  361. wire [07:00] MemoryByte_002 = MemoryBlock[02];
  362. wire [07:00] MemoryByte_003 = MemoryBlock[03];
  363. wire [07:00] MemoryByte_004 = MemoryBlock[04];
  364. wire [07:00] MemoryByte_005 = MemoryBlock[05];
  365. wire [07:00] MemoryByte_006 = MemoryBlock[06];
  366. wire [07:00] MemoryByte_007 = MemoryBlock[07];
  367. wire [07:00] MemoryByte_008 = MemoryBlock[08];
  368. wire [07:00] MemoryByte_009 = MemoryBlock[09];
  369. wire [07:00] MemoryByte_00A = MemoryBlock[10];
  370. wire [07:00] MemoryByte_00B = MemoryBlock[11];
  371. wire [07:00] MemoryByte_00C = MemoryBlock[12];
  372. wire [07:00] MemoryByte_00D = MemoryBlock[13];
  373. wire [07:00] MemoryByte_00E = MemoryBlock[14];
  374. wire [07:00] MemoryByte_00F = MemoryBlock[15];
  375. // -------------------------------------------------------------------------------------------------------
  376. // 2.02: Write Data Buffer
  377. // -------------------------------------------------------------------------------------------------------
  378. wire [07:00] WriteData_00 = WrDataByte[00];
  379. wire [07:00] WriteData_01 = WrDataByte[01];
  380. wire [07:00] WriteData_02 = WrDataByte[02];
  381. wire [07:00] WriteData_03 = WrDataByte[03];
  382. wire [07:00] WriteData_04 = WrDataByte[04];
  383. wire [07:00] WriteData_05 = WrDataByte[05];
  384. wire [07:00] WriteData_06 = WrDataByte[06];
  385. wire [07:00] WriteData_07 = WrDataByte[07];
  386. wire [07:00] WriteData_08 = WrDataByte[08];
  387. wire [07:00] WriteData_09 = WrDataByte[09];
  388. wire [07:00] WriteData_0A = WrDataByte[10];
  389. wire [07:00] WriteData_0B = WrDataByte[11];
  390. wire [07:00] WriteData_0C = WrDataByte[12];
  391. wire [07:00] WriteData_0D = WrDataByte[13];
  392. wire [07:00] WriteData_0E = WrDataByte[14];
  393. wire [07:00] WriteData_0F = WrDataByte[15];
  394. wire [07:00] WriteData_10 = WrDataByte[16];
  395. wire [07:00] WriteData_11 = WrDataByte[17];
  396. wire [07:00] WriteData_12 = WrDataByte[18];
  397. wire [07:00] WriteData_13 = WrDataByte[19];
  398. wire [07:00] WriteData_14 = WrDataByte[20];
  399. wire [07:00] WriteData_15 = WrDataByte[21];
  400. wire [07:00] WriteData_16 = WrDataByte[22];
  401. wire [07:00] WriteData_17 = WrDataByte[23];
  402. wire [07:00] WriteData_18 = WrDataByte[24];
  403. wire [07:00] WriteData_19 = WrDataByte[25];
  404. wire [07:00] WriteData_1A = WrDataByte[26];
  405. wire [07:00] WriteData_1B = WrDataByte[27];
  406. wire [07:00] WriteData_1C = WrDataByte[28];
  407. wire [07:00] WriteData_1D = WrDataByte[29];
  408. wire [07:00] WriteData_1E = WrDataByte[30];
  409. wire [07:00] WriteData_1F = WrDataByte[31];
  410. // *******************************************************************************************************
  411. // ** TIMING CHECKS **
  412. // *******************************************************************************************************
  413. wire TimingCheckEnable = (RESET == 0) & (SDA_OE == 0);
  414. wire StopTimingCheckEnable = TimingCheckEnable && SCL;
  415. //--------------------------------
  416. //-------仿真时时序约束需改动--------
  417. //--------------------------------
  418. specify
  419. specparam
  420. tHI = 600, // SCL pulse width - high
  421. // tLO = 1300, // SCL pulse width - low
  422. tLO = 600,
  423. tSU_STA = 600, // SCL to SDA setup time
  424. tHD_STA = 600, // SCL to SDA hold time
  425. tSU_DAT = 100, // SDA to SCL setup time
  426. tSU_STO = 600, // SCL to SDA setup time
  427. tSU_WP = 600, // WP to SDA setup time
  428. tHD_WP = 1300, // WP to SDA hold time
  429. // tBUF = 1300; // Bus free time
  430. tBUF = 600;
  431. $width (posedge SCL, tHI);
  432. $width (negedge SCL, tLO);
  433. $width (posedge SDA &&& SCL, tBUF);
  434. $setup (posedge SCL, negedge SDA &&& TimingCheckEnable, tSU_STA);
  435. $setup (SDA, posedge SCL &&& TimingCheckEnable, tSU_DAT);
  436. $setup (posedge SCL, posedge SDA &&& TimingCheckEnable, tSU_STO);
  437. $setup (WP, posedge SDA &&& StopTimingCheckEnable, tSU_WP);
  438. $hold (negedge SDA &&& TimingCheckEnable, negedge SCL, tHD_STA);
  439. $hold (posedge SDA &&& StopTimingCheckEnable, WP, tHD_WP);
  440. endspecify
  441. endmodule