Bursts, Locks and the Waiting Request

An arbiter that chooses again at every cycle is fair, and sometimes wrong. Some transfers belong together, and one pair must never be split.

You Will Be Able To

  • Define a beat and a burst, and weigh keeping a burst together against splitting it.
  • Show how two correct managers lose an update, and how a locked sequence prevents it.
  • Say when the bus is locked and unlocked, and what an input stage must keep.
Lesson
S4.13
Time
26 min
Runs on
Your browser
Check
5 questions

Two blocks each add to one counter in memory. Every transfer they make is legal, and the arbiter is perfectly fair. At the end the counter is too small. One addition has vanished.

Before You Start

You need the arbiter, the grant and round robin from Many Managers. You need the address phase, the data phase and HTRANS from S0.5.

Five questions this page answers
Question
1What are a beat and a burst?
2What may an arbiter do in the middle of a burst, and what does each choice cost?
3How can two managers that each work correctly lose an update to a shared word?
4What does HMASTLOCK promise, and when is the bus locked and unlocked?
5Where does a request wait while another manager holds the port, and what must be kept?

The Idea: Some Transfers Belong Together

The arbiter of the last lesson treated every transfer alone. After each one it chose again. Three things complicate that picture.

First, a manager often wants a run of words, one after the other. Such a run is a burst, and each transfer in it is a beat. An arbiter can let the burst finish, or let another manager in between two beats. Either is allowed. Each has a price.

Second, some pairs of transfers must not be separated at all. To add to a word in memory, a manager reads it and writes it back. That is a read-modify-write. If another manager writes the word between the two halves, one of the two changes is lost. A manager can ask the bus to keep such a pair together. The pair is then a locked sequence.

Third, while one manager holds the port, the other still has a request. On AHB a request is on the wires for one address phase only. So something must keep it until its turn comes. That something is the input stage.

New Words

New words on this page
WordMeaning
BurstA run of transfers to neighbouring addresses, made by one manager as one piece of work.
BeatOne transfer of a burst.
Read-modify-writeReading a word, changing the value, and writing it back: two transfers that depend on each other.
Locked sequenceTransfers that no other manager may get between.
HMASTLOCKThe AHB wire with which a manager marks the transfers of a locked sequence.
Input stageRegisters in front of an arbiter. They keep one manager's request until it is served.

On AHB the first beat of a burst is a NONSEQ transfer. Every later beat uses another value of HTRANS, called SEQ. That is how a subordinate and an arbiter can tell that a beat continues a burst.

One Picture: One Port and a Place to Wait

One Port, Two Managers, and a Place to WaitStep through the picture. Two managers reach one SRAM port through an arbiter.
  • Processor
  • Peripheral
  • Bus
  • Memory
One Port, Two Managers, and a Place to WaitStep through the picture. Two managers reach one SRAM port through an arbiter. component Processor adds 1 to thecounter: a read, thena write component DMA adds 10 to the samecounter: a read, thena write component Input stage 0 a register: keeps theprocessor's requestuntil it is served component Input stage 1 a register: keeps therequest of DMA untilit is served component Arbiter round robin, and itremembers who holds alock component SRAM port one transfer in acycle; one of itswords is the counter request,HMASTLOCKrequest,HMASTLOCKwaiting requestwaiting requestthe winner
  1. component Processoradds 1 to the counter: a read, then a write
    • request, HMASTLOCK → Input stage 0
  2. component DMAadds 10 to the same counter: a read, then a write
    • request, HMASTLOCK → Input stage 1
  3. component SRAM portone transfer in a cycle; one of its words is the counter
  4. component Input stage 0a register: keeps the processor's request until it is served
    • waiting request → Arbiter
  5. component Input stage 1a register: keeps the request of DMA until it is served
    • waiting request → Arbiter
  6. component Arbiterround robin, and it remembers who holds a lock
    • the winner → SRAM port
  1. Two sequences

    Each manager needs two transfers to add to the counter: read it, then write the new value. Both start in the same cycle.

  2. Choosing at every cycle

    Round robin serves the read of one, then the read of the other. Both have now read the same old value. One addition will be lost.

  3. A lock

    The processor raises HMASTLOCK with its read. Once that read is served, the arbiter grants the port to nobody else.

  4. The request that waits

    DMA showed its request for one cycle and moved on. Input stage 1 keeps that request, for as long as the lock lasts.

  5. Unlock

    The processor writes, then lowers HMASTLOCK. One cycle later the port is free, and the waiting read of DMA is served. It reads the new value.

