Electronic – Is it good to code combinational and sequential logic separated into two always blocks

digital-logicverilog

Modeling both sequential and combinational logic within the same always block a good practice or is it recommended to code them in separate blocks.

always @(a or b) 
  y = a ^ b;

always @(posedge clk or negedge rst_n) 
  if (!rst_n) q <= 1'b0; 
  else
  q <= y;

Or doing this:

always @(posedge clk or negedge rst_n) 
  if (!rst_n) q <= 1'b0; 
  else
  q <= a ^ b;

I know both would result in same functionality.

But it is also said that mixing blocking and nonblocking assignments in the same always block is a poor coding style. Do we not use blocking for combinational logic and non-blocking for sequential logic.

  1. Modeling both sequential and combinational logic within the same always block a good practice.

  2. Mixing blocking and nonblocking assignments in the same always block is a poor coding style.

Then in that case above statements are contradicting isn't it?

Best Answer

Mainly opinion based, and below is mine.

Each always block should either implement combinatorial logic or sequential design, but the sequential design may contain expressions that result in combinatorial logic, as long as the result that is assigned is either combinatorial logic or sequential design for all results of the always block. You two examples already adhere to this rule.

Use blocking assignment for combinatorial logic, and non-blocking assignment for sequential design to avoid race conditions between different always blocks driven by the same clock.

Finally, benefit from using an advanced synthesis/simulation tool, and write the code in a way that is most obvious and readable, in order to reduce risk of bugs, and make future maintenance easier. The purpose of having an advanced synthesis/simulation tool is that the tool handles the tedious task of using resources the best way, so you can write the code the easy way. Thus don't write hardware tailored code with the purpose of utilizing resources the best way, except if you are aware of a specific problem, which is probably not the case with your example code.

With your example, I think the single always block is most obvious and readable, so that should be preferred.