c64-ready 0.5.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.
- package/README.md +269 -0
- package/bin/headless.mjs +95 -0
- package/dist-ts/emulator/c64-emulator.js +307 -0
- package/dist-ts/emulator/c64-emulator.test.js +104 -0
- package/dist-ts/emulator/c64-wasm.js +149 -0
- package/dist-ts/emulator/c64-wasm.test.js +65 -0
- package/dist-ts/emulator/constants.js +9 -0
- package/dist-ts/emulator/input.js +62 -0
- package/dist-ts/emulator/input.test.js +53 -0
- package/dist-ts/emulator/memory.js +0 -0
- package/package.json +59 -0
- package/public/c64.wasm +0 -0
- package/public/games/cartridges/legend-of-wilf.crt +0 -0
- package/src/headless/audio-capture.ts +30 -0
- package/src/headless/c64-headless.ts +104 -0
- package/src/headless/c64-wasm.mjs +128 -0
- package/src/headless/ffmpeg-runner.mjs +442 -0
- package/src/headless/ffmpeg-runner.ts +74 -0
- package/src/headless/frame-capture.ts +30 -0
- package/src/headless/headless-cli.d.ts +16 -0
- package/src/headless/headless-cli.mjs +456 -0
- package/src/headless/headless-cli.mjs.d.ts +18 -0
- package/src/headless/input-bridge.ts +42 -0
package/README.md
ADDED
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
[](https://hayesmaker.github.io/c64-ready/)
|
|
2
|
+
|
|
3
|
+
# c64-ready
|
|
4
|
+
|
|
5
|
+
`c64-ready` is a TypeScript/Vite frontend prototype for running and rendering a Commodore 64 emulator in the browser.
|
|
6
|
+
|
|
7
|
+
It is based on `c64.js (from lvllvl.com by James)` from the original project source: https://github.com/jaammees/lvllvl
|
|
8
|
+
|
|
9
|
+
## Goal
|
|
10
|
+
|
|
11
|
+
Build a clean, testable C64 emulator for the web, with a focus on:
|
|
12
|
+
|
|
13
|
+
- low-level WASM access,
|
|
14
|
+
- emulator control/state,
|
|
15
|
+
- canvas-based rendering
|
|
16
|
+
- node based headless rendering
|
|
17
|
+
- framework agnostic integration
|
|
18
|
+
|
|
19
|
+
### Live URL
|
|
20
|
+
|
|
21
|
+
https://hayesmaker.github.io/c64-ready/
|
|
22
|
+
|
|
23
|
+
## Install and run locally
|
|
24
|
+
- Prerequisites: Node.js 18+ and npm (see https://nodejs.org/)
|
|
25
|
+
|
|
26
|
+
Install dependencies:
|
|
27
|
+
|
|
28
|
+
```zsh
|
|
29
|
+
npm install
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
Start the dev server:
|
|
33
|
+
|
|
34
|
+
```zsh
|
|
35
|
+
npm run dev
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
Create a production build:
|
|
39
|
+
|
|
40
|
+
```zsh
|
|
41
|
+
npm run build
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## Unit tests
|
|
45
|
+
|
|
46
|
+
This project uses Vitest with a jsdom environment (Jest-like API, faster integration with Vite/TypeScript).
|
|
47
|
+
|
|
48
|
+
Run tests:
|
|
49
|
+
|
|
50
|
+
```zsh
|
|
51
|
+
npm test
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Run tests in watch mode:
|
|
55
|
+
|
|
56
|
+
```zsh
|
|
57
|
+
npm run test:watch
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Headless streaming (Docker)
|
|
61
|
+
|
|
62
|
+
The headless player can stream the C64 output over RTMP / HTTP-FLV using Docker Compose.
|
|
63
|
+
Two containers are started:
|
|
64
|
+
|
|
65
|
+
| Container | Role |
|
|
66
|
+
|-----------|------|
|
|
67
|
+
| `c64-nms` | [Node Media Server](https://github.com/illuspas/Node-Media-Server) — RTMP ingest on `:1935`, HTTP-FLV on `:8000` |
|
|
68
|
+
| `c64-headless` | Headless C64 emulator — encodes frames with ffmpeg and pushes to NMS over RTMP |
|
|
69
|
+
|
|
70
|
+
**Prerequisites:** Docker and Docker Compose v2.
|
|
71
|
+
|
|
72
|
+
### Quick start
|
|
73
|
+
|
|
74
|
+
```zsh
|
|
75
|
+
# 1. Copy the env file (optional — defaults boot to BASIC, stream forever)
|
|
76
|
+
cp docker/.env.example docker/.env
|
|
77
|
+
|
|
78
|
+
# 2. Build and start both services
|
|
79
|
+
docker compose up --build
|
|
80
|
+
|
|
81
|
+
# 3. Watch the stream
|
|
82
|
+
ffplay rtmp://localhost:1935/live/c64
|
|
83
|
+
# or open in VLC / OBS: http://localhost:8000/live/c64.flv
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
### Load a cartridge
|
|
87
|
+
|
|
88
|
+
Games are bind-mounted from `public/games/` — no rebuild needed:
|
|
89
|
+
|
|
90
|
+
```zsh
|
|
91
|
+
GAME_PATH=/app/public/games/cartridges/legend-of-wilf.crt docker compose up
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### Environment variables
|
|
95
|
+
|
|
96
|
+
All options can be set in `.env` or passed inline. See `docker/.env.example` for the full list.
|
|
97
|
+
|
|
98
|
+
| Variable | Default | Description |
|
|
99
|
+
|----------|---------|-------------|
|
|
100
|
+
| `WASM_PATH` | `/app/public/c64.wasm` | Path to the WASM binary inside the container |
|
|
101
|
+
| `GAME_PATH` | *(empty)* | Cartridge/disk to load — leave blank to boot to BASIC |
|
|
102
|
+
| `RTMP_URL` | `rtmp://nms:1935/live/c64` | Stream destination (RTMP URL or file path) |
|
|
103
|
+
| `FPS` | `50` | Target frame rate (50 = PAL, 60 = NTSC) |
|
|
104
|
+
| `AUDIO` | *(empty)* | Set to `1` to mux SID audio (AAC) into the stream/recording |
|
|
105
|
+
| `DURATION` | *(empty = forever)* | Stop after this many seconds — omit to stream indefinitely |
|
|
106
|
+
| `VERBOSE` | *(empty)* | Set to any non-empty value for per-frame diagnostics |
|
|
107
|
+
| `NMS_RTMP_HOST_PORT` | `1935` | Host port mapped to the NMS RTMP ingest |
|
|
108
|
+
| `NMS_HTTP_HOST_PORT` | `8000` | Host port mapped to the NMS HTTP-FLV endpoint |
|
|
109
|
+
|
|
110
|
+
### Stop
|
|
111
|
+
|
|
112
|
+
```zsh
|
|
113
|
+
docker compose down
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
## Deployment
|
|
117
|
+
|
|
118
|
+
The project deploys to GitHub Pages automatically via GitHub Actions.
|
|
119
|
+
|
|
120
|
+
On every push to `master`:
|
|
121
|
+
1. Tests run (`npm test`)
|
|
122
|
+
2. If tests pass, a production build is created (`npm run build`)
|
|
123
|
+
3. The `dist/` output is deployed to GitHub Pages
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
## Work in Progress:
|
|
127
|
+
- Proof of Concept Implementation:
|
|
128
|
+
- [x] WASM module loading and initialization
|
|
129
|
+
- [x] Emulator control and state management
|
|
130
|
+
- [x] Canvas-based rendering
|
|
131
|
+
- [x] Node-based headless rendering
|
|
132
|
+
- [x] Framework agnostic integration (e.g., Vanilla HTML+JS, React, Vue, Angular etc)
|
|
133
|
+
- Additional features:
|
|
134
|
+
- [x] Audio output
|
|
135
|
+
- [x] Input handling (keyboard)
|
|
136
|
+
- [x] Loading and running .crt cartridge roms
|
|
137
|
+
- [x] Display settings
|
|
138
|
+
- [x] Docker headless streaming (RTMP / HTTP-FLV via Node Media Server)
|
|
139
|
+
- [ ] Gamepad support
|
|
140
|
+
- [ ] Touch controls
|
|
141
|
+
|
|
142
|
+
## Changelog & Releases
|
|
143
|
+
|
|
144
|
+
This repository includes an automated changelog generator and a simple release workflow to help keep releases and the in-app version in sync.
|
|
145
|
+
|
|
146
|
+
- `tools/generate_changelog.js` builds `CHANGELOG.md` from git history and GitHub Release notes. In CI the generator opens a Pull Request with the updated `CHANGELOG.md` for review instead of committing directly to `master`.
|
|
147
|
+
- The CI workflow that runs the generator is `.github/workflows/generate_changelog.yml`. It runs on pushes to `master` and when a release is published.
|
|
148
|
+
- The wiki publisher (`.github/workflows/publish_wiki.yml`) runs on pushes to `master` and publishes `PROJECT_OVERVIEW.md` to the repository wiki.
|
|
149
|
+
Recommended release workflow (simple git flow)
|
|
150
|
+
|
|
151
|
+
If you prefer the standard git-based release flow (no `gh` CLI required), follow these steps:
|
|
152
|
+
|
|
153
|
+
1) Update the version and create a tag locally (npm will update package.json and create a tag):
|
|
154
|
+
|
|
155
|
+
```zsh
|
|
156
|
+
# bump the patch version (or use minor/major)
|
|
157
|
+
npm version patch -m "chore(release): %s"
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
2) Push the commit and tags to GitHub:
|
|
161
|
+
|
|
162
|
+
```zsh
|
|
163
|
+
git push origin master
|
|
164
|
+
git push --tags
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
3) Create a GitHub Release from the tag using the GitHub web UI:
|
|
168
|
+
|
|
169
|
+
- Go to your repository → Releases → Draft a new release
|
|
170
|
+
- Select the tag you just pushed, add release notes, and publish the release
|
|
171
|
+
|
|
172
|
+
What happens next
|
|
173
|
+
|
|
174
|
+
- Publishing a release will trigger the changelog workflow in CI. The generator will create or update `CHANGELOG.md` and open a Pull Request with the changelog changes for review. Merge that PR to update `master` with the changelog.
|
|
175
|
+
- The wiki publisher workflow runs on pushes to `master` and will publish `PROJECT_OVERVIEW.md` to the wiki when the changelog PR (or any other commit) is merged.
|
|
176
|
+
|
|
177
|
+
About tokens / authentication
|
|
178
|
+
|
|
179
|
+
- You do not need to provide secrets for GitHub Actions to run in CI: Actions injects an automatic `GITHUB_TOKEN` into workflow runs, and the workflows here use that token to create branches/PRs and push changes back to the same repository.
|
|
180
|
+
- If you run `tools/generate_changelog.js` locally and want it to push branches or create a PR from your machine, that script will use your local git credentials (SSH keys or credential helper); you do not have to set `GITHUB_TOKEN` locally unless you specifically want the script to authenticate via token.
|
|
181
|
+
|
|
182
|
+
Quick local tools
|
|
183
|
+
|
|
184
|
+
- Generate the changelog locally (writes `CHANGELOG.md` but will not create a PR unless CI runs the script with its `GITHUB_TOKEN`):
|
|
185
|
+
|
|
186
|
+
```zsh
|
|
187
|
+
node tools/generate_changelog.js
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
Verify the in-app version
|
|
191
|
+
|
|
192
|
+
- After bumping the version, rebuild the site so the new version is embedded in the app:
|
|
193
|
+
|
|
194
|
+
```zsh
|
|
195
|
+
npm run build
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
- Open the built site (or run the dev server) and check the Help dialog — the version (and short git hash when available) appears at the bottom.
|
|
199
|
+
|
|
200
|
+
## Docs & Wiki publishing
|
|
201
|
+
|
|
202
|
+
This repository now keeps human-facing documentation in a `docs/` folder at the repository root (except for `AGENTS.md` and `README.md`, which remain in root).
|
|
203
|
+
|
|
204
|
+
- `docs/` contains user and developer docs that should be published to the repository wiki.
|
|
205
|
+
- The helper script `scripts/publish_wiki.sh` will prepare a wiki clone, copy `docs/*.md` plus the canonical `CHANGELOG.md` (from the repo root) into the wiki clone, update `Home.md` with links, and commit the changes so you can review before pushing.
|
|
206
|
+
|
|
207
|
+
To prepare a wiki update locally (no push):
|
|
208
|
+
```bash
|
|
209
|
+
# clone the wiki and stage the docs in the wiki clone (script prints the clone path)
|
|
210
|
+
./scripts/publish_wiki.sh git@github.com:YOUR_USER/c64-ready.wiki.git
|
|
211
|
+
|
|
212
|
+
# Inspect the prepared wiki clone (the script prints where it wrote files)
|
|
213
|
+
cd /tmp/<the-script-printed-path>/wiki
|
|
214
|
+
git status
|
|
215
|
+
git show --name-only
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
To publish the changes to the GitHub wiki (requires push access):
|
|
219
|
+
```bash
|
|
220
|
+
cd /tmp/<the-script-printed-path>/wiki
|
|
221
|
+
git push origin HEAD
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
Note: the script does not push to the wiki remote automatically to avoid accidental publishing. It also appends links to `Home.md` only if they are not already present.
|
|
225
|
+
|
|
226
|
+
## Release helper (`tools/release.sh`)
|
|
227
|
+
|
|
228
|
+
This project includes a small release helper script `./tools/release.sh` that wraps `npm version` and git push steps. The script now supports a safe `--dry-run` mode that reports the version that WOULD be created without modifying files, and a `--preid` option for prereleases.
|
|
229
|
+
|
|
230
|
+
Examples
|
|
231
|
+
|
|
232
|
+
- Dry-run a minor bump (no changes made):
|
|
233
|
+
```bash
|
|
234
|
+
./tools/release.sh minor --dry-run
|
|
235
|
+
# -> prints predicted new version (e.g. 0.4.0) and does not edit package.json
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
- Create the version commit and tag locally but do not push:
|
|
239
|
+
```bash
|
|
240
|
+
./tools/release.sh minor --no-push
|
|
241
|
+
# creates the commit and tag (npm version) locally so you can inspect before pushing
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
- Full release (commit, tag, and push to origin/master):
|
|
245
|
+
```bash
|
|
246
|
+
# Run this on master with a clean working tree
|
|
247
|
+
./tools/release.sh minor
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
- Create a prerelease (example with `beta` identifier) — dry-run:
|
|
251
|
+
```bash
|
|
252
|
+
./tools/release.sh preminor --preid beta --dry-run
|
|
253
|
+
# -> e.g. predicts 0.4.0-beta.0 without changing files
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
- Create a prerelease (create locally, no push):
|
|
257
|
+
```bash
|
|
258
|
+
./tools/release.sh prerelease --preid beta --no-push
|
|
259
|
+
# creates tag like v0.3.1-beta.0 locally
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
Notes and safety
|
|
263
|
+
|
|
264
|
+
- `--dry-run` will never edit `package.json` or create git commits/tags — it only prints the predicted version and the commands the script would run.
|
|
265
|
+
- Use `--no-push` to create the commit and tag locally for inspection, then push manually when you're ready.
|
|
266
|
+
- The script expects a clean working tree and will prompt if you are not on `master` (unless you use `--dry-run` which skips branch checks).
|
|
267
|
+
|
|
268
|
+
If you want, I can add examples to the CI workflow to automatically create releases on merges to `master` using this helper.
|
|
269
|
+
|
package/bin/headless.mjs
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* c64-headless — CLI entry point
|
|
4
|
+
*
|
|
5
|
+
* Usage:
|
|
6
|
+
* c64-headless --wasm <path/to/c64.wasm> [options]
|
|
7
|
+
*
|
|
8
|
+
* Options:
|
|
9
|
+
* --wasm <path> Path to c64.wasm [default: public/c64.wasm]
|
|
10
|
+
* --game <path> Cartridge / disk image to load
|
|
11
|
+
* --no-game Boot to BASIC prompt (no game)
|
|
12
|
+
* --record Stream/record frames via ffmpeg
|
|
13
|
+
* --output <path|url> Output file or rtmp:// URL [default: temp/c64-record-<ts>.mp4]
|
|
14
|
+
* --duration <secs> Recording duration in seconds [default: 60]
|
|
15
|
+
* --fps <n> Frame rate [default: 50 (PAL)]
|
|
16
|
+
* --verbose Print per-frame diagnostics
|
|
17
|
+
* --help Show this help
|
|
18
|
+
*
|
|
19
|
+
* Prerequisites:
|
|
20
|
+
* 1. ffmpeg must be on PATH (for --record)
|
|
21
|
+
* 2. Build the JS wrapper once: npx tsc -p tsconfig.build2.json
|
|
22
|
+
*
|
|
23
|
+
* Examples:
|
|
24
|
+
* # Stream to RTMP indefinitely
|
|
25
|
+
* c64-headless --wasm public/c64.wasm --record --output rtmp://localhost:1935/live/c64 --duration 3600 --no-game
|
|
26
|
+
*
|
|
27
|
+
* # Record 30 seconds to a file
|
|
28
|
+
* c64-headless --wasm public/c64.wasm --record --output out.mp4 --duration 30 --no-game
|
|
29
|
+
*
|
|
30
|
+
* # Load a game and stream
|
|
31
|
+
* c64-headless --wasm public/c64.wasm --game games/mygame.crt --record --output rtmp://localhost:1935/live/c64 --duration 3600
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
const argv = process.argv.slice(2);
|
|
35
|
+
|
|
36
|
+
if (argv.includes('--help') || argv.includes('-h')) {
|
|
37
|
+
const help = `
|
|
38
|
+
c64-headless — headless Commodore 64 emulator
|
|
39
|
+
|
|
40
|
+
Usage:
|
|
41
|
+
c64-headless [options]
|
|
42
|
+
|
|
43
|
+
Options:
|
|
44
|
+
--wasm <path> Path to c64.wasm (default: public/c64.wasm)
|
|
45
|
+
--game <path> Cartridge or disk image to load
|
|
46
|
+
--no-game Boot to BASIC prompt without loading a game
|
|
47
|
+
--record Encode frames with ffmpeg
|
|
48
|
+
--audio Include SID audio in the recording / stream (requires --record)
|
|
49
|
+
--output <path|url> Output file path or rtmp:// stream URL
|
|
50
|
+
--duration <secs> Recording duration in seconds (omit for endless streaming)
|
|
51
|
+
--fps <n> Target frame rate (default: 50 for PAL)
|
|
52
|
+
--verbose Print per-frame diagnostics to stderr
|
|
53
|
+
--help Show this help
|
|
54
|
+
|
|
55
|
+
Prerequisites:
|
|
56
|
+
ffmpeg must be on PATH for --record
|
|
57
|
+
Build the JS wrapper once: npx tsc -p tsconfig.build2.json
|
|
58
|
+
|
|
59
|
+
Examples:
|
|
60
|
+
# RTMP live stream (5 minutes)
|
|
61
|
+
c64-headless --wasm public/c64.wasm --no-game --record \\
|
|
62
|
+
--output rtmp://localhost:1935/live/c64 --duration 300
|
|
63
|
+
|
|
64
|
+
# Record to file (30 seconds)
|
|
65
|
+
c64-headless --wasm public/c64.wasm --no-game --record \\
|
|
66
|
+
--output out.mp4 --duration 30
|
|
67
|
+
|
|
68
|
+
# Load a cartridge and stream
|
|
69
|
+
c64-headless --wasm public/c64.wasm --game game.crt --record \\
|
|
70
|
+
--output rtmp://localhost:1935/live/c64 --duration 3600
|
|
71
|
+
`.trim();
|
|
72
|
+
console.log(help);
|
|
73
|
+
process.exit(0);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
import { runHeadless } from '../src/headless/headless-cli.mjs';
|
|
77
|
+
|
|
78
|
+
const res = await runHeadless({ argv });
|
|
79
|
+
|
|
80
|
+
if (Array.isArray(res.output)) {
|
|
81
|
+
for (const line of res.output) console.log(line);
|
|
82
|
+
} else if (res.output && res.output !== 'help') {
|
|
83
|
+
console.log(res.output);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (!res.ok) {
|
|
87
|
+
if (res.err === 'no-wasm') {
|
|
88
|
+
console.error('Error: no c64.wasm found. Provide --wasm <path>');
|
|
89
|
+
} else if (res.err === 'ffmpeg-start-failed') {
|
|
90
|
+
console.error('Error: ffmpeg failed to start. Is ffmpeg on your PATH?');
|
|
91
|
+
} else if (res.err) {
|
|
92
|
+
console.error(`Error: ${res.err}`);
|
|
93
|
+
}
|
|
94
|
+
process.exit(1);
|
|
95
|
+
}
|
|
@@ -0,0 +1,307 @@
|
|
|
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
|
+
config;
|
|
20
|
+
running = false;
|
|
21
|
+
frameCount = 0;
|
|
22
|
+
/** Called every tick with the latest video frame */
|
|
23
|
+
onFrame;
|
|
24
|
+
/** Called every tick with the latest audio samples */
|
|
25
|
+
onAudio;
|
|
26
|
+
constructor(wasm, config = {}) {
|
|
27
|
+
this.wasm = wasm;
|
|
28
|
+
this.config = {
|
|
29
|
+
videoWidth: FRAME_WIDTH,
|
|
30
|
+
videoHeight: FRAME_HEIGHT,
|
|
31
|
+
sampleRate: DEFAULT_SAMPLE_RATE,
|
|
32
|
+
audioChannels: 1,
|
|
33
|
+
cyclesPerFrame: 50,
|
|
34
|
+
...config,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
// ---------------------------------------------------------------------------
|
|
38
|
+
// Factory
|
|
39
|
+
// ---------------------------------------------------------------------------
|
|
40
|
+
/**
|
|
41
|
+
* Main entry point for any consumer (browser page, headless server, tests).
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* const emulator = await C64Emulator.load();
|
|
45
|
+
*/
|
|
46
|
+
static async load(wasmUrl = '/src/emulator/c64.wasm', config = {}) {
|
|
47
|
+
const wasm = await C64WASM.load(wasmUrl);
|
|
48
|
+
const emulator = new C64Emulator(wasm, config);
|
|
49
|
+
emulator.init();
|
|
50
|
+
return emulator;
|
|
51
|
+
}
|
|
52
|
+
// ---------------------------------------------------------------------------
|
|
53
|
+
// Lifecycle
|
|
54
|
+
// ---------------------------------------------------------------------------
|
|
55
|
+
init() {
|
|
56
|
+
const x = this.wasm.exports;
|
|
57
|
+
if (!x)
|
|
58
|
+
throw new Error('WASM exports not available');
|
|
59
|
+
x.c64_init();
|
|
60
|
+
}
|
|
61
|
+
start() {
|
|
62
|
+
this.running = true;
|
|
63
|
+
}
|
|
64
|
+
pause() {
|
|
65
|
+
this.running = false;
|
|
66
|
+
}
|
|
67
|
+
isRunning() {
|
|
68
|
+
return this.running;
|
|
69
|
+
}
|
|
70
|
+
getFrameCount() {
|
|
71
|
+
return this.frameCount;
|
|
72
|
+
}
|
|
73
|
+
reset() {
|
|
74
|
+
this.wasm.exports?.c64_reset();
|
|
75
|
+
this.frameCount = 0;
|
|
76
|
+
}
|
|
77
|
+
// ---------------------------------------------------------------------------
|
|
78
|
+
// Frame loop — call once per animation frame (requestAnimationFrame)
|
|
79
|
+
// ---------------------------------------------------------------------------
|
|
80
|
+
/**
|
|
81
|
+
* The SID continuously fills a circular audio buffer as CPU cycles execute.
|
|
82
|
+
* Must match the ScriptProcessorNode buffer length used by the original
|
|
83
|
+
* c64.js runtime (4096 samples).
|
|
84
|
+
*/
|
|
85
|
+
static AUDIO_BUFFER_SIZE = 4096;
|
|
86
|
+
tick(dTime = 0) {
|
|
87
|
+
if (!this.running)
|
|
88
|
+
return;
|
|
89
|
+
const x = this.wasm.exports;
|
|
90
|
+
// Clamp dTime exactly like the original c64.js render loop:
|
|
91
|
+
// if dTime is 0 or > 100 ms, lock to ~60 fps to prevent runaway cycles.
|
|
92
|
+
// if (!dTime || dTime > 100) {
|
|
93
|
+
// dTime = 1000 / 60;
|
|
94
|
+
// }
|
|
95
|
+
const updated = x.debugger_update(dTime);
|
|
96
|
+
this.frameCount++;
|
|
97
|
+
// Only push a video frame when the emulator actually advanced
|
|
98
|
+
if (updated && this.onFrame) {
|
|
99
|
+
const ptr = x.c64_getPixelBuffer();
|
|
100
|
+
const data = this.wasm.heap.heapU8.subarray(ptr, ptr + FRAME_WIDTH * FRAME_HEIGHT * 4);
|
|
101
|
+
this.onFrame({ width: FRAME_WIDTH, height: FRAME_HEIGHT, data, timestamp: this.frameCount });
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
// ---------------------------------------------------------------------------
|
|
105
|
+
// Game loading
|
|
106
|
+
// ---------------------------------------------------------------------------
|
|
107
|
+
loadGame(options) {
|
|
108
|
+
const x = this.wasm.exports;
|
|
109
|
+
if (!x || !this.wasm.heap)
|
|
110
|
+
throw new Error('WASM not ready');
|
|
111
|
+
const ptr = this.wasm.allocAndWrite(options.data);
|
|
112
|
+
try {
|
|
113
|
+
switch (options.type) {
|
|
114
|
+
case 'prg':
|
|
115
|
+
x.c64_loadPRG(ptr, options.data.length);
|
|
116
|
+
break;
|
|
117
|
+
case 'd64':
|
|
118
|
+
x.c64_insertDisk(ptr, options.data.length);
|
|
119
|
+
break;
|
|
120
|
+
case 'crt':
|
|
121
|
+
x.c64_loadCartridge(ptr, options.data.length);
|
|
122
|
+
break;
|
|
123
|
+
case 'snapshot':
|
|
124
|
+
x.c64_loadSnapshot(ptr, options.data.length);
|
|
125
|
+
break;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
finally {
|
|
129
|
+
this.wasm.free(ptr);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
removeCartridge() {
|
|
133
|
+
this.wasm.exports?.c64_removeCartridge();
|
|
134
|
+
}
|
|
135
|
+
// ---------------------------------------------------------------------------
|
|
136
|
+
// Input
|
|
137
|
+
// ---------------------------------------------------------------------------
|
|
138
|
+
handleInput(event) {
|
|
139
|
+
const x = this.wasm.exports;
|
|
140
|
+
if (!x)
|
|
141
|
+
return;
|
|
142
|
+
if (event.type === 'key' && event.key !== undefined) {
|
|
143
|
+
``;
|
|
144
|
+
x.keyboard_keyPressed(Number(event.key));
|
|
145
|
+
}
|
|
146
|
+
else if (event.type === 'joystick') {
|
|
147
|
+
const port = (event.joystickPort ?? 1) - 1; // callers use 1/2, WASM uses 0/1
|
|
148
|
+
const dirMap = { up: 1, down: 2, left: 4, right: 8 };
|
|
149
|
+
if (event.direction)
|
|
150
|
+
x.c64_joystick_push(port, dirMap[event.direction] ?? 0);
|
|
151
|
+
if (event.fire1)
|
|
152
|
+
x.c64_joystick_push(port, 16);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
keyDown(keyCode) {
|
|
156
|
+
this.wasm.exports?.keyboard_keyPressed(keyCode);
|
|
157
|
+
}
|
|
158
|
+
keyUp(keyCode) {
|
|
159
|
+
this.wasm.exports?.keyboard_keyReleased(keyCode);
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Push (press) a joystick direction or fire button.
|
|
163
|
+
*
|
|
164
|
+
* @param port - 1-based joystick port: `1` = port 1, `2` = port 2
|
|
165
|
+
* @param dir - Bitmask value for the direction or fire button:
|
|
166
|
+
* - `0x01` — Up (`JOYSTICK_DIRECTION.UP`)
|
|
167
|
+
* - `0x02` — Down (`JOYSTICK_DIRECTION.DOWN`)
|
|
168
|
+
* - `0x04` — Left (`JOYSTICK_DIRECTION.LEFT`)
|
|
169
|
+
* - `0x08` — Right (`JOYSTICK_DIRECTION.RIGHT`)
|
|
170
|
+
* - `0x10` — Fire (`JOYSTICK_FIRE_1`)
|
|
171
|
+
*
|
|
172
|
+
* @example
|
|
173
|
+
* emulator.joystickPush(2, JOYSTICK_DIRECTION.UP); // port 2 up
|
|
174
|
+
* emulator.joystickPush(2, JOYSTICK_FIRE_1); // port 2 fire
|
|
175
|
+
*/
|
|
176
|
+
joystickPush(port, dir) {
|
|
177
|
+
this.wasm.exports?.c64_joystick_push(port - 1, dir);
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Release a joystick direction or fire button.
|
|
181
|
+
*
|
|
182
|
+
* @param port - 1-based joystick port: `1` = port 1, `2` = port 2
|
|
183
|
+
* @param dir - Bitmask value to release (same values as {@link joystickPush})
|
|
184
|
+
*
|
|
185
|
+
* @example
|
|
186
|
+
* emulator.joystickRelease(2, JOYSTICK_DIRECTION.UP); // release port 2 up
|
|
187
|
+
*/
|
|
188
|
+
joystickRelease(port, dir) {
|
|
189
|
+
this.wasm.exports?.c64_joystick_release(port - 1, dir);
|
|
190
|
+
}
|
|
191
|
+
mousePosition(x, y) {
|
|
192
|
+
this.wasm.exports?.c64_mouse_position(x, y);
|
|
193
|
+
}
|
|
194
|
+
// ---------------------------------------------------------------------------
|
|
195
|
+
// Memory access
|
|
196
|
+
// ---------------------------------------------------------------------------
|
|
197
|
+
ramRead(addr) {
|
|
198
|
+
return this.wasm.exports?.c64_ramRead(addr) ?? 0;
|
|
199
|
+
}
|
|
200
|
+
ramWrite(addr, v) {
|
|
201
|
+
this.wasm.exports?.c64_ramWrite(addr, v);
|
|
202
|
+
}
|
|
203
|
+
cpuRead(addr) {
|
|
204
|
+
return this.wasm.exports?.c64_cpuRead(addr) ?? 0;
|
|
205
|
+
}
|
|
206
|
+
cpuWrite(addr, v) {
|
|
207
|
+
this.wasm.exports?.c64_cpuWrite(addr, v);
|
|
208
|
+
}
|
|
209
|
+
// ---------------------------------------------------------------------------
|
|
210
|
+
// CPU state
|
|
211
|
+
// ---------------------------------------------------------------------------
|
|
212
|
+
getPC() {
|
|
213
|
+
return this.wasm.exports?.c64_getPC() ?? 0;
|
|
214
|
+
}
|
|
215
|
+
getRegA() {
|
|
216
|
+
return this.wasm.exports?.c64_getRegA() ?? 0;
|
|
217
|
+
}
|
|
218
|
+
getRegX() {
|
|
219
|
+
return this.wasm.exports?.c64_getRegX() ?? 0;
|
|
220
|
+
}
|
|
221
|
+
getRegY() {
|
|
222
|
+
return this.wasm.exports?.c64_getRegY() ?? 0;
|
|
223
|
+
}
|
|
224
|
+
getSP() {
|
|
225
|
+
return this.wasm.exports?.c64_getSP() ?? 0;
|
|
226
|
+
}
|
|
227
|
+
getCycleCount() {
|
|
228
|
+
return this.wasm.exports?.c64_getCycleCount() ?? 0;
|
|
229
|
+
}
|
|
230
|
+
// ---------------------------------------------------------------------------
|
|
231
|
+
// Video
|
|
232
|
+
// ---------------------------------------------------------------------------
|
|
233
|
+
getFrameBuffer() {
|
|
234
|
+
if (!this.wasm.exports || !this.wasm.heap)
|
|
235
|
+
throw new Error('WASM not ready');
|
|
236
|
+
const ptr = this.wasm.exports.c64_getPixelBuffer();
|
|
237
|
+
const data = this.wasm.heap.heapU8.slice(ptr, ptr + FRAME_WIDTH * FRAME_HEIGHT * 4);
|
|
238
|
+
return { width: FRAME_WIDTH, height: FRAME_HEIGHT, data, timestamp: this.frameCount };
|
|
239
|
+
}
|
|
240
|
+
// ---------------------------------------------------------------------------
|
|
241
|
+
// Snapshot
|
|
242
|
+
// ---------------------------------------------------------------------------
|
|
243
|
+
getSnapshot() {
|
|
244
|
+
const x = this.wasm.exports;
|
|
245
|
+
if (!x || !this.wasm.heap)
|
|
246
|
+
throw new Error('WASM not ready');
|
|
247
|
+
const size = x.c64_getSnapshotSize();
|
|
248
|
+
const ptr = x.malloc(size);
|
|
249
|
+
x.c64_getSnapshot(ptr);
|
|
250
|
+
const snap = this.wasm.heap.heapU8.slice(ptr, ptr + size);
|
|
251
|
+
x.free(ptr);
|
|
252
|
+
return snap;
|
|
253
|
+
}
|
|
254
|
+
// ---------------------------------------------------------------------------
|
|
255
|
+
// Debugger
|
|
256
|
+
// ---------------------------------------------------------------------------
|
|
257
|
+
debuggerPause() {
|
|
258
|
+
this.wasm.exports?.debugger_pause();
|
|
259
|
+
}
|
|
260
|
+
debuggerPlay() {
|
|
261
|
+
this.wasm.exports?.debugger_play();
|
|
262
|
+
}
|
|
263
|
+
debuggerStep() {
|
|
264
|
+
this.wasm.exports?.debugger_step();
|
|
265
|
+
}
|
|
266
|
+
debuggerIsRunning() {
|
|
267
|
+
return (this.wasm.exports?.debugger_isRunning() ?? 0) !== 0;
|
|
268
|
+
}
|
|
269
|
+
setDebugSpeed(speed) {
|
|
270
|
+
this.wasm.exports?.debugger_set_speed(speed);
|
|
271
|
+
}
|
|
272
|
+
// ---------------------------------------------------------------------------
|
|
273
|
+
// Audio / SID
|
|
274
|
+
// ---------------------------------------------------------------------------
|
|
275
|
+
setSampleRate(rate) {
|
|
276
|
+
this.wasm.exports?.sid_setSampleRate(rate);
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
* Return a snapshot of the SID's circular audio buffer (4096 Float32 samples).
|
|
280
|
+
* This mirrors the original c64.js `audioProcess` which reads
|
|
281
|
+
* `sid_getAudioBuffer()` with a fixed `audioBufferLength` — never calling
|
|
282
|
+
* `sid_dumpBuffer()`.
|
|
283
|
+
*/
|
|
284
|
+
getSidBuffer() {
|
|
285
|
+
const x = this.wasm.exports;
|
|
286
|
+
if (!x || !this.wasm.heap)
|
|
287
|
+
return null;
|
|
288
|
+
const ptr = x.sid_getAudioBuffer();
|
|
289
|
+
// Return a copy so the caller owns the data (the SID overwrites this buffer)
|
|
290
|
+
return new Float32Array(this.wasm.heap.heapF32.subarray(ptr >> 2, (ptr >> 2) + C64Emulator.AUDIO_BUFFER_SIZE));
|
|
291
|
+
}
|
|
292
|
+
setSIDModel(model) {
|
|
293
|
+
this.wasm.exports?.sid_setModel(model);
|
|
294
|
+
}
|
|
295
|
+
setVoiceEnabled(voice, enabled) {
|
|
296
|
+
this.wasm.exports?.sid_setVoiceEnabled(enabled ? voice : 0);
|
|
297
|
+
}
|
|
298
|
+
// ---------------------------------------------------------------------------
|
|
299
|
+
// Drive
|
|
300
|
+
// ---------------------------------------------------------------------------
|
|
301
|
+
setDriveEnabled(enabled) {
|
|
302
|
+
this.wasm.exports?.c64_setDriveEnabled(enabled ? 1 : 0);
|
|
303
|
+
}
|
|
304
|
+
getDriveEnabled() {
|
|
305
|
+
return (this.wasm.exports?.c64_getDriveEnabled() ?? 0) !== 0;
|
|
306
|
+
}
|
|
307
|
+
}
|