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

Automatic three-FIFO adder with backpressure

1. Purpose and prerequisites

At each clock cycle, enqueue A and B independently. Automatically consume their oldest pair and enqueue the sum when both are available and the result FIFO can accept it.

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

Three independent 4×16 FIFOs pair the oldest A and B automatically. Transfer occurs only when both inputs are nonempty and the sum FIFO is not full. One accepted pair can advance every clock. The result is modulo 16; overflow is deliberately discarded. A_VALID/B_VALID must remain asserted with stable data until the corresponding READY is high at the sampling edge.

Backpressure propagates from a full result FIFO to both input queues. SUM_READY drains only a valid result. No manual internal POP or clock divider is used. Pin-model outputs settle in the low half-cycle, as in the single-FIFO lesson.

PortDirectionBits
clkinput1
rstinput1
ainput4
binput4
a_validinput1
b_validinput1
sum_readyinput1
a_readyoutput1
b_readyoutput1
sumoutput4
sum_validoutput1
Automatic three-FIFO adder with backpressure · timing
SVG · PNG

3. Parts you need

PartQuantityLSSK row
CD4081B · DIP310
CD4069UB · DIP424
CD40174B · DIP326
CD4071B · DIP120
CD74HC40105 · DIP336
CD4008B · DIP16
830-contact breadboard445
5 V regulated power module156
USB-to-9 V cable to regulator input157
Qualified button channels (sets of five)1266
Assembled 555 clock module154
5 V current-limited LED746
M–M jumper16070
External-module lead (M–F assumed)5571

CSV · Jumper schedule · Pin/net data

Counts describe this exact layout, without spare jumpers. The button quantity counts independent channels: buy 3 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

CD4008B

4-bit adder · Manufacturer datasheet

PinSignalType
1a4input
2b3input
3a3input
4b2input
5a2input
6b1input
7a1input
8vsssupply
9ciinput
10s1output
11s2output
12s3output
13s4output
14cooutput
15b4input
16vddsupply

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

Automatic three-FIFO adder with backpressure · 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

