Breadboard exercises/ P4EnglishРусский

Exercise P4 · project · arbiter

Round-robin arbiter: two requests, two grants

Two blocks of a chip want the same memory, and only one may use it at a time. An arbiter decides who goes first, and a round-robin arbiter is fair: when both keep asking, they take turns. One flip-flop and six gates make one, on four chips.

4 chips4013, 4011, 4081, 4071
37 + 16 wireson the board + jumper leads to the modules
1 bit of statewho got the last grant
~1 Hzfrom the 555 clock module

1What it is and why it matters

What it does. Buttons REQUEST 0 and REQUEST 1 are two requesters. LED 1 is grant 0 and LED 2 grant 1. A requester that asks alone gets the grant. When both ask, the one that did not get the last grant wins, so with both buttons held the grants alternate at every blink of the clock. LED 4 shows L, who got the last grant.

How it works. The flip-flop of a 4013 holds L: 1 means that requester 1 got the last grant. The gates compute the grants and the next value of L:

Two NAND gates make x = NAND (r1, ~L) and y = NAND (r0, L), with ~L from the inverted output of the flip-flop. Then g0 = x AND r0 and g1 = r1 AND y. One more trick saves an inverter: L AND y = L AND NOT r0, so the third AND gate reuses y, and an OR gate makes L'.

Where it is used. Arbiters sit wherever several blocks share one resource: processor cores and a memory controller, the ports of a network switch, the units of a GPU and its caches. Writing a round-robin arbiter in SystemVerilog is a classic first question at a job interview for a chip designer.

Why learn it.

2The key elements: a priority that moves

All eight cases:

LREQ 0REQ 1GNT 0GNT 1L'
000000
010100
001011
011011
100001
110100
101011
111100

Waveforms

CLKRSTREQ 0REQ 1GNT 0GNT 1L
From the simulation of tb.sv in Icarus Verilog. One requester alone gets the grant at once; with both requests held, the grants alternate at every clock edge, and L follows the last grant. · SVG · PNG

3Parts

PartIn this circuitQtyPriceSupplier
CD4071quad OR: the next L (one of four used)1$0.25Elexp (elexp.com)
CD4013dual D flip-flop: L, who got the last grant (one of two used)1$0.35Elexp (elexp.com)
CD4081quad AND: the two grants and h (three of four used)1$0.55Elexp (elexp.com)
CD4011quad NAND: x and y (two of four used)1$0.35Elexp (elexp.com)
Button modulesRESET, REQUEST 0 and REQUEST 1; 3 from the pack of five3$0.66 / packaliexpress.us
555 clock modulethe clock, set to about 1 Hz1$0.53aliexpress.us
8-LED moduleGNT 0 and GNT 1 on LEDs 1-2, L on LED 41$0.38aliexpress.us
Breadboard, 830 points1$3.00amazon.com
Breadboard power module5 V on the rails1$1.00aliexpress.us
USB-to-DC cable, 9 Vpowers the module from a USB charger1$1.17aliexpress.us
Pre-cut jumper wires37 wires on the board (or male-male Dupont jumpers)37$11.95 / packjameco.com
Male-female Dupont jumpers16 leads to the clock module, the buttons and the LED module16$1.30 / packaliexpress.us

Prices from the LSSK components sheet; these chips are in the arbiter kit.

4The chips, pin by pin

The chips are CMOS 4000B series: they work from 3 to 18 V, here from the 5 V of the power module. Every input must be connected to something, because a CMOS input left open picks up noise and can switch at random. The drawings show each chip from above with the notch on the left, as in the datasheet: pin 1 at the bottom left, pins numbered counter-clockwise. Blue arrows are inputs, orange arrows are outputs.

4013: dual D flip-flop

1Q12Q13CLK14R15D16S17VSS8S29D210R211CLK212Q213Q214VDD40132 × D flip-flop
CD4013B. · SVG · PNG

As in exercise B4. Flip-flop 1 holds L: D1 (pin 5) gets L', CLOCK1 (pin 3) comes from the clock module, RESET1 (pin 4) from the RESET button, and SET1 (pin 6) is at GND. Q1 (pin 1) is L and ~Q1 (pin 2) is NOT L. The inputs of flip-flop 2 are tied to the rails.

PinNameDirectionFunctionIn this circuit
1Q1outputflip-flop 1 outputL
2Q1outputflip-flop 1 output, invertedL
3CLOCK1inputflip-flop 1 stores D1 on the rising edge, 0 to 1CLK
4RESET1input1 sets Q1 = 0 at once, without the clock. Active highRST
5D1inputflip-flop 1 data inputD
6SET1input1 sets Q1 = 1 at once, without the clock. Active highGND
7VSSpowerground, 0 VGND
8SET2input1 sets Q2 = 1 at once. Active hightied to GND
9D2inputflip-flop 2 data inputtied to GND
10RESET2input1 sets Q2 = 0 at once. Active hightied to GND
11CLOCK2inputflip-flop 2 stores D2 on the rising edgetied to GND
12Q2outputflip-flop 2 output, invertednot connected
13Q2outputflip-flop 2 outputnot connected
14VDDpowersupply, +5 V (3 to 18 V allowed)+5V

4011: quad 2-input NAND

1A2B3J4K5C6D7VSS8E9F10L11M12G13H14VDD4011
CD4011B. · SVG · PNG

Two gates make x and y; the inputs of the other two are tied to the rails.

PinNameDirectionFunctionIn this circuit
1Ainputinput of the gate with its output on pin 3R0
2Binputinput of the gate with its output on pin 3L
3JoutputJ = NOT (A AND B)y
4KoutputK = NOT (C AND D)not connected
5Cinputinput of the gate with its output on pin 4tied to GND
6Dinputinput of the gate with its output on pin 4tied to GND
7VSSpowerground, 0 VGND
8Einputinput of the gate with its output on pin 10tied to +5V
9Finputinput of the gate with its output on pin 10tied to +5V
10LoutputL = NOT (E AND F)not connected
11MoutputM = NOT (G AND H)x
12Ginputinput of the gate with its output on pin 11L
13Hinputinput of the gate with its output on pin 11R1
14VDDpowersupply, +5 V (3 to 18 V allowed)+5V

4081: quad 2-input AND

1A2B3J4K5C6D7VSS8E9F10L11M12G13H14VDD4081
CD4081B. · SVG · PNG

Three gates make g0, g1 and h = L AND y; the inputs of the fourth are tied to GND.

