Work out the two memory budgets
Each sector contains one byte of terrain, one byte of flags, a two-byte visit counter, and four tile bytes. Forty sectors need 320 bytes of REU. A page of four sectors needs 32 bytes of C64 RAM. Keep those allocations separate so you can reuse the C64 buffer for the next page.
| Owner | Bytes | Lifetime |
|---|---|---|
Far arena map | 320 at REU $020000 | Holds all forty records until map.reset(). |
Near arena scratch | 32 at C64 $9000 | Holds one working page until scratch.reset(). |
The far region ends at $020140 exclusive. A 128 KiB REU ends at $020000 exclusive, so this layout needs at least a 256 KiB unit. The command page uses a 16 MiB REU. A successful presence check tells you the expansion responds. Check installed capacity against the map before relying on any address. See the capacity examples for other layouts.
Translate between table and page indexes
Allocate the far bytes and build a typed view. The handle describes a range in REU, so the CPU reaches its contents through transfers. Use the table index to select what to copy, then use a page index when working in C64 RAM.
| Page index in C64 RAM | Table index in REU | Address of first byte in REU |
|---|---|---|
| 0 | 36 | $020120 |
| 1 | 37 | $020128 |
| 2 | 38 | $020130 |
| 3 | 39 | $020138 |
Each far address is the table base plus the table index multiplied by eight. The language performs that calculation for the typed view. The page index starts over at zero after every materialization.
Read, edit, and flush one page
The program initializes all forty sectors, then selects sectors 36 through 39. Local index zero is far index 36. Update visits and flags in the near view, and pass the original start to flushRange so the result goes back to the right place. Reset scratch once the near view is out of use.
const c64 = @import("c64");
const ZeroPage = @import("ZeroPage");
const Terrain = enum(u8) { grass = 1, water = 2 };
const Sector = struct {
terrain: Terrain,
flags: u8,
visits: u16,
tiles: [4]u8,
};
memory {
region work_ram: c64 at 0x9000 size 32;
region map_reu: reu at 0x020000 size 320;
arena scratch in work_ram;
arena map in map_reu;
}
fn visitPage(start: u8, mark: u8) bool {
if (ZeroPage.reu.present() == false) {
return false;
}
if (map.allocBytes(@sizeOf(Sector) * 40)) |bytes| {
var sectors = ZeroPage.reu.view(Sector, bytes, 40);
const initial = Sector{
.terrain = .grass,
.flags = 0,
.visits = 0,
.tiles = [_]u8{ 1, 2, 3, 4 },
};
var index: u8 = 0;
while (index < 40) {
sectors.set(index, initial);
index += 1;
}
var ok = false;
if (sectors.materializeRange(scratch, start, 4)) |page| {
for (0..4) |i| {
var sector = page.get(i);
sector.visits += 1;
sector.flags |= mark;
page.set(i, sector);
}
sectors.flushRange(start, page);
scratch.reset();
const changed = sectors.get(start);
const last_changed = sectors.get(start + 3);
const unchanged = sectors.get(0);
ok = changed.visits == 1 and changed.flags == mark and
changed.terrain == .grass and changed.tiles[3] == 4 and
last_changed.visits == 1 and unchanged.visits == 0;
}
map.reset();
return ok;
}
return false;
}
export fn main() void {
c64.console.clear();
if (visitPage(36, 0x04)) {
c64.console.print("MAP SECTORS OK\n", .{});
c64.vic.border = 5;
} else {
c64.console.print("MAP SECTORS FAILED\n", .{});
c64.vic.border = 2;
}
}
Expect MAP SECTORS OK. The result comes from a fresh far read after the flush. Sector zero is checked as well because it is outside the edited page. page.set alone edits the local copy. Flush makes those changes visible to later far reads. Reset scratch only when the page is out of use, and reset the far arena only after its views and handles are out of use.
Try two changes that make the page unavailable
- Change the requested start from 36 to 37. The four-sector page would cross the end of the view.
- Restore start 36 and change the C64 scratch reservation from 32 bytes to 31. The page fits the far view but cannot fit in scratch.
Both changes produce MAP SECTORS FAILED. Restore both values and rerun before continuing. The REU guide shows how an application can report the two causes separately. Next, reuse the same near arena for multiple ranges.