← All exercisesРусский
LSSK · C11

Single FIFO with clock, push/pop and four data switches

1. Purpose and prerequisites

Set a 4-bit word with four self-locking TTP223 switches. PUSH and POP are requests sampled with the assembled clock; observe Q[3:0], EMPTY and FULL.

This lesson connects a circuit’s logic contract to physical package pins and an RTL implementation. Start with the power/breadboard guide and the SystemVerilog rules.

Use regulated 5 V at the logic rails and a common ground. A USB-to-9 V cable feeds the regulator input, never a logic pin. Begin at a slow clock. The supplied simulations are ideal digital checks; these assemblies have not been measured on hardware.

2. Behavior, symbols and observations

Exactly 4 bits × 16 words, with no external occupancy counter. At each rising CLK, capture DATA and accepted PUSH/POP using pre-edge flags. During that same period’s falling edge, SI rises and SO falls for accepted operations. Requests can be accepted on every consecutive clock.

Use the inventory’s CD74HC40105E. Legacy CD40105 parts may have different SO polarity. Empty blocks POP; full blocks PUSH, even if the opposite operation is requested simultaneously. Empty Q is undefined. Check flags/data after the falling edge has settled and before the next rising edge. This physical implementation has half-cycle latency relative to the ideal rising-edge RTL; their continuous-time waveforms are not identical.

Clock-low pulse widths and the half-cycle settling interval must satisfy the HC datasheet, including ripple propagation through all 16 stages. Start at approximately 1 Hz. The supplied pin model checks logical ordering only; it does not establish a maximum hardware frequency.

PortDirectionBits
clkinput1
rstinput1
datainput4
pushinput1
popinput1
qoutput4
emptyoutput1
fulloutput1
Single FIFO with clock, push/pop and four data switches · timing
SVG · PNG

3. Parts you need

PartQuantityLSSK row
CD4081B · DIP110
CD4069UB · DIP124
CD40174B · DIP126
CD4071B · DIP120
CD74HC40105 · DIP136
830-contact breadboard245
5 V regulated power module156
USB-to-9 V cable to regulator input157
Qualified button channels (sets of five)766
Assembled 555 clock module154
5 V current-limited LED646
M–M jumper5470
External-module lead (M–F assumed)3871

CSV · Jumper schedule · Pin/net data

Counts describe this exact layout, without spare jumpers. The button quantity counts independent channels: buy 2 sets of five if using that source listing. Qualify the module’s 0/5 V output and built-in bias first; a bare switch is not equivalent. External connector genders and shared module power terminals must be adapted to the actual part.

4. Chip functions, pins and polarity

CD40174B

Six D flip-flops, clear LOW · Manufacturer datasheet

PinSignalType
1clear_ninput
2q0output
3d0input
4d1input
5q1output
6d2input
7q2output
8vsssupply
9clkinput
10q3output
11d3input
12q4output
13d4input
14d5input
15q5output
16vddsupply

CD4069UB

CD4069UB · Manufacturer datasheet

PinSignalType
1a1input
2y1output
3a2input
4y2output
5a3input
6y3output
7vsssupply
8y4output
9a4input
10y5output
11a5input
12y6output
13a6input
14vddsupply

CD4071B

CD4071B · Manufacturer datasheet

PinSignalType
1a1input
2b1input
3y1output
4y2output
5b2input
6a2input
7vsssupply
8a3input
9b3input
10y3output
11y4output
12b4input
13a4input
14vddsupply

CD4081B

CD4081B · Manufacturer datasheet

PinSignalType
1a1input
2b1input
3y1output
4y2output
5b2input
6a2input
7vsssupply
8a3input
9b3input
10y3output
11y4output
12b4input
13a4input
14vddsupply

CD74HC40105

4 × 16 asynchronous FIFO · Manufacturer datasheet

PinSignalType
1tsinput
2diroutput
3siinput
4d0input
5d1input
6d2input
7d3input
8vsssupply
9rstinput
10q3output
11q2output
12q1output
13q0output
14doroutput
15soinput
16vddsupply

A suffix _n means active LOW. Power pins are physical only and omitted from simulation models. NC means unconnected; it is never a command to ground an output.

5. Schematic 1 · Generic logic

Single FIFO with clock, push/pop and four data switches · logic
SVG · PNG

Net labels join the drawing without long crossing wires. Logic gates and state/storage blocks have no package dependency in this view. The RTL in section 8 defines the behavior of every block.

6. Schematic 2 · Chip connections

