@henols/c64-re-tools 2.3.0-rc.4 → 2.3.0-rc.6

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.
Files changed (62) hide show
  1. package/package.json +1 -1
  2. package/skills/c64-assembler/SKILL.md +149 -78
  3. package/skills/c64-assembler/scripts/assembler.ts +18 -11
  4. package/skills/c64-basic/SKILL.md +90 -52
  5. package/skills/c64-basic/scripts/basic.ts +15 -6
  6. package/skills/c64-development/SKILL.md +46 -73
  7. package/skills/c64-disk/SKILL.md +145 -73
  8. package/skills/c64-disk/scripts/disk.ts +11 -6
  9. package/skills/c64-docs/SKILL.md +199 -118
  10. package/skills/c64-docs/references/page-types.md +3 -3
  11. package/skills/c64-docs/references/style.md +9 -13
  12. package/skills/c64-docs/scripts/docs.ts +39 -15
  13. package/skills/c64-docs/scripts/lib/links.ts +1 -1
  14. package/skills/c64-docs/scripts/lib/reference.ts +2 -2
  15. package/skills/c64-docs/scripts/lib/source-map.ts +2 -2
  16. package/skills/c64-export/SKILL.md +21 -45
  17. package/skills/c64-knowledge/SKILL.md +489 -233
  18. package/skills/c64-knowledge/scripts/knowledge.ts +31 -26
  19. package/skills/c64-memory-map/SKILL.md +131 -65
  20. package/skills/c64-memory-map/scripts/memory-map.ts +19 -11
  21. package/skills/c64-packer/SKILL.md +120 -72
  22. package/skills/c64-packer/scripts/packer.ts +10 -3
  23. package/skills/c64-provenance/SKILL.md +301 -173
  24. package/skills/c64-provenance/scripts/lib/library.ts +6 -6
  25. package/skills/c64-provenance/scripts/provenance.ts +85 -36
  26. package/skills/c64-reverse-engineering/SKILL.md +55 -72
  27. package/skills/c64-source/SKILL.md +316 -224
  28. package/skills/c64-source/scripts/lib/export.ts +9 -14
  29. package/skills/c64-source/scripts/lib/loader.ts +5 -5
  30. package/skills/c64-source/scripts/lib/source.ts +5 -5
  31. package/skills/c64-source/scripts/source.ts +106 -21
  32. package/skills/c64-static-analysis/SKILL.md +188 -112
  33. package/skills/c64-static-analysis/scripts/static-analysis.ts +29 -9
  34. package/skills/c64-testing/SKILL.md +272 -166
  35. package/skills/c64-testing/references/recording-format.md +11 -12
  36. package/skills/c64-testing/references/scenario-format.md +20 -5
  37. package/skills/c64-testing/scripts/lib/diff-files.ts +8 -8
  38. package/skills/c64-testing/scripts/lib/play.ts +1 -1
  39. package/skills/c64-testing/scripts/lib/recording.ts +7 -7
  40. package/skills/c64-testing/scripts/lib/replay.ts +7 -7
  41. package/skills/c64-testing/scripts/lib/runs.ts +6 -7
  42. package/skills/c64-testing/scripts/lib/scenario.ts +12 -6
  43. package/skills/c64-testing/scripts/testing.ts +36 -16
  44. package/skills/c64-unpacker/SKILL.md +273 -127
  45. package/skills/c64-unpacker/scripts/lib/loads.ts +3 -3
  46. package/skills/c64-unpacker/scripts/unpacker.ts +70 -11
  47. package/skills/emulator/SKILL.md +12 -12
  48. package/src/errors.ts +22 -2
  49. package/src/host/vice/session.ts +1 -1
  50. package/src/host-client/entry-state.ts +1 -1
  51. package/src/host-client/image.ts +3 -3
  52. package/src/host-client/steps.ts +1 -1
  53. package/src/knowledge/expression.ts +1 -1
  54. package/src/knowledge/names.ts +2 -3
  55. package/src/knowledge/program.ts +2 -3
  56. package/src/knowledge/write.ts +2 -2
  57. package/src/mcp/inputs.ts +1 -1
  58. package/src/mcp/tools/input.ts +1 -1
  59. package/src/mcp/tools/media.ts +1 -1
  60. package/src/native/exomizer.ts +2 -2
  61. package/src/project.ts +2 -2
  62. package/src/script.ts +164 -75
@@ -1,178 +1,324 @@
1
1
  ---
2
2
  name: c64-unpacker
3
- description: Use this skill to find out if a packer or a cruncher compressed a C64 program, and to get the unpacked program for analysis. It shows evidence of packing without a guess of the packer name. It runs the program in an emulator until the unpacked code starts, then writes the memory into the project. It also captures each part of a multi-load program and the drive RAM. Do not use it to analyze the unpacked code.
3
+ description: Finds evidence that a packer or a cruncher compressed a C64 program, and captures the unpacked program from the emulator into the project. Use it when a program can be packed, crunched, encrypted or relocated. Use it to get the unpacked program, its segments and its entry state for analysis. Use it to capture each part of a multi-load program and the drive RAM. It does not name the packer. It does not analyze the unpacked code.
4
4
  ---
5
5
 
6
- # c64-unpacker
6
+ # C64 unpacker
7
7
 
8
- ## Purpose
8
+ Use this script to find evidence of packing and to capture the unpacked program.
9
+ Use only the commands of the script. Do not unpack or capture a program in another way.
9
10
 
10
- Find out if a packer compressed a C64 program. Get the unpacked program for analysis.
11
+ ```bash
12
+ node <skill>/scripts/unpacker.ts <command>
13
+ ```
11
14
 
