Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ crate-type = ["cdylib", "rlib", "staticlib"]

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

[profile.release]
opt-level = "s"
Expand All @@ -31,6 +30,10 @@ split-debuginfo = "unpacked"
lto = false
incremental = true
opt-level = 0
panic = 'abort'

[dependencies]
linked_list_allocator = { version = "0.10", optional = true }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
libc = { version = "0.2.126", optional = true }
Expand All @@ -40,7 +43,7 @@ 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", "png"], optional = true }
ratatui-image = { version = "4.2.0", optional = true }
base64 = { version = "0.21.7", optional = true }


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

ffi-build:
# cargo install cbindgen
cargo build --release --no-default-features --features ffi
cargo rustc --release --no-default-features --features ffi --crate-type staticlib
cbindgen . -o gameboy.h --lang c

ffi-size:
Expand Down
7 changes: 7 additions & 0 deletions cbindgen.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# cbindgen.toml

[export]
exclude = ["memcpy", "memmove", "memset", "memcmp", "bzero"]

[defines]
"feature = ffi" = "FFI"
2 changes: 1 addition & 1 deletion examples/desktop/bin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ fn main() {
// TODO: Allow receive path by arguments
// let gb = Gameboy::new("./../../tests/cpu_instrs/cpu_instrs.gb");
// if let Ok((data, filepath)) = load_rom("./../the-machine.gb") {
if let Ok((data, filepath)) = load_rom("/Users/rapha/Documents/a/boyband/game/SuperWish/game.gb") {
if let Ok((data, filepath)) = load_rom("/Users/rapha/Downloads/dragon-ball.gbc") {
// if let Ok((data, filepath)) = load_rom("./../bakery.gb") {
let gb = Gameboy::new(data, Some(filepath));
gb.render(Desktop);
Expand Down
6 changes: 4 additions & 2 deletions gameboy.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ typedef struct ImageBuffer {
const uint8_t *data;
} ImageBuffer;

#if defined(FFI)
void init_allocator(uint8_t *heap_start, uintptr_t heap_size);
#endif

/**
* # Safety
*
Expand All @@ -39,5 +43,3 @@ void keydown(enum KeypadKey key);
void keyup(enum KeypadKey key);

struct ImageBuffer image(void);

const char *image_base64(void);
21 changes: 21 additions & 0 deletions src/cpu/core.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::cpu::registers::Registers;
use crate::cpu::{data, ld, misc, stack};
use crate::mmu::MemoryManagementUnit;
#[cfg(feature = "ffi")]
use alloc::vec::Vec;

#[allow(dead_code)]
pub enum Interrupt {
Expand All @@ -25,6 +27,7 @@ pub struct Cpu<'a> {
}

impl Cpu<'_> {
#[cfg(not(feature = "ffi"))]
pub fn new(data: Vec<u8>, file: Option<std::path::PathBuf>) -> Self {
let memory = MemoryManagementUnit::new_cgb(data, file).unwrap();
let registers = Registers::new(memory.gbmode);
Expand All @@ -40,6 +43,23 @@ impl Cpu<'_> {
_executed_operations: Vec::new(),
}
}

#[cfg(feature = "ffi")]
pub fn new(data: Vec<u8>, _file: Option<()>) -> Self {
let memory = MemoryManagementUnit::new_cgb(data, None).unwrap();
let registers = Registers::new(memory.gbmode);

Cpu {
registers,
memory,
ime: false,
setdi: 0,
setei: 0,
halt: 0,
stop: 0,
_executed_operations: Vec::new(),
}
}
fn mut_find_or_insert<T: PartialEq>(vec: &mut Vec<T>, val: T) -> &mut T {
if let Some(i) = vec.iter().position(|each| *each == val) {
&mut vec[i]
Expand Down Expand Up @@ -1115,6 +1135,7 @@ impl Cpu<'_> {
4
}
_ => {
#[cfg(not(feature = "ffi"))]
println!(
"Instruction at {:#01x} | {:#06x} | {} not implemented, stopping.",
op, op, op
Expand Down
3 changes: 3 additions & 0 deletions src/cpu/registers.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use crate::mode::GbMode;
#[cfg(not(feature = "ffi"))]
use std::fmt;
#[cfg(feature = "ffi")]
use core::fmt;

#[derive(Debug)]
pub struct Registers {
Expand Down
14 changes: 14 additions & 0 deletions src/gameboy.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::cpu::core::Cpu;
use crate::input::KeypadKey;
#[cfg(feature = "ffi")]
use alloc::vec::Vec;

pub struct Gameboy {
cpu: Cpu<'static>,
Expand Down Expand Up @@ -51,6 +53,7 @@ pub fn load_rom(filepath: &str) -> Result<(Vec<u8>, std::path::PathBuf), String>
pub const CYCLES: u32 = 70224;

impl Gameboy {
#[cfg(not(feature = "ffi"))]
pub fn new(data: Vec<u8>, filepath: Option<std::path::PathBuf>) -> Gameboy {
// let rom = load_rom();

Expand All @@ -62,6 +65,17 @@ impl Gameboy {

gb
}

#[cfg(feature = "ffi")]
pub fn new(data: Vec<u8>, _filepath: Option<()>) -> Gameboy {
let gb = Gameboy {
cpu: Cpu::new(data, None),
width: 160,
height: 144,
};

gb
}

pub fn render(self, render_mode: RenderMode) {
match render_mode {
Expand Down
5 changes: 5 additions & 0 deletions src/gpu/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
use crate::mode::GbMode;
#[cfg(not(feature = "ffi"))]
use std::cmp::Ordering;
#[cfg(feature = "ffi")]
use core::cmp::Ordering;
#[cfg(feature = "ffi")]
use alloc::boxed::Box;

const VRAM_SIZE: usize = 0x4000;
const VOAM_SIZE: usize = 0xA0;
Expand Down
Loading
Loading