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

8-to-3 priority encoder with decimal display

1. Purpose and prerequisites

Assert multiple requests and show the highest selected input number; show valid status separately.

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

D7 has highest priority. No request and D0 both encode 000; VALID distinguishes them. The display shows 0 even when VALID=0.

PortDirectionBits
requestsinput8
enableinput1
codeoutput3
validoutput1
segoutput7
requestsenablecodevalidseg
000063
100063
200063
300063
400063
500063
600063
700063
010063
2551717

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

3. Parts you need

PartQuantityLSSK row
CD4532B · DIP134
CD4511B · DIP17
830-contact breadboard245
5 V regulated power module156
USB-to-9 V cable to regulator input157
Qualified button channels (sets of five)966
5 V current-limited LED146
Common-cathode seven-segment display149
560 Ω resistor, 1/2 W751
M–M jumper3270
External-module lead (M–F assumed)4071

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

CD4511B

BCD → seven segments · Manufacturer datasheet

PinSignalType
1binput
2cinput
3lt_ninput
4bi_ninput
5leinput
6dinput
7ainput
8vsssupply
9seg_eoutput
10seg_doutput
11seg_coutput
12seg_boutput
13seg_aoutput
14seg_goutput
15seg_foutput
16vddsupply

CD4532B

8-input priority encoder · Manufacturer datasheet

PinSignalType
1d4input
2d5input
3d6input
4d7input
5enableinput
6q2output
7q1output
8vsssupply
9q0output
10d0input
11d1input
12d2input
13d3input
14validoutput
15emptyoutput
16vddsupply

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

5. Schematic 1 · Generic logic

8-to-3 priority encoder with decimal display · 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

8-to-3 priority encoder with decimal display · connections
SVG · PNG
DevicePinSignalNet
U11d4requests[4]
U12d5requests[5]
U13d6requests[6]
U14d7requests[7]
U15enableenable
U16q2code[2]
U17q1code[1]
U18vssGND
U19q0code[0]
U110d0requests[0]
U111d1requests[1]
U112d2requests[2]
U113d3requests[3]
U114validvalid
U115emptyNC
U116vdd+5V
U21bcode[1]
U22ccode[2]
U23lt_n+5V
U24bi_n+5V
U25leGND
U26dGND
U27acode[0]
U28vssGND
U29seg_eseg[4]
U210seg_dseg[3]
U211seg_cseg[2]
U212seg_bseg[1]
U213seg_aseg[0]
U214seg_gseg[6]
U215seg_fseg[5]
U216vdd+5V
DisplaySegments a…gCommon pins
DS1seg[0]…seg[6]Both cathodes → GND; dp open

The source inventory does not establish a universal display pinout. Identify a…g, both common cathodes and dp from your exact display datasheet before wiring. The diagram uses functional terminal names.

7. Schematic 3 · Breadboard and assembly

8-to-3 priority encoder with decimal display · 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. Assert requests 1 and 6 together, then release 6; expect 6 then 1. Test no request, D0 alone and ENABLE=0.

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
W015requests[4]B1.j5S5.OUT
W016requests[5]B1.j6S6.OUT
W017requests[6]B1.j7S7.OUT
W018requests[7]B1.j8S8.OUT
W019enableB1.j9S9.OUT
W020code[2]B1.j10B1.j20
W021code[1]B1.j11B1.j19
W022GNDB1.j12B1.BG2
W023GNDB1.j23B1.BG3
W024GNDB1.j24B1.BG4
W025GNDB1.j26B1.BG5
W026GNDS1.GNDB1.BG6
W027GNDS2.GNDB2.BG2
W028GNDS3.GNDB1.BG7
W029GNDS4.GNDB2.BG3
W030GNDS5.GNDB1.BG8
W031GNDS6.GNDB2.BG4
W032GNDS7.GNDB1.BG9
W033GNDS8.GNDB2.BG5
W034GNDS9.GNDB1.BG10
W035GNDPWR.GNDB1.BG11
W036GNDL1.KB1.BG12
W037GNDDS1.COM1B2.BG6
W038GNDDS1.COM2B2.BG7
W039code[0]B1.a12B1.j25
W040requests[0]B1.a11S1.OUT
W041requests[1]B1.a10S2.OUT
W042requests[2]B1.a9S3.OUT
W043requests[3]B1.a8S4.OUT
W044validB1.a7L1.A
W045+5VB1.a5B1.TP2
W046+5VB1.j21B1.BP2
W047+5VB1.j22B1.BP3
W048+5VB1.a19B1.TP3
W049+5VS1.VCCB1.BP4
W050+5VS2.VCCB2.BP2
W051+5VS3.VCCB1.BP5
W052+5VS4.VCCB2.BP3
W053+5VS5.VCCB1.BP6
W054+5VS6.VCCB2.BP4
W055+5VS7.VCCB1.BP7
W056+5VS8.VCCB2.BP5
W057+5VS9.VCCB1.BP8
W058+5VPWR.+5VB1.BP9
W059seg[4]B1.a26B2.a24
W060seg[3]B1.a25B2.a23
W061seg[2]B1.a24B2.a22
W062seg[1]B1.a23B2.a21
W063seg[0]B1.a22B2.a20
W064seg[6]B1.a21B2.a26
W065seg[5]B1.a20B2.a25
W066DS1.aB2.j20DS1.a
W067DS1.bB2.j21DS1.b
W068DS1.cB2.j22DS1.c
W069DS1.dB2.j23DS1.d
W070DS1.eB2.j24DS1.e
W071DS1.fB2.j25DS1.f
W072DS1.gB2.j26DS1.g

