P2.13.2 The Exclusive Retry Loop

HardImplementIcarus · browser samples / server checksReview the topic

Build the manager side of an atomic increment. Read and write exclusively, retry when someone else wrote first, and stop cleanly when exclusives cannot work.

Hardware context: The monitor hardware behind a subordinate that supports exclusive accesses. Implement the manager loop of an atomic increment that retries on OKAY and stops when exclusives are unsupported or fail with an error.

Lesson P2.13 showed the subordinate's side of an exclusive sequence. A manager that loses the race gets OKAY on its write and must try again. This is the loop every lock and counter in an SoC relies on.

Write module excl_incr. When start is 1, increment the 32-bit word at addr. Read it exclusively, then write the value plus one exclusively, with the same ID, address, length, size and burst. EXOKAY on the write means the increment is done.

OKAY on the write means another manager wrote the word first. Nothing was written, so read the word again and repeat. OKAY on the read means the subordinate has no exclusive support. Stop without writing. SLVERR or DECERR on either one is an error: stop and report it.

The fixture plays other managers. They write the same word, write the word next to it, or read it exclusively with another ID. Their moves are tied to your exclusive reads, so your timing cannot change the outcome. It also stalls your requests and checks every rule you must keep.

Signals in the Timeline

SignalWidthRole
ops3input of each check
Full specification
The module you write (keep these ports)
module excl_incr #(parameter logic [3:0] ID = 4'd5) (
  input  logic        aclk,
  input  logic        aresetn,     // active low, asynchronous assert
  // the request: increment the 32-bit word at addr
  input  logic        start,       // one edge, only while busy is 0
  input  logic [31:0] addr,        // 4-byte aligned
  output logic        busy,
  output logic        done,        // 1 for exactly one edge when the request is over
  output logic [1:0]  status,      // with done: 0 incremented, 1 no exclusive support, 2 error response
  output logic [7:0]  tries,       // with done: exclusive writes issued for this request
  output logic [31:0] old_value,   // with done and status 0: the value before your increment
  // AXI read address and read data
  output logic [3:0]  arid,
  output logic [31:0] araddr,
  output logic [7:0]  arlen,
  output logic [2:0]  arsize,
  output logic [1:0]  arburst,
  output logic        arlock,
  output logic        arvalid,
  input  logic        arready,
  input  logic [31:0] rdata,
  input  logic [1:0]  rresp,
  input  logic        rlast,
  input  logic        rvalid,
  output logic        rready,
  // AXI write address, write data and write response
  output logic [3:0]  awid,
  output logic [31:0] awaddr,
  output logic [7:0]  awlen,
  output logic [2:0]  awsize,
  output logic [1:0]  awburst,
  output logic        awlock,
  output logic        awvalid,
  input  logic        awready,
  output logic [31:0] wdata,
  output logic [3:0]  wstrb,
  output logic        wlast,
  output logic        wvalid,
  input  logic        wready,
  input  logic [1:0]  bresp,
  input  logic        bvalid,
  output logic        bready
);
  // your manager
endmodule
StepWhat your manager must do
Exclusive readARID = ID, ARADDR = addr, ARLEN = 0, ARSIZE = 2 (4 bytes), ARBURST = INCR, ARLOCK = 1.
WaitDo not offer the write until the read data has been taken.
Exclusive writeThe same ID, address, length, size and burst, AWLOCK = 1, WDATA = the value read + 1, WSTRB = 4'hf, WLAST = 1.
Write responseEXOKAY: done, status 0. OKAY: read again. SLVERR or DECERR: done, status 2.
Read responseEXOKAY: go on to the write. OKAY: done, status 1, no write. SLVERR or DECERR: done, status 2, no write.
Reportdone is 1 for exactly one edge. With it: status, tries (exclusive writes issued for this request) and, for status 0, old_value.

Every VALID you raise stays 1, with its payload unchanged, until its READY. The fixture offers start only while busy is 0, at most once per request.

