This commit is contained in:
2025-11-09 16:05:11 -07:00
parent 56d5f02907
commit 026a5f2404
2 changed files with 41 additions and 50 deletions

View File

@@ -4,23 +4,22 @@
extern crate alloc; extern crate alloc;
use abi::{ use abi::{
Rng, display::Display,
display::{Display, SCREEN_HEIGHT, SCREEN_WIDTH},
fs::{file_len, read_file}, fs::{file_len, read_file},
get_key, get_key,
keyboard::{KeyCode, KeyState}, keyboard::{KeyCode, KeyState},
print, sleep, print,
}; };
use alloc::{vec, vec::Vec}; use alloc::{vec, vec::Vec};
use core::{cell::LazyCell, ffi::c_void, mem::MaybeUninit, panic::PanicInfo}; use core::{ffi::c_void, mem::MaybeUninit, panic::PanicInfo};
use embedded_graphics::{pixelcolor::Rgb565, prelude::RgbColor};
mod peanut; mod peanut;
use peanut::gb_run_frame; use peanut::gb_run_frame;
use crate::peanut::{ use crate::peanut::{
gb_cart_ram_read, gb_cart_ram_write, gb_error, gb_init, gb_init_lcd, gb_rom_read, gb_s, JOYPAD_A, JOYPAD_B, JOYPAD_DOWN, JOYPAD_LEFT, JOYPAD_RIGHT, JOYPAD_SELECT, JOYPAD_START,
lcd_draw_line, 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,
}; };
static mut DISPLAY: Display = Display; static mut DISPLAY: Display = Display;
@@ -36,15 +35,6 @@ pub extern "Rust" fn _start() {
main() main()
} }
const PEANUT_A: u8 = 0x01;
const PEANUT_B: u8 = 0x02;
const PEANUT_SELECT: u8 = 0x04;
const PEANUT_START: u8 = 0x08;
const PEANUT_RIGHT: u8 = 0x10;
const PEANUT_LEFT: u8 = 0x20;
const PEANUT_UP: u8 = 0x40;
const PEANUT_DOWN: u8 = 0x80;
const GAME: &'static str = "/games/gameboy/zelda.gb"; const GAME: &'static str = "/games/gameboy/zelda.gb";
static mut GAME_ROM: Option<Vec<u8>> = None; static mut GAME_ROM: Option<Vec<u8>> = None;
@@ -76,40 +66,39 @@ pub fn main() {
}; };
print!("gb init status: {}", init_status); print!("gb init status: {}", init_status);
unsafe { gb_init_lcd(gb.as_mut_ptr(), Some(lcd_draw_line)) }; unsafe {
gb_init_lcd(gb.as_mut_ptr(), Some(lcd_draw_line));
// enable frame skip
gb.assume_init().direct.set_frame_skip(true);
};
loop { loop {
let event = get_key(); let event = get_key();
if event.state != KeyState::Idle { let keycode = match event.key {
match event.key { KeyCode::Esc => break,
KeyCode::Esc => return, KeyCode::Tab => Some(JOYPAD_START),
KeyCode::Tab => unsafe { KeyCode::Del => Some(JOYPAD_SELECT),
(*gb.as_mut_ptr()).direct.__bindgen_anon_1.joypad &= !PEANUT_START; 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,
};
if let Some(keycode) = keycode {
match event.state {
KeyState::Pressed => unsafe {
(*gb.as_mut_ptr()).direct.__bindgen_anon_1.joypad &= !keycode as u8
}, },
KeyCode::Del => unsafe { KeyState::Released => unsafe {
(*gb.as_mut_ptr()).direct.__bindgen_anon_1.joypad &= !PEANUT_SELECT; (*gb.as_mut_ptr()).direct.__bindgen_anon_1.joypad |= keycode as u8
},
KeyCode::Enter => unsafe {
(*gb.as_mut_ptr()).direct.__bindgen_anon_1.joypad &= !PEANUT_A;
},
KeyCode::Backspace => unsafe {
(*gb.as_mut_ptr()).direct.__bindgen_anon_1.joypad &= !PEANUT_B;
},
KeyCode::JoyUp => unsafe {
(*gb.as_mut_ptr()).direct.__bindgen_anon_1.joypad &= !PEANUT_UP;
},
KeyCode::JoyDown => unsafe {
(*gb.as_mut_ptr()).direct.__bindgen_anon_1.joypad &= !PEANUT_DOWN;
},
KeyCode::JoyLeft => unsafe {
(*gb.as_mut_ptr()).direct.__bindgen_anon_1.joypad &= !PEANUT_LEFT;
},
KeyCode::JoyRight => unsafe {
(*gb.as_mut_ptr()).direct.__bindgen_anon_1.joypad &= !PEANUT_RIGHT;
}, },
_ => (), _ => (),
}; }
}; }
unsafe { gb_run_frame(gb.as_mut_ptr()) }; unsafe { gb_run_frame(gb.as_mut_ptr()) };
} }

View File

@@ -63,13 +63,15 @@ pub unsafe extern "C" fn lcd_draw_line(_gb: *mut gb_s, pixels: *const u8, line:
.copied() .copied()
.unwrap_or(Rgb565::new(0, 0, 0)); .unwrap_or(Rgb565::new(0, 0, 0));
let sx = (x as u16) * 2; // let sx = (x as u16) * 2;
let sy = y * 2; // let sy = y * 2;
draw_color(color, sx, sy); // draw_color(color, sx, sy);
draw_color(color, sx + 1, sy); // draw_color(color, sx + 1, sy);
draw_color(color, sx, sy + 1); // draw_color(color, sx, sy + 1);
draw_color(color, sx + 1, sy + 1); // draw_color(color, sx + 1, sy + 1);
//
draw_color(color, x as u16, y as u16);
} }
} }
} }