8. Generic synthesizable SystemVerilog

rtl/priority_encoder.sv

Download

// Synthesizable generic design.

module priority_encoder
(
    input        [7:0] requests,
    input              enable,
    output logic [2:0] code,
    output             valid,
    output logic [6:0] seg
);

    always_comb
    begin
        code = '0;
        for (int i = 0; i < 8; i ++)
            if (enable && requests [i])
                code = 3' (i);
    end
    assign valid = enable & (| requests);
    always_comb
        case (code)
            0: seg       = 7'h3f;
            1: seg       = 7'h06;
            2: seg       = 7'h5b;
            3: seg       = 7'h4f;
            4: seg       = 7'h66;
            5: seg       = 7'h6d;
            6: seg       = 7'h7c;
            7: seg       = 7'h07;
            8: seg       = 7'h7f;
            9: seg       = 7'h67;
            10: seg      = 7'h00;
            11: seg      = 7'h00;
            12: seg      = 7'h00;
            13: seg      = 7'h00;
            14: seg      = 7'h00;
            15: seg      = 7'h00;
            default: seg = '0;
        endcase

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

Download

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

module cd4511b
# (
    parameter TRANSPARENT = 0
)
(
    input  p1_b,
    input  p2_c,
    input  p3_lt_n,
    input  p4_bi_n,
    input  p5_le,
    input  p6_d,
    input  p7_a,
    output p9_seg_e,
    output p10_seg_d,
    output p11_seg_c,
    output p12_seg_b,
    output p13_seg_a,
    output p14_seg_g,
    output p15_seg_f
);

    logic [3:0] held;
    logic [6:0] segments;

    generate
        if (TRANSPARENT != 0)
        begin : fixed_transparent
            // Physical LE is tied low in the adder lesson.
            assign held = { p6_d, p2_c, p1_b, p7_a };
        end
        else
        begin : controlled_latch
            always_latch
                if (~ p5_le)
                    held = { p6_d, p2_c, p1_b, p7_a };
        end
    endgenerate

    always_comb
    begin
        if (~ p3_lt_n)
            segments = 7'h7f;
        else if (~ p4_bi_n)
            segments = '0;
        else
        begin
            case (held)
            4'd0:     segments = 7'h3f;
            4'd1:     segments = 7'h06;
            4'd2:     segments = 7'h5b;
            4'd3:     segments = 7'h4f;
            4'd4:     segments = 7'h66;
            4'd5:     segments = 7'h6d;
            4'd6:     segments = 7'h7c;
            4'd7:     segments = 7'h07;
            4'd8:     segments = 7'h7f;
            4'd9:     segments = 7'h67;
            default:  segments = '0;
            endcase
        end
    end

    assign { p14_seg_g, p15_seg_f, p9_seg_e, p10_seg_d,
                p11_seg_c, p12_seg_b, p13_seg_a } = segments;

endmodule
models/cd4532b.sv

Download

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

module cd4532b
(
    input  p1_d4,
    input  p2_d5,
    input  p3_d6,
    input  p4_d7,
    input  p5_enable,
    input  p10_d0,
    input  p11_d1,
    input  p12_d2,
    input  p13_d3,
    output p6_q2,
    output p7_q1,
    output p9_q0,
    output p14_valid,
    output p15_empty
);

    logic [2:0] code;
    wire [7:0] requests = { p4_d7, p3_d6, p2_d5, p1_d4, p13_d3, p12_d2, p11_d1, p10_d0 };
    always_comb
    begin
        code = '0;
        for (int i = 0; i < 8; i ++)
            if (p5_enable && requests [i])
                code = 3' (i);
    end
    assign { p6_q2, p7_q1, p9_q0 } = code;
    assign p14_valid               = p5_enable & (| requests);
    assign p15_empty               = p5_enable & ~ (| requests);

endmodule

10. Pin-connected SystemVerilog

rtl/priority_encoder_chips.sv

Download

