๐ `always` and `end`
Understanding the role of always and end in Verilog is essential for modeling reactive logic and building audit-ready hardware systems. This note scaffolds their purpose, behavior, and relationship to control flow.
๐ always Block
Definition: A procedural block that executes when triggered by a sensitivity list.
always @(*) begin
y = a & b;
endBehavior
- Runs whenever any signal in the sensitivity list changes.
- Can describe combinational or sequential logic depending on the trigger.
- Acts like a reactive container, not a conditional.
โ Characteristics
- Event-driven: triggered by changes or clock edges.
- Encapsulates logic: can include
if,case, loops, assignments. - Used for: FSMs, counters, decoders, ALUs.
๐ Notes
always @(*)โ combinational logicalways @(posedge clk)โ sequential logic- Not a decisionโitโs a scope for reactive behavior
๐ end Keyword
Definition: Closes a begin block or control structure.
always @(*) begin
if (sel)
y = a;
else
y = b;
endBehavior
- Paired with
beginto define multi-statement blocks. - Also used to close
if,case,for, and other control structures.
โ Characteristics
- Structural: defines block boundaries.
- Required when using
beginfor multiple statements. - Optional for single-line control flow.
๐งช Comparison Table
| Keyword | Role | Required Whenโฆ |
|---|---|---|
always | Starts reactive logic block | You want event-driven behavior |
begin | Opens multi-statement block | You have more than one statement |
end | Closes the block | Always paired with begin |
๐งญ Is always Like if-else?
Misconception
always is not a conditionalโitโs a reactive scope.โ Clarification
- You write
if-elseinside analwaysblock. - The
alwaysblock itself is not a decision, itโs a triggered container.
๐งช Example
always @(*) begin
if (sel)
y = a;
else
y = b;
end- This models a multiplexer.
- The block runs whenever
sel,a, orbchanges. - The
if-elseis the decision logic;alwaysis the reactive wrapper.
๐งฐ Vault Notes
Rule of Thumb
Use always to define when logic should run.
Use if, case, etc. inside to define what logic should run.
โน๏ธ
Combinational Decoder
always @(*) begin
case (in)
2'b00: out = 4'b0001;
2'b01: out = 4'b0010;
2'b10: out = 4'b0100;
2'b11: out = 4'b1000;
endcase
endโน๏ธ
Sequential Counter
always @(posedge clk) begin
if (rst)
count <= 0;
else
count <= count + 1;
endโ ๏ธ
Common Mistake
Donโt confuse
always with if.always defines when logic runsโnot what decision is made.Last updated on