@henols/c64-re-tools 2.6.0-rc.8 → 2.6.0

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 (82) hide show
  1. package/package.json +2 -2
  2. package/skills/c64-assembler/SKILL.md +3 -3
  3. package/skills/c64-assembler/scripts/assembler.ts +4 -3
  4. package/skills/c64-development/SKILL.md +3 -3
  5. package/skills/c64-docs/SKILL.md +6 -4
  6. package/skills/c64-docs/scripts/docs.ts +23 -10
  7. package/skills/c64-docs/scripts/lib/reference.ts +117 -31
  8. package/skills/c64-docs/scripts/lib/source-map.ts +14 -0
  9. package/skills/c64-knowledge/SKILL.md +10 -9
  10. package/skills/c64-knowledge/scripts/knowledge.ts +16 -9
  11. package/skills/c64-reverse-engineering/SKILL.md +9 -3
  12. package/skills/c64-source/SKILL.md +13 -10
  13. package/skills/c64-source/scripts/lib/export.ts +13 -2
  14. package/skills/c64-source/scripts/lib/loader.ts +51 -28
  15. package/skills/c64-source/scripts/lib/source.ts +24 -7
  16. package/skills/c64-source/scripts/source.ts +65 -47
  17. package/skills/c64-static-analysis/SKILL.md +26 -22
  18. package/skills/c64-static-analysis/scripts/lib/findings.ts +11 -11
  19. package/skills/c64-static-analysis/scripts/lib/instructions.ts +4 -4
  20. package/skills/c64-static-analysis/scripts/lib/macros.ts +3 -1
  21. package/skills/c64-static-analysis/scripts/static-analysis.ts +51 -35
  22. package/skills/c64-testing/SKILL.md +11 -4
  23. package/skills/c64-testing/references/recording-format.md +19 -7
  24. package/skills/c64-testing/references/scenario-format.md +6 -5
  25. package/skills/c64-testing/scripts/lib/diff-files.ts +4 -18
  26. package/skills/c64-testing/scripts/lib/play.ts +39 -37
  27. package/skills/c64-testing/scripts/lib/recording.ts +213 -65
  28. package/skills/c64-testing/scripts/lib/replay.ts +31 -5
  29. package/skills/c64-testing/scripts/lib/runs.ts +8 -3
  30. package/skills/c64-testing/scripts/lib/scenario.ts +4 -6
  31. package/skills/c64-testing/scripts/testing.ts +23 -2
  32. package/skills/c64-unpacker/SKILL.md +6 -3
  33. package/skills/c64-unpacker/scripts/lib/loads.ts +2 -2
  34. package/skills/c64-unpacker/scripts/lib/stages.ts +18 -3
  35. package/skills/c64-unpacker/scripts/unpacker.ts +38 -6
  36. package/skills/emulator/SKILL.md +2 -5
  37. package/src/build-name.ts +9 -4
  38. package/src/c64-keys.ts +98 -0
  39. package/src/c64.ts +1 -1
  40. package/src/host/main.ts +2 -2
  41. package/src/host/server.ts +3 -1
  42. package/src/host/tools/index.ts +1 -1
  43. package/src/host/vice/process.ts +34 -17
  44. package/src/host/vice/session.ts +132 -30
  45. package/src/host-client/steps.ts +21 -18
  46. package/src/host-client/tools.ts +1 -1
  47. package/src/host-client/vice-session.ts +7 -1
  48. package/src/knowledge/evidence.ts +66 -0
  49. package/src/knowledge/history.ts +5 -5
  50. package/src/knowledge/import.ts +12 -5
  51. package/src/knowledge/program.ts +81 -33
  52. package/src/knowledge/read.ts +12 -3
  53. package/src/knowledge/schema.ts +7 -7
  54. package/src/knowledge/start-screen.ts +2 -0
  55. package/src/knowledge/text.ts +28 -3
  56. package/src/knowledge/write.ts +6 -6
  57. package/src/mcp/inputs.ts +12 -0
  58. package/src/mcp/server.ts +1 -0
  59. package/src/mcp/tools/debug.ts +23 -21
  60. package/src/mcp/tools/input.ts +38 -5
  61. package/src/mcp/tools/memory.ts +12 -9
  62. package/src/native/c64run.ts +7 -4
  63. package/src/native/{ghidra → decompiler}/analyze.ts +17 -17
  64. package/src/native/{ghidra → decompiler}/language/c64rt_6510.slaspec +1 -1
  65. package/src/native/{ghidra → decompiler}/walk.ts +6 -6
  66. package/src/native/discover.ts +3 -3
  67. package/src/native/processes.ts +3 -3
  68. package/src/native/types.ts +25 -25
  69. package/src/opcodes.ts +1 -1
  70. package/src/protocol/vice.ts +47 -0
  71. package/src/reference-pages.ts +64 -0
  72. package/src/script.ts +1 -1
  73. package/src/vsf.ts +98 -0
  74. package/tools/c64run.c +158 -15
  75. package/skills/c64-testing/scripts/lib/keys.ts +0 -36
  76. /package/src/native/{ghidra → decompiler}/c64rt-wasm.ts +0 -0
  77. /package/src/native/{ghidra → decompiler}/language/c64rt_6510.cspec +0 -0
  78. /package/src/native/{ghidra → decompiler}/language/c64rt_6510.ldefs +0 -0
  79. /package/src/native/{ghidra → decompiler}/language/c64rt_6510.pspec +0 -0
  80. /package/src/native/{ghidra → decompiler}/language/c64rt_6510.sla +0 -0
  81. /package/src/native/{ghidra → decompiler}/pcode.ts +0 -0
  82. /package/src/native/{ghidra → decompiler}/wasm.ts +0 -0
