# 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`.