c64-ready 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/README.md +70 -62
  2. package/bin/headless.mjs +1 -1
  3. package/dist/assets/{CHANGELOG-CPlXKLCV.js → CHANGELOG-0a7A8Ekq.js} +113 -1
  4. package/dist/assets/index-BESofQoh.js +627 -0
  5. package/dist/games/cartridges/goat-beat.crt +0 -0
  6. package/dist/index.html +1 -1
  7. package/dist-ts/emulator/c64-emulator.d.ts +3 -0
  8. package/dist-ts/emulator/c64-emulator.d.ts.map +1 -1
  9. package/dist-ts/emulator/c64-emulator.js +36 -4
  10. package/dist-ts/emulator/c64-emulator.js.map +1 -1
  11. package/dist-ts/emulator/crt-info.d.ts +61 -0
  12. package/dist-ts/emulator/crt-info.d.ts.map +1 -0
  13. package/dist-ts/emulator/crt-info.js +190 -0
  14. package/dist-ts/emulator/crt-info.js.map +1 -0
  15. package/dist-ts/emulator/input.d.ts +96 -0
  16. package/dist-ts/emulator/input.d.ts.map +1 -1
  17. package/dist-ts/emulator/input.js +549 -4
  18. package/dist-ts/emulator/input.js.map +1 -1
  19. package/dist-ts/headless/c64-headless.d.ts +1 -0
  20. package/dist-ts/headless/c64-headless.d.ts.map +1 -1
  21. package/dist-ts/headless/c64-headless.js +5 -0
  22. package/dist-ts/headless/c64-headless.js.map +1 -1
  23. package/dist-ts/types/emulator.d.ts +2 -1
  24. package/dist-ts/types/emulator.d.ts.map +1 -1
  25. package/package.json +1 -1
  26. package/public/games/cartridges/goat-beat.crt +0 -0
  27. package/src/emulator/c64-emulator.ts +37 -7
  28. package/src/emulator/crt-info.ts +35 -23
  29. package/src/emulator/input.ts +239 -19
  30. package/src/headless/c64-headless.ts +5 -0
  31. package/src/headless/headless-cli.mjs +462 -249
  32. package/src/headless/input-server.mjs +649 -278
  33. package/src/headless/webrtc-server.mjs +218 -71
  34. package/src/main.ts +30 -4
  35. package/src/player/c64-player.ts +121 -40
  36. package/src/player/input-handler.ts +8 -0
  37. package/src/player/styles/ui-controller.css +103 -21
  38. package/src/player/ui-controller.ts +186 -23
  39. package/src/types/emulator.ts +1 -1
  40. package/dist/assets/index-Ckbi7gQo.js +0 -535
@@ -1,6 +1,6 @@
1
1
  import { C64Emulator } from '../emulator/c64-emulator';
2
2
  import { domKeyToC64Actions } from '../emulator/input';
3
- import { parseCrtInfo } from '../emulator/crt-info';
3
+ import { getUnsupportedCrtReason, parseCrtInfo } from '../emulator/crt-info';
4
4
  import type { GameLoadOptions } from '../types';
5
5
  import type { JoystickPort } from '../emulator/constants';
6
6
  import type { InputMode } from '../emulator/input';
