11. Gray code counter
Build a 4-bit counter whose output counts in Gray code: 0000, 0001, 0011, 0010, 0110, 0111, 0101, 0100, 1100 and so on, wrapping after 1000 (the code for 15) back to 0000.
gray must come straight from flip-flops (no logic between the registers and the output), so that it never shows a glitch while it changes.
On each rising clock edge: rst = 1 sets the count to 0 (gray = 0000); otherwise, when en = 1, the count advances by one; when en = 0 it holds. rst is synchronous and active high.
Ports
| Port | Dir | Bits | What it is |
|---|---|---|---|
clk | in | 1 | Clock. |
rst | in | 1 | Synchronous reset, active high. |
en | in | 1 | Count enable. |
gray | out | 4 | The count in Gray code, registered. |
Example
After reset gray = 0000. Four enabled edges later gray = 0110 (the code for 4). Exactly one bit changes on every step.
Written hints
AI hint
Submissions
No submissions yet. Submit your answer and it shows here. Sign in to keep them.
Submit your answer to have it graded. The hidden test runs on the server; you see whether it passes and, if not, the first case it gets wrong.
Press Build to check your code with Yosys. Errors and warnings show here, and a clean build draws the logic diagram.
Build your design to drive it by hand and see its signals here.