S9.7.1 Follow a Spin Lock by Hand

EasyImplement12 minIcarus Verilog · serverReview the topic

Follow two harts through one spin lock by hand: who holds it on which edge, how many swaps are wasted, and when the second hart gets in.

Hardware context: Two harts of ap_soc that share a lock word and a data word in SRAM. Spin locks, exclusive transfers and locked transfers on a bus with several managers

This is the spin lock of lesson S9.7 with a longer critical section. Two harts each make one update. An update takes the lock with a swap of 1, changes three data words, and gives the lock back with a store of 0. For each data word the hart makes one load and one store, and it adds 1. A hart that gets 1 back from its swap tries the swap again.

This worksheet uses named constants. The first three entries show how to record your decisions.

Signals in the Timeline

SignalWidthRole
question8input of each check
Full specification
ItemValue
Before the runThe lock word and the three data words hold 0.
Turns while both harts askThe bus serves one access on each clock edge: hart 0 on odd edges, hart 1 on even edges, from edge 1 on.
Turns when one hart asksA hart that is alone is served on every second edge. Hart 1 stays on the even edges.
The order inside the lockLoad word 0, store word 0, load word 1, store word 1, load word 2, store word 2, then the store of 0 to the lock word.

Enter an edge as its number: 5 for edge 5.

EntryYour decision
H0_TAKESThe edge on which the swap of hart 0 returns 0.
H1_FIRSTWhat the first swap of hart 1, on edge 2, returns.
H0_DATAHow many accesses to the data does hart 0 make inside the lock?
H0_LAST_DATAThe edge of the last data store of hart 0.
H0_GIVESThe edge on which hart 0 stores 0 to the lock word.
SPINSHow many swaps of hart 1 return 1?
H1_TAKESThe edge on which a swap of hart 1 returns 0.
H1_FIRST_LOADThe edge of the first load of hart 1.
H1_LOADSThe value that this load returns.
H1_GIVESThe edge on which hart 1 stores 0 to the lock word.
WORD_ENDWhat each data word holds at the end.
SWAPSHow many swaps were made in all, by both harts?
SPINS_NSuppose one update changed N words under the lock in place of 3. How many swaps of hart 1 would return 1?

Examples

Example 1

H0 TAKES
Input
The edge on which the swap of hart 0 returns 0.
Output
matches = 1

Hart 0 is served first, and the lock word holds 0.

Example 2

H1 FIRST
Input
What the first swap of hart 1, on edge 2, returns.
Output
matches = 1

Hart 0 wrote a 1 on edge 1. Hart 1 gets that 1 back.

Example 3

H0 DATA
Input
How many accesses to the data does hart 0 make inside the lock?
Output
matches = 1

A load and a store for each of three words.

Constraints and Contract

  • Keep the parameter and named constants. Use the numeric codes and units on this page.
  • For SPINS_N, enter a constant expression using N. Only this entry changes with the parameter. The supported integer range is 3 through 6, inclusive.

Hints

Hint 1

Hart 0 has the odd edges: one swap, six data accesses, one store to the lock word.

Hint 2

Hart 1 swaps on every even edge until the lock word holds 0.

Hint 3

From its good swap on, hart 1 is served on every second edge.

What the grader checks: Each check compares the full eight-bit answer. Parameter trials change one condition and keep the other decisions fixed.

13 checks on Icarus Verilog · server. Run the examples first, then check the full regression. Full checks use the server judge. Downloads contain your source only; samples require the simulation service. A passing result establishes the tested cases, not a proof for all possible inputs.

See all 1 problem on this path

Report a problem with this page

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

H0 TAKES

Input
The edge on which the swap of hart 0 returns 0.
Expected output
matches = 1

Hart 0 is served first, and the lock word holds 0.

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

Icarus Verilog · server