Skip to content
๐Ÿ” `always` and `end`

๐Ÿ” `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;
end

Behavior

  • 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 logic
  • always @(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;
end

Behavior

  • Paired with begin to define multi-statement blocks.
  • Also used to close if, case, for, and other control structures.

โœ… Characteristics

  • Structural: defines block boundaries.
  • Required when using begin for multiple statements.
  • Optional for single-line control flow.

๐Ÿงช Comparison Table

KeywordRoleRequired Whenโ€ฆ
alwaysStarts reactive logic blockYou want event-driven behavior
beginOpens multi-statement blockYou have more than one statement
endCloses the blockAlways paired with begin

๐Ÿงญ Is always Like if-else?

Misconception always is not a conditionalโ€”itโ€™s a reactive scope.

โœ… Clarification

  • You write if-else inside an always block.
  • The always block 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, or b changes.
  • The if-else is the decision logic; always is 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