@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,107 +1,155 @@
1
1
  ---
2
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 checks each byte and the entry of the result 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.
3
+ description: Packs and crunches C64 PRG files with Exomizer into one PRG that unpacks itself and starts with RUN. It checks each byte and the entry of the result before it writes the PRG. Use it to make a small release file of a rebuilt or new program. Use it to merge more PRG files into one program. It does not assemble or run the program. It does not unpack a packed program.
4
4
  ---
5
5
 
6
- # c64-packer
6
+ # C64 packer
7
7
 
8
- ## Purpose
8
+ Use this script to pack PRG files of the project into one PRG that unpacks itself.
9
+ Use only the commands of the script. Do not pack or crunch a program in another way.
9
10
 
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
+ ```bash
12
+ node <skill>/scripts/packer.ts <command>
13
+ ```
11
14
 
12
- This skill does not assemble the program, does not run it in the emulator and 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 `pack build/game.prg --out build/game-packed.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
- - 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.
22
+ ## Select the command
20
23
 
21
- ## Tools
24
+ | Goal | Command |
25
+ |---|---|
26
+ | Pack a BASIC program at `$0801` that starts with `SYS` | `pack build/game.prg --out build/game-packed.prg` |
27
+ | Pack a program that starts at a known address | `pack build/main.prg --entry '$0400' --out build/release.prg` |
28
+ | Merge more PRG files into one packed program | `pack build/loader.prg build/main.prg --entry '$0400' --out build/release.prg` |
29
+ | Start the program with RAM in place of the BASIC and KERNAL ROMs | `pack build/game.prg --exit-memory '$35' --out build/game-packed.prg` |
22
30
 
23
- ```
24
- node <skill>/scripts/packer.ts pack build/game.prg --out build/game-packed.prg
25
- node <skill>/scripts/packer.ts pack build/loader.prg build/main.prg --entry '$0400' --out build/release.prg
26
- ```
31
+ ---
27
32
 
28
- | Option | Meaning |
29
- |---|---|
30
- | `--entry '$0400'` | The address where the program starts after the unpack. It must be `$0400` or higher. It can also be a name of the knowledge. Without it, the script uses the `SYS` address of the BASIC line of the first input. |
31
- | `--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. |
33
+ ## Inputs and the entry
34
+
35
+ - `<input.prg>` is a PRG file of the project. Give more files in load order to merge them into one program. Where two files overlap, the later file wins.
36
+ - The packed PRG loads at `$0801` and starts with RUN. It unpacks the program and jumps to the entry.
37
+ - The entry is the address where the program starts after the unpack. When the first input is a BASIC program at `$0801` with `SYS` and a number, the script uses that address. Else, give `--entry` with the start address of the program, for example the entry of the program in the project knowledge.
38
+ - `--exit-memory` is the value of `$01` when the program starts. Give it when the program uses RAM under BASIC or KERNAL and does not set `$01` itself.
32
39
 
33
- ### Calling the script
40
+ ### Paths and the project folder
34
41
 
35
- - Call a script as `node <skill>/scripts/<script>.ts <command> [subjects] [options]`. The command comes first.
36
- - `<script>.ts --help` lists the commands. `<script>.ts <command> --help` gives the subjects and the options of one command. The help is JSON: `{"text": "..."}`.
37
- - 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.
42
+ - 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/`.
43
+ - 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.
44
+ - 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.
38
45
  - A range is `<start>-<end>`, for example `'$c000-$cfff'`. In a JSON file, a range is `start` and `end`.
39
46
  - Bytes in a JSON file are hex in a field `data`, for example `"data": "a900"`.
40
- - 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.
41
- - 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.
42
- - An error is `{"error": {"code": "...", "message": "..."}}`. The message tells what is wrong and what to do.
43
- - The exit status is 0 for done or PASS, 1 for FAIL, 2 for an error and 3 for INCONCLUSIVE.
44
-
45
- ### Project folder
46
-
47
- - 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/`.
48
- - 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.
49
47
  - 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.
50
- - 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/`.
51
- - 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.
52
- - 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.
48
+ - 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.
49
+ - The script refuses a folder where the command writes a file. It refuses a file where the command writes a folder.
50
+ - 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.
51
+ - 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.
52
+ - 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.
53
53
  - 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`.
54
54
 
