//产生复位信号
initial
begin
rst_n=1'b1;
#20 rst_n=1'b0;
#60 rst_n=1'b1;
end
//产生时钟信号
parameter period_clk=10;
initial
begin
clk50M=1'b0;
forever #period_clk clk50M=~clk50M;
end
//循环发送数据0-255
reg[7:0] tr_data;
reg[7:0] cmp_data;
always@(negedge rst_n,posedge clk50M)
begin
if(!rst_n)
begin
tr_data<=8'h00;
end
else
begin
if((txd_start==1'b0) && (txd_busy==1'b0))
begin
txd_start<=1'b1;
txd_data<=tr_data;
cmp_data<=tr_data;
tr_data<=tr_data+1'b1;
$display("write data is %d",tr_data);
end
else
txd_start<=1'b0;
end
end
//上升沿检测
reg rxd_ready1,rxd_ready2,rxd_ready3;
wire rxd_ready4;
always@(negedge rst_n,posedge clk50M)
begin
if(!rst_n)
begin
rxd_ready1<=1'b0;
rxd_ready2<=1'b0;
rxd_ready3<=1'b0;
end
else
begin
rxd_ready1<=rxd_data_ready;
rxd_ready2<=rxd_ready1;
rxd_ready3<=rxd_ready2;
end
end
assign rxd_ready4= rxd_ready2 & (!rxd_ready3);
//接收数据,同时和发送数据进行校对
always@(posedge clk50M)
begin
if(rxd_ready4==1'b1)
begin
$display("receive data is %d",rxd_data);
if(cmp_data==rxd_data)
begin
$display("(^_^)");
end
else
begin
$display("error is occur('_')");
end
end
end