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

4051 replaced by decoder + AND/OR

1. Purpose and prerequisites

Tie decoder bit 3 low; gate eight fixed data bits with decoded selects and OR the terms. Compare against C04 by rewiring.

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

4051/4052/4053 are bilateral analog switches; this exercise uses only a digital forward path with INHIBIT=0 and VEE=GND. Their digital models do not reproduce resistance, analog levels or high impedance.

PortDirectionBits
dinput8
selinput3
youtput1
dsely
000
101
200
301
400
501
600
701
040
25571

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

3. Parts you need

PartQuantityLSSK row
CD4028B · DIP128
CD4081B · DIP210
CD4071B · DIP220
830-contact breadboard245
5 V regulated power module156
USB-to-9 V cable to regulator input157
Qualified button channels (sets of five)1166
5 V current-limited LED146
M–M jumper4970
External-module lead (M–F assumed)3771

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

CD4028B

BCD → one-hot 10 · Manufacturer datasheet

PinSignalType
1q4output
2q2output
3q0output
4q7output
5q9output
6q5output
7q6output
8vsssupply
9q8output
10d0input
11d3input
12d2input
13d1input
14q1output
15q3output
16vddsupply

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

4051 replaced by decoder + AND/OR · 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

4051 replaced by decoder + AND/OR · connections
SVG · PNG
DevicePinSignalNet
U11q4select_one[4]
U12q2select_one[2]
U13q0select_one[0]
U14q7select_one[7]
U15q9NC
U16q5select_one[5]
U17q6select_one[6]
U18vssGND
U19q8NC
U110d0sel[0]
U111d3GND
U112d2sel[2]
U113d1sel[1]
U114q1select_one[1]
U115q3select_one[3]
U116vdd+5V
U21a1d[0]
U22b1select_one[0]
U23y1n1
U24y2n2
U25b2select_one[1]
U26a2d[1]
U27vssGND
U28a3d[2]
U29b3select_one[2]
U210y3n3
U211y4n4
U212b4select_one[3]
U213a4d[3]
U214vdd+5V
U31a1d[4]
U32b1select_one[4]
U33y1n5
U34y2n6
U35b2select_one[5]
U36a2d[5]
U37vssGND
U38a3d[6]
U39b3select_one[6]
U310y3n7
U311y4n8
U312b4select_one[7]
U313a4d[7]
U314vdd+5V
U41a1n1
U42b1n2
U43y1n9
U44y2n10
U45b2n3
U46a2n9
U47vssGND
U48a3n10
U49b3n4
U410y3n11
U411y4n12
U412b4n5
U413a4n11
U414vdd+5V
U51a1n12
U52b1n6
U53y1n13
U54y2n14
U55b2n7
U56a2n13
U57vssGND
U58a3n14
U59b3n8
U510y3y
U511y4NC
U512b4GND
U513a4GND
U514vdd+5V

7. Schematic 3 · Breadboard and assembly

4051 replaced by decoder + AND/OR · 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. Hold a one-hot data pattern and sweep SEL; only the selected input reaches the output. Repeat with all-zero/all-one data. For a decoder implementation, inspect the one-hot selection with a high-impedance probe.

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
W015select_one[4]B1.j5B1.j34
W016select_one[2]B1.j6B1.a24
W017select_one[0]B1.j7B1.j20
W018select_one[7]B1.j8B1.a35
W019select_one[5]B1.j10B1.j37
W020select_one[6]B1.j11B1.a38
W021GNDB1.j12B1.BG2
W022GNDB1.a10B1.TG2
W023GNDB1.j25B1.BG3
W024GNDB1.j39B1.BG4
W025GNDB1.j53B1.BG5
W026GNDB2.j11B2.BG2
W027GNDB2.a7B2.TG2
W028GNDB2.a6B2.TG3
W029GNDS1.GNDB1.BG6
W030GNDS2.GNDB2.BG3
W031GNDS3.GNDB1.BG7
W032GNDS4.GNDB2.BG4
W033GNDS5.GNDB1.BG8
W034GNDS6.GNDB2.BG5
W035GNDS7.GNDB1.BG9
W036GNDS8.GNDB2.BG6
W037GNDS9.GNDB1.BG10
W038GNDS10.GNDB2.BG7
W039GNDS11.GNDB1.BG11
W040GNDPWR.GNDB1.BG12
W041GNDL1.KB1.BG13
W042sel[0]B1.a11S9.OUT
W043sel[2]B1.a9S11.OUT
W044sel[1]B1.a8S10.OUT
W045select_one[1]B1.a7B1.j23
W046select_one[3]B1.a6B1.a21
W047+5VB1.a5B1.TP2
W048+5VB1.a19B1.TP3
W049+5VB1.a33B1.TP4
W050+5VB1.a47B1.TP5
W051+5VB2.a5B2.TP2
W052+5VS1.VCCB1.BP2
W053+5VS2.VCCB2.BP2
W054+5VS3.VCCB1.BP3
W055+5VS4.VCCB2.BP3
W056+5VS5.VCCB1.BP4
W057+5VS6.VCCB2.BP4
W058+5VS7.VCCB1.BP5
W059+5VS8.VCCB2.BP5
W060+5VS9.VCCB1.BP6
W061+5VS10.VCCB2.BP6
W062+5VS11.VCCB1.BP7
W063+5VPWR.+5VB1.BP8
W064d[0]B1.j19S1.OUT
W065n1B1.j21B1.j47
W066n2B1.j22B1.j48
W067d[1]B1.j24S2.OUT
W068d[2]B1.a25S3.OUT
W069n3B1.a23B1.j51
W070n4B1.a22B1.a52
W071d[3]B1.a20S4.OUT
W072d[4]B1.j33S5.OUT
W073n5B1.j35B1.a49
W074n6B1.j36B2.j6
W075d[5]B1.j38S6.OUT
W076d[6]B1.a39S7.OUT
W077n7B1.a37B2.j9
W078n8B1.a36B2.a10
W079d[7]B1.a34S8.OUT
W080n9B1.j49B1.j52
W081n10B1.j50B1.a53
W082n11B1.a51B1.a48
W083n12B1.a50B2.j5
W084n13B2.j7B2.j10
W085n14B2.j8B2.a11
W086yB2.a9L1.A

