18Implementation
Add the yellow phase
Problem
Intended behavior. TrafficLight cycles red, green, yellow, and back to
red. It moves one step on each clock edge where tick is high and holds its
state otherwise. Reset starts it in red. The light output carries the state
as a Light value, numbered in declaration order: red 0, green 1, yellow 2.
stop is 1 in red and in yellow, and 0 only in green.
Current behavior. The starter declares only two states, so green returns
straight to red, stop is tied to red alone, and Light is one bit wide.
Task. Add the yellow state and route the machine through it. Keep the
TrafficLight name and its ports.
Read the starter
Start with object Light. Count its values and work out the width Chisel
chooses for Light(). Then read the switch block: each is branch says
where one state goes on a tick. Last, read how stop is computed from
state.
Predict before editing
The public run prints the encoding line and then eight cycles with tick high
on even cycles. For the finished design, predict:
- the width on the
ENCODINGline, and which numbers the three names receive; - the state in every cycle from 0 to 7. Remember that a tick seen in one cycle shows up as the new state in the next;
- the cycles in which
stopis 0.
Run the starter first and note where its trace leaves green too early.
Make the change
Three places describe the yellow phase: the enum declaration, the transitions
in switch, and the stop condition. Change each so that the code states
the intended behavior directly. Compare states only with Light values,
never with raw numbers.
Run and explain
Run the finished design and compare every line with your prediction. Explain
why yellow first appears one line after the tick that ends green, and why the
width on the ENCODING line changed without any edit to the port
declarations.
Completion check
Lighthas three values numbered 0, 1, 2 in the order red, green, yellow, and is two bits wide.- Every state holds while
tickis low, for any number of cycles. - Each tick advances exactly one step: red to green, green to yellow, yellow to red.
stopfollows the state in every cycle, not only in the printed ones.- Reset returns the machine to red from any state.
Progressive hints
Hint 1
A new state is a new Value in the enum. Its number comes from its position
in the declaration, so the order in which you list the names matters.
Hint 2
Green must no longer go to red. Yellow needs its own is branch; without one,
no assignment happens in yellow and the register would hold there forever.
Hint 3
isOneOf tests a value against several enum values at once and returns a
Bool.
Concept
A ChiselEnum gives each state a name, a number, and a type. Adding a value
can change the width of every port and register of that type, which Chisel
recalculates for you. Because comparisons accept only values of the same enum,
the transitions and stop keep meaning the same thing even when the numbering
changes.
Extension experiment
Imagine a fourth state, flashing yellow, declared last. Predict the width of
Light and whether the numbers of the existing three states change. Then
imagine declaring it first instead, and say which printed numbers would move.