-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuart.v
82 lines (65 loc) · 1.88 KB
/
uart.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
module simpleuart
#(
parameter SFR_ADDRESS=8'h90,
parameter BAUD_DIVISOR=8'h02
) (
input wire rst,
input wire iclk,
input wire ram_wr_en_sfr,
input wire [7:0] ram_wr_addr,
input wire [7:0] ram_wr_byte,
// input ram_rd_en_sfr,
// input ram_rd_addr,
// output [7:0] ram_rd_byte,
output reg tx,
//output reg test_baud,
//input wire rx,
output reg [1:0] state
);
parameter STATE_IDLE = 2'b00;
parameter STATE_START = 2'b01;
parameter STATE_BITS = 2'b10;
parameter STATE_STOP = 2'b11;
reg [3:0] bit_counter = 4'b0;
//reg [1:0] state = 2'b00; // = STATE_IDLE;
reg [9:0] data;
reg [7:0] datain;
// Baudrate generator
reg baud_clk = 0;
reg [7:0] baud_counter = 8'b0;
always @ (posedge iclk) begin
if (rst == 1) begin
state <= STATE_IDLE;
data <= 10'b0;
end else if (ram_wr_en_sfr == 1 && state == STATE_IDLE ) begin
//Write byte to fifo
data <= {1'b0, ram_wr_byte, 1'b1};
state <= STATE_BITS;
bit_counter <= 0;
end
// Baudrate generator and state machine
if(baud_counter < BAUD_DIVISOR) begin
baud_counter <= baud_counter + 1 ;
//on each baudrate generator tick we advance the FSM
end else begin
baud_counter <= 8'b0;
case (state)
STATE_IDLE: begin
tx <= 1'b1;
end
STATE_BITS: begin
//tx <= !tx; //TEST
tx <= data[9];
data <= {data[8:0], 1'b0};
bit_counter <= bit_counter + 1;
if(bit_counter == 9) begin state <= STATE_STOP; end
end
STATE_STOP: begin
tx <= 1'b1;
state <= STATE_IDLE;
bit_counter <= 0;
end
endcase // case (state)
end
end // always @ (posedge iclk)
endmodule // simpleuart