G6.4.1 A Synchronous Counter from T Flip-Flops

MediumImplementIcarus · browser samples / server checksReview the topic

Build a 4-bit synchronous down counter with an enable and a borrow out from T flip-flop cells on one clock.

Hardware context: A countdown timer's 4-bit down counter built from T flip-flop cells, and a sequencer with no reset that steps 0, 3, 5, 6. Build a synchronous down counter with an enable and a borrow out from T flip-flop cells.

A countdown timer needs a 4-bit down counter that only counts while en is 1. Its borrow out must warn the next stage one cycle before the count wraps from 0000 to 1111. Every flip-flop must take the system clock, so no false counts appear.

This is an Implement problem. Build dcount4 from the read-only cells. Use only cell instances and wire declarations. The check also reports any TFF whose clock edge did not come straight from clk.

Signals in the Timeline

SignalWidthRole
script12input of each check
Full specification
The module (keep the name and the ports)
module dcount4 (
  input  logic       clk,
  input  logic       clr,   // asynchronous clear: q = 0000 at once
  input  logic       en,    // 1: count down by one at the rising edge
  output wire  [3:0] q,
  output wire        bo     // borrow out: 1 while en = 1 and q = 0000 (the next edge wraps to 1111)
);
  // your cells
endmodule
cells.sv: the cell library (read-only)
// cells.sv: the cell library (read-only). Ports are (output, inputs...).
// Each cell adds 1 to the bench's cell counter when the simulation starts. Gates take 1 time unit.
// TFF: at a rising clk edge it toggles when t = 1 (1 unit later); clr clears it at once. It also tells the bench if
// its clock edge did not come straight from the bench's clk.
module TFF  (output reg q, input wire t, input wire clk, input wire clr);
  initial tb.cells++;
  always @(posedge clk or posedge clr)
    if (clr) q <= #1 1'b0;
    else begin
      if ($time != tb.t_clk) tb.bad_clock++;
      if (t) q <= #1 ~q;
    end
endmodule
module INV  (output wire y, input wire a);                                 not  #1 (y, a);          initial tb.cells++; endmodule
module AND2 (output wire y, input wire a, b);                              and  #1 (y, a, b);       initial tb.cells++; endmodule
module AND3 (output wire y, input wire a, b, c);                           and  #1 (y, a, b, c);    initial tb.cells++; endmodule
module AND4 (output wire y, input wire a, b, c, d);                        and  #1 (y, a, b, c, d); initial tb.cells++; endmodule
module NOR2 (output wire y, input wire a, b);                              nor  #1 (y, a, b);       initial tb.cells++; endmodule
module NOR3 (output wire y, input wire a, b, c);                           nor  #1 (y, a, b, c);    initial tb.cells++; endmodule
module NOR4 (output wire y, input wire a, b, c, d);                        nor  #1 (y, a, b, c, d); initial tb.cells++; endmodule

At a rising edge of clkq becomes
en = 1q - 1 (0000 wraps to 1111)
en = 0q

bo = 1 while en = 1 and q = 0000. Each case clears the counter, then runs twelve edges. In each step en is set 7 time units before the edge, and bo is read 1 unit before it. So bo must settle within 5 gate delays of en.

The result shows q after each edge and bo before each edge. It also flags a clock that was not clk and a cell count outside 5 to 16.

Examples

Example 1

Counting Down from 0
Input
en per edge: 111111111111
Output
q_after_edge1 = 'b1111, q_after_edge2 = 'b1110, q_after_edge3 = 'b1101, q_after_edge4 = 'b1100, q_after_edge5 = 'b1011, q_after_edge6 = 'b1010, q_after_edge7 = 'b1001, q_after_edge8 = 'b1000, q_after_edge9 = 'b0111, q_after_edge10 = 'b0110, q_after_edge11 = 'b0101, q_after_edge12 = 'b0100, bo_before_each_edge = 'b000000000001, a_clock_was_not_clk = no, cell_count_out_of_range = no

With en at 1, the first edge wraps 0000 to 1111, then the count goes 1110, 1101 and on down. bo is 1 only before the first edge, while q is 0000.

Example 2

Enable off for a While
Input
en per edge: 100110101110
Output
q_after_edge1 = 'b1111, q_after_edge2 = 'b1111, q_after_edge3 = 'b1111, q_after_edge4 = 'b1110, q_after_edge5 = 'b1101, q_after_edge6 = 'b1101, q_after_edge7 = 'b1100, q_after_edge8 = 'b1100, q_after_edge9 = 'b1011, q_after_edge10 = 'b1010, q_after_edge11 = 'b1001, q_after_edge12 = 'b1001, bo_before_each_edge = 'b000000000001, a_clock_was_not_clk = no, cell_count_out_of_range = no

Edges with en = 0 hold the count. bo stays 0 whenever en is 0, even if q is 0000.

Example 3

Holding at 0, Then the Borrow
Input
en per edge: 001000110111
Output
q_after_edge1 = 'b0000, q_after_edge2 = 'b0000, q_after_edge3 = 'b1111, q_after_edge4 = 'b1111, q_after_edge5 = 'b1111, q_after_edge6 = 'b1111, q_after_edge7 = 'b1110, q_after_edge8 = 'b1101, q_after_edge9 = 'b1101, q_after_edge10 = 'b1100, q_after_edge11 = 'b1011, q_after_edge12 = 'b1010, bo_before_each_edge = 'b000000000100, a_clock_was_not_clk = no, cell_count_out_of_range = no

The counter sits at 0000 with en = 0, so bo is 0. bo turns 1 in the cycle where en is 1 and q is still 0000.

Constraints and Contract

  • en changes only while clk is low.
  • Use from 5 to 16 cells. Keep the module name and the ports.

Hints

Hint 1

Count down in binary and watch bit k. It flips when the count borrows from it. When does subtracting 1 borrow from bit k?

Hint 2

Bit k flips on the way down exactly when every bit below it is 0. A NOR of the lower bits is 1 in that case.

Hint 3

Every stage, and bo, must also need en = 1. Give each TFF the clk port itself.

What the grader checks: Three directed and ten seeded scripts of twelve edges. q after each edge and bo before each edge are compared with a JavaScript model. Each TFF reports a clock edge that did not come at the bench's clk edge. Structural rule: cell instances and wires only, 5 to 16 cells.

13 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 2 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

Counting Down from 0

Input
en per edge: 111111111111
Expected output
q_after_edge1 = 'b1111, q_after_edge2 = 'b1110, q_after_edge3 = 'b1101, q_after_edge4 = 'b1100, q_after_edge5 = 'b1011, q_after_edge6 = 'b1010, q_after_edge7 = 'b1001, q_after_edge8 = 'b1000, q_after_edge9 = 'b0111, q_after_edge10 = 'b0110, q_after_edge11 = 'b0101, q_after_edge12 = 'b0100, bo_before_each_edge = 'b000000000001, a_clock_was_not_clk = no, cell_count_out_of_range = no

With en at 1, the first edge wraps 0000 to 1111, then the count goes 1110, 1101 and on down. bo is 1 only before the first edge, while q is 0000.

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

Icarus · browser samples / server checks