Automatic three-FIFO adder with backpressure · connections
SVG · PNG
DevicePinSignalNet
U11a1a_valid
U12b1a_dir
U13y1n1
U14y2n2
U15b2a_dor
U16a2transfer
U17vssGND
U18a3n4
U19b3a_held[4]
U110y3n5
U111y4n8
U112b4b_dir
U113a4b_valid
U114vdd+5V
U21a1rst
U22y1n3
U23a2clk
U24y2n4
U25a3a_held[5]
U26y3n6
U27vssGND
U28y4a_full
U29a4a_dir
U210y5a_empty
U211a5a_dor
U212y6a_ready
U213a6a_full
U214vdd+5V
U31clear_nn3
U32q0a_held[0]
U33d0a[0]
U34d1a[1]
U35q1a_held[1]
U36d2a[2]
U37q2a_held[2]
U38vssGND
U39clkclk
U310q3a_held[3]
U311d3a[3]
U312q4a_held[4]
U313d4n1
U314d5n2
U315q5a_held[5]
U316vdd+5V
U41a1clk
U42b1n6
U43y1n7
U44y2n14
U45b2n13
U46a2clk
U47vssGND
U48a3clk
U49b3n24
U410y3n25
U411y4NC
U412b4GND
U413a4GND
U414vdd+5V
U51tsGND
U52dira_dir
U53sin5
U54d0a_held[0]
U55d1a_held[1]
U56d2a_held[2]
U57d3a_held[3]
U58vssGND
U59rstrst
U510q3a_head[3]
U511q2a_head[2]
U512q1a_head[1]
U513q0a_head[0]
U514dora_dor
U515son7
U516vdd+5V
U61a1transfer
U62b1b_dor
U63y1n9
U64y2n12
U65b2b_held[4]
U66a2n11
U67vssGND
U68a3n15
U69b3n16
U610y3n18
U611y4transfer
U612b4n17
U613a4n18
U614vdd+5V
U71a1rst
U72y1n10
U73a2clk
U74y2n11
U75a3b_held[5]
U76y3n13
U77vssGND
U78y4b_full
U79a4b_dir
U710y5b_empty
U711a5b_dor
U712y6b_ready
U713a6b_full
U714vdd+5V
U81clear_nn10
U82q0b_held[0]
U83d0b[0]
U84d1b[1]
U85q1b_held[1]
U86d2b[2]
U87q2b_held[2]
U88vssGND
U89clkclk
U810q3b_held[3]
U811d3b[3]
U812q4b_held[4]
U813d4n8
U814d5n9
U815q5b_held[5]
U816vdd+5V
U91tsGND
U92dirb_dir
U93sin12
U94d0b_held[0]
U95d1b_held[1]
U96d2b_held[2]
U97d3b_held[3]
U98vssGND
U99rstrst
U910q3b_head[3]
U911q2b_head[2]
U912q1b_head[1]
U913q0b_head[0]
U914dorb_dor
U915son14
U916vdd+5V
U101a4a_head[3]
U102b3b_head[2]
U103a3a_head[2]
U104b2b_head[1]
U105a2a_head[1]
U106b1b_head[0]
U107a1a_head[0]
U108vssGND
U109ciGND
U1010s1added[0]
U1011s2added[1]
U1012s3added[2]
U1013s4added[3]
U1014coNC
U1015b4b_head[3]
U1016vdd+5V
U111a1a_empty
U112y1n15
U113a2b_empty
U114y2n16
U115a3sum_full
U116y3n17
U117vssGND
U118y4n21
U119a4rst
U1110y5n22
U1111a5clk
U1112y6n24
U1113a6s_held[5]
U1114vdd+5V
U121a1transfer
U122b1s_dir
U123y1n19
U124y2n20
U125b2s_dor
U126a2sum_ready
U127vssGND
U128a3n22
U129b3s_held[4]
U1210y3n23
U1211y4NC
U1212b4GND
U1213a4GND
U1214vdd+5V
U131clear_nn21
U132q0s_held[0]
U133d0added[0]
U134d1added[1]
U135q1s_held[1]
U136d2added[2]
U137q2s_held[2]
U138vssGND
U139clkclk
U1310q3s_held[3]
U1311d3added[3]
U1312q4s_held[4]
U1313d4n19
U1314d5n20
U1315q5s_held[5]
U1316vdd+5V
U141tsGND
U142dirs_dir
U143sin23
U144d0s_held[0]
U145d1s_held[1]
U146d2s_held[2]
U147d3s_held[3]
U148vssGND
U149rstrst
U1410q3sum[3]
U1411q2sum[2]
U1412q1sum[1]
U1413q0sum[0]
U1414dors_dor
U1415son25
U1416vdd+5V
U151a1s_dir
U152y1sum_full
U153a2s_dor
U154y2sum_empty
U155a3sum_empty
U156y3sum_valid
U157vssGND
U158y4NC
U159a4GND
U1510y5NC
U1511a5GND
U1512y6NC
U1513a6GND
U1514vdd+5V

7. Schematic 3 · Breadboard and assembly

