Skip to content

Commit 02d759b

Browse files
committed
no-std
1 parent 80ca390 commit 02d759b

File tree

19 files changed

+541
-134
lines changed

19 files changed

+541
-134
lines changed

Cargo.lock

Lines changed: 19 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ crate-type = ["cdylib", "rlib", "staticlib"]
1414

1515
[features]
1616
default = ["libc", "glutin", "gl" ,"ratatui" ,"icy_sixel", "image", "ratatui-image"]
17-
ffi = []
18-
ffi-base64 = ["image", "base64"]
17+
ffi = ["linked_list_allocator"]
1918

2019
[profile.release]
2120
opt-level = "s"
@@ -31,6 +30,10 @@ split-debuginfo = "unpacked"
3130
lto = false
3231
incremental = true
3332
opt-level = 0
33+
panic = 'abort'
34+
35+
[dependencies]
36+
linked_list_allocator = { version = "0.10", optional = true }
3437

3538
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
3639
libc = { version = "0.2.126", optional = true }
@@ -40,7 +43,7 @@ ratatui = { version = "^0.29.0", features = ["crossterm"], optional = true }
4043
icy_sixel = { version = "^0.1.1", optional = true }
4144
image = { version = "^0.25.1", default-features = false, features = ["jpeg", "png"], optional = true }
4245
ratatui-image = { version = "4.2.0", optional = true }
43-
base64 = { version = "0.21.7", optional = true }
46+
4447

4548
[target.'cfg(target_arch = "wasm32")'.dependencies]
4649
js-sys = "0.3.59"

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ web-build:
4444

4545
ffi-build:
4646
# cargo install cbindgen
47-
cargo build --release --no-default-features --features ffi
47+
cargo rustc --release --no-default-features --features ffi --crate-type staticlib
4848
cbindgen . -o gameboy.h --lang c
4949

5050
ffi-size:

cbindgen.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# cbindgen.toml
2+
3+
[export]
4+
exclude = ["memcpy", "memmove", "memset", "memcmp", "bzero"]
5+
6+
[defines]
7+
"feature = ffi" = "FFI"

examples/desktop/bin.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ fn main() {
66
// TODO: Allow receive path by arguments
77
// let gb = Gameboy::new("./../../tests/cpu_instrs/cpu_instrs.gb");
88
// if let Ok((data, filepath)) = load_rom("./../the-machine.gb") {
9-
if let Ok((data, filepath)) = load_rom("/Users/rapha/Documents/a/boyband/game/SuperWish/game.gb") {
9+
if let Ok((data, filepath)) = load_rom("/Users/rapha/Downloads/dragon-ball.gbc") {
1010
// if let Ok((data, filepath)) = load_rom("./../bakery.gb") {
1111
let gb = Gameboy::new(data, Some(filepath));
1212
gb.render(Desktop);

gameboy.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ typedef struct ImageBuffer {
2525
const uint8_t *data;
2626
} ImageBuffer;
2727

28+
#if defined(FFI)
29+
void init_allocator(uint8_t *heap_start, uintptr_t heap_size);
30+
#endif
31+
2832
/**
2933
* # Safety
3034
*
@@ -39,5 +43,3 @@ void keydown(enum KeypadKey key);
3943
void keyup(enum KeypadKey key);
4044

4145
struct ImageBuffer image(void);
42-
43-
const char *image_base64(void);

src/cpu/core.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use crate::cpu::registers::Registers;
22
use crate::cpu::{data, ld, misc, stack};
33
use crate::mmu::MemoryManagementUnit;
4+
#[cfg(feature = "ffi")]
5+
use alloc::vec::Vec;
46

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

2729
impl Cpu<'_> {
30+
#[cfg(not(feature = "ffi"))]
2831
pub fn new(data: Vec<u8>, file: Option<std::path::PathBuf>) -> Self {
2932
let memory = MemoryManagementUnit::new_cgb(data, file).unwrap();
3033
let registers = Registers::new(memory.gbmode);
@@ -40,6 +43,23 @@ impl Cpu<'_> {
4043
_executed_operations: Vec::new(),
4144
}
4245
}
46+
47+
#[cfg(feature = "ffi")]
48+
pub fn new(data: Vec<u8>, _file: Option<()>) -> Self {
49+
let memory = MemoryManagementUnit::new_cgb(data, None).unwrap();
50+
let registers = Registers::new(memory.gbmode);
51+
52+
Cpu {
53+
registers,
54+
memory,
55+
ime: false,
56+
setdi: 0,
57+
setei: 0,
58+
halt: 0,
59+
stop: 0,
60+
_executed_operations: Vec::new(),
61+
}
62+
}
4363
fn mut_find_or_insert<T: PartialEq>(vec: &mut Vec<T>, val: T) -> &mut T {
4464
if let Some(i) = vec.iter().position(|each| *each == val) {
4565
&mut vec[i]
@@ -1115,6 +1135,7 @@ impl Cpu<'_> {
11151135
4
11161136
}
11171137
_ => {
1138+
#[cfg(not(feature = "ffi"))]
11181139
println!(
11191140
"Instruction at {:#01x} | {:#06x} | {} not implemented, stopping.",
11201141
op, op, op

src/cpu/registers.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
use crate::mode::GbMode;
2+
#[cfg(not(feature = "ffi"))]
23
use std::fmt;
4+
#[cfg(feature = "ffi")]
5+
use core::fmt;
36

47
#[derive(Debug)]
58
pub struct Registers {

src/gameboy.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use crate::cpu::core::Cpu;
22
use crate::input::KeypadKey;
3+
#[cfg(feature = "ffi")]
4+
use alloc::vec::Vec;
35

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

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

@@ -62,6 +65,17 @@ impl Gameboy {
6265

6366
gb
6467
}
68+
69+
#[cfg(feature = "ffi")]
70+
pub fn new(data: Vec<u8>, _filepath: Option<()>) -> Gameboy {
71+
let gb = Gameboy {
72+
cpu: Cpu::new(data, None),
73+
width: 160,
74+
height: 144,
75+
};
76+
77+
gb
78+
}
6579

6680
pub fn render(self, render_mode: RenderMode) {
6781
match render_mode {

src/gpu/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
use crate::mode::GbMode;
2+
#[cfg(not(feature = "ffi"))]
23
use std::cmp::Ordering;
4+
#[cfg(feature = "ffi")]
5+
use core::cmp::Ordering;
6+
#[cfg(feature = "ffi")]
7+
use alloc::boxed::Box;
38

49
const VRAM_SIZE: usize = 0x4000;
510
const VOAM_SIZE: usize = 0xA0;

0 commit comments

Comments
 (0)