The result is {final_word[31:0], old_value[15:0], tries[7:0], status[1:0], dones[5:0], bad[7:0], hold[7:0]}. final_word is the memory word of the last request after the run; old_value counts only for status 0. bad counts requests and beats that break a rule above. hold counts VALIDs withdrawn or changed before their READY. Each case runs 200 edges.

Examples

Example 1

Nobody in the Way
Input
Increment 0x104; word i at 0x100 + 4i starts as 0x40 + i
Subordinate: exclusive access supported
Other managers: none
ARREADY 16'hffff, AWREADY 16'hffff, WREADY 16'hffff (bit = edge mod 16)
Output
final_word = 0x42, old_value = 0x41, tries = 1, status = 0, dones = 1, bad = 0, hold = 0

The word at 0x104 starts as 0x41. One exclusive read, one exclusive write of 0x42, EXOKAY. tries is 1 and old_value is 0x41.

Example 2

One Lost Race
Input
Increment 0x108; word i at 0x100 + 4i starts as 0x40 + i
Subordinate: exclusive access supported
Other managers: after exclusive read 1: same word written
ARREADY 16'hffff, AWREADY 16'hffff, WREADY 16'hffff (bit = edge mod 16)
Output
final_word = 0x143, old_value = 0x142, tries = 2, status = 0, dones = 1, bad = 0, hold = 0

After your first exclusive read, another manager adds 0x100 to the word. Your write gets OKAY and writes nothing. The second read sees 0x142, and the second write stores 0x143.

Example 3

No Exclusive Support
Input
Increment 0x118; word i at 0x100 + 4i starts as 0x40 + i
Subordinate: no exclusive support
Other managers: none
ARREADY 16'hffff, AWREADY 16'hffff, WREADY 16'hffff (bit = edge mod 16)
Output
final_word = 0x46, old_value = 0x0, tries = 0, status = 1, dones = 1, bad = 0, hold = 0

The read answers OKAY instead of EXOKAY. The specification recommends that the manager does not attempt the write. status is 1 and the word is unchanged.

Constraints and Contract

  • Keep the module name, the ID parameter and the ports. Use one read and one write at a time.
  • The fixture drives its outputs after each rising edge and samples yours just before the next one.
  • Do not reach outside your ports.

Hints

Hint 1

Five states are enough: idle, read address, read data, write, write response.

Hint 2

In the write state, AWVALID and WVALID are separate offers. Remember which one has been taken, and leave the state when both have.

Hint 3

Keep the value from the latest read, not the first one. A retry reads a new value.

What the grader checks: Your manager runs ten cases. They include no contention, up to three lost races, writes next door and another ID's exclusive read. Others have no exclusive support, error responses, stalled readies or two requests in a row. Every request and beat is checked against the rules. Passing shows the loop is right in these cases, not in every possible system.

10 checks on Icarus · browser samples / server checks. Run the examples first, then check the full regression. Full checks use the server judge. Downloads contain public samples only. A passing result establishes the tested cases, not a proof for all possible inputs.

See all 4 steps and the final capstone

Report a problem with this page

Your code SystemVerilogSaved locally
Ln 1, Col 1Tab indents · Ctrl/⌘ + Enter runs samples
Not run yet

Nobody in the Way

Input
Increment 0x104; word i at 0x100 + 4i starts as 0x40 + i
Subordinate: exclusive access supported
Other managers: none
ARREADY 16'hffff, AWREADY 16'hffff, WREADY 16'hffff (bit = edge mod 16)
Expected output
final_word = 0x42, old_value = 0x41, tries = 1, status = 0, dones = 1, bad = 0, hold = 0

The word at 0x104 starts as 0x41. One exclusive read, one exclusive write of 0x42, EXOKAY. tries is 1 and old_value is 0x41.

Run samples executes all 3 public checks. Check all tests runs 10 checks on the server.

Icarus · browser samples / server checks