Skip to content

Commit

Permalink
update ffi and remove unnecessary stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
raphamorim committed Feb 6, 2025
1 parent 4f10d87 commit e055f62
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 27 deletions.
18 changes: 11 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ name = "gameboy"
path = "src/lib.rs"
crate-type = ["cdylib", "rlib", "staticlib"]

[features]
default = ["libc", "glutin", "gl" ,"ratatui" ,"icy_sixel", "image", "ratatui-image"]
ffi = []

[profile.release]
opt-level = "s"
lto = "thin"
Expand All @@ -28,13 +32,13 @@ incremental = true
opt-level = 0

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
libc = { version = "0.2.126" }
glutin = { version = "0.28.0" }
gl = { version = "0.14.0" }
ratatui = { version = "^0.29.0", features = ["crossterm"] }
icy_sixel = { version = "^0.1.1" }
image = { version = "^0.25.1", default-features = false, features = ["jpeg"] }
ratatui-image = "4.2.0"
libc = { version = "0.2.126", optional = true }
glutin = { version = "0.28.0", optional = true }
gl = { version = "0.14.0", optional = true }
ratatui = { version = "^0.29.0", features = ["crossterm"], optional = true }
icy_sixel = { version = "^0.1.1", optional = true }
image = { version = "^0.25.1", default-features = false, features = ["jpeg"], optional = true }
ratatui-image = { version = "4.2.0", optional = true }

[target.'cfg(target_arch = "wasm32")'.dependencies]
js-sys = "0.3.59"
Expand Down
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,12 @@ web-build:

ffi-build:
cargo install cbindgen
cargo build --release
cargo build --release --no-default-features --features ffi
cbindgen . -o gameboy.h --lang c

ffi-size:
du -k ./target/release/libgameboy.a

ffi:
cp target/release/libgameboy.a examples/ffi-go/
cd examples/ffi-go && go build main_static.go
19 changes: 12 additions & 7 deletions examples/ffi-go/main_static.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,19 @@ package main
#include <stdlib.h>
*/
import "C"
// import "unsafe"

func main() {
// str1 := C.CString("world")
// str2 := C.CString("this is code from the static library")
// defer C.free(unsafe.Pointer(str1))
// defer C.free(unsafe.Pointer(str2))
if len(os.Args) <= 1 {
fmt.Fprintf(os.Stderr, "Please provide the game path\ne.g: $ boyband my-game.gb")
os.Exit(0)
}

// C.hello(str1)
// C.whisper(str2)
romPath := os.Args[1]
romData, romErr := loadROMData(romPath)
if romErr != nil {
fmt.Fprintf(os.Stderr, "Error while loading the rom file: %s", romErr)
os.Exit(1)
}

C.load((*C.uchar)(&romData[0]), C.size_t(len(romData)))
}
7 changes: 6 additions & 1 deletion gameboy.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ typedef struct ImageBuffer {
const uint8_t *data;
} ImageBuffer;

void load_rom(const unsigned char *bytes, uintptr_t bytes_length);
/**
* # Safety
*
* This function is not safe due to from_raw_parts.
*/
void load(const unsigned char *bytes, uintptr_t bytes_length);

void frame(void);

Expand Down
18 changes: 9 additions & 9 deletions src/gameboy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ pub use self::Target::{GameBoy, GameBoyColor, SuperGameBoy};

#[derive(PartialEq, Eq, Debug, Copy, Clone, Default)]
pub enum RenderMode {
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(any(target_arch = "wasm32", feature = "ffi")))]
#[default]
Desktop,
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(any(target_arch = "wasm32", feature = "ffi")))]
Terminal,
#[cfg(target_arch = "wasm32")]
#[cfg(any(target_arch = "wasm32", feature = "ffi"))]
#[default]
WebAssembly,
}
Expand All @@ -28,7 +28,7 @@ pub enum Target {
SuperGameBoy,
}

#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(any(target_arch = "wasm32", feature = "ffi")))]
pub fn load_rom(filepath: &str) -> Result<(Vec<u8>, std::path::PathBuf), String> {
use std::fs::File;
use std::io::Read;
Expand Down Expand Up @@ -65,22 +65,22 @@ impl Gameboy {

pub fn render(self, render_mode: RenderMode) {
match render_mode {
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(any(target_arch = "wasm32", feature = "ffi")))]
RenderMode::Desktop => {
self.render_desktop();
}
#[cfg(target_arch = "wasm32")]
#[cfg(any(target_arch = "wasm32", feature = "ffi"))]
RenderMode::WebAssembly => {
// crate::screen::web::render();
}
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(any(target_arch = "wasm32", feature = "ffi")))]
RenderMode::Terminal => {
self.render_terminal();
}
}
}

#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(any(target_arch = "wasm32", feature = "ffi")))]
pub fn render_desktop(mut self) {
use crate::screen::desktop::*;

Expand Down Expand Up @@ -131,7 +131,7 @@ impl Gameboy {
});
}

#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(any(target_arch = "wasm32", feature = "ffi")))]
pub fn render_terminal(self) {
use crate::screen::tui;

Expand Down
5 changes: 3 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::sync::Mutex;
use std::sync::OnceLock;

#[cfg(target_arch = "wasm32")]
#[cfg(all(target_arch = "wasm32", feature = "ffi"))]
use wasm_bindgen::prelude::*;

pub mod cpu;
Expand All @@ -11,6 +11,7 @@ mod input;
mod mbc;
mod mmu;
mod mode;
#[cfg(not(feature = "ffi"))]
mod screen;

pub use crate::input::KeypadKey;
Expand All @@ -33,7 +34,7 @@ unsafe impl Sync for crate::gameboy::Gameboy {}
///
/// This function is not safe due to from_raw_parts.
#[no_mangle]
pub unsafe extern "C" fn load_rom(bytes: *const std::ffi::c_uchar, bytes_length: usize) {
pub unsafe extern "C" fn load(bytes: *const std::ffi::c_uchar, bytes_length: usize) {
let bytes = std::slice::from_raw_parts(bytes, bytes_length);
let bytes: Vec<u8> = Vec::from(bytes);
GAMEBOY
Expand Down

0 comments on commit e055f62

Please sign in to comment.