PinNameDirectionFunctionIn this circuit
1Ainputinput of the gate with its output on pin 3L
2Binputinput of the gate with its output on pin 3y
3JoutputJ = A AND Bh
4KoutputK = C AND DG1
5Cinputinput of the gate with its output on pin 4R1
6Dinputinput of the gate with its output on pin 4y
7VSSpowerground, 0 VGND
8Einputinput of the gate with its output on pin 10R0
9Finputinput of the gate with its output on pin 10x
10LoutputL = E AND FG0
11MoutputM = G AND Hnot connected
12Ginputinput of the gate with its output on pin 11tied to +5V
13Hinputinput of the gate with its output on pin 11tied to +5V
14VDDpowersupply, +5 V (3 to 18 V allowed)+5V

4071: quad 2-input OR

1A2B3J4K5C6D7VSS8E9F10L11M12G13H14VDD4071
CD4071B. · SVG · PNG

One gate makes L' = g1 OR h; the inputs of the other three are tied to GND.

PinNameDirectionFunctionIn this circuit
1Ainputinput of the gate with its output on pin 3G1
2Binputinput of the gate with its output on pin 3h
3JoutputJ = A OR BD
4KoutputK = C OR Dnot connected
5Cinputinput of the gate with its output on pin 4tied to GND
6Dinputinput of the gate with its output on pin 4tied to GND
7VSSpowerground, 0 VGND
8Einputinput of the gate with its output on pin 10tied to +5V
9Finputinput of the gate with its output on pin 10tied to +5V
10LoutputL = E OR Fnot connected
11MoutputM = G OR Hnot connected
12Ginputinput of the gate with its output on pin 11tied to +5V
13Hinputinput of the gate with its output on pin 11tied to +5V
14VDDpowersupply, +5 V (3 to 18 V allowed)+5V

5Schematic 1: gates and a flip-flop

CLKRESETREQ 0REQ 1401140114081408140814071DRQQ4013G0LED 1G1LED 2LLED 4
The circuit as gates and a flip-flop. This is what arbiter.sv describes. The lines along the top and the bottom bring NOT L and L back to the gates. · SVG · PNG

6Schematic 2: connections between the chips

123456789101112131440711234567891011121314Q1Q1CLK1R1D1S1S2D2R2CLK2Q2Q240132 × D flip-flop1234567891011121314408112345678910111213144011RSTR0R1LLyxG0G1hDclockRESETREQ 0REQ 1LED module: LEDs 1, 2, 4+5V
The same circuit as chips, pin by pin. · SVG · PNG
NetFromToWire
CLK555 clock module, OUT4013 3 (CLK1)M-F
RSTbutton RESET, OUT4013 4 (R1)M-F
R0button REQ 0, OUT4011 1 (A), 4081 8 (E)M-F, M-M
R1button REQ 1, OUT4011 13 (H), 4081 5 (C)M-F, M-M
L4013 1 (Q1)4011 2 (B), 4081 1 (A), LED module, LED 4M-F, M-M
L4013 2 (Q1)4011 12 (G)M-M
x4011 11 (M)4081 9 (F)M-M
y4011 3 (J)4081 6 (D), 4081 2 (B)M-M
G04081 10 (L)LED module, LED 1M-F, M-M
G14081 4 (K)4071 1 (A), LED module, LED 2M-F, M-M
h4081 3 (J)4071 2 (B)M-M
D4071 3 (J)4013 5 (D1)M-M
+5V+5V4071 14 (VDD), 4013 14 (VDD), 4081 14 (VDD), 4011 14 (VDD), 4071 13 (H), 4071 12 (G), 4071 9 (F), 4071 8 (E), 4081 13 (H), 4081 12 (G), 4011 9 (F), 4011 8 (E)
GNDGND4071 7 (VSS), 4013 7 (VSS), 4081 7 (VSS), 4011 7 (VSS), 4071 5 (C), 4071 6 (D), 4013 8 (S2), 4013 6 (S1), 4013 9 (D2), 4013 10 (R2), 4013 11 (CLK2), 4011 5 (C), 4011 6 (D)
not connected4071 4 (K), 4071 10 (L), 4071 11 (M), 4013 12 (Q2), 4013 13 (Q2), 4081 11 (M), 4011 4 (K), 4011 10 (L)

7Schematic 3: the breadboard

−+−+123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263abcdefghij5V5VPOWER MODULE · 5V9V USB-DC4071401340814011R1LLyyxG0G1hD−OUT+555CLOCK 555~1 Hz+OUT−RESET+OUT−REQ 0+OUT−REQ 1G1G02G134L56788-LED MODULELEDs 1, 2, 4 used
Every wire in its hole. Column numbers are printed along the top; rows a–e are above the gap, f–j below it. The clock module and the three buttons hang above the board, the LED module below it. · SVG · PNG
CLKRSTR0R1LLxyG0G1hD+5VGND

Build it step by step

  1. Do the getting-started steps first: how the breadboard is connected inside, the power module with both jumpers on 5 V, how a chip sits across the gap.
  2. Insert the chips across the gap: the 4071, notch to the left, pin 1 in hole 6f; the 4013 turned around, notch to the right, pin 1 in hole 20e; the 4081, notch to the left, pin 1 in hole 23f; the 4011, notch to the left, pin 1 in hole 32f. Read the numbers on the chips: many look alike. A chip turned around has pin 1 at the top right: it puts the pins this circuit needs next to its neighbours.
  3. Power: for each chip a red wire from its VDD pin (14, or 16 on a 16-pin chip) to the + rail and a black wire from its VSS pin (7 or 8) to the − rail, each to the rail on its own side of the gap. On a chip turned around VDD is on the bottom row and VSS on the top row. Make sure no wire touches + and − at the same time.
  4. The fixed inputs: SET1 of the 4013, the inputs of flip-flop 2 and of the unused gates.
  5. The signal wires, following the checklist: L and NOT L to the NAND gates, the requests to the NAND and AND gates, x and y to the AND gates, g1 and h to the OR gate, L' back to D1. Tick each wire as you insert it.
  6. With the power off, the modules: the clock module, RESET, REQUEST 0 and REQUEST 1 above the board, the LED module below it.
  7. Turn the power on and set the clock module to about one blink per second.

Wiring checklist

·

