If-else statements inside always blocks help decide what the circuit should do based on conditions.
If-else in always blocks in Verilog
always @(sensitivity_list) begin
if (condition) begin
// statements when condition is true
end else begin
// statements when condition is false
end
endThe always block runs whenever signals in the sensitivity list change.
Use begin and end to group multiple statements inside if or else.
always @(posedge clk) begin
if (reset) begin
q <= 0;
end else begin
q <= d;
end
endalways @(a or b) begin if (a > b) out = a; else out = b; end
This module sets output q to 0 when reset is high at clock's rising edge. Otherwise, it copies input d to q.
module simple_if_else(
input wire clk,
input wire reset,
input wire d,
output reg q
);
always @(posedge clk) begin
if (reset) begin
q <= 0;
end else begin
q <= d;
end
end
endmoduleUse non-blocking assignments (<=) inside clocked always blocks for correct timing.
Always include all conditions to avoid unintended latches.
The sensitivity list controls when the always block runs; for clocked logic, use posedge or negedge.
If-else inside always blocks lets you choose actions based on conditions.
Use begin and end to group multiple statements.
Always blocks run when signals in their sensitivity list change.
Practice
if-else statements inside always blocks in Verilog?Solution
Step 1: Understand the role of
alwaysblocksalwaysblocks describe hardware that reacts to changes in signals.Step 2: Understand
if-elseinsidealwaysif-elselets hardware choose different actions based on conditions.Final Answer:
To control hardware behavior based on conditions -> Option CQuick Check:
if-else controls hardware actions = B [OK]
- Thinking if-else declares variables
- Confusing if-else with loops
- Using if-else to define ports
if-else statement inside an always block in Verilog?Solution
Step 1: Check the correct sensitivity list syntax
It must bealways @ (posedge clk), not missing @ or parentheses.Step 2: Check if-else syntax
Condition must be in parentheses, and statements can be single-line withoutbegin-end.Final Answer:
always @ (posedge clk) if (a) b = 1; else b = 0; -> Option BQuick Check:
Correct always and if-else syntax = D [OK]
- Omitting @ in always sensitivity
- Missing parentheses around condition
- Using braces {} instead of begin-end
out after the following code runs if sel = 1 and in = 5?
always @(*) begin
if (sel == 1)
out = in + 1;
else
out = in - 1;
endSolution
Step 1: Check the condition with given inputs
Sincesel == 1is true, the first branch executes.Step 2: Calculate the assigned value
out = in + 1 = 5 + 1 = 6.Final Answer:
6 -> Option DQuick Check:
sel=1 triggers out=in+1=6 [OK]
- Ignoring the if condition value
- Subtracting instead of adding
- Assuming out is unchanged
always @ (posedge clk) begin
if (reset == 1)
count = 0;
else if (enable == 1)
count = count + 1;
endSolution
Step 1: Check assignment type inside clocked
Inside clocked blocks, non-blocking assignments (always<=) should be used for registers.Step 2: Verify other parts
Sensitivity list and if-else structure are correct; missing else is allowed but not best practice.Final Answer:
Using blocking assignment = instead of non-blocking <= -> Option AQuick Check:
Clocked always needs non-blocking <= [OK]
- Using = instead of <= in sequential logic
- Forgetting begin-end for multiple statements
- Ignoring missing else cases
always block with if-else. The counter increments on enable and resets to 0 on reset. It saturates at 3 (does not wrap). Which code snippet correctly implements this behavior?Solution
Step 1: Check reset and enable behavior
Reset sets count to 0; enable increments count only if less than 3.Step 2: Verify saturation logic
always @ (posedge clk) begin if (reset) count <= 0; else if (enable && count < 3) count <= count + 1; else count <= count; end increments only if count < 3, else holds count, correctly saturating at 3.Step 3: Check assignment type and completeness
Uses non-blocking <= and covers all cases with else count <= count.Final Answer:
always @ (posedge clk) begin if (reset) count <= 0; else if (enable && count < 3) count <= count + 1; else count <= count; end -> Option AQuick Check:
Saturate at 3 by conditional increment and hold = A [OK]
- Using blocking assignments in sequential logic
- Resetting incorrectly or missing saturation check
- Wrapping counter instead of saturating
Ready to prove it?
Pass the test and this pattern moves unknown