Read the diagram as text
  • Processor: adds 1 to the counter: a read, then a write
  • DMA: adds 10 to the same counter: a read, then a write
  • Input stage 0: a register: keeps the processor's request until it is served
  • Input stage 1: a register: keeps the request of DMA until it is served
  • Arbiter: round robin, and it remembers who holds a lock
  • SRAM port: one transfer in a cycle; one of its words is the counter
  • Processor → Input stage 0: request, HMASTLOCK
  • DMA → Input stage 1: request, HMASTLOCK
  • Input stage 0 → Arbiter: waiting request
  • Input stage 1 → Arbiter: waiting request
  • Arbiter → SRAM port: the winner

Each arrow stands for a group of wires. The way back from SRAM is not drawn.

The arbiter in this picture is the round-robin arbiter of the last lesson. It has one more register, which says who holds a lock.

The Smallest Example, Worked by Hand: A Lost Addition

The counter holds 5. The processor adds 1 and DMA adds 10, so the right result is 16. Round robin serves one transfer in each cycle, and chooses again each time.

Without a lock
CycleProcessorDMACounter afterwards
1reads 55
2reads 55
3writes 5 + 1 = 66
4writes 5 + 10 = 1515

The counter ends at 15. DMA computed its sum from a value that was already out of date. Now let each manager lock its read and its write together.

With a lock
StepProcessorDMACounter afterwards
1reads 5waits5
2writes 6waits6
3reads 66
4writes 6 + 10 = 1616

The same reasoning gives the price of a burst. DMA has a burst of four beats to SRAM. One read of the processor arrives when the second beat is due.

One read against a burst of four beats
The arbiterThe read waitsThe burst
keeps its grant until the last beatthree cyclesstays whole
chooses again after every beatno cycleis split after its first beat, and ends one cycle later

The Rules

  1. Rule 1: A burst may be kept whole or split between beats.

    Keeping it whole makes other managers wait for its full length. Splitting it serves them sooner, and the burst must be picked up again.

  2. Rule 2: A locked sequence is never split.

    The manager raises HMASTLOCK for its transfers. Until the sequence ends, no other manager is served at that subordinate.

  3. Rule 3: The bus is locked after a cycle with HMASTLOCK at 1 and HREADY at 1.

    It is unlocked after a cycle with HMASTLOCK at 0 and HREADY at 1. So unlocking takes one cycle after the last locked transfer.

  4. Rule 4: A locked sequence stays with one subordinate, and stays short.

    All its transfers go to the same region. While it lasts, every other manager at that subordinate waits.

  5. Rule 5: A request that waits is kept whole.

    The input stage keeps the address, the direction and the lock. Forget any of them, and the wrong transfer is served later, or none.

Sources for These Rules

Arm AMBA AHB Protocol Specification, document IHI 0033, issue C. Locked transfers, and the two conditions of rule 3: section 3.3. The same section recommends an idle cycle after a locked transfer, and requires one region. That section also requires every subordinate that several managers can reach to obey HMASTLOCK.

Bursts: section 3.6. An interconnect may end a burst early to let another manager in, and a subordinate must cope with that: section 3.6.2. The transfer type SEQ: section 3.2.

The specification does not say how an arbiter treats a burst, and does not describe an input stage. Both are design practice. The lab follows rule 3 on a simplified port, where a request is taken and served in one step.

Now Run It

The lab at the end of this page is the hand example as a circuit. Two managers, both test code, add 1 and 10 to a counter that starts at 5. Each shows a request for one cycle, and waits until the SRAM port has served it. Here both raise hmastlock, the lab's name for HMASTLOCK.

Printed by the lab at each rising clock edge. The two hmastlock bits are the cpu and then the dma. "locked" names the manager that holds the port. The counter is shown as it is just before the edge.
e0 waiting: cpu=none dma=none | hmastlock=00 locked=nobody | served: nobody | counter=5
e1 waiting: cpu=none dma=none | hmastlock=11 locked=nobody | served: nobody | counter=5
e2 waiting: cpu=READ dma=READ | hmastlock=11 locked=nobody | served: cpu READ 5 | counter=5
e3 waiting: cpu=none dma=READ | hmastlock=11 locked=cpu | served: nobody | counter=5
e4 waiting: cpu=WRITE dma=READ | hmastlock=11 locked=cpu | served: cpu WRITE 6 | counter=5
e5 waiting: cpu=none dma=READ | hmastlock=01 locked=cpu | served: nobody | counter=6
e6 waiting: cpu=none dma=READ | hmastlock=01 locked=nobody | served: dma READ 6 | counter=6
e7 waiting: cpu=none dma=none | hmastlock=01 locked=dma | served: nobody | counter=6
e8 waiting: cpu=none dma=WRITE | hmastlock=01 locked=dma | served: dma WRITE 16 | counter=6
e9 waiting: cpu=none dma=none | hmastlock=00 locked=dma | served: nobody | counter=16
e10 waiting: cpu=none dma=none | hmastlock=00 locked=nobody | served: nobody | counter=16
counter=16 | finished: cpu=1 dma=1

Rule 3 at work: e2 serves the read of the cpu with its hmastlock at 1, and e3 sees the port locked. Rule 2 at work: at e3 the dma has a READ waiting and it is the dma's turn, yet nobody is served.

Rule 5 at work: the READ of the dma stays in the "waiting" column from e2 to e6. The dma showed it for one cycle, before e2. Rule 3 again: e5 sees the cpu's hmastlock at 0, and e6 is the first edge with the port free. The counter ends at 16.

The lab has two constants, LOCK and KEEP. Each of the next two runs changes one of them.

Three runs of the same two additions
RunLast line of the lab
LOCK 1, KEEP 1 (the run above)counter=16 | finished: cpu=1 dma=1
LOCK 0: no locked sequencecounter=15 | finished: cpu=1 dma=1
KEEP 0: the input stage forgets a request that is not served at oncecounter=6 | finished: cpu=1 dma=0

Recorded from the lab. "finished" is 1 when a manager has completed its read and its write.

With no lock the result is 15, as in the hand example. With no memory in the input stage, the read of the dma is forgotten while the port is locked. The dma never finishes, and the counter stays at 6.

For the burst, the lab of the last lesson gains one constant, called HOLD_BURST. With it at 1, the arbiter keeps its grant while more beats follow.

Recorded from the burst lab with HOLD_BURST at 1. The dma has a burst of four beats to SRAM. The cpu has one read of ROM, then one of SRAM.
e1 cpu wants ROM, dma wants SRAM | granted: cpu and dma | served: cpu and dma
e2 cpu wants SRAM, dma wants SRAM | granted: dma | served: dma
e3 cpu wants SRAM, dma wants SRAM | granted: dma | served: dma
e4 cpu wants SRAM, dma wants SRAM | granted: dma | served: dma
e5 cpu wants SRAM, dma wants nothing | granted: cpu | served: cpu

The read of SRAM waits from e2 to e4 and is served at e5: a longest wait of 3 edges. With HOLD_BURST at 0 the same run gives the cpu a longest wait of 0, and the dma one of 1.

The Burst Lab
The lab of the last lesson, with a grant that can be kept. The new lines carry the words "more" and "kept".
// Two managers and two destinations. Each manager works through a list of requests, one at a time.
// An arbiter chooses when both managers ask for the same thing in the same cycle.
module arbiter #(parameter ROTATE = 1) (
  input  logic clk,
  input  logic req_cpu, req_dma,       // who asks now
  input  logic ready,                  // 1: one transfer can be served in this cycle
  input  logic more_cpu, more_dma,     // 1: the transfer being served is not the last beat of its burst
  output logic grant_cpu, grant_dma    // who may go
);
  logic last_dma = 1;                  // who was served last. At the start, the cpu wins a tie
  wire  tie       = req_cpu && req_dma;
  wire  cpu_first = !ROTATE || last_dma;       // fixed priority: always the cpu. Round robin: whoever did not go last
  logic kept_cpu = 0, kept_dma = 0;    // 1: the grant is kept for the next beat of this manager's burst
  assign grant_cpu = req_cpu && !kept_dma && (kept_cpu || !tie ||  cpu_first);
  assign grant_dma = req_dma && !kept_cpu && (kept_dma || !tie || !cpu_first);
  always @(posedge clk) if (ready && (grant_cpu || grant_dma)) begin
    last_dma <= grant_dma;
    kept_cpu <= grant_cpu && more_cpu;
    kept_dma <= grant_dma && more_dma;
  end
endmodule

module tb;
  logic clk = 0;
  always #5 clk = ~clk;
  localparam ROTATE = 1;               // 1: round robin. 0: fixed priority, the cpu first
  localparam MATRIX = 1;               // 1: a bus matrix, an arbiter at each destination. 0: one shared bus
  localparam HOLD_BURST = 1;           // 1: the arbiter keeps its grant until a burst is over

  // ---- The managers: test code. Each has a list of requests, and asks for one at a time ----
  localparam NONE = 0, ROM = 1, SRAM = 2;
  logic [1:0] cpu_list [0:9], dma_list [0:9];
  logic       dma_more [0:9];          // 1: another beat of the same burst follows this request
  int         cpu_i = 0, dma_i = 0;    // how far each manager has come in its list
  wire [1:0]  cpu_wants = cpu_list[cpu_i], dma_wants = dma_list[dma_i];

  // ---- The destinations ----
  int   edge_no = 0;                   // counts the rising clock edges
  wire  rom_ready = 1;
  wire  ram_ready = (edge_no != -1);    // in this run SRAM is always ready

  // ---- A bus matrix: each destination has its own arbiter, and sees only the requests meant for it ----
  wire rom_cpu, rom_dma, ram_cpu, ram_dma;       // the four grants
  arbiter #(ROTATE) at_rom (clk, cpu_wants == ROM,  dma_wants == ROM,  rom_ready, 1'b0, 1'b0, rom_cpu, rom_dma);
  arbiter #(ROTATE) at_ram (clk, cpu_wants == SRAM, dma_wants == SRAM, ram_ready, 1'b0, HOLD_BURST && dma_more[dma_i], ram_cpu, ram_dma);

  // ---- A shared bus: one arbiter for everything. Only one manager may use the bus in a cycle ----
  wire bus_cpu, bus_dma;
  wire [1:0] bus_wants = bus_cpu ? cpu_wants : dma_wants;        // where the manager with the bus wants to go
  wire bus_ready = (bus_wants == SRAM) ? ram_ready : rom_ready;
  arbiter #(ROTATE) at_bus (clk, cpu_wants != NONE, dma_wants != NONE, bus_ready, 1'b0, 1'b0, bus_cpu, bus_dma);

  // Granted: the manager may go. Served: its destination was also ready, so the transfer happened.
  wire cpu_grant  = MATRIX ? (rom_cpu || ram_cpu) : bus_cpu;
  wire dma_grant  = MATRIX ? (rom_dma || ram_dma) : bus_dma;
  wire cpu_served = MATRIX ? ((rom_cpu && rom_ready) || (ram_cpu && ram_ready)) : (bus_cpu && bus_ready);
  wire dma_served = MATRIX ? ((rom_dma && rom_ready) || (ram_dma && ram_ready)) : (bus_dma && bus_ready);

  // Counting and printing. A function turns bits into a name, for printing only.
  int cpu_count = 0, dma_count = 0;        // transfers served
  int cpu_wait = 0, dma_wait = 0;          // edges waited in a row
  int cpu_longest = 0, dma_longest = 0;    // the longest such wait
  function string place(input logic [1:0] w);
    return (w == ROM) ? "ROM" : (w == SRAM) ? "SRAM" : "nothing";
  endfunction
  function string who(input logic c, input logic d);
    return (c && d) ? "cpu and dma" : c ? "cpu" : d ? "dma" : "nobody";
  endfunction
  always @(posedge clk) begin
    $display("e%0d cpu wants %s, dma wants %s | granted: %s | served: %s", edge_no, place(cpu_wants), place(dma_wants),
             who(cpu_grant, dma_grant), who(cpu_served, dma_served));
    // A manager moves on to its next request when this one is served ("<=" takes effect after the edge).
    if ((cpu_served || cpu_wants == NONE) && cpu_i < 9) cpu_i <= cpu_i + 1;
    if ((dma_served || dma_wants == NONE) && dma_i < 9) dma_i <= dma_i + 1;
    if (cpu_served) cpu_count++;
    if (dma_served) dma_count++;
    cpu_wait = (cpu_wants != NONE && !cpu_served) ? cpu_wait + 1 : 0;
    dma_wait = (dma_wants != NONE && !dma_served) ? dma_wait + 1 : 0;
    if (cpu_wait > cpu_longest) cpu_longest = cpu_wait;
    if (dma_wait > dma_longest) dma_longest = dma_wait;
    edge_no <= edge_no + 1;
  end

  initial begin
    for (int i = 0; i < 10; i++) begin cpu_list[i] = NONE; dma_list[i] = NONE; dma_more[i] = 0; end
    // Entry 0 is the idle cycle that edge 0 sees. The requests start at entry 1.
    cpu_list[1] = ROM;
    cpu_list[2] = SRAM;
    dma_list[1] = SRAM;  dma_more[1] = 1;
    dma_list[2] = SRAM;  dma_more[2] = 1;
    dma_list[3] = SRAM;  dma_more[3] = 1;
    dma_list[4] = SRAM;  dma_more[4] = 0;
    repeat (8) @(posedge clk);
    #1 $display("cpu: served %0d, longest wait %0d | dma: served %0d, longest wait %0d", cpu_count, cpu_longest, dma_count, dma_longest);
    $finish;                           // $finish ends the simulation
  end
