|
| 1 | +import { Nes as ONes, Button, Player } from '@mantou/nes'; |
| 2 | +import { encodeQoiFrame, decodeQoiFrame } from '@mantou/nes-sandbox'; |
| 3 | +import { |
| 4 | + ADDR_GAMEPAD1, |
| 5 | + ADDR_MOUSE_BUTTONS, |
| 6 | + ADDR_MOUSE_X, |
| 7 | + ADDR_MOUSE_Y, |
| 8 | + BUTTON_DOWN, |
| 9 | + BUTTON_LEFT, |
| 10 | + BUTTON_RIGHT, |
| 11 | + BUTTON_UP, |
| 12 | + BUTTON_X, |
| 13 | + BUTTON_Z, |
| 14 | + HEIGHT, |
| 15 | + MOUSE_LEFT, |
| 16 | + MOUSE_RIGHT, |
| 17 | + PAUSE_REBOOTING, |
| 18 | + WIDTH, |
| 19 | + Runtime, |
| 20 | +} from '@mantou/nes-wasm4'; |
| 21 | + |
| 22 | +function getPlayerIdx(player: Player) { |
| 23 | + switch (player) { |
| 24 | + case Player.One: |
| 25 | + return 0; |
| 26 | + case Player.Two: |
| 27 | + return 1; |
| 28 | + case Player.Three: |
| 29 | + return 2; |
| 30 | + case Player.Four: |
| 31 | + return 3; |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +function getButtonBitFlag(button: Button) { |
| 36 | + switch (button) { |
| 37 | + case Button.JoypadA: |
| 38 | + return BUTTON_X; |
| 39 | + case Button.JoypadB: |
| 40 | + return BUTTON_Z; |
| 41 | + case Button.JoypadLeft: |
| 42 | + return BUTTON_LEFT; |
| 43 | + case Button.JoypadRight: |
| 44 | + return BUTTON_RIGHT; |
| 45 | + case Button.JoypadUp: |
| 46 | + return BUTTON_UP; |
| 47 | + case Button.JoypadDown: |
| 48 | + return BUTTON_DOWN; |
| 49 | + case Button.PointerPrimary: |
| 50 | + return MOUSE_LEFT; |
| 51 | + case Button.PointerSecondary: |
| 52 | + return MOUSE_RIGHT; |
| 53 | + default: |
| 54 | + return 0; |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +export class Wasm4 implements ONes { |
| 59 | + static new(_output_sample_rate: number): Wasm4 { |
| 60 | + return new Wasm4(); |
| 61 | + } |
| 62 | + |
| 63 | + #runtime = new Runtime(''); |
| 64 | + |
| 65 | + #frameNum = 0; |
| 66 | + #frameLen = WIDTH * HEIGHT * 4; |
| 67 | + #frameSpace = [0, this.#frameLen - 1]; |
| 68 | + #qoiLen = this.#frameLen; |
| 69 | + #qoiSpace = [this.#frameSpace[1] + 1, this.#frameSpace[1] + this.#qoiLen]; |
| 70 | + #deQoiLen = this.#frameLen; |
| 71 | + #deQoiSpace = [this.#qoiSpace[1] + 1, this.#qoiSpace[1] + this.#deQoiLen]; |
| 72 | + #mem = new Uint8Array(this.#deQoiSpace[1] + 1); |
| 73 | + |
| 74 | + #prevFrame = new Uint8ClampedArray(); |
| 75 | + #currentQoiFrameLen = 0; |
| 76 | + #currentDeQoiLen = 0; |
| 77 | + #ready = false; |
| 78 | + #sound = false; |
| 79 | + |
| 80 | + mem(): Uint8Array { |
| 81 | + return this.#mem; |
| 82 | + } |
| 83 | + width() { |
| 84 | + return WIDTH; |
| 85 | + } |
| 86 | + height() { |
| 87 | + return HEIGHT; |
| 88 | + } |
| 89 | + frame(qoi: boolean, qoiWholeFrame: boolean): number { |
| 90 | + if (qoi) { |
| 91 | + const currentFrame = new Uint8ClampedArray(this.#mem.buffer, this.#frameSpace[0], this.#frameLen); |
| 92 | + const [qoiFrame, partArr] = encodeQoiFrame(this.#prevFrame, currentFrame, this.width(), qoiWholeFrame); |
| 93 | + if (qoiFrame.length + partArr.length <= this.#qoiLen) { |
| 94 | + this.#currentQoiFrameLen = qoiFrame.length + partArr.length; |
| 95 | + new Uint8Array(this.#mem.buffer, this.#qoiSpace[0], this.#qoiLen).set(qoiFrame); |
| 96 | + new Uint8Array(this.#mem.buffer, this.#qoiSpace[0] + qoiFrame.byteLength, partArr.length).set(partArr); |
| 97 | + this.#prevFrame = currentFrame.slice(0); |
| 98 | + } |
| 99 | + } else { |
| 100 | + this.#prevFrame = new Uint8ClampedArray(); |
| 101 | + } |
| 102 | + return this.#frameSpace[0]; |
| 103 | + } |
| 104 | + frame_len(): number { |
| 105 | + return this.#frameLen; |
| 106 | + } |
| 107 | + qoi_frame(): number { |
| 108 | + return this.#qoiSpace[0]; |
| 109 | + } |
| 110 | + qoi_frame_len(): number { |
| 111 | + return this.#currentQoiFrameLen; |
| 112 | + } |
| 113 | + decode_qoi(bytes: Uint8Array): number { |
| 114 | + const frame = decodeQoiFrame(bytes.buffer).data; |
| 115 | + this.#currentDeQoiLen = frame.length; |
| 116 | + new Uint8Array(this.#mem.buffer, this.#deQoiSpace[0], this.#deQoiLen).set(frame); |
| 117 | + return this.#deQoiSpace[0]; |
| 118 | + } |
| 119 | + decode_qoi_len(): number { |
| 120 | + return this.#currentDeQoiLen; |
| 121 | + } |
| 122 | + async load_rom(bytes: Uint8Array) { |
| 123 | + if (!this.#ready) await this.#runtime.init(); |
| 124 | + await this.#runtime.load(bytes); |
| 125 | + this.#runtime.start(); |
| 126 | + } |
| 127 | + clock_frame(): number { |
| 128 | + this.#runtime.update(); |
| 129 | + this.#runtime.composite(); |
| 130 | + const { gl } = this.#runtime.compositor; |
| 131 | + const currentFrame = new Uint8ClampedArray(this.#mem.buffer, this.#frameSpace[0], this.#frameLen); |
| 132 | + gl.readPixels(0, 0, WIDTH, HEIGHT, this.#runtime.compositor.gl.RGBA, gl.UNSIGNED_BYTE, currentFrame); |
| 133 | + return this.#frameNum++; |
| 134 | + } |
| 135 | + audio_callback(out: Float32Array) { |
| 136 | + if (!this.#sound) return; |
| 137 | + const left = new Float32Array(out.length); |
| 138 | + const right = new Float32Array(out.length); |
| 139 | + this.#runtime.apu.process([[]], [[left, right]], {}); |
| 140 | + for (let i = 0; i < out.length; i++) { |
| 141 | + out[i] = (left[i] + right[i]) / 2; |
| 142 | + } |
| 143 | + } |
| 144 | + handle_button_event(player: Player, button: Button, pressed: boolean) { |
| 145 | + if (button === Button.Start && pressed) { |
| 146 | + switch (this.#runtime.pauseState) { |
| 147 | + case 0: |
| 148 | + this.#runtime.pauseState = PAUSE_REBOOTING; |
| 149 | + return; |
| 150 | + case PAUSE_REBOOTING: |
| 151 | + this.#runtime.pauseState = 0; |
| 152 | + return; |
| 153 | + } |
| 154 | + } |
| 155 | + const x = this.#runtime.data.getInt16(ADDR_MOUSE_X, true); |
| 156 | + const y = this.#runtime.data.getInt16(ADDR_MOUSE_Y, true); |
| 157 | + const btn = getButtonBitFlag(button); |
| 158 | + const playerIdx = getPlayerIdx(player); |
| 159 | + const mouseButtons = this.#runtime.data.getUint8(ADDR_MOUSE_BUTTONS); |
| 160 | + const buttons = this.#runtime.data.getUint8(ADDR_GAMEPAD1 + playerIdx); |
| 161 | + |
| 162 | + if (button === Button.PointerPrimary || button === Button.PointerSecondary) { |
| 163 | + this.#runtime.setMouse(x, y, pressed ? mouseButtons | btn : mouseButtons & ~btn); |
| 164 | + return; |
| 165 | + } |
| 166 | + this.#runtime.setGamepad(playerIdx, pressed ? buttons | btn : buttons & ~btn); |
| 167 | + } |
| 168 | + handle_motion_event(_player: Player, x: number, y: number, _dx: number, _dy: number) { |
| 169 | + this.#runtime.setMouse(x, y, 0); |
| 170 | + } |
| 171 | + state(): Uint8Array { |
| 172 | + return new Uint8Array(this.#runtime.memory.buffer); |
| 173 | + } |
| 174 | + load_state(state: Uint8Array) { |
| 175 | + new Uint8Array(this.#runtime.memory.buffer).set(state); |
| 176 | + } |
| 177 | + async reset() { |
| 178 | + const wasmBuffer = this.#runtime.wasmBuffer; |
| 179 | + if (wasmBuffer) { |
| 180 | + this.#runtime.reset(true); |
| 181 | + this.#runtime.pauseState = PAUSE_REBOOTING; |
| 182 | + await this.#runtime.load(wasmBuffer); |
| 183 | + this.#runtime.pauseState = 0; |
| 184 | + this.#runtime.start(); |
| 185 | + } |
| 186 | + } |
| 187 | + set_sound(enabled: boolean) { |
| 188 | + this.#sound = enabled; |
| 189 | + } |
| 190 | + sound(): boolean { |
| 191 | + return this.#sound; |
| 192 | + } |
| 193 | + ram_map(): Uint32Array { |
| 194 | + const lastArea = this.#runtime.framebuffer.bytes; |
| 195 | + return new Uint32Array([lastArea.byteOffset + lastArea.length, 0xffff]); |
| 196 | + } |
| 197 | + ram(): Uint8Array { |
| 198 | + const lastArea = this.#runtime.framebuffer.bytes; |
| 199 | + return new Uint8Array(this.#runtime.data.buffer, lastArea.byteOffset + lastArea.length); |
| 200 | + } |
| 201 | + read_ram(addr: number): number { |
| 202 | + return this.#runtime.data.getUint8(addr); |
| 203 | + } |
| 204 | + write_ram(addr: number, val: number) { |
| 205 | + return this.#runtime.data.setUint8(addr, val); |
| 206 | + } |
| 207 | + /** |
| 208 | + * @ignore |
| 209 | + */ |
| 210 | + free() { |
| 211 | + // no body |
| 212 | + } |
| 213 | +} |
0 commit comments