浏览代码

Add the c64-workflow skill and retire the c64os debug examples.

The new skill is the starting point for creating, building, testing, and
debugging programs: toolchain choice, c64project.json, the $D7FF result
protocol, test artifacts, live monitor use, PETSCII/charset traps, and the
VICE automation behaviors verified while building tools/.

The four c64os examples targeted a c64os.prg that is not in this repository.
examples/vice-debug-session.mon replaces them for any built program, and the
vice-emulator skill and reference now point at the c64 commands.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
refactor/agent-workflow
父节点
当前提交
b109dc04a7
共有 8 个文件被更改,包括 261 次插入 和 121 次删除
  1. +199
    -0
      .claude/skills/c64-workflow/SKILL.md
  2. +16
    -13
      .claude/skills/vice-emulator/SKILL.md
  3. +0
    -14
      examples/vice-autostart-c64os.bat
  4. +0
    -22
      examples/vice-debug-c64os.bat
  5. +35
    -0
      examples/vice-debug-session.mon
  6. +0
    -37
      examples/vice-monitor-c64os-debug.txt
  7. +0
    -20
      examples/vice-monitor-c64os.txt
  8. +11
    -15
      reference/vice-usage.md

+ 199
- 0
.claude/skills/c64-workflow/SKILL.md 查看文件

@@ -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 |

+ 16
- 13
.claude/skills/vice-emulator/SKILL.md 查看文件

@@ -117,7 +117,7 @@ This enables the binary monitor protocol used by automation tools and external d
For agent-driven debugging, prefer launching VICE with:

```text
x64sc -autostart c64os.prg -autostart-warp -nativemonitor -keepmonopen -refreshonbreak
x64sc -autostart build/program.prg -autostart-warp -nativemonitor -keepmonopen -refreshonbreak
```

Useful companion options from the manual:
@@ -279,16 +279,14 @@ For C64 work:
VICE can execute a command file from inside the monitor, and can also record command sequences:

```text
playback "examples/vice-monitor-c64os.txt"
playback "examples/vice-debug-session.mon"
record "my-session.txt"
```

This is the easiest way for an agent to set up a repeatable debug session.

In this repository, prefer:

- `examples/vice-monitor-c64os.txt` for startup and banking inspection
- `examples/vice-monitor-c64os-debug.txt` for a fuller debugging session with labels, checkpoints, and keyboard injection
In this repository, `examples/vice-debug-session.mon` sets up entry, banking, and IRQ-vector
checkpoints for any program built with `c64 build`.

---

@@ -343,14 +341,19 @@ The VICE manual explicitly warns that snapshot compatibility is not reliable acr

## Recommended Repo Workflow

For this repository:
Use the repository toolchain (`.claude/skills/c64-workflow/SKILL.md`) instead of raw command lines:

1. `c64 test <project>`: headless run. Prints the decoded screen and PASS/FAIL, and leaves
`ram.bin`, `io.bin`, `screenshot.png`, and `monitor.log` in `build/test/`.
2. `c64 test <project> -Until <label>`: capture state when a routine is reached.
3. `c64 run <project> -Monitor`: interactive run with the remote monitor on port 6510 and
the build's labels loaded.
4. `c64 mon "r" "m .score .score+1"`: query the running program; it resumes afterwards.
5. `c64 mon "pb \"<abs path>/examples/vice-debug-session.mon\"" -Stay`: standard checkpoint set.
6. Use `step`, `next`, `return`, and `bt` to narrow the failing routine.

1. Build the program with `build.bat`
2. Launch `c64os.prg` with `x64sc -autostart c64os.prg -autostart-warp -nativemonitor -keepmonopen -refreshonbreak`
3. Run `playback "examples/vice-monitor-c64os-debug.txt"` for an immediate debug setup
4. Use `watch store $0001` to confirm banking writes
5. Use `step`, `next`, `return`, and `backtrace` to narrow the failing routine
6. Use snapshots for quick iteration, not long-term storage
The c64-workflow skill lists VICE automation behaviors verified in this repo (debugcart
ordering, `quit` in playback files, the quoting limits of checkpoint commands).

---



+ 0
- 14
examples/vice-autostart-c64os.bat 查看文件

@@ -1,14 +0,0 @@
@echo off
setlocal

set "VICE_EXE=C:\Program Files\GTK3VICE-3.10-win64\bin\x64sc.exe"

if not exist "%VICE_EXE%" (
echo x64sc.exe not found at "%VICE_EXE%"
exit /b 1
)

call "%~dp0..\build.bat"
if errorlevel 1 exit /b 1

"%VICE_EXE%" -autostart "%~dp0..\c64os.prg" -autostart-warp

+ 0
- 22
examples/vice-debug-c64os.bat 查看文件

@@ -1,22 +0,0 @@
@echo off
setlocal

set "VICE_EXE=C:\Program Files\GTK3VICE-3.10-win64\bin\x64sc.exe"
set "MONLOG=%~dp0..\vice-monitor.log"

if not exist "%VICE_EXE%" (
echo x64sc.exe not found at "%VICE_EXE%"
exit /b 1
)

call "%~dp0..\build.bat"
if errorlevel 1 exit /b 1

"%VICE_EXE%" ^
-autostart "%~dp0..\c64os.prg" ^
-autostart-warp ^
-nativemonitor ^
-keepmonopen ^
-refreshonbreak ^
-monlog ^
-monlogname "%MONLOG%"

+ 35
- 0
examples/vice-debug-session.mon 查看文件

