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

Nibble comparator: MSB first

1. Purpose and prerequisites

Use 4063 for each input nibble pair and two state bits to remember the first unequal nibble. The assembled clock advances the comparison; RESET starts a word.

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

The LT and GT lamps use the unused complementary outputs of the 4013: anodes to +5 V, cathodes to lt_n_led/gt_n_led. A lit lamp means the corresponding positive flag is 1. Both off means EQ. This leaves the state-feedback nets free of LED load.

The first unequal chunk fixes the result; later chunks cannot overturn it. Reset to EQ before every word. All chunks advance together; there is no implicit word boundary.

PortDirectionBits
clkinput1
rstinput1
ainput4
binput4
ltoutput1
eqoutput1
gtoutput1
Nibble comparator: MSB first · states
SVG · PNG
Nibble comparator: MSB first · timing
SVG · PNG

3. Parts you need

PartQuantityLSSK row
CD4063B · DIP132
CD4071B · DIP120
CD4069UB · DIP124
CD4081B · DIP110
CD4013B · DIP13
830-contact breadboard245
5 V regulated power module156
USB-to-9 V cable to regulator input157
Qualified button channels (sets of five)966
Assembled 555 clock module154
5 V current-limited LED246
M–M jumper5570
External-module lead (M–F assumed)3671

CSV · Jumper schedule · Pin/net data

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

4. Chip functions, pins and polarity

CD4013B

CD4013B · Manufacturer datasheet

PinSignalType
1q1output
2qn1output
3clk1input
4rst1input
5d1input
6set1input
7vsssupply
8set2input
9d2input
10rst2input
11clk2input
12qn2output
13q2output
14vddsupply

CD4063B

4-bit compare + cascade · Manufacturer datasheet

PinSignalType
1b3input
2lt_ininput
3eq_ininput
4gt_ininput
5gtoutput
6eqoutput
7ltoutput
8vsssupply
9b0input
10a0input
11b1input
12a1input
13a2input
14b2input
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

Nibble comparator: MSB first · 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

Nibble comparator: MSB first · connections
SVG · PNG
DevicePinSignalNet
U11b3b[3]
U12lt_inGND
U13eq_in+5V
U14gt_inGND
U15gtchunk_gt
U16eqchunk_eq
U17ltchunk_lt
U18vssGND
U19b0b[0]
U110a0a[0]
U111b1b[1]
U112a1a[1]
U113a2a[2]
U114b2b[2]
U115a3a[3]
U116vdd+5V
U21a1lt
U22b1gt
U23y1n1
U24y2n3
U25b2n2
U26a2lt
U27vssGND
U28a3gt
U29b3n4
U210y3n5
U211y4NC
U212b4GND
U213a4GND
U214vdd+5V
U31a1n1
U32y1eq
U33a2GND
U34y2NC
U35a3GND
U36y3NC
U37vssGND
U38y4NC
U39a4GND
U310y5NC
U311a5GND
U312y6NC
U313a6GND
U314vdd+5V
U41a1eq
U42b1chunk_lt
U43y1n2
U44y2n4
U45b2chunk_gt
U46a2eq
U47vssGND
U48a3GND
U49b3GND
U410y3NC
U411y4NC
U412b4GND
U413a4GND
U414vdd+5V
U51q1lt
U52qn1lt_n_led
U53clk1clk
U54rst1rst
U55d1n3
U56set1GND
U57vssGND
U58set2GND
U59d2n5
U510rst2rst
U511clk2clk
U512qn2gt_n_led
U513q2gt
U514vdd+5V

7. Schematic 3 · Breadboard and assembly