package/tools/c64run.c CHANGED
@@ -27,11 +27,9 @@
27
27
  #include "systems/c1541.h"
28
28
  #include "systems/c64.h"
29
29
 
30
- #define C64RUN_PROTOCOL 3
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
30
+ #define C64RUN_PROTOCOL 7
31
+ // The key code of matrix place 0 (row 0, column 0); place n has code 0x80 + n.
32
+ #define C64RUN_KEY_PLACE 0x80
35
33
  #ifndef CHIPS_COMMIT
36
34
  #define CHIPS_COMMIT "unknown"
37
35
  #endif
@@ -228,6 +226,80 @@ static void sample(void) {
228
226
  if (sample_stop && samples >= sample_stop) halted = 1;
229
227
  }
230
228
 
229
+ // ---- Counts: the opcode fetches at some pcs, from "count-pc" on ----
230
+
231
+ #define MAX_COUNTED 64
232
+ // 0: the pc is not counted; else the index of its count + 1.
233
+ static uint8_t counted_at[0x10000];
234
+ static uint16_t counted_pc[MAX_COUNTED];
235
+ static uint64_t counted[MAX_COUNTED];
236
+ static int counted_pcs;
237
+ // "mark": the counts and the tick at the first opcode fetch at mark_pc, before that fetch counts.
238
+ static uint64_t marked[MAX_COUNTED];
239
+ static uint64_t mark_tick;
240
+ static int mark_pc = -1; // -1: no mark waits
241
+ static int mark_set; // marked and mark_tick hold the mark
242
+
243
+ // The phase of the machine at the mark: the beam position, the timers and interrupt
244
+ // state of both CIAs and the interrupt state of the CPU. It depends on how the
245
+ // program was started, and no program can set all of it. "align" gives it to
246
+ // another run at its own entry, so both runs go on from the same phase.
247
+ typedef struct {
248
+ uint16_t line, cycle;
249
+ m6526_timer_t ta[2], tb[2];
250
+ m6526_int_t intr[2];
251
+ uint16_t irq_pip;
252
+ uint32_t nmi_pip;
253
+ uint8_t brk_flags;
254
+ uint64_t pins, cpu_pins; // only the IRQ and NMI lines
255
+ } phase_t;
256
+ static phase_t mark_phase;
257
+
258
+ static void phase_get(phase_t* p) {
259
+ memset(p, 0, sizeof *p);
260
+ p->line = sys.vic.rs.v_count;
261
+ p->cycle = sys.vic.rs.h_count;
262
+ const m6526_t* cia[2] = { &sys.cia_1, &sys.cia_2 };
263
+ for (int i = 0; i < 2; i++) { p->ta[i] = cia[i]->ta; p->tb[i] = cia[i]->tb; p->intr[i] = cia[i]->intr; }
264
+ p->irq_pip = sys.cpu.irq_pip;
265
+ p->nmi_pip = sys.cpu.nmi_pip;
266
+ p->brk_flags = sys.cpu.brk_flags;
267
+ p->pins = pins & (M6502_IRQ | M6502_NMI);
268
+ p->cpu_pins = sys.cpu.PINS & (M6502_IRQ | M6502_NMI);
269
+ }
270
+
271
+ static void phase_set(const phase_t* p) {
272
+ m6526_t* cia[2] = { &sys.cia_1, &sys.cia_2 };
273
+ for (int i = 0; i < 2; i++) { cia[i]->ta = p->ta[i]; cia[i]->tb = p->tb[i]; cia[i]->intr = p->intr[i]; }
274
+ sys.cpu.irq_pip = p->irq_pip;
275
+ sys.cpu.nmi_pip = p->nmi_pip;
276
+ sys.cpu.brk_flags = p->brk_flags;
277
+ pins = (pins & ~(M6502_IRQ | M6502_NMI)) | p->pins;
278
+ sys.cpu.PINS = (sys.cpu.PINS & ~(M6502_IRQ | M6502_NMI)) | p->cpu_pins;
279
+ }
280
+
281
+ static void clear_counts(void) {
282
+ for (int i = 0; i < counted_pcs; i++) counted_at[counted_pc[i]] = 0;
283
+ counted_pcs = 0;
284
+ mark_pc = -1;
285
+ mark_set = 0;
286
+ }
287
+
288
+ static void count_fetch(uint16_t pc) {
289
+ if (mark_pc == pc) {
290
+ for (int i = 0; i < counted_pcs; i++) marked[i] = counted[i];
291
+ mark_tick = ticks;
292
+ phase_get(&mark_phase);
293
+ mark_pc = -1;
294
+ mark_set = 1;
295
+ }
296
+ if (counted_at[pc]) counted[counted_at[pc] - 1]++;
297
+ }
298
+
299
+ static uint64_t count_of(uint16_t pc) {
300
+ return counted_at[pc] ? counted[counted_at[pc] - 1] : 0;
301
+ }
302
+
231
303
  // ---- The tick ----
