# 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 | | $0340-$037E | 63 B | (not linked) | Mouse pointer sprite shape (cassette buffer), written by `ms_init` (src/mouse1351.s) | | $0400-$07E7 | 1000 B | (not linked) | VIC-II screen RAM | | $07F8 | 1 B | (not linked) | Sprite 0 pointer = 13 ($0340 / 64) | | $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` on row 1 instead of the map, and the smoke test's `layout_ok` check fails. 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.3: the program ends at $1FC6 (about 6 KB, see build/stonehold-64.map). That leaves about 4 KB of room below the stack. ## Tile byte encoding Each map byte is also the screen code it is drawn with, and its low nibble is its colour. Colour RAM keeps only the low 4 bits, so `draw_view` (src/view.s) copies the byte to both screen RAM and colour RAM. That costs 14 cycles per tile, about 14,900 cycles per full redraw. Changed in M1.3 with user approval: a glyph/colour lookup table measured 22,754 (PAL) / 23,177 (NTSC) cycles, over one frame. | Bits | Meaning | |---|---| | 7 | always 1: game glyphs live at codes 128-255 | | 6 | claimed by a dwarf | | 5-4 | type: 00 floor, 01 soft rock, 10 hard rock | | 3-0 | colour: the type's colour, or 7 (yellow) when marked for digging | | Code | Tile | Colour | |---|---|---| | $8B | floor | 11 dark grey | | $99 | soft rock | 9 brown | | $AC | hard rock | 12 grey | | $97 / $A7 | marked soft / hard rock | 7 yellow | | $D7 / $E7 | marked and claimed soft / hard rock | 7 yellow | | $88 | dwarf (drawn over the map, not stored in it) | 8 orange | | $81 | cursor (drawn over the map, not stored in it) | 1 white | Use the macros in src/map.h (`TILE_IS_ROCK`, `TILE_IS_MARKED`, `TILE_MARK`, `TILE_UNMARK`) instead of testing bits by hand. A new tile state needs a code whose low nibble is its colour, and a glyph at that code in src/charset.c. charset.c has compile-time checks that keep the codes and the spec's colours in step. ## 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`.