G6.5.3 Lookahead Enables

MediumImplementIcarus · browser samples / server checksReview the topic

Chain four 4-bit counter stages into a 16-bit counter whose enables are fast enough for a short clock period.

Hardware context: A kitchen timer's BCD seconds counter that counts up or down with a carry, and a display line counter with clear, load and enable that loses a line every frame. Chain four counter stages with lookahead enables that fit a short clock period.

A frame timer needs a 16-bit counter built from four 4-bit counter stages. The clock is fast: there are only 3 ns between a stage's last output settling and the next edge. An AND gate takes 2 ns.

This is an Implement problem. Complete cnt16 from the read-only cells. The starter places stage 0; add the other stages and enables that settle in time. Use only cell instances and wire declarations.

Signals in the Timeline

SignalWidthRole
start16input of each check
Full specification
The module (keep the name and the ports)
module cnt16 (
  input  logic        clk,
  input  logic        clr,    // asynchronous clear: q = 0000 at once
  input  logic        load,   // 1: q = d at the rising edge
  input  logic        en,     // 1 (and load = 0): count up by one at the rising edge
  input  logic [15:0] d,
  output wire  [15:0] q,
  output wire         tc      // 1 while en = 1 and q = ffff: the carry to a next counter
);
  // your cells
endmodule
cells.sv: the cell library (read-only)
// cells.sv: the cell library (read-only). Ports are (outputs, inputs...).
// Each cell adds 1 to the bench's cell counter when the simulation starts. AND gates take 2 ns.
// CNT4: one 4-bit counter stage. At a rising clk edge: load = 1 loads d; otherwise en = 1 counts up by one (1111 wraps
// to 0000). q changes 1 ns after the edge; clr clears it at once. last = 1 while q = 1111, 2 ns after q changes.
// It also tells the bench if its clock edge did not come straight from the bench's clk.
module CNT4 (output reg [3:0] q, output wire last, input wire [3:0] d, input wire load, input wire en, input wire clk, input wire clr);
  initial tb.cells++;
  assign #2 last = &q;
  always @(posedge clk or posedge clr)
    if (clr) q <= #1 4'd0;
    else begin
      if ($time != tb.t_clk) tb.bad_clock++;
      if (load)    q <= #1 d;
      else if (en) q <= #1 q + 4'd1;
    end
endmodule
module AND2 (output wire y, input wire a, b);             and #2 (y, a, b);          initial tb.cells++; endmodule
module AND3 (output wire y, input wire a, b, c);          and #2 (y, a, b, c);       initial tb.cells++; endmodule
module AND4 (output wire y, input wire a, b, c, d);       and #2 (y, a, b, c, d);    initial tb.cells++; endmodule
module AND5 (output wire y, input wire a, b, c, d, e);    and #2 (y, a, b, c, d, e); initial tb.cells++; endmodule

At a rising edge of clkq becomes
load = 1d
load = 0, en = 1q + 1 (ffff wraps to 0000)
load = 0, en = 0q

tc = 1 while en = 1 and q = ffff. After each edge, a stage's last output settles 3 ns later, and en may change at that moment too. The next edge comes 3 ns after that, so each enable and tc may pass through one AND gate only.

Each case clears, loads a start value, then runs four edges. The result shows q after each edge and tc before each edge. It also flags a clock that was not clk and a cell count outside 5 to 12.

Examples

Example 1

Across 000f to 0010
Input
start 000e, en per edge: 1111
Output
q_after_edge1 = 0xf, q_after_edge2 = 0x10, q_after_edge3 = 0x11, q_after_edge4 = 0x12, tc_before_each_edge = 'b0000, a_clock_was_not_clk = no, cell_count_out_of_range = no

From 000e: 000f, 0010, 0011, 0012. Here only stage 1 has to count, once.

Example 2

Across 00ff to 0100
Input
start 00fe, en per edge: 1111
Output
q_after_edge1 = 0xff, q_after_edge2 = 0x100, q_after_edge3 = 0x101, q_after_edge4 = 0x102, tc_before_each_edge = 'b0000, a_clock_was_not_clk = no, cell_count_out_of_range = no

At 00ff stage 2 must count too. Its enable must be ready 3 ns after stage 1's last output settles.

Example 3

Across 0fff to 1000
Input
start 0ffe, en per edge: 1111
Output
q_after_edge1 = 0xfff, q_after_edge2 = 0x1000, q_after_edge3 = 0x1001, q_after_edge4 = 0x1002, tc_before_each_edge = 'b0000, a_clock_was_not_clk = no, cell_count_out_of_range = no

At 0fff all four stages change at one edge. Stage 3's enable depends on three last outputs and en.

Constraints and Contract

  • load, en and d change only while clk is low.
  • Use from 5 to 12 cells. Keep the module name and the ports.

Hints

Hint 1

Stage k must count when en = 1 and every stage below it is at 1111, about to wrap.

Hint 2

Rippling the enable through the stages below adds one AND delay per stage. How many delays fit before the edge?

Hint 3

Give each stage, and tc, one wide AND of en and the last outputs it needs. The cell library has AND gates with up to five inputs.

What the grader checks: Ten directed and ten seeded starts near every carry boundary, each with four edges. q after each edge and tc before each edge are compared with a JavaScript model. Each CNT4 reports a clock edge that did not come at the bench's clk edge. Structural rule: cell instances and wires only, 5 to 12 cells.

20 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 3 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

Across 000f to 0010

Input
start 000e, en per edge: 1111
Expected output
q_after_edge1 = 0xf, q_after_edge2 = 0x10, q_after_edge3 = 0x11, q_after_edge4 = 0x12, tc_before_each_edge = 'b0000, a_clock_was_not_clk = no, cell_count_out_of_range = no

From 000e: 000f, 0010, 0011, 0012. Here only stage 1 has to count, once.

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

Icarus · browser samples / server checks