Breadboard exercises/ BB2EnglishРусский

Exercise BB2 · building blocks · shift register

4014 parallel-in, serial-out shift register

Eight wires set an 8-bit value; the LOAD button puts it into a 4014 at a clock edge, and every later edge moves it one stage on. The value comes out of Q8 one bit per second, P8 first: an 8-bit word turned into a stream of bits.

1 chip4014 8-stage shift register
15 + 12 wireson the board + jumper leads to the modules
8 clocksto send one value out
~1 Hzfrom the 555 clock module

1What it is and why it matters

What it does. The 4014 holds eight bits in eight stages. At a rising clock edge it does one of two things, chosen by its PARALLEL/SERIAL CONTROL input, here the LOAD button: with LOAD pressed it copies the eight inputs P1–P8 into the stages; with LOAD released it shifts, every stage taking the bit of the stage before it and stage 1 taking SERIAL IN, which is tied to GND. Only the last three stages have pins, Q6, Q7 and Q8. The LED module shows the clock, LOAD, Q6, Q7 and Q8.

How it works. Press LOAD, hold it until the clock LED blinks once, release. Q8 now shows P8, Q7 P7, Q6 P6. At each following blink the bits move one LED toward Q8: Q8 shows P7, then P6, and after seven more blinks P1. Then zeros follow, because SERIAL IN is 0.

Where it is used. Wherever many bits travel over one wire: a serial port, USB, SPI between a processor and a sensor, the link between a keyboard and a computer. The sender has a parallel-in, serial-out register like this one; the receiver has a serial-in, parallel-out register like the 4015 of exercise B6. In this course the 4014 is the serializer of the bit-serial adder (P1, P2): two 4014s feed the bits of two numbers into an adder, one pair per clock.

Why learn it.

2The key elements: load, then shift

With the wires as on the breadboard, P8 … P1 = 01001011. One load and nine shifts, stage by stage:

Clock edgeQ8 … Q1Q6Q7Q8
LOAD01001011010
shift 110010110001
shift 200101100100
shift 301011000010
shift 410110000101
shift 501100000110
shift 611000000011
shift 710000000001
shift 800000000000
shift 900000000000

Each row is what the stages hold after that clock edge. The column Q8 is the serial output: read down, it is the loaded value from P8 to P1, followed by zeros.

Waveforms

CLKLOADP8…P10100101110100101Q6Q7Q8
From the simulation of tb.sv in Icarus Verilog. Before the first load the stages are unknown (grey). LOAD is 1 during one clock period; at the edge that ends it the value is loaded, and Q8 then shows one bit per clock. · SVG · PNG

3Parts

PartIn this circuitQtyPriceSupplier
CD40148-stage parallel-in / serial-out shift register1$0.19Jameco (jameco.com)
Button moduleLOAD; one from the pack of five1$0.66 / packaliexpress.us
555 clock modulethe clock, set to about 1 Hz1$0.53aliexpress.us
8-LED moduleshows CLK, LOAD, Q6, Q7 and Q81$0.38aliexpress.us
Breadboard, 830 points1$3.00amazon.com
Breadboard power module5 V on the rails1$1.00aliexpress.us
USB-to-DC cable, 9 Vpowers the module from a USB charger1$1.17aliexpress.us
Pre-cut jumper wires15 wires on the board (or male-male Dupont jumpers)15$11.95 / packjameco.com
Male-female Dupont jumpers12 leads to the clock module, the button and the LED module12$1.30 / packaliexpress.us

Prices from the LSSK components sheet; the 4014 is in the Comprehensive kit. Set the clock module slow, about one blink per second, so that each step can be seen.

4The chip, pin by pin

The chips are CMOS 4000B series: they work from 3 to 18 V, here from the 5 V of the power module. Every input must be connected to something, because a CMOS input left open picks up noise and can switch at random. The drawings show each chip from above with the notch on the left, as in the datasheet: pin 1 at the bottom left, pins numbered counter-clockwise. Blue arrows are inputs, orange arrows are outputs.

4014: 8-stage parallel-in / serial-out shift register

1P82Q63Q84P45P36P27P18VSS9P/S10CLK11SER12Q713P514P615P716VDD40148-bit PISO register
CD4014B. A 16-pin chip: VDD is pin 16 and VSS pin 8. · SVG · PNG

P1–P4 are on pins 7–4, P5–P7 on pins 13–15 and P8 on pin 1. PARALLEL/SERIAL CONTROL is pin 9, CLOCK pin 10, SERIAL IN pin 11. The outputs are Q6 on pin 2, Q7 on pin 12 and Q8 on pin 3.

Polarity. Everything happens at the rising edge of CLOCK. PARALLEL/SERIAL CONTROL = 1 means load, 0 means shift. The chip has no reset: after power-up the stages hold random bits until the first load.

