4位并行移位1个时钟周期,Verilog上的延迟仿真

问题描述 投票:0回答:1

我想实现下面的时序图(这里的数字随意,主要是总的原理)。在我的任务中,有一个 4 位数据序列。该序列必须写入寄存器 A、B 和 C,并在每个相应的寄存器中相对于前一个寄存器移位 1 个时钟周期。

enter image description here

这是我在 Verilog 上的代码:

module shift4bit(clock, data, out, outRegA, outRegB, outRegC);
input clock;
input [3:0] data;
output [3:0] out;
output [3:0] outRegA;
output [3:0] outRegB;
output [3:0] outRegC;
reg [3:0] RegA;
reg [3:0] RegB;
reg [3:0] RegC;

always @(posedge clock) begin
RegA <= data;
RegB <= RegA;
RegC <= RegB;
end

assign out = data;
assign outRegA = RegA;
assign outRegB = RegB;
assign outRegC = RegC;

endmodule

和测试台:

`timescale 1ns / 1ps
module shift4bit_tb; 
// Inputs
reg [3:0] data;
reg clock;
// Outputs
wire [3:0] out;
wire [3:0] outRegA;
wire [3:0] outRegB;
wire [3:0] outRegC;
// Instantiate the Unit Under Test (UUT)
shift4bit uut (.clock(clock),.data(data),.out(out),.outRegA(outRegA),.outRegB(outRegB),.outRegC(outRegC));
initial begin
// Initialize Inputs
data  = 4'b1111;
clock  = 1'b1;
// Wait 100 ns for global reset to finish
#20;
// Add stimulus here
data = 4'b0001; #20;
data = 4'b0010; #20;
data = 4'b0011; #20;
data = 4'b0100; #20;
data = 4'b0101; #20;
data = 4'b0110; #20;
data = 4'b0111; #20;
data = 4'b1000; #20;
end
always #10 clock = ~clock;  
endmodule

我得到了以下结果。似乎有转变。告诉我,我的代码是否正确?它可以在 FPGA 上合成为真实电路吗?

enter image description here

verilog hdl
1个回答
0
投票

像这样重新编写测试平台会更好:

@( posedge clock );
data <= 4'b0001;
@( posedge clock );
data <= 4'b0010;
@( posedge clock );
data <= 4'b0011;
@( posedge clock );

因为如果使用'=',posege时钟和数据同时变化,模拟器无法判断哪个先出现。如果它首先处理数据更改,然后处理 posege 时钟,您将看到数据传递到 outRegA,而没有 1 个时钟周期延迟。

在实际设计中,D 触发器输出稍微落后于 posege 时钟。因此,将“@( posege Clock )”放入测试平台中以确保这一点。

© www.soinside.com 2019 - 2024. All rights reserved.