文章目录
用Verilog实现串并和并串转换
一、了解并串和串并转换的原理
串行数据输出是将组成数据和字符的码元按时序逐位予以传输,并行数据传输是将固定位数(通常为8位或16位等)的数据和字符码元同时传输至接收端,串并和并串转换是完成这两种传输方式之间转换的技术。
例如:需要传输的数据有32bit,用串行传输则需要32个时钟周期完成传输,如果用8位并行传输,则32bit数据只需要4个时钟周期就可以完成传输。
并串转换
并转串电路主要由时钟(clk)、复位信号(rst)、并行输入信号(pdin)、串行输出信号(sdout)和使能信号(en)组成。使能信号表示开始执行并转串操作,由于并转串是移位操作,先将八位数据暂存于一个八位寄存器器中,然后左移输出到一位输出端口,通过一个“移位”来实现,当一次并转串完成后,需要重新载入待转换的并行数据时,使能信号要再起来一次。
串并转换
输入一位的数据,每一个clk接收一位,最终以多位(8位)的形式输出。通常采用位拼接的方式来实现。典型的案例,给一位输入,实现8位数据的左移和右移操作。
右移:舍弃低位,高位补输入的值
左移:舍弃高位,低位补输入的值。
转换可以采用两种方式实现
一:lsb优先 Least Significant Bit,最低比特,最低位优先
二:msb优先 Most Significant Bit,最高比特,最高位优先
二、串转并(以一输入八输出为例)
2.1 Verilog实现左移和右移
module test(
input clk,
input rst_n,
input din,
output reg [7:0] dout1,//左移并行输出
output reg [7:0] dout2//右移并行输出
);
reg din_r;
//输入寄存一拍
always @(posedge clk or negedge rst_n)
if (!rst_n)
din_r <= 1'b0;
else
din_r <= din;
//左移并行输出
always @(posedge clk or negedge rst_n) begin
if (!rst_n)
dout1 <= 0;
else
dout1 <= {dout1[6:0],din_r};
end
//右移并行输出
always @(posedge clk or negedge rst_n) begin
if (!rst_n)
dout2 <= 0;
else
dout2 <= {din_r,dout2[7:1]};
end
endmodule
`timescale 1ns/1ns
module test_tb();
reg clk;
reg rst_n;
reg din;
wire [7:0] dout1;//左移并行输出
wire [7:0] dout2;//右移并行输出
test u1 (
. clk(clk),
. rst_n(rst_n),
. din(din),
. dout1(dout1),//左移并行输出
. dout2(dout2)//右移并行输出
);
initial begin
clk = 1;
rst_n = 0;
din <= 0;
#30;
rst_n = 1;
#10;
din <= 1;
#60;
din <= 0;
#1000;
$stop;
end
always #10 clk = ~clk;
endmodule
2.2 MSB、LSB优先(利用计数器实现串转并)
代码:
module test(
input clk,
input rst_n,
input din,
input en,
output reg [7:0] dout1,//MSB并行输出(右移)
output reg [7:0] dout2//LSB并行输出(左移)
);
reg din_r;
reg [3:0] cnt;
//输入寄存一拍
always @(posedge clk or negedge rst_n)
if (!rst_n)
din_r <= 1'b0;
else
din_r <= din;
//设计计数器
always @(posedge clk or negedge rst_n)
if (!rst_n)
cnt <= 1'b0;
else if(en)begin
if(cnt == 4'd7)
cnt <= 1'b0;
else
cnt <= cnt + 1'b1;
end
else
cnt <= 1'b0;
//MSB并行输出
always @(posedge clk or negedge rst_n) begin
if (!rst_n)
dout1 <= 0;
else
dout1[7-cnt] <= din_r;
end
//LSB并行输出(左移)
always @(posedge clk or negedge rst_n) begin
if (!rst_n)
dout2 <= 0;
else
dout2[cnt] <= din_r;
end
endmodule
tb文件:
`timescale 1ns/1ns
module test_tb();
reg clk;
reg rst_n;
reg din;
reg en;
wire [7:0] dout1;
wire [7:0] dout2;
test u1 (
. clk(clk),
. rst_n(rst_n),
. din(din),
. en(en),
. dout1(dout1),
. dout2(dout2)
);
initial begin
clk = 1;
rst_n = 0;
din <= 0;
en = 0;
#30;
rst_n = 1;
#10;
din <= 1;
#22;
en = 1;
#140;
din <= 0;
#500;
$stop;
end
always #10 clk = ~clk;
endmodule
波形图
可看到,dout1表示MSB
dout2表示LSB
三、并转串(以八输入一输出为例)
采用计数器实现MSB和LSB串行输出
module test(
input clk,
input rst_n,
input [7:0] din,
input en,
output reg dout1, // MSB
output reg dout2 // LSB
);
reg [7:0] din_r;
reg [3:0] cnt;
//输入寄存一拍
always @(posedge clk or negedge rst_n)
if (!rst_n)
din_r <= 8'd0;
else
din_r <= din;
//设计计数器
always @(posedge clk or negedge rst_n)
if (!rst_n)
cnt <= 1'b0;
else if(en)begin
if(cnt == 4'd7)
cnt <= 1'b0;
else
cnt <= cnt + 1'b1;
end
else
cnt <= 1'b0;
//MSB串行输出
always @(posedge clk or negedge rst_n) begin
if (!rst_n)
dout1 <= 0;
else
dout1 <= din_r[7-cnt];
end
//LSB串行输出
always @(posedge clk or negedge rst_n) begin
if (!rst_n)
dout2 <= 0;
else
dout2 <= din_r[cnt];
end
endmodule
`timescale 1ns/1ns
module test_tb();
reg clk;
reg rst_n;
reg [7:0] din;
reg en;
wire dout1;//MSB
wire dout2;//LSB
test u1 (
. clk(clk),
. rst_n(rst_n),
. din(din),
. en(en),
. dout1(dout1),
. dout2(dout2)
);
initial begin
clk = 1;
rst_n = 0;
din <= 0;
en = 0;
#30;
rst_n = 1;
#10;
din <= 8'd5;
#22;
en = 1;
#140;
din <= 8'd7;
#500;
$stop;
end
always #10 clk = ~clk;
endmodule
运算符实现移位
module test(
input [7:0] in,
input clk,
input rst_n,
input en,
output out
);
reg [7:0] shift_out;
always@(posedge clk)
if(!rst_n)
shift_out <= 0;
else if(en == 1)
shift_out <= in;
else
shift_out <= shift_out << 1;
assign out = shift_out[7];
endmodule
tb文件:
`timecasle 1ps/1ps
module top_module ();
reg clk=0;
reg rst_n;
reg en;
wire out;
always #5 clk = ~clk; // Create clock with period=10
reg [7:0] in;
initial begin
rst_n = 0;
in = 1;
en = 0;
#10;
rst_n = 1;
#10;
en = 1; //可以将in给shifout暂存
#10
en = 0; //开始移位
#80;
en = 1;
in = 8'b00000111;
#10;
en = 0;
#10;
en = 0;
#80;
in = 0;
$display ("The current time is (%0d ps)", $time);
#120 $finish; // Quit the simulation
end
test inst1 (
.in(in) ,
.clk(clk),
.rst_n(rst_n),
.en(en),
.out(out)
);
endmodule
串入串出移位寄存器
如下是8个D触发器构成的八位移位寄存器。数据在时钟下一位一位的传输,实现数据的先入先出。
这里直接采用在线的HDLBits仿真器进行仿真。代码如下:
module top_module ();
reg clk=0;
always #5 clk = ~clk; // Create clock with period=10
initial `probe_start; // Start the timing diagram
`probe(clk); // Probe signal "clk"
reg in=0;
wire out;
initial begin
#10 in = 1;
#10 in = 0;
#20 in = 1;
#20 in = 0;
$display ("Hello world! The current time is (%0d ps)", $time);
#100 $finish; // Quit the simulation
end
invert inst1 ( .in(in) ,.clk(clk),.out(out) ); // Sub-modules work too.
endmodule
module invert(input in,input clk,output reg out );
reg tmp1,tmp2,tmp3,tmp4,tmp5,tmp6,tmp7;
always@(posedge clk)
begin
tmp1<=in;
tmp2<=tmp1;
tmp3<=tmp2;
tmp4<=tmp3;
tmp5<=tmp4;
tmp6<=tmp5;
tmp7<=tmp6;
out<=tmp7;
end
`probe(in); // Sub-modules can also have `probe()
`probe(out);
endmodule
可看出,当out一开始是未知的,8个clk之前都是未知,当第九个clk上升沿的时候,开始输出in的数据。