232
304
 
233
305
  static void tick(void) {
@@ -242,6 +314,14 @@ static void tick(void) {
242
314
  last_sync_pc = addr;
243
315
  if (sample_file && addr == sample_pc && decoded) sample();
244
316
  if (coverage_armed_pc && addr == coverage_pc && decoded) { coverage_armed_pc = 0; coverage_start(addr); }
317
+ if (decoded) count_fetch(addr);
318
+ }
319
+ if (read && !sync && decoded && sys.io_mapped && (addr & 0xFF0F) == 0xDC00) {
320
+ // chips gives port A of CIA 1 only joystick 2. A key also pulls its line of port A
321
+ // low while port B drives its column low: the scan from port B that some games use.
322
+ const m6526_port_t* pb = &sys.cia_1.pb;
323
+ const uint8_t low_columns = (uint8_t)~(pb->reg | ~pb->ddr);
324
+ if (low_columns) M6502_SET_DATA(pins, M6502_GET_DATA(pins) & (uint8_t)~kbd_test_lines(&sys.kbd, low_columns));
245
325
  }
246
326
  if (read && !sync && decoded && sys.io_mapped && (addr & 0xF000) == 0xD000 && input_addr[addr & 0x0FFF]) {
247
327
  input_read(last_sync_pc, addr);
@@ -335,10 +415,9 @@ static const char* init(const char* romdir) {
335
415
  .chars = { .ptr = rom[2], .size = 0x1000 },
336
416
  },
337
417
  });