PinNameDirectionFunctionIn this circuit
1P8inputparallel input, stage 8P8 (GND)
2Q6outputoutput of stage 6Q6
3Q8outputoutput of stage 8, the serial outputQ8
4P4inputparallel input, stage 4P4 (+5V)
5P3inputparallel input, stage 3P3 (GND)
6P2inputparallel input, stage 2P2 (+5V)
7P1inputparallel input, stage 1P1 (+5V)
8VSSpowerground, 0 VGND
9PAR/SERinput1 = load P1-P8 at the next clock edge, 0 = shift; works with the clock (synchronous)LOAD
10CLOCKinputeverything happens on the rising edgeCLK
11SERIAL INinputserial input: goes into stage 1 when shiftingGND
12Q7outputoutput of stage 7Q7
13P5inputparallel input, stage 5P5 (GND)
14P6inputparallel input, stage 6P6 (GND)
15P7inputparallel input, stage 7P7 (+5V)
16VDDpowersupply, +5 V (3 to 18 V allowed)+5V

5Schematic 1: the register as a block

CLKLOADP1P2P3P4P5P6P7P84014CLKP/SSERP1P2P3P4P5P6P7P8Q6Q7Q88-stage shift registerCLKLED 1LOADLED 2Q6LED 3Q7LED 4Q8LED 5
The circuit with the 4014 as one block. This is what serializer.sv describes. The open circles are the wires to the rails that set P1–P8; SERIAL IN is tied to GND. · SVG · PNG

Inside, each stage is a D flip-flop with a 2:1 multiplexer in front of it: the multiplexer passes Pk while LOAD is 1 and the output of the stage before while it is 0.

6Schematic 2: connections

12345678910111213141516P8Q6Q8P4P3P2P1P/SCLKSERQ7P5P6P740148-bit PISO registerCLKLOADQ6Q7Q8clockLOADLED module: LEDs 1-5P1→ +5VP2→ +5VP3→ GNDP4→ +5VP8→ GNDP5→ GNDP6→ GNDP7→ +5V+5V
The same circuit with the chip, pin by pin. The chip is turned around on the breadboard and drawn the same way here. The boxes with an arrow are the wires to the rails, with the rail they start on. · SVG · PNG
NetFromToWire
CLK555 clock module, OUT4014 10 (CLK), LED module, LED 1M-F, M-M
LOADbutton LOAD, OUT4014 9 (P/S), LED module, LED 2M-F, M-M
P1P1 wire: +5V4014 7 (P1)M-M
P2P2 wire: +5V4014 6 (P2)M-M
P3P3 wire: GND4014 5 (P3)M-M
P4P4 wire: +5V4014 4 (P4)M-M
P5P5 wire: GND4014 13 (P5)M-M
P6P6 wire: GND4014 14 (P6)M-M
P7P7 wire: +5V4014 15 (P7)M-M
P8P8 wire: GND4014 1 (P8)M-M
Q64014 2 (Q6)LED module, LED 3M-F, M-M
Q74014 12 (Q7)LED module, LED 4M-F
Q84014 3 (Q8)LED module, LED 5M-F, M-M
+5V+5V4014 16 (VDD)
GNDGND4014 8 (VSS), 4014 11 (SER)

7Schematic 3: the breadboard

−+−+12345678910111213141516171819202122232425262728293031323334353637383940414243444546abcdefghij5V5VPOWER MODULE · 5V9V USB-DC4014P1P2P3P4P5P6P7P8Q8−OUT+555CLOCK 555~1 Hz+OUT−LOADG1CLK2LOAD3Q64Q75Q86788-LED MODULELEDs 1-5 used
Every wire in its hole. Column numbers are printed along the top; rows a–e are above the gap, f–j below it. The clock module and the LOAD button hang above the board, the LED module below it. The wires with a tag set P1–P8: + is 1, − is 0. · SVG · PNG
CLKLOADP1P2P3P4P5P6P7P8Q6Q7Q8+5VGND

Build it step by step

  1. Do the getting-started steps first: how the breadboard is connected inside, the power module with both jumpers on 5 V, how a chip sits across the gap.
  2. Insert the chips across the gap: the 4014 turned around, notch to the right, pin 1 in hole 21e. Read the numbers on the chips: many look alike. A chip turned around has pin 1 at the top right: it puts the pins this circuit needs next to its neighbours.
  3. Power: for each chip a red wire from its VDD pin (14, or 16 on a 16-pin chip) to the + rail and a black wire from its VSS pin (7 or 8) to the − rail, each to the rail on its own side of the gap. On a chip turned around VDD is on the bottom row and VSS on the top row. Make sure no wire touches + and − at the same time.
  4. SERIAL IN, pin 11 (column 16, bottom half), to the bottom − rail.
  5. The eight data wires P1–P8, each from its pin to a rail: + for a 1, − for a 0. Start with the value of the checklist, P8 … P1 = 01001011.
  6. Four short signal wires cross the gap: CLOCK (column 15) and LOAD (column 14) go up to columns 13 and 12 of the top half, where the leads of the modules above the board land; Q6 (column 20) and Q8 (column 19) go down to columns 22 and 23 of the bottom half, for the LED module below.
  7. With the power off, the modules: the clock module and the LOAD button above the board, the LED module below.
  8. Turn the power on and set the clock module to about one blink per second.

Wiring checklist

·

