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 <noreply@anthropic.com>main
| @@ -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__; | |||||
| } | |||||
| @@ -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`. | |||||
| @@ -51,7 +51,7 @@ Starter configurations marked verified were exercised by the Foundry's automated | |||||
| ### Target `c64` | ### Target `c64` | ||||
| - Status: **verified** starter configuration (cc65, c64/vicii-40, mixed-c-asm) | - 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: `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. | - 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: | - 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 | ## 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). | - [ ] 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. | - [ ] 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. | - [ ] Frame budget: the view redraw finishes within one frame on PAL and NTSC (timing bar) in M1. | ||||
| @@ -8,8 +8,8 @@ manual = `pending`, `confirmed by user (<date>)`, `problem reported (<date>): <n | |||||
| | Item | Automated check | Manual verification by user | | | Item | Automated check | Manual verification by user | | ||||
| |---|---|---| | |---|---|---| | ||||
| | M0.1 Run tools/check-prereqs.ps1, then tools/smoke.ps1 for every target. | passed (2026-11-26, `pwsh -File tools/check-prereqs.ps1` exit 0; `pwsh -File tools/build.ps1` exit 0; `pwsh -File tools/smoke.ps1` exit 0, [PASS] c64, 4/4 checks). Run on the working tree including the uncommitted src/charset.c/.h edit (glyphs at codes 128+), see Notes. | pending | | |||||
| | M1.1 Settle the memory map (A3): where the 16 KB map, the charset and the program live. Add a check that fails the build or shows an error if they overlap. | not run | pending | | |||||
| | M0.1 Run tools/check-prereqs.ps1, then tools/smoke.ps1 for every target. | passed (2026-11-26, `pwsh -File tools/check-prereqs.ps1` exit 0; `pwsh -File tools/build.ps1` exit 0; `pwsh -File tools/smoke.ps1` exit 0, [PASS] c64, 4/4 checks). Run on the working tree including the uncommitted src/charset.c/.h edit (glyphs at codes 128+), see Notes. | confirmed by user (2026-11-26) | | |||||
| | M1.1 Settle the memory map (A3): where the 16 KB map, the charset and the program live. Add a check that fails the build or shows an error if they overlap. | passed (2026-11-26): `pwsh -File tools/build.ps1` exit 0, build/stonehold-64.map shows program $0801-$1702, CHARSET $3800-$3FFF, MAP $4000-$7FFF, no overlap; deliberate overflow links (1 extra byte in MAP, 10 KB extra BSS) failed with ld65 'overflows memory area', cl65 exit 1; `pwsh -File tools/smoke.ps1` exit 0, [PASS] c64 4/4. | pending | | |||||
| | M1.2 Map generator in C: seeded PRNG, soft rock, hard-rock veins, caverns, starting cave near the centre. Show the seed on row 24. | not run | pending | | | M1.2 Map generator in C: seeded PRNG, soft rock, hard-rock veins, caverns, starting cave near the centre. Show the seed on row 24. | not run | pending | | ||||
| | M1.3 Assembly view redraw (draw_view(x, y), C prototype) copying a 40x23 window of the map to screen and colour RAM via a tile-to-glyph/colour table. | not run | pending | | | M1.3 Assembly view redraw (draw_view(x, y), C prototype) copying a 40x23 window of the map to screen and colour RAM via a tile-to-glyph/colour table. | not run | pending | | ||||
| | M1.4 Keyboard cursor (cursor keys and W/A/S/D) with edge scrolling and map-border clamping. Status line: 'STONE 000 SEED nnnnn RUN'. | not run | pending | | | M1.4 Keyboard cursor (cursor keys and W/A/S/D) with edge scrolling and map-border clamping. Status line: 'STONE 000 SEED nnnnn RUN'. | not run | pending | | ||||
| @@ -723,7 +723,7 @@ | |||||
| "build": { | "build": { | ||||
| "kind": "cc65", | "kind": "cc65", | ||||
| "cc65Target": "c64", | "cc65Target": "c64", | ||||
| "config": null, | |||||
| "config": "cfg/stonehold-64.cfg", | |||||
| "defines": [ | "defines": [ | ||||
| "FOUNDRY_TARGET_C64" | "FOUNDRY_TARGET_C64" | ||||
| ], | ], | ||||
| @@ -22,11 +22,16 @@ static const unsigned char glyphs[] = { | |||||
| 0xE7, 0x81, 0x81, 0x00, 0x00, 0x81, 0x81, 0xE7 | 0xE7, 0x81, 0x81, 0x00, 0x00, 0x81, 0x81, 0xE7 | ||||
| }; | }; | ||||
| /* The 2 KB charset itself, placed at CHARSET_ADDR by the linker (segment CHARSET). */ | |||||
| #pragma bss-name (push, "CHARSET") | |||||
| unsigned char charset[2048]; | |||||
| #pragma bss-name (pop) | |||||
| static unsigned char saved_vic_addr; | static unsigned char saved_vic_addr; | ||||
| void charset_install(void) | void charset_install(void) | ||||
| { | { | ||||
| unsigned char *dst = (unsigned char *)CHARSET_ADDR; | |||||
| unsigned char *dst = charset; | |||||
| unsigned char old_port; | unsigned char old_port; | ||||
| unsigned i; | unsigned i; | ||||
| @@ -4,6 +4,9 @@ | |||||
| /* Custom character set location (VIC bank 0, 2 KB). See docs and assumption A3. */ | /* Custom character set location (VIC bank 0, 2 KB). See docs and assumption A3. */ | ||||
| #define CHARSET_ADDR 0x3800u | #define CHARSET_ADDR 0x3800u | ||||
| /* The charset storage, placed at CHARSET_ADDR by the linker (cfg/stonehold-64.cfg). */ | |||||
| extern unsigned char charset[2048]; | |||||
| /* Game glyph screen codes. | /* Game glyph screen codes. | ||||
| ** cc65 runs the C64 in the lower/upper-case character set, where conio puts | ** cc65 runs the C64 in the lower/upper-case character set, where conio puts | ||||
| ** capital letters at screen codes 65-90. Codes 0-127 therefore keep the ROM | ** capital letters at screen codes 65-90. Codes 0-127 therefore keep the ROM | ||||
| @@ -21,11 +21,13 @@ | |||||
| #include "smoke.h" | #include "smoke.h" | ||||
| #include "frame.h" | #include "frame.h" | ||||
| #include "charset.h" | #include "charset.h" | ||||
| #include "map.h" | |||||
| #define SCREEN ((unsigned char *)0x0400) | #define SCREEN ((unsigned char *)0x0400) | ||||
| #define COLRAM ((unsigned char *)0xD800) | #define COLRAM ((unsigned char *)0xD800) | ||||
| /* Linker symbols: end of the program's BSS must stay below the charset. */ | |||||
| /* Linker symbols. cfg/stonehold-64.cfg already stops the build if an area overflows; | |||||
| ** this run-time check also catches a config that disagrees with charset.h / map.h. */ | |||||
| extern unsigned char _BSS_RUN__[]; | extern unsigned char _BSS_RUN__[]; | ||||
| extern unsigned char _BSS_SIZE__[]; | extern unsigned char _BSS_SIZE__[]; | ||||
| @@ -86,8 +88,11 @@ int main(void) | |||||
| bgcolor(COLOR_BLACK); | bgcolor(COLOR_BLACK); | ||||
| clrscr(); | clrscr(); | ||||
| /* The charset area must not overlap the program (assumption A3). */ | |||||
| charset_ok = (unsigned)_BSS_RUN__ + (unsigned)_BSS_SIZE__ <= CHARSET_ADDR; | |||||
| /* Memory map (assumption A3, docs/memory-map.md): program below the charset, | |||||
| ** charset and map exactly where the VIC-II setup and the C code expect them. */ | |||||
| charset_ok = (unsigned)_BSS_RUN__ + (unsigned)_BSS_SIZE__ <= CHARSET_ADDR | |||||
| && (unsigned)charset == CHARSET_ADDR | |||||
| && (unsigned)map == MAP_ADDR; | |||||
| textcolor(COLOR_WHITE); | textcolor(COLOR_WHITE); | ||||
| cputsxy(0, 0, "STONEHOLD 64"); | cputsxy(0, 0, "STONEHOLD 64"); | ||||
| @@ -0,0 +1,6 @@ | |||||
| /* World map storage. Generation comes in M1.2. */ | |||||
| #include "map.h" | |||||
| #pragma bss-name (push, "MAP") | |||||
| unsigned char map[MAP_W * MAP_H]; | |||||
| #pragma bss-name (pop) | |||||
| @@ -0,0 +1,13 @@ | |||||
| #ifndef STONEHOLD_MAP_H | |||||
| #define STONEHOLD_MAP_H | |||||
| /* The 128x128 world map, one byte per tile, at MAP_ADDR ($4000-$7FFF). | |||||
| ** The linker places it (segment MAP in cfg/stonehold-64.cfg); see docs/memory-map.md. | |||||
| */ | |||||
| #define MAP_W 128u | |||||
| #define MAP_H 128u | |||||
| #define MAP_ADDR 0x4000u | |||||
| extern unsigned char map[MAP_W * MAP_H]; | |||||
| #endif | |||||
| @@ -180,8 +180,8 @@ function Invoke-TargetBuild([hashtable]$Project, [hashtable]$Target, [hashtable] | |||||
| $cl65 = Get-ToolPath $Project 'cl65' | $cl65 = Get-ToolPath $Project 'cl65' | ||||
| $sources = Get-SourceFiles $build | $sources = Get-SourceFiles $build | ||||
| if ($sources.Count -eq 0) { throw "No .c or .s files found in $(@($build.sourceDirs) -join ', ')." } | if ($sources.Count -eq 0) { throw "No .c or .s files found in $(@($build.sourceDirs) -join ', ')." } | ||||
| $toolArgs = @('-t', $build.cc65Target, '-g') | |||||
| if ($build.config) { $toolArgs += @('-C', $build.config, '-u', '__EXEHDR__') } else { $toolArgs += '-O' } | |||||
| $toolArgs = @('-t', $build.cc65Target, '-g', '-O') | |||||
| if ($build.config) { $toolArgs += @('-C', (Get-RepoPath $build.config), '-u', '__EXEHDR__') } | |||||
| foreach ($d in @($build.defines)) { if ($d) { $toolArgs += @('-D', $d) } } | foreach ($d in @($build.defines)) { if ($d) { $toolArgs += @('-D', $d) } } | ||||
| foreach ($dir in @($build.sourceDirs)) { $toolArgs += @('-I', (Get-RepoPath $dir)) } | foreach ($dir in @($build.sourceDirs)) { $toolArgs += @('-I', (Get-RepoPath $dir)) } | ||||
| $toolArgs += @('-o', $artifact, '-Ln', (Get-RepoPath $build.labels), '-m', (Get-RepoPath $build.map)) | $toolArgs += @('-o', $artifact, '-Ln', (Get-RepoPath $build.labels), '-m', (Get-RepoPath $build.map)) | ||||
Powered by TurnKey Linux.