@henols/c64-re-tools 2.1.3 → 2.1.5

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 (37) hide show
  1. package/README.md +3 -3
  2. package/package.json +2 -1
  3. package/skills/c64-assembler/SKILL.md +1 -1
  4. package/skills/c64-assembler/scripts/assembler.ts +2 -2
  5. package/skills/c64-docs/SKILL.md +1 -1
  6. package/skills/c64-docs/scripts/docs.ts +5 -3
  7. package/skills/c64-emulator/SKILL.md +2 -0
  8. package/skills/c64-knowledge/SKILL.md +1 -0
  9. package/skills/c64-memory-map/SKILL.md +1 -1
  10. package/skills/c64-memory-map/scripts/memory-map.ts +4 -2
  11. package/skills/c64-provenance/SKILL.md +1 -1
  12. package/skills/c64-provenance/scripts/lib/compare.ts +12 -0
  13. package/skills/c64-provenance/scripts/lib/library.ts +7 -2
  14. package/skills/c64-provenance/scripts/provenance.ts +13 -7
  15. package/skills/c64-source/SKILL.md +2 -1
  16. package/skills/c64-source/scripts/source.ts +31 -15
  17. package/skills/c64-static-analysis/scripts/lib/findings.ts +24 -2
  18. package/skills/c64-testing/references/scenario-format.md +2 -0
  19. package/skills/c64-testing/scripts/lib/runs.ts +1 -1
  20. package/skills/c64-testing/scripts/lib/scenario.ts +4 -3
  21. package/skills/c64-testing/scripts/testing.ts +1 -1
  22. package/skills/c64-unpacker/SKILL.md +3 -2
  23. package/skills/c64-unpacker/scripts/lib/loads.ts +5 -2
  24. package/src/host/vice/session.ts +22 -2
  25. package/src/host-client/vice-session.ts +3 -3
  26. package/src/knowledge/write.ts +46 -1
  27. package/src/mcp/tools/media.ts +3 -4
  28. package/src/media.ts +3 -0
  29. package/src/native/c64run.ts +1 -1
  30. package/src/native/discover.ts +2 -2
  31. package/src/project.ts +6 -2
  32. package/src/script.ts +9 -1
  33. package/tools/c64run.c +655 -0
  34. package/tools/install-chips.sh +66 -0
  35. package/tools/install-container.sh +61 -0
  36. package/tools/install-dxa.sh +336 -0
  37. package/tools/install-ghidra.sh +40 -0
