mirror of
https://github.com/LegitCamper/picocalc-os-rs.git
synced 2025-12-27 15:55:25 +00:00
WIP
This commit is contained in:
@@ -9,4 +9,5 @@ cc = "1.2.44"
|
||||
|
||||
[dependencies]
|
||||
abi = { path = "../../abi" }
|
||||
selection_ui = { path = "../../selection_ui" }
|
||||
embedded-graphics = "0.8.1"
|
||||
|
||||
@@ -45,6 +45,7 @@ fn bindgen() {
|
||||
.expect("Couldn't write bindings!");
|
||||
|
||||
cc::Build::new()
|
||||
.define("PEANUT_GB_IS_LITTLE_ENDIAN", None)
|
||||
.file("peanut_gb_stub.c")
|
||||
.include("Peanut-GB")
|
||||
// optimization flags
|
||||
|
||||
@@ -5,24 +5,34 @@
|
||||
extern crate alloc;
|
||||
use abi::{
|
||||
display::Display,
|
||||
fs::{file_len, read_file},
|
||||
format,
|
||||
fs::{Entries, file_len, list_dir, read_file, write_file},
|
||||
get_key,
|
||||
keyboard::{KeyCode, KeyState},
|
||||
print,
|
||||
};
|
||||
use alloc::{vec, vec::Vec};
|
||||
use core::{ffi::c_void, mem::MaybeUninit, panic::PanicInfo};
|
||||
use alloc::{string::String, vec, vec::Vec};
|
||||
use core::{cell::LazyCell, ffi::c_void, mem::MaybeUninit, panic::PanicInfo};
|
||||
use embedded_graphics::{
|
||||
mono_font::{MonoTextStyle, ascii::FONT_6X10},
|
||||
pixelcolor::Rgb565,
|
||||
prelude::RgbColor,
|
||||
};
|
||||
use selection_ui::{SelectionUi, SelectionUiError, draw_text_center};
|
||||
|
||||
mod peanut;
|
||||
use peanut::gb_run_frame;
|
||||
|
||||
use crate::peanut::{
|
||||
JOYPAD_A, JOYPAD_B, JOYPAD_DOWN, JOYPAD_LEFT, JOYPAD_RIGHT, JOYPAD_SELECT, JOYPAD_START,
|
||||
JOYPAD_UP, gb_cart_ram_read, gb_cart_ram_write, gb_error, gb_init, gb_init_lcd, gb_rom_read,
|
||||
gb_s, lcd_draw_line,
|
||||
JOYPAD_UP, gb_cart_ram_read, gb_cart_ram_write, gb_error, gb_get_rom_name, gb_get_save_size,
|
||||
gb_init, gb_init_lcd, gb_rom_read, gb_s, lcd_draw_line,
|
||||
};
|
||||
|
||||
static mut DISPLAY: Display = Display;
|
||||
static mut DISPLAY: LazyCell<Display> = LazyCell::new(|| Display::take().unwrap());
|
||||
|
||||
const RAM_SIZE: usize = 32 * 1024; // largest ram size is 32k
|
||||
static mut RAM: [u8; RAM_SIZE] = [0; RAM_SIZE];
|
||||
|
||||
#[panic_handler]
|
||||
fn panic(info: &PanicInfo) -> ! {
|
||||
@@ -35,7 +45,7 @@ pub extern "Rust" fn _start() {
|
||||
main()
|
||||
}
|
||||
|
||||
const GAME: &'static str = "/games/gameboy/zelda.gb";
|
||||
const GAME_PATH: &'static str = "/games/gameboy";
|
||||
|
||||
static mut GAME_ROM: Option<Vec<u8>> = None;
|
||||
|
||||
@@ -45,9 +55,43 @@ struct Priv {}
|
||||
pub fn main() {
|
||||
print!("Starting Gameboy app");
|
||||
|
||||
let size = file_len(GAME);
|
||||
let mut entries = Entries::new();
|
||||
list_dir(GAME_PATH, &mut entries);
|
||||
|
||||
let mut files = entries.entries();
|
||||
files.retain(|e| {
|
||||
let ext = e.extension().unwrap_or("");
|
||||
ext == "gb" || ext == "GB"
|
||||
});
|
||||
let mut roms = files.iter().map(|e| e.full_name()).collect::<Vec<&str>>();
|
||||
roms.sort();
|
||||
|
||||
let selection = {
|
||||
let display = unsafe { &mut *DISPLAY };
|
||||
let mut selection_ui = SelectionUi::new(&roms);
|
||||
match selection_ui.run_selection_ui(display) {
|
||||
Ok(maybe_sel) => maybe_sel,
|
||||
Err(e) => match e {
|
||||
SelectionUiError::SelectionListEmpty => {
|
||||
draw_text_center(
|
||||
display,
|
||||
&format!("No Roms were found in {}", GAME_PATH),
|
||||
MonoTextStyle::new(&FONT_6X10, Rgb565::RED),
|
||||
)
|
||||
.expect("Display Error");
|
||||
None
|
||||
}
|
||||
SelectionUiError::DisplayError(_) => panic!("Display Error"),
|
||||
},
|
||||
}
|
||||
};
|
||||
|
||||
assert!(selection.is_some());
|
||||
|
||||
let file_name = format!("{}/{}", GAME_PATH, roms[selection.unwrap()]);
|
||||
let size = file_len(&file_name);
|
||||
unsafe { GAME_ROM = Some(vec![0_u8; size]) };
|
||||
let read = read_file(GAME, 0, unsafe { GAME_ROM.as_mut().unwrap() });
|
||||
let read = read_file(&file_name, 0, unsafe { GAME_ROM.as_mut().unwrap() });
|
||||
assert!(size == read);
|
||||
print!("Rom size: {}", read);
|
||||
|
||||
@@ -66,40 +110,92 @@ pub fn main() {
|
||||
};
|
||||
print!("gb init status: {}", init_status);
|
||||
|
||||
unsafe {
|
||||
load_save(&mut gb.assume_init());
|
||||
}
|
||||
|
||||
unsafe {
|
||||
gb_init_lcd(gb.as_mut_ptr(), Some(lcd_draw_line));
|
||||
|
||||
// enable frame skip
|
||||
gb.assume_init().direct.set_frame_skip(true);
|
||||
gb.assume_init().direct.set_frame_skip(!true); // active low
|
||||
};
|
||||
|
||||
loop {
|
||||
let event = get_key();
|
||||
let keycode = match event.key {
|
||||
KeyCode::Esc => break,
|
||||
KeyCode::Tab => Some(JOYPAD_START),
|
||||
KeyCode::Del => Some(JOYPAD_SELECT),
|
||||
KeyCode::Enter => Some(JOYPAD_A),
|
||||
KeyCode::Backspace => Some(JOYPAD_B),
|
||||
KeyCode::JoyUp => Some(JOYPAD_UP),
|
||||
KeyCode::JoyDown => Some(JOYPAD_DOWN),
|
||||
KeyCode::JoyLeft => Some(JOYPAD_LEFT),
|
||||
KeyCode::JoyRight => Some(JOYPAD_RIGHT),
|
||||
_ => None,
|
||||
let button = match event.key {
|
||||
KeyCode::Esc => {
|
||||
unsafe { write_save(&mut gb.assume_init()) };
|
||||
break;
|
||||
}
|
||||
KeyCode::Tab => JOYPAD_START as u8,
|
||||
KeyCode::Del => JOYPAD_SELECT as u8,
|
||||
KeyCode::Enter => JOYPAD_A as u8,
|
||||
KeyCode::Backspace => JOYPAD_B as u8,
|
||||
KeyCode::Up => JOYPAD_UP as u8,
|
||||
KeyCode::Down => JOYPAD_DOWN as u8,
|
||||
KeyCode::Left => JOYPAD_LEFT as u8,
|
||||
KeyCode::Right => JOYPAD_RIGHT as u8,
|
||||
_ => 0,
|
||||
};
|
||||
|
||||
if let Some(keycode) = keycode {
|
||||
if button != 0 {
|
||||
let mut joypad = unsafe { (*gb.as_mut_ptr()).direct.__bindgen_anon_1.joypad };
|
||||
match event.state {
|
||||
KeyState::Pressed => unsafe {
|
||||
(*gb.as_mut_ptr()).direct.__bindgen_anon_1.joypad &= !keycode as u8
|
||||
},
|
||||
KeyState::Released => unsafe {
|
||||
(*gb.as_mut_ptr()).direct.__bindgen_anon_1.joypad |= keycode as u8
|
||||
},
|
||||
_ => (),
|
||||
KeyState::Pressed => joypad &= !button,
|
||||
KeyState::Released => joypad |= button,
|
||||
_ => {}
|
||||
}
|
||||
|
||||
print!("joypad now: {:#010b}\n", joypad);
|
||||
}
|
||||
|
||||
unsafe { gb_run_frame(gb.as_mut_ptr()) };
|
||||
unsafe {
|
||||
gb_run_frame(gb.as_mut_ptr());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
unsafe fn load_save(gb: &mut gb_s) {
|
||||
let mut buf = [0; 16];
|
||||
|
||||
unsafe {
|
||||
gb_get_rom_name(gb, buf.as_mut_ptr());
|
||||
|
||||
let save_size = gb_get_save_size(gb);
|
||||
|
||||
if save_size > 0 {
|
||||
read_file(
|
||||
&format!(
|
||||
"{}/saves/{}.sav",
|
||||
GAME_PATH,
|
||||
str::from_utf8(&buf).expect("bad rom name")
|
||||
),
|
||||
0,
|
||||
&mut RAM,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
unsafe fn write_save(gb: &mut gb_s) {
|
||||
let mut buf = [0; 16];
|
||||
|
||||
unsafe {
|
||||
gb_get_rom_name(gb, buf.as_mut_ptr());
|
||||
|
||||
let save_size = gb_get_save_size(gb);
|
||||
|
||||
if save_size > 0 {
|
||||
write_file(
|
||||
&format!(
|
||||
"{}/saves/{}.sav",
|
||||
GAME_PATH,
|
||||
str::from_utf8(&buf).expect("bad rom name")
|
||||
),
|
||||
0,
|
||||
&mut RAM,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,27 +2,42 @@
|
||||
#![allow(non_camel_case_types)]
|
||||
#![allow(non_snake_case)]
|
||||
|
||||
use crate::{DISPLAY, GAME_ROM};
|
||||
use crate::{DISPLAY, GAME_ROM, RAM};
|
||||
|
||||
#[allow(unused)]
|
||||
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
|
||||
|
||||
use abi::{display::Pixel565, fs::read_file};
|
||||
use abi::{display::Pixel565, print};
|
||||
use embedded_graphics::{Drawable, pixelcolor::Rgb565, prelude::Point};
|
||||
|
||||
pub const GBOY_WIDTH: usize = 160;
|
||||
pub const GBOY_HEIGHT: usize = 144;
|
||||
|
||||
pub unsafe extern "C" fn gb_rom_read(gb: *mut gb_s, addr: u32) -> u8 {
|
||||
pub unsafe extern "C" fn gb_rom_read(_gb: *mut gb_s, addr: u32) -> u8 {
|
||||
unsafe { GAME_ROM.as_ref().unwrap()[addr as usize] }
|
||||
}
|
||||
|
||||
pub unsafe extern "C" fn gb_cart_ram_read(gb: *mut gb_s, addr: u32) -> u8 {
|
||||
0
|
||||
pub unsafe extern "C" fn gb_cart_ram_read(_gb: *mut gb_s, addr: u32) -> u8 {
|
||||
unsafe { RAM[addr as usize] }
|
||||
}
|
||||
|
||||
pub unsafe extern "C" fn gb_cart_ram_write(gb: *mut gb_s, addr: u32, val: u8) {}
|
||||
pub unsafe extern "C" fn gb_cart_ram_write(_gb: *mut gb_s, addr: u32, val: u8) {
|
||||
unsafe { RAM[addr as usize] = val }
|
||||
}
|
||||
|
||||
pub unsafe extern "C" fn gb_error(gb: *mut gb_s, err: gb_error_e, addr: u16) {}
|
||||
pub unsafe extern "C" fn gb_error(_gb: *mut gb_s, err: gb_error_e, addr: u16) {
|
||||
let e = match err {
|
||||
0 => "UNKNOWN ERROR",
|
||||
1 => "INVALID OPCODE",
|
||||
2 => "INVALID READ",
|
||||
3 => "INVALID WRITE",
|
||||
4 => "HALT FOREVER",
|
||||
5 => "INVALID MAX",
|
||||
_ => unreachable!(),
|
||||
};
|
||||
|
||||
print!("PeanutGB error: {}, addr: {}", e, addr);
|
||||
}
|
||||
|
||||
const NUM_PALETTES: usize = 3;
|
||||
const SHADES_PER_PALETTE: usize = 4;
|
||||
@@ -82,6 +97,6 @@ fn draw_color(color: Rgb565, x: u16, y: u16) {
|
||||
pixel.1 = color;
|
||||
|
||||
unsafe {
|
||||
pixel.draw(&mut DISPLAY).unwrap();
|
||||
pixel.draw(&mut *DISPLAY).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user