|
|
|
@@ -0,0 +1,199 @@ |
|
|
|
--- |
|
|
|
name: c64-workflow |
|
|
|
description: > |
|
|
|
Start here for any task that creates, builds, runs, tests, or debugs a Commodore 64 |
|
|
|
or C128 program in this repository. Covers the `c64` command (doctor, new, build, |
|
|
|
run, test, mon), c64project.json, the $D7FF test-result protocol, reading test |
|
|
|
artifacts, choosing between cc65 C, ca65 assembly, BASIC, and Power C, and the |
|
|
|
PETSCII/charset traps that make text show up wrong. |
|
|
|
--- |
|
|
|
|
|
|
|
# C64 Development Workflow |
|
|
|
|
|
|
|
Use one loop for every program: **scaffold → build → headless test → inspect → fix**. |
|
|
|
Never call a change finished until `c64 test` passes. Its output shows the actual |
|
|
|
C64 screen, so you can check the result yourself instead of asking the user to look. |
|
|
|
|
|
|
|
``` |
|
|
|
c64 doctor # once per machine: cc65 + VICE present? |
|
|
|
c64 new invaders -Template c # projects/invaders from templates/c |
|
|
|
c64 build projects/invaders # -> build/invaders.prg .lbl .map .d64 |
|
|
|
c64 test projects/invaders # headless; prints screen + PASS/FAIL |
|
|
|
c64 run projects/invaders # for the user to play (opens a window) |
|
|
|
``` |
|
|
|
|
|
|
|
`c64` is `c64.cmd` at the repo root, which calls `pwsh tools/c64.ps1`. From PowerShell use |
|
|
|
`.\c64.cmd`; from Git Bash use `./c64.cmd` or `pwsh tools/c64.ps1 ...`. Exit codes: |
|
|
|
0 = success/pass, 1 = test failed, 2 = usage or tool error. |
|
|
|
|
|
|
|
Any command also accepts a single source file (`c64 test examples/moonlander.c`). Output |
|
|
|
then goes to `build/` next to the file. |
|
|
|
|
|
|
|
## Choose the toolchain |
|
|
|
|
|
|
|
| Need | Use | Template | |
|
|
|
|------|-----|----------| |
|
|
|
| Games, tools, most new programs | cc65 C (`cl65`), with asm for hot loops | `c` | |
|
|
|
| Raster effects, cycle timing, size-critical code, IRQ handlers | ca65 assembly | `asm` | |
|
|
|
| Small demos, teaching, a type-in listing | BASIC 2.0 (`petcat`) | `basic` | |
|
|
|
| C that must compile **on** the C64 | Power C: load `.claude/skills/powerc/SKILL.md` | none | |
|
|
|
| C128 native mode | any of the above with `"machine": "c128"` | `c` / `asm` | |
|
|
|
|
|
|
|
Mixed C + asm: put `.s` files in `src/`. The C template's manifest already includes |
|
|
|
`src/*.s`. cc65 calling convention: arguments on the C stack (`popa`/`popax`), the last |
|
|
|
argument in A/X when `__fastcall__` (the default). Return values go in A (8-bit) or A/X (16-bit). |
|
|
|
|
|
|
|
## c64project.json |
|
|
|
|
|
|
|
```json |
|
|
|
{ |
|
|
|
"name": "invaders", // output name; disk filename (16 chars max) |
|
|
|
"machine": "c64", // c64 | c128 |
|
|
|
"language": "c", // c | asm | basic |
|
|
|
"sources": ["src/*.c", "src/*.s"], |
|
|
|
"config": "src/game.cfg", // optional ld65 config (asm default: c64-asm.cfg + BASIC stub) |
|
|
|
"flags": ["-Oirs"], // extra cl65 flags (-D NAME=1, --static-locals, ...) |
|
|
|
"disk": true, // also write build/<name>.d64 (or a path: "build/x.d81") |
|
|
|
"diskFiles": [{ "path": "data/level1.bin", "name": "level1" }, |
|
|
|
{ "path": "data/notes.txt", "name": "notes", "type": "seq" }], |
|
|
|
"basicCase": "lower", // BASIC only: "preserve" keeps case for shifted PETSCII |
|
|
|
"test": { // defaults for `c64 test`; CLI options override |
|
|
|
"expect": ["SCORE 0"], |
|
|
|
"until": "main_loop", // OR "afterJiffies": 300 (default: wait for $D7FF) |
|
|
|
"keys": "", // typed with VICE -keybuf; \n is RETURN |
|
|
|
"timeoutSeconds": 60, |
|
|
|
"maxCycles": 150000000 |
|
|
|
} |
|
|
|
} |
|
|
|
``` |
|
|
|
|
|
|
|
(JSON has no comments; they are shown here only to explain the fields.) |
|
|
|
|
|
|
|
Builds always emit `build/<name>.lbl` (VICE labels) and `build/<name>.map`. Read the map |
|
|
|
when memory gets tight: it lists every segment's start, end, and size. |
|
|
|
|
|
|
|
## Headless testing: `c64 test` |
|
|
|
|
|
|
|
The runner boots VICE minimized in warp mode and injects the PRG. When a **trigger** |
|
|
|
fires it dumps RAM and I/O, decodes the text screen, and closes VICE. |
|
|
|
|
|
|
|
| Trigger | Use when | |
|
|
|
|---------|----------| |
|
|
|
| default: first store to `$D7FF` | You control the code. Store 0 for pass, 1-255 for a failure code. | |
|
|
|
| `-Until <label or $addr>` | Stop when execution reaches a routine, e.g. `-Until main_loop`. Labels come from the `.lbl` file. | |
|
|
|
| `-AfterJiffies <n>` | Existing programs you don't want to change. Fires after n Kernal IRQs counted from power-on (about 2.5 s of boot comes first). | |
|
|
|
|
|
|
|
If a program replaces the Kernal IRQ (`$0314` to code that never reaches `$EA31`), the |
|
|
|
jiffy trigger never fires. Pass `-TickAddress <your IRQ entry>` instead. |
|
|
|
|
|
|
|
Verdict: FAIL on timeout, on a non-zero `$D7FF` code, on a BASIC error (the runner |
|
|
|
breaks on `$A437` and names the error, e.g. `BASIC error 11 (SYNTAX)`), or when any |
|
|
|
`-Expect "TEXT"` is missing from the screen (case-insensitive; repeat `-Expect` for |
|
|
|
several strings). |
|
|
|
|
|
|
|
Report results from code: |
|
|
|
|
|
|
|
```c |
|
|
|
#include "c64test.h" /* lib/ is on the include path */ |
|
|
|
c64test_check(score == 0, 1); /* on failure: report code 1 and stop */ |
|
|
|
c64test_pass(); /* screen is ready: capture now */ |
|
|
|
``` |
|
|
|
```asm |
|
|
|
.include "c64test.inc" |
|
|
|
TEST_FAIL 3 ; or TEST_PASS |
|
|
|
``` |
|
|
|
```basic |
|
|
|
90 POKE 55295,0 : REM $D7FF, 0 = PASS |
|
|
|
``` |
|
|
|
|
|
|
|
On real hardware `$D7FF` is an unused SID register image, so these stores do nothing |
|
|
|
and can stay in shipped code. `x64sc -debugcart` uses the same register (VICE exits |
|
|
|
with the stored value) if you need a bare-VICE check. |
|
|
|
|
|
|
|
### Artifacts (`<project>/build/test/`) |
|
|
|
|
|
|
|
| File | Contents | |
|
|
|
|------|----------| |
|
|
|
| `screen.txt` | Decoded 40×25 text: follows `$DD00`/`$D018`, so moved screens work; graphics show as `#` | |
|
|
|
| `screenshot.png` | Real VIC-II output. Open it with the Read tool for bitmaps, sprites, and colors | |
|
|
|
| `ram.bin` | 64KB RAM, banking ignored (2-byte load-address header, then $0000-$FFFF) | |
|
|
|
| `io.bin` | $D000-$DFFF as the CPU sees I/O: VIC, SID, color RAM, CIAs (2-byte header) | |
|
|
|
| `monitor.log` | VICE monitor output. The `.;xxxx` line holds PC/A/X/Y at the trigger | |
|
|
|
| `result.json` | `passed`, `reason`, `code`, `pc`, `missing`, `screen` | |
|
|
|
|
|
|
|
Inspect a variable: find it in `build/<name>.lbl`, then read `ram.bin` at |
|
|
|
`2 + address`. For example, with pwsh: |
|
|
|
`$r=[IO.File]::ReadAllBytes("build/test/ram.bin"); $r[2+0x0834]`. |
|
|
|
|
|
|
|
### Testing patterns that work |
|
|
|
|
|
|
|
- **Logic first**: keep game rules in plain functions and check them in a self-test |
|
|
|
that runs before the main loop (see `templates/c/src/main.c`). |
|
|
|
- **Screen states**: draw a screen, `c64test_pass()`, and assert it with `-Expect`. |
|
|
|
- **Input**: `-Keys "N\nY\n"` types into the keyboard buffer. Joystick input can't be |
|
|
|
injected from the command line. Add a test hook instead (e.g. a `#define` that feeds |
|
|
|
scripted moves) or check the state after a fixed number of jiffies. |
|
|
|
- **Regression set**: `c64 test projects/a projects/b examples/x.c` runs several and |
|
|
|
prints `n/m passed`. |
|
|
|
|
|
|
|
## Live debugging: `c64 run -Monitor` + `c64 mon` |
|
|
|
|
|
|
|
``` |
|
|
|
c64 run projects/invaders -Monitor # remote monitor on 127.0.0.1:6510, labels loaded |
|
|
|
c64 mon "r" "m .score .score+1" "d .main" # each call pauses, runs commands, resumes |
|
|
|
c64 mon "break .player_hit" -Stay # leave the monitor active (paused) |
|
|
|
c64 mon "x" # resume |
|
|
|
``` |
|
|
|
|
|
|
|
Commands are VICE monitor syntax (`.claude/skills/vice-emulator/SKILL.md`). Labels use a |
|
|
|
leading dot. Use `watch store <addr>` to find who corrupts a variable, and `bank io` + |
|
|
|
`m d000 d02e` for VIC registers. |
|
|
|
|
|
|
|
## Text and charset traps |
|
|
|
|
|
|
|
| Source | Write text as | Why | |
|
|
|
|--------|---------------|-----| |
|
|
|
| ca65 `.byte "..."` | **lower case** (`"press fire"`) | Upper-case ASCII becomes shifted PETSCII and shows as graphics in the default charset | |
|
|
|
| cc65 C strings | normal case (`"Press Fire"`) | The C startup code switches to the mixed-case charset and maps ASCII correctly | |
|
|
|
| BASIC (`.bas` via petcat) | either; it is lowered by default | petcat tokenizes lower-case keywords; `{clr}`, `{wht}`, `{down}` are control codes | |
|
|
|
| Screen RAM pokes | screen codes, not PETSCII | `A` = 1, `@` = 0, space = 32 | |
|
|
|
|
|
|
|
## Memory and build gotchas |
|
|
|
|
|
|
|
- cc65 C programs load at `$0801`. The C stack and heap sit below `$D000` by default. |
|
|
|
`$C000-$CFFF` is not free in C programs unless you change the config. |
|
|
|
- In assembly, free zero page is `$FB-$FE` (plus `$02`) while BASIC and the Kernal are |
|
|
|
in use. See `.claude/skills/memory-map/SKILL.md` for the complete table. |
|
|
|
- A PRG that extends past `$9FFF` gets a build warning. It must bank out BASIC |
|
|
|
(`$01` = `$36`) before it runs code or data that sits under the ROM. |
|
|
|
- Sprite, charset, and bitmap data must lie inside the VIC bank selected by `$DD00`, and |
|
|
|
not at `$1000-$1FFF` or `$9000-$9FFF` in banks 0 and 2 (the VIC sees character ROM there). |
|
|
|
- PAL is the VICE default (`-Ntsc` on `c64 test` switches it). Raster timing and SID |
|
|
|
pitch tables differ, so state which one your code assumes. |
|
|
|
|
|
|
|
## VICE automation facts (verified with VICE 3.10 on this repo) |
|
|
|
|
|
|
|
These shaped `tools/C64Tools.psm1`. Read them before you script VICE yourself: |
|
|
|
|
|
|
|
- `-debugcart` exits **during** the `$D7FF` store, before any watchpoint on `$D7FF` |
|
|
|
runs. To dump state at the end, watch the store *without* `-debugcart`. |
|
|
|
- A checkpoint `command` string with two quoted filenames is mis-parsed. Put the commands |
|
|
|
in a playback file and use `command 1 "pb \"file\""`. |
|
|
|
- `quit` is ignored inside a `command` string but works from a playback file. Quitting |
|
|
|
cleanly flushes `-monlog` and writes `-exitscreenshot`; killing the process loses both. |
|
|
|
- The monitor `screenshot` command hangs when it runs from a checkpoint. Use `-exitscreenshot`. |
|
|
|
- `ignore <n> <count>` on `break exec ea31` gives a deterministic "after N jiffies" |
|
|
|
trigger. A full-range `break exec 0000 ffff` with an ignore count never fired. |
|
|
|
- Autostarting a `.d64` is about 10 s slower than injecting the PRG |
|
|
|
(`-autostartprgmode 1`). Use `-FromDisk` only when testing disk loading itself. |
|
|
|
|
|
|
|
## Where things are |
|
|
|
|
|
|
|
| Path | What | |
|
|
|
|------|------| |
|
|
|
| `tools/C64Tools.psm1`, `tools/c64.ps1` | The toolchain (PowerShell 7) | |
|
|
|
| `templates/{c,asm,basic}` | Project skeletons used by `c64 new` | |
|
|
|
| `lib/c64test.h`, `lib/c64test.inc` | Test-result helpers; `lib/` is on cc65's include path | |
|
|
|
| `projects/` | New programs | |
|
|
|
| `examples/` | Small single-file examples; `c64 test examples/<file>` works on them | |
|
|
|
| `reference/` | Register, opcode, Kernal, memory, and DOS tables | |