#NetFromTo
Power (red to +, black to −) · 2 wires
1+5V21j (4014 pin 16, VDD)bottom + rail, column 21
2GND14a (4014 pin 8, VSS)top − rail, column 14
Fixed inputs: CMOS inputs must not float · 1 wire
11GND16j (4014 pin 11, SER)bottom − rail, column 16
Mode wires: move them between + and − to change the mode · 8 wires
3P115a (4014 pin 7, P1)top + rail, column 15
4P216a (4014 pin 6, P2)top + rail, column 16
5P317a (4014 pin 5, P3)top − rail, column 17
6P418a (4014 pin 4, P4)top + rail, column 18
7P518j (4014 pin 13, P5)bottom − rail, column 18
8P619j (4014 pin 14, P6)bottom − rail, column 19
9P720j (4014 pin 15, P7)bottom + rail, column 20
10P821a (4014 pin 1, P8)top − rail, column 21
Signal wires · 4 wires
12CLK15g (4014 pin 10, CLK)13e
13LOAD14g (4014 pin 9, P/S)12e
14Q620d (4014 pin 2, Q6)22f
15Q819c (4014 pin 3, Q8)23f
Modules on male-female jumpers · 12 wires
16GND555 clock module, −top − rail, column 26
17CLK555 clock module, OUT13a
18+5V555 clock module, +top + rail, column 28
19+5Vbutton LOAD, +top + rail, column 31
20LOADbutton LOAD, OUT12a
21GNDbutton LOAD, −top − rail, column 33
22GNDLED module, Gbottom − rail, column 28
23CLKLED module, LED 115j (4014 pin 10, CLK)
24LOADLED module, LED 214j (4014 pin 9, P/S)
25Q6LED module, LED 322j
26Q7LED module, LED 417j (4014 pin 12, Q7)
27Q8LED module, LED 523j

Test it

If it does not work: if nothing ever changes, check the clock lead to pin 10 and the power. If LOAD has no effect, check pin 9: it must be 1 while the button is pressed. If the bits come out in a strange order, check which pin each data wire is on: P1–P8 are spread over both rows. If all bits are 1 or flicker, a data wire is missing.

8SystemVerilog: the generic design

serializer.sv describes the register as an 8-bit vector: at each clock edge it either takes p or shifts itself one place up with a 0 coming in at the bottom. It is synthesizable.

serializer.svfile
// Exercise BB2: an 8-bit parallel-in / serial-out shift register, like the
// 4014.
//
// Generic version that matches schematic 1, not tied to any chip.  At each
// rising clock edge it either loads the parallel value p (load = 1) or
// shifts every bit one stage up, taking a 0 into stage 1 (load = 0).  The
// outputs are the last three stages, as on the 4014.  Synthesizable.
//
// Like the 4014, it has no reset: after power-up it holds whatever value,
// until the first load.