@@ -22,6 +22,7 @@ export interface C64PlayerOptions {
22
22
  export class C64Player {
23
23
  private emulator: C64Emulator | null = null;
24
24
  private inputHandler: InputHandler | null = null;
25
+ private disableCrtPreloadChecks: boolean = false;
25
26
  readonly audio = new AudioEngine();
26
27
  private readonly options: Required<Pick<C64PlayerOptions, 'wasmUrl' | 'gameUrl' | 'gameType'>> &
27
28
  C64PlayerOptions;
@@ -35,10 +36,8 @@ export class C64Player {
35
36
 
36
37
  onProgress?.(10, 'INITIALISING WASM...');
37
38
  this.emulator = await C64Emulator.load(wasmUrl);
38
-
39
- this.inputHandler = new InputHandler(this.emulator);
40
- this.inputHandler.attach();
41
- renderer.attachTo(this.emulator);
39
+ this.emulator.setCrtPreloadChecksEnabled(!this.disableCrtPreloadChecks);
40
+ this.attachInputAndRenderer(this.emulator, renderer);
42
41
 
43
42
  if (gameUrl && gameUrl !== 'null') {
44
43
  await this.loadGame(gameUrl, gameType, onProgress);
@@ -60,7 +59,6 @@ export class C64Player {
60
59
  */
61
60
  private initAudio(): void {
62
61
  if (!this.emulator) return;
63
- const emulator = this.emulator;
64
62
 
65
63
  // Notify the UI whenever audio state changes
66
64
  this.audio.onStateChange = (state) => {
@@ -69,11 +67,12 @@ export class C64Player {
69
67
 
70
68
  // Fire-and-forget init
71
69
  this.audio.init().then((autoplayOk) => {
70
+ if (!this.emulator) return;
72
71
  // Tell the SID what sample rate we're using (matches AudioContext)
73
- emulator.setSampleRate(this.audio.sampleRate);
72
+ this.emulator.setSampleRate(this.audio.sampleRate);
74
73
 
75
74
  // Give the engine a reader that snapshots the SID circular buffer
76
- this.audio.setSidBufferReader(() => emulator.getSidBuffer());
75
+ this.audio.setSidBufferReader(() => this.emulator?.getSidBuffer() ?? null);
77
76
 
78
77
  if (!autoplayOk) {
79
78
  window.dispatchEvent(new CustomEvent('c64-audio-suspended'));
@@ -96,11 +95,11 @@ export class C64Player {
96
95
  }
97
96
 
98
97
  const contentLength = Number(response.headers.get('content-length') ?? 0);
99
- let data: Uint8Array;
98
+ let data: Uint8Array<ArrayBufferLike>;
100
99
 
101
100
  if (contentLength > 0 && response.body) {
102
101
  const reader = response.body.getReader();
103
- const chunks: Uint8Array[] = [];
102
+ const chunks: Uint8Array<ArrayBufferLike>[] = [];
104
103
  let loaded = 0;
105
104
 
106
105
  let result = await reader.read();
@@ -123,22 +122,13 @@ export class C64Player {
123
122
  }
124
123
 
125
124
  onProgress?.(90, `INSERTING ${formatLoadProgressLabel(type)}...`);
126
- // Basic validation for cartridge files — some malformed .crt files can
127
- // silently fail inside the WASM loader; catch common format problems here
128
- if (type === 'crt' && !isValidCRT(data)) {
129
- onProgress?.(0, 'INVALID CRT');
130
- const err = new Error('Invalid CRT file format');
131
- console.error(err);
132
- throw err;
133
- }
134
- if (type === 'crt') {
135
- const filename = url.split('/').pop() ?? url;
136
- const info = parseCrtInfo(data, filename);
137
- if (info) console.log(info.line);
138
- }
139
125
  this.assertSnapshotFormatSupported(data, type, url);
140
126
  this.emitSnapshotLoadInfo(type, url);
141
127
  try {
128
+ if (type === 'crt') {
129
+ const filename = url.split('/').pop() ?? url;
130
+ this.handleCrtPreloadChecks(data, filename, onProgress);
131
+ }
142
132
  this.emulator.loadGame({ type, data });
143
133
  if (type === 'prg') {
144
134
  await this.autoRunPrgIfRunning();
@@ -151,7 +141,9 @@ export class C64Player {
151
141
  console.error('Failed to insert cartridge or load game:', err);
152
142
  // Surface a global event so UI can react if needed
153
143
  window.dispatchEvent(
154
- new CustomEvent('c64-load-error', { detail: { error: String(err), url, type } }),
144
+ new CustomEvent('c64-load-error', {
145
+ detail: { error: err instanceof Error ? err.message : String(err), url, type },
146
+ }),
155
147
  );
156
148
  throw err;
157
149
  }
@@ -167,19 +159,12 @@ export class C64Player {
167
159
  const ab = await file.arrayBuffer();
168
160
  const data = new Uint8Array(ab);
169
161
  onProgress?.(90, `INSERTING ${formatLoadProgressLabel(resolvedType)}...`);
170
- if (resolvedType === 'crt' && !isValidCRT(data)) {
171
- onProgress?.(0, 'INVALID CRT');
172
- const err = new Error('Invalid CRT file format');
173
- console.error(err);
174
- throw err;
175
- }
176
- if (resolvedType === 'crt') {
177
- const info = parseCrtInfo(data, file.name);
178
- if (info) console.log(info.line);
179
- }
180
162
  this.assertSnapshotFormatSupported(data, resolvedType, file.name);
181
163
  this.emitSnapshotLoadInfo(resolvedType, file.name);
182
164
  try {
165
+ if (resolvedType === 'crt') {
166
+ this.handleCrtPreloadChecks(data, file.name, onProgress);
167
+ }
183
168
  this.emulator.loadGame({ type: resolvedType, data });
184
169
  if (resolvedType === 'prg') {
185
170
  await this.autoRunPrgIfRunning();
@@ -193,7 +178,11 @@ export class C64Player {
193
178
  console.error('Failed to insert cartridge from file:', err);
194
179
  window.dispatchEvent(
195
180
  new CustomEvent('c64-load-error', {
196
- detail: { error: String(err), file: file.name, type: resolvedType },
181
+ detail: {
182
+ error: err instanceof Error ? err.message : String(err),
183
+ file: file.name,
184
+ type: resolvedType,
185
+ },
197
186
  }),
198
187
  );
199
188
  throw err;
@@ -247,6 +236,22 @@ export class C64Player {
247
236
  }
248
237
  }
249
238
 
239
+ async reboot(): Promise<void> {
240
+ if (!this.emulator) return;
241
+ try {
242
+ await this.emulator.reboot();
243
+ this.attachInputAndRenderer(this.emulator, this.options.renderer);
244
+ this.emulator.start();
245
+ this.emulator.setSampleRate(this.audio.sampleRate);
246
+ this.audio.setSidBufferReader(() => this.emulator?.getSidBuffer() ?? null);
247
+ window.dispatchEvent(new CustomEvent('c64-reboot', { detail: {} }));
248
+ window.dispatchEvent(new CustomEvent('c64-close-menu'));
249
+ } catch (e) {
250
+ console.error('Failed to reboot emulator:', e);
251
+ throw e;
252
+ }
253
+ }
254
+
250
255
  /**
251
256
  * Change which joystick port keyboard events map to (1 or 2).
252
257
  * Delegates to the InputHandler created during start().
@@ -266,6 +271,26 @@ export class C64Player {
266
271
  this.inputHandler?.setInputMode(mode);
267
272
  }
268
273
 
274
+ setActiveGamepadIndex(index: number): void {
275
+ this.inputHandler?.setActiveGamepadIndex(index);
276
+ }
277
+
278
+ getActiveGamepadIndex(): number {
279
+ return this.inputHandler?.getActiveGamepadIndex() ?? -1;
280
+ }
281
+
282
+ getSnapshot(): Uint8Array {
283
+ if (!this.emulator) {
284
+ throw new Error('Emulator not initialised');
285
+ }
286
+ return this.emulator.getSnapshot();
287
+ }
288
+
289
+ setCrtPreloadChecksDisabled(disabled: boolean): void {
290
+ this.disableCrtPreloadChecks = disabled;
291
+ this.emulator?.setCrtPreloadChecksEnabled(!disabled);
292
+ }
293
+
269
294
  private async autoRunPrgIfRunning(): Promise<void> {
270
295
  if (!this.emulator || !this.emulator.isRunning()) return;
271
296
  await waitMs(220);
@@ -307,7 +332,7 @@ export class C64Player {
307
332
  }
308
333
 
309
334
  private assertSnapshotFormatSupported(
310
- data: Uint8Array,
335
+ data: Uint8Array<ArrayBufferLike>,
311
336
  type: GameLoadOptions['type'],
312
337
  source: string,
313
338
  ): void {
@@ -329,11 +354,67 @@ export class C64Player {
329
354
  );
330
355
  throw err;
331
356
  }
357
+
358
+ private attachInputAndRenderer(emulator: C64Emulator, renderer: CanvasRenderer): void {
359
+ this.inputHandler?.detach();
360
+ this.inputHandler = new InputHandler(emulator);
361
+ this.inputHandler.attach();
362
+ renderer.attachTo(emulator);
363
+ }
364
+
365
+ private handleCrtPreloadChecks(
366
+ data: Uint8Array<ArrayBufferLike>,
367
+ source: string,
368
+ onProgress?: ProgressCallback,
369
+ ): void {
370
+ if (!this.disableCrtPreloadChecks) {
371
+ validateCrtSupportOrThrow(data, source, onProgress);
372
+ return;
373
+ }
374
+
375
+ const info = parseCrtInfo(data, source);
376
+ if (!info) return;
377
+
378
+ console.log(info.line);
379
+ const unsupportedReason = getUnsupportedCrtReason(info);
380
+ if (!unsupportedReason) return;
381
+
382
+ onProgress?.(90, 'UNSAFE CRT LOAD');
383
+ const message =
384
+ 'Preload checks are disabled. This cartridge is normally flagged as incompatible and may hang the emulator.';
385
+ console.warn(`${unsupportedReason} Proceeding because preload checks are disabled.`);
386
+ window.dispatchEvent(
387
+ new CustomEvent('c64-load-info', {
388
+ detail: {
389
+ mode: 'warning',
390
+ source,
391
+ message,
392
+ },
393
+ }),
394
+ );
395
+ }
332
396
  }
333
397
 
334
- // CRT format validator — delegates to parseCrtInfo so magic-check logic lives in one place.
335
- function isValidCRT(data: Uint8Array): boolean {
336
- return parseCrtInfo(data) !== null;
398
+ function validateCrtSupportOrThrow(
399
+ data: Uint8Array<ArrayBufferLike>,
400
+ source: string,
401
+ onProgress?: ProgressCallback,
402
+ ): void {
403
+ const info = parseCrtInfo(data, source);
404
+ if (!info) {
405
+ onProgress?.(0, 'INVALID CRT');
406
+ const err = new Error('Invalid CRT file format');
407
+ console.error(err);
408
+ throw err;
409
+ }
410
+ console.log(info.line);
411
+ const unsupportedReason = getUnsupportedCrtReason(info);
412
+ if (unsupportedReason) {
413
+ onProgress?.(0, 'UNSUPPORTED CRT');
414
+ const err = new Error(unsupportedReason);
415
+ console.error(err);
416
+ throw err;
417
+ }
337
418
  }
338
419
 
339
420
  function formatLoadProgressLabel(type: GameLoadOptions['type']): string {
@@ -347,7 +428,7 @@ function waitMs(ms: number): Promise<void> {
347
428
  return new Promise((resolve) => setTimeout(resolve, ms));
348
429
  }
349
430
 
350
- function hasViceSnapshotMagic(data: Uint8Array): boolean {
431
+ function hasViceSnapshotMagic(data: Uint8Array<ArrayBufferLike>): boolean {
351
432
  const magic = 'VICE Snapshot File\x1a';
352
433
  if (data.length < magic.length) return false;
353
434
  for (let i = 0; i < magic.length; i++) {
@@ -40,6 +40,14 @@ export default class InputHandler {
40
40
  return this.emulatorInput.getInputMode();
41
41
  }
42
42
 
43
+ setActiveGamepadIndex(index: number): void {
44
+ this.emulatorInput.setActiveGamepadIndex(index);
45
+ }
46
+
47
+ getActiveGamepadIndex(): number {
48
+ return this.emulatorInput.getActiveGamepadIndex();
49
+ }
50
+
43
51
  private onCaptureKeyDown(e: KeyboardEvent): void {
44
52
  // Block joystick-mapped keys (standard + mixed mode) when overlays are
45
53
  // visible or the document is unfocused. Use the union of both key sets.
@@ -17,7 +17,9 @@
17
17
  display: flex;
18
18
  align-items: center;
19
19
  justify-content: center;
20
- transition: border-color 0.2s, color 0.2s;
20
+ transition:
21
+ border-color 0.2s,
22
+ color 0.2s;
21
23
  z-index: 1000;
22
24
  }
23
25
  .c64-help-btn:hover {
@@ -155,7 +157,10 @@
155
157
  align-items: center;
156
158
  justify-content: center;
157
159
  z-index: 1010;
158
- transition: border-color 0.2s, color 0.2s, opacity 0.3s;
160
+ transition:
161
+ border-color 0.2s,
162
+ color 0.2s,
163
+ opacity 0.3s;
159
164
  animation: c64-unmute-pulse 2s ease-in-out infinite;
160
165
  }
161
166
  .c64-unmute-btn:hover {
@@ -167,8 +172,13 @@
167
172
  pointer-events: none;
168
173
  }
169
174
  @keyframes c64-unmute-pulse {
170
- 0%, 100% { transform: scale(1); }
171
- 50% { transform: scale(1.05); }
175
+ 0%,
176
+ 100% {
177
+ transform: scale(1);
178
+ }
179
+ 50% {
180
+ transform: scale(1.05);
181
+ }
172
182
  }
173
183
  .c64-audio-section {
174
184
  margin-top: 16px;
@@ -199,8 +209,12 @@
199
209
  justify-content: center;
200
210
  flex-shrink: 0;
201
211
  }
202
- .c64-mute-btn:hover { border-color: #7b71d5; }
203
- .c64-mute-btn.muted { color: #f44; }
212
+ .c64-mute-btn:hover {
213
+ border-color: #7b71d5;
214
+ }
215
+ .c64-mute-btn.muted {
216
+ color: #f44;
217
+ }
204
218
  .c64-volume-slider {
205
219
  flex: 1;
206
220
  -webkit-appearance: none;
@@ -255,7 +269,7 @@
255
269
  .c64-menu-overlay {
256
270
  position: fixed;
257
271
  inset: 0;
258
- background: rgba(0,0,0,0.5);
272
+ background: rgba(0, 0, 0, 0.5);
259
273
  display: flex;
260
274
  align-items: flex-start;
261
275
  justify-content: flex-start;
@@ -265,7 +279,10 @@
265
279
  opacity: 0;
266
280
  transition: opacity 0.2s ease;
267
281
  }
268
- .c64-menu-overlay.visible { opacity: 1; pointer-events: auto; }
282
+ .c64-menu-overlay.visible {
283
+ opacity: 1;
284
+ pointer-events: auto;
285
+ }
269
286
  .c64-menu-panel {
270
287
  background: #0f0f1a;
271
288
  border: 2px solid #333;
@@ -275,8 +292,18 @@
275
292
  color: #ddd;
276
293
  font-family: monospace;
277
294
  }
278
- .c64-menu-header { display:flex; align-items:center; justify-content:space-between; }
279
- .c64-menu-close { background:none; border:none; color:#888; font-size:20px; cursor:pointer; }
295
+ .c64-menu-header {
296
+ display: flex;
297
+ align-items: center;
298
+ justify-content: space-between;
299
+ }
300
+ .c64-menu-close {
301
+ background: none;
302
+ border: none;
303
+ color: #888;
304
+ font-size: 20px;
305
+ cursor: pointer;
306
+ }
280
307
  .c64-settings-tabs {
281
308
  display: flex;
282
309
  gap: 8px;
@@ -331,6 +358,7 @@
331
358
  }
332
359
  .c64-radio-row {
333
360
  margin-top: 8px;
361
+ margin-bottom: 16px;
334
362
  display: flex;
335
363
  gap: 12px;
336
364
  align-items: center;
@@ -344,6 +372,18 @@
344
372
  color: #888;
345
373
  line-height: 1.4;
346
374
  }
375
+ .c64-gamepad-list {
376
+ margin-top: 8px;
377
+ margin-bottom: 8px;
378
+ display: flex;
379
+ flex-wrap: wrap;
380
+ gap: 8px;
381
+ }
382
+ .c64-gamepad-btn.active {
383
+ background: #7b71d5;
384
+ border-color: #948bff;
385
+ color: #fff;
386
+ }
347
387
  .c64-dragarea {
348
388
  border: 2px dashed #2a2a3e;
349
389
  border-radius: 6px;
@@ -351,22 +391,49 @@
351
391
  margin-top: 12px;
352
392
  text-align: center;
353
393
  color: #aaa;
354
- background: linear-gradient(180deg, rgba(255,255,255,0.01), transparent);
394
+ background: linear-gradient(180deg, rgba(255, 255, 255, 0.01), transparent);
355
395
  }
356
396
  .c64-dragarea button {
357
397
  margin-left: 6px;
358
398
  }
359
- .c64-dragarea.dragover { border-color: #7b71d5; color: #fff; }
360
- .c64-cart-preview { display:flex; gap:12px; align-items:center; margin-top:12px; }
361
- .c64-cart-icon { width:56px; height:40px; background:#222; border:1px solid #333; display:flex; align-items:center; justify-content:center; border-radius:4px; }
362
- .c64-cart-filename { font-size:13px; color:#ccc; word-break:break-all; }
363
- .c64-cart-type { font-size: 11px; color: #888; margin-top: 2px; }
364
- .c64-file-input { display:none; }
399
+ .c64-dragarea.dragover {
400
+ border-color: #7b71d5;
401
+ color: #fff;
402
+ }
403
+ .c64-cart-preview {
404
+ display: flex;
405
+ gap: 12px;
406
+ align-items: center;
407
+ margin-top: 12px;
408
+ }
409
+ .c64-cart-icon {
410
+ width: 56px;
411
+ height: 40px;
412
+ background: #222;
413
+ border: 1px solid #333;
414
+ display: flex;
415
+ align-items: center;
416
+ justify-content: center;
417
+ border-radius: 4px;
418
+ }
419
+ .c64-cart-filename {
420
+ font-size: 13px;
421
+ color: #ccc;
422
+ word-break: break-all;
423
+ }
424
+ .c64-cart-type {
425
+ font-size: 11px;
426
+ color: #888;
427
+ margin-top: 2px;
428
+ }
429
+ .c64-file-input {
430
+ display: none;
431
+ }
365
432
  .c64-menu-actions {
366
- margin-top:12px;
367
- display:flex;
433
+ margin-top: 12px;
434
+ display: flex;
368
435
  flex-wrap: wrap;
369
- gap:8px;
436
+ gap: 8px;
370
437
  }
371
438
  .c64-system-actions {
372
439
  margin-top: 4px;
@@ -374,4 +441,19 @@
374
441
  gap: 8px;
375
442
  flex-wrap: wrap;
376
443
  }
377
- .c64-btn { background:#222; border:1px solid #444; color:#ddd; padding:6px 10px; border-radius:4px; cursor:pointer; }
444
+ .c64-checkbox-row {
445
+ margin-top: 10px;
446
+ display: inline-flex;
447
+ gap: 8px;
448
+ align-items: center;
449
+ color: #ccc;
450
+ font-size: 13px;
451
+ }
452
+ .c64-btn {
453
+ background: #222;
454
+ border: 1px solid #444;
455
+ color: #ddd;
456
+ padding: 6px 10px;
457
+ border-radius: 4px;
458
+ cursor: pointer;
459
+ }