@henols/c64-re-tools 2.2.0-rc.2 → 2.2.0-rc.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -111,6 +111,7 @@ container and give each program its own folder. A folder with `.c64-re-tools/` i
111
111
  | c64-emulator | Runs, debugs and observes the program in VICE. |
112
112
  | c64-memory-map | What an address or a register value means. |
113
113
  | c64-development | Writes new programs, and adds features and fixes, with tests that give PASS or FAIL. |
114
+ | c64-export | Exports a rebuilt program as the start of a new program: one value per switch, no unused code, a zip file. |
114
115
  | c64-testing | Runs scenarios: original against rebuild, or many steps on one program with expected values. |
115
116
  | c64-disk | Directory, BAM, entries, sector chains and extraction of disk images. |
116
117
  | c64-basic | BASIC V2 listings and the SYS/USR handoff to machine code. |
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@henols/c64-re-tools",
3
- "version": "2.2.0-rc.2",
3
+ "version": "2.2.0-rc.3",
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",
@@ -12,7 +12,7 @@ import { knowledgeNames } from "#src/knowledge/names.ts";
12
12
  import { requireProgram } from "#src/knowledge/program.ts";
13
13
  import { releaseLabeled } from "#src/knowledge/read.ts";
14
14
  import { RELEASE_DEFINE } from "#src/knowledge/schema.ts";
15
- import { assemble } from "#src/native/acme.ts";
15
+ import { assemble, showSymbol, symbolsFileText } from "#src/native/acme.ts";
16
16
  import { localToolContext } from "#src/native/local.ts";
17
17
  import type { AcmeParams, AssembledSymbol } from "#src/native/types.ts";
18
18
  import { listed as listedItems, OPTIONS, runScript, UsageError } from "#src/script.ts";
@@ -28,13 +28,6 @@ function parseDefine(text: string): [string, number | boolean] {
28
28
  throw new UsageError(`--define ${match[1]} needs an integer or true/false, not ${value}`);
29
29
  }
30
30
 
