Say I have multiple registers set up to update on the same clock, based on different conditions.
I could set this up as a series of multiple always blocks:
reg [7:0] counter;
reg output_even;
reg output_multiple_of_5;
always_ff @ (posedge clk)
if(reset)
counter <= 0;
else
counter <= counter + 1;
always_ff @ (posedge clk)
if(reset)
output_even <= 0;
else begin
if(counter % 2 == 0)
output_even <= 1;
else
output_even <= 0;
end
always_ff @ (posedge clk)
if(reset)
output_multiple_of_5 = 0;
else begin
if(counter % 5 == 0)
output_multiple_of_5 <= 1;
else
output_multiple_of_5 <= 0;
end
Alternatively, I could set this up as a single always block with multiple conditions:
reg [7:0] counter;
reg output_even;
reg output_multiple_of_5;
always_ff @ (posedge clk)
if(reset) begin
counter = 0;
output_even = 0;
output_multiple_of_5 <= 0;
end
else begin
counter = counter + 1;
if(counter % 2 == 0)
output_even <= 1;
else
output_even <= 0;
if(counter % 5 == 0)
output_multiple_of_5 <= 1;
else
output_multiple_of_5 <= 0;
end
I suppose one way of looking at this is whether I should use separate always blocks for separate clocks, or for separate registers.
- Is there any difference between how these two code blocks will be synthesized? Extra clock cycle, implied latch, etc, or are they functionally equivalent?
- Is there any stylistic or code-maintenance reason to prefer one style vs the other?