Single FIFO with clock, push/pop and four data switches · connections
SVG · PNG
DevicePinSignalNet
U11a1push
U12b1f_dir
U13y1n1
U14y2n2
U15b2f_dor
U16a2pop
U17vssGND
U18a3n4
U19b3f_held[4]
U110y3n5
U111y4NC
U112b4GND
U113a4GND
U114vdd+5V
U21a1rst
U22y1n3
U23a2clk
U24y2n4
U25a3f_held[5]
U26y3n6
U27vssGND
U28y4full
U29a4f_dir
U210y5empty
U211a5f_dor
U212y6NC
U213a6GND
U214vdd+5V
U31clear_nn3
U32q0f_held[0]
U33d0data[0]
U34d1data[1]
U35q1f_held[1]
U36d2data[2]
U37q2f_held[2]
U38vssGND
U39clkclk
U310q3f_held[3]
U311d3data[3]
U312q4f_held[4]
U313d4n1
U314d5n2
U315q5f_held[5]
U316vdd+5V
U41a1clk
U42b1n6
U43y1n7
U44y2NC
U45b2GND
U46a2GND
U47vssGND
U48a3GND
U49b3GND
U410y3NC
U411y4NC
U412b4GND
U413a4GND
U414vdd+5V
U51tsGND
U52dirf_dir
U53sin5
U54d0f_held[0]
U55d1f_held[1]
U56d2f_held[2]
U57d3f_held[3]
U58vssGND
U59rstrst
U510q3q[3]
U511q2q[2]
U512q1q[1]
U513q0q[0]
U514dorf_dor
U515son7
U516vdd+5V

7. Schematic 3 · Breadboard and assembly

Single FIFO with clock, push/pop and four data switches · breadboard
SVG · PNG
  1. Disconnect power. Bridge each split power rail as shown; verify that +5 V and GND are separate.
  2. Place each DIP across the center gap with its notch LEFT and pin 1 BELOW the gap. Pin diagrams in section 6 instead show the notch UP.
  3. Wire supplies, fixed controls and unused inputs first. Follow jumpers.csv from W001 onward; matching wire labels across boards denote one wire.
  4. Connect data/control modules, then the indicators and clock. Do not leave CMOS inputs floating. Start/reset/load as specified in section 2.
  5. Reset with PUSH=POP=0. Push 0…15 on sixteen consecutive clocks; verify FULL. Attempt an extra push, then pop all sixteen values. Repeat push,push,push,push+pop,push+pop,pop and test simultaneous requests at empty/full.

LEDs are assigned only to outputs that do not also feed another CMOS input. Feedback and inter-chip data nodes are observed in simulation or with a high-impedance probe; a low-current indicator is not assumed to be a logic buffer.

Connectivity check verifies connected nets, isolated outputs, occupied holes and split-rail bridges. This checks the drawing’s topology, not electrical timing or contact quality.

Complete jumper list
IDNetFromTo
W001+5VB1.TP25B1.TP26
W002GNDB1.TG25B1.TG26
W003+5VB1.BP25B1.BP26
W004GNDB1.BG25B1.BG26
W005+5VB1.TP1B1.BP1
W006GNDB1.TG1B1.BG1
W007+5VB2.TP25B2.TP26
W008GNDB2.TG25B2.TG26
W009+5VB2.BP25B2.BP26
W010GNDB2.BG25B2.BG26
W011+5VB2.TP1B2.BP1
W012GNDB2.TG1B2.BG1
W013+5VB1.BP50B2.TP50
W014GNDB1.BG50B2.TG50
W015pushB1.j5S6.OUT
W016f_dirB1.j6B1.a24
W017f_dirB1.b24B2.j6
W018n1B1.j7B1.a36
W019n2B1.j8B1.a35
W020f_dorB1.j9B1.a22
W021f_dorB1.b22B2.a7
W022popB1.j10S7.OUT
W023GNDB1.j11B1.BG2
W024GNDB1.a7B1.TG2
W025GNDB1.a6B1.TG3
W026GNDB1.j25B1.BG3
W027GNDB1.a20B1.TG4
W028GNDB1.j40B1.BG4
W029GNDB1.j51B1.BG5
W030GNDB1.j52B1.BG6
W031GNDB1.j53B1.BG7
W032GNDB1.a53B1.TG5
W033GNDB1.a52B1.TG6
W034GNDB1.a49B1.TG7
W035GNDB1.a48B1.TG8
W036GNDB2.j5B2.BG2
W037GNDB2.j12B2.BG3
W038GNDS1.GNDB1.BG8
W039GNDS2.GNDB2.BG4
W040GNDS3.GNDB1.BG9
W041GNDS4.GNDB2.BG5
W042GNDS5.GNDB1.BG10
W043GNDS6.GNDB2.BG6
W044GNDS7.GNDB1.BG11
W045GNDCLK.GNDB1.BG12
W046GNDPWR.GNDB1.BG13
W047GNDL1.KB1.BG14
W048GNDL2.KB2.BG7
W049GNDL3.KB1.BG15
W050GNDL4.KB2.BG8
W051GNDL5.KB1.BG16
W052GNDL6.KB2.BG9
W053n4B1.a11B1.j22
W054f_held[4]B1.a10B1.a37
W055n5B1.a9B2.j7
W056+5VB1.a5B1.TP2
W057+5VB1.a19B1.TP3
W058+5VB1.a33B1.TP4
W059+5VB1.a47B1.TP5
W060+5VB2.a5B2.TP2
W061+5VS1.VCCB1.BP2
W062+5VS2.VCCB2.BP2
W063+5VS3.VCCB1.BP3
W064+5VS4.VCCB2.BP3
W065+5VS5.VCCB1.BP4
W066+5VS6.VCCB2.BP4
W067+5VS7.VCCB1.BP5
W068+5VCLK.VCCB1.BP6
W069+5VPWR.+5VB1.BP7
W070rstB1.j19B2.a12
W071rstB2.b12S1.OUT
W072n3B1.j20B1.j33
W073clkB1.j21B1.a40
W074clkB1.b40B1.j47
W075clkB1.i47CLK.OUT
W076f_held[5]B1.j23B1.a34
W077n6B1.j24B1.j48
W078fullB1.a25L6.A
W079emptyB1.a23L5.A
W080f_held[0]B1.j34B2.j8
W081data[0]B1.j35S2.OUT
W082data[1]B1.j36S3.OUT
W083f_held[1]B1.j37B2.j9
W084data[2]B1.j38S4.OUT
W085f_held[2]B1.j39B2.j10
W086f_held[3]B1.a39B2.j11
W087data[3]B1.a38S5.OUT
W088n7B1.j49B2.a6
W089q[3]B2.a11L4.A
W090q[2]B2.a10L3.A
W091q[1]B2.a9L2.A
W092q[0]B2.a8L1.A

