# Stonehold 64 - Toolchain ## Decision - Language: **C (cc65) with ca65 assembly modules** - Recipe: **Windows cross-development with cc65 (C, ca65 assembly or mixed) and VICE** (`cc65`) - Build location: Windows cross-development - Foundry recommendation: `cc65` / `mixed-c-asm` (C (cc65) with ca65 assembly modules) No user override. The recommended language is used. ### Why this language - BASIC V2 is far too slow: generating a 16 KB map, running pathfinding for three dwarves and redrawing a scrolling 920-tile view every frame is beyond interpreted BASIC. - C (cc65) keeps the game rules, map generator and pathfinding readable and easy to change, and cc65 ships a 1351 mouse driver. - Small ca65 assembly functions with C prototypes handle the speed-critical parts: the map view redraw and the frame wait. More can be moved to assembly later if profiling shows a hot spot (for example the pathfinding inner loop). - The user asked for exactly this split: C with assembly functions callable from C. It matches the Foundry recommendation, so no override is needed. - Everything builds on Windows with one cc65 install and runs in VICE x64sc. ### Alternatives considered | Alternative | Why not chosen | |---|---| | BASIC V2 | Too slow for map generation, pathfinding and per-frame view redraws. | | C only (cc65) | A 920-tile view redraw in compiled C takes several frames, so scrolling would stutter. Assembly for that loop is needed. | | 6502 assembly only (ca65) | Fastest, but map generation, pathfinding and job logic would take much longer to write and change. The speed is only needed in a few routines. | ## Tools | Tool | Purpose | Verified version | Install source | First command if missing | Override variable | |---|---|---|---|---|---| | `cl65` | cc65 compile/assemble/link driver (C and ca65 assembly) | cl65 V2.19 - Git cc3c40c | https://cc65.github.io/getting-started.html | Download https://sourceforge.net/projects/cc65/files/cc65-snapshot-win64.zip, unzip it (for example to C:\cc65), then run: [Environment]::SetEnvironmentVariable('CC65_HOME','C:\cc65','User') and open a new terminal. | `CC65_HOME` | | `x64sc` | VICE cycle-exact Commodore 64 emulator | VICE 3.10 | https://vice-emu.sourceforge.io/index.html#download | winget install --id VICE-Team.VICE.GTK3 -e (or unzip GTK3VICE-3.10-win64.zip and set VICE_HOME to the unzipped folder) | `VICE_HOME` | Check them with `pwsh -File tools/check-prereqs.ps1`. Lookup order for each tool: its environment variable (folder or folder\bin), then `PATH`, then common install folders. ## Commands | Purpose | Command | |---|---| | Check prerequisites | `pwsh -File tools/check-prereqs.ps1` | | Build all targets | `pwsh -File tools/build.ps1` | | Build one target | `pwsh -File tools/build.ps1 -Target ` | | Build with a fixed map seed | `pwsh -File tools/build.ps1 -Define FIXED_SEED=12345` (`tools/run.ps1` takes `-Define` too) | | Run in VICE | `pwsh -File tools/run.ps1 -Target ` (add `-Video ntsc` to try NTSC) | | Automated smoke test | `pwsh -File tools/smoke.ps1` (exit 0 pass, 1 fail, 2 missing tool, 3 manual only) | ## Target settings and verification status Starter configurations marked verified were exercised by the Foundry's automated checks on 2026-11-26 with cl65 V2.19 - Git cc3c40c (Windows x64 snapshot), VICE 3.10 GTK3 win64 (x64sc, x128, c1541, petcat), PowerShell 7.6.7 on Windows 11 Pro 10.0.26200. ### Target `c64` - Status: **verified** starter configuration (cc65, c64/vicii-40, mixed-c-asm) - 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: - Title on row 0 - Custom charset installed without a memory clash - Stone counter on the status line - C64 KERNAL reset vector at $FFFC (machine is in C64 mode) ### How the smoke test works `tools/smoke.ps1` starts VICE with `-config -default -warp -minimized -sounddev dummy`, so your own VICE settings are never read or changed. It gives the monitor a command file (`-moncommands`) that sets non-stopping tracepoints on the program's ready point. When the program reaches it, VICE saves memory with `bsave` into `build/smoke//`. VICE exits after `-limitcycles` cycles and writes `-exitscreenshot`; that is the normal way the run ends, so VICE's own exit code is ignored. The script then compares the saved memory with the expected screen text (as VIC-II/VDC screen codes) and mode bytes. Smoke hooks in the source: - cc65 C programs: `smoke_capture()` and `smoke_ready()` in `src/smoke.c`. 80-column builds copy the first VDC rows into `smoke_probe[]`, because VDC memory is not visible to the CPU. - ca65 programs: the `smoke_ready` label. - BASIC programs: `POKE 679,1` (BASIC V2) or `POKE 3071,1` (BASIC 7.0) after the first screen is drawn. ## Known compiler issue cl65 V2.19 Git cc3c40c with `-O` miscompiled an indexed table lookup in a loop (`scr[col] = tile_glyph[t]`, src/view.c, found in M1.2). It used the low byte of the destination address as the table index. If a C lookup table gives wrong results, look at the generated assembly (`cc65 -t c64 -O -I src -o out.s src/.c`) before you suspect the logic. ## 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). - [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. ## Sources - https://cc65.github.io/doc/cl65.html - https://cc65.github.io/doc/c64.html#toc4.2 - https://cc65.github.io/doc/c128.html - https://cc65.github.io/doc/ca65.html - https://vice-emu.sourceforge.io/vice_2.html#SEC50 - https://vice-emu.sourceforge.io/vice_7.html#SEC194 - https://vice-emu.sourceforge.io/vice_12.html - https://cc65.github.io/getting-started.html - https://vice-emu.sourceforge.io/index.html#download