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.
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:
- g0 = r0 AND NOT (r1 AND NOT L): requester 0 gets it unless requester 1 also asks and has priority;
- g1 = r1 AND NOT (r0 AND L): the same, the other way round;
- L' = g1 OR (L AND NOT r0): L becomes 1 when requester 1 gets the grant, 0 when requester 0 gets it, and stays when nobody does.
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.
- It is the main idea of digital design in one small circuit: a cloud of gates computes the outputs and the next state from the inputs and the state kept in a flip-flop.
- Its outputs follow the inputs at once, within the clock cycle, while the state changes only at the clock edge.
- It shows what fairness and starvation mean in hardware.
2The key elements: a priority that moves
All eight cases:
| L | REQ 0 | REQ 1 | GNT 0 | GNT 1 | L' |
|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 | 0 |
| 0 | 1 | 0 | 1 | 0 | 0 |
| 0 | 0 | 1 | 0 | 1 | 1 |
| 0 | 1 | 1 | 0 | 1 | 1 |
| 1 | 0 | 0 | 0 | 0 | 1 |
| 1 | 1 | 0 | 1 | 0 | 0 |
| 1 | 0 | 1 | 0 | 1 | 1 |
| 1 | 1 | 1 | 1 | 0 | 0 |
Waveforms
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 · PNG3Parts
| Part | In this circuit | Qty | Price | Supplier |
|---|---|---|---|---|
| CD4071 | quad OR: the next L (one of four used) | 1 | $0.25 | Elexp (elexp.com) |
| CD4013 | dual D flip-flop: L, who got the last grant (one of two used) | 1 | $0.35 | Elexp (elexp.com) |
| CD4081 | quad AND: the two grants and h (three of four used) | 1 | $0.55 | Elexp (elexp.com) |
| CD4011 | quad NAND: x and y (two of four used) | 1 | $0.35 | Elexp (elexp.com) |
| Button modules | RESET, REQUEST 0 and REQUEST 1; 3 from the pack of five | 3 | $0.66 / pack | aliexpress.us |
| 555 clock module | the clock, set to about 1 Hz | 1 | $0.53 | aliexpress.us |
| 8-LED module | GNT 0 and GNT 1 on LEDs 1-2, L on LED 4 | 1 | $0.38 | aliexpress.us |
| Breadboard, 830 points | 1 | $3.00 | amazon.com | |
| Breadboard power module | 5 V on the rails | 1 | $1.00 | aliexpress.us |
| USB-to-DC cable, 9 V | powers the module from a USB charger | 1 | $1.17 | aliexpress.us |
| Pre-cut jumper wires | 37 wires on the board (or male-male Dupont jumpers) | 37 | $11.95 / pack | jameco.com |
| Male-female Dupont jumpers | 16 leads to the clock module, the buttons and the LED module | 16 | $1.30 / pack | aliexpress.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
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.
| Pin | Name | Direction | Function | In this circuit |
|---|---|---|---|---|
| 1 | Q1 | output | flip-flop 1 output | L |
| 2 | Q1 | output | flip-flop 1 output, inverted | L |
| 3 | CLOCK1 | input | flip-flop 1 stores D1 on the rising edge, 0 to 1 | CLK |
| 4 | RESET1 | input | 1 sets Q1 = 0 at once, without the clock. Active high | RST |
| 5 | D1 | input | flip-flop 1 data input | D |
| 6 | SET1 | input | 1 sets Q1 = 1 at once, without the clock. Active high | GND |
| 7 | VSS | power | ground, 0 V | GND |
| 8 | SET2 | input | 1 sets Q2 = 1 at once. Active high | tied to GND |
| 9 | D2 | input | flip-flop 2 data input | tied to GND |
| 10 | RESET2 | input | 1 sets Q2 = 0 at once. Active high | tied to GND |
| 11 | CLOCK2 | input | flip-flop 2 stores D2 on the rising edge | tied to GND |
| 12 | Q2 | output | flip-flop 2 output, inverted | not connected |
| 13 | Q2 | output | flip-flop 2 output | not connected |
| 14 | VDD | power | supply, +5 V (3 to 18 V allowed) | +5V |
4011: quad 2-input NAND
| Pin | Name | Direction | Function | In this circuit |
|---|---|---|---|---|
| 1 | A | input | input of the gate with its output on pin 3 | R0 |
| 2 | B | input | input of the gate with its output on pin 3 | L |
| 3 | J | output | J = NOT (A AND B) | y |
| 4 | K | output | K = NOT (C AND D) | not connected |
| 5 | C | input | input of the gate with its output on pin 4 | tied to GND |
| 6 | D | input | input of the gate with its output on pin 4 | tied to GND |
| 7 | VSS | power | ground, 0 V | GND |
| 8 | E | input | input of the gate with its output on pin 10 | tied to +5V |
| 9 | F | input | input of the gate with its output on pin 10 | tied to +5V |
| 10 | L | output | L = NOT (E AND F) | not connected |
| 11 | M | output | M = NOT (G AND H) | x |
| 12 | G | input | input of the gate with its output on pin 11 | L |
| 13 | H | input | input of the gate with its output on pin 11 | R1 |
| 14 | VDD | power | supply, +5 V (3 to 18 V allowed) | +5V |
4081: quad 2-input AND
Three gates make g0, g1 and h = L AND y; the inputs of the fourth are tied to GND.
| Pin | Name | Direction | Function | In this circuit |
|---|---|---|---|---|
| 1 | A | input | input of the gate with its output on pin 3 | L |
| 2 | B | input | input of the gate with its output on pin 3 | y |
| 3 | J | output | J = A AND B | h |
| 4 | K | output | K = C AND D | G1 |
| 5 | C | input | input of the gate with its output on pin 4 | R1 |
| 6 | D | input | input of the gate with its output on pin 4 | y |
| 7 | VSS | power | ground, 0 V | GND |
| 8 | E | input | input of the gate with its output on pin 10 | R0 |
| 9 | F | input | input of the gate with its output on pin 10 | x |
| 10 | L | output | L = E AND F | G0 |
| 11 | M | output | M = G AND H | not connected |
| 12 | G | input | input of the gate with its output on pin 11 | tied to +5V |
| 13 | H | input | input of the gate with its output on pin 11 | tied to +5V |
| 14 | VDD | power | supply, +5 V (3 to 18 V allowed) | +5V |
4071: quad 2-input OR
| Pin | Name | Direction | Function | In this circuit |
|---|---|---|---|---|
| 1 | A | input | input of the gate with its output on pin 3 | G1 |
| 2 | B | input | input of the gate with its output on pin 3 | h |
| 3 | J | output | J = A OR B | D |
| 4 | K | output | K = C OR D | not connected |
| 5 | C | input | input of the gate with its output on pin 4 | tied to GND |
| 6 | D | input | input of the gate with its output on pin 4 | tied to GND |
| 7 | VSS | power | ground, 0 V | GND |
| 8 | E | input | input of the gate with its output on pin 10 | tied to +5V |
| 9 | F | input | input of the gate with its output on pin 10 | tied to +5V |
| 10 | L | output | L = E OR F | not connected |
| 11 | M | output | M = G OR H | not connected |
| 12 | G | input | input of the gate with its output on pin 11 | tied to +5V |
| 13 | H | input | input of the gate with its output on pin 11 | tied to +5V |
| 14 | VDD | power | supply, +5 V (3 to 18 V allowed) | +5V |
5Schematic 1: gates and a flip-flop
arbiter.sv describes. The lines
along the top and the bottom bring NOT L and L back to the gates. · SVG · PNG6Schematic 2: connections between the chips
| Net | From | To | Wire |
|---|---|---|---|
| CLK | 555 clock module, OUT | 4013 3 (CLK1) | M-F |
| RST | button RESET, OUT | 4013 4 (R1) | M-F |
| R0 | button REQ 0, OUT | 4011 1 (A), 4081 8 (E) | M-F, M-M |
| R1 | button REQ 1, OUT | 4011 13 (H), 4081 5 (C) | M-F, M-M |
| L | 4013 1 (Q1) | 4011 2 (B), 4081 1 (A), LED module, LED 4 | M-F, M-M |
| L | 4013 2 (Q1) | 4011 12 (G) | M-M |
| x | 4011 11 (M) | 4081 9 (F) | M-M |
| y | 4011 3 (J) | 4081 6 (D), 4081 2 (B) | M-M |
| G0 | 4081 10 (L) | LED module, LED 1 | M-F, M-M |
| G1 | 4081 4 (K) | 4071 1 (A), LED module, LED 2 | M-F, M-M |
| h | 4081 3 (J) | 4071 2 (B) | M-M |
| D | 4071 3 (J) | 4013 5 (D1) | M-M |
| +5V | +5V | 4071 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) | |
| GND | GND | 4071 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 connected | 4071 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
Build it step by step
- 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.
- 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.
- 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.
- The fixed inputs: SET1 of the 4013, the inputs of flip-flop 2 and of the unused gates.
- 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.
- With the power off, the modules: the clock module, RESET, REQUEST 0 and REQUEST 1 above the board, the LED module below it.
- Turn the power on and set the clock module to about one blink per second.
Wiring checklist
| # | Net | From | To | |
|---|---|---|---|---|
| Power (red to +, black to −) · 8 wires | ||||
| 1 | +5V | 6a (4071 pin 14, VDD) | top + rail, column 6 | |
| 2 | GND | 12j (4071 pin 7, VSS) | bottom − rail, column 12 | |
| 3 | +5V | 20j (4013 pin 14, VDD) | bottom + rail, column 20 | |
| 4 | GND | 14a (4013 pin 7, VSS) | top − rail, column 14 | |
| 5 | +5V | 23a (4081 pin 14, VDD) | top + rail, column 23 | |
| 6 | GND | 29j (4081 pin 7, VSS) | bottom − rail, column 29 | |
| 7 | +5V | 32a (4011 pin 14, VDD) | top + rail, column 32 | |
| 8 | GND | 38j (4011 pin 7, VSS) | bottom − rail, column 38 | |
| Fixed inputs: CMOS inputs must not float · 17 wires | ||||
| 9 | +5V | 7a (4071 pin 13, H) | top + rail, column 7 | |
| 10 | +5V | 8a (4071 pin 12, G) | top + rail, column 8 | |
| 11 | GND | 10j (4071 pin 5, C) | bottom − rail, column 10 | |
| 12 | +5V | 11a (4071 pin 9, F) | top + rail, column 11 | |
| 13 | GND | 11j (4071 pin 6, D) | bottom − rail, column 11 | |
| 14 | +5V | 12a (4071 pin 8, E) | top + rail, column 12 | |
| 15 | GND | 14j (4013 pin 8, S2) | bottom − rail, column 14 | |
| 16 | GND | 15a (4013 pin 6, S1) | top − rail, column 15 | |
| 17 | GND | 15j (4013 pin 9, D2) | bottom − rail, column 15 | |
| 18 | GND | 16j (4013 pin 10, R2) | bottom − rail, column 16 | |
| 19 | GND | 17j (4013 pin 11, CLK2) | bottom − rail, column 17 | |
| 20 | +5V | 24a (4081 pin 13, H) | top + rail, column 24 | |
| 21 | +5V | 25a (4081 pin 12, G) | top + rail, column 25 | |
| 22 | GND | 36j (4011 pin 5, C) | bottom − rail, column 36 | |
| 23 | +5V | 37a (4011 pin 9, F) | top + rail, column 37 | |
| 24 | GND | 37j (4011 pin 6, D) | bottom − rail, column 37 | |
| 25 | +5V | 38a (4011 pin 8, E) | top + rail, column 38 | |
| Signal wires · 12 wires | ||||
| 26 | R0 | 32g (4011 pin 1, A) | 29d (4081 pin 8, E) | |
| 27 | R1 | 33d (4011 pin 13, H) | 27g (4081 pin 5, C) | |
| 28 | L | 20d (4013 pin 1, Q1) | 23g (4081 pin 1, A) | |
| 29 | L | 23i (4081 pin 1, A) | 33i (4011 pin 2, B) | |
| 30 | L | 19b (4013 pin 2, Q1) | 34b (4011 pin 12, G) | |
| 31 | y | 34h (4011 pin 3, J) | 28h (4081 pin 6, D) | |
| 32 | y | 28g (4081 pin 6, D) | 24g (4081 pin 2, B) | |
| 33 | x | 35c (4011 pin 11, M) | 28c (4081 pin 9, F) | |
| 34 | G0 | 27d (4081 pin 10, L) | 22f | |
| 35 | G1 | 26h (4081 pin 4, K) | 6h (4071 pin 1, A) | |
| 36 | h | 25g (4081 pin 3, J) | 7g (4071 pin 2, B) | |
| 37 | D | 8g (4071 pin 3, J) | 16d (4013 pin 5, D1) | |
| Modules on male-female jumpers · 16 wires | ||||
| 38 | GND | 555 clock module, − | top − rail, column 17 | |
| 39 | CLK | 555 clock module, OUT | 18a (4013 pin 3, CLK1) | |
| 40 | +5V | 555 clock module, + | top + rail, column 19 | |
| 41 | +5V | button RESET, + | top + rail, column 22 | |
| 42 | RST | button RESET, OUT | 17a (4013 pin 4, R1) | |
| 43 | GND | button RESET, − | top − rail, column 24 | |
| 44 | +5V | button REQ 0, + | top + rail, column 30 | |
| 45 | R0 | button REQ 0, OUT | 29a (4081 pin 8, E) | |
| 46 | GND | button REQ 0, − | top − rail, column 32 | |
| 47 | +5V | button REQ 1, + | top + rail, column 36 | |
| 48 | R1 | button REQ 1, OUT | 33a (4011 pin 13, H) | |
| 49 | GND | button REQ 1, − | top − rail, column 39 | |
| 50 | GND | LED module, G | bottom − rail, column 18 | |
| 51 | G0 | LED module, LED 1 | 22j | |
| 52 | G1 | LED module, LED 2 | 26j (4081 pin 4, K) | |
| 53 | L | LED module, LED 4 | 23j (4081 pin 1, A) | |
Test it
- Press RESET: LED 4 (L) goes dark.
- Press REQUEST 0: LED 1 lights at once. Release it.
- Press REQUEST 1: LED 2 lights at once, and at the next blink LED 4 lights: requester 1 got the last grant.
- Hold both: LEDs 1 and 2 take turns at every blink.
- Hold both, then release REQUEST 1 while requester 0 has the grant: requester 0 keeps it as long as it asks alone.
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.
// 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.
// 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
// 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
// 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
// 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.
// 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.
- Copy
labs/1_basics/1_10_snail_fsmto a new directory. - Replace its
lab_top.svwithbgm_lab/lab_top.svfrom this exercise and addarbiter.sv. The BGM scripts compile every.svfile of the directory excepttb.sv. - Run
./03_synthesize_for_fpga.bashand./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.
- Copy
hackathon/problems/3_d_flip_flop. - Replace its
hackathon_top.svwithbgm_hackathon/hackathon_top.svand addarbiter.sv. - Run
./03_synthesize_for_fpga.bashand./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"
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
- Fixed priority. Remove the flip-flop and let requester 0 always win. Hold both buttons: what happens to requester 1? This is starvation.
- Grant only on the clock. Store the grants in flip-flops too, so that they change only at clock edges. Which flip-flops and wires do you need?
- Three requesters. Write the table for three requesters and a priority that moves round in a circle. How many state bits do you need?
- In SystemVerilog: write a round-robin arbiter for N requesters with a parameter, as asked at job interviews, and check it with a testbench that counts the grants of each requester.
Files of this exercise
- arbiter.sv: the generic design (section 8)
- cd4071.sv, cd4013.sv, cd4081.sv and cd4011.sv: chip models (section 9)
- arbiter_chips.sv: the chip-level design (section 10)
- tb.sv, run_icarus.bash, run_verilator.bash, run_questa.bash, clean.bash (section 11)
- bgm_lab/lab_top.sv, bgm_hackathon/hackathon_top.sv, tiny_tapeout/project.v (sections 12–14)
- Drawings as SVG and PNG in images/: the schematics, the pinouts and the waveforms; the Russian versions end in
_ru
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.