Automatic three-FIFO adder with backpressure · 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. Send A values before B values; confirm no partial pair is consumed. Supply B, stall SUM_READY until backpressure appears, then drain and compare every sum in order.

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+5VB3.TP25B3.TP26
W014GNDB3.TG25B3.TG26
W015+5VB3.BP25B3.BP26
W016GNDB3.BG25B3.BG26
W017+5VB3.TP1B3.BP1
W018GNDB3.TG1B3.BG1
W019+5VB4.TP25B4.TP26
W020GNDB4.TG25B4.TG26
W021+5VB4.BP25B4.BP26
W022GNDB4.BG25B4.BG26
W023+5VB4.TP1B4.BP1
W024GNDB4.TG1B4.BG1
W025+5VB1.BP50B2.TP50
W026GNDB1.BG50B2.TG50
W027+5VB2.BP50B3.TP50
W028GNDB2.BG50B3.TG50
W029+5VB3.BP50B4.TP50
W030GNDB3.BG50B4.TG50
W031a_validB1.j5S10.OUT
W032a_dirB1.j6B1.a24
W033a_dirB1.b24B2.j6
W034n1B1.j7B1.a36
W035n2B1.j8B1.a35
W036a_dorB1.j9B1.a22
W037a_dorB1.b22B2.a7
W038transferB1.j10B2.j19
W039transferB2.i19B2.a22
W040transferB2.b22B3.j47
W041GNDB1.j11B1.BG2
W042GNDB1.j25B1.BG3
W043GNDB1.j40B1.BG4
W044GNDB1.j53B1.BG5
W045GNDB1.a49B1.TG2
W046GNDB1.a48B1.TG3
W047GNDB2.j5B2.BG2
W048GNDB2.j12B2.BG3
W049GNDB2.j25B2.BG4
W050GNDB2.j39B2.BG5
W051GNDB2.j54B2.BG6
W052GNDB3.j5B3.BG2
W053GNDB3.j12B3.BG3
W054GNDB3.j26B3.BG4
W055GNDB3.a26B3.TG2
W056GNDB3.j39B3.BG5
W057GNDB3.j53B3.BG6
W058GNDB3.a49B3.TG3
W059GNDB3.a48B3.TG4
W060GNDB4.j12B4.BG2
W061GNDB4.j19B4.BG3
W062GNDB4.j26B4.BG4
W063GNDB4.j39B4.BG5
W064GNDB4.a38B4.TG2
W065GNDB4.a36B4.TG3
W066GNDB4.a34B4.TG4
W067GNDS1.GNDB1.BG6
W068GNDS2.GNDB2.BG7
W069GNDS3.GNDB3.BG7
W070GNDS4.GNDB4.BG6
W071GNDS5.GNDB1.BG7
W072GNDS6.GNDB2.BG8
W073GNDS7.GNDB3.BG8
W074GNDS8.GNDB4.BG7
W075GNDS9.GNDB1.BG8
W076GNDS10.GNDB2.BG9
W077GNDS11.GNDB3.BG9
W078GNDS12.GNDB4.BG8
W079GNDCLK.GNDB1.BG9
W080GNDPWR.GNDB1.BG10
W081GNDL1.KB1.BG11
W082GNDL2.KB2.BG10
W083GNDL3.KB3.BG10
W084GNDL4.KB4.BG9
W085GNDL5.KB1.BG12
W086GNDL6.KB2.BG11
W087GNDL7.KB3.BG11
W088n4B1.a11B1.j22
W089a_held[4]B1.a10B1.a37
W090n5B1.a9B2.j7
W091n8B1.a8B2.a50
W092b_dirB1.a7B2.a38
W093b_dirB2.b38B3.j6
W094b_validB1.a6S11.OUT
W095+5VB1.a5B1.TP2
W096+5VB1.a19B1.TP3
W097+5VB1.a33B1.TP4
W098+5VB1.a47B1.TP5
W099+5VB2.a5B2.TP2
W100+5VB2.a19B2.TP3
W101+5VB2.a33B2.TP4
W102+5VB2.a47B2.TP5
W103+5VB3.a5B3.TP2
W104+5VB3.a19B3.TP3
W105+5VB3.a33B3.TP4
W106+5VB3.a47B3.TP5
W107+5VB4.a5B4.TP2
W108+5VB4.a19B4.TP3
W109+5VB4.a33B4.TP4
W110+5VS1.VCCB1.BP2
W111+5VS2.VCCB2.BP2
W112+5VS3.VCCB3.BP2
W113+5VS4.VCCB4.BP2
W114+5VS5.VCCB1.BP3
W115+5VS6.VCCB2.BP3
W116+5VS7.VCCB3.BP3
W117+5VS8.VCCB4.BP3
W118+5VS9.VCCB1.BP4
W119+5VS10.VCCB2.BP4
W120+5VS11.VCCB3.BP4
W121+5VS12.VCCB4.BP4
W122+5VCLK.VCCB1.BP5
W123+5VPWR.+5VB1.BP6
W124rstB1.j19B2.a12
W125rstB2.b12B2.j33
W126rstB2.i33B3.a12
W127rstB3.b12B3.a38
W128rstB3.b38B4.a26
W129rstB4.b26S1.OUT
W130n3B1.j20B1.j33
W131clkB1.j21B1.a40
W132clkB1.b40B1.j47
W133clkB1.i47B1.j52
W134clkB1.i52B1.a53
W135clkB1.b53B2.j35
W136clkB2.i35B2.a54
W137clkB2.b54B3.a36
W138clkB3.b36B4.a12
W139clkB4.b12CLK.OUT
W140a_held[5]B1.j23B1.a34
W141n6B1.j24B1.j48
W142a_fullB1.a25B1.a20
W143a_emptyB1.a23B3.j33
W144a_readyB1.a21L1.A
W145a_held[0]B1.j34B2.j8
W146a[0]B1.j35S2.OUT
W147a[1]B1.j36S3.OUT
W148a_held[1]B1.j37B2.j9
W149a[2]B1.j38S4.OUT
W150a_held[2]B1.j39B2.j10
W151a_held[3]B1.a39B2.j11
W152a[3]B1.a38S5.OUT
W153n7B1.j49B2.a6
W154n14B1.j50B3.a6
W155n13B1.j51B2.j38
W156n24B1.a52B3.a35
W157n25B1.a51B4.a20
W158a_head[3]B2.a11B3.j19
W159a_head[2]B2.a10B3.j21
W160a_head[1]B2.a9B3.j23
W161a_head[0]B2.a8B3.j25
W162b_dorB2.j20B2.a36
W163b_dorB2.b36B3.a7
W164n9B2.j21B2.a49
W165n12B2.j22B3.j7
W166b_held[4]B2.j23B2.a51
W167n11B2.j24B2.j36
W168n15B2.a25B3.j34
W169n16B2.a24B3.j36
W170n18B2.a23B2.a20
W171n17B2.a21B3.j38
W172n10B2.j34B2.j47
W173b_held[5]B2.j37B2.a48
W174b_fullB2.a39B2.a34
W175b_emptyB2.a37B3.j35
W176b_readyB2.a35L2.A
W177b_held[0]B2.j48B3.j8
W178b[0]B2.j49S6.OUT
W179b[1]B2.j50S7.OUT
W180b_held[1]B2.j51B3.j9
W181b[2]B2.j52S8.OUT
W182b_held[2]B2.j53B3.j10
W183b_held[3]B2.a53B3.j11
W184b[3]B2.a52S9.OUT
W185b_head[3]B3.a11B3.a20
W186b_head[2]B3.a10B3.j20
W187b_head[1]B3.a9B3.j22
W188b_head[0]B3.a8B3.j24
W189added[0]B3.a25B4.j7
W190added[1]B3.a24B4.j8
W191added[2]B3.a23B4.j10
W192added[3]B3.a22B4.a10
W193sum_fullB3.j37B4.j34
W194n21B3.a39B4.j5
W195n22B3.a37B3.a53
W196s_held[5]B3.a34B4.a6
W197s_dirB3.j48B4.j20
W198s_dirB4.i20B4.j33
W199n19B3.j49B4.a8
W200n20B3.j50B4.a7
W201s_dorB3.j51B4.a21
W202s_dorB4.b21B4.j35
W203sum_readyB3.j52S12.OUT
W204s_held[4]B3.a52B4.a9
W205n23B3.a51B4.j21
W206s_held[0]B4.j6B4.j22
W207s_held[1]B4.j9B4.j23
W208s_held[2]B4.j11B4.j24
W209s_held[3]B4.a11B4.j25
W210sum[3]B4.a25L6.A
W211sum[2]B4.a24L5.A
W212sum[1]B4.a23L4.A
W213sum[0]B4.a22L3.A
W214sum_emptyB4.j36B4.j37
W215sum_validB4.j38L7.A

8. Generic synthesizable SystemVerilog

rtl/fifo_adder_pipeline.sv

Download

// Synthesizable generic design.

module fifo_adder_pipeline
(
    input              clk,
    input              rst,
    input        [3:0] a,
    input        [3:0] b,
    input              a_valid,
    input              b_valid,
    input              sum_ready,
    output             a_ready,
    output             b_ready,
    output       [3:0] sum,
    output             sum_valid
);

    logic [3:0] a_head, b_head;
    logic a_empty, b_empty, a_full, b_full, sum_empty, sum_full;
    wire transfer    = ~ a_empty & ~ b_empty & ~ sum_full;
    wire [3:0] added = a_head + b_head;
    assign a_ready   = ~ a_full;
    assign b_ready   = ~ b_full;
    assign sum_valid = ~ sum_empty;
    fifo_queue a_fifo
    (
        .clk          (clk),
        .rst          (rst),
        .data         (a),
        .push         (a_valid),
        .pop          (transfer),
        .q            (a_head),
        .empty        (a_empty),
        .full         (a_full)
    );
    fifo_queue b_fifo
    (
        .clk          (clk),
        .rst          (rst),
        .data         (b),
        .push         (b_valid),
        .pop          (transfer),
        .q            (b_head),
        .empty        (b_empty),
        .full         (b_full)
    );
    fifo_queue s_fifo
    (
        .clk          (clk),
        .rst          (rst),
        .data         (added),
        .push         (transfer),
        .pop          (sum_ready),
        .q            (sum),
        .empty        (sum_empty),
        .full         (sum_full)
    );

