Rev | Line | |
---|
[2] | 1 | //Listing 8.3
|
---|
| 2 | module uart_tx
|
---|
| 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 tx_start, s_tick,
|
---|
| 10 | input wire [7:0] din,
|
---|
| 11 | output reg tx_done_tick,
|
---|
| 12 | output wire tx
|
---|
| 13 | );
|
---|
| 14 |
|
---|
| 15 | // symbolic state declaration
|
---|
| 16 | localparam [1:0]
|
---|
| 17 | idle = 2'b00,
|
---|
| 18 | start = 2'b01,
|
---|
| 19 | data = 2'b10,
|
---|
| 20 | stop = 2'b11;
|
---|
| 21 |
|
---|
| 22 | // signal declaration
|
---|
| 23 | reg [1:0] state_reg, state_next;
|
---|
| 24 | reg [3:0] s_reg, s_next;
|
---|
| 25 | reg [2:0] n_reg, n_next;
|
---|
| 26 | reg [7:0] b_reg, b_next;
|
---|
| 27 | reg tx_reg, tx_next;
|
---|
| 28 |
|
---|
| 29 | // body
|
---|
| 30 | // FSMD state & data registers
|
---|
| 31 | always @(posedge clk, posedge reset)
|
---|
| 32 | if (reset)
|
---|
| 33 | begin
|
---|
| 34 | state_reg <= idle;
|
---|
| 35 | s_reg <= 0;
|
---|
| 36 | n_reg <= 0;
|
---|
| 37 | b_reg <= 0;
|
---|
| 38 | tx_reg <= 1'b1;
|
---|
| 39 | end
|
---|
| 40 | else
|
---|
| 41 | begin
|
---|
| 42 | state_reg <= state_next;
|
---|
| 43 | s_reg <= s_next;
|
---|
| 44 | n_reg <= n_next;
|
---|
| 45 | b_reg <= b_next;
|
---|
| 46 | tx_reg <= tx_next;
|
---|
| 47 | end
|
---|
| 48 |
|
---|
| 49 | // FSMD next-state logic & functional units
|
---|
| 50 | always @*
|
---|
| 51 | begin
|
---|
| 52 | state_next = state_reg;
|
---|
| 53 | tx_done_tick = 1'b0;
|
---|
| 54 | s_next = s_reg;
|
---|
| 55 | n_next = n_reg;
|
---|
| 56 | b_next = b_reg;
|
---|
| 57 | tx_next = tx_reg ;
|
---|
| 58 | case (state_reg)
|
---|
| 59 | idle:
|
---|
| 60 | begin
|
---|
| 61 | tx_next = 1'b1;
|
---|
| 62 | if (tx_start)
|
---|
| 63 | begin
|
---|
| 64 | state_next = start;
|
---|
| 65 | s_next = 0;
|
---|
| 66 | b_next = din;
|
---|
| 67 | end
|
---|
| 68 | end
|
---|
| 69 | start:
|
---|
| 70 | begin
|
---|
| 71 | tx_next = 1'b0;
|
---|
| 72 | if (s_tick)
|
---|
| 73 | if (s_reg==15)
|
---|
| 74 | begin
|
---|
| 75 | state_next = data;
|
---|
| 76 | s_next = 0;
|
---|
| 77 | n_next = 0;
|
---|
| 78 | end
|
---|
| 79 | else
|
---|
| 80 | s_next = s_reg + 1;
|
---|
| 81 | end
|
---|
| 82 | data:
|
---|
| 83 | begin
|
---|
| 84 | tx_next = b_reg[0];
|
---|
| 85 | if (s_tick)
|
---|
| 86 | if (s_reg==15)
|
---|
| 87 | begin
|
---|
| 88 | s_next = 0;
|
---|
| 89 | b_next = b_reg >> 1;
|
---|
| 90 | if (n_reg==(DBIT-1))
|
---|
| 91 | state_next = stop ;
|
---|
| 92 | else
|
---|
| 93 | n_next = n_reg + 1;
|
---|
| 94 | end
|
---|
| 95 | else
|
---|
| 96 | s_next = s_reg + 1;
|
---|
| 97 | end
|
---|
| 98 | stop:
|
---|
| 99 | begin
|
---|
| 100 | tx_next = 1'b1;
|
---|
| 101 | if (s_tick)
|
---|
| 102 | if (s_reg==(SB_TICK-1))
|
---|
| 103 | begin
|
---|
| 104 | state_next = idle;
|
---|
| 105 | tx_done_tick = 1'b1;
|
---|
| 106 | end
|
---|
| 107 | else
|
---|
| 108 | s_next = s_reg + 1;
|
---|
| 109 | end
|
---|
| 110 | endcase
|
---|
| 111 | end
|
---|
| 112 | // output
|
---|
| 113 | assign tx = tx_reg;
|
---|
| 114 |
|
---|
| 115 | endmodule
|
---|
Note:
See
TracBrowser
for help on using the repository browser.