// Pin connections are generated from the same netlist as the diagrams.
module priority_encoder_chips
(
    input        [7:0] requests,
    input              enable,
    output       [2:0] code,
    output             valid,
    output       [6:0] seg
);


    cd4532b u1
    (
        .p1_d4        (requests [4]),
        .p2_d5        (requests [5]),
        .p3_d6        (requests [6]),
        .p4_d7        (requests [7]),
        .p5_enable    (enable),
        .p6_q2        (code [2]),
        .p7_q1        (code [1]),
        .p9_q0        (code [0]),
        .p10_d0       (requests [0]),
        .p11_d1       (requests [1]),
        .p12_d2       (requests [2]),
        .p13_d3       (requests [3]),
        .p14_valid    (valid),
        .p15_empty    ()
    );

    cd4511b # (.TRANSPARENT (1)) u2
    (
        .p1_b         (code [1]),
        .p2_c         (code [2]),
        .p3_lt_n      (1'b1),
        .p4_bi_n      (1'b1),
        .p5_le        (1'b0),
        .p6_d         (1'b0),
        .p7_a         (code [0]),
        .p9_seg_e     (seg [4]),
        .p10_seg_d    (seg [3]),
        .p11_seg_c    (seg [2]),
        .p12_seg_b    (seg [1]),
        .p13_seg_a    (seg [0]),
        .p14_seg_g    (seg [6]),
        .p15_seg_f    (seg [5])
    );

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

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

    logic [7:0] requests = '0;
    logic enable         = '0;
    logic [2:0] code_rtl;
    logic [2:0] code_chips;
    logic valid_rtl;
    logic valid_chips;
    logic [6:0] seg_rtl;
    logic [6:0] seg_chips;

    priority_encoder dut_rtl
    (
        .requests     (requests),
        .enable       (enable),
        .code         (code_rtl),
        .valid        (valid_rtl),
        .seg          (seg_rtl)
    );

    priority_encoder_chips dut_chips
    (
        .requests     (requests),
        .enable       (enable),
        .code         (code_chips),
        .valid        (valid_chips),
        .seg          (seg_chips)
    );

    initial
    begin
        $readmemh ("vectors.hex", vectors);
        @ (posedge clk);
        # 1;
        for (int i = 0; i < vector_count; i ++)
        begin
            requests   = vectors [i] [0 +: 8];
            enable     = vectors [i] [8 +: 1];
            expected   = vectors [i] [9 +: 11];
            check_mask = vectors [i] [20 +: 11];
            @ (posedge clk);
            # 1;  // Allow nonblocking state and combinational outputs to settle.
            if (({ seg_rtl, valid_rtl, code_rtl } & check_mask) !== (expected & check_mask))
                $fatal (1, "priority_encoder_rtl vector %0d got=%h expected=%h mask=%h", i, { seg_rtl, valid_rtl, code_rtl }, expected, check_mask);
            if (({ seg_chips, valid_chips, code_chips } & check_mask) !== (expected & check_mask))
                $fatal (1, "priority_encoder_chips vector %0d got=%h expected=%h mask=%h", i, { seg_chips, valid_chips, code_chips }, expected, check_mask);
        end
        $display ("PASS priority_encoder: 512 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_requests = data [0 +: 8];
    wire i_enable         = data [8 +: 1];
    logic [2:0] o_code;
    logic o_valid;
    logic [6:0] o_seg;
    priority_encoder reference_core
    (
        .requests     (i_requests),
        .enable       (i_enable),
        .code         (o_code),
        .valid        (o_valid),
        .seg          (o_seg)
    );
    assign reference_result = { 21'b0, o_seg, o_valid, o_code };
    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 = { 7'b0, 7'b0, o_seg };
    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_priority_encoder # (.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_priority_encoder # (.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/.

512 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
requestsdata[7:0]
enabledata[8:8]
coderesult[2:0]
validresult[3:3]
segresult[10: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 [7:0] io_requests = data [0 +: 8];
    wire io_enable         = data [8 +: 1];
    logic [2:0] io_code;
    logic io_valid;
    logic [6:0] io_seg;
    generate
        if (CHIP_MODEL == 0)
        begin : generic_design
            priority_encoder dut
            (
                .requests     (io_requests),
                .enable       (io_enable),
                .code         (io_code),
                .valid        (io_valid),
                .seg          (io_seg)
            );
        end
        else
        begin : physical_design
            priority_encoder_chips dut
            (
                .requests     (io_requests),
                .enable       (io_enable),
                .code         (io_code),
                .valid        (io_valid),
                .seg          (io_seg)
            );
        end
    endgenerate
    assign result   = { 21'b0, { io_seg, io_valid, io_code } };
    assign segments = { 7'b0, 7'b0, io_seg };
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_priority_encoder. 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_priority_encoder
# (
    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

Assert requests 1 and 6 together, then release 6; expect 6 then 1. Test no request, D0 alone and ENABLE=0.

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