1
\$\begingroup\$

If I want to create a pipelined flip-flop (FF) structure, where data from the input is at the output after 2 clock cycles. This is a top-down blocking assignment code.

module circuit (
 input wire clk,
 input wire rstn,
 input wire [7:0] din,
 output reg [7:0] dout
);
 reg [7:0] mid;
 always @ (posedge clk) begin
 if (!rstn) begin
 mid = 0;
 dout = 0;
 end else begin
 mid = din;
 dout = mid;
 end
 end
endmodule

Schematic for this code:

enter image description here

If I use blocking assignment in reversed order:

module circuit (
 input wire clk,
 input wire rstn,
 input wire [7:0] din,
 output reg [7:0] dout
);
 reg [7:0] mid;
 always @(posedge clk) begin
 if (!rstn) begin
 mid = 0;
 dout = 0;
 end else begin
 /*
 mid <= din;
 dout <= mid;
 */
 dout = mid;
 mid = din;
 end
 end
endmodule

Then I get a schematic like this, which is identical to what would I get if I were to use non-blocking assignments.

enter image description here

If I keep the ordering in mind, can I use the blocking assignments to get the behavior from the circuit for which I usually use the NBA? For example, avoiding race conditions, sampling asynchronous input, etc.?

toolic
10.8k11 gold badges31 silver badges35 bronze badges
asked Oct 15, 2023 at 16:28
\$\endgroup\$
0

1 Answer 1

1
\$\begingroup\$

The best approach is to follow the advice in the paper:

Guideline: Use nonblocking assignments in always blocks that are written to generate sequential logic

Then you never need to worry about race conditions or other unexpected simulation/synthesis issues. For sequential logic, use this, for example:

always @(posedge clk) begin
 if (!rstn) begin
 mid <= 0;
 dout <= 0;
 end else begin
 mid <= din;
 dout <= mid;
 end
end
answered Oct 15, 2023 at 19:14
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.