← Design Verification Interviews
Interview Question · Concept
Putting an Integer in an Enum
The Question
The interviewer asks
You read an opcode from a register as an int and need it in an op_t enum variable. How do you assign it, and what can go wrong?
You have two and a half minutes. Speak or type, then press Get Feedback under your answer. No credits needed.
Speech-to-text runs in your browser. Chrome and Edge may send audio to their own speech service.
You can edit your answer before you send it. Feedback uses AI and checks your answer only against our written answer. No credits are used and your answer is not saved.
Reveal the Model Answer
A plain assignment from int to an enum is a compile error, so you must cast. A static cast op_t'(raw) is unchecked: it stores whatever value arrives, even one with no name. $cast(op, raw) checks that the value is a member, returns 0 if not and leaves op unchanged. Use $cast with its return value tested whenever the value comes from the DUT.
op_t (IDLE, READ, WRITE). Arrows show values moving; each token is the code being converted.- Static cast of 3
The static cast copies 3 into
opwithout looking at it.opnow holds a value with no label, andop.name()returns an empty string.Follow the calls, values and waits
- data 3: raw → Static cast op_t'(raw)
- data 3: Static cast op_t'(raw) → op
- Checked conversion of 2
The checked conversion compares 2 with the labels first. WRITE has code 2, so the value is accepted and
opbecomes WRITE.Follow the calls, values and waits
- data 2: raw → Checked conversion
- data WRITE: Checked conversion → op
- Checked conversion of 3
Code 3 matches no label, so it is rejected.
opkeeps its old value, WRITE, and the checker can report the reserved code.Follow the calls, values and waits
- data 3: raw → Checked conversion
- data 3: Checked conversion → Rejected
- Unknown bits
2'bx1matches no label either, so it is rejected as well. With anintbase type, a static cast would have turned the same bits into 1, a legal READ.Follow the calls, values and waits
- data x1: raw → Checked conversion
- data x1: Checked conversion → Rejected
Read the diagram as text
- raw: logic [1:0] from the bus
- Static cast op_t'(raw): copies the bits, checks nothing
- Checked conversion: $cast, or a case over the labels
- op: op_t: IDLE, READ, WRITE
- Rejected: reported; op keeps its value
- raw → Static cast op_t'(raw)
- raw → Checked conversion
- Static cast op_t'(raw) → op: any code
- Checked conversion → op: code with a label
- Checked conversion → Rejected: no label
Only the checked path stops a code that has no label.
The value 3 has no name in op_t. A static cast and $cast side by side.
static cast: op=3 name="" $cast refused 3, op2 is still READSelf-Check
Tick each key point you covered in your answer.
What Interviewers Listen For (Fresher)
A good fresher answer knows that a direct assignment from int does not compile. It names both kinds of cast: the static cast and $cast.
What Interviewers Listen For (Junior)
A good junior answer tests the return value of $cast. It also says what happens to the destination variable when the cast fails.
What Interviewers Listen For (Senior)
A good senior answer treats an illegal opcode from the DUT as a checker event, not just a cast error. It decodes through $cast, counts the failures and covers the illegal-value path.
Cross-Questions
Where the interviewer goes next. Open a node to practise that follow-up.
WhyWhat IfProve ItBreak ItCompare
- Putting an Integer in an Enum
-
Break ItAn Enum Whose Default Is Not a MemberL1 JuniorL2 Mid-Level
What does this print? The enum's members are 1, 2 and 4.
systemverilog module tb; typedef enum int {START = 1, RUN = 2, STOP = 4} st_t; st_t s; // never assigned initial $display("default s=%0d name=\"%s\"", s, s.name()); endmodule2:30You have two and a half minutes. Speak or type, then press Get Feedback under your answer. No credits needed.
Speech-to-text runs in your browser. Chrome and Edge may send audio to their own speech service.
You can edit your answer before you send it. Feedback uses AI and checks your answer only against our written answer. No credits are used and your answer is not saved.
Reveal the Model Answer
It prints
default s=0 name="". An unassigned enum variable takes the default of its base type, here 0 forint, which is not one of the members. A decoder that expects a member then meets a value with no name. Give the variable an explicit initial member, or reset it.RUNVerilator · simulator serviceExpected:default s=0 name=""Self-Check
Tick each key point you covered in your answer.
Misconception
"An enum variable starts at its first member."
It starts at the base type's default: 0 for two-state, x for four-state. That equals the first member only when the first member is 0.
Source IEEE 1800-2017 §6.19
-
ComparePacked Structs, Packed Unions and Tagged UnionsL1 JuniorL2 Mid-Level
Compare a packed struct, a packed union and a tagged union. The bus word is either a read command or a write command.
2:30You have two and a half minutes. Speak or type, then press Get Feedback under your answer. No credits needed.
Speech-to-text runs in your browser. Chrome and Edge may send audio to their own speech service.
You can edit your answer before you send it. Feedback uses AI and checks your answer only against our written answer. No credits are used and your answer is not saved.
Reveal the Model Answer
A packed struct lays every field side by side, so it suits a header whose fields are all present. A packed union gives several views of the same bits. You can write through the read view and read through the write view, with no check. A tagged union also stores which member is valid. Reading the wrong member is an error, so it suits a command that is one kind or the other.
Two Names for the Same 16 BitsA packed union hdr_ugives two names to one set of 16 bits. Arrows connect each member to those bits; tokens are values written or read.Illustrative sequenceCallValueReturnWaiting- Write the wire view
u.raw = 16'h5A10stores 16 bits. A packed union has only this one set of bits, and every member is a name for it.Follow the calls, values and waits
- data 5A10: u.raw → Shared bits
- Read the field view
u.f.kindreads the top 4 bits, 5, andu.f.addrreads the low 12 bits,A10. No slicing code was needed.Follow the calls, values and waits
- data 5: Shared bits → u.f.kind
- data A10: Shared bits → u.f.addr
- Write one field
u.f.addr = 12'hFFFoverwrites bits 11 to 0 only. The top nibble, whichu.f.kindreads, still holds 5.Follow the calls, values and waits
- data FFF: u.f.addr → Shared bits
- Read the wire view again
u.rawnow reads16'h5FFF. Writing one member changed what the other member reads, and nothing records which member was written last.Follow the calls, values and waits
- data 5FFF: Shared bits → u.raw
Read the diagram as text
- u.raw: logic [15:0], the wire view
- Shared bits: one 16-bit vector
- u.f.kind: bits 15:12
- u.f.addr: bits 11:0
- u.raw → Shared bits: write or read
- Shared bits → u.f.kind: bits 15:12
- Shared bits → u.f.addr: bits 11:0
Each member is a different view of the same bits, so keep union reads close to the code that captured the bus.
Self-Check
Tick each key point you covered in your answer.
Half-Truth
"A union saves memory, so use it for every variant packet."
A packed union gives no protection: reading the wrong member just reinterprets the bits. If the code must know the kind, a tagged union or a class hierarchy says so explicitly.
Source IEEE 1800-2017 §7.3.2
- Write the wire view
-
WhyWhy Testbenches Use Enums for States and OpcodesL0 Fresher
Why bother with an enum instead of
localparamconstants for opcodes?2:30You have two and a half minutes. Speak or type, then press Get Feedback under your answer. No credits needed.
Speech-to-text runs in your browser. Chrome and Edge may send audio to their own speech service.
You can edit your answer before you send it. Feedback uses AI and checks your answer only against our written answer. No credits are used and your answer is not saved.
Reveal the Model Answer
An enum is a type: assignments from unrelated integers need a cast, so a mixed-up value fails to compile. Its values carry names, so
$displaywith%sandname(), and most waveform viewers, showWRITEinstead of 2. Iteration methods such asfirst()andnext()also let a test walk every legal value.Self-Check
Tick each key point you covered in your answer.
Half-Truth
"Enums are only for readability."
The cast rule is a type check that catches real bugs, and the methods let tests enumerate the legal values.
Source IEEE 1800-2017 §6.19.5
-
What IfWalking Every Member of an EnumL0 FresherL1 Junior
What if a test must try every opcode? How do you loop over the enum?
2:30You have two and a half minutes. Speak or type, then press Get Feedback under your answer. No credits needed.
Speech-to-text runs in your browser. Chrome and Edge may send audio to their own speech service.
You can edit your answer before you send it. Feedback uses AI and checks your answer only against our written answer. No credits are used and your answer is not saved.
Reveal the Model Answer
Start at
first(), step withnext(), and stop afterlast(). This covers every member, even after someone adds a new opcode. A loop over the integers 0 to N would hit gaps or miss new values.num()gives the member count, which a check can use.Self-Check
Tick each key point you covered in your answer.
Misconception
"Loop until
next()returns an invalid value."next()wraps from the last member back to the first. The loop never sees an invalid value and never ends.Source IEEE 1800-2017 §6.19.5
-
Prove ItSolve 1.4.2: State-Transition ValidatorSimulator-graded
Show it, don't just say it: this Silicon Lab problem is graded by the simulator.
What People Get Wrong
Common wrong answers and why they fail. Run It shows the wrong belief failing on a real simulator.
Misconception
"A static cast to an enum checks the value."
Only $cast checks. The twin static-casts 3 into a type whose members are 0, 1 and 2 and gets a value whose name() is empty.
Run It
A static cast of 3 into an enum with members 0, 1 and 2.
FAIL: the static cast stored 3, a value with no enum nameSee the Fix
module tb;
typedef enum logic [1:0] {IDLE, READ, WRITE} op_t;
op_t op = IDLE;
int raw = 3;
initial begin
if (!$cast(op, raw)) $display("PASS: $cast rejected 3 and op stayed %s", op.name());
end
endmodule
Prints: PASS: $cast rejected 3 and op stayed IDLE
Misconception
"A failed $cast sets the variable to its first member."
The destination keeps its previous value, which is why stale values survive when the return value is ignored.
Source IEEE 1800-2017 §6.24.2
Tool as Truth
"Icarus would not run $cast, so it must not be standard."
$cast is standard; Icarus 12 does not implement it. The course runs this on Verilator for that reason.
Source Lesson 1.4 simulator note (Icarus Verilog 12 lacks $cast)