#NetFromTo
Power (red to +, black to −) · 8 wires
1+5V6a (4071 pin 14, VDD)top + rail, column 6
2GND12j (4071 pin 7, VSS)bottom − rail, column 12
3+5V20j (4013 pin 14, VDD)bottom + rail, column 20
4GND14a (4013 pin 7, VSS)top − rail, column 14
5+5V23a (4081 pin 14, VDD)top + rail, column 23
6GND29j (4081 pin 7, VSS)bottom − rail, column 29
7+5V32a (4011 pin 14, VDD)top + rail, column 32
8GND38j (4011 pin 7, VSS)bottom − rail, column 38
Fixed inputs: CMOS inputs must not float · 17 wires
9+5V7a (4071 pin 13, H)top + rail, column 7
10+5V8a (4071 pin 12, G)top + rail, column 8
11GND10j (4071 pin 5, C)bottom − rail, column 10
12+5V11a (4071 pin 9, F)top + rail, column 11
13GND11j (4071 pin 6, D)bottom − rail, column 11
14+5V12a (4071 pin 8, E)top + rail, column 12
15GND14j (4013 pin 8, S2)bottom − rail, column 14
16GND15a (4013 pin 6, S1)top − rail, column 15
17GND15j (4013 pin 9, D2)bottom − rail, column 15
18GND16j (4013 pin 10, R2)bottom − rail, column 16
19GND17j (4013 pin 11, CLK2)bottom − rail, column 17
20+5V24a (4081 pin 13, H)top + rail, column 24
21+5V25a (4081 pin 12, G)top + rail, column 25
22GND36j (4011 pin 5, C)bottom − rail, column 36
23+5V37a (4011 pin 9, F)top + rail, column 37
24GND37j (4011 pin 6, D)bottom − rail, column 37
25+5V38a (4011 pin 8, E)top + rail, column 38
Signal wires · 12 wires
26R032g (4011 pin 1, A)29d (4081 pin 8, E)
27R133d (4011 pin 13, H)27g (4081 pin 5, C)
28L20d (4013 pin 1, Q1)23g (4081 pin 1, A)
29L23i (4081 pin 1, A)33i (4011 pin 2, B)
30L19b (4013 pin 2, Q1)34b (4011 pin 12, G)
31y34h (4011 pin 3, J)28h (4081 pin 6, D)
32y28g (4081 pin 6, D)24g (4081 pin 2, B)
33x35c (4011 pin 11, M)28c (4081 pin 9, F)
34G027d (4081 pin 10, L)22f
35G126h (4081 pin 4, K)6h (4071 pin 1, A)
36h25g (4081 pin 3, J)7g (4071 pin 2, B)
37D8g (4071 pin 3, J)16d (4013 pin 5, D1)
Modules on male-female jumpers · 16 wires
38GND555 clock module, −top − rail, column 17
39CLK555 clock module, OUT18a (4013 pin 3, CLK1)
40+5V555 clock module, +top + rail, column 19
41+5Vbutton RESET, +top + rail, column 22
42RSTbutton RESET, OUT17a (4013 pin 4, R1)
43GNDbutton RESET, −top − rail, column 24
44+5Vbutton REQ 0, +top + rail, column 30
45R0button REQ 0, OUT29a (4081 pin 8, E)
46GNDbutton REQ 0, −top − rail, column 32
47+5Vbutton REQ 1, +top + rail, column 36
48R1button REQ 1, OUT33a (4011 pin 13, H)
49GNDbutton REQ 1, −top − rail, column 39
50GNDLED module, Gbottom − rail, column 18
51G0LED module, LED 122j
52G1LED module, LED 226j (4081 pin 4, K)
53LLED module, LED 423j (4081 pin 1, A)

Test it

If it does not work: if both grants light together, the NAND gate that makes x gets L instead of NOT L: check the wire from pin 2 of the 4013. If the grants do not alternate, check L' to D1 and the clock wire to pin 3. If L never changes, check the OR gate and h.

8SystemVerilog: the generic design

arbiter.sv writes the grants as two assign statements and the state as an always_ff block that says in words what the gates compute: L becomes 1 after a grant to requester 1, 0 after a grant to requester 0, and stays otherwise. It is synthesizable.

arbiter.svfile
// Exercise P4: a round-robin arbiter for two requesters.
//
// Generic version that matches schematic 1, not tied to any chip.  A grant
// goes to a requester that asks for it; when both ask, the one that did not
// get the last grant wins.  last remembers who got the last grant: 1 means
// requester 1.  The grants follow the requests at once, within the clock
// cycle; last changes at the rising edge of clk.  rst clears last at once,
// without the clock.  Synthesizable.
//
// With both requests held at 1, the grants alternate every clock, so
// neither requester can starve the other.

module arbiter
(
    input  logic       clk,
    input  logic       rst,
    input  logic [1:0] req,
    output logic [1:0] gnt,
    output logic       last   // 1: the last grant went to requester 1
);

    assign gnt [0] = req [0] & ~ (req [1] & ~ last);
    assign gnt [1] = req [1] & ~ (req [0] &   last);

    always_ff @ (posedge clk or posedge rst)
        if (rst)
            last <= 1'b0;
        else if (gnt [1])
            last <= 1'b1;
        else if (gnt [0])
            last <= 1'b0;

endmodule

9SystemVerilog models of the chips

One module per chip, with ports named as in the datasheet and the pin number of each port in a comment. The models leave out the power pins: in SystemVerilog a wire simply has the value 0 or 1.

cd4013.svfile
// CD4013B: dual D flip-flop with set and reset.
//
// Ports in pin order, named as in the datasheet.  The power pins, VSS (7)
// and VDD (14), are left out.  Synthesizable.
//
// Each flip-flop stores D at the rising edge of its clock.  SET and RESET
// are active high and asynchronous: they act at once, without the clock.
// With SET and RESET both high the chip sets both Q and ~Q high; the model
// does the same while they are both high.  Do not release them at the same
// moment: which of the two wins is not defined.

module cd4013
(
    output logic q1,      // Pin 1
    output logic q1_n,    // Pin 2:  ~Q1
    input  logic clock1,  // Pin 3
    input  logic reset1,  // Pin 4:  1 = Q1 goes to 0 at once
    input  logic d1,      // Pin 5
    input  logic set1,    // Pin 6:  1 = Q1 goes to 1 at once
                          // Pin 7:  VSS
    input  logic set2,    // Pin 8
    input  logic d2,      // Pin 9
    input  logic reset2,  // Pin 10
    input  logic clock2,  // Pin 11
    output logic q2_n,    // Pin 12: ~Q2
    output logic q2       // Pin 13
                          // Pin 14: VDD
);

    logic r1, r2;

    always_ff @ (posedge clock1 or posedge reset1 or posedge set1)
        if (reset1)
            r1 <= 1'b0;
        else if (set1)
            r1 <= 1'b1;
        else
            r1 <= d1;

    always_ff @ (posedge clock2 or posedge reset2 or posedge set2)
        if (reset2)
            r2 <= 1'b0;
        else if (set2)
            r2 <= 1'b1;
        else
            r2 <= d2;

    assign q1   =   r1 | (set1 & reset1);
    assign q1_n = ~ r1 | (set1 & reset1);
    assign q2   =   r2 | (set2 & reset2);
    assign q2_n = ~ r2 | (set2 & reset2);

endmodule
cd4011.svfile
// CD4011B: quad 2-input NAND gate.
//
// Ports in pin order, named as in the datasheet.  The power pins, VSS (7)
// and VDD (14), are left out.  Synthesizable.

