Skip to content

Commit

Permalink
ffi go
Browse files Browse the repository at this point in the history
  • Loading branch information
raphamorim committed Jan 5, 2025
1 parent 47332e0 commit 3a2534e
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 26 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Cargo.lock
# These are backup files generated by rustfmt
**/*.rs.bk

main_static

.DS_Store
log
NOTES
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ edition = "2021"
[lib]
name = "gameboy"
path = "src/lib.rs"
crate-type = ["cdylib", "rlib"]
crate-type = ["cdylib", "rlib", "staticlib"]

[profile.release]
opt-level = "s"
Expand Down
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,11 @@ web-publish:
web-build:
cd web && npm run build

# cargo install cbindgen
ffi-build:
cargo build --release
cbindgen . -o gameboy.h --lang c

ffi:
cp target/release/libgameboy.a examples/ffi-go/
cd examples/ffi-go && go build main_static.go
Binary file added examples/ffi-go/libgameboy.a
Binary file not shown.
22 changes: 22 additions & 0 deletions examples/ffi-go/main_static.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package main

// NOTE: There should be NO space between the comments and the `import "C"` line.
// The -ldl is sometimes necessary to fix linker errors about `dlsym`.

/*
#cgo LDFLAGS: ./libgameboy.a -ldl
#include "../../gameboy.h"
#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))

// C.hello(str1)
// C.whisper(str2)
}
43 changes: 43 additions & 0 deletions gameboy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

#define CYCLES 70224

#define HEIGHT 144

#define WIDTH 160

enum KeypadKey {
Right,
Left,
Up,
Down,
A,
B,
Select,
Start,
};
typedef uint8_t KeypadKey;

typedef struct String String;

typedef struct ImageBuffer {
int32_t len;
const uint8_t *data;
} ImageBuffer;

void load_rom(const unsigned char *bytes, uintptr_t bytes_length);

void frame(void);

void keydown(KeypadKey key);

void keyup(KeypadKey key);

struct ImageBuffer image(void);

extern void log(struct String s);

extern void log_u32(uint32_t a);
2 changes: 1 addition & 1 deletion src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub struct Keypad {
}

#[derive(Copy, Clone)]
#[repr(C)]
#[repr(u8)]
pub enum KeypadKey {
Right,
Left,
Expand Down
25 changes: 1 addition & 24 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ pub async fn start() {
// TODO: Process GL
}

#[no_mangle]
pub static GAMEBOY: OnceLock<Mutex<Option<crate::gameboy::Gameboy>>> = OnceLock::new();
static GAMEBOY: OnceLock<Mutex<Option<crate::gameboy::Gameboy>>> = OnceLock::new();

#[cfg(target_vendor = "apple")]
unsafe impl Send for crate::gameboy::Gameboy {}
Expand Down Expand Up @@ -65,28 +64,6 @@ pub extern "C" fn keyup(key: KeypadKey) {
}
}

#[no_mangle]
pub extern "C" fn width() -> u32 {
if let Some(gb) = GAMEBOY.get() {
if let Ok(mut locked_gb) = gb.lock() {
return locked_gb.as_mut().unwrap().width;
}
}

0
}

#[no_mangle]
pub extern "C" fn height() -> u32 {
if let Some(gb) = GAMEBOY.get() {
if let Ok(mut locked_gb) = gb.lock() {
return locked_gb.as_mut().unwrap().height;
}
}

0
}

#[repr(C)]
pub struct ImageBuffer {
len: i32,
Expand Down

0 comments on commit 3a2534e

Please sign in to comment.