G6.6.1 Divide by Five, Fifty Percent Duty

HardImplementIcarus Verilog + Yosys · serverReview the topic

Build a clock divider for ratios 2 to 15, 5 among them, whose output is high for exactly half its period.

Hardware context: A codec bit clock made by dividing the system clock by a ratio from 2 to 15, divide by 5 among them. Build a programmable clock divider that keeps a 50% duty cycle for odd ratios.

A codec needs a bit clock that is the system clock divided by 5, with a 50% duty cycle. The next board revision will need other ratios, so the divider takes its ratio as an input, from 2 to 15.

This is an Implement problem. Complete divn. The check sets div, resets, then times every high and low level of clk_out for 90 clock periods. The starter divides by div, but its output is high for only one period.

Signals in the Timeline

SignalWidthRole
div4input of each check
Full specification
The module (keep the name and the ports)
module divn (
  input  logic       clk,
  input  logic       rst,      // synchronous reset, active high
  input  logic [3:0] div,      // 2 to 15; changes only while rst = 1
  output logic       clk_out   // clk / div, 50% duty
);
  ...
endmodule
Property of clk_outRequired, with a 10 ns clock
Perioddiv clock periods: 10 × div ns
High time and low timeexactly half the period each: 5 × div ns, also for odd div
Phaseit rises at the first rising clk edge after rst falls, and every div periods after that

The result shows when the first rise came, in ns after that clk edge. It also shows the shortest and longest levels and the rising edges.

Examples

Example 1

Divide by 5
Input
div = 5
Output
first_rise_ns_after_the_clk_edge = 0, shortest_high_ns = 25, longest_high_ns = 25, shortest_low_ns = 25, longest_low_ns = 25, rising_edges = 18

The period is 50 ns, so clk_out must be high for 25 ns and low for 25 ns, every time. 25 ns is two and a half clock periods.

Example 2

Divide by 4
Input
div = 4
Output
first_rise_ns_after_the_clk_edge = 0, shortest_high_ns = 20, longest_high_ns = 20, shortest_low_ns = 20, longest_low_ns = 20, rising_edges = 23

Even: high for two periods (20 ns) and low for two. No half period is needed.

Example 3

Divide by 3
Input
div = 3
Output
first_rise_ns_after_the_clk_edge = 0, shortest_high_ns = 15, longest_high_ns = 15, shortest_low_ns = 15, longest_low_ns = 15, rising_edges = 30

High for one and a half periods (15 ns) and low for 15 ns. The first rise is at the first clk edge after the reset.

Constraints and Contract

  • div is 2 to 15 and changes only while rst = 1. rst changes only while clk is low.
  • Keep the module name and the ports. The design must synthesize with no latches.

Hints

Hint 1

For even div, a signal that is high for div / 2 periods out of div is already 50%.

Hint 2

For odd div, rising edges alone give a whole number of periods. A flop on the falling edge can delay a signal by exactly half a period.

Hint 3

OR the rising-edge pulse with its own half-period-delayed copy, and only for odd div. Let the pulse rise on the first counted edge.

What the grader checks: All fourteen settings of div, 2 to 15. For each, 90 periods are timed from the first rising clk edge after the reset. The first rise, the level lengths and the rising edges must match exactly.

The submitted design must synthesize with 0 latches and no combinational loop. Counts come from the pinned open-source synthesis flow. They are proxies for logic size and depth, not physical area or timing.

14 checks on Icarus Verilog + Yosys · server. 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 1 steps and the final capstone

Report a problem with this page

Your code SystemVerilogSaved locally
Ln 1, Col 1Tab indents · Ctrl/⌘ + Enter runs samples
Not run yet

Divide by 5

Input
div = 5
Expected output
first_rise_ns_after_the_clk_edge = 0, shortest_high_ns = 25, longest_high_ns = 25, shortest_low_ns = 25, longest_low_ns = 25, rising_edges = 18

The period is 50 ns, so clk_out must be high for 25 ns and low for 25 ns, every time. 25 ns is two and a half clock periods.

Run samples executes all 3 public checks. Check all tests runs 14 checks on the server.

Icarus Verilog + Yosys · server