12
- This skill does not change project knowledge.
15
+ `<skill>` is the base directory of this skill. Run the script with `node`. You can run it from any directory.
16
+ Each command in this skill, for example `inspect original/game.prg`, goes in the place of `<command>`.
17
+ Each command prints one JSON envelope on stdout. See the section "JSON output".
18
+ To get the command list, run the script with no arguments.
13
19
 
14
- ## Inputs
20
+ ---
15
21
 
16
- - The program: a PRG file, relative to the project folder. `inspect`, `trace`, `stages` and `capture` refuse a disk, tape or cartridge image. For such an image, use `loads`, or get the PRG with `disk.ts read` of the c64-disk skill.
17
- - For a capture: the address where the unpacked program starts, and the directory for the result.
22
+ ## Select the command
18
23
 
19
- ## Tools
24
+ | Goal | Command |
25
+ |---|---|
26
+ | Find evidence of packing in the file, without a run | `inspect original/game.prg` |
27
+ | Find the code that the program wrote and then executed | `trace original/game.prg` |
28
+ | Run a program that is not a BASIC program at `$0801` | `trace original/game.prg --entry '$c000'` |
29
+ | Give input to an intro or a menu before the evidence starts | `trace original/game.prg --steps .c64-re-tools/local/input.json` |
30
+ | Find each stage and the entry of the unpacked program | `stages original/game.prg` |
31
+ | Capture the unpacked program at its entry | `capture original/game.prg --until '$c000' --out .c64-re-tools/local/capture` |
32
+ | Capture again without the bytes of a cracktro | `capture original/game.prg --until '$c000' --out .c64-re-tools/local/capture --exclude '$a000-$a9ff'` |
33
+ | Capture only one known range of the program | `capture original/game.prg --until '$c000' --out .c64-re-tools/local/capture --range '$c000-$cfff'` |
34
+ | Capture each part of a multi-load program | `loads original/game.d64 --steps .c64-re-tools/local/parts.json --out .c64-re-tools/local/parts` |
35
+ | Capture each part and the RAM of the disk drive | `loads original/game.d64 --steps .c64-re-tools/local/parts.json --out .c64-re-tools/local/parts --drive` |
20
36
 
21
- ```
22
- node <skill>/scripts/unpacker.ts inspect original/game.prg
23
- node <skill>/scripts/unpacker.ts trace original/game.prg
24
- node <skill>/scripts/unpacker.ts stages original/game.prg --steps .c64-re-tools/local/input.json
25
- node <skill>/scripts/unpacker.ts capture original/game.prg --until '$c000' --out .c64-re-tools/local/capture
26
- node <skill>/scripts/unpacker.ts loads original/game.d64 --steps .c64-re-tools/local/parts.json --out .c64-re-tools/local/parts --drive
27
- ```
37
+ ---
28
38
 
29
- `inspect` examines the file and does not run the program. `trace`, `stages`, `capture` and `loads` run the program. `trace` runs the program and finds the memory that the program wrote and then executed. `stages` runs the program and finds each stage: a decruncher, an intro, a copier, and then the program. For each stage it gives the entry, the frames, the written memory (`wrote`) and the entry of the next stage (`next`). `wrote` lists each block that the stage wrote, also a small block. `capture` stops the program when the CPU gets to the `--until` address. In `stages` and `capture`, a `frames` step and a `runUntil` step to an address without a condition count as run time. `stages` refuses a different `runUntil` step.
39
+ ## Programs, addresses and steps
30
40
 
31
- | Option | Meaning |
32
- |---|---|
33
- | `--until '$c000'` | `capture`: the address where the unpacked program starts. The capture stops there after the steps. To capture a later arrival at the address, add a `runUntil` step to an address after the first arrival. |
34
- | `--out .c64-re-tools/local/capture` | `capture` and `loads`: the folder for the result, relative to the project folder. |
35
- | `--range '$c000-$cfff'` | `capture` only: write this range as the one segment. Without it, the segments are the blocks that the stages wrote. The range must hold the `--until` address. |
36
- | `--exclude '$a000-$a9ff'` | `capture` and `loads`: remove this range from the segments. You can give it more than once. `image.json` lists the ranges in `excluded`. For `capture`, the range must not hold the `--until` address. |
37
- | `--timeout-frames 3000` | `capture` only: how many frames the program can run before the capture stops (default 3000). |
38
- | `--frames 3000` | `trace` and `stages`: how many frames the program runs (default 3000). |
39
- | `--entry '$c000'` | `trace`, `stages` and `capture`: start the program at this address. Without it, the script types RUN. A file that is not a BASIC program at `$0801` needs it. |
40
- | `--steps input.json` | Input for the program, right after it starts: a JSON list of steps. Use it when an intro waits for a key or a menu waits for a choice. |
41
- | `--max-listed 50` | `trace` only: the most ranges that `writtenThenExecuted` lists (default 50). |
42
- | `--drive` | `loads` only: each capture also writes the 2 KiB RAM of the 1541 drive as `drive8.prg`. |
43
- | `--min-block 64` | `loads` only: the smallest block of changed memory that becomes a segment (default 64). The result counts smaller changes as `smallChanges`. |
44
- | `--min-stage 64` | `stages` and `capture`: the smallest block of written memory that starts a new stage when the CPU executes it (default 64). It does not change `wrote` or the segments. |
45
-
46
- The capture reads RAM, also the RAM under the ROMs and the I/O area. A segment can be in the RAM under the I/O area at `$d000`-`$dfff`. The script refuses an option that the command does not use. It refuses a wrong `--range` or `--exclude` before the program runs.
47
-
48
- A steps file is a JSON list of steps. Each step has the input of the MCP tool of the same action. An address in a step can be a name. The script refuses an unknown name before the program runs. The message gives the file and the step. For example, wait 50 frames and type a space:
41
+ - `<program>` is a PRG file of the project. `inspect`, `trace`, `stages` and `capture` refuse a disk, tape or cartridge image. For an image, use `loads`. Or extract the PRG from the image, and give the path of that PRG.
42
+ - `loads` takes a PRG, or a disk, tape or cartridge image. It starts a PRG with RUN. Thus the PRG must have a BASIC start line, for example `10 SYS 2061`.
43
+ - `trace`, `stages` and `capture` start the program with RUN. RUN needs a BASIC program at `$0801`. For another program, give `--entry` with the start address of its code. Get that address from the load address of the file, or from a disassembly of the file.
44
+ - Get the `--until` address of `capture` from `stages`, or from `runningRange.start` or `suggestedUntil` of `trace`. Or set a breakpoint in the emulator after the unpack loop. Then read the address where the CPU stops.
45
+ - Get each `--exclude` range from the segments of an earlier capture. Exclude only bytes that the program does not read. See step 5 of the section "Workflow".
46
+
47
+ ### Steps files
48
+
49
+ A steps file is a JSON list of steps. The steps run right after the program starts. Use a steps file when an intro waits for a key or a menu waits for a choice. For example, wait 50 frames and type a space:
49
50
 
