Experimental background rendering with scrolling

This commit is contained in:
Franco Colmenarez 2021-10-28 12:58:03 -05:00
parent ce4afa96bf
commit 6ad3f9f29b
2 changed files with 15 additions and 13 deletions

View File

@ -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],

View File

@ -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);