module cd4011
(
    input  logic a,  // Pin 1
    input  logic b,  // Pin 2
    output logic j,  // Pin 3:  j = ~ (a & b)
    output logic k,  // Pin 4:  k = ~ (c & d)
    input  logic c,  // Pin 5
    input  logic d,  // Pin 6
                     // Pin 7:  VSS
    input  logic e,  // Pin 8
    input  logic f,  // Pin 9
    output logic l,  // Pin 10: l = ~ (e & f)
    output logic m,  // Pin 11: m = ~ (g & h)
    input  logic g,  // Pin 12
    input  logic h   // Pin 13
                     // Pin 14: VDD
);

    assign j = ~ (a & b);
    assign k = ~ (c & d);
    assign l = ~ (e & f);
    assign m = ~ (g & h);

endmodule
cd4081.svfile
// CD4081B: quad 2-input AND gate.
//
// Ports in pin order, named as in the datasheet.  The power pins, VSS (7)
// and VDD (14), are left out.  Synthesizable.
//
// The 4081, 4071 and 4070 have the same pinout: only the function of the
// gates differs.

module cd4081
(
    input  logic a,  // Pin 1
    input  logic b,  // Pin 2
    output logic j,  // Pin 3:  j = a & b
    output logic k,  // Pin 4:  k = c & d
    input  logic c,  // Pin 5
    input  logic d,  // Pin 6
                     // Pin 7:  VSS
    input  logic e,  // Pin 8
    input  logic f,  // Pin 9
    output logic l,  // Pin 10: l = e & f
    output logic m,  // Pin 11: m = g & h
    input  logic g,  // Pin 12
    input  logic h   // Pin 13
                     // Pin 14: VDD
);

    assign j = a & b;
    assign k = c & d;
    assign l = e & f;
    assign m = g & h;

endmodule
cd4071.svfile
// CD4071B: quad 2-input OR gate.
//
// Ports in pin order, named as in the datasheet.  The power pins, VSS (7)
// and VDD (14), are left out.  Synthesizable.

module cd4071
(
    input  logic a,  // Pin 1
    input  logic b,  // Pin 2
    output logic j,  // Pin 3:  j = a | b
    output logic k,  // Pin 4:  k = c | d
    input  logic c,  // Pin 5
    input  logic d,  // Pin 6
                     // Pin 7:  VSS
    input  logic e,  // Pin 8
    input  logic f,  // Pin 9
    output logic l,  // Pin 10: l = e | f
    output logic m,  // Pin 11: m = g | h
    input  logic g,  // Pin 12
    input  logic h   // Pin 13
                     // Pin 14: VDD
);

    assign j = a | b;
    assign k = c | d;
    assign l = e | f;
    assign m = g | h;

endmodule

10SystemVerilog: the chip-level design

arbiter_chips.sv connects the chip models exactly as schematic 2 and the breadboard connect the chips, with the same ports as arbiter.sv. Unused inputs are tied to vdd or gnd as on the breadboard, and unused outputs go to wires named unused_…. The file is generated from the breadboard wire list after a pin-level check of that list, so the code, the drawings and the checklist cannot disagree.

arbiter_chips.svfile
// Exercise P4, chip-level version: the chips and wires of schematic 2 and
// of the breadboard.  Same ports as arbiter.sv.
//
// Generated by tools/bb_lib.py from the checked breadboard wire list;
// change the layout in tools/bb_p4.py, not this file.  Synthesizable.

module arbiter_chips
(
    input  logic       clk,
    input  logic       rst,
    input  logic [1:0] req,
    output logic [1:0] gnt,
    output logic       last
);

    // Connections between the chips that are not ports

    wire h;
    wire d;
    wire lb;
    wire y;
    wire x;

    // The power rails of the breadboard, for tied-off inputs

    wire gnd = 1'b0;
    wire vdd = 1'b1;

    // Outputs that go nowhere

    wire unused_u4_k;
    wire unused_u4_l;
    wire unused_u4_m;
    wire unused_u1_q2_n;
    wire unused_u1_q2;
    wire unused_u3_m;
    wire unused_u2_k;
    wire unused_u2_l;

    //------------------------------------------------------------------------

    // U4: CD4071 quad 2-input OR, pin 1 in column 6

    cd4071 i_u4
    (
        .a ( gnt [1]     ),  // Pin 1 <- U3 pin 4 (K)
        .b ( h           ),  // Pin 2 <- U3 pin 3 (J)
        .j ( d           ),  // Pin 3 -> D
        .k ( unused_u4_k ),  // Pin 4, unused output
        .c ( gnd         ),  // Pin 5 <- GND, unused input
        .d ( gnd         ),  // Pin 6 <- GND, unused input
        .e ( vdd         ),  // Pin 8 <- +5V, unused input
        .f ( vdd         ),  // Pin 9 <- +5V, unused input
        .l ( unused_u4_l ),  // Pin 10, unused output
        .m ( unused_u4_m ),  // Pin 11, unused output
        .g ( vdd         ),  // Pin 12 <- +5V, unused input
        .h ( vdd         )   // Pin 13 <- +5V, unused input
    );

    //------------------------------------------------------------------------

    // U1: CD4013 dual D flip-flop with set and reset, pin 1 in column 14

    cd4013 i_u1
    (
        .q1     ( last           ),  // Pin 1 -> L
        .q1_n   ( lb             ),  // Pin 2 -> ~L
        .clock1 ( clk            ),  // Pin 3 <- 555 clock module
        .reset1 ( rst            ),  // Pin 4 <- button RESET
        .d1     ( d              ),  // Pin 5 <- U4 pin 3 (J)
        .set1   ( gnd            ),  // Pin 6 <- GND
        .set2   ( gnd            ),  // Pin 8 <- GND, unused input
        .d2     ( gnd            ),  // Pin 9 <- GND, unused input
        .reset2 ( gnd            ),  // Pin 10 <- GND, unused input
        .clock2 ( gnd            ),  // Pin 11 <- GND, unused input
        .q2_n   ( unused_u1_q2_n ),  // Pin 12, unused output
        .q2     ( unused_u1_q2   )   // Pin 13, unused output
    );

    //------------------------------------------------------------------------

    // U3: CD4081 quad 2-input AND, pin 1 in column 23

    cd4081 i_u3
    (
        .a ( last        ),  // Pin 1 <- U1 pin 1 (Q1)
        .b ( y           ),  // Pin 2 <- U2 pin 3 (J)
        .j ( h           ),  // Pin 3 -> h
        .k ( gnt [1]     ),  // Pin 4 -> G1
        .c ( req [1]     ),  // Pin 5 <- button REQ 1
        .d ( y           ),  // Pin 6 <- U2 pin 3 (J)
        .e ( req [0]     ),  // Pin 8 <- button REQ 0
        .f ( x           ),  // Pin 9 <- U2 pin 11 (M)
        .l ( gnt [0]     ),  // Pin 10 -> G0
        .m ( unused_u3_m ),  // Pin 11, unused output
        .g ( vdd         ),  // Pin 12 <- +5V, unused input
        .h ( vdd         )   // Pin 13 <- +5V, unused input
    );

    //------------------------------------------------------------------------

    // U2: CD4011 quad 2-input NAND, pin 1 in column 32

    cd4011 i_u2
    (
        .a ( req [0]     ),  // Pin 1 <- button REQ 0
        .b ( last        ),  // Pin 2 <- U1 pin 1 (Q1)
        .j ( y           ),  // Pin 3 -> y
        .k ( unused_u2_k ),  // Pin 4, unused output
        .c ( gnd         ),  // Pin 5 <- GND, unused input
        .d ( gnd         ),  // Pin 6 <- GND, unused input
        .e ( vdd         ),  // Pin 8 <- +5V, unused input
        .f ( vdd         ),  // Pin 9 <- +5V, unused input
        .l ( unused_u2_l ),  // Pin 10, unused output
        .m ( x           ),  // Pin 11 -> x
        .g ( lb          ),  // Pin 12 <- U1 pin 2 (~Q1)
        .h ( req [1]     )   // Pin 13 <- button REQ 1
    );

