G5.6.1 A Flip-Flop from Two Latches

MediumImplementIcarus · browser samples / server checksReview the topic

Build a rising-edge D flip-flop from two latches with active-low enables and an inverter: a master and a slave on opposite phases.

Hardware context: A counter in a small cell library that offers latches and inverters but no flip-flop. Build a master-slave D flip-flop from two active-low-enable latch cells and one inverter.

A small cell library has latches but no flip-flop. A counter needs edge-triggered storage. It must keep the value of d at the rising edge of clk for the whole period, whatever d does.

This is an Implement problem. Build dff from the read-only cells: an inverter and a D latch that is open while its enable is 0. Each takes one time unit. The check changes d while clk is high and again while it is low, and reads q three times per period.

Use only cell instances and wire or logic declarations without initial values. Operators, assignments and procedural code are refused before simulation. Use from 3 to 6 cells.

Signals in the Timeline

SignalWidthRole
script18input of each check
Full specification
The module (keep the name and the ports)
module dff (
  input  logic clk,
  input  logic d,
  output wire  q      // d as it was at the rising edge of clk
);
  // your netlist
endmodule
cells.sv: the cell library (read-only)
// cells.sv: the cell library (read-only). Ports are (output, inputs...).
// Each cell takes 1 time unit and adds 1 to the bench's cell counter when the simulation starts.
module INV    (output wire y, input wire a);           not #1 (y, a);                         initial tb.cells++; endmodule
module DLATCHN (output reg q, input wire d, input wire en);  // transparent while en = 0, holds while en = 1
  always @(d or en) if (!en) q <= #1 d;
  initial tb.cells++;
endmodule

Each period (4 time units per phase)q must show
d is set, clk is 0the value of the last rising edge
clk rises; then d changesd as it was at this rising edge, read twice
clk falls; then d changesstill d of this rising edge

Each case runs six periods. The result shows q after the rise, after the change in the high phase and after the fall, for each period. The last field says whether the cell count is out of range.

Examples

Example 1

d Steady Around Every Edge
Input
d at the edge, in the high phase, after the fall: 111 000 111 111 000 000
Output
q_after_rise1 = yes, q_in_high_phase1 = yes, q_after_fall1 = yes, q_after_rise2 = no, q_in_high_phase2 = no, q_after_fall2 = no, q_after_rise3 = yes, q_in_high_phase3 = yes, q_after_fall3 = yes, q_after_rise4 = yes, q_in_high_phase4 = yes, q_after_fall4 = yes, q_after_rise5 = no, q_in_high_phase5 = no, q_after_fall5 = no, q_after_rise6 = no, q_in_high_phase6 = no, q_after_fall6 = no, cell_count_out_of_range = no

d does not move inside a period, so q simply takes it at each rising edge and keeps it.

Example 2

d Changes While the Clock Is High
Input
d at the edge, in the high phase, after the fall: 100 011 101 010 100 011
Output
q_after_rise1 = yes, q_in_high_phase1 = yes, q_after_fall1 = yes, q_after_rise2 = no, q_in_high_phase2 = no, q_after_fall2 = no, q_after_rise3 = yes, q_in_high_phase3 = yes, q_after_fall3 = yes, q_after_rise4 = no, q_in_high_phase4 = no, q_after_fall4 = no, q_after_rise5 = yes, q_in_high_phase5 = yes, q_after_fall5 = yes, q_after_rise6 = no, q_in_high_phase6 = no, q_after_fall6 = no, cell_count_out_of_range = no

In each period d flips just after the rising edge. q must still show the value d had at the edge.

Example 3

d Changes After the Falling Edge
Input
d at the edge, in the high phase, after the fall: 001 110 001 110 110 001
Output
q_after_rise1 = no, q_in_high_phase1 = no, q_after_fall1 = no, q_after_rise2 = yes, q_in_high_phase2 = yes, q_after_fall2 = yes, q_after_rise3 = no, q_in_high_phase3 = no, q_after_fall3 = no, q_after_rise4 = yes, q_in_high_phase4 = yes, q_after_fall4 = yes, q_after_rise5 = yes, q_in_high_phase5 = yes, q_after_fall5 = yes, q_after_rise6 = no, q_in_high_phase6 = no, q_after_fall6 = no, cell_count_out_of_range = no

Now d changes in the low phase, after the fall. That change belongs to the next edge, so q must not move yet.

Constraints and Contract

  • d is a known 0/1 value. Keep the module name and the ports.
  • From 3 to 6 cells.

Hints

Hint 1

A single latch is transparent for a whole phase, so it cannot give edge behaviour alone. Two latches in a row, never open together, can.

Hint 2

Let the first latch (the master) follow d while the clock is low. Let the second (the slave) copy the master while the clock is high. Work out which enable each latch needs: these latches open on 0.

Hint 3

At the rising edge the master shuts and keeps d; the slave opens and shows it. While the clock is high the master is shut, so changes of d cannot reach q.

What the grader checks: Three directed and ten seeded scripts of six clock periods. In each period d changes during the high phase and during the low phase. q is read after the rise, during the high phase and after the fall. Each value is compared with d at the rising edge. The cell count must be from 3 to 6.

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

d Steady Around Every Edge

Input
d at the edge, in the high phase, after the fall: 111 000 111 111 000 000
Expected output
q_after_rise1 = yes, q_in_high_phase1 = yes, q_after_fall1 = yes, q_after_rise2 = no, q_in_high_phase2 = no, q_after_fall2 = no, q_after_rise3 = yes, q_in_high_phase3 = yes, q_after_fall3 = yes, q_after_rise4 = yes, q_in_high_phase4 = yes, q_after_fall4 = yes, q_after_rise5 = no, q_in_high_phase5 = no, q_after_fall5 = no, q_after_rise6 = no, q_in_high_phase6 = no, q_after_fall6 = no, cell_count_out_of_range = no

d does not move inside a period, so q simply takes it at each rising edge and keeps it.

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

Icarus · browser samples / server checks