New implementation for reading/showing vram
This commit is contained in:
+1
-1
@@ -377,7 +377,7 @@ impl AppWindow for GameWindow {
|
||||
toasts.show(ctx);
|
||||
}
|
||||
|
||||
fn on_init(&mut self, render_state: &egui_wgpu::RenderState) {
|
||||
fn on_init(&mut self, _ctx: &Context, render_state: &egui_wgpu::RenderState) {
|
||||
let (screen, sink) = GameScreen::init(render_state);
|
||||
let (message_sink, message_source) = mpsc::channel();
|
||||
self.client.send_command(EmulatorCommand::ConnectToSim(
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
mod bgmap;
|
||||
mod chardata;
|
||||
mod utils;
|
||||
|
||||
pub use bgmap::*;
|
||||
pub use chardata::*;
|
||||
|
||||
+127
-5
@@ -1,14 +1,28 @@
|
||||
use egui::{CentralPanel, Context, ViewportBuilder, ViewportId};
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::{emulator::SimId, window::AppWindow};
|
||||
use egui::{CentralPanel, ColorImage, Context, Image, TextureOptions, ViewportBuilder, ViewportId};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{
|
||||
emulator::SimId,
|
||||
memory::{MemoryMonitor, MemoryView},
|
||||
vram::{VramImageLoader, VramResource as _, VramTextureLoader},
|
||||
window::AppWindow,
|
||||
};
|
||||
|
||||
use super::utils::parse_palette;
|
||||
|
||||
pub struct BgMapWindow {
|
||||
sim_id: SimId,
|
||||
loader: Option<BgMapLoader>,
|
||||
}
|
||||
|
||||
impl BgMapWindow {
|
||||
pub fn new(sim_id: SimId) -> Self {
|
||||
Self { sim_id }
|
||||
pub fn new(sim_id: SimId, memory: &mut MemoryMonitor) -> Self {
|
||||
Self {
|
||||
sim_id,
|
||||
loader: Some(BgMapLoader::new(sim_id, memory)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,13 +31,121 @@ impl AppWindow for BgMapWindow {
|
||||
ViewportId::from_hash_of(format!("bgmap-{}", self.sim_id))
|
||||
}
|
||||
|
||||
fn sim_id(&self) -> SimId {
|
||||
self.sim_id
|
||||
}
|
||||
|
||||
fn initial_viewport(&self) -> ViewportBuilder {
|
||||
ViewportBuilder::default()
|
||||
.with_title(format!("BG Map Data ({})", self.sim_id))
|
||||
.with_inner_size((640.0, 480.0))
|
||||
}
|
||||
|
||||
fn on_init(&mut self, ctx: &Context, _render_state: &egui_wgpu::RenderState) {
|
||||
let loader = self.loader.take().unwrap();
|
||||
ctx.add_texture_loader(Arc::new(VramTextureLoader::new(loader)));
|
||||
}
|
||||
|
||||
fn show(&mut self, ctx: &Context) {
|
||||
CentralPanel::default().show(ctx, |ui| ui.label("TODO"));
|
||||
CentralPanel::default().show(ctx, |ui| {
|
||||
let resource = BgMapResource { index: 0 };
|
||||
let image = Image::new(resource.to_uri()).texture_options(TextureOptions::NEAREST);
|
||||
ui.add(image);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, PartialEq, Eq, Hash)]
|
||||
struct BgMapResource {
|
||||
index: usize,
|
||||
}
|
||||
|
||||
struct BgMapLoader {
|
||||
chardata: MemoryView,
|
||||
bgmaps: MemoryView,
|
||||
brightness: MemoryView,
|
||||
palettes: MemoryView,
|
||||
}
|
||||
|
||||
impl BgMapLoader {
|
||||
pub fn new(sim_id: SimId, memory: &mut MemoryMonitor) -> Self {
|
||||
Self {
|
||||
chardata: memory.view(sim_id, 0x00078000, 0x8000),
|
||||
bgmaps: memory.view(sim_id, 0x00020000, 0x1d800),
|
||||
brightness: memory.view(sim_id, 0x0005f824, 8),
|
||||
palettes: memory.view(sim_id, 0x0005f860, 16),
|
||||
}
|
||||
}
|
||||
|
||||
fn load_bgmap(&self, index: usize) -> Option<ColorImage> {
|
||||
let chardata = self.chardata.borrow();
|
||||
let bgmaps = self.bgmaps.borrow();
|
||||
let brightness = self.brightness.borrow();
|
||||
let palettes = self.palettes.borrow();
|
||||
|
||||
let brts = brightness.range::<u8>(0, 8);
|
||||
let colors = [
|
||||
parse_palette(palettes.read(0), brts),
|
||||
parse_palette(palettes.read(2), brts),
|
||||
parse_palette(palettes.read(4), brts),
|
||||
parse_palette(palettes.read(6), brts),
|
||||
];
|
||||
|
||||
let mut data = vec![0u8; 512 * 512];
|
||||
for (i, cell) in bgmaps.range::<u16>(index * 4096, 4096).iter().enumerate() {
|
||||
let char_index = (cell & 0x7ff) as usize;
|
||||
let char = chardata.range::<u16>(char_index * 8, 8);
|
||||
let vflip = cell & 0x1000 != 0;
|
||||
let hflip = cell & 0x2000 != 0;
|
||||
let palette_index = (cell >> 14) as usize;
|
||||
let palette = &colors[palette_index];
|
||||
|
||||
let mut target_idx = (i % 64) * 8 + (i / 64) * 8 * 512;
|
||||
for row in 0..8 {
|
||||
let dests = &mut data[target_idx..target_idx + 8];
|
||||
let pixels = self.read_char_row(char, hflip, vflip, row);
|
||||
for (dest, pixel) in dests.iter_mut().zip(pixels) {
|
||||
*dest = palette[pixel as usize];
|
||||
}
|
||||
target_idx += 512;
|
||||
}
|
||||
}
|
||||
|
||||
Some(ColorImage::from_gray([512, 512], &data))
|
||||
}
|
||||
|
||||
fn read_char_row(
|
||||
&self,
|
||||
char: &[u16],
|
||||
hflip: bool,
|
||||
vflip: bool,
|
||||
row: usize,
|
||||
) -> impl Iterator<Item = u8> {
|
||||
let pixels = if vflip { char[7 - row] } else { char[row] };
|
||||
(0..16).step_by(2).map(move |i| {
|
||||
let pixel = if hflip { 14 - i } else { i };
|
||||
((pixels >> pixel) & 0x3) as u8
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl VramImageLoader for BgMapLoader {
|
||||
type Resource = BgMapResource;
|
||||
|
||||
fn id(&self) -> &str {
|
||||
concat!(module_path!(), "::BgMapLoader")
|
||||
}
|
||||
|
||||
fn add(&self, resource: &Self::Resource) -> Option<ColorImage> {
|
||||
let BgMapResource { index } = resource;
|
||||
self.load_bgmap(*index)
|
||||
}
|
||||
|
||||
fn update<'a>(
|
||||
&'a self,
|
||||
resources: impl Iterator<Item = &'a Self::Resource>,
|
||||
) -> Vec<(&'a Self::Resource, ColorImage)> {
|
||||
let _ = resources;
|
||||
vec![]
|
||||
}
|
||||
}
|
||||
|
||||
+194
-13
@@ -1,17 +1,86 @@
|
||||
use std::{fmt::Display, sync::Arc};
|
||||
|
||||
use egui::{
|
||||
Align, CentralPanel, Color32, ComboBox, Context, Frame, Image, RichText, ScrollArea, Sense,
|
||||
Slider, TextEdit, TextureOptions, Ui, UiBuilder, Vec2, ViewportBuilder, ViewportId,
|
||||
Align, CentralPanel, Color32, ColorImage, ComboBox, Context, Frame, Image, RichText,
|
||||
ScrollArea, Sense, Slider, TextEdit, TextureOptions, Ui, UiBuilder, Vec2, ViewportBuilder,
|
||||
ViewportId,
|
||||
};
|
||||
use egui_extras::{Column, Size, StripBuilder, TableBuilder};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{
|
||||
emulator::SimId,
|
||||
vram::{VramPalette, VramResource},
|
||||
memory::{MemoryMonitor, MemoryView},
|
||||
vram::{VramImageLoader, VramResource as _, VramTextureLoader},
|
||||
window::AppWindow,
|
||||
};
|
||||
|
||||
use super::utils;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
||||
pub enum VramPalette {
|
||||
Generic,
|
||||
Bg0,
|
||||
Bg1,
|
||||
Bg2,
|
||||
Bg3,
|
||||
Obj0,
|
||||
Obj1,
|
||||
Obj2,
|
||||
Obj3,
|
||||
}
|
||||
|
||||
impl VramPalette {
|
||||
pub const fn values() -> [VramPalette; 9] {
|
||||
[
|
||||
Self::Generic,
|
||||
Self::Bg0,
|
||||
Self::Bg1,
|
||||
Self::Bg2,
|
||||
Self::Bg3,
|
||||
Self::Obj0,
|
||||
Self::Obj1,
|
||||
Self::Obj2,
|
||||
Self::Obj3,
|
||||
]
|
||||
}
|
||||
|
||||
pub const fn offset(self) -> Option<usize> {
|
||||
match self {
|
||||
Self::Generic => None,
|
||||
Self::Bg0 => Some(0),
|
||||
Self::Bg1 => Some(2),
|
||||
Self::Bg2 => Some(4),
|
||||
Self::Bg3 => Some(6),
|
||||
Self::Obj0 => Some(8),
|
||||
Self::Obj1 => Some(10),
|
||||
Self::Obj2 => Some(12),
|
||||
Self::Obj3 => Some(14),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for VramPalette {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Self::Generic => f.write_str("Generic"),
|
||||
Self::Bg0 => f.write_str("BG 0"),
|
||||
Self::Bg1 => f.write_str("BG 1"),
|
||||
Self::Bg2 => f.write_str("BG 2"),
|
||||
Self::Bg3 => f.write_str("BG 3"),
|
||||
Self::Obj0 => f.write_str("OBJ 0"),
|
||||
Self::Obj1 => f.write_str("OBJ 1"),
|
||||
Self::Obj2 => f.write_str("OBJ 2"),
|
||||
Self::Obj3 => f.write_str("OBJ 3"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct CharacterDataWindow {
|
||||
sim_id: SimId,
|
||||
loader: Option<CharDataLoader>,
|
||||
brightness: MemoryView,
|
||||
palettes: MemoryView,
|
||||
palette: VramPalette,
|
||||
index: usize,
|
||||
index_str: String,
|
||||
@@ -20,9 +89,12 @@ pub struct CharacterDataWindow {
|
||||
}
|
||||
|
||||
impl CharacterDataWindow {
|
||||
pub fn new(sim_id: SimId) -> Self {
|
||||
pub fn new(sim_id: SimId, memory: &mut MemoryMonitor) -> Self {
|
||||
Self {
|
||||
sim_id,
|
||||
loader: Some(CharDataLoader::new(sim_id, memory)),
|
||||
brightness: memory.view(sim_id, 0x0005f824, 8),
|
||||
palettes: memory.view(sim_id, 0x0005f860, 16),
|
||||
palette: VramPalette::Generic,
|
||||
index: 0,
|
||||
index_str: "0".into(),
|
||||
@@ -89,7 +161,10 @@ impl CharacterDataWindow {
|
||||
});
|
||||
});
|
||||
});
|
||||
let resource = VramResource::character(self.sim_id, self.palette, self.index);
|
||||
let resource = CharDataResource::Character {
|
||||
palette: self.palette,
|
||||
index: self.index,
|
||||
};
|
||||
let image = Image::new(resource.to_uri())
|
||||
.maintain_aspect_ratio(true)
|
||||
.tint(Color32::RED)
|
||||
@@ -114,18 +189,18 @@ impl CharacterDataWindow {
|
||||
TableBuilder::new(ui)
|
||||
.columns(Column::remainder(), 4)
|
||||
.body(|mut body| {
|
||||
let palette = self.load_palette_colors();
|
||||
body.row(30.0, |mut row| {
|
||||
for index in 0..4 {
|
||||
let resource =
|
||||
VramResource::palette_color(self.sim_id, self.palette, index);
|
||||
for color in palette {
|
||||
row.col(|ui| {
|
||||
let rect = ui.available_rect_before_wrap();
|
||||
let scale = rect.height() / rect.width();
|
||||
let rect = rect.scale_from_center2(Vec2::new(scale, 1.0));
|
||||
let image = Image::new(resource.to_uri())
|
||||
.tint(Color32::RED)
|
||||
.fit_to_exact_size(rect.max - rect.min);
|
||||
ui.put(rect, image);
|
||||
ui.painter().rect_filled(
|
||||
rect,
|
||||
0.0,
|
||||
Color32::RED * Color32::from_gray(color),
|
||||
);
|
||||
});
|
||||
}
|
||||
});
|
||||
@@ -145,9 +220,21 @@ impl CharacterDataWindow {
|
||||
});
|
||||
}
|
||||
|
||||
fn load_palette_colors(&self) -> [u8; 4] {
|
||||
let Some(offset) = self.palette.offset() else {
|
||||
return utils::GENERIC_PALETTE;
|
||||
};
|
||||
let palette = self.palettes.borrow().read(offset);
|
||||
let brightnesses = self.brightness.borrow();
|
||||
let brts = brightnesses.range(0, 8);
|
||||
utils::parse_palette(palette, brts)
|
||||
}
|
||||
|
||||
fn show_chardata(&mut self, ui: &mut Ui) {
|
||||
let start_pos = ui.cursor().min;
|
||||
let resource = VramResource::character_data(self.sim_id, self.palette);
|
||||
let resource = CharDataResource::CharacterData {
|
||||
palette: self.palette,
|
||||
};
|
||||
let image = Image::new(resource.to_uri())
|
||||
.fit_to_original_size(self.scale)
|
||||
.tint(Color32::RED)
|
||||
@@ -210,6 +297,11 @@ impl AppWindow for CharacterDataWindow {
|
||||
.with_inner_size((640.0, 480.0))
|
||||
}
|
||||
|
||||
fn on_init(&mut self, ctx: &Context, _render_state: &egui_wgpu::RenderState) {
|
||||
let loader = self.loader.take().unwrap();
|
||||
ctx.add_texture_loader(Arc::new(VramTextureLoader::new(loader)));
|
||||
}
|
||||
|
||||
fn show(&mut self, ctx: &Context) {
|
||||
CentralPanel::default().show(ctx, |ui| {
|
||||
ui.horizontal_top(|ui| {
|
||||
@@ -229,6 +321,95 @@ impl AppWindow for CharacterDataWindow {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, PartialEq, Eq, Hash)]
|
||||
enum CharDataResource {
|
||||
Character { palette: VramPalette, index: usize },
|
||||
CharacterData { palette: VramPalette },
|
||||
}
|
||||
|
||||
struct CharDataLoader {
|
||||
chardata: MemoryView,
|
||||
brightness: MemoryView,
|
||||
palettes: MemoryView,
|
||||
}
|
||||
|
||||
impl CharDataLoader {
|
||||
pub fn new(sim_id: SimId, memory: &mut MemoryMonitor) -> Self {
|
||||
Self {
|
||||
chardata: memory.view(sim_id, 0x00078000, 0x8000),
|
||||
brightness: memory.view(sim_id, 0x0005f824, 8),
|
||||
palettes: memory.view(sim_id, 0x0005f860, 16),
|
||||
}
|
||||
}
|
||||
|
||||
fn load_character(&self, palette: VramPalette, index: usize) -> Option<ColorImage> {
|
||||
if index >= 2048 {
|
||||
return None;
|
||||
}
|
||||
let palette = self.load_palette(palette);
|
||||
let chardata = self.chardata.borrow();
|
||||
let character = chardata.range::<u16>(index * 8, 8);
|
||||
let mut buffer = Vec::with_capacity(8 * 8);
|
||||
for row in character {
|
||||
for offset in (0..16).step_by(2) {
|
||||
let char = (row >> offset) & 0x3;
|
||||
buffer.push(palette[char as usize]);
|
||||
}
|
||||
}
|
||||
Some(ColorImage::from_gray([8, 8], &buffer))
|
||||
}
|
||||
|
||||
fn load_character_data(&self, palette: VramPalette) -> Option<ColorImage> {
|
||||
let palette = self.load_palette(palette);
|
||||
let chardata = self.chardata.borrow();
|
||||
let mut buffer = vec![0; 8 * 8 * 2048];
|
||||
for (i, row) in chardata.range::<u16>(0, 2048).iter().enumerate() {
|
||||
let bytes =
|
||||
[0, 2, 4, 6, 8, 10, 12, 14].map(|off| palette[(*row as usize >> off) & 0x3]);
|
||||
let char_index = i / 8;
|
||||
let row_index = i % 8;
|
||||
let x = (char_index % 16) * 8;
|
||||
let y = (char_index / 16) * 8 + row_index;
|
||||
let write_index = (y * 16 * 8) + x;
|
||||
buffer[write_index..write_index + 8].copy_from_slice(&bytes);
|
||||
}
|
||||
Some(ColorImage::from_gray([8 * 16, 8 * 128], &buffer))
|
||||
}
|
||||
|
||||
fn load_palette(&self, palette: VramPalette) -> [u8; 4] {
|
||||
let Some(offset) = palette.offset() else {
|
||||
return utils::GENERIC_PALETTE;
|
||||
};
|
||||
let palette = self.palettes.borrow().read(offset);
|
||||
let brightnesses = self.brightness.borrow();
|
||||
let brts = brightnesses.range(0, 8);
|
||||
utils::parse_palette(palette, brts)
|
||||
}
|
||||
}
|
||||
|
||||
impl VramImageLoader for CharDataLoader {
|
||||
type Resource = CharDataResource;
|
||||
|
||||
fn id(&self) -> &str {
|
||||
concat!(module_path!(), "::CharDataLoader")
|
||||
}
|
||||
|
||||
fn add(&self, resource: &Self::Resource) -> Option<ColorImage> {
|
||||
match resource {
|
||||
CharDataResource::Character { palette, index } => self.load_character(*palette, *index),
|
||||
CharDataResource::CharacterData { palette } => self.load_character_data(*palette),
|
||||
}
|
||||
}
|
||||
|
||||
fn update<'a>(
|
||||
&'a self,
|
||||
resources: impl Iterator<Item = &'a Self::Resource>,
|
||||
) -> Vec<(&'a Self::Resource, ColorImage)> {
|
||||
let _ = resources;
|
||||
vec![]
|
||||
}
|
||||
}
|
||||
|
||||
trait UiExt {
|
||||
fn section(&mut self, title: impl Into<String>, add_contents: impl FnOnce(&mut Ui));
|
||||
}
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
pub const GENERIC_PALETTE: [u8; 4] = [0, 64, 128, 255];
|
||||
|
||||
pub fn parse_palette(palette: u8, brts: &[u8]) -> [u8; 4] {
|
||||
let shades = [
|
||||
0,
|
||||
brts[0],
|
||||
brts[2],
|
||||
brts[0].saturating_add(brts[2]).saturating_add(brts[4]),
|
||||
];
|
||||
[
|
||||
0,
|
||||
shades[(palette >> 2) as usize & 0x03],
|
||||
shades[(palette >> 4) as usize & 0x03],
|
||||
shades[(palette >> 6) as usize & 0x03],
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user