55
- ## Workflow
56
-
57
- 1. Assemble the program with the c64-assembler skill. Use the release or the switches that you want in the packed file.
58
- 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.
59
- 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.
60
- 4. Run the script.
61
- 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.
62
- 6. If `result` is `FAIL`, read `check`. Do not use the packed program. The script does not write it.
63
- 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`.
55
+ ---
64
56
 
65
- 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.
57
+ ## Flags
66
58
 
67
- The unpacker uses $0334 to $03ff and the zero page while it runs. A program that loads into these addresses can fail. Then the script gives `operation-failed`, or the check gives `FAIL`.
59
+ Each flag applies only to the commands in the table. On another command, the script refuses the flag.
68
60
 
69
- ## Knowledge
61
+ | Flag | Command | Effect |
62
+ |---|---|---|
63
+ | `--out <file.prg>` | `pack` | Write the packed program to this file. `pack` needs this flag. The file must not be an input. |
64
+ | `--entry <address>` | `pack` | Jump to this address after the unpack. It must be `$0400` or higher. The default is the `SYS` address of the BASIC line of the first input. |
65
+ | `--exit-memory <value>` | `pack` | Set `$01` to this value, `$34` to `$37`, when the program starts. With it, the program starts with interrupts off. The default is `$37`: BASIC, KERNAL and I/O. |
70
66
 
71
- Automatic write:
72
- - none.
67
+ ---
73
68
 
74
- Do not store:
75
- - the packed program
76
- - the check result
69
+ ## Pack
77
70
 
78
- ## Result
71
+ ### `pack <input.prg>...`
79
72
 
80
- `result` is `PASS`, `FAIL` or `INCONCLUSIVE`.
73
+ `pack` packs the inputs into one program, and unpacks the result again to check it. It writes the packed program only when the check finds no difference.
81
74
 
82
- - `PASS`: the packed program unpacks to the bytes of all inputs and jumps to the entry. The PRG is at `out`.
83
- - `FAIL`: the packed program unpacks to different bytes, or jumps to a different address. The script did not write a file.
84
- - `INCONCLUSIVE`: the check has 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`.
75
+ `body` holds:
85
76
 
86
- Other fields:
77
+ - `result`:
78
+ - `PASS`: the packed program unpacks to the bytes of all inputs and jumps to the entry. The PRG is at `out`.
79
+ - `FAIL`: the packed program unpacks to different bytes, or jumps to a different address. The script did not write a file.
80
+ - `INCONCLUSIVE`: the check has 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`.
81
+ - `out`, `bytes` and `sha256`: the packed PRG. A `FAIL` has no `out`.
87
82
  - `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.
83
+ - `entry` and `exitMemory`: the entry and the value of `$01` of the call.
88
84
  - `unpackSeconds`: the time of the unpack, in seconds at the PAL clock. A real C64 can need more time.
89
- - `inputs`: each input with its `loadRange` and its `bytes`.
85
+ - `inputs`: each input with its `file`, `loadRange` and `bytes`.
90
86
  - `check.entry`: the address where the unpacker jumped.
91
- - `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`).
87
+ - `check.segments`: one check for each segment of the merged inputs. The `result` of a segment is `identical`, `different` or `not covered`. A `different` segment has `differences` and `firstDifference`. `firstDifference` gives the `address`, the unpacked byte (`built`) and the input byte (`reference`).
88
+
89
+ `hint` gives the action for the result:
92
90
 
93
- ## Failure and conflicts
91
+ - `PASS`: run the packed program in the emulator with RUN. Make sure that the program starts and works.
92
+ - `INCONCLUSIVE`: run the packed program in the emulator. Examine the memory of each segment that is `not covered`.
93
+ - `FAIL`: do not use the packed program. Give the call and `check` to the user.
94
94
 
95
- - `FAIL`: do not use the packed program. Read `check`. Give the call and the result to the user.
96
- - `invalid-input` with `Give --entry`: the first input has no BASIC line with `SYS` and a number. Give `--entry`.
97
- - `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.
98
- - `operation-failed` with `Exomizer could not pack the program`: read the last output of Exomizer in the message. Change the inputs, or give the message to the user.
99
- - `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.
100
- - `not-found`: there is no file at an input path. Give a path relative to the project folder.
95
+ Traps:
101
96
 