endmodule

11Testbench and simulation scripts

tb.sv runs both designs side by side and compares them with a reference model written as a priority rule. Like the testbenches of Basics-Graphics-Music, it drives and checks signals only at rising clock edges. It tries every pair of requests with both values of L, checks that the grant changes hands at every edge while both requests stay held, makes 80 clocks of pseudo-random requests, resets in the middle and checks that reset acts at once. The output below is shortened.

tb.sv225 lines · show the codefile
// Testbench for exercise P4.
//
// Runs the generic design (arbiter.sv) and the chip-level design
// (arbiter_chips.sv) side by side and compares both with a reference model
// written as a priority rule: one requester alone gets the grant; when both
// ask, the one that did not get the last grant does.  Works with Icarus
// Verilog, Verilator (--binary) and Questa.
//
// Like the testbenches of Basics-Graphics-Music, it drives and samples
// signals only at rising clock edges.  It tries every request with both
// values of the state, holds both requests for a while to check that the
// grants alternate, makes pseudo-random requests, resets in the middle, and
// checks that reset acts at once.

// Time units.  The design files have none: Yosys does not accept timeunit
// and timeprecision, and the Tiny Tapeout template sets `timescale 1ns / 1ps
// in its own testbench.  This line sets the same units here and for the
// design files compiled after it, so tb.sv comes first in the file list.