338
- // The matrix positions (column, line) of the keys that chips has no code for.
339
- kbd_register_key(&sys.kbd, C64RUN_KEY_LSHIFT, 7, 1, 0);
340
- kbd_register_key(&sys.kbd, C64RUN_KEY_RSHIFT, 4, 6, 0);
341
- kbd_register_key(&sys.kbd, C64RUN_KEY_UPARROW, 6, 6, 0);
418
+ // One code for each place of the matrix: place row * 8 + column, where the row is the
419
+ // line of CIA 1 port A and the column the bit of port B (chips: column, line).
420
+ for (int place = 0; place < 64; place++) kbd_register_key(&sys.kbd, C64RUN_KEY_PLACE + place, place & 7, place >> 3, 0);
342
421
  const uint16_t vector = rom[0][0x1FFC] | (rom[0][0x1FFD] << 8);
343
422
  for (int i = 0; i < 3; i++) free(rom[i]);
344
423
  pins = sys.pins;
@@ -440,6 +519,7 @@ int main(int argc, char** argv) {
440
519
  halted = 0;
441
520
  sample_stop = 0;
442
521
  maps = 0;
522
+ clear_counts();
443
523
  coverage_reset();
444
524
  if (pin_file) { fclose(pin_file); pin_file = NULL; }
445
525
  if (record_file) { fclose(record_file); record_file = NULL; }
@@ -489,6 +569,7 @@ int main(int argc, char** argv) {
489
569
  last_sync_pc = pc;
490
570
  // The prefetch is the opcode fetch at pc: it does not pass through tick.
491
571
  if (coverage_armed_pc && pc == coverage_pc) { coverage_armed_pc = 0; coverage_start(pc); }
572
+ count_fetch((uint16_t)pc);
492
573
  if (coverage_on) cover(pc, 1, 1);
493
574
  status("ok");
494
575
  } else if (!strcmp(cmd, "run")) {
@@ -501,12 +582,74 @@ int main(int argc, char** argv) {
501
582
  do tick(); while (!(pins & M6502_SYNC) && !halted);
502
583
  status("ok");
503
584
  } else if (!strcmp(cmd, "run-pc")) {
504
- // run-pc <pc hex> <until tick>: run to the next opcode fetch at pc
585
+ // run-pc <pc hex> <until tick> [<n>]: run to the next opcode fetch at pc; with n, run
586
+ // until the count of pc ("count-pc") is n
505
587
  unsigned pc;
506
- const unsigned long long until = strtoull(a2, NULL, 10);
507
- if (sscanf(a1, "%x", &pc) != 1) { printf("err run-pc: give a pc and a tick\n"); continue; }
588
+ unsigned long long until, n;
589
+ const int fields = sscanf(line, "%*s %x %llu %llu", &pc, &until, &n);
590
+ if (fields < 2 || pc > 0xFFFF) { printf("err run-pc: give a pc and a tick\n"); continue; }
591
+ if (fields == 3) {
592
+ if (!counted_at[pc]) { printf("err run-pc: give count-pc with %04x first\n", pc); continue; }
593
+ while (count_of((uint16_t)pc) < n && ticks < until && !halted) tick();
594
+ status(count_of((uint16_t)pc) >= n ? "ok reached" : "ok not-reached");
595
+ continue;
596
+ }
508
597
  do tick(); while (!((pins & M6502_SYNC) && M6502_GET_ADDR(pins) == pc) && ticks < until && !halted);
509
598
  status((pins & M6502_SYNC) && M6502_GET_ADDR(pins) == pc ? "ok reached" : "ok not-reached");
599
+ } else if (!strcmp(cmd, "count-pc")) {
600
+ // count-pc <pc hex,...> | off: count the opcode fetches at each pc from 0
601
+ clear_counts();
602
+ if (!strcmp(a1, "off")) { printf("ok counted=0\n"); continue; }
603
+ int bad = 0;
604
+ for (char* part = strtok(a1, ","); part; part = strtok(NULL, ",")) {
605
+ unsigned pc;
606
+ if (sscanf(part, "%x", &pc) != 1 || pc > 0xFFFF || counted_pcs == MAX_COUNTED) { bad = 1; break; }
607
+ if (counted_at[pc]) continue;
608
+ counted_pc[counted_pcs] = (uint16_t)pc;
609
+ counted[counted_pcs] = 0;
610
+ counted_at[pc] = (uint8_t)++counted_pcs;
611
+ }
612
+ if (bad) { clear_counts(); printf("err count-pc: give 1 to %d pcs in hex\n", MAX_COUNTED); } else printf("ok counted=%d\n", counted_pcs);
613
+ } else if (!strcmp(cmd, "mark")) {
614
+ // mark <pc hex>: keep the counts of count-pc at the next opcode fetch at pc, before it counts
615
+ unsigned pc;
616
+ if (sscanf(a1, "%x", &pc) != 1 || pc > 0xFFFF) { printf("err mark: give a pc\n"); continue; }
617
+ mark_pc = (int)pc;
618
+ mark_set = 0;
619
+ printf("ok\n");
620
+ } else if (!strcmp(cmd, "marked")) {
621
+ // marked: the tick and the phase (hex) of the mark when it came, else none
622
+ if (mark_set) {
623
+ printf("ok tick=%llu phase=", (unsigned long long)mark_tick);
624
+ for (size_t i = 0; i < sizeof mark_phase; i++) printf("%02x", ((const uint8_t*)&mark_phase)[i]);
625
+ printf("\n");
626
+ }
627
+ else printf("ok none\n");
628
+ } else if (!strcmp(cmd, "align")) {
629
+ // align <phase hex>: at an opcode fetch, hold the CPU on the fetch (RDY, as the VIC-II
630
+ // does on a bad line) until the beam is at the position of the phase, then give the
631
+ // CIAs and the CPU the timers and interrupt state of the phase (from "marked")
632
+ phase_t p;
633
+ if (strlen(a1) != 2 * sizeof p || !(pins & M6502_SYNC)) { printf("err align: give the phase of marked, at an opcode fetch\n"); continue; }
634
+ for (size_t i = 0; i < sizeof p; i++) ((uint8_t*)&p)[i] = (uint8_t)hexbyte(a1 + 2 * i);
635
+ // A PAL frame holds each beam position one time.
636
+ uint64_t held = 0;
637
+ while ((sys.vic.rs.v_count != p.line || sys.vic.rs.h_count != p.cycle) && held <= 312 * 63) {
638
+ pins = _c64_tick(&sys, pins | M6502_RDY);
639
+ ticks++;
640
+ held++;
641
+ }
642
+ if (sys.vic.rs.v_count != p.line || sys.vic.rs.h_count != p.cycle) { printf("err align: the beam never came to line %u cycle %u\n", p.line, p.cycle); continue; }
643
+ phase_set(&p);
644
+ char prefix[64];
645
+ snprintf(prefix, sizeof prefix, "ok held=%llu", (unsigned long long)held);
646
+ status(prefix);
647
+ } else if (!strcmp(cmd, "hits")) {
648
+ // hits <pc hex>: the count of a pc of count-pc, and its count at the mark when the mark came
649
+ unsigned pc;
650
+ if (sscanf(a1, "%x", &pc) != 1 || pc > 0xFFFF) { printf("err hits: give a pc\n"); continue; }
651
+ if (mark_set && counted_at[pc]) printf("ok count=%llu marked=%llu\n", (unsigned long long)count_of((uint16_t)pc), (unsigned long long)marked[counted_at[pc] - 1]);
652
+ else printf("ok count=%llu\n", (unsigned long long)count_of((uint16_t)pc));
510
653
  } else if (!strcmp(cmd, "run-reads")) {
511
654
  // run-reads <pc hex> <addr hex> <n> <until tick>: run until the site has n reads
512
655
  unsigned pc, addr;
@@ -536,11 +679,11 @@ int main(int argc, char** argv) {
536
679
  c64_joystick(&sys, (uint8_t)j1, (uint8_t)j2);
537
680
  printf("ok\n");
538
681
  } else if (!strcmp(cmd, "key")) {
539
- // key <code hex> down|up: a key of the keyboard matrix; the code is ASCII as
540
- // chips maps it (A-Z, 0-9, space 20, return 0d, ...); shifted keys press SHIFT.
541
- // 11 and 12 are the left and right SHIFT alone, 5e the up arrow, ff RESTORE (NMI).
682
+ // key <code hex> down|up: 80 + place is the key at matrix place row * 8 + column
683
+ // (80-bf), ff is RESTORE (NMI). chips holds at most 4 keys at once.
542
684
  unsigned code;
543
685
  if (sscanf(a1, "%x", &code) != 1 || (strcmp(a2, "down") && strcmp(a2, "up"))) { printf("err key: give a code and down or up\n"); continue; }
686
+ if ((code < C64RUN_KEY_PLACE || code >= C64RUN_KEY_PLACE + 64) && code != C64_KEY_RESTORE) { printf("err key: give 80-bf (a matrix place) or ff (RESTORE)\n"); continue; }
544
687
  if (!strcmp(a2, "down")) c64_key_down(&sys, (int)code);
545
688
  else {
546
689
  // chips releases a key in kbd_update, after its sticky time: one call
@@ -1,36 +0,0 @@
1
- // The keys of the keyboard matrix that the replay core presses: the key codes
2
- // of chips c64.h, and 11, 12 and 5e of c64run. The play page and the press
3
- // step of a record file use the same codes.
4
-
5
- /** The key codes of the keys that have no character of their own. */
6
- export const C64_KEYS = {
7
- del: 0x01, clr: 0x02, stop: 0x03, leftArrow: 0x04, run: 0x07, csrLeft: 0x08, csrRight: 0x09, csrDown: 0x0a, csrUp: 0x0b,
8
- home: 0x0c, return: 0x0d, ctrl: 0x0e, cbm: 0x0f, inst: 0x10, leftShift: 0x11, rightShift: 0x12, space: 0x20, upArrow: 0x5e, pound: 0x7e,
9
- } as const;
10
-
11
- /** F1 to F8: F2, F4, F6 and F8 press SHIFT with the key. */
12
- export const F_KEYS = [0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8];
13
-
14
- /** The keys that a press step names in braces, for example {F1}. */
15
- export const NAMED_KEYS: Readonly<Record<string, number>> = {
16
- ...Object.fromEntries(F_KEYS.map((key, index) => [`F${index + 1}`, key])),
17
- RETURN: C64_KEYS.return,
18
- SPACE: C64_KEYS.space,
19
- DEL: C64_KEYS.del,
20
- INST: C64_KEYS.inst,
21
- HOME: C64_KEYS.home,
22
- CLR: C64_KEYS.clr,
23
- STOP: C64_KEYS.stop,
24
- RUN: C64_KEYS.run,
25
- CTRL: C64_KEYS.ctrl,
26
- CBM: C64_KEYS.cbm,
27
- LSHIFT: C64_KEYS.leftShift,
28
- RSHIFT: C64_KEYS.rightShift,
29
- UP: C64_KEYS.csrUp,
30
- DOWN: C64_KEYS.csrDown,
31
- LEFT: C64_KEYS.csrLeft,
32
- RIGHT: C64_KEYS.csrRight,
33
- POUND: C64_KEYS.pound,
34
- UPARROW: C64_KEYS.upArrow,
35
- LEFTARROW: C64_KEYS.leftArrow,
36
- };
File without changes
File without changes
File without changes