Lesson 2. The counter: six flip-flops count to 63

In pictures

After this lesson six lamps count in binary — 0, 1, 2 … 63 and around again — and a display shows the same number in decimal. The counter is the heart of the raster scan: in the next lesson two of them visit every dot of the screen.


2.1. The idea: add 1 in a column

On every clock edge the counter adds 1. We know how to add 1 in a column: the lowest digit always flips, the next one when the lowest was 1, and so on.

  • XOR with the carry flips a digit: t(i) = Q(i) ⊕ c(i);
  • AND passes the carry on: c(i+1) = c(i) · Q(i);
  • the carry into the very lowest digit is EN: with EN = 0 counting stops.
XOR + DFFQ0 = 1becomes 0c1=1XOR + DFFQ1 = 1becomes 0c2=1XOR + DFFQ2 = 1becomes 0c3=1XOR + DFFQ3 = 0becomes 1EN=10111 (7) → 1000 (8): the carry flipped the three low digits
The counter adds 1 in a column. The carry into the lowest digit is EN. A digit flips if a carry came into it; the carry goes on if the digit was 1. Everything changes at once, on one clock edge.

Reset. Right after power-up the flip-flops are in an unpredictable state. RST = 1 for one clock edge writes 0 into every digit — real circuits reset this way at power-up. In real chips the reset is usually active-low (/MR on the 74HC161 counter, /RES on the 6502 processor), with one inverter for the whole board; here, for simplicity, it is RST = 1.

2.2. Building

The same project. Delete the lesson 1 setup (switches D, CLK, the DFF chip on the canvas, the lamp); keep Power/Ground.

The idea. The counter adds 1 on every clock edge — and we know how to add 1 in a column: the lowest digit always flips, the next one when the lowest was 1, and so on. XOR with the carry flips a digit, AND passes the carry on: c(i+1) = c(i) · Q(i). The carry into the very lowest digit is EN: EN = 0 — counting stops.

Reset. Right after power-up the flip-flops are in an unpredictable state. To start from zero, RST = 1 for one clock edge writes 0 into every digit — real circuits reset this way at power-up.

Step 1. CNT6 — six digits

PartPinLabel
Input portoutCLK
Input portoutEN
Input portoutRST
NOTI0
O0
RST
nRST
XOR (bit 0)I0
I1
O0
Q0
EN
t0
AND (reset 0)I0
I1
O0
t0
nRST
d0
AND (carry 0)I0
I1
O0
EN
Q0
c1
DFF (bit 0)D
CLK
Q
nQ
d0
CLK
Q0
— (free)
XOR (bit 1)I0
I1
O0
Q1
c1
t1
AND (reset 1)I0
I1
O0
t1
nRST
d1
AND (carry 1)I0
I1
O0
c1
Q1
c2
DFF (bit 1)D
CLK
Q
nQ
d1
CLK
Q1
— (free)
XOR (bit 2)I0
I1
O0
Q2
c2
t2
AND (reset 2)I0
I1
O0
t2
nRST
d2
AND (carry 2)I0
I1
O0
c2
Q2
c3
DFF (bit 2)D
CLK
Q
nQ
d2
CLK
Q2
— (free)
XOR (bit 3)I0
I1
O0
Q3
c3
t3
AND (reset 3)I0
I1
O0
t3
nRST
d3
AND (carry 3)I0
I1
O0
c3
Q3
c4
DFF (bit 3)D
CLK
Q
nQ
d3
CLK
Q3
— (free)
XOR (bit 4)I0
I1
O0
Q4
c4
t4
AND (reset 4)I0
I1
O0
t4
nRST
d4
AND (carry 4)I0
I1
O0
c4
Q4
c5
DFF (bit 4)D
CLK
Q
nQ
d4
CLK
Q4
— (free)
XOR (bit 5)I0
I1
O0
Q5
c5
t5
AND (reset 5)I0
I1
O0
t5
nRST
d5
AND (carry 5)I0
I1
O0
c5
Q5
CO
DFF (bit 5)D
CLK
Q
nQ
d5
CLK
Q5
— (free)
Output portinQ0
Output portinQ1
Output portinQ2
Output portinQ3
Output portinQ4
Output portinQ5
Output portinCO

Ctrl+G → name CNT6. Chip pins: inputs CLK, EN, RST; outputs Q0–Q5, CO.

CO is lit when the counter has reached 63 and counting is enabled: it is the carry into the next counter — in lesson 3 the row and column counters of the screen connect this way.

Step 2. Canvas: clock, switches, counter, lamps, display

PartPinLabel
ClockoutCLK
InputoutEN
InputoutRST
CNT6CLK
EN
RST
Q0
Q1
Q2
Q3
Q4
Q5
CO
CLK
EN
RST
Q0
Q1
Q2
Q3
Q4
Q5
CO
LEDinQ5
LEDinQ4
LEDinQ3
LEDinQ2
LEDinQ1
LEDinQ0
LEDinCO
Display⚙width 6, DEC, bus Q

Put the lamps in a row from Q5 on the left to Q0 on the right — that way they read as a binary number. A click on the Clock body changes its frequency: 1Hz, 2Hz, 10Hz, 60Hz and the fast ones.

2.3. Check

№What to doWhat you see
1Clock at 2Hz (click its body), EN = 0, RST = 1, wait a secondall Q lamps go out, the display shows 0 — reset on the clock edge
2RST = 0, EN = 1the lamps count in binary: 1, 2, 3…; the display shows the same in decimal
3wait for 63all six Q lamps and the CO lamp are lit
4one more clock0 again: the counter wrapped around
5EN = 0counting stops, the lamps hold the number

If the lamps do not go out after RST, check the AND (reset) gates: each has nRST on its second pin, and the NOT is on RST.


Lesson 2 paste (Cmd+I)

Needs NOT, XOR, AND and DFF from lesson 1.

# Lesson 2 — the 6-bit counter CNT6 with enable, reset and carry.chip CNT6 {  portIn(CLK)  portIn(EN)  portIn(RST)  portOut(Q0)  portOut(Q1)  portOut(Q2)  portOut(Q3)  portOut(Q4)  portOut(Q5)  portOut(CO)  NOT(RST, nRST)  XOR(Q0, EN, t0)  AND(t0, nRST, d0)  AND(EN, Q0, c1)  DFF(D=d0, CLK=CLK, Q=Q0)  XOR(Q1, c1, t1)  AND(t1, nRST, d1)  AND(c1, Q1, c2)  DFF(D=d1, CLK=CLK, Q=Q1)  XOR(Q2, c2, t2)  AND(t2, nRST, d2)  AND(c2, Q2, c3)  DFF(D=d2, CLK=CLK, Q=Q2)  XOR(Q3, c3, t3)  AND(t3, nRST, d3)  AND(c3, Q3, c4)  DFF(D=d3, CLK=CLK, Q=Q3)  XOR(Q4, c4, t4)  AND(t4, nRST, d4)  AND(c4, Q4, c5)  DFF(D=d4, CLK=CLK, Q=Q4)  XOR(Q5, c5, t5)  AND(t5, nRST, d5)  AND(c5, Q5, CO)  DFF(D=d5, CLK=CLK, Q=Q5)} clock{rt=2}(CLK)input(EN)input(RST)CNT6(CLK=CLK, EN=EN, RST=RST, Q0=Q0, Q1=Q1, Q2=Q2, Q3=Q3, Q4=Q4, Q5=Q5, CO=CO)led(Q0)led(Q1)led(Q2)led(Q3)led(Q4)led(Q5)led(CO)

Stuck? Something doesn't work →