8. Generic synthesizable SystemVerilog

rtl/fifo_4x16.sv

Download

// Synthesizable generic design.

module fifo_4x16
(
    input              clk,
    input              rst,
    input        [3:0] data,
    input              push,
    input              pop,
    output logic [3:0] q,
    output             empty,
    output             full
);

    logic [15:0] valid;
    logic [3:0] words [0:15];
    wire put  = push & ~ full;
    wire take = pop & ~ empty;

    always_ff @ (posedge clk or posedge rst)
        if (rst)
            valid <= '0;
        else
            case ({ put, take })
                2'b10: valid   <= { valid [14:0], 1'b1 };
                2'b01: valid   <= { 1'b0, valid [15:1] };
                default: valid <= valid;
            endcase

    always_ff @ (posedge clk)
        if (put)
        begin
            words [0] <= data;
            for (int i = 1; i < 16; i ++)
                words [i] <= words [i - 1];
        end

    always_comb
    begin
        q = words [0];
        for (int i = 0; i < 16; i ++)
            if (valid [i])
                q = words [i];
    end
    assign empty = ~ valid [0];
    assign full  = valid [15];

endmodule

RTL contains no timescale declaration. State uses always_ff and nonblocking assignments; combinational behavior uses assign/always_comb. Four-space indentation follows the local style rules.

9. CMOS chip models

Read each model’s opening comment: it states whether the model is synthesizable and which tied-pin subset it covers. These are functional teaching models, not transistor-level models or manufacturer timing libraries.

The HC FIFO pin model is simulation-only. Synthesize the generic FIFO for FPGA/ASIC work. The physical design needs no external occupancy counter.

Why the asynchronous pin model uses an event-sensitive process

models/cd40174b.sv

Download

// Synthesizable ideal digital model; power and propagation delays omitted.

module cd40174b
(
    input  p1_clear_n,
    input  p3_d0,
    input  p4_d1,
    input  p6_d2,
    input  p9_clk,
    input  p11_d3,
    input  p13_d4,
    input  p14_d5,
    output p2_q0,
    output p5_q1,
    output p7_q2,
    output p10_q3,
    output p12_q4,
    output p15_q5
);

    logic [5:0] state;
    always_ff @ (posedge p9_clk or negedge p1_clear_n)
        if (~ p1_clear_n)
            state <= '0;
        else
            state <= { p14_d5, p13_d4, p11_d3, p6_d2, p4_d1, p3_d0 };
    assign { p15_q5, p12_q4, p10_q3, p7_q2, p5_q1, p2_q0 } = state;

endmodule
models/cd4069ub.sv

Download

// Synthesizable ideal digital model; power and propagation delays omitted.

module cd4069ub
(
    input  p1_a1,
    input  p3_a2,
    input  p5_a3,
    input  p9_a4,
    input  p11_a5,
    input  p13_a6,
    output p2_y1,
    output p4_y2,
    output p6_y3,
    output p8_y4,
    output p10_y5,
    output p12_y6
);

    assign p2_y1  = ~ p1_a1;
    assign p4_y2  = ~ p3_a2;
    assign p6_y3  = ~ p5_a3;
    assign p8_y4  = ~ p9_a4;
    assign p10_y5 = ~ p11_a5;
    assign p12_y6 = ~ p13_a6;

endmodule
models/cd4071b.sv

Download

// Synthesizable ideal digital model; power and propagation delays omitted.

module cd4071b
(
    input  p1_a1,
    input  p2_b1,
    input  p5_b2,
    input  p6_a2,
    input  p8_a3,
    input  p9_b3,
    input  p12_b4,
    input  p13_a4,
    output p3_y1,
    output p4_y2,
    output p10_y3,
    output p11_y4
);

    assign p3_y1  = p1_a1 | p2_b1;
    assign p4_y2  = p6_a2 | p5_b2;
    assign p10_y3 = p8_a3 | p9_b3;
    assign p11_y4 = p13_a4 | p12_b4;

