c64-ready 1.1.0 → 2.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.
- package/README.md +17 -12
- package/dist/assets/index-B3vw_7xz.js +169 -0
- package/dist/index.html +1 -1
- package/dist-ts/emulator/c64-emulator.d.ts +5 -2
- package/dist-ts/emulator/c64-emulator.d.ts.map +1 -1
- package/dist-ts/emulator/c64-emulator.js +24 -12
- package/dist-ts/emulator/c64-emulator.js.map +1 -1
- package/dist-ts/emulator/c64-wasm.d.ts +1 -1
- package/dist-ts/emulator/constants.d.ts +3 -1
- package/dist-ts/emulator/constants.d.ts.map +1 -1
- package/dist-ts/emulator/constants.js +2 -0
- package/dist-ts/emulator/constants.js.map +1 -1
- package/dist-ts/emulator/input.d.ts +26 -3
- package/dist-ts/emulator/input.d.ts.map +1 -1
- package/dist-ts/emulator/input.js +287 -10
- package/dist-ts/emulator/input.js.map +1 -1
- package/dist-ts/headless/audio-capture.d.ts +1 -1
- package/dist-ts/headless/c64-headless.d.ts +2 -2
- package/dist-ts/headless/c64-headless.js +4 -4
- package/dist-ts/headless/frame-capture.d.ts +1 -1
- package/dist-ts/headless/input-bridge.d.ts +1 -1
- package/dist-ts/player/audio-engine.d.ts +73 -0
- package/dist-ts/player/audio-engine.d.ts.map +1 -0
- package/dist-ts/{src/player → player}/audio-engine.js +7 -2
- package/dist-ts/player/audio-engine.js.map +1 -0
- package/dist-ts/player/c64-player.d.ts +69 -0
- package/dist-ts/player/c64-player.d.ts.map +1 -0
- package/dist-ts/player/c64-player.js +416 -0
- package/dist-ts/player/c64-player.js.map +1 -0
- package/dist-ts/player/canvas-renderer.d.ts +21 -0
- package/dist-ts/player/canvas-renderer.d.ts.map +1 -0
- package/dist-ts/{src/player → player}/canvas-renderer.js +55 -44
- package/dist-ts/player/canvas-renderer.js.map +1 -0
- package/dist-ts/player/index.d.ts +10 -0
- package/dist-ts/player/index.d.ts.map +1 -0
- package/dist-ts/player/index.js +6 -0
- package/dist-ts/player/index.js.map +1 -0
- package/dist-ts/player/input-handler.d.ts +21 -0
- package/dist-ts/player/input-handler.d.ts.map +1 -0
- package/dist-ts/player/input-handler.js +122 -0
- package/dist-ts/player/input-handler.js.map +1 -0
- package/dist-ts/player/load-formats.d.ts +15 -0
- package/dist-ts/player/load-formats.d.ts.map +1 -0
- package/dist-ts/player/load-formats.js +38 -0
- package/dist-ts/player/load-formats.js.map +1 -0
- package/dist-ts/player/ui-controller.d.ts +44 -0
- package/dist-ts/player/ui-controller.d.ts.map +1 -0
- package/dist-ts/player/ui-controller.js +844 -0
- package/dist-ts/player/ui-controller.js.map +1 -0
- package/dist-ts/types/emulator.d.ts +2 -2
- package/dist-ts/types/emulator.d.ts.map +1 -1
- package/package.json +31 -3
- package/src/emulator/c64-emulator.ts +15 -5
- package/src/emulator/constants.ts +5 -1
- package/src/emulator/input.ts +131 -39
- package/src/headless/webrtc-server.mjs +73 -8
- package/src/player/audio-engine.ts +13 -2
- package/src/player/c64-player.ts +128 -58
- package/src/player/canvas-renderer.ts +57 -5
- package/src/player/index.ts +7 -0
- package/src/player/ui-controller.ts +34 -14
- package/src/types/emulator.ts +1 -1
- package/dist/assets/CHANGELOG-0a7A8Ekq.js +0 -391
- package/dist/assets/index-BESofQoh.js +0 -627
- package/dist-ts/emulator/c64-emulator.test.js +0 -104
- package/dist-ts/emulator/c64-wasm.test.js +0 -65
- package/dist-ts/emulator/input.test.js +0 -53
- package/dist-ts/src/counter.js +0 -9
- package/dist-ts/src/emulator/c64-emulator.js +0 -308
- package/dist-ts/src/emulator/c64-emulator.test.js +0 -104
- package/dist-ts/src/emulator/c64-wasm.js +0 -149
- package/dist-ts/src/emulator/c64-wasm.test.js +0 -65
- package/dist-ts/src/emulator/constants.js +0 -9
- package/dist-ts/src/emulator/input.js +0 -62
- package/dist-ts/src/emulator/input.test.js +0 -53
- package/dist-ts/src/emulator/memory.js +0 -1
- package/dist-ts/src/headless/audio-capture.js +0 -23
- package/dist-ts/src/headless/c64-headless.js +0 -85
- package/dist-ts/src/headless/ffmpeg-runner.js +0 -33
- package/dist-ts/src/headless/frame-capture.js +0 -23
- package/dist-ts/src/headless/headless.cli.test.js +0 -46
- package/dist-ts/src/headless/input-bridge.js +0 -37
- package/dist-ts/src/main.js +0 -57
- package/dist-ts/src/player/audio-engine.test.js +0 -284
- package/dist-ts/src/player/c64-player.js +0 -105
- package/dist-ts/src/player/c64-player.test.js +0 -165
- package/dist-ts/src/player/canvas-renderer.test.js +0 -135
- package/dist-ts/src/player/input-handler.js +0 -13
- package/dist-ts/src/player/ui-controller.js +0 -575
- package/dist-ts/src/player/ui-controller.test.js +0 -67
- package/dist-ts/src/types/emulator.js +0 -1
- package/dist-ts/src/types/index.js +0 -4
- package/dist-ts/tools/run-wrapper.js +0 -36
- /package/dist/assets/{marked.esm-BOXuzyNF.js → marked.esm--kBm026b.js} +0 -0
- /package/dist/assets/{purify.es-y41lKIN9.js → purify.es-BcoGFDcJ.js} +0 -0
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
2
|
-
import { C64Emulator } from './c64-emulator';
|
|
3
|
-
import { C64WASM } from './c64-wasm';
|
|
4
|
-
const FRAME_BYTES = 384 * 272 * 4;
|
|
5
|
-
describe('C64Emulator', () => {
|
|
6
|
-
beforeEach(() => {
|
|
7
|
-
vi.restoreAllMocks();
|
|
8
|
-
});
|
|
9
|
-
function makeFakeWasm() {
|
|
10
|
-
const heapU8 = new Uint8Array(FRAME_BYTES + 64);
|
|
11
|
-
for (let i = 0; i < 16; i++) {
|
|
12
|
-
heapU8[i] = i + 1;
|
|
13
|
-
}
|
|
14
|
-
const heapF32 = new Float32Array(4096 + 32);
|
|
15
|
-
heapF32[0] = 0.25;
|
|
16
|
-
heapF32[1] = -0.5;
|
|
17
|
-
const exports = {
|
|
18
|
-
c64_init: vi.fn(),
|
|
19
|
-
c64_reset: vi.fn(),
|
|
20
|
-
debugger_update: vi.fn(() => 1),
|
|
21
|
-
c64_getPixelBuffer: vi.fn(() => 0),
|
|
22
|
-
sid_getAudioBuffer: vi.fn(() => 0),
|
|
23
|
-
sid_dumpBuffer: vi.fn(() => 2),
|
|
24
|
-
c64_loadPRG: vi.fn(),
|
|
25
|
-
c64_insertDisk: vi.fn(),
|
|
26
|
-
c64_loadCartridge: vi.fn(),
|
|
27
|
-
c64_loadSnapshot: vi.fn(),
|
|
28
|
-
c64_ramRead: vi.fn(() => 0xab),
|
|
29
|
-
c64_getSnapshotSize: vi.fn(() => 4),
|
|
30
|
-
malloc: vi.fn(() => 8),
|
|
31
|
-
c64_getSnapshot: vi.fn((ptr) => {
|
|
32
|
-
heapU8.set([9, 8, 7, 6], ptr);
|
|
33
|
-
}),
|
|
34
|
-
free: vi.fn(),
|
|
35
|
-
};
|
|
36
|
-
const wasm = {
|
|
37
|
-
exports,
|
|
38
|
-
heap: {
|
|
39
|
-
heapU8,
|
|
40
|
-
heapF32,
|
|
41
|
-
heapU32: new Uint32Array(heapU8.buffer),
|
|
42
|
-
},
|
|
43
|
-
allocAndWrite: vi.fn(() => 16),
|
|
44
|
-
free: vi.fn(),
|
|
45
|
-
};
|
|
46
|
-
return { wasm, exports, heapU8 };
|
|
47
|
-
}
|
|
48
|
-
it('loads and initializes through C64WASM.load', async () => {
|
|
49
|
-
const { wasm, exports } = makeFakeWasm();
|
|
50
|
-
vi.spyOn(C64WASM, 'load').mockResolvedValue(wasm);
|
|
51
|
-
const emulator = await C64Emulator.load('/custom.wasm');
|
|
52
|
-
expect(C64WASM.load).toHaveBeenCalledWith('/custom.wasm');
|
|
53
|
-
expect(exports.c64_init).toHaveBeenCalledOnce();
|
|
54
|
-
expect(emulator.ramRead(0)).toBe(0xab);
|
|
55
|
-
});
|
|
56
|
-
it('does not tick when paused', async () => {
|
|
57
|
-
const { wasm, exports } = makeFakeWasm();
|
|
58
|
-
vi.spyOn(C64WASM, 'load').mockResolvedValue(wasm);
|
|
59
|
-
const emulator = await C64Emulator.load();
|
|
60
|
-
emulator.tick(12);
|
|
61
|
-
expect(exports.debugger_update).not.toHaveBeenCalled();
|
|
62
|
-
expect(emulator.getFrameCount()).toBe(0);
|
|
63
|
-
});
|
|
64
|
-
it('emits frame callback while running', async () => {
|
|
65
|
-
const { wasm, exports } = makeFakeWasm();
|
|
66
|
-
vi.spyOn(C64WASM, 'load').mockResolvedValue(wasm);
|
|
67
|
-
const emulator = await C64Emulator.load();
|
|
68
|
-
const onFrame = vi.fn();
|
|
69
|
-
emulator.onFrame = onFrame;
|
|
70
|
-
emulator.start();
|
|
71
|
-
emulator.tick(10);
|
|
72
|
-
expect(exports.debugger_update).toHaveBeenCalledWith(10);
|
|
73
|
-
expect(emulator.getFrameCount()).toBe(1);
|
|
74
|
-
expect(onFrame).toHaveBeenCalledOnce();
|
|
75
|
-
expect(onFrame.mock.calls[0][0].width).toBe(384);
|
|
76
|
-
});
|
|
77
|
-
it('provides SID audio buffer via getSidBuffer', async () => {
|
|
78
|
-
const { wasm } = makeFakeWasm();
|
|
79
|
-
vi.spyOn(C64WASM, 'load').mockResolvedValue(wasm);
|
|
80
|
-
const emulator = await C64Emulator.load();
|
|
81
|
-
const buf = emulator.getSidBuffer();
|
|
82
|
-
expect(buf).toBeInstanceOf(Float32Array);
|
|
83
|
-
expect(buf.length).toBe(4096);
|
|
84
|
-
});
|
|
85
|
-
it('always frees allocated memory after loadGame', async () => {
|
|
86
|
-
const { wasm, exports } = makeFakeWasm();
|
|
87
|
-
exports.c64_loadPRG.mockImplementation(() => {
|
|
88
|
-
throw new Error('bad prg');
|
|
89
|
-
});
|
|
90
|
-
vi.spyOn(C64WASM, 'load').mockResolvedValue(wasm);
|
|
91
|
-
const emulator = await C64Emulator.load();
|
|
92
|
-
expect(() => emulator.loadGame({ type: 'prg', data: new Uint8Array([1, 2]) })).toThrow('bad prg');
|
|
93
|
-
expect(wasm.free).toHaveBeenCalledWith(16);
|
|
94
|
-
});
|
|
95
|
-
it('returns a copied framebuffer in getFrameBuffer', async () => {
|
|
96
|
-
const { wasm, heapU8 } = makeFakeWasm();
|
|
97
|
-
vi.spyOn(C64WASM, 'load').mockResolvedValue(wasm);
|
|
98
|
-
const emulator = await C64Emulator.load();
|
|
99
|
-
const frame = emulator.getFrameBuffer();
|
|
100
|
-
heapU8[0] = 250;
|
|
101
|
-
expect(frame.data[0]).toBe(1);
|
|
102
|
-
expect(frame.data.length).toBe(FRAME_BYTES);
|
|
103
|
-
});
|
|
104
|
-
});
|
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
2
|
-
import { C64WASM } from './c64-wasm';
|
|
3
|
-
describe('C64WASM', () => {
|
|
4
|
-
beforeEach(() => {
|
|
5
|
-
vi.restoreAllMocks();
|
|
6
|
-
});
|
|
7
|
-
it('throws when updateHeapViews is called before memory init', () => {
|
|
8
|
-
const wasm = new C64WASM();
|
|
9
|
-
expect(() => wasm.updateHeapViews()).toThrow('WASM memory not initialized');
|
|
10
|
-
});
|
|
11
|
-
it('instantiates wasm and calls ctors', async () => {
|
|
12
|
-
const ctors = vi.fn();
|
|
13
|
-
const instantiateSpy = vi.spyOn(WebAssembly, 'instantiate').mockResolvedValue({
|
|
14
|
-
instance: {
|
|
15
|
-
exports: {
|
|
16
|
-
__wasm_call_ctors: ctors,
|
|
17
|
-
},
|
|
18
|
-
},
|
|
19
|
-
module: {},
|
|
20
|
-
});
|
|
21
|
-
const wasm = new C64WASM();
|
|
22
|
-
await wasm.instantiate(new ArrayBuffer(8));
|
|
23
|
-
expect(instantiateSpy).toHaveBeenCalledOnce();
|
|
24
|
-
expect(wasm.exports).toBeTruthy();
|
|
25
|
-
expect(wasm.heap).toBeTruthy();
|
|
26
|
-
expect(ctors).toHaveBeenCalledOnce();
|
|
27
|
-
});
|
|
28
|
-
it('wraps instantiate failures with a clear error', async () => {
|
|
29
|
-
vi.spyOn(WebAssembly, 'instantiate').mockRejectedValue(new Error('boom'));
|
|
30
|
-
const wasm = new C64WASM();
|
|
31
|
-
await expect(wasm.instantiate(new ArrayBuffer(8))).rejects.toThrow('WASM instantiation failed');
|
|
32
|
-
});
|
|
33
|
-
it('allocates and writes bytes to heap', () => {
|
|
34
|
-
const mem = new WebAssembly.Memory({ initial: 1 });
|
|
35
|
-
const wasm = new C64WASM();
|
|
36
|
-
// Expose the private wasmMemory so updateHeapViews works
|
|
37
|
-
wasm.wasmMemory = mem;
|
|
38
|
-
wasm.exports = {
|
|
39
|
-
malloc: vi.fn(() => 4),
|
|
40
|
-
free: vi.fn(),
|
|
41
|
-
};
|
|
42
|
-
wasm.updateHeapViews();
|
|
43
|
-
const data = new Uint8Array([10, 20, 30]);
|
|
44
|
-
const ptr = wasm.allocAndWrite(data);
|
|
45
|
-
expect(ptr).toBe(4);
|
|
46
|
-
expect(Array.from(wasm.heap.heapU8.slice(4, 7))).toEqual([10, 20, 30]);
|
|
47
|
-
});
|
|
48
|
-
it('load fetches binary and delegates to instantiate', async () => {
|
|
49
|
-
const arrayBuffer = new ArrayBuffer(16);
|
|
50
|
-
const fetchMock = vi.fn().mockResolvedValue({
|
|
51
|
-
ok: true,
|
|
52
|
-
arrayBuffer: vi.fn().mockResolvedValue(arrayBuffer),
|
|
53
|
-
});
|
|
54
|
-
vi.stubGlobal('fetch', fetchMock);
|
|
55
|
-
const instantiateSpy = vi.spyOn(C64WASM.prototype, 'instantiate').mockResolvedValue();
|
|
56
|
-
const wasm = await C64WASM.load('/roms/c64.wasm');
|
|
57
|
-
expect(fetchMock).toHaveBeenCalledWith('/roms/c64.wasm');
|
|
58
|
-
expect(instantiateSpy).toHaveBeenCalledWith(arrayBuffer);
|
|
59
|
-
expect(wasm).toBeInstanceOf(C64WASM);
|
|
60
|
-
});
|
|
61
|
-
it('load throws when fetch is not ok', async () => {
|
|
62
|
-
vi.stubGlobal('fetch', vi.fn().mockResolvedValue({ ok: false, status: 404 }));
|
|
63
|
-
await expect(C64WASM.load('/missing.wasm')).rejects.toThrow('Failed to fetch /missing.wasm: 404');
|
|
64
|
-
});
|
|
65
|
-
});
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
2
|
-
import { EmulatorInput } from './input';
|
|
3
|
-
import { JOYSTICK_DIRECTION, JOYSTICK_FIRE_1, JOYSTICK_PORT_2 } from './constants';
|
|
4
|
-
describe('EmulatorInput', () => {
|
|
5
|
-
beforeEach(() => {
|
|
6
|
-
vi.restoreAllMocks();
|
|
7
|
-
});
|
|
8
|
-
it('maps cursor keys to joystick port 2', () => {
|
|
9
|
-
const emulator = {
|
|
10
|
-
joystickPush: vi.fn(),
|
|
11
|
-
joystickRelease: vi.fn(),
|
|
12
|
-
};
|
|
13
|
-
const input = new EmulatorInput(emulator, window);
|
|
14
|
-
input.attach();
|
|
15
|
-
const down = new KeyboardEvent('keydown', { code: 'ArrowLeft', cancelable: true });
|
|
16
|
-
window.dispatchEvent(down);
|
|
17
|
-
expect(down.defaultPrevented).toBe(true);
|
|
18
|
-
expect(emulator.joystickPush).toHaveBeenCalledWith(JOYSTICK_PORT_2, JOYSTICK_DIRECTION.LEFT);
|
|
19
|
-
const up = new KeyboardEvent('keyup', { code: 'ArrowLeft', cancelable: true });
|
|
20
|
-
window.dispatchEvent(up);
|
|
21
|
-
expect(up.defaultPrevented).toBe(true);
|
|
22
|
-
expect(emulator.joystickRelease).toHaveBeenCalledWith(JOYSTICK_PORT_2, JOYSTICK_DIRECTION.LEFT);
|
|
23
|
-
input.detach();
|
|
24
|
-
});
|
|
25
|
-
it('maps left control to fire1 and ignores held key repeats', () => {
|
|
26
|
-
const emulator = {
|
|
27
|
-
joystickPush: vi.fn(),
|
|
28
|
-
joystickRelease: vi.fn(),
|
|
29
|
-
};
|
|
30
|
-
const input = new EmulatorInput(emulator, window);
|
|
31
|
-
input.attach();
|
|
32
|
-
window.dispatchEvent(new KeyboardEvent('keydown', { code: 'ControlLeft', cancelable: true }));
|
|
33
|
-
window.dispatchEvent(new KeyboardEvent('keydown', { code: 'ControlLeft', cancelable: true }));
|
|
34
|
-
expect(emulator.joystickPush).toHaveBeenCalledTimes(1);
|
|
35
|
-
expect(emulator.joystickPush).toHaveBeenCalledWith(JOYSTICK_PORT_2, JOYSTICK_FIRE_1);
|
|
36
|
-
window.dispatchEvent(new KeyboardEvent('keyup', { code: 'ControlLeft', cancelable: true }));
|
|
37
|
-
expect(emulator.joystickRelease).toHaveBeenCalledWith(JOYSTICK_PORT_2, JOYSTICK_FIRE_1);
|
|
38
|
-
input.detach();
|
|
39
|
-
});
|
|
40
|
-
it('releases any held controls when detached', () => {
|
|
41
|
-
const emulator = {
|
|
42
|
-
joystickPush: vi.fn(),
|
|
43
|
-
joystickRelease: vi.fn(),
|
|
44
|
-
};
|
|
45
|
-
const input = new EmulatorInput(emulator, window);
|
|
46
|
-
input.attach();
|
|
47
|
-
window.dispatchEvent(new KeyboardEvent('keydown', { code: 'ArrowUp', cancelable: true }));
|
|
48
|
-
window.dispatchEvent(new KeyboardEvent('keydown', { code: 'ControlLeft', cancelable: true }));
|
|
49
|
-
input.detach();
|
|
50
|
-
expect(emulator.joystickRelease).toHaveBeenCalledWith(JOYSTICK_PORT_2, JOYSTICK_DIRECTION.UP);
|
|
51
|
-
expect(emulator.joystickRelease).toHaveBeenCalledWith(JOYSTICK_PORT_2, JOYSTICK_FIRE_1);
|
|
52
|
-
});
|
|
53
|
-
});
|
package/dist-ts/src/counter.js
DELETED
|
@@ -1,308 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* C64Emulator — public interface for all emulator interactions.
|
|
3
|
-
*
|
|
4
|
-
* Responsibilities:
|
|
5
|
-
* - Own the C64WASM instance and its lifecycle
|
|
6
|
-
* - Expose clean, meaningful methods to frontend/headless consumers
|
|
7
|
-
* - Manage emulation state (running, paused, frameCount)
|
|
8
|
-
* - Fire onFrame / onAudio callbacks each tick
|
|
9
|
-
*
|
|
10
|
-
* Consumers (C64Player, C64Headless, Vue pages) use this class only.
|
|
11
|
-
* They never touch C64WASM directly.
|
|
12
|
-
*/
|
|
13
|
-
import { C64WASM } from './c64-wasm';
|
|
14
|
-
const FRAME_WIDTH = 384;
|
|
15
|
-
const FRAME_HEIGHT = 272;
|
|
16
|
-
const DEFAULT_SAMPLE_RATE = 44100;
|
|
17
|
-
export class C64Emulator {
|
|
18
|
-
wasm;
|
|
19
|
-
// @ts-expect-error Config is assigned in constructor for future use
|
|
20
|
-
config;
|
|
21
|
-
running = false;
|
|
22
|
-
frameCount = 0;
|
|
23
|
-
/** Called every tick with the latest video frame */
|
|
24
|
-
onFrame;
|
|
25
|
-
/** Called every tick with the latest audio samples */
|
|
26
|
-
onAudio;
|
|
27
|
-
constructor(wasm, config = {}) {
|
|
28
|
-
this.wasm = wasm;
|
|
29
|
-
this.config = {
|
|
30
|
-
videoWidth: FRAME_WIDTH,
|
|
31
|
-
videoHeight: FRAME_HEIGHT,
|
|
32
|
-
sampleRate: DEFAULT_SAMPLE_RATE,
|
|
33
|
-
audioChannels: 1,
|
|
34
|
-
cyclesPerFrame: 50,
|
|
35
|
-
...config,
|
|
36
|
-
};
|
|
37
|
-
}
|
|
38
|
-
// ---------------------------------------------------------------------------
|
|
39
|
-
// Factory
|
|
40
|
-
// ---------------------------------------------------------------------------
|
|
41
|
-
/**
|
|
42
|
-
* Main entry point for any consumer (browser page, headless server, tests).
|
|
43
|
-
*
|
|
44
|
-
* @example
|
|
45
|
-
* const emulator = await C64Emulator.load();
|
|
46
|
-
*/
|
|
47
|
-
static async load(wasmUrl = '/src/emulator/c64.wasm', config = {}) {
|
|
48
|
-
const wasm = await C64WASM.load(wasmUrl);
|
|
49
|
-
const emulator = new C64Emulator(wasm, config);
|
|
50
|
-
emulator.init();
|
|
51
|
-
return emulator;
|
|
52
|
-
}
|
|
53
|
-
// ---------------------------------------------------------------------------
|
|
54
|
-
// Lifecycle
|
|
55
|
-
// ---------------------------------------------------------------------------
|
|
56
|
-
init() {
|
|
57
|
-
const x = this.wasm.exports;
|
|
58
|
-
if (!x)
|
|
59
|
-
throw new Error('WASM exports not available');
|
|
60
|
-
x.c64_init();
|
|
61
|
-
}
|
|
62
|
-
start() {
|
|
63
|
-
this.running = true;
|
|
64
|
-
}
|
|
65
|
-
pause() {
|
|
66
|
-
this.running = false;
|
|
67
|
-
}
|
|
68
|
-
isRunning() {
|
|
69
|
-
return this.running;
|
|
70
|
-
}
|
|
71
|
-
getFrameCount() {
|
|
72
|
-
return this.frameCount;
|
|
73
|
-
}
|
|
74
|
-
reset() {
|
|
75
|
-
this.wasm.exports?.c64_reset();
|
|
76
|
-
this.frameCount = 0;
|
|
77
|
-
}
|
|
78
|
-
// ---------------------------------------------------------------------------
|
|
79
|
-
// Frame loop — call once per animation frame (requestAnimationFrame)
|
|
80
|
-
// ---------------------------------------------------------------------------
|
|
81
|
-
/**
|
|
82
|
-
* The SID continuously fills a circular audio buffer as CPU cycles execute.
|
|
83
|
-
* Must match the ScriptProcessorNode buffer length used by the original
|
|
84
|
-
* c64.js runtime (4096 samples).
|
|
85
|
-
*/
|
|
86
|
-
static AUDIO_BUFFER_SIZE = 4096;
|
|
87
|
-
tick(dTime = 0) {
|
|
88
|
-
if (!this.running)
|
|
89
|
-
return;
|
|
90
|
-
const x = this.wasm.exports;
|
|
91
|
-
// Clamp dTime exactly like the original c64.js render loop:
|
|
92
|
-
// if dTime is 0 or > 100 ms, lock to ~60 fps to prevent runaway cycles.
|
|
93
|
-
if (!dTime || dTime > 100) {
|
|
94
|
-
dTime = 1000 / 60;
|
|
95
|
-
}
|
|
96
|
-
const updated = x.debugger_update(dTime);
|
|
97
|
-
this.frameCount++;
|
|
98
|
-
// Only push a video frame when the emulator actually advanced
|
|
99
|
-
if (updated && this.onFrame) {
|
|
100
|
-
const ptr = x.c64_getPixelBuffer();
|
|
101
|
-
const data = this.wasm.heap.heapU8.subarray(ptr, ptr + FRAME_WIDTH * FRAME_HEIGHT * 4);
|
|
102
|
-
this.onFrame({ width: FRAME_WIDTH, height: FRAME_HEIGHT, data, timestamp: this.frameCount });
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
// ---------------------------------------------------------------------------
|
|
106
|
-
// Game loading
|
|
107
|
-
// ---------------------------------------------------------------------------
|
|
108
|
-
loadGame(options) {
|
|
109
|
-
const x = this.wasm.exports;
|
|
110
|
-
if (!x || !this.wasm.heap)
|
|
111
|
-
throw new Error('WASM not ready');
|
|
112
|
-
const ptr = this.wasm.allocAndWrite(options.data);
|
|
113
|
-
try {
|
|
114
|
-
switch (options.type) {
|
|
115
|
-
case 'prg':
|
|
116
|
-
x.c64_loadPRG(ptr, options.data.length);
|
|
117
|
-
break;
|
|
118
|
-
case 'd64':
|
|
119
|
-
x.c64_insertDisk(ptr, options.data.length);
|
|
120
|
-
break;
|
|
121
|
-
case 'crt':
|
|
122
|
-
x.c64_loadCartridge(ptr, options.data.length);
|
|
123
|
-
break;
|
|
124
|
-
case 'snapshot':
|
|
125
|
-
x.c64_loadSnapshot(ptr, options.data.length);
|
|
126
|
-
break;
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
finally {
|
|
130
|
-
this.wasm.free(ptr);
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
removeCartridge() {
|
|
134
|
-
this.wasm.exports?.c64_removeCartridge();
|
|
135
|
-
}
|
|
136
|
-
// ---------------------------------------------------------------------------
|
|
137
|
-
// Input
|
|
138
|
-
// ---------------------------------------------------------------------------
|
|
139
|
-
handleInput(event) {
|
|
140
|
-
const x = this.wasm.exports;
|
|
141
|
-
if (!x)
|
|
142
|
-
return;
|
|
143
|
-
if (event.type === 'key' && event.key !== undefined) {
|
|
144
|
-
``;
|
|
145
|
-
x.keyboard_keyPressed(Number(event.key));
|
|
146
|
-
}
|
|
147
|
-
else if (event.type === 'joystick') {
|
|
148
|
-
const port = (event.joystickPort ?? 1) - 1; // callers use 1/2, WASM uses 0/1
|
|
149
|
-
const dirMap = { up: 1, down: 2, left: 4, right: 8 };
|
|
150
|
-
if (event.direction)
|
|
151
|
-
x.c64_joystick_push(port, dirMap[event.direction] ?? 0);
|
|
152
|
-
if (event.fire1)
|
|
153
|
-
x.c64_joystick_push(port, 16);
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
keyDown(keyCode) {
|
|
157
|
-
this.wasm.exports?.keyboard_keyPressed(keyCode);
|
|
158
|
-
}
|
|
159
|
-
keyUp(keyCode) {
|
|
160
|
-
this.wasm.exports?.keyboard_keyReleased(keyCode);
|
|
161
|
-
}
|
|
162
|
-
/**
|
|
163
|
-
* Push (press) a joystick direction or fire button.
|
|
164
|
-
*
|
|
165
|
-
* @param port - 1-based joystick port: `1` = port 1, `2` = port 2
|
|
166
|
-
* @param dir - Bitmask value for the direction or fire button:
|
|
167
|
-
* - `0x01` — Up (`JOYSTICK_DIRECTION.UP`)
|
|
168
|
-
* - `0x02` — Down (`JOYSTICK_DIRECTION.DOWN`)
|
|
169
|
-
* - `0x04` — Left (`JOYSTICK_DIRECTION.LEFT`)
|
|
170
|
-
* - `0x08` — Right (`JOYSTICK_DIRECTION.RIGHT`)
|
|
171
|
-
* - `0x10` — Fire (`JOYSTICK_FIRE_1`)
|
|
172
|
-
*
|
|
173
|
-
* @example
|
|
174
|
-
* emulator.joystickPush(2, JOYSTICK_DIRECTION.UP); // port 2 up
|
|
175
|
-
* emulator.joystickPush(2, JOYSTICK_FIRE_1); // port 2 fire
|
|
176
|
-
*/
|
|
177
|
-
joystickPush(port, dir) {
|
|
178
|
-
this.wasm.exports?.c64_joystick_push(port - 1, dir);
|
|
179
|
-
}
|
|
180
|
-
/**
|
|
181
|
-
* Release a joystick direction or fire button.
|
|
182
|
-
*
|
|
183
|
-
* @param port - 1-based joystick port: `1` = port 1, `2` = port 2
|
|
184
|
-
* @param dir - Bitmask value to release (same values as {@link joystickPush})
|
|
185
|
-
*
|
|
186
|
-
* @example
|
|
187
|
-
* emulator.joystickRelease(2, JOYSTICK_DIRECTION.UP); // release port 2 up
|
|
188
|
-
*/
|
|
189
|
-
joystickRelease(port, dir) {
|
|
190
|
-
this.wasm.exports?.c64_joystick_release(port - 1, dir);
|
|
191
|
-
}
|
|
192
|
-
mousePosition(x, y) {
|
|
193
|
-
this.wasm.exports?.c64_mouse_position(x, y);
|
|
194
|
-
}
|
|
195
|
-
// ---------------------------------------------------------------------------
|
|
196
|
-
// Memory access
|
|
197
|
-
// ---------------------------------------------------------------------------
|
|
198
|
-
ramRead(addr) {
|
|
199
|
-
return this.wasm.exports?.c64_ramRead(addr) ?? 0;
|
|
200
|
-
}
|
|
201
|
-
ramWrite(addr, v) {
|
|
202
|
-
this.wasm.exports?.c64_ramWrite(addr, v);
|
|
203
|
-
}
|
|
204
|
-
cpuRead(addr) {
|
|
205
|
-
return this.wasm.exports?.c64_cpuRead(addr) ?? 0;
|
|
206
|
-
}
|
|
207
|
-
cpuWrite(addr, v) {
|
|
208
|
-
this.wasm.exports?.c64_cpuWrite(addr, v);
|
|
209
|
-
}
|
|
210
|
-
// ---------------------------------------------------------------------------
|
|
211
|
-
// CPU state
|
|
212
|
-
// ---------------------------------------------------------------------------
|
|
213
|
-
getPC() {
|
|
214
|
-
return this.wasm.exports?.c64_getPC() ?? 0;
|
|
215
|
-
}
|
|
216
|
-
getRegA() {
|
|
217
|
-
return this.wasm.exports?.c64_getRegA() ?? 0;
|
|
218
|
-
}
|
|
219
|
-
getRegX() {
|
|
220
|
-
return this.wasm.exports?.c64_getRegX() ?? 0;
|
|
221
|
-
}
|
|
222
|
-
getRegY() {
|
|
223
|
-
return this.wasm.exports?.c64_getRegY() ?? 0;
|
|
224
|
-
}
|
|
225
|
-
getSP() {
|
|
226
|
-
return this.wasm.exports?.c64_getSP() ?? 0;
|
|
227
|
-
}
|
|
228
|
-
getCycleCount() {
|
|
229
|
-
return this.wasm.exports?.c64_getCycleCount() ?? 0;
|
|
230
|
-
}
|
|
231
|
-
// ---------------------------------------------------------------------------
|
|
232
|
-
// Video
|
|
233
|
-
// ---------------------------------------------------------------------------
|
|
234
|
-
getFrameBuffer() {
|
|
235
|
-
if (!this.wasm.exports || !this.wasm.heap)
|
|
236
|
-
throw new Error('WASM not ready');
|
|
237
|
-
const ptr = this.wasm.exports.c64_getPixelBuffer();
|
|
238
|
-
const data = this.wasm.heap.heapU8.slice(ptr, ptr + FRAME_WIDTH * FRAME_HEIGHT * 4);
|
|
239
|
-
return { width: FRAME_WIDTH, height: FRAME_HEIGHT, data, timestamp: this.frameCount };
|
|
240
|
-
}
|
|
241
|
-
// ---------------------------------------------------------------------------
|
|
242
|
-
// Snapshot
|
|
243
|
-
// ---------------------------------------------------------------------------
|
|
244
|
-
getSnapshot() {
|
|
245
|
-
const x = this.wasm.exports;
|
|
246
|
-
if (!x || !this.wasm.heap)
|
|
247
|
-
throw new Error('WASM not ready');
|
|
248
|
-
const size = x.c64_getSnapshotSize();
|
|
249
|
-
const ptr = x.malloc(size);
|
|
250
|
-
x.c64_getSnapshot(ptr);
|
|
251
|
-
const snap = this.wasm.heap.heapU8.slice(ptr, ptr + size);
|
|
252
|
-
x.free(ptr);
|
|
253
|
-
return snap;
|
|
254
|
-
}
|
|
255
|
-
// ---------------------------------------------------------------------------
|
|
256
|
-
// Debugger
|
|
257
|
-
// ---------------------------------------------------------------------------
|
|
258
|
-
debuggerPause() {
|
|
259
|
-
this.wasm.exports?.debugger_pause();
|
|
260
|
-
}
|
|
261
|
-
debuggerPlay() {
|
|
262
|
-
this.wasm.exports?.debugger_play();
|
|
263
|
-
}
|
|
264
|
-
debuggerStep() {
|
|
265
|
-
this.wasm.exports?.debugger_step();
|
|
266
|
-
}
|
|
267
|
-
debuggerIsRunning() {
|
|
268
|
-
return (this.wasm.exports?.debugger_isRunning() ?? 0) !== 0;
|
|
269
|
-
}
|
|
270
|
-
setDebugSpeed(speed) {
|
|
271
|
-
this.wasm.exports?.debugger_set_speed(speed);
|
|
272
|
-
}
|
|
273
|
-
// ---------------------------------------------------------------------------
|
|
274
|
-
// Audio / SID
|
|
275
|
-
// ---------------------------------------------------------------------------
|
|
276
|
-
setSampleRate(rate) {
|
|
277
|
-
this.wasm.exports?.sid_setSampleRate(rate);
|
|
278
|
-
}
|
|
279
|
-
/**
|
|
280
|
-
* Return a snapshot of the SID's circular audio buffer (4096 Float32 samples).
|
|
281
|
-
* This mirrors the original c64.js `audioProcess` which reads
|
|
282
|
-
* `sid_getAudioBuffer()` with a fixed `audioBufferLength` — never calling
|
|
283
|
-
* `sid_dumpBuffer()`.
|
|
284
|
-
*/
|
|
285
|
-
getSidBuffer() {
|
|
286
|
-
const x = this.wasm.exports;
|
|
287
|
-
if (!x || !this.wasm.heap)
|
|
288
|
-
return null;
|
|
289
|
-
const ptr = x.sid_getAudioBuffer();
|
|
290
|
-
// Return a copy so the caller owns the data (the SID overwrites this buffer)
|
|
291
|
-
return new Float32Array(this.wasm.heap.heapF32.subarray(ptr >> 2, (ptr >> 2) + C64Emulator.AUDIO_BUFFER_SIZE));
|
|
292
|
-
}
|
|
293
|
-
setSIDModel(model) {
|
|
294
|
-
this.wasm.exports?.sid_setModel(model);
|
|
295
|
-
}
|
|
296
|
-
setVoiceEnabled(voice, enabled) {
|
|
297
|
-
this.wasm.exports?.sid_setVoiceEnabled(enabled ? voice : 0);
|
|
298
|
-
}
|
|
299
|
-
// ---------------------------------------------------------------------------
|
|
300
|
-
// Drive
|
|
301
|
-
// ---------------------------------------------------------------------------
|
|
302
|
-
setDriveEnabled(enabled) {
|
|
303
|
-
this.wasm.exports?.c64_setDriveEnabled(enabled ? 1 : 0);
|
|
304
|
-
}
|
|
305
|
-
getDriveEnabled() {
|
|
306
|
-
return (this.wasm.exports?.c64_getDriveEnabled() ?? 0) !== 0;
|
|
307
|
-
}
|
|
308
|
-
}
|
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
2
|
-
import { C64Emulator } from './c64-emulator';
|
|
3
|
-
import { C64WASM } from './c64-wasm';
|
|
4
|
-
const FRAME_BYTES = 384 * 272 * 4;
|
|
5
|
-
describe('C64Emulator', () => {
|
|
6
|
-
beforeEach(() => {
|
|
7
|
-
vi.restoreAllMocks();
|
|
8
|
-
});
|
|
9
|
-
function makeFakeWasm() {
|
|
10
|
-
const heapU8 = new Uint8Array(FRAME_BYTES + 64);
|
|
11
|
-
for (let i = 0; i < 16; i++) {
|
|
12
|
-
heapU8[i] = i + 1;
|
|
13
|
-
}
|
|
14
|
-
const heapF32 = new Float32Array(4096 + 32);
|
|
15
|
-
heapF32[0] = 0.25;
|
|
16
|
-
heapF32[1] = -0.5;
|
|
17
|
-
const exports = {
|
|
18
|
-
c64_init: vi.fn(),
|
|
19
|
-
c64_reset: vi.fn(),
|
|
20
|
-
debugger_update: vi.fn(() => 1),
|
|
21
|
-
c64_getPixelBuffer: vi.fn(() => 0),
|
|
22
|
-
sid_getAudioBuffer: vi.fn(() => 0),
|
|
23
|
-
sid_dumpBuffer: vi.fn(() => 2),
|
|
24
|
-
c64_loadPRG: vi.fn(),
|
|
25
|
-
c64_insertDisk: vi.fn(),
|
|
26
|
-
c64_loadCartridge: vi.fn(),
|
|
27
|
-
c64_loadSnapshot: vi.fn(),
|
|
28
|
-
c64_ramRead: vi.fn(() => 0xab),
|
|
29
|
-
c64_getSnapshotSize: vi.fn(() => 4),
|
|
30
|
-
malloc: vi.fn(() => 8),
|
|
31
|
-
c64_getSnapshot: vi.fn((ptr) => {
|
|
32
|
-
heapU8.set([9, 8, 7, 6], ptr);
|
|
33
|
-
}),
|
|
34
|
-
free: vi.fn(),
|
|
35
|
-
};
|
|
36
|
-
const wasm = {
|
|
37
|
-
exports,
|
|
38
|
-
heap: {
|
|
39
|
-
heapU8,
|
|
40
|
-
heapF32,
|
|
41
|
-
heapU32: new Uint32Array(heapU8.buffer),
|
|
42
|
-
},
|
|
43
|
-
allocAndWrite: vi.fn(() => 16),
|
|
44
|
-
free: vi.fn(),
|
|
45
|
-
};
|
|
46
|
-
return { wasm, exports, heapU8 };
|
|
47
|
-
}
|
|
48
|
-
it('loads and initializes through C64WASM.load', async () => {
|
|
49
|
-
const { wasm, exports } = makeFakeWasm();
|
|
50
|
-
vi.spyOn(C64WASM, 'load').mockResolvedValue(wasm);
|
|
51
|
-
const emulator = await C64Emulator.load('/custom.wasm');
|
|
52
|
-
expect(C64WASM.load).toHaveBeenCalledWith('/custom.wasm');
|
|
53
|
-
expect(exports.c64_init).toHaveBeenCalledOnce();
|
|
54
|
-
expect(emulator.ramRead(0)).toBe(0xab);
|
|
55
|
-
});
|
|
56
|
-
it('does not tick when paused', async () => {
|
|
57
|
-
const { wasm, exports } = makeFakeWasm();
|
|
58
|
-
vi.spyOn(C64WASM, 'load').mockResolvedValue(wasm);
|
|
59
|
-
const emulator = await C64Emulator.load();
|
|
60
|
-
emulator.tick(12);
|
|
61
|
-
expect(exports.debugger_update).not.toHaveBeenCalled();
|
|
62
|
-
expect(emulator.getFrameCount()).toBe(0);
|
|
63
|
-
});
|
|
64
|
-
it('emits frame callback while running', async () => {
|
|
65
|
-
const { wasm, exports } = makeFakeWasm();
|
|
66
|
-
vi.spyOn(C64WASM, 'load').mockResolvedValue(wasm);
|
|
67
|
-
const emulator = await C64Emulator.load();
|
|
68
|
-
const onFrame = vi.fn();
|
|
69
|
-
emulator.onFrame = onFrame;
|
|
70
|
-
emulator.start();
|
|
71
|
-
emulator.tick(10);
|
|
72
|
-
expect(exports.debugger_update).toHaveBeenCalledWith(10);
|
|
73
|
-
expect(emulator.getFrameCount()).toBe(1);
|
|
74
|
-
expect(onFrame).toHaveBeenCalledOnce();
|
|
75
|
-
expect(onFrame.mock.calls[0][0].width).toBe(384);
|
|
76
|
-
});
|
|
77
|
-
it('provides SID audio buffer via getSidBuffer', async () => {
|
|
78
|
-
const { wasm } = makeFakeWasm();
|
|
79
|
-
vi.spyOn(C64WASM, 'load').mockResolvedValue(wasm);
|
|
80
|
-
const emulator = await C64Emulator.load();
|
|
81
|
-
const buf = emulator.getSidBuffer();
|
|
82
|
-
expect(buf).toBeInstanceOf(Float32Array);
|
|
83
|
-
expect(buf.length).toBe(4096);
|
|
84
|
-
});
|
|
85
|
-
it('always frees allocated memory after loadGame', async () => {
|
|
86
|
-
const { wasm, exports } = makeFakeWasm();
|
|
87
|
-
exports.c64_loadPRG.mockImplementation(() => {
|
|
88
|
-
throw new Error('bad prg');
|
|
89
|
-
});
|
|
90
|
-
vi.spyOn(C64WASM, 'load').mockResolvedValue(wasm);
|
|
91
|
-
const emulator = await C64Emulator.load();
|
|
92
|
-
expect(() => emulator.loadGame({ type: 'prg', data: new Uint8Array([1, 2]) })).toThrow('bad prg');
|
|
93
|
-
expect(wasm.free).toHaveBeenCalledWith(16);
|
|
94
|
-
});
|
|
95
|
-
it('returns a copied framebuffer in getFrameBuffer', async () => {
|
|
96
|
-
const { wasm, heapU8 } = makeFakeWasm();
|
|
97
|
-
vi.spyOn(C64WASM, 'load').mockResolvedValue(wasm);
|
|
98
|
-
const emulator = await C64Emulator.load();
|
|
99
|
-
const frame = emulator.getFrameBuffer();
|
|
100
|
-
heapU8[0] = 250;
|
|
101
|
-
expect(frame.data[0]).toBe(1);
|
|
102
|
-
expect(frame.data.length).toBe(FRAME_BYTES);
|
|
103
|
-
});
|
|
104
|
-
});
|