用verilog实现串行信号转8bit并行信号

串行信号转并行信号即为解串器(deserialize)。

输入信号有时钟信号clk,复位信号rst和串行数据输入信号din。

输出信号为8bit并行信号dout。

每经过8个时钟周期,便把收到的8个串行信号合成并行信号并输出,等下8个时钟周期过后再输出下一个并行信号。

verilog代码:

module deserialize(
input rst,clk,din,
output reg[7:0] dout
);

reg[7:0] data;
reg[2:0] cnt;


always@(negedge rst, posedge clk)
begin
	if(rst == 1'b0)//reset signial
	begin
		data <= 0;
		cnt <= 0;
		dout <= 0;
	end
	
	else
	begin
		if (cnt == 3'b111)//get all 8bit data,change dout
		begin		
			dout[7-cnt] <= din;
			dout[7:1] <= data[7:1];
			cnt <= 0;
		end
		else
		begin
			data[7-cnt] <= din;
			cnt <= cnt + 1;
		end		
	end
end


endmodule

testbench:

`timescale 1ns/1ns

module deseralize_tb;

reg RST,CLK,DIN;
wire[7:0] DOUT;
integer i,j;

deserialize U_deserialize(.rst(RST),.clk(CLK),.din(DIN),.dout(DOUT));

initial 
begin
	RST = 1;
	#5 RST = 0;
	#2 RST = 1;
end

initial 
begin
	CLK = 0;
	for(i = 0;i<100000;i = i+1)
	begin
		#2 CLK = ~CLK;
	end
end

initial
begin
	#1 DIN = 0;
	for(j = 0;j<10000;j = j+1)
	begin
		#4 DIN = 0;
		#4 DIN = 1;
		#4 DIN = 1;
		#4 DIN = 0;
		#4 DIN = 1;
		#4 DIN = 0;
		#4 DIN = 1;
	end
end

endmodule

这里testbench中的串行输入序列为‘0110101’共7bit(dout是8bit并行数据)的循环,故可以造成相邻dout的不同。

仿真波形:

目前还没想明白并行转串行的时钟问题,需要继续学习

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章