From e6c7af89fba08810ca1beaa38968a381b1c3fe89 Mon Sep 17 00:00:00 2001 From: Franco Colmenarez Date: Sun, 21 Jan 2024 00:23:15 -0500 Subject: [PATCH] Fix issues when displaying the background tiles --- snes-core/src/cpu/instructions/brl.rs | 2 +- .../src/cpu/instructions/decoder_common.rs | 18 ++++++++++++++---- snes-core/src/cpu/instructions/pea.rs | 2 +- snes-core/src/cpu/instructions/per.rs | 2 +- snes-core/src/cpu/internal_registers.rs | 6 ++++-- snes-core/src/rom/lo_rom.rs | 2 +- snes-frontend/src/emu_state/debug_options.rs | 2 ++ snes-frontend/src/emu_ui/debug/ppu.rs | 6 ++++-- snes-frontend/src/emu_ui/debug/ppu_graphics.rs | 5 +++-- 9 files changed, 31 insertions(+), 14 deletions(-) diff --git a/snes-core/src/cpu/instructions/brl.rs b/snes-core/src/cpu/instructions/brl.rs index 38d6a67..4847f84 100644 --- a/snes-core/src/cpu/instructions/brl.rs +++ b/snes-core/src/cpu/instructions/brl.rs @@ -24,7 +24,7 @@ impl CPUInstruction for BRL { } fn mnemonic(&self, registers: &Registers, bus: &Bus, opcode: u8) -> String { - decoder_common::mnemonic_absolute(opcode, INSTR_NAME, registers, bus) + decoder_common::mnemonic_absolute_16bit(opcode, INSTR_NAME, registers, bus) } } diff --git a/snes-core/src/cpu/instructions/decoder_common.rs b/snes-core/src/cpu/instructions/decoder_common.rs index 1838eea..0b388a0 100644 --- a/snes-core/src/cpu/instructions/decoder_common.rs +++ b/snes-core/src/cpu/instructions/decoder_common.rs @@ -10,7 +10,10 @@ pub fn mnemonic_arithmetic(is_16bit: bool, opcode: u8, instr_name: &str, address true => mnemonic_16bit_immediate(opcode, instr_name, registers, bus), false => mnemonic_8bit_immediate(opcode, instr_name, registers, bus), }, - A::Absolute => mnemonic_absolute(opcode, instr_name, registers, bus), + A::Absolute => match is_16bit { + true => mnemonic_absolute_16bit(opcode, instr_name, registers, bus), + false => mnemonic_absolute_8bit(opcode, instr_name, registers, bus), + }, A::AbsoluteLong => mnemonic_absolute_long(opcode, instr_name, registers, bus), A::DirectPage => mnemonic_direct_page(opcode, instr_name, registers, bus), A::DirectPageIndirect => mnemonic_direct_page_indirect(opcode, instr_name, registers, bus), @@ -38,14 +41,21 @@ pub fn mnemonic_8bit_immediate(opcode: u8, instr_name: &str, registers: &Registe pub fn mnemonic_16bit_immediate(opcode: u8, instr_name: &str, registers: &Registers, bus: &Bus) -> String { let next_byte = bus.read_external(registers.get_pc_address() + 1); let next_second_byte = bus.read_external(registers.get_pc_address() + 2); - let word = (next_byte as u16) | ((next_byte as u16) << 8); + let word = (next_byte as u16) | ((next_second_byte as u16) << 8); format!("{:02X} {:02X} {:02X} __ | {} #${:04X}", opcode, next_byte, next_second_byte, instr_name, word) } -pub fn mnemonic_absolute(opcode: u8, instr_name: &str, registers: &Registers, bus: &Bus) -> String { +pub fn mnemonic_absolute_8bit(opcode: u8, instr_name: &str, registers: &Registers, bus: &Bus) -> String { let next_byte = bus.read_external(registers.get_pc_address() + 1); let next_second_byte = bus.read_external(registers.get_pc_address() + 2); - let word = (next_byte as u16) | ((next_byte as u16) << 8); + let word = (next_byte as u16) | ((next_second_byte as u16) << 8); + format!("{:02X} {:02X} {:02X} __ | {} ${:04X}", opcode, next_byte, next_second_byte, instr_name, word) +} + +pub fn mnemonic_absolute_16bit(opcode: u8, instr_name: &str, registers: &Registers, bus: &Bus) -> String { + let next_byte = bus.read_external(registers.get_pc_address() + 1); + let next_second_byte = bus.read_external(registers.get_pc_address() + 2); + let word = (next_byte as u16) | ((next_second_byte as u16) << 8); format!("{:02X} {:02X} {:02X} __ | {} ${:04X}", opcode, next_byte, next_second_byte, instr_name, word) } diff --git a/snes-core/src/cpu/instructions/pea.rs b/snes-core/src/cpu/instructions/pea.rs index 1e7b808..913adf9 100644 --- a/snes-core/src/cpu/instructions/pea.rs +++ b/snes-core/src/cpu/instructions/pea.rs @@ -19,7 +19,7 @@ impl CPUInstruction for PEA { } fn mnemonic(&self, registers: &Registers, bus: &Bus, opcode: u8) -> String { - decoder_common::mnemonic_absolute(opcode, INSTR_NAME, registers, bus) + decoder_common::mnemonic_absolute_16bit(opcode, INSTR_NAME, registers, bus) } } diff --git a/snes-core/src/cpu/instructions/per.rs b/snes-core/src/cpu/instructions/per.rs index 77c7155..92b848b 100644 --- a/snes-core/src/cpu/instructions/per.rs +++ b/snes-core/src/cpu/instructions/per.rs @@ -24,7 +24,7 @@ impl CPUInstruction for PER { } fn mnemonic(&self, registers: &Registers, bus: &Bus, opcode: u8) -> String { - decoder_common::mnemonic_absolute(opcode, INSTR_NAME, registers, bus) + decoder_common::mnemonic_absolute_16bit(opcode, INSTR_NAME, registers, bus) } } diff --git a/snes-core/src/cpu/internal_registers.rs b/snes-core/src/cpu/internal_registers.rs index dc92c06..dd44fc3 100644 --- a/snes-core/src/cpu/internal_registers.rs +++ b/snes-core/src/cpu/internal_registers.rs @@ -57,9 +57,11 @@ impl InternalRegisters { } fn read_vblank_nmi_mut(&self, ppu_registers: &mut PPURegisters) -> u8 { - let byte = self._read(RDNMI); + let result = self.read_vblank_nmi(ppu_registers); + if result == 0x80 { + println!("nmi set"); + } // When register is read, bit 7 is cleared - let result = (byte & 0x7F) | ((ppu_registers.vblank_nmi as u8) << 7); ppu_registers.vblank_nmi = false; result } diff --git a/snes-core/src/rom/lo_rom.rs b/snes-core/src/rom/lo_rom.rs index 9d96d1a..5288dea 100644 --- a/snes-core/src/rom/lo_rom.rs +++ b/snes-core/src/rom/lo_rom.rs @@ -27,7 +27,7 @@ impl ROM for LoROM { let address = LoROM::adjust_address(address); match self.data.get(address as usize) { Some(byte) => *byte, - None => 0xFF, + None => 0x00, } } diff --git a/snes-frontend/src/emu_state/debug_options.rs b/snes-frontend/src/emu_state/debug_options.rs index 9b9af1f..94ceb57 100644 --- a/snes-frontend/src/emu_state/debug_options.rs +++ b/snes-frontend/src/emu_state/debug_options.rs @@ -103,6 +103,7 @@ pub struct PPUDebugControlOptions { pub show_registers: bool, pub show_vram: bool, pub vram_inputs: VramInputs, + pub vram_inputs_result: VramInputs, pub backgrounds: [BgDebug; 4], } @@ -113,6 +114,7 @@ impl PPUDebugControlOptions { show_registers: true, show_vram: true, vram_inputs: VramInputs::new(), + vram_inputs_result: VramInputs::new(), backgrounds: [ BgDebug::new(PPUBg::Bg1), BgDebug::new(PPUBg::Bg2), diff --git a/snes-frontend/src/emu_ui/debug/ppu.rs b/snes-frontend/src/emu_ui/debug/ppu.rs index afd2a72..9cc0704 100644 --- a/snes-frontend/src/emu_ui/debug/ppu.rs +++ b/snes-frontend/src/emu_ui/debug/ppu.rs @@ -164,13 +164,15 @@ fn build_vram_window(ctx: &egui::Context, ppu_debug_options: &mut PPUDebugContro if ui.button("Search").clicked() { sanitize_input(&mut ppu_debug_options.vram_inputs.address_start, false); sanitize_input(&mut ppu_debug_options.vram_inputs.address_end, false); + ppu_debug_options.vram_inputs_result.address_start = ppu_debug_options.vram_inputs.address_start.to_string(); + ppu_debug_options.vram_inputs_result.address_end = ppu_debug_options.vram_inputs.address_end.to_string(); } ui.separator(); egui::ScrollArea::both().show(ui, |ui| { - let address_start = u16::from_str_radix(&ppu_debug_options.vram_inputs.address_start, 16).unwrap(); - let address_end = u16::from_str_radix(&ppu_debug_options.vram_inputs.address_end, 16).unwrap(); + let address_start = u16::from_str_radix(&ppu_debug_options.vram_inputs_result.address_start, 16).unwrap(); + let address_end = u16::from_str_radix(&ppu_debug_options.vram_inputs_result.address_end, 16).unwrap(); let mut header = String::from(" | "); for page in 0x00..=0x0F { header = format!("{} {:02X} ", header, page); diff --git a/snes-frontend/src/emu_ui/debug/ppu_graphics.rs b/snes-frontend/src/emu_ui/debug/ppu_graphics.rs index 4d695b4..4ea0a4f 100644 --- a/snes-frontend/src/emu_ui/debug/ppu_graphics.rs +++ b/snes-frontend/src/emu_ui/debug/ppu_graphics.rs @@ -134,7 +134,8 @@ fn compute_2bpp_bg_background_framebuffer(background: Background, framebuffer: & for y in 0..height { for x in 0..width { let current_tile = (x / tile_size_width) + ((y / tile_size_height) * bg_size_width); - let char_index = vram[(tileset_vram_base_address + current_tile) & 0x7FFF] & 0b11_11111111; + let tile_byte = vram[tileset_vram_base_address + current_tile]; + let char_index = tile_byte & 0b11_11111111; let current_char_column = x.rem_euclid(tile_size_width); let current_char_row = y.rem_euclid(tile_size_height); // 8x8 pixels, 2 bitplanes, each word (16bit) holds 8 pixels @@ -225,7 +226,7 @@ fn paint_texture(ui: &mut Ui, texture: &mut Option, framebuffer: TextureOptions::default(), ); let (whole_rect, _) = - ui.allocate_exact_size(Vec2::from([width as f32, height as f32]), egui::Sense::focusable_noninteractive()); + ui.allocate_exact_size(Vec2::from([(width * 2) as f32, (height * 2) as f32]), egui::Sense::focusable_noninteractive()); egui::Image::new(( txt.id(), txt.size_vec2(),