0
0
Verilogprogramming~5 mins

If-else in always blocks in Verilog - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of an always block in Verilog?
An always block is used to describe behavior that should happen whenever certain signals change. It helps model sequential or combinational logic by running the code inside whenever the specified signals in the sensitivity list change.
Click to reveal answer
beginner
How does an if-else statement work inside an always block?
Inside an always block, an if-else statement checks a condition and executes one block of code if the condition is true, and another block if it is false. This helps decide which signals to assign based on conditions.
Click to reveal answer
intermediate
Why should you use non-blocking assignments (<=) inside sequential always blocks?
Non-blocking assignments (<=) allow all assignments to happen in parallel at the end of the time step, which models real hardware registers correctly and avoids race conditions.
Click to reveal answer
intermediate
What happens if you forget the else part in an if-else inside an always block?
If you omit the else, the signal might keep its old value unintentionally, causing inferred latches or unexpected behavior in hardware.
Click to reveal answer
beginner
Example: What does this code do?<br>
always @(posedge clk) begin
  if (reset) count <= 0;
  else count <= count + 1;
end
This code resets the count to 0 when reset is true at the rising edge of clk. Otherwise, it increments count by 1 on each clock cycle.
Click to reveal answer
In Verilog, which keyword starts a block that runs whenever signals change?
Aalways
Bif
Cmodule
Dassign
Inside an always block, what does an if-else statement do?
AStarts a loop
BDeclares a new module
CChecks a condition and chooses which code to run
DAssigns a value permanently
Which assignment operator models registers correctly inside a clocked always block?
A<code>=</code> (blocking)
B<code>&lt;=</code> (non-blocking)
C<code>==</code>
D<code>!=</code>
What risk do you face if you omit the else in an if-else inside an always block?
AInferred latches causing unintended memory
BSyntax error
CFaster simulation
DNo effect
What does this code do?<br>
always @(posedge clk) begin
  if (reset) count <= 0;
  else count <= count + 1;
end
ADoes nothing
BCounts down from reset
CAssigns count to reset value always
DResets count on reset, else increments count each clock
Explain how an if-else statement inside an always block controls signal assignments in Verilog.
Think about how hardware decides what value to hold based on conditions.
You got /4 concepts.
    Describe why non-blocking assignments are preferred inside clocked always blocks with if-else statements.
    Consider how hardware registers update all at once on a clock edge.
    You got /4 concepts.