102
- ## Handoffs
97
+ - The unpacker starts 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.
98
+ - The unpacker uses `$0334` to `$03ff` and the zero page while it runs. A program that loads into these addresses can fail. Then the command fails with `operation-failed`, or the check gives `FAIL`.
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
+ - `invalid-input` with "There is no SYS address": the first input has no BASIC line with `SYS` and a number. Give `--entry`.
101
+ - `operation-failed` with "Exomizer could not pack the program": `hint` gives the last output of Exomizer. Change the inputs, or give the message to the user.
102
+
103
+ ---
104
+
105
+ ## Workflow
106
+
107
+ 1. Assemble the program. Use the release or the switches that you want in the packed file.
108
+ 2. Find the entry and the value of `$01`. See the section "Inputs and the entry".
109
+ 3. Run `pack`.
110
+ 4. Do the action that `hint` gives for `body.result`.
111
+ 5. To release the program on a disk, write the packed PRG to a disk image.
112
+
113
+ The packed program and the check result do not go into the project knowledge.
114
+
115
+ ---
116
+
117
+ ## JSON output
118
+
119
+ Each command prints one JSON envelope on one line on stdout. Notes go to stderr.
120
+
121
+ On success, the exit code is 0:
122
+
123
+ ```json
124
+ {"ok": true, "command": "<command>", "body": {}, "hint": "...", "nextAction": "..."}
125
+ ```
126
+
127
+ On failure, the exit code is 2:
128
+
129
+ ```json
130
+ {"ok": false, "command": "<command>", "code": "<code>", "error": "...", "hint": "...", "nextAction": "..."}
131
+ ```
132
+
133
+ - `body` holds the result. The section of each command gives the fields of its body.
134
+ - 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.
135
+ - `error` is one sentence. It tells what is wrong.
136
+ - `hint` tells what to do or what to examine. Read it before your next step. Some envelopes have no `hint`.
137
+ - `nextAction` is a full command line. It runs from any directory. Run it when it agrees with your task. Some envelopes have no `nextAction`.
138
+ - `code` does not change between versions. Do the action for the code:
139
+
140
+ | `code` | Action |
141
+ |---|---|
142
+ | `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. |
143
+ | `invalid-input`, `not-found` | Correct the call as `hint` tells. Then run the command again. |
144
+ | `operation-failed` | Give `error` and `hint` to the user. |
145
+ | Another code | Do as `hint` tells. |
146
+
147
+ These fields have the same meaning in each body:
148
+
149
+ | Field | Meaning |
150
+ |---|---|
151
+ | `out`, `bytes`, `sha256` | A file that the command wrote: its path, its size and its SHA-256. |
152
+ | `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. |
103
153
 
104
- - Assemble the program before the pack → c64-assembler
105
- - Run the packed program → emulator
106
- - Put the packed program on a disk image → c64-disk
107
- - Unpack a packed program → c64-unpacker
154
+ 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.
155
+ To get the subjects and the flags of one command, run `<command> --help`. Then `body.subjects` and `body.options` give them.
@@ -12,7 +12,7 @@ import { readProjectFile, writeReported } from "#src/host-client/transfer.ts";
12
12
  import { knowledgeNames } from "#src/knowledge/names.ts";
13
13
  import { pack } from "#src/native/exomizer.ts";
14
14
  import { localToolContext } from "#src/native/local.ts";
15
- import { OPTIONS, runScript, UsageError } from "#src/script.ts";
15
+ import { OPTIONS, reply, runScript, UsageError } from "#src/script.ts";
16
16
 
17
17
  /** CPU cycles in one second of a PAL C64. */
18
18
  const PAL_CYCLES_PER_SECOND = 985_248;
@@ -64,7 +64,7 @@ await runScript({
64
64
  let entry = call.address("entry");
65
65
  if (entry === undefined) {
66
66
  const found = sysEntry(programs[0]!.bytes);
67
- if (typeof found === "string") throw new UsageError(`Give --entry: there is no SYS address in ${files[0]}. ${found}`);
67
+ if (typeof found === "string") throw new UsageError(`There is no SYS address in ${files[0]}. ${found} Give --entry with the start address of the program.`);
68
68
  entry = found;
69
69
  }
70
70
  const exitMemory = call.count("exit-memory");
@@ -76,7 +76,7 @@ await runScript({
76
76
  const different = !entryReached || check.segments.some((segment) => segment.result === "different");
77
77
  const result = different ? "FAIL" : check.identical ? "PASS" : "INCONCLUSIVE";
78
78
  const unpackedBytes = reference.segments.reduce((sum, segment) => sum + segment.end - segment.start + 1, 0);
79
- return {
79
+ const body = {
80
80
  result,
81
81
  // A program that does not unpack to its inputs is not written.
82
82
  ...(different ? {} : writeReported(out, packed.program, { what: "--out" })),
@@ -89,6 +89,13 @@ await runScript({
89
89
  inputs: programs.map((program) => ({ file: program.file, loadRange: { start: formatC64Address(program.start), end: formatC64Address(program.end) }, bytes: program.body.length })),
90
90
  check: { entry: formatC64Address(packed.entry), ...check },
91
91
  };
92
+ const hint =
93
+ result === "PASS"
94
+ ? `Run ${out} in the emulator with RUN. Make sure that the program starts and works.`
95
+ : result === "INCONCLUSIVE"
96
+ ? `The check does not hold all bytes of the inputs. Run ${out} in the emulator, and examine the memory of each segment in check.segments that is not covered.`
97
+ : "The packed program does not unpack to the inputs, or it jumps to a different entry. The script did not write it. Do not use it. Give the call and check to the user.";
98
+ return reply(body, { hint });
92
99
  },
93
100
  },
94
101
  ],