endmodule
models/cd4081b.sv

Download

// Synthesizable ideal digital model; power and propagation delays omitted.

module cd4081b
(
    input  p1_a1,
    input  p2_b1,
    input  p5_b2,
    input  p6_a2,
    input  p8_a3,
    input  p9_b3,
    input  p12_b4,
    input  p13_a4,
    output p3_y1,
    output p4_y2,
    output p10_y3,
    output p11_y4
);

    assign p3_y1  = p1_a1 & p2_b1;
    assign p4_y2  = p6_a2 & p5_b2;
    assign p10_y3 = p8_a3 & p9_b3;
    assign p11_y4 = p13_a4 & p12_b4;

endmodule
models/cd74hc40105.sv

Download

// SIMULATION ONLY. Ideal 4x16 FIFO pin behavior at settled edges.
// No ripple delays, electrical behavior or asynchronous boundary races.
// TS is tied low; simultaneous SI rising and SO falling use pre-edge flags.

module cd74hc40105
(
    input  p1_ts,
    input  p3_si,
    input  p4_d0,
    input  p5_d1,
    input  p6_d2,
    input  p7_d3,
    input  p9_rst,
    input  p15_so,
    output p2_dir,
    output p10_q3,
    output p11_q2,
    output p12_q1,
    output p13_q0,
    output p14_dor
);

    logic [3:0] memory [0:15];
    integer read_ptr = 0, write_ptr = 0, count = 0;
    logic si_old     = 0, so_old = 1;
    logic put, take;
    always @ (p3_si or p15_so or p9_rst)
    begin
        if (p9_rst)
        begin
            read_ptr  = 0;
            write_ptr = 0;
            count     = 0;
        end
        else
        begin
            put  = p3_si & ~ si_old & (count < 16);
            take = ~ p15_so & so_old & (count > 0);
            if (put)
            begin
                memory [write_ptr] = { p7_d3, p6_d2, p5_d1, p4_d0 };
                write_ptr          = (write_ptr + 1) % 16;
            end
            if (take)
                read_ptr = (read_ptr + 1) % 16;
            count = count + int' (put) - int' (take);
        end
        si_old = p3_si;
        so_old = p15_so;
    end
    assign p2_dir                             = count < 16;
    assign p14_dor                            = count != 0;
    assign { p10_q3, p11_q2, p12_q1, p13_q0 } = memory [read_ptr];

endmodule

10. Pin-connected SystemVerilog

rtl/fifo_4x16_chips.sv

Download

