2020-02-26 22:08:21 +00:00
|
|
|
/// JNI Bindings for rustboyadvance
|
2020-02-23 21:05:09 +00:00
|
|
|
///
|
2020-02-22 22:25:50 +00:00
|
|
|
use std::cell::RefCell;
|
|
|
|
use std::os::raw::c_void;
|
2020-02-26 22:08:21 +00:00
|
|
|
use std::path::Path;
|
2020-02-22 22:25:50 +00:00
|
|
|
use std::rc::Rc;
|
2020-02-26 22:08:21 +00:00
|
|
|
use std::sync::{Mutex, MutexGuard};
|
2020-02-23 21:05:09 +00:00
|
|
|
|
2020-02-26 22:08:21 +00:00
|
|
|
use jni::objects::*;
|
|
|
|
use jni::sys::*;
|
2020-02-23 21:05:09 +00:00
|
|
|
use jni::{JNIEnv, JavaVM};
|
2020-02-22 22:25:50 +00:00
|
|
|
|
|
|
|
#[macro_use]
|
|
|
|
extern crate log;
|
|
|
|
|
2020-02-23 21:05:09 +00:00
|
|
|
#[cfg(target_os = "android")]
|
|
|
|
use android_log;
|
|
|
|
#[cfg(not(target_os = "android"))]
|
2020-02-22 22:25:50 +00:00
|
|
|
use env_logger;
|
|
|
|
|
|
|
|
use rustboyadvance_ng::prelude::*;
|
|
|
|
|
|
|
|
struct Hardware {
|
2020-02-26 22:08:21 +00:00
|
|
|
jvm: JavaVM,
|
2020-02-23 21:05:09 +00:00
|
|
|
// frame_buffer: [u32; DISPLAY_WIDTH * DISPLAY_HEIGHT],
|
|
|
|
key_state: u16,
|
2020-02-22 22:25:50 +00:00
|
|
|
}
|
|
|
|
|
2020-02-23 21:05:09 +00:00
|
|
|
impl VideoInterface for Hardware {}
|
|
|
|
impl AudioInterface for Hardware {}
|
|
|
|
impl InputInterface for Hardware {
|
|
|
|
fn poll(&mut self) -> u16 {
|
|
|
|
self.key_state
|
2020-02-22 22:25:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-26 22:08:21 +00:00
|
|
|
struct Context {
|
2020-02-22 22:25:50 +00:00
|
|
|
hwif: Rc<RefCell<Hardware>>,
|
|
|
|
gba: GameBoyAdvance,
|
|
|
|
}
|
|
|
|
|
2020-02-23 21:05:09 +00:00
|
|
|
static mut DID_LOAD: bool = false;
|
|
|
|
|
2020-02-26 22:08:21 +00:00
|
|
|
const NATIVE_EXCEPTION_CLASS: &'static str =
|
|
|
|
"com/mrmichel/rustboyadvance/EmulatorBindings/NativeBindingException";
|
|
|
|
|
|
|
|
unsafe fn internal_open_context(
|
|
|
|
env: &JNIEnv,
|
|
|
|
bios: jbyteArray,
|
|
|
|
rom: jbyteArray,
|
|
|
|
save_file: JString,
|
|
|
|
) -> Result<Context, String> {
|
|
|
|
let bios = env
|
|
|
|
.convert_byte_array(bios)
|
|
|
|
.map_err(|e| format!("could not get bios buffer, error {}", e))?
|
|
|
|
.into_boxed_slice();
|
|
|
|
let rom = env
|
|
|
|
.convert_byte_array(rom)
|
|
|
|
.map_err(|e| format!("could not get rom buffer, error {}", e))?
|
|
|
|
.into_boxed_slice();
|
|
|
|
let save_file: String = env
|
|
|
|
.get_string(save_file)
|
|
|
|
.map_err(|_| String::from("could not get save path"))?
|
|
|
|
.into();
|
|
|
|
|
|
|
|
let gamepak = GamepakBuilder::new()
|
|
|
|
.take_buffer(rom)
|
|
|
|
.save_path(&Path::new(&save_file))
|
|
|
|
.build()
|
|
|
|
.map_err(|e| format!("failed to load rom, gba result: {:?}", e))?;
|
|
|
|
|
|
|
|
info!("Loaded ROM file {:?}", gamepak.header);
|
|
|
|
|
|
|
|
let hw = Hardware {
|
|
|
|
jvm: env.get_java_vm().unwrap(),
|
|
|
|
key_state: 0xffff,
|
2020-02-23 21:05:09 +00:00
|
|
|
};
|
2020-02-26 22:08:21 +00:00
|
|
|
let hw = Rc::new(RefCell::new(hw));
|
|
|
|
|
|
|
|
let gba = GameBoyAdvance::new(bios, gamepak, hw.clone(), hw.clone(), hw.clone());
|
|
|
|
|
|
|
|
debug!("creating context");
|
|
|
|
let context = Context {
|
|
|
|
gba: gba,
|
|
|
|
hwif: hw.clone(),
|
2020-02-23 21:05:09 +00:00
|
|
|
};
|
2020-02-26 22:08:21 +00:00
|
|
|
|
|
|
|
Ok(context)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn save_state(env: &JNIEnv, gba: &mut GameBoyAdvance) -> Result<jbyteArray, String> {
|
|
|
|
let saved_state = gba
|
|
|
|
.save_state()
|
|
|
|
.map_err(|e| format!("failed to serielize state, error: {:?}", e))?;
|
|
|
|
let byte_array = env
|
|
|
|
.byte_array_from_slice(&saved_state)
|
|
|
|
.map_err(|e| format!("failed to create byte array, error: {:?}", e))?;
|
|
|
|
Ok(byte_array)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn load_state(env: &JNIEnv, gba: &mut GameBoyAdvance, state: jbyteArray) -> Result<(), String> {
|
|
|
|
let state = env
|
|
|
|
.convert_byte_array(state)
|
|
|
|
.map_err(|e| format!("failed to convert byte array, error: {:?}", e))?;
|
|
|
|
gba.restore_state(&state)
|
|
|
|
.map_err(|e| format!("failed to restore state, error: {:?}", e))
|
2020-02-23 21:05:09 +00:00
|
|
|
}
|
|
|
|
|
2020-02-22 22:25:50 +00:00
|
|
|
#[allow(non_snake_case)]
|
2020-02-23 21:05:09 +00:00
|
|
|
pub mod bindings {
|
2020-02-22 22:25:50 +00:00
|
|
|
use super::*;
|
|
|
|
|
2020-02-26 22:08:21 +00:00
|
|
|
unsafe fn lock_ctx<'a>(ctx: jlong) -> MutexGuard<'a, Context> {
|
|
|
|
(*(ctx as *mut Mutex<Context>)).lock().unwrap()
|
|
|
|
}
|
2020-02-22 22:25:50 +00:00
|
|
|
|
2020-02-23 21:05:09 +00:00
|
|
|
#[no_mangle]
|
|
|
|
pub unsafe extern "C" fn JNI_OnLoad(vm: *mut JavaVM, _reserved: *mut c_void) -> jint {
|
|
|
|
if DID_LOAD {
|
|
|
|
return JNI_VERSION_1_6;
|
|
|
|
}
|
|
|
|
#[cfg(target_os = "android")]
|
|
|
|
android_log::init("EmulatorBindings").unwrap();
|
|
|
|
#[cfg(not(target_os = "android"))]
|
|
|
|
env_logger::init();
|
2020-02-22 22:25:50 +00:00
|
|
|
|
2020-02-23 21:05:09 +00:00
|
|
|
debug!("library loaded and logger initialized!");
|
|
|
|
debug!("JVM: {:?}", vm);
|
2020-02-22 22:25:50 +00:00
|
|
|
|
2020-02-23 21:05:09 +00:00
|
|
|
DID_LOAD = true;
|
2020-02-22 22:25:50 +00:00
|
|
|
|
|
|
|
JNI_VERSION_1_6
|
|
|
|
}
|
|
|
|
|
|
|
|
#[no_mangle]
|
2020-02-23 21:05:09 +00:00
|
|
|
pub unsafe extern "C" fn Java_com_mrmichel_rustboyadvance_EmulatorBindings_openEmulator(
|
2020-02-22 22:25:50 +00:00
|
|
|
env: JNIEnv,
|
2020-02-26 22:08:21 +00:00
|
|
|
_obj: JClass,
|
|
|
|
bios: jbyteArray,
|
|
|
|
rom: jbyteArray,
|
|
|
|
save_file: JString,
|
|
|
|
) -> jlong {
|
|
|
|
match internal_open_context(&env, bios, rom, save_file) {
|
|
|
|
Ok(ctx) => Box::into_raw(Box::new(Mutex::new(ctx))) as jlong,
|
|
|
|
Err(msg) => {
|
|
|
|
env.throw_new(NATIVE_EXCEPTION_CLASS, msg).unwrap();
|
|
|
|
0
|
2020-02-23 21:05:09 +00:00
|
|
|
}
|
2020-02-22 22:25:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[no_mangle]
|
2020-02-23 21:05:09 +00:00
|
|
|
pub unsafe extern "C" fn Java_com_mrmichel_rustboyadvance_EmulatorBindings_closeEmulator(
|
|
|
|
_env: JNIEnv,
|
2020-02-26 22:08:21 +00:00
|
|
|
_obj: JClass,
|
|
|
|
ctx: jlong,
|
2020-02-22 22:25:50 +00:00
|
|
|
) {
|
2020-02-26 22:08:21 +00:00
|
|
|
info!("destroying context {:#x}", ctx);
|
|
|
|
// consume the wrapped content
|
|
|
|
let _ = Box::from_raw(ctx as *mut Mutex<Context>);
|
2020-02-22 22:25:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[no_mangle]
|
2020-02-23 21:05:09 +00:00
|
|
|
pub unsafe extern "C" fn Java_com_mrmichel_rustboyadvance_EmulatorBindings_runFrame(
|
2020-02-22 22:25:50 +00:00
|
|
|
env: JNIEnv,
|
2020-02-26 22:08:21 +00:00
|
|
|
_obj: JClass,
|
|
|
|
ctx: jlong,
|
2020-02-22 22:25:50 +00:00
|
|
|
frame_buffer: jintArray,
|
2020-02-26 22:08:21 +00:00
|
|
|
) {
|
|
|
|
let mut ctx = lock_ctx(ctx);
|
|
|
|
|
|
|
|
ctx.gba.frame();
|
|
|
|
let gpu_buffer =
|
|
|
|
std::mem::transmute::<&[u32], &[i32]>(&ctx.gba.get_frame_buffer() as &[u32]);
|
|
|
|
let result = env.set_int_array_region(frame_buffer, 0, gpu_buffer);
|
|
|
|
if let Err(e) = result {
|
|
|
|
env.throw_new(
|
|
|
|
NATIVE_EXCEPTION_CLASS,
|
|
|
|
format!("failed to copy framebuffer into Java, error: {}", e),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
}
|
2020-02-23 21:05:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[no_mangle]
|
|
|
|
pub unsafe extern "C" fn Java_com_mrmichel_rustboyadvance_EmulatorBindings_setKeyState(
|
2020-02-26 22:08:21 +00:00
|
|
|
_env: JNIEnv,
|
|
|
|
_obj: JClass,
|
|
|
|
ctx: jlong,
|
2020-02-23 21:05:09 +00:00
|
|
|
key_state: jint,
|
2020-02-26 22:08:21 +00:00
|
|
|
) {
|
|
|
|
let mut ctx = lock_ctx(ctx);
|
|
|
|
ctx.hwif.borrow_mut().key_state = key_state as u16;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[no_mangle]
|
|
|
|
pub unsafe extern "C" fn Java_com_mrmichel_rustboyadvance_EmulatorBindings_saveState(
|
|
|
|
env: JNIEnv,
|
|
|
|
_obj: JClass,
|
|
|
|
ctx: jlong,
|
|
|
|
) -> jbyteArray {
|
|
|
|
let mut ctx = lock_ctx(ctx);
|
|
|
|
match save_state(&env, &mut ctx.gba) {
|
|
|
|
Ok(result) => {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
Err(msg) => {
|
|
|
|
env.throw_new(NATIVE_EXCEPTION_CLASS, msg).unwrap();
|
|
|
|
return JObject::null().into_inner();
|
2020-02-23 21:05:09 +00:00
|
|
|
}
|
2020-02-26 22:08:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[no_mangle]
|
|
|
|
pub unsafe extern "C" fn Java_com_mrmichel_rustboyadvance_EmulatorBindings_loadState(
|
|
|
|
env: JNIEnv,
|
|
|
|
_obj: JClass,
|
|
|
|
ctx: jlong,
|
|
|
|
state: jbyteArray,
|
|
|
|
) {
|
|
|
|
let mut ctx = lock_ctx(ctx);
|
|
|
|
match load_state(&env, &mut ctx.gba, state) {
|
|
|
|
Ok(_) => {}
|
|
|
|
Err(msg) => env.throw_new(NATIVE_EXCEPTION_CLASS, msg).unwrap(),
|
|
|
|
}
|
2020-02-23 21:05:09 +00:00
|
|
|
}
|
2020-02-22 22:25:50 +00:00
|
|
|
|
2020-02-23 21:05:09 +00:00
|
|
|
#[no_mangle]
|
|
|
|
pub unsafe extern "C" fn Java_com_mrmichel_rustboyadvance_EmulatorBindings_log(
|
|
|
|
_env: JNIEnv,
|
2020-02-26 22:08:21 +00:00
|
|
|
_obj: JClass,
|
|
|
|
ctx: jlong,
|
|
|
|
) {
|
|
|
|
let ctx = lock_ctx(ctx);
|
|
|
|
info!("CPU LOG: {:#x?}", ctx.gba.cpu);
|
2020-02-22 22:25:50 +00:00
|
|
|
}
|
|
|
|
}
|