@henols/c64-re-tools 2.4.0-rc.2 → 2.4.0-rc.4

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 (50) hide show
  1. package/package.json +1 -1
  2. package/skills/c64-assembler/SKILL.md +8 -3
  3. package/skills/c64-assembler/scripts/assembler.ts +6 -16
  4. package/skills/c64-basic/SKILL.md +2 -0
  5. package/skills/c64-disk/SKILL.md +21 -4
  6. package/skills/c64-disk/scripts/disk.ts +43 -3
  7. package/skills/c64-docs/SKILL.md +2 -0
  8. package/skills/c64-docs/scripts/docs.ts +6 -6
  9. package/skills/c64-knowledge/SKILL.md +2 -0
  10. package/skills/c64-memory-map/SKILL.md +2 -0
  11. package/skills/c64-packer/SKILL.md +2 -0
  12. package/skills/c64-provenance/SKILL.md +11 -4
  13. package/skills/c64-provenance/scripts/lib/library.ts +99 -19
  14. package/skills/c64-provenance/scripts/provenance.ts +4 -3
  15. package/skills/c64-reverse-engineering/SKILL.md +3 -3
  16. package/skills/c64-source/SKILL.md +10 -2
  17. package/skills/c64-source/scripts/lib/export.ts +3 -3
  18. package/skills/c64-source/scripts/lib/source.ts +80 -24
  19. package/skills/c64-source/scripts/source.ts +49 -8
  20. package/skills/c64-static-analysis/SKILL.md +3 -1
  21. package/skills/c64-testing/SKILL.md +13 -1
  22. package/skills/c64-testing/references/recording-format.md +5 -1
  23. package/skills/c64-testing/references/scenario-format.md +5 -3
  24. package/skills/c64-testing/scripts/lib/recording.ts +32 -6
  25. package/skills/c64-testing/scripts/lib/runs.ts +13 -7
  26. package/skills/c64-testing/scripts/lib/scenario.ts +13 -6
  27. package/skills/c64-testing/scripts/testing.ts +6 -3
  28. package/skills/c64-unpacker/SKILL.md +2 -0
  29. package/src/host/server.ts +31 -6
  30. package/src/host/tools/c1541.ts +32 -8
  31. package/src/host/vice/binary-monitor.ts +19 -7
  32. package/src/host/vice/session.ts +6 -2
  33. package/src/host-client/connect.ts +12 -2
  34. package/src/host-client/tools.ts +9 -4
  35. package/src/host-client/transfer.ts +58 -4
  36. package/src/host-client/vice-session.ts +10 -3
  37. package/src/knowledge/import.ts +28 -5
  38. package/src/knowledge/write.ts +4 -0
  39. package/src/mcp/main.ts +4 -66
  40. package/src/mcp/session-source.ts +76 -0
  41. package/src/native/acme.ts +45 -12
  42. package/src/native/c64run.ts +1 -1
  43. package/src/native/types.ts +5 -2
  44. package/src/project.ts +58 -1
  45. package/src/protocol/framing.ts +15 -2
  46. package/src/protocol/messages.ts +77 -25
  47. package/src/protocol/tools.ts +33 -4
  48. package/src/protocol/vice.ts +40 -20
  49. package/src/script.ts +11 -6
  50. package/tools/c64run.c +48 -12
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@henols/c64-re-tools",
3
- "version": "2.4.0-rc.2",
3
+ "version": "2.4.0-rc.4",
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",
@@ -25,6 +25,7 @@ To get the command list, run the script with no arguments.
25
25
  |---|---|
26
26
  | Build the program from the source | `build src main.a --out build/game.prg` |
27
27
  | Build a source that includes files from a folder | `build src main.a --out build/game.prg --include lib` |
28
+ | Build a source that includes a file that you generated into a folder next to the source root | `build src boot.asm --out build/boot.prg --include ../build` |
28
29
  | Build a source that sets no start address | `build src main.a --out build/game.prg --set-pc '$0801'` |
29
30
  | Check that the rebuild is identical to the program in the project knowledge | `build src main.asm --out build/game.prg --compare` |
30
31
  | Build a release and check it against the file of that release | `build src main.asm --out build/original.prg --release ORIGINAL --compare` |
@@ -37,7 +38,8 @@ To get the command list, run the script with no arguments.
37
38
 
38
39
  ## The source and the program
39
40
 
