Practice
Ferry Chain Count
A chain ferry crosses the river at Aldermere by pulling itself along a 140 m chain. The new dock controller needs to know where the ferry is and which way it is moving. The chain turns a pulley with a 2-bit position sensor called a quadrature encoder, which sends signals on channels a and b. Each step of the chain flips exactly 1 of the 2 bits. The order of the flips tells you the direction.
The commissioning log shows two problems. First, at the docks, waves from passing boats rock the ferry, and the pulley jitters: 1 step forward, 1 step back. The old decoder reported a direction change on every jitter step. Each report re-arms the boarding barrier, so the barrier lights blinked all afternoon. Second, a greasy sensor sometimes misses one change, and then the next sample shows both bits changed at once. After such a jump, the sample cannot show whether the chain moved 2 steps forward or back, so the position is unknown and the count must not guess.
Your task: build the decoder. It keeps a wrapping position count on pos and a direction output dir that dock jitter cannot flip. It also raises a sticky fault flag fault (it stays set until reset) for the both-bits jumps.
Interface
| Port | Direction | Type | Description |
|---|---|---|---|
clk | in | 1 bit | Clock |
rst | in | 1 bit | Synchronous reset (active high) |
a | in | 1 bit | Encoder channel A, synchronous |
b | in | 1 bit | Encoder channel B, synchronous |
pos | out | 8-bit vector | Position count, wraps mod 256, registered |
dir | out | 1 bit | 1 toward the far dock, 0 toward the near dock, registered |
fault | out | 1 bit | Sticky double-transition flag, cleared by reset |
Behavior
- The pair
(a, b)is sampled at every rising edge and compared with the previous sample - No change:
pos,dirandfaultall hold - Exactly one bit changed: a step. Forward order 00, 01, 11, 10, 00 adds 1 to
pos, and the reverse order subtracts 1.poswraps mod 256 in both directions dirupdates only when a step goes the same way as the step before it- A single step against the current run (the dock jitter) moves
posbut leavesdiralone - Both bits changed at once: no count change, no direction change;
faultgoes high and stays high until reset - After a double change, decoding re-anchors on the new pair; later valid steps keep counting while the flag stays up
rst = 1at a rising edge:pos = 0,dir = 1,fault = 0, and the previous-step memory is set to forward- Reset also latches the current line pair as the new baseline, so an unmoving chain produces no phantom step on release
What the bench checks
- The testbench checks
pos,dirandfaulton every cycle - Forward and reverse runs cover steady parking and position wrap in both directions
- Direction must change only after two consecutive contrary steps, while alternating jitter must leave it unchanged
- Both illegal double transitions must set the sticky fault without changing position, and valid counting must continue afterward
- Reset on nonzero encoder lines must clear every output and re-anchor there without a phantom step
Constraints
everything is synchronous to clk; all three outputs are registers.
this models the step decoder alone; the lines arrive clean and synchronous. The pulley, the chain, the barrier, and the sensor's electrical side are out of scope.
The encoder lines are already clean and synchronous. There is nothing to debounce (the lines never glitch). Do not add ports.
Click Run to execute your code. Output will appear here.