Skip to content
Snippets Groups Projects
Verified Commit 43cb8982 authored by João Magalhães's avatar João Magalhães :rocket:
Browse files

feat: initial WASM support

parent 240446b4
No related branches found
No related tags found
1 merge request!4Support for WASM 🤖
Pipeline #633 passed
......@@ -10,8 +10,12 @@ edition = "2018"
[lib]
crate-type = ["cdylib", "rlib"]
[features]
wasm = ["wasm-bindgen"]
[dependencies]
getrandom = { version = "0.2", features = ["js"] }
wasm-bindgen = { version = "0.2", optional = true }
[profile.release]
debug = false
......
......@@ -23,6 +23,15 @@ The work of this emulator was inspired/started by [jc-chip8](https://github.com/
* Full compliant with test CHIP-8 ROMs
* RAM snapshot saving and loading
## Build
### WASM
```bash
cargo install wasm-pack
wasm-pack build --release --target=nodejs -- --features wasm
```
## Reason
And... yes this is the real inspiration behind the emulator's name:
......
......@@ -2,7 +2,7 @@ use std::fmt::Display;
use crate::{chip8::Chip8, util::random};
#[cfg(feature = "web")]
#[cfg(feature = "wasm")]
use wasm_bindgen::prelude::*;
/// The width of the screen in pixels.
......@@ -50,7 +50,7 @@ static FONT_SET: [u8; 80] = [
0xf0, 0x80, 0xf0, 0x80, 0x80, // F
];
#[cfg_attr(feature = "web", wasm_bindgen)]
#[cfg_attr(feature = "wasm", wasm_bindgen)]
pub struct Chip8Classic {
vram: [u8; SCREEN_PIXEL_WIDTH * SCREEN_PIXEL_HEIGHT],
ram: [u8; RAM_SIZE],
......@@ -66,7 +66,6 @@ pub struct Chip8Classic {
keys: [bool; NUM_KEYS],
}
#[cfg_attr(feature = "web", wasm_bindgen)]
impl Chip8 for Chip8Classic {
fn name(&self) -> &str {
"classic"
......@@ -152,8 +151,9 @@ impl Chip8 for Chip8Classic {
}
}
#[cfg_attr(feature = "wasm", wasm_bindgen)]
impl Chip8Classic {
#[cfg_attr(feature = "web", wasm_bindgen(constructor))]
#[cfg_attr(feature = "wasm", wasm_bindgen(constructor))]
pub fn new() -> Chip8Classic {
let mut chip8 = Chip8Classic {
vram: [0u8; SCREEN_PIXEL_WIDTH * SCREEN_PIXEL_HEIGHT],
......
......@@ -2,6 +2,9 @@ use std::io::{Cursor, Read};
use crate::{chip8::Chip8, util::random};
#[cfg(feature = "wasm")]
use wasm_bindgen::prelude::*;
pub const DISPLAY_WIDTH: usize = 64;
pub const DISPLAY_HEIGHT: usize = 32;
......@@ -47,7 +50,6 @@ pub struct Chip8Neo {
last_key: u8,
}
#[cfg_attr(feature = "web", wasm_bindgen)]
impl Chip8 for Chip8Neo {
fn name(&self) -> &str {
"neo"
......@@ -306,8 +308,9 @@ impl Chip8 for Chip8Neo {
}
}
#[cfg_attr(feature = "wasm", wasm_bindgen)]
impl Chip8Neo {
#[cfg_attr(feature = "web", wasm_bindgen(constructor))]
#[cfg_attr(feature = "wasm", wasm_bindgen(constructor))]
pub fn new() -> Chip8Neo {
let mut chip8 = Chip8Neo {
ram: [0u8; RAM_SIZE],
......@@ -359,3 +362,9 @@ impl Chip8Neo {
}
}
}
impl Default for Chip8Neo {
fn default() -> Chip8Neo {
Chip8Neo::new()
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment