c64-ready 0.11.0 → 1.0.1

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 (39) hide show
  1. package/README.md +10 -2
  2. package/dist/assets/CHANGELOG-B12WeYEc.js +116 -0
  3. package/dist/assets/index-fUdEY2R7.js +610 -0
  4. package/dist/index.html +1 -1
  5. package/dist/tools/anykey-c64.prg +0 -0
  6. package/dist/tools/tools.json +6 -0
  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/src/emulator/c64-emulator.ts +34 -2
  27. package/src/emulator/crt-info.ts +35 -23
  28. package/src/emulator/input.ts +5 -2
  29. package/src/headless/c64-headless.ts +5 -0
  30. package/src/headless/c64-key-map.mjs +1 -1
  31. package/src/headless/headless-cli.mjs +410 -232
  32. package/src/headless/input-server.mjs +463 -198
  33. package/src/headless/webrtc-server.mjs +235 -70
  34. package/src/main.ts +37 -3
  35. package/src/player/c64-player.ts +121 -37
  36. package/src/player/styles/ui-controller.css +114 -21
  37. package/src/player/ui-controller.ts +176 -8
  38. package/dist/assets/CHANGELOG-CPMeNyjb.js +0 -261
  39. package/dist/assets/index-CeQto0sd.js +0 -499
@@ -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'));
@@ -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;
@@ -204,6 +193,24 @@ export class C64Player {
204
193
  }
205
194
  }
206
195
 
196
+ async loadTool(url: string, type?: GameLoadOptions['type']): Promise<void> {
197
+ if (!this.emulator) throw new Error('Emulator not initialised — call start() first');
198
+ const onProgress = this.options.onProgress;
199
+ const resolvedType = type ?? inferLoadTypeFromFilename(url) ?? 'prg';
200
+
201
+ onProgress?.(10, 'PREPARING TOOL...');
202
+ try {
203
+ this.emulator.removeCartridge();
204
+ } catch {
205
+ // ignore if no cartridge is mounted
206
+ }
207
+ this.emulator.reset();
208
+ this.emulator.start();
209
+ await waitMs(120);
210
+
211
+ await this.loadGame(url, resolvedType, onProgress);
212
+ }
213
+
207
214
  // Expose cartridge / reset controls for UI
208
215
  detachCartridge(): void {
209
216
  if (!this.emulator) return;
@@ -229,6 +236,22 @@ export class C64Player {
229
236
  }
230
237
  }
