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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (119) hide show
  1. package/package.json +1 -1
  2. package/skills/c64-assembler/SKILL.md +5 -2
  3. package/skills/c64-assembler/scripts/assembler.ts +55 -48
  4. package/skills/c64-disk/scripts/disk.ts +3 -3
  5. package/skills/c64-disk/scripts/lib/containers.ts +2 -2
  6. package/skills/c64-docs/SKILL.md +1 -1
  7. package/skills/c64-docs/scripts/docs.ts +12 -20
  8. package/skills/c64-docs/scripts/lib/links.ts +1 -1
  9. package/skills/c64-docs/scripts/lib/markdown.ts +1 -1
  10. package/skills/c64-docs/scripts/lib/reference.ts +59 -13
  11. package/skills/c64-knowledge/SKILL.md +17 -8
  12. package/skills/c64-knowledge/scripts/knowledge.ts +200 -218
  13. package/skills/c64-knowledge/scripts/lib/check.ts +2 -3
  14. package/skills/c64-knowledge/scripts/lib/listing.ts +56 -24
  15. package/skills/c64-packer/scripts/packer.ts +3 -3
  16. package/skills/c64-provenance/SKILL.md +25 -16
  17. package/skills/c64-provenance/scripts/lib/compare.ts +31 -21
  18. package/skills/c64-provenance/scripts/lib/leftovers.ts +17 -35
  19. package/skills/c64-provenance/scripts/lib/library.ts +10 -10
  20. package/skills/c64-provenance/scripts/lib/runtime.ts +3 -14
  21. package/skills/c64-provenance/scripts/provenance.ts +129 -98
  22. package/skills/c64-source/references/loader.md +2 -1
  23. package/skills/c64-source/scripts/lib/export.ts +54 -303
  24. package/skills/c64-source/scripts/lib/loader.ts +31 -51
  25. package/skills/c64-source/scripts/lib/source.ts +51 -41
  26. package/skills/c64-source/scripts/lib/stage.ts +9 -25
  27. package/skills/c64-source/scripts/source.ts +134 -294
  28. package/skills/c64-static-analysis/SKILL.md +12 -7
  29. package/skills/c64-static-analysis/scripts/lib/coverage.ts +1 -3
  30. package/skills/c64-static-analysis/scripts/lib/findings.ts +16 -3
  31. package/skills/c64-static-analysis/scripts/lib/idioms.ts +82 -10
  32. package/skills/c64-static-analysis/scripts/lib/triage.ts +112 -0
  33. package/skills/c64-static-analysis/scripts/static-analysis.ts +239 -147
  34. package/skills/c64-testing/SKILL.md +13 -7
  35. package/skills/c64-testing/scripts/lib/diff-files.ts +6 -13
  36. package/skills/c64-testing/scripts/lib/recording.ts +47 -66
  37. package/skills/c64-testing/scripts/lib/replay.ts +53 -9
  38. package/skills/c64-testing/scripts/lib/runs.ts +11 -29
  39. package/skills/c64-testing/scripts/lib/scenario.ts +30 -31
  40. package/skills/c64-testing/scripts/lib/start-screen.ts +42 -5
  41. package/skills/c64-testing/scripts/testing.ts +32 -7
  42. package/skills/c64-unpacker/SKILL.md +10 -7
  43. package/skills/c64-unpacker/scripts/lib/evidence.ts +23 -38
  44. package/skills/c64-unpacker/scripts/lib/loads.ts +24 -39
  45. package/skills/c64-unpacker/scripts/lib/machine.ts +38 -18
  46. package/skills/c64-unpacker/scripts/lib/stages.ts +142 -41
  47. package/skills/c64-unpacker/scripts/unpacker.ts +85 -68
  48. package/skills/emulator/SKILL.md +1 -0
  49. package/src/c64.ts +117 -10
  50. package/src/cli/ap-sdk.ts +1 -1
  51. package/src/cli/cleanup.ts +4 -2
  52. package/src/compare.ts +36 -16
  53. package/src/errors.ts +10 -1
  54. package/src/host/main.ts +2 -1
  55. package/src/host/server.ts +7 -11
  56. package/src/host/tools/c1541.ts +9 -3
  57. package/src/host/vice/adapter.ts +20 -43
  58. package/src/host/vice/binary-monitor.ts +22 -21
  59. package/src/host/vice/process.ts +31 -40
  60. package/src/host/vice/session.ts +187 -247
  61. package/src/host/vice/text-monitor.ts +3 -3
  62. package/src/host-client/autostart.ts +1 -1
  63. package/src/host-client/connect.ts +2 -1
  64. package/src/host-client/coverage.ts +0 -3
  65. package/src/host-client/entry-state.ts +4 -11
  66. package/src/host-client/image.ts +6 -11
  67. package/src/host-client/steps.ts +13 -113
  68. package/src/host-client/tools.ts +4 -4
  69. package/src/host-client/transfer.ts +10 -0
  70. package/src/host-client/vice-session.ts +10 -6
  71. package/src/knowledge/code-reach.ts +364 -0
  72. package/src/knowledge/conflicts.ts +34 -72
  73. package/src/knowledge/database.ts +40 -28
  74. package/src/knowledge/evidence.ts +2 -2
  75. package/src/knowledge/graph.ts +3 -3
  76. package/src/knowledge/history.ts +50 -90
  77. package/src/knowledge/import.ts +43 -71
  78. package/src/knowledge/join.ts +1 -1
  79. package/src/knowledge/names.ts +12 -7
  80. package/src/knowledge/program.ts +25 -46
  81. package/src/knowledge/read.ts +104 -100
  82. package/src/knowledge/schema.ts +9 -3
  83. package/src/knowledge/separate.ts +47 -19
  84. package/src/knowledge/start-screen.ts +7 -5
  85. package/src/knowledge/text.ts +56 -33
  86. package/src/knowledge/write.ts +274 -515
  87. package/src/loader.ts +7 -25
  88. package/src/mcp/inputs.ts +3 -3
  89. package/src/mcp/server.ts +19 -38
  90. package/src/mcp/session-source.ts +2 -1
  91. package/src/mcp/tools/debug.ts +97 -24
  92. package/src/mcp/tools/execution.ts +1 -1
  93. package/src/mcp/tools/input.ts +3 -3
  94. package/src/mcp/tools/machine.ts +3 -3
  95. package/src/mcp/tools/media.ts +4 -4
  96. package/src/mcp/tools/memory.ts +13 -17
  97. package/src/mcp/tools/video.ts +11 -25
  98. package/src/native/acme.ts +3 -3
  99. package/src/native/c64run.ts +2 -1
  100. package/src/native/decompiler/analyze.ts +10 -7
  101. package/src/native/decompiler/walk.ts +3 -4
  102. package/src/native/decompiler/wasm.ts +2 -2
  103. package/src/native/discover.ts +1 -1
  104. package/src/native/dxa.ts +9 -11
  105. package/src/native/exomizer.ts +1 -1
  106. package/src/native/processes.ts +4 -1
  107. package/src/native/run.ts +3 -3
  108. package/src/native/types.ts +5 -6
  109. package/src/opcodes.ts +3 -0
  110. package/src/platform.ts +9 -10
  111. package/src/program-code.ts +151 -0
  112. package/src/project.ts +8 -5
  113. package/src/protocol/framing.ts +51 -18
  114. package/src/protocol/messages.ts +1 -11
  115. package/src/protocol/tools.ts +0 -3
  116. package/src/protocol/vice.ts +588 -773
  117. package/src/script.ts +9 -14
  118. package/src/time.ts +1 -1
  119. package/src/trace.ts +3 -2
@@ -1,7 +1,7 @@
1
1
  // The VICE-session operations of the Host Runtime wire: their vocabulary, shapes and validators.
2
2
 
3
3
  import { MAX_PRESS_FRAMES, MAX_PRESS_TAPS, MAX_TAP_KEYS, type KeyTap } from "../c64-keys.ts";
4
- import { invalid, isInteger, isObject, isOneOf, onlyFields, ProtocolError, VIDEO_STANDARDS, type VideoStandard } from "./messages.ts";
4
+ import { type Fields, invalid, isInteger, isObject, isOneOf, onlyFields, ProtocolError, VIDEO_STANDARDS, type VideoStandard } from "./messages.ts";
5
5
 
6
6
  export const SPACES = ["c64", "drive8"] as const;
7
7
  export type Space = (typeof SPACES)[number];