8. Generic synthesizable SystemVerilog

rtl/mux_decoder_gates.sv

Download

// Synthesizable generic design.

module mux_decoder_gates
(
    input        [7:0] d,
    input        [2:0] sel,
    output             y
);

    assign y = d [sel];

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/cd4028b.sv

Download

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

module cd4028b
(
    input  p10_d0,
    input  p11_d3,
    input  p12_d2,
    input  p13_d1,
    output p1_q4,
    output p2_q2,
    output p3_q0,
    output p4_q7,
    output p5_q9,
    output p6_q5,
    output p7_q6,
    output p9_q8,
    output p14_q1,
    output p15_q3
);

    assign p3_q0  = { p11_d3, p12_d2, p13_d1, p10_d0 } == 4'd0;
    assign p14_q1 = { p11_d3, p12_d2, p13_d1, p10_d0 } == 4'd1;
    assign p2_q2  = { p11_d3, p12_d2, p13_d1, p10_d0 } == 4'd2;
    assign p15_q3 = { p11_d3, p12_d2, p13_d1, p10_d0 } == 4'd3;
    assign p1_q4  = { p11_d3, p12_d2, p13_d1, p10_d0 } == 4'd4;
    assign p6_q5  = { p11_d3, p12_d2, p13_d1, p10_d0 } == 4'd5;
    assign p7_q6  = { p11_d3, p12_d2, p13_d1, p10_d0 } == 4'd6;
    assign p4_q7  = { p11_d3, p12_d2, p13_d1, p10_d0 } == 4'd7;
    assign p9_q8  = { p11_d3, p12_d2, p13_d1, p10_d0 } == 4'd8;
    assign p5_q9  = { p11_d3, p12_d2, p13_d1, p10_d0 } == 4'd9;

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/mux_decoder_gates_chips.sv

Download

