Challenge - 5 Problems
If-else Always Block Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of if-else in always block with clock edge
What is the value of
q after the positive edge of clk when d = 1 and reset = 0?Verilog
module test(input clk, input reset, input d, output reg q); always @(posedge clk or posedge reset) begin if (reset) q <= 0; else if (d) q <= 1; else q <= 0; end endmodule
Attempts:
2 left
💡 Hint
Check the order of if-else conditions and the values of inputs at clock edge.
✗ Incorrect
When reset is 0 and d is 1 at the positive clock edge, the always block sets q to 1 because the else if (d) condition is true.
❓ Predict Output
intermediate2:00remaining
Behavior of nested if-else in always block
Given the inputs
enable = 1, mode = 0, and reset = 0, what is the value of out after the always block executes?Verilog
module nested_if(input enable, input mode, input reset, output reg out); always @(*) begin if (reset) out = 0; else begin if (enable) begin if (mode) out = 1; else out = 0; end else out = 0; end end endmodule
Attempts:
2 left
💡 Hint
Trace the nested if-else conditions carefully with given inputs.
✗ Incorrect
reset is 0, enable is 1, mode is 0, so the inner if (mode) is false, setting out to 0.
🔧 Debug
advanced2:00remaining
Identify the error in if-else usage inside always block
What error will this Verilog code produce when synthesized or simulated?
Verilog
module error_example(input clk, input reset, input d, output reg q); always @(posedge clk) begin if (reset == 1) q <= 0; else q <= d; end endmodule
Attempts:
2 left
💡 Hint
Check the syntax of the if statement condition.
✗ Incorrect
In Verilog, the if condition must be enclosed in parentheses. Missing parentheses cause a syntax error.
❓ Predict Output
advanced2:00remaining
Output of if-else with blocking and non-blocking assignments
What is the value of
q after the always block executes on a positive clock edge if d = 1 and reset = 0?Verilog
module blocking_nonblocking(input clk, input reset, input d, output reg q); always @(posedge clk or posedge reset) begin if (reset) q = 0; else q <= d; end endmodule
Attempts:
2 left
💡 Hint
Understand difference between blocking (=) and non-blocking (<=) assignments.
✗ Incorrect
On reset, q is assigned 0 immediately (blocking). On clock edge with reset=0, q is updated to d (1) using non-blocking assignment.
🧠 Conceptual
expert3:00remaining
Effect of missing else in if-else chain inside always block
Consider this always block snippet:
always @(posedge clk) begin
if (reset)
q <= 0;
if (enable)
q <= d;
end
What is the behavior of q when both reset and enable are high at the same clock edge?Attempts:
2 left
💡 Hint
Remember that both if statements execute independently if no else is used.
✗ Incorrect
Both if statements execute independently. The second assignment to q (q <= d) overwrites the first (q <= 0) in the same clock cycle.