7. Up/down counter with load
Build a 4-bit counter that can count up or down and can be loaded with a value.
On each rising clock edge, in this order of priority: 1. rst = 1: q becomes 0. 2. load = 1: q becomes d (whatever en is). 3. en = 1: q goes up by 1 when up = 1, or down by 1 when up = 0. It wraps: 15 + 1 is 0 and 0 - 1 is 15. 4. Otherwise q keeps its value.
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. |
up | in | 1 | 1 counts up, 0 counts down. |
load | in | 1 | Load d into q (beats en). |
d | in | 4 | Value to load. |
q | out | 4 | The count. |
Example
After reset q = 0. With en = 1 and up = 0, the next edge gives q = 15. With load = 1 and d = 9, the next edge gives q = 9 even if en is 1.
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.