@@ -0,0 +1,35 @@
; Repeatable VICE monitor session for any program built by `c64 build`.
;
; Load it after starting the program with the monitor enabled:
; c64 run projects/<name> -Monitor
; c64 mon "pb \"C:/path/to/examples/vice-debug-session.mon\"" -Stay
; or, in an interactive monitor window:
; playback "examples/vice-debug-session.mon"
;
; `c64 run -Monitor` already loaded build/<name>.lbl, so labels such as .main work.

radix H
device c:
sidefx off

; Entry of a program with a cc65 BASIC stub (10 SYS 2061). For C programs use .main
; or ._main instead.
break exec $080D

; Banking changes: show the CPU port each time it is written.
watch store $0001
command 2 "m $0000 $0001"

; IRQ vector changes (custom interrupt handlers, or corruption).
watch store $0314 $0315

; Current state.
r
m $0000 $0001

; Useful next commands once a checkpoint hits:
; step / next / return step in, step over JSR, run to RTS
; bt JSR call chain
; bank io then `m d000 d02e` for VIC-II registers, `m dc00 dc0f` for CIA 1
; watch store .score find who writes a variable
; x resume

+ 0
- 37
examples/vice-monitor-c64os-debug.txt 查看文件

@@ -1,37 +0,0 @@
; Full VICE debug session for c64os.prg
; Launch with examples/vice-debug-c64os.bat, then in the monitor run:
; playback "examples/vice-monitor-c64os-debug.txt"

radix H
device c:
sidefx off

; Give useful labels to the known startup locations.
add_label $0801 .basic_stub
add_label $0810 .start

; Break when BASIC transfers control to the machine code entry.
break exec .start

; Watch memory banking changes and dump the CPU port state when they happen.
watch store $0001
command 2 "m $0000 $0002"

; Watch IRQ vector changes in case startup or later code patches them.
watch store $0314 $0315

; Show the BASIC stub, startup code, and current CPU port state.
d .basic_stub $0840
m $0000 $0002
r

; Helpful follow-up commands once the first breakpoint triggers:
; step ; step into the next instruction
; next ; step over KERNAL calls
; return ; run until the current subroutine exits
; bt ; show JSR call chain
; io $d000 ; inspect VIC-II registers
; io $dc00 ; inspect CIA #1 registers
; keybuf "help\x0d" ; inject a HELP command into the shell

stop

+ 0
- 20
examples/vice-monitor-c64os.txt 查看文件

@@ -1,20 +0,0 @@
; VICE monitor playback file for c64os.prg
; Usage inside the VICE monitor:
; playback "examples/vice-monitor-c64os.txt"

radix H
device c:
sidefx off

; Break when the BASIC stub transfers control to the machine code entry.
break exec $0810

; Watch for writes to the 6510 CPU port so banking changes are visible.
watch store $0001

; Show the BASIC stub, start of code, and current banking registers.
d $0801 $0840
m $0000 $0002

; Stop playback so the user or agent can resume with X when ready.
stop

+ 11
- 15
reference/vice-usage.md 查看文件

@@ -59,10 +59,11 @@ Notes:
| `-binarymonitor` | Enable the binary monitor protocol server |
| `-binarymonitoraddress ip4://127.0.0.1:6502` | Bind address for the binary monitor server |

Recommended debug launch for this repo:
Recommended debug launch for this repo: `c64 run <project> -Monitor` (remote monitor on
port 6510 with labels loaded). The raw equivalent:

```text
"C:\Program Files\GTK3VICE-3.10-win64\bin\x64sc.exe" -autostart c64os.prg -autostart-warp -nativemonitor -keepmonopen -refreshonbreak
x64sc -autostart build/program.prg -autostart-warp -remotemonitor -moncommands build/program.lbl
```

---
@@ -103,7 +104,7 @@ d $0810 $0840
m $0000 $0002
bt
return
playback "examples/vice-monitor-c64os.txt"
playback "examples/vice-debug-session.mon"
```

Conditional checkpoints support register expressions, raster line `RL`, and cycle `CY`.
@@ -165,21 +166,16 @@ Important manual warning:

## Repo-Focused Launch Examples

Build and run the current program:

```text
build.bat
"C:\Program Files\GTK3VICE-3.10-win64\bin\x64sc.exe" -autostart c64os.prg -autostart-warp
```

Open with terminal monitor ready for debugging:

```text
"C:\Program Files\GTK3VICE-3.10-win64\bin\x64sc.exe" -autostart c64os.prg -autostart-warp -nativemonitor -keepmonopen -refreshonbreak
c64 build projects/<name> # build/<name>.prg + .lbl + .map
c64 run projects/<name> # autostart (PRG injected)
c64 run projects/<name> -FromDisk # autostart build/<name>.d64 instead
c64 run projects/<name> -Monitor # remote monitor + labels
c64 test projects/<name> # headless, prints the screen, exit code 0/1
```

Open with monitor logging enabled:
Raw VICE, with monitor logging:

```text
"C:\Program Files\GTK3VICE-3.10-win64\bin\x64sc.exe" -autostart c64os.prg -autostart-warp -nativemonitor -keepmonopen -refreshonbreak -monlog -monlogname vice-monitor.log
x64sc -autostart build/program.prg -autostart-warp -nativemonitor -keepmonopen -refreshonbreak -monlog -monlogname vice-monitor.log
```

正在加载...
取消
保存

Powered by TurnKey Linux.