mirror of
https://github.com/FranLMSP/rmg-001.git
synced 2024-11-23 18:21:31 +00:00
Compare commits
No commits in common. "c5ff92c0ab0b7d2101f3fbd4e64265edc4dcab71" and "659f602b2a0b6264844d8cf27de5d66f706e5cbc" have entirely different histories.
c5ff92c0ab
...
659f602b2a
13
src/bus.rs
13
src/bus.rs
@ -59,7 +59,7 @@ pub struct Bus {
|
||||
|
||||
impl Bus {
|
||||
pub fn new() -> Self {
|
||||
let game_rom = match ROM::load_file("ignore/m3_scy_change.gb".to_string()) {
|
||||
let game_rom = match ROM::load_file("ignore/tetris.gb".to_string()) {
|
||||
// let game_rom = match ROM::load_file("roms/cpu_instrs.gb".to_string()) {
|
||||
// let game_rom = match ROM::load_file("roms/cpu_instrs_individual/01-special.gb".to_string()) {
|
||||
// let game_rom = match ROM::load_file("roms/cpu_instrs_individual/02-interrupts.gb".to_string()) {
|
||||
@ -78,7 +78,7 @@ impl Bus {
|
||||
};
|
||||
let mut data = [0x00; 0x10000];
|
||||
// Hardware registers after the bootrom
|
||||
data[0xFF00] = 0xCF;
|
||||
data[0xFF00] = 0b11001111;
|
||||
data[0xFF01] = 0x00;
|
||||
data[0xFF02] = 0x7E;
|
||||
data[0xFF04] = 0x18;
|
||||
@ -139,14 +139,9 @@ impl Bus {
|
||||
} else if address == LCD_CONTROL_ADDRESS && get_bit(data, BitIndex::I7) {
|
||||
self.data[address as usize] = data;
|
||||
self.data[LCD_Y_ADDRESS as usize] = 0x00;
|
||||
} else if address == LCD_Y_ADDRESS {
|
||||
// println!("Write to LCD_Y not allowed");
|
||||
} else if address == LCD_STATUS_ADDRESS {
|
||||
let byte = self.data[address as usize];
|
||||
self.data[address as usize] = (data & 0b11111000) | (byte & 0b00000111);
|
||||
} else if address == JOYPAD_ADDRESS {
|
||||
let byte = self.data[address as usize];
|
||||
self.data[address as usize] = (data & 0b11110000) | (byte & 0b00001111);
|
||||
let byte = self.data[JOYPAD_ADDRESS as usize];
|
||||
self.data[JOYPAD_ADDRESS as usize] = (data & 0b11110000) | (byte & 0b00001111);
|
||||
} else {
|
||||
self.data[address as usize] = data;
|
||||
}
|
||||
|
@ -96,11 +96,19 @@ impl Emulator {
|
||||
self.joypad.release(Button::Select);
|
||||
}
|
||||
if change {
|
||||
self.joypad.update(&mut self.bus);
|
||||
self.bus.set_interrupt_flag(Interrupt::Joypad, true);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn run(&mut self, cpu_cycles: Cycles, frame_buffer: &mut [u8]) {
|
||||
pub fn draw(&mut self, frame: &mut [u8]) {
|
||||
let ppu_frame = self.ppu.get_rgba_frame();
|
||||
for (i, pixel) in frame.chunks_exact_mut(4).enumerate() {
|
||||
pixel.copy_from_slice(&ppu_frame[i]);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn run(&mut self, cpu_cycles: Cycles) {
|
||||
self.cpu.reset_cycles();
|
||||
while self.cpu.get_cycles().0 <= cpu_cycles.0 {
|
||||
self.cpu.run(&mut self.bus);
|
||||
@ -108,7 +116,7 @@ impl Emulator {
|
||||
self.bus.reset_timer = false;
|
||||
self.timer.reset(&mut self.bus);
|
||||
}
|
||||
self.ppu.do_cycles(&mut self.bus, self.cpu.get_last_op_cycles(), frame_buffer);
|
||||
self.ppu.do_cycles(&mut self.bus, self.cpu.get_last_op_cycles());
|
||||
self.timer.do_cycles(&mut self.bus, self.cpu.get_last_op_cycles());
|
||||
|
||||
// 1 CPU cycle = 238.42ns
|
||||
@ -119,14 +127,13 @@ impl Emulator {
|
||||
|
||||
pub fn cpu_loop(&mut self) {
|
||||
let mut exit = false;
|
||||
let mut frame: [u8; 144 * 160] = [0; 144 * 160];
|
||||
while !exit {
|
||||
self.cpu.run(&mut self.bus);
|
||||
if self.bus.reset_timer {
|
||||
self.bus.reset_timer = false;
|
||||
self.timer.reset(&mut self.bus);
|
||||
}
|
||||
self.ppu.do_cycles(&mut self.bus, self.cpu.get_last_op_cycles(), &mut frame);
|
||||
self.ppu.do_cycles(&mut self.bus, self.cpu.get_last_op_cycles());
|
||||
self.timer.do_cycles(&mut self.bus, self.cpu.get_last_op_cycles());
|
||||
|
||||
// exit = self.cpu.get_exec_calls_count() >= 1258895; // log 1
|
||||
|
@ -68,7 +68,7 @@ impl Joypad {
|
||||
};
|
||||
}
|
||||
|
||||
pub fn read(&self, bus: &mut Bus) -> u8 {
|
||||
pub fn update(&self, bus: &mut Bus) {
|
||||
let byte = bus.read(JOYPAD_ADDRESS);
|
||||
let direction = !get_bit(byte, BitIndex::I4);
|
||||
let action = !get_bit(byte, BitIndex::I5);
|
||||
@ -83,6 +83,7 @@ impl Joypad {
|
||||
) | (
|
||||
(!((direction && self.right) || (action && self.a)) as u8)
|
||||
);
|
||||
return data;
|
||||
println!("New joypad write: {:08b}", data);
|
||||
bus.force_write(JOYPAD_ADDRESS, data);
|
||||
}
|
||||
}
|
||||
|
99
src/ppu.rs
99
src/ppu.rs
@ -91,7 +91,6 @@ pub enum LCDStatus {
|
||||
}
|
||||
|
||||
pub struct PPU {
|
||||
prev_state: bool,
|
||||
cycles: Cycles,
|
||||
rgba_frame: [[u8; 4]; FRAME_BUFFER_LENGTH as usize],
|
||||
}
|
||||
@ -104,7 +103,6 @@ enum TileNumber {
|
||||
impl PPU {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
prev_state: false,
|
||||
cycles: Cycles(0),
|
||||
rgba_frame: [[0xFF, 0xFF, 0xFF, 0]; FRAME_BUFFER_LENGTH as usize],
|
||||
}
|
||||
@ -118,15 +116,15 @@ impl PPU {
|
||||
self.cycles.0 += cycles.0;
|
||||
}
|
||||
|
||||
pub fn do_cycles(&mut self, bus: &mut Bus, cycles: Cycles, frame_buffer: &mut [u8]) {
|
||||
pub fn do_cycles(&mut self, bus: &mut Bus, cycles: Cycles) {
|
||||
let mut count = 0;
|
||||
while count < cycles.0 {
|
||||
self.cycle(bus, frame_buffer);
|
||||
self.cycle(bus);
|
||||
count += 1;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn cycle(&mut self, bus: &mut Bus, frame_buffer: &mut [u8]) {
|
||||
pub fn cycle(&mut self, bus: &mut Bus) {
|
||||
if !PPU::get_lcd_control(bus, LCDControl::LCDEnable) {
|
||||
self.increment_cycles(Cycles(1));
|
||||
return;
|
||||
@ -136,21 +134,26 @@ impl PPU {
|
||||
if self.cycles.0 == 0 {
|
||||
// Mode 2 OAM scan
|
||||
PPU::set_lcd_status(bus, LCDStatus::ModeFlag(LCDStatusModeFlag::SearchingOAM), true);
|
||||
self.stat_interrupt(bus);
|
||||
if PPU::get_lcd_status(bus, LCDStatus::Mode2OAMInterrupt) {
|
||||
PPU::request_interrupt(bus, Interrupt::LCDSTAT);
|
||||
}
|
||||
} else if self.cycles.0 == 80 + 1 {
|
||||
// Mode 3 drawing pixel line. This could also last 289 cycles
|
||||
self.draw_line(bus, frame_buffer);
|
||||
self.draw_line(bus);
|
||||
PPU::set_lcd_status(bus, LCDStatus::ModeFlag(LCDStatusModeFlag::TransferringToLCD), true);
|
||||
} else if self.cycles.0 == 80 + 172 + 1 {
|
||||
// Mode 0 Horizontal blank. This could last 87 or 204 cycles depending on the mode 3
|
||||
PPU::set_lcd_status(bus, LCDStatus::ModeFlag(LCDStatusModeFlag::HBlank), true);
|
||||
self.stat_interrupt(bus);
|
||||
if PPU::get_lcd_status(bus, LCDStatus::Mode0HBlankInterrupt) {
|
||||
PPU::request_interrupt(bus, Interrupt::LCDSTAT);
|
||||
}
|
||||
}
|
||||
} else if PPU::get_lcd_y(bus) == 144 && self.cycles.0 == 0 {
|
||||
// Mode 1 Vertical blank
|
||||
bus.set_interrupt_flag(Interrupt::VBlank, true);
|
||||
PPU::set_lcd_status(bus, LCDStatus::ModeFlag(LCDStatusModeFlag::VBlank), true);
|
||||
self.stat_interrupt(bus);
|
||||
if PPU::get_lcd_status(bus, LCDStatus::Mode1VBlankInterrupt) {
|
||||
PPU::request_interrupt(bus, Interrupt::VBlank);
|
||||
}
|
||||
}
|
||||
|
||||
self.increment_cycles(Cycles(1));
|
||||
@ -164,28 +167,21 @@ impl PPU {
|
||||
if PPU::get_lcd_y(bus) > 153 {
|
||||
PPU::set_lcd_y(bus, 0);
|
||||
}
|
||||
self.check_lyc(bus);
|
||||
PPU::check_lyc(bus);
|
||||
}
|
||||
}
|
||||
|
||||
fn stat_interrupt(&mut self, bus: &mut Bus) {
|
||||
let state = self.prev_state;
|
||||
self.prev_state = (
|
||||
(PPU::get_lcd_status(bus, LCDStatus::Mode2OAMInterrupt) && PPU::get_lcd_status(bus, LCDStatus::ModeFlag(LCDStatusModeFlag::SearchingOAM))) ||
|
||||
(PPU::get_lcd_status(bus, LCDStatus::Mode0HBlankInterrupt) && PPU::get_lcd_status(bus, LCDStatus::ModeFlag(LCDStatusModeFlag::HBlank))) ||
|
||||
(PPU::get_lcd_status(bus, LCDStatus::Mode1VBlankInterrupt) && PPU::get_lcd_status(bus, LCDStatus::ModeFlag(LCDStatusModeFlag::VBlank)))
|
||||
);
|
||||
if self.prev_state && !state {
|
||||
bus.set_interrupt_flag(Interrupt::LCDSTAT, true);
|
||||
}
|
||||
}
|
||||
|
||||
fn check_lyc(&mut self, bus: &mut Bus) {
|
||||
fn check_lyc(bus: &mut Bus) {
|
||||
let lyc_compare = PPU::get_lcd_y(bus) == bus.read(LCD_Y_COMPARE_ADDRESS);
|
||||
if PPU::get_lcd_status(bus, LCDStatus::LYCInterrupt) && lyc_compare {
|
||||
PPU::set_lcd_status(bus, LCDStatus::LYCFlag, lyc_compare);
|
||||
if lyc_compare && PPU::get_lcd_status(bus, LCDStatus::LYCInterrupt) {
|
||||
bus.set_interrupt_flag(Interrupt::LCDSTAT, true);
|
||||
self.prev_state = true;
|
||||
PPU::request_interrupt(bus, Interrupt::LCDSTAT);
|
||||
}
|
||||
}
|
||||
|
||||
fn request_interrupt(bus: &mut Bus, interrupt: Interrupt) {
|
||||
if PPU::get_lcd_control(bus, LCDControl::LCDEnable) {
|
||||
bus.set_interrupt_flag(interrupt, true);
|
||||
}
|
||||
}
|
||||
|
||||
@ -194,7 +190,7 @@ impl PPU {
|
||||
}
|
||||
|
||||
fn set_lcd_y(bus: &mut Bus, val: u8) {
|
||||
bus.force_write(LCD_Y_ADDRESS, val);
|
||||
bus.write(LCD_Y_ADDRESS, val);
|
||||
}
|
||||
|
||||
fn get_scroll_x(bus: &Bus) -> u8 {
|
||||
@ -220,7 +216,7 @@ impl PPU {
|
||||
|
||||
fn set_lcd_control(bus: &mut Bus, control: LCDControl, val: bool) {
|
||||
let mut byte = bus.read(LCD_CONTROL_ADDRESS);
|
||||
bus.force_write(LCD_CONTROL_ADDRESS, control.set(byte, val));
|
||||
bus.write(LCD_CONTROL_ADDRESS, control.set(byte, val));
|
||||
}
|
||||
|
||||
pub fn get_lcd_status(bus: &Bus, status: LCDStatus) -> bool {
|
||||
@ -255,7 +251,7 @@ impl PPU {
|
||||
LCDStatusModeFlag::TransferringToLCD => (byte & 0b11111100) | 3,
|
||||
},
|
||||
};
|
||||
bus.force_write(LCD_STATUS_ADDRESS, byte);
|
||||
bus.write(LCD_STATUS_ADDRESS, byte);
|
||||
}
|
||||
|
||||
fn get_tile_bytes(x: u8, y: u8, tile_number_type: TileNumber, default_method: bool, bus: &Bus) -> (u8, u8) {
|
||||
@ -281,15 +277,15 @@ impl PPU {
|
||||
|
||||
fn get_window_pixel(lcd_x: u8, bus: &Bus) -> Option<Pixel> {
|
||||
let lcd_y = PPU::get_lcd_y(bus);
|
||||
let window_x = PPU::get_window_x(bus);
|
||||
let window_x = (PPU::get_window_x(bus) as i8 - 7) as u8;
|
||||
let window_y = PPU::get_window_y(bus);
|
||||
|
||||
if !PPU::get_lcd_control(bus, LCDControl::WindowEnable) || lcd_x < (window_x.saturating_sub(7)) || window_y != lcd_y {
|
||||
if !PPU::get_lcd_control(bus, LCDControl::WindowEnable) || window_x != lcd_x || window_y != lcd_y {
|
||||
return None;
|
||||
}
|
||||
|
||||
let x = lcd_x.wrapping_sub(window_x.saturating_sub(7));
|
||||
let y = lcd_y.wrapping_sub(window_y);
|
||||
let x = lcd_x - window_x;
|
||||
let y = lcd_y - window_x;
|
||||
|
||||
let default_mode = PPU::get_lcd_control(bus, LCDControl::TileAddressMode);
|
||||
let tilemap_area = match PPU::get_lcd_control(bus, LCDControl::WindowTileMapAddress) {
|
||||
@ -304,7 +300,7 @@ impl PPU {
|
||||
Some(pixels[(x as usize).rem_euclid(8)])
|
||||
}
|
||||
|
||||
fn draw_line(&mut self, bus: &Bus, frame_buffer: &mut [u8]) {
|
||||
fn draw_line(&mut self, bus: &Bus) {
|
||||
let palette = bus.read(BACKGROUND_PALETTE_ADDRESS);
|
||||
let lcd_y = PPU::get_lcd_y(bus);
|
||||
if lcd_y as u32 >= LCD_HEIGHT {
|
||||
@ -324,36 +320,15 @@ impl PPU {
|
||||
|
||||
let bg_pixels = PPU::get_byte_pixels(tile_byte_1, tile_byte_2, palette);
|
||||
|
||||
let idx = (lcd_x as usize + (lcd_y as usize * LCD_WIDTH as usize)) * 4;
|
||||
let pixel = bg_pixels[x.rem_euclid(8) as usize];
|
||||
let rgba = PPU::get_rgba(pixel);
|
||||
frame_buffer[idx] = rgba[0];
|
||||
frame_buffer[idx + 1] = rgba[1];
|
||||
frame_buffer[idx + 2] = rgba[2];
|
||||
for pixel in bg_pixels {
|
||||
let idx = lcd_x as usize + (lcd_y as usize * LCD_WIDTH as usize);
|
||||
self.rgba_frame[idx] = PPU::get_rgba(pixel);
|
||||
if let Some(window_pixel) = PPU::get_window_pixel(lcd_x, bus) {
|
||||
let rgba = PPU::get_rgba(pixel);
|
||||
frame_buffer[idx] = rgba[0];
|
||||
frame_buffer[idx + 1] = rgba[1];
|
||||
frame_buffer[idx + 2] = rgba[2];
|
||||
self.rgba_frame[idx] = PPU::get_rgba(window_pixel);
|
||||
}
|
||||
|
||||
lcd_x += 1;
|
||||
|
||||
/* for pixel in bg_pixels {
|
||||
let idx = (lcd_x as usize + (lcd_y as usize * LCD_WIDTH as usize)) * 4;
|
||||
let rgba = PPU::get_rgba(pixel);
|
||||
frame_buffer[idx] = rgba[0];
|
||||
frame_buffer[idx + 1] = rgba[1];
|
||||
frame_buffer[idx + 2] = rgba[2];
|
||||
if let Some(window_pixel) = PPU::get_window_pixel(lcd_x, bus) {
|
||||
let rgba = PPU::get_rgba(pixel);
|
||||
frame_buffer[idx] = rgba[0];
|
||||
frame_buffer[idx + 1] = rgba[1];
|
||||
frame_buffer[idx + 2] = rgba[2];
|
||||
}
|
||||
|
||||
lcd_x += 1;
|
||||
} */
|
||||
}
|
||||
}
|
||||
|
||||
@ -398,4 +373,8 @@ impl PPU {
|
||||
PPU::get_pixel(PPU::get_palette((byte1 & 0b01) | ((byte2 << 1) & 0b10), palette)),
|
||||
]
|
||||
}
|
||||
|
||||
pub fn get_rgba_frame(&self) -> &[[u8; 4]; FRAME_BUFFER_LENGTH as usize] {
|
||||
&self.rgba_frame
|
||||
}
|
||||
}
|
||||
|
@ -64,10 +64,10 @@ pub fn start_eventloop() {
|
||||
*control_flow = ControlFlow::Exit
|
||||
},
|
||||
Event::MainEventsCleared => {
|
||||
emulator.run(Cycles(70224), pixels.get_frame());
|
||||
// emulator.draw(pixels.get_frame());
|
||||
emulator.run(Cycles(70224));
|
||||
emulator.draw(pixels.get_frame());
|
||||
|
||||
// thread::sleep(time::Duration::from_millis(1000));
|
||||
// thread::sleep(time::Duration::from_millis(14));
|
||||
window.request_redraw();
|
||||
},
|
||||
Event::RedrawRequested(_) => {
|
||||
|
Loading…
Reference in New Issue
Block a user