`timescale 1ns / 1ps

module tb;

    logic       clk;
    logic       rst;
    logic [1:0] req = '0;

    wire  [1:0] gnt,  gnt_chips;
    wire        last, last_chips;

    arbiter i_generic
    (
        .clk  ( clk  ),
        .rst  ( rst  ),
        .req  ( req  ),
        .gnt  ( gnt  ),
        .last ( last )
    );

    arbiter_chips i_chips
    (
        .clk  ( clk        ),
        .rst  ( rst        ),
        .req  ( req        ),
        .gnt  ( gnt_chips  ),
        .last ( last_chips )
    );

    //------------------------------------------------------------------------
    // Clock with a period of 10 ns.
    // On the breadboard one period is about a second.

    initial
    begin
        clk = '0;

        forever
            # 5 clk = ~ clk;
    end

    //------------------------------------------------------------------------
    // Reference model

    logic last_model;

    wire [1:0] gnt_expected = req != 2'b11 ? req : last_model ? 2'b01 : 2'b10;

    always @ (posedge clk or posedge rst)
        if (rst)
            last_model <= 1'b0;
        else if (gnt_expected != 2'b00)
            last_model <= gnt_expected [1];

    wire ok_generic = gnt       === gnt_expected && last       === last_model;
    wire ok_chips   = gnt_chips === gnt_expected && last_chips === last_model;

    //------------------------------------------------------------------------
    // Checking at every rising clock edge.  At the edge both designs and the
    // model still show the clock cycle that ends there, the same values that
    // a flip-flop would sample.

    // Initial values are fine in a testbench, and the checker samples rst at
    // the clock edge on purpose, although the designs reset asynchronously

    // verilator lint_off PROCASSINIT
    // verilator lint_off SYNCASYNCNET

    logic was_reset = 1'b0;
    logic done      = 1'b0;  // Set by the stimulus before $finish

    int cycle    = 0;
    int n_checks = 0;
    int n_errors = 0;

    logic [7:0] seen = '0;  // { last, req } combinations checked

    // Fairness: while both requests stay held, the grant changes hands at
    // every clock edge

    logic [1:0] req_before = '0, gnt_before = '0;
    int         n_alternations = 0, n_unfair = 0;

    always @ (posedge clk)
    begin
        if (rst)
            was_reset <= 1'b1;

        if ((rst | was_reset) & ~ done)
        begin
            cycle    <= cycle + 1;
            n_checks <= n_checks + 2;
            n_errors <= n_errors + (ok_generic ? 0 : 1) + (ok_chips ? 0 : 1);

            if (~ rst)
                seen [{ last_model, req }] <= 1'b1;

            req_before <= rst ? 2'b00 : req;  // a reset clears L: start counting again
            gnt_before <= gnt;

            if (~ rst & req == 2'b11 & req_before == 2'b11)
            begin
                n_alternations <= n_alternations + 1;
                n_unfair       <= n_unfair + (gnt == gnt_before ? 1 : 0);
            end

            $display ("%5d  %b     %b %b     %b %b     %b   %s  %s",
                cycle + 1, rst, req [1], req [0], gnt [1], gnt [0], last,
                ok_generic ? "ok     " : "WRONG  ", ok_chips ? "ok" : "WRONG");
        end
    end

    //------------------------------------------------------------------------
    // Asynchronous reset: right after rst rises, last must be 0 in both.

    int n_async = 0;

    always @ (posedge rst)
    begin
        # 1;

        if ({ last, last_chips } !== 2'b00)
        begin
            $display ("WRONG: reset did not act at once");
            n_errors <= n_errors + 1;
        end

        n_async <= n_async + 1;
    end

    //------------------------------------------------------------------------
    // Driving the inputs

    logic [31:0] seed = 32'd3;

    initial
    begin
        $dumpfile ("tb.vcd");

        `ifdef __ICARUS__
            $dumpvars (0, tb);  // Without a scope Icarus also tries to dump $unit
        `else
            $dumpvars;
        `endif

        $display ("cycle  rst  REQ 1 0  GNT 1 0  L   generic  chips");

        // Before reset the state is unknown, as after power-up

        rst <= 1'bx;
        repeat (2) @ (posedge clk);
        rst <= 1'b1;
        repeat (2) @ (posedge clk);
        rst <= 1'b0;

        // One requester at a time, then both for a while, then none

        req <= 2'b01;  repeat (2) @ (posedge clk);
        req <= 2'b10;  repeat (2) @ (posedge clk);
        req <= 2'b11;  repeat (6) @ (posedge clk);
        req <= 2'b00;  repeat (2) @ (posedge clk);
        req <= 2'b10;  @ (posedge clk);
        req <= 2'b00;  @ (posedge clk);
        req <= 2'b01;  @ (posedge clk);
        req <= 2'b11;  repeat (5) @ (posedge clk);

        // Reset in the middle, after requester 1 got the last grant

        req <= 2'b10;
        @ (posedge clk);
        rst <= 1'b1;
        req <= 2'b11;
        @ (posedge clk);
        rst <= 1'b0;
        repeat (3) @ (posedge clk);

        // Pseudo-random requests, the same in every simulator

        for (int i = 0; i < 80; i ++)
        begin
            seed = seed * 32'd1664525 + 32'd1013904223;
            req <= seed [31:30];
            @ (posedge clk);
        end

        // Stop checking one clock edge before $finish, so that every
        // simulator reports the same number of checks

        done <= '1;
        @ (posedge clk);

        $finish;
    end

    //------------------------------------------------------------------------

    final
    begin
        if (n_errors == 0 && seen == 8'hff && n_alternations >= 8 && n_unfair == 0 && n_async >= 2)
            $display ("PASS: %0d checks, no errors", n_checks);
        else
            $display ("FAIL: %0d errors in %0d checks, combinations seen %b, %0d of %0d held requests unfair, %0d resets",
                      n_errors, n_checks, seen, n_unfair, n_alternations, n_async);
    end

endmodule

Each script compiles the files, runs the simulation, writes a log and the waveform tb.vcd, and exits with an error unless the test passes. Run them from any directory: ./run_icarus.bash, ./run_verilator.bash or ./run_questa.bash; ./clean.bash removes what they make. The waveform opens in GTKWave or Surfer.

run_icarus.bash22 lines · show the codefile
#!/usr/bin/env bash

# Exercise P4: run the testbench with Icarus Verilog 12 or later.
# The output goes to icarus.log and the waveform to tb.vcd,
# for GTKWave or Surfer.

set -Eeuo pipefail

cd "$(dirname "$0")"

files=(tb.sv arbiter.sv arbiter_chips.sv cd4071.sv cd4013.sv cd4081.sv cd4011.sv)

if ! command -v iverilog > /dev/null
then
    printf '%s\n' "$0: iverilog not found, install Icarus Verilog 12 or later" >&2
    exit 1
fi

iverilog -g2012 -o tb.vvp "${files[@]}"
vvp -n tb.vvp | tee icarus.log

grep -q '^PASS' icarus.log
run_verilator.bash29 lines · show the codefile
#!/usr/bin/env bash

# Exercise P4: run the testbench with Verilator 5.  --binary builds
# a simulator with the testbench's delays; it needs a C++20 compiler.
# The output goes to verilator.log and the waveform to tb.vcd.

set -Eeuo pipefail

cd "$(dirname "$0")"

files=(tb.sv arbiter.sv arbiter_chips.sv cd4071.sv cd4013.sv cd4081.sv cd4011.sv)

# OSS CAD Suite unpacked in the home directory has Verilator too

if ! command -v verilator > /dev/null && [ -x "$HOME/oss-cad-suite/bin/verilator" ]
then
    PATH="$HOME/oss-cad-suite/bin:$PATH"
fi

if ! command -v verilator > /dev/null
then
    printf '%s\n' "$0: verilator not found, install Verilator 5" >&2
    exit 1
fi

verilator --binary -Wall --trace --top-module tb -o tb "${files[@]}"
obj_dir/tb | tee verilator.log

grep -q '^PASS' verilator.log
run_questa.bash64 lines · show the codefile
#!/usr/bin/env bash

# Exercise P4: run the testbench with Questa, for example the free
# Questa Starter Edition that comes with Quartus Prime Lite.
# The output goes to questa.log and the waveform to tb.vcd.

set -Eeuo pipefail

cd "$(dirname "$0")"

files=(tb.sv arbiter.sv arbiter_chips.sv cd4071.sv cd4013.sv cd4081.sv cd4011.sv)

# When vsim is not in PATH, look for it where Quartus installs it

if ! command -v vsim > /dev/null
then
    for dir in "$HOME"/intelFPGA_lite/*/questa_fse/bin  \
               "$HOME"/altera_lite/*/questa_fse/bin     \
               /opt/intelFPGA_lite/*/questa_fse/bin     \
               /opt/altera_lite/*/questa_fse/bin
    do
        if [ -x "$dir/vsim" ]
        then
            PATH="$dir:$PATH"
            break
        fi
    done
fi

if ! command -v vsim > /dev/null
then
    printf '%s\n' "$0: vsim not found, install Questa or add it to PATH" >&2
    exit 1
fi

# The Starter Edition needs a free license file from the Intel
# licensing site; Quartus keeps it in ~/flexlm/license.dat

license="$HOME/flexlm/license.dat"

# ${SALT_LICENSE_SERVER-} is empty when the variable is not set,
# where a plain $SALT_LICENSE_SERVER would stop the script (set -u)
salt_license="${SALT_LICENSE_SERVER-}"

if [ -z "$salt_license" ] && [ -f "$license" ]
then
    export SALT_LICENSE_SERVER="$license"
fi

# Older Questa versions read LM_LICENSE_FILE; ${LM_LICENSE_FILE-}
# is empty when the variable is not set
lm_license="${LM_LICENSE_FILE-}"

if [ -z "$lm_license" ] && [ -f "$license" ]
then
    export LM_LICENSE_FILE="$license"
fi

rm -rf work
vlib work > /dev/null
vlog -sv -lint "${files[@]}"
vsim -c tb -do "run -all; quit -f" | tee questa.log

grep -q 'PASS' questa.log

Tested with Icarus Verilog 12.0 and 14.0 (development), Verilator 5.053 with -Wall and no warnings, and Questa Altera Starter FPGA Edition 2025.2 from Quartus Prime Lite 25.1. The code uses only what Icarus Verilog 13 supports. This is the output, from Icarus Verilog:

VCD info: dumpfile tb.vcd opened for output.
cycle  rst  REQ 1 0  GNT 1 0  L   generic  chips
    1  1     0 0     0 0     0   ok          ok
    2  1     0 0     0 0     0   ok          ok
    3  0     0 1     0 1     0   ok          ok
    4  0     0 1     0 1     0   ok          ok
    5  0     1 0     1 0     0   ok          ok
    6  0     1 0     1 0     1   ok          ok
    7  0     1 1     0 1     1   ok          ok
    8  0     1 1     1 0     0   ok          ok
    9  0     1 1     0 1     1   ok          ok
   10  0     1 1     1 0     0   ok          ok
   11  0     1 1     0 1     1   ok          ok
   12  0     1 1     1 0     0   ok          ok
   13  0     0 0     0 0     1   ok          ok
   14  0     0 0     0 0     1   ok          ok
   15  0     1 0     1 0     1   ok          ok
   16  0     0 0     0 0     1   ok          ok
   17  0     0 1     0 1     1   ok          ok
   18  0     1 1     1 0     0   ok          ok
   19  0     1 1     0 1     1   ok          ok
   20  0     1 1     1 0     0   ok          ok
   21  0     1 1     0 1     1   ok          ok
   22  0     1 1     1 0     0   ok          ok
   23  0     1 0     1 0     1   ok          ok
   24  1     1 1     1 0     0   ok          ok
   25  0     1 1     1 0     0   ok          ok
   26  0     1 1     0 1     1   ok          ok
   27  0     1 1     1 0     0   ok          ok
   28  0     0 0     0 0     1   ok          ok
  …
  106  0     1 1     0 1     1   ok          ok
  107  0     0 1     0 1     0   ok          ok
tb.sv:211: $finish called at 1095000 (1ps)
PASS: 214 checks, no errors

To see that the testbench catches mistakes, break something and run it again: replace ~ last with last in the first assign of arbiter.sv, or connect pin 5 of the 4013 to g1 instead of L' in arbiter_chips.sv. The script reports FAIL and the trace shows the lines marked WRONG.

12On an FPGA board: lab_top in BGM

The Basics-Graphics-Music (BGM) repository runs the same labs on dozens of FPGA boards. Each lab has a lab_top module with the same ports on every board: clock, a 1 Hz slow_clk, reset, keys, LEDs, a 7-segment display.

  1. Copy labs/1_basics/1_10_snail_fsm to a new directory.
  2. Replace its lab_top.sv with bgm_lab/lab_top.sv from this exercise and add arbiter.sv. The BGM scripts compile every .sv file of the directory except tb.sv.
  3. Run ./03_synthesize_for_fpga.bash and ./04_configure_fpga.bash; the first run asks which board you have.

The design runs from slow_clk, which every BGM board makes at 1 Hz, so it steps once a second like the breadboard with the 555 module. BGM makes it in peripherals/slow_clk_gen.sv: a counter on the board clock toggles a flip-flop, and on AMD (Xilinx) and Intel (Altera) FPGAs a BUFG or global primitive puts that signal on the FPGA clock network, so it reaches all flip-flops like a real clock.

Keys 0 and 1 are the requests. LEDs 0 and 1 show the grants and LED 3 shows L. The rightmost digit shows the number of the requester that has the grant and is dark when nobody has it. To run the chip-level design instead, add arbiter_chips.sv, cd4013.sv, cd4011.sv, cd4081.sv and cd4071.sv and change the module name in the instance.

bgm_lab/lab_top.sv106 lines · show the codefile
// Exercise P4 on an FPGA board with the Basics-Graphics-Music (BGM) labs:
// https://github.com/yuri-panchul/basics-graphics-music
//
// Copy labs/1_basics/1_10_snail_fsm to a new directory, replace its
// lab_top.sv with this file and add arbiter.sv.  For the chip-level design
// add arbiter_chips.sv and cd4013.sv, cd4011.sv, cd4081.sv, cd4071.sv
// instead, and change the module name below.
//
// The arbiter runs from slow_clk, 1 Hz on every BGM board, and rst is the
// reset of the board.  Keys 0 and 1 are the requests.  LEDs 0 and 1 show
// the grants, LED 3 shows L.  The rightmost digit shows the number of the
// requester that has the grant, and is dark when none has.

`include "config.svh"

module lab_top
# (
    parameter  clk_mhz       = 50,
               w_key         = 4,
               w_sw          = 8,
               w_led         = 8,
               w_digit       = 8,
               w_gpio        = 100,

               screen_width  = 640,
               screen_height = 480,

               w_red         = 4,
               w_green       = 4,
               w_blue        = 4,

               w_x           = $clog2 ( screen_width  ),
               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 logic [w_led   - 1:0] led,

    // A dynamic seven-segment display

    output logic [          7:0] abcdefgh,
    output logic [w_digit - 1:0] digit,

    // Graphics

    input        [w_x     - 1:0] x,
    input        [w_y     - 1:0] y,

    output logic [w_red   - 1:0] red,
    output logic [w_green - 1:0] green,
    output logic [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
);

    //------------------------------------------------------------------------

       assign red        = '0;
       assign green      = '0;
       assign blue       = '0;
       assign sound      = '0;
       assign uart_tx    = '1;

    //------------------------------------------------------------------------

    logic [1:0] gnt;
    logic       last;

    arbiter i_arbiter
    (
        .clk  ( slow_clk  ),
        .rst  ( rst       ),
        .req  ( key [1:0] ),
        .gnt  ( gnt       ),
        .last ( last      )
    );

    assign led = w_led' ({ last, 1'b0, gnt });

    always_comb
        case (gnt)
        2'b01:   abcdefgh = 8'b1111_1100;  // 0
        2'b10:   abcdefgh = 8'b0110_0000;  // 1
        default: abcdefgh = 8'b0000_0000;  // no grant
        endcase

    assign digit = w_digit' (1);

endmodule

13On an FPGA board: hackathon_top in BGM

The hackathon setup of BGM uses a Tang Nano 9K with a 480×272 screen and a TM1638 board of keys, LEDs and digits. Its top module, hackathon_top, has simpler ports; the first line of the file names the board, so the scripts need no questions.

  1. Copy hackathon/problems/3_d_flip_flop.
  2. Replace its hackathon_top.sv with bgm_hackathon/hackathon_top.sv and add arbiter.sv.
  3. Run ./03_synthesize_for_fpga.bash and ./04_configure_fpga.bash.

Keys 0 and 1 are the requests. The screen shows two columns, one per requester: the request lamp on top in orange, the grant lamp below it in blue. The LEDs and the rightmost digit are as in lab_top.

bgm_hackathon/hackathon_top.sv93 lines · show the codefile
// Board configuration: tang_nano_9k_lcd_480_272_tm1638_hackathon
// This module uses few parameterization and relaxed typing rules

// Exercise P4 in the hackathon setup of Basics-Graphics-Music (BGM):
// https://github.com/yuri-panchul/basics-graphics-music
//
// Copy a directory such as hackathon/problems/3_d_flip_flop, replace its
// hackathon_top.sv with this file and add arbiter.sv.
//
// The arbiter runs from slow_clock.  Keys 0 and 1 are the requests.  The
// LEDs and the rightmost digit are as in lab_top.  The screen shows two
// columns, one per requester: the request lamp on top in orange, the grant
// lamp below it in blue.

module hackathon_top
(
    input  logic       clock,
    input  logic       slow_clock,
    input  logic       reset,

    input  logic [7:0] key,
    output logic [7:0] led,

    // A dynamic seven-segment display

    output logic [7:0] abcdefgh,
    output logic [7:0] digit,

    // LCD screen interface

    input  logic [8:0] x,
    input  logic [8:0] y,

    output logic [4:0] red,
    output logic [5:0] green,
    output logic [4:0] blue,

    inout  logic [3:0] gpio
);

    //------------------------------------------------------------------------

    logic [1:0] gnt;
    logic       last;

    arbiter i_arbiter
    (
        .clk  ( slow_clock ),
        .rst  ( reset      ),
        .req  ( key [1:0]  ),
        .gnt  ( gnt        ),
        .last ( last       )
    );

    assign led = { 4'b0, last, 1'b0, gnt };

    always_comb
        case (gnt)
        2'b01:   abcdefgh = 8'b1111_1100;  // 0
        2'b10:   abcdefgh = 8'b0110_0000;  // 1
        default: abcdefgh = 8'b0000_0000;  // no grant
        endcase

    assign digit = 8'b0000_0001;

    //------------------------------------------------------------------------
    // Requester 0 on the left, requester 1 on the right: the request lamp
    // on top, the grant lamp below it.  Bright for 1, dim for 0.

    always_comb
    begin
        red   = 0;
        green = 0;
        blue  = 0;

        for (int i = 0; i < 2; i ++)
            if (x >= 9' (130 + i * 160) & x < 9' (190 + i * 160))
            begin
                if (y >= 50 & y < 110)
                begin
                    red   = key [i] ? 31 : 6;
                    green = key [i] ? 24 : 5;
                end

                if (y >= 150 & y < 210)
                begin
                    green = gnt [i] ? 40 : 8;
                    blue  = gnt [i] ? 31 : 6;
                end
            end
    end

endmodule

14On a chip: Tiny Tapeout

Tiny Tapeout makes real chips from student designs. Each design is a module with the ports below; on the demo board ui_in comes from DIP switches and uo_out drives a 7-segment display.

The demo board makes the project clock itself and can set it anywhere from 1 Hz to 66.5 MHz (see Clock in the Tiny Tapeout docs). Set it to 1 Hz, and project.v connects clk straight to the arbiter, just as the 555 module clocks the chips on the breadboard.

DIP switches 0 and 1 are the requests. The display shows 0 or 1 for the requester with the grant, and its dot is L. The grants also go to BIDIR pins 0 and 1.

tiny_tapeout/project.v61 lines · show the codefile
/*
 * Exercise P4 on Tiny Tapeout: a round-robin arbiter for two requesters.
 * The 7-segment display of the demo board shows who has the grant.
 *
 * Add arbiter.sv to source_files in info.yaml next to project.v.
 * Rename the module to tt_um_<your GitHub name>_<project name> and use the
 * same name as top_module in info.yaml.
 */

`default_nettype none

module tt_um_example (
    input  wire [7:0] ui_in,    // DIP switches, INPUT Pmod, RP2350 GPIO17-24
    output wire [7:0] uo_out,   // 7-segment display, OUTPUT Pmod, RP2350 GPIO33-40
    input  wire [7:0] uio_in,   // BIDIR Pmod, RP2350 GPIO25-32
    output wire [7:0] uio_out,  //   (same pins)
    output wire [7:0] uio_oe,   //   1 = your design drives the pin
    input  wire       ena,      // set by the on-chip project mux, not a board pin
    input  wire       clk,      // RP2350 GPIO16
    input  wire       rst_n     // RP2350 GPIO14
);

    // The demo board makes the project clock; set it to 1 Hz.  DIP switches
    // 0 and 1 are the requests.  The reset button of the board clears L.

    wire [1:0] gnt;
    wire       last;

    arbiter i_arbiter (
        .clk  ( clk         ),
        .rst  ( ~ rst_n     ),
        .req  ( ui_in [1:0] ),
        .gnt  ( gnt         ),
        .last ( last        )
    );

    // 7-segment display: uo_out [0] is segment a, ..., uo_out [6] is g,
    // uo_out [7] is the dot.  0 or 1 for the requester with the grant, dark
    // when none has it; the dot is L.

    reg [6:0] gfedcba;

    always @(*)
        case (gnt)
        2'b01:   gfedcba = 7'b0111111;  // 0
        2'b10:   gfedcba = 7'b0000110;  // 1
        default: gfedcba = 7'b0000000;  // no grant
        endcase

    assign uo_out  = { last, gfedcba };

    // The grants on BIDIR pins 0-1, for LEDs or a logic analyzer

    assign uio_out = { 6'b0, gnt };
    assign uio_oe  = 8'b0000_0011;

    // List all unused inputs to prevent warnings

    wire _unused = & { ena, ui_in [7:2], uio_in, 1'b0 };

endmodule

In info.yaml set the clock to 1 Hz, list the files and use the module name as the top module:

project:
  clock_hz:   1                 # 1 Hz from the demo board
  top_module: "tt_um_example"   # rename to tt_um_<your GitHub name>_arbiter
  source_files:
    - "project.v"
    - "arbiter.sv"
No clock divider on a chip. BGM makes slow_clk with a counter, but on AMD and Intel FPGAs it routes that signal onto the clock network with BUFG or global (section 12). In an ASIC a counter bit used as a clock is bad practice: it is a second clock with its own clock tree, its edges come later than those of clk by a delay nobody controls, and every signal between the two clocks crosses clock domains. Here the board makes the 1 Hz clock, so no divider is needed. When a chip needs a slow tick from a fast clock, keep one clock and add a clock enable: a pulse one clock long that lets the slow logic change only on the edges where it is 1.

15Going further

Tasks for students

Files of this exercise

Sources

The exercise is in lesson 1.6 of the Habr article about the electronics course and in “Teaching children electronics is due for a major redesign”: pictures 6 and 7 show arbiters built from small-scale chips, one of them soldered by 11-year-old Matvei, picture 8 the idea of a cloud of gates around flip-flops, and picture 16 an arbiter in SystemVerilog. The circuit is the one of the arbiter build guide of the LSSK arbiter kit. The chips are described in the Texas Instruments datasheets for the CD4013B, CD4011B, CD4081B and CD4071B.