2020-02-26 22:08:21 +00:00
|
|
|
/// JNI Bindings for rustboyadvance
|
2020-02-23 21:05:09 +00:00
|
|
|
///
|
2020-02-29 21:40:05 +00:00
|
|
|
mod rom_helper;
|
|
|
|
|
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;
|
|
|
|
|
2020-02-28 16:33:43 +00:00
|
|
|
use ringbuf::{Consumer, Producer, RingBuffer};
|
|
|
|
|
2020-02-22 22:25:50 +00:00
|
|
|
use rustboyadvance_ng::prelude::*;
|
2020-02-28 16:33:43 +00:00
|
|
|
use rustboyadvance_ng::StereoSample;
|
|
|
|
|
|
|
|
struct AudioRingBuffer {
|
|
|
|
pub prod: Producer<i16>,
|
|
|
|
pub cons: Consumer<i16>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AudioRingBuffer {
|
|
|
|
fn new() -> AudioRingBuffer {
|
|
|
|
let rb = RingBuffer::new(4096 * 2);
|
|
|
|
let (prod, cons) = rb.split();
|
|
|
|
|
|
|
|
AudioRingBuffer { prod, cons }
|
|
|
|
}
|
|
|
|
}
|
2020-02-22 22:25:50 +00:00
|
|
|
|
|
|
|
struct Hardware {
|
2020-02-26 22:08:21 +00:00
|
|
|
jvm: JavaVM,
|
2020-02-27 09:03:34 +00:00
|
|
|
frame_buffer_global_ref: GlobalRef,
|
2020-02-28 16:33:43 +00:00
|
|
|
audio_buffer: AudioRingBuffer,
|
2020-02-23 21:05:09 +00:00
|
|
|
key_state: u16,
|
2020-02-22 22:25:50 +00:00
|
|
|
}
|
|
|
|
|
2020-02-27 09:03:34 +00:00
|
|
|
impl VideoInterface for Hardware {
|
|
|
|
fn render(&mut self, buffer: &[u32]) {
|
|
|
|
let env = self.jvm.get_env().unwrap();
|
|
|
|
unsafe {
|
|
|
|
env.set_int_array_region(
|
|
|
|
self.frame_buffer_global_ref.as_obj().into_inner(),
|
|
|
|
0,
|
|
|
|
std::mem::transmute::<&[u32], &[i32]>(buffer),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-02-28 16:33:43 +00:00
|
|
|
impl AudioInterface for Hardware {
|
|
|
|
fn push_sample(&mut self, sample: StereoSample<i16>) {
|
|
|
|
self.audio_buffer.prod.push(sample.0);
|
|
|
|
self.audio_buffer.prod.push(sample.1);
|
|
|
|
}
|
|
|
|
}
|
2020-02-23 21:05:09 +00:00
|
|
|
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,
|
2020-02-27 09:03:34 +00:00
|
|
|
frame_buffer: jintArray,
|
2020-02-26 22:08:21 +00:00
|
|
|
save_file: JString,
|
2020-02-29 21:40:05 +00:00
|
|
|
skip_bios: jboolean,
|
2020-02-26 22:08:21 +00:00
|
|
|
) -> 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);
|
|
|
|
|
2020-02-27 09:03:34 +00:00
|
|
|
let frame_buffer_global_ref = env
|
|
|
|
.new_global_ref(JObject::from(frame_buffer))
|
|
|
|
.map_err(|e| format!("failed to add new global ref, error: {:?}", e))?;
|
|
|
|
|
2020-02-26 22:08:21 +00:00
|
|
|
let hw = Hardware {
|
|
|
|
jvm: env.get_java_vm().unwrap(),
|
2020-02-27 09:03:34 +00:00
|
|
|
frame_buffer_global_ref: frame_buffer_global_ref,
|
2020-02-28 16:33:43 +00:00
|
|
|
audio_buffer: AudioRingBuffer::new(),
|
2020-02-26 22:08:21 +00:00
|
|
|
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));
|
|
|
|
|
2020-02-29 21:40:05 +00:00
|
|
|
let mut gba = GameBoyAdvance::new(bios, gamepak, hw.clone(), hw.clone(), hw.clone());
|
|
|
|
|
|
|
|
if skip_bios != 0 {
|
|
|
|
debug!("skipping bios");
|
|
|
|
gba.skip_bios();
|
|
|
|
}
|
2020-02-26 22:08:21 +00:00
|
|
|
|
|
|
|
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,
|
2020-02-27 09:03:34 +00:00
|
|
|
frame_buffer: jintArray,
|
2020-02-26 22:08:21 +00:00
|
|
|
save_file: JString,
|
2020-02-29 21:40:05 +00:00
|
|
|
skip_bios: jboolean
|
|
|
|
) -> jlong {
|
|
|
|
match internal_open_context(&env, bios, rom, frame_buffer, save_file, skip_bios) {
|
|
|
|
Ok(ctx) => Box::into_raw(Box::new(Mutex::new(ctx))) as jlong,
|
|
|
|
Err(msg) => {
|
|
|
|
env.throw_new(NATIVE_EXCEPTION_CLASS, msg).unwrap();
|
|
|
|
-1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn inetrnal_open_saved_state(
|
|
|
|
env: &JNIEnv,
|
|
|
|
state: jbyteArray,
|
|
|
|
frame_buffer: jintArray,
|
|
|
|
) -> Result<Context, String> {
|
|
|
|
let state = env
|
|
|
|
.convert_byte_array(state)
|
|
|
|
.map_err(|e| format!("could not get state buffer, error {}", e))?;
|
|
|
|
|
|
|
|
let frame_buffer_global_ref = env
|
|
|
|
.new_global_ref(JObject::from(frame_buffer))
|
|
|
|
.map_err(|e| format!("failed to add new global ref, error: {:?}", e))?;
|
|
|
|
|
|
|
|
let hw = Hardware {
|
|
|
|
jvm: env.get_java_vm().unwrap(),
|
|
|
|
frame_buffer_global_ref: frame_buffer_global_ref,
|
|
|
|
audio_buffer: AudioRingBuffer::new(),
|
|
|
|
key_state: 0xffff,
|
|
|
|
};
|
|
|
|
let hw = Rc::new(RefCell::new(hw));
|
|
|
|
|
|
|
|
let gba = GameBoyAdvance::from_saved_state(&state, hw.clone(), hw.clone(), hw.clone())
|
|
|
|
.map_err(|e| {
|
|
|
|
format!(
|
|
|
|
"failed to create GameBoyAdvance from saved state, error {:?}",
|
|
|
|
e
|
|
|
|
)
|
|
|
|
})?;
|
|
|
|
|
|
|
|
Ok(Context {
|
|
|
|
gba: gba,
|
|
|
|
hwif: hw.clone(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[no_mangle]
|
|
|
|
pub unsafe extern "C" fn Java_com_mrmichel_rustboyadvance_EmulatorBindings_openSavedState(
|
|
|
|
env: JNIEnv,
|
|
|
|
_obj: JClass,
|
|
|
|
state: jbyteArray,
|
|
|
|
frame_buffer: jintArray,
|
2020-02-26 22:08:21 +00:00
|
|
|
) -> jlong {
|
2020-02-29 21:40:05 +00:00
|
|
|
match inetrnal_open_saved_state(&env, state, frame_buffer) {
|
2020-02-26 22:08:21 +00:00
|
|
|
Ok(ctx) => Box::into_raw(Box::new(Mutex::new(ctx))) as jlong,
|
|
|
|
Err(msg) => {
|
|
|
|
env.throw_new(NATIVE_EXCEPTION_CLASS, msg).unwrap();
|
2020-02-29 21:40:05 +00:00
|
|
|
-1
|
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(
|
2020-02-27 09:03:34 +00:00
|
|
|
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();
|
2020-02-28 16:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[no_mangle]
|
|
|
|
pub unsafe extern "C" fn Java_com_mrmichel_rustboyadvance_EmulatorBindings_collectAudioSamples(
|
|
|
|
env: JNIEnv,
|
|
|
|
_obj: JClass,
|
|
|
|
ctx: jlong,
|
|
|
|
) -> jshortArray {
|
|
|
|
let mut ctx = lock_ctx(ctx);
|
|
|
|
|
|
|
|
let mut hw = ctx.hwif.borrow_mut();
|
|
|
|
|
|
|
|
let mut samples = Vec::with_capacity(1024);
|
|
|
|
|
|
|
|
while let Some(sample) = hw.audio_buffer.cons.pop() {
|
|
|
|
samples.push(sample);
|
|
|
|
}
|
|
|
|
|
|
|
|
let arr = env.new_short_array(samples.len() as jsize).unwrap();
|
|
|
|
env.set_short_array_region(arr, 0, &samples);
|
|
|
|
|
|
|
|
return arr;
|
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-27 09:03:34 +00:00
|
|
|
#[no_mangle]
|
|
|
|
pub unsafe extern "C" fn Java_com_mrmichel_rustboyadvance_EmulatorBindings_getGameTitle(
|
|
|
|
env: JNIEnv,
|
|
|
|
_obj: JClass,
|
|
|
|
ctx: jlong,
|
|
|
|
) -> jstring {
|
|
|
|
let ctx = lock_ctx(ctx);
|
2020-02-28 16:33:43 +00:00
|
|
|
env.new_string(ctx.gba.get_game_title())
|
|
|
|
.unwrap()
|
|
|
|
.into_inner()
|
2020-02-27 09:03:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[no_mangle]
|
|
|
|
pub unsafe extern "C" fn Java_com_mrmichel_rustboyadvance_EmulatorBindings_getGameCode(
|
|
|
|
env: JNIEnv,
|
|
|
|
_obj: JClass,
|
|
|
|
ctx: jlong,
|
|
|
|
) -> jstring {
|
|
|
|
let ctx = lock_ctx(ctx);
|
2020-02-28 16:33:43 +00:00
|
|
|
env.new_string(ctx.gba.get_game_code())
|
|
|
|
.unwrap()
|
|
|
|
.into_inner()
|
2020-02-27 09:03:34 +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
|
|
|
}
|
|
|
|
}
|