2

I want to infer a simple flip flop using verilog. Here is the code.

module tb_simpleRegister();

  reg clk;
  reg a;
  wire b;

  simpleRegister dut
  (
    .clk(clk),
    .a(a),
    .b(b),
    .c(c)
  );


  initial begin
    clk=1;
    a=0;
    #10
    a=1;
    #10
    a=0;
  end

  always #5 clk = ~clk;
endmodule

module simpleRegister(
    input clk,
    input a,
    output reg b,
    output reg c
    );

    always @(posedge clk) begin
        b <= a;
        c <= b;
    end
endmodule

And here is the result when I run it. The b output of dut does not behave like a flip flop. However output c does. Any comments on why is this happening?

1
  • What do you mean with "does not behave like a flip flop"? What is the intended result? Commented Apr 12, 2015 at 6:26

1 Answer 1

4

I would rewrite the test bench as follows:

initial begin
    @(posedge clk);
    a<=0;
    @(posedge clk);
    a<=1;
    @(posedge clk);
    a<=0;
  end

//Clock in separate process
initial begin
  clk=1;
  forever begin
    #5 clk = ~clk;
  end
end

The non-blocking assignment will ensure the value of a is changed just after the clock edge giving the testbench a full cycle setup.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. Your code works fine. Also when I change my initial loop to "initial begin clk=1; a<=0; #10 a<=1; #10 a<=0; end" it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.