package/tools/c64run.c ADDED
@@ -0,0 +1,655 @@
1
+ // c64run: the replay core of c64-re-tools. A PAL C64 (floooh/chips c64.h) with a
2
+ // line-command interface: one command per line on stdin, one answer line on
3
+ // stdout ("ok ..." or "err ..."). Time is counted in CPU cycles ("ticks") from
4
+ // the opcode fetch at the reset vector (tick 0).
5
+ //
6
+ // It records and replays the reads of chosen I/O addresses ("inputs", for
7
+ // example the joystick ports and the SID noise), keyed by site (the pc of the
8
+ // reading instruction) and read index, and writes coverage maps and samples
9
+ // of RAM at a routine entry. chips is not changed: the harness calls
10
+ // _c64_tick and edits the data pins between two ticks.
11
+
12
+ #define CHIPS_IMPL
13
+ #include <stdio.h>
14
+ #include <stdlib.h>
15
+ #include <string.h>
16
+ #include <stdint.h>
17
+ #include "chips/chips_common.h"
18
+ #include "chips/m6502.h"
19
+ #include "chips/m6526.h"
20
+ #include "chips/m6569.h"
21
+ #include "chips/m6581.h"
22
+ #include "chips/kbd.h"
23
+ #include "chips/mem.h"
24
+ #include "chips/clk.h"
25
+ #include "systems/c1530.h"
26
+ #include "chips/m6522.h"
27
+ #include "systems/c1541.h"
28
+ #include "systems/c64.h"
29
+
30
+ #define C64RUN_PROTOCOL 2
31
+ // Keys that chips has no code for: each SHIFT alone, and the up arrow.
32
+ #define C64RUN_KEY_LSHIFT 0x11
33
+ #define C64RUN_KEY_RSHIFT 0x12
34
+ #define C64RUN_KEY_UPARROW 0x5E
35
+ #ifndef CHIPS_COMMIT
36
+ #define CHIPS_COMMIT "unknown"
37
+ #endif
38
+
39
+ static c64_t sys;
40
+ static uint64_t pins;
41
+ static uint64_t ticks;
42
+ static int ready;
43
+ static uint16_t last_sync_pc;
44
+
45
+ // ---- Sites: the reads of input addresses, counted, recorded and replayed ----
46
+
47
+ typedef struct { uint64_t from; uint8_t value; } run_t;
48
+ typedef struct {
49
+ uint16_t pc, addr;
50
+ int rom; // the reading instruction is in ROM (KERNAL or BASIC)
51
+ uint64_t count; // reads at this site so far
52
+ int last; // the last value recorded (-1: none yet)
53
+ run_t* runs; // replay: the value from each read index on
54
+ size_t nruns, cap, at;
55
+ uint64_t total; // replay: the reads in the recording
56
+ uint64_t misses; // replay: reads after the recording ended
57
+ } site_t;
58
+ #define MAX_SITES 1024
59
+ static site_t sites[MAX_SITES];
60
+ static int site_count;
61
+ static uint8_t input_addr[0x1000];
62
+ static FILE* record_file;
63
+ static int replaying;
64
+ static uint64_t replay_unknown;
65
+ static uint16_t map_from[MAX_SITES], map_to[MAX_SITES];
66
+ static int maps;
67
+
68
+ // A watch stops run-reads when the count of one site reaches a number.
69
+ static site_t* watch_site;
70
+ static uint16_t watch_pc, watch_addr;
71
+ static uint64_t watch_count;
72
+ static int watch_hit;
73
+
74
+ static int area(uint16_t addr);
75
+ static int coverage_on, coverage_armed_input;
76
+
77
+ static site_t* site_for(uint16_t pc, uint16_t addr) {
78
+ for (int i = 0; i < site_count; i++) if (sites[i].pc == pc && sites[i].addr == addr) return &sites[i];
79
+ if (site_count == MAX_SITES) return NULL;
80
+ site_t* s = &sites[site_count++];
81
+ memset(s, 0, sizeof *s);
82
+ s->pc = pc;
83
+ s->addr = addr;
84
+ s->rom = ready && area(pc) == 1;
85
+ s->last = -1;
86
+ return s;
87
+ }
88
+
89
+ static void clear_sites(void) {
90
+ for (int i = 0; i < site_count; i++) free(sites[i].runs);
91
+ site_count = 0;
92
+ watch_site = NULL;
93
+ }
94
+
95
+ // The value of an input read that the recording has no value for: a site that it
96
+ // does not know, or a read after its end. A fixed value keeps the replay free of
97
+ // values that depend on timing: no joystick, key or paddle ($FF), else $00.
98
+ static uint8_t idle_value(uint16_t addr) {
99
+ if (addr == 0xDC00 || addr == 0xDC01 || addr == 0xD419 || addr == 0xD41A) return 0xFF;
100
+ return 0x00;
101
+ }
102
+
103
+ static void input_read(uint16_t pc, uint16_t addr) {
104
+ site_t* s = site_for(pc, addr);
105
+ if (s == NULL) {
106
+ if (replaying) { M6502_SET_DATA(pins, idle_value(addr)); replay_unknown++; }
107
+ return;
108
+ }
109
+ if (coverage_armed_input && !s->rom) { coverage_armed_input = 0; coverage_on = 1; }
110
+ const uint64_t index = s->count++;
111
+ if (replaying) {
112
+ if (s->nruns == 0) {
113
+ M6502_SET_DATA(pins, idle_value(addr));
114
+ replay_unknown++;
115
+ } else if (index >= s->total) {
116
+ M6502_SET_DATA(pins, idle_value(addr));
117
+ s->misses++;
118
+ } else {
119
+ while (s->at + 1 < s->nruns && s->runs[s->at + 1].from <= index) s->at++;
120
+ M6502_SET_DATA(pins, s->runs[s->at].value);
121
+ }
122
+ }
123
+ if (record_file) {
124
+ const uint8_t value = M6502_GET_DATA(pins);
125
+ if (s->last != value) {
126
+ fprintf(record_file, "%04x %04x %llu %02x\n", pc, addr, (unsigned long long)index, value);
127
+ s->last = value;
128
+ }
129
+ }
130
+ if (s == watch_site && s->count >= watch_count) watch_hit = 1;
131
+ else if (watch_site == NULL && watch_count && pc == watch_pc && addr == watch_addr) {
132
+ watch_site = s;
133
+ if (s->count >= watch_count) watch_hit = 1;
134
+ }
135
+ }
136
+
137
+ // ---- Coverage: one byte per address, the bit format of src/c64.ts COVERAGE ----
138
+
139
+ enum { COV_RAM_R = 1, COV_RAM_W = 2, COV_RAM_X = 4, COV_ROM_R = 8, COV_ROM_X = 16, COV_IO_R = 32, COV_IO_W = 64, COV_IO_X = 128 };
140
+ static uint8_t coverage[0x10000];
141
+ // Set by "coverage on-checkpoint": the map starts at the first sample. coverage_armed_input,
142
+ // set by "coverage on-input": the map starts at the first input read of code in RAM.
143
+ static int coverage_armed;
144
+
145
+ // 0: RAM, 1: ROM, 2: I/O, as the CPU sees the address now (the CPU port bits).
146
+ static int area(uint16_t addr) {
147
+ const uint8_t port = sys.cpu_port;
148
+ if (addr < 0xA000 || (addr >= 0xC000 && addr < 0xD000)) return 0;
149
+ if ((port & 3) == 0) return 0;
150
+ if (addr < 0xC000) return (port & 3) == 3 ? 1 : 0;
151
+ if (addr < 0xE000) return (port & 4) ? 2 : 1;
152
+ return (port & 2) ? 1 : 0;
153
+ }
154
+
155
+ // As the emulator (VICE) marks a map: an opcode fetch is an execute of its byte,
156
+ // and the reads of the next two bytes (the operand, or the dummy read of a
157
+ // one-byte instruction) are not counted.
158
+ static void cover(uint16_t addr, int sync, int read) {
159
+ const int where = area(addr);
160
+ uint8_t bit;
161
+ if (sync) bit = where == 0 ? COV_RAM_X : where == 1 ? COV_ROM_X : COV_IO_X;
162
+ else if (read) {
163
+ if ((uint16_t)(addr - last_sync_pc - 1) < 2) return;
164
+ bit = where == 0 ? COV_RAM_R : where == 1 ? COV_ROM_R : COV_IO_R;
165
+ }
166
+ else bit = where == 2 ? COV_IO_W : COV_RAM_W;
167
+ coverage[addr] |= bit;
168
+ }
169
+
170
+ // ---- Samples: at each opcode fetch at one pc, the tick and some RAM ranges ----
171
+
172
+ static FILE* sample_file;
173
+ static uint16_t sample_pc;
174
+ static uint16_t sample_from[16], sample_size[16];
175
+ static int sample_ranges;
176
+ static uint64_t samples, sample_stop;
177
+ // Set when the sample count reaches sample_stop: every run command stops there.
178
+ static int halted;
179
+
180
+ // Pins: at each sample, before it is taken, the next line of a pin file
181
+ // ("hex hex ...", one field for each range) goes into RAM. A replay pins the
182
+ // random state of a rebuild to the values of the original this way.
183
+ static FILE* pin_file;
184
+ static uint16_t pin_from[16], pin_size[16];
185
+ static int pin_ranges;
186
+ static uint64_t pinned;
187
+
188
+ static void pin(void) {
189
+ for (int r = 0; r < pin_ranges; r++) {
190
+ for (int i = 0; i < pin_size[r]; i++) {
191
+ unsigned value;
192
+ if (fscanf(pin_file, " %2x", &value) != 1) { fclose(pin_file); pin_file = NULL; return; }
193
+ sys.ram[(pin_from[r] + i) & 0xFFFF] = (uint8_t)value;
194
+ }
195
+ }
196
+ pinned++;
197
+ }
198
+
199
+ static void sample(void) {
200
+ if (pin_file) pin();
201
+ fprintf(sample_file, "%llu", (unsigned long long)ticks);
202
+ for (int r = 0; r < sample_ranges; r++) {
203
+ fputc(' ', sample_file);
204
+ for (int i = 0; i < sample_size[r]; i++) fprintf(sample_file, "%02x", sys.ram[(sample_from[r] + i) & 0xFFFF]);
205
+ }
206
+ fputc('\n', sample_file);
207
+ samples++;
208
+ if (coverage_armed) { coverage_armed = 0; coverage_on = 1; }
209
+ if (sample_stop && samples >= sample_stop) halted = 1;
210
+ }
211
+
212
+ // ---- The tick ----
213
+
214
+ static void tick(void) {
215
+ pins = _c64_tick(&sys, pins);
216
+ // A read cycle repeats while the VIC-II holds RDY (the CPU sees it on its
217
+ // next tick); only the tick that returns with RDY off is the read.
218
+ const int decoded = !(pins & M6502_RDY);
219
+ const uint16_t addr = M6502_GET_ADDR(pins);
220
+ const int read = (pins & M6502_RW) != 0;
221
+ const int sync = (pins & M6502_SYNC) != 0;
222
+ if (sync) {
223
+ last_sync_pc = addr;
224
+ if (sample_file && addr == sample_pc && decoded) sample();
225
+ }
226
+ if (read && !sync && decoded && sys.io_mapped && (addr & 0xF000) == 0xD000 && input_addr[addr & 0x0FFF]) {
227
+ input_read(last_sync_pc, addr);
228
+ }
229
+ if (coverage_on && (decoded || !read) && !M6510_CHECK_IO(pins)) cover(addr, sync, read);
230
+ ticks++;
231
+ }
232
+
233
+ // ---- Files and helpers ----
234
+
235
+ static uint8_t* read_file(const char* path, size_t want) {
236
+ FILE* f = fopen(path, "rb");
237
+ if (!f) return NULL;
238
+ uint8_t* buf = malloc(want);
239
+ size_t got = fread(buf, 1, want, f);
240
+ fclose(f);
241
+ if (got != want) { free(buf); return NULL; }
242
+ return buf;
243
+ }
244
+
245
+ static int write_file(const char* path, const void* data, size_t size) {
246
+ FILE* f = fopen(path, "wb");
247
+ if (!f) return 0;
248
+ size_t put = fwrite(data, 1, size, f);
249
+ fclose(f);
250
+ return put == size;
251
+ }
252
+
253
+ // FNV-1a over RAM from $0002 (not the RAM under the CPU port) and the low nibbles of colour RAM.
254
+ static uint32_t state_hash(void) {
255
+ uint32_t h = 2166136261u;
256
+ for (int i = 2; i < 0x10000; i++) { h ^= sys.ram[i]; h *= 16777619u; }
257
+ for (int i = 0; i < 0x400; i++) { h ^= sys.color_ram[i] & 0x0F; h *= 16777619u; }
258
+ return h;
259
+ }
260
+
261
+ static void status(const char* prefix) {
262
+ if (halted) prefix = "ok halted";
263
+ printf("%s tick=%llu sync=%d pc=%04x a=%02x x=%02x y=%02x s=%02x p=%02x line=%u cycle=%u hash=%08x\n", prefix,
264
+ (unsigned long long)ticks, (pins & M6502_SYNC) ? 1 : 0, (pins & M6502_SYNC) ? M6502_GET_ADDR(pins) : 0xFFFF,
265
+ sys.cpu.A, sys.cpu.X, sys.cpu.Y, sys.cpu.S, sys.cpu.P, sys.vic.rs.v_count, sys.vic.rs.h_count, state_hash());
266
+ }
267
+
268
+ static const char* rom_names[3] = { "kernal-901227-03.bin", "basic-901226-01.bin", "chargen-901225-01.bin" };
269
+
270
+ // The ROM folder: the argument, C64RT_C64_ROMS, or ../share/c64-re-tools/roms next to the program.
271
+ static void rom_dir(const char* given, const char* argv0, char* out, size_t size) {
272
+ if (given && given[0]) { snprintf(out, size, "%s", given); return; }
273
+ const char* env = getenv("C64RT_C64_ROMS");
274
+ if (env && env[0]) { snprintf(out, size, "%s", env); return; }
275
+ // Started by name: find the program on PATH.
276
+ static char found[1024];
277
+ if (!strchr(argv0, '/') && !strchr(argv0, '\\')) {
278
+ #ifdef _WIN32
279
+ const char separator = ';';
280
+ #else
281
+ const char separator = ':';
282
+ #endif
283
+ const char* path = getenv("PATH");
284
+ while (path && *path) {
285
+ const char* next = strchr(path, separator);
286
+ const int length = next ? (int)(next - path) : (int)strlen(path);
287
+ snprintf(found, sizeof found, "%.*s/%s", length, path, argv0);
288
+ FILE* f = fopen(found, "rb");
289
+ if (f) { fclose(f); argv0 = found; break; }
290
+ path = next ? next + 1 : NULL;
291
+ }
292
+ }
293
+ const char* slash = strrchr(argv0, '/');
294
+ const char* back = strrchr(argv0, '\\');
295
+ if (back && (!slash || back > slash)) slash = back;
296
+ if (slash) snprintf(out, size, "%.*s/../share/c64-re-tools/roms", (int)(slash - argv0), argv0);
297
+ else snprintf(out, size, "../share/c64-re-tools/roms");
298
+ }
299
+
300
+ static const char* init(const char* romdir) {
301
+ static char missing[1200];
302
+ char path[1100];
303
+ uint8_t* rom[3];
304
+ const size_t sizes[3] = { 0x2000, 0x2000, 0x1000 };
305
+ for (int i = 0; i < 3; i++) {
306
+ snprintf(path, sizeof path, "%s/%s", romdir, rom_names[i]);
307
+ rom[i] = read_file(path, sizes[i]);
308
+ if (!rom[i]) { snprintf(missing, sizeof missing, "no ROM %s (%zu bytes)", path, sizes[i]); return missing; }
309
+ }
310
+ if (ready) c64_discard(&sys);
311
+ c64_init(&sys, &(c64_desc_t){
312
+ .roms = {
313
+ .kernal = { .ptr = rom[0], .size = 0x2000 },
314
+ .basic = { .ptr = rom[1], .size = 0x2000 },
315
+ .chars = { .ptr = rom[2], .size = 0x1000 },
316
+ },
317
+ });
318
+ // The matrix positions (column, line) of the keys that chips has no code for.
319
+ kbd_register_key(&sys.kbd, C64RUN_KEY_LSHIFT, 7, 1, 0);
320
+ kbd_register_key(&sys.kbd, C64RUN_KEY_RSHIFT, 4, 6, 0);
321
+ kbd_register_key(&sys.kbd, C64RUN_KEY_UPARROW, 6, 6, 0);
322
+ const uint16_t vector = rom[0][0x1FFC] | (rom[0][0x1FFD] << 8);
323
+ for (int i = 0; i < 3; i++) free(rom[i]);
324
+ pins = sys.pins;
325
+ ticks = 0;
326
+ for (int i = 0; i < 100; i++) {
327
+ tick();
328
+ if ((pins & M6502_SYNC) && M6502_GET_ADDR(pins) == vector) {
329
+ ticks = 0;
330
+ ready = 1;
331
+ return NULL;
332
+ }
333
+ }
334
+ return "the CPU did not reach the reset vector";
335
+ }
336
+
337
+ static int hexbyte(const char* s) {
338
+ unsigned v;
339
+ return sscanf(s, "%2x", &v) == 1 ? (int)v : -1;
340
+ }
341
+
342
+ static const char B64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
343
+
344
+ static void print_base64(const uint8_t* data, size_t size) {
345
+ for (size_t i = 0; i < size; i += 3) {
346
+ const uint32_t n = (data[i] << 16) | (i + 1 < size ? data[i + 1] << 8 : 0) | (i + 2 < size ? data[i + 2] : 0);
347
+ putchar(B64[(n >> 18) & 63]);
348
+ putchar(B64[(n >> 12) & 63]);
349
+ putchar(i + 1 < size ? B64[(n >> 6) & 63] : '=');
350
+ putchar(i + 2 < size ? B64[n & 63] : '=');
351
+ }
352
+ }
353
+
354
+ // Loads a recording ("site addr from value" lines and "end site addr count" lines).
355
+ static const char* load_replay(const char* path) {
356
+ static char problem[1200];
357
+ FILE* f = fopen(path, "r");
358
+ if (!f) { snprintf(problem, sizeof problem, "cannot read %s", path); return problem; }
359
+ char row[256];
360
+ int line = 0;
361
+ while (fgets(row, sizeof row, f)) {
362
+ line++;
363
+ unsigned pc, addr, value;
364
+ unsigned long long n;
365
+ int end = 0;
366
+ if (sscanf(row, "end %x %x %llu", &pc, &addr, &n) == 3) end = 1;
367
+ else if (sscanf(row, "%x %x %llu %x", &pc, &addr, &n, &value) != 4) {
368
+ if (row[0] == '\n' || row[0] == '#') continue;
369
+ fclose(f);
370
+ snprintf(problem, sizeof problem, "%s line %d is not \"site addr from value\" or \"end site addr count\"", path, line);
371
+ return problem;
372
+ }
373
+ for (int i = 0; i < maps; i++) if (map_from[i] == pc) { pc = map_to[i]; break; }
374
+ site_t* s = site_for(pc, addr);
375
+ if (!s) { fclose(f); snprintf(problem, sizeof problem, "more than %d sites", MAX_SITES); return problem; }
376
+ if (end) { s->total = n; continue; }
377
+ if (s->nruns == s->cap) { s->cap = s->cap ? s->cap * 2 : 64; s->runs = realloc(s->runs, s->cap * sizeof(run_t)); }
378
+ s->runs[s->nruns++] = (run_t){ .from = n, .value = (uint8_t)value };
379
+ }
380
+ fclose(f);
381
+ return NULL;
382
+ }
383
+
384
+ static int need_ready(void) {
385
+ if (!ready) printf("err give init first\n");
386
+ return ready;
387
+ }
388
+
389
+ int main(int argc, char** argv) {
390
+ setvbuf(stdout, NULL, _IOLBF, 0);
391
+ if (argc > 1 && !strcmp(argv[1], "--version")) {
392
+ // The version, and whether the default ROM folder has the three ROMs.
393
+ char dir[1024], path[1100];
394
+ rom_dir(NULL, argv[0], dir, sizeof dir);
395
+ const char* absent = NULL;
396
+ for (int i = 0; i < 3 && !absent; i++) {
397
+ snprintf(path, sizeof path, "%s/%s", dir, rom_names[i]);
398
+ FILE* f = fopen(path, "rb");
399
+ if (f) fclose(f); else absent = rom_names[i];
400
+ }
401
+ printf("c64run protocol=%d chips=%s\n", C64RUN_PROTOCOL, CHIPS_COMMIT);
402
+ if (absent) printf("roms=missing %s has no %s\n", dir, absent); else printf("roms=ok %s\n", dir);
403
+ return 0;
404
+ }
405
+ static char line[1 << 17], cmd[64], a1[1024], a2[1 << 16];
406
+ while (fgets(line, sizeof line, stdin)) {
407
+ a1[0] = a2[0] = 0;
408
+ int n = sscanf(line, "%63s %1023s %65535s", cmd, a1, a2);
409
+ if (n < 1) continue;
410
+ if (!strcmp(cmd, "version")) {
411
+ printf("ok c64run protocol=%d chips=%s\n", C64RUN_PROTOCOL, CHIPS_COMMIT);
412
+ } else if (!strcmp(cmd, "init")) {
413
+ // init [<rom folder>]: power on; the CPU stops at the reset vector, tick 0
414
+ char dir[1024];
415
+ rom_dir(n >= 2 ? a1 : NULL, argv[0], dir, sizeof dir);
416
+ clear_sites();
417
+ replaying = 0;
418
+ replay_unknown = 0;
419
+ last_sync_pc = 0;
420
+ halted = 0;
421
+ sample_stop = 0;
422
+ maps = 0;
423
+ coverage_on = coverage_armed = coverage_armed_input = 0;
424
+ if (pin_file) { fclose(pin_file); pin_file = NULL; }
425
+ if (record_file) { fclose(record_file); record_file = NULL; }
426
+ if (sample_file) { fclose(sample_file); sample_file = NULL; }
427
+ memset(coverage, 0, sizeof coverage);
428
+ const char* problem = init(dir);
429
+ if (problem) printf("err init: %s\n", problem); else status("ok");
430
+ } else if (!strcmp(cmd, "quit")) {
431
+ break;
432
+ } else if (!need_ready()) {
433
+ continue;
434
+ } else if (!strcmp(cmd, "status")) {
435
+ status("ok");
436
+ } else if (!strcmp(cmd, "ram-load")) {
437
+ // ram-load <file>: 64 KiB into RAM (the RAM view)
438
+ uint8_t* b = read_file(a1, 0x10000);
439
+ if (b) { memcpy(sys.ram, b, 0x10000); free(b); printf("ok\n"); } else printf("err ram-load: cannot read 65536 bytes from %s\n", a1);
440
+ } else if (!strcmp(cmd, "ram-dump")) {
441
+ printf(write_file(a1, sys.ram, 0x10000) ? "ok\n" : "err ram-dump: cannot write %s\n", a1);
442
+ } else if (!strcmp(cmd, "write")) {
443
+ // write <addr hex> <bytes hex>: into RAM (the RAM view)
444
+ unsigned addr;
445
+ size_t len = strlen(a2);
446
+ if (sscanf(a1, "%x", &addr) != 1 || len % 2) { printf("err write: give an address and hex bytes\n"); continue; }
447
+ for (size_t i = 0; i < len / 2; i++) sys.ram[(addr + i) & 0xFFFF] = (uint8_t)hexbyte(a2 + 2 * i);
448
+ printf("ok %zu\n", len / 2);
449
+ } else if (!strcmp(cmd, "peek")) {
450
+ // peek <addr hex> <count>: RAM bytes (the RAM view) as hex
451
+ unsigned addr, count;
452
+ if (sscanf(line, "%*s %x %u", &addr, &count) != 2 || count > 4096) { printf("err peek: give an address and 1-4096\n"); continue; }
453
+ printf("ok ");
454
+ for (unsigned i = 0; i < count; i++) printf("%02x", sys.ram[(addr + i) & 0xFFFF]);
455
+ printf("\n");
456
+ } else if (!strcmp(cmd, "regs")) {
457
+ // regs <a> <x> <y> <s> <p> (hex)
458
+ unsigned a, x, y, s, p;
459
+ if (sscanf(line, "%*s %x %x %x %x %x", &a, &x, &y, &s, &p) != 5) { printf("err regs: give a x y s p in hex\n"); continue; }
460
+ sys.cpu.A = a; sys.cpu.X = x; sys.cpu.Y = y; sys.cpu.S = s; sys.cpu.P = p;
461
+ status("ok");
462
+ } else if (!strcmp(cmd, "goto")) {
463
+ // goto <pc hex>: continue at pc, at an opcode fetch (the "prefetch" of m6502.h)
464
+ unsigned pc;
465
+ if (sscanf(a1, "%x", &pc) != 1 || !(pins & M6502_SYNC)) { printf("err goto: give a pc, at an opcode fetch (run-sync first)\n"); continue; }
466
+ pins = M6502_SYNC | M6502_RW;
467
+ M6502_SET_ADDR(pins, pc);
468
+ M6502_SET_DATA(pins, mem_rd(&sys.mem_cpu, pc));
469
+ m6502_set_pc(&sys.cpu, pc);
470
+ last_sync_pc = pc;
471
+ status("ok");
472
+ } else if (!strcmp(cmd, "run")) {
473
+ // run <tick>: run to the absolute tick (a tick in the past does nothing)
474
+ const unsigned long long target = strtoull(a1, NULL, 10);
475
+ while (ticks < target && !halted) tick();
476
+ status("ok");
477
+ } else if (!strcmp(cmd, "run-sync")) {
478
+ // run-sync: run to the next opcode fetch
479
+ do tick(); while (!(pins & M6502_SYNC) && !halted);
480
+ status("ok");
481
+ } else if (!strcmp(cmd, "run-pc")) {
482
+ // run-pc <pc hex> <until tick>: run to the next opcode fetch at pc
483
+ unsigned pc;
484
+ const unsigned long long until = strtoull(a2, NULL, 10);
485
+ if (sscanf(a1, "%x", &pc) != 1) { printf("err run-pc: give a pc and a tick\n"); continue; }
486
+ do tick(); while (!((pins & M6502_SYNC) && M6502_GET_ADDR(pins) == pc) && ticks < until && !halted);
487
+ status((pins & M6502_SYNC) && M6502_GET_ADDR(pins) == pc ? "ok reached" : "ok not-reached");
488
+ } else if (!strcmp(cmd, "run-reads")) {
489
+ // run-reads <pc hex> <addr hex> <n> <until tick>: run until the site has n reads
490
+ unsigned pc, addr;
491
+ unsigned long long count, until;
492
+ if (sscanf(line, "%*s %x %x %llu %llu", &pc, &addr, &count, &until) != 4) { printf("err run-reads: give a site, a count and a tick\n"); continue; }
493
+ watch_pc = pc;
494
+ watch_addr = addr;
495
+ watch_count = count;
496
+ watch_site = NULL;
497
+ for (int i = 0; i < site_count; i++) if (sites[i].pc == pc && sites[i].addr == addr) watch_site = &sites[i];
498
+ watch_hit = watch_site && watch_site->count >= count;
499
+ while (!watch_hit && ticks < until && !halted) tick();
500
+ watch_count = 0;
501
+ watch_site = NULL;
502
+ status(watch_hit ? "ok reached" : "ok not-reached");
503
+ } else if (!strcmp(cmd, "reads")) {
504
+ // reads <pc hex> <addr hex>: the read count of a site
505
+ unsigned pc, addr;
506
+ if (sscanf(line, "%*s %x %x", &pc, &addr) != 2) { printf("err reads: give a site\n"); continue; }
507
+ uint64_t count = 0;
508
+ for (int i = 0; i < site_count; i++) if (sites[i].pc == pc && sites[i].addr == addr) count = sites[i].count;
509
+ printf("ok count=%llu\n", (unsigned long long)count);
510
+ } else if (!strcmp(cmd, "joy")) {
511
+ // joy <port 1 mask> <port 2 mask> (hex): 1 up, 2 down, 4 left, 8 right, 10 fire
512
+ unsigned j1, j2;
513
+ if (sscanf(line, "%*s %x %x", &j1, &j2) != 2) { printf("err joy: give two masks\n"); continue; }
514
+ c64_joystick(&sys, (uint8_t)j1, (uint8_t)j2);
515
+ printf("ok\n");
516
+ } else if (!strcmp(cmd, "key")) {
517
+ // key <code hex> down|up: a key of the keyboard matrix; the code is ASCII as
518
+ // chips maps it (A-Z, 0-9, space 20, return 0d, ...); shifted keys press SHIFT.
519
+ // 11 and 12 are the left and right SHIFT alone, 5e the up arrow, ff RESTORE (NMI).
520
+ unsigned code;
521
+ if (sscanf(a1, "%x", &code) != 1 || (strcmp(a2, "down") && strcmp(a2, "up"))) { printf("err key: give a code and down or up\n"); continue; }
522
+ if (!strcmp(a2, "down")) c64_key_down(&sys, (int)code);
523
+ else {
524
+ // chips releases a key in kbd_update, after its sticky time: one call
525
+ // passes that time, the next one releases.
526
+ c64_key_up(&sys, (int)code);
527
+ kbd_update(&sys.kbd, 20000);
528
+ kbd_update(&sys.kbd, 0);
529
+ }
530
+ printf("ok\n");
531
+ } else if (!strcmp(cmd, "inputs")) {
532
+ // inputs <addr|from-to,...>: the I/O addresses that record and replay handle
533
+ memset(input_addr, 0, sizeof input_addr);
534
+ int count = 0;
535
+ for (char* part = strtok(a1, ","); part; part = strtok(NULL, ",")) {
536
+ unsigned from, to;
537
+ int fields = sscanf(part, "%x-%x", &from, &to);
538
+ if (fields == 1) to = from;
539
+ if (fields < 1 || (from & 0xF000) != 0xD000 || (to & 0xF000) != 0xD000 || to < from) { count = -1; break; }
540
+ for (unsigned addr = from; addr <= to; addr++) { input_addr[addr & 0x0FFF] = 1; count++; }
541
+ }
542
+ if (count < 0) printf("err inputs: give addresses in $D000-$DFFF\n"); else printf("ok inputs=%d\n", count);
543
+ } else if (!strcmp(cmd, "record")) {
544
+ // record <file> | off: "site addr from value" when the value at a site changes;
545
+ // off adds "end site addr count" for each site
546
+ if (record_file) {
547
+ for (int i = 0; i < site_count; i++) fprintf(record_file, "end %04x %04x %llu\n", sites[i].pc, sites[i].addr, (unsigned long long)sites[i].count);
548
+ fclose(record_file);
549
+ record_file = NULL;
550
+ }
551
+ if (!strcmp(a1, "off")) { printf("ok\n"); continue; }
552
+ if (replaying) { printf("err record: a replay runs\n"); continue; }
553
+ for (int i = 0; i < site_count; i++) sites[i].last = -1;
554
+ record_file = fopen(a1, "w");
555
+ if (record_file) printf("ok\n"); else printf("err record: cannot write %s\n", a1);
556
+ } else if (!strcmp(cmd, "site-map")) {
557
+ // site-map <file> | off: "old new" pc pairs that a following replay uses
558
+ maps = 0;
559
+ if (!strcmp(a1, "off")) { printf("ok maps=0\n"); continue; }
560
+ FILE* m = fopen(a1, "r");
561
+ if (!m) { printf("err site-map: cannot read %s\n", a1); continue; }
562
+ unsigned from, to;
563
+ while (maps < MAX_SITES && fscanf(m, "%x %x", &from, &to) == 2) { map_from[maps] = from; map_to[maps] = to; maps++; }
564
+ fclose(m);
565
+ printf("ok maps=%d\n", maps);
566
+ } else if (!strcmp(cmd, "replay")) {
567
+ // replay <file>: the reads of input addresses at each site get the recorded values
568
+ if (record_file) { printf("err replay: a recording runs\n"); continue; }
569
+ const char* problem = load_replay(a1);
570
+ if (problem) { printf("err replay: %s\n", problem); continue; }
571
+ replaying = 1;
572
+ replay_unknown = 0;
573
+ int with = 0;
574
+ for (int i = 0; i < site_count; i++) with += sites[i].nruns > 0;
575
+ printf("ok sites=%d\n", with);
576
+ } else if (!strcmp(cmd, "sites")) {
577
+ // sites: each site as pc/addr:reads:ram|rom (in a replay also :recorded:misses)
578
+ printf("ok unknown=%llu", (unsigned long long)replay_unknown);
579
+ for (int i = 0; i < site_count; i++) {
580
+ printf(" %04x/%04x:%llu:%s", sites[i].pc, sites[i].addr, (unsigned long long)sites[i].count, sites[i].rom ? "rom" : "ram");
581
+ if (replaying) printf(":%llu:%llu", (unsigned long long)sites[i].total, (unsigned long long)sites[i].misses);
582
+ }
583
+ printf("\n");
584
+ } else if (!strcmp(cmd, "sample")) {
585
+ // sample <pc hex> <file> <addr:size,...> [<stop>] | off: RAM at each opcode fetch at
586
+ // pc; with <stop>, every run command stops after that many samples ("ok halted")
587
+ if (sample_file) { fclose(sample_file); sample_file = NULL; }
588
+ if (!strcmp(a1, "off")) { printf("ok samples=%llu\n", (unsigned long long)samples); continue; }
589
+ char path[1024], ranges[1024];
590
+ unsigned pc;
591
+ unsigned long long stop = 0;
592
+ if (sscanf(line, "%*s %x %1023s %1023s %llu", &pc, path, ranges, &stop) < 3) { printf("err sample: give a pc, a file and ranges\n"); continue; }
593
+ sample_stop = stop;
594
+ halted = 0;
595
+ sample_ranges = 0;
596
+ for (char* part = strtok(ranges, ","); part && sample_ranges < 16; part = strtok(NULL, ",")) {
597
+ unsigned from, size;
598
+ if (sscanf(part, "%x:%u", &from, &size) == 2 && size >= 1 && size <= 4096) { sample_from[sample_ranges] = from; sample_size[sample_ranges] = size; sample_ranges++; }
599
+ }
600
+ sample_pc = pc;
601
+ samples = 0;
602
+ sample_file = fopen(path, "w");
603
+ if (sample_file) printf("ok ranges=%d\n", sample_ranges); else printf("err sample: cannot write %s\n", path);
604
+ } else if (!strcmp(cmd, "pin")) {
605
+ // pin <file> <addr:size,...> | off: at each sample, the next line of <file> into
606
+ // the ranges (give it before sample)
607
+ if (pin_file) { fclose(pin_file); pin_file = NULL; }
608
+ if (!strcmp(a1, "off")) { printf("ok pinned=%llu\n", (unsigned long long)pinned); continue; }
609
+ char ranges[1024];
610
+ if (sscanf(line, "%*s %1023s %1023s", a1, ranges) != 2) { printf("err pin: give a file and ranges\n"); continue; }
611
+ pin_ranges = 0;
612
+ for (char* part = strtok(ranges, ","); part && pin_ranges < 16; part = strtok(NULL, ",")) {
613
+ unsigned from, size;
614
+ if (sscanf(part, "%x:%u", &from, &size) == 2 && size >= 1 && size <= 4096) { pin_from[pin_ranges] = from; pin_size[pin_ranges] = size; pin_ranges++; }
615
+ }
616
+ pinned = 0;
617
+ pin_file = fopen(a1, "r");
618
+ if (pin_file) printf("ok ranges=%d\n", pin_ranges); else printf("err pin: cannot read %s\n", a1);
619
+ } else if (!strcmp(cmd, "coverage")) {
620
+ // coverage on | on-checkpoint | on-input | off | <file>: start now, at the next sample,
621
+ // at the first input read of code in RAM; stop; or write the 65536-byte map
622
+ if (!strcmp(a1, "on")) { memset(coverage, 0, sizeof coverage); coverage_on = 1; coverage_armed = coverage_armed_input = 0; printf("ok\n"); }
623
+ else if (!strcmp(a1, "on-checkpoint")) { memset(coverage, 0, sizeof coverage); coverage_on = coverage_armed_input = 0; coverage_armed = 1; printf("ok\n"); }
624
+ else if (!strcmp(a1, "on-input")) { memset(coverage, 0, sizeof coverage); coverage_on = coverage_armed = 0; coverage_armed_input = 1; printf("ok\n"); }
625
+ else if (!strcmp(a1, "off")) { coverage_on = coverage_armed = coverage_armed_input = 0; printf("ok\n"); }
626
+ else printf(write_file(a1, coverage, sizeof coverage) ? "ok\n" : "err coverage: cannot write %s\n", a1);
627
+ } else if (!strcmp(cmd, "palette")) {
628
+ // palette: the 16 colours as RRGGBB
629
+ chips_display_info_t info = c64_display_info(&sys);
630
+ const uint32_t* pal = info.palette.ptr;
631
+ printf("ok");
632
+ for (int i = 0; i < 16; i++) printf(" %02x%02x%02x", pal[i] & 0xFF, (pal[i] >> 8) & 0xFF, (pal[i] >> 16) & 0xFF);
633
+ printf("\n");
634
+ } else if (!strcmp(cmd, "frame")) {
635
+ // frame: the visible screen as palette indices, "ok <width> <height> <base64>"
636
+ chips_display_info_t info = c64_display_info(&sys);
637
+ const int w = info.screen.width, h = info.screen.height, stride = info.frame.dim.width;
638
+ const uint8_t* fb = info.frame.buffer.ptr;
639
+ static uint8_t visible[512 * 320];
640
+ for (int y = 0; y < h; y++) for (int x = 0; x < w; x++) visible[y * w + x] = fb[(info.screen.y + y) * stride + info.screen.x + x] & 0x0F;
641
+ printf("ok %d %d ", w, h);
642
+ print_base64(visible, (size_t)w * h);
643
+ printf("\n");
644
+ } else {
645
+ printf("err unknown %s\n", cmd);
646
+ }
647
+ }
648
+ if (record_file) {
649
+ for (int i = 0; i < site_count; i++) fprintf(record_file, "end %04x %04x %llu\n", sites[i].pc, sites[i].addr, (unsigned long long)sites[i].count);
650
+ fclose(record_file);
651
+ }
652
+ if (sample_file) fclose(sample_file);
653
+ if (pin_file) fclose(pin_file);
654
+ return 0;
655
+ }