Skip to content
Snippets Groups Projects
Verified Commit 3d0c0ab4 authored by João Magalhães's avatar João Magalhães :rocket:
Browse files

chore: new reload operation

Also fixed frame global memory issue in libretro.
parent 2faa3e75
No related branches found
No related tags found
1 merge request!29Support for Libretro
Pipeline #3079 passed
...@@ -11,7 +11,7 @@ use std::{ ...@@ -11,7 +11,7 @@ use std::{
use boytacean::{ use boytacean::{
gb::{AudioProvider, GameBoy}, gb::{AudioProvider, GameBoy},
pad::PadKey, pad::PadKey,
ppu::{DISPLAY_HEIGHT, DISPLAY_WIDTH, RGB1555_SIZE}, ppu::{DISPLAY_HEIGHT, DISPLAY_WIDTH, FRAME_BUFFER_RGB155_SIZE, RGB1555_SIZE},
rom::Cartridge, rom::Cartridge,
}; };
...@@ -31,6 +31,7 @@ const RETRO_DEVICE_JOYPAD: usize = 1; ...@@ -31,6 +31,7 @@ const RETRO_DEVICE_JOYPAD: usize = 1;
static mut EMULATOR: Option<GameBoy> = None; static mut EMULATOR: Option<GameBoy> = None;
static mut KEY_STATES: Option<HashMap<RetroJoypad, bool>> = None; static mut KEY_STATES: Option<HashMap<RetroJoypad, bool>> = None;
static mut FRAME_BUFFER: [u8; FRAME_BUFFER_RGB155_SIZE] = [0x00; FRAME_BUFFER_RGB155_SIZE];
static mut ENVIRONMENT_CALLBACK: Option<extern "C" fn(u32, *const c_void) -> bool> = None; static mut ENVIRONMENT_CALLBACK: Option<extern "C" fn(u32, *const c_void) -> bool> = None;
static mut VIDEO_REFRESH_CALLBACK: Option<extern "C" fn(*const u8, c_uint, c_uint, usize)> = None; static mut VIDEO_REFRESH_CALLBACK: Option<extern "C" fn(*const u8, c_uint, c_uint, usize)> = None;
...@@ -176,6 +177,8 @@ pub extern "C" fn retro_deinit() { ...@@ -176,6 +177,8 @@ pub extern "C" fn retro_deinit() {
#[no_mangle] #[no_mangle]
pub extern "C" fn retro_reset() { pub extern "C" fn retro_reset() {
println!("retro_reset()"); println!("retro_reset()");
let emulator = unsafe { EMULATOR.as_mut().unwrap() };
emulator.reload();
} }
#[no_mangle] #[no_mangle]
...@@ -348,8 +351,9 @@ pub extern "C" fn retro_run() { ...@@ -348,8 +351,9 @@ pub extern "C" fn retro_run() {
let frame_buffer = emulator.frame_buffer_rgb1555(); let frame_buffer = emulator.frame_buffer_rgb1555();
unsafe { unsafe {
FRAME_BUFFER.copy_from_slice(&frame_buffer);
VIDEO_REFRESH_CALLBACK.unwrap()( VIDEO_REFRESH_CALLBACK.unwrap()(
frame_buffer.as_ptr(), FRAME_BUFFER.as_ptr(),
DISPLAY_WIDTH as u32, DISPLAY_WIDTH as u32,
DISPLAY_HEIGHT as u32, DISPLAY_HEIGHT as u32,
DISPLAY_WIDTH * RGB1555_SIZE, DISPLAY_WIDTH * RGB1555_SIZE,
......
...@@ -406,6 +406,13 @@ impl GameBoy { ...@@ -406,6 +406,13 @@ impl GameBoy {
self.cpu.reset(); self.cpu.reset();
} }
pub fn reload(&mut self) {
let rom = self.rom().clone();
self.reset();
self.load(true);
self.load_cartridge(rom);
}
pub fn clock(&mut self) -> u16 { pub fn clock(&mut self) -> u16 {
let cycles = self.cpu_clock() as u16; let cycles = self.cpu_clock() as u16;
let cycles_n = cycles / self.multiplier() as u16; let cycles_n = cycles / self.multiplier() as u16;
...@@ -989,13 +996,17 @@ impl GameBoy { ...@@ -989,13 +996,17 @@ impl GameBoy {
self.load_boot_file(BootRom::Cgb); self.load_boot_file(BootRom::Cgb);
} }
pub fn load_cartridge(&mut self, rom: Cartridge) -> &mut Cartridge {
self.mmu().set_rom(rom);
self.mmu().rom()
}
pub fn load_rom(&mut self, data: &[u8], ram_data: Option<&[u8]>) -> &mut Cartridge { pub fn load_rom(&mut self, data: &[u8], ram_data: Option<&[u8]>) -> &mut Cartridge {
let mut rom = Cartridge::from_data(data); let mut rom = Cartridge::from_data(data);
if let Some(ram_data) = ram_data { if let Some(ram_data) = ram_data {
rom.set_ram_data(ram_data) rom.set_ram_data(ram_data)
} }
self.mmu().set_rom(rom); self.load_cartridge(rom)
self.mmu().rom()
} }
pub fn load_rom_file(&mut self, path: &str, ram_path: Option<&str>) -> &mut Cartridge { pub fn load_rom_file(&mut self, path: &str, ram_path: Option<&str>) -> &mut Cartridge {
......
...@@ -599,6 +599,10 @@ impl Cartridge { ...@@ -599,6 +599,10 @@ impl Cartridge {
self.ram_data = data.to_vec(); self.ram_data = data.to_vec();
} }
pub fn clear_ram_data(&mut self) {
self.ram_data = vec![0u8; self.ram_data.len()];
}
pub fn description(&self, column_length: usize) -> String { pub fn description(&self, column_length: usize) -> String {
let name_l = format!("{:width$}", "Name", width = column_length); let name_l = format!("{:width$}", "Name", width = column_length);
let type_l = format!("{:width$}", "Type", width = column_length); let type_l = format!("{:width$}", "Type", width = column_length);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment