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

4019 vector selection and gate equivalent

1. Purpose and prerequisites

Compare Y=(A AND {4{Ka}}) OR (B AND {4{Kb}}) for two 4-bit vectors, using common enables.

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

4019 controls are shared across all four bits: 00→0, 10→A, 01→B, 11→A|B. It cannot implement an arbitrary four-bit mask alone; the separate gate circuit does that.

PortDirectionBits
ainput4
binput4
kainput1
kbinput1
maskinput4
sharedoutput4
maskedoutput4
abkakbmasksharedmasked
0000000
1000001
2000002
3000003
4000004
5000005
6000006
7000007
0000800
151511151515

The table is a sample; the testbench checks all 16384 combinations in vectors.hex. Values in the table are unsigned decimal.

3. Parts you need

PartQuantityLSSK row
CD4019B · DIP138
CD4069UB · DIP124
CD4081B · DIP210
CD4071B · DIP120
830-contact breadboard245
5 V regulated power module156
USB-to-9 V cable to regulator input157
Qualified button channels (sets of five)1466
5 V current-limited LED846
M–M jumper5070
External-module lead (M–F assumed)6071

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

CD4019B

Shared-enable vector selector · Manufacturer datasheet

PinSignalType
1b3input
2a2input
3b2input
4a1input
5b1input
6a0input
7b0input
8vsssupply
9kainput
10y0output
11y1output
12y2output
13y3output
14kbinput
15a3input
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

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

4019 vector selection and gate equivalent · 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

4019 vector selection and gate equivalent · connections
SVG · PNG
DevicePinSignalNet
U11b3b[3]
U12a2a[2]
U13b2b[2]
U14a1a[1]
U15b1b[1]
U16a0a[0]
U17b0b[0]
U18vssGND
U19kaka
U110y0shared[0]
U111y1shared[1]
U112y2shared[2]
U113y3shared[3]
U114kbkb
U115a3a[3]
U116vdd+5V
U21a1mask[0]
U22y1n1
U23a2mask[1]
U24y2n4
U25a3mask[2]
U26y3n7
U27vssGND
U28y4n10
U29a4mask[3]
U210y5NC
U211a5GND
U212y6NC
U213a6GND
U214vdd+5V
U31a1n1
U32b1a[0]
U33y1n2
U34y2n3
U35b2b[0]
U36a2mask[0]
U37vssGND
U38a3n4
U39b3a[1]
U310y3n5
U311y4n6
U312b4b[1]
U313a4mask[1]
U314vdd+5V
U41a1n2
U42b1n3
U43y1masked[0]
U44y2masked[1]
U45b2n6
U46a2n5
U47vssGND
U48a3n8
U49b3n9
U410y3masked[2]
U411y4masked[3]
U412b4n12
U413a4n11
U414vdd+5V
U51a1n7
U52b1a[2]
U53y1n8
U54y2n9
U55b2b[2]
U56a2mask[2]
U57vssGND
U58a3n10
U59b3a[3]
U510y3n11
U511y4n12
U512b4b[3]
U513a4mask[3]
U514vdd+5V

7. Schematic 3 · Breadboard and assembly

4019 vector selection and gate equivalent · 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. Use A=0101 and B=1010. Test all KA/KB pairs, then mask=0011 and 1100. Explain which per-bit results shared enables cannot select.

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
W015b[3]B1.j5B2.a7
W016b[3]B2.b7S8.OUT
W017a[2]B1.j6B2.j6
W018a[2]B2.i6S3.OUT
W019b[2]B1.j7B2.j9
W020b[2]B2.i9S7.OUT
W021a[1]B1.j8B1.a38
W022a[1]B1.b38S2.OUT
W023b[1]B1.j9B1.a35
W024b[1]B1.b35S6.OUT
W025a[0]B1.j10B1.j34
W026a[0]B1.i34S1.OUT
W027b[0]B1.j11B1.j37
W028b[0]B1.i37S5.OUT
W029GNDB1.j12B1.BG2
W030GNDB1.j25B1.BG3
W031GNDB1.a22B1.TG2
W032GNDB1.a20B1.TG3
W033GNDB1.j39B1.BG4
W034GNDB1.j53B1.BG5
W035GNDB2.j11B2.BG2
W036GNDS1.GNDB1.BG6
W037GNDS2.GNDB2.BG3
W038GNDS3.GNDB1.BG7
W039GNDS4.GNDB2.BG4
W040GNDS5.GNDB1.BG8
W041GNDS6.GNDB2.BG5
W042GNDS7.GNDB1.BG9
W043GNDS8.GNDB2.BG6
W044GNDS9.GNDB1.BG10
W045GNDS10.GNDB2.BG7
W046GNDS11.GNDB1.BG11
W047GNDS12.GNDB2.BG8
W048GNDS13.GNDB1.BG12
W049GNDS14.GNDB2.BG9
W050GNDPWR.GNDB1.BG13
W051GNDL1.KB1.BG14
W052GNDL2.KB2.BG10
W053GNDL3.KB1.BG15
W054GNDL4.KB2.BG11
W055GNDL5.KB1.BG16
W056GNDL6.KB2.BG12
W057GNDL7.KB1.BG17
W058GNDL8.KB2.BG13
W059kaB1.a12S9.OUT
W060shared[0]B1.a11L1.A
W061shared[1]B1.a10L2.A
W062shared[2]B1.a9L3.A
W063shared[3]B1.a8L4.A
W064kbB1.a7S10.OUT
W065a[3]B1.a6B2.a10
W066a[3]B2.b10S4.OUT
W067+5VB1.a5B1.TP2
W068+5VB1.a19B1.TP3
W069+5VB1.a33B1.TP4
W070+5VB1.a47B1.TP5
W071+5VB2.a5B2.TP2
W072+5VS1.VCCB1.BP2
W073+5VS2.VCCB2.BP2
W074+5VS3.VCCB1.BP3
W075+5VS4.VCCB2.BP3
W076+5VS5.VCCB1.BP4
W077+5VS6.VCCB2.BP4
W078+5VS7.VCCB1.BP5
W079+5VS8.VCCB2.BP5
W080+5VS9.VCCB1.BP6
W081+5VS10.VCCB2.BP6
W082+5VS11.VCCB1.BP7
W083+5VS12.VCCB2.BP7
W084+5VS13.VCCB1.BP8
W085+5VS14.VCCB2.BP8
W086+5VPWR.+5VB1.BP9
W087mask[0]B1.j19B1.j38
W088mask[0]B1.i38S11.OUT
W089n1B1.j20B1.j33
W090mask[1]B1.j21B1.a34
W091mask[1]B1.b34S12.OUT
W092n4B1.j22B1.a39
W093mask[2]B1.j23B2.j10
W094mask[2]B2.i10S13.OUT
W095n7B1.j24B2.j5
W096n10B1.a25B2.a11
W097mask[3]B1.a24B2.a6
W098mask[3]B2.b6S14.OUT
W099n2B1.j35B1.j47
W100n3B1.j36B1.j48
W101n5B1.a37B1.j52
W102n6B1.a36B1.j51
W103masked[0]B1.j49L5.A
W104masked[1]B1.j50L6.A
W105n8B1.a53B2.j7
W106n9B1.a52B2.j8
W107masked[2]B1.a51L7.A
W108masked[3]B1.a50L8.A
W109n12B1.a49B2.a8
W110n11B1.a48B2.a9

8. Generic synthesizable SystemVerilog

rtl/vector_merge.sv

Download

// Synthesizable generic design.

module vector_merge
(
    input        [3:0] a,
    input        [3:0] b,
    input              ka,
    input              kb,
    input        [3:0] mask,
    output       [3:0] shared,
    output       [3:0] masked
);

    assign shared = (a & { 4 { ka } }) | (b & { 4 { kb } });
    assign masked = (a & ~ mask) | (b & mask);

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.