endmodule

These labs leave things out. A request is served in one cycle, with no separate data phase. The SRAM is always ready. A real input stage also has to take the write data, which arrives one phase after the address.

Why It Is Built This Way

Design choiceThe alternativeWhat the choice buys, and costs
Keep a burst wholeChoose again after every beatThe burst is over soonest, and memory works at its best. An urgent request waits for the whole burst.
A lock wire on the busTrust every manager to stay awayA read-modify-write is safe from any other manager. One manager can now hold a subordinate, so the sequence must be short.
An input stage that keeps the requestMake the manager show its request until it is servedThe manager follows the plain bus rules, and need not know that an arbiter exists. It costs a register set for each manager.

Could software do without the lock, by being careful? Not here. A program can stop itself from being interrupted. It cannot stop a DMA block, which is hardware and takes its turn when the arbiter gives it.

A count of received bytes is sometimes ten too low. The team checks the bus with a protocol checker, a tool that tests every transfer against the bus rules. It reports no error, for days.

It cannot report one. Each read and each write is correct by itself. The fault is in the order of four correct transfers, as in the first table above. Only a check that knows the two sequences belong together can see it.

When a shared word goes wrong now and then, write down who reads it and who writes it. Look for a read by one manager between the read and the write of another.

At Scale

This page2 managers, 1 port
A large chipdozens of managers, bursts of many beats

This section looks ahead. One lock on one port is easy to follow. A large chip has many managers, long bursts and deadlines.

  1. The longest turn

    The longest burst and the longest locked sequence decide how long the others can wait. The lesson on quality of service turns that into a number.

  2. A lighter tool than a lock

    A lock stops everybody. Newer buses also offer an exclusive transfer: the write goes ahead only if nobody touched the word since the read. The multicore lessons use it.

  3. The signals

    The Protocols track shows HMASTLOCK on real waveforms, in Many managers, arbitration and locking.

Try It