231
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
+
232
255
  /**
233
256
  * Change which joystick port keyboard events map to (1 or 2).
234
257
  * Delegates to the InputHandler created during start().
@@ -248,9 +271,14 @@ export class C64Player {
248
271
  this.inputHandler?.setInputMode(mode);
249
272
  }
250
273
 
274
+ setCrtPreloadChecksDisabled(disabled: boolean): void {
275
+ this.disableCrtPreloadChecks = disabled;
276
+ this.emulator?.setCrtPreloadChecksEnabled(!disabled);
277
+ }
278
+
251
279
  private async autoRunPrgIfRunning(): Promise<void> {
252
280
  if (!this.emulator || !this.emulator.isRunning()) return;
253
- await waitMs(120);
281
+ await waitMs(220);
254
282
  await this.typeCommand('run\n');
255
283
  }
256
284
 
@@ -311,11 +339,67 @@ export class C64Player {
311
339
  );
312
340
  throw err;
313
341
  }
342
+
343
+ private attachInputAndRenderer(emulator: C64Emulator, renderer: CanvasRenderer): void {
344
+ this.inputHandler?.detach();
345
+ this.inputHandler = new InputHandler(emulator);
346
+ this.inputHandler.attach();
347
+ renderer.attachTo(emulator);
348
+ }
349
+
350
+ private handleCrtPreloadChecks(
351
+ data: Uint8Array,
352
+ source: string,
353
+ onProgress?: ProgressCallback,
354
+ ): void {
355
+ if (!this.disableCrtPreloadChecks) {
356
+ validateCrtSupportOrThrow(data, source, onProgress);
357
+ return;
358
+ }
359
+
360
+ const info = parseCrtInfo(data, source);
361
+ if (!info) return;
362
+
363
+ console.log(info.line);
364
+ const unsupportedReason = getUnsupportedCrtReason(info);
365
+ if (!unsupportedReason) return;
366
+
367
+ onProgress?.(90, 'UNSAFE CRT LOAD');
368
+ const message =
369
+ 'Preload checks are disabled. This cartridge is normally flagged as incompatible and may hang the emulator.';
370
+ console.warn(`${unsupportedReason} Proceeding because preload checks are disabled.`);
371
+ window.dispatchEvent(
372
+ new CustomEvent('c64-load-info', {
373
+ detail: {
374
+ mode: 'warning',
375
+ source,
376
+ message,
377
+ },
378
+ }),
379
+ );
380
+ }
314
381
  }
315
382
 
316
- // CRT format validator — delegates to parseCrtInfo so magic-check logic lives in one place.
317
- function isValidCRT(data: Uint8Array): boolean {
318
- return parseCrtInfo(data) !== null;
383
+ function validateCrtSupportOrThrow(
384
+ data: Uint8Array,
385
+ source: string,
386
+ onProgress?: ProgressCallback,
387
+ ): void {
388
+ const info = parseCrtInfo(data, source);
389
+ if (!info) {
390
+ onProgress?.(0, 'INVALID CRT');
391
+ const err = new Error('Invalid CRT file format');
392
+ console.error(err);
393
+ throw err;
394
+ }
395
+ console.log(info.line);
396
+ const unsupportedReason = getUnsupportedCrtReason(info);
397
+ if (unsupportedReason) {
398
+ onProgress?.(0, 'UNSUPPORTED CRT');
399
+ const err = new Error(unsupportedReason);
400
+ console.error(err);
401
+ throw err;
402
+ }
319
403
  }
320
404
 
321
405
  function formatLoadProgressLabel(type: GameLoadOptions['type']): string {
@@ -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 {
@@ -91,6 +93,21 @@
91
93
  list-style: none;
92
94
  font-size: 13px;
93
95
  }
96
+ .c64-help-special {
97
+ margin: 0 0 12px;
98
+ padding: 0;
99
+ list-style: none;
100
+ font-size: 12px;
101
+ }
102
+ .c64-help-special li {
103
+ display: flex;
104
+ justify-content: space-between;
105
+ padding: 3px 0;
106
+ border-bottom: 1px solid #2a2a3e;
107
+ }
108
+ .c64-help-special li:last-child {
109
+ border-bottom: none;
110
+ }
94
111
  .c64-help-controls li {
95
112
  display: flex;
96
113
  justify-content: space-between;
@@ -140,7 +157,10 @@
140
157
  align-items: center;
141
158
  justify-content: center;
142
159
  z-index: 1010;
143
- 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;
144
164
  animation: c64-unmute-pulse 2s ease-in-out infinite;
145
165
  }
146
166
  .c64-unmute-btn:hover {
@@ -152,8 +172,13 @@
152
172
  pointer-events: none;
153
173
  }
154
174
  @keyframes c64-unmute-pulse {
155
- 0%, 100% { transform: scale(1); }
156
- 50% { transform: scale(1.05); }
175
+ 0%,
176
+ 100% {
177
+ transform: scale(1);
178
+ }
179
+ 50% {
180
+ transform: scale(1.05);
181
+ }
157
182
  }
158
183
  .c64-audio-section {
159
184
  margin-top: 16px;
@@ -184,8 +209,12 @@
184
209
  justify-content: center;
185
210
  flex-shrink: 0;
186
211
  }
187
- .c64-mute-btn:hover { border-color: #7b71d5; }
188
- .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
+ }
189
218
  .c64-volume-slider {
190
219
  flex: 1;
191
220
  -webkit-appearance: none;
@@ -240,7 +269,7 @@
240
269
  .c64-menu-overlay {
241
270
  position: fixed;
242
271
  inset: 0;
243
- background: rgba(0,0,0,0.5);
272
+ background: rgba(0, 0, 0, 0.5);
244
273
  display: flex;
245
274
  align-items: flex-start;
246
275
  justify-content: flex-start;
@@ -250,7 +279,10 @@
250
279
  opacity: 0;
251
280
  transition: opacity 0.2s ease;
252
281
  }
253
- .c64-menu-overlay.visible { opacity: 1; pointer-events: auto; }
282
+ .c64-menu-overlay.visible {
283
+ opacity: 1;
284
+ pointer-events: auto;
285
+ }
254
286
  .c64-menu-panel {
255
287
  background: #0f0f1a;
256
288
  border: 2px solid #333;
@@ -260,8 +292,18 @@
260
292
  color: #ddd;
261
293
  font-family: monospace;
262
294
  }
263
- .c64-menu-header { display:flex; align-items:center; justify-content:space-between; }
264
- .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
+ }
265
307
  .c64-settings-tabs {
266
308
  display: flex;
267
309
  gap: 8px;
@@ -305,6 +347,15 @@
305
347
  padding: 4px 8px;
306
348
  font-family: monospace;
307
349
  }
350
+ .c64-tool-select {
351
+ flex: 1;
352
+ min-width: 180px;
353
+ }
354
+ .c64-tools-section {
355
+ margin-top: 12px;
356
+ padding-top: 10px;
357
+ border-top: 1px solid #2a2a3e;
358
+ }
308
359
  .c64-radio-row {
309
360
  margin-top: 8px;
310
361
  display: flex;
@@ -327,22 +378,49 @@
327
378
  margin-top: 12px;
328
379
  text-align: center;
329
380
  color: #aaa;
330
- background: linear-gradient(180deg, rgba(255,255,255,0.01), transparent);
381
+ background: linear-gradient(180deg, rgba(255, 255, 255, 0.01), transparent);
331
382
  }
332
383
  .c64-dragarea button {
333
384
  margin-left: 6px;
334
385
  }
335
- .c64-dragarea.dragover { border-color: #7b71d5; color: #fff; }
336
- .c64-cart-preview { display:flex; gap:12px; align-items:center; margin-top:12px; }
337
- .c64-cart-icon { width:56px; height:40px; background:#222; border:1px solid #333; display:flex; align-items:center; justify-content:center; border-radius:4px; }
338
- .c64-cart-filename { font-size:13px; color:#ccc; word-break:break-all; }
339
- .c64-cart-type { font-size: 11px; color: #888; margin-top: 2px; }
340
- .c64-file-input { display:none; }
386
+ .c64-dragarea.dragover {
387
+ border-color: #7b71d5;
388
+ color: #fff;
389
+ }
390
+ .c64-cart-preview {
391
+ display: flex;
392
+ gap: 12px;
393
+ align-items: center;
394
+ margin-top: 12px;
395
+ }
396
+ .c64-cart-icon {
397
+ width: 56px;
398
+ height: 40px;
399
+ background: #222;
400
+ border: 1px solid #333;
401
+ display: flex;
402
+ align-items: center;
403
+ justify-content: center;
404
+ border-radius: 4px;
405
+ }
406
+ .c64-cart-filename {
407
+ font-size: 13px;
408
+ color: #ccc;
409
+ word-break: break-all;
410
+ }
411
+ .c64-cart-type {
412
+ font-size: 11px;
413
+ color: #888;
414
+ margin-top: 2px;
415
+ }
416
+ .c64-file-input {
417
+ display: none;
418
+ }
341
419
  .c64-menu-actions {
342
- margin-top:12px;
343
- display:flex;
420
+ margin-top: 12px;
421
+ display: flex;
344
422
  flex-wrap: wrap;
345
- gap:8px;
423
+ gap: 8px;
346
424
  }
347
425
  .c64-system-actions {
348
426
  margin-top: 4px;
@@ -350,4 +428,19 @@
350
428
  gap: 8px;
351
429
  flex-wrap: wrap;
352
430
  }
353
- .c64-btn { background:#222; border:1px solid #444; color:#ddd; padding:6px 10px; border-radius:4px; cursor:pointer; }
431
+ .c64-checkbox-row {
432
+ margin-top: 10px;
433
+ display: inline-flex;
434
+ gap: 8px;
435
+ align-items: center;
436
+ color: #ccc;
437
+ font-size: 13px;
438
+ }
439
+ .c64-btn {
440
+ background: #222;
441
+ border: 1px solid #444;
442
+ color: #ddd;
443
+ padding: 6px 10px;
444
+ border-radius: 4px;
445
+ cursor: pointer;
446
+ }