Advertisement
Guest User

Untitled

a guest
Nov 18th, 2017
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.99 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;
  73. reg [9:0] num_the=0;
  74. reg start_flag, end_flag;
  75. reg not_this_block, not_this_block_2;
  76. reg [127:0] row_A = "SD card cannot ";
  77. reg [127:0] row_B = "be initialized! ";
  78. reg [0:63] dlab_tag = "DLAB_TAG";
  79. reg [0:63] dlab_end = "DLAB_END";
  80. reg [0:56] print_out = " ";
  81. reg done_flag; // Signals the completion of reading one SD sector.
  82. reg [0:7] word="@", word_1="@", word_2='h1 ,word_3 = "!",word_4='h2, word_5, word_6, word_7;
  83. reg [0:31] p_addr, p_addr_1;
  84. // Declare SD card interface signals
  85. wire clk_sel;
  86. wire clk_500k;
  87. reg rd_req;
  88. reg [31:0] rd_addr;
  89. wire init_finished;
  90. wire [7:0] sd_dout;
  91. wire sd_valid;
  92.  
  93. // Declare the control/data signals of an SRAM memory block
  94. wire [7:0] data_in;
  95. wire [7:0] data_out;
  96. wire [8:0] sram_addr;
  97. wire sram_we, sram_en;
  98.  
  99. assign clk_sel = (init_finished)? clk : clk_500k; // clock for the SD controller
  100. assign usr_led = P;
  101.  
  102. clk_divider#(200) clk_divider0(
  103. .clk(clk),
  104. .reset(~reset_n),
  105. .clk_out(clk_500k)
  106. );
  107.  
  108. debounce btn_db0(
  109. .clk(clk),
  110. .btn_input(usr_btn[2]),
  111. .btn_output(btn_level)
  112. );
  113.  
  114. LCD_module lcd0(
  115. .clk(clk),
  116. .reset(~reset_n),
  117. .row_A(row_A),
  118. .row_B(row_B),
  119. .LCD_E(LCD_E),
  120. .LCD_RS(LCD_RS),
  121. .LCD_RW(LCD_RW),
  122. .LCD_D(LCD_D)
  123. );
  124.  
  125. sd_card sd_card0(
  126. .cs(spi_ss),
  127. .sclk(spi_sck),
  128. .mosi(spi_mosi),
  129. .miso(spi_miso),
  130.  
  131. .clk(clk_sel),
  132. .rst(~reset_n),
  133. .rd_req(rd_req),
  134. .block_addr(rd_addr),
  135. .init_finished(init_finished),
  136. .dout(sd_dout),
  137. .sd_valid(sd_valid)
  138. );
  139.  
  140. sram ram0(
  141. .clk(clk),
  142. .we(sram_we),
  143. .en(sram_en),
  144. .addr(sram_addr),
  145. .data_i(data_in),
  146. .data_o(data_out)
  147. );
  148.  
  149.  
  150. //
  151. // Enable one cycle of btn_pressed per each button hit
  152. //
  153. always @(posedge clk) begin
  154. if (~reset_n)
  155. prev_btn_level <= 0;
  156. else
  157. prev_btn_level <= btn_level;
  158. end
  159.  
  160. assign btn_pressed = (btn_level == 1 && prev_btn_level == 0)? 1 : 0;
  161.  
  162. // ------------------------------------------------------------------------
  163. // The following code sets the control signals of an SRAM memory block
  164. // that is connected to the data output port of the SD controller.
  165. // Once the read request is made to the SD controller, 512 bytes of data
  166. // will be sequentially read into the SRAM memory block, one byte per
  167. // clock cycle (as long as the sd_valid signal is high).
  168. assign sram_we = sd_valid; // Write data into SRAM when sd_valid is high.
  169. assign sram_en = 1; // Always enable the SRAM block.
  170. assign data_in = sd_dout; // Input data always comes from the SD controller.
  171. assign sram_addr = sd_counter[8:0]; // Set the driver of the SRAM address signal.
  172. // End of the SRAM memory block
  173. // ------------------------------------------------------------------------
  174.  
  175. // ------------------------------------------------------------------------
  176. // FSM of the SD card reader that reads the super block (512 bytes)
  177. always @(posedge clk) begin
  178. if (~reset_n) begin
  179. P <= S_MAIN_INIT;
  180. done_flag <= 0;
  181. end
  182. else begin
  183. P <= P_next;
  184. if (P == S_MAIN_DONE)
  185. done_flag <= 1;
  186. else if (P == S_MAIN_SHOW && P_next == S_MAIN_IDLE)
  187. done_flag <= 0;
  188. else
  189. done_flag <= done_flag;
  190. end
  191. end
  192.  
  193.  
  194.  
  195. always @(*) begin // FSM next-state logic
  196. case (P)
  197. S_MAIN_INIT: // wait for SD card initialization
  198. if (init_finished == 1) P_next = S_MAIN_IDLE;
  199. else P_next = S_MAIN_INIT;
  200. S_MAIN_IDLE: // wait for button click
  201. if (btn_pressed == 1) P_next = S_MAIN_WAIT;
  202. else P_next = S_MAIN_IDLE;
  203. S_MAIN_WAIT: // issue a rd_req to the SD controller until it's ready
  204. P_next = S_MAIN_READ;
  205. S_MAIN_READ: // wait for the input data to enter the SRAM buffer
  206. if (sd_counter == 512) P_next = S_MAIN_DONE;
  207. else P_next = S_MAIN_READ;
  208. S_MAIN_DONE: // read byte 0 of the superblock from sram[]
  209. if (dlab_tag_match=='d7) P_next = S_MAIN_CAL;
  210. else if (not_this_block) P_next = S_MAIN_WAIT;
  211. else P_next = S_MAIN_DONE;
  212. S_MAIN_CAL:
  213. if (dlab_end_match=='d7) P_next = S_MAIN_SHOW;
  214. else if (not_this_block_2) P_next = S_MAIN_WAIT;
  215. else P_next = S_MAIN_CAL;
  216. S_MAIN_SHOW:
  217. if (sd_counter < 512) P_next = S_MAIN_DONE;
  218. else P_next = S_MAIN_IDLE;
  219. default:
  220. P_next = S_MAIN_IDLE;
  221. endcase
  222. end
  223.  
  224. // FSM output logic: controls the 'rd_req' and 'rd_addr' signals.
  225. always @(*) begin
  226. rd_req = (P == S_MAIN_WAIT);
  227. rd_addr = blk_addr;
  228. end
  229.  
  230. always @(posedge clk) begin
  231. if (~reset_n) begin
  232. counter <= 0;
  233. counter_2 <= 0;
  234. //start_flag <= 0;
  235. blk_addr <= 32'h2000;
  236. dlab_tag_match <=0;
  237. dlab_end_match <=0;
  238. //word_2 <= counter;
  239. end
  240. if (P==S_MAIN_WAIT) begin
  241. not_this_block <= 0;
  242. end
  243. if (P==S_MAIN_DONE) begin
  244. // if (data_byte=="D") begin
  245. // word <= data_byte;
  246. // word_3 <= done_counter;
  247. // p_addr[0+:8]<=blk_addr[15:12];
  248. // p_addr[8+:8]<=blk_addr[11:8];
  249. // p_addr[16+:8]<=blk_addr[7:4];
  250. // p_addr[24+:8]<=blk_addr[3:0];
  251. // end
  252. // else if (data_byte=="L") begin
  253. // word_1 <="^";
  254. // p_addr_1[0+:8]<=blk_addr[15:12];
  255. // p_addr_1[8+:8]<=blk_addr[11:8];
  256. // p_addr_1[16+:8]<=blk_addr[7:4];
  257. // p_addr_1[24+:8]<=blk_addr[3:0];
  258. // end
  259. if(data_byte==dlab_tag[counter+:8]) begin
  260. dlab_tag_match <= dlab_tag_match + 1;
  261. counter <= counter + 8;
  262. not_this_block <= 0;
  263. if (dlab_tag_match == 'd0) begin
  264. word <= dlab_tag[counter+:8];
  265. p_addr[0+:8]<=blk_addr[15:12];
  266. p_addr[8+:8]<=blk_addr[11:8];
  267. p_addr[16+:8]<=blk_addr[7:4];
  268. p_addr[24+:8]<=blk_addr[3:0];
  269. end
  270. if (dlab_tag_match == 'd1) word_1 <= dlab_tag[counter+:8];
  271. if (dlab_tag_match == 'd2) word_2 <= dlab_tag[counter+:8];
  272. if (dlab_tag_match == 'd3) word_3 <= dlab_tag[counter+:8];
  273. if (dlab_tag_match == 'd4) word_4 <= dlab_tag[counter+:8];
  274. if (dlab_tag_match == 'd5) word_5 <= dlab_tag[counter+:8];
  275. if (dlab_tag_match == 'd6) word_6 <= dlab_tag[counter+:8];
  276. if (dlab_tag_match == 'd7) word_7 <= dlab_tag[counter+:8];
  277. end
  278. else begin
  279. dlab_tag_match <= 0;
  280. counter <= 0;
  281. not_this_block <=1;
  282. blk_addr <= blk_addr+1;
  283. if (sd_counter=='d512) begin
  284. dlab_tag_match <= 0;
  285. counter <= 0;
  286. not_this_block <=1;
  287. blk_addr <= blk_addr + 1;
  288. end
  289. end
  290. end
  291.  
  292. if (P==S_MAIN_CAL) begin
  293. // //start_blk_addr <= blk_addr;
  294. // p_addr[0+:8]<=blk_addr[15:12];
  295. // p_addr[8+:8]<=blk_addr[11:8];
  296. // p_addr[16+:8]<=blk_addr[7:4];
  297. // p_addr[24+:8]<=blk_addr[3:0];
  298. // if(data_byte==dlab_end[counter+:8]) begin
  299. // dlab_end_match <= dlab_end_match + 1;
  300. // counter_2 <= counter_2 + 8;
  301. // not_this_block_2<= 0;
  302. // if (dlab_end_match == 'd0) begin
  303. // word <= dlab_end[counter+:8];
  304. // p_addr_1[0+:8]<=blk_addr[15:12];
  305. // p_addr_1[8+:8]<=blk_addr[11:8];
  306. // p_addr_1[16+:8]<=blk_addr[7:4];
  307. // p_addr_1[24+:8]<=blk_addr[3:0];
  308. // end
  309.  
  310. // if (dlab_end_match == 'd1) word_1 <= dlab_end[counter+:8];
  311. // if (dlab_end_match == 'd2) word_2 <= dlab_end[counter+:8];
  312. // if (dlab_end_match == 'd3) word_3 <= dlab_end[counter+:8];
  313. // if (dlab_end_match == 'd4) word_4 <= dlab_end[counter+:8];
  314. // if (dlab_end_match == 'd5) word_5 <= dlab_end[counter+:8];
  315. // if (dlab_end_match == 'd6) word_6 <= dlab_end[counter+:8];
  316. // if (dlab_end_match == 'd7) word_7 <= dlab_end[counter+:8];
  317. // end
  318. // else begin
  319. // if (sd_counter=='d512) begin
  320. // if (dlab_end_match != 7 ) dlab_end_match <= 0;
  321. // counter_2 <= 0;
  322. // not_this_block_2 <=1;
  323. // blk_addr <= blk_addr + 1;
  324. // end
  325. // end
  326. end
  327.  
  328. end
  329.  
  330. /*
  331. always @(posedge clk) begin
  332. if (~reset_n) blk_addr <= 32'h2000;
  333. else if (P==S_MAIN_READ && P_next==S_MAIN_DONE) begin
  334. blk_addr <= blk_addr + 1; // In lab 6, change this line to scan all blocks
  335. end
  336. end
  337. */
  338.  
  339. // FSM output logic: controls the 'sd_counter' signal.
  340. // SD card read address incrementer
  341. always @(posedge clk) begin
  342. 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)))
  343. sd_counter <= 0;
  344. 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)) begin
  345. sd_counter <= sd_counter + 1;
  346. end
  347. end
  348.  
  349. //always @(posedge clk) begin
  350. // if(~reset_n || ((P == S_MAIN_DONE) && (P_next == S_MAIN_WAIT))) begin
  351. // done_counter <= 0;
  352.  
  353. // end
  354. // else if ((P == S_MAIN_DONE) && (done_counter<512)) begin
  355. // done_counter <= done_counter +1;
  356. // end
  357.  
  358. //end
  359.  
  360. // FSM ouput logic: Retrieves the content of sram[] for display
  361. always @(posedge clk) begin
  362. if (~reset_n) data_byte <= 8'b0;
  363. else if (sram_en && (P == S_MAIN_DONE || P==S_MAIN_CAL)) data_byte <= data_out;
  364. //else data_byte <= data_out;
  365. end
  366. // End of the FSM of the SD card reader
  367. // ------------------------------------------------------------------------
  368.  
  369. // ------------------------------------------------------------------------
  370. // LCD Display function.
  371. always @(posedge clk) begin
  372. if (~reset_n) begin
  373. row_A = "SD card cannot ";
  374. row_B = "be initialized! ";
  375. end else if (done_flag) begin
  376. row_A <= {word, word_1, word_2, word_3, word_4,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]
  377. , ((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]};
  378. //row_A <= {word, word_1};
  379. row_B <= { "Byte ",
  380. sd_counter[9:8] + "0",
  381. ((sd_counter[7:4] > 9)? "7" : "0") + sd_counter[7:4],
  382. ((sd_counter[3:0] > 9)? "7" : "0") + sd_counter[3:0],
  383. "h = ",
  384. ((data_byte[7:4] > 9)? "7" : "0") + data_byte[7:4],
  385. ((data_byte[3:0] > 9)? "7" : "0") + data_byte[3:0], "h." };
  386. end
  387. else if (P == S_MAIN_IDLE) begin
  388. row_A <= "Hit BTN2 to read";
  389. row_B <= "the SD card ... ";
  390. end
  391. end
  392. // End of the LCD display function
  393. // ------------------------------------------------------------------------
  394.  
  395. endmodule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement