我正在尝试模拟 t-flipflop 的工作。
`timescale 1ns / 1ps
module t_flipflop(
input t,
input clk,
input clear,
output q,
output qbar
);
wire sbar, rbar;
assign sbar= ~(t & clk & qbar & clear);
assign rbar= ~(t & clk & q);
assign q= ~(sbar & qbar);
assign qbar= ~(rbar & q & clear);
endmodule
现在,在输出中,当 t=1 时,q 的值会切换,但 qbar 的值始终为 1。
同样,当 t=1 时,q 始终为 0,qbar 为 1。
我究竟做错了什么?
测试治具:
`timescale 1ns / 1ps
module test_t_flipflop;
// Inputs
reg t;
reg clk;
reg clear;
// Outputs
wire q;
wire qbar;
// Instantiate the Unit Under Test (UUT)
t_flipflop uut (
.t(t),
.clk(clk),
.clear(clear),
.q(q),
.qbar(qbar)
);
initial begin
clear=1'b0;
#34 clear=1'b1;
end
initial begin
t=1'b0;
clk=1'b0;
forever #15 clk=~clk;
end
initial begin
#10 t=1;
#95 t=0;
#40 t=1;
end
我想用数据流模型来实现这一点,以便清楚地理解它。
您正在尝试使用连续分配来建模顺序逻辑。这可能会导致不可预测的模拟结果。例如,当我使用 Incisive 运行您的代码时,它会导致无限循环,这通常表明存在竞争条件。我认为比赛是由于反馈路径造成的:q
依赖于取决于qbar
这又取决于q
.
对顺序逻辑进行建模的正确方法是使用以下寄存器传输逻辑 (RTL) 编码风格:
module t_flipflop (
input t,
input clk,
input clear,
output reg q,
output qbar
);
assign qbar = ~q;
always @(posedge clk or negedge clear) begin
if (!clear) begin
q <= 0;
end else if (t) begin
q <= ~q;
end
end
endmodule
这消除了反馈路径,并通过消除内部连线简化了代码。也可用于合成。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)