50
51
  ```json
51
52
  [{ "frames": 50 }, { "type": { "mode": "text", "text": " " } }]
52
53
  ```
53
54
 
55
+ Each step has one of these kinds:
56
+
54
57
  - `{ "write": { "address": "$c000", "data": "0102" } }` writes bytes.
55
58
  - `{ "registers": { "pc": "start", "a": 1 } }` sets registers. Only `pc` takes a name.
56
59
  - `{ "joystick": { "port": 2, "direction": "up", "fire": false } }` sets a joystick.
57
60
  - `{ "type": { "mode": "text", "text": "RUN\n" } }` or `{ "type": { "mode": "petscii", "bytes": [147] } }` puts keys in the keyboard queue of the KERNAL.
58
61
  - `{ "frames": 50 }` runs 1 to 30000 frames.
59
- - `{ "runUntil": { "target": { "kind": "address", "address": "main_loop" }, "timeoutFrames": 300 } }` runs until the target. The target kinds are `address`, `memory` and `raster`. `timeoutFrames` is 1 to 30000 (default 3000).
62
+ - `{ "runUntil": { "target": { "kind": "address", "address": "main_loop" }, "timeoutFrames": 300 } }` runs until the target. The target kinds are `address`, `memory` and `raster`. `timeoutFrames` is 1 to 30000. The default is 3000.
63
+ - `{ "capture": "level1" }` captures a part. Only `loads` takes this step. Give each capture step a different name of 1 to 64 letters, digits, dots, underscores or hyphens.
64
+
65
+ An address in a step can be a name. The script refuses an unknown name before the program runs. The message gives the file and the step.
60
66
 
61
- A program that reads the keyboard matrix does not see the keys of a `type` step. These scripts refuse a `press` step. A wait for any key that reads all keyboard columns can end without a key.
67
+ The script refuses a `press` step. A program that reads the keyboard matrix does not see the keys of a `type` step. A wait for any key that reads all keyboard columns can end without a key.
62
68
 
63
- The scripts refuse a step in the form of version 2.0, for example `{ "runUntil": { "at": "main_loop" }, "timeoutFrames": 300 }`. The message names the old field and gives the step to write.
69
+ The script refuses a step in an old form. The message gives the step to write.
64
70
 
65
- ### Calling the script
71
+ ### Paths and the project folder
66
72
 
67
- - Call a script as `node <skill>/scripts/<script>.ts <command> [subjects] [options]`. The command comes first.
68
- - `<script>.ts --help` lists the commands. `<script>.ts <command> --help` gives the subjects and the options of one command. The help is JSON: `{"text": "..."}`.
69
- - 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.
73
+ - Give each path relative to the project folder. The project folder is the nearest folder at or above the working directory that holds `.c64-re-tools/`.
74
+ - If the working directory is not in a project, give `--project <folder>` to each command. If the working directory has projects below it, the script refuses and lists them.
75
+ - 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. Put an address in single quotes in a shell.
70
76
  - A range is `<start>-<end>`, for example `'$c000-$cfff'`. In a JSON file, a range is `start` and `end`.
71
77
  - Bytes in a JSON file are hex in a field `data`, for example `"data": "a900"`.
