Tutorial · Chapter 20 of 23

Combine a disk rule with REU records

Update an application record in an arena while a disk-loaded rule uses a separate REU cache and execution window.

Jump to a section

Predict the update before planning the addresses

The score starts at $0200 after three rounds. The rule adds 3 to the round count and returns 6 points. The new score is $0206 after round 4. The application owns the score record. The DLL only receives one byte and returns one byte, so the same rule can serve a caller with a different storage layout.

  1. Acquire the scoring rule from disk.
  2. Bring the score record from REU into a C64 scratch arena.
  3. Call the rule through the execution window and update the local record.
  4. Flush the record, reset scratch, and verify the new score with a far read.

Reserve a separate range for each owner

Range starts atOwnerWhen it is in use
C64 $6000Execution windowWhile the scoring rule is active.
C64 $8000Near arenaWhile the materialized score is live.
C64 $9000Disk stagingWhile the loader reads the payload.
C64 $c800Software stackWhile the scoring call has a live frame.
REU $010000Score arenaUntil the application resets the far arena.
REU $030000Module loaderWhile the registry's payloads occupy their cache slots.

The two REU regions have distinct owners. A write-back to the score arena never needs to touch the cached DLL. The near score stays live while the export runs, so scratch RAM and the execution window also need separate ranges.

Follow the score through the rule call

Edit and flush · from play
if (table.materialize(scratch)) |local| {
    var score = local.get(0);
    if (FixedMath.bias(score.rounds)) |points| {
        score.total += @as(u16, points);
        score.rounds += 1;
        local.set(0, score);
        table.flush(local);
        scratch.reset();
        const saved = table.get(0);
        scores.reset();
        FixedMath.release();
        return saved.total == 0x0206 and saved.rounds == roundLimit();
    }
}

The producer exports bias as a stack-frame function. The caller declares the same u8 → u8 signature. The rule sees only the current round count. The application widens the returned points before adding them to the word total. After the flush it resets scratch and reads the score back from the far table.

Complete scoring rule · Complete score application

Expect MEMORY PLAN OK and a green border.

Change the score_reu region base to $030000 and rebuild the caller. Its data region would overlap the module cache, so the compiler rejects the declaration. Restore the base before trying a runtime failure such as a disk with no fixedmath entry. The guide's repair exercise also checks an ABI mismatch.