串并转换
一、并转串
设计一个并转串模块,要求四位输入转为一位输出。
- 分析:使用计数器(0-3计数)来计算输出的bit位数,当计数器的值为3时,将输入的数据data装载到缓存data_buff中,同时将valid_out置为1,其他情况将data_buff<<1,data_out为data_buff的最高位,valid_out为0;
module parallel2serial(
input clk,
input rst_n,
input[3:0] data,
output reg valid_out,
output data_out
);
reg[3:0] data_reg;
reg[2:0] cnt;
assign data_out = data_reg[3];
always@(posedge clk, negedge rst_n)begin
if(!rst_n)begin
cnt <= 'b0;
end
else begin
cnt <= cnt==3'd3? 0:cnt+1'b1;
end
end
always@(posedge clk, negedge rst_n)begin
if(!rst_n)begin
data_reg <= 4'b0;
end
else if(cnt==3'd3)begin
data_reg <= data;
end
else begin
data_reg <= data_reg <<1;
end
end
always@(posedge clk, negedge rst_n)begin
if(!rst_n)begin
valid_out <= 1'b0;
end
else begin
valid_out <= cnt==3'd3? 1'b1:1'b0;
end
end
endmodule
- 仿真结果
二、串转并
设计一个串转并模块,1 bit输入4 bit输出,输入数据有有效标志符,输出数据也有有效标志符
module serial2parallel(
input clk,
input rst_n,
input data_in,
input valid_in,
output reg[3:0]data_out,
output reg valid_out
);
reg[2:0] cnt;
reg[3:0] data_reg;
always@(posedge clk, negedge rst_n)begin
if(!rst_n)begin
cnt <= 3'd0;
end
else if(valid_in)begin
cnt <= cnt==3'd3? 0:cnt+1'b1;
end
end
always@(posedge clk, negedge rst_n)begin
if(!rst_n)begin
valid_out <= 1'b0;
end
else if(valid_in & cnt==3'd3)begin
valid_out <= 1'b1;
end
else begin
valid_out <= 1'b0;
end
end
always@(posedge clk, negedge rst_n)begin
if(!rst_n)begin
data_reg <= 4'b0;
end
else if(valid_in)begin
data_reg <= {data_reg[2:0], data_in};;
end
end
always@(posedge clk, negedge rst_n)begin
if(!rst_n)begin
data_out <= 'b0;
end
else if(valid_in & cnt==3'd3)begin
data_out <= {data_reg[2:0], data_in};
end
end
endmodule
- 仿真结果