Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.04 KB | None | 0 0
  1. `timescale 1ns / 1ps
  2.  
  3. module mid(
  4. input clk,
  5. input reset_n,
  6. input [3:0] usr_btn,
  7. output [3:0] usr_led,
  8. input uart_rx,
  9. output uart_tx,
  10. // SD card specific I/O ports
  11. output spi_ss,
  12. output spi_sck,
  13. output spi_mosi,
  14. input spi_miso,
  15. // 1602 LCD Module Interface
  16. output LCD_RS,
  17. output LCD_RW,
  18. output LCD_E,
  19. output [3:0] LCD_D
  20. );
  21.  
  22. localparam [2:0] S_MAIN_INIT = 0, S_MAIN_WAIT= 4,
  23. S_MAIN_REPLY = 6;
  24. localparam [1:0] S_UART_IDLE = 0, S_UART_WAIT = 1,
  25. S_UART_SEND = 2, S_UART_INCR = 3;
  26.  
  27. // declare text message parameters
  28. localparam MSG3_SIZE = 139;
  29. localparam MEM_SIZE = 139;
  30. localparam PROMPT1_STR = 0;
  31. localparam REPLY_STR = 0;
  32. localparam INIT_DELAY = 100_000; // 1 msec @ 100 MHz
  33.  
  34. // declare system variables
  35. wire enter_pressed;
  36. wire print_enable, print_done;
  37. reg [$clog2(MEM_SIZE):0] send_counter;
  38. reg [2:0] P, P_next;
  39. reg [1:0] Q, Q_next;
  40. reg [$clog2(INIT_DELAY):0] init_counter;
  41.  
  42. reg [7:0] data[0:MEM_SIZE-1];
  43. reg [0:MSG3_SIZE*8-1] msg3 = { "The result is:\015\012[ 11CE9, 18749, 0EE26, 16F64 ]",
  44. "\015\012[ 58415, 7579, 04446, 55564 ]",
  45. "\015\012[ 666E9, 77749, 88886, 99964 ]",
  46. "\015\012[ AAAE9, BBB49, CCC26, DDD64 ]",8'h00 };
  47.  
  48.  
  49. reg [15:0] num_reg; // The keyin number register
  50. reg [2:0] key_cnt;
  51. reg [15:0] num1, num2, tmp1, tmp2;
  52. reg [15:0] gcd;
  53. reg gcd_done;
  54.  
  55. // declare UART signals
  56. wire transmit;
  57. wire received;
  58. wire [7:0] rx_byte;
  59. reg [7:0] rx_temp;
  60. wire [7:0] tx_byte;
  61. wire is_receiving;
  62. wire is_transmitting;
  63. wire recv_error;
  64.  
  65. reg sram_ok,uart_ok;
  66.  
  67. /* The UART device takes a 100MHz clock to handle I/O at 9600 baudrate */
  68. uart uart0(
  69. .clk(clk),
  70. .rst(~reset_n),
  71. .rx(uart_rx),
  72. .tx(uart_tx),
  73. .transmit(transmit),
  74. .tx_byte(tx_byte),
  75. .received(received),
  76. .rx_byte(rx_byte),
  77. .is_receiving(is_receiving),
  78. .is_transmitting(is_transmitting),
  79. .recv_error(recv_error)
  80. );
  81.  
  82. integer idx;
  83.  
  84. //////////////////////// sd card ///////////////////////////////////////////////
  85.  
  86. localparam [2:0] S_MAIN_INIT6 = 3'b000, S_MAIN_IDLE6 = 3'b001,
  87. S_MAIN_WAIT6 = 3'b010, S_MAIN_READ6 = 3'b011,
  88. S_MAIN_DONE6 = 3'b100, S_MAIN_SHOW6 = 3'b101,
  89. S_MAIN_CALC6 = 3'b110, S_MAIN_MULT6 = 3'b111;
  90. // Declare system variables
  91. wire btn_level, btn_pressed;
  92. reg prev_btn_level;
  93. reg [2:0] K, K_next;
  94. reg [9:0] sd_counter;
  95. reg [7:0] data_byte;
  96. reg [31:0] blk_addr;
  97.  
  98. reg [127:0] row_A = "SD card cannot ";
  99. reg [127:0] row_B = "be initialized! ";
  100. reg done_flag; // Signals the completion of reading one SD sector.
  101.  
  102. // Declare SD card interface signals
  103. wire clk_sel;
  104. wire clk_500k;
  105. reg rd_req;
  106. reg [31:0] rd_addr;
  107. wire init_finished;
  108. wire [7:0] sd_dout;
  109. wire sd_valid;
  110.  
  111. // Declare the control/data signals of an SRAM memory block
  112. wire [7:0] data_in;
  113. wire [7:0] data_out;
  114. wire [8:0] sram_addr;
  115. wire sram_we, sram_en;
  116.  
  117. reg [0:63] start_line = "MATX_TAG";
  118. reg [0:63] end_line = "DLAB_END";
  119. reg [0:31] the = " the";
  120.  
  121. assign clk_sel = (init_finished)? clk : clk_500k; // clock for the SD controller
  122. assign usr_led = K;
  123.  
  124. reg[1:0] find_head,find_tail;
  125. reg[4:0] head_index;
  126. //reg[0:31] data;
  127. reg[0:255] A,B;
  128. //reg[0:287] C; ///////////////////tem
  129. reg[0:15] mat_index;
  130.  
  131. LCD_module lcd0(
  132. .clk(clk),
  133. .reset(~reset_n),
  134. .row_A(row_A),
  135. .row_B(row_B),
  136. .LCD_E(LCD_E),
  137. .LCD_RS(LCD_RS),
  138. .LCD_RW(LCD_RW),
  139. .LCD_D(LCD_D)
  140. );
  141.  
  142. clk_divider#(200) clk_divider0(
  143. .clk(clk),
  144. .reset(~reset_n),
  145. .clk_out(clk_500k)
  146. );
  147.  
  148. debounce btn_db0(
  149. .clk(clk),
  150. .btn_input(usr_btn[2]),
  151. .btn_output(btn_level)
  152. );
  153.  
  154. sd_card sd_card0(
  155. .cs(spi_ss),
  156. .sclk(spi_sck),
  157. .mosi(spi_mosi),
  158. .miso(spi_miso),
  159.  
  160. .clk(clk_sel),
  161. .rst(~reset_n),
  162. .rd_req(rd_req),
  163. .block_addr(rd_addr),
  164. .init_finished(init_finished),
  165. .dout(sd_dout),
  166. .sd_valid(sd_valid)
  167. );
  168.  
  169. sram ram0(
  170. .clk(clk),
  171. .we(sram_we),
  172. .en(sram_en),
  173. .addr(sram_addr),
  174. .data_i(data_in),
  175. .data_o(data_out)
  176. );
  177.  
  178. always @(posedge clk) begin
  179. if (~reset_n)
  180. prev_btn_level <= 0;
  181. else
  182. prev_btn_level <= btn_level;
  183. end
  184.  
  185. assign btn_pressed = (btn_level == 1 && prev_btn_level == 0)? 1 : 0;
  186.  
  187. assign sram_we = sd_valid; // Write data into SRAM when sd_valid is high.
  188. assign sram_en = 1; // Always enable the SRAM block.
  189. assign data_in = sd_dout; // Input data always comes from the SD controller.
  190. assign sram_addr = sd_counter[8:0]; // Set the driver of the SRAM address signal.
  191.  
  192. always @(posedge clk) begin
  193. if (~reset_n) begin
  194. K <= S_MAIN_INIT6;
  195. done_flag <= 0;
  196. end
  197. else begin
  198. K <= K_next;
  199. if (K == S_MAIN_DONE6)
  200. done_flag <= 1;
  201. else if (K == S_MAIN_SHOW6 && K_next == S_MAIN_IDLE6)
  202. done_flag <= 0;
  203. else
  204. done_flag <= done_flag;
  205. end
  206. end
  207.  
  208. always @(*) begin // FSM next-state logic
  209. case (K)
  210. S_MAIN_INIT6: // wait for SD card initialization
  211. if (init_finished == 1) K_next = S_MAIN_IDLE6;
  212. else K_next = S_MAIN_INIT6;
  213. S_MAIN_IDLE6: // wait for button click
  214. if (btn_pressed == 1) K_next = S_MAIN_WAIT6;
  215. else K_next = S_MAIN_IDLE6;
  216. S_MAIN_WAIT6: // issue a rd_req to the SD controller until it's ready
  217. K_next = S_MAIN_READ6;
  218. S_MAIN_READ6: // wait for the input data to enter the SRAM buffer
  219. if (sd_counter == 512) K_next = S_MAIN_DONE6;
  220. else K_next = S_MAIN_READ6;
  221. S_MAIN_DONE6: // read byte 0 of the superblock from sram[]
  222. K_next = S_MAIN_CALC6;
  223. S_MAIN_CALC6:
  224. if (sram_ok) K_next = S_MAIN_MULT6;
  225. else if (sd_counter == 512) K_next = S_MAIN_WAIT6;
  226. else K_next = S_MAIN_CALC6;
  227. S_MAIN_MULT6:
  228. //if(uart_ok) K_next = S_MAIN_SHOW6;
  229. K_next = S_MAIN_SHOW6;
  230. //else K_next = S_MAIN_MULT6;
  231. S_MAIN_SHOW6:
  232. if(print_done) K_next = S_MAIN_IDLE6;
  233. else K_next = S_MAIN_SHOW6;
  234. default:
  235. K_next = S_MAIN_IDLE6;
  236. endcase
  237. end
  238.  
  239. always @(posedge clk) begin
  240. if (~reset_n) begin
  241. find_head = 0;
  242. head_index = 0;
  243. A = 0; B = 0; //C = 0;
  244. mat_index = 0;
  245. sram_ok = 0;
  246. end
  247. else begin
  248. if(K == S_MAIN_CALC6 && sd_counter < 512) begin
  249. if(find_head == 1) begin
  250. if((data_byte >= 48 && data_byte <= 57) ||(data_byte >= 65 && data_byte <= 70)) begin
  251. if(mat_index >= 256)
  252. sram_ok = 1;
  253. else if(mat_index < 127) begin
  254. A[mat_index +:4] = (data_byte >= "A") ? data_byte - "7" : data_byte - "0" ;
  255. mat_index = mat_index + 4;
  256. end
  257. else if(mat_index >= 128) begin
  258. B[mat_index - 128 +:4] = (data_byte >= "A") ? data_byte - "7" : data_byte - "0" ;
  259. mat_index = mat_index + 4;
  260. end
  261. end
  262. end
  263. else begin
  264. if(head_index >= 8) begin
  265. find_head = 1;
  266. head_index = 0;
  267. end
  268. else begin
  269. if(data_byte != start_line[head_index*8 +:8])
  270. head_index = 0;
  271. else
  272. head_index = head_index + 1;
  273. end
  274. end
  275. end
  276. end
  277. end
  278.  
  279. // FSM output logic: controls the 'rd_req' and 'rd_addr' signals.
  280. always @(*) begin
  281. rd_req = (K == S_MAIN_WAIT6);
  282. rd_addr = blk_addr;
  283. end
  284.  
  285. always @(posedge clk) begin
  286. if (~reset_n) blk_addr <= 32'h2000;
  287. else if(K == S_MAIN_CALC6 && K_next == S_MAIN_WAIT6)
  288. blk_addr = blk_addr + 1;
  289. end
  290.  
  291. // FSM output logic: controls the 'sd_counter' signal.
  292. // SD card read address incrementer
  293. always @(posedge clk) begin
  294. if (~reset_n || (K == S_MAIN_READ6 && K_next == S_MAIN_DONE6) || (K == S_MAIN_CALC6 && K_next == S_MAIN_WAIT6))
  295. sd_counter <= 0;
  296. else if ((K == S_MAIN_READ6 && sd_valid) ||
  297. (K == S_MAIN_CALC6) )
  298. sd_counter <= sd_counter + 1;
  299. end
  300.  
  301. // FSM ouput logic: Retrieves the content of sram[] for display
  302. always @(posedge clk) begin
  303. if (~reset_n) data_byte <= 8'b0;
  304. else if (K == S_MAIN_DONE6 || K == S_MAIN_CALC6) data_byte <= data_out;
  305. end
  306.  
  307. always @(posedge clk) begin
  308. if (~reset_n) begin
  309. row_A = "SD card cannot ";
  310. row_B = "be initialized! ";
  311. end
  312. else if (K == S_MAIN_IDLE6) begin
  313. row_A <= "Hit BTN2 to read";
  314. row_B <= "the SD card ... ";
  315. end
  316. /*else if(K == S_MAIN_SHOW6) begin
  317. row_A <= {A[0:127]};
  318. row_B <= {B[0:127]};
  319. end*/
  320. end
  321.  
  322. ///////////////////////// uart ///////////////////////////////////////////
  323.  
  324. // Combinational I/O logics
  325. assign enter_pressed = (rx_temp == 8'h0D);
  326.  
  327. // Main FSM that reads the UART input, compute GCD, and
  328. // print the output of the GCD.
  329. always @(posedge clk) begin
  330. if (~reset_n) P <= S_MAIN_INIT;
  331. else P <= P_next;
  332. end
  333.  
  334. always @(*) begin // FSM next-state logic
  335. case (P)
  336. S_MAIN_INIT: // Wait for initial delay of the circuit.
  337. if (init_counter < INIT_DELAY) P_next = S_MAIN_INIT;
  338. else P_next = S_MAIN_WAIT;
  339. S_MAIN_WAIT: // wait for <Enter> key.
  340. if (K == S_MAIN_SHOW6) P_next = S_MAIN_REPLY;
  341. else P_next = S_MAIN_WAIT;
  342. S_MAIN_REPLY: // Print the reply message.
  343. if (print_done) P_next = S_MAIN_INIT;
  344. else P_next = S_MAIN_REPLY;
  345. default:
  346. P_next = S_MAIN_INIT;
  347. endcase
  348. end
  349.  
  350. // FSM output logics: print string control signals.
  351. assign print_enable = (P == S_MAIN_WAIT && P_next == S_MAIN_REPLY);
  352. assign print_done = (tx_byte == 8'h00);
  353.  
  354. // Initialization counter.
  355. always @(posedge clk) begin
  356. if (P == S_MAIN_INIT) init_counter <= init_counter + 1;
  357. else init_counter <= 0;
  358. end
  359. // End of the FSM of the print string controller
  360. // ------------------------------------------------------------------------
  361.  
  362. // ------------------------------------------------------------------------
  363. // FSM of the controller to send a string to the UART.
  364. always @(posedge clk) begin
  365. if (~reset_n) Q <= S_UART_IDLE;
  366. else Q <= Q_next;
  367. end
  368.  
  369. always @(*) begin // FSM next-state logic
  370. case (Q)
  371. S_UART_IDLE: // wait for the print_string flag
  372. if (print_enable) Q_next = S_UART_WAIT;
  373. else Q_next = S_UART_IDLE;
  374. S_UART_WAIT: // wait for the transmission of current data byte begins
  375. if (is_transmitting == 1) Q_next = S_UART_SEND;
  376. else Q_next = S_UART_WAIT;
  377. S_UART_SEND: // wait for the transmission of current data byte finishes
  378. if (is_transmitting == 0) Q_next = S_UART_INCR; // transmit next character
  379. else Q_next = S_UART_SEND;
  380. S_UART_INCR:
  381. if (tx_byte == 8'h00) Q_next = S_UART_IDLE; // string transmission ends
  382. else Q_next = S_UART_WAIT;
  383. endcase
  384. end
  385.  
  386. // FSM output logics
  387. assign transmit = (Q_next == S_UART_WAIT || print_enable);
  388. assign tx_byte = data[send_counter];
  389.  
  390. // UART send_counter control circuit
  391. always @(posedge clk) begin
  392. case (P_next)
  393. S_MAIN_INIT: send_counter <= 0;
  394. default: send_counter <= send_counter + (Q_next == S_UART_INCR);
  395. endcase
  396. end
  397.  
  398. always @(posedge clk) begin
  399. rx_temp <= (received)? rx_byte : 8'h0;
  400. end
  401.  
  402. always @(posedge clk) begin
  403. if (~reset_n)
  404. for (idx = 0; idx < MSG3_SIZE; idx = idx + 1) data[idx] = msg3[idx*8 +: 8];
  405. end
  406.  
  407. // End of the GCD computation logic
  408. // ------------------------------------------------------------------------
  409.  
  410. endmodule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement