15Implementation
Bump a counter
Problem
Counter.bump is an Action method: one accepted call must schedule exactly one
8-bit state transition and return no value. The starter leaves that transition
as a don't-care expression. With the supplied toolchain, the public run ends at
decimal 170 rather than the required value 3; that number is an arbitrary
choice for ?, not a usable counter implementation.
Edit only the TODO inside mkCounter. Preserve the interface, the value
method, and the supplied top module. The repair should express the next-state
relation for any current count, not special-case the three calls made by
mkTop.
Read, predict, and repair
Read starter/Main.bsv in this order:
- Classify
bumpandvaluefrom their interface result types. - Locate the
countregister and its initial value. - Follow
drive: it callsbumponce and advancesstepsonce per firing. - Follow
show: it reads the state only afterdrivehas fired three times. - Return to the
TODOand state the required old-state-to-next-state relation in words before writing BSV.
The worked trace should be count 0 at reset, then 1, 2, and 3 at successive
cycle boundaries. <= does not change the register immediately inside the
method; it schedules the value visible in the next cycle. The minimal repair
therefore supplies one deterministic next-state expression and adds no new
register, rule, guard, or return value.
Run after editing. Completion is observable when the public run reports decimal
3, the method remains Action, and each call accounts for exactly one
transition. An implementation that merely forces the final sample to three is
incorrect because one call and two calls must expose the corresponding earlier
states.
Progressive hints
Hint 1 — interpret the method kind
An Action method is called as a statement. Its useful result here is the scheduled register write, not a value returned to the caller.
Hint 2 — interpret the placeholder
? permits an arbitrary bit pattern. Replace it with a next value derived from
the register's current value; do not derive it from steps, which belongs to
the test-driving top module.
Hint 3 — express one transition
The contract is one modular 8-bit increment per call. Use the existing
register and the ordinary arithmetic operation that states that relation on
the right-hand side of <=.
Transfer experiment
After the required version works, initialize count to 8'hfe and reason
through two bump calls. Predict both intermediate states, including the
8-bit wraparound, before running. This separates the general next-state rule
from an implementation fitted only to reset value zero.