c64-ready 3.3.1 → 3.4.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.
package/README.md CHANGED
@@ -137,12 +137,20 @@ and native snapshots (`.c64`, `.snapshot`, `.s64`).
137
137
 
138
138
  Note: VICE `.vsf` snapshots are currently disabled in the browser runtime.
139
139
 
140
- ### Keyboard limitations (current WASM build)
140
+ ### RESTORE key
141
141
 
142
- - The C64 `RESTORE` key is currently not functional in this build.
143
- - We verified browser key mapping for `Page Up` is received, but the underlying
144
- WASM core does not appear to trigger restore behavior from key input.
145
- - Treat this as a runtime limitation of the shipped binary for now.
142
+ `Page Up` is the C64 `RESTORE` key. On real hardware RESTORE is not part of the
143
+ keyboard matrix: it fires a one-shot pulse into the CPU's NMI line, which CIA2 shares.
144
+ The original lvllvl core emulates that line but doesn't export it, so `public/c64.wasm`
145
+ is patched by `tools/patch-wasm-exports.mjs` to export it as `c64_nmiLine`. This adds an
146
+ export entry and changes no code.
147
+
148
+ - `RUN/STOP` + `RESTORE` gives the usual warm reset (default colours, `READY.`).
149
+ - Programs that hook the NMI vector (`$0318`/`$FFFA`) receive it, even with IRQs disabled.
150
+ - Holding RESTORE fires once. It does nothing while an unacknowledged CIA2 NMI holds the
151
+ line, which is how some games disable it on real hardware.
152
+ - API: `player.restore()` / `emulator.restore()`. In the agent CLI, use `press=restore`
153
+ (for example `--at 100:hold=run_stop --at 103:press=restore`).
146
154
 
147
155
  ### Environment variables
148
156
 
@@ -169,6 +177,20 @@ the full annotated list.
169
177
  | `WS_PORT` | `9001` | WebSocket input server port inside the container |
170
178
  | `WS_HOST_PORT` | `9001` | Host-side port mapping for the input WebSocket |
171
179
 
180
+ ### Host controls in local dev
181
+
182
+ Taking host controls in the WebRTC player page needs `C64_ADMIN_TOKEN`. With no token
183
+ set, nobody can take host. For a local run, `--no-host-auth` drops the token check, so the
184
+ page shows a single "Take host controls" button:
185
+
186
+ ```zsh
187
+ node bin/headless.mjs --wasm public/c64.wasm --no-game \
188
+ --webrtc --webrtc-port 9002 --input --ws-port 9001 --fps 50 --no-host-auth
189
+ ```
190
+
191
+ This is a CLI flag only, with no env var, so `docker/.env` can't turn it on. Never use it on
192
+ a server other people can reach. `c64-admin` still requires the token.
193
+
172
194
  ### Admin CLI
173
195
 
174
196
  Use `c64-admin` to inspect active players/spectators and run admin actions over the input WebSocket:
package/bin/headless.mjs CHANGED
@@ -63,6 +63,8 @@ Options:
63
63
  --webrtc-max-bitrate-kbps <n> VP8 SDP x-google-max-bitrate hint (default: 600)
64
64
  --webrtc-output-fps <n> Max WebRTC video send FPS cap (default: 40)
65
65
  --admin-token <token> Shared token for c64-admin and browser host controls
66
+ --no-host-auth Local dev: any browser can take host controls without the
67
+ admin token. Never use on a server reachable by others.
66
68
  --input Start WebSocket input server for remote control
67
69
  --ws-port <n> WebSocket server port (default: 9001)
68
70
  --verbose Print per-frame diagnostics to stderr
package/package.json CHANGED
@@ -14,7 +14,7 @@
14
14
  "agent"
15
15
  ],
16
16
  "license": "MIT",
17
- "version": "3.3.1",
17
+ "version": "3.4.0",
18
18
  "type": "module",
