From e3f188abe95ab537cca4fc69e27fc476be85c3d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Magalh=C3=A3es?= <joamag@gmail.com> Date: Sun, 3 Jul 2022 02:19:40 +0100 Subject: [PATCH] feat: initial web support --- README.md | 19 +++++++++++++++++++ src/gb.rs | 36 ++++++++++++++++++++++-------------- 2 files changed, 41 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 92442418..3d3d479a 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,25 @@ A Game Boy emulator that is written in Rust 🦀. +## Build + +### WASM for Node.js + +```bash +cargo install wasm-pack +wasm-pack build --release --target=nodejs -- --features wasm +``` + +### WASM for Web + +```bash +cargo install wasm-pack +wasm-pack build --release --target=web --out-dir=examples/web/lib -- --features wasm +cd examples/web +npm install && npm run build +cd dist && python3 -m http.server +``` + ## Inspiration ### Documentation diff --git a/src/gb.rs b/src/gb.rs index f1ae2ce1..e918ba68 100644 --- a/src/gb.rs +++ b/src/gb.rs @@ -5,11 +5,17 @@ use crate::{ util::read_file, }; +#[cfg(feature = "wasm")] +use wasm_bindgen::prelude::*; + +#[cfg_attr(feature = "wasm", wasm_bindgen)] pub struct GameBoy { cpu: Cpu, } +#[cfg_attr(feature = "wasm", wasm_bindgen)] impl GameBoy { + #[cfg_attr(feature = "wasm", wasm_bindgen(constructor))] pub fn new() -> GameBoy { let ppu = Ppu::new(); let mmu = Mmu::new(ppu); @@ -31,6 +37,22 @@ impl GameBoy { self.ppu().clock(cycles) } + pub fn load_rom(&mut self, path: &str) { + let data = read_file(path); + self.cpu.mmu().write_rom(0x0000, &data); + } + + pub fn load_boot(&mut self, path: &str) { + let data = read_file(path); + self.cpu.mmu().write_boot(0x0000, &data); + } + + pub fn load_boot_default(&mut self) { + self.load_boot("./res/dmg_rom.bin"); + } +} + +impl GameBoy { pub fn cpu(&mut self) -> &mut Cpu { &mut self.cpu } @@ -46,18 +68,4 @@ impl GameBoy { pub fn frame_buffer(&mut self) -> &Box<[u8; FRAME_BUFFER_SIZE]> { &(self.ppu().frame_buffer) } - - pub fn load_rom(&mut self, path: &str) { - let data = read_file(path); - self.cpu.mmu().write_rom(0x0000, &data); - } - - pub fn load_boot(&mut self, path: &str) { - let data = read_file(path); - self.cpu.mmu().write_boot(0x0000, &data); - } - - pub fn load_boot_default(&mut self) { - self.load_boot("./res/dmg_rom.bin"); - } } -- GitLab