31
- /** A symbol of the build: an address as $ and four hex digits, a constant as $ and at least four hex digits. */
32
- function showSymbol(symbol: AssembledSymbol) {
33
- return symbol.kind === "address"
34
- ? { name: symbol.name, kind: "address", address: formatC64Address(symbol.address), used: symbol.used }
35
- : { name: symbol.name, kind: "constant", value: `$${symbol.value.toString(16).padStart(4, "0")}`, used: symbol.used };
36
- }
37
-
38
31
  await runScript({
39
32
  name: "assembler",
40
33
  summary: "Assembles project source with ACME and writes the program into the project.",
@@ -114,7 +107,7 @@ await runScript({
114
107
  ...(plain ? { plain: true } : {}),
115
108
  loadRange: { start: formatC64Address(range.start), end: formatC64Address(range.end), bytes: range.bytes },
116
109
  symbols: listedItems(shown, call.count("max-listed")!, showSymbol, "a larger --max-listed, or --out-symbols <file.json> for the whole list"),
117
- ...(symbolsPath === undefined ? {} : { symbolsFile: writeReported(symbolsPath, `${JSON.stringify({ symbols: shown.map(showSymbol) }, null, 1)}\n`, { what: "--out-symbols" }) }),
110
+ ...(symbolsPath === undefined ? {} : { symbolsFile: writeReported(symbolsPath, symbolsFileText(shown), { what: "--out-symbols" }) }),
118
111
  ...(all ? {} : { unusedConstants: result.symbols!.length - shown.length }),
119
112
  diagnostics: result.diagnostics,
120
113
  ...(compared === undefined ? {} : { compare: compared }),
@@ -47,7 +47,7 @@ One repository can hold several programs, with one project folder for each progr
47
47
  First select the path:
48
48
 
49
49
  - The project has no program yet: path A.
50
- - The program has source that a person wrote: path B.
50
+ - The program has source that a person wrote: path B. A project from the c64-export skill also takes path B.
51
51
  - The source comes from c64-source: path C. The source has `main.asm` and `source-map.json` from c64-source. Do not edit this source. The next `source.ts generate` writes over it.
52
52
 
53
53
  ### Acceptance checks
@@ -177,3 +177,4 @@ A program that builds, with a run file in `tests/` for each acceptance check, an
177
177
  - Unused ranges of a rebuilt program → c64-provenance
178
178
  - Documentation → c64-docs
179
179
  - Code that nobody understands yet → c64-reverse-engineering
180
+ - The start of a new program from a rebuilt program → c64-export
@@ -0,0 +1,87 @@
1
+ ---
2
+ name: c64-export
3
+ description: Use this skill to export a rebuilt C64 program as the start of a new program. The export sets each switch to one value in the code. Corrections, trainers and cheats are in, and copy protection and crack changes are out. It removes code and data that nothing uses. It writes a new project folder with the source, a baseline program, the tests and a small knowledge, and a zip file of the folder. Use it for requests such as "export the project", "start a new game from this rebuild" or "make a clean package of the code". Do not use it to change the rebuild. Use c64-development for that.
4
+ ---
5
+
6
+ # c64-export
7
+
8
+ ## Purpose
9
+
10
+ Make the start of a new program from a rebuilt program. The new program is a new project. A person changes its source directly.
11
+
12
+ This skill does not change the rebuilt project or its knowledge.
13
+
14
+ ## Inputs
15
+
16
+ - A rebuilt project: `source.ts generate` of the c64-source skill gives PASS.
17
+ - The folder of the new project. It must be new or empty, and no project can hold it. Use a folder beside the project, for example `../game-new`.
18
+ - Optional: the coverage of the runs in the knowledge. Without coverage, nothing becomes free by itself.
19
+
20
+ ## Tools / execution path
21
+
22
+ This skill uses the other skills. It does not use the host runtime directly.
23
+
24
+ | Work | Skill |
25
+ |---|---|
26
+ | The export | c64-source: `source.ts export` |
27
+ | The switches and the code at an address | c64-knowledge |
28
+ | The build of the new project | c64-assembler |
29
+ | The tests of the new project | c64-testing |
30
+
31
+ ## Workflow
32
+
33
+ 1. Run `source.ts generate --out src` of c64-source. It must give PASS. If it does not, stop and complete the rebuild with the c64-reverse-engineering skill.
34
+ 2. Read the switches with `knowledge.ts switches` of c64-knowledge. Tell the user the export value of each switch:
35
+ - `fix`, `trainer` and `cheat`: 1. The code becomes a part of the new program.
36
+ - `protection` and `crack`: 0.
37
+ - `change`: its default.
38
+
39
+ If the user wants another value for a switch, change its kind or its default with `knowledge.ts switch` first.
40
+ 3. Run `source.ts export --to <folder>` of c64-source.
41
+ 4. Read `unreachableExecuted`. Each range is code that a run executed and that the entry does not get to now. For each range, find the code that calls it with c64-knowledge:
42
+ - Only code that the export removed calls it, for example a copy check: drop it.
43
+ - Code that the knowledge does not show can call it, for example through a pointer: keep it.
44
+ - If you are not sure, ask the user.
45
+ 5. To drop a range, remove the folder and the zip file of the export. Then run `source.ts export` again with `--drop '<range>'` for each range.
46
+ 6. Build the new project with c64-assembler. Give `--project <folder>`. Use the build commands in its `README.md`. The build with `--compare` must give PASS.
47
+ 7. Run each test of the new project with c64-testing. Give `--project <folder>`. Use the test commands in its `README.md`. Each test must give PASS.
48
+
49
+ ### The answer
50
+
51
+ Give the user a short answer at the end:
52
+
53
+ 1. The folder and the zip file.
54
+ 2. The value of each switch.
55
+ 3. The free ranges, with their bytes.
56
+ 4. Each test and its result, and the tests that the export left out, with the reason.
57
+ 5. The next step: the c64-development skill, path B.
58
+
59
+ ## Knowledge
60
+
61
+ Read:
62
+
63
+ - the switches, and the code at the addresses of `unreachableExecuted`.
64
+
65
+ Automatic write:
66
+
67
+ - none in the rebuilt project. The export writes the knowledge of the new project.
68
+
69
+ ## Result
70
+
71
+ A new project folder and a zip file of it. Its build is identical to its baseline, and each of its tests gives PASS.
72
+
73
+ ## Failure and conflicts
74
+
75
+ - `export` gives FAIL with `stage`: read `reason`. The c64-source skill tells what to do for each stage.
76
+ - `export` refuses the folder: give a new or empty folder outside every project. Remove a folder or a zip file of an earlier export only when this task made it.
77
+ - A test of the new project gives FAIL: the export dropped a range that the program uses. Remove the folder and the zip file. Run `export` again with `--keep` for the range, then test again.
78
+ - A skill reports `installation-incomplete`, or `machine-unavailable` with a message that the host runtime does not run: stop the whole task. Give the message to the user and wait for the user. Do not make a replacement for the tool or the host runtime.
79
+
80
+ ## Handoffs
81
+
82
+ - The rebuild is not complete → c64-reverse-engineering
83
+ - The export → c64-source
84
+ - Switches and the code at an address → c64-knowledge
85
+ - The build of the new project → c64-assembler
86
+ - The tests of the new project → c64-testing
87
+ - A new feature or a fix in the new project → c64-development
@@ -8,7 +8,7 @@
8
8
 
9
9
  import { type AddressRange, basicLineText } from "#src/c64.ts";
10
10
  import { MNEMONICS } from "#src/opcodes.ts";
11
- import type { Access, Separation } from "./separate.ts";
11
+ import type { Access, Separation } from "#src/knowledge/separate.ts";
12
12
 
13
13
  export interface LeftoversInput {
14
14
  segments: AddressRange[];
@@ -47,7 +47,7 @@ import { compareReleases, identicalShare, shiftSuggestion, suggestShifts, type R
47
47
  import { findRoutine, libraryFolder, readLibrary, routineFrom, saveRoutine, suggest } from "./lib/library.ts";
48
48
  import { watchRelease, type Start } from "./lib/runtime.ts";
49
49
  import { findLeftovers } from "./lib/leftovers.ts";
50
- import { separate, type Root } from "./lib/separate.ts";
50
+ import { separate, type Root } from "#src/knowledge/separate.ts";
51
51
 
52
52
  const hexBytes = (bytes: number[]) => Buffer.from(bytes).toString("hex").replace(/(..)(?!$)/g, "$1 ");
53
53
 
@@ -186,4 +186,6 @@ A clear answer for the scope, with the durable conclusions in the project knowle
186
186
 
187
187
  All specialist skills in the table above.
188
188
 
189
- A new feature or a fix for the rebuilt program → c64-development
189
+ A new feature or a correction of the rebuilt program → c64-development
190
+
191
+ The start of a new program from the rebuilt program → c64-export
@@ -1,6 +1,6 @@
1
1
  ---
2
2
  name: c64-source
3
- description: Use this skill to generate ACME source code for a C64 program from the project knowledge. The knowledge holds the program, its entry state, the coverage and the names. The script runs Ghidra for the instructions and writes the source files. It checks with ACME that the source assembles to the same bytes as the program. It can also write a loader that sets the entry state of an unpacked program. Use it to make a rebuild. Do not edit the generated files. Change the knowledge and generate again.
3
+ description: Use this skill to generate ACME source code for a C64 program from the project knowledge. The knowledge holds the program, its entry state, the coverage and the names. The script runs Ghidra for the instructions and writes the source files. It checks with ACME that the source assembles to the same bytes as the program. It can also write a loader that sets the entry state of an unpacked program. It can also export the program as the start of a new program. The export is a new project with one value for each switch and without unused code. Use it to make a rebuild. Do not edit the generated files. Change the knowledge and generate again.
4
4
  ---
5
5
 
6
6
  # c64-source
@@ -137,6 +137,88 @@ The script simulates the copies first. Then it runs `loader.prg` in the emulator
137
137
 
138
138
  The entry state is the state of the base release. Thus the script builds and checks `loader.prg` with the switch values of the base release. For a loader of the default build, build `loader.asm` with the c64-assembler skill and no option.
139
139
 
140
+ ## Export for a new application
141
+
142
+ `export` writes the program of the knowledge as the start of a new program: a new project folder and a zip file of it. A person changes the source of the new project directly. The c64-export skill gives the full procedure.
143
+
144
+ ```
145
+ node <skill>/scripts/source.ts export --to ../game-new
146
+ node <skill>/scripts/source.ts export --to ../game-new --name game --drop '$081b-$0822'
147
+ ```
148
+
149
+ | Option | Meaning |
150
+ |---|---|
151
+ | `--to ../game-new` | The folder of the new project, relative to the project folder. The folder must be new or empty, and no project can hold it. The script also writes `../game-new.zip`. |
152
+ | `--name game` | The name of `baseline/game.prg` and `build/game.prg`. The default is the name of the folder. |
153
+ | `--keep '$2000-$20ff'` | A range that stays, also when nothing uses it. Use it again for more ranges. |
154
+ | `--drop '$081b-$0822'` | A range that becomes free. Use it again for more ranges. Code that stays must not go to the range or read it. |
155
+ | `--max-listed 20` | List at most this number of items in each list. The default is 50. |
156
+
157
+ The script does these steps. It writes nothing until all the checks pass.
158
+
159
+ 1. It builds the source of the base release. The build must give the program byte by byte, with all the patches.
160
+ 2. It sets each switch to its export value:
161
+ - `fix`, `trainer` and `cheat`: 1. Trainers and cheats become a part of the new program.
162
+ - `protection` and `crack`: 0.
163
+ - `change`: its default.
164
+ 3. It builds the program with these values. This build can change bytes only in the patches.
165
+ 4. It finds the free ranges. Each free range gets the bytes $00 and the label `free_xxxx`. No address moves.
166
+ 5. It makes the source of the new program. ACME must assemble the source to the new program byte by byte. For a capture with an entry state, the script also makes `loader.asm` and simulates its copies. It does not run the loader in the emulator.
167
+ 6. It writes the new project and the zip file.
168
+
169
+ ### The free ranges
170
+
171
+ The script divides the decoded instructions into blocks. A block stops after RTS, RTI, JMP, BRK or a JAM, and before a gap. A block of code becomes free only when both of these are true:
172
+
173
+ - No run of the coverage executed it.
174
+ - No path gets to it from the entry, the hardware vectors or the code that stays. A path is a call, a jump, a branch, a reference of the knowledge or an operand expression. An entry of a table and two immediate operands that give its address are paths too.
175
+
176
+ Code that a run executed always stays. `unreachableExecuted` lists the executed code that the entry and the vectors do not get to. An example is a copy check whose call the export removed. Give its range to `--drop` to make it free.
177
+
178
+ Data becomes free only when all of these are true:
179
+
180
+ - Its region type is `bytes`, `words`, a table of addresses, `petscii`, `workspace` or `unused`.
181
+ - No run read, wrote or executed a byte of it.
182
+ - No code that stays reads it or names it, and no table that stays has an entry in it.
183
+ - It is not at the first address of the program and not at $fffa-$ffff.
184
+
185
+ The VIC-II reads `charset`, `sprite`, `bitmap` and `screen` data without the CPU, so the coverage does not show these reads. This data stays unless `--drop` names it. Bytes without a region also stay.
186
+
187
+ The entry is the entry of the knowledge. Without it, the entry is the address of the first SYS in the BASIC line of the program. Without coverage, nothing becomes free by itself. Without an entry and without the hardware vectors in the image, no code is unreachable.
188
+
189
+ ### The new project
190
+
191
+ ```
192
+ README.md the commands to build and test, the free ranges and the origin
193
+ src/ main.asm, constants.asm, macros.asm, the modules and loader.asm
194
+ baseline/<name>.prg the exported program, and baseline/<name>.json with its symbols
195
+ tests/ the tests of the project, changed for the new program
196
+ .c64-re-tools/knowledge/ the knowledge of the new program
197
+ ```
198
+
199
+ - The source has no mark line, no `switches.asm`, no patch zones and no `; not executed` lines. It keeps the names, the comments, the operand expressions and the macros. A line comment at each changed patch gives the switch, its kind, its value and the comments of the switch and the patch.
200
+ - The knowledge has one revision `export`. It holds the program, the entry and its state, the names, the comments, the regions and the modules. It has no history, analyzer names, references, operand expressions, macros, patches, switches, releases or coverage.
201
+ - The tests come from the JSON files in `tests/`:
202
+ - A scenario compares the build of the new project with the baseline. Both sides are the same program, so a step of the original side alone goes. A step of the rebuild side alone runs on both sides.
203
+ - A run file runs the build of the new project.
204
+ - Both take the names from the symbol files.
205
+ - The script leaves out a test that restores a snapshot. It also leaves out a test with a name that the new program does not have, or with an address in a free range.
206
+ - The script does not copy recordings, record files or `runs/`.
207
+ - The zip file holds the folder without `.c64-re-tools/local/`.
208
+
209
+ The result of `export`:
210
+
211
+ - `result`: `PASS` when the script wrote the new project. `FAIL` with `stage` when a check failed: `original`, `switches`, `export` or `loader`. Then the script wrote nothing.
212
+ - `to` and `zip`: the folder and the zip file.
213
+ - `switches`: each switch with `kind`, `base` and the export `value`.
214
+ - `free`: the count of free bytes, and each free range with `bytes`, `what` and `label`.
215
+ - `unreachableExecuted`: each range of executed code that the entry does not get to, with the `routine` before it and its `--drop` option.
216
+ - `knowledge`: the counts of the rows of the new knowledge.
217
+ - `tests`: the `written` tests, and the `leftOut` tests with the reason.
218
+ - `loader`: the copies, the copier, `stackByte` and `simulated: true`.
219
+ - `noCoverage` or `unrooted`: why nothing became free by itself.
220
+ - `spacesLeftOut`: the spaces that the export does not have. The export has only the space `c64`.
221
+
140
222
  ## Knowledge
141
223
 
142
224
  Read:
@@ -199,6 +281,10 @@ Do not store:
199
281
  - `loader.verified: false` with a `reason`: the script did not make the loader. The script still writes and checks the source. When no copy order or no free pages exist, or the loader needs too many copies, the reason gives the ranges. Capture the program again with fewer segments, or write the loader by hand.
200
282
  - `loader.verified: false` with `differences`: the loader ran, and `differences` gives the bytes and registers that are not equal.
201
283
  - `loader.verified: false` with `stop`: the loader did not get to its copier or to its jump. `stop` gives the address where it stopped and `port`, the value of $01. `history` gives the last 16 instructions. `cause` gives the first byte of the copier that is not equal to its byte in the loader file. If the copier ran, `cause` can also give the first byte that a copy did not write correctly.
284
+ - `export` with `stage: "original"`: the source of the knowledge does not pass. Run `generate`, and correct the knowledge until `generate` gives PASS.
285
+ - `export` with `stage: "switches"`: the build with the export values changes bytes outside the patches, or a patch does not fit. Read `build`. Correct the patches.
286
+ - `export` refuses two switches that change one range: the export puts both alternatives in. Give all but one of them the kind `change` and the default of the base release with `switch` of the c64-knowledge skill.
287
+ - `export` refuses `--drop`: read the message. It names the code that stays and goes to the range or reads it, or the code or region that the range cuts.
202
288
  - `installation-incomplete`, or `machine-unavailable` with a message that the host runtime does not run: stop the task. Give the message to the user and wait for the user. The message gives the remedy. Do not make a replacement for the tool or the host runtime, for example your own disassembler or emulator script.
203
289
 
204
290
  ## Handoffs
@@ -208,3 +294,4 @@ Do not store:
208
294
  - Names, regions, comments and modules → c64-knowledge
209
295
  - Build the program → c64-assembler
210
296
  - Test the rebuild against the original → c64-testing
297
+ - The start of a new program from the rebuild → c64-export