From 6ad3f9f29b30e516c39364776c55cc4d2d74e49d Mon Sep 17 00:00:00 2001 From: Franco Colmenarez Date: Thu, 28 Oct 2021 12:58:03 -0500 Subject: [PATCH] Experimental background rendering with scrolling --- src/bus.rs | 7 ++++--- src/ppu.rs | 21 +++++++++++---------- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/bus.rs b/src/bus.rs index 11ec949..773ff52 100644 --- a/src/bus.rs +++ b/src/bus.rs @@ -40,8 +40,8 @@ pub struct Bus { impl Bus { pub fn new() -> Self { - // let game_rom = match ROM::load_file("ignore/pokemon-yellow.gbc".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("ignore/dmg-acid2.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/03-op sp,hl.gb".to_string()) { // let game_rom = match ROM::load_file("roms/cpu_instrs_individual/04-op r,imm.gb".to_string()) { // let game_rom = match ROM::load_file("roms/cpu_instrs_individual/05-op rp.gb".to_string()) { @@ -52,7 +52,8 @@ impl Bus { // let game_rom = match ROM::load_file("roms/cpu_instrs_individual/10-bit ops.gb".to_string()) { // let game_rom = match ROM::load_file("roms/cpu_instrs_individual/11-op a,(hl).gb".to_string()) { Ok(rom) => rom, - _ => ROM::from_bytes(&[0; 0xFFFF]) + // _ => ROM::from_bytes(&[0; 0xFFFF]) + _ => panic!("Could not read ROM"), }; Self { data: [0x00; 0x10000], diff --git a/src/ppu.rs b/src/ppu.rs index 00db321..c256017 100644 --- a/src/ppu.rs +++ b/src/ppu.rs @@ -53,8 +53,8 @@ pub const FRAME_BUFFER_LENGTH: u32 = WIDTH * HEIGHT; const LCD_CONTROL_ADDRESS: u16 = 0xFF40; const LCD_STATUS_ADDRESS: u16 = 0xFF41; -const SCROLL_X_ADDRESS: u16 = 0xFF42; -const SCROLL_Y_ADDRESS: u16 = 0xFF43; +const SCROLL_Y_ADDRESS: u16 = 0xFF42; +const SCROLL_X_ADDRESS: u16 = 0xFF43; const LCD_Y_ADDRESS: u16 = 0xFF44; const LCD_Y_COMPARE_ADDRESS: u16 = 0xFF45; const DMA_ADDRESS: u16 = 0xFF46; @@ -180,19 +180,20 @@ impl PPU { pub fn draw_background(&mut self, bus: &Bus) { let mut idx = 0; // let mut tile_line: u16 = 0; - let mut lcd_y: u16 = 0; + let mut lcd_y: u8 = 0; while lcd_y < 144 { - let mut lcd_x: u16 = 0; + let mut lcd_x: u8 = 0; while lcd_x < 160 { - let index_x = (lcd_x / 8); - let index_y = (lcd_y / 8) * 32; + let y = lcd_y.wrapping_add(PPU::get_scroll_y(bus)); + let x = lcd_x.wrapping_add(PPU::get_scroll_x(bus)); + let index_x = (x as u16 / 8); + let index_y = (y as u16 / 8) * 32; let index = index_x + index_y; - let tile_line = (lcd_y + PPU::get_scroll_y(bus) as u16).rem_euclid(8) * 2; + let tile_line = (y).rem_euclid(8) * 2; let index_byte = (bus.read(0x9800 + index as u16) as u16) * 16; - let tile_byte_1 = bus.read(0x8000 + tile_line + index_byte as u16); - let tile_byte_2 = bus.read(0x8000 + tile_line + index_byte as u16 + 1); - println!("{} {}", tile_byte_1, tile_byte_2); + let tile_byte_1 = bus.read(0x8000 + tile_line as u16 + index_byte); + let tile_byte_2 = bus.read(0x8000 + tile_line as u16 + index_byte + 1); let pixels = PPU::get_byte_pixels(tile_byte_1, tile_byte_2);