From a61bf8e01a11fb52df3425de44fdc8c3f76bf84e Mon Sep 17 00:00:00 2001 From: Franco Colmenarez Date: Mon, 4 Nov 2024 18:29:55 -0500 Subject: [PATCH] WIP cgram viewer --- snes-core/src/ppu/registers.rs | 4 ++ snes-core/src/utils/color.rs | 10 +-- snes-frontend/src/emu_state/debug_options.rs | 2 + snes-frontend/src/emu_ui/debug/ppu.rs | 67 +++++++++++++++++++- 4 files changed, 77 insertions(+), 6 deletions(-) diff --git a/snes-core/src/ppu/registers.rs b/snes-core/src/ppu/registers.rs index de1b0f4..efb04d3 100644 --- a/snes-core/src/ppu/registers.rs +++ b/snes-core/src/ppu/registers.rs @@ -448,6 +448,10 @@ impl PPURegisters { value } + pub fn read_cgram(&self, address: u8) -> u16 { + self.cgram[address as usize] + } + fn write_cgram(&mut self, data: u8) { let cgram_index = self.get_cgram_index() as usize; match self.cgram_data_read_flipflop { diff --git a/snes-core/src/utils/color.rs b/snes-core/src/utils/color.rs index b45d52a..f8daf1e 100644 --- a/snes-core/src/utils/color.rs +++ b/snes-core/src/utils/color.rs @@ -1,4 +1,4 @@ -pub fn rbg555_to_rgb888(rgb555: (u8, u8, u8)) -> (u8, u8, u8) { +pub fn rgb555_to_rgb888(rgb555: (u8, u8, u8)) -> (u8, u8, u8) { let red = rgb555.0 & 0b11111; let green = rgb555.1 & 0b11111; let blue = rgb555.2 & 0b11111; @@ -15,14 +15,14 @@ mod color_tests { use super::*; #[test] - fn test_rbg555_to_rgb888() { - assert_eq!(rbg555_to_rgb888((0x00, 0x00, 0x00)), (0x00, 0x00, 0x00)); + fn test_rgb555_to_rgb888() { + assert_eq!(rgb555_to_rgb888((0x00, 0x00, 0x00)), (0x00, 0x00, 0x00)); assert_eq!( - rbg555_to_rgb888((0b0001_1111, 0b0001_1111, 0b0001_1111)), + rgb555_to_rgb888((0b0001_1111, 0b0001_1111, 0b0001_1111)), (0xFF, 0xFF, 0xFF) ); assert_eq!( - rbg555_to_rgb888((0b10101, 0b01010, 0b11011)), + rgb555_to_rgb888((0b10101, 0b01010, 0b11011)), (0xAD, 0x52, 0xDE) ); } diff --git a/snes-frontend/src/emu_state/debug_options.rs b/snes-frontend/src/emu_state/debug_options.rs index a456352..cbb5d13 100644 --- a/snes-frontend/src/emu_state/debug_options.rs +++ b/snes-frontend/src/emu_state/debug_options.rs @@ -102,6 +102,7 @@ pub struct PPUDebugControlOptions { pub is_enabled: bool, pub show_registers: bool, pub show_vram: bool, + pub show_cgram: bool, pub vram_inputs: VramInputs, pub vram_inputs_result: VramInputs, pub backgrounds: [BgDebug; 4], @@ -113,6 +114,7 @@ impl PPUDebugControlOptions { is_enabled: true, show_registers: true, show_vram: true, + show_cgram: true, vram_inputs: VramInputs::new(), vram_inputs_result: VramInputs::new(), backgrounds: [ diff --git a/snes-frontend/src/emu_ui/debug/ppu.rs b/snes-frontend/src/emu_ui/debug/ppu.rs index 9cc0704..b9c342e 100644 --- a/snes-frontend/src/emu_ui/debug/ppu.rs +++ b/snes-frontend/src/emu_ui/debug/ppu.rs @@ -1,5 +1,6 @@ -use eframe::egui; +use eframe::egui::{self, Color32, Rect, Ui, Vec2}; use snes_core::ppu::registers::PPURegisters; +use snes_core::utils::color::rgb555_to_rgb888; use crate::emu_state::debug_options::PPUDebugControlOptions; use crate::emu_ui::debug::common::sanitize_input; @@ -45,6 +46,7 @@ pub fn build_ppu_debug_controls(ctx: &egui::Context, ppu_debug_options: &mut PPU build_ppu_registers_window(ctx, ppu_debug_options, ppu_registers); build_vram_window(ctx, ppu_debug_options, ppu_registers); + build_cgram_window(ctx, ppu_debug_options, ppu_registers); } fn build_ppu_registers_window(ctx: &egui::Context, ppu_debug_options: &mut PPUDebugControlOptions, ppu_registers: &PPURegisters) { @@ -195,3 +197,66 @@ fn build_vram_window(ctx: &egui::Context, ppu_debug_options: &mut PPUDebugContro }); }); } + +fn build_cgram_window(ctx: &egui::Context, ppu_debug_options: &mut PPUDebugControlOptions, ppu_registers: &PPURegisters) { + if !ppu_debug_options.show_cgram { + return; + } + egui::Window::new("CGRAM Viewer") + .open(&mut ppu_debug_options.show_cgram) + .default_width(610.0) + .max_width(610.0) + .show(ctx, |ui| { + egui::ScrollArea::both().show(ui, |ui| { + let address_start = 0x00; + let address_end = 0xFF; + let mut header = String::from(" | "); + for page in 0x00..=0x0F { + header = format!("{} {:02X} ", header, page); + } + ui.monospace(header); + let mut divider = String::from("-----|-"); + for _ in 0x00..=0x0F { + divider = format!("{}-----", divider); + } + ui.monospace(divider); + let vector = (address_start..=address_end).collect::>(); + let chunks = vector.chunks(0x10); + for row in chunks { + let mut address_row = format!("{:04X} | ", row[0]); + for address in row { + address_row = format!("{}{:04X} ", address_row, ppu_registers.cgram()[((*address) & 0x7FFF) as usize]); + } + ui.monospace(address_row); + } + }); + + ui.separator(); + + ui.horizontal(|ui| { + ui.label("0x00: "); + paint_cgram_color_address(ui, 0x00, ppu_registers); + ui.label("0x01: "); + paint_cgram_color_address(ui, 0x01, ppu_registers); + ui.label("0x02: "); + paint_cgram_color_address(ui, 0x02, ppu_registers); + }); + }); +} + +fn paint_cgram_color_address(ui: &mut Ui, address: u8, ppu_registers: &PPURegisters) { + let rgb555_value = ppu_registers.read_cgram(address); + let rgb888_value = rgb555_to_rgb888(( + (rgb555_value & 0b11111) as u8, + ((rgb555_value >> 5) & 0b11111) as u8, + ((rgb555_value >> 10) & 0b11111) as u8, + )); + paint_square_color(ui, rgb888_value); +} + +fn paint_square_color(ui: &mut Ui, color: (u8, u8, u8)) { + let (_, painter) = ui.allocate_painter(Vec2::splat(15.0), egui::Sense::hover()); + let square_color = Color32::from_rgb(color.0, color.1, color.2); + let rect = Rect::EVERYTHING; + painter.rect_filled(rect, 0.0, square_color); +}