@henols/c64-re-tools 2.6.0-rc.2 → 2.6.0-rc.20
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/package.json +2 -2
- package/skills/c64-assembler/SKILL.md +28 -14
- package/skills/c64-assembler/scripts/assembler.ts +28 -15
- package/skills/c64-development/SKILL.md +4 -4
- package/skills/c64-docs/SKILL.md +9 -7
- package/skills/c64-docs/scripts/docs.ts +24 -11
- package/skills/c64-docs/scripts/lib/reference.ts +119 -33
- package/skills/c64-docs/scripts/lib/source-map.ts +23 -4
- package/skills/c64-knowledge/SKILL.md +67 -42
- package/skills/c64-knowledge/scripts/knowledge.ts +90 -67
- package/skills/c64-knowledge/scripts/lib/listing.ts +18 -3
- package/skills/c64-packer/SKILL.md +1 -1
- package/skills/c64-provenance/SKILL.md +11 -10
- package/skills/c64-provenance/scripts/lib/library.ts +16 -16
- package/skills/c64-provenance/scripts/provenance.ts +33 -23
- package/skills/c64-reverse-engineering/SKILL.md +17 -11
- package/skills/c64-source/SKILL.md +71 -47
- package/skills/c64-source/scripts/lib/export.ts +89 -34
- package/skills/c64-source/scripts/lib/loader.ts +79 -36
- package/skills/c64-source/scripts/lib/source.ts +38 -32
- package/skills/c64-source/scripts/lib/stage.ts +11 -5
- package/skills/c64-source/scripts/lib/start-screen.asm +4 -2
- package/skills/c64-source/scripts/source.ts +278 -145
- package/skills/c64-static-analysis/SKILL.md +29 -25
- package/skills/c64-static-analysis/scripts/lib/coverage.ts +2 -25
- package/skills/c64-static-analysis/scripts/lib/findings.ts +11 -11
- package/skills/c64-static-analysis/scripts/lib/instructions.ts +26 -24
- package/skills/c64-static-analysis/scripts/lib/macros.ts +3 -1
- package/skills/c64-static-analysis/scripts/static-analysis.ts +69 -67
- package/skills/c64-testing/SKILL.md +15 -5
- package/skills/c64-testing/references/recording-format.md +26 -5
- package/skills/c64-testing/references/scenario-format.md +11 -8
- package/skills/c64-testing/scripts/lib/diff-files.ts +4 -18
- package/skills/c64-testing/scripts/lib/play.ts +39 -41
- package/skills/c64-testing/scripts/lib/recording.ts +220 -46
- package/skills/c64-testing/scripts/lib/replay.ts +32 -6
- package/skills/c64-testing/scripts/lib/runs.ts +8 -3
- package/skills/c64-testing/scripts/lib/scenario.ts +18 -11
- package/skills/c64-testing/scripts/testing.ts +23 -2
- package/skills/c64-unpacker/SKILL.md +10 -5
- package/skills/c64-unpacker/scripts/lib/loads.ts +34 -13
- package/skills/c64-unpacker/scripts/lib/machine.ts +7 -3
- package/skills/c64-unpacker/scripts/lib/stages.ts +18 -3
- package/skills/c64-unpacker/scripts/unpacker.ts +43 -9
- package/skills/emulator/SKILL.md +7 -12
- package/src/build-name.ts +55 -0
- package/src/c64-keys.ts +98 -0
- package/src/c64.ts +22 -2
- package/src/cli/main.ts +3 -29
- package/src/host/main.ts +3 -3
- package/src/host/server.ts +3 -1
- package/src/host/tools/index.ts +2 -7
- package/src/host/vice/process.ts +34 -17
- package/src/host/vice/session.ts +138 -31
- package/src/host-client/steps.ts +21 -18
- package/src/host-client/tools.ts +2 -7
- package/src/host-client/vice-session.ts +7 -1
- package/src/knowledge/evidence.ts +66 -0
- package/src/knowledge/history.ts +5 -5
- package/src/knowledge/import.ts +12 -5
- package/src/knowledge/program.ts +123 -32
- package/src/knowledge/read.ts +36 -13
- package/src/knowledge/schema.ts +43 -17
- package/src/knowledge/start-screen.ts +70 -0
- package/src/knowledge/text.ts +33 -5
- package/src/knowledge/write.ts +75 -36
- package/src/mcp/inputs.ts +12 -0
- package/src/mcp/server.ts +1 -1
- package/src/mcp/tools/debug.ts +23 -21
- package/src/mcp/tools/input.ts +38 -5
- package/src/mcp/tools/machine.ts +3 -42
- package/src/mcp/tools/media.ts +1 -1
- package/src/mcp/tools/memory.ts +12 -9
- package/src/native/acme.ts +1 -1
- package/src/native/c64run.ts +7 -4
- package/src/native/decompiler/analyze.ts +98 -0
- package/src/native/decompiler/c64rt-wasm.ts +4 -0
- package/src/native/decompiler/language/c64rt_6510.sla +0 -0
- package/src/native/{ghidra → decompiler}/language/c64rt_6510.slaspec +1 -1
- package/src/native/decompiler/pcode.ts +222 -0
- package/src/native/decompiler/walk.ts +205 -0
- package/src/native/decompiler/wasm.ts +98 -0
- package/src/native/discover.ts +3 -3
- package/src/native/dxa.ts +12 -1
- package/src/native/processes.ts +4 -4
- package/src/native/types.ts +27 -27
- package/src/opcodes.ts +1 -1
- package/src/platform.ts +30 -1
- package/src/project.ts +17 -4
- package/src/protocol/tools.ts +1 -45
- package/src/protocol/vice.ts +47 -0
- package/src/reference-pages.ts +64 -0
- package/src/script.ts +4 -4
- package/src/vsf.ts +98 -0
- package/tools/c64run.c +158 -15
- package/tools/install-container.sh +1 -2
- package/src/cli/status.ts +0 -127
- package/src/host/tools/status.ts +0 -47
- package/src/native/ghidra/analyze.ts +0 -87
- package/src/native/ghidra/index.ts +0 -170
- package/src/native/ghidra/scripts/C64Export.java +0 -274
- package/src/native/ghidra/scripts/C64Prepare.java +0 -66
- package/src/native/status.ts +0 -135
- package/tools/install-ghidra.sh +0 -40
- /package/src/native/{ghidra → decompiler}/language/c64rt_6510.cspec +0 -0
- /package/src/native/{ghidra → decompiler}/language/c64rt_6510.ldefs +0 -0
- /package/src/native/{ghidra → decompiler}/language/c64rt_6510.pspec +0 -0
|
Binary file
|
|
@@ -264,7 +264,7 @@ REL: target is rel [ target = inst_next + rel; ] { export *:2 target; }
|
|
|
264
264
|
|
|
265
265
|
# BRK skips its padding byte: the return address is two bytes on.
|
|
266
266
|
# The vector at $FFFE is in ROM unless the program banks the KERNAL out, and
|
|
267
|
-
# the analysis cannot know the banking. Thus BRK ends the flow, and
|
|
267
|
+
# the analysis cannot know the banking. Thus BRK ends the flow, and the decompiler
|
|
268
268
|
# does not follow the RAM bytes at $FFFE of a flat image.
|
|
269
269
|
:BRK is op=0x00 {
|
|
270
270
|
*:2 (SP - 1) = inst_next + 1;
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
// A p-code interpreter for integer operations over byte-addressed spaces,
|
|
2
|
+
// machine-neutral: it knows no CPU, only the p-code that SLEIGH gives. A value
|
|
3
|
+
// that depends on an unknown byte is unknown (undefined). The analysis uses it
|
|
4
|
+
// to find the target of an indirect jump from the stores before it; the
|
|
5
|
+
// language tests use it to execute one instruction.
|
|
6
|
+
|
|
7
|
+
import type { PcodeOp, Varnode } from "./wasm.ts";
|
|
8
|
+
|
|
9
|
+
/** Where an instruction leaves the flow; next is unknown for a computed target that the state does not give. */
|
|
10
|
+
export type Exit = { kind: "fall" } | { kind: "branch" | "call" | "return" | "indirect"; next: number | undefined } | { kind: "stop" };
|
|
11
|
+
|
|
12
|
+
/** Values are kept below 2^48, so sizes up to 6 bytes are exact. */
|
|
13
|
+
const MAX_SIZE = 6;
|
|
14
|
+
|
|
15
|
+
const mask = (size: number) => 2 ** (8 * Math.min(size, MAX_SIZE)) - 1;
|
|
16
|
+
/** The value modulo 2^(8 * size), also for a negative value. */
|
|
17
|
+
const wrap = (value: number, size: number) => ((value % (mask(size) + 1)) + mask(size) + 1) % (mask(size) + 1);
|
|
18
|
+
const signed = (value: number, size: number) => (value >= 2 ** (8 * size - 1) ? value - 2 ** (8 * size) : value);
|
|
19
|
+
|
|
20
|
+
export class PcodeState {
|
|
21
|
+
/** Space name to byte offset to byte value. */
|
|
22
|
+
private readonly spaces = new Map<string, Map<number, number>>();
|
|
23
|
+
private readonly memory: (address: number) => number | undefined;
|
|
24
|
+
|
|
25
|
+
/** memory gives the RAM bytes that no store of this state wrote, or undefined for an unknown byte. */
|
|
26
|
+
constructor(memory: (address: number) => number | undefined = () => undefined) {
|
|
27
|
+
this.memory = memory;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
private bytes(space: string): Map<number, number> {
|
|
31
|
+
let bytes = this.spaces.get(space);
|
|
32
|
+
if (bytes === undefined) this.spaces.set(space, (bytes = new Map()));
|
|
33
|
+
return bytes;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
get(varnode: Varnode): number | undefined {
|
|
37
|
+
if (varnode.space === "const") return varnode.offset & mask(varnode.size);
|
|
38
|
+
const bytes = this.spaces.get(varnode.space);
|
|
39
|
+
let value = 0;
|
|
40
|
+
for (let index = 0; index < varnode.size; index++) {
|
|
41
|
+
const at = varnode.space === "ram" ? (varnode.offset + index) & 0xffff : varnode.offset + index;
|
|
42
|
+
const byte = bytes?.get(at) ?? (varnode.space === "ram" ? this.memory(at) : undefined);
|
|
43
|
+
if (byte === undefined) return undefined;
|
|
44
|
+
value += byte * 2 ** (8 * index);
|
|
45
|
+
}
|
|
46
|
+
return value;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
set(varnode: Varnode, value: number | undefined): void {
|
|
50
|
+
const bytes = this.bytes(varnode.space);
|
|
51
|
+
for (let index = 0; index < varnode.size; index++) {
|
|
52
|
+
const at = varnode.space === "ram" ? (varnode.offset + index) & 0xffff : varnode.offset + index;
|
|
53
|
+
if (value === undefined) bytes.delete(at);
|
|
54
|
+
else bytes.set(at, Math.floor(value / 2 ** (8 * index)) & 0xff);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Forgets every RAM byte: a store through an unknown pointer can change any of them. */
|
|
59
|
+
forgetRam(): void {
|
|
60
|
+
this.spaces.delete("ram");
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** Forgets every value: after a call, the routine can have changed anything. */
|
|
64
|
+
forget(): void {
|
|
65
|
+
this.spaces.clear();
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Runs the p-code of one instruction and tells where the flow goes. */
|
|
69
|
+
run(pcode: readonly PcodeOp[]): Exit {
|
|
70
|
+
for (let index = 0; index < pcode.length; ) {
|
|
71
|
+
const operation = pcode[index]!;
|
|
72
|
+
const inputs = operation.in.map((varnode) => this.get(varnode));
|
|
73
|
+
const [a, b] = inputs;
|
|
74
|
+
const size = operation.out?.size ?? operation.in[0]?.size ?? 1;
|
|
75
|
+
const known = a !== undefined && b !== undefined;
|
|
76
|
+
let out: number | undefined;
|
|
77
|
+
switch (operation.op) {
|
|
78
|
+
case "COPY":
|
|
79
|
+
case "INT_ZEXT":
|
|
80
|
+
out = a;
|
|
81
|
+
break;
|
|
82
|
+
case "INT_SEXT":
|
|
83
|
+
out = a === undefined ? undefined : signed(a, operation.in[0]!.size);
|
|
84
|
+
break;
|
|
85
|
+
case "INT_ADD":
|
|
86
|
+
out = known ? a + b : undefined;
|
|
87
|
+
break;
|
|
88
|
+
case "INT_SUB":
|
|
89
|
+
out = known ? a - b : undefined;
|
|
90
|
+
break;
|
|
91
|
+
case "INT_MULT":
|
|
92
|
+
out = known ? a * b : undefined;
|
|
93
|
+
break;
|
|
94
|
+
case "INT_DIV":
|
|
95
|
+
out = known && b !== 0 ? Math.floor(a / b) : undefined;
|
|
96
|
+
break;
|
|
97
|
+
case "INT_REM":
|
|
98
|
+
out = known && b !== 0 ? a % b : undefined;
|
|
99
|
+
break;
|
|
100
|
+
case "INT_AND":
|
|
101
|
+
out = known ? bitwise(a, b, (x, y) => x & y) : undefined;
|
|
102
|
+
break;
|
|
103
|
+
case "INT_OR":
|
|
104
|
+
out = known ? bitwise(a, b, (x, y) => x | y) : undefined;
|
|
105
|
+
break;
|
|
106
|
+
case "INT_XOR":
|
|
107
|
+
out = known ? bitwise(a, b, (x, y) => x ^ y) : undefined;
|
|
108
|
+
break;
|
|
109
|
+
case "INT_NEGATE":
|
|
110
|
+
out = a === undefined ? undefined : mask(size) - a;
|
|
111
|
+
break;
|
|
112
|
+
case "INT_2COMP":
|
|
113
|
+
out = a === undefined ? undefined : -a;
|
|
114
|
+
break;
|
|
115
|
+
case "INT_LEFT":
|
|
116
|
+
out = known ? (b >= 8 * size ? 0 : a * 2 ** b) : undefined;
|
|
117
|
+
break;
|
|
118
|
+
case "INT_RIGHT":
|
|
119
|
+
out = known ? Math.floor(a / 2 ** b) : undefined;
|
|
120
|
+
break;
|
|
121
|
+
case "INT_SRIGHT":
|
|
122
|
+
out = known ? Math.floor(signed(a, operation.in[0]!.size) / 2 ** b) : undefined;
|
|
123
|
+
break;
|
|
124
|
+
case "INT_EQUAL":
|
|
125
|
+
out = known ? Number(a === b) : undefined;
|
|
126
|
+
break;
|
|
127
|
+
case "INT_NOTEQUAL":
|
|
128
|
+
out = known ? Number(a !== b) : undefined;
|
|
129
|
+
break;
|
|
130
|
+
case "INT_LESS":
|
|
131
|
+
out = known ? Number(a < b) : undefined;
|
|
132
|
+
break;
|
|
133
|
+
case "INT_LESSEQUAL":
|
|
134
|
+
out = known ? Number(a <= b) : undefined;
|
|
135
|
+
break;
|
|
136
|
+
case "INT_SLESS":
|
|
137
|
+
out = known ? Number(signed(a, operation.in[0]!.size) < signed(b, operation.in[1]!.size)) : undefined;
|
|
138
|
+
break;
|
|
139
|
+
case "INT_SLESSEQUAL":
|
|
140
|
+
out = known ? Number(signed(a, operation.in[0]!.size) <= signed(b, operation.in[1]!.size)) : undefined;
|
|
141
|
+
break;
|
|
142
|
+
case "INT_CARRY":
|
|
143
|
+
out = known ? Number(a + b > mask(operation.in[0]!.size)) : undefined;
|
|
144
|
+
break;
|
|
145
|
+
case "INT_SCARRY":
|
|
146
|
+
case "INT_SBORROW": {
|
|
147
|
+
if (!known) break;
|
|
148
|
+
const width = operation.in[0]!.size;
|
|
149
|
+
const result = operation.op === "INT_SCARRY" ? signed(a, width) + signed(b, width) : signed(a, width) - signed(b, width);
|
|
150
|
+
out = Number(result < -(2 ** (8 * width - 1)) || result >= 2 ** (8 * width - 1));
|
|
151
|
+
break;
|
|
152
|
+
}
|
|
153
|
+
case "BOOL_NEGATE":
|
|
154
|
+
out = a === undefined ? undefined : Number(a === 0);
|
|
155
|
+
break;
|
|
156
|
+
case "BOOL_AND":
|
|
157
|
+
out = known ? Number(a !== 0 && b !== 0) : undefined;
|
|
158
|
+
break;
|
|
159
|
+
case "BOOL_OR":
|
|
160
|
+
out = known ? Number(a !== 0 || b !== 0) : undefined;
|
|
161
|
+
break;
|
|
162
|
+
case "BOOL_XOR":
|
|
163
|
+
out = known ? Number((a !== 0) !== (b !== 0)) : undefined;
|
|
164
|
+
break;
|
|
165
|
+
case "PIECE":
|
|
166
|
+
out = known ? a * 2 ** (8 * operation.in[1]!.size) + b : undefined;
|
|
167
|
+
break;
|
|
168
|
+
case "SUBPIECE":
|
|
169
|
+
out = known ? Math.floor(a / 2 ** (8 * b)) : undefined;
|
|
170
|
+
break;
|
|
171
|
+
case "POPCOUNT":
|
|
172
|
+
out = a === undefined ? undefined : [...a.toString(2)].filter((bit) => bit === "1").length;
|
|
173
|
+
break;
|
|
174
|
+
case "LOAD":
|
|
175
|
+
out = b === undefined ? undefined : this.get({ space: "ram", offset: b & 0xffff, size: operation.out!.size });
|
|
176
|
+
break;
|
|
177
|
+
case "STORE":
|
|
178
|
+
if (b === undefined) this.forgetRam();
|
|
179
|
+
else this.set({ space: "ram", offset: b & 0xffff, size: operation.in[2]!.size }, inputs[2]);
|
|
180
|
+
break;
|
|
181
|
+
case "BRANCH":
|
|
182
|
+
case "CBRANCH": {
|
|
183
|
+
const target = operation.in[0]!;
|
|
184
|
+
const taken = operation.op === "BRANCH" ? 1 : b;
|
|
185
|
+
if (taken === undefined) return target.space === "const" ? { kind: "stop" } : { kind: "branch", next: undefined };
|
|
186
|
+
if (taken !== 0) {
|
|
187
|
+
// A const target is a p-code relative branch inside this instruction.
|
|
188
|
+
if (target.space === "const") {
|
|
189
|
+
index += signed(target.offset & mask(target.size), target.size);
|
|
190
|
+
continue;
|
|
191
|
+
}
|
|
192
|
+
return { kind: "branch", next: target.offset };
|
|
193
|
+
}
|
|
194
|
+
break;
|
|
195
|
+
}
|
|
196
|
+
case "BRANCHIND":
|
|
197
|
+
return { kind: "indirect", next: a === undefined ? undefined : a & 0xffff };
|
|
198
|
+
case "CALL":
|
|
199
|
+
return { kind: "call", next: operation.in[0]!.offset };
|
|
200
|
+
case "CALLIND":
|
|
201
|
+
return { kind: "call", next: a === undefined ? undefined : a & 0xffff };
|
|
202
|
+
case "RETURN":
|
|
203
|
+
return { kind: "return", next: a === undefined ? undefined : a & 0xffff };
|
|
204
|
+
case "CALLOTHER":
|
|
205
|
+
// An opaque operation: its result is unknown, and a jam ends the flow at its branch to itself.
|
|
206
|
+
break;
|
|
207
|
+
default:
|
|
208
|
+
break;
|
|
209
|
+
}
|
|
210
|
+
if (operation.out !== null) this.set(operation.out, out === undefined ? undefined : wrap(out, operation.out.size));
|
|
211
|
+
index++;
|
|
212
|
+
}
|
|
213
|
+
return { kind: "fall" };
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/** A bitwise operation on values that can be wider than the 32 bits of JavaScript's operators. */
|
|
218
|
+
function bitwise(a: number, b: number, operation: (x: number, y: number) => number): number {
|
|
219
|
+
const low = operation(a % 2 ** 24, b % 2 ** 24) >>> 0;
|
|
220
|
+
const high = operation(Math.floor(a / 2 ** 24), Math.floor(b / 2 ** 24)) >>> 0;
|
|
221
|
+
return high * 2 ** 24 + (low & 0xffffff);
|
|
222
|
+
}
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
// The analysis from seeds: a recursive descent over the instructions that the
|
|
2
|
+
// SLEIGH decoder gives, with flows and references read from their p-code.
|
|
3
|
+
// Entry points and call targets are functions; data ranges are never decoded.
|
|
4
|
+
// A byte that an operand reads or writes, and that is no code, is data.
|
|
5
|
+
// The target of an indirect jump comes from the stores of the straight-line
|
|
6
|
+
// code before it, or else from the pointer in the image.
|
|
7
|
+
|
|
8
|
+
import { SYMBOL_NAME } from "../../c64.ts";
|
|
9
|
+
import type { DecompilerInstruction, DecompilerResult } from "../types.ts";
|
|
10
|
+
import { PcodeState } from "./pcode.ts";
|
|
11
|
+
import { decode, type Decoded, type Varnode } from "./wasm.ts";
|
|
12
|
+
|
|
13
|
+
export interface WalkSeeds {
|
|
14
|
+
entryPoints: number[];
|
|
15
|
+
codeStarts: number[];
|
|
16
|
+
dataRanges: Array<{ start: number; end: number }>;
|
|
17
|
+
labels: Array<{ address: number; name: string }>;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
interface Instruction {
|
|
21
|
+
decoded: Decoded;
|
|
22
|
+
flows: number[];
|
|
23
|
+
fallthrough: boolean;
|
|
24
|
+
references: Array<{ to: number; type: "call" | "jump" | "read" | "write" }>;
|
|
25
|
+
/** The addresses that an operand reads or writes without an index. */
|
|
26
|
+
accessed: number[];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** The longest straight-line run before an indirect jump that gives its target. */
|
|
30
|
+
const MAX_CHAIN = 32;
|
|
31
|
+
|
|
32
|
+
const isRam = (varnode: Varnode) => varnode.space === "ram";
|
|
33
|
+
|
|
34
|
+
/** The value of the operand bytes after the first byte, little-endian; undefined without operand bytes. */
|
|
35
|
+
function operandValue(bytes: readonly number[]): number | undefined {
|
|
36
|
+
if (bytes.length === 2) return bytes[1]!;
|
|
37
|
+
if (bytes.length === 3) return bytes[1]! | (bytes[2]! << 8);
|
|
38
|
+
return undefined;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Decodes [start, end] from the seeds; the image bytes are already loaded at start. */
|
|
42
|
+
export function walk(image: Uint8Array, start: number, end: number, seeds: WalkSeeds): Omit<DecompilerResult, "decompilations" | "completeness" | "toolVersion"> {
|
|
43
|
+
const isData = new Uint8Array(0x10000);
|
|
44
|
+
for (const range of seeds.dataRanges) isData.fill(1, range.start, range.end + 1);
|
|
45
|
+
const imageByte = (address: number) => (address >= start && address <= end ? image[address - start] : undefined);
|
|
46
|
+
|
|
47
|
+
const instructions = new Map<number, Instruction>();
|
|
48
|
+
/** The start of the instruction that holds each byte, or -1. */
|
|
49
|
+
const owner = new Int32Array(0x10000).fill(-1);
|
|
50
|
+
const functions = new Set(seeds.entryPoints);
|
|
51
|
+
const work = [...seeds.entryPoints, ...seeds.codeStarts];
|
|
52
|
+
|
|
53
|
+
while (work.length > 0) {
|
|
54
|
+
const address = work.pop()!;
|
|
55
|
+
if (address < start || address > end || instructions.has(address) || owner[address] !== -1 || isData[address]) continue;
|
|
56
|
+
let decoded: Decoded;
|
|
57
|
+
try {
|
|
58
|
+
decoded = decode(address);
|
|
59
|
+
} catch {
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
const last = address + decoded.length - 1;
|
|
63
|
+
let free = last <= end;
|
|
64
|
+
for (let at = address; free && at <= last; at++) free = owner[at] === -1 && !isData[at];
|
|
65
|
+
if (!free) continue;
|
|
66
|
+
|
|
67
|
+
const instruction: Instruction = { decoded, flows: [], fallthrough: true, references: [], accessed: [] };
|
|
68
|
+
const reference = (to: number, type: Instruction["references"][number]["type"]) => instruction.references.push({ to, type });
|
|
69
|
+
const access = (to: number, type: "read" | "write") => {
|
|
70
|
+
reference(to, type);
|
|
71
|
+
instruction.accessed.push(to);
|
|
72
|
+
};
|
|
73
|
+
const operand = operandValue(decoded.bytes);
|
|
74
|
+
// Unique and register varnodes that hold an operand address plus an index.
|
|
75
|
+
const indexed = new Map<string, number>();
|
|
76
|
+
const key = (varnode: Varnode) => `${varnode.space}:${varnode.offset}`;
|
|
77
|
+
const jams = decoded.pcode.some((operation) => operation.op === "CALLOTHER");
|
|
78
|
+
for (const operation of decoded.pcode) {
|
|
79
|
+
const [first, second] = operation.in;
|
|
80
|
+
switch (operation.op) {
|
|
81
|
+
case "BRANCH":
|
|
82
|
+
case "CBRANCH":
|
|
83
|
+
if (isRam(first!)) {
|
|
84
|
+
// A jam branches to itself: the flow ends there.
|
|
85
|
+
if (first!.offset === address && operation.op === "BRANCH" && jams) instruction.fallthrough = false;
|
|
86
|
+
else {
|
|
87
|
+
instruction.flows.push(first!.offset);
|
|
88
|
+
reference(first!.offset, "jump");
|
|
89
|
+
if (operation.op === "BRANCH") instruction.fallthrough = false;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
break;
|
|
93
|
+
case "CALL":
|
|
94
|
+
if (isRam(first!)) {
|
|
95
|
+
instruction.flows.push(first!.offset);
|
|
96
|
+
reference(first!.offset, "call");
|
|
97
|
+
functions.add(first!.offset);
|
|
98
|
+
}
|
|
99
|
+
break;
|
|
100
|
+
case "RETURN":
|
|
101
|
+
instruction.fallthrough = false;
|
|
102
|
+
break;
|
|
103
|
+
case "BRANCHIND": {
|
|
104
|
+
instruction.fallthrough = false;
|
|
105
|
+
const target = indirectTarget(address, decoded, instructions, owner, imageByte);
|
|
106
|
+
if (target !== undefined) {
|
|
107
|
+
instruction.flows.push(target);
|
|
108
|
+
reference(target, "jump");
|
|
109
|
+
}
|
|
110
|
+
break;
|
|
111
|
+
}
|
|
112
|
+
case "LOAD":
|
|
113
|
+
case "STORE": {
|
|
114
|
+
const type = operation.op === "LOAD" ? "read" : "write";
|
|
115
|
+
if (second!.space === "const") access(second!.offset & 0xffff, type);
|
|
116
|
+
else if (indexed.has(key(second!))) reference(indexed.get(key(second!))!, type);
|
|
117
|
+
break;
|
|
118
|
+
}
|
|
119
|
+
case "INT_ADD": {
|
|
120
|
+
// An address operand plus an index register: the reference goes to the operand address.
|
|
121
|
+
const constant = operation.in.find((varnode) => varnode.space === "const");
|
|
122
|
+
if (operation.out !== null && constant !== undefined && constant.offset === operand) indexed.set(key(operation.out), operand);
|
|
123
|
+
break;
|
|
124
|
+
}
|
|
125
|
+
case "COPY":
|
|
126
|
+
case "INT_ZEXT":
|
|
127
|
+
case "INT_AND":
|
|
128
|
+
if (operation.out !== null && first !== undefined && indexed.has(key(first))) indexed.set(key(operation.out), indexed.get(key(first))!);
|
|
129
|
+
break;
|
|
130
|
+
}
|
|
131
|
+
if (!["BRANCH", "CBRANCH", "CALL", "LOAD", "STORE"].includes(operation.op)) {
|
|
132
|
+
for (const input of operation.in) if (isRam(input)) access(input.offset, "read");
|
|
133
|
+
if (operation.out !== null && isRam(operation.out)) access(operation.out.offset, "write");
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
owner.fill(address, address, last + 1);
|
|
137
|
+
instructions.set(address, instruction);
|
|
138
|
+
for (const flow of instruction.flows) work.push(flow);
|
|
139
|
+
// Last in, first out: the next instruction is decoded first, so the code before an indirect jump is known.
|
|
140
|
+
if (instruction.fallthrough) work.push(last + 1);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
for (const instruction of instructions.values()) {
|
|
144
|
+
for (const address of instruction.accessed) if (address >= start && address <= end && owner[address] === -1) isData[address] = 1;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const labels = new Map(seeds.labels.map((label) => [label.address, label.name]));
|
|
148
|
+
const sorted = [...instructions.keys()].sort((a, b) => a - b);
|
|
149
|
+
const seen = new Set<string>();
|
|
150
|
+
const references: DecompilerResult["references"] = [];
|
|
151
|
+
for (const from of sorted) {
|
|
152
|
+
for (const { to, type } of instructions.get(from)!.references) {
|
|
153
|
+
const id = `${from}:${to}:${type}`;
|
|
154
|
+
if (seen.has(id)) continue;
|
|
155
|
+
seen.add(id);
|
|
156
|
+
references.push({ from, to, type });
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
return {
|
|
160
|
+
coverage: [{ start, end }],
|
|
161
|
+
functions: [...functions]
|
|
162
|
+
.filter((entry) => instructions.has(entry))
|
|
163
|
+
.sort((a, b) => a - b)
|
|
164
|
+
.map((entry) => {
|
|
165
|
+
const name = labels.get(entry);
|
|
166
|
+
return name !== undefined && SYMBOL_NAME.test(name) ? { entry, name, nameSource: "seed" as const } : { entry, name: `FUN_${hex4(entry)}`, nameSource: "generated" as const };
|
|
167
|
+
}),
|
|
168
|
+
regions: regions(start, end, (address) => (owner[address] !== -1 ? "code" : isData[address] ? "data" : undefined)),
|
|
169
|
+
references,
|
|
170
|
+
instructions: sorted.map((address): DecompilerInstruction => {
|
|
171
|
+
const { decoded, flows } = instructions.get(address)!;
|
|
172
|
+
return { address, bytes: decoded.bytes.map((byte) => byte.toString(16).padStart(2, "0")).join(""), mnemonic: decoded.mnemonic.toLowerCase(), operands: decoded.operands, flows };
|
|
173
|
+
}),
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** The target of the indirect jump at address, or undefined; a zero pointer is no target. */
|
|
178
|
+
function indirectTarget(address: number, decoded: Decoded, instructions: Map<number, Instruction>, owner: Int32Array, imageByte: (address: number) => number | undefined): number | undefined {
|
|
179
|
+
const chain: Decoded[] = [];
|
|
180
|
+
for (let at = address; chain.length < MAX_CHAIN && at > 0; ) {
|
|
181
|
+
const previous = owner[at - 1]!;
|
|
182
|
+
const instruction = previous === -1 ? undefined : instructions.get(previous);
|
|
183
|
+
if (instruction === undefined || !instruction.fallthrough) break;
|
|
184
|
+
chain.unshift(instruction.decoded);
|
|
185
|
+
at = previous;
|
|
186
|
+
}
|
|
187
|
+
const state = new PcodeState(imageByte);
|
|
188
|
+
for (const instruction of chain) if (state.run(instruction.pcode).kind === "call") state.forget();
|
|
189
|
+
const exit = state.run(decoded.pcode);
|
|
190
|
+
return exit.kind === "indirect" && exit.next !== undefined && exit.next !== 0 ? exit.next : undefined;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function regions(start: number, end: number, kind: (address: number) => "code" | "data" | undefined): DecompilerResult["regions"] {
|
|
194
|
+
const result: DecompilerResult["regions"] = [];
|
|
195
|
+
for (let address = start; address <= end; address++) {
|
|
196
|
+
const classification = kind(address);
|
|
197
|
+
if (classification === undefined) continue;
|
|
198
|
+
const previous = result.at(-1);
|
|
199
|
+
if (previous !== undefined && previous.classification === classification && previous.end === address - 1) previous.end = address;
|
|
200
|
+
else result.push({ start: address, end: address, classification });
|
|
201
|
+
}
|
|
202
|
+
return result;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
const hex4 = (value: number) => value.toString(16).padStart(4, "0");
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
// The Ghidra SLEIGH decoder and decompiler for the c64-re-tools 6510
|
|
2
|
+
// language, as a WebAssembly module (built from native/sleigh/). It has no imports and holds one program image at a time.
|
|
3
|
+
// A panic in the module traps: the instance is then dropped and the next
|
|
4
|
+
// load makes a new one.
|
|
5
|
+
|
|
6
|
+
import { Refusal } from "../../errors.ts";
|
|
7
|
+
import { C64RT_WASM } from "./c64rt-wasm.ts";
|
|
8
|
+
|
|
9
|
+
/** The decoder and decompiler, as a result names its tool. */
|
|
10
|
+
export const SLEIGH_VERSION = "ghidra-decompiler 2.1.0";
|
|
11
|
+
|
|
12
|
+
export interface Varnode {
|
|
13
|
+
/** "ram", "const", "register" or "unique". */
|
|
14
|
+
space: string;
|
|
15
|
+
offset: number;
|
|
16
|
+
size: number;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface PcodeOp {
|
|
20
|
+
/** Ghidra's name of the operation, for example "INT_ADD" or "CBRANCH". */
|
|
21
|
+
op: string;
|
|
22
|
+
out: Varnode | null;
|
|
23
|
+
in: Varnode[];
|
|
24
|
+
/** The name of a CALLOTHER operation, for example "jam". */
|
|
25
|
+
userop?: string | null;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface Decoded {
|
|
29
|
+
length: number;
|
|
30
|
+
bytes: number[];
|
|
31
|
+
mnemonic: string;
|
|
32
|
+
operands: string;
|
|
33
|
+
pcode: PcodeOp[];
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
interface Exports {
|
|
37
|
+
memory: WebAssembly.Memory;
|
|
38
|
+
alloc(length: number): number;
|
|
39
|
+
out_ptr(): number;
|
|
40
|
+
load(image: number, length: number, base: number): number;
|
|
41
|
+
decode(address: number): number;
|
|
42
|
+
name(address: number, text: number, length: number, kind: number): number;
|
|
43
|
+
decompile(address: number): number;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const utf8 = new TextDecoder();
|
|
47
|
+
|
|
48
|
+
let module: WebAssembly.Module | undefined;
|
|
49
|
+
let instance: Exports | undefined;
|
|
50
|
+
|
|
51
|
+
function exports(): Exports {
|
|
52
|
+
module ??= new WebAssembly.Module(Buffer.from(C64RT_WASM, "base64"));
|
|
53
|
+
instance ??= new WebAssembly.Instance(module, {}).exports as unknown as Exports;
|
|
54
|
+
return instance;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Runs one call; a trap drops the instance, and the reply's error becomes a Refusal. */
|
|
58
|
+
function call<T>(run: (wasm: Exports) => number): T {
|
|
59
|
+
const wasm = exports();
|
|
60
|
+
let length: number;
|
|
61
|
+
try {
|
|
62
|
+
length = run(wasm);
|
|
63
|
+
} catch (error) {
|
|
64
|
+
instance = undefined;
|
|
65
|
+
throw new Refusal("operation-failed", `the decompiler stopped: ${error instanceof Error ? error.message : String(error)}`);
|
|
66
|
+
}
|
|
67
|
+
const reply = JSON.parse(utf8.decode(new Uint8Array(wasm.memory.buffer, wasm.out_ptr(), length))) as T & { error?: string };
|
|
68
|
+
if (reply.error !== undefined) throw new Refusal("operation-failed", reply.error);
|
|
69
|
+
return reply;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Copies bytes into a buffer of the module, which the called function takes. */
|
|
73
|
+
function put(wasm: Exports, bytes: Uint8Array): number {
|
|
74
|
+
const pointer = wasm.alloc(bytes.length);
|
|
75
|
+
new Uint8Array(wasm.memory.buffer, pointer, bytes.length).set(bytes);
|
|
76
|
+
return pointer;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** The program image that decode, name and decompile work on: the bytes at base. */
|
|
80
|
+
export function loadImage(bytes: Uint8Array, base: number): void {
|
|
81
|
+
call((wasm) => wasm.load(put(wasm, bytes), bytes.length, base));
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** The instruction at an address of the loaded image. */
|
|
85
|
+
export function decode(address: number): Decoded {
|
|
86
|
+
return call((wasm) => wasm.decode(address));
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** Gives the decompiler the name of a routine or of a data address. */
|
|
90
|
+
export function setName(address: number, name: string, kind: "function" | "data"): void {
|
|
91
|
+
const text = new TextEncoder().encode(name);
|
|
92
|
+
call((wasm) => wasm.name(address, put(wasm, text), text.length, kind === "function" ? 0 : 1));
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** The C text of the routine at an address. */
|
|
96
|
+
export function decompile(address: number): string {
|
|
97
|
+
return call<{ text: string }>((wasm) => wasm.decompile(address)).text;
|
|
98
|
+
}
|
package/src/native/discover.ts
CHANGED
|
@@ -99,8 +99,8 @@ export function viceVersionOf(text: string): ViceVersion | undefined {
|
|
|
99
99
|
return { major: Number(match[1]), minor: Number(match[2]), ...(match[3] === undefined ? {} : { revision: Number(match[3]) }) };
|
|
100
100
|
}
|
|
101
101
|
|
|
102
|
-
/** What a VICE flaw stops from working: joystick input,
|
|
103
|
-
export type ViceFeature = "joystick" | "tape";
|
|
102
|
+
/** What a VICE flaw stops from working: joystick input, the load of a tape image, or keys of the keyboard matrix. */
|
|
103
|
+
export type ViceFeature = "joystick" | "tape" | "keyboard";
|
|
104
104
|
|
|
105
105
|
/** A known VICE flaw: the text for the person at the host, and what it stops from working. */
|
|
106
106
|
export interface ViceFlaw {
|
|
@@ -120,7 +120,7 @@ const VICE_FLAWS = [
|
|
|
120
120
|
{
|
|
121
121
|
upTo: { major: 3, minor: 10 },
|
|
122
122
|
fixedRevision: 45971,
|
|
123
|
-
breaks: ["joystick", "tape"],
|
|
123
|
+
breaks: ["joystick", "tape", "keyboard"],
|
|
124
124
|
text:
|
|
125
125
|
"VICE 3.10 and older hold bits 5-7 of CIA 1 ports A and B low with the joystick device that c64-re-tools uses (VICE bug #2202). " +
|
|
126
126
|
"$DC00 and $DC01 read $1F with no input. A program that waits for one key of the keyboard matrix (for example LDA $DC01 / CMP #$EF for SPACE) waits forever. " +
|
package/src/native/dxa.ts
CHANGED
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
// changed listing is refused instead of half imported.
|
|
7
7
|
|
|
8
8
|
import { writeFileSync } from "node:fs";
|
|
9
|
+
import { stripVTControlCharacters } from "node:util";
|
|
9
10
|
|
|
10
11
|
import { Refusal } from "../errors.ts";
|
|
11
12
|
import { SYMBOL_NAME } from "../c64.ts";
|
|
@@ -14,7 +15,17 @@ import { checkDxaResult, checkRequestBounds, imageRange, seedsInside, type DxaPa
|
|
|
14
15
|
import { Workspace } from "./staging.ts";
|
|
15
16
|
import { DXA, findTool } from "./discover.ts";
|
|
16
17
|
import { outputTail, runTool, runToolOrFail, type ToolContext } from "./run.ts";
|
|
17
|
-
|
|
18
|
+
|
|
19
|
+
/** dxa's version line, also as "D:\\tools\\dxa.exe v0.1.5 -- symbolic 65xx disassembler" on Windows. */
|
|
20
|
+
export const DXA_VERSION = /^(?:.*[\\/])?dxa(?:\.exe)? v\d/i;
|
|
21
|
+
|
|
22
|
+
/** The first output line that matches, without terminal color codes. */
|
|
23
|
+
export function versionLine(output: string, pattern: RegExp): string | undefined {
|
|
24
|
+
return stripVTControlCharacters(output)
|
|
25
|
+
.split(/\r?\n/)
|
|
26
|
+
.map((line) => line.trim())
|
|
27
|
+
.find((line) => pattern.test(line));
|
|
28
|
+
}
|
|
18
29
|
|
|
19
30
|
const TIMEOUT_MS = 120_000;
|
|
20
31
|
/** The listing is returned as an attachment; larger output is refused. */
|
package/src/native/processes.ts
CHANGED
|
@@ -91,7 +91,7 @@ export function windowsQuote(arg: string): string {
|
|
|
91
91
|
}
|
|
92
92
|
|
|
93
93
|
/**
|
|
94
|
-
* On Windows a .bat or .cmd file
|
|
94
|
+
* On Windows a .bat or .cmd file
|
|
95
95
|
* starts only through cmd.exe: Node refuses it without a shell (EINVAL). /s
|
|
96
96
|
* keeps the quoted command line as it is. Undefined for any other program.
|
|
97
97
|
*/
|
|
@@ -350,7 +350,7 @@ export class ProcessSupervisor {
|
|
|
350
350
|
} catch {
|
|
351
351
|
// The leader has ended already.
|
|
352
352
|
}
|
|
353
|
-
await Promise.race([exited, sleep(this.#graceMs)]);
|
|
353
|
+
await Promise.race([exited, sleep(this.#graceMs, undefined, { ref: false })]);
|
|
354
354
|
if (!hasExited(child)) throw new Refusal("operation-failed", `process ${pid} did not end${failure === undefined ? "" : `: ${failure}`}`);
|
|
355
355
|
return;
|
|
356
356
|
}
|
|
@@ -362,7 +362,7 @@ export class ProcessSupervisor {
|
|
|
362
362
|
}
|
|
363
363
|
}
|
|
364
364
|
// A group of zombies counts as gone before Node reaps the leader and reports its exit;
|
|
365
|
-
// stopped means that Node has seen that exit.
|
|
366
|
-
await Promise.race([exited, sleep(KILL_WAIT_MS)]);
|
|
365
|
+
// stopped means that Node has seen that exit. The limit does not keep this process running.
|
|
366
|
+
await Promise.race([exited, sleep(KILL_WAIT_MS, undefined, { ref: false })]);
|
|
367
367
|
}
|
|
368
368
|
}
|