Fix a Gray-pointer checker that never checks the binary step.
Hardware context: Gray-pointer asynchronous FIFO with a declared reset and synchronization model. Check the logical one-bit Gray transition for one source-domain pointer increment.
A FIFO pointer moves in two forms at once: a binary count and its Gray copy. A checker watches each update on the side that owns the pointer. It must catch a bad code, a bad step and a Gray change of the wrong size.
The function gray_transition gets the old and new binary values, the old and new Gray values, the width and an increment bit. increment is 1 when this edge accepted an operation, so the pointer must step by one. When it is 0, both forms must hold.
It returns four error bits, and each check works on its own. In one test a pointer jumped by two counts, and no step error came out. The tests try holds, steps, wraps and corrupted codes at widths 2 to 6.
Find the bug and fix it.
Signals in the Timeline
| Signal | Width | Role |
|---|---|---|
old_binary | 8 | input of each check |
new_binary | 8 | input of each check |
old_gray | 8 | input of each check |
new_gray | 8 | input of each check |
pointer_bits | 4 | input of each check |
increment | 1 | input of each check |
Full specification
function automatic logic [79:0] gray_transition(input logic [7:0] old_binary, new_binary, old_gray, new_gray,
input logic [3:0] pointer_bits, input logic increment);
| Bit | Error | Set when |
|---|---|---|
| 0 | old_code_error | old_gray is not the Gray code of old_binary. |
| 1 | new_code_error | new_gray is not the Gray code of new_binary. |
| 2 | step_error | new_binary is not old_binary + increment, modulo 2^pointer_bits. |
| 3 | change_count_error | The number of Gray bits that differ is not increment: one for a step, zero for a hold. |
Bits 79:4 are always 0. Compare only the low pointer_bits bits of every value. The Gray code of b is b ^ (b >> 1).
The four checks are independent, so several bits can be set at once. The step wraps: with 3 bits, 7 + 1 is 0.
Use this check only on consecutive updates on the side that owns the pointer. The other side samples the pointer through two flops on its own clock. It may see several counts go by at once, and that is legal.
Examples
Example 1
A Wrap from 7 to 0- Input
old_binary = 7, new_binary = 0, old_gray = 4, new_gray = 0, pointer_bits = 3, increment = 1
- Output
change_count_error = no, step_error = no, new_code_error = no, old_code_error = no
Both codes are right, and 7 + 1 wraps to 0 in 3 bits. Gray 100 to 000 changes one bit. No error bit is set.
Example 2
A Hold- Input
old_binary = 2, new_binary = 2, old_gray = 3, new_gray = 3, pointer_bits = 3, increment = 0
- Output
change_count_error = no, step_error = no, new_code_error = no, old_code_error = no
No operation was accepted, so nothing may change. Both forms hold, so no error bit is set.
Example 3
Two Corrupted Gray Bits- Input
old_binary = 3, new_binary = 4, old_gray = 2, new_gray = 5, pointer_bits = 3, increment = 1
- Output
change_count_error = yes, step_error = no, new_code_error = yes, old_code_error = no
The step from 3 to 4 is right. The code of 4 is 110, not 101, so new_code_error is set. Gray 010 to 101 changes three bits, so change_count_error is set too.
Constraints and Contract
- pointer_bits is 2 to 6. The caller keeps all four values within that width.
- increment is the accepted operation, not the request.
- Keep the function name, the argument names and their widths.
Hints
Hint 1
List what can go wrong in one update. A code can be bad, the step can be wrong, or too many Gray bits can change. Each needs its own check.
Hint 2
Try a jump by two at 3 bits: old_binary = 3, new_binary = 5, old_gray = 2, new_gray = 7, increment = 1. Bits 2 and 3 should be set. Which one is missing?
Hint 3
Look at the line for errors[2]. It only checks that the pointer moved. Set it when new_binary differs from old_binary + increment, both masked to pointer_bits.
What the grader checks: The tests try every value with holds, steps and corrupted codes at widths 2 to 6, plus pointers that move by two. They check logic only, not wire skew or how the other side samples the pointer.
755 checks on Icarus · browser samples / server checks. Run the examples first, then check the full regression. Full checks use the server judge. Downloads contain public samples only. A passing result establishes the tested cases, not a proof for all possible inputs.
See all 8 steps and the final capstone