@henols/c64-re-tools 2.2.1 → 2.3.0-rc.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
 
6
6
  Let an AI agent work on Commodore 64 programs. The agent drives a real VICE
7
- emulator and runs ACME, DXA and Ghidra. Use it to build new C64 apps and games,
7
+ emulator and runs ACME, DXA, Ghidra and Exomizer. Use it to build new C64 apps and games,
8
8
  and to reverse engineer old ones.
9
9
 
10
10
  It works with Claude Code, Codex, pi, opencode, Gemini, Copilot, Cursor and
@@ -17,6 +17,7 @@ Ask for what you want in plain words. The agent picks the skill for each step.
17
17
  **Build new apps and games**
18
18
 
19
19
  - Assemble 6510 source with ACME, with errors by file and line, and symbols.
20
+ - Pack and crunch a program with Exomizer into one PRG that unpacks itself, checked byte for byte before it is written.
20
21
  - Load and run the program in VICE. Set breakpoints and watchpoints, step, and read the CPU history and the call stack.
21
22
  - Read, write, search and compare memory, and disassemble it.
22
23
  - Show the VIC-II, sprite, SID and CIA state, and capture the screen.
@@ -42,7 +43,7 @@ Ask for what you want in plain words. The agent picks the skill for each step.
42
43
 
43
44
  VICE and the Host Runtime run on your computer, where VICE can open a window.
44
45
  The agent runs in a dev container in your project. It reaches VICE through an
45
- MCP server, and it runs ACME, DXA, Ghidra and c64run through its skills.
46
+ MCP server, and it runs ACME, DXA, Ghidra, Exomizer and c64run through its skills.
46
47
 
47
48
  ## Install
48
49
 
@@ -72,7 +73,7 @@ and the address of the Host Runtime. If the project already has a
72
73
 
73
74
  Open the project in the container, for example with "Reopen in Container" in
74
75
  VS Code, or with `npx -y @devcontainers/cli up --workspace-folder .`. The
75
- Feature gives the container ACME, dxa, Java 21, Ghidra and c64run. At each
76
+ Feature gives the container ACME, dxa, Exomizer, Java 21, Ghidra and c64run. At each
76
77
  start it installs the newest toolkit of its channel into the project, with the
77
78
  tools of that version. It reads its options only when the container is built:
78
79
  rebuild after a change.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@henols/c64-re-tools",
3
- "version": "2.2.1",
3
+ "version": "2.3.0-rc.1",
4
4
  "description": "Agent-friendly Commodore 64 engineering toolkit for reverse engineering, debugging, development and testing.",
5
5
  "license": "MIT",
6
6
  "author": "Henrik Olsson",
@@ -1,6 +1,6 @@
1
1
  ---
2
2
  name: c64-assembler
3
- description: Use this skill to assemble C64 6510 source code with the ACME assembler and write the program (PRG) into the project. It reports source errors with file and line, and the symbols with their addresses or values. Use it to build a rebuilt or new program. Do not use it to run the program. Give the PRG to the c64-emulator skill for that.
3
+ description: Use this skill to assemble C64 6510 source code with the ACME assembler and write the program (PRG) into the project. It reports source errors with file and line, and the symbols with their addresses or values. Use it to build a rebuilt or new program. Do not use it to run the program. Give the PRG to the emulator skill for that.
4
4
  ---
5
5
 
6
6
  # c64-assembler
