M24LC04B.v 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. // *******************************************************************************************************
  2. // ** **
  3. // ** 24LC04B.v - Microchip 24LC04B 4K-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) 2003 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.3 **
  34. // ** Modified Date : 12/04/2006 **
  35. // ** Revision History: **
  36. // ** **
  37. // ** 02/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. // ** **
  43. // *******************************************************************************************************
  44. // ** TABLE OF CONTENTS **
  45. // *******************************************************************************************************
  46. // **---------------------------------------------------------------------------------------------------**
  47. // ** DECLARATIONS **
  48. // **---------------------------------------------------------------------------------------------------**
  49. // **---------------------------------------------------------------------------------------------------**
  50. // ** INITIALIZATION **
  51. // **---------------------------------------------------------------------------------------------------**
  52. // **---------------------------------------------------------------------------------------------------**
  53. // ** CORE LOGIC **
  54. // **---------------------------------------------------------------------------------------------------**
  55. // ** 1.01: START Bit Detection **
  56. // ** 1.02: STOP Bit Detection **
  57. // ** 1.03: Input Shift Register **
  58. // ** 1.04: Input Bit Counter **
  59. // ** 1.05: Control Byte Register **
  60. // ** 1.06: Byte Address Register **
  61. // ** 1.07: Write Data Buffer **
  62. // ** 1.08: Acknowledge Generator **
  63. // ** 1.09: Acknowledge Detect **
  64. // ** 1.10: Write Cycle Timer **
  65. // ** 1.11: Write Cycle Processor **
  66. // ** 1.12: Read Data Multiplexor **
  67. // ** 1.13: Read Data Processor **
  68. // ** 1.14: SDA Data I/O Buffer **
  69. // ** **
  70. // **---------------------------------------------------------------------------------------------------**
  71. // ** DEBUG LOGIC **
  72. // **---------------------------------------------------------------------------------------------------**
  73. // ** 2.01: Memory Data Bytes **
  74. // ** 2.02: Write Data Buffer **
  75. // ** **
  76. // **---------------------------------------------------------------------------------------------------**
  77. // ** TIMING CHECKS **
  78. // **---------------------------------------------------------------------------------------------------**
  79. // ** **
  80. // *******************************************************************************************************
  81. `timescale 1ns/10ps
  82. module M24LC04B (A0, A1, A2, WP, SDA, SCL, RESET);
  83. input A0; // unconnected pin
  84. input A1; // unconnected pin
  85. input A2; // unconnected pin
  86. input WP; // write protect pin
  87. inout SDA; // serial data I/O
  88. input SCL; // serial data clock
  89. input RESET; // system reset
  90. // *******************************************************************************************************
  91. // ** DECLARATIONS **
  92. // *******************************************************************************************************
  93. reg SDA_DO; // serial data - output
  94. reg SDA_OE; // serial data - output enable
  95. wire SDA_DriveEnable; // serial data output enable
  96. reg SDA_DriveEnableDlyd; // serial data output enable - delayed
  97. wire [02:00] ChipAddress; // hardwired chip address
  98. reg [03:00] BitCounter; // serial bit counter
  99. reg START_Rcvd; // START bit received flag
  100. reg STOP_Rcvd; // STOP bit received flag
  101. reg CTRL_Rcvd; // control byte received flag
  102. reg ADDR_Rcvd; // byte address received flag
  103. reg MACK_Rcvd; // master acknowledge received flag
  104. reg WrCycle; // memory write cycle
  105. reg RdCycle; // memory read cycle
  106. reg [07:00] ShiftRegister; // input data shift register
  107. reg [07:00] ControlByte; // control byte register
  108. wire BlockSelect; // memory block select
  109. wire RdWrBit; // read/write control bit
  110. reg [08:00] StartAddress; // memory access starting address
  111. reg [03:00] PageAddress; // memory page address
  112. reg [07:00] WrDataByte [0:15]; // memory write data buffer
  113. wire [07:00] RdDataByte; // memory read data
  114. reg [15:00] WrCounter; // write buffer counter
  115. reg [03:00] WrPointer; // write buffer pointer
  116. reg [08:00] RdPointer; // read address pointer
  117. reg WriteActive; // memory write cycle active
  118. reg [07:00] MemoryBlock0 [0:255]; // EEPROM data memory array
  119. reg [07:00] MemoryBlock1 [0:255]; // EEPROM data memory array
  120. integer LoopIndex; // iterative loop index
  121. integer tAA; // timing parameter
  122. integer tWC; // timing parameter
  123. // *******************************************************************************************************
  124. // ** INITIALIZATION **
  125. // *******************************************************************************************************
  126. initial tAA = 900; // SCL to SDA output delay
  127. initial tWC = 5000000; // memory write cycle time
  128. initial begin
  129. SDA_DO = 0;
  130. SDA_OE = 0;
  131. end
  132. initial begin
  133. START_Rcvd = 0;
  134. STOP_Rcvd = 0;
  135. CTRL_Rcvd = 0;
  136. ADDR_Rcvd = 0;
  137. MACK_Rcvd = 0;
  138. end
  139. initial begin
  140. BitCounter = 0;
  141. ControlByte = 0;
  142. end
  143. initial begin
  144. WrCycle = 0;
  145. RdCycle = 0;
  146. WriteActive = 0;
  147. end
  148. assign ChipAddress = {A2,A1,A0};
  149. // *******************************************************************************************************
  150. // ** CORE LOGIC **
  151. // *******************************************************************************************************
  152. // -------------------------------------------------------------------------------------------------------
  153. // 1.01: START Bit Detection
  154. // -------------------------------------------------------------------------------------------------------
  155. always @(negedge SDA) begin
  156. if (SCL == 1) begin
  157. START_Rcvd <= 1;
  158. STOP_Rcvd <= 0;
  159. CTRL_Rcvd <= 0;
  160. ADDR_Rcvd <= 0;
  161. MACK_Rcvd <= 0;
  162. WrCycle <= #1 0;
  163. RdCycle <= #1 0;
  164. BitCounter <= 0;
  165. end
  166. end
  167. // -------------------------------------------------------------------------------------------------------
  168. // 1.02: STOP Bit Detection
  169. // -------------------------------------------------------------------------------------------------------
  170. always @(posedge SDA) begin
  171. if (SCL == 1) begin
  172. START_Rcvd <= 0;
  173. STOP_Rcvd <= 1;
  174. CTRL_Rcvd <= 0;
  175. ADDR_Rcvd <= 0;
  176. MACK_Rcvd <= 0;
  177. WrCycle <= #1 0;
  178. RdCycle <= #1 0;
  179. BitCounter <= 10;
  180. end
  181. end
  182. // -------------------------------------------------------------------------------------------------------
  183. // 1.03: Input Shift Register
  184. // -------------------------------------------------------------------------------------------------------
  185. always @(posedge SCL) begin
  186. ShiftRegister[00] <= SDA;
  187. ShiftRegister[01] <= ShiftRegister[00];
  188. ShiftRegister[02] <= ShiftRegister[01];
  189. ShiftRegister[03] <= ShiftRegister[02];
  190. ShiftRegister[04] <= ShiftRegister[03];
  191. ShiftRegister[05] <= ShiftRegister[04];
  192. ShiftRegister[06] <= ShiftRegister[05];
  193. ShiftRegister[07] <= ShiftRegister[06];
  194. end
  195. // -------------------------------------------------------------------------------------------------------
  196. // 1.04: Input Bit Counter
  197. // -------------------------------------------------------------------------------------------------------
  198. always @(posedge SCL) begin
  199. if (BitCounter < 10) BitCounter <= BitCounter + 1;
  200. end
  201. // -------------------------------------------------------------------------------------------------------
  202. // 1.05: Control Byte Register
  203. // -------------------------------------------------------------------------------------------------------
  204. always @(negedge SCL) begin
  205. if (START_Rcvd & (BitCounter == 8)) begin
  206. if (!WriteActive & (ShiftRegister[07:01] == {4'b1010,ChipAddress[02:00]})) begin
  207. if (ShiftRegister[00] == 0) WrCycle <= 1;
  208. if (ShiftRegister[00] == 1) RdCycle <= 1;
  209. ControlByte <= ShiftRegister[07:00];
  210. CTRL_Rcvd <= 1;
  211. end
  212. START_Rcvd <= 0;
  213. end
  214. end
  215. assign BlockSelect = ControlByte[01];
  216. assign RdWrBit = ControlByte[00];
  217. // -------------------------------------------------------------------------------------------------------
  218. // 1.06: Byte Address Register
  219. // -------------------------------------------------------------------------------------------------------
  220. always @(negedge SCL) begin
  221. if (CTRL_Rcvd & (BitCounter == 8)) begin
  222. if (RdWrBit == 0) begin
  223. StartAddress <= {BlockSelect,ShiftRegister[07:00]};
  224. RdPointer <= {BlockSelect,ShiftRegister[07:00]};
  225. ADDR_Rcvd <= 1;
  226. end
  227. WrCounter <= 0;
  228. WrPointer <= 0;
  229. CTRL_Rcvd <= 0;
  230. end
  231. end
  232. // -------------------------------------------------------------------------------------------------------
  233. // 1.07: Write Data Buffer
  234. // -------------------------------------------------------------------------------------------------------
  235. always @(negedge SCL) begin
  236. if (ADDR_Rcvd & (BitCounter == 8)) begin
  237. if ((WP == 0) & (RdWrBit == 0)) begin
  238. WrDataByte[WrPointer] <= ShiftRegister[07:00];
  239. WrCounter <= WrCounter + 1;
  240. WrPointer <= WrPointer + 1;
  241. end
  242. end
  243. end
  244. // -------------------------------------------------------------------------------------------------------
  245. // 1.08: Acknowledge Generator
  246. // -------------------------------------------------------------------------------------------------------
  247. always @(negedge SCL) begin
  248. if (!WriteActive) begin
  249. if (BitCounter == 8) begin
  250. if (WrCycle | (START_Rcvd & (ShiftRegister[07:01] == {4'b1010,ChipAddress[02:00]}))) begin
  251. SDA_DO <= 0;
  252. SDA_OE <= 1;
  253. end
  254. end
  255. if (BitCounter == 9) begin
  256. BitCounter <= 0;
  257. if (!RdCycle) begin
  258. SDA_DO <= 0;
  259. SDA_OE <= 0;
  260. end
  261. end
  262. end
  263. end
  264. // -------------------------------------------------------------------------------------------------------
  265. // 1.09: Acknowledge Detect
  266. // -------------------------------------------------------------------------------------------------------
  267. always @(posedge SCL) begin
  268. if (RdCycle & (BitCounter == 8)) begin
  269. if ((SDA == 0) & (SDA_OE == 0)) MACK_Rcvd <= 1;
  270. end
  271. end
  272. always @(negedge SCL) MACK_Rcvd <= 0;
  273. // -------------------------------------------------------------------------------------------------------
  274. // 1.10: Write Cycle Timer
  275. // -------------------------------------------------------------------------------------------------------
  276. always @(posedge STOP_Rcvd) begin
  277. if (WrCycle & (WP == 0) & (WrCounter > 0)) begin
  278. WriteActive = 1;
  279. #(tWC);
  280. WriteActive = 0;
  281. end
  282. end
  283. always @(posedge STOP_Rcvd) begin
  284. #(1.0);
  285. STOP_Rcvd = 0;
  286. end
  287. // -------------------------------------------------------------------------------------------------------
  288. // 1.11: Write Cycle Processor
  289. // -------------------------------------------------------------------------------------------------------
  290. always @(negedge WriteActive) begin
  291. for (LoopIndex = 0; LoopIndex < WrCounter; LoopIndex = LoopIndex + 1) begin
  292. if (StartAddress[08] == 0) begin
  293. PageAddress = StartAddress[03:00] + LoopIndex;
  294. MemoryBlock0[{StartAddress[07:04],PageAddress[03:00]}] = WrDataByte[LoopIndex[03:00]];
  295. end
  296. if (StartAddress[08] == 1) begin
  297. PageAddress = StartAddress[03:00] + LoopIndex;
  298. MemoryBlock1[{StartAddress[07:04],PageAddress[03:00]}] = WrDataByte[LoopIndex[03:00]];
  299. end
  300. end
  301. end
  302. // -------------------------------------------------------------------------------------------------------
  303. // 1.12: Read Data Multiplexor
  304. // -------------------------------------------------------------------------------------------------------
  305. always @(negedge SCL) begin
  306. if (BitCounter == 8) begin
  307. if (WrCycle & ADDR_Rcvd) begin
  308. RdPointer <= StartAddress + WrPointer + 1;
  309. end
  310. if (RdCycle) begin
  311. RdPointer <= RdPointer + 1;
  312. end
  313. end
  314. end
  315. assign RdDataByte = RdPointer[08] ? MemoryBlock1[RdPointer[07:00]] : MemoryBlock0[RdPointer[07:00]];
  316. // -------------------------------------------------------------------------------------------------------
  317. // 1.13: Read Data Processor
  318. // -------------------------------------------------------------------------------------------------------
  319. always @(negedge SCL) begin
  320. if (RdCycle) begin
  321. if (BitCounter == 8) begin
  322. SDA_DO <= 0;
  323. SDA_OE <= 0;
  324. end
  325. else if (BitCounter == 9) begin
  326. SDA_DO <= RdDataByte[07];
  327. if (MACK_Rcvd) SDA_OE <= 1;
  328. end
  329. else begin
  330. SDA_DO <= RdDataByte[7-BitCounter];
  331. end
  332. end
  333. end
  334. // -------------------------------------------------------------------------------------------------------
  335. // 1.14: SDA Data I/O Buffer
  336. // -------------------------------------------------------------------------------------------------------
  337. bufif1 (SDA, 1'b0, SDA_DriveEnableDlyd);
  338. assign SDA_DriveEnable = !SDA_DO & SDA_OE;
  339. always @(SDA_DriveEnable) SDA_DriveEnableDlyd <= #(tAA) SDA_DriveEnable;
  340. // *******************************************************************************************************
  341. // ** DEBUG LOGIC **
  342. // *******************************************************************************************************
  343. // -------------------------------------------------------------------------------------------------------
  344. // 2.01: Memory Data Bytes
  345. // -------------------------------------------------------------------------------------------------------
  346. wire [07:00] MemoryByte0_00 = MemoryBlock0[00];
  347. wire [07:00] MemoryByte0_01 = MemoryBlock0[01];
  348. wire [07:00] MemoryByte0_02 = MemoryBlock0[02];
  349. wire [07:00] MemoryByte0_03 = MemoryBlock0[03];
  350. wire [07:00] MemoryByte0_04 = MemoryBlock0[04];
  351. wire [07:00] MemoryByte0_05 = MemoryBlock0[05];
  352. wire [07:00] MemoryByte0_06 = MemoryBlock0[06];
  353. wire [07:00] MemoryByte0_07 = MemoryBlock0[07];
  354. wire [07:00] MemoryByte0_08 = MemoryBlock0[08];
  355. wire [07:00] MemoryByte0_09 = MemoryBlock0[09];
  356. wire [07:00] MemoryByte0_0A = MemoryBlock0[10];
  357. wire [07:00] MemoryByte0_0B = MemoryBlock0[11];
  358. wire [07:00] MemoryByte0_0C = MemoryBlock0[12];
  359. wire [07:00] MemoryByte0_0D = MemoryBlock0[13];
  360. wire [07:00] MemoryByte0_0E = MemoryBlock0[14];
  361. wire [07:00] MemoryByte0_0F = MemoryBlock0[15];
  362. wire [07:00] MemoryByte1_00 = MemoryBlock1[00];
  363. wire [07:00] MemoryByte1_01 = MemoryBlock1[01];
  364. wire [07:00] MemoryByte1_02 = MemoryBlock1[02];
  365. wire [07:00] MemoryByte1_03 = MemoryBlock1[03];
  366. wire [07:00] MemoryByte1_04 = MemoryBlock1[04];
  367. wire [07:00] MemoryByte1_05 = MemoryBlock1[05];
  368. wire [07:00] MemoryByte1_06 = MemoryBlock1[06];
  369. wire [07:00] MemoryByte1_07 = MemoryBlock1[07];
  370. wire [07:00] MemoryByte1_08 = MemoryBlock1[08];
  371. wire [07:00] MemoryByte1_09 = MemoryBlock1[09];
  372. wire [07:00] MemoryByte1_0A = MemoryBlock1[10];
  373. wire [07:00] MemoryByte1_0B = MemoryBlock1[11];
  374. wire [07:00] MemoryByte1_0C = MemoryBlock1[12];
  375. wire [07:00] MemoryByte1_0D = MemoryBlock1[13];
  376. wire [07:00] MemoryByte1_0E = MemoryBlock1[14];
  377. wire [07:00] MemoryByte1_0F = MemoryBlock1[15];
  378. // -------------------------------------------------------------------------------------------------------
  379. // 2.02: Write Data Buffer
  380. // -------------------------------------------------------------------------------------------------------
  381. wire [07:00] WriteData_0 = WrDataByte[00];
  382. wire [07:00] WriteData_1 = WrDataByte[01];
  383. wire [07:00] WriteData_2 = WrDataByte[02];
  384. wire [07:00] WriteData_3 = WrDataByte[03];
  385. wire [07:00] WriteData_4 = WrDataByte[04];
  386. wire [07:00] WriteData_5 = WrDataByte[05];
  387. wire [07:00] WriteData_6 = WrDataByte[06];
  388. wire [07:00] WriteData_7 = WrDataByte[07];
  389. wire [07:00] WriteData_8 = WrDataByte[08];
  390. wire [07:00] WriteData_9 = WrDataByte[09];
  391. wire [07:00] WriteData_A = WrDataByte[10];
  392. wire [07:00] WriteData_B = WrDataByte[11];
  393. wire [07:00] WriteData_C = WrDataByte[12];
  394. wire [07:00] WriteData_D = WrDataByte[13];
  395. wire [07:00] WriteData_E = WrDataByte[14];
  396. wire [07:00] WriteData_F = WrDataByte[15];
  397. // *******************************************************************************************************
  398. // ** TIMING CHECKS **
  399. // *******************************************************************************************************
  400. wire TimingCheckEnable = (RESET == 0) & (SDA_OE == 0);
  401. specify
  402. specparam
  403. tHI = 600, // SCL pulse width - high
  404. tLO = 1300, // SCL pulse width - low
  405. tSU_STA = 600, // SCL to SDA setup time
  406. tHD_STA = 600, // SCL to SDA hold time
  407. tSU_DAT = 100, // SDA to SCL setup time
  408. tSU_STO = 600, // SCL to SDA setup time
  409. tBUF = 1300; // Bus free time
  410. $width (posedge SCL, tHI);
  411. $width (negedge SCL, tLO);
  412. $width (posedge SDA &&& SCL, tBUF);
  413. $setup (posedge SCL, negedge SDA &&& TimingCheckEnable, tSU_STA);
  414. $setup (SDA, posedge SCL &&& TimingCheckEnable, tSU_DAT);
  415. $setup (posedge SCL, posedge SDA &&& TimingCheckEnable, tSU_STO);
  416. $hold (negedge SDA &&& TimingCheckEnable, negedge SCL, tHD_STA);
  417. endspecify
  418. endmodule