-
Notifications
You must be signed in to change notification settings - Fork 202
Expand file tree
/
Copy pathruntime.ts
More file actions
421 lines (355 loc) · 14 KB
/
runtime.ts
File metadata and controls
421 lines (355 loc) · 14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
import * as constants from "./constants";
import * as z85 from "./z85";
import { APU } from "./apu";
import { Framebuffer } from "./framebuffer";
import { WebGLCompositor } from "./compositor";
import * as devkit from "./devkit";
import { wasmPatchExportGlobals } from "./wasm-patch";
import * as configConstants from "./config-constants";
export class Runtime {
canvas: HTMLCanvasElement;
memory: WebAssembly.Memory;
apu: APU;
compositor: WebGLCompositor;
data: DataView;
framebuffer: Framebuffer;
pauseState: number;
wasmBuffer: Uint8Array | null = null;
wasmBufferByteLen: number;
wasm: WebAssembly.Instance | null = null;
warnedFileSize = false;
playerIdx : number;
diskName: string;
diskBuffer: ArrayBuffer;
diskSize: number;
constructor (diskName: string) {
const canvas = document.createElement("canvas");
canvas.width = constants.WIDTH;
canvas.height = constants.HEIGHT;
this.canvas = canvas;
const gl = canvas.getContext("webgl", {
alpha: false,
depth: false,
antialias: false,
});
if(!gl) {
throw new Error('web-runtime: could not create wegl context') // TODO(2021-08-01): Fallback to Canvas2DCompositor
}
this.compositor = new WebGLCompositor(gl);
this.apu = new APU();
this.diskName = diskName;
this.diskBuffer = new ArrayBuffer(constants.STORAGE_SIZE);
// Try to load from localStorage
let str;
try {
str = localStorage.getItem(diskName);
} catch (error) {
console.error("Error reading disk", error);
}
this.diskSize = (str != null)
? z85.decode(str, new Uint8Array(this.diskBuffer))
: 0;
this.memory = new WebAssembly.Memory({initial: 1, maximum: 1});
this.data = new DataView(this.memory.buffer);
this.framebuffer = new Framebuffer(this.memory.buffer);
this.reset();
this.pauseState = 0;
this.playerIdx = 0;
this.wasmBufferByteLen = 0;
}
async init () {
await this.apu.init();
}
setMouse (x: number, y: number, buttons: number) {
this.data.setInt16(constants.ADDR_MOUSE_X, x, true);
this.data.setInt16(constants.ADDR_MOUSE_Y, y, true);
this.data.setUint8(constants.ADDR_MOUSE_BUTTONS, buttons);
}
setGamepad (idx: number, buttons: number) {
this.data.setUint8(constants.ADDR_GAMEPAD1 + idx, buttons);
}
setNetplay (localPlayerIdx: number) {
this.data.setUint8(constants.ADDR_NETPLAY, 0b100 | (localPlayerIdx & 0b11));
this.playerIdx = localPlayerIdx;
}
getSystemFlag (mask: number) {
return this.data.getUint8(constants.ADDR_SYSTEM_FLAGS) & mask;
}
unlockAudio () {
this.apu.unlockAudio();
}
pauseAudio() {
this.apu.pauseAudio();
}
reset (zeroMemory?: boolean) {
// Initialize default color table and palette
const mem32 = new Uint32Array(this.memory.buffer);
if (zeroMemory) {
mem32.fill(0);
}
this.pauseState &= ~constants.PAUSE_CRASHED;
mem32.set(constants.COLORS, constants.ADDR_PALETTE >> 2);
this.data.setUint16(constants.ADDR_DRAW_COLORS, 0x1203, true);
// Initialize the mouse off screen
this.data.setInt16(constants.ADDR_MOUSE_X, 0x7fff, true);
this.data.setInt16(constants.ADDR_MOUSE_Y, 0x7fff, true);
}
async load (wasmBuffer: Uint8Array, enforceSizeLimit = true) {
const limit = 1 << 16;
this.wasmBuffer = wasmBuffer;
this.wasmBufferByteLen = wasmBuffer.byteLength;
this.wasm = null;
if (wasmBuffer.byteLength > limit) {
if (configConstants.GAMEDEV_MODE) {
if (!this.warnedFileSize) {
this.warnedFileSize = true;
this.print(`Warning: Cart is larger than ${limit} bytes. Ensure the release build of your cart is small enough to be bundled.`);
}
} else if (enforceSizeLimit) {
throw new Error("Cart too big!");
}
}
const env = {
memory: this.memory,
rect: this.framebuffer.drawRect.bind(this.framebuffer),
oval: this.framebuffer.drawOval.bind(this.framebuffer),
line: this.framebuffer.drawLine.bind(this.framebuffer),
hline: this.framebuffer.drawHLine.bind(this.framebuffer),
vline: this.framebuffer.drawVLine.bind(this.framebuffer),
text: this.text.bind(this),
textUtf8: this.textUtf8.bind(this),
textUtf16: this.textUtf16.bind(this),
blit: this.blit.bind(this),
blitSub: this.blitSub.bind(this),
tone: this.apu.tone.bind(this.apu),
diskr: this.diskr.bind(this),
diskw: this.diskw.bind(this),
trace: this.trace.bind(this),
traceUtf8: this.traceUtf8.bind(this),
traceUtf16: this.traceUtf16.bind(this),
tracef: this.tracef.bind(this),
};
await this.bluescreenOnError(async () => {
const patchedWasmBuffer = wasmPatchExportGlobals(wasmBuffer);
const module = await WebAssembly.instantiate(patchedWasmBuffer, { env });
this.wasm = module.instance;
// Call the WASI _start/_initialize function (different from WASM-4's start callback!)
if (typeof this.wasm.exports["_start"] === 'function') {
this.wasm.exports._start();
}
if (typeof this.wasm.exports["_initialize"] === 'function') {
this.wasm.exports._initialize();
}
});
}
async bluescreenOnError (fn: Function) {
try {
await fn();
} catch (err) {
if (err instanceof Error) {
const errorExplanation = errorToBlueScreenText(err);
this.blueScreen(errorExplanation);
this.printToServer(err.stack ?? '');
}
throw err;
}
}
text (textPtr: number, x: number, y: number) {
const text = new Uint8Array(this.memory.buffer, textPtr);
this.framebuffer.drawText(text, x, y);
}
textUtf8 (textPtr: number, byteLength: number, x: number, y: number) {
const text = new Uint8Array(this.memory.buffer, textPtr, byteLength);
this.framebuffer.drawText(text, x, y);
}
textUtf16 (textPtr: number, byteLength: number, x: number, y: number) {
const text = new Uint16Array(this.memory.buffer, textPtr, byteLength >> 1);
this.framebuffer.drawText(text, x, y);
}
blit (spritePtr: number, x: number, y: number, width: number, height: number, flags: number) {
this.blitSub(spritePtr, x, y, width, height, 0, 0, width, flags);
}
blitSub (spritePtr: number, x: number, y: number, width: number, height: number, srcX: number, srcY: number, stride: number, flags: number) {
const sprite = new Uint8Array(this.memory.buffer, spritePtr);
const bpp2 = (flags & 1);
const flipX = (flags & 2);
const flipY = (flags & 4);
const rotate = (flags & 8);
this.framebuffer.blit(sprite, x, y, width, height, srcX, srcY, stride, bpp2, flipX, flipY, rotate);
}
diskr (destPtr: number, size: number): number {
const bytesRead = Math.min(size, this.diskSize);
const src = new Uint8Array(this.diskBuffer, 0, bytesRead);
const dest = new Uint8Array(this.memory.buffer, destPtr);
dest.set(src);
return bytesRead;
}
diskw (srcPtr: number, size: number): number {
const bytesWritten = Math.min(size, constants.STORAGE_SIZE);
const src = new Uint8Array(this.memory.buffer, srcPtr, bytesWritten);
const dest = new Uint8Array(this.diskBuffer);
// save to localStorage only for the hosting player. (index 0)
// as the disk buffer is a part of the shared state provided by
// the hosting player, don't risk overwriting the local data
// for others.
if (this.playerIdx == 0) {
// Try to save to localStorage
const str = z85.encode(src);
try {
localStorage.setItem(this.diskName, str);
} catch (error) {
// TODO(2022-02-13): Show a warning to the user that storage is not persisted
console.error("Error writing disk", error);
}
}
dest.set(src);
this.diskSize = bytesWritten;
return bytesWritten;
}
getCString (ptr: number) {
let str = "";
for (;;) {
const c = this.data.getUint8(ptr++);
if (c == 0) {
break;
}
str += String.fromCharCode(c);
}
return str;
}
print (str: string ) {
console.log(str);
this.printToServer(str);
}
printToServer (str: string) {
if (devkit.cli_websocket != null && devkit.cli_websocket.readyState == 1) {
devkit.cli_websocket.send(str);
}
}
trace (cstrPtr: number) {
this.print(this.getCString(cstrPtr));
}
traceUtf8 (strUtf8Ptr: number, byteLength: number) {
const strUtf8 = new Uint8Array(this.memory.buffer, strUtf8Ptr, byteLength);
const str = new TextDecoder().decode(strUtf8);
this.print(str);
}
traceUtf16 (strUtf16Ptr: number, byteLength: number) {
const strUtf16 = new Uint8Array(this.memory.buffer, strUtf16Ptr, byteLength);
const str = new TextDecoder("utf-16").decode(strUtf16);
this.print(str);
}
tracef (fmtPtr: number, argPtr: number) {
let output = "";
let ch;
while ((ch = this.data.getUint8(fmtPtr++))) {
if (ch == 37) {
switch (ch = this.data.getUint8(fmtPtr++)) {
case 37: // %
output += "%";
break;
case 99: // c
output += String.fromCharCode(this.data.getInt32(argPtr, true));
argPtr += 4;
break;
case 100: // d
case 120: // x
output += this.data.getInt32(argPtr, true).toString(ch == 100 ? 10 : 16);
argPtr += 4;
break;
case 115: // s
output += this.getCString(this.data.getUint32(argPtr, true));
argPtr += 4;
break;
case 102: // f
output += this.data.getFloat64(argPtr, true);
argPtr += 8;
break;
default: // unknown
output += "%" + String.fromCharCode(ch);
break;
}
} else {
output += String.fromCharCode(ch);
}
}
this.print(output);
}
start () {
let start_function = this.wasm!.exports["start"];
if (typeof start_function === "function") {
this.bluescreenOnError(start_function);
}
}
update () {
if (this.pauseState != 0) {
return;
}
if (!this.getSystemFlag(constants.SYSTEM_PRESERVE_FRAMEBUFFER)) {
this.framebuffer.clear();
}
let update_function = this.wasm!.exports["update"];
if (typeof update_function === "function") {
this.bluescreenOnError(update_function);
}
this.apu.tick();
}
blueScreen (text: string) {
this.pauseState |= constants.PAUSE_CRASHED;
const COLORS = [
0x1111ee, // blue
0x86c06c,
0xaaaaaa, // grey
0xffffff, // white
];
const toCharArr = (s: string) => [...s].map(x => x.charCodeAt(0));
const title = ` ${constants.CRASH_TITLE} `;
const headerTitle = title;
const headerWidth = (8 * title.length);
const headerX = (160 - (8 * title.length)) / 2;
const headerY = 20;
const messageX = 9;
const messageY = 60;
const mem32 = new Uint32Array(this.memory.buffer);
mem32.set(COLORS, constants.ADDR_PALETTE >> 2);
this.data.setUint16(constants.ADDR_DRAW_COLORS, 0x1203, true);
this.framebuffer.clear();
this.framebuffer.drawHLine(headerX, headerY-1, headerWidth);
this.data.setUint16(constants.ADDR_DRAW_COLORS, 0x1131, true);
this.framebuffer.drawText(toCharArr(headerTitle), headerX, headerY);
this.data.setUint16(constants.ADDR_DRAW_COLORS, 0x1203, true);
this.framebuffer.drawText(toCharArr(text), messageX, messageY);
this.composite();
}
composite () {
const palette = new Uint32Array(this.memory.buffer, constants.ADDR_PALETTE, 4);
this.compositor.composite(palette, this.framebuffer);
}
}
function errorToBlueScreenText(err: Error) {
// hand written messages for specific errors
if (err instanceof WebAssembly.RuntimeError) {
let message;
if (err.message.match(/unreachable/)) {
message = "The cartridge has\nreached a code \nsegment marked as\nunreachable.";
} else if (err.message.match(/out of bounds/)) {
message = "The cartridge has\nattempted a memory\naccess that is\nout of bounds.";
}
return message + "\n\n\n\n\nHit R to reboot.";
} else if (err instanceof WebAssembly.LinkError) {
return "The cartridge has\ntried to import\na missing function.\n\n\n\nSee console for\nmore details.";
} else if (err instanceof WebAssembly.CompileError) {
return "The cartridge is\ncorrupted.\n\n\n\nSee console for\nmore details.";
} else if (err instanceof Wasm4Error) {
return err.wasm4Message;
}
return "Unknown error.\n\n\n\nSee console for\nmore details.";
}
class Wasm4Error extends Error {
wasm4Message: string;
constructor(w4Message: string) {
super(w4Message.replace('\n', ' '));
this.name = "Wasm4Error";
this.wasm4Message = w4Message;
}
}