Nibble comparator: MSB first · 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. Compare 0xA5 with 0xA3, then reverse the operands and compare equal words. Feed bits/nibbles in the specified order and inspect the result after the last chunk.

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.j5S9.OUT
W016GNDB1.j6B1.BG2
W017GNDB1.j8B1.BG3
W018GNDB1.j12B1.BG4
W019GNDB1.j25B1.BG5
W020GNDB1.a21B1.TG2
W021GNDB1.a20B1.TG3
W022GNDB1.j35B1.BG6
W023GNDB1.j37B1.BG7
W024GNDB1.j39B1.BG8
W025GNDB1.a38B1.TG4
W026GNDB1.a36B1.TG5
W027GNDB1.a34B1.TG6
W028GNDB1.j53B1.BG9
W029GNDB1.a53B1.TG7
W030GNDB1.a52B1.TG8
W031GNDB1.a49B1.TG9
W032GNDB1.a48B1.TG10
W033GNDB2.j10B2.BG2
W034GNDB2.j11B2.BG3
W035GNDB2.a11B2.TG2
W036GNDS1.GNDB1.BG10
W037GNDS2.GNDB2.BG4
W038GNDS3.GNDB1.BG11
W039GNDS4.GNDB2.BG5
W040GNDS5.GNDB1.BG12
W041GNDS6.GNDB2.BG6
W042GNDS7.GNDB1.BG13
W043GNDS8.GNDB2.BG7
W044GNDS9.GNDB1.BG14
W045GNDCLK.GNDB1.BG15
W046GNDPWR.GNDB1.BG16
W047+5VB1.j7B1.BP2
W048+5VB1.a5B1.TP2
W049+5VB1.a19B1.TP3
W050+5VB1.a33B1.TP4
W051+5VB1.a47B1.TP5
W052+5VB2.a5B2.TP2
W053+5VS1.VCCB1.BP3
W054+5VS2.VCCB2.BP2
W055+5VS3.VCCB1.BP4
W056+5VS4.VCCB2.BP3
W057+5VS5.VCCB1.BP5
W058+5VS6.VCCB2.BP4
W059+5VS7.VCCB1.BP6
W060+5VS8.VCCB2.BP5
W061+5VS9.VCCB1.BP7
W062+5VCLK.VCCB1.BP8
W063+5VPWR.+5VB1.BP9
W064+5VL1.AB1.BP10
W065+5VL2.AB2.BP6
W066chunk_gtB1.j9B1.j51
W067chunk_ltB1.j11B1.j48
W068b[0]B1.a12S6.OUT
W069a[0]B1.a11S2.OUT
W070b[1]B1.a10S7.OUT
W071a[1]B1.a9S3.OUT
W072a[2]B1.a8S4.OUT
W073b[2]B1.a7S8.OUT
W074a[3]B1.a6S5.OUT
W075ltB1.j19B1.j24
W076ltB1.i24B2.j5
W077gtB1.j20B1.a25
W078gtB1.b25B2.a6
W079n1B1.j21B1.j33
W080n3B1.j22B2.j9
W081n2B1.j23B1.j49
W082n4B1.a24B1.j50
W083n5B1.a23B2.a10
W084eqB1.j34B1.j47
W085eqB1.i47B1.j52
W086lt_n_ledB2.j6L1.K
W087clkB2.j7B2.a8
W088clkB2.b8CLK.OUT
W089rstB2.j8B2.a9
W090rstB2.b9S1.OUT
W091gt_n_ledB2.a7L2.K

8. Generic synthesizable SystemVerilog

rtl/nibble_compare_msb.sv

Download

// Synthesizable generic design.

module nibble_compare_msb
(
    input              clk,
    input              rst,
    input        [3:0] a,
    input        [3:0] b,
    output logic       lt,
    output             eq,
    output logic       gt
);

    always_ff @ (posedge clk or posedge rst)
        if (rst)
        begin
            lt <= 1'b0;
            gt <= 1'b0;
        end
        else if (eq)
        begin
            lt <= a < b;
            gt <= a > b;
        end
    assign eq = ~ (lt | gt);

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

Download

// Ideal functional model. SET_TIED_LOW=1 is synthesizable with the
// supplied toolchain. General SET/RESET branch is a simulation model.
// SET and RESET simultaneously high is forbidden in this lesson.
// The model shows both outputs high then; release behavior is unspecified.

