Skip to content
DenseChip
Learn
PracticeCode labInterview

DenseChip

Navigate

Menu

LearnPracticeCode labInterview

Tools

All toolsSV editorCode repoResume
DenseChip

Practice, courses, and chip-design tools in one focused workspace.

LearnPracticeCode labInterviewToolsLearning paths

© 2026 DenseChip

AboutRoadmapCareersInternshipsFAQContactPrivacyTerms
Skip to lesson

Course contents

  • How to Use This Puzzle Bank
  • SystemVerilog Language Puzzles25
    • Datatype and Bit Puzzles
    • Fork Join Puzzles
    • Queue and Array Puzzles
    • String and Casting Puzzles
      • String Substr with Runtime Class Cast
      • putc/getc Index Semantics
      • Case-Sensitive vs Case-Insensitive Compare
      • Enum Safety with $cast
      • Upcast, Virtual Dispatch and Downcast
      • String Number Parsing Stops Early
    • SystemVerilog Language Puzzles: Tricky Q&A
  • Difficulty Guide
  • Full Topic Map
  • Interview-Day Tips
  • Study Route: One Week Before the Interview
  • Study Route: Two Weeks
  • Study Route: Four Weeks (Full Course)4
  • OOP & Testbench Puzzles25
  • Concurrency & Timing Puzzles25
  • Randomization & Constraint Puzzles25
  • SVA Assertion Puzzles25
  • Functional Coverage Puzzles25
  • Logic & Aptitude Puzzles25
  • Scenario & Debug Puzzles25
  • UVM Testbench Puzzles25
  • Clock, Reset & CDC Puzzles25
  • Bus and Interface Protocol Puzzles25
  • Interview Prep: Puzzle Rounds7
  • DV Puzzle Cheatsheet
Loading lesson

←String and Casting Puzzles
  1. Topics
  2. DV Interview Puzzles
  3. SystemVerilog Language Puzzles
  4. String and Casting Puzzles
  5. Enum Safety with $cast
On this page

    VLSI DV Interview PuzzlesAll levels

    Enum Safety with $cast

    Predict the cast success bit and the enum value after each attempt.

    Puzzle

    Difficulty: Medium · Puzzle 4 of 6 · Topic: String and Casting Puzzles

    Predict the cast success bit and the enum value after each attempt.

    Code

    p4.sv
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    typedef enum int {IDLE=0, BUSY=1, DONE=2} state_e;
    
    module p4;
      int raw1 = 2;
      int raw2 = 3;
      state_e s = IDLE;
      bit ok;
      initial begin
        ok = $cast(s, raw1);
        $display("c1=%0d s=%s", ok, s.name());
        ok = $cast(s, raw2);
        $display("c2=%0d s=%s", ok, s.name());
      end
    endmodule

    Hint

    `$cast` to enum succeeds only for legal enumerator values. On failure, destination keeps previous value.

    Step-by-step solution

    Diagram
    1) `raw1=2` is the legal enum value DONE, so the first cast returns 1 and `s` becomes DONE.
    2) `raw2=3` is not a legal enumerator, so the second cast returns 0.
    3) A failed cast does not modify the destination, so `s` stays DONE.
    (The cast and the use of `s` are in separate statements on purpose: reading `s` in the same `$display` argument list as the `$cast` that writes it would depend on argument evaluation order, which the LRM does not define.)

    Answer

    Answer: c1=1 s=DONE, then c2=0 s=DONE

    How this was checked

    Checked by running the code in Verilator 5; the printed output supports the answer above. A failed $cast leaves the enum unchanged; Icarus 13 does not implement $cast.

    Why candidates get it wrong

    People assume a failed `$cast` zeroes the destination or sets it to the first enum literal (IDLE), but it leaves DONE untouched.

    Interviewer follow-up

    How would a static cast `state_e'(raw2)` behave differently? Model answer: it performs no legality check and raises no error: s becomes 3, which matches no enumerator, so `s.name()` returns an empty string (run in Verilator; some tools warn).

    Say it out loud

    I would check whether each raw number is a legal enum value. The value 2 is DONE, so the first cast returns 1, and the value 3 is not a legal enumerator, so the second cast returns 0. A failed cast leaves the destination alone, so the state stays DONE. The trap is thinking it resets to the first literal, and I would also keep the cast and the display in separate statements, because the argument evaluation order is not defined.

    Related topics

    • Casting with $cast

    • Enums, Structs & Unions

    • Back to String and Casting Puzzles

    • SystemVerilog Language Puzzles section

    Previous

    32 / 293

    Next: Upcast, Virtual Dispatch and Downcast

    Up next

    Up nextUpcast, Virtual Dispatch and Downcast

    Sign in to save progress

    Practice this lessonPrevious lessonReport an error

    See also

    • String Substr with Runtime Class Cast
    • putc/getc Index Semantics
    • String Number Parsing Stops Early