From f70421853678581d1ee160e5659e87c70e527dc0 Mon Sep 17 00:00:00 2001 From: Daniel Covington Date: Thu, 26 Nov 2026 14:03:07 -0500 Subject: [PATCH] M1.1: settle memory map with a custom ld65 config Program $0801-$37FF (stack at top), charset $3800-$3FFF, 128x128 map $4000-$7FFF, spare HIBSS $8000-$CFFF. ld65 fails the build on overflow; main() also checks charset/map addresses at run time (MEMORY CLASH). build: keep -O when a linker config is set and resolve the config path against the repo root. Documented in docs/memory-map.md. Co-Authored-By: Claude Opus 5.5 --- cfg/stonehold-64.cfg | 62 +++++++++++++++++++++++++++++++++++++ docs/memory-map.md | 43 +++++++++++++++++++++++++ docs/toolchain.md | 4 +-- docs/verification-status.md | 4 +-- project.json | 2 +- src/charset.c | 7 ++++- src/charset.h | 3 ++ src/main.c | 11 +++++-- src/map.c | 6 ++++ src/map.h | 13 ++++++++ tools/lib.ps1 | 4 +-- 11 files changed, 148 insertions(+), 11 deletions(-) create mode 100644 cfg/stonehold-64.cfg create mode 100644 docs/memory-map.md create mode 100644 src/map.c create mode 100644 src/map.h diff --git a/cfg/stonehold-64.cfg b/cfg/stonehold-64.cfg new file mode 100644 index 0000000..af82529 --- /dev/null +++ b/cfg/stonehold-64.cfg @@ -0,0 +1,62 @@ +# ld65 configuration for Stonehold 64 (C64). See docs/memory-map.md. +# +# $0801-$37FF MAIN program: code, rodata, data, BSS, C stack (top 2 KB) +# $3800-$3FFF CHARMEM custom character set (VIC bank 0) segment CHARSET +# $4000-$7FFF MAPMEM 128x128 tile map, one byte per tile segment MAP +# $8000-$CFFF HIMEM large uninitialised buffers segment HIBSS +# +# The areas are laid out end to end, so ld65 stops the build with +# "Memory area ... overflow" if anything grows past its boundary. +# cc65's startup code puts the top of the C stack at the end of MAIN ($3800); +# the heap (if ever used) lies between the end of BSS and the stack. + +FEATURES { + STARTADDRESS: default = $0801; +} +SYMBOLS { + __LOADADDR__: type = import; + __EXEHDR__: type = import; + __STACKSIZE__: type = weak, value = $0800; # 2k stack + __LOWTOP__: type = weak, value = $3800; # end of program memory = charset start +} +MEMORY { + ZP: file = "", define = yes, start = $0002, size = $001A; + LOADADDR: file = %O, start = %S - 2, size = $0002; + HEADER: file = %O, define = yes, start = %S, size = $000D; + MAIN: file = %O, define = yes, start = __HEADER_LAST__, size = __LOWTOP__ - __HEADER_LAST__; + BSS: file = "", start = __ONCE_RUN__, size = __LOWTOP__ - __STACKSIZE__ - __ONCE_RUN__; + CHARMEM: file = "", define = yes, start = $3800, size = $0800; + MAPMEM: file = "", define = yes, start = $4000, size = $4000; + HIMEM: file = "", define = yes, start = $8000, size = $5000; +} +SEGMENTS { + ZEROPAGE: load = ZP, type = zp; + LOADADDR: load = LOADADDR, type = ro; + EXEHDR: load = HEADER, type = ro; + STARTUP: load = MAIN, type = ro; + LOWCODE: load = MAIN, type = ro, optional = yes; + CODE: load = MAIN, type = ro; + RODATA: load = MAIN, type = ro; + DATA: load = MAIN, type = rw; + INIT: load = MAIN, type = rw; # uninitialized, but reserves output space + ONCE: load = MAIN, type = ro, define = yes; + BSS: load = BSS, type = bss, define = yes; + CHARSET: load = CHARMEM, type = bss, define = yes; + MAP: load = MAPMEM, type = bss, define = yes; + HIBSS: load = HIMEM, type = bss, define = yes, optional = yes; +} +FEATURES { + CONDES: type = constructor, + label = __CONSTRUCTOR_TABLE__, + count = __CONSTRUCTOR_COUNT__, + segment = ONCE; + CONDES: type = destructor, + label = __DESTRUCTOR_TABLE__, + count = __DESTRUCTOR_COUNT__, + segment = RODATA; + CONDES: type = interruptor, + label = __INTERRUPTOR_TABLE__, + count = __INTERRUPTOR_COUNT__, + segment = RODATA, + import = __CALLIRQ__; +} diff --git a/docs/memory-map.md b/docs/memory-map.md new file mode 100644 index 0000000..e391dfe --- /dev/null +++ b/docs/memory-map.md @@ -0,0 +1,43 @@ +# Stonehold 64 - Memory map + +Settled in milestone M1.1 (assumption A3). The linker config `cfg/stonehold-64.cfg` defines +these areas. `tools/build.ps1` passes it to cl65 (`resolved.targets[0].build.config` in +`project.json`). + +| Range | Size | ld65 memory area / segment | Contents | +|---|---|---|---| +| $0002-$001B | 26 B | ZP / ZEROPAGE | cc65 runtime zero page | +| $0400-$07E7 | 1000 B | (not linked) | VIC-II screen RAM | +| $0801-$37FF | 12 KB | MAIN / STARTUP, CODE, RODATA, DATA, INIT, ONCE, BSS | Program. BSS may use up to $2FFF | +| $3000-$37FF | 2 KB | (top of MAIN) | C stack (`__STACKSIZE__` = $0800), grows down from $3800 | +| $3800-$3FFF | 2 KB | CHARMEM / CHARSET | Custom character set (`charset[]`), VIC bank 0 | +| $4000-$7FFF | 16 KB | MAPMEM / MAP | 128x128 tile map (`map[]`), one byte per tile | +| $8000-$CFFF | 20 KB | HIMEM / HIBSS | Large uninitialised buffers (planned: BFS scratch, about 4.2 KB) | +| $D000-$DFFF | 4 KB | (I/O) | VIC-II, SID, colour RAM, CIAs | + +## Rules + +- The areas sit end to end, and each area's size is fixed by the next area's start. If code, data + or BSS grows past its boundary, ld65 stops the build: `Segment 'X' overflows memory area 'Y'`, + then cl65 exits 1 and `tools/build.ps1` exits 1. This was tested in M1.1 with one extra byte in + MAP and with 10 KB of extra BSS. Both links failed. +- Only the program part (MAIN, $0801 up to the end of DATA/INIT/ONCE) is in the PRG file. CHARSET, + MAP and HIBSS are uninitialised, take no file space and are **not** zeroed by the startup code. + Code that uses them must initialise them. +- To put a variable in one of these areas, wrap it in `#pragma bss-name (push, "HIBSS")` and + `#pragma bss-name (pop)`. `src/map.c` and `src/charset.c` show how. +- At run time `main()` checks that `charset` is at `CHARSET_ADDR` (charset.h), that `map` is at + `MAP_ADDR` (map.h) and that BSS ends below the charset. If any check fails it shows + `MEMORY CLASH` instead of `CHARSET OK`. This catches a linker config that has drifted from the + headers. +- cc65's startup puts the top of the C stack at the end of MAIN ($3800), so the stack and any heap + stay below the charset. The game does not use `malloc`. +- Size at M1.1: the program ends at $1702 (about 3.8 KB). That leaves about 6 KB of room below the + stack. + +## If MAIN fills up + +Move large tables and buffers to HIBSS first. If code still does not fit, a later task can add a +file-backed area at $4000 and move the map to $8000-$BFFF. The charset area would then need +`fill = yes`, so the PRG would grow by 2 KB. That change touches this file, the linker config and +`MAP_ADDR`. diff --git a/docs/toolchain.md b/docs/toolchain.md index eaa574a..bb90ab1 100644 --- a/docs/toolchain.md +++ b/docs/toolchain.md @@ -51,7 +51,7 @@ Starter configurations marked verified were exercised by the Foundry's automated ### Target `c64` - Status: **verified** starter configuration (cc65, c64/vicii-40, mixed-c-asm) -- Build (what tools/build.ps1 runs): `cl65 -t c64 -g -O -D FOUNDRY_TARGET_C64 -I src -o build/stonehold-64.prg -Ln build/stonehold-64.lbl -m build/stonehold-64.map src/*.c src/*.s` +- Build (what tools/build.ps1 runs): `cl65 -t c64 -g -O -C cfg/stonehold-64.cfg -u __EXEHDR__ -D FOUNDRY_TARGET_C64 -I src -o build/stonehold-64.prg -Ln build/stonehold-64.lbl -m build/stonehold-64.map src/*.c src/*.s` - Emulator: `x64sc -pal -controlport1device 3 -autostart build/stonehold-64.prg` - Emulator note - mouse-1351: Control port 1 = Mouse (1351). Grab the host mouse in VICE with Alt+M (default GTK3 hotkeys; Alt+Q with the WinVICE legacy hotkey file) or start VICE with -mouse; without a grab the C64 pointer does not move. Press Alt+M again to release it. - Smoke test: `pwsh -File tools/smoke.ps1 -Target c64`. It runs VICE headless for 30000000 cycles, saves memory when the program reaches `_smoke_ready`, and checks: @@ -73,7 +73,7 @@ Smoke hooks in the source: ## Assumptions still to verify - [ ] A1: the pointer follows the grabbed host mouse and the left button is detected while the joystick in port 2 and the keyboard still work (before M2 is complete). -- [ ] A3: memory map. Check the map file (build/stonehold-64.map) at every milestone; settle where the 16 KB map lives in M1. +- [x] A3: memory map settled in M1.1 (2026-11-26): custom ld65 config `cfg/stonehold-64.cfg`, map at $4000-$7FFF, see docs/memory-map.md. Still check build/stonehold-64.map at every milestone. - [ ] A4: measure worst-case pathfinding time with a border-colour timing bar in M3. - [ ] Frame budget: the view redraw finishes within one frame on PAL and NTSC (timing bar) in M1. diff --git a/docs/verification-status.md b/docs/verification-status.md index 9721c64..6610c9f 100644 --- a/docs/verification-status.md +++ b/docs/verification-status.md @@ -8,8 +8,8 @@ manual = `pending`, `confirmed by user ()`, `problem reported ():