HDLBits之DFFs and gates

Given the finite state machine circuit as shown, assume that the D flip-flops are initially reset to zero before the machine begins.

Build this circuit.

错误代码使用非阻塞赋值:Q1Q2Q3未全部到达最后的与非逻辑,所以仿真结果不对。

module top_module (
    input clk,
    input x,
    output z
); 
   reg     Q1,Q2,Q3;
    always@(posedge clk)begin
        Q1<=x^Q1;
        Q2<=x&(~Q2);
        Q3<=x|(~Q3);
        z=~(Q1|Q2|Q3);
    end

endmodule
  仿真结果:

 

 改为阻塞赋值:(还需要注意z初始化为1)

 代码:

module top_module (
    input clk,
    input x,
    output z
); 
   reg     Q1,Q2,Q3;
    initial z=1’b1;
    always@(posedge clk)begin
        Q1=x^Q1;
        Q2=x&(~Q2);
        Q3=x|(~Q3);
        z=~(Q1|Q2|Q3);
    end
        

        

endmodule


版权声明:本文为rosemaryandthyme原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。