Concept Flow - If-else in always blocks
Start always block
Evaluate if condition
Yes No
Execute if block
Update outputs
End always block
The always block starts, checks the if condition, executes the matching branch, updates outputs, then ends.
always @(posedge clk) begin if (reset) begin q <= 0; end else begin q <= d; end end
| Step | Condition (reset) | Branch Taken | Action | q value |
|---|---|---|---|---|
| 1 | 1 (true) | if | q <= 0 | 0 |
| 2 | 0 (false) | else | q <= d (assume d=5) | 5 |
| 3 | 0 (false) | else | q <= d (assume d=10) | 10 |
| 4 | 1 (true) | if | q <= 0 | 0 |
| 5 | 0 (false) | else | q <= d (assume d=3) | 3 |
| Variable | Start | After 1 | After 2 | After 3 | After 4 | After 5 |
|---|---|---|---|---|---|---|
| reset | X | 1 | 0 | 0 | 1 | 0 |
| d | X | X | 5 | 10 | X | 3 |
| q | X | 0 | 5 | 10 | 0 | 3 |
always @(event) begin
if (condition) begin
// if block
end else begin
// else block
end
end
- If condition true, execute if block
- Else execute else block
- Used for sequential logic triggered by events