D 触发器 Verilog 行为实现有编译错误

D Flip Flop Verilog Behavioral Implementation has compile errors

我有一个d触发器的教程,当我尝试编译时,出现了一些错误。我从 technobyte.org 获取了本教程,并进行了任何更改,但它不起作用。 D触发器和测试平台代码如下。

你能找到问题吗?

D触发器

module D_Flip_Flop(d,clk,clear,q,qbar);

input d, clk, clear; 
output reg q, qbar; 
always@(posedge clk) 
begin
if(clear== 1)
q <= 0;
qbar <= 1;
else 
q <= d; 
qbar = !d; 
end 
endmodule

D型触发器测试台

//test bench for d flip flop
//1. Declare module and ports

module dff_test;
reg D, CLK,reset;
wire Q, QBAR;

//2. Instantiate the module we want to test. We have instantiated the dff_behavior

D_Flip_Flop dut(.q(Q), .qbar(QBAR), .clear(reset), .d(D), .clk(CLK)); // instantiation by port name.

//3. Monitor TB ports
$monitor("simtime = %g, CLK = %b, D = %b,reset = %b, Q = %b, QBAR = %b", $time, CLK, D, reset, Q, QBAR);

//4. apply test vectors
initial begin
  clk=0;
     forever #10 clk = ~clk;  
end 
initial begin 
 reset=1; D <= 0;
 #100; reset=0; D <= 1;
 #100; D <= 0;
 #100; D <= 1;
end 
endmodule

错误

您的代码中存在 3 种类型的语法错误:

  1. if/else 中的多个语句需要 begin/end
  2. $monitor 语句应该在初始块内。
  3. Verilog 区分大小写。 clk 未声明。将所有 CLK 更改为 clk.

您应该向创建该教程的人报告这些错误。

这是为我编译干净的代码。我还为您的 DFF 添加了适当的缩进:

module D_Flip_Flop(d,clk,clear,q,qbar);

input d, clk, clear; 
output reg q, qbar; 
always@(posedge clk) begin
    if(clear== 1) begin
        q <= 0;
        qbar <= 1;
    end else begin 
        q <= d; 
        qbar = !d; 
    end
end
endmodule

module dff_test;
reg D, clk,reset;
wire Q, QBAR;

//2. Instantiate the module we want to test. We have instantiated the dff_behavior

D_Flip_Flop dut(.q(Q), .qbar(QBAR), .clear(reset), .d(D), .clk(clk)); // instantiation by port name.

//3. Monitor TB ports
initial $monitor("simtime = %g, clk = %b, D = %b,reset = %b, Q = %b, QBAR = %b", $time, clk, D, reset, Q, QBAR);

//4. apply test vectors
initial begin
  clk=0;
     forever #10 clk = ~clk;  
end 

initial begin 
 reset=1; D <= 0;
 #100; reset=0; D <= 1;
 #100; D <= 0;
 #100; D <= 1;
end 
endmodule

良好的编码习惯建议对时序逻辑使用非阻塞分配。变化:

qbar = !d; 

qbar <= !d;