40
- - `<source-root>` is the folder in the project that holds all the source files of the program. Put each file that the program includes under the source root. The script cannot read a file outside it.
41
+ - `<source-root>` is the folder in the project that holds the source files of the program. The script reads only the files in the source root and in the include folders.
42
+ - An include folder is a path relative to the source root, inside the project folder. It can be outside the source root, for example `--include ../build`.
41
43
  - `<entry>` is the main source file, relative to the source root. It normally sets its start address, for example `* = $0801`. If it does not, give `--set-pc`.
42
44
  - For a source that you generated from the project knowledge, the source root is the folder of that source. The entry is `main.asm`.
43
45
  - `--out` is the path of the program, relative to the project folder.
@@ -57,6 +59,8 @@ A source that you generated from the project knowledge has switches in `config.a
57
59
 
58
60
  - 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/`.
59
61
  - 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.
62
+ - In a group folder, each version of a program is a project. The script refuses the group folder and each folder in it that is not a project. Give `--project` with a project of the group, for example `--project v1`.
63
+ - A path that a command only reads can go into another project of the group, for example `../v1/original/game.prg`. The command writes only in its own project.
60
64
  - 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.
61
65
  - A range is `<start>-<end>`, for example `'$c000-$cfff'`. In a JSON file, a range is `start` and `end`.
62
66
  - Bytes in a JSON file are hex in a field `data`, for example `"data": "a900"`.
@@ -77,7 +81,7 @@ Each flag applies only to the commands in the table. On another command, the scr
77
81
  | Flag | Command | Effect |
78
82
  |---|---|---|
79
83
  | `--out <file.prg>` | `build` | Required. The path of the program, relative to the project folder. |
80
- | `--include <dir>` | `build` | An include folder, relative to the source root. Give the flag again for each folder. The default is no include folder. |
84
+ | `--include <dir>` | `build` | An include folder: a path relative to the source root, inside the project folder. Give the flag again for each folder. The default is no include folder. |
81
85
  | `--define NAME=VALUE` | `build` | An assembler symbol: an integer, also `$` and hex digits, or `true` or `false`. Give the flag again for each symbol. Do not use it for a switch. |
82
86
  | `--set-pc <address>` | `build` | The start address when the source does not set one, for example `'$0801'`. |
83
87
  | `--release <label>` | `build` | Build a release of the project knowledge. Each switch gets the value of that release. With `--compare`, the reference is the file of the release. |
@@ -109,7 +113,7 @@ Assembles the source and writes the program to `--out`.
109
113
  - `used` is `false` when the source does not use the symbol.
110
114
  - `symbolsFile`: the file of `--out-symbols`, with `out`, `bytes` and `sha256`.
111
115
  - `unusedConstants`: the count of the constants that the list does not include. It is not in the body with `--all-symbols`.
112
- - `diagnostics`: each item has `severity` (`error`, `warning` or `note`), `file`, `line` and `message`. `file` and `line` are not in an item without a location in the source.
116
+ - `diagnostics`: each item has `severity` (`error`, `warning` or `note`), `file`, `line` and `message`. `file` is relative to the source root. A file in an include folder outside the source root starts with `../`. `file` and `line` are not in an item without a location in the source.
113
117
  - `compare`: only with `--compare`. `identical` is `true` when each segment is identical. `segments` has one item for each segment of the reference program, with `start`, `end` and `result`:
114
118
  - `identical`.
115
119
  - `different`, with `differences` and `firstDifference`. `firstDifference` gives `address`, and the byte of the build (`built`) and of the reference (`reference`), each as `$` and two hex digits.
@@ -123,6 +127,7 @@ Do as the result tells:
123
127
  - `hint` tells that `symbols` is cut: give a larger `--max-listed`, or `--out-symbols <file.json>` for the full list.
124
128
  - Error `not-found` names the entry: give the main source file relative to the source root.
125
129
  - Error `not-found` names an include folder: give `--include` a folder with files, relative to the source root.
130
+ - Error `invalid-input` names an include folder: the folder is outside the project folder. Give `--include` a folder inside the project folder.
126
131
  - Error `not-found` names a release: the project knowledge has no release with that label. Record the release in the project knowledge, or give the label of a recorded release.
127
132
  - Error `not-found` tells that the knowledge has no program: put the program bytes into the project knowledge. Then use `--compare` again.
128
133
  - Error `invalid-input`: an option or a path is wrong. Do as `hint` tells. The script refuses `--space` without `--compare` or `--release`.
@@ -1,12 +1,10 @@
1
1
  // The c64-assembler script: assembles project source with ACME, which it runs
2
2
  // itself, and writes the program into the project.
3
3
 
4
- import { posix } from "node:path";
5
-
6
4
  import { formatC64Address } from "#src/c64.ts";
