Rev | Line | |
---|
[2] | 1 | //Listing 8.1
|
---|
| 2 | module uart_rx
|
---|
| 3 | #(
|
---|
| 4 | parameter DBIT = 8, // # data bits
|
---|
| 5 | SB_TICK = 16 // # ticks for stop bits
|
---|
| 6 | )
|
---|
| 7 | (
|
---|
| 8 | input wire clk, reset,
|
---|
| 9 | input wire rx, s_tick,
|
---|
| 10 | output reg rx_done_tick,
|
---|
| 11 | output wire [7:0] dout
|
---|
| 12 | );
|
---|
| 13 |
|
---|
| 14 | // symbolic state declaration
|
---|
| 15 | localparam [1:0]
|
---|
| 16 | idle = 2'b00,
|
---|
| 17 | start = 2'b01,
|
---|
| 18 | data = 2'b10,
|
---|
| 19 | stop = 2'b11;
|
---|
| 20 |
|
---|
| 21 | // signal declaration
|
---|
| 22 | reg [1:0] state_reg, state_next;
|
---|
| 23 | reg [3:0] s_reg, s_next;
|
---|
| 24 | reg [2:0] n_reg, n_next;
|
---|
| 25 | reg [7:0] b_reg, b_next;
|
---|
| 26 |
|
---|
| 27 | // body
|
---|
| 28 | // FSMD state & data registers
|
---|
| 29 | always @(posedge clk, posedge reset)
|
---|
| 30 | if (reset)
|
---|
| 31 | begin
|
---|
| 32 | state_reg <= idle;
|
---|
| 33 | s_reg <= 0;
|
---|
| 34 | n_reg <= 0;
|
---|
| 35 | b_reg <= 0;
|
---|
| 36 | end
|
---|
| 37 | else
|
---|
| 38 | begin
|
---|
| 39 | state_reg <= state_next;
|
---|
| 40 | s_reg <= s_next;
|
---|
| 41 | n_reg <= n_next;
|
---|
| 42 | b_reg <= b_next;
|
---|
| 43 | end
|
---|
| 44 |
|
---|
| 45 | // FSMD next-state logic
|
---|
| 46 | always @*
|
---|
| 47 | begin
|
---|
| 48 | state_next = state_reg;
|
---|
| 49 | rx_done_tick = 1'b0;
|
---|
| 50 | s_next = s_reg;
|
---|
| 51 | n_next = n_reg;
|
---|
| 52 | b_next = b_reg;
|
---|
| 53 | case (state_reg)
|
---|
| 54 | idle:
|
---|
| 55 | if (~rx)
|
---|
| 56 | begin
|
---|
| 57 | state_next = start;
|
---|
| 58 | s_next = 0;
|
---|
| 59 | end
|
---|
| 60 | start:
|
---|
| 61 | if (s_tick)
|
---|
| 62 | if (s_reg==7)
|
---|
| 63 | begin
|
---|
| 64 | state_next = data;
|
---|
| 65 | s_next = 0;
|
---|
| 66 | n_next = 0;
|
---|
| 67 | end
|
---|
| 68 | else
|
---|
| 69 | s_next = s_reg + 1;
|
---|
| 70 | data:
|
---|
| 71 | if (s_tick)
|
---|
| 72 | if (s_reg==15)
|
---|
| 73 | begin
|
---|
| 74 | s_next = 0;
|
---|
| 75 | b_next = {rx, b_reg[7:1]};
|
---|
| 76 | if (n_reg==(DBIT-1))
|
---|
| 77 | state_next = stop ;
|
---|
| 78 | else
|
---|
| 79 | n_next = n_reg + 1;
|
---|
| 80 | end
|
---|
| 81 | else
|
---|
| 82 | s_next = s_reg + 1;
|
---|
| 83 | stop:
|
---|
| 84 | if (s_tick)
|
---|
| 85 | if (s_reg==(SB_TICK-1))
|
---|
| 86 | begin
|
---|
| 87 | state_next = idle;
|
---|
| 88 | rx_done_tick =1'b1;
|
---|
| 89 | end
|
---|
| 90 | else
|
---|
| 91 | s_next = s_reg + 1;
|
---|
| 92 | endcase
|
---|
| 93 | end
|
---|
| 94 | // output
|
---|
| 95 | assign dout = b_reg;
|
---|
| 96 |
|
---|
| 97 | endmodule
|
---|
Note:
See
TracBrowser
for help on using the repository browser.