models/cd4019b.sv

Download

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

module cd4019b
(
    input  p1_b3,
    input  p2_a2,
    input  p3_b2,
    input  p4_a1,
    input  p5_b1,
    input  p6_a0,
    input  p7_b0,
    input  p9_ka,
    input  p14_kb,
    input  p15_a3,
    output p10_y0,
    output p11_y1,
    output p12_y2,
    output p13_y3
);

    assign p10_y0 = (p9_ka & p6_a0) | (p14_kb & p7_b0);
    assign p11_y1 = (p9_ka & p4_a1) | (p14_kb & p5_b1);
    assign p12_y2 = (p9_ka & p2_a2) | (p14_kb & p3_b2);
    assign p13_y3 = (p9_ka & p15_a3) | (p14_kb & p1_b3);

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

10. Pin-connected SystemVerilog

rtl/vector_merge_chips.sv

Download

// Pin connections are generated from the same netlist as the diagrams.
module vector_merge_chips
(
    input        [3:0] a,
    input        [3:0] b,
    input              ka,
    input              kb,
    input        [3:0] mask,
    output       [3:0] shared,
    output       [3:0] masked
);

    logic n1;
    logic n10;
    logic n11;
    logic n12;
    logic n2;
    logic n3;
    logic n4;
    logic n5;
    logic n6;
    logic n7;
    logic n8;
    logic n9;

    cd4019b u1
    (
        .p1_b3        (b [3]),
        .p2_a2        (a [2]),
        .p3_b2        (b [2]),
        .p4_a1        (a [1]),
        .p5_b1        (b [1]),
        .p6_a0        (a [0]),
        .p7_b0        (b [0]),
        .p9_ka        (ka),
        .p10_y0       (shared [0]),
        .p11_y1       (shared [1]),
        .p12_y2       (shared [2]),
        .p13_y3       (shared [3]),
        .p14_kb       (kb),
        .p15_a3       (a [3])
    );

    cd4069ub u2
    (
        .p1_a1        (mask [0]),
        .p2_y1        (n1),
        .p3_a2        (mask [1]),
        .p4_y2        (n4),
        .p5_a3        (mask [2]),
        .p6_y3        (n7),
        .p8_y4        (n10),
        .p9_a4        (mask [3]),
        .p10_y5       (),
        .p11_a5       (1'b0),
        .p12_y6       (),
        .p13_a6       (1'b0)
    );

    cd4081b u3
    (
        .p1_a1        (n1),
        .p2_b1        (a [0]),
        .p3_y1        (n2),
        .p4_y2        (n3),
        .p5_b2        (b [0]),
        .p6_a2        (mask [0]),
        .p8_a3        (n4),
        .p9_b3        (a [1]),
        .p10_y3       (n5),
        .p11_y4       (n6),
        .p12_b4       (b [1]),
        .p13_a4       (mask [1])
    );

    cd4071b u4
    (
        .p1_a1        (n2),
        .p2_b1        (n3),
        .p3_y1        (masked [0]),
        .p4_y2        (masked [1]),
        .p5_b2        (n6),
        .p6_a2        (n5),
        .p8_a3        (n8),
        .p9_b3        (n9),
        .p10_y3       (masked [2]),
        .p11_y4       (masked [3]),
        .p12_b4       (n12),
        .p13_a4       (n11)
    );

    cd4081b u5
    (
        .p1_a1        (n7),
        .p2_b1        (a [2]),
        .p3_y1        (n8),
        .p4_y2        (n9),
        .p5_b2        (b [2]),
        .p6_a2        (mask [2]),
        .p8_a3        (n10),
        .p9_b3        (a [3]),
        .p10_y3       (n11),
        .p11_y4       (n12),
        .p12_b4       (b [3]),
        .p13_a4       (mask [3])
    );

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

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

    logic [3:0] a    = '0;
    logic [3:0] b    = '0;
    logic ka         = '0;
    logic kb         = '0;
    logic [3:0] mask = '0;
    logic [3:0] shared_rtl;
    logic [3:0] shared_chips;
    logic [3:0] masked_rtl;
    logic [3:0] masked_chips;

    vector_merge dut_rtl
    (
        .a            (a),
        .b            (b),
        .ka           (ka),
        .kb           (kb),
        .mask         (mask),
        .shared       (shared_rtl),
        .masked       (masked_rtl)
    );

    vector_merge_chips dut_chips
    (
        .a            (a),
        .b            (b),
        .ka           (ka),
        .kb           (kb),
        .mask         (mask),
        .shared       (shared_chips),
        .masked       (masked_chips)
    );

    initial
    begin
        $readmemh ("vectors.hex", vectors);
        @ (posedge clk);
        # 1;
        for (int i = 0; i < vector_count; i ++)
        begin
            a          = vectors [i] [0 +: 4];
            b          = vectors [i] [4 +: 4];
            ka         = vectors [i] [8 +: 1];
            kb         = vectors [i] [9 +: 1];
            mask       = vectors [i] [10 +: 4];
            expected   = vectors [i] [14 +: 8];
            check_mask = vectors [i] [22 +: 8];
            @ (posedge clk);
            # 1;  // Allow nonblocking state and combinational outputs to settle.
            if (({ masked_rtl, shared_rtl } & check_mask) !== (expected & check_mask))
                $fatal (1, "vector_merge_rtl vector %0d got=%h expected=%h mask=%h", i, { masked_rtl, shared_rtl }, expected, check_mask);
            if (({ masked_chips, shared_chips } & check_mask) !== (expected & check_mask))
                $fatal (1, "vector_merge_chips vector %0d got=%h expected=%h mask=%h", i, { masked_chips, shared_chips }, expected, check_mask);
        end
        $display ("PASS vector_merge: 16384 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_ka         = data [8 +: 1];
    wire i_kb         = data [9 +: 1];
    wire [3:0] i_mask = data [10 +: 4];
    logic [3:0] o_shared;
    logic [3:0] o_masked;
    vector_merge reference_core
    (
        .a            (i_a),
        .b            (i_b),
        .ka           (i_ka),
        .kb           (i_kb),
        .mask         (i_mask),
        .shared       (o_shared),
        .masked       (o_masked)
    );
    assign reference_result = { 24'b0, o_masked, o_shared };
    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_vector_merge # (.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_vector_merge # (.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/.

16384 vectors. Test manifest · Source list

CheckResult
iverilog12PASS
iverilog14PASS
verilatorPASS
integrationsPASS
questa_compilePASS
questa_integrations_compilePASS
yosys_genericPASS
yosys_chipsPASS
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]
kadata[8:8]
kbdata[9:9]
maskdata[13:10]
sharedresult[3:0]
maskedresult[7:4]
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_ka         = data [8 +: 1];
    wire io_kb         = data [9 +: 1];
    wire [3:0] io_mask = data [10 +: 4];
    logic [3:0] io_shared;
    logic [3:0] io_masked;
    generate
        if (CHIP_MODEL == 0)
        begin : generic_design
            vector_merge dut
            (
                .a            (io_a),
                .b            (io_b),
                .ka           (io_ka),
                .kb           (io_kb),
                .mask         (io_mask),
                .shared       (io_shared),
                .masked       (io_masked)
            );
        end
        else
        begin : physical_design
            vector_merge_chips dut
            (
                .a            (io_a),
                .b            (io_b),
                .ka           (io_ka),
                .kb           (io_kb),
                .mask         (io_mask),
                .shared       (io_shared),
                .masked       (io_masked)
            );
        end
    endgenerate
    assign result = { 24'b0, { io_masked, io_shared } };
    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_vector_merge. 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_vector_merge
# (
    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

Use A=0101 and B=1010. Test all KA/KB pairs, then mask=0011 and 1100. Explain which per-bit results shared enables cannot select.

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