You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
2.9 KiB
Coq

8 months ago
module uart_tx(
input sys_clk, // U18
input sys_rst, //J15
input start_en,
input [7:0] tx_data, //
output reg txd, // rx,
output reg tx_busy
);
parameter CLK_FREQ = 5000_0000;
parameter BPS = 115200;
localparam B_MAX = CLK_FREQ / BPS;
reg [3:0]tx_d_cnt;
reg [15:0] baud_cnt;
// start_en , , busy
reg [7:0] temp_tx_data; //
always @(posedge sys_clk or negedge sys_rst) begin
if (sys_rst == 1'b0) begin
temp_tx_data <= 'b0;
tx_busy <= 'b0;
end
else if(start_en) begin
temp_tx_data <= tx_data; //
tx_busy <= 'b1; //
end
else if (tx_d_cnt == 'b1001 && baud_cnt == B_MAX-1) begin //
8 months ago
temp_tx_data <= 'b0;
tx_busy <= 0;
end
else begin
temp_tx_data <= temp_tx_data;
tx_busy <= tx_busy;
end
end
//
always @(posedge sys_clk or negedge sys_rst) begin
if (sys_rst == 1'b0) begin
baud_cnt <= 'b0;
end
else if (start_en) begin // ,
baud_cnt <= 'b0;
end
8 months ago
//
else if (tx_busy) begin
if (baud_cnt == B_MAX-'b1) begin // 0 0~433 434
baud_cnt <= 'b0;
end
else begin
baud_cnt <= baud_cnt + 'b1;
end
end
else begin
baud_cnt <= 'b0;
end
end
//
always @(posedge sys_clk or negedge sys_rst) begin
if (sys_rst == 1'b0) begin
tx_d_cnt <= 'b0;
end
else if (start_en) begin // ,
tx_d_cnt <= 'b0;
end
8 months ago
//
else if (tx_busy) begin
if (baud_cnt == B_MAX-'b1) begin
tx_d_cnt <= tx_d_cnt + 'b1;
end
else begin
tx_d_cnt <= tx_d_cnt;
end
end
else begin
tx_d_cnt <= 'b0;
end
end
always @(posedge sys_clk or negedge sys_rst) begin
if (sys_rst == 1'b0) begin
txd <= 'b1;
end
else if (tx_busy) begin
case (tx_d_cnt)
'b0000: txd <= 'b0;
'b0001: txd <= temp_tx_data[0];
'b0010: txd <= temp_tx_data[1];
'b0011: txd <= temp_tx_data[2];
'b0100: txd <= temp_tx_data[3];
'b0101: txd <= temp_tx_data[4];
'b0110: txd <= temp_tx_data[5];
'b0111: txd <= temp_tx_data[6];
'b1000: txd <= temp_tx_data[7];
'b1001: txd <= 'b1;
default: txd <= 'b1;
endcase
end
else begin
txd <= 'b1;
end
end
endmodule