module cd4013b
# (
    parameter SET_TIED_LOW = 0
)
(
    input        p3_clk1,
    input        p4_rst1,
    input        p5_d1,
    input        p6_set1,
    input        p8_set2,
    input        p9_d2,
    input        p10_rst2,
    input        p11_clk2,
    output logic p1_q1,
    output       p2_qn1,
    output       p12_qn2,
    output logic p13_q2
);

    generate
        if (SET_TIED_LOW != 0)
        begin : reset_only
            always_ff @ (posedge p3_clk1 or posedge p4_rst1)
                if (p4_rst1)
                    p1_q1 <= 1'b0;
                else
                    p1_q1 <= p5_d1;

            assign p2_qn1 = ~ p1_q1;

            always_ff @ (posedge p11_clk2 or posedge p10_rst2)
                if (p10_rst2)
                    p13_q2 <= 1'b0;
                else
                    p13_q2 <= p9_d2;

            assign p12_qn2 = ~ p13_q2;

        end
        else
        begin : set_reset_model
            always_ff @ (posedge p3_clk1 or posedge p4_rst1 or posedge p6_set1)
                if (p6_set1)
                    p1_q1 <= 1'b1;
                else if (p4_rst1)
                    p1_q1 <= 1'b0;
                else
                    p1_q1 <= p5_d1;

            assign p2_qn1 = (p6_set1 & p4_rst1) | ~ p1_q1;

            always_ff @ (posedge p11_clk2 or posedge p10_rst2 or posedge p8_set2)
                if (p8_set2)
                    p13_q2 <= 1'b1;
                else if (p10_rst2)
                    p13_q2 <= 1'b0;
                else
                    p13_q2 <= p9_d2;

            assign p12_qn2 = (p8_set2 & p10_rst2) | ~ p13_q2;

        end
    endgenerate

endmodule
models/cd4063b.sv

Download

// Synthesizable ideal digital model; power and propagation delays omitted.
// Cascade inputs are mutually exclusive: LT/EQ/GT = 100, 010 or 001.

module cd4063b
(
    input  p1_b3,
    input  p2_lt_in,
    input  p3_eq_in,
    input  p4_gt_in,
    input  p9_b0,
    input  p10_a0,
    input  p11_b1,
    input  p12_a1,
    input  p13_a2,
    input  p14_b2,
    input  p15_a3,
    output p5_gt,
    output p6_eq,
    output p7_lt
);

    wire [3:0] a = { p15_a3, p13_a2, p12_a1, p10_a0 };
    wire [3:0] b = { p1_b3, p14_b2, p11_b1, p9_b0 };
    assign p7_lt = a == b ? p2_lt_in : (a < b);
    assign p6_eq = a == b ? p3_eq_in : (a == b);
    assign p5_gt = a == b ? p4_gt_in : (a > b);

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

Download