endmodule
rtl/fifo_queue.sv

Download

module fifo_queue
(
    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/cd4008b.sv

Download

// Synthesizable ideal functional model; physical power pins omitted.

module cd4008b
(
    input  p1_a4,
    input  p2_b3,
    input  p3_a3,
    input  p4_b2,
    input  p5_a2,
    input  p6_b1,
    input  p7_a1,
    input  p9_ci,
    input  p15_b4,
    output p10_s1,
    output p11_s2,
    output p12_s3,
    output p13_s4,
    output p14_co
);

    logic [4:0] carry;
    logic [3:0] a, b, sum;

    assign a         = { p1_a4, p3_a3, p5_a2, p7_a1 };
    assign b         = { p15_b4, p2_b3, p4_b2, p6_b1 };
    assign carry [0] = p9_ci;

    for (genvar i = 0; i < 4; i ++)
    begin : full_adder
        assign sum [i]       = a [i] ^ b [i] ^ carry [i];
        assign carry [i + 1] = (a [i] & b [i])
                                | ((a [i] ^ b [i]) & carry [i]);
    end

    assign { p13_s4, p12_s3, p11_s2, p10_s1 } = sum;
    assign p14_co                             = carry [4];

endmodule
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_adder_pipeline_chips.sv

Download

// Pin connections are generated from the same netlist as the diagrams.
module fifo_adder_pipeline_chips
(
    input              clk,
    input              rst,
    input        [3:0] a,
    input        [3:0] b,
    input              a_valid,
    input              b_valid,
    input              sum_ready,
    output             a_ready,
    output             b_ready,
    output       [3:0] sum,
    output             sum_valid
);

    logic a_dir;
    logic a_dor;
    logic a_empty;
    logic a_full;
    logic [3:0] a_head;
    logic [5:0] a_held;
    logic [3:0] added;
    logic b_dir;
    logic b_dor;
    logic b_empty;
    logic b_full;
    logic [3:0] b_head;
    logic [5:0] b_held;
    logic n1;
    logic n10;
    logic n11;
    logic n12;
    logic n13;
    logic n14;
    logic n15;
    logic n16;
    logic n17;
    logic n18;
    logic n19;
    logic n2;
    logic n20;
    logic n21;
    logic n22;
    logic n23;
    logic n24;
    logic n25;
    logic n3;
    logic n4;
    logic n5;
    logic n6;
    logic n7;
    logic n8;
    logic n9;
    logic s_dir;
    logic s_dor;
    logic [5:0] s_held;
    logic sum_empty;
    logic sum_full;
    logic transfer;

    cd4081b u1
    (
        .p1_a1        (a_valid),
        .p2_b1        (a_dir),
        .p3_y1        (n1),
        .p4_y2        (n2),
        .p5_b2        (a_dor),
        .p6_a2        (transfer),
        .p8_a3        (n4),
        .p9_b3        (a_held [4]),
        .p10_y3       (n5),
        .p11_y4       (n8),
        .p12_b4       (b_dir),
        .p13_a4       (b_valid)
    );

    cd4069ub u2
    (
        .p1_a1        (rst),
        .p2_y1        (n3),
        .p3_a2        (clk),
        .p4_y2        (n4),
        .p5_a3        (a_held [5]),
        .p6_y3        (n6),
        .p8_y4        (a_full),
        .p9_a4        (a_dir),
        .p10_y5       (a_empty),
        .p11_a5       (a_dor),
        .p12_y6       (a_ready),
        .p13_a6       (a_full)
    );

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

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

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

    cd4081b u6
    (
        .p1_a1        (transfer),
        .p2_b1        (b_dor),
        .p3_y1        (n9),
        .p4_y2        (n12),
        .p5_b2        (b_held [4]),
        .p6_a2        (n11),
        .p8_a3        (n15),
        .p9_b3        (n16),
        .p10_y3       (n18),
        .p11_y4       (transfer),
        .p12_b4       (n17),
        .p13_a4       (n18)
    );

    cd4069ub u7
    (
        .p1_a1        (rst),
        .p2_y1        (n10),
        .p3_a2        (clk),
        .p4_y2        (n11),
        .p5_a3        (b_held [5]),
        .p6_y3        (n13),
        .p8_y4        (b_full),
        .p9_a4        (b_dir),
        .p10_y5       (b_empty),
        .p11_a5       (b_dor),
        .p12_y6       (b_ready),
        .p13_a6       (b_full)
    );

    cd40174b u8
    (
        .p1_clear_n   (n10),
        .p2_q0        (b_held [0]),
        .p3_d0        (b [0]),
        .p4_d1        (b [1]),
        .p5_q1        (b_held [1]),
        .p6_d2        (b [2]),
        .p7_q2        (b_held [2]),
        .p9_clk       (clk),
        .p10_q3       (b_held [3]),
        .p11_d3       (b [3]),
        .p12_q4       (b_held [4]),
        .p13_d4       (n8),
        .p14_d5       (n9),
        .p15_q5       (b_held [5])
    );

    cd74hc40105 u9
    (
        .p1_ts        (1'b0),
        .p2_dir       (b_dir),
        .p3_si        (n12),
        .p4_d0        (b_held [0]),
        .p5_d1        (b_held [1]),
        .p6_d2        (b_held [2]),
        .p7_d3        (b_held [3]),
        .p9_rst       (rst),
        .p10_q3       (b_head [3]),
        .p11_q2       (b_head [2]),
        .p12_q1       (b_head [1]),
        .p13_q0       (b_head [0]),
        .p14_dor      (b_dor),
        .p15_so       (n14)
    );

    cd4008b u10
    (
        .p1_a4        (a_head [3]),
        .p2_b3        (b_head [2]),
        .p3_a3        (a_head [2]),
        .p4_b2        (b_head [1]),
        .p5_a2        (a_head [1]),
        .p6_b1        (b_head [0]),
        .p7_a1        (a_head [0]),
        .p9_ci        (1'b0),
        .p10_s1       (added [0]),
        .p11_s2       (added [1]),
        .p12_s3       (added [2]),
        .p13_s4       (added [3]),
        .p14_co       (),
        .p15_b4       (b_head [3])
    );

    cd4069ub u11
    (
        .p1_a1        (a_empty),
        .p2_y1        (n15),
        .p3_a2        (b_empty),
        .p4_y2        (n16),
        .p5_a3        (sum_full),
        .p6_y3        (n17),
        .p8_y4        (n21),
        .p9_a4        (rst),
        .p10_y5       (n22),
        .p11_a5       (clk),
        .p12_y6       (n24),
        .p13_a6       (s_held [5])
    );

    cd4081b u12
    (
        .p1_a1        (transfer),
        .p2_b1        (s_dir),
        .p3_y1        (n19),
        .p4_y2        (n20),
        .p5_b2        (s_dor),
        .p6_a2        (sum_ready),
        .p8_a3        (n22),
        .p9_b3        (s_held [4]),
        .p10_y3       (n23),
        .p11_y4       (),
        .p12_b4       (1'b0),
        .p13_a4       (1'b0)
    );

    cd40174b u13
    (
        .p1_clear_n   (n21),
        .p2_q0        (s_held [0]),
        .p3_d0        (added [0]),
        .p4_d1        (added [1]),
        .p5_q1        (s_held [1]),
        .p6_d2        (added [2]),
        .p7_q2        (s_held [2]),
        .p9_clk       (clk),
        .p10_q3       (s_held [3]),
        .p11_d3       (added [3]),
        .p12_q4       (s_held [4]),
        .p13_d4       (n19),
        .p14_d5       (n20),
        .p15_q5       (s_held [5])
    );

    cd74hc40105 u14
    (
        .p1_ts        (1'b0),
        .p2_dir       (s_dir),
        .p3_si        (n23),
        .p4_d0        (s_held [0]),
        .p5_d1        (s_held [1]),
        .p6_d2        (s_held [2]),
        .p7_d3        (s_held [3]),
        .p9_rst       (rst),
        .p10_q3       (sum [3]),
        .p11_q2       (sum [2]),
        .p12_q1       (sum [1]),
        .p13_q0       (sum [0]),
        .p14_dor      (s_dor),
        .p15_so       (n25)
    );

    cd4069ub u15
    (
        .p1_a1        (s_dir),
        .p2_y1        (sum_full),
        .p3_a2        (s_dor),
        .p4_y2        (sum_empty),
        .p5_a3        (sum_empty),
        .p6_y3        (sum_valid),
        .p8_y4        (),
        .p9_a4        (1'b0),
        .p10_y5       (),
        .p11_a5       (1'b0),
        .p12_y6       (),
        .p13_a6       (1'b0)
    );

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 = 1600;
    logic [25:0] vectors [0:vector_count-1];
    logic [6:0] expected, check_mask;
    logic clk = 1'b0;

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

    logic rst       = '0;
    logic [3:0] a   = '0;
    logic [3:0] b   = '0;
    logic a_valid   = '0;
    logic b_valid   = '0;
    logic sum_ready = '0;
    logic a_ready_rtl;
    logic a_ready_chips;
    logic b_ready_rtl;
    logic b_ready_chips;
    logic [3:0] sum_rtl;
    logic [3:0] sum_chips;
    logic sum_valid_rtl;
    logic sum_valid_chips;

    fifo_adder_pipeline dut_rtl
    (
        .clk          (clk),
        .rst          (rst),
        .a            (a),
        .b            (b),
        .a_valid      (a_valid),
        .b_valid      (b_valid),
        .sum_ready    (sum_ready),
        .a_ready      (a_ready_rtl),
        .b_ready      (b_ready_rtl),
        .sum          (sum_rtl),
        .sum_valid    (sum_valid_rtl)
    );

    fifo_adder_pipeline_chips dut_chips
    (
        .clk          (clk),
        .rst          (rst),
        .a            (a),
        .b            (b),
        .a_valid      (a_valid),
        .b_valid      (b_valid),
        .sum_ready    (sum_ready),
        .a_ready      (a_ready_chips),
        .b_ready      (b_ready_chips),
        .sum          (sum_chips),
        .sum_valid    (sum_valid_chips)
    );

    initial
    begin
        $readmemh ("vectors.hex", vectors);
        @ (posedge clk);
        # 1;
        for (int i = 0; i < vector_count; i ++)
        begin
            rst        = vectors [i] [0 +: 1];
            a          = vectors [i] [1 +: 4];
            b          = vectors [i] [5 +: 4];
            a_valid    = vectors [i] [9 +: 1];
            b_valid    = vectors [i] [10 +: 1];
            sum_ready  = vectors [i] [11 +: 1];
            expected   = vectors [i] [12 +: 7];
            check_mask = vectors [i] [19 +: 7];
            @ (posedge clk);
            # 1;  // Allow nonblocking state and combinational outputs to settle.
            @ (negedge clk);
            # 1;  // HC FIFO commands commit in this half-cycle.
            if (({ sum_valid_rtl, sum_rtl, b_ready_rtl, a_ready_rtl } & check_mask) !== (expected & check_mask))
                $fatal (1, "fifo_adder_pipeline_rtl vector %0d got=%h expected=%h mask=%h", i, { sum_valid_rtl, sum_rtl, b_ready_rtl, a_ready_rtl }, expected, check_mask);
            if (({ sum_valid_chips, sum_chips, b_ready_chips, a_ready_chips } & check_mask) !== (expected & check_mask))
                $fatal (1, "fifo_adder_pipeline_chips vector %0d got=%h expected=%h mask=%h", i, { sum_valid_chips, sum_chips, b_ready_chips, a_ready_chips }, expected, check_mask);
        end
        $display ("PASS fifo_adder_pipeline: 1600 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_a   = data [0 +: 4];
    wire [3:0] i_b   = data [4 +: 4];
    wire i_a_valid   = data [8 +: 1];
    wire i_b_valid   = data [9 +: 1];
    wire i_sum_ready = data [10 +: 1];
    logic o_a_ready;
    logic o_b_ready;
    logic [3:0] o_sum;
    logic o_sum_valid;
    fifo_adder_pipeline reference_core
    (
        .clk          (clk),
        .rst          (rst),
        .a            (i_a),
        .b            (i_b),
        .a_valid      (i_a_valid),
        .b_valid      (i_b_valid),
        .sum_ready    (i_sum_ready),
        .a_ready      (o_a_ready),
        .b_ready      (o_b_ready),
        .sum          (o_sum),
        .sum_valid    (o_sum_valid)
    );
    assign reference_result = { 25'b0, o_sum_valid, o_sum, o_b_ready, o_a_ready };
    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_adder_pipeline # (.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_adder_pipeline # (.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/.

1600 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
adata[3:0]
bdata[7:4]
a_validdata[8:8]
b_validdata[9:9]
sum_readydata[10:10]
a_readyresult[0:0]
b_readyresult[1:1]
sumresult[5:2]
sum_validresult[6:6]
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_a   = data [0 +: 4];
    wire [3:0] io_b   = data [4 +: 4];
    wire io_a_valid   = data [8 +: 1];
    wire io_b_valid   = data [9 +: 1];
    wire io_sum_ready = data [10 +: 1];
    logic io_a_ready;
    logic io_b_ready;
    logic [3:0] io_sum;
    logic io_sum_valid;
    generate
        if (CHIP_MODEL == 0)
        begin : generic_design
            fifo_adder_pipeline dut
            (
                .clk          (clk),
                .rst          (rst),
                .a            (io_a),
                .b            (io_b),
                .a_valid      (io_a_valid),
                .b_valid      (io_b_valid),
                .sum_ready    (io_sum_ready),
                .a_ready      (io_a_ready),
                .b_ready      (io_b_ready),
                .sum          (io_sum),
                .sum_valid    (io_sum_valid)
            );
        end
        else
        begin : physical_design
            fifo_adder_pipeline_chips dut
            (
                .clk          (clk),
                .rst          (rst),
                .a            (io_a),
                .b            (io_b),
                .a_valid      (io_a_valid),
                .b_valid      (io_b_valid),
                .sum_ready    (io_sum_ready),
                .a_ready      (io_a_ready),
                .b_ready      (io_b_ready),
                .sum          (io_sum),
                .sum_valid    (io_sum_valid)
            );
        end
    endgenerate
    assign result = { 25'b0, { io_sum_valid, io_sum, io_b_ready, io_a_ready } };
    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_adder_pipeline. 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_adder_pipeline
# (
    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

Send A values before B values; confirm no partial pair is consumed. Supply B, stall SUM_READY until backpressure appears, then drain and compare every sum in order.

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