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.

108 lines
2.8 KiB
Coq

7 months ago
`include "param.v"
module uart_rx(
input wire sys_clk ,
input wire sys_rst_n ,
input wire rx_din ,//
input wire rcv_start ,
output wire [7: 0] rx_dout ,//
output wire rx_vld //
);
parameter BAUD = `SYS_FRQ / `MAX;//1bit
reg din_reg0;//
reg din_reg1;//沿
wire nedge;//沿
reg rx_flag;//
//bit
reg [3: 0] cnt_bit ;
wire add_cnt_bit;
wire end_cnt_bit;
//
reg [12: 0] cnt_bps ;
wire add_cnt_bps ;
wire end_cnt_bps ;
//
reg [7: 0] data_reg;
always @(posedge sys_clk or negedge sys_rst_n) begin
if(!sys_rst_n)begin
din_reg0 <= 1'b1;
din_reg1 <= 1'b1;
end
else begin
din_reg0 <= rx_din;
din_reg1 <= din_reg0;
end
end
assign nedge = ~din_reg0 && din_reg1;//沿
//rx_flag
always @(posedge sys_clk or negedge sys_rst_n)begin
if(!sys_rst_n)begin
rx_flag <= 1'b0;
end
else if(nedge && rcv_start)begin//沿
rx_flag <= 1'b1;
end
else if(end_cnt_bit)begin//
rx_flag <= 1'b0;
end
else begin
rx_flag <= rx_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 = rx_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;//0-8
//data_reg
always @(posedge sys_clk or negedge sys_rst_n)begin
if(!sys_rst_n)begin
data_reg <= 8'hx;
end
else if((cnt_bit >= 4'd1 && cnt_bit <= 4'd8) && (cnt_bps == BAUD >> 1))begin
data_reg[cnt_bit - 1'd1] = din_reg0;//
end
else begin
data_reg <= data_reg;
end
end
assign rx_dout = data_reg;//
assign rx_vld = end_cnt_bit;//
endmodule