Run the lab. In each row, say why the request in the "waiting" column was or was not served.

Set LOCK to 0. Predict the last line from the hand example, then run.

Set LOCK back to 1 and KEEP to 0. Find the edge where the read of the dma disappears.

With LOCK and KEEP at 1, make the dma add 0. Say whether the lock still costs the dma any cycles.

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: e0 waiting: cpu=none dma=none | hmastlock=00 locked=nobody | served: nobody | counter=5 e1 waiting: cpu=none dma=none | hmastlock=11 locked=nobody | served: nobody | counter=5 e2 waiting: cpu=READ dma=READ | hmastlock=11 locked=nobody | served: cpu READ 5 | counter=5 e3 waiting: cpu=none dma=READ | hmastlock=11 locked=cpu | served: nobody | counter=5 e4 waiting: cpu=WRITE dma=READ | hmastlock=11 locked=cpu | served: cpu WRITE 6 | counter=5 e5 waiting: cpu=none dma=READ | hmastlock=01 locked=cpu | served: nobody | counter=6 e6 waiting: cpu=none dma=READ | hmastlock=01 locked=nobody | served: dma READ 6 | counter=6 e7 waiting: cpu=none dma=none | hmastlock=01 locked=dma | served: nobody | counter=6 e8 waiting: cpu=none dma=WRITE | hmastlock=01 locked=dma | served: dma WRITE 16 | counter=6 e9 waiting: cpu=none dma=none | hmastlock=00 locked=dma | served: nobody | counter=16 e10 waiting: cpu=none dma=none | hmastlock=00 locked=nobody | served: nobody | counter=16 counter=16 | finished: cpu=1 dma=1

Check Yourself

