13. Divide by N
Divide the clock by a number n given on an input (n is 2 to 15; smaller values are not tested). Do not create a new clock: produce two signals that stay in the clk domain.
Keep a counter that runs 0, 1, ..., n - 1 and then returns to 0, so one period is n clock cycles.
- tick is 1 during the last cycle of each period (when the counter is n - 1). It is a clock enable for logic that should run once every n cycles.
- sq is a square wave: 1 for the first ceil(n / 2) cycles of each period and 0 for the rest. For even n it has a 50% duty cycle; for odd n it is high one cycle longer than it is low.
If n is lowered while the counter is already at or above the new n - 1, the counter returns to 0 on the next edge. rst (synchronous, active high) sets the counter to 0.
A note on odd n: a true 50% duty cycle for odd n needs logic on the falling clock edge as well. This playground samples outputs once per full clock cycle, so it cannot show half-cycle timing; the problem asks for the single-edge version.
Ports
| Port | Dir | Bits | What it is |
|---|---|---|---|
clk | in | 1 | Clock. |
rst | in | 1 | Synchronous reset, active high. |
n | in | 4 | Divisor, 2 to 15. |
tick | out | 1 | 1 in the last cycle of each period. |
sq | out | 1 | Square wave, high for the first ceil(n / 2) cycles. |
Example
With n = 5, after reset the counter reads 0, 1, 2, 3, 4, 0, ... ; sq is 1, 1, 1, 0, 0 and tick is 0, 0, 0, 0, 1 in those cycles.
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.