72
- - 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.
73
- - 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.
74
- - An error is `{"error": {"code": "...", "message": "..."}}`. The message tells what is wrong and what to do.
75
- - The exit status is 0 for done or PASS, 1 for FAIL, 2 for an error and 3 for INCONCLUSIVE.
76
-
77
- ### Project folder
78
-
79
- - 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/`.
80
- - 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.
81
78
  - 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.
82
- - 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/`.
83
- - 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.
84
- - 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.
79
+ - The script refuses an `--out` path in `.c64-re-tools/knowledge/` and the index of the knowledge. Change the project knowledge only with the commands that change it.
80
+ - The script refuses a folder where the command writes a file. It refuses a file where the command writes a folder.
81
+ - When a file is the evidence for a finding in the project knowledge, move it out of `.c64-re-tools/local/` first. The project knowledge refuses evidence in that folder.
82
+ - Do not move a coverage map after you put it into the project knowledge. The knowledge refers to the map by its path at that time.
83
+ - Put a run that is evidence in `runs/<name>/`, with its run file, its results and its snapshot files. Change the paths in the run file to the new paths. Then git keeps them, and another person can do the run again.
85
84
  - 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`.
86
85
 
86
+ ---
87
+
88
+ ## Flags
89
+
90
+ Each flag applies only to the commands in the table. On another command, the script refuses the flag.
91
+
92
+ | Flag | Command | Effect |
93
+ |---|---|---|
94
+ | `--entry <address>` | `trace`, `stages`, `capture` | Start the program at this address in place of RUN. Without it, the script types RUN. |
95
+ | `--steps <file>` | `trace`, `stages`, `capture`, `loads` | Run the steps of the file right after the program starts. See "Steps files". `loads` needs this flag. The other commands run no steps without it. |
96
+ | `--frames <n>` | `trace`, `stages` | Run the program for n frames, at most 30000. The default is 3000. In `stages`, the frames of the steps count in n. |
97
+ | `--max-listed <n>` | `trace` | List at most n ranges in `writtenThenExecuted`. The default is 50. |
98
+ | `--min-stage <bytes>` | `stages`, `capture` | A new stage starts when the CPU executes a written block of at least this size. The default is 64. It does not change `wrote` or the segments. |
99
+ | `--until <address>` | `capture` | Capture when the CPU gets to this address after the steps. `capture` needs this flag. |
100
+ | `--out <dir>` | `capture`, `loads` | Write the result into this folder. These commands need this flag. |
101
+ | `--range <start>-<end>` | `capture` | Write only this range as the one segment. The range must hold the `--until` address. Without it, the segments are the blocks that the stages wrote. |
102
+ | `--exclude <start>-<end>` | `capture`, `loads` | Remove this range from the segments. You can give it more than once. For `capture`, the range must not hold the `--until` address. The default is no range. |
103
+ | `--timeout-frames <n>` | `capture` | Stop the wait for `--until` after n frames, at most 30000. The default is 3000. The frames of the steps count in n. |
104
+ | `--drive` | `loads` | Also write the 2 KiB RAM of the 1541 drive at each capture, as `drive8.prg`. The default is off. |
105
+ | `--min-block <bytes>` | `loads` | Write a changed block as a segment when it has at least this size. The default is 64. |
106
+
107
+ The script refuses a wrong `--range` or `--exclude` before the program runs.
108
+
109
+ ---
110
+
111
+ ## Examine the file
112
+
113
+ ### `inspect <program>`
114
+
115
+ `inspect` examines the file. It does not run the program.
116
+
117
+ `body` holds:
118
+
119
+ - `loadRange` and `bytes`: the addresses and the size of the program.
120
+ - `basicStart`: the first SYS line and its address in `sys`. When the SYS argument needs values at run time, `sys` is null and `expression` gives the argument. Then `stages` and `capture` start the first stage at the first instruction after the BASIC text. `basicStart` is null when the program has no SYS.
121
+ - `packing`, with the reason in `evidence`:
122
+ - `likely`: most of the bytes look compressed. Packed data has this property.
123
+ - `unlikely`: the bytes look like code and data. A simple cruncher can also give this result.
124
+ - `unclear`: the file is short, or the blocks do not give clear evidence. Or the code at the SYS address decodes or moves bytes outside the program.
125
+ - `entropy`: the count of blocks, the count of blocks that look compressed, and the median.
126
+ - `basicText`: the BASIC lines as raw evidence. A byte that is not a printable character shows as `{$xx}`. A packer or a crack often puts its name there. This is not a listing. To get a listing, list the BASIC program of the file.
127
+ - `strings`: readable text near the start of the code.
128
+ - `start`: the code at the SYS address. `jumpsOutside` and `writesOutside` give an address outside the program, where the code jumps or writes.
129
+ - `decodeLoops`: EOR instructions in loops that store with an index. This is usually decryption or decoding.
130
+
131
+ The script never names a packer. Do not name a packer from this evidence.
132
+
133
+ `nextAction` is `trace` of the same program. When the program is not a BASIC program at `$0801`, there is no `nextAction`. Then `hint` tells you to give `--entry`.
134
+
135
+ ---
136
+
137
+ ## Run the program
138
+
139
+ `trace`, `stages`, `capture` and `loads` run the program in the emulator. `body.result` is `PASS` or `FAIL`. A `FAIL` has `reason`, which tells what stopped the run. Do the action for the reason:
140
+
141
+ - The program was not in memory in the time limit. The reason gives the load address. Make sure that the file is a PRG that the C64 can load.
142
+ - The body has `stopReason`. The CPU stopped before the frames ran, for example at a JAM instruction. The reason gives the address. Make sure that `--entry` is the start of the code at that time. Make sure that the steps have no error.
143
+ - A step did not reach its target. The reason names the step. Change the step, or give it more frames.
144
+ - After RUN, BASIC ran no program line. The program has no BASIC start line. Give `--entry`. For `loads`, use `capture` with `--entry`.
145
+
146
+ ### `trace <program>`
147
+
148
+ `trace` runs the program and finds the memory that the program wrote and then executed.
149
+
150
+ `body` holds:
151
+
152
+ - `traced`: true when the run completed. `frames` and `pc` give where the run ended.
153
+ - `writtenThenExecuted`: the ranges that the program wrote and then executed, as a list. `underRom: true` marks a range in RAM under a ROM.
154
+ - `bytes`: the count of written bytes that the CPU executed.
155
+ - `packing`, with the reason in `evidence`:
156
+ - `likely`: the program wrote code and then executed it. It unpacked, decrypted or moved code.
157
+ - `unclear`: the program executed a small quantity of code that it wrote. This can be code that changes itself.
158
+ - `unlikely`: the program executed no code that it wrote.
159
+ - `runningRange`: the written code that runs at the end of the run. When the run ends in ROM code, for example in an interrupt, it is the written code that runs next. Its `start` is code that runs after the unpacking. It is not always the entry point of the program.
160
+ - `suggestedUntil`: an address that the CPU gets to after the unpacking.
161
+
162
+ When no written code runs in 50 more frames, `body` has no `runningRange` and no `suggestedUntil`.
163
+
164
+ For `likely` and `unclear`, `nextAction` is `stages` with the same `--entry` and `--steps`. For `unlikely`, `hint` tells you to give the input of the program in `--steps`. A program that waits for a key or a joystick can unpack its code after that input. When `writtenThenExecuted` is cut, `hint` tells you to give a larger `--max-listed`.
165
+
166
+ ### `stages <program>`
167
+
168
+ `stages` runs the program and finds each stage, for example a decruncher, an intro, a copier, and then the program.
169
+
170
+ `body` holds:
171
+
172
+ - `followed`: true when the run completed. `frames` and `pc` give where the run ended.
173
+ - `stages`: one item for each stage, with `stage`, `entry`, `frames`, `wrote` and `next`. `wrote` lists each block that the stage wrote, also a small block. `next` is the entry of the next stage.
174
+ - `moreStages: true`: the script followed only the first stages. Then `hint` tells you to give a larger `--min-stage`.
175
+
176
+ The `entry` of the last stage is usually the start of the program. `nextAction` is `capture` at that entry, into `.c64-re-tools/local/capture`, with the same `--entry` and `--steps`. `hint` tells you to disassemble the code at that entry first.
177
+
178
+ Traps:
179
+
180
+ - A routine that a stage writes and calls is part of that stage when it returns to the stage.
181
+ - A program can be relocated and not packed. Then a short copier moves the code and jumps to it. The moved code is the last stage.
182
+ - The `entry` of the last stage can be code that the program writes at run time. Disassemble the entry to make sure that it is the start of the program.
183
+ - `stages` takes a `runUntil` step only with an address target without a condition. It refuses each other `runUntil` step. Use `frames` steps, or use `capture`.
184
+
185
+ ### `capture <program>`
186
+
187
+ `capture` runs the program until the CPU gets to `--until` after the steps. Then it writes the memory into `--out`.
188
+
189
+ `body` holds:
190
+
191
+ - `captured`: true when the CPU got to `--until`. `pc` is that address.
192
+ - `files`: each file that the command wrote, as `{out, bytes, sha256}`. `removed` names the segment files of an earlier capture that the new `image.json` does not list.
193
+ - `segments` and `excluded`: the ranges of the segment files, and the ranges that `--exclude` removed.
194
+ - `stages`: the count of stages.
195
+ - `differsFromReset`: the count of bytes that are not as a clean C64 has them at READY.
196
+
197
+ The folder `--out` gets these files:
198
+
199
+ - `image.bin`: all 64 KiB of RAM at the entry. This RAM includes the RAM under the ROMs and the I/O area. A segment can be in the RAM under the I/O area at `$d000`-`$dfff`.
200
+ - `image.json`: the entry, the segments, the vectors and the stages. `writtenBy` of a segment tells the last stage that wrote into it.
201
+ - One PRG for each segment.
202
+ - `entry_state.json`: the registers, `$00` and `$01`, pages 0 to 3, the VIC, SID and CIA registers, and the vectors. `cia` gives the CIA timer latches and interrupt masks. `differsFromReset` lists the bytes that are not as a clean C64 has them at READY. A rebuild must make these bytes the same, because the program can read them.
203
+
204
+ A segment is a block that a stage wrote, also a small block. These blocks are not segments:
205
+
206
+ - A block that holds the entry of a later stage before the program, when no stage wrote over that entry.
207
+ - A block that a stage before the program wrote in such a block, for example a variable of a loader.
208
+ - A block that is fully in pages 0 to 3. `entry_state.json` holds these pages.
209
+
210
+ The block with the capture entry is always a segment. Segments do not overlap.
211
+
212
+ A capture into a folder with an earlier capture removes only the segment files that the new `image.json` does not list. It keeps all other files.
213
+
214
+ On `PASS`, `hint` tells you to disassemble the code at `pc`, and then to put `<out>/image.json` into the project knowledge.
215
+
216
+ Traps:
217
+
218
+ - The capture stops at the first arrival at `--until` after the steps. To capture a later arrival, add a `runUntil` step to an address after the first arrival.
219
+ - `captured: false`: the program did not get to `--until` in `--timeout-frames`. The reason tells where it stopped. Find a better address, or give more frames. If it stopped in a key wait, give `--steps` with a `type` step.
220
+
221
+ ### `loads <program>`
222
+
223
+ `loads` runs a multi-load program with the steps file. Each `capture` step writes `<out>/<name>/` with `image.bin`, `image.json`, `entry_state.json` and a PRG for each segment. With `--drive`, it also writes `drive8.prg`: the drive RAM at `$0000`-`$07ff`.
224
+
225
+ A segment is a block of RAM from `$0400` that the program changed after the capture before. The first capture also has the program itself.
226
+
227
+ `body` holds:
228
+
229
+ - `loaded`: true when a capture has a segment.
230
+ - `captures`: one item for each capture step, with `capture`, `out`, `files`, `removed`, `pc`, `segments`, `excluded`, `differsFromReset`, `smallChanges` and `drive`.
231
+ - `smallChanges`: the count of changed bytes in blocks that are smaller than `--min-block`.
232
+ - `keyWait: true`: the CPU was in the KERNAL wait for a key, at READY or at an INPUT statement.
233
+ - `drive`: the name of the drive file, with `--drive`.
234
+
235
+ Each `entry_state.json` has `differsFromReset`, as the one of `capture` has. A capture into a folder with an earlier capture removes the segment files and the `drive8.prg` that the new `image.json` does not list.
236
+
237
+ On `PASS`, `hint` tells you to put the `image.json` of the part that you work on into the project knowledge. With `--drive`, it also tells you to put `drive8.prg` into the project knowledge in the space `drive8`.
238
+
239
+ Traps:
240
+
241
+ - `loaded: false` and no capture has a segment: the program did not load a part, or it did not start. `keyWait: true` on a capture tells that the C64 waited for a key at READY or at an INPUT statement.
242
+
243
+ ---
244
+
87
245
  ## Workflow
88
246
 
89
- 1. Run `inspect`. Read `basicStart` and `packing`. `basicStart` gives the first SYS line and its address. When the SYS argument needs values at run time, `sys` is null and `expression` gives the argument. Then `trace`, `stages` and `capture` stop at the first instruction after the BASIC text:
90
- - `likely`: most of the bytes look compressed. Packed data has this property.
91
- - `unlikely`: the bytes look like code and data. A simple cruncher can also give this result.
92
- - `unclear`: the file is short, or the blocks do not give clear evidence. Or the code at the SYS address decodes or moves bytes outside the program.
93
-
94
- Also read the evidence that the script finds in the file. The script never names a packer from it:
95
- - `basicText`: raw evidence from the BASIC lines, not a listing. A byte that is not a printable character shows as `{$xx}`. A packer or a crack often puts its name there. For a listing, use the c64-basic skill.
96
- - `strings`: readable text near the start of the code.
97
- - `start`: the code at the SYS address. `jumpsOutside` and `writesOutside` give an address outside the loaded program, where the code jumps or writes.
98
- - `decodeLoops`: EOR instructions in loops that store with an index. This is usually decryption or decoding.
99
- 2. Run `trace`. It gives the evidence from the run. `writtenThenExecuted` lists the ranges that the program wrote and then executed. Read `packing`:
100
- - `likely`: the program wrote code and then executed it. It unpacked, decrypted or moved code. `runningRange` is the written code that runs at the end of the trace. When the trace ends in ROM code, for example in an interrupt, it is the written code that runs next. Its `start` is code that runs after the unpacking. It is not always the entry point of the program. When no written code runs in 50 more frames, the result has no `runningRange` and no `suggestedUntil`.
101
- - `unclear`: the program executed a small quantity of code that it wrote. This can be code that changes itself.
102
- - `unlikely`: the program executed no code that it wrote. If the program waits for a key or a joystick, it can move code after that input. Then use the emulator skill.
247
+ ### Packed program
248
+
249
+ 1. Run `inspect`. Read `packing` and the evidence.
250
+ 2. Run `trace`. Read `packing`. If it is `unlikely` and the program waits for input, give the input in `--steps`.
103
251
  3. Find the address where the unpacked program starts:
104
- - Run `stages`. The `entry` of the last stage is usually the start of the program. A routine that a stage writes and calls is part of that stage when it returns to the stage.
105
- - Or use `runningRange.start` from `trace`, or `suggestedUntil`. The CPU gets to `suggestedUntil` after the unpacking.
106
- - Or use the emulator skill: set a breakpoint after the unpack loop, or look at the last JMP of the unpacker.
107
- - Disassemble the code at the address with the emulator skill. Make sure that it is the start of the program. The start of a program often sets the stack pointer, the interrupts and the vectors.
108
- - A program can be relocated and not packed. Then a short copier moves the code and jumps to it. The moved code is the last stage. Make sure that the `entry` of the last stage is not code that the program writes later. An example is a routine that the program makes at run time. Disassemble the entry to make sure.
109
- 4. Run `capture` with that address and a directory in `--out`. Give the same `--steps` as before. The directory gets:
110
- - `image.bin`: all 64 KiB of RAM at the entry.
111
- - `image.json`: the entry, the segments, the vectors and the stages. A segment is a block that a stage wrote, also a small block. A block is not a segment when it holds the entry of a later stage before the program, and no stage wrote over that entry. A block that a stage before the program wrote in such a block is also not a segment, for example a variable of a loader. A block that is fully in pages 0 to 3 is not a segment, because `entry_state.json` holds these pages. The block with the capture entry is always a segment. Segments do not overlap. `writtenBy` tells the last stage that wrote into the segment.
112
- - One PRG for each segment. A capture into a directory that has a capture removes the segment PRGs that the new `image.json` does not list. `removed` in the result names them. The capture keeps all other files.
113
- - `entry_state.json`: the registers, `$00` and `$01`, pages 0 to 3, the VIC, SID and CIA registers, and the vectors. `cia` gives the CIA timer latches and interrupt masks. `differsFromReset` lists the bytes that are not as a clean C64 has them at READY. A rebuild must make these bytes the same, because the program can read them.
114
- 5. Make sure that the result is the application: the code at the entry must not be the unpacker again. A segment can be data that a loader or a cracktro left, for example intro graphics. Keep it until other evidence shows that the program does not use it. Then capture again with `--exclude` for that range. Thus the source and the loader do not include the range.
115
- - Cut each range of a cracktro that the program does not read. A rebuild has no cracktro.
116
- - Do not cut other unused bytes, for example leftovers of the development or an old copy of a routine. A rebuild keeps them.
117
- - A crack change that the program reads, for example a credit text in the game, stays. Record it as a `crack` switch with the c64-knowledge skill.
118
- 6. Put the capture into the knowledge with `image <out>/image.json` of the c64-knowledge skill. The knowledge keeps the segments, the entry, the entry state and the stages. The other skills read them there. The files in `<out>` are not necessary after that.
119
- 7. Use `--range` only when you know the area of the program. Then the capture writes that one segment.
120
-
121
- ## Multi-load programs
122
-
123
- A multi-load program loads more parts from the disk while it runs, for example one part for each level. Use `loads` to capture each part. The program can be a PRG, or a disk, tape or cartridge image. `loads` starts a PRG with RUN. Thus the PRG must have a BASIC start line. For a PRG without a BASIC start line, use `capture` with `--entry`.
252
+ - Run `stages`. Use the `entry` of the last stage.
253
+ - Or use `runningRange.start` or `suggestedUntil` of `trace`.
254
+ - Or set a breakpoint in the emulator after the unpack loop. Or examine the last JMP of the unpacker.
255
+ - Disassemble the code at the address. Make sure that it is the start of the program. The start of a program often sets the stack pointer, the interrupts and the vectors.
256
+ 4. Run `capture` with that address in `--until` and a folder in `--out`. Give the same `--steps` as before.
257
+ 5. Make sure that the result is the program. The code at the entry must not be the unpacker again. A segment can be data that a loader or a cracktro left, for example intro graphics. Keep it until other evidence shows that the program does not use it. Then capture again with `--exclude` for that range.
258
+ - Exclude each range of a cracktro that the program does not read. A rebuild has no cracktro.
259
+ - Do not exclude other unused bytes, for example leftovers of the development or an old copy of a routine. A rebuild keeps them.
260
+ - Keep a crack change that the program reads, for example a credit text in the game. Record it as a `crack` switch in the project knowledge.
261
+ 6. Put `<out>/image.json` into the project knowledge. The knowledge keeps the segments, the entry, the entry state and the stages. After that, you do not need the files in `<out>`.
262
+ 7. Use `--range` only when you know the area of the program.
263
+
264
+ Do not record the unpacker in the project knowledge, unless the user asks you to examine the unpacker.
265
+
266
+ ### Multi-load program
124
267
 
125
268
  1. Write a steps file. Put a `capture` step after each point where a part is in memory, for example:
126
269
 
127
- ```json
128
- [
129
- { "runUntil": { "target": { "kind": "address", "address": "$080d" }, "timeoutFrames": 6000 } },
130
- { "capture": "main" },
131
- { "type": { "mode": "text", "text": " " } },
132
- { "runUntil": { "target": { "kind": "address", "address": "$c000" }, "timeoutFrames": 6000 } },
133
- { "capture": "level1" }
134
- ]
135
- ```
136
-
137
- 2. Run `loads` with the steps file and a directory in `--out`. Add `--drive` when the program has its own drive code, for example a fast loader. Add `--exclude` for each range of a loader, an intro or a loading picture that the program does not read. The script removes these ranges from the segments of each capture.
138
- 3. Each capture writes `<out>/<name>/` with `image.bin`, `image.json`, `entry_state.json` and a PRG for each segment. It removes the segment PRGs and the `drive8.prg` of an earlier capture that the new `image.json` does not list. A segment is a block of RAM from `$0400` that the program changed after the capture before. The first capture also has the program itself.
139
- 4. Put the part that you work on into the knowledge with `image <out>/<name>/image.json` of the c64-knowledge skill.
140
- 5. With `--drive`, `drive8.prg` is the drive RAM at `$0000`-`$07ff`. Put it into the knowledge with `image <out>/<name>/drive8.prg --space drive8`. Then analyze it with c64-static-analysis and `--space drive8`.
270
+ ```json
271
+ [
272
+ { "runUntil": { "target": { "kind": "address", "address": "$080d" }, "timeoutFrames": 6000 } },
273
+ { "capture": "main" },
274
+ { "type": { "mode": "text", "text": " " } },
275
+ { "runUntil": { "target": { "kind": "address", "address": "$c000" }, "timeoutFrames": 6000 } },
276
+ { "capture": "level1" }
277
+ ]
278
+ ```
141
279
 
142
- No tool gives coverage of drive code.
280
+ 2. Run `loads` with the steps file and a folder in `--out`. Add `--drive` when the program has its own drive code, for example a fast loader. Add `--exclude` for each range of a loader, an intro or a loading picture that the program does not read.
281
+ 3. Put `<out>/<name>/image.json` of the part that you work on into the project knowledge.
282
+ 4. With `--drive`, put `<out>/<name>/drive8.prg` into the project knowledge in the space `drive8`. Then analyze the drive code in the space `drive8`.
143
283
 
144
- ## Knowledge
284
+ ---
145
285
 
146
- Automatic write:
147
- - none.
286
+ ## JSON output
148
287
 
149
- Do not record the unpacker in the project knowledge, unless the user asks to examine the unpacker.
288
+ Each command prints one JSON envelope on one line on stdout. Notes go to stderr.
150
289
 
151
- ## Result
290
+ On success, the exit code is 0:
152
291
 
153
- The packing evidence, the stages, or the unpacked program with its image, segments and entry state in a directory of the project.
292
+ ```json
293
+ {"ok": true, "command": "<command>", "body": {}, "hint": "...", "nextAction": "..."}
294
+ ```
154
295
 
155
- `trace`, `stages`, `capture` and `loads` give `result: "PASS"` or `result: "FAIL"`. A FAIL has `reason`. `capture` gives the written files in `files`, each as `{out, bytes, sha256}`.
296
+ On failure, the exit code is 2:
156
297
 
157
- `capture` gives `differsFromReset`: the count of bytes that are not as a clean C64 has them at READY.
298
+ ```json
299
+ {"ok": false, "command": "<command>", "code": "<code>", "error": "...", "hint": "...", "nextAction": "..."}
300
+ ```
158
301
 
159
- For `loads`: `captures`, with the name, the PC, the segments, `differsFromReset`, the files and the drive file of each capture. Each `entry_state.json` of `loads` has `differsFromReset` as the one of `capture` has. `keyWait: true` tells that the CPU was in the KERNAL wait for a key.
302
+ - `body` holds the result. The section of each command gives the fields of its body.
303
+ - A check gives its verdict in `body.result`: `PASS`, `FAIL` or `INCONCLUSIVE`. The exit code is 0 for each verdict. Thus read `body.result`, not the exit code.
304
+ - `error` is one sentence. It tells what is wrong.
305
+ - `hint` tells what to do or what to examine. Read it before your next step. Some envelopes have no `hint`.
306
+ - `nextAction` is a full command line. It runs from any directory. Run it when it agrees with your task. Some envelopes have no `nextAction`.
307
+ - `code` does not change between versions. Do the action for the code:
160
308
 
161
- ## Failure and conflicts
309
+ | `code` | Action |
310
+ |---|---|
311
+ | `installation-incomplete`, `machine-unavailable` | Stop the task. Give `error` and `hint` to the user and wait for the user. Do not install, start or change a tool. Do not make a replacement for the tool, for example your own disk parser, disassembler or emulator script. |
312
+ | `invalid-input`, `not-found` | Correct the call as `hint` tells. Then run the command again. |
313
+ | `operation-failed` | Give `error` and `hint` to the user. |
314
+ | Another code | Do as `hint` tells. |
162
315
 
163
- - `traced: false` or `followed: false`: the C64 did not load the program into memory in the time limit. The reason gives the load address. Make sure that the file is a PRG that the C64 can load.
164
- - `traced: false` or `followed: false` with `stopReason`: the CPU stopped before the frames ran, for example at a JAM instruction. `pc` gives the address. Make sure that `--entry` is the start of the code that the file holds at that time, and that the steps have no error.
165
- - `captured: false`: the program did not get to the address in the time limit. The reason tells where it stopped. Find a better address with the emulator skill, or give more frames. If it stopped in a key wait, give `--steps` with a `type` step.
166
- - A step did not reach its target: the reason names the step. Change the step or give it more frames.
167
- - `loaded: false` with an empty `captures` and the reason `BASIC ran no program line`: the PRG has no BASIC start line. Use `capture` with `--entry`.
168
- - `loaded: false` and no capture has a segment: the program did not load a part, or it did not start. `keyWait: true` on a capture tells that the C64 waited for a key at the READY prompt or at an INPUT statement.
169
- - Do not name a packer from weak evidence. The script never names one.
170
- - Some programs unpack in more than one stage. Run `stages` to find each stage. Capture at the entry of the last stage. Make sure that it is the application and not a second unpacker.
171
- - `not-found` or `invalid-input`: a path or an argument is wrong. Read the message.
172
- - `installation-incomplete` or `machine-unavailable`: stop the task. Give the message to the user and wait for the user. Do not install, start or change a tool yourself. Do not make a replacement for the tool, for example your own disk parser, disassembler or emulator script.
173
- - `not-implemented`: the tool does not work in this setup. Give the message to the user. Do not use that tool or step again in this task.
316
+ These fields have the same meaning in each body:
174
317
 
175
- ## Handoffs
318
+ | Field | Meaning |
319
+ |---|---|
320
+ | `out`, `bytes`, `sha256` | A file that the command wrote: its path, its size and its SHA-256. |
321
+ | `count`, `listed`, `rest` | A list. `count` is the number of items. `listed` holds the first items, at most `--max-listed`. `rest` tells how to get the other items. |
176
322
 
177
- - Unpacked machine code → c64-static-analysis
178
- - Find the start address at run time → emulator
323
+ To get the command list, run the script with no arguments. The envelope has `command: "help"`, and `body.commands` gives the name, the summary and the synopsis of each command.
324
+ To get the subjects and the flags of one command, run `<command> --help`. Then `body.subjects` and `body.options` give them.
@@ -5,6 +5,7 @@
5
5
  // knowledge space drive8. No tool guesses where a load ends: the steps decide.
6
6
 
7
7
  import { type AddressRange, COVERAGE, formatC64Address } from "#src/c64.ts";
8
+ import { Refusal } from "#src/errors.ts";
8
9
  import { autostartLoaded, LOAD_LIMIT_FRAMES, RUN_LIMIT_FRAMES, typeRun } from "#src/host-client/autostart.ts";
9
10
  import { IO_RAM, markIoRamWrites, readCoverage } from "#src/host-client/coverage.ts";
10
11
  import { ACTION_STEP_KINDS, checkActionStep, checkStepNames, isObject, runActionStep, VICE_STEP_KINDS, type ActionStep, type ActionStepKind } from "#src/host-client/steps.ts";
@@ -12,7 +13,6 @@ import { knowledgeAt } from "#src/knowledge/names.ts";
12
13
  import { WireFailure } from "#src/host-client/tools.ts";
13
14
  import { readProjectFile, writeReported, type Written } from "#src/host-client/transfer.ts";
14
15
  import { ViceSessionClient } from "#src/host-client/vice-session.ts";
15
- import { UsageError } from "#src/script.ts";
16
16
  import { entryStateFile, json, machineState, readRam, readyState, removeStaleFiles, vectorsOf } from "./machine.ts";
17
17
  import { FIRST_SEGMENT_ADDRESS } from "./stages.ts";
18
18
 
@@ -35,10 +35,10 @@ export function readLoadSteps(path: string): LoadStep[] {
35
35
  value = JSON.parse(readProjectFile(path).bytes.toString("utf8"));
36
36
  } catch (error) {
37
37
  if (error instanceof WireFailure) throw error;
38
- throw new UsageError(`--steps ${path} is not JSON: ${(error as Error).message}`);
38
+ throw new Refusal("invalid-input", `--steps ${path} is not JSON: ${(error as Error).message}`);
39
39
  }
40
40
  const fail = (message: string): never => {
41
- throw new UsageError(`--steps ${path}: ${message}`);
41
+ throw new Refusal("invalid-input", `--steps ${path}: ${message}`);
42
42
  };
43
43
  if (!Array.isArray(value) || value.length === 0) return fail("the steps are a JSON list with at least one step");
44
44
  const names = new Set<string>();