// Pin connections are generated from the same netlist as the diagrams.
module mux_decoder_gates_chips
(
    input        [7:0] d,
    input        [2:0] sel,
    output             y
);

    logic n1;
    logic n10;
    logic n11;
    logic n12;
    logic n13;
    logic n14;
    logic n2;
    logic n3;
    logic n4;
    logic n5;
    logic n6;
    logic n7;
    logic n8;
    logic n9;
    logic [7:0] select_one;

    cd4028b u1
    (
        .p1_q4        (select_one [4]),
        .p2_q2        (select_one [2]),
        .p3_q0        (select_one [0]),
        .p4_q7        (select_one [7]),
        .p5_q9        (),
        .p6_q5        (select_one [5]),
        .p7_q6        (select_one [6]),
        .p9_q8        (),
        .p10_d0       (sel [0]),
        .p11_d3       (1'b0),
        .p12_d2       (sel [2]),
        .p13_d1       (sel [1]),
        .p14_q1       (select_one [1]),
        .p15_q3       (select_one [3])
    );

    cd4081b u2
    (
        .p1_a1        (d [0]),
        .p2_b1        (select_one [0]),
        .p3_y1        (n1),
        .p4_y2        (n2),
        .p5_b2        (select_one [1]),
        .p6_a2        (d [1]),
        .p8_a3        (d [2]),
        .p9_b3        (select_one [2]),
        .p10_y3       (n3),
        .p11_y4       (n4),
        .p12_b4       (select_one [3]),
        .p13_a4       (d [3])
    );

    cd4081b u3
    (
        .p1_a1        (d [4]),
        .p2_b1        (select_one [4]),
        .p3_y1        (n5),
        .p4_y2        (n6),
        .p5_b2        (select_one [5]),
        .p6_a2        (d [5]),
        .p8_a3        (d [6]),
        .p9_b3        (select_one [6]),
        .p10_y3       (n7),
        .p11_y4       (n8),
        .p12_b4       (select_one [7]),
        .p13_a4       (d [7])
    );

    cd4071b u4
    (
        .p1_a1        (n1),
        .p2_b1        (n2),
        .p3_y1        (n9),
        .p4_y2        (n10),
        .p5_b2        (n3),
        .p6_a2        (n9),
        .p8_a3        (n10),
        .p9_b3        (n4),
        .p10_y3       (n11),
        .p11_y4       (n12),
        .p12_b4       (n5),
        .p13_a4       (n11)
    );

    cd4071b u5
    (
        .p1_a1        (n12),
        .p2_b1        (n6),
        .p3_y1        (n13),
        .p4_y2        (n14),
        .p5_b2        (n7),
        .p6_a2        (n13),
        .p8_a3        (n14),
        .p9_b3        (n8),
        .p10_y3       (y),
        .p11_y4       (),
        .p12_b4       (1'b0),
        .p13_a4       (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 = 2048;
    logic [12:0] vectors [0:vector_count-1];
    logic [0:0] expected, check_mask;
    logic clk = 1'b0;

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

    logic [7:0] d   = '0;
    logic [2:0] sel = '0;
    logic y_rtl;
    logic y_chips;

    mux_decoder_gates dut_rtl
    (
        .d            (d),
        .sel          (sel),
        .y            (y_rtl)
    );

    mux_decoder_gates_chips dut_chips
    (
        .d            (d),
        .sel          (sel),
        .y            (y_chips)
    );

    initial
    begin
        $readmemh ("vectors.hex", vectors);
        @ (posedge clk);
        # 1;
        for (int i = 0; i < vector_count; i ++)
        begin
            d          = vectors [i] [0 +: 8];
            sel        = vectors [i] [8 +: 3];
            expected   = vectors [i] [11 +: 1];
            check_mask = vectors [i] [12 +: 1];
            @ (posedge clk);
            # 1;  // Allow nonblocking state and combinational outputs to settle.
            if (({ y_rtl } & check_mask) !== (expected & check_mask))
                $fatal (1, "mux_decoder_gates_rtl vector %0d got=%h expected=%h mask=%h", i, { y_rtl }, expected, check_mask);
            if (({ y_chips } & check_mask) !== (expected & check_mask))
                $fatal (1, "mux_decoder_gates_chips vector %0d got=%h expected=%h mask=%h", i, { y_chips }, expected, check_mask);
        end
        $display ("PASS mux_decoder_gates: 2048 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 [7:0] i_d   = data [0 +: 8];
    wire [2:0] i_sel = data [8 +: 3];
    logic o_y;
    mux_decoder_gates reference_core
    (
        .d            (i_d),
        .sel          (i_sel),
        .y            (o_y)
    );
    assign reference_result = { 31'b0, o_y };
    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_mux_decoder_gates # (.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_mux_decoder_gates # (.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/.

2048 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
ddata[7:0]
seldata[10:8]
yresult[0:0]
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 [7:0] io_d   = data [0 +: 8];
    wire [2:0] io_sel = data [8 +: 3];
    logic io_y;
    generate
        if (CHIP_MODEL == 0)
        begin : generic_design
            mux_decoder_gates dut
            (
                .d            (io_d),
                .sel          (io_sel),
                .y            (io_y)
            );
        end
        else
        begin : physical_design
            mux_decoder_gates_chips dut
            (
                .d            (io_d),
                .sel          (io_sel),
                .y            (io_y)
            );
        end
    endgenerate
    assign result = { 31'b0, { io_y } };
    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_mux_decoder_gates. 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_mux_decoder_gates
# (
    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

Hold a one-hot data pattern and sweep SEL; only the selected input reaches the output. Repeat with all-zero/all-one data. For a decoder implementation, inspect the one-hot selection with a high-impedance probe.

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