module serializer
(
    input  logic       clk,
    input  logic       load,  // 1: load p at the clock edge; 0: shift
    input  logic [7:0] p,     // p [0] is P1, p [7] is P8

    output logic       q6,
    output logic       q7,
    output logic       q8     // The serial output: P8 first, then P7, ...
);

    logic [7:0] q;  // q [0] is stage 1, q [7] is stage 8

    always_ff @ (posedge clk)
        if (load)
            q <= p;
        else
            q <= { q [6:0], 1'b0 };  // SERIAL IN is tied to GND

    assign q6 = q [5];
    assign q7 = q [6];
    assign q8 = q [7];

endmodule

{ q [6:0], 1'b0 } is a concatenation: the lower seven bits move one place up and a 0 comes in at the bottom. This one line is the eight multiplexers and the wiring between the stages.

9SystemVerilog model of the chip

One module per chip, with ports named as in the datasheet and the pin number of each port in a comment. The models leave out the power pins: in SystemVerilog a wire simply has the value 0 or 1.

cd4014.svfile
// CD4014B: 8-stage static shift register, synchronous parallel or serial
// input, serial output.
//
// Ports in pin order, named as in the datasheet.  The power pins, VSS (8)
// and VDD (16), are left out.  Synthesizable.
//
// Everything happens on the rising edge of CLOCK.  With PARALLEL/SERIAL
// CONTROL = 1 the edge loads P1-P8 into the eight stages; with 0 it shifts:
// SERIAL IN goes into stage 1 and every stage takes the bit of the stage
// before it.  Q8, the output of the last stage, is the serial output; Q6 and
// Q7 are there too.  The chip has no reset.

module cd4014
(
    input  logic p8,               // Pin 1
    output logic q6,               // Pin 2
    output logic q8,               // Pin 3
    input  logic p4,               // Pin 4
    input  logic p3,               // Pin 5
    input  logic p2,               // Pin 6
    input  logic p1,               // Pin 7
                                   // Pin 8:  VSS
    input  logic parallel_serial,  // Pin 9:  1 = load, 0 = shift
    input  logic clock,            // Pin 10
    input  logic serial_in,        // Pin 11
    output logic q7,               // Pin 12
    input  logic p5,               // Pin 13
    input  logic p6,               // Pin 14
    input  logic p7                // Pin 15
                                   // Pin 16: VDD
);

    logic [8:1] stage;

    always_ff @ (posedge clock)
        if (parallel_serial)
            stage <= { p8, p7, p6, p5, p4, p3, p2, p1 };
        else
            stage <= { stage [7:1], serial_in };

    assign q6 = stage [6];
    assign q7 = stage [7];
    assign q8 = stage [8];

endmodule

10SystemVerilog: the chip-level design

serializer_chips.sv connects the chip models exactly as schematic 2 and the breadboard connect the chips, with the same ports as serializer.sv. Unused inputs are tied to vdd or gnd as on the breadboard, and unused outputs go to wires named unused_…. The file is generated from the breadboard wire list after a pin-level check of that list, so the code, the drawings and the checklist cannot disagree.

serializer_chips.svfile
// Exercise BB2, chip-level version: the chips and wires of schematic 2 and
// of the breadboard.  Same ports as serializer.sv.
//
// Generated by tools/bb_lib.py from the checked breadboard wire list;
// change the layout in tools/bb_bb2.py, not this file.  Synthesizable.

module serializer_chips
(
    input  logic       clk,
    input  logic       load,
    input  logic [7:0] p,
    output logic       q6,
    output logic       q7,
    output logic       q8
);

    // The power rails of the breadboard, for tied-off inputs

    wire gnd = 1'b0;

    //------------------------------------------------------------------------

    // U1: CD4014 8-stage shift register, parallel or serial in, serial out, pin 1 in column 14

    cd4014 i_u1
    (
        .p8              ( p [7] ),  // Pin 1 <- P8 wire, at GND on the breadboard
        .q6              ( q6    ),  // Pin 2 -> Q6
        .q8              ( q8    ),  // Pin 3 -> Q8
        .p4              ( p [3] ),  // Pin 4 <- P4 wire, at +5V on the breadboard
        .p3              ( p [2] ),  // Pin 5 <- P3 wire, at GND on the breadboard
        .p2              ( p [1] ),  // Pin 6 <- P2 wire, at +5V on the breadboard
        .p1              ( p [0] ),  // Pin 7 <- P1 wire, at +5V on the breadboard
        .parallel_serial ( load  ),  // Pin 9 <- button LOAD
        .clock           ( clk   ),  // Pin 10 <- 555 clock module
        .serial_in       ( gnd   ),  // Pin 11 <- GND
        .q7              ( q7    ),  // Pin 12 -> Q7
        .p5              ( p [4] ),  // Pin 13 <- P5 wire, at GND on the breadboard
        .p6              ( p [5] ),  // Pin 14 <- P6 wire, at GND on the breadboard
        .p7              ( p [6] )   // Pin 15 <- P7 wire, at +5V on the breadboard
    );

endmodule

11Testbench and simulation scripts

tb.sv runs both designs side by side from the same clock and compares them with a reference model, an 8-bit vector that loads and shifts. Like the testbenches of Basics-Graphics-Music, it drives and checks signals only at rising clock edges. The register has no reset, so the check starts at the first load. The testbench loads the value of the breadboard and shifts it all out with two zeros after it, loads again in the middle of shifting, and holds LOAD for two edges; it prints PASS only if both designs match every time and all of this happened.

tb.sv184 lines · show the codefile
// Testbench for exercise BB2.
//
// Runs the generic design (serializer.sv) and the chip-level design
// (serializer_chips.sv) side by side and compares both with a reference
// model.  Works with Icarus Verilog, Verilator (--binary) and Questa.
//
// Like the testbenches of Basics-Graphics-Music, it drives and samples
// signals only at rising clock edges.  The shift register has no reset, so
// the check starts at the first load.  The testbench shifts a whole value
// out and the zeros after it, loads in the middle of shifting, and holds
// LOAD for two edges.

// Time units.  The design files have none: Yosys does not accept timeunit
// and timeprecision, and the Tiny Tapeout template sets `timescale 1ns / 1ps
// in its own testbench.  This line sets the same units here and for the
// design files compiled after it, so tb.sv comes first in the file list.

`timescale 1ns / 1ps

module tb;

    logic       clk;
    logic       load;
    logic [7:0] p;

    wire        q6, q6_chips;
    wire        q7, q7_chips;
    wire        q8, q8_chips;

    serializer i_generic
    (
        .clk  ( clk  ),
        .load ( load ),
        .p    ( p    ),
        .q6   ( q6   ),
        .q7   ( q7   ),
        .q8   ( q8   )
    );

    serializer_chips i_chips
    (
        .clk  ( clk      ),
        .load ( load     ),
        .p    ( p        ),
        .q6   ( q6_chips ),
        .q7   ( q7_chips ),
        .q8   ( q8_chips )
    );

    //------------------------------------------------------------------------
    // Clock with a period of 10 ns.
    // On the breadboard one period is about a second.

    initial
    begin
        clk = '0;

        forever
            # 5 clk = ~ clk;
    end

    //------------------------------------------------------------------------
    // Reference model: the eight stages as a vector, model [7] is stage 8.
    // loaded is 1 from the first load on; before it the stages are unknown.

    // Initial values are fine in a testbench
    // verilator lint_off PROCASSINIT

    logic [7:0] model;
    logic       loaded = 1'b0;

    always @ (posedge clk)
    begin
        if (load)
            model <= p;
        else
            model <= { model [6:0], 1'b0 };

        if (load)
            loaded <= 1'b1;
    end

    wire [2:0] expected  = model [7:5];
    wire [2:0] out       = { q8,       q7,       q6       };
    wire [2:0] out_chips = { q8_chips, q7_chips, q6_chips };

    //------------------------------------------------------------------------
    // Checking at every rising clock edge.  At the edge both designs and the
    // model still show the clock cycle that ends there, the same values that
    // a flip-flop would sample.

    logic done = 1'b0;  // Set by the stimulus before $finish

    int cycle    = 0;
    int n_checks = 0;
    int n_errors = 0;
    int n_loads  = 0;
    int n_zeros  = 0;   // Cycles in which a zero shifted in after the value reached Q8

    always @ (posedge clk)
    begin
        cycle <= cycle + 1;

        if (loaded & ~ done)
        begin
            n_checks <= n_checks + 2;
            n_errors <= n_errors + (out === expected ? 0 : 1) + (out_chips === expected ? 0 : 1);

            if (~ load & model == 8'b0)
                n_zeros <= n_zeros + 1;
        end

        if (load & ~ done)
            n_loads <= n_loads + 1;

        if (~ done)
            $display ("%5d   %b    %b   %b  %b  %b   %b    %s  %s",
                cycle + 1, load, p, q8, q7, q6, loaded ? expected : 3'bxxx,
                ~ loaded ? "-      " : out       === expected ? "ok     " : "WRONG  ",
                ~ loaded ? "-"       : out_chips === expected ? "ok   "   : "WRONG");
    end

    //------------------------------------------------------------------------
    // Driving the inputs

    task automatic load_value (input logic [7:0] value, input int n_shifts);
        p    <= value;
        load <= 1'b1;
        @ (posedge clk);

        load <= 1'b0;
        repeat (n_shifts) @ (posedge clk);
    endtask

    initial
    begin
        $dumpfile ("tb.vcd");

        `ifdef __ICARUS__
            $dumpvars (0, tb);  // Without a scope Icarus also tries to dump $unit
        `else
            $dumpvars;
        `endif

        $display ("cycle  LOAD  P8...P1   Q8 Q7 Q6  model  generic  chips");

        // After power-up the stages are unknown, as on the breadboard

        p    <= 8'b0100_1011;
        load <= 1'b0;
        repeat (2) @ (posedge clk);

        load_value (8'b0100_1011, 10);  // The value on the wires of the breadboard, and two zeros after it
        load_value (8'b1010_0101,  4);  // Load again in the middle of shifting
        load_value (8'b1111_1111,  3);

        // LOAD held for two edges loads twice

        p    <= 8'b0000_0001;
        load <= 1'b1;
        repeat (2) @ (posedge clk);
        load <= 1'b0;
        repeat (9) @ (posedge clk);

        // Stop checking one clock edge before $finish, so that every
        // simulator reports the same number of checks

        done <= '1;
        @ (posedge clk);

        $finish;
    end

    //------------------------------------------------------------------------

    final
    begin
        if (n_errors == 0 && n_loads >= 5 && n_zeros >= 2)
            $display ("PASS: %0d checks, no errors", n_checks);
        else
            $display ("FAIL: %0d errors in %0d checks, %0d loads, %0d zeros", n_errors, n_checks, n_loads, n_zeros);
    end

endmodule

Each script compiles the files, runs the simulation, writes a log and the waveform tb.vcd, and exits with an error unless the test passes. Run them from any directory: ./run_icarus.bash, ./run_verilator.bash or ./run_questa.bash; ./clean.bash removes what they make. The waveform opens in GTKWave or Surfer.

run_icarus.bash22 lines · show the codefile
#!/usr/bin/env bash

# Exercise BB2: run the testbench with Icarus Verilog 12 or later.
# The output goes to icarus.log and the waveform to tb.vcd,
# for GTKWave or Surfer.

set -Eeuo pipefail

cd "$(dirname "$0")"

files=(tb.sv serializer.sv serializer_chips.sv cd4014.sv)

if ! command -v iverilog > /dev/null
then
    printf '%s\n' "$0: iverilog not found, install Icarus Verilog 12 or later" >&2
    exit 1
fi

iverilog -g2012 -o tb.vvp "${files[@]}"
vvp -n tb.vvp | tee icarus.log

grep -q '^PASS' icarus.log
run_verilator.bash29 lines · show the codefile
#!/usr/bin/env bash

# Exercise BB2: run the testbench with Verilator 5.  --binary builds
# a simulator with the testbench's delays; it needs a C++20 compiler.
# The output goes to verilator.log and the waveform to tb.vcd.

set -Eeuo pipefail

cd "$(dirname "$0")"

files=(tb.sv serializer.sv serializer_chips.sv cd4014.sv)

# OSS CAD Suite unpacked in the home directory has Verilator too

if ! command -v verilator > /dev/null && [ -x "$HOME/oss-cad-suite/bin/verilator" ]
then
    PATH="$HOME/oss-cad-suite/bin:$PATH"
fi

if ! command -v verilator > /dev/null
then
    printf '%s\n' "$0: verilator not found, install Verilator 5" >&2
    exit 1
fi

verilator --binary -Wall --trace --top-module tb -o tb "${files[@]}"
obj_dir/tb | tee verilator.log

grep -q '^PASS' verilator.log
run_questa.bash64 lines · show the codefile
#!/usr/bin/env bash

# Exercise BB2: run the testbench with Questa, for example the free
# Questa Starter Edition that comes with Quartus Prime Lite.
# The output goes to questa.log and the waveform to tb.vcd.

set -Eeuo pipefail

cd "$(dirname "$0")"

files=(tb.sv serializer.sv serializer_chips.sv cd4014.sv)

# When vsim is not in PATH, look for it where Quartus installs it

if ! command -v vsim > /dev/null
then
    for dir in "$HOME"/intelFPGA_lite/*/questa_fse/bin  \
               "$HOME"/altera_lite/*/questa_fse/bin     \
               /opt/intelFPGA_lite/*/questa_fse/bin     \
               /opt/altera_lite/*/questa_fse/bin
    do
        if [ -x "$dir/vsim" ]
        then
            PATH="$dir:$PATH"
            break
        fi
    done
fi

if ! command -v vsim > /dev/null
then
    printf '%s\n' "$0: vsim not found, install Questa or add it to PATH" >&2
    exit 1
fi

# The Starter Edition needs a free license file from the Intel
# licensing site; Quartus keeps it in ~/flexlm/license.dat

license="$HOME/flexlm/license.dat"

# ${SALT_LICENSE_SERVER-} is empty when the variable is not set,
# where a plain $SALT_LICENSE_SERVER would stop the script (set -u)
salt_license="${SALT_LICENSE_SERVER-}"

if [ -z "$salt_license" ] && [ -f "$license" ]
then
    export SALT_LICENSE_SERVER="$license"
fi

# Older Questa versions read LM_LICENSE_FILE; ${LM_LICENSE_FILE-}
# is empty when the variable is not set
lm_license="${LM_LICENSE_FILE-}"

if [ -z "$lm_license" ] && [ -f "$license" ]
then
    export LM_LICENSE_FILE="$license"
fi

rm -rf work
vlib work > /dev/null
vlog -sv -lint "${files[@]}"
vsim -c tb -do "run -all; quit -f" | tee questa.log

grep -q 'PASS' questa.log

Tested with Icarus Verilog 12.0 and 14.0 (development), Verilator 5.053 with -Wall and no warnings, and Questa Altera Starter FPGA Edition 2025.2 from Quartus Prime Lite 25.1. The code uses only what Icarus Verilog 13 supports. This is the output, from Icarus Verilog:

VCD info: dumpfile tb.vcd opened for output.
cycle  LOAD  P8...P1   Q8 Q7 Q6  model  generic  chips
    1   0    01001011   x  x  x   xxx    -            -
    2   0    01001011   x  x  x   xxx    -            -
    3   1    01001011   x  x  x   xxx    -            -
    4   0    01001011   0  1  0   010    ok       ok   
    5   0    01001011   1  0  0   100    ok       ok   
    6   0    01001011   0  0  1   001    ok       ok   
    7   0    01001011   0  1  0   010    ok       ok   
    8   0    01001011   1  0  1   101    ok       ok   
    9   0    01001011   0  1  1   011    ok       ok   
   10   0    01001011   1  1  0   110    ok       ok   
   11   0    01001011   1  0  0   100    ok       ok   
   12   0    01001011   0  0  0   000    ok       ok   
   13   0    01001011   0  0  0   000    ok       ok   
   14   1    10100101   0  0  0   000    ok       ok   
   15   0    10100101   1  0  1   101    ok       ok   
   16   0    10100101   0  1  0   010    ok       ok   
   17   0    10100101   1  0  0   100    ok       ok   
   18   0    10100101   0  0  1   001    ok       ok   
   19   1    11111111   0  1  0   010    ok       ok   
   20   0    11111111   1  1  1   111    ok       ok   
   21   0    11111111   1  1  1   111    ok       ok   
   22   0    11111111   1  1  1   111    ok       ok   
   23   1    00000001   1  1  1   111    ok       ok   
   24   1    00000001   0  0  0   000    ok       ok   
   25   0    00000001   0  0  0   000    ok       ok   
   26   0    00000001   0  0  0   000    ok       ok   
   27   0    00000001   0  0  0   000    ok       ok   
   28   0    00000001   0  0  0   000    ok       ok   
   29   0    00000001   0  0  0   000    ok       ok   
   30   0    00000001   0  0  1   001    ok       ok   
   31   0    00000001   0  1  0   010    ok       ok   
   32   0    00000001   1  0  0   100    ok       ok   
   33   0    00000001   0  0  0   000    ok       ok   
tb.sv:171: $finish called at 335000 (1ps)
PASS: 60 checks, no errors

To see that the testbench catches mistakes, break something and run it again: shift the other way, q <= { 1'b0, q [7:1] };, in serializer.sv, or swap Q6 and Q7 in serializer_chips.sv. The script reports FAIL and the trace shows the lines marked WRONG.

12On an FPGA board: lab_top in BGM

The Basics-Graphics-Music (BGM) repository runs the same labs on dozens of FPGA boards. Each lab has a lab_top module with the same ports on every board: clock, a 1 Hz slow_clk, reset, keys, LEDs, a 7-segment display.

  1. Copy labs/1_basics/1_07_shift_register to a new directory.
  2. Replace its lab_top.sv with bgm_lab/lab_top.sv from this exercise and add serializer.sv. The BGM scripts compile every .sv file of the directory except tb.sv.
  3. Run ./03_synthesize_for_fpga.bash and ./04_configure_fpga.bash; the first run asks which board you have.

The design runs from slow_clk, which every BGM board makes at 1 Hz, so it steps once a second like the breadboard with the 555 module. BGM makes it in peripherals/slow_clk_gen.sv: a counter on the board clock toggles a flip-flop, and on AMD (Xilinx) and Intel (Altera) FPGAs a BUFG or global primitive puts that signal on the FPGA clock network, so it reaches all flip-flops like a real clock.

Switches 7–0 are P8–P1 and key 0 is LOAD; LEDs 0–2 show Q6, Q7 and Q8. To run the chip-level design instead, add serializer_chips.sv and cd4014.sv and change the module name in the instance.

bgm_lab/lab_top.sv99 lines · show the codefile
// Exercise BB2 on an FPGA board with the Basics-Graphics-Music (BGM) labs:
// https://github.com/yuri-panchul/basics-graphics-music
//
// Copy labs/1_basics/1_07_shift_register to a new directory, replace its
// lab_top.sv with this file and add serializer.sv.  For the chip-level
// design add serializer_chips.sv and cd4014.sv instead, and change the
// module name below.
//
// Switches 7-0 are the parallel value P8-P1 and key 0 is LOAD.  The design
// runs from slow_clk, 1 Hz on every BGM board: hold key 0 through a tick to
// load, then watch the value leave on LED 2 (Q8), P8 first.  LEDs 0 and 1
// show Q6 and Q7.

`include "config.svh"

module lab_top
# (
    parameter  clk_mhz       = 50,
               w_key         = 4,
               w_sw          = 8,
               w_led         = 8,
               w_digit       = 8,
               w_gpio        = 100,

               screen_width  = 640,
               screen_height = 480,

               w_red         = 4,
               w_green       = 4,
               w_blue        = 4,

               w_x           = $clog2 ( screen_width  ),
               w_y           = $clog2 ( screen_height )
)
(
    input                        clk,
    input                        slow_clk,
    input                        rst,

    // Keys, switches, LEDs

    input        [w_key   - 1:0] key,
    input        [w_sw    - 1:0] sw,
    output logic [w_led   - 1:0] led,

    // A dynamic seven-segment display

    output logic [          7:0] abcdefgh,
    output logic [w_digit - 1:0] digit,

    // Graphics

    input        [w_x     - 1:0] x,
    input        [w_y     - 1:0] y,

    output logic [w_red   - 1:0] red,
    output logic [w_green - 1:0] green,
    output logic [w_blue  - 1:0] blue,

    // Microphone, sound output and UART

    input        [         23:0] mic,
    output       [         15:0] sound,

    input                        uart_rx,
    output                       uart_tx,

    // General-purpose Input/Output

    inout        [w_gpio  - 1:0] gpio
);

    //------------------------------------------------------------------------

       assign abcdefgh   = '0;
       assign digit      = '0;
       assign red        = '0;
       assign green      = '0;
       assign blue       = '0;
       assign sound      = '0;
       assign uart_tx    = '1;

    //------------------------------------------------------------------------

    logic q6, q7, q8;

    serializer i_serializer
    (
        .clk  ( slow_clk  ),
        .load ( key [0]   ),
        .p    ( 8' (sw)   ),
        .q6   ( q6        ),
        .q7   ( q7        ),
        .q8   ( q8        )
    );

    assign led = w_led' ({ q8, q7, q6 });

endmodule

13On an FPGA board: hackathon_top in BGM

The hackathon setup of BGM uses a Tang Nano 9K with a 480×272 screen and a TM1638 board of keys, LEDs and digits. Its top module, hackathon_top, has simpler ports; the first line of the file names the board, so the scripts need no questions.

  1. Copy hackathon/problems/5_shift_register.
  2. Replace its hackathon_top.sv with bgm_hackathon/hackathon_top.sv and add serializer.sv.
  3. Run ./03_synthesize_for_fpga.bash and ./04_configure_fpga.bash.

Key 0 is LOAD and keys 7–1 are P7–P1; the board has only eight keys, so P8 is 0. The screen shows Q6 and Q7 as small squares and Q8, the serial output, as a large one.

bgm_hackathon/hackathon_top.sv80 lines · show the codefile
// Board configuration: tang_nano_9k_lcd_480_272_tm1638_hackathon
// This module uses few parameterization and relaxed typing rules

// Exercise BB2 in the hackathon setup of Basics-Graphics-Music (BGM):
// https://github.com/yuri-panchul/basics-graphics-music
//
// Copy a directory such as hackathon/problems/5_shift_register, replace its
// hackathon_top.sv with this file and add serializer.sv.
//
// Key 0 is LOAD and keys 7-1 are P7-P1; P8 is 0.  The design runs from
// slow_clock.  The LEDs show Q6, Q7 and Q8, and the screen shows the three
// outputs as lamps: Q8 large, in orange, Q6 and Q7 small, in green.

module hackathon_top
(
    input  logic       clock,
    input  logic       slow_clock,
    input  logic       reset,

    input  logic [7:0] key,
    output logic [7:0] led,

    // A dynamic seven-segment display

    output logic [7:0] abcdefgh,
    output logic [7:0] digit,

    // LCD screen interface

    input  logic [8:0] x,
    input  logic [8:0] y,

    output logic [4:0] red,
    output logic [5:0] green,
    output logic [4:0] blue,

    inout  logic [3:0] gpio
);

    //------------------------------------------------------------------------

    logic q6, q7, q8;

    serializer i_serializer
    (
        .clk  ( slow_clock          ),
        .load ( key [0]             ),
        .p    ( { 1'b0, key [7:1] } ),
        .q6   ( q6                  ),
        .q7   ( q7                  ),
        .q8   ( q8                  )
    );

    assign led      = { 5'b0, q8, q7, q6 };

    assign abcdefgh = '0;
    assign digit    = '0;

    //------------------------------------------------------------------------
    // Q6 and Q7 as small green squares, Q8, the serial output, as a large
    // orange one.  Bright when 1, dim when 0.

    always_comb
    begin
        red   = 0;
        green = 0;
        blue  = 0;

        if (x >= 80 & x < 120 & y >= 116 & y < 156)
            green = q6 ? 63 : 12;
        else if (x >= 160 & x < 200 & y >= 116 & y < 156)
            green = q7 ? 63 : 12;
        else if (x >= 260 & x < 360 & y >= 86 & y < 186)
        begin
            red   = q8 ? 31 : 6;
            green = q8 ? 32 : 6;
        end
    end

endmodule

14On a chip: Tiny Tapeout

Tiny Tapeout makes real chips from student designs. Each design is a module with the ports below; on the demo board ui_in comes from DIP switches and uo_out drives a 7-segment display.

The demo board makes the project clock itself and can set it anywhere from 1 Hz to 66.5 MHz (see Clock in the Tiny Tapeout docs). Set it to 1 Hz, and project.v connects clk straight to the shift register, just as the 555 module clocks the chips on the breadboard.

Switch 0 is LOAD and switches 7–1 are P7–P1; P8 is 0. Segment a shows Q8, segments b and c show Q7 and Q6.

tiny_tapeout/project.v52 lines · show the codefile
/*
 * Exercise BB2 on Tiny Tapeout: an 8-bit parallel-in / serial-out shift
 * register like the 4014, with the DIP switches as inputs and the 7-segment
 * display of the demo board as lamps.
 *
 * Add serializer.sv to source_files in info.yaml next to project.v.
 * Rename the module to tt_um_<your GitHub name>_<project name> and use the
 * same name as top_module in info.yaml.
 */

`default_nettype none

module tt_um_example (
    input  wire [7:0] ui_in,    // DIP switches, INPUT Pmod, RP2350 GPIO17-24
    output wire [7:0] uo_out,   // 7-segment display, OUTPUT Pmod, RP2350 GPIO33-40
    input  wire [7:0] uio_in,   // BIDIR Pmod, RP2350 GPIO25-32
    output wire [7:0] uio_out,  //   (same pins)
    output wire [7:0] uio_oe,   //   1 = your design drives the pin
    input  wire       ena,      // set by the on-chip project mux, not a board pin
    input  wire       clk,      // RP2350 GPIO16
    input  wire       rst_n     // RP2350 GPIO14
);

    // Switch 0 is LOAD, switches 7-1 are P7-P1; P8 is 0.  The demo board
    // makes the project clock; set it to 1 Hz.

    wire q6, q7, q8;

    serializer i_serializer (
        .clk  ( clk                   ),
        .load ( ui_in [0]             ),
        .p    ( { 1'b0, ui_in [7:1] } ),
        .q6   ( q6                    ),
        .q7   ( q7                    ),
        .q8   ( q8                    )
    );

    // 7-segment display: uo_out [0] is segment a, ..., uo_out [6] is g,
    // uo_out [7] is the dot.  Segment a (top) is Q8, the serial output;
    // segments b and c (top right, bottom right) are Q7 and Q6.

    assign uo_out  = { 5'b0, q6, q7, q8 };

    assign uio_out = 8'b0;
    assign uio_oe  = 8'b0;

    // The 4014 has no reset, and neither has this design.
    // List all unused inputs to prevent warnings

    wire _unused = & { ena, rst_n, uio_in, 1'b0 };

endmodule

In info.yaml set the clock to 1 Hz, list the files and use the module name as the top module:

project:
  clock_hz:   1                 # 1 Hz from the demo board
  top_module: "tt_um_example"   # rename to tt_um_<your GitHub name>_serializer
  source_files:
    - "project.v"
    - "serializer.sv"
No clock divider on a chip. BGM makes slow_clk with a counter, but on AMD and Intel FPGAs it routes that signal onto the clock network with BUFG or global (section 12). In an ASIC a counter bit used as a clock is bad practice: it is a second clock with its own clock tree, its edges come later than those of clk by a delay nobody controls, and every signal between the two clocks crosses clock domains. Here the board makes the 1 Hz clock, so no divider is needed. When a chip needs a slow tick from a fast clock, keep one clock and add a clock enable: a pulse one clock long that lets the slow logic change only on the edges where it is 1.

15Going further

Tasks for students

Files of this exercise

Sources

The 4014 is in the Building blocks list of “An improved way of teaching school kids digital electronics” and in lesson 1.4 of the Habr article about the electronics course, where it feeds the serial adder. The chip is described in the Texas Instruments datasheet for the CD4014B.