Decide which bytes must outlive a call
A module can finish a call and leave useful data behind, such as the selected sector or the last result. Locals in the call frame expire on return. A persistent range belongs to the module and remains available after a later activation. Decide which values need this lifetime before assigning offsets.
| Storage | What survives | Who manages it |
|---|---|---|
| Software-stack frame | Arguments and locals for one invocation. | Call and return code. |
| REU payload cache | Code and original data needed to activate a loaded DLL. | Module loader. |
| REU state snapshot | Selected modified bytes from the active window. | Module loader, using declared offsets. |
| Resident RAM record | Application-owned data while different modules use the window. | Application. |
Save one byte, clear RAM, and activate again
The execution window starts at $c000. An offset of $0780 selects byte $c780. This program calls Counter.tick(5), writes the result to the state byte, and unloads the active module. It then clears the window byte itself. The next Counter.load() must restore the value from the saved state for the exercise to succeed.
const c64 = @import("c64");
extern module Counter = counter {
fn tick(value: u8) u8;
}
memory {
region frames: c64 at 0xc800 size 0x0400;
region execution: c64 at 0xc000 size 0x0800;
region cache: reu at 0x030000 size 0x1000;
stack in frames;
}
module_loader {
window main in execution;
reu_cache in cache;
current {
persist_in_reu at 0x0780 size 1;
}
}
module counter abi 1 {
export tick;
}
fn tick(value: u8) callconv(.stack) u8 {
return value + 1;
}
fn checkCycle() bool {
const state: *volatile u8 = @ptrFromInt(0xc780);
// The direct call works before the DLL is loaded.
if (tick(5) != 6 or Counter.tick(5) != null) {
return false;
}
if (Counter.load() == false) {
return false;
}
if (Counter.tick(5)) |value| {
state.* = value;
} else {
return false;
}
Counter.unload();
state.* = 0;
if (tick(5) != 6 or Counter.tick(5) != null) {
return false;
}
if (Counter.load() == false) {
return false;
}
const restored = state.*;
c64.console.print("STATE {x}\n", .{restored});
return restored == 6;
}
export fn main() void {
c64.console.clear();
if (checkCycle()) {
c64.console.print("STATE CYCLE OK\n", .{});
c64.vic.border = 5;
} else {
c64.console.print("STATE CYCLE FAILED\n", .{});
c64.vic.border = 2;
}
}
Expect STATE 06 and STATE CYCLE OK. The typed call is unavailable after unload until the module is loaded again. The direct call to the resident tick function still works. This example seeds the payload from the boot PRG, so the observed state cycle needs only a PRG and an REU.
Choose what happens to state at unload
Replace persist_in_reu at 0x0780 size 1; with discard_on_unload; and rebuild. The manually cleared byte stays zero after reload. Expect STATE 00 and STATE CYCLE FAILED. Restore the snapshot declaration and compare the results.
Snapshots also cover replacement by another module in the same window. The loader saves the outgoing module's declared bytes, copies and relocates the next payload, then restores that module's saved bytes. Keep state offsets inside the assigned window and separate from code. State consumes REU space in addition to the payload cache. The shared-window state example writes two bytes and checks them after another DLL runs.