0

I was going through a document from Microsemi website (Actel HDL Code) and I found a few implementations of flip-flop (Synchronous, Asynchronous etc.).In all the cases the author has modelled the flip-flops with blocking statements.

I want to know are these implementations correct, because I have always used non blocking to model sequential logic? Am I missing something or is it just a way to model only flip flop and not a sequential circuit in general?

// Rising Edge Flip-Flop with Asynchronous Reset
module dff_async_rst (data, clk, reset, q);
  input data, clk, reset;
  output q;
  reg q;

   always @(posedge clk or negedge reset)
     if (~reset)
       q = 1'b0;
     else
       q = data;

   endmodule

//Rising Edge Flip-Flop with Synchronous Reset
module dff_sync_rst (data, clk, reset, q);
  input data, clk, reset;
  output q;
  reg q;

  always @ (posedge clk)
    if (~reset)
      q = 1'b0;
    else 
      q = data;

endmodule

NOTE : Blocking assignments used in always block to get a sequential logic

Qiu
  • 5,651
  • 10
  • 49
  • 56
chitranna
  • 1,579
  • 6
  • 25
  • 42
  • 1
    Most synthesis tools will generate a flip flop for these examples, although blocking assignments are not common for sequential logic. This answer will give you more detailed insight: http://stackoverflow.com/a/4774450/1383356 – Ari Sep 17 '14 at 18:22

1 Answers1

1

Flip-flops should be modelled with non-blocking (<=) as you previously thought.

If your using any version of verilog after 1995 then your port declarations can be tidied up a little. NB I add begin ends for clarity and _n to designate active low signals.

Rising Edge Flip-Flop with Asynchronous Reset

module dff_async_rst (
  input  data,
  input  clk, 
  input  reset_n,
  output reg q    //SystemVerilog logic is preferred over reg
);

always @(posedge clk or negedge reset_n) begin
  if (~reset_n) begin
    q <= 1'b0;
  end
  else begin
    q <= data;
  end
end

endmodule

Rising Edge Flip-Flop with Synchronous Reset

module dff_sync_rst(
  input  data,
  input  clk, 
  input  reset_n,
  output reg q    //SystemVerilog logic is preferred over reg
);


always @(posedge clk) begin
  if (~reset_n) begin
    q <= 1'b0;
  end
  else begin
    q <= data;
  end
end

endmodule
Morgan
  • 19,934
  • 8
  • 58
  • 84
  • When I code , I use the same coding style as your answer. I use non-blocking assignments to code . But since the author in the given link uses Blocking assignments,is the coding style correct ? I had to ask is it correct as it is from the Microsemi website – chitranna Sep 17 '14 at 16:20
  • No it is not correct. Non-blocking is there to model the behaviour of flip-flops that is why it exists. Not using the correct form will may lead to RTL to gate level simulation mismatches. – Morgan Sep 17 '14 at 18:41