FPGA Verilog 串行转并行(1位-16位)带tb

fpga Verilog 串行转并行(1位-16位)带tb



使用步骤

代码如下(示例):将所有变量输出是为了方便仿真

module one_sixteen(
	input clk,
	input rst_n,
	input din,
	input start,//检测帧头标志
	output reg   [15:0] data,//缓存数据
	output reg   [5:0]  cnt,//计数器
	output reg   [15:0] dout//实际数据
);


//输入进来之后一直进行移位 与start标志无关
always @(posedge clk or negedge rst_n) begin
   if (rst_n == 1'b0) begin
   	data <= 16'b0;
		end
   else begin
   	data <= {data[14:0], din};	//低位先赋值
   	end
   end
	
//start标志决定cnt的增加 当cnt==16 cnt归为1 注意写法要写成000001
always @(posedge clk or negedge rst_n) begin
   if (rst_n == 1'b0) begin
   	cnt <= 6'b0;
		end
   else if (start == 1 && cnt < 16) begin
   	cnt <= cnt + 1'b1;	
   	end
   else begin
		cnt <= 6'b000001;
		end
   end	
	
//当cnt=16的时候把想要的值取出来
always @(posedge clk or negedge rst_n) begin
   if (rst_n == 1'b0) begin
   	dout <= 16'b0;
		end
   else if(cnt == 6'b010000)begin
   	dout <= data;	//低位先赋值
   	end
   else begin 
		dout <= dout;
	end
   end	

endmodule

tb仿真文件

代码如下(示例):



`timescale 1 ns/ 1 ps
module one_sixteen_vlg_tst();
// constants                                           
// general purpose registers
reg eachvec;
// test vector input registers
reg clk;
reg din;
reg rst_n;
reg start;
// wires                                               
wire [15:0]  dout;
wire   [15:0] data;
wire  [5:0]  cnt;

integer j;
// assign statements (if any)                          
one_sixteen i1 (
// port map - connection between master ports and signals/registers   
	.clk(clk),
	.din(din),
	.dout(dout),
	.rst_n(rst_n),
	.data(data),
	.cnt(cnt),
	.start(start)
);
initial begin
  clk <= 1'b0;
  rst_n <= 1'b0;
  start = 1;

#5
  rst_n <= 1'b1;
  
  
#100000
  start <= 1'b0;
#500
  start <= 1'b1;
end

always #100 clk =~ clk;  //5m

initial
begin
	#5 din <=1;
	for(j = 0;j<10000;j = j+1)
	begin
		#200 din = 0;     
		#200 din = 1;
		#200 din = 0;
		#200 din = 1;

		#200 din = 0;
		#200 din = 1;
		#200 din = 0;
		#200 din = 1;

		#200 din = 0;
		#200 din = 1;
		#200 din = 0;
		#200 din = 1;

		#200 din = 0;
		#200 din = 1;
		#200 din = 0;
		#200 din = 1;
		end
end

endmodule



总结

可以通过仿真
在这里插入图片描述
码字不易 留下你的足迹!

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值