// Pin connections are generated from the same netlist as the diagrams.
module nibble_compare_msb_chips
(
    input              clk,
    input              rst,
    input        [3:0] a,
    input        [3:0] b,
    output             lt,
    output             eq,
    output             gt
);

    logic chunk_eq;
    logic chunk_gt;
    logic chunk_lt;
    logic gt_n_led;
    logic lt_n_led;
    logic n1;
    logic n2;
    logic n3;
    logic n4;
    logic n5;

    cd4063b u1
    (
        .p1_b3        (b [3]),
        .p2_lt_in     (1'b0),
        .p3_eq_in     (1'b1),
        .p4_gt_in     (1'b0),
        .p5_gt        (chunk_gt),
        .p6_eq        (chunk_eq),
        .p7_lt        (chunk_lt),
        .p9_b0        (b [0]),
        .p10_a0       (a [0]),
        .p11_b1       (b [1]),
        .p12_a1       (a [1]),
        .p13_a2       (a [2]),
        .p14_b2       (b [2]),
        .p15_a3       (a [3])
    );

    cd4071b u2
    (
        .p1_a1        (lt),
        .p2_b1        (gt),
        .p3_y1        (n1),
        .p4_y2        (n3),
        .p5_b2        (n2),
        .p6_a2        (lt),
        .p8_a3        (gt),
        .p9_b3        (n4),
        .p10_y3       (n5),
        .p11_y4       (),
        .p12_b4       (1'b0),
        .p13_a4       (1'b0)
    );

    cd4069ub u3
    (
        .p1_a1        (n1),
        .p2_y1        (eq),
        .p3_a2        (1'b0),
        .p4_y2        (),
        .p5_a3        (1'b0),
        .p6_y3        (),
        .p8_y4        (),
        .p9_a4        (1'b0),
        .p10_y5       (),
        .p11_a5       (1'b0),
        .p12_y6       (),
        .p13_a6       (1'b0)
    );

    cd4081b u4
    (
        .p1_a1        (eq),
        .p2_b1        (chunk_lt),
        .p3_y1        (n2),
        .p4_y2        (n4),
        .p5_b2        (chunk_gt),
        .p6_a2        (eq),
        .p8_a3        (1'b0),
        .p9_b3        (1'b0),
        .p10_y3       (),
        .p11_y4       (),
        .p12_b4       (1'b0),
        .p13_a4       (1'b0)
    );

    cd4013b # (.SET_TIED_LOW (1)) u5
    (
        .p1_q1        (lt),
        .p2_qn1       (lt_n_led),
        .p3_clk1      (clk),
        .p4_rst1      (rst),
        .p5_d1        (n3),
        .p6_set1      (1'b0),
        .p8_set2      (1'b0),
        .p9_d2        (n5),
        .p10_rst2     (rst),
        .p11_clk2     (clk),
        .p12_qn2      (gt_n_led),
        .p13_q2       (gt)
    );

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 = 375;
    logic [14:0] vectors [0:vector_count-1];
    logic [2: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 lt_rtl;
    logic lt_chips;
    logic eq_rtl;
    logic eq_chips;
    logic gt_rtl;
    logic gt_chips;

    nibble_compare_msb dut_rtl
    (
        .clk          (clk),
        .rst          (rst),
        .a            (a),
        .b            (b),
        .lt           (lt_rtl),
        .eq           (eq_rtl),
        .gt           (gt_rtl)
    );

    nibble_compare_msb_chips dut_chips
    (
        .clk          (clk),
        .rst          (rst),
        .a            (a),
        .b            (b),
        .lt           (lt_chips),
        .eq           (eq_chips),
        .gt           (gt_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];
            expected   = vectors [i] [9 +: 3];
            check_mask = vectors [i] [12 +: 3];
            @ (posedge clk);
            # 1;  // Allow nonblocking state and combinational outputs to settle.
            if (({ gt_rtl, eq_rtl, lt_rtl } & check_mask) !== (expected & check_mask))
                $fatal (1, "nibble_compare_msb_rtl vector %0d got=%h expected=%h mask=%h", i, { gt_rtl, eq_rtl, lt_rtl }, expected, check_mask);
            if (({ gt_chips, eq_chips, lt_chips } & check_mask) !== (expected & check_mask))
                $fatal (1, "nibble_compare_msb_chips vector %0d got=%h expected=%h mask=%h", i, { gt_chips, eq_chips, lt_chips }, expected, check_mask);
        end
        $display ("PASS nibble_compare_msb: 375 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];
    logic o_lt;
    logic o_eq;
    logic o_gt;
    nibble_compare_msb reference_core
    (
        .clk          (clk),
        .rst          (rst),
        .a            (i_a),
        .b            (i_b),
        .lt           (o_lt),
        .eq           (o_eq),
        .gt           (o_gt)
    );
    assign reference_result = { 29'b0, o_gt, o_eq, o_lt };
    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_nibble_compare_msb # (.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_nibble_compare_msb # (.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/.

375 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]
ltresult[0:0]
eqresult[1:1]
gtresult[2:2]
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];
    logic io_lt;
    logic io_eq;
    logic io_gt;
    generate
        if (CHIP_MODEL == 0)
        begin : generic_design
            nibble_compare_msb dut
            (
                .clk          (clk),
                .rst          (rst),
                .a            (io_a),
                .b            (io_b),
                .lt           (io_lt),
                .eq           (io_eq),
                .gt           (io_gt)
            );
        end
        else
        begin : physical_design
            nibble_compare_msb_chips dut
            (
                .clk          (clk),
                .rst          (rst),
                .a            (io_a),
                .b            (io_b),
                .lt           (io_lt),
                .eq           (io_eq),
                .gt           (io_gt)
            );
        end
    endgenerate
    assign result = { 29'b0, { io_gt, io_eq, io_lt } };
    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_nibble_compare_msb. 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_nibble_compare_msb
# (
    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

Compare 0xA5 with 0xA3, then reverse the operands and compare equal words. Feed bits/nibbles in the specified order and inspect the result after the last chunk.

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