@@ -300,7 +300,7 @@ export interface MemmapRange {
300
300
  // Screen baselines and snapshots
301
301
 
302
302
  /** 1-64 letters, digits, dots, underscores or hyphens. */
303
- export const TRANSIENT_NAME = /^[A-Za-z0-9._-]{1,64}$/;
303
+ const TRANSIENT_NAME = /^[A-Za-z0-9._-]{1,64}$/;
304
304
  export const MAX_BASELINES = 64;
305
305
  export const MAX_SNAPSHOTS = 64;
306
306
  /** Most rectangles one screen comparison mask holds. */
@@ -423,6 +423,9 @@ export interface CpuFlags {
423
423
  c: boolean;
424
424
  }
425
425
 
426
+ /** The bit of each flag in the 6502 status register; bit 5 always reads as 1. */
427
+ export const FLAG_BITS: Record<keyof CpuFlags, number> = { n: 0x80, v: 0x40, b: 0x10, d: 0x08, i: 0x04, z: 0x02, c: 0x01 };
428
+
426
429
  export interface Registers {
427
430
  pc: number;
428
431
  a: number;
@@ -589,47 +592,6 @@ export interface ViceOperations {
589
592
  window: { params: { action: (typeof WINDOW_ACTIONS)[number] }; result: WindowResult };
590
593
  }
591
594
  export type ViceOperation = keyof ViceOperations;
592
- export const VICE_OPERATIONS = [
593
- "status",
594
- "memoryRead",
595
- "registersGet",
596
- "memoryWrite",
597
- "memorySearch",
598
- "memoryCompare",
599
- "disassemble",
600
- "registersSet",
601
- "execution",
602
- "runUntil",
603
- "programLoad",
604
- "autostart",
605
- "diskAttach",
606
- "reset",
607
- "keyboard",
608
- "joystick",
609
- "press",
610
- "breakpoint",
611
- "watchpoint",
612
- "screenCapture",
613
- "screenCompare",
614
- "screenBaselines",
615
- "screenDiscard",
616
- "snapshot",
617
- "snapshotFile",
618
- "ioRead",
619
- "cpuHistory",
620
- "trace",
621
- "backtrace",
622
- "timing",
623
- "profile",
624
- "memmap",
625
- "vicii",
626
- "sprites",
627
- "cia",
628
- "sid",
629
- "observe",
630
- "warp",
631
- "window",
632
- ] as const satisfies readonly ViceOperation[];
633
595
 
634
596
  /** How many attachments an operation takes; a snapshotFile restore takes the .vsf. */
635
597
  export function attachmentCount(op: ViceOperation, params?: unknown): number {
@@ -637,381 +599,389 @@ export function attachmentCount(op: ViceOperation, params?: unknown): number {
637
599
  return op === "programLoad" || op === "autostart" || op === "diskAttach" ? 1 : 0;
638
600
  }
639
601
 
640
- /** Validates request parameters on the host. Throws WireFailure(invalid-input). */
641
- export function validateViceParams<O extends ViceOperation>(op: O, params: unknown): ViceOperations[O]["params"] {
642
- if (!isObject(params)) invalid("parameters must be an object");
643
- switch (op) {
644
- case "status":
645
- case "vicii":
646
- case "sid":
647
- case "screenBaselines":
648
- onlyFields(params, []);
649
- return {} as ViceOperations[O]["params"];
650
- case "screenCapture": {
651
- onlyFields(params, ["baseline"]);
652
- if (params.baseline === undefined) return {} as ViceOperations[O]["params"];
653
- return { baseline: validName(params.baseline, "baseline") } as ViceOperations[O]["params"];
654
- }
655
- case "screenDiscard":
656
- onlyFields(params, ["baseline"]);
657
- return { baseline: validName(params.baseline, "baseline") } as ViceOperations[O]["params"];
658
- case "snapshot": {
659
- onlyFields(params, params.action === "list" ? ["action"] : ["action", "name"]);
660
- if (params.action === "list") return { action: "list" } as ViceOperations[O]["params"];
661
- if (params.action !== "save" && params.action !== "restore" && params.action !== "remove") {
662
- invalid("action must be save, restore, list or remove");
663
- }
664
- return { action: params.action, name: validName(params.name, "name") } as ViceOperations[O]["params"];
665
- }
666
- case "snapshotFile": {
667
- if (params.action === "restore") {
668
- onlyFields(params, ["action"]);
669
- return { action: "restore" } as ViceOperations[O]["params"];
670
- }
671
- if (params.action !== "export") invalid("action must be export or restore");
672
- onlyFields(params, ["action", "name"]);
673
- return { action: "export", name: validName(params.name, "name") } as ViceOperations[O]["params"];
674
- }
675
- case "screenCompare": {
676
- onlyFields(params, ["baseline", "maxMismatchRatio", "mask", "includeDiff"]);
677
- const ratio = params.maxMismatchRatio;
678
- if (typeof ratio !== "number" || !(ratio >= 0 && ratio <= 1)) invalid("maxMismatchRatio must be a number from 0 to 1");
679
- if (!Array.isArray(params.mask) || params.mask.length > MAX_MASK_RECTANGLES) invalid(`mask must be a list of at most ${MAX_MASK_RECTANGLES} rectangles`);
680
- const mask = params.mask.map((rectangle): Rectangle => {
681
- if (!isObject(rectangle)) invalid("each mask entry must be a rectangle");
682
- onlyFields(rectangle, ["x", "y", "width", "height"], "a mask rectangle");
683
- const ok = isInteger(rectangle.x, 0, 4095) && isInteger(rectangle.y, 0, 4095) && isInteger(rectangle.width, 1, 4096) && isInteger(rectangle.height, 1, 4096);
684
- if (!ok) invalid("a mask rectangle needs integer x, y, width and height, with width and height at least 1");
685
- return { x: rectangle.x as number, y: rectangle.y as number, width: rectangle.width as number, height: rectangle.height as number };
686
- });
687
- if (typeof params.includeDiff !== "boolean") invalid("includeDiff must be true or false");
688
- return { baseline: validName(params.baseline, "baseline"), maxMismatchRatio: ratio, mask, includeDiff: params.includeDiff } as ViceOperations[O]["params"];
689
- }
690
- case "cpuHistory": {
691
- onlyFields(params, ["limit", "space"]);
692
- if (!isInteger(params.limit, 1, MAX_HISTORY)) invalid(`limit must be an integer from 1 to ${MAX_HISTORY}`);
693
- if (!isOneOf(SPACES, params.space)) invalid("space must be c64 or drive8");
694
- return { limit: params.limit, space: params.space } as ViceOperations[O]["params"];
695
- }
696
- case "trace": {
697
- onlyFields(params, ["points", "frames", "maxHits"]);
698
- if (!Array.isArray(params.points) || params.points.length === 0 || params.points.length > MAX_TRACE_POINTS) invalid(`points must be a list of 1 to ${MAX_TRACE_POINTS} points`);
699
- const points = (params.points as unknown[]).map((point) => {
700
- if (!isObject(point)) return invalid("a point is an object with kind, start and end");
701
- onlyFields(point, ["kind", "start", "end"]);
702
- if (!isOneOf(TRACE_KINDS, point.kind)) invalid("a point kind must be exec, read or write");
703
- if (!isInteger(point.start, 0, 0xffff) || !isInteger(point.end, point.start as number, 0xffff)) invalid("a point needs start and end from $0000 to $ffff, the end not before the start");
704
- return { kind: point.kind, start: point.start, end: point.end } as TracePoint;
705
- });
706
- if (!isInteger(params.frames, 1, MAX_TRACE_FRAMES)) invalid(`frames must be an integer from 1 to ${MAX_TRACE_FRAMES}`);
707
- if (!isInteger(params.maxHits, 1, MAX_TRACE_HITS)) invalid(`maxHits must be an integer from 1 to ${MAX_TRACE_HITS}`);
708
- return { points, frames: params.frames, maxHits: params.maxHits } as ViceOperations[O]["params"];
709
- }
710
- case "backtrace": {
711
- onlyFields(params, ["depth", "space"]);
712
- if (!isInteger(params.depth, 1, MAX_BACKTRACE)) invalid(`depth must be an integer from 1 to ${MAX_BACKTRACE}`);
713
- if (!isOneOf(SPACES, params.space)) invalid("space must be c64 or drive8");
714
- return { depth: params.depth, space: params.space } as ViceOperations[O]["params"];
715
- }
716
- case "timing": {
602
+ /** A space field. `what` names the field in the refusal. */
603
+ function validSpace(value: unknown, what = "space"): Space {
604
+ if (!isOneOf(SPACES, value)) invalid(`${what} must be c64 or drive8`);
605
+ return value;
606
+ }
607
+
608
+ /** A view field. `what` names the field in the refusal. */
609
+ function validView(value: unknown, what = "view"): MemoryView {
610
+ if (!isOneOf(MEMORY_VIEWS, value)) invalid(`${what} must be cpu or ram`);
611
+ return value;
612
+ }
613
+
614
+ /** An address field. `what` names the field in the refusal. */
615
+ function validAddress(value: unknown, what = "address"): number {
616
+ if (!isInteger(value, 0, 0xffff)) invalid(`${what} must be an integer from 0 to 65535`);
617
+ return value;
618
+ }
619
+
620
+ function noParams(params: Fields): Record<string, never> {
621
+ onlyFields(params, []);
622
+ return {};
623
+ }
624
+
625
+ /** The parameters of a breakpoint or watchpoint action other than add; undefined for add. */
626
+ function pointAction(params: Fields): { action: "list" } | { action: "remove" | "enable" | "disable"; id: number } | undefined {
627
+ if (!isOneOf(CHECKPOINT_ACTIONS, params.action)) invalid(`action must be one of ${CHECKPOINT_ACTIONS.join(", ")}`);
628
+ if (params.action === "list") {
629
+ onlyFields(params, ["action"]);
630
+ return { action: "list" };
631
+ }
632
+ if (params.action === "add") return undefined;
633
+ onlyFields(params, ["action", "id"]);
634
+ if (!isInteger(params.id, 1, 0xffff_ffff)) invalid("id must be a positive integer");
635
+ return { action: params.action, id: params.id };
636
+ }
637
+
638
+ /** The parameter check of each operation: refuses with invalid-input, else returns the checked parameters. */
639
+ const PARAMS: { [O in ViceOperation]: (params: Fields) => ViceOperations[O]["params"] } = {
640
+ status: noParams,
641
+ vicii: noParams,
642
+ sid: noParams,
643
+ screenBaselines: noParams,
644
+ screenCapture: (params) => {
645
+ onlyFields(params, ["baseline"]);
646
+ return params.baseline === undefined ? {} : { baseline: validName(params.baseline, "baseline") };
647
+ },
648
+ screenDiscard: (params) => {
649
+ onlyFields(params, ["baseline"]);
650
+ return { baseline: validName(params.baseline, "baseline") };
651
+ },
652
+ snapshot: (params) => {
653
+ onlyFields(params, params.action === "list" ? ["action"] : ["action", "name"]);
654
+ if (params.action === "list") return { action: "list" };
655
+ if (params.action !== "save" && params.action !== "restore" && params.action !== "remove") {
656
+ invalid("action must be save, restore, list or remove");
657
+ }
658
+ return { action: params.action, name: validName(params.name, "name") };
659
+ },
660
+ snapshotFile: (params) => {
661
+ if (params.action === "restore") {
717
662
  onlyFields(params, ["action"]);
718
- if (!isOneOf(TIMING_ACTIONS, params.action)) invalid("action must be start or read");
719
- return { action: params.action } as ViceOperations[O]["params"];
720
- }
721
- case "observe": {
722
- onlyFields(params, ["registers", "memory", "vicii", "sprites", "cia", "sid", "screen", "timing"]);
723
- const result: ObserveParams = {};
724
- if (params.registers !== undefined) {
725
- if (!isOneOf(SPACES, params.registers)) invalid("registers must be c64 or drive8");
726
- result.registers = params.registers;
727
- }
728
- if (params.memory !== undefined) {
729
- const ranges = params.memory;
730
- if (!Array.isArray(ranges) || ranges.length === 0 || ranges.length > MAX_OBSERVE_RANGES) {
731
- invalid(`memory must hold 1 to ${MAX_OBSERVE_RANGES} ranges`);
732
- }
733
- result.memory = ranges.map((range) => validateViceParams("memoryRead", range));
734
- if (result.memory.reduce((sum, range) => sum + range.size, 0) > MAX_OBSERVE_BYTES) {
735
- invalid(`the memory ranges must hold at most ${MAX_OBSERVE_BYTES} bytes together`);
736
- }
737
- }
738
- for (const flag of ["vicii", "sid", "screen", "timing"] as const) {
739
- if (params[flag] === undefined) continue;
740
- if (typeof params[flag] !== "boolean") invalid(`${flag} must be true or false`);
741
- if (params[flag]) result[flag] = true;
742
- }
743
- if (params.sprites !== undefined) result.sprites = validateViceParams("sprites", { indexes: params.sprites }).indexes;
744
- if (params.cia !== undefined) result.cia = validateViceParams("cia", { which: params.cia }).which;
745
- if (Object.keys(result).length === 0) invalid("ask for at least one observation");
746
- return result as ViceOperations[O]["params"];
747
- }
748
- case "profile": {
749
- onlyFields(params, params.action === "clear" ? ["action"] : ["action", "limit"]);
750
- if (params.action === "clear") return { action: "clear" } as ViceOperations[O]["params"];
751
- if (params.action !== "read") invalid("action must be read or clear");
752
- if (!isInteger(params.limit, 1, MAX_PROFILE)) invalid(`limit must be an integer from 1 to ${MAX_PROFILE}`);
753
- return { action: "read", limit: params.limit } as ViceOperations[O]["params"];
754
- }
755
- case "memmap": {
756
- onlyFields(params, params.action === "clear" ? ["action"] : ["action", "start", "end", "maxRanges"]);
757
- if (params.action === "clear") return { action: "clear" } as ViceOperations[O]["params"];
758
- if (params.action !== "read") invalid("action must be read or clear");
759
- if (!isInteger(params.start, 0, 0xffff) || !isInteger(params.end, 0, 0xffff)) invalid("start and end must be integers from 0 to 65535");
760
- if (params.end < params.start) invalid("end must not be before start");
761
- if (!isInteger(params.maxRanges, 1, MAX_MEMMAP_RANGES)) invalid(`maxRanges must be an integer from 1 to ${MAX_MEMMAP_RANGES}`);
762
- return { action: "read", start: params.start, end: params.end, maxRanges: params.maxRanges } as ViceOperations[O]["params"];
763
- }
764
- case "sprites": {
765
- onlyFields(params, ["indexes"]);
766
- const indexes = params.indexes;
767
- if (!Array.isArray(indexes) || !indexes.every((index) => isInteger(index, 0, 7))) invalid("sprites holds sprite numbers 0 to 7");
768
- if (new Set(indexes).size !== indexes.length) invalid("sprites holds each sprite number once");
769
- return { indexes: indexes as number[] } as ViceOperations[O]["params"];
770
- }
771
- case "cia": {
772
- onlyFields(params, ["which"]);
773
- if (!isOneOf(CIA_SELECTIONS, params.which)) invalid("cia must be 1, 2 or both");
774
- return { which: params.which } as ViceOperations[O]["params"];
775
- }
776
- case "memoryRead": {
777
- onlyFields(params, ["address", "size", "space", "view"]);
778
- if (!isInteger(params.address, 0, 0xffff)) invalid("address must be an integer from 0 to 65535");
779
- if (!isInteger(params.size, 1, MAX_MEMORY_READ)) invalid(`size must be an integer from 1 to ${MAX_MEMORY_READ}`);
780
- if (params.address + params.size > 0x10000) invalid("the range runs past $ffff");
781
- if (!isOneOf(SPACES, params.space)) invalid("space must be c64 or drive8");
782
- if (!isOneOf(MEMORY_VIEWS, params.view)) invalid("view must be cpu or ram");
783
- const result: MemoryReadParams = { address: params.address, size: params.size, space: params.space, view: params.view };
784
- return result as ViceOperations[O]["params"];
785
- }
786
- case "ioRead": {
787
- onlyFields(params, ["address", "size"]);
788
- if (!isInteger(params.address, 0xd000, 0xdfff)) invalid("address must be in the I/O area $d000-$dfff");
789
- if (!isInteger(params.size, 1, 0x1000) || params.address + params.size > 0xe000) invalid("the range must stay inside $d000-$dfff");
790
- return { address: params.address, size: params.size } as ViceOperations[O]["params"];
791
- }
792
- case "registersGet": {
793
- onlyFields(params, ["space"]);
794
- if (!isOneOf(SPACES, params.space)) invalid("space must be c64 or drive8");
795
- return { space: params.space } as ViceOperations[O]["params"];
796
- }
797
- case "memoryWrite": {
798
- onlyFields(params, ["address", "data", "space", "view"]);
799
- if (!isInteger(params.address, 0, 0xffff)) invalid("address must be an integer from 0 to 65535");
800
- if (!isHexData(params.data) || params.data.length === 0) invalid("data must be lowercase hex bytes");
801
- const size = params.data.length / 2;
802
- if (size > MAX_MEMORY_WRITE) invalid(`data must be at most ${MAX_MEMORY_WRITE} bytes`);
803
- if (params.address + size > 0x10000) invalid("the range runs past $ffff");
804
- if (!isOneOf(SPACES, params.space)) invalid("space must be c64 or drive8");
805
- if (!isOneOf(MEMORY_VIEWS, params.view)) invalid("view must be cpu or ram");
806
- const result: MemoryWriteParams = { address: params.address, data: params.data, space: params.space, view: params.view };
807
- return result as ViceOperations[O]["params"];
808
- }
809
- case "memorySearch": {
810
- onlyFields(params, ["start", "end", "pattern", "space", "view", "maxResults"]);
811
- if (!isInteger(params.start, 0, 0xffff) || !isInteger(params.end, 0, 0xffff)) invalid("start and end must be integers from 0 to 65535");
812
- if (params.end < params.start) invalid("end must not be before start");
813
- const pattern = params.pattern;
814
- if (!Array.isArray(pattern) || pattern.length === 0 || pattern.length > MAX_SEARCH_PATTERN) {
815
- invalid(`pattern must have 1 to ${MAX_SEARCH_PATTERN} bytes`);
816
- }
817
- if (!pattern.every((token) => token === null || isInteger(token, 0, 0xff))) invalid("pattern holds bytes 0 to 255 or null wildcards");
818
- if (pattern.every((token) => token === null)) invalid("pattern needs at least one byte that is not a wildcard");
819
- if (!isOneOf(SPACES, params.space)) invalid("space must be c64 or drive8");
820
- if (!isOneOf(MEMORY_VIEWS, params.view)) invalid("view must be cpu or ram");
821
- if (!isInteger(params.maxResults, 1, MAX_SEARCH_RESULTS)) invalid(`maxResults must be an integer from 1 to ${MAX_SEARCH_RESULTS}`);
822
- return {
823
- start: params.start,
824
- end: params.end,
825
- pattern: pattern as Array<number | null>,
826
- space: params.space,
827
- view: params.view,
828
- maxResults: params.maxResults,
829
- } as ViceOperations[O]["params"];
830
- }
831
- case "memoryCompare": {
832
- onlyFields(params, ["left", "right", "size"]);
833
- if (!isInteger(params.size, 1, MAX_COMPARE_SIZE)) invalid(`size must be an integer from 1 to ${MAX_COMPARE_SIZE}`);
834
- const location = (value: unknown, name: string): MemoryLocation => {
835
- if (!isObject(value)) invalid(`${name} must be an object`);
836
- onlyFields(value, ["address", "space", "view"], name);
837
- if (!isInteger(value.address, 0, 0xffff)) invalid(`${name} address must be an integer from 0 to 65535`);
838
- if ((value.address as number) + (params.size as number) > 0x10000) invalid(`the ${name} range runs past $ffff`);
839
- if (!isOneOf(SPACES, value.space)) invalid(`${name} space must be c64 or drive8`);
840
- if (!isOneOf(MEMORY_VIEWS, value.view)) invalid(`${name} view must be cpu or ram`);
841
- return { address: value.address as number, space: value.space, view: value.view };
842
- };
843
- return { left: location(params.left, "left"), right: location(params.right, "right"), size: params.size } as ViceOperations[O]["params"];
844
- }
845
- case "disassemble": {
846
- onlyFields(params, ["address", "count", "space", "view"]);
847
- if (!isInteger(params.address, 0, 0xffff)) invalid("address must be an integer from 0 to 65535");
848
- if (!isInteger(params.count, 1, MAX_DISASSEMBLE)) invalid(`count must be an integer from 1 to ${MAX_DISASSEMBLE}`);
849
- if (!isOneOf(SPACES, params.space)) invalid("space must be c64 or drive8");
850
- if (!isOneOf(MEMORY_VIEWS, params.view)) invalid("view must be cpu or ram");
851
- return { address: params.address, count: params.count, space: params.space, view: params.view } as ViceOperations[O]["params"];
852
- }
853
- case "registersSet": {
854
- onlyFields(params, ["space", "values"]);
855
- if (!isOneOf(SPACES, params.space)) invalid("space must be c64 or drive8");
856
- const values = params.values;
857
- if (!isObject(values)) invalid("values must be an object");
858
- const known = ["pc", "a", "x", "y", "sp", "flags"];
859
- for (const key of Object.keys(values)) if (!known.includes(key)) invalid(`values has an unknown register: ${key}`);
860
- if (Object.keys(values).length === 0) invalid("values must name at least one register");
861
- const result: RegisterValues = {};
862
- if (values.pc !== undefined) {
863
- if (!isInteger(values.pc, 0, 0xffff)) invalid("pc must be an integer from 0 to 65535");
864
- result.pc = values.pc;
663
+ return { action: "restore" };
664
+ }
665
+ if (params.action !== "export") invalid("action must be export or restore");
666
+ onlyFields(params, ["action", "name"]);
667
+ return { action: "export", name: validName(params.name, "name") };
668
+ },
669
+ screenCompare: (params) => {
670
+ onlyFields(params, ["baseline", "maxMismatchRatio", "mask", "includeDiff"]);
671
+ const ratio = params.maxMismatchRatio;
672
+ if (typeof ratio !== "number" || !(ratio >= 0 && ratio <= 1)) invalid("maxMismatchRatio must be a number from 0 to 1");
673
+ if (!Array.isArray(params.mask) || params.mask.length > MAX_MASK_RECTANGLES) invalid(`mask must be a list of at most ${MAX_MASK_RECTANGLES} rectangles`);
674
+ const mask = params.mask.map((rectangle): Rectangle => {
675
+ if (!isObject(rectangle)) invalid("each mask entry must be a rectangle");
676
+ onlyFields(rectangle, ["x", "y", "width", "height"], "a mask rectangle");
677
+ const ok = isInteger(rectangle.x, 0, 4095) && isInteger(rectangle.y, 0, 4095) && isInteger(rectangle.width, 1, 4096) && isInteger(rectangle.height, 1, 4096);
678
+ if (!ok) invalid("a mask rectangle needs integer x, y, width and height, with width and height at least 1");
679
+ return { x: rectangle.x as number, y: rectangle.y as number, width: rectangle.width as number, height: rectangle.height as number };
680
+ });
681
+ if (typeof params.includeDiff !== "boolean") invalid("includeDiff must be true or false");
682
+ return { baseline: validName(params.baseline, "baseline"), maxMismatchRatio: ratio, mask, includeDiff: params.includeDiff };
683
+ },
684
+ cpuHistory: (params) => {
685
+ onlyFields(params, ["limit", "space"]);
686
+ if (!isInteger(params.limit, 1, MAX_HISTORY)) invalid(`limit must be an integer from 1 to ${MAX_HISTORY}`);
687
+ return { limit: params.limit, space: validSpace(params.space) };
688
+ },
689
+ trace: (params) => {
690
+ onlyFields(params, ["points", "frames", "maxHits"]);
691
+ if (!Array.isArray(params.points) || params.points.length === 0 || params.points.length > MAX_TRACE_POINTS) invalid(`points must be a list of 1 to ${MAX_TRACE_POINTS} points`);
692
+ const points = (params.points as unknown[]).map((point) => {
693
+ if (!isObject(point)) return invalid("a point is an object with kind, start and end");
694
+ onlyFields(point, ["kind", "start", "end"]);
695
+ if (!isOneOf(TRACE_KINDS, point.kind)) invalid("a point kind must be exec, read or write");
696
+ if (!isInteger(point.start, 0, 0xffff) || !isInteger(point.end, point.start as number, 0xffff)) invalid("a point needs start and end from $0000 to $ffff, the end not before the start");
697
+ return { kind: point.kind, start: point.start, end: point.end } as TracePoint;
698
+ });
699
+ if (!isInteger(params.frames, 1, MAX_TRACE_FRAMES)) invalid(`frames must be an integer from 1 to ${MAX_TRACE_FRAMES}`);
700
+ if (!isInteger(params.maxHits, 1, MAX_TRACE_HITS)) invalid(`maxHits must be an integer from 1 to ${MAX_TRACE_HITS}`);
701
+ return { points, frames: params.frames, maxHits: params.maxHits };
702
+ },
703
+ backtrace: (params) => {
704
+ onlyFields(params, ["depth", "space"]);
705
+ if (!isInteger(params.depth, 1, MAX_BACKTRACE)) invalid(`depth must be an integer from 1 to ${MAX_BACKTRACE}`);
706
+ return { depth: params.depth, space: validSpace(params.space) };
707
+ },
708
+ timing: (params) => {
709
+ onlyFields(params, ["action"]);
710
+ if (!isOneOf(TIMING_ACTIONS, params.action)) invalid("action must be start or read");
711
+ return { action: params.action };
712
+ },
713
+ observe: (params) => {
714
+ onlyFields(params, ["registers", "memory", "vicii", "sprites", "cia", "sid", "screen", "timing"]);
715
+ const result: ObserveParams = {};
716
+ if (params.registers !== undefined) result.registers = validSpace(params.registers, "registers");
717
+ if (params.memory !== undefined) {
718
+ const ranges = params.memory;
719
+ if (!Array.isArray(ranges) || ranges.length === 0 || ranges.length > MAX_OBSERVE_RANGES) {
720
+ invalid(`memory must hold 1 to ${MAX_OBSERVE_RANGES} ranges`);
865
721
  }
866
- for (const name of ["a", "x", "y", "sp"] as const) {
867
- if (values[name] === undefined) continue;
868
- if (!isInteger(values[name], 0, 0xff)) invalid(`${name} must be an integer from 0 to 255`);
869
- result[name] = values[name];
722
+ result.memory = ranges.map((range) => validateViceParams("memoryRead", range));
723
+ if (result.memory.reduce((sum, range) => sum + range.size, 0) > MAX_OBSERVE_BYTES) {
724
+ invalid(`the memory ranges must hold at most ${MAX_OBSERVE_BYTES} bytes together`);
870
725
  }
871
- if (values.flags !== undefined) {
872
- const flags = values.flags;
873
- if (!isObject(flags)) invalid("flags must be an object");
874
- const set: Partial<CpuFlags> = {};
875
- for (const [flag, value] of Object.entries(flags)) {
876
- if (!["n", "v", "b", "d", "i", "z", "c"].includes(flag)) invalid(`flags has an unknown flag: ${flag}`);
877
- if (typeof value !== "boolean") invalid(`flag ${flag} must be true or false`);
878
- set[flag as keyof CpuFlags] = value;
879
- }
880
- result.flags = set;
881
- }
882
- return { space: params.space, values: result } as ViceOperations[O]["params"];
883
726
  }
884
- case "execution": {
885
- onlyFields(params, ["action", "space", "count", "anchor"]);
886
- if (!isOneOf(EXECUTION_ACTIONS, params.action)) invalid(`action must be one of ${EXECUTION_ACTIONS.join(", ")}`);
887
- if (!isOneOf(SPACES, params.space)) invalid("space must be c64 or drive8");
888
- const counted = params.action === "step" || params.action === "next" || params.action === "advance-frames";
889
- if (params.count !== undefined) {
890
- if (!counted) invalid(`count is not used with action ${params.action}`);
891
- if (!isInteger(params.count, 1, MAX_EXECUTION_COUNT)) invalid(`count must be an integer from 1 to ${MAX_EXECUTION_COUNT}`);
892
- } else if (params.action === "advance-frames") {
893
- invalid("advance-frames needs count, the number of frames");
727
+ for (const flag of ["vicii", "sid", "screen", "timing"] as const) {
728
+ if (params[flag] === undefined) continue;
729
+ if (typeof params[flag] !== "boolean") invalid(`${flag} must be true or false`);
730
+ if (params[flag]) result[flag] = true;
731
+ }
732
+ if (params.sprites !== undefined) result.sprites = validateViceParams("sprites", { indexes: params.sprites }).indexes;
733
+ if (params.cia !== undefined) result.cia = validateViceParams("cia", { which: params.cia }).which;
734
+ if (Object.keys(result).length === 0) invalid("ask for at least one observation");
735
+ return result;
736
+ },
737
+ profile: (params) => {
738
+ onlyFields(params, params.action === "clear" ? ["action"] : ["action", "limit"]);
739
+ if (params.action === "clear") return { action: "clear" };
740
+ if (params.action !== "read") invalid("action must be read or clear");
741
+ if (!isInteger(params.limit, 1, MAX_PROFILE)) invalid(`limit must be an integer from 1 to ${MAX_PROFILE}`);
742
+ return { action: "read", limit: params.limit };
743
+ },
744
+ memmap: (params) => {
745
+ onlyFields(params, params.action === "clear" ? ["action"] : ["action", "start", "end", "maxRanges"]);
746
+ if (params.action === "clear") return { action: "clear" };
747
+ if (params.action !== "read") invalid("action must be read or clear");
748
+ if (!isInteger(params.start, 0, 0xffff) || !isInteger(params.end, 0, 0xffff)) invalid("start and end must be integers from 0 to 65535");
749
+ if (params.end < params.start) invalid("end must not be before start");
750
+ if (!isInteger(params.maxRanges, 1, MAX_MEMMAP_RANGES)) invalid(`maxRanges must be an integer from 1 to ${MAX_MEMMAP_RANGES}`);
751
+ return { action: "read", start: params.start, end: params.end, maxRanges: params.maxRanges };
752
+ },
753
+ sprites: (params) => {
754
+ onlyFields(params, ["indexes"]);
755
+ const indexes = params.indexes;
756
+ if (!Array.isArray(indexes) || !indexes.every((index) => isInteger(index, 0, 7))) invalid("sprites holds sprite numbers 0 to 7");
757
+ if (new Set(indexes).size !== indexes.length) invalid("sprites holds each sprite number once");
758
+ return { indexes: indexes as number[] };
759
+ },
760
+ cia: (params) => {
761
+ onlyFields(params, ["which"]);
762
+ if (!isOneOf(CIA_SELECTIONS, params.which)) invalid("cia must be 1, 2 or both");
763
+ return { which: params.which };
764
+ },
765
+ memoryRead: (params) => {
766
+ onlyFields(params, ["address", "size", "space", "view"]);
767
+ const address = validAddress(params.address);
768
+ if (!isInteger(params.size, 1, MAX_MEMORY_READ)) invalid(`size must be an integer from 1 to ${MAX_MEMORY_READ}`);
769
+ if (address + params.size > 0x10000) invalid("the range runs past $ffff");
770
+ return { address, size: params.size, space: validSpace(params.space), view: validView(params.view) };
771
+ },
772
+ ioRead: (params) => {
773
+ onlyFields(params, ["address", "size"]);
774
+ if (!isInteger(params.address, 0xd000, 0xdfff)) invalid("address must be in the I/O area $d000-$dfff");
775
+ if (!isInteger(params.size, 1, 0x1000) || params.address + params.size > 0xe000) invalid("the range must stay inside $d000-$dfff");
776
+ return { address: params.address, size: params.size };
777
+ },
778
+ registersGet: (params) => {
779
+ onlyFields(params, ["space"]);
780
+ return { space: validSpace(params.space) };
781
+ },
782
+ memoryWrite: (params) => {
783
+ onlyFields(params, ["address", "data", "space", "view"]);
784
+ const address = validAddress(params.address);
785
+ if (!isHexData(params.data) || params.data.length === 0) invalid("data must be lowercase hex bytes");
786
+ const size = params.data.length / 2;
787
+ if (size > MAX_MEMORY_WRITE) invalid(`data must be at most ${MAX_MEMORY_WRITE} bytes`);
788
+ if (address + size > 0x10000) invalid("the range runs past $ffff");
789
+ return { address, data: params.data, space: validSpace(params.space), view: validView(params.view) };
790
+ },
791
+ memorySearch: (params) => {
792
+ onlyFields(params, ["start", "end", "pattern", "space", "view", "maxResults"]);
793
+ if (!isInteger(params.start, 0, 0xffff) || !isInteger(params.end, 0, 0xffff)) invalid("start and end must be integers from 0 to 65535");
794
+ if (params.end < params.start) invalid("end must not be before start");
795
+ const pattern = params.pattern;
796
+ if (!Array.isArray(pattern) || pattern.length === 0 || pattern.length > MAX_SEARCH_PATTERN) {
797
+ invalid(`pattern must have 1 to ${MAX_SEARCH_PATTERN} bytes`);
798
+ }
799
+ if (!pattern.every((token) => token === null || isInteger(token, 0, 0xff))) invalid("pattern holds bytes 0 to 255 or null wildcards");
800
+ if (pattern.every((token) => token === null)) invalid("pattern needs at least one byte that is not a wildcard");
801
+ const space = validSpace(params.space);
802
+ const view = validView(params.view);
803
+ if (!isInteger(params.maxResults, 1, MAX_SEARCH_RESULTS)) invalid(`maxResults must be an integer from 1 to ${MAX_SEARCH_RESULTS}`);
804
+ return { start: params.start, end: params.end, pattern: pattern as Array<number | null>, space, view, maxResults: params.maxResults };
805
+ },
806
+ memoryCompare: (params) => {
807
+ onlyFields(params, ["left", "right", "size"]);
808
+ const size = params.size;
809
+ if (!isInteger(size, 1, MAX_COMPARE_SIZE)) invalid(`size must be an integer from 1 to ${MAX_COMPARE_SIZE}`);
810
+ const location = (value: unknown, name: string): MemoryLocation => {
811
+ if (!isObject(value)) invalid(`${name} must be an object`);
812
+ onlyFields(value, ["address", "space", "view"], name);
813
+ const address = validAddress(value.address, `${name} address`);
814
+ if (address + size > 0x10000) invalid(`the ${name} range runs past $ffff`);
815
+ return { address, space: validSpace(value.space, `${name} space`), view: validView(value.view, `${name} view`) };
816
+ };
817
+ return { left: location(params.left, "left"), right: location(params.right, "right"), size };
818
+ },
819
+ disassemble: (params) => {
820
+ onlyFields(params, ["address", "count", "space", "view"]);
821
+ const address = validAddress(params.address);
822
+ if (!isInteger(params.count, 1, MAX_DISASSEMBLE)) invalid(`count must be an integer from 1 to ${MAX_DISASSEMBLE}`);
823
+ return { address, count: params.count, space: validSpace(params.space), view: validView(params.view) };
824
+ },
825
+ registersSet: (params) => {
826
+ onlyFields(params, ["space", "values"]);
827
+ const space = validSpace(params.space);
828
+ const values = params.values;
829
+ if (!isObject(values)) invalid("values must be an object");
830
+ const known = ["pc", "a", "x", "y", "sp", "flags"];
831
+ for (const key of Object.keys(values)) if (!known.includes(key)) invalid(`values has an unknown register: ${key}`);
832
+ if (Object.keys(values).length === 0) invalid("values must name at least one register");
833
+ const result: RegisterValues = {};
834
+ if (values.pc !== undefined) result.pc = validAddress(values.pc, "pc");
835
+ for (const name of ["a", "x", "y", "sp"] as const) {
836
+ if (values[name] === undefined) continue;
837
+ if (!isInteger(values[name], 0, 0xff)) invalid(`${name} must be an integer from 0 to 255`);
838
+ result[name] = values[name];
839
+ }
840
+ if (values.flags !== undefined) {
841
+ const flags = values.flags;
842
+ if (!isObject(flags)) invalid("flags must be an object");
843
+ const set: Partial<CpuFlags> = {};
844
+ for (const [flag, value] of Object.entries(flags)) {
845
+ if (!Object.hasOwn(FLAG_BITS, flag)) invalid(`flags has an unknown flag: ${flag}`);
846
+ if (typeof value !== "boolean") invalid(`flag ${flag} must be true or false`);
847
+ set[flag as keyof CpuFlags] = value;
894
848
  }
895
- const result: ExecutionParams = { action: params.action, space: params.space };
896
- if (counted) result.count = (params.count as number | undefined) ?? 1;
897
- if (params.anchor !== undefined) {
898
- if (params.action !== "advance-frames") invalid(`anchor is not used with action ${params.action}`);
899
- const anchor = params.anchor;
900
- if (!isObject(anchor) || !isInteger(anchor.line, 0, 511) || !isInteger(anchor.cycle, 0, 127)) invalid("anchor needs line from 0 to 511 and cycle from 0 to 127");
901
- onlyFields(anchor as Record<string, unknown>, ["line", "cycle"]);
902
- result.anchor = { line: anchor.line as number, cycle: anchor.cycle as number };
849
+ result.flags = set;
850
+ }
851
+ return { space, values: result };
852
+ },
853
+ execution: (params) => {
854
+ onlyFields(params, ["action", "space", "count", "anchor"]);
855
+ if (!isOneOf(EXECUTION_ACTIONS, params.action)) invalid(`action must be one of ${EXECUTION_ACTIONS.join(", ")}`);
856
+ const result: ExecutionParams = { action: params.action, space: validSpace(params.space) };
857
+ const counted = params.action === "step" || params.action === "next" || params.action === "advance-frames";
858
+ if (params.count !== undefined) {
859
+ if (!counted) invalid(`count is not used with action ${params.action}`);
860
+ if (!isInteger(params.count, 1, MAX_EXECUTION_COUNT)) invalid(`count must be an integer from 1 to ${MAX_EXECUTION_COUNT}`);
861
+ } else if (params.action === "advance-frames") {
862
+ invalid("advance-frames needs count, the number of frames");
863
+ }
864
+ if (counted) result.count = (params.count as number | undefined) ?? 1;
865
+ if (params.anchor !== undefined) {
866
+ if (params.action !== "advance-frames") invalid(`anchor is not used with action ${params.action}`);
867
+ const anchor = params.anchor;
868
+ if (!isObject(anchor) || !isInteger(anchor.line, 0, 511) || !isInteger(anchor.cycle, 0, 127)) invalid("anchor needs line from 0 to 511 and cycle from 0 to 127");
869
+ onlyFields(anchor as Record<string, unknown>, ["line", "cycle"]);
870
+ result.anchor = { line: anchor.line as number, cycle: anchor.cycle as number };
871
+ }
872
+ return result;
873
+ },
874
+ runUntil: (params) => {
875
+ onlyFields(params, ["target", "timeoutFrames"]);
876
+ if (!isInteger(params.timeoutFrames, 1, MAX_TIMEOUT_FRAMES)) invalid(`timeoutFrames must be an integer from 1 to ${MAX_TIMEOUT_FRAMES}`);
877
+ return { target: validateRunTarget(params.target), timeoutFrames: params.timeoutFrames };
878
+ },
879
+ programLoad: (params) => {
880
+ onlyFields(params, ["address"]);
881
+ return params.address === undefined ? {} : { address: validAddress(params.address) };
882
+ },
883
+ autostart: (params) => {
884
+ onlyFields(params, ["type", "index", "run"]);
885
+ if (!isOneOf(AUTOSTART_TYPES, params.type)) invalid(`the file must be one of: ${AUTOSTART_TYPES.map((type) => `.${type}`).join(", ")}`);
886
+ if (!isInteger(params.index, 0, 0xffff)) invalid("index must be an integer from 0 to 65535");
887
+ if (typeof params.run !== "boolean") invalid("run must be true or false");
888
+ return { type: params.type, index: params.index, run: params.run };
889
+ },
890
+ diskAttach: (params) => {
891
+ onlyFields(params, ["type"]);
892
+ if (!isOneOf(DISK_TYPES, params.type)) invalid(`the disk image must be one of: ${DISK_TYPES.map((type) => `.${type}`).join(", ")}`);
893
+ return { type: params.type };
894
+ },
895
+ keyboard: (params) => {
896
+ onlyFields(params, ["data"]);
897
+ if (!isHexData(params.data) || params.data.length === 0) invalid("data must be PETSCII bytes as lowercase hex");
898
+ if (params.data.length / 2 > MAX_KEYBOARD_BYTES) invalid(`at most ${MAX_KEYBOARD_BYTES} bytes can be queued at once`);
899
+ // VICE feeds a C string, so a zero byte would end the text early.
900
+ if (/^(?:..)*?00/.test(params.data)) invalid("PETSCII byte 0 cannot be typed");
901
+ return { data: params.data };
902
+ },
903
+ press: (params) => {
904
+ onlyFields(params, ["taps", "frames"]);
905
+ if (!Array.isArray(params.taps) || params.taps.length === 0 || params.taps.length > MAX_PRESS_TAPS) invalid(`taps must be a list of 1 to ${MAX_PRESS_TAPS} taps`);
906
+ const taps = (params.taps as unknown[]).map((tap): KeyTap => {
907
+ if (!isObject(tap)) invalid("each tap must be an object with keys and restore");
908
+ onlyFields(tap, ["keys", "restore"], "a tap");
909
+ const { keys, restore } = tap;
910
+ if (!Array.isArray(keys) || !keys.every((key, index) => isInteger(key, 0, 63) && (index === 0 || key > (keys[index - 1] as number)))) {
911
+ invalid("keys of a tap must be matrix places from 0 to 63, each once, in rising order");
903
912
  }
904
- return result as ViceOperations[O]["params"];
905
- }
906
- case "runUntil": {
907
- onlyFields(params, ["target", "timeoutFrames"]);
908
- if (!isInteger(params.timeoutFrames, 1, MAX_TIMEOUT_FRAMES)) invalid(`timeoutFrames must be an integer from 1 to ${MAX_TIMEOUT_FRAMES}`);
909
- return { target: validateRunTarget(params.target), timeoutFrames: params.timeoutFrames } as ViceOperations[O]["params"];
910
- }
911
- case "programLoad": {
912
- onlyFields(params, ["address"]);
913
- if (params.address !== undefined && !isInteger(params.address, 0, 0xffff)) invalid("address must be an integer from 0 to 65535");
914
- return (params.address === undefined ? {} : { address: params.address }) as ViceOperations[O]["params"];
915
- }
916
- case "autostart": {
917
- onlyFields(params, ["type", "index", "run"]);
918
- if (!isOneOf(AUTOSTART_TYPES, params.type)) invalid(`the file must be one of: ${AUTOSTART_TYPES.map((type) => `.${type}`).join(", ")}`);
919
- if (!isInteger(params.index, 0, 0xffff)) invalid("index must be an integer from 0 to 65535");
920
- if (typeof params.run !== "boolean") invalid("run must be true or false");
921
- return { type: params.type, index: params.index, run: params.run } as ViceOperations[O]["params"];
922
- }
923
- case "diskAttach": {
924
- onlyFields(params, ["type"]);
925
- if (!isOneOf(DISK_TYPES, params.type)) invalid(`the disk image must be one of: ${DISK_TYPES.map((type) => `.${type}`).join(", ")}`);
926
- return { type: params.type } as ViceOperations[O]["params"];
927
- }
928
- case "keyboard": {
929
- onlyFields(params, ["data"]);
930
- if (!isHexData(params.data) || params.data.length === 0) invalid("data must be PETSCII bytes as lowercase hex");
931
- if (params.data.length / 2 > MAX_KEYBOARD_BYTES) invalid(`at most ${MAX_KEYBOARD_BYTES} bytes can be queued at once`);
932
- // VICE feeds a C string, so a zero byte would end the text early.
933
- if (/^(?:..)*?00/.test(params.data)) invalid("PETSCII byte 0 cannot be typed");
934
- return { data: params.data } as ViceOperations[O]["params"];
935
- }
936
- case "press": {
937
- onlyFields(params, ["taps", "frames"]);
938
- if (!Array.isArray(params.taps) || params.taps.length === 0 || params.taps.length > MAX_PRESS_TAPS) invalid(`taps must be a list of 1 to ${MAX_PRESS_TAPS} taps`);
939
- const taps = (params.taps as unknown[]).map((tap): KeyTap => {
940
- if (!isObject(tap)) invalid("each tap must be an object with keys and restore");
941
- onlyFields(tap, ["keys", "restore"], "a tap");
942
- const { keys, restore } = tap;
943
- if (!Array.isArray(keys) || !keys.every((key, index) => isInteger(key, 0, 63) && (index === 0 || key > (keys[index - 1] as number)))) {
944
- invalid("keys of a tap must be matrix places from 0 to 63, each once, in rising order");
945
- }
946
- if (typeof restore !== "boolean") invalid("restore of a tap must be true or false");
947
- const count = keys.length + (restore ? 1 : 0);
948
- if (count === 0 || count > MAX_TAP_KEYS) invalid(`a tap holds 1 to ${MAX_TAP_KEYS} keys, RESTORE included`);
949
- return { keys: keys as number[], restore };
950
- });
951
- if (!isInteger(params.frames, 1, MAX_PRESS_FRAMES)) invalid(`frames must be a whole number from 1 to ${MAX_PRESS_FRAMES}`);
952
- return { taps, frames: params.frames } as ViceOperations[O]["params"];
953
- }
954
- case "joystick": {
955
- onlyFields(params, ["port", "direction", "fire"]);
956
- if (params.port !== 1 && params.port !== 2) invalid("port must be 1 or 2");
957
- if (!isOneOf(JOYSTICK_DIRECTIONS, params.direction)) invalid(`direction must be one of ${JOYSTICK_DIRECTIONS.join(", ")}`);
958
- if (typeof params.fire !== "boolean") invalid("fire must be true or false");
959
- return { port: params.port, direction: params.direction, fire: params.fire } as ViceOperations[O]["params"];
960
- }
961
- case "breakpoint":
962
- case "watchpoint": {
963
- if (!isOneOf(CHECKPOINT_ACTIONS, params.action)) invalid(`action must be one of ${CHECKPOINT_ACTIONS.join(", ")}`);
964
- if (params.action === "list") {
965
- onlyFields(params, ["action"]);
966
- return { action: "list" } as ViceOperations[O]["params"];
967
- }
968
- if (params.action !== "add") {
969
- onlyFields(params, ["action", "id"]);
970
- if (!isInteger(params.id, 1, 0xffff_ffff)) invalid("id must be a positive integer");
971
- return { action: params.action, id: params.id } as ViceOperations[O]["params"];
972
- }
973
- onlyFields(params, op === "watchpoint" ? ["action", "address", "space", "size", "access", "condition"] : ["action", "address", "space", "size", "condition"]);
974
- if (!isInteger(params.address, 0, 0xffff)) invalid("address must be an integer from 0 to 65535");
975
- if (!isOneOf(SPACES, params.space)) invalid("space must be c64 or drive8");
976
- const added: Record<string, unknown> = { action: "add", address: params.address, space: params.space };
977
- if (op === "watchpoint") {
978
- if (!isInteger(params.size, 1, MAX_WATCH_SIZE)) invalid(`size must be an integer from 1 to ${MAX_WATCH_SIZE}`);
979
- if (params.address + params.size > 0x10000) invalid("the range runs past $ffff");
980
- if (!isOneOf(WATCH_ACCESS, params.access)) invalid("access must be read, write or read-write");
981
- added.size = params.size;
982
- added.access = params.access;
983
- } else if (params.size !== undefined) {
984
- if (!isInteger(params.size, 1, 0x10000) || params.address + params.size > 0x10000) invalid("size must keep the range inside $0000-$ffff");
985
- added.size = params.size;
986
- }
987
- if (params.condition !== undefined) added.condition = validateCondition(params.condition);
988
- return added as ViceOperations[O]["params"];
989
- }
990
- case "reset": {
991
- onlyFields(params, ["mode", "run"]);
992
- if (!isOneOf(RESET_MODES, params.mode)) invalid("mode must be soft or hard");
993
- if (typeof params.run !== "boolean") invalid("run must be true or false");
994
- return { mode: params.mode, run: params.run } as ViceOperations[O]["params"];
995
- }
996
- case "warp": {
997
- onlyFields(params, ["enabled"]);
998
- if (typeof params.enabled !== "boolean") invalid("enabled must be true or false");
999
- return { enabled: params.enabled } as ViceOperations[O]["params"];
1000
- }
1001
- case "window": {
1002
- onlyFields(params, ["action"]);
1003
- if (!isOneOf(WINDOW_ACTIONS, params.action)) invalid("action must be open or close");
1004
- return { action: params.action } as ViceOperations[O]["params"];
1005
- }
1006
- }
1007
- return invalid(`unknown operation: ${String(op)}`);
913
+ if (typeof restore !== "boolean") invalid("restore of a tap must be true or false");
914
+ const count = keys.length + (restore ? 1 : 0);
915
+ if (count === 0 || count > MAX_TAP_KEYS) invalid(`a tap holds 1 to ${MAX_TAP_KEYS} keys, RESTORE included`);
916
+ return { keys: keys as number[], restore };
917
+ });
918
+ if (!isInteger(params.frames, 1, MAX_PRESS_FRAMES)) invalid(`frames must be a whole number from 1 to ${MAX_PRESS_FRAMES}`);
919
+ return { taps, frames: params.frames };
920
+ },
921
+ joystick: (params) => {
922
+ onlyFields(params, ["port", "direction", "fire"]);
923
+ if (params.port !== 1 && params.port !== 2) invalid("port must be 1 or 2");
924
+ if (!isOneOf(JOYSTICK_DIRECTIONS, params.direction)) invalid(`direction must be one of ${JOYSTICK_DIRECTIONS.join(", ")}`);
925
+ if (typeof params.fire !== "boolean") invalid("fire must be true or false");
926
+ return { port: params.port, direction: params.direction, fire: params.fire };
927
+ },
928
+ breakpoint: (params) => {
929
+ const other = pointAction(params);
930
+ if (other !== undefined) return other;
931
+ onlyFields(params, ["action", "address", "space", "size", "condition"]);
932
+ const added: Extract<BreakpointParams, { action: "add" }> = { action: "add", address: validAddress(params.address), space: validSpace(params.space) };
933
+ if (params.size !== undefined) {
934
+ if (!isInteger(params.size, 1, 0x10000) || added.address + params.size > 0x10000) invalid("size must keep the range inside $0000-$ffff");
935
+ added.size = params.size;
936
+ }
937
+ if (params.condition !== undefined) added.condition = validateCondition(params.condition);
938
+ return added;
939
+ },
940
+ watchpoint: (params) => {
941
+ const other = pointAction(params);
942
+ if (other !== undefined) return other;
943
+ onlyFields(params, ["action", "address", "space", "size", "access", "condition"]);
944
+ const address = validAddress(params.address);
945
+ const space = validSpace(params.space);
946
+ if (!isInteger(params.size, 1, MAX_WATCH_SIZE)) invalid(`size must be an integer from 1 to ${MAX_WATCH_SIZE}`);
947
+ if (address + params.size > 0x10000) invalid("the range runs past $ffff");
948
+ if (!isOneOf(WATCH_ACCESS, params.access)) invalid("access must be read, write or read-write");
949
+ const added: Extract<WatchpointParams, { action: "add" }> = { action: "add", address, space, size: params.size, access: params.access };
950
+ if (params.condition !== undefined) added.condition = validateCondition(params.condition);
951
+ return added;
952
+ },
953
+ reset: (params) => {
954
+ onlyFields(params, ["mode", "run"]);
955
+ if (!isOneOf(RESET_MODES, params.mode)) invalid("mode must be soft or hard");
956
+ if (typeof params.run !== "boolean") invalid("run must be true or false");
957
+ return { mode: params.mode, run: params.run };
958
+ },
959
+ warp: (params) => {
960
+ onlyFields(params, ["enabled"]);
961
+ if (typeof params.enabled !== "boolean") invalid("enabled must be true or false");
962
+ return { enabled: params.enabled };
963
+ },
964
+ window: (params) => {
965
+ onlyFields(params, ["action"]);
966
+ if (!isOneOf(WINDOW_ACTIONS, params.action)) invalid("action must be open or close");
967
+ return { action: params.action };
968
+ },
969
+ };
970
+
971
+ export const VICE_OPERATIONS = Object.keys(PARAMS) as readonly ViceOperation[];
972
+
973
+ /** Validates request parameters on the host. Throws WireFailure(invalid-input). */
974
+ export function validateViceParams<O extends ViceOperation>(op: O, params: unknown): ViceOperations[O]["params"] {
975
+ if (!isObject(params)) invalid("parameters must be an object");
976
+ if (!Object.hasOwn(PARAMS, op)) invalid(`unknown operation: ${String(op)}`);
977
+ return PARAMS[op](params);
1008
978
  }
1009
979
 
1010
980
  /**
1011
981
  * Validates a typed condition. Raster bounds here cover both video
1012
982
  * standards; the session checks them against its own standard.
1013
983
  */
1014
- export function validateCondition(value: unknown): Condition {
984
+ function validateCondition(value: unknown): Condition {
1015
985
  if (!isObject(value)) invalid("condition must be an object");
1016
986
  const byte = (field: unknown, name: string) => {
1017
987
  if (!isInteger(field, 0, 0xff)) invalid(`condition ${name} must be an integer from 0 to 255`);
@@ -1026,18 +996,11 @@ export function validateCondition(value: unknown): Condition {
1026
996
  }
1027
997
  case "memory": {
1028
998
  onlyFields(value, ["kind", "address", "operator", "value", "space", "view"], "the condition");
1029
- if (!isInteger(value.address, 0, 0xffff)) invalid("condition address must be an integer from 0 to 65535");
999
+ const address = validAddress(value.address, "condition address");
1030
1000
  if (!isOneOf(COMPARISONS, value.operator)) invalid(`condition operator must be one of ${COMPARISONS.join(", ")}`);
1031
- if (!isOneOf(SPACES, value.space)) invalid("condition space must be c64 or drive8");
1032
- if (!isOneOf(MEMORY_VIEWS, value.view)) invalid("condition view must be cpu or ram");
1033
- return {
1034
- kind: "memory",
1035
- address: value.address,
1036
- operator: value.operator,
1037
- value: byte(value.value, "value"),
1038
- space: value.space,
1039
- view: value.view,
1040
- };
1001
+ const space = validSpace(value.space, "condition space");
1002
+ const view = validView(value.view, "condition view");
1003
+ return { kind: "memory", address, operator: value.operator, value: byte(value.value, "value"), space, view };
1041
1004
  }
1042
1005
  case "raster": {
1043
1006
  onlyFields(value, ["kind", "line", "cycle"], "the condition");
@@ -1056,22 +1019,16 @@ function validateRunTarget(value: unknown): RunTarget {
1056
1019
  switch (value.kind) {
1057
1020
  case "address": {
1058
1021
  onlyFields(value, ["kind", "address", "space", "condition"], "the target");
1059
- if (!isInteger(value.address, 0, 0xffff)) invalid("target address must be an integer from 0 to 65535");
1060
- if (!isOneOf(SPACES, value.space)) invalid("target space must be c64 or drive8");
1061
- const target: RunTarget = { kind: "address", address: value.address, space: value.space };
1022
+ const target: Extract<RunTarget, { kind: "address" }> = { kind: "address", address: validAddress(value.address, "target address"), space: validSpace(value.space, "target space") };
1062
1023
  if (value.condition !== undefined) target.condition = validateCondition(value.condition);
1063
1024
  return target;
1064
1025
  }
1065
- case "memory": {
1026
+ case "memory":
1066
1027
  onlyFields(value, ["kind", "address", "operator", "value", "space", "view"], "the target");
1067
- const { kind: _kind, ...condition } = validateCondition({ ...value, kind: "memory" }) as Extract<Condition, { kind: "memory" }>;
1068
- return { kind: "memory", ...condition };
1069
- }
1070
- case "raster": {
1028
+ return validateCondition(value) as Extract<Condition, { kind: "memory" }>;
1029
+ case "raster":
1071
1030
  onlyFields(value, ["kind", "line", "cycle"], "the target");
1072
- const raster = validateCondition(value) as Extract<Condition, { kind: "raster" }>;
1073
- return raster;
1074
- }
1031
+ return validateCondition(value) as Extract<Condition, { kind: "raster" }>;
1075
1032
  }
1076
1033
  return invalid("target kind must be address, memory or raster");
1077
1034
  }
@@ -1116,236 +1073,127 @@ function isHexData(value: unknown): value is string {
1116
1073
  return typeof value === "string" && /^(?:[0-9a-f]{2})*$/.test(value);
1117
1074
  }
1118
1075
 
1119
- function isFlags(value: unknown): value is CpuFlags {
1120
- return isObject(value) && (["n", "v", "b", "d", "i", "z", "c"] as const).every((flag) => typeof value[flag] === "boolean");
1076
+ function isRegisters(value: Fields): boolean {
1077
+ return (
1078
+ isInteger(value.pc, 0, 0xffff) &&
1079
+ (["a", "x", "y", "sp"] as const).every((name) => isInteger(value[name], 0, 0xff)) &&
1080
+ isObject(value.flags) &&
1081
+ Object.keys(FLAG_BITS).every((flag) => typeof (value.flags as Fields)[flag] === "boolean")
1082
+ );
1121
1083
  }
1122
1084
 
1123
- /** Validates a reply result on the client. Throws ProtocolError on a malformed result. */
1124
- export function validateViceResult<O extends ViceOperation>(op: O, value: unknown): ViceOperations[O]["result"] {
1125
- if (!isObject(value)) throw new ProtocolError(`${op} result is not an object`);
1126
- switch (op) {
1127
- case "status": {
1128
- const ok =
1129
- isOneOf(RUN_STATES, value.state) &&
1130
- isOneOf(VIDEO_STANDARDS, value.videoStandard) &&
1131
- typeof value.warp === "boolean" &&
1132
- typeof value.window === "boolean" &&
1133
- (value.pc === undefined || isInteger(value.pc, 0, 0xffff)) &&
1134
- typeof value.cartridge === "boolean" &&
1135
- (value.stopReason === undefined || isOneOf(POINT_STOP_REASONS, value.stopReason)) &&
1136
- (value.pointId === undefined || isInteger(value.pointId, 1, Number.MAX_SAFE_INTEGER)) &&
1137
- (value.space === undefined || isOneOf(SPACES, value.space));
1138
- if (!ok) throw new ProtocolError("status result is malformed");
1139
- break;
1140
- }
1141
- case "memoryRead":
1142
- case "ioRead": {
1143
- if (!isInteger(value.address, 0, 0xffff) || !isHexData(value.data)) {
1144
- throw new ProtocolError(`${op} result is malformed`);
1145
- }
1146
- break;
1147
- }
1148
- case "registersGet": {
1149
- const ok =
1150
- isInteger(value.pc, 0, 0xffff) &&
1151
- (["a", "x", "y", "sp"] as const).every((name) => isInteger(value[name], 0, 0xff)) &&
1152
- isFlags(value.flags);
1153
- if (!ok) throw new ProtocolError("registersGet result is malformed");
1154
- break;
1155
- }
1156
- case "memoryWrite": {
1157
- if (!isInteger(value.address, 0, 0xffff) || !isInteger(value.bytesWritten, 1, MAX_MEMORY_WRITE)) {
1158
- throw new ProtocolError("memoryWrite result is malformed");
1159
- }
1160
- break;
1161
- }
1162
- case "registersSet": {
1163
- return validateViceResult("registersGet", value) as unknown as ViceOperations[O]["result"];
1164
- }
1165
- case "memorySearch": {
1166
- if (!Array.isArray(value.matches) || !value.matches.every((match) => isInteger(match, 0, 0xffff))) {
1167
- throw new ProtocolError("memorySearch result is malformed");
1168
- }
1169
- break;
1170
- }
1171
- case "memoryCompare": {
1172
- const ok =
1173
- typeof value.equal === "boolean" &&
1174
- isInteger(value.differentBytes, 0, MAX_COMPARE_SIZE) &&
1175
- Array.isArray(value.firstDifferences) &&
1176
- value.firstDifferences.length <= MAX_COMPARE_DIFFERENCES &&
1177
- value.firstDifferences.every(
1178
- (difference) =>
1179
- isObject(difference) &&
1180
- isInteger(difference.offset, 0, MAX_COMPARE_SIZE - 1) &&
1181
- isInteger(difference.left, 0, 0xff) &&
1182
- isInteger(difference.right, 0, 0xff),
1183
- );
1184
- if (!ok) throw new ProtocolError("memoryCompare result is malformed");
1185
- break;
1186
- }
1187
- case "disassemble": {
1188
- const ok =
1189
- Array.isArray(value.instructions) &&
1190
- value.instructions.every(
1191
- (instruction) =>
1192
- isObject(instruction) &&
1193
- isInteger(instruction.address, 0, 0xffff) &&
1194
- isHexData(instruction.bytes) &&
1195
- typeof instruction.text === "string",
1196
- );
1197
- if (!ok) throw new ProtocolError("disassemble result is malformed");
1198
- break;
1199
- }
1200
- case "runUntil": {
1201
- const ok =
1202
- typeof value.reached === "boolean" &&
1203
- isOneOf(STOP_REASONS, value.stopReason) &&
1204
- isOneOf(RUN_STATES, value.state) &&
1205
- isInteger(value.pc, 0, 0xffff) &&
1206
- isOneOf(SPACES, value.space) &&
1207
- (value.pointId === undefined || isInteger(value.pointId, 1, Number.MAX_SAFE_INTEGER));
1208
- if (!ok) throw new ProtocolError("runUntil result is malformed");
1209
- break;
1210
- }
1211
- case "execution": {
1212
- const ok =
1213
- isOneOf(RUN_STATES, value.state) &&
1214
- (value.pc === undefined || isInteger(value.pc, 0, 0xffff)) &&
1215
- (value.executed === undefined || isInteger(value.executed, 0, MAX_EXECUTION_COUNT)) &&
1216
- (value.advancedFrames === undefined || isInteger(value.advancedFrames, 0, MAX_EXECUTION_COUNT)) &&
1217
- (value.stopReason === undefined || value.stopReason === "frames" || isOneOf(POINT_STOP_REASONS, value.stopReason)) &&
1218
- (value.pointId === undefined || isInteger(value.pointId, 1, Number.MAX_SAFE_INTEGER)) &&
1219
- (value.space === undefined || isOneOf(SPACES, value.space));
1220
- if (!ok) throw new ProtocolError("execution result is malformed");
1221
- break;
1222
- }
1223
- case "reset":
1224
- case "autostart": {
1225
- if (!isOneOf(RUN_STATES, value.state)) throw new ProtocolError(`${op} result is malformed`);
1226
- if (op === "reset" && value.pc !== undefined && !isInteger(value.pc, 0, 0xffff)) throw new ProtocolError("reset result is malformed");
1227
- if (op === "autostart" && !(value.joysticksCentred === undefined || (Array.isArray(value.joysticksCentred) && value.joysticksCentred.every((port) => port === 1 || port === 2)))) {
1228
- throw new ProtocolError("autostart result is malformed");
1229
- }
1230
- break;
1231
- }
1232
- case "programLoad": {
1233
- const ok = isOneOf(RUN_STATES, value.state) && isInteger(value.loadAddress, 0, 0xffff) && isInteger(value.size, 1, 0x10000);
1234
- if (!ok) throw new ProtocolError("programLoad result is malformed");
1235
- break;
1236
- }
1237
- case "diskAttach": {
1238
- if (typeof value.attached !== "boolean") throw new ProtocolError("diskAttach result is malformed");
1239
- break;
1240
- }
1241
- case "keyboard": {
1242
- if (!isInteger(value.queuedBytes, 1, MAX_KEYBOARD_BYTES)) throw new ProtocolError("keyboard result is malformed");
1243
- break;
1244
- }
1245
- case "press": {
1246
- const ok =
1247
- isInteger(value.taps, 0, MAX_PRESS_TAPS) &&
1248
- isOneOf(RUN_STATES, value.state) &&
1249
- (value.stopReason === undefined || isOneOf(POINT_STOP_REASONS, value.stopReason)) &&
1250
- (value.pc === undefined || isInteger(value.pc, 0, 0xffff)) &&
1251
- (value.pointId === undefined || isInteger(value.pointId, 1, Number.MAX_SAFE_INTEGER)) &&
1252
- (value.space === undefined || isOneOf(SPACES, value.space));
1253
- if (!ok) throw new ProtocolError("press result is malformed");
1254
- break;
1255
- }
1256
- case "breakpoint": {
1257
- const ok = Array.isArray(value.breakpoints) ? value.breakpoints.every(isBreakpoint) : isRemoved(value) || isBreakpoint(value);
1258
- if (!ok) throw new ProtocolError("breakpoint result is malformed");
1259
- break;
1260
- }
1261
- case "watchpoint": {
1262
- const ok = Array.isArray(value.watchpoints) ? value.watchpoints.every(isWatchpoint) : isRemoved(value) || isWatchpoint(value);
1263
- if (!ok) throw new ProtocolError("watchpoint result is malformed");
1264
- break;
1265
- }
1266
- case "screenCompare": {
1267
- const bounds = value.bounds;
1268
- const ok =
1269
- typeof value.match === "boolean" &&
1270
- isInteger(value.mismatchingPixels, 0, 1 << 20) &&
1271
- isInteger(value.comparedPixels, 1, 1 << 20) &&
1272
- (value.mismatchingPixels as number) <= (value.comparedPixels as number) &&
1273
- typeof value.mismatchRatio === "number" &&
1274
- value.mismatchRatio >= 0 &&
1275
- value.mismatchRatio <= 1 &&
1276
- (bounds === undefined ||
1277
- (isObject(bounds) && (["x", "y", "width", "height"] as const).every((field) => isInteger(bounds[field], 0, 4096)))) &&
1278
- (value.diffPng === undefined || (typeof value.diffPng === "string" && /^[A-Za-z0-9+/]+={0,2}$/.test(value.diffPng)));
1279
- if (!ok) throw new ProtocolError("screenCompare result is malformed");
1280
- break;
1281
- }
1282
- case "screenBaselines": {
1283
- if (!Array.isArray(value.baselines) || !value.baselines.every((name) => typeof name === "string")) {
1284
- throw new ProtocolError("screenBaselines result is malformed");
1285
- }
1286
- break;
1287
- }
1288
- case "screenDiscard": {
1289
- if (value.removed !== true) throw new ProtocolError("screenDiscard result is malformed");
1290
- break;
1291
- }
1292
- case "snapshot": {
1293
- const ok =
1294
- (value.saved === true && typeof value.name === "string") ||
1295
- (value.restored === true && isOneOf(RUN_STATES, value.state)) ||
1296
- (Array.isArray(value.snapshots) && value.snapshots.every((name) => typeof name === "string")) ||
1297
- value.removed === true;
1298
- if (!ok) throw new ProtocolError("snapshot result is malformed");
1299
- break;
1300
- }
1301
- case "snapshotFile": {
1302
- const ok = (value.exported === true && isInteger(value.bytes, 1, 0xffff_ffff)) || (value.restored === true && isOneOf(RUN_STATES, value.state));
1303
- if (!ok) throw new ProtocolError("snapshotFile result is malformed");
1304
- break;
1305
- }
1306
- case "cpuHistory": {
1307
- const ok =
1308
- Array.isArray(value.entries) &&
1309
- value.entries.every(
1310
- (entry) =>
1311
- isObject(entry) &&
1312
- isInteger(entry.address, 0, 0xffff) &&
1313
- isHexData(entry.bytes) &&
1314
- typeof entry.text === "string" &&
1315
- (["a", "x", "y", "sp"] as const).every((name) => isInteger(entry[name], 0, 0xff)) &&
1316
- (entry.rasterLine === undefined || isInteger(entry.rasterLine, 0, 511)) &&
1317
- (entry.rasterCycle === undefined || isInteger(entry.rasterCycle, 0, 127)),
1318
- );
1319
- if (!ok) throw new ProtocolError("cpuHistory result is malformed");
1320
- break;
1321
- }
1322
- case "trace": {
1323
- const ok = isInteger(value.hits, 0, MAX_TRACE_HITS) && isInteger(value.frames, 0, MAX_TRACE_FRAMES) && isOneOf(TRACE_ENDS, value.end) && isInteger(value.pc, 0, 0xffff);
1324
- if (!ok) throw new ProtocolError("trace result is malformed");
1325
- break;
1326
- }
1327
- case "backtrace": {
1328
- const ok =
1329
- Array.isArray(value.frames) &&
1330
- value.frames.every(
1331
- (frame) =>
1332
- isObject(frame) && isInteger(frame.address, 0, 0xffff) && (frame.returnAddress === undefined || isInteger(frame.returnAddress, 0, 0xffff)),
1333
- );
1334
- if (!ok) throw new ProtocolError("backtrace result is malformed");
1335
- break;
1336
- }
1337
- case "timing": {
1338
- const ok = "cycles" in value ? typeof value.cycles === "string" && /^\d+$/.test(value.cycles) : value.started === true;
1339
- if (!ok) throw new ProtocolError("timing result is malformed");
1340
- break;
1341
- }
1342
- case "profile": {
1343
- if ("cleared" in value) {
1344
- if (value.cleared !== true) throw new ProtocolError("profile result is malformed");
1345
- break;
1346
- }
1347
- const ok =
1348
- Array.isArray(value.entries) &&
1085
+ /** The fields that a stop can add to a result: the pc, the point that stopped the machine, and the CPU that stopped. */
1086
+ function hasStopFields(value: Fields): boolean {
1087
+ return (
1088
+ (value.pc === undefined || isInteger(value.pc, 0, 0xffff)) &&
1089
+ (value.pointId === undefined || isInteger(value.pointId, 1, Number.MAX_SAFE_INTEGER)) &&
1090
+ (value.space === undefined || isOneOf(SPACES, value.space))
1091
+ );
1092
+ }
1093
+
1094
+ function isMemoryRead(value: Fields): boolean {
1095
+ return isInteger(value.address, 0, 0xffff) && isHexData(value.data);
1096
+ }
1097
+
1098
+ /** The shape check of each operation's result. A check can also throw its own ProtocolError. */
1099
+ const RESULTS: Record<ViceOperation, (value: Fields) => boolean> = {
1100
+ status: (value) =>
1101
+ isOneOf(RUN_STATES, value.state) &&
1102
+ isOneOf(VIDEO_STANDARDS, value.videoStandard) &&
1103
+ typeof value.warp === "boolean" &&
1104
+ typeof value.window === "boolean" &&
1105
+ typeof value.cartridge === "boolean" &&
1106
+ (value.stopReason === undefined || isOneOf(POINT_STOP_REASONS, value.stopReason)) &&
1107
+ hasStopFields(value),
1108
+ memoryRead: isMemoryRead,
1109
+ ioRead: isMemoryRead,
1110
+ registersGet: isRegisters,
1111
+ // A registersSet result is the full register set, and is refused as one.
1112
+ registersSet: (value) => (validateViceResult("registersGet", value), true),
1113
+ memoryWrite: (value) => isInteger(value.address, 0, 0xffff) && isInteger(value.bytesWritten, 1, MAX_MEMORY_WRITE),
1114
+ memorySearch: (value) => Array.isArray(value.matches) && value.matches.every((match) => isInteger(match, 0, 0xffff)),
1115
+ memoryCompare: (value) =>
1116
+ typeof value.equal === "boolean" &&
1117
+ isInteger(value.differentBytes, 0, MAX_COMPARE_SIZE) &&
1118
+ Array.isArray(value.firstDifferences) &&
1119
+ value.firstDifferences.length <= MAX_COMPARE_DIFFERENCES &&
1120
+ value.firstDifferences.every(
1121
+ (difference) =>
1122
+ isObject(difference) && isInteger(difference.offset, 0, MAX_COMPARE_SIZE - 1) && isInteger(difference.left, 0, 0xff) && isInteger(difference.right, 0, 0xff),
1123
+ ),
1124
+ disassemble: (value) =>
1125
+ Array.isArray(value.instructions) &&
1126
+ value.instructions.every((instruction) => isObject(instruction) && isInteger(instruction.address, 0, 0xffff) && isHexData(instruction.bytes) && typeof instruction.text === "string"),
1127
+ runUntil: (value) =>
1128
+ typeof value.reached === "boolean" &&
1129
+ isOneOf(STOP_REASONS, value.stopReason) &&
1130
+ isOneOf(RUN_STATES, value.state) &&
1131
+ isInteger(value.pc, 0, 0xffff) &&
1132
+ isOneOf(SPACES, value.space) &&
1133
+ hasStopFields(value),
1134
+ execution: (value) =>
1135
+ isOneOf(RUN_STATES, value.state) &&
1136
+ (value.executed === undefined || isInteger(value.executed, 0, MAX_EXECUTION_COUNT)) &&
1137
+ (value.advancedFrames === undefined || isInteger(value.advancedFrames, 0, MAX_EXECUTION_COUNT)) &&
1138
+ (value.stopReason === undefined || value.stopReason === "frames" || isOneOf(POINT_STOP_REASONS, value.stopReason)) &&
1139
+ hasStopFields(value),
1140
+ reset: (value) => isOneOf(RUN_STATES, value.state) && (value.pc === undefined || isInteger(value.pc, 0, 0xffff)),
1141
+ autostart: (value) =>
1142
+ isOneOf(RUN_STATES, value.state) &&
1143
+ (value.joysticksCentred === undefined || (Array.isArray(value.joysticksCentred) && value.joysticksCentred.every((port) => port === 1 || port === 2))),
1144
+ programLoad: (value) => isOneOf(RUN_STATES, value.state) && isInteger(value.loadAddress, 0, 0xffff) && isInteger(value.size, 1, 0x10000),
1145
+ diskAttach: (value) => typeof value.attached === "boolean",
1146
+ keyboard: (value) => isInteger(value.queuedBytes, 1, MAX_KEYBOARD_BYTES),
1147
+ press: (value) =>
1148
+ isInteger(value.taps, 0, MAX_PRESS_TAPS) &&
1149
+ isOneOf(RUN_STATES, value.state) &&
1150
+ (value.stopReason === undefined || isOneOf(POINT_STOP_REASONS, value.stopReason)) &&
1151
+ hasStopFields(value),
1152
+ breakpoint: (value) => (Array.isArray(value.breakpoints) ? value.breakpoints.every(isBreakpoint) : isRemoved(value) || isBreakpoint(value)),
1153
+ watchpoint: (value) => (Array.isArray(value.watchpoints) ? value.watchpoints.every(isWatchpoint) : isRemoved(value) || isWatchpoint(value)),
1154
+ screenCompare: (value) => {
1155
+ const bounds = value.bounds;
1156
+ return (
1157
+ typeof value.match === "boolean" &&
1158
+ isInteger(value.mismatchingPixels, 0, 1 << 20) &&
1159
+ isInteger(value.comparedPixels, 1, 1 << 20) &&
1160
+ (value.mismatchingPixels as number) <= (value.comparedPixels as number) &&
1161
+ typeof value.mismatchRatio === "number" &&
1162
+ value.mismatchRatio >= 0 &&
1163
+ value.mismatchRatio <= 1 &&
1164
+ (bounds === undefined || (isObject(bounds) && (["x", "y", "width", "height"] as const).every((field) => isInteger(bounds[field], 0, 4096)))) &&
1165
+ (value.diffPng === undefined || (typeof value.diffPng === "string" && /^[A-Za-z0-9+/]+={0,2}$/.test(value.diffPng)))
1166
+ );
1167
+ },
1168
+ screenBaselines: (value) => Array.isArray(value.baselines) && value.baselines.every((name) => typeof name === "string"),
1169
+ screenDiscard: (value) => value.removed === true,
1170
+ snapshot: (value) =>
1171
+ (value.saved === true && typeof value.name === "string") ||
1172
+ (value.restored === true && isOneOf(RUN_STATES, value.state)) ||
1173
+ (Array.isArray(value.snapshots) && value.snapshots.every((name) => typeof name === "string")) ||
1174
+ value.removed === true,
1175
+ snapshotFile: (value) => (value.exported === true && isInteger(value.bytes, 1, 0xffff_ffff)) || (value.restored === true && isOneOf(RUN_STATES, value.state)),
1176
+ cpuHistory: (value) =>
1177
+ Array.isArray(value.entries) &&
1178
+ value.entries.every(
1179
+ (entry) =>
1180
+ isObject(entry) &&
1181
+ isInteger(entry.address, 0, 0xffff) &&
1182
+ isHexData(entry.bytes) &&
1183
+ typeof entry.text === "string" &&
1184
+ (["a", "x", "y", "sp"] as const).every((name) => isInteger(entry[name], 0, 0xff)) &&
1185
+ (entry.rasterLine === undefined || isInteger(entry.rasterLine, 0, 511)) &&
1186
+ (entry.rasterCycle === undefined || isInteger(entry.rasterCycle, 0, 127)),
1187
+ ),
1188
+ trace: (value) => isInteger(value.hits, 0, MAX_TRACE_HITS) && isInteger(value.frames, 0, MAX_TRACE_FRAMES) && isOneOf(TRACE_ENDS, value.end) && isInteger(value.pc, 0, 0xffff),
1189
+ backtrace: (value) =>
1190
+ Array.isArray(value.frames) &&
1191
+ value.frames.every((frame) => isObject(frame) && isInteger(frame.address, 0, 0xffff) && (frame.returnAddress === undefined || isInteger(frame.returnAddress, 0, 0xffff))),
1192
+ timing: (value) => ("cycles" in value ? typeof value.cycles === "string" && /^\d+$/.test(value.cycles) : value.started === true),
1193
+ profile: (value) =>
1194
+ "cleared" in value
1195
+ ? value.cleared === true
1196
+ : Array.isArray(value.entries) &&
1349
1197
  value.entries.every(
1350
1198
  (entry) =>
1351
1199
  isObject(entry) &&
@@ -1355,71 +1203,59 @@ export function validateViceResult<O extends ViceOperation>(op: O, value: unknow
1355
1203
  typeof entry.selfCycles === "string" &&
1356
1204
  /^\d+$/.test(entry.selfCycles) &&
1357
1205
  typeof entry.percent === "number",
1358
- );
1359
- if (!ok) throw new ProtocolError("profile result is malformed");
1360
- break;
1361
- }
1362
- case "memmap": {
1363
- const isRange = (range: unknown) =>
1364
- isObject(range) &&
1365
- isInteger(range.start, 0, 0xffff) &&
1366
- isInteger(range.end, 0, 0xffff) &&
1367
- isInteger(range.flags, 1, 0xff);
1368
- const ok = Array.isArray(value.ranges)
1369
- ? value.ranges.every(isRange) && (value.next === undefined || isInteger(value.next, 0, 0xffff))
1370
- : value.cleared === true;
1371
- if (!ok) throw new ProtocolError("memmap result is malformed");
1372
- break;
1373
- }
1374
- case "observe": {
1375
- // Each present section must have the shape of the single-purpose result.
1376
- if (value.registers !== undefined) validateViceResult("registersGet", value.registers);
1377
- if (value.memory !== undefined) {
1378
- const ok = Array.isArray(value.memory) && value.memory.every((range) => isObject(range) && isInteger(range.address, 0, 0xffff) && isHexData(range.data));
1379
- if (!ok) throw new ProtocolError("observe memory is malformed");
1206
+ ),
1207
+ memmap: (value) =>
1208
+ Array.isArray(value.ranges)
1209
+ ? value.ranges.every((range) => isObject(range) && isInteger(range.start, 0, 0xffff) && isInteger(range.end, 0, 0xffff) && isInteger(range.flags, 1, 0xff)) &&
1210
+ (value.next === undefined || isInteger(value.next, 0, 0xffff))
1211
+ : value.cleared === true,
1212
+ observe: (value) => {
1213
+ // Each present section must have the shape of the single-purpose result.
1214
+ if (value.registers !== undefined) validateViceResult("registersGet", value.registers);
1215
+ if (value.memory !== undefined) {
1216
+ const ok = Array.isArray(value.memory) && value.memory.every((range) => isObject(range) && isMemoryRead(range));
1217
+ if (!ok) throw new ProtocolError("observe memory is malformed");
1218
+ }
1219
+ if (value.vicii !== undefined) validateViceResult("vicii", value.vicii);
1220
+ if (value.sprites !== undefined) validateViceResult("sprites", { sprites: value.sprites });
1221
+ if (value.cia !== undefined) validateViceResult("cia", { chips: value.cia });
1222
+ if (value.sid !== undefined) validateViceResult("sid", value.sid);
1223
+ if (value.timing !== undefined) {
1224
+ const timing = value.timing;
1225
+ if (!isObject(timing) || !isInteger(timing.rasterLine, 0, 511) || !isInteger(timing.rasterCycle, 0, 127)) {
1226
+ throw new ProtocolError("observe timing is malformed");
1380
1227
  }
1381
- if (value.vicii !== undefined) validateViceResult("vicii", value.vicii);
1382
- if (value.sprites !== undefined) validateViceResult("sprites", { sprites: value.sprites });
1383
- if (value.cia !== undefined) validateViceResult("cia", { chips: value.cia });
1384
- if (value.sid !== undefined) validateViceResult("sid", value.sid);
1385
- if (value.timing !== undefined) {
1386
- const timing = value.timing;
1387
- if (!isObject(timing) || !isInteger(timing.rasterLine, 0, 511) || !isInteger(timing.rasterCycle, 0, 127)) {
1388
- throw new ProtocolError("observe timing is malformed");
1389
- }
1390
- }
1391
- if (value.screen !== undefined) validateViceResult("screenCapture", value.screen);
1392
- break;
1393
- }
1394
- case "vicii": {
1395
- const ok =
1396
- isInteger(value.rasterLine, 0, 511) &&
1397
- isOneOf(VICII_MODES, value.mode) &&
1398
- isInteger(value.screenAddress, 0, 0xffff) &&
1399
- isInteger(value.graphicsAddress, 0, 0xffff) &&
1400
- isInteger(value.scrollX, 0, 7) &&
1401
- isInteger(value.scrollY, 0, 7) &&
1402
- isInteger(value.borderColor, 0, 15) &&
1403
- Array.isArray(value.backgroundColors) &&
1404
- value.backgroundColors.length === 4 &&
1405
- value.backgroundColors.every((color) => isInteger(color, 0, 15));
1406
- if (!ok) throw new ProtocolError("vicii result is malformed");
1407
- break;
1408
1228
  }
1409
- case "sprites": {
1410
- const isSprite = (sprite: unknown) =>
1229
+ if (value.screen !== undefined) validateViceResult("screenCapture", value.screen);
1230
+ return true;
1231
+ },
1232
+ vicii: (value) =>
1233
+ isInteger(value.rasterLine, 0, 511) &&
1234
+ isOneOf(VICII_MODES, value.mode) &&
1235
+ isInteger(value.screenAddress, 0, 0xffff) &&
1236
+ isInteger(value.graphicsAddress, 0, 0xffff) &&
1237
+ isInteger(value.scrollX, 0, 7) &&
1238
+ isInteger(value.scrollY, 0, 7) &&
1239
+ isInteger(value.borderColor, 0, 15) &&
1240
+ Array.isArray(value.backgroundColors) &&
1241
+ value.backgroundColors.length === 4 &&
1242
+ value.backgroundColors.every((color) => isInteger(color, 0, 15)),
1243
+ sprites: (value) =>
1244
+ Array.isArray(value.sprites) &&
1245
+ value.sprites.every(
1246
+ (sprite) =>
1411
1247
  isObject(sprite) &&
1412
1248
  isInteger(sprite.index, 0, 7) &&
1413
1249
  isInteger(sprite.x, 0, 511) &&
1414
1250
  isInteger(sprite.y, 0, 255) &&
1415
1251
  isInteger(sprite.color, 0, 15) &&
1416
1252
  isInteger(sprite.dataAddress, 0, 0xffff) &&
1417
- (["enabled", "multicolor", "expandX", "expandY", "behindBackground"] as const).every((flag) => typeof sprite[flag] === "boolean");
1418
- if (!Array.isArray(value.sprites) || !value.sprites.every(isSprite)) throw new ProtocolError("sprites result is malformed");
1419
- break;
1420
- }
1421
- case "cia": {
1422
- const isCia = (chip: unknown) =>
1253
+ (["enabled", "multicolor", "expandX", "expandY", "behindBackground"] as const).every((flag) => typeof sprite[flag] === "boolean"),
1254
+ ),
1255
+ cia: (value) =>
1256
+ Array.isArray(value.chips) &&
1257
+ value.chips.every(
1258
+ (chip) =>
1423
1259
  isObject(chip) &&
1424
1260
  (chip.id === 1 || chip.id === 2) &&
1425
1261
  (["portA", "portB", "ddrA", "ddrB", "controlA", "controlB", "interruptStatus"] as const).every((name) => isInteger(chip[name], 0, 0xff)) &&
@@ -1429,66 +1265,45 @@ export function validateViceResult<O extends ViceOperation>(op: O, value: unknow
1429
1265
  isInteger(chip.tod.hours, 0, 23) &&
1430
1266
  isInteger(chip.tod.minutes, 0, 99) &&
1431
1267
  isInteger(chip.tod.seconds, 0, 99) &&
1432
- isInteger(chip.tod.tenths, 0, 15);
1433
- if (!Array.isArray(value.chips) || !value.chips.every(isCia)) throw new ProtocolError("cia result is malformed");
1434
- break;
1435
- }
1436
- case "sid": {
1437
- const voices = value.voices;
1438
- const ok =
1439
- Array.isArray(voices) &&
1440
- voices.length === 3 &&
1441
- voices.every(
1442
- (voice) =>
1443
- isObject(voice) &&
1444
- isInteger(voice.index, 1, 3) &&
1445
- isInteger(voice.frequency, 0, 0xffff) &&
1446
- isInteger(voice.pulseWidth, 0, 0x0fff) &&
1447
- (["control", "attackDecay", "sustainRelease"] as const).every((name) => isInteger(voice[name], 0, 0xff)),
1448
- ) &&
1449
- isObject(value.filter) &&
1450
- isInteger(value.filter.cutoff, 0, 0x7ff) &&
1451
- isInteger(value.filter.resonance, 0, 15) &&
1452
- isInteger(value.filter.routing, 0, 15) &&
1453
- isInteger(value.filter.mode, 0, 15) &&
1454
- isInteger(value.volume, 0, 15);
1455
- if (!ok) throw new ProtocolError("sid result is malformed");
1456
- break;
1457
- }
1458
- case "screenCapture": {
1459
- const ok =
1460
- isInteger(value.width, 1, 1024) &&
1461
- isInteger(value.height, 1, 1024) &&
1462
- typeof value.png === "string" &&
1463
- /^[A-Za-z0-9+/]+={0,2}$/.test(value.png) &&
1464
- (value.baseline === undefined || (typeof value.baseline === "string" && TRANSIENT_NAME.test(value.baseline)));
1465
- if (!ok) throw new ProtocolError("screenCapture result is malformed");
1466
- break;
1467
- }
1468
- case "joystick": {
1469
- const ok =
1470
- (value.port === 1 || value.port === 2) &&
1471
- isOneOf(JOYSTICK_DIRECTIONS, value.direction) &&
1472
- typeof value.fire === "boolean";
1473
- if (!ok) throw new ProtocolError("joystick result is malformed");
1474
- break;
1475
- }
1476
- case "warp": {
1477
- if (typeof value.enabled !== "boolean") throw new ProtocolError("warp result is malformed");
1478
- break;
1479
- }
1480
- case "window": {
1481
- const ok =
1482
- typeof value.window === "boolean" &&
1483
- isOneOf(RUN_STATES, value.state) &&
1484
- Array.isArray(value.notCarried) &&
1485
- value.notCarried.every((entry) => typeof entry === "string");
1486
- if (!ok) throw new ProtocolError("window result is malformed");
1487
- break;
1488
- }
1489
- default:
1490
- throw new ProtocolError(`unknown operation: ${String(op)}`);
1491
- }
1268
+ isInteger(chip.tod.tenths, 0, 15),
1269
+ ),
1270
+ sid: (value) => {
1271
+ const voices = value.voices;
1272
+ return (
1273
+ Array.isArray(voices) &&
1274
+ voices.length === 3 &&
1275
+ voices.every(
1276
+ (voice) =>
1277
+ isObject(voice) &&
1278
+ isInteger(voice.index, 1, 3) &&
1279
+ isInteger(voice.frequency, 0, 0xffff) &&
1280
+ isInteger(voice.pulseWidth, 0, 0x0fff) &&
1281
+ (["control", "attackDecay", "sustainRelease"] as const).every((name) => isInteger(voice[name], 0, 0xff)),
1282
+ ) &&
1283
+ isObject(value.filter) &&
1284
+ isInteger(value.filter.cutoff, 0, 0x7ff) &&
1285
+ isInteger(value.filter.resonance, 0, 15) &&
1286
+ isInteger(value.filter.routing, 0, 15) &&
1287
+ isInteger(value.filter.mode, 0, 15) &&
1288
+ isInteger(value.volume, 0, 15)
1289
+ );
1290
+ },
1291
+ screenCapture: (value) =>
1292
+ isInteger(value.width, 1, 1024) &&
1293
+ isInteger(value.height, 1, 1024) &&
1294
+ typeof value.png === "string" &&
1295
+ /^[A-Za-z0-9+/]+={0,2}$/.test(value.png) &&
1296
+ (value.baseline === undefined || (typeof value.baseline === "string" && TRANSIENT_NAME.test(value.baseline))),
1297
+ joystick: (value) => (value.port === 1 || value.port === 2) && isOneOf(JOYSTICK_DIRECTIONS, value.direction) && typeof value.fire === "boolean",
1298
+ warp: (value) => typeof value.enabled === "boolean",
1299
+ window: (value) => typeof value.window === "boolean" && isOneOf(RUN_STATES, value.state) && Array.isArray(value.notCarried) && value.notCarried.every((entry) => typeof entry === "string"),
1300
+ };
1301
+
1302
+ /** Validates a reply result on the client. Throws ProtocolError on a malformed result. */
1303
+ export function validateViceResult<O extends ViceOperation>(op: O, value: unknown): ViceOperations[O]["result"] {
1304
+ if (!Object.hasOwn(RESULTS, op)) throw new ProtocolError(`unknown operation: ${String(op)}`);
1305
+ if (!isObject(value)) throw new ProtocolError(`${op} result is not an object`);
1306
+ if (!RESULTS[op](value)) throw new ProtocolError(`${op} result is malformed`);
1492
1307
  return value as unknown as ViceOperations[O]["result"];
1493
1308
  }
1494
1309