// Pin connections are generated from the same netlist as the diagrams.
module fifo_4x16_chips
(
    input              clk,
    input              rst,
    input        [3:0] data,
    input              push,
    input              pop,
    output       [3:0] q,
    output             empty,
    output             full
);

    logic f_dir;
    logic f_dor;
    logic [5:0] f_held;
    logic n1;
    logic n2;
    logic n3;
    logic n4;
    logic n5;
    logic n6;
    logic n7;

    cd4081b u1
    (
        .p1_a1        (push),
        .p2_b1        (f_dir),
        .p3_y1        (n1),
        .p4_y2        (n2),
        .p5_b2        (f_dor),
        .p6_a2        (pop),
        .p8_a3        (n4),
        .p9_b3        (f_held [4]),
        .p10_y3       (n5),
        .p11_y4       (),
        .p12_b4       (1'b0),
        .p13_a4       (1'b0)
    );

    cd4069ub u2
    (
        .p1_a1        (rst),
        .p2_y1        (n3),
        .p3_a2        (clk),
        .p4_y2        (n4),
        .p5_a3        (f_held [5]),
        .p6_y3        (n6),
        .p8_y4        (full),
        .p9_a4        (f_dir),
        .p10_y5       (empty),
        .p11_a5       (f_dor),
        .p12_y6       (),
        .p13_a6       (1'b0)
    );

    cd40174b u3
    (
        .p1_clear_n   (n3),
        .p2_q0        (f_held [0]),
        .p3_d0        (data [0]),
        .p4_d1        (data [1]),
        .p5_q1        (f_held [1]),
        .p6_d2        (data [2]),
        .p7_q2        (f_held [2]),
        .p9_clk       (clk),
        .p10_q3       (f_held [3]),
        .p11_d3       (data [3]),
        .p12_q4       (f_held [4]),
        .p13_d4       (n1),
        .p14_d5       (n2),
        .p15_q5       (f_held [5])
    );

    cd4071b u4
    (
        .p1_a1        (clk),
        .p2_b1        (n6),
        .p3_y1        (n7),
        .p4_y2        (),
        .p5_b2        (1'b0),
        .p6_a2        (1'b0),
        .p8_a3        (1'b0),
        .p9_b3        (1'b0),
        .p10_y3       (),
        .p11_y4       (),
        .p12_b4       (1'b0),
        .p13_a4       (1'b0)
    );

    cd74hc40105 u5
    (
        .p1_ts        (1'b0),
        .p2_dir       (f_dir),
        .p3_si        (n5),
        .p4_d0        (f_held [0]),
        .p5_d1        (f_held [1]),
        .p6_d2        (f_held [2]),
        .p7_d3        (f_held [3]),
        .p9_rst       (rst),
        .p10_q3       (q [3]),
        .p11_q2       (q [2]),
        .p12_q1       (q [1]),
        .p13_q0       (q [0]),
        .p14_dor      (f_dor),
        .p15_so       (n7)
    );

endmodule

This top has exactly the generic top’s interface. Its connections are generated from the same pin data used for the diagrams and wiring schedule.

11. Testbenches and simulator scripts

tb/tb.sv

Download

`timescale 1ns / 1ps

module tb;

    localparam period       = 20;
    localparam vector_count = 1045;
    logic [18:0] vectors [0:vector_count-1];
    logic [5:0] expected, check_mask;
    logic clk = 1'b0;

    always # (period / 2) clk = ~ clk;

    logic rst        = '0;
    logic [3:0] data = '0;
    logic push       = '0;
    logic pop        = '0;
    logic [3:0] q_rtl;
    logic [3:0] q_chips;
    logic empty_rtl;
    logic empty_chips;
    logic full_rtl;
    logic full_chips;

    fifo_4x16 dut_rtl
    (
        .clk          (clk),
        .rst          (rst),
        .data         (data),
        .push         (push),
        .pop          (pop),
        .q            (q_rtl),
        .empty        (empty_rtl),
        .full         (full_rtl)
    );

    fifo_4x16_chips dut_chips
    (
        .clk          (clk),
        .rst          (rst),
        .data         (data),
        .push         (push),
        .pop          (pop),
        .q            (q_chips),
        .empty        (empty_chips),
        .full         (full_chips)
    );

    initial
    begin
        $readmemh ("vectors.hex", vectors);
        @ (posedge clk);
        # 1;
        for (int i = 0; i < vector_count; i ++)
        begin
            rst        = vectors [i] [0 +: 1];
            data       = vectors [i] [1 +: 4];
            push       = vectors [i] [5 +: 1];
            pop        = vectors [i] [6 +: 1];
            expected   = vectors [i] [7 +: 6];
            check_mask = vectors [i] [13 +: 6];
            @ (posedge clk);
            # 1;  // Allow nonblocking state and combinational outputs to settle.
            @ (negedge clk);
            # 1;  // HC FIFO commands commit in this half-cycle.
            if (({ full_rtl, empty_rtl, q_rtl } & check_mask) !== (expected & check_mask))
                $fatal (1, "fifo_4x16_rtl vector %0d got=%h expected=%h mask=%h", i, { full_rtl, empty_rtl, q_rtl }, expected, check_mask);
            if (({ full_chips, empty_chips, q_chips } & check_mask) !== (expected & check_mask))
                $fatal (1, "fifo_4x16_chips vector %0d got=%h expected=%h mask=%h", i, { full_chips, empty_chips, q_chips }, expected, check_mask);
        end
        $display ("PASS fifo_4x16: 1045 oracle vectors");
        $finish;
    end

    initial
    begin
        repeat (vector_count * 3 + 20) @ (posedge clk);
        $fatal (1, "Cycle watchdog");
    end

endmodule
tb/tb_integrations.sv

Download

`timescale 1ns / 1ps

module tb_integrations;

    localparam period = 20;
    logic clk         = 1'b0;
    logic rst         = 1'b1;
    logic ena         = 1'b1;
    logic [15:0] data = 16'ha000;
    logic [31:0] reference_result;
    logic [7:0] lab_led, hack_led;
    logic [7:0] lab_seg, hack_seg, lab_digit, hack_digit;
    logic [7:0] tt_out [4], tt_io [4], tt_oe [4];
    logic [7:0] tt_display;
    logic [20:0] reference_segments;
    logic [6:0] hex_digit;
    wire [99:0] lab_gpio = { 92'b0, data [15:8] };
    wire [3:0] hack_gpio = data [11:8];

    always # (period / 2) clk = ~ clk;
    wire [3:0] i_data = data [0 +: 4];
    wire i_push       = data [4 +: 1];
    wire i_pop        = data [5 +: 1];
    logic [3:0] o_q;
    logic o_empty;
    logic o_full;
    fifo_4x16 reference_core
    (
        .clk          (clk),
        .rst          (rst),
        .data         (i_data),
        .push         (i_push),
        .pop          (i_pop),
        .q            (o_q),
        .empty        (o_empty),
        .full         (o_full)
    );
    assign reference_result = { 26'b0, o_full, o_empty, o_q };
    always_comb
        case (reference_result [3:0])
            0: hex_digit       = 7'h3f;
            1: hex_digit       = 7'h06;
            2: hex_digit       = 7'h5b;
            3: hex_digit       = 7'h4f;
            4: hex_digit       = 7'h66;
            5: hex_digit       = 7'h6d;
            6: hex_digit       = 7'h7d;
            7: hex_digit       = 7'h07;
            8: hex_digit       = 7'h7f;
            9: hex_digit       = 7'h67;
            10: hex_digit      = 7'h77;
            11: hex_digit      = 7'h7c;
            12: hex_digit      = 7'h39;
            13: hex_digit      = 7'h5e;
            14: hex_digit      = 7'h79;
            15: hex_digit      = 7'h71;
            default: hex_digit = '0;
        endcase
    assign reference_segments = { 14'b0, hex_digit };
    lab_top # (.SCAN_BITS (4)) lab
    (
        .clk          (clk),
        .slow_clk     (clk),
        .rst          (rst),
        .key          (4'b0),
        .sw           (data [7:0]),
        .led          (lab_led),
        .abcdefgh     (lab_seg),
        .digit        (lab_digit),
        .x            (10'b0),
        .y            (9'b0),
        .red          (),
        .green        (),
        .blue         (),
        .mic          (24'b0),
        .sound        (),
        .uart_rx      (1'b1),
        .uart_tx      (),
        .gpio         (lab_gpio)
    );
    hackathon_top # (.SCAN_BITS (4), .UPPER_INPUTS (4'ha)) hack
    (
        .clock        (clk),
        .slow_clock   (clk),
        .reset        (rst),
        .key          (data [7:0]),
        .led          (hack_led),
        .abcdefgh     (hack_seg),
        .digit        (hack_digit),
        .x            (10'b0),
        .y            (9'b0),
        .red          (),
        .green        (),
        .blue         (),
        .gpio         (hack_gpio)
    );
    for (genvar v = 0; v < 4; v ++)
    begin : views
        tt_um_fifo_4x16 # (.OUTPUT_VIEW (v)) tt
        (
            .clk          (clk),
            .rst_n        (~ rst),
            .ena          (ena),
            .ui_in        (data [7:0]),
            .uio_in       (data [15:8]),
            .uo_out       (tt_out [v]),
            .uio_out      (tt_io [v]),
            .uio_oe       (tt_oe [v])
        );
    end
    tt_um_fifo_4x16 # (.DISPLAY_MODE (1)) display_tt
    (
        .clk          (clk),
        .rst_n        (~ rst),
        .ena          (ena),
        .ui_in        (data [7:0]),
        .uio_in       (data [15:8]),
        .uo_out       (tt_display),
        .uio_out      (),
        .uio_oe       ()
    );
    initial
    begin
        @ (posedge clk);
        # 1;
        rst = 1'b0;
        for (int i = 0; i < 160; i ++)
        begin
            data = { 4'ha, 12' (i * 137) };
            ena  = i % 7 != 0;
            @ (posedge clk);
            # 1;
            if (lab_led !== reference_result [7:0] || hack_led !== reference_result [7:0])
                $fatal (1, "BGM input/output mapping");
            if (lab_seg !== hack_seg || lab_digit !== hack_digit)
                $fatal (1, "BGM scan disagreement");
            for (int v = 0; v < 4; v ++)
            begin
                if (tt_out [v] !== (ena ? reference_result [v * 8 +: 8] : 8'b0))
                    $fatal (1, "TinyTapeout output byte %0d", v);
                if (tt_io [v] !== 8'b0 || tt_oe [v] !== 8'b0)
                    $fatal (1, "TinyTapeout bidirectional pins must be inputs");
            end
            if (tt_display !== (ena ? { 1'b0, reference_segments [6:0] } : 8'b0))
                $fatal (1, "TinyTapeout segment mapping");
            for (int digit_index = 0; digit_index < 3; digit_index ++)
                if (lab_digit == (8'b1 << digit_index))
                begin
                    logic [6:0] active_segments;
                    active_segments = reference_segments [digit_index * 7 +: 7];
                    if (lab_seg !== { active_segments [0], active_segments [1], active_segments [2],
                        active_segments [3], active_segments [4], active_segments [5], active_segments [6], 1'b0 })
                        $fatal (1, "BGM segment order");
                end
        end
        $display ("PASS integrations");
        $finish;
    end
    initial
    begin
        repeat (200) @ (posedge clk);
        $fatal (1, "Cycle watchdog");
    end
endmodule

Run from this lesson directory:

bash scripts/run_iverilog.sh
bash scripts/run_verilator.sh
bash scripts/run_questa.sh
bash scripts/check_integrations.sh

The testbench begins with `timescale 1ns / 1ps. It checks both designs against independent arithmetic, queues or input-history expectations. A separate clock process and cycle watchdog are used. Simulator logs and exact vector count are stored under research/.

1045 vectors. Test manifest · Source list

CheckResult
iverilog12PASS
iverilog14PASS
verilatorPASS
integrationsPASS
questa_compilePASS
questa_integrations_compilePASS
yosys_genericPASS
yosys_chipssimulation-only FIFO model; generic core synthesized
yosys_tinytapeoutPASS
wrappersPASS

Tool results

Questa scripts use /home/verilog/altera_lite/25.1std/questa_fse/bin and /home/verilog/flexlm/license.dat by default. QUESTA_BIN, SALT_LICENSE_SERVER and LM_LICENSE_FILE can override them. Compilation and runtime are reported separately. The current sandbox run stops with “couldn’t open socket: not owner”; no completed Questa simulation is claimed. Run the script in an ordinary user terminal for licensed simulation.

Questa runtime log

12. BGM lab_top integration

Copy rtl/, the required models/ if selecting CHIP_MODEL=1, and integration/exercise_adapter.sv plus exercise_scan.sv into the BGM source set. Replace lab_top with the supplied integration/lab_top.sv. Default CHIP_MODEL=0 uses the generic design. Use slow_clk for the exercise and clk for display scanning. The GPIO inputs must be driven externally.

data[7:0]=SW, data[15:8]=GPIO[7:0]. LED shows result[OUTPUT_VIEW*8 +: 8]. The dynamic display shows the exercise’s segment outputs, or the low result nibble in hexadecimal.

RTLPacked bus
datadata[3:0]
pushdata[4:4]
popdata[5:5]
qresult[3:0]
emptyresult[4:4]
fullresult[5:5]
integration/exercise_adapter.sv

Download

module exercise_adapter
# (
    parameter CHIP_MODEL = 0
)
(
    input               clk,
    input               rst,
    input        [15:0] data,
    output       [31:0] result,
    output       [20:0] segments
);

    wire [3:0] io_data = data [0 +: 4];
    wire io_push       = data [4 +: 1];
    wire io_pop        = data [5 +: 1];
    logic [3:0] io_q;
    logic io_empty;
    logic io_full;
    generate
        if (CHIP_MODEL == 0)
        begin : generic_design
            fifo_4x16 dut
            (
                .clk          (clk),
                .rst          (rst),
                .data         (io_data),
                .push         (io_push),
                .pop          (io_pop),
                .q            (io_q),
                .empty        (io_empty),
                .full         (io_full)
            );
        end
        else
        begin : physical_design
            fifo_4x16_chips dut
            (
                .clk          (clk),
                .rst          (rst),
                .data         (io_data),
                .push         (io_push),
                .pop          (io_pop),
                .q            (io_q),
                .empty        (io_empty),
                .full         (io_full)
            );
        end
    endgenerate
    assign result = { 26'b0, { io_full, io_empty, io_q } };
    logic [6:0] digit_zero;
    always_comb
        case (result [3:0])
            0: digit_zero       = 7'h3f;
            1: digit_zero       = 7'h06;
            2: digit_zero       = 7'h5b;
            3: digit_zero       = 7'h4f;
            4: digit_zero       = 7'h66;
            5: digit_zero       = 7'h6d;
            6: digit_zero       = 7'h7d;
            7: digit_zero       = 7'h07;
            8: digit_zero       = 7'h7f;
            9: digit_zero       = 7'h67;
            10: digit_zero      = 7'h77;
            11: digit_zero      = 7'h7c;
            12: digit_zero      = 7'h39;
            13: digit_zero      = 7'h5e;
            14: digit_zero      = 7'h79;
            15: digit_zero      = 7'h71;
            default: digit_zero = '0;
        endcase
    assign segments = { 14'b0, digit_zero };
endmodule
integration/lab_top.sv

Download

// Full BGM interface. Requires w_sw >= 8 and w_gpio >= 4.
// The adder uses three display positions; the 4511 lesson uses one.

module lab_top
# (
    parameter OUTPUT_VIEW = 0,
    parameter SCAN_BITS   = 16,
    parameter CHIP_MODEL  = 0,
    parameter clk_mhz     = 50,
    parameter w_key       = 4,
    parameter w_sw        = 8,
    parameter w_led       = 8,
    parameter w_digit     = 8,
    parameter w_gpio      = 100,

    parameter screen_width  = 640,
    parameter screen_height = 480,

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

    parameter w_x = $clog2 (screen_width),
    parameter 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 [w_led   - 1:0] led,

    // A dynamic seven-segment display

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

    // Graphics

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

    output [w_red   - 1:0] red,
    output [w_green - 1:0] green,
    output [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
);

    logic [31:0] result;
    logic [20:0] segments;
    exercise_adapter # (.CHIP_MODEL (CHIP_MODEL)) adapter
    (
        .clk          (slow_clk),
        .rst          (rst),
        .data         ({ 8' (gpio), 8' (sw) }),
        .result       (result),
        .segments     (segments)
    );
    assign led      = w_led' (result [OUTPUT_VIEW * 8 +: 8]);
    logic [7:0] scan_digit;
    exercise_scan # (.SCAN_BITS (SCAN_BITS)) scan
    (
        .clk          (clk),
        .rst          (rst),
        .segments     (segments),
        .abcdefgh     (abcdefgh),
        .digit        (scan_digit)
    );
    assign digit   = w_digit' (scan_digit);
    assign red     = '0;
    assign green   = '0;
    assign blue    = '0;
    assign sound   = '0;
    assign uart_tx = 1'b1;
endmodule
integration/exercise_scan.sv

Download

// Synthesizable scan adapter: fast infrastructure clock, not slow exercise clock.
module exercise_scan
# (
    parameter SCAN_BITS = 16
)
(
    input               clk,
    input               rst,
    input        [20:0] segments,
    output        [7:0] abcdefgh,
    output logic  [7:0] digit
);
    logic [SCAN_BITS - 1:0] phase;
    logic [6:0] selected;

    always_ff @ (posedge clk or posedge rst)
        if (rst)
            phase <= '0;
        else
            phase <= phase + 1'b1;

    always_comb
    begin
        selected = '0;
        digit    = '0;
        case (phase [SCAN_BITS - 1 -: 2])
        2'd0: begin selected = segments [ 6: 0]; digit = 8'b0000_0001; end
        2'd1: begin selected = segments [13: 7]; digit = 8'b0000_0010; end
        2'd2: begin selected = segments [20:14]; digit = 8'b0000_0100; end
        default: begin end
        endcase
    end

    // BGM order [7:0] = { a,b,c,d,e,f,g,dp }, active high.
    assign abcdefgh = { selected [0], selected [1], selected [2],
                        selected [3], selected [4], selected [5],
                        selected [6], 1'b0 };
endmodule

13. BGM hackathon_top integration

Use the supplied hackathon_top for the BGM 800×480 interface. data[7:0]=KEY, data[11:8]=GPIO, data[15:12]=UPPER_INPUTS (a four-bit compile-time parameter). For exercises needing more than twelve independent inputs, vary UPPER_INPUTS between builds or use lab_top/TinyTapeout for all live bits.

integration/hackathon_top.sv

Download

module hackathon_top
# (
    parameter OUTPUT_VIEW        = 0,
    parameter SCAN_BITS          = 16,
    parameter [3:0] UPPER_INPUTS = 0
)
(
    input               clock,
    input               slow_clock,
    input               reset,
    input        [7:0]  key,
    input        [9:0]  x,
    input        [8:0]  y,
    output       [7:0]  led,
    output       [7:0]  abcdefgh,
    output       [7:0]  digit,
    output       [4:0]  red,
    output       [5:0]  green,
    output       [4:0]  blue,
    inout         [3:0] gpio
);

    logic [31:0] result;
    logic [20:0] segments;
    exercise_adapter adapter
    (
        .clk          (slow_clock),
        .rst          (reset),
        .data         ({ UPPER_INPUTS, gpio, key }),
        .result       (result),
        .segments     (segments)
    );
    assign led      = result [OUTPUT_VIEW * 8 +: 8];
    exercise_scan # (.SCAN_BITS (SCAN_BITS)) scan
    (
        .clk          (clock),
        .rst          (reset),
        .segments     (segments),
        .abcdefgh     (abcdefgh),
        .digit        (digit)
    );
    assign red   = '0;
    assign green = '0;
    assign blue  = '0;
endmodule

14. Tiny Tapeout project.v integration

Copy rtl/ and integration/exercise_adapter.sv plus integration/project.v into the TinyTapeout project source list; set top_module to tt_um_fifo_4x16. ui_in maps to data[7:0], uio_in to data[15:8]. All bidirectional pins are inputs (uio_oe=0); rst_n is inverted for the active-high exercise reset. No BGM scanner is needed here.

DISPLAY_MODE=0 puts one raw result byte on uo_out; OUTPUT_VIEW selects bytes 0…3. DISPLAY_MODE=1 drives the first seven-segment digit, with uo_out[6:0]={g,f,e,d,c,b,a} and bit7=0. ena=0 blanks outputs. The three physical breadboard displays cannot all appear simultaneously on eight TinyTapeout output pins.

integration/project.v

Download

module tt_um_fifo_4x16
# (
    parameter OUTPUT_VIEW  = 0,
    parameter DISPLAY_MODE = 0
)
(
    input        [7:0] ui_in,
    input        [7:0] uio_in,
    input              ena,
    input              clk,
    input              rst_n,
    output       [7:0] uo_out,
    output       [7:0] uio_out,
    output       [7:0] uio_oe
);

    logic [31:0] result;
    logic [20:0] segments;
    exercise_adapter adapter
    (
        .clk          (clk),
        .rst          (~ rst_n),
        .data         ({ uio_in, ui_in }),
        .result       (result),
        .segments     (segments)
    );
    assign uo_out  = ~ ena ? 8'b0 : DISPLAY_MODE ? { 1'b0, segments [6:0] } : result [OUTPUT_VIEW * 8 +: 8];
    assign uio_out = '0;
    assign uio_oe  = '0;
endmodule

15. Experiments, troubleshooting and sources

Reset with PUSH=POP=0. Push 0…15 on sixteen consecutive clocks; verify FULL. Attempt an extra push, then pop all sixteen values. Repeat push,push,push,push+pop,push+pop,pop and test simultaneous requests at empty/full.

If the result is wrong, check power and package orientation, then fixed control polarities, then the first differing internal net. For sequential circuits check reset/load and clock edges before changing equations. Record expected and observed values for every experiment.

July 31 curriculum · Habr curriculum · LSSK inventory · Coverage of all 61 source entries