Predict the outputThe counter starts at 20. The cpu adds 3 and the dma adds 40. LOCK is 0, so nobody raises hmastlock. What is the last line the lab prints?
// Two managers add to one counter in SRAM. Each must read it, add its amount, and write it back.
// One SRAM port serves one transfer in a cycle. In front of it, an input stage keeps a waiting request.
module tb;
  logic clk = 0;
  always #5 clk = ~clk;
  localparam LOCK = 0;          // 1: a manager raises hmastlock for its read and its write
  localparam KEEP = 1;          // 1: the input stage keeps a request until the port has served it

  logic [7:0] counter = 20;      // the shared word in SRAM

  // ---- What the two managers drive. Index 0 is the cpu, index 1 is the dma. ----
  localparam NONE = 0, READ = 1, WRITE = 2;
  logic [1:0] offer [2];        // a request in its address phase: on the wires for one cycle only
  logic [7:0] wdata [2];        // the value, for a write
  logic       hmastlock [2];    // 1: this transfer belongs to a locked sequence

  // ---- The arbiter of the SRAM port (round robin, as in S4.4) ----
  localparam FREE = 2;
  logic [1:0] held [2];         // the input stage: the request that waits for the port
  logic [7:0] held_data [2];
  logic       held_lock [2];
  logic       last = 1;         // who was served last. At the start, the cpu wins a tie
  logic [1:0] locked = FREE;    // FREE, or the manager (0 or 1) that has locked the port
  logic       grant [2];        // this SRAM is always ready, so a granted request is served
  always_comb begin
    for (int m = 0; m < 2; m++) grant[m] = (held[m] != NONE) && (locked == FREE || locked == m);
    if (grant[0] && grant[1]) grant[last] = 0;       // a tie: the manager served last waits
  end

  always @(posedge clk) begin
    // The lock rule. Locked: after a served transfer that had hmastlock at 1.
    // Unlocked: after a cycle in which the manager that holds the lock shows hmastlock at 0.
    if (locked != FREE && !hmastlock[locked]) locked <= FREE;
    for (int m = 0; m < 2; m++) begin
      if (grant[m]) begin
        if (held[m] == WRITE) counter <= held_data[m];   // the SRAM port serves the transfer
        last <= m;
        if (held_lock[m]) locked <= m;
      end
      // The input stage. A new request is stored. A served one leaves. With KEEP at 0, one that
      // was not served at once is forgotten.
      if (offer[m] != NONE) begin held[m] <= offer[m]; held_data[m] <= wdata[m]; held_lock[m] <= hmastlock[m]; end
      else if (grant[m] || !KEEP) held[m] <= NONE;
    end
  end

  // ---- The managers: test code. Each runs this task once, at the same time as the other ----
  logic finished [2];
  task automatic add(input int m, input logic [7:0] amount);   // "automatic": each call has its own variables
    logic [7:0] value;
    hmastlock[m] = LOCK;
    offer[m] = READ;                            // the address phase of the read
    @(posedge clk); #1 offer[m] = NONE;         // "#1" is a tiny delay: just after the edge
    do @(posedge clk); while (!grant[m]);       // wait in the data phase until the port serves it
    value = counter;                            // the value read
    #1 wdata[m] = value + amount;
    offer[m] = WRITE;                           // the address phase of the write
    @(posedge clk); #1 offer[m] = NONE;
    do @(posedge clk); while (!grant[m]);
    #1 hmastlock[m] = 0;                        // the sequence is over
    finished[m] = 1;
  endtask

  // Print what each rising clock edge sees. A function turns numbers into names, for printing only.
  int edge_no = 0;
  function string kind(input logic [1:0] k);
    return (k == READ) ? "READ" : (k == WRITE) ? "WRITE" : "none";
  endfunction
  function string name(input logic [1:0] n);
    return (n == 0) ? "cpu" : (n == 1) ? "dma" : "nobody";
  endfunction
  always @(posedge clk) begin
    $write("e%0d waiting: cpu=%s dma=%s | hmastlock=%b%b locked=%s | served: ", edge_no, kind(held[0]), kind(held[1]),
           hmastlock[0], hmastlock[1], name(locked));
    if (grant[0])      $display("cpu %s %0d | counter=%0d", kind(held[0]), (held[0] == WRITE) ? held_data[0] : counter, counter);
    else if (grant[1]) $display("dma %s %0d | counter=%0d", kind(held[1]), (held[1] == WRITE) ? held_data[1] : counter, counter);
    else               $display("nobody | counter=%0d", counter);
    edge_no <= edge_no + 1;
  end

  initial begin
    for (int m = 0; m < 2; m++) begin offer[m] = NONE; held[m] = NONE; hmastlock[m] = 0; finished[m] = 0; held_lock[m] = 0; end
    @(posedge clk); #1;
    fork                               // "fork" starts both managers at once
      add(0, 3);                         // the cpu adds 3
      add(1, 40);                        // the dma adds 40
    join_none
    repeat (10) @(posedge clk);
    #1 $display("counter=%0d | finished: cpu=%0d dma=%0d", counter, finished[0], finished[1]);
    $finish;                           // $finish ends the simulation
  end
endmodule
ConceptWhat is a beat?
ConceptAn arbiter keeps its grant until a burst of eight beats is over. What is the cost?
ConceptThe last locked transfer of a manager is served at edge 4. It lowers HMASTLOCK right after. Which is the first edge that can serve another manager?
ConceptA request loses a tie and must wait. What must the input stage keep?

Common Mistakes

Wrong Treat a read and a later write of a shared word as two separate jobs.

Another manager can change the word between them.

Fix Lock the pair, or use another scheme that detects the change.

Wrong Expect the port to be free in the cycle after the last locked transfer.

The bus is unlocked after a cycle with HMASTLOCK at 0.

Fix Count one more cycle before another manager is served.

Wrong Keep only "this manager wants the port" while a request waits.

By then the manager no longer shows the address or the direction.

Fix Keep the whole request in the input stage until it is served.

Remember

  • A burst is a run of beats; an arbiter may keep it whole or split it, and each choice has a cost.
  • A read-modify-write by two managers can lose an update even when every transfer is legal.
  • A locked sequence, marked with HMASTLOCK, is never split.
  • The bus is locked after a cycle with HMASTLOCK and HREADY at 1, and unlocked one cycle after HMASTLOCK falls.
  • A request that waits is kept whole in an input stage until it is served.

Practice

Report a problem with this page