基于FPGA并行数据转串行数据(verilog)

   日期:2020-09-25     浏览:261    评论:0    
核心提示:代码:module bit4_bit1(input clk_50M_i,input rst_n_i,input [3:0]data_i,input data_en_i,output data_o,output data_en_o);reg [3:0]data_i_tmp1;reg [3:0]data_i_tmp2;reg [3:0]data_i_valid;reg data_en_tmp1;reg

Verilog代码:

module bit4_bit1(
    input    clk_50M_i,
	input    rst_n_i,
	
	input    [3:0]data_i,
	input    data_en_i,
	
	output   data_o,
	output   data_en_o
);

reg    [3:0]data_i_tmp1;
reg    [3:0]data_i_tmp2;
reg    [3:0]data_i_valid;
reg         data_en_tmp1;
reg         data_en_tmp2;

reg    [3:0]cnt;

reg  data_o_tmp;
reg  data_en_o_tmp;
//进来的数据和使能打两拍同步一下
always@(posedge clk_50M_i or negedge rst_n_i)
	if(!rst_n_i)begin
	    data_i_tmp1    <= 4'b0;
		data_i_tmp2    <= 4'b0;
		data_en_tmp1   <= 1'b0;
		data_en_tmp2   <= 1'b0;
	end
	else  begin
	    data_i_tmp1    <= data_i;
		data_i_tmp2    <= data_i_tmp1;
		data_en_tmp1   <= data_en_i;
		data_en_tmp2   <= data_en_tmp1;
    end
always@(posedge clk_50M_i or negedge rst_n_i)
	if(!rst_n_i)
	    data_i_valid <= 4'b0;
	else if(data_en_tmp2)
	    data_i_valid <= data_i_tmp2;
	else
	    data_i_valid <= data_i_valid;
always@(posedge clk_50M_i or negedge rst_n_i)
	if(!rst_n_i)
	    cnt <= 4'b0;
	else if(data_en_tmp2 || (cnt>0&&cnt<4))
	    cnt <= cnt + 1'b1;
	else 
	    cnt <= 4'b0;
		
always@(posedge clk_50M_i or negedge rst_n_i)
	if(!rst_n_i)
	    data_o_tmp <= 1'b0;
	else if(cnt!=0)
	    data_o_tmp <= data_i_valid[4-cnt];
	else
	    data_o_tmp <= 1'b0;
always@(posedge clk_50M_i or negedge rst_n_i)
    if(!rst_n_i)
	    data_en_o_tmp <= 1'b0;
	else if(cnt!=0)
	    data_en_o_tmp <= 1'b1;
	else
	    data_en_o_tmp <= 1'b0;
	
assign  data_o = data_o_tmp;
assign  data_en_o = data_en_o_tmp;
endmodule

测试文件:

`timescale 1ns/1ns

module  bit4_bit1_tb();

parameter  clk_period = 20;

reg    clk_50M_i;
reg    rst_n_i;
reg    [3:0]data_i;
reg    data_en_i;



initial begin
        clk_50M_i = 1'b0;
        rst_n_i   = 1'b0;
	data_i = 4'b0000;
        data_en_i = 1'b0;
	
        #100
	rst_n_i = 1'b1;
	
	#200
	data_i = 4'b0001;
        data_en_i = 1'b1;
        #clk_period  data_en_i = 1'b0;

	#200
	data_i = 4'b0010;        
        data_en_i = 1'b1;
        #clk_period  data_en_i = 1'b0;

	#200
	data_i = 4'b0011;
        data_en_i = 1'b1;
        #clk_period  data_en_i = 1'b0;
 
	#200
	data_i = 4'b0100;
        data_en_i = 1'b1;
        #clk_period  data_en_i = 1'b0;

	#200
	data_i = 4'b0101;
        data_en_i = 1'b1;
        #clk_period  data_en_i = 1'b0;

	#200
	data_i = 4'b0110;
        data_en_i = 1'b1;
        #clk_period  data_en_i = 1'b0;

	#200
	data_i = 4'b0111;
         data_en_i = 1'b1;
        #clk_period  data_en_i = 1'b0;

	#200
	data_i = 4'b1000;
        data_en_i = 1'b1;
        #clk_period  data_en_i = 1'b0;
	
	#500
	data_i = 4'b0;
end

always #(clk_period/2) clk_50M_i = ~clk_50M_i;

bit4_bit1  bit4_bit1_inst(
    .clk_50M_i(clk_50M_i),
	.rst_n_i(rst_n_i),
	
	.data_i(data_i),
	.data_en_i(data_en_i),
	
	.data_o(),
    .data_en_o()
);

endmodule

仿真截图:

 
打赏
 本文转载自:网络 
所有权利归属于原作者,如文章来源标示错误或侵犯了您的权利请联系微信13520258486
更多>最近资讯中心
更多>最新资讯中心
0相关评论

推荐图文
推荐资讯中心
点击排行
最新信息
新手指南
采购商服务
供应商服务
交易安全
关注我们
手机网站:
新浪微博:
微信关注:

13520258486

周一至周五 9:00-18:00
(其他时间联系在线客服)

24小时在线客服