From 19ce9f0c27254959410fe2acab34d566a3cf282a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jo=C3=A3o=20Magalh=C3=A3es?= <joamag@gmail.com>
Date: Sun, 23 Jul 2023 23:50:42 +0100
Subject: [PATCH] chore: initial code support

---
 Cargo.toml                    |  2 +-
 frontends/libretro/Cargo.toml | 14 ++++++
 frontends/libretro/src/lib.rs | 86 +++++++++++++++++++++++++++++++++++
 frontends/sdl/Cargo.toml      |  1 +
 4 files changed, 102 insertions(+), 1 deletion(-)
 create mode 100644 frontends/libretro/Cargo.toml
 create mode 100644 frontends/libretro/src/lib.rs

diff --git a/Cargo.toml b/Cargo.toml
index d9f49161..20c91020 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -36,7 +36,7 @@ opt-level = 3
 codegen-units = 1
 
 [workspace]
-members = [".", "frontends/sdl"]
+members = [".", "frontends/libretro", "frontends/sdl"]
 
 [package.metadata.docs.rs]
 features = ["wasm", "gen-mock"]
diff --git a/frontends/libretro/Cargo.toml b/frontends/libretro/Cargo.toml
new file mode 100644
index 00000000..90024376
--- /dev/null
+++ b/frontends/libretro/Cargo.toml
@@ -0,0 +1,14 @@
+[package]
+name = "boytacean-libretro"
+version = "0.9.6"
+authors = ["João Magalhães <joamag@gmail.com>"]
+description = "A Lib Retro frontend for Boytacen"
+license = "Apache-2.0"
+keywords = ["gameboy", "emulator", "rust", "libretro"]
+edition = "2018"
+
+[lib]
+crate-type = ["cdylib"]
+
+[dependencies.boytacean]
+path = "../.."
diff --git a/frontends/libretro/src/lib.rs b/frontends/libretro/src/lib.rs
new file mode 100644
index 00000000..9f58a135
--- /dev/null
+++ b/frontends/libretro/src/lib.rs
@@ -0,0 +1,86 @@
+#![allow(clippy::uninlined_format_args)]
+
+use std::os::raw::{c_char, c_void};
+
+const RETRO_API_VERSION: u32 = 1;
+
+#[repr(C)]
+pub struct retro_system_info {
+    pub library_name: *const c_char,
+    pub library_version: *const c_char,
+    pub valid_extensions: *const c_char,
+    pub need_fullpath: bool,
+    pub block_extract: bool,
+}
+
+#[repr(C)]
+pub struct retro_game_geometry {
+    pub base_width: u32,
+    pub base_height: u32,
+    pub max_width: u32,
+    pub max_height: u32,
+    pub aspect_ratio: f32,
+}
+
+#[repr(C)]
+pub struct retro_system_timing {
+    pub fps: f64,
+    pub sample_rate: f64,
+}
+
+#[repr(C)]
+pub struct retro_system_api {
+    pub retro_api_version: u32,
+    pub retro_get_system_info: extern "C" fn(*mut retro_system_info),
+    pub retro_set_environment: extern "C" fn(extern "C" fn(u32, *const c_void), *const c_void),
+    pub retro_set_video_refresh: extern "C" fn(extern "C" fn()),
+    pub retro_set_audio_sample: extern "C" fn(extern "C" fn(i16)),
+    pub retro_set_audio_sample_batch: extern "C" fn(extern "C" fn(*const i16, usize)),
+    pub retro_set_input_poll: extern "C" fn(extern "C" fn()),
+    pub retro_set_input_state: extern "C" fn(extern "C" fn(u32, u32, u16, i16) -> i16),
+    // Add other Libretro core functions here as needed
+}
+
+#[no_mangle]
+extern "C" fn retro_get_system_info(info: *mut retro_system_info) {
+    unsafe {
+        (*info).library_name = "Boytacean\0".as_ptr() as *const c_char;
+        (*info).library_version = "0.9.6\0".as_ptr() as *const c_char;
+        (*info).valid_extensions = "gb\0gbc\0\0".as_ptr() as *const c_char;
+        (*info).need_fullpath = false;
+        (*info).block_extract = false;
+    }
+}
+
+#[no_mangle]
+extern "C" fn retro_set_environment(
+    _callback: extern "C" fn(u32, *const c_void),
+    _data: *const c_void,
+) {
+    // Set any environment variables or configuration options here if needed
+    // For example, you might handle system RAM allocation using this function
+}
+
+// Add other core functions here as needed
+
+// Call this function to initialize the libretro core
+#[no_mangle]
+pub extern "C" fn retro_api_version() -> u32 {
+    RETRO_API_VERSION
+}
+
+// Define other core functions here as needed
+
+// Add your Game Boy emulator code here
+// ...
+
+// Entry point for the libretro core
+#[no_mangle]
+pub extern "C" fn retro_init() {}
+
+// Other libretro core functions
+// ...
+
+// Entry point to deinitialize the libretro core
+#[no_mangle]
+pub extern "C" fn retro_deinit() {}
diff --git a/frontends/sdl/Cargo.toml b/frontends/sdl/Cargo.toml
index 9d884f80..6032c921 100644
--- a/frontends/sdl/Cargo.toml
+++ b/frontends/sdl/Cargo.toml
@@ -6,6 +6,7 @@ description = "An SDL frontend for Boytacen"
 license = "Apache-2.0"
 keywords = ["gameboy", "emulator", "rust", "sdl"]
 edition = "2018"
+readme = "README.md"
 
 [features]
 slow = []
-- 
GitLab