Advertisement
Guest User

Untitled

a guest
Nov 19th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.42 KB | None | 0 0
  1. `timescale 1ns / 1ps
  2. //////////////////////////////////////////////////////////////////////////////////
  3. // Company: Dept. of Computer Science, National Chiao Tung University
  4. // Engineer: Chun-Jen Tsai
  5. //
  6. // Create Date: 2017/05/08 15:29:41
  7. // Design Name:
  8. // Module Name: lab6
  9. // Project Name:
  10. // Target Devices:
  11. // Tool Versions:
  12. // Description: The sample top module of lab 6: sd card reader. The behavior of
  13. // this module is as follows
  14. // 1. When the SD card is initialized, display a message on the LCD.
  15. // If the initialization fails, an error message will be shown.
  16. // 2. The user can then press usr_btn[2] to trigger the sd card
  17. // controller to read the super block of the sd card (located at
  18. // block # 8192) into the SRAM memory.
  19. // 3. During SD card reading time, the four LED lights will be turned on.
  20. // They will be turned off when the reading is done.
  21. // 4. The LCD will then displayer the sector just been read, and the
  22. // first byte of the sector.
  23. // 5. Everytime you press usr_btn[2], the next byte will be displayed.
  24. //
  25. // Dependencies: clk_divider, LCD_module, debounce, sd_card
  26. //
  27. // Revision:
  28. // Revision 0.01 - File Created
  29. // Additional Comments:
  30. //
  31. //////////////////////////////////////////////////////////////////////////////////
  32.  
  33. module lab6(
  34. // General system I/O ports
  35. input clk,
  36. input reset_n,
  37. input [3:0] usr_btn,
  38. output [3:0] usr_led,
  39.  
  40. // SD card specific I/O ports
  41. output spi_ss,
  42. output spi_sck,
  43. output spi_mosi,
  44. input spi_miso,
  45.  
  46. // 1602 LCD Module Interface
  47. output LCD_RS,
  48. output LCD_RW,
  49. output LCD_E,
  50. output [3:0] LCD_D
  51. );
  52.  
  53. localparam [3:0] S_MAIN_INIT = 3'b000, S_MAIN_IDLE = 3'b001,
  54. S_MAIN_WAIT = 3'b010, S_MAIN_READ = 3'b011,
  55. S_MAIN_DONE = 3'b100, S_MAIN_CAL = 3'b101,
  56. S_MAIN_SHOW = 3'b110;
  57.  
  58. // Declare system variables
  59. wire btn_level, btn_pressed;
  60. reg prev_btn_level;
  61. reg [5:0] send_counter;
  62. reg [2:0] P, P_next;
  63. reg [9:0] sd_counter;
  64. reg [9:0] done_counter;
  65. reg [9:0] counter=0;
  66. reg [9:0] counter_2=0;
  67. reg [9:0] counter_the;
  68. reg [9:0] start_counter;
  69. reg [7:0] data_byte;
  70. reg [31:0] blk_addr;
  71. reg [31:0] start_blk_addr;
  72. reg [3:0] dlab_tag_match=0, dlab_end_match=0, dlab_the_match=0;
  73. reg [9:0] num_the=0;
  74. reg [9:0] num_the_p=0;
  75. reg start_flag, end_flag;
  76. reg not_this_block, not_this_block_2;
  77. reg [127:0] row_A = "SD card cannot ";
  78. reg [127:0] row_B = "be initialized! ";
  79. reg [0:63] dlab_tag = "DLAB_TAG";
  80. reg [0:63] dlab_end = "DLAB_END";
  81. reg [0:23] THE = "THE";
  82. reg [0:23] the = "the";
  83. reg [0:56] print_out = " ";
  84. reg done_flag; // Signals the completion of reading one SD sector.
  85. reg [0:7] word="@", word_1="@", word_2='h1 ,word_3 = "!",word_4='h2, word_5, word_6, word_7;
  86. reg [0:31] p_addr, p_addr_1;
  87. // Declare SD card interface signals
  88. wire clk_sel;
  89. wire clk_500k;
  90. reg rd_req;
  91. reg [31:0] rd_addr;
  92. wire init_finished;
  93. wire [7:0] sd_dout;
  94. wire sd_valid;
  95.  
  96. // Declare the control/data signals of an SRAM memory block
  97. wire [7:0] data_in;
  98. wire [7:0] data_out;
  99. wire [8:0] sram_addr;
  100. wire sram_we, sram_en;
  101.  
  102. assign clk_sel = (init_finished)? clk : clk_500k; // clock for the SD controller
  103. assign usr_led = P;
  104.  
  105. clk_divider#(200) clk_divider0(
  106. .clk(clk),
  107. .reset(~reset_n),
  108. .clk_out(clk_500k)
  109. );
  110.  
  111. debounce btn_db0(
  112. .clk(clk),
  113. .btn_input(usr_btn[2]),
  114. .btn_output(btn_level)
  115. );
  116.  
  117. LCD_module lcd0(
  118. .clk(clk),
  119. .reset(~reset_n),
  120. .row_A(row_A),
  121. .row_B(row_B),
  122. .LCD_E(LCD_E),
  123. .LCD_RS(LCD_RS),
  124. .LCD_RW(LCD_RW),
  125. .LCD_D(LCD_D)
  126. );
  127.  
  128. sd_card sd_card0(
  129. .cs(spi_ss),
  130. .sclk(spi_sck),
  131. .mosi(spi_mosi),
  132. .miso(spi_miso),
  133.  
  134. .clk(clk_sel),
  135. .rst(~reset_n),
  136. .rd_req(rd_req),
  137. .block_addr(rd_addr),
  138. .init_finished(init_finished),
  139. .dout(sd_dout),
  140. .sd_valid(sd_valid)
  141. );
  142.  
  143. sram ram0(
  144. .clk(clk),
  145. .we(sram_we),
  146. .en(sram_en),
  147. .addr(sram_addr),
  148. .data_i(data_in),
  149. .data_o(data_out)
  150. );
  151.  
  152.  
  153. //
  154. // Enable one cycle of btn_pressed per each button hit
  155. //
  156. always @(posedge clk) begin
  157. if (~reset_n)
  158. prev_btn_level <= 0;
  159. else
  160. prev_btn_level <= btn_level;
  161. end
  162.  
  163. assign btn_pressed = (btn_level == 1 && prev_btn_level == 0)? 1 : 0;
  164.  
  165. // ------------------------------------------------------------------------
  166. // The following code sets the control signals of an SRAM memory block
  167. // that is connected to the data output port of the SD controller.
  168. // Once the read request is made to the SD controller, 512 bytes of data
  169. // will be sequentially read into the SRAM memory block, one byte per
  170. // clock cycle (as long as the sd_valid signal is high).
  171. assign sram_we = sd_valid; // Write data into SRAM when sd_valid is high.
  172. assign sram_en = 1; // Always enable the SRAM block.
  173. assign data_in = sd_dout; // Input data always comes from the SD controller.
  174. assign sram_addr = sd_counter[8:0]; // Set the driver of the SRAM address signal.
  175. // End of the SRAM memory block
  176. // ------------------------------------------------------------------------
  177.  
  178. // ------------------------------------------------------------------------
  179. // FSM of the SD card reader that reads the super block (512 bytes)
  180. always @(posedge clk) begin
  181. if (~reset_n) begin
  182. P <= S_MAIN_INIT;
  183. done_flag <= 0;
  184. end
  185. else begin
  186. P <= P_next;
  187. if (P == S_MAIN_DONE)
  188. done_flag <= 1;
  189. else if (P == S_MAIN_SHOW && P_next == S_MAIN_IDLE)
  190. done_flag <= 0;
  191. else
  192. done_flag <= done_flag;
  193. end
  194. end
  195.  
  196.  
  197.  
  198. always @(*) begin // FSM next-state logic
  199. case (P)
  200. S_MAIN_INIT: // wait for SD card initialization
  201. if (init_finished == 1) P_next = S_MAIN_IDLE;
  202. else P_next = S_MAIN_INIT;
  203. S_MAIN_IDLE: // wait for button click
  204. if (btn_pressed == 1) P_next = S_MAIN_WAIT;
  205. else P_next = S_MAIN_IDLE;
  206. S_MAIN_WAIT: // issue a rd_req to the SD controller until it's ready
  207. P_next = S_MAIN_READ;
  208. S_MAIN_READ: // wait for the input data to enter the SRAM buffer
  209. if (sd_counter == 512) P_next = S_MAIN_DONE;
  210. else P_next = S_MAIN_READ;
  211. S_MAIN_DONE: // read byte 0 of the superblock from sram[]
  212. P_next = S_MAIN_CAL;
  213. S_MAIN_CAL:
  214. if (dlab_end_match=='d7) P_next = S_MAIN_SHOW;
  215. else if (sd_counter =='d512) P_next = S_MAIN_WAIT;
  216. else P_next = S_MAIN_CAL;
  217. S_MAIN_SHOW:
  218. if (~reset_n) P_next = S_MAIN_INIT;
  219. else P_next = S_MAIN_SHOW;
  220. default:
  221. P_next = S_MAIN_IDLE;
  222. endcase
  223. end
  224.  
  225. // FSM output logic: controls the 'rd_req' and 'rd_addr' signals.
  226. always @(*) begin
  227. rd_req = (P == S_MAIN_WAIT);
  228. rd_addr = blk_addr;
  229. end
  230.  
  231. always @(posedge clk) begin
  232. if (~reset_n) begin
  233. counter <= 0;
  234. counter_2 <= 0;
  235. counter_the <= 0;
  236. blk_addr <= 32'h2000;
  237. dlab_tag_match <=0;
  238. dlab_end_match <=0;
  239. dlab_the_match <=0;
  240. num_the <= 0;
  241. num_the_p <= 0;
  242. start_flag <= 0;
  243. end_flag <= 0;
  244. end
  245. if (P==S_MAIN_WAIT) begin
  246. not_this_block <= 0;
  247. not_this_block_2 <= 0;
  248. end
  249.  
  250. if (P==S_MAIN_CAL) begin
  251. if(data_byte==dlab_tag[counter+:8]) begin
  252. dlab_tag_match <= dlab_tag_match + 1;
  253. counter <= counter + 8;
  254. not_this_block <= 0;
  255. if (dlab_tag_match == 'd7) begin
  256. start_flag <= 1;
  257. end
  258. end
  259. else begin
  260. dlab_tag_match <= 0;
  261. counter <= 0;
  262. end
  263.  
  264. if(data_byte==dlab_end[counter_2+:8]) begin
  265. dlab_end_match <= dlab_end_match + 1;
  266. counter_2 <= counter_2 + 8;
  267. not_this_block_2 <= 0;
  268.  
  269. if (dlab_end_match == 'd7) begin
  270. p_addr[0+:8]<=blk_addr[15:12];
  271. p_addr[8+:8]<=blk_addr[11:8];
  272. p_addr[16+:8]<=blk_addr[7:4];
  273. p_addr[24+:8]<=blk_addr[3:0];
  274. end_flag <= 1;
  275. end
  276. end
  277. else begin
  278. counter_2 <= 0;
  279. dlab_end_match <= 0;
  280. end
  281.  
  282.  
  283.  
  284. if(((data_byte==the[counter_the+:8]) || (data_byte==THE[counter_the+:8])) ) begin
  285. dlab_the_match <= dlab_the_match + 1;
  286. counter_the <= counter_the + 8;
  287. if (dlab_the_match == 'd2) begin
  288. num_the <= num_the + 'd1;
  289. p_addr_1[0+:8]<=blk_addr[15:12];
  290. p_addr_1[8+:8]<=blk_addr[11:8];
  291. p_addr_1[16+:8]<=blk_addr[7:4];
  292. p_addr_1[24+:8]<=blk_addr[3:0];
  293. end
  294. if (dlab_the_match == 'd0) word <= data_byte;
  295. if (dlab_the_match == 'd1) word_1 <= data_byte;
  296. if (dlab_the_match == 'd2) word_2 <= data_byte;
  297. end
  298. else begin
  299. dlab_the_match <= 0;
  300. counter_the <= 0;
  301. end
  302.  
  303.  
  304. end
  305.  
  306. if (P==S_MAIN_SHOW) num_the_p <= num_the;
  307. if (sd_counter == 'd512) blk_addr <= blk_addr+1;
  308. end
  309.  
  310.  
  311. always @(posedge clk) begin
  312. if (~reset_n || (P == S_MAIN_READ && P_next == S_MAIN_DONE) || ((P == S_MAIN_DONE) && (P_next == S_MAIN_WAIT)) || ((P == S_MAIN_CAL) && (P_next == S_MAIN_WAIT)) || ((P == S_MAIN_DONE) && (P_next == S_MAIN_CAL)))
  313. sd_counter <= 0;
  314. else if ((P == S_MAIN_READ && sd_valid) ||(P == S_MAIN_DONE && P_next==S_MAIN_CAL) || (P==S_MAIN_DONE && sd_counter<512) || (P==S_MAIN_CAL && sd_counter<512) ||((P == S_MAIN_CAL) && (P_next==S_MAIN_SHOW)))
  315. sd_counter <= sd_counter + 1;
  316.  
  317. end
  318.  
  319. //always @(posedge clk) begin
  320. // if(~reset_n || ((P == S_MAIN_DONE) && (P_next == S_MAIN_WAIT))) begin
  321. // done_counter <= 0;
  322.  
  323. // end
  324. // else if ((P == S_MAIN_DONE) && (done_counter<512)) begin
  325. // done_counter <= done_counter +1;
  326. // end
  327.  
  328. //end
  329.  
  330. // FSM ouput logic: Retrieves the content of sram[] for display
  331. always @(posedge clk) begin
  332. if (~reset_n) data_byte <= 8'b0;
  333. else if (sram_en && (P == S_MAIN_DONE || P==S_MAIN_CAL)) data_byte <= data_out;
  334. //else data_byte <= data_out;
  335. end
  336. // End of the FSM of the SD card reader
  337. // ------------------------------------------------------------------------
  338.  
  339. // ------------------------------------------------------------------------
  340. // LCD Display function.
  341. always @(posedge clk) begin
  342. if (~reset_n) begin
  343. row_A = "SD card cannot ";
  344. row_B = "be initialized! ";
  345. end else if (done_flag) begin
  346. row_A <= {word, word_1, word_2, ((num_the_p[7:4] > 9)? "7" : "0") + num_the_p[7:4], ((num_the_p[3:0] > 9)? "7" : "0") + num_the_p[3:0],word_5,word_6,word_7,((p_addr[0:7] > 9)? "7" : "0") + p_addr[0:7], ((p_addr[8:15] > 9)? "7" : "0") + p_addr[8:15], ((p_addr[16:23] > 9)? "7" : "0") + p_addr[16:23], ((p_addr[24:31] > 9)? "7" : "0") + p_addr[24:31]
  347. , ((p_addr_1[0:7] > 9)? "7" : "0") + p_addr_1[0:7], ((p_addr_1[8:15] > 9)? "7" : "0") + p_addr_1[8:15], ((p_addr_1[16:23] > 9)? "7" : "0") + p_addr_1[16:23], ((p_addr_1[24:31] > 9)? "7" : "0") + p_addr_1[24:31]};
  348. //row_A <= {word, word_1};
  349. row_B <= { "Byte ",
  350. sd_counter[9:8] + "0",
  351. ((sd_counter[7:4] > 9)? "7" : "0") + sd_counter[7:4],
  352. ((sd_counter[3:0] > 9)? "7" : "0") + sd_counter[3:0],
  353. "h = ",
  354. ((data_byte[7:4] > 9)? "7" : "0") + data_byte[7:4],
  355. ((data_byte[3:0] > 9)? "7" : "0") + data_byte[3:0], "h." };
  356. end
  357. else if (P == S_MAIN_IDLE) begin
  358. row_A <= "Hit BTN2 to read";
  359. row_B <= "the SD card ... ";
  360. end
  361. end
  362. // End of the LCD display function
  363. // ------------------------------------------------------------------------
  364.  
  365. endmodule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement