Exercise B6 · basics · shift register
Shift register: serial in, parallel out
Press the D button during one blink of the clock and a lit LED appears on the left of the LED module; at every following blink it moves one LED to the right, until it falls off the end after eight. A 4015 holds the last eight bits you entered, one per clock period, and shows all of them at once.
1What it is and why it matters
What it does. A shift register is a chain of D flip-flops on one clock: at each rising edge, every flip-flop takes the value of the one before it, and the first takes the serial input. Bits enter one at a time and come out side by side: serial in, parallel out (SIPO). The 4015 has two 4-bit registers; connecting the last stage of the first (Q4A) to the input of the second (DATA B) makes one 8-bit register. The button gives the serial input D, the clock module the clock, another button the reset, and the LED module shows all eight stages. This is the fourth exercise of the Basics list in “An improved way of teaching school kids digital electronics”: “Demo 4015, a serial-to-parallel (SiPo – serial input / parallel output) shift register”; in lesson 1.3 of the Habr article half of the class enters bits with a button and watches the LEDs.
How it works. Hold D during a blink and Q1 becomes 1 at the edge. At the next edge Q1 passes its 1 to Q2 and takes the new value of D, and so on. What you see on the LEDs is the history of the button: Q1 is what D was one clock ago, Q8 what it was eight clocks ago.
Where it is used. Wherever bits travel one at a time: a serial port, SPI, USB and Ethernet receive bits on one wire and turn them into bytes with a shift register; a chip with few pins reads a long configuration word bit by bit. Shift registers also delay signals by a fixed number of clocks, make pseudo-random numbers (with a little XOR feedback), and become the Johnson counter inside the 4017 of exercise M11. The bit-serial adder project of the course uses a 4015 to collect the sum bits.
Why learn it.
- It shows how a register works over time: all flip-flops take their new values at the same edge, so the bits move exactly one place per clock and do not run through the whole chain.
- It is the simplest pipeline: data moves forward one stage per clock, like the stages of a processor.
- Serial-to-parallel conversion is part of almost every interface between chips.
- In SystemVerilog the whole register is one line:
q <= { q [6:0], d };.
2The key element: a chain of flip-flops
D flip-flop with reset
| rst | clk | D | Q after |
|---|---|---|---|
| 1 | any | any | 0, at once |
| 0 | ↑ | 0 | 0 |
| 0 | ↑ | 1 | 1 |
| 0 | no edge | any | keeps Q |
Each stage is one of these. Q takes the value of D at the rising edge; the reset clears it at once.
Four clock edges, the input 1, 0, 1, 1
After a reset all stages are 0. Each row is one rising edge: D comes in at Q1 and everything moves one place to the right.
| edge | D | Q1 | Q2 | Q3 | Q4 | Q5 | Q6 | Q7 | Q8 |
|---|---|---|---|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | |
| 1 | 1 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 2 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 |
| 3 | 1 | 1 | 0 | 1 | 0 | 0 | 0 | 0 | 0 |
| 4 | 1 | 1 | 1 | 0 | 1 | 0 | 0 | 0 | 0 |
Why does a bit move only one place per edge, and not all the way through? Because all flip-flops sample their
inputs at the same instant, the rising edge, and change only after it. Q2 takes the value Q1 had before the
edge. In SystemVerilog the non-blocking assignment <= says exactly this.
Waveforms
tb.sv in Icarus Verilog. Dotted lines are the rising edges of the
clock. Each bit of D appears on Q1 at the next edge and moves down one row per edge, Q1 to Q8. After 20 bits a
reset clears all eight stages at once. · SVG · PNG3Parts
| Part | In this circuit | Qty | Price | Supplier |
|---|---|---|---|---|
| CD4015 | dual 4-bit shift register: both halves, joined into 8 bits | 1 | $0.35 | Elexp (elexp.com) |
| 555 clock module | the clock, set to about 1 Hz | 1 | $0.53 | aliexpress.us |
| Button modules | D and reset; two modules from the pack of five | 2 | $0.66 / pack | aliexpress.us |
| 8-LED module | shows all eight bits | 1 | $0.38 | aliexpress.us |
| Breadboard, 830 points | 1 | $3.00 | amazon.com | |
| Breadboard power module | 5 V on the rails | 1 | $1.00 | aliexpress.us |
| USB-to-DC cable, 9 V | powers the module from a USB charger | 1 | $1.17 | aliexpress.us |
| Male-male Dupont jumpers | 9 wires on the board (or pre-cut wires) | 9 | $1.30 / pack | aliexpress.us |
| Male-female Dupont jumpers | 18 leads to the modules | 18 | $1.30 / pack | aliexpress.us |
Prices from the LSSK components sheet; all of these parts are in the Basics kit. The 4015 needs no tie-off wires: all its inputs are used.
4The chip, pin by pin
The 4015 is CMOS 4000B series: it works 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; here every input is used, so there is nothing to tie off. The drawing shows the chip from above, the way it sits on the breadboard: notch on the left, pin 1 at the bottom left, pins numbered counter-clockwise. Blue arrows are inputs, orange arrows are outputs.
4015: dual 4-stage shift register
Polarity. Each register shifts on the rising edge of its CLOCK. RESET is active high and asynchronous: while it is 1, all four stages of that register are 0. All outputs are active high.
The pins of the two registers are mixed over both rows: register A has its outputs Q1A–Q3A at the bottom and Q4A at the top, register B the other way round. That is why the breadboard needs wires from the top row to the bottom half for the LED module.
| Pin | Name | Direction | Function | In this circuit |
|---|---|---|---|---|
| 1 | CLOCK B | input | register B shifts on the rising edge | CLK |
| 2 | Q4B | output | register B, stage 4 (the oldest bit) | Q8 |
| 3 | Q3A | output | register A, stage 3 | Q3 |
| 4 | Q2A | output | register A, stage 2 | Q2 |
| 5 | Q1A | output | register A, stage 1 (the newest bit) | Q1 |
| 6 | RESET A | input | 1 clears register A at once, without the clock. Active high | RST |
| 7 | DATA A | input | register A, serial input | D |
| 8 | VSS | power | ground, 0 V | GND |
| 9 | CLOCK A | input | register A shifts on the rising edge | CLK |
| 10 | Q4A | output | register A, stage 4 (the oldest bit) | Q4 |
| 11 | Q3B | output | register B, stage 3 | Q7 |
| 12 | Q2B | output | register B, stage 2 | Q6 |
| 13 | Q1B | output | register B, stage 1 | Q5 |
| 14 | RESET B | input | 1 clears register B at once. Active high | RST |
| 15 | DATA B | input | register B, serial input | Q4 |
| 16 | VDD | power | supply, +5 V (3 to 18 V allowed) | +5V |
5Schematic 1: eight flip-flops
shift_register.sv describes. · SVG · PNGThe output of each flip-flop is the input of the next one and also drives an LED. The wire from FF4 to FF5 is the one wire on the breadboard that joins the two halves of the chip, from Q4A to DATA B.
6Schematic 2: connections of the chip
Every connection of the circuit, net by net. shift_register_chips.sv in section 10 is written from
the same list.
| Net | From | To | Wire |
|---|---|---|---|
| CLK | 555 clock module, OUT | 4015 9 (CLKA), 4015 1 (CLKB) | M-F, M-M |
| RST | reset button, OUT | 4015 6 (RA), 4015 14 (RB) | M-F, M-M |
| D | button D, OUT | 4015 7 (DA) | M-F |
| Q1 | 4015 5 (Q1A) | LED module, LED 1 | M-F |
| Q2 | 4015 4 (Q2A) | LED module, LED 2 | M-F |
| Q3 | 4015 3 (Q3A) | LED module, LED 3 | M-F |
| Q4 | 4015 10 (Q4A) | 4015 15 (DB), LED module, LED 4 | M-F, M-M |
| Q5 | 4015 13 (Q1B) | LED module, LED 5 | M-F, M-M |
| Q6 | 4015 12 (Q2B) | LED module, LED 6 | M-F, M-M |
| Q7 | 4015 11 (Q3B) | LED module, LED 7 | M-F, M-M |
| Q8 | 4015 2 (Q4B) | LED module, LED 8 | M-F |
| +5V | +5V | 4015 16 (VDD) | |
| GND | GND | 4015 8 (VSS) |
7Schematic 3: the breadboard
Build it step by step
- 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.
- Insert the 4015 across the gap, notch to the left, pin 1 in hole 18f.
- Power: a red wire from pin 16 to the top + rail, a black wire from pin 8 to the bottom − rail.
- The cascade wire from Q4A (pin 10) to DATA B (pin 15), then the clock wire from CLOCK A to CLOCK B and the reset wire from RESET A to RESET B.
- The four wires that bring Q4A, Q1B, Q2B and Q3B down to columns 15, 28, 29 and 30.
- With the power off, the modules: the clock module above the board, the reset button, button D and the LED module below. Connect − and G first.
- Turn the power on and set the clock module to about 1 Hz.
Wiring checklist
| # | Net | From | To | |
|---|---|---|---|---|
| Power (red to +, black to −) · 2 wires | ||||
| 1 | +5V | 18a (4015 pin 16, VDD) | top + rail, column 18 | |
| 2 | GND | 25j (4015 pin 8, VSS) | bottom − rail, column 25 | |
| Signal wires · 7 wires | ||||
| 3 | CLK | 25d (4015 pin 9, CLKA) | 18g (4015 pin 1, CLKB) | |
| 4 | RST | 23g (4015 pin 6, RA) | 20c (4015 pin 14, RB) | |
| 5 | Q4 | 24d (4015 pin 10, Q4A) | 19d (4015 pin 15, DB) | |
| 6 | Q4 | 19c (4015 pin 15, DB) | 15f | |
| 7 | Q5 | 21c (4015 pin 13, Q1B) | 28f | |
| 8 | Q6 | 22b (4015 pin 12, Q2B) | 29f | |
| 9 | Q7 | 23a (4015 pin 11, Q3B) | 30f | |
| Modules on male-female jumpers · 18 wires | ||||
| 10 | GND | 555 clock module, − | top − rail, column 25 | |
| 11 | CLK | 555 clock module, OUT | 25a (4015 pin 9, CLKA) | |
| 12 | +5V | 555 clock module, + | top + rail, column 27 | |
| 13 | +5V | reset button, + | bottom + rail, column 8 | |
| 14 | RST | reset button, OUT | 23j (4015 pin 6, RA) | |
| 15 | GND | reset button, − | bottom − rail, column 10 | |
| 16 | +5V | button D, + | bottom + rail, column 13 | |
| 17 | D | button D, OUT | 24j (4015 pin 7, DA) | |
| 18 | GND | button D, − | bottom − rail, column 15 | |
| 19 | GND | LED module, G | bottom − rail, column 22 | |
| 20 | Q1 | LED module, LED 1 | 22j (4015 pin 5, Q1A) | |
| 21 | Q2 | LED module, LED 2 | 21j (4015 pin 4, Q2A) | |
| 22 | Q3 | LED module, LED 3 | 20j (4015 pin 3, Q3A) | |
| 23 | Q4 | LED module, LED 4 | 15j | |
| 24 | Q5 | LED module, LED 5 | 28j | |
| 25 | Q6 | LED module, LED 6 | 29j | |
| 26 | Q7 | LED module, LED 7 | 30j | |
| 27 | Q8 | LED module, LED 8 | 19j (4015 pin 2, Q4B) | |
Test it
- Press reset: all eight LEDs go dark.
- Hold D across one blink and let go: LED 1 lights, then the light walks one LED per blink to LED 8 and disappears.
- Hold D for three blinks: a group of three lit LEDs walks along.
- Press reset while the light is walking: all LEDs go dark at once.
If it does not work: if the light stops at LED 4, the cascade wire from Q4A to DATA B is missing or in the wrong hole. If LEDs 5–8 never light but 1–4 work, check the clock wire to CLOCK B (pin 1) and RESET B (pin 14). If the order of the LEDs is mixed up, compare the LED leads with the checklist: the outputs of the 4015 are not in pin order.
8SystemVerilog: the generic design
shift_register.sv describes schematic 1. The concatenation { q [6:0], d } is the whole
chain: bits 6 to 0 move up to 7 to 1, and d comes in at bit 0. With the non-blocking
<=, all eight bits change together at the edge. It is synthesizable: eight flip-flops and no
gates.
// Exercise B6: an 8-bit shift register, serial input, parallel output. // // Generic version that matches schematic 1: eight D flip-flops in a chain, // with a common clock and an asynchronous reset, not tied to any chip. // Synthesizable. module shift_register ( input logic clk, input logic rst, // 1 = all bits go to 0 at once, like RESET A and B of the 4015 input logic d, // The serial input output logic [7:0] q // q [0] is the newest bit, q [7] the oldest ); // Every clock the bits move one place up and d comes in at the bottom always_ff @ (posedge clk or posedge rst) if (rst) q <= '0; else q <= { q [6:0], d }; endmodule
9SystemVerilog model of the chip
One module with ports named as in the datasheet and the pin number of each port in a comment. The model is synthesizable; it leaves out the power pins, because in SystemVerilog a wire simply has the value 0 or 1.
// CD4015B: dual 4-stage shift register, serial input, parallel output. // // Ports in pin order, named as in the datasheet. The power pins, VSS (8) // and VDD (16), are left out. Synthesizable. // // Each register shifts on the rising edge of its clock: DATA goes into Q1, // Q1 into Q2, Q2 into Q3, Q3 into Q4. RESET is active high and // asynchronous: it clears all four stages at once, without the clock. // Connect Q4A to DATA B to make one 8-stage register. module cd4015 ( input logic clock_b, // Pin 1 output logic q4b, // Pin 2 output logic q3a, // Pin 3 output logic q2a, // Pin 4 output logic q1a, // Pin 5 input logic reset_a, // Pin 6: 1 = Q1A-Q4A go to 0 at once input logic data_a, // Pin 7 // Pin 8: VSS input logic clock_a, // Pin 9 output logic q4a, // Pin 10 output logic q3b, // Pin 11 output logic q2b, // Pin 12 output logic q1b, // Pin 13 input logic reset_b, // Pin 14: 1 = Q1B-Q4B go to 0 at once input logic data_b // Pin 15 // Pin 16: VDD ); logic [4:1] ra, rb; // ra [1] is Q1A, ra [4] is Q4A always_ff @ (posedge clock_a or posedge reset_a) if (reset_a) ra <= '0; else ra <= { ra [3:1], data_a }; always_ff @ (posedge clock_b or posedge reset_b) if (reset_b) rb <= '0; else rb <= { rb [3:1], data_b }; assign { q4a, q3a, q2a, q1a } = ra; assign { q4b, q3b, q2b, q1b } = rb; endmodule
10SystemVerilog: the chip-level design
shift_register_chips.sv connects the chip model exactly as schematic 2 and the breadboard connect the
chip, with the same ports as shift_register.sv. Q4A drives both q [3] and DATA B, as on the
breadboard. 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.
// Exercise B6, chip-level version: the chip and wires of schematic 2 and // of the breadboard. Same ports as shift_register.sv. // // Generated by tools/bb_lib.py from the checked breadboard wire list; // change the layout in tools/bb_b6.py, not this file. Synthesizable. module shift_register_chips ( input logic clk, input logic rst, input logic d, output logic [7:0] q ); //------------------------------------------------------------------------ // U1: CD4015 dual 4-stage shift register, serial in, parallel out, pin 1 in column 18 cd4015 i_u1 ( .clock_b ( clk ), // Pin 1 <- 555 clock module .q4b ( q [7] ), // Pin 2 -> Q8 .q3a ( q [2] ), // Pin 3 -> Q3 .q2a ( q [1] ), // Pin 4 -> Q2 .q1a ( q [0] ), // Pin 5 -> Q1 .reset_a ( rst ), // Pin 6 <- reset button .data_a ( d ), // Pin 7 <- button D .clock_a ( clk ), // Pin 9 <- 555 clock module .q4a ( q [3] ), // Pin 10 -> Q4 .q3b ( q [6] ), // Pin 11 -> Q7 .q2b ( q [5] ), // Pin 12 -> Q6 .q1b ( q [4] ), // Pin 13 -> Q5 .reset_b ( rst ), // Pin 14 <- reset button .data_b ( q [3] ) // Pin 15 <- U1 pin 10 (Q4A) ); endmodule
11Testbench and simulation scripts
tb.sv runs both designs side by side from the same clock and reset and compares them with a reference
model: the last eight values of d, one per clock edge since the last reset. Like the testbenches of
Basics-Graphics-Music, it drives and checks signals only at rising clock edges. It shifts in a 28-bit pattern, as if
you pressed the button during some clock periods and not others, and resets the register after 20 bits: both
designs must clear at once, not at the next edge. At the end it prints PASS or FAIL.
tb.sv182 lines · show the codefile
// Testbench for exercise B6. // // Runs the generic design (shift_register.sv) and the chip-level design // (shift_register_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. It shifts in a bit pattern, checks // that every bit comes out of q [k] k + 1 clock edges after it went in, and // resets the register in the middle of the run. // 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 rst; logic d; wire [7:0] q, q_chips; shift_register i_generic ( .clk ( clk ), .rst ( rst ), .d ( d ), .q ( q ) ); shift_register_chips i_chips ( .clk ( clk ), .rst ( rst ), .d ( d ), .q ( q_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 last eight values of d, one per clock edge since // the last reset, the newest at the bottom. q [k] must equal the bit // that came in k + 1 edges ago. logic [7:0] history; int n_in; // Bits shifted in since the last reset always @ (posedge clk or posedge rst) if (rst) begin history <= '0; n_in <= 0; end else begin history <= { history [6:0], d }; n_in <= n_in + 1; end wire [7:0] q_expected = history [7:0]; //------------------------------------------------------------------------ // 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. // Initial values are fine in a testbench, and the checker samples rst at // the clock edge on purpose, although the designs reset asynchronously // verilator lint_off PROCASSINIT // verilator lint_off SYNCASYNCNET logic was_reset = 1'b0; logic done = 1'b0; // Set by the stimulus before $finish int cycle = 0; int n_checks = 0; int n_errors = 0; int max_in = 0; // The longest run without a reset always @ (posedge clk) begin if (rst) was_reset <= 1'b1; if ((rst | was_reset) & ~ done) begin cycle <= cycle + 1; n_checks <= n_checks + 2; n_errors <= n_errors + (q === q_expected ? 0 : 1) + (q_chips === q_expected ? 0 : 1); max_in <= n_in > max_in ? n_in : max_in; $display ("%5d %b %b %b %s %s", cycle + 1, rst, d, q, q === q_expected ? "ok " : "WRONG ", q_chips === q_expected ? "ok " : "WRONG"); end end //------------------------------------------------------------------------ // Driving the inputs: a pattern of bits, one per clock, as if you pressed // the D button during some clock periods and not during others localparam n_bits = 28; wire [n_bits - 1:0] pattern = 28'b1011_0011_1000_0110_1111_0100_1101; // From the left initial begin $dumpfile ("tb.vcd"); `ifdef __ICARUS__ $dumpvars (0, tb); // Without a scope Icarus also tries to dump $unit `else $dumpvars; `endif $display ("cycle rst d q [7:0] generic chips"); // Before reset the register is unknown, as after power-up d <= '0; rst <= 'x; repeat (3) @ (posedge clk); rst <= '1; repeat (3) @ (posedge clk); rst <= '0; for (int i = 0; i < n_bits; i ++) begin d <= pattern [n_bits - 1 - i]; @ (posedge clk); // Reset in the middle of the run. Both designs must clear at // once, not at the next clock edge, like RESET of the 4015. if (i == 19) begin rst <= '1; @ (posedge clk); rst <= '0; end end d <= '0; repeat (4) @ (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 && max_in >= 16) $display ("PASS: %0d checks, no errors", n_checks); else $display ("FAIL: %0d errors in %0d checks, longest run %0d bits", n_errors, n_checks, max_in); 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 B6: 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 shift_register.sv shift_register_chips.sv cd4015.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 B6: 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 shift_register.sv shift_register_chips.sv cd4015.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 B6: 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 shift_register.sv shift_register_chips.sv cd4015.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; q [7:0] is printed with Q8 on the left, so the
bits move left in the trace:
VCD info: dumpfile tb.vcd opened for output.
cycle rst d q [7:0] generic chips
1 1 0 00000000 ok ok
2 1 0 00000000 ok ok
3 1 0 00000000 ok ok
4 0 1 00000000 ok ok
5 0 0 00000001 ok ok
6 0 1 00000010 ok ok
7 0 1 00000101 ok ok
8 0 0 00001011 ok ok
9 0 0 00010110 ok ok
10 0 1 00101100 ok ok
11 0 1 01011001 ok ok
12 0 1 10110011 ok ok
13 0 0 01100111 ok ok
14 0 0 11001110 ok ok
15 0 0 10011100 ok ok
16 0 0 00111000 ok ok
17 0 1 01110000 ok ok
18 0 1 11100001 ok ok
19 0 0 11000011 ok ok
20 0 1 10000110 ok ok
21 0 1 00001101 ok ok
22 0 1 00011011 ok ok
23 0 1 00110111 ok ok
24 1 1 00000000 ok ok
25 0 0 00000000 ok ok
26 0 1 00000000 ok ok
27 0 0 00000001 ok ok
28 0 0 00000010 ok ok
29 0 1 00000100 ok ok
30 0 1 00001001 ok ok
31 0 0 00010011 ok ok
32 0 1 00100110 ok ok
33 0 0 01001101 ok ok
34 0 0 10011010 ok ok
35 0 0 00110100 ok ok
36 0 0 01101000 ok ok
tb.sv:169: $finish called at 395000 (1ps)
PASS: 72 checks, no errors
To see that the testbench catches mistakes, break something and run it again: in
shift_register_chips.sv connect data_b to q [2] instead of q [3], or
remove or posedge reset_b in cd4015.sv. The script reports FAIL and the trace
shows the cycles 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. BGM has its own lab on the
subject, 1_07_shift_register.
- Copy
labs/1_basics/1_07_shift_registerto a new directory. - Replace its
lab_top.svwithbgm_lab/lab_top.svfrom this exercise and addshift_register.sv. - Run
./03_synthesize_for_fpga.bashand./04_configure_fpga.bash; the first run asks which board you have.
The register shifts on slow_clk, which every BGM board makes at 1 Hz. 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. Key 0 is D, the reset button of the board clears the register, and
the LEDs show the eight bits.
bgm_lab/lab_top.sv97 lines · show the codefile
// Exercise B6 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 shift_register.sv. For the chip-level // design add shift_register_chips.sv and cd4015.sv instead, and change the // module name below. // // The register shifts on slow_clk, the 1 Hz clock that every BGM board // wrapper makes, like the breadboard with the 555 module. Key 0 is D, the // reset button of the board clears the register. The LEDs show the eight // bits; a board with fewer LEDs shows the first ones. `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 [7:0] q; shift_register i_shift_register ( .clk ( slow_clk ), .rst ( rst ), .d ( key [0] ), .q ( q ) ); assign led = w_led' (q); 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.
- Copy
hackathon/problems/5_shift_register. - Replace its
hackathon_top.svwithbgm_hackathon/hackathon_top.svand addshift_register.sv. - Run
./03_synthesize_for_fpga.bashand./04_configure_fpga.bash.
Any key is D. The eight LEDs and a row of eight squares on the screen show the bits, Q1 on the left.
bgm_hackathon/hackathon_top.sv74 lines · show the codefile
// Board configuration: tang_nano_9k_lcd_480_272_tm1638_hackathon // This module uses few parameterization and relaxed typing rules // Exercise B6 in the hackathon setup of Basics-Graphics-Music (BGM): // https://github.com/yuri-panchul/basics-graphics-music // // Copy hackathon/problems/5_shift_register, replace its hackathon_top.sv // with this file and add shift_register.sv. // // The register shifts on slow_clock, one beat per second, like the // breadboard with the 555 module. Any key is D. The LEDs and a row of // eight squares on the screen show the bits; Q1, the newest, is on the left. 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 [7:0] q; shift_register i_shift_register ( .clk ( slow_clock ), .rst ( reset ), .d ( | key ), .q ( q ) ); assign led = q; assign abcdefgh = '0; assign digit = '0; //------------------------------------------------------------------------ // Eight squares on the 480 x 272 screen, bright orange for 1, dim for 0 always_comb begin red = 0; green = 0; blue = 0; for (int i = 0; i < 8; i ++) if (x >= 9' (24 + i * 56) & x < 9' (64 + i * 56) & y >= 116 & y < 156) begin red = q [i] ? 31 : 6; green = q [i] ? 40 : 8; 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 register, just as the 555 module clocks the 4015 on
the breadboard. Switch 0 is D. The eight bits light the seven segments and the dot, so a 1 runs around the digit: a,
b, c, d, e, f, g, dot.
tiny_tapeout/project.v49 lines · show the codefile
/* * Exercise B6 on Tiny Tapeout: an 8-bit shift register. Switch 0 is D; * the eight bits light the seven segments and the dot of the demo board. * * Add shift_register.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 ); // Set the project clock to 1 Hz (clock_hz: 1 in info.yaml): the demo // board makes it, and clk drives the register directly, like the 555 // module on the breadboard. The reset button of the board is rst_n. wire [7:0] q; shift_register i_shift_register ( .clk ( clk ), .rst ( ~ rst_n ), .d ( ui_in [0] ), .q ( q ) ); // 7-segment display: uo_out [0] is segment a, ..., uo_out [6] is g, // uo_out [7] is the dot. Bit k lights segment k: a 1 entered with // switch 0 runs a, b, c, d, e, f, g and the dot. assign uo_out = q; assign uio_out = 8'b0; assign uio_oe = 8'b0; // List all unused inputs to prevent warnings wire _unused = & { ena, ui_in [7:1], uio_in, 1'b0 }; endmodule
In info.yaml set the clock to 1 Hz, list both files and use the module name as the top
module:
project:
clock_hz: 1 # the demo board makes a 1 Hz clock
top_module: "tt_um_example" # rename to tt_um_<your GitHub name>_shift_register
source_files:
- "project.v"
- "shift_register.sv"
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. Here the board makes the
1 Hz clock, so no divider is needed; on a chip that needs a slow tick from a fast clock, keep one clock and add a
clock enable.15Going further
Tasks for students
- Two separate registers. Remove the cascade wire and give DATA B its own button: now there are two 4-bit registers on one clock. Which LEDs belong to which?
- A ring. Feed Q4B back to DATA A, so that every bit that leaves Q8 comes back in at Q1. How do you still enter bits with the button? (Hint: an OR gate of the 4071 from exercise B1.) Enter a single 1 and watch it circle.
- A Johnson counter (exercise M10): feed Q4A back to DATA A through a 4069 inverter. Four flip-flops now count through eight states, like the five of the 4017 count through ten.
- A sequence detector. With a free AND gate, light an LED when Q1, Q2 and Q3 are all 1: the last three presses were all 1. The FSM exercises do the same with fewer flip-flops.
- In SystemVerilog: add a parallel load, so that the register can also take a whole byte at once: the reverse conversion, parallel in, serial out, which the 4014 does.
Files of this exercise
- shift_register.sv: the generic design (section 8)
- cd4015.sv: the chip model (section 9)
- shift_register_chips.sv: the chip-level design (section 10)
- tb.sv and run_icarus.bash, run_verilator.bash, run_questa.bash, clean.bash (section 11)
- bgm_lab/lab_top.sv, bgm_hackathon/hackathon_top.sv, tiny_tapeout/project.v (sections 12–14)
- Drawings as SVG and PNG in images/: the three schematics, the pinout and the waveforms;
the Russian versions end in
_ru
Sources
The exercise is the fourth of the Basics list in “An improved way of teaching school kids digital electronics using venerable small-scale integration ICs”; lesson 1.3 of the Habr article about the electronics course describes the class with the 4015, a button and the LED module. The 4015 is described in the Texas Instruments datasheet for the CD4015B.