19
19
  "engines": {
20
20
  "node": ">=24.14"
package/public/c64.wasm CHANGED
Binary file
@@ -15,6 +15,7 @@ import { C64WASM } from './c64-wasm';
15
15
  import { getUnsupportedCrtReason, parseCrtInfo } from './crt-info';
16
16
  import type { C64Config, FrameBuffer, AudioBuffer, GameLoadOptions, InputEvent } from '../types';
17
17
  import type { JoystickPort, JoystickInput } from './constants';
18
+ import { C64_KEY } from './c64-keys';
18
19
 
19
20
  const FRAME_WIDTH = 384;
20
21
  const FRAME_HEIGHT = 272;
@@ -27,6 +28,7 @@ export class C64Emulator {
27
28
  private running: boolean = false;
28
29
  private frameCount: number = 0;
29
30
  private crtPreloadChecksEnabled: boolean = true;
31
+ private restoreHeld: boolean = false;
30
32
 
31
33
  /** Called every tick with the latest video frame */
32
34
  onFrame?: (frame: FrameBuffer) => void;
@@ -218,7 +220,10 @@ export class C64Emulator {
218
220
  if (!x) return;
219
221
 
220
222
  if (event.type === 'key' && event.key !== undefined) {
221
- x.keyboard_keyPressed(Number(event.key));
223
+ const key = Number(event.key);
224
+ // A one-shot press event with no matching release: RESTORE is a single pulse.
225
+ if (key === C64_KEY.RESTORE) this.restore();
226
+ else x.keyboard_keyPressed(key);
222
227
  } else if (event.type === 'joystick') {
223
228
  const port = (event.joystickPort ?? 1) - 1; // callers use 1/2, WASM uses 0/1
224
229
  const dirMap: Record<string, number> = { up: 1, down: 2, left: 4, right: 8 };
@@ -228,12 +233,38 @@ export class C64Emulator {
228
233
  }
229
234
 
230
235
  keyDown(keyCode: number): void {
236
+ if (keyCode === C64_KEY.RESTORE) {
237
+ // Holding RESTORE (or OS key auto-repeat) must not fire repeated NMIs.
238
+ if (!this.restoreHeld) this.restore();
239
+ this.restoreHeld = true;
240
+ return;
241
+ }
231
242
  this.wasm.exports?.keyboard_keyPressed(keyCode);
232
243
  }
233
244
  keyUp(keyCode: number): void {
245
+ if (keyCode === C64_KEY.RESTORE) {
246
+ this.restoreHeld = false;
247
+ return;
248
+ }
234
249
  this.wasm.exports?.keyboard_keyReleased(keyCode);
235
250
  }
236
251
 
252
+ /**
253
+ * Press RESTORE. It isn't part of the keyboard matrix: on a real C64 it fires a
254
+ * one-shot pulse into the CPU's NMI line, which CIA2 shares. The NMI is edge
255
+ * triggered, so it does nothing while CIA2 holds the line (an unacknowledged
256
+ * CIA2 NMI), exactly as on hardware. Hold RUN/STOP for the classic warm reset.
257
+ *
258
+ * @returns false if the loaded c64.wasm lacks the `c64_nmiLine` export.
259
+ */
260
+ restore(): boolean {
261
+ const x = this.wasm.exports;
262
+ if (!x?.c64_nmiLine) return false;
263
+ x.c64_nmiLine(1);
264
+ x.c64_nmiLine(0);
265
+ return true;
266
+ }
267
+
237
268
  /**
238
269
  * Push (press) a joystick direction or fire button.
239
270
  *
@@ -16,7 +16,7 @@ import { readFileSync } from 'node:fs';
16
16
  import path from 'node:path';
17
17
  import { spawn } from 'node:child_process';
18
18
  import { C64WASM } from './c64-wasm.mjs';
19
- import { C64_KEY } from './c64-key-map.mjs';
19
+ import { C64_KEY, pressC64Key, releaseC64Key } from './c64-key-map.mjs';
20
20
  import { encodePng } from './png.mjs';
21
21
 
22
22
  export const FRAME_WIDTH = 384;
@@ -80,7 +80,7 @@ Output:
80
80
  Addresses are hex ($c000, 0xc000, c000), #49152 for decimal, or a label with --symbols.
81
81
  Quote "$…" in shells.
82
82
  Key names: A-Z, 0-9, SPACE, RETURN, F1 F3 F5 F7, RUN_STOP, SHIFT_LEFT, CTRL, COMMODORE,
83
- CURSOR_UP_DOWN, CURSOR_LEFT_RIGHT, CLEAR_HOME, INS_DEL, … (see C64_KEY).
83
+ CURSOR_UP_DOWN, CURSOR_LEFT_RIGHT, CLEAR_HOME, INS_DEL, RESTORE (NMI pulse), … (see C64_KEY).
84
84
 
85
85
  Exit codes: 0 ok · 1 error · 3 --until-text not reached.
86
86
 
@@ -700,17 +700,17 @@ export async function runAgent(options = {}) {
700
700
  for (const act of schedule.get(frame) ?? []) {
701
701
  if (act.kind === 'type') typeQueue.push(...act.bytes);
702
702
  else if (act.kind === 'press' || act.kind === 'hold') {
703
- act.keys.forEach((k) => x.keyboard_keyPressed(k));
703
+ act.keys.forEach((k) => pressC64Key(x, k));
704
704
  if (act.kind === 'press') {
705
705
  const at = frame + DEFAULT_TAP_FRAMES;
706
706
  releases.set(at, [...(releases.get(at) ?? []), ...act.keys]);
707
707
  }
708
- } else if (act.kind === 'release') act.keys.forEach((k) => x.keyboard_keyReleased(k));
708
+ } else if (act.kind === 'release') act.keys.forEach((k) => releaseC64Key(x, k));
709
709
  else if (act.kind === 'joy') setJoy(act.port, act.bits);
710
710
  else if (act.kind === 'poke') act.values.forEach((v, i) => x.c64_cpuWrite(act.addr + i, v));
711
711
  else if (act.kind === 'screenshot') await saveShot(act.path);
712
712
  }
713
- for (const k of releases.get(frame) ?? []) x.keyboard_keyReleased(k);
713
+ for (const k of releases.get(frame) ?? []) releaseC64Key(x, k);
714
714
  releases.delete(frame);
715
715
 
716
716
  // Feed the KERNAL keyboard buffer whenever it has drained.
@@ -81,6 +81,42 @@ export const C64_KEY = {
81
81
  RESTORE: 64,
82
82
  };
83
83
 
84
+ /**
85
+ * RESTORE is not in the keyboard matrix: on real hardware it fires a 556 one-shot
86
+ * into the CPU's NMI line (shared with CIA2). The core ignores key index 64, so it
87
+ * is routed to the `c64_nmiLine` export (added by tools/patch-wasm-exports.mjs)
88
+ * as a single pulse on the press edge. Holding or auto-repeating it does not
89
+ * fire again, just like the hardware.
90
+ */
91
+ const restoreHeld = new WeakMap();
92
+
93
+ /** Pulse the NMI line once, as the RESTORE key does. Returns false if unsupported. */
94
+ export function pulseRestore(exports) {
95
+ if (typeof exports?.c64_nmiLine !== 'function') return false;
96
+ exports.c64_nmiLine(1);
97
+ exports.c64_nmiLine(0);
98
+ return true;
99
+ }
100
+
101
+ /** Press a C64 key by matrix index; RESTORE becomes an NMI pulse. */
102
+ export function pressC64Key(exports, key) {
103
+ if (key === C64_KEY.RESTORE) {
104
+ if (!restoreHeld.get(exports)) pulseRestore(exports);
105
+ restoreHeld.set(exports, true);
106
+ return;
107
+ }
108
+ exports.keyboard_keyPressed(key);
109
+ }
110
+
111
+ /** Release a C64 key by matrix index. */
112
+ export function releaseC64Key(exports, key) {
113
+ if (key === C64_KEY.RESTORE) {
114
+ restoreHeld.set(exports, false);
115
+ return;
116
+ }
117
+ exports.keyboard_keyReleased(key);
118
+ }
119
+
84
120
  /**
85
121
  * Translate a browser key event into C64 matrix actions.
86
122
  *
@@ -5,7 +5,7 @@ import { fileURLToPath } from 'url';
5
5
  import { execSync } from 'child_process';
6
6
  import { timingSafeEqual } from 'crypto';
7
7
  import FFmpegRunner from './ffmpeg-runner.mjs';
8
- import { domKeyToC64Actions } from './c64-key-map.mjs';
8
+ import { domKeyToC64Actions, pressC64Key, releaseC64Key } from './c64-key-map.mjs';
9
9
 
10
10
  const nowMonoMs = () => {
11
11
  if (
@@ -211,16 +211,16 @@ async function typeCommandText(exports, text, opts = {}) {
211
211
  const key = ch === '\n' ? 'Enter' : ch;
212
212
  const down = domKeyToC64Actions(key, false, 'keydown');
213
213
  for (const act of down) {
214
- if (act.action === 'press') exports.keyboard_keyPressed(act.key);
215
- else exports.keyboard_keyReleased(act.key);
214
+ if (act.action === 'press') pressC64Key(exports, act.key);
215
+ else releaseC64Key(exports, act.key);
216
216
  }
217
217
  if (typeof exports.debugger_update === 'function') exports.debugger_update(12);
218
218
  if (keyDelayMs > 0) await sleepMs(keyDelayMs);
219
219
 
220
220
  const up = domKeyToC64Actions(key, false, 'keyup');
221
221
  for (const act of up) {
222
- if (act.action === 'press') exports.keyboard_keyPressed(act.key);
223
- else exports.keyboard_keyReleased(act.key);
222
+ if (act.action === 'press') pressC64Key(exports, act.key);
223
+ else releaseC64Key(exports, act.key);
224
224
  }
225
225
  if (typeof exports.debugger_update === 'function') exports.debugger_update(12);
226
226
  if (keyDelayMs > 0) await sleepMs(keyDelayMs);
@@ -338,6 +338,9 @@ export async function runHeadless(options = {}) {
338
338
  let webrtcMaxBitrateKbps = 600;
339
339
  let webrtcOutputFps = 40;
340
340
  let adminToken = process.env.C64_ADMIN_TOKEN ?? '';
341
+ // Local dev only: let any browser claim host without the admin token.
342
+ // Deliberately CLI-only (no env var), so docker/.env can't switch it on by accident.
343
+ let hostAuth = true;
341
344
  let logFile = false;
342
345
  let logRetainDays = 7;
343
346
  let hostTimeoutMs = Number(process.env.HOST_TIMEOUT_MS ?? '');
@@ -373,6 +376,7 @@ export async function runHeadless(options = {}) {
373
376
  else if (a === '--webrtc-max-bitrate-kbps') webrtcMaxBitrateKbps = Number(argv[++i]);
374
377
  else if (a === '--webrtc-output-fps') webrtcOutputFps = Number(argv[++i]);
375
378
  else if (a === '--admin-token') adminToken = argv[++i] ?? '';
379
+ else if (a === '--no-host-auth') hostAuth = false;
376
380
  else if (a === '--attract-mode') attractModeEnabled = true;
377
381
  else if (a === '--attract-mode-autostart') attractModeAutostart = true;
378
382
  else if (a === '--attract-mode-base-url') attractModeBaseUrl = argv[++i] ?? '';
@@ -754,8 +758,8 @@ export async function runHeadless(options = {}) {
754
758
  const evType = event.action === 'up' ? 'keyup' : 'keydown';
755
759
  const c64acts = domKeyToC64Actions(domKey, shiftKey, evType);
756
760
  for (const act of c64acts) {
757
- if (act.action === 'press') exports.keyboard_keyPressed(act.key);
758
- else exports.keyboard_keyReleased(act.key);
761
+ if (act.action === 'press') pressC64Key(exports, act.key);
762
+ else releaseC64Key(exports, act.key);
759
763
  }
760
764
  if (verbose && c64acts.length > 0) {
761
765
  const role = event._role ?? 'unknown';
@@ -766,13 +770,18 @@ export async function runHeadless(options = {}) {
766
770
  }
767
771
  }
768
772
 
773
+ if (!hostAuth) {
774
+ console.error(
775
+ '[input] WARNING: --no-host-auth — any browser that can reach this server can take host controls. Local dev only.',
776
+ );
777
+ }
769
778
  inputServer = createInputServer({
770
779
  port: wsPort,
771
780
  verbose,
772
781
  logEvents,
773
782
  hostTimeoutMs,
774
783
  validateKickToken,
775
- hostAuthRequired: true,
784
+ hostAuthRequired: hostAuth,
776
785
  validateHostToken: validateAdminToken,
777
786
  validateAdminToken,
778
787
  initialCartFilename: gamePath ? path.basename(gamePath) : null,
@@ -1989,7 +1989,7 @@ function buildBrowserHtml(
1989
1989
  z: { fire: true }, Z: { fire: true }, x: { fire: true }, X: { fire: true },
1990
1990
  };
1991
1991
  const MIXED_JOY_KEYS = new Set(['ArrowUp','ArrowDown','ArrowLeft','ArrowRight','z','Z','x','X']);
1992
- const PREVENT_DEFAULTS = new Set(['ArrowUp','ArrowDown','ArrowLeft','ArrowRight',' ','Tab']);
1992
+ const PREVENT_DEFAULTS = new Set(['ArrowUp','ArrowDown','ArrowLeft','ArrowRight',' ','Tab','PageUp']);
1993
1993
  document.addEventListener('keydown', (e) => {
1994
1994
  if (!isHost) return;
1995
1995
  if (PREVENT_DEFAULTS.has(e.key)) e.preventDefault();
@@ -267,6 +267,11 @@ export class C64Player {
267
267
  }
268
268
  }
269
269
 
270
+ /** Press the C64 RESTORE key (an NMI pulse). Returns false if the core lacks support. */
271
+ restore(): boolean {
272
+ return this.emulator?.restore() ?? false;
273
+ }
274
+
270
275
  async reboot(): Promise<void> {
271
276
  if (!this.emulator) return;
272
277
  try {
@@ -68,7 +68,7 @@ const FULL_KEYMAP: ReadonlyArray<readonly [string, readonly KeymapEntry[]]> = [
68
68
  'C64 keyboard',
69
69
  [
70
70
  ['Run/Stop', ['Esc']],
71
- ['Restore', [], 'not supported yet'],
71
+ ['Restore', ['Page Up']],
72
72
  ['Return', ['Enter']],
73
73
  ['Shift', ['Shift']],
74
74
  ['Inst/Del', ['⌫', 'Del']],
@@ -37,6 +37,12 @@ export interface WASMExports {
37
37
  keyboard_keyPressed(key: number): void;
38
38
  keyboard_keyReleased(key: number): void;
39
39
 
40
+ /**
41
+ * NMI line (shared with CIA2), reference-counted; NMI fires on the 0→1 edge.
42
+ * Not in the original lvllvl build: added by tools/patch-wasm-exports.mjs.
43
+ */
44
+ c64_nmiLine?(assert: number): void;
45
+
40
46
  // Joystick
41
47
  c64_joystick_push(port: number, dir: number): void;
42
48
  c64_joystick_release(port: number, dir: number): void;