7
5
  import { Refusal } from "#src/errors.ts";
8
6
  import { readImage } from "#src/host-client/image.ts";
9
- import { readProjectTree, writeReported } from "#src/host-client/transfer.ts";
7
+ import { readSourceTree, writeReported } from "#src/host-client/transfer.ts";
10
8
  import { openForRead } from "#src/knowledge/database.ts";
11
9
  import { knowledgeNames } from "#src/knowledge/names.ts";
12
10
  import { requireProgram } from "#src/knowledge/program.ts";
@@ -42,7 +40,7 @@ await runScript({
42
40
  ],
43
41
  options: {
44
42
  out: { ...OPTIONS.out, output: "file", value: "<file.prg>", help: "where to write the program, relative to the project folder", required: true },
45
- include: { kind: "text", value: "<dir>", multiple: true, help: "an include folder, relative to the source root" },
43
+ include: { kind: "text", value: "<dir>", multiple: true, help: "an include folder: a path relative to the source root, inside the project folder" },
46
44
  define: { kind: "text", value: "NAME=VALUE", multiple: true, help: "an assembler symbol: an integer ($hex allowed) or true/false" },
47
45
  "set-pc": { kind: "address", value: "<address>", help: "the start address when the source sets none, for example '$0801'" },
48
46
  release: { kind: "text", value: "<label>", help: `build a release of the knowledge: defines ${RELEASE_DEFINE}<label>=1, so each switch has the value of that release. With --compare, the reference is the file of the release` },
@@ -77,19 +75,11 @@ await runScript({
77
75
  }
78
76
  }
79
77
  const setPc = call.address("set-pc");
80
- const tree = readProjectTree(sourceRoot);
81
- if (!tree.files.some((file) => file.path === entry)) throw new Refusal("not-found", `${entry} is not a file in ${sourceRoot}. Give the main source file relative to the source root.`);
82
- // ACME sees only the files of the source root: an include folder holds some of them. A path out of the root is refused later.
83
- for (const folder of call.strings("include")) {
84
- const inside = posix.normalize(folder.replaceAll("\\", "/")).replace(/\/+$/, "");
85
- if (inside === "." || inside === ".." || inside.startsWith("../") || posix.isAbsolute(inside)) continue;
86
- if (!tree.files.some((file) => file.path.startsWith(`${inside}/`))) {
87
- throw new Refusal("not-found", `There is no include folder ${folder} with files in the source root ${sourceRoot}. Give --include a folder relative to the source root.`);
88
- }
89
- }
90
- const params: AcmeParams = { files: tree.files, entrySource: entry, includeDirs: call.strings("include"), defines: { ...releaseDefine, ...Object.fromEntries(call.strings("define").map(parseDefine)) }, ...(setPc === undefined ? {} : { setPc }) };
78
+ const { contents, ...tree } = readSourceTree(sourceRoot, call.strings("include"));
79
+ if (!tree.files.some((file) => file.path === (tree.sourceDir === "" ? entry : `${tree.sourceDir}/${entry}`))) throw new Refusal("not-found", `${entry} is not a file in ${sourceRoot}. Give the main source file relative to the source root.`);
80
+ const params: AcmeParams = { ...tree, entrySource: entry, defines: { ...releaseDefine, ...Object.fromEntries(call.strings("define").map(parseDefine)) }, ...(setPc === undefined ? {} : { setPc }) };
91
81
 
92
- const { result, attachments } = await assemble(params, tree.contents, localToolContext());
82
+ const { result, attachments } = await assemble(params, contents, localToolContext());
93
83
  if (!result.assembled) return reply({ result: "FAIL", assembled: false, diagnostics: result.diagnostics }, { hint: "The source has errors, and no program was written. Correct the source at the file and the line of each diagnostic. Then run build again." });
94
84
  const plain = call.flag("plain");
95
85
  const written = writeReported(out, plain ? attachments![0]!.subarray(2) : attachments![0]!, { what: "--out" });
@@ -35,6 +35,8 @@ To get the command list, run the script with no arguments.
35
35
 
36
36
  - 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/`.
37
37
  - 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.
38
+ - In a group folder, each version of a program is a project. The script refuses the group folder and each folder in it that is not a project. Give `--project` with a project of the group, for example `--project v1`.
39
+ - A path that a command only reads can go into another project of the group, for example `../v1/original/game.prg`. The command writes only in its own project.
38
40
  - 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.
39
41
  - A range is `<start>-<end>`, for example `'$c000-$cfff'`. In a JSON file, a range is `start` and `end`.
40
42
  - Bytes in a JSON file are hex in a field `data`, for example `"data": "a900"`.
@@ -1,6 +1,6 @@
1
1
  ---
2
2
  name: c64-disk
3
- description: Examines a C64 disk image (D64, D71, D81 or G64), a T64 tape file or a CRT cartridge file. It extracts files from it into the project. It shows the directory, the BAM, the directory entry of a file and the sector chain of a file. It writes each bank of a cartridge as a PRG. Use it when a program comes as a disk image, a tape file or a cartridge file. Use it to find the sectors that a loader reads directly. It does not run the program, it does not decode BASIC, and it does not change the disk image.
3
+ description: Examines a C64 disk image (D64, D71, D81 or G64), a T64 tape file or a CRT cartridge file. It extracts files from it into the project. It shows the directory, the BAM, the directory entry of a file and the sector chain of a file. It writes the bytes of sectors to a file. It writes each bank of a cartridge as a PRG. Use it when a program comes as a disk image, a tape file or a cartridge file. Use it to find the sectors that a loader reads directly. It does not run the program, it does not decode BASIC, and it does not change the disk image.
4
4
  ---
5
5
 
6
6
  # C64 disk
@@ -28,6 +28,8 @@ To get the command list, run the script with no arguments.
28
28
  | Get the type, the size and the first block of one file | `entry original/game.d64 GAME` |
29
29
  | Get the track and the sector of each block of one file | `chain original/game.d64 GAME` |
30
30
  | Find the used sectors that no file uses | `bam original/game.d64` |
31
+ | Write the bytes of the used sectors that no file uses | `sectors original/game.d64 --unreferenced --out .c64-re-tools/local/hidden.bin` |
32
+ | Write the bytes of some sectors | `sectors original/game.d64 18/0 20/5 --out .c64-re-tools/local/sectors.bin` |
31
33
  | Get the files of a tape | `directory original/game.t64` |
32
34
  | Extract one file of a tape | `read original/game.t64 GAME --out extracted/game.prg` |
33
35
  | Get the banks of a cartridge | `directory original/game.crt` |
@@ -41,7 +43,7 @@ To get the command list, run the script with no arguments.
41
43
  |---|---|
42
44
  | `directory` | `.d64`, `.d71`, `.d81`, `.g64`, `.t64`, `.crt` |
43
45
  | `read` | `.d64`, `.d71`, `.d81`, `.g64`, `.t64` |
44
- | `bam`, `entry`, `chain` | `.d64`, `.d71`, `.d81`, `.g64` |
46
+ | `bam`, `entry`, `chain`, `sectors` | `.d64`, `.d71`, `.d81`, `.g64` |
45
47
  | `crt` | `.crt` |
46
48
 
47
49
  - `<name>` is a file name from the result of `directory`. Write the name in the same form as the directory shows it. The letter case of a name is not important.
@@ -59,6 +61,8 @@ To get the command list, run the script with no arguments.
59
61
 
60
62
  - 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/`.
61
63
  - 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.
64
+ - In a group folder, each version of a program is a project. The script refuses the group folder and each folder in it that is not a project. Give `--project` with a project of the group, for example `--project v1`.
65
+ - A path that a command only reads can go into another project of the group, for example `../v1/original/game.prg`. The command writes only in its own project.
62
66
  - 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.
63
67
  - A range is `<start>-<end>`, for example `'$c000-$cfff'`. In a JSON file, a range is `start` and `end`.
64
68
  - Bytes in a JSON file are hex in a field `data`, for example `"data": "a900"`.
@@ -76,7 +80,8 @@ Each flag applies only to the commands in the table. On another command, the scr
76
80
 
77
81
  | Flag | Command | Effect |
78
82
  |---|---|---|
79
- | `--out <file>` | `read` | The file to write, relative to the project folder. Required. |
83
+ | `--out <file>` | `read`, `sectors` | The file to write, relative to the project folder. Required. |
84
+ | `--unreferenced` | `sectors` | Write also the used sectors that no file uses, after the given sectors. |
80
85
  | `--out <dir>` | `crt` | The folder for the bank files and `crt.json`, relative to the project folder. Required. |
81
86
 
82
87
  ---
@@ -99,6 +104,8 @@ Do not record the directory list in the project knowledge.
99
104
 
100
105
  `body.tracks` gives the `freeSectors` and the `usedSectors` of each track. `body.unreferenced` lists the used sectors that no file uses. The list does not include the header, the directory and the BAM blocks. These sectors can hold data that a loader reads directly.
101
106
 
107
+ When `body.unreferenced` is not empty, `nextAction` is the `sectors` command that writes their bytes.
108
+
102
109
  ### `entry <image> <name>`
103
110
 
104
111
  `body` holds `found: true` and `entry`: the `name`, `type`, `blocks`, `closed`, `locked`, `startTrack` and `startSector` of the file. Use it when the disk structure is important, for example for a loader that reads sectors directly.
@@ -119,6 +126,16 @@ Writes the bytes of one file at `--out`. A PRG file keeps its 2-byte load addres
119
126
 
120
127
  For a cartridge file, the script refuses `read`. Use `crt`.
121
128
 
129
+ ### `sectors <image> [<track>/<sector>...]`
130
+
131
+ Writes the 256 bytes of each sector at `--out`, in the given sequence. Give each sector as `<track>/<sector>` in decimal, for example `18/1`. With `--unreferenced`, the file also gets the used sectors that no file uses, after the given sectors. Give at least one sector or `--unreferenced`.
132
+
133
+ `body.count` is the number of sectors. `body.sectors` gives the `track`, the `sector` and the `offset` of each sector in the file. `body` also holds the written file as `out`, `bytes` and `sha256`.
134
+
135
+ When the disk has no used sectors that no file uses and you give no sectors, the script writes no file. Then `body.count` is 0, and `hint` tells it.
136
+
137
+ - `invalid-input` with "The disk has no track": the disk does not have that sector. Run `bam` and give a sector that it shows.
138
+
122
139
  ### `crt <file.crt>`
123
140
 
124
141
  Writes one PRG for each CHIP packet of the cartridge, for example `bank3-8000.prg`. It also writes `crt.json` with the header and the packets. The script reads hardware types 0 (normal 8K, 16K or Ultimax), 5 (Ocean), 19 (Magic Desk) and 32 (EasyFlash).
@@ -134,7 +151,7 @@ All banks use the same addresses. Thus `hint` tells you to analyze each bank PRG
134
151
  1. Run `directory`. Find the file in `entries`.
135
152
  2. Run `read` to extract the file. For a cartridge, run `crt`.
136
153
  3. Run `entry` or `chain` only when the disk structure is important.
137
- 4. Run `bam` only to find the used sectors that no file uses.
154
+ 4. Run `bam` only to find the used sectors that no file uses. Then run `sectors --unreferenced` to examine their bytes.
138
155
  5. Give the extracted file to the next task. Decode a BASIC program, unpack a packed program, or disassemble machine code.
139
156
 
140
157
  ---
@@ -4,7 +4,7 @@
4
4
 
5
5
  import { formatC64Address } from "#src/c64.ts";
6
6
  import { isRefusal, Refusal } from "#src/errors.ts";
7
- import { DISK_IMAGE_TYPES, inspectDisk } from "#src/host-client/tools.ts";
7
+ import { DISK_BLOCK_BYTES, DISK_IMAGE_TYPES, inspectDisk, MAX_DISK_SECTORS } from "#src/host-client/tools.ts";
8
8
  import { readProjectFile, writeReported } from "#src/host-client/transfer.ts";
9
9
  import { OPTIONS, reply, runScript, type Call, type SubjectSpec } from "#src/script.ts";
10
10
  import { parseCrt, parseT64, tapeFile } from "./lib/containers.ts";
@@ -75,6 +75,16 @@ function cartridge(image: string) {
75
75
  /** A command that takes only a disk image. */
76
76
  const diskOnly = (call: Call, action: string) => kindFor(action, call.subject(0), ["disk"]);
77
77
 
78
+ /** A sector as <track>/<sector> in decimal, for example 18/1. */
79
+ function sectorOf(text: string): { track: number; sector: number } {
80
+ const match = /^(\d{1,3})\/(\d{1,3})$/.exec(text);
81
+ const [track, sector] = [Number(match?.[1]), Number(match?.[2])];
82
+ if (match === null || track < 1 || track > 255 || sector > 255) {
83
+ throw new Refusal("invalid-input", `${text} is not a sector. Give <track>/<sector> in decimal, for example 18/1.`);
84
+ }
85
+ return { track, sector };
86
+ }
87
+
78
88
  await runScript({
79
89
  name: "disk",
80
90
  summary: "Inspects a disk image and extracts files into the project. It also reads T64 tape files and CRT cartridge files. A CRT can have hardware type 0 (normal 8K, 16K or Ultimax), 5 (Ocean), 19 (Magic Desk) or 32 (EasyFlash).",
@@ -101,9 +111,16 @@ await runScript({
101
111
  name: "bam",
102
112
  summary: "The free and used sectors of each track, and the used sectors that no file holds.",
103
113
  subjects: [IMAGE],
104
- run: (call) => {
114
+ run: async (call) => {
105
115
  diskOnly(call, "bam");
106
- return inspect(call, "bam");
116
+ const image = call.subject(0);
117
+ const output = (await inspect(call, "bam")) as { unreferenced: unknown[] };
118
+ if (output.unreferenced.length === 0) return output;
119
+ const base = image.split("/").at(-1)!.replace(/\.[^.]*$/, "");
120
+ return reply(output, {
121
+ hint: "Write the unreferenced sectors to a file to examine their bytes.",
122
+ nextAction: call.next("sectors", image, "--unreferenced", "--out", `.c64-re-tools/local/${base}-unreferenced.bin`),
123
+ });
107
124
  },
108
125
  },
109
126
  {
@@ -146,6 +163,29 @@ await runScript({
146
163
  return { name: result.name, ...writeReported(out, result.data!, { what: "--out" }) };
147
164
  },
148
165
  },
166
+ {
167
+ name: "sectors",
168
+ summary: "Writes the 256 bytes of each given sector into one file, in the given order. It can also write the used sectors that no file holds.",
169
+ subjects: [IMAGE, { name: "<track>/<sector>", help: "a sector in decimal, for example 18/1. Give more sectors to write them in that order", optional: true, list: true }],
170
+ options: {
171
+ unreferenced: { kind: "flag", help: "write also the used sectors that no file holds, as bam lists them, after the given sectors" },
172
+ out: { ...OPTIONS.out, output: "file", value: "<file>", help: "the file to write, relative to the project folder", required: true },
173
+ },
174
+ run: async (call) => {
175
+ diskOnly(call, "sectors");
176
+ const image = call.subject(0);
177
+ const out = call.string("out")!;
178
+ const sectors = call.rest(1).map(sectorOf);
179
+ const unreferenced = call.flag("unreferenced");
180
+ if (sectors.length === 0 && !unreferenced) throw new Refusal("invalid-input", "sectors needs a sector or --unreferenced. Give <track>/<sector>, for example 18/1, or --unreferenced.");
181
+ if (sectors.length > MAX_DISK_SECTORS) throw new Refusal("limit-exceeded", `${sectors.length} sectors are too many. Give at most ${MAX_DISK_SECTORS} sectors.`);
182
+ const result = await inspectDisk({ image, action: "sectors", sectors, ...(unreferenced ? { unreferenced } : {}) });
183
+ if (result.action !== "sectors") throw new Refusal("operation-failed", "The disk inspection gave no sectors.");
184
+ const index = result.sectors.map((at, position) => ({ track: at.track, sector: at.sector, offset: position * DISK_BLOCK_BYTES }));
185
+ if (index.length === 0) return reply({ count: 0, sectors: [] }, { hint: "The disk has no used sectors that no file holds. The script wrote no file." });
186
+ return { count: index.length, sectors: index, ...writeReported(out, result.data!, { what: "--out" }) };
187
+ },
188
+ },
149
189
  {
150
190
  name: "crt",
151
191
  summary: "Writes each CHIP packet of a cartridge as a PRG, and crt.json with the header and the packets.",
@@ -50,6 +50,8 @@ Get the values for the commands from these places:
50
50
 
51
51
  - 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/`.
52
52
  - 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.
53
+ - In a group folder, each version of a program is a project. The script refuses the group folder and each folder in it that is not a project. Give `--project` with a project of the group, for example `--project v1`.
54
+ - A path that a command only reads can go into another project of the group, for example `../v1/original/game.prg`. The command writes only in its own project.
53
55
  - 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.
54
56
  - A range is `<start>-<end>`, for example `'$c000-$cfff'`. In a JSON file, a range is `start` and `end`.
55
57
  - Bytes in a JSON file are hex in a field `data`, for example `"data": "a900"`.
@@ -15,7 +15,7 @@ import { conflicts } from "#src/knowledge/conflicts.ts";
15
15
  import { openForRead } from "#src/knowledge/database.ts";
16
16
  import { programImage, type ProgramImage } from "#src/knowledge/program.ts";
17
17
  import { currentRevision, listComments, listModules, listReferences, listRegions, listSymbols } from "#src/knowledge/read.ts";
18
- import { groupAt, groupOf, projectRoot, resolveProjectFile, resolveProjectPath, type ProjectGroup } from "#src/project.ts";
18
+ import { groupOf, namedGroup, projectRoot, resolveProjectFile, resolveProjectPath, type ProjectGroup } from "#src/project.ts";
19
19
  import { listed, OPTIONS, reply, runScript, UsageError, type Call, type Reply } from "#src/script.ts";
20
20
  import { checkLinks, linkPage, type Files, type LinkContext, type Names, type Problem } from "./lib/links.ts";
21
21
  import { buildReference, GENERATED_MARK } from "./lib/reference.ts";
@@ -124,7 +124,7 @@ interface Scope {
124
124
  group: ProjectGroup | undefined;
125
125
  /** The folder of the given pages, relative to root: the project of the run, or "" for root itself. */
126
126
  base: string;
127
- /** True when the run is in the group folder and no --project names it: nextAction gives it. */
127
+ /** True when the run is on the group folder: the project lookup refuses it, so nextAction gives it as --project. */
128
128
  unnamed: boolean;
129
129
  }
130
130
 
@@ -141,13 +141,11 @@ function linkScope(): Scope {
141
141
  try {
142
142
  root = projectRoot();
143
143
  } catch (error) {
144
- // The project lookup refuses a folder with projects in it: in a group folder, link works on the group.
145
- const group = groupAt(process.cwd());
144
+ // The project lookup refuses a group folder: given with --project or as the working directory, link works on the group.
145
+ const group = namedGroup();
146
146
  if (group === undefined) throw error;
147
147
  return { root: group.root, group, base: "", unnamed: true };
148
148
  }
149
- const at = groupAt(root);
150
- if (at !== undefined) return { root, group: at, base: "", unnamed: false };
151
149
  const group = groupOf(root);
152
150
  if (group === undefined) return { root, group, base: "", unnamed: false };
153
151
  return { root: group.root, group, base: basename(root), unnamed: false };
@@ -212,6 +210,8 @@ function link(call: Call): Reply {
212
210
  const walk = (relative: string) => {
213
211
  for (const entry of readdirSync(join(real, relative), { withFileTypes: true }).sort((a, b) => a.name.localeCompare(b.name))) {
214
212
  const path = posix.join(relative, entry.name);
213
+ // A dot folder is no documentation: .c64-re-tools/ holds the knowledge and the scratch of the agent, .git/ the history.
214
+ if (entry.isDirectory() && entry.name.startsWith(".")) continue;
215
215
  if (entry.isDirectory()) walk(path);
216
216
  else if (entry.name.endsWith(".md")) found.push(posix.join(directory, path));
217
217
  }
@@ -77,6 +77,8 @@ The knowledge is text in `.c64-re-tools/knowledge/` of the project. Commit that
77
77
 
78
78
  - 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/`.
79
79
  - 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.
80
+ - In a group folder, each version of a program is a project. The script refuses the group folder and each folder in it that is not a project. Give `--project` with a project of the group, for example `--project v1`.
81
+ - A path that a command only reads can go into another project of the group, for example `../v1/original/game.prg`. The command writes only in its own project.
80
82
  - 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.
81
83
  - A range is `<start>-<end>`, for example `'$c000-$cfff'`. In a JSON file, a range is `start` and `end`.
82
84
  - Bytes in a JSON file are hex in a field `data`, for example `"data": "a900"`.
@@ -42,6 +42,8 @@ To get the command list, run the script with no arguments.
42
42
 
43
43
  - 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/`.
44
44
  - 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.
45
+ - In a group folder, each version of a program is a project. The script refuses the group folder and each folder in it that is not a project. Give `--project` with a project of the group, for example `--project v1`.
46
+ - A path that a command only reads can go into another project of the group, for example `../v1/original/game.prg`. The command writes only in its own project.
45
47
  - 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.
46
48
  - A range is `<start>-<end>`, for example `'$c000-$cfff'`. In a JSON file, a range is `start` and `end`.
47
49
  - Bytes in a JSON file are hex in a field `data`, for example `"data": "a900"`.
@@ -41,6 +41,8 @@ To get the command list, run the script with no arguments.
41
41
 
42
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
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
+ - In a group folder, each version of a program is a project. The script refuses the group folder and each folder in it that is not a project. Give `--project` with a project of the group, for example `--project v1`.
45
+ - A path that a command only reads can go into another project of the group, for example `../v1/original/game.prg`. The command writes only in its own project.
44
46
  - 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.
45
47
  - A range is `<start>-<end>`, for example `'$c000-$cfff'`. In a JSON file, a range is `start` and `end`.
46
48
  - Bytes in a JSON file are hex in a field `data`, for example `"data": "a900"`.
@@ -58,6 +58,8 @@ To get the command list, run the script with no arguments.
58
58
 
59
59
  - 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/`.
60
60
  - 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.
61
+ - In a group folder, each version of a program is a project. The script refuses the group folder and each folder in it that is not a project. Give `--project` with a project of the group, for example `--project v1`.
62
+ - A path that a command only reads can go into another project of the group, for example `../v1/original/game.prg`. The command writes only in its own project.
61
63
  - 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.
62
64
  - A range is `<start>-<end>`, for example `'$c000-$cfff'`. In a JSON file, a range is `start` and `end`.
63
65
  - Bytes in a JSON file are hex in a field `data`, for example `"data": "a900"`.
@@ -247,6 +249,7 @@ Traps:
247
249
  - Error `invalid-input` with "no user or LLM name": give `--name`.
248
250
  - Error `conflict`: the library has a routine with that name. Give another `--name`, or give `--replace` to write over it.
249
251
  - Error `invalid-input` with "is a project folder": the library folder is a project. Give a folder beside the projects.
252
+ - Error `invalid-input` with "a new library must not be in the project folder": the library folder does not exist, and it is in the project. Give a folder beside the projects, for example `../library`.
250
253
 
251
254
  ### `match`
252
255
 
@@ -261,20 +264,24 @@ Finds each routine of the library in the program of the project knowledge.
261
264
  - `library` and `routines`: the library folder and the number of routines in it.
262
265
  - `matches`: each item has `routine`, `start`, `end` and `shift`. `shift` is the address in the program minus the address in the library.
263
266
  - `differing` in each match: each operand that addresses something outside the routine and that is different. It gives the value in the `library` and in the `program`. Examine each one. For example, the program can use other variables or another SID voice.
264
- - `batch`: only with `--out`. `out`, `bytes` and `sha256` give the batch file. `writes` counts its writes. `known` counts the writes that the project knowledge has already. `leftOut` lists the writes that the script left out, and why.
267
+ - `batch`: only with `--out`. `out`, `bytes` and `sha256` give the batch file. `writes` counts its writes. `known` counts the writes that the project knowledge has already. `leftOut` lists the writes that the script left out, and why. `check` lists each comment of the batch that gives an address outside its routine. Each item has `routine`, `address`, `comment` and the `outside` addresses. Such an address can be different in this program. Examine each comment, and correct its text in the batch file if it is necessary.
265
268
 
266
269
  The batch has the names, regions, comments, references and operand expressions of each routine at its address in the program. The script leaves out a write in these conditions:
267
270
 
268
271
  - The program has a user or LLM row at that place.
269
272
  - The program uses the name at another address.
270
273
  - A removal holds at that place.
271
- - The routine occurs two times. Only the first copy gets the names.
274
+ - The routine occurs more than one time, and the script did not choose this copy.
272
275
  - The script left out the name that an operand expression uses, or the expression does not give the operand in the program.
273
276
 
277
+ A routine can occur more than one time. Then the script finds the nearest routines before and after it in the library that occur only one time. The script writes the names at the copy that has the shift of one of these routines. If no copy or more than one copy has such a shift, the script writes no names for the routine. Then `batch.leftOut` gives each copy with the reason `ambiguous`.
278
+
279
+ An address `$xxxx` in a comment moves with the routine when the address is in the routine. An address outside the routine does not move, and `batch.check` gives the comment.
280
+
274
281
  `hint` and `nextAction`:
275
282
 
276
283
  - Without `--out`, with matches: `nextAction` runs `match` with `--out`. Examine `differing` of each match first.
277
- - With `--out`, with writes: `hint` tells you to apply the changes in the batch file to the project knowledge. Read `batch.leftOut` first. Apply the changes as one revision of the LLM.
284
+ - With `--out`, with writes: `hint` tells you to apply the changes in the batch file to the project knowledge. Read `batch.leftOut` first. When `batch.check` has comments, `hint` tells you to examine them first. Apply the changes as one revision of the LLM.
278
285
  - No match: `hint` tells that a packed program can hold a routine in a packed form. Unpack the program, put it into the project knowledge, and run `match` again.
279
286
 
280
287
  Traps:
@@ -325,7 +332,7 @@ Traps:
325
332
  1. Put the program into the project knowledge, and name the routine there.
326
333
  2. Run `routine`. Read `leftOut`.
327
334
  3. In another project, put the program into the project knowledge. Run `match`.
328
- 4. Run `match` with `--out`. Read `batch.leftOut`.
335
+ 4. Run `match` with `--out`. Read `batch.leftOut` and `batch.check`.
329
336
  5. Apply the changes in the batch file to the project knowledge.
330
337
 
331
338
  ---