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.

104 lines
2.5 KiB
Coq

7 months ago
`include "param.v"
module uart_tx(
input wire sys_clk ,
input wire sys_rst_n ,
input wire [7: 0] tx_din ,//
input wire rx_vld ,//
output wire tx_vld ,
output reg tx_dout //
);
parameter BAUD = `SYS_FRQ / `MAX;//
reg [8: 0] data_reg;//
reg tx_flag ;//
//
reg [12: 0] cnt_bps;
wire add_cnt_bps;
wire end_cnt_bps;
//
reg [3: 0] cnt_bit;
wire add_cnt_bit;
wire end_cnt_bit;
//
always @(posedge sys_clk or negedge sys_rst_n)begin
if(!sys_rst_n)begin
data_reg <= 9'hx;
end
else if(rx_vld)begin
data_reg <= {tx_din, 1'b0};//
end
else begin
data_reg <= data_reg;
end
end
//tx_flag
always @(posedge sys_clk or negedge sys_rst_n)begin
if(!sys_rst_n)begin
tx_flag <= 1'b0;
end
else if(rx_vld)begin
tx_flag <= 1'b1;
end
else if(end_cnt_bit)begin
tx_flag <= 1'b0;
end
else begin
tx_flag <= tx_flag;
end
end
//cnt_bps
always @(posedge sys_clk or negedge sys_rst_n)begin
if(!sys_rst_n)begin
cnt_bps <= 13'd0;
end
else if(add_cnt_bps)begin
if(end_cnt_bps)begin
cnt_bps <= 13'd0;
end
else begin
cnt_bps <= cnt_bps + 1'd1;
end
end
else begin
cnt_bps <= cnt_bps;
end
end
assign add_cnt_bps = tx_flag;
assign end_cnt_bps = add_cnt_bps && cnt_bps == BAUD - 1'd1;
//cnt_bit
always @(posedge sys_clk or negedge sys_rst_n)begin
if(!sys_rst_n)begin
cnt_bit <= 4'd0;
end
else if(add_cnt_bit)begin
if(end_cnt_bit)begin
cnt_bit <= 4'd0;
end
else begin
cnt_bit <= cnt_bit + 1'd1;
end
end
else begin
cnt_bit <= cnt_bit;
end
end
assign add_cnt_bit = end_cnt_bps;//
assign end_cnt_bit = add_cnt_bit && cnt_bit == 4'd8;
//tx_dout
always @(posedge sys_clk or negedge sys_rst_n)begin
if(!sys_rst_n)begin
tx_dout <= 1'b1;
end
else if(tx_flag)begin
tx_dout <= data_reg[cnt_bit];//
end
else begin
tx_dout <= 1'b1;
end
end
assign tx_vld = end_cnt_bit;
endmodule