c64-ready 0.10.0 → 1.0.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.
@@ -1,4 +1,5 @@
1
1
  import { C64Emulator } from '../emulator/c64-emulator';
2
+ import { domKeyToC64Actions } from '../emulator/input';
2
3
  import { parseCrtInfo } from '../emulator/crt-info';
3
4
  import type { GameLoadOptions } from '../types';
4
5
  import type { JoystickPort } from '../emulator/constants';
@@ -6,6 +7,7 @@ import type { InputMode } from '../emulator/input';
6
7
  import type CanvasRenderer from './canvas-renderer';
7
8
  import { AudioEngine } from './audio-engine';
8
9
  import InputHandler from './input-handler';
10
+ import { getLoadTypeLabel, inferLoadTypeFromFilename } from './load-formats';
9
11
 
10
12
  export type ProgressCallback = (percent: number, label: string) => void;
11
13
 
@@ -38,7 +40,9 @@ export class C64Player {
38
40
  this.inputHandler.attach();
39
41
  renderer.attachTo(this.emulator);
40
42
 
41
- await this.loadGame(gameUrl, gameType, onProgress);
43
+ if (gameUrl && gameUrl !== 'null') {
44
+ await this.loadGame(gameUrl, gameType, onProgress);
45
+ }
42
46
 
43
47
  this.emulator.start();
44
48
 
@@ -118,7 +122,7 @@ export class C64Player {
118
122
  data = new Uint8Array(await response.arrayBuffer());
119
123
  }
120
124
 
121
- onProgress?.(90, 'INSERTING CARTRIDGE...');
125
+ onProgress?.(90, `INSERTING ${formatLoadProgressLabel(type)}...`);
122
126
  // Basic validation for cartridge files — some malformed .crt files can
123
127
  // silently fail inside the WASM loader; catch common format problems here
124
128
  if (type === 'crt' && !isValidCRT(data)) {
@@ -132,8 +136,13 @@ export class C64Player {
132
136
  const info = parseCrtInfo(data, filename);
133
137
  if (info) console.log(info.line);
134
138
  }
139
+ this.assertSnapshotFormatSupported(data, type, url);
140
+ this.emitSnapshotLoadInfo(type, url);
135
141
  try {
136
142
  this.emulator.loadGame({ type, data });
143
+ if (type === 'prg') {
144
+ await this.autoRunPrgIfRunning();
145
+ }
137
146
  onProgress?.(100, 'READY!');
138
147
  // Notify UI that load succeeded
139
148
  window.dispatchEvent(new CustomEvent('c64-load-success', { detail: { url, type } }));
@@ -149,36 +158,42 @@ export class C64Player {
149
158
  }
150
159
 
151
160
  // Load a game from a File/Blob provided by the user (drag & drop / file input)
152
- async loadFile(file: File, type: GameLoadOptions['type'] = 'crt'): Promise<void> {
161
+ async loadFile(file: File, type?: GameLoadOptions['type']): Promise<void> {
153
162
  if (!this.emulator) throw new Error('Emulator not initialised — call start() first');
154
163
  const onProgress = this.options.onProgress;
164
+ const resolvedType = type ?? inferLoadTypeFromFilename(file.name) ?? 'crt';
155
165
  try {
156
166
  onProgress?.(20, `READING FILE ${file.name}...`);
157
167
  const ab = await file.arrayBuffer();
158
168
  const data = new Uint8Array(ab);
159
- onProgress?.(90, 'INSERTING CARTRIDGE...');
160
- if (type === 'crt' && !isValidCRT(data)) {
169
+ onProgress?.(90, `INSERTING ${formatLoadProgressLabel(resolvedType)}...`);
170
+ if (resolvedType === 'crt' && !isValidCRT(data)) {
161
171
  onProgress?.(0, 'INVALID CRT');
162
172
  const err = new Error('Invalid CRT file format');
163
173
  console.error(err);
164
174
  throw err;
165
175
  }
166
- if (type === 'crt') {
176
+ if (resolvedType === 'crt') {
167
177
  const info = parseCrtInfo(data, file.name);
168
178
  if (info) console.log(info.line);
169
179
  }
180
+ this.assertSnapshotFormatSupported(data, resolvedType, file.name);
181
+ this.emitSnapshotLoadInfo(resolvedType, file.name);
170
182
  try {
171
- this.emulator.loadGame({ type, data });
183
+ this.emulator.loadGame({ type: resolvedType, data });
184
+ if (resolvedType === 'prg') {
185
+ await this.autoRunPrgIfRunning();
186
+ }
172
187
  onProgress?.(100, 'READY!');
173
188
  window.dispatchEvent(
174
- new CustomEvent('c64-close-dialog', { detail: { file: file.name, type } }),
189
+ new CustomEvent('c64-close-dialog', { detail: { file: file.name, type: resolvedType } }),
175
190
  );
176
191
  } catch (err) {
177
192
  onProgress?.(0, 'FAILED');
178
193
  console.error('Failed to insert cartridge from file:', err);
179
194
  window.dispatchEvent(
180
195
  new CustomEvent('c64-load-error', {
181
- detail: { error: String(err), file: file.name, type },
196
+ detail: { error: String(err), file: file.name, type: resolvedType },
182
197
  }),
183
198
  );
184
199
  throw err;
@@ -189,6 +204,24 @@ export class C64Player {
189
204
  }
190
205
  }
191
206
 
207
+ async loadTool(url: string, type?: GameLoadOptions['type']): Promise<void> {
208
+ if (!this.emulator) throw new Error('Emulator not initialised — call start() first');
209
+ const onProgress = this.options.onProgress;
210
+ const resolvedType = type ?? inferLoadTypeFromFilename(url) ?? 'prg';
211
+
212
+ onProgress?.(10, 'PREPARING TOOL...');
213
+ try {
214
+ this.emulator.removeCartridge();
215
+ } catch {
216
+ // ignore if no cartridge is mounted
217
+ }
218
+ this.emulator.reset();
219
+ this.emulator.start();
220
+ await waitMs(120);
221
+
222
+ await this.loadGame(url, resolvedType, onProgress);
223
+ }
224
+
192
225
  // Expose cartridge / reset controls for UI
193
226
  detachCartridge(): void {
194
227
  if (!this.emulator) return;
@@ -232,9 +265,93 @@ export class C64Player {
232
265
  setInputMode(mode: InputMode): void {
233
266
  this.inputHandler?.setInputMode(mode);
234
267
  }
268
+
269
+ private async autoRunPrgIfRunning(): Promise<void> {
270
+ if (!this.emulator || !this.emulator.isRunning()) return;
271
+ await waitMs(220);
272
+ await this.typeCommand('run\n');
273
+ }
274
+
275
+ private async typeCommand(command: string): Promise<void> {
276
+ if (!this.emulator) return;
277
+
278
+ for (const char of command) {
279
+ const domKey = char === '\n' ? 'enter' : char;
280
+
281
+ const down = domKeyToC64Actions(domKey, false, 'keydown');
282
+ for (const act of down) {
283
+ if (act.action === 'press') this.emulator.keyDown(act.key);
284
+ else this.emulator.keyUp(act.key);
285
+ }
286
+
287
+ await waitMs(22);
288
+
289
+ const up = domKeyToC64Actions(domKey, false, 'keyup');
290
+ for (const act of up) {
291
+ if (act.action === 'press') this.emulator.keyDown(act.key);
292
+ else this.emulator.keyUp(act.key);
293
+ }
294
+
295
+ await waitMs(22);
296
+ }
297
+ }
298
+
299
+ private emitSnapshotLoadInfo(type: GameLoadOptions['type'], source: string): void {
300
+ if (type !== 'snapshot') return;
301
+
302
+ const mode = 'native';
303
+ const message = `Loading native LVL snapshot (${source})`;
304
+
305
+ console.info(`[snapshot] mode=${mode} source=${source}`);
306
+ window.dispatchEvent(new CustomEvent('c64-load-info', { detail: { mode, source, message } }));
307
+ }
308
+
309
+ private assertSnapshotFormatSupported(
310
+ data: Uint8Array,
311
+ type: GameLoadOptions['type'],
312
+ source: string,
313
+ ): void {
314
+ if (type !== 'snapshot') return;
315
+ if (!hasViceSnapshotMagic(data)) return;
316
+
317
+ const err = new Error(
318
+ `Unsupported snapshot format for ${source}. VICE .vsf snapshots are disabled for now; use LVLLVL/native snapshots (.c64, .snapshot, .s64).`,
319
+ );
320
+ console.error(err);
321
+ window.dispatchEvent(
322
+ new CustomEvent('c64-load-info', {
323
+ detail: {
324
+ mode: 'warning',
325
+ source,
326
+ message: 'Snapshot must be LVLLVL/native format (.c64, .snapshot, .s64).',
327
+ },
328
+ }),
329
+ );
330
+ throw err;
331
+ }
235
332
  }
236
333
 
237
334
  // CRT format validator — delegates to parseCrtInfo so magic-check logic lives in one place.
238
335
  function isValidCRT(data: Uint8Array): boolean {
239
336
  return parseCrtInfo(data) !== null;
240
337
  }
338
+
339
+ function formatLoadProgressLabel(type: GameLoadOptions['type']): string {
340
+ const label = getLoadTypeLabel(type);
341
+ const suffixStart = label.indexOf(' (');
342
+ const plain = suffixStart > 0 ? label.slice(0, suffixStart) : label;
343
+ return plain.toUpperCase();
344
+ }
345
+
346
+ function waitMs(ms: number): Promise<void> {
347
+ return new Promise((resolve) => setTimeout(resolve, ms));
348
+ }
349
+
350
+ function hasViceSnapshotMagic(data: Uint8Array): boolean {
351
+ const magic = 'VICE Snapshot File\x1a';
352
+ if (data.length < magic.length) return false;
353
+ for (let i = 0; i < magic.length; i++) {
354
+ if (data[i] !== magic.charCodeAt(i)) return false;
355
+ }
356
+ return true;
357
+ }
@@ -0,0 +1,58 @@
1
+ import type { GameLoadOptions } from '../types';
2
+
3
+ export type LoadType = GameLoadOptions['type'];
4
+ export type LoadTypeSelection = LoadType | 'auto';
5
+
6
+ export interface LoadFormatOption {
7
+ type: LoadType;
8
+ label: string;
9
+ extensions: string[];
10
+ }
11
+
12
+ export const LOAD_FORMAT_OPTIONS: LoadFormatOption[] = [
13
+ { type: 'crt', label: 'Cartridge (.crt)', extensions: ['.crt'] },
14
+ { type: 'prg', label: 'Program (.prg)', extensions: ['.prg'] },
15
+ { type: 'd64', label: 'Disk image (.d64)', extensions: ['.d64'] },
16
+ {
17
+ type: 'snapshot',
18
+ label: 'Snapshot (.snapshot, .c64, .s64)',
19
+ extensions: ['.snapshot', '.c64', '.s64'],
20
+ },
21
+ ];
22
+
23
+ const LOAD_TYPE_SET = new Set<LoadType>(LOAD_FORMAT_OPTIONS.map((f) => f.type));
24
+
25
+ export function isSupportedLoadType(value: unknown): value is LoadType {
26
+ return typeof value === 'string' && LOAD_TYPE_SET.has(value as LoadType);
27
+ }
28
+
29
+ export function inferLoadTypeFromFilename(filename: string): LoadType | null {
30
+ const lower = filename.toLowerCase();
31
+ for (const format of LOAD_FORMAT_OPTIONS) {
32
+ if (format.extensions.some((ext) => lower.endsWith(ext))) {
33
+ return format.type;
34
+ }
35
+ }
36
+ return null;
37
+ }
38
+
39
+ export function resolveLoadTypeSelection(
40
+ selection: LoadTypeSelection,
41
+ filename: string,
42
+ fallback: LoadType = 'crt',
43
+ ): LoadType {
44
+ if (selection !== 'auto') return selection;
45
+ return inferLoadTypeFromFilename(filename) ?? fallback;
46
+ }
47
+
48
+ export function getAcceptForLoadTypeSelection(selection: LoadTypeSelection): string {
49
+ const exts =
50
+ selection === 'auto'
51
+ ? LOAD_FORMAT_OPTIONS.flatMap((f) => f.extensions)
52
+ : LOAD_FORMAT_OPTIONS.find((f) => f.type === selection)?.extensions ?? [];
53
+ return exts.join(',');
54
+ }
55
+
56
+ export function getLoadTypeLabel(type: LoadType): string {
57
+ return LOAD_FORMAT_OPTIONS.find((f) => f.type === type)?.label ?? type.toUpperCase();
58
+ }
@@ -91,6 +91,21 @@
91
91
  list-style: none;
92
92
  font-size: 13px;
93
93
  }
94
+ .c64-help-special {
95
+ margin: 0 0 12px;
96
+ padding: 0;
97
+ list-style: none;
98
+ font-size: 12px;
99
+ }
100
+ .c64-help-special li {
101
+ display: flex;
102
+ justify-content: space-between;
103
+ padding: 3px 0;
104
+ border-bottom: 1px solid #2a2a3e;
105
+ }
106
+ .c64-help-special li:last-child {
107
+ border-bottom: none;
108
+ }
94
109
  .c64-help-controls li {
95
110
  display: flex;
96
111
  justify-content: space-between;
@@ -262,6 +277,73 @@
262
277
  }
263
278
  .c64-menu-header { display:flex; align-items:center; justify-content:space-between; }
264
279
  .c64-menu-close { background:none; border:none; color:#888; font-size:20px; cursor:pointer; }
280
+ .c64-settings-tabs {
281
+ display: flex;
282
+ gap: 8px;
283
+ flex-wrap: wrap;
284
+ margin-bottom: 12px;
285
+ }
286
+ .c64-settings-tab {
287
+ background: #171729;
288
+ border: 1px solid #353552;
289
+ color: #c5c5df;
290
+ padding: 6px 10px;
291
+ border-radius: 4px;
292
+ cursor: pointer;
293
+ font-size: 12px;
294
+ }
295
+ .c64-settings-tab.active {
296
+ border-color: #7b71d5;
297
+ color: #fff;
298
+ background: #25253d;
299
+ }
300
+ .c64-settings-section {
301
+ padding-top: 2px;
302
+ }
303
+ .c64-section-label {
304
+ display: block;
305
+ margin: 0 0 8px;
306
+ font-size: 13px;
307
+ color: #aaa;
308
+ }
309
+ .c64-form-row {
310
+ display: flex;
311
+ align-items: center;
312
+ gap: 10px;
313
+ margin-bottom: 10px;
314
+ }
315
+ .c64-select {
316
+ background: #171729;
317
+ color: #ddd;
318
+ border: 1px solid #444;
319
+ border-radius: 4px;
320
+ padding: 4px 8px;
321
+ font-family: monospace;
322
+ }
323
+ .c64-tool-select {
324
+ flex: 1;
325
+ min-width: 180px;
326
+ }
327
+ .c64-tools-section {
328
+ margin-top: 12px;
329
+ padding-top: 10px;
330
+ border-top: 1px solid #2a2a3e;
331
+ }
332
+ .c64-radio-row {
333
+ margin-top: 8px;
334
+ display: flex;
335
+ gap: 12px;
336
+ align-items: center;
337
+ }
338
+ .c64-radio-row-wrap {
339
+ flex-wrap: wrap;
340
+ }
341
+ .c64-section-hint {
342
+ margin-top: 6px;
343
+ font-size: 11px;
344
+ color: #888;
345
+ line-height: 1.4;
346
+ }
265
347
  .c64-dragarea {
266
348
  border: 2px dashed #2a2a3e;
267
349
  border-radius: 6px;
@@ -271,16 +353,25 @@
271
353
  color: #aaa;
272
354
  background: linear-gradient(180deg, rgba(255,255,255,0.01), transparent);
273
355
  }
356
+ .c64-dragarea button {
357
+ margin-left: 6px;
358
+ }
274
359
  .c64-dragarea.dragover { border-color: #7b71d5; color: #fff; }
275
360
  .c64-cart-preview { display:flex; gap:12px; align-items:center; margin-top:12px; }
276
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; }
277
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; }
278
364
  .c64-file-input { display:none; }
279
365
  .c64-menu-actions {
280
366
  margin-top:12px;
281
367
  display:flex;
282
- flex-direction: column;
368
+ flex-wrap: wrap;
283
369
  gap:8px;
284
370
  }
371
+ .c64-system-actions {
372
+ margin-top: 4px;
373
+ display: flex;
374
+ gap: 8px;
375
+ flex-wrap: wrap;
376
+ }
285
377
  .c64-btn { background:#222; border:1px solid #444; color:#ddd; padding:6px 10px; border-radius:4px; cursor:pointer; }
286
-