@@ -73,7 +73,7 @@ The target CPU is the 6510. The output is a C64 PRG, or the bytes only with `--p
73
73
  - Kind `constant` has a value outside `loadRange`. It gives the `value` as `$` and four or more hex digits. A label in a `!pseudopc` block can be a constant, because its value is the run address, not the load address.
74
74
  - `used` is `false` when the source does not use the symbol.
75
75
  4. If `result` is `FAIL` and `assembled` is `false`, read `diagnostics`. Each one has a severity, a file, a line and a message. Correct the source and run the script again.
76
- 5. To run the program, give the PRG path to the c64-emulator skill.
76
+ 5. To run the program, give the PRG path to the emulator skill.
77
77
 
78
78
  The source of the c64-source skill has switches in `config.asm`. Without an option, the build uses the values in `config.asm`. With the defaults, this is the clean program without copy protection, trainers, cheats and crack changes, and with all corrections. This build can differ from the program in the knowledge, so `--compare` can give `FAIL`. Use `--release <label> --compare` to check the build of a release. To put in the patches of the switch `TRAINER`, set `TRAINER = 1` in `config.asm`. Do not use `--define` for a switch: ACME stops with `Symbol already defined`.
79
79
 
@@ -109,6 +109,7 @@ With `--compare`, `compare` gives `identical` and `segments`: one check for each
109
109
 
110
110
  ## Handoffs
111
111
 
112
- - Run or debug the program → c64-emulator
112
+ - Run or debug the program → emulator
113
113
  - Compare the rebuild with the original → c64-testing
114
+ - Pack the program into one small PRG that unpacks itself → c64-packer
114
115
  - Write a new program, or add a feature or a fix → c64-development
@@ -16,7 +16,7 @@ import { assemble, showSymbol, symbolsFileText } from "#src/native/acme.ts";
16
16
  import { localToolContext } from "#src/native/local.ts";
17
17
  import type { AcmeParams, AssembledSymbol } from "#src/native/types.ts";
18
18
  import { listed as listedItems, OPTIONS, runScript, UsageError } from "#src/script.ts";
19
- import { compareBuild, type Reference } from "./lib/compare.ts";
19
+ import { compareBuild, type Reference } from "#src/compare.ts";
20
20
 
21
21
  function parseDefine(text: string): [string, number | boolean] {
22
22
  const match = /^([A-Za-z_][A-Za-z0-9_]*)=(.+)$/.exec(text);
@@ -1,6 +1,6 @@
1
1
  ---
2
2
  name: c64-basic
3
- description: Use this skill to decode a C64 BASIC V2 program (PRG) into a readable listing. It also finds where the BASIC gives control to machine code with SYS or USR. Use it on the start file of a game or of a demo. Do not use it on machine code or to run the program. Give the handoff address to the c64-static-analysis or c64-emulator skill.
3
+ description: Use this skill to decode a C64 BASIC V2 program (PRG) into a readable listing. It also finds where the BASIC gives control to machine code with SYS or USR. Use it on the start file of a game or of a demo. Do not use it on machine code or to run the program. Give the handoff address to the c64-static-analysis or emulator skill.
4
4
  ---
5
5
 
6
6
  # c64-basic
@@ -51,7 +51,7 @@ node <skill>/scripts/basic.ts list extracted/game.prg
51
51
  2. Read `listing`. Each line starts with its BASIC line number. Keywords show in lower case. Control characters in strings show in braces, for example `{clr}`.
52
52
  3. Read `handoffs`. Each handoff has a `kind` (`sys` or `usr`) and the BASIC `line`.
53
53
  - A `sys` handoff with an `address`: BASIC calls the machine code at that address. The script calculates a SYS argument with numbers, π, `+`, `-`, `*`, `/` and brackets. For example, `SYS π*656` calls `$080c`.
54
- - A handoff with `computed: true`: the address comes from a calculation at run time, for example with `PEEK`. A `sys` handoff gives its argument in `expression`. Use the c64-emulator skill to find the address.
54
+ - A handoff with `computed: true`: the address comes from a calculation at run time, for example with `PEEK`. A `sys` handoff gives its argument in `expression`. Use the emulator skill to find the address.
55
55
  4. Compare the address with `loadRange` and `basicEnd`. From `basicEnd` to the end of `loadRange`, the file holds bytes that are not BASIC. Usually this is the machine code that the handoff calls. Some programs have no end marker (two zero bytes) after the last line. The machine code starts directly after the last line. Then `basicEnd` is the first byte of that machine code.
56
56
 
57
57
  The address ranges are for a load to the load address of the file. A load without `,1` puts a BASIC program at `$0801`. Most loaders have `$0801` as their load address.
@@ -80,5 +80,5 @@ The listing and the handoff addresses.
80
80
  ## Handoffs
81
81
 
82
82
  - Machine code at the handoff address → c64-static-analysis
83
- - Run the program to the handoff → c64-emulator
83
+ - Run the program to the handoff → emulator
84
84
  - Record the entry point → c64-knowledge
@@ -24,7 +24,7 @@ This skill uses the other skills. It does not use the host runtime or the emulat
24
24
  | Work | Skill |
25
25
  |---|---|
26
26
  | Assemble the source to a PRG, with errors and symbols | c64-assembler |
27
- | Run, look, debug and measure | c64-emulator |
27
+ | Run, look, debug and measure | emulator |
28
28
  | Tests with expected values (`expect` steps in a run), and original against rebuild | c64-testing |
29
29
  | The meaning of an address or a register, and an include of the platform names | c64-memory-map |
30
30
  | Patches and switches of a rebuilt program, names and comments | c64-knowledge |
@@ -88,11 +88,11 @@ Do these steps for each small part of the program:
88
88
  1. Find the meaning of each register with c64-memory-map. Make an include of the platform names with `memory-map.ts export --out src/hw.a`. Include it with `!source`. Do not type the addresses of registers yourself.
89
89
  2. Write the code for one part.
90
90
  3. Build it with c64-assembler: `assembler.ts build src main.a --out build/<name>.prg --out-symbols build/<name>.json`. Change the source at the file and the line of each error.
91
- 4. Start it with `c64_autostart` of c64-emulator. Look at the screen with `c64_screen`. Read the screen memory, not only the image.
91
+ 4. Start it with `c64_autostart` of the emulator skill. Look at the screen with `c64_screen`. Read the screen memory, not only the image.
92
92
  5. Write the `expect` steps of this part in a run file. Give `"symbols": "build/<name>.json"`, so that a step can use the labels of the source.
93
93
  6. Run all run files in `tests/` with `testing.ts run`. Go to the next part only when each run gives PASS.
94
94
 
95
- To find a defect, use c64-emulator:
95
+ To find a defect, use the emulator skill:
96
96
 
97
97
  - Stop at the code with `c64_breakpoint`, or at a write to a variable with `c64_watchpoint`.
98
98
  - Get to a state with `c64_run_until`.
@@ -101,13 +101,13 @@ To find a defect, use c64-emulator:
101
101
 
102
102
  Find the cause before you change the code. Do not change code at random until a test passes.
103
103
 
104
- When the user must play or look at the program, use the window procedure of c64-emulator. Open the window, tell the user what to do, wait, and close the window.
104
+ When the user must play or look at the program, use the window procedure of the emulator skill.
105
105
 
106
106
  ### B. A feature or a fix in source that a person wrote
107
107
 
108
108
  1. Write a run file with an `expect` step that shows the defect or the missing feature.
109
109
  2. Run it with `testing.ts run`. Make sure that it gives FAIL. A test that gives PASS before the change does not test the change.
110
- 3. For a defect, find the cause with c64-emulator.
110
+ 3. For a defect, find the cause with the emulator skill.
111
111
  4. Change the source. Build it with c64-assembler.
112
112
  5. Run the new run file and all other run files in `tests/`. Each run must give PASS.
113
113
 
@@ -120,7 +120,7 @@ A rebuilt program gets its source from the knowledge. A change is a switch and i
120
120
  - A fix: `fix`, for example `FIX_FLICKER`. The default build has it.
121
121
  - A trainer or a cheat: `trainer` or `cheat`. The default build does not have it.
122
122
  - Another feature: `change`, for example `HISCORE`. Ask the user if the default build must have it. Give `--default 1` or `--default 0`.
123
- 3. Find the code to change with c64-knowledge (`at`, `regions`) and c64-emulator.
123
+ 3. Find the code to change with c64-knowledge (`at`, `regions`) and the emulator skill.
124
124
  4. Write the code in a file in `.c64-re-tools/local/`. Write the patch with `knowledge.ts patch '<range>' <SWITCH> --code-file <file>` of c64-knowledge. The code must fit in the range.
125
125
  5. If the code does not fit, write a short patch with a `jsr` to the new code. Put the new code in a second patch of the same switch, in a range that the program does not use. The range must have evidence: `provenance.ts leftovers` of c64-provenance, or code that no run executed and that the program cannot reach. If no range has this evidence, stop and ask the user.
126
126
  6. Make the source again with `source.ts generate --out src` of c64-source. The checks `check`, `switches`, `default` and `releases` must pass. A switch must not give `overflow` or `does not assemble`.
@@ -162,14 +162,14 @@ A program that builds, with a run file in `tests/` for each acceptance check, an
162
162
  - A skill reports `installation-incomplete`, or `machine-unavailable` with a message that the host runtime does not run: stop the whole task. Give the message to the user and wait for the user. Do not continue with other skills. Do not make a replacement for the tool or the host runtime, for example your own assembler or emulator script.
163
163
  - A tool gives a wrong result: tell the user the defect and the evidence, and wait for the user. Do not make a workaround.
164
164
  - A test gives FAIL: change the program until the test gives PASS. Do not change the expected value to make the test pass. Change an expected value only when the user changes the acceptance check.
165
- - A run gives FAIL because a `runUntil` did not get to its target: the program did not get to that state. Find why with c64-emulator.
166
- - The program reads the keyboard hardware, and a key does not arrive: read the keyboard text of c64-emulator. Do not change the program to make it accept a key.
165
+ - A run gives FAIL because a `runUntil` did not get to its target: the program did not get to that state. Find why with the emulator skill.
166
+ - The program reads the keyboard hardware, and a key does not arrive: read the description of `c64_keyboard`. Do not change the program to make it accept a key.
167
167
  - A patch does not fit, and no unused range has evidence: stop and ask the user.
168
168
 
169
169
  ## Handoffs
170
170
 
171
171
  - Assemble → c64-assembler
172
- - Run, debug and measure → c64-emulator
172
+ - Run, debug and measure → emulator
173
173
  - Tests with expected values, and original against rebuild → c64-testing
174
174
  - The meaning of an address or a register → c64-memory-map
175
175
  - Patches, names and comments → c64-knowledge
@@ -87,7 +87,7 @@ The script reads T64 and CRT files itself. The host runtime is not necessary for
87
87
  - T64: `directory` and `read`. Many T64 files have a wrong end address. Then the entry has `fixedSize: true`, and the size comes from the position of the next file.
88
88
  - CRT: `directory` and `crt`. The script reads hardware types 0 (normal 8K, 16K or Ultimax), 5 (Ocean), 19 (Magic Desk) and 32 (EasyFlash). It refuses other types by name. `crt` writes `crt.json` and one PRG for each CHIP packet, for example `bank3-8000.prg`.
89
89
  - A cartridge bank uses the same addresses as other banks. Analyze each bank PRG with c64-static-analysis and `--space bank3`, and record its knowledge in space `bank3`.
90
- - TAP: a TAP file holds tape signals, and turbo loaders have their own formats. The script does not read it. Start it with the c64-emulator skill, or capture its parts with `loads` of the c64-unpacker skill.
90
+ - TAP: a TAP file holds tape signals, and turbo loaders have their own formats. The script does not read it. Start it with the emulator skill, or capture its parts with `loads` of the c64-unpacker skill.
91
91
 
92
92
  ## Result
93
93
 
@@ -110,4 +110,4 @@ A file that is not `closed` was not written fully. Its data can be incomplete.
110
110
  - Packed program → c64-unpacker
111
111
  - Machine code, also a cartridge bank → c64-static-analysis
112
112
  - A TAP file → c64-unpacker
113
- - Run the program → c64-emulator
113
+ - Run the program → emulator
@@ -71,7 +71,7 @@ node <skill>/scripts/docs.ts link README.md docs --source src --check
71
71
  3. Run `docs.ts generate`. Read `index.md` of the reference. A module with no routines, or a routine with no comment, shows what the knowledge does not have yet.
72
72
  4. Make the plan of the pages. Start from the modules and the call graphs. Find the main loop, the interrupt chain, the dispatch tables, the state machines and the data formats. Each subsystem gets one page in `docs/`. The entry page `README.md` in the project folder lists the pages. Use the page types in `references/page-types.md`.
73
73
  5. Collect the facts about the development and the copy protection. These facts are the most interesting parts for a reader, so look for them carefully. Read the leftover spaces and their comments in the knowledge. If the knowledge has none, run `provenance.ts leftovers` of c64-provenance. For two or more releases, run `provenance.ts compare` of c64-provenance to separate the program from the changes of each release. Find each check of the copy protection in the code, and the effect of a failed check. These facts go into the development section, the copy protection section and their pages. Do not examine the cracks in detail. They are not part of the program.
74
- 6. Write each page. Read the code and the knowledge for each fact. Run the program with c64-testing or c64-emulator when a fact needs a run. Explain each part before you name the code. Link only the main parts: write `` `draw_room` `$09BC` `` for a link, and `` `draw_room` `` for each other mention. Mark each fact with its evidence. Several subagents can write the pages at the same time, one page each. Give each subagent `references/page-types.md` and `references/style.md`.
74
+ 6. Write each page. Read the code and the knowledge for each fact. Run the program with c64-testing or the emulator skill when a fact needs a run. Explain each part before you name the code. Link only the main parts: write `` `draw_room` `$09BC` `` for a link, and `` `draw_room` `` for each other mention. Mark each fact with its evidence. Several subagents can write the pages at the same time, one page each. Give each subagent `references/page-types.md` and `references/style.md`.
75
75
  7. Record each new fact in the knowledge first: a name, a comment, a region or a module. Then the source and the reference show it too. A fact only in a page is lost at the next change of the knowledge.
76
76
  8. Run `docs.ts link`. Read `problems` and correct each one. Then run `docs.ts link --check`. Make sure that `result` is `PASS`.
77
77
  9. When the project folder is in a workspace with other projects, find `README.md` at the root of the workspace. This page lists each program of the workspace. Add one row for the program, with a link to `<project>/README.md` and one sentence. If the page does not exist, make it. Use the workspace index in `references/page-types.md`. `docs.ts link` does not read this page, because the page is not in the project folder.
@@ -156,6 +156,6 @@ Do not store:
156
156
 
157
157
  - Names, comments, regions and modules → c64-knowledge
158
158
  - The source and its source map → c64-source
159
- - A fact that needs a run → c64-testing or c64-emulator
159
+ - A fact that needs a run → c64-testing or emulator
160
160
  - Leftovers, traces of the development and the differences of releases → c64-provenance
161
161
  - What an address of the C64 is → c64-memory-map
@@ -357,7 +357,7 @@ A revert makes a new revision. The history stays complete.
357
357
 
358
358
  ## Handoffs
359
359
 
360
- - Runtime evidence → c64-emulator
360
+ - Runtime evidence → emulator
361
361
  - Static evidence → c64-static-analysis
362
362
  - Source from the knowledge → c64-source
363
363
  - Names of routines that other programs share → c64-provenance
@@ -65,7 +65,7 @@ Write a value for `decode` and `--bank` as `$` and hex digits, as a decimal numb
65
65
  1. Run `at` for the addresses in the code or the question.
66
66
  2. Read `kind`:
67
67
  - `platform`: the C64 gives the address a meaning. Read `name` and `meaning`.
68
- - `application`: the address has no platform meaning. The program decides its use. Find its use with the c64-static-analysis or c64-emulator skill.
68
+ - `application`: the address has no platform meaning. The program decides its use. Find its use with the c64-static-analysis or emulator skill.
69
69
  3. For an I/O register, `mirrorOf` shows the register that a mirror address repeats. `visible` tells that an address in `$D000`-`$DFFF` is visible only when `$01` shows I/O at `$D000`. This area holds the chip registers, the color RAM and the cartridge I/O areas.
70
70
  4. For a byte in a multi-byte entry, for example the high byte of a vector, `entryStart` gives the first address of the entry.
71
71
  5. To understand a value in a register, run `decode`. For the memory configuration, decode the value of `$01`.
@@ -95,5 +95,5 @@ Some registers have a different meaning for a read and for a write, for example
95
95
 
96
96
  ## Handoffs
97
97
 
98
- - The value of an address at run time → c64-emulator
98
+ - The value of an address at run time → emulator
99
99
  - A conclusion about the application → c64-knowledge
@@ -0,0 +1,109 @@
1
+ ---
2
+ name: c64-packer
3
+ description: Use this skill to pack and crunch a C64 program with Exomizer into one PRG that unpacks itself and starts with RUN. It can merge more PRG files into one program. It unpacks the result in an emulator and checks each byte and the entry before it writes the PRG. Use it to make a small release file of a rebuilt or new program. Do not use it to unpack a packed program. Use the c64-unpacker skill for that.
4
+ ---
5
+
6
+ # c64-packer
7
+
8
+ ## Purpose
9
+
10
+ Pack and crunch PRG files of the project with Exomizer into one PRG. The PRG loads at $0801, starts with RUN, unpacks the program and jumps to its entry. Check the result before you use it.
11
+
12
+ This skill does not assemble the program, does not run it in VICE and does not change project knowledge.
13
+
14
+ ## Inputs
15
+
16
+ - One or more PRG files in the project, in load order. Where two files overlap, the later file wins.
17
+ - The output path: where to write the packed PRG, relative to the project folder.
18
+ - The entry: the address where the program starts after the unpack. If the first PRG is a BASIC program at $0801 with `SYS` and a number, the script uses that address.
19
+ - Optional: the value of $01 when the program starts.
20
+
21
+ ## Tools / execution path
22
+
23
+ The script runs Exomizer on this machine. Exomizer packs the files with `sfx`. Then the script unpacks the result with `desfx` in the 6502 emulator of Exomizer. It compares the unpacked bytes with the bytes of the inputs, and the jump address with the entry. The host runtime is not necessary.
24
+
25
+ ```
26
+ node <skill>/scripts/packer.ts pack build/game.prg --out build/game-packed.prg
27
+ node <skill>/scripts/packer.ts pack build/loader.prg build/main.prg --entry '$0400' --out build/release.prg
28
+ ```
29
+
30
+ | Option | Meaning |
31
+ |---|---|
32
+ | `--entry '$0400'` | The address where the program starts after the unpack. It must be `$0400` or higher. A name of the knowledge is also correct. Without it, the script uses the `SYS` address of the BASIC line of the first input. |
33
+ | `--exit-memory '$35'` | The value of $01 when the program starts, `$34` to `$37`. Without it, the value is `$37`: BASIC, KERNAL and I/O. With it, the program starts with interrupts off. |
34
+
35
+ ### Calling the script
36
+
37
+ - Call a script as `node <skill>/scripts/<script>.ts <command> [subjects] [options]`. The command comes first.
38
+ - `<script>.ts --help` lists the commands. `<script>.ts <command> --help` gives the subjects and the options of one command. The help is JSON: `{"text": "..."}`.
39
+ - An address is `$` and 1 to 4 hex digits, for example `'$c000'` or `'$fb'`, or a name. A name is a symbol, else an analyzer name that only one analyzer gives. Quote an address in a shell.
40
+ - A range is `<start>-<end>`, for example `'$c000-$cfff'`. In a JSON file, a range is `start` and `end`.
41
+ - Bytes in a JSON file are hex in a field `data`, for example `"data": "a900"`.
42
+ - The script examines each `--out` path before it starts work. It refuses a path in `.c64-re-tools/knowledge/` and the index of the knowledge, because only c64-knowledge writes them. It refuses a folder where the command writes a file, and a file where the command writes a folder.
43
+ - The result is one JSON object on one line. A file that the script writes shows as `{"out", "bytes", "sha256"}`. A list shows as `{"count", "listed"}`, and `rest` tells where to get the rest of a long list.
44
+ - An error is `{"error": {"code": "...", "message": "..."}}`. The message tells what is wrong and what to do.
45
+ - The exit status is 0 for done or PASS, 1 for FAIL, 2 for an error and 3 for INCONCLUSIVE.
46
+
47
+ ### Project folder
48
+
49
+ - Paths are relative to the project folder. The project folder is the nearest folder at or above the working directory that holds `.c64-re-tools/`.
50
+ - If the working directory is not in a project and has projects below it, the script refuses and lists them. Then give `--project <folder>` to each script.
51
+ - Write each file that you make for your work in `.c64-re-tools/local/` of the project. Git ignores that folder. Examples are results, batches, step files, runs, snapshots and memory dumps.
52
+ - Move a file out of `.c64-re-tools/local/` only when it is the evidence for a finding. Move it before you give it to `--evidence test:<file>` or `--evidence file:<file>` of c64-knowledge. These two refuse a file in `.c64-re-tools/local/`.
53
+ - Do not move a coverage map after `coverage` of c64-knowledge imported it. `--evidence coverage:<map>` names the map by its path at the import. The knowledge keeps the coverage and the SHA-256 of the map, so the evidence stays valid.
54
+ - Put a run that is evidence in `runs/<name>/`, with its run file, its results and the snapshot files that it uses. Change the paths in the run file to the new paths. Then git keeps them, and another person can do the run again.
55
+ - A shell redirect (`>`) is relative to the working directory, not to the project folder. From the folder above the project, write `<folder>/.c64-re-tools/local/result.json`.
56
+
57
+ ## Workflow
58
+
59
+ 1. Assemble the program with the c64-assembler skill. Use the release or the switches that you want in the packed file.
60
+ 2. Find the entry. For a BASIC program at $0801 with `SYS`, the script finds it. Else, use the start address of the program, for example the entry of the program in the knowledge.
61
+ 3. Find the value of $01 that the program needs at its start. Give `--exit-memory` when the program uses RAM under BASIC or KERNAL and does not set $01 itself.
62
+ 4. Run the script.
63
+ 5. If `result` is `PASS`, the packed PRG is at `out`. Give it to the emulator skill and run it with RUN. Make sure that the program starts and works.
64
+ 6. If `result` is `FAIL`, read `check`. Do not use the packed program. The script does not write it.
65
+ 7. If `result` is `INCONCLUSIVE`, the check did not hold all bytes of the inputs. Run the packed program in the emulator and examine the memory of the segments that are `not covered`.
66
+
67
+ The packed program starts the unpacker with `SYS 2061`, at $080d. A BASIC program at $0801 often has its entry at $080d too. Then the CPU gets to $080d two times: first before the unpack, then at the start of the program. To examine the program at its entry in the emulator, stop at $080d, run one frame, and stop at $080d again.
68
+
69
+ The unpacker uses $0334 to $03ff and the zero page while it runs. A program that loads into these addresses can fail. Then Exomizer stops with an error, or the check gives `FAIL`.
70
+
71
+ ## Knowledge
72
+
73
+ Automatic write:
74
+ - none.
75
+
76
+ Do not store:
77
+ - the packed program
78
+ - the check result
79
+
80
+ ## Result
81
+
82
+ `result` is `PASS`, `FAIL` or `INCONCLUSIVE`.
83
+
84
+ - `PASS`: the packed program unpacks to the bytes of all inputs and jumps to the entry. The PRG is at `out`.
85
+ - `FAIL`: the packed program unpacks to different bytes, or jumps to a different address. The script did not write a file.
86
+ - `INCONCLUSIVE`: the emulator of Exomizer keeps only the largest range that the unpacker wrote. Bytes of the inputs outside that range have no check. This occurs when the inputs have gaps between them. The PRG is at `out`.
87
+
88
+ Other fields:
89
+ - `packedBytes` and `unpackedBytes`: the size of the packed PRG and of the bytes of the inputs. `percent` is the packed size as a percentage of the unpacked size.
90
+ - `unpackSeconds`: the time of the unpack in the emulator of Exomizer, in seconds at the PAL clock. A real C64 can need more time.
91
+ - `inputs`: each input with its `loadRange` and its `bytes`.
92
+ - `check.entry`: the address where the unpacker jumped.
93
+ - `check.segments`: one check for each segment of the merged inputs. The `result` of a segment is `identical`, `different` with `firstDifference`, or `not covered`. `firstDifference` gives the `address`, the unpacked byte (`built`) and the input byte (`reference`).
94
+
95
+ ## Failure and conflicts
96
+
97
+ - `FAIL`: do not use the packed program. Read `check`. Give the call and the result to the user.
98
+ - `invalid-input` with `Give --entry`: the first input has no BASIC line with `SYS` and a number. Give `--entry`.
99
+ - `invalid-input` for the entry: the entry is below $0400. The unpacker runs there. A program with an entry below $0400 needs a different packer.
100
+ - `operation-failed` with `Exomizer could not pack the program`: read the last output of Exomizer in the message. Correct the inputs, or give the message to the user.
101
+ - `installation-incomplete`, or `machine-unavailable` with a message that the host runtime does not run: stop the task. Give the message to the user and wait for the user. The message gives the remedy. Do not make a replacement for the tool or the host runtime, for example your own disk parser, disassembler or emulator script.
102
+ - `not-found`: there is no file at an input path. Give a path relative to the project folder.
103
+
104
+ ## Handoffs
105
+
106
+ - Assemble the program before the pack → c64-assembler
107
+ - Run the packed program → emulator
108
+ - Put the packed program on a disk image → c64-disk
109
+ - Unpack a packed program → c64-unpacker
@@ -0,0 +1,113 @@
1
+ // The c64-packer script: packs and crunches PRG files of the project into one
2
+ // C64 program that unpacks itself, with Exomizer, which it runs itself. It
3
+ // unpacks the result again in the emulator of Exomizer and checks the bytes
4
+ // and the entry before it writes the program.
5
+
6
+ import { posix } from "node:path";
7
+
8
+ import { findHandoffs, formatC64Address, parseTokenized, prgRange } from "#src/c64.ts";
9
+ import { compareBuild, type Reference } from "#src/compare.ts";
10
+ import { isRefusal, Refusal } from "#src/errors.ts";
11
+ import { readProjectFile, writeReported } from "#src/host-client/transfer.ts";
12
+ import { knowledgeNames } from "#src/knowledge/names.ts";
13
+ import { pack } from "#src/native/exomizer.ts";
14
+ import { localToolContext } from "#src/native/local.ts";
15
+ import { OPTIONS, runScript, UsageError } from "#src/script.ts";
16
+
17
+ /** CPU cycles in one second of a PAL C64. */
18
+ const PAL_CYCLES_PER_SECOND = 985_248;
19
+
20
+ /** The SYS address of the BASIC line of a PRG at $0801, or the reason that there is none. */
21
+ function sysEntry(program: Uint8Array): number | string {
22
+ const load = prgRange(program).start;
23
+ if (load !== 0x0801) return `It loads at ${formatC64Address(load)}, not at $0801 where BASIC starts.`;
24
+ const tokenized = parseTokenized(program);
25
+ if ("reason" in tokenized) return tokenized.reason;
26
+ const sys = findHandoffs(tokenized.lines).find((handoff) => handoff.kind === "sys");
27
+ if (sys === undefined) return "Its BASIC program has no SYS.";
28
+ if (!("address" in sys)) return `Its SYS in line ${sys.line} has the argument ${sys.expression}, which needs run-time values.`;
29
+ return sys.address;
30
+ }
31
+
32
+ /** The inputs merged in load order, as the reference of the check: a later file wins where two overlap. */
33
+ function merged(programs: ReadonlyArray<{ start: number; body: Uint8Array }>): Reference {
34
+ const memory = new Uint8Array(0x10000);
35
+ const loaded = new Uint8Array(0x10000);
36
+ for (const { start, body } of programs) {
37
+ memory.set(body, start);
38
+ loaded.fill(1, start, start + body.length);
39
+ }
40
+ const segments: Array<{ start: number; end: number }> = [];
41
+ for (let address = 0; address < 0x10000; address++) {
42
+ if (loaded[address] === 0) continue;
43
+ const last = segments.at(-1);
44
+ if (last !== undefined && last.end === address - 1) last.end = address;
45
+ else segments.push({ start: address, end: address });
46
+ }
47
+ return { segments, byteAt: (address) => memory[address]! };
48
+ }
49
+
50
+ const samePath = (a: string, b: string) => posix.normalize(a.replaceAll("\\", "/")) === posix.normalize(b.replaceAll("\\", "/"));
51
+
52
+ await runScript({
53
+ name: "packer",
54
+ summary: "Packs and crunches PRG files of the project into one C64 program that unpacks itself, with Exomizer, and checks that it unpacks to the same bytes.",
55
+ names: knowledgeNames,
56
+ commands: [
57
+ {
58
+ name: "pack",
59
+ summary:
60
+ "Packs the PRG files into one program that unpacks itself and starts at the entry, and checks it in the emulator of Exomizer. result is PASS when it unpacks to the bytes of the inputs and jumps to the entry, FAIL when it does not (no program is written), and INCONCLUSIVE when the check does not hold all bytes of the inputs.",
61
+ subjects: [{ name: "<input.prg>", help: "a PRG in the project. Give more files to put them into one program: in load order, a later file wins where two overlap", list: true }],
62
+ options: {
63
+ out: { ...OPTIONS.out, output: "file", value: "<file.prg>", help: "where to write the packed program, relative to the project folder", required: true },
64
+ entry: { ...OPTIONS.entry, help: "where the program starts after the unpack, $0400 or above. Without it, the SYS address of the BASIC line of the first input" },
65
+ "exit-memory": { kind: "number", value: "<value>", min: 0x34, max: 0x37, help: "the value of $01 when the program starts, $34 to $37, for example '$35'. Without it, $37: BASIC, KERNAL and I/O. With it, the program starts with interrupts off" },
66
+ },
67
+ notes: "The packed program loads at $0801 and starts with RUN. The unpacker uses $0334 to $03ff and the zero page while it runs.",
68
+ run: async (call) => {
69
+ const files = call.rest(0);
70
+ const out = call.string("out")!;
71
+ const same = files.find((file) => samePath(file, out));
72
+ if (same !== undefined) throw new UsageError(`--out ${out} is the input ${same}. Write the packed program to a different file.`);
73
+ const programs = files.map((file) => {
74
+ const { bytes } = readProjectFile(file, { what: file });
75
+ try {
76
+ return { file, bytes, ...prgRange(bytes) };
77
+ } catch (error) {
78
+ if (isRefusal(error)) throw new Refusal("invalid-input", `${file} is not a PRG: ${error.message}`);
79
+ throw error;
80
+ }
81
+ });
82
+ let entry = call.address("entry");
83
+ if (entry === undefined) {
84
+ const found = sysEntry(programs[0]!.bytes);
85
+ if (typeof found === "string") throw new UsageError(`Give --entry: there is no SYS address in ${files[0]}. ${found}`);
86
+ entry = found;
87
+ }
88
+ const exitMemory = call.count("exit-memory");
89
+ const packed = await pack({ programs: programs.map((program) => program.bytes), entry, ...(exitMemory === undefined ? {} : { exitMemory }) }, localToolContext());
90
+
91
+ const reference = merged(programs);
92
+ const check = compareBuild(packed.unpacked, reference);
93
+ const entryReached = packed.entry === entry;
94
+ const different = !entryReached || check.segments.some((segment) => segment.result === "different");
95
+ const result = different ? "FAIL" : check.identical ? "PASS" : "INCONCLUSIVE";
96
+ const unpackedBytes = reference.segments.reduce((sum, segment) => sum + segment.end - segment.start + 1, 0);
97
+ return {
98
+ result,
99
+ // A program that does not unpack to its inputs is not written.
100
+ ...(different ? {} : writeReported(out, packed.program, { what: "--out" })),
101
+ packedBytes: packed.program.length,
102
+ unpackedBytes,
103
+ percent: Math.round((packed.program.length / unpackedBytes) * 1000) / 10,
104
+ entry: formatC64Address(entry),
105
+ ...(exitMemory === undefined ? {} : { exitMemory: `$${exitMemory.toString(16)}` }),
106
+ ...(packed.cycles === undefined ? {} : { unpackSeconds: Math.round((packed.cycles / PAL_CYCLES_PER_SECOND) * 10) / 10 }),
107
+ inputs: programs.map((program) => ({ file: program.file, loadRange: { start: formatC64Address(program.start), end: formatC64Address(program.end) }, bytes: program.body.length })),
108
+ check: { entry: formatC64Address(packed.entry), ...check },
109
+ };
110
+ },
111
+ },
112
+ ],
113
+ });
@@ -85,7 +85,7 @@ With `--frames`, a PRG starts with autostart and RUN. An image starts from its b
85
85
  - A range with `none` in each release is not used in this run. Run more frames or other steps before you decide.
86
86
  - `run.skipped`: the releases have no differences, so no release ran.
87
87
  5. Examine each important difference:
88
- - Disassemble the bytes of each release with the c64-emulator skill, or analyze them with the c64-static-analysis skill.
88
+ - Disassemble the bytes of each release with the emulator skill, or analyze them with the c64-static-analysis skill.
89
89
  - Find what the change does, for example a trainer, a removed copy protection, or a changed text.
90
90
  6. Decide which bytes are probably the original application. Use more than the byte comparison: the code, the text, and the history of the releases.
91
91
  7. Record each release with `release` of the c64-knowledge skill. The base release is the program of the knowledge.
@@ -143,7 +143,7 @@ Do these steps:
143
143
  3. Run c64-static-analysis. Without code regions, `deadCode` is empty, and `residue` can include code.
144
144
  4. Run `leftovers`. When you have two or more releases, also compare them.
145
145
  5. Read `text` first: names, dates, greetings and credits. Then read `copies`, `residue`, `basic` and `deadCode`.
146
- 6. Examine each important finding with the c64-emulator skill or the c64-static-analysis skill.
146
+ 6. Examine each important finding with the emulator skill or the c64-static-analysis skill.
147
147
  7. Record each confirmed finding with the c64-knowledge skill. Do not remove a leftover from the program: a rebuild keeps it. Only the data of a cracktro goes out, with `capture --exclude` of the c64-unpacker skill. Classify a confirmed PETSCII text as a `petscii` region, and a screen code text as a `screen` region. Classify other text as `bytes`. Write a comment that tells what the leftover is.
148
148
 
149
149
  Read the findings with care:
@@ -226,7 +226,7 @@ The runtime evidence and the leftovers are evidence only. The script does not de
226
226
 
227
227
  - Get releases from disk images → c64-disk
228
228
  - Unpack a packed release → c64-unpacker
229
- - Run or examine the different code → c64-emulator
229
+ - Run or examine the different code → emulator
230
230
  - Analyze the different code → c64-static-analysis
231
231
  - Record a conclusion → c64-knowledge
232
232
  - Apply the batch of `match` → c64-knowledge
@@ -72,7 +72,7 @@ Select the path for the scope:
72
72
  | The files on a disk image, or a file to extract | c64-disk |
73
73
  | A BASIC program, and where it starts machine code | c64-basic |
74
74
  | A program that can be packed or crunched | c64-unpacker |
75
- | What the program does at run time: values, input, timing, screen | c64-emulator |
75
+ | What the program does at run time: values, input, timing, screen | emulator |
76
76
  | The routines, code, data and references of the program | c64-static-analysis |
77
77
  | The platform meaning of an address or a register | c64-memory-map |
78
78
  | Differences between releases of the program | c64-provenance `compare`, then c64-knowledge `release`, `switch` and `patch` |
@@ -96,7 +96,7 @@ Do the stages in this sequence. Go to the next stage only when its exit check is
96
96
  | 2. Get | c64-disk: list the disk. Extract the start file into `original/`. | The start file is in the project. |
97
97
  | 3. Start | c64-basic: find the SYS address of the BASIC start. | You have the entry address. |
98
98
  | 4. Unpack | c64-unpacker: examine the program with `unpacker.ts inspect`, `trace` and `stages`. Capture a packed program with `unpacker.ts capture` into `.c64-re-tools/local/`. When a segment is the data of a cracktro and the program does not read it, capture again with `--exclude` for that range. Then c64-knowledge: put the capture, or the PRG of a program without a packer, into the knowledge with `knowledge.ts image`. | `program` of c64-knowledge shows the segments and the entry. |
99
- | 5. Run | First record: ask the user to play each part that the scope names, with `testing.ts record --play` of c64-testing. Tell the user which parts. Record a part yourself only when the sessions of the user do not have it. Put the recordings in `tests/recordings/`. Then use c64-emulator and `testing.ts run` of c64-testing for the parts that a recording cannot have. Write a coverage map for each part into `.c64-re-tools/local/`. Then c64-knowledge: add the maps and the `coverage.bin` of each recording with `knowledge.ts coverage`. | `program` of c64-knowledge lists a coverage map for each part that the scope names. |
99
+ | 5. Run | First record: ask the user to play each part that the scope names, with `testing.ts record --play` of c64-testing. Tell the user which parts. Record a part yourself only when the sessions of the user do not have it. Put the recordings in `tests/recordings/`. Then use the emulator skill and `testing.ts run` of c64-testing for the parts that a recording cannot have. Write a coverage map for each part into `.c64-re-tools/local/`. Then c64-knowledge: add the maps and the `coverage.bin` of each recording with `knowledge.ts coverage`. | `program` of c64-knowledge lists a coverage map for each part that the scope names. |
100
100
  | 6. Analyze | c64-static-analysis: `static-analysis.ts analyze`. It reads the program and the coverage from the knowledge. | No coverage conflict is open. |
101
101
  | 7. Name | c64-provenance: if the workspace has a library, find its routines with `provenance.ts match` and apply the batch. Then c64-knowledge and c64-memory-map: name each routine in the scope and give it a comment. Record the modules. When the scope has hundreds of routines, ask the user which routines to name. The source gives the other routines an `l_xxxx` label. | Each routine that the user selected has a name and a comment. |
102
102
  | 8. Releases and switches | c64-knowledge: record the base release with `knowledge.ts release <label> --file original/<file> --base`. For a capture, copy its folder to `original/<label>/` first, because git ignores `.c64-re-tools/local/`. Record each other release. c64-provenance: compare the releases with `provenance.ts compare`, and find leftovers with `provenance.ts leftovers`. c64-knowledge: record a switch for each copy protection, trainer, cheat, crack change and correction, and a patch for each of its ranges. Record each leftover as a region with a comment. | `knowledge.ts switches` lists each switch. Each difference between the releases is in a patch, or the answer gives it as an exception. |
@@ -179,7 +179,7 @@ A clear answer for the scope, with the durable conclusions in the project knowle
179
179
  - A tool gives a wrong result, and the exit check of a stage cannot be true. Tell the user the defect and the evidence, and wait for the user. Do not make a workaround, for example a change to a generated file. When the user accepts an exception, give the defect and the exception in the answer of stage 12.
180
180
  - If the evidence does not support a conclusion, keep the unknown and get more evidence. Do not make up a meaning.
181
181
  - Before you replace current knowledge from the user or the LLM, read its history and the evidence for it.
182
- - A conflict from the static analysis: find the correct answer at run time with the c64-emulator skill.
182
+ - A conflict from the static analysis: find the correct answer at run time with the emulator skill.
183
183
  - A test that is INCONCLUSIVE does not show a defect. Find why the test could not compare.
184
184
 
185
185
  ## Handoffs
@@ -128,10 +128,10 @@ The exit status is 0, also with conflicts.
128
128
 
129
129
  ## Failure and conflicts
130
130
 
131
- - A conflict: the analyzer finds code where the knowledge has data, or data where the knowledge has code. The knowledge stays unchanged. Find the correct answer with the c64-emulator skill, then correct the knowledge with the c64-knowledge skill.
131
+ - A conflict: the analyzer finds code where the knowledge has data, or data where the knowledge has code. The knowledge stays unchanged. Find the correct answer with the emulator skill, then correct the knowledge with the c64-knowledge skill.
132
132
  - A conflict with the problem "another address has this name": the knowledge has the generated name at a different address. The knowledge stays unchanged.
133
133
  - A false reference through a vector: the code jumps through a vector, for example `JMP ($0408)`, and the program writes the vector at run time. The analyzer follows the bytes that the program has there. Do these steps:
134
- 1. Find the real targets with the c64-emulator skill, or in a coverage file.
134
+ 1. Find the real targets with the emulator skill, or in a coverage file.
135
135
  2. If the vector bytes are also code at another time, classify them as `dual-use`.
136
136
  3. Remove the false reference with `unreference`. It does not come back.
137
137
  4. Add a `jump` reference from the JMP to each real target, and name each target as a routine.
@@ -143,7 +143,7 @@ The exit status is 0, also with conflicts.
143
143
 
144
144
  ## Handoffs
145
145
 
146
- - Runtime behavior → c64-emulator
146
+ - Runtime behavior → emulator
147
147
  - Names and classifications → c64-knowledge
148
148
  - The meaning of a C64 hardware address → c64-memory-map
149
149
  - Source code from the knowledge → c64-source
@@ -165,7 +165,7 @@ The script refuses a snapshot that does not restore in its emulator. An example
165
165
  - `FAIL`: `differences` lists each difference with the checkpoint and the item. A memory item gives `bytes`: the count of the bytes that differ, and the first of these bytes. Each byte has the address, the original value and the rebuild value. `--max-listed` sets how many bytes it lists (default 50). `memory.json` in `--out` has all of them. Another item gives the original value and the rebuild value.
166
166
  - Each value has one form. The pc and each address are `$xxxx`. Memory is lowercase hex. Other register values and chip values are decimal numbers.
167
167
  - `INCONCLUSIVE`: `reason` tells why the script could not compare. This is not a failure of the program.
168
- 7. For a FAIL, examine the first difference with the c64-emulator skill.
168
+ 7. For a FAIL, examine the first difference with the emulator skill.
169
169
 
170
170
  ## Knowledge
171
171
 
@@ -195,7 +195,7 @@ PASS, FAIL or INCONCLUSIVE, the checkpoints and the smallest differences that ex
195
195
  - INCONCLUSIVE, `step N (kind): ...`: the step failed on one side. `reason` names the step and why.
196
196
  - `invalid-input` that names the 2.0 form: a step, a memory item or a recording has a form of version 2.0. An example is `"at"` in place of `"address"`. The message gives the form to write. Change the file. Record a recording of version 2.0 again.
197
197
  - `not-found` before the run: a file of the scenario or the run file is missing, for example the rebuild program. Build the rebuild, or change the path.
198
- - INCONCLUSIVE, "did not reach the checkpoint": one side did not reach the checkpoint in the frame limit. Find the reason with the c64-emulator skill. Do not report a FAIL.
198
+ - INCONCLUSIVE, "did not reach the checkpoint": one side did not reach the checkpoint in the frame limit. Find the reason with the emulator skill. Do not report a FAIL.
199
199
  - INCONCLUSIVE, "has no symbol": one side has no symbol with that name. Add the name to the knowledge or to the rebuild source.
200
200
  - INCONCLUSIVE, "no emulator" or "could not run": the host runtime or the emulator failed. Tell the user the message.
201
201
  - `installation-incomplete`, or `machine-unavailable` with a message that the host runtime does not run: stop the task. Give the message to the user and wait for the user. The message gives the remedy. Do not make a replacement for the tool or the host runtime, for example your own disk parser, disassembler or emulator script.
@@ -208,7 +208,7 @@ PASS, FAIL or INCONCLUSIVE, the checkpoints and the smallest differences that ex
208
208
  - INCONCLUSIVE, "is not the program of the recording": the program file changed after the recording. Record the session again.
209
209
  - A record result FAIL with `recorded` false: `reason` names the step that did not complete. Examples are a `runUntil` that did not reach its target, and a `type` step with too many keys. Change the step.
210
210
  - A record result with `executed` 0: the program never read its input from RAM, so the coverage map is empty. The recording can still replay.
211
- - A FAIL from a replay: compare the two RAM files of `--out` with `testing.ts diff`. Then examine the first different state item with the c64-emulator skill.
211
+ - A FAIL from a replay: compare the two RAM files of `--out` with `testing.ts diff`. Then examine the first different state item with the emulator skill.
212
212
  - A program that loads more files from a disk: the replay core has no disk drive. Use a test scenario in the emulator.
213
213
  - A byte that cannot be the same on both sides, for example a read-only collision register: use `exclude` with the reason. Do not use `exclude` to hide a real difference.
214
214
 
@@ -216,7 +216,7 @@ Do not add a mask or a tolerance only to make a test pass. A tolerance must have
216
216
 
217
217
  ## Handoffs
218
218
 
219
- - Find the cause of a difference → c64-emulator
219
+ - Find the cause of a difference → emulator
220
220
  - Find the code that causes it → c64-static-analysis
221
221
  - Rebuild after a correction → c64-assembler
222
222
  - Write or change a program, with expect steps as its tests → c64-development