Compare commits
No commits in common. "vram" and "main" have entirely different histories.
21
src/app.rs
21
src/app.rs
|
@ -18,12 +18,8 @@ use crate::{
|
|||
controller::ControllerManager,
|
||||
emulator::{EmulatorClient, EmulatorCommand, SimId},
|
||||
input::MappingProvider,
|
||||
memory::MemoryMonitor,
|
||||
persistence::Persistence,
|
||||
window::{
|
||||
AboutWindow, AppWindow, BgMapWindow, CharacterDataWindow, GameWindow, GdbServerWindow,
|
||||
InputWindow,
|
||||
},
|
||||
window::{AboutWindow, AppWindow, GameWindow, GdbServerWindow, InputWindow},
|
||||
};
|
||||
|
||||
fn load_icon() -> anyhow::Result<IconData> {
|
||||
|
@ -43,7 +39,6 @@ pub struct Application {
|
|||
proxy: EventLoopProxy<UserEvent>,
|
||||
mappings: MappingProvider,
|
||||
controllers: ControllerManager,
|
||||
memory: MemoryMonitor,
|
||||
persistence: Persistence,
|
||||
viewports: HashMap<ViewportId, Viewport>,
|
||||
focused: Option<ViewportId>,
|
||||
|
@ -60,7 +55,6 @@ impl Application {
|
|||
let persistence = Persistence::new();
|
||||
let mappings = MappingProvider::new(persistence.clone());
|
||||
let controllers = ControllerManager::new(client.clone(), &mappings);
|
||||
let memory = MemoryMonitor::new(client.clone());
|
||||
{
|
||||
let mappings = mappings.clone();
|
||||
let proxy = proxy.clone();
|
||||
|
@ -71,7 +65,6 @@ impl Application {
|
|||
client,
|
||||
proxy,
|
||||
mappings,
|
||||
memory,
|
||||
controllers,
|
||||
persistence,
|
||||
viewports: HashMap::new(),
|
||||
|
@ -201,14 +194,6 @@ impl ApplicationHandler<UserEvent> for Application {
|
|||
let about = AboutWindow;
|
||||
self.open(event_loop, Box::new(about));
|
||||
}
|
||||
UserEvent::OpenCharacterData(sim_id) => {
|
||||
let vram = CharacterDataWindow::new(sim_id, &mut self.memory);
|
||||
self.open(event_loop, Box::new(vram));
|
||||
}
|
||||
UserEvent::OpenBgMap(sim_id) => {
|
||||
let bgmap = BgMapWindow::new(sim_id, &mut self.memory);
|
||||
self.open(event_loop, Box::new(bgmap));
|
||||
}
|
||||
UserEvent::OpenDebugger(sim_id) => {
|
||||
let debugger =
|
||||
GdbServerWindow::new(sim_id, self.client.clone(), self.proxy.clone());
|
||||
|
@ -315,7 +300,7 @@ impl Viewport {
|
|||
let (window, state) = create_window_and_state(&ctx, event_loop, &builder, &mut painter);
|
||||
egui_winit::update_viewport_info(&mut info, &ctx, &window, true);
|
||||
|
||||
app.on_init(&ctx, painter.render_state().as_ref().unwrap());
|
||||
app.on_init(painter.render_state().as_ref().unwrap());
|
||||
Self {
|
||||
painter,
|
||||
ctx,
|
||||
|
@ -418,8 +403,6 @@ impl Drop for Viewport {
|
|||
pub enum UserEvent {
|
||||
GamepadEvent(gilrs::Event),
|
||||
OpenAbout,
|
||||
OpenCharacterData(SimId),
|
||||
OpenBgMap(SimId),
|
||||
OpenDebugger(SimId),
|
||||
OpenInput,
|
||||
OpenPlayer2,
|
||||
|
|
|
@ -19,9 +19,7 @@ mod emulator;
|
|||
mod gdbserver;
|
||||
mod graphics;
|
||||
mod input;
|
||||
mod memory;
|
||||
mod persistence;
|
||||
mod vram;
|
||||
mod window;
|
||||
|
||||
#[derive(Parser)]
|
||||
|
|
|
@ -1,88 +0,0 @@
|
|||
use std::{
|
||||
collections::HashMap,
|
||||
sync::{Arc, Mutex, MutexGuard},
|
||||
};
|
||||
|
||||
use bytemuck::BoxBytes;
|
||||
|
||||
use crate::emulator::{EmulatorClient, EmulatorCommand, SimId};
|
||||
|
||||
pub struct MemoryMonitor {
|
||||
client: EmulatorClient,
|
||||
regions: HashMap<MemoryRegion, Arc<Mutex<BoxBytes>>>,
|
||||
}
|
||||
|
||||
impl MemoryMonitor {
|
||||
pub fn new(client: EmulatorClient) -> Self {
|
||||
Self {
|
||||
client,
|
||||
regions: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn view(&mut self, sim: SimId, start: u32, length: usize) -> MemoryView {
|
||||
let region = MemoryRegion { sim, start, length };
|
||||
let memory = self.regions.entry(region).or_insert_with(|| {
|
||||
let mut buf = aligned_memory(start, length);
|
||||
let (tx, rx) = oneshot::channel();
|
||||
self.client
|
||||
.send_command(EmulatorCommand::ReadMemory(sim, start, length, vec![], tx));
|
||||
let bytes = pollster::block_on(rx).unwrap();
|
||||
buf.copy_from_slice(&bytes);
|
||||
#[expect(clippy::arc_with_non_send_sync)] // TODO: remove after bytemuck upgrade
|
||||
Arc::new(Mutex::new(buf))
|
||||
});
|
||||
MemoryView {
|
||||
memory: memory.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn aligned_memory(start: u32, length: usize) -> BoxBytes {
|
||||
if start % 4 == 0 && length % 4 == 0 {
|
||||
let memory = vec![0u32; length / 4].into_boxed_slice();
|
||||
return bytemuck::box_bytes_of(memory);
|
||||
}
|
||||
if start % 2 == 0 && length % 2 == 0 {
|
||||
let memory = vec![0u16; length / 2].into_boxed_slice();
|
||||
return bytemuck::box_bytes_of(memory);
|
||||
}
|
||||
let memory = vec![0u8; length].into_boxed_slice();
|
||||
bytemuck::box_bytes_of(memory)
|
||||
}
|
||||
|
||||
pub struct MemoryView {
|
||||
memory: Arc<Mutex<BoxBytes>>,
|
||||
}
|
||||
// SAFETY: BoxBytes is supposed to be Send+Sync, will be in a new version
|
||||
unsafe impl Send for MemoryView {}
|
||||
|
||||
impl MemoryView {
|
||||
pub fn borrow(&self) -> MemoryRef<'_> {
|
||||
MemoryRef {
|
||||
inner: self.memory.lock().unwrap(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct MemoryRef<'a> {
|
||||
inner: MutexGuard<'a, BoxBytes>,
|
||||
}
|
||||
|
||||
impl MemoryRef<'_> {
|
||||
pub fn read(&self, index: usize) -> u8 {
|
||||
self.inner[index]
|
||||
}
|
||||
pub fn range<T: bytemuck::AnyBitPattern>(&self, start: usize, count: usize) -> &[T] {
|
||||
let from = start * size_of::<T>();
|
||||
let to = from + (count * size_of::<T>());
|
||||
bytemuck::cast_slice(&self.inner[from..to])
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
|
||||
struct MemoryRegion {
|
||||
sim: SimId,
|
||||
start: u32,
|
||||
length: usize,
|
||||
}
|
121
src/vram.rs
121
src/vram.rs
|
@ -1,121 +0,0 @@
|
|||
use std::{
|
||||
collections::{hash_map::Entry, HashMap},
|
||||
hash::Hash,
|
||||
sync::Mutex,
|
||||
};
|
||||
|
||||
use egui::{
|
||||
epaint::ImageDelta,
|
||||
load::{LoadError, SizedTexture, TextureLoader, TexturePoll},
|
||||
ColorImage, Context, TextureHandle, TextureOptions,
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
pub trait VramResource: Sized + PartialEq + Eq + Hash {
|
||||
fn to_uri(&self) -> String;
|
||||
fn from_uri(uri: &str) -> Option<Self>;
|
||||
}
|
||||
|
||||
impl<T: Serialize + for<'a> Deserialize<'a> + PartialEq + Eq + Hash> VramResource for T {
|
||||
fn to_uri(&self) -> String {
|
||||
format!("vram://{}", serde_json::to_string(self).unwrap())
|
||||
}
|
||||
|
||||
fn from_uri(uri: &str) -> Option<Self> {
|
||||
let content = uri.strip_prefix("vram://")?;
|
||||
serde_json::from_str(content).ok()
|
||||
}
|
||||
}
|
||||
|
||||
pub trait VramImageLoader {
|
||||
type Resource: VramResource;
|
||||
|
||||
fn id(&self) -> &str;
|
||||
fn add(&self, resource: &Self::Resource) -> Option<ColorImage>;
|
||||
fn update<'a>(
|
||||
&'a self,
|
||||
resources: impl Iterator<Item = &'a Self::Resource>,
|
||||
) -> Vec<(&'a Self::Resource, ColorImage)>;
|
||||
}
|
||||
|
||||
pub struct VramTextureLoader<T: VramImageLoader> {
|
||||
id: String,
|
||||
loader: Mutex<T>,
|
||||
cache: Mutex<HashMap<T::Resource, TextureHandle>>,
|
||||
}
|
||||
|
||||
impl<T: VramImageLoader> VramTextureLoader<T> {
|
||||
pub fn new(loader: T) -> Self {
|
||||
Self {
|
||||
id: loader.id().to_string(),
|
||||
loader: Mutex::new(loader),
|
||||
cache: Mutex::new(HashMap::new()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: VramImageLoader> TextureLoader for VramTextureLoader<T> {
|
||||
fn id(&self) -> &str {
|
||||
&self.id
|
||||
}
|
||||
|
||||
fn load(
|
||||
&self,
|
||||
ctx: &Context,
|
||||
uri: &str,
|
||||
texture_options: TextureOptions,
|
||||
_size_hint: egui::SizeHint,
|
||||
) -> Result<TexturePoll, LoadError> {
|
||||
let Some(resource) = T::Resource::from_uri(uri) else {
|
||||
return Err(LoadError::NotSupported);
|
||||
};
|
||||
if texture_options != TextureOptions::NEAREST {
|
||||
return Err(LoadError::Loading(
|
||||
"Only TextureOptions::NEAREST are supported".into(),
|
||||
));
|
||||
}
|
||||
let loader = self.loader.lock().unwrap();
|
||||
let mut cache = self.cache.lock().unwrap();
|
||||
for (resource, updated_image) in loader.update(cache.keys()) {
|
||||
if let Some(handle) = cache.get(resource) {
|
||||
let delta = ImageDelta::full(updated_image, TextureOptions::NEAREST);
|
||||
ctx.tex_manager().write().set(handle.id(), delta);
|
||||
}
|
||||
}
|
||||
match cache.entry(resource) {
|
||||
Entry::Occupied(entry) => {
|
||||
let texture = SizedTexture::from_handle(entry.get());
|
||||
Ok(TexturePoll::Ready { texture })
|
||||
}
|
||||
Entry::Vacant(entry) => {
|
||||
if let Some(image) = loader.add(entry.key()) {
|
||||
let handle =
|
||||
entry.insert(ctx.load_texture(uri, image, TextureOptions::NEAREST));
|
||||
let texture = SizedTexture::from_handle(handle);
|
||||
Ok(TexturePoll::Ready { texture })
|
||||
} else {
|
||||
Err(LoadError::Loading("could not load texture".into()))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn forget(&self, uri: &str) {
|
||||
if let Some(resource) = T::Resource::from_uri(uri) {
|
||||
self.cache.lock().unwrap().remove(&resource);
|
||||
}
|
||||
}
|
||||
|
||||
fn forget_all(&self) {
|
||||
self.cache.lock().unwrap().clear();
|
||||
}
|
||||
|
||||
fn byte_size(&self) -> usize {
|
||||
self.cache
|
||||
.lock()
|
||||
.unwrap()
|
||||
.values()
|
||||
.map(|h| h.byte_size())
|
||||
.sum()
|
||||
}
|
||||
}
|
|
@ -3,7 +3,6 @@ use egui::{Context, ViewportBuilder, ViewportId};
|
|||
pub use game::GameWindow;
|
||||
pub use gdb::GdbServerWindow;
|
||||
pub use input::InputWindow;
|
||||
pub use vram::{BgMapWindow, CharacterDataWindow};
|
||||
use winit::event::KeyEvent;
|
||||
|
||||
use crate::emulator::SimId;
|
||||
|
@ -13,7 +12,6 @@ mod game;
|
|||
mod game_screen;
|
||||
mod gdb;
|
||||
mod input;
|
||||
mod vram;
|
||||
|
||||
pub trait AppWindow {
|
||||
fn viewport_id(&self) -> ViewportId;
|
||||
|
@ -22,8 +20,7 @@ pub trait AppWindow {
|
|||
}
|
||||
fn initial_viewport(&self) -> ViewportBuilder;
|
||||
fn show(&mut self, ctx: &Context);
|
||||
fn on_init(&mut self, ctx: &Context, render_state: &egui_wgpu::RenderState) {
|
||||
let _ = ctx;
|
||||
fn on_init(&mut self, render_state: &egui_wgpu::RenderState) {
|
||||
let _ = render_state;
|
||||
}
|
||||
fn on_destroy(&mut self) {}
|
||||
|
|
|
@ -132,18 +132,6 @@ impl GameWindow {
|
|||
.unwrap();
|
||||
ui.close_menu();
|
||||
}
|
||||
if ui.button("Character Data").clicked() {
|
||||
self.proxy
|
||||
.send_event(UserEvent::OpenCharacterData(self.sim_id))
|
||||
.unwrap();
|
||||
ui.close_menu();
|
||||
}
|
||||
if ui.button("Background Maps").clicked() {
|
||||
self.proxy
|
||||
.send_event(UserEvent::OpenBgMap(self.sim_id))
|
||||
.unwrap();
|
||||
ui.close_menu();
|
||||
}
|
||||
});
|
||||
ui.menu_button("About", |ui| {
|
||||
self.proxy.send_event(UserEvent::OpenAbout).unwrap();
|
||||
|
@ -381,7 +369,7 @@ impl AppWindow for GameWindow {
|
|||
toasts.show(ctx);
|
||||
}
|
||||
|
||||
fn on_init(&mut self, _ctx: &Context, render_state: &egui_wgpu::RenderState) {
|
||||
fn on_init(&mut self, 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,6 +0,0 @@
|
|||
mod bgmap;
|
||||
mod chardata;
|
||||
mod utils;
|
||||
|
||||
pub use bgmap::*;
|
||||
pub use chardata::*;
|
|
@ -1,151 +0,0 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
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, memory: &mut MemoryMonitor) -> Self {
|
||||
Self {
|
||||
sim_id,
|
||||
loader: Some(BgMapLoader::new(sim_id, memory)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl AppWindow for BgMapWindow {
|
||||
fn viewport_id(&self) -> ViewportId {
|
||||
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| {
|
||||
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![]
|
||||
}
|
||||
}
|
|
@ -1,433 +0,0 @@
|
|||
use std::{fmt::Display, sync::Arc};
|
||||
|
||||
use egui::{
|
||||
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,
|
||||
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,
|
||||
scale: f32,
|
||||
show_grid: bool,
|
||||
}
|
||||
|
||||
impl CharacterDataWindow {
|
||||
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(),
|
||||
scale: 4.0,
|
||||
show_grid: true,
|
||||
}
|
||||
}
|
||||
|
||||
fn show_form(&mut self, ui: &mut Ui) {
|
||||
let row_height = ui.spacing().interact_size.y;
|
||||
ui.vertical(|ui| {
|
||||
TableBuilder::new(ui)
|
||||
.column(Column::auto())
|
||||
.column(Column::remainder())
|
||||
.body(|mut body| {
|
||||
body.row(row_height, |mut row| {
|
||||
row.col(|ui| {
|
||||
ui.label("Index");
|
||||
});
|
||||
row.col(|ui| {
|
||||
let res = ui.add(
|
||||
TextEdit::singleline(&mut self.index_str)
|
||||
.horizontal_align(Align::Max),
|
||||
);
|
||||
if res.changed() {
|
||||
if let Some(index) =
|
||||
self.index_str.parse().ok().filter(|id| *id < 2048)
|
||||
{
|
||||
self.index = index;
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
body.row(row_height, |mut row| {
|
||||
row.col(|ui| {
|
||||
ui.label("Address");
|
||||
});
|
||||
row.col(|ui| {
|
||||
let address = match self.index {
|
||||
0x000..0x200 => 0x00060000 + self.index,
|
||||
0x200..0x400 => 0x000e0000 + (self.index - 0x200),
|
||||
0x400..0x600 => 0x00160000 + (self.index - 0x400),
|
||||
0x600..0x800 => 0x001e0000 + (self.index - 0x600),
|
||||
_ => unreachable!("can't happen"),
|
||||
};
|
||||
let mut address_str = format!("{address:08x}");
|
||||
ui.add_enabled(
|
||||
false,
|
||||
TextEdit::singleline(&mut address_str).horizontal_align(Align::Max),
|
||||
);
|
||||
});
|
||||
});
|
||||
body.row(row_height, |mut row| {
|
||||
row.col(|ui| {
|
||||
ui.label("Mirror");
|
||||
});
|
||||
row.col(|ui| {
|
||||
let mirror = 0x00078000 + (self.index * 16);
|
||||
let mut mirror_str = format!("{mirror:08x}");
|
||||
ui.add_enabled(
|
||||
false,
|
||||
TextEdit::singleline(&mut mirror_str).horizontal_align(Align::Max),
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
let resource = CharDataResource::Character {
|
||||
palette: self.palette,
|
||||
index: self.index,
|
||||
};
|
||||
let image = Image::new(resource.to_uri())
|
||||
.maintain_aspect_ratio(true)
|
||||
.tint(Color32::RED)
|
||||
.texture_options(TextureOptions::NEAREST);
|
||||
ui.add(image);
|
||||
ui.section("Colors", |ui| {
|
||||
ui.horizontal(|ui| {
|
||||
ui.label("Palette");
|
||||
ComboBox::from_id_salt("palette")
|
||||
.selected_text(self.palette.to_string())
|
||||
.width(ui.available_width())
|
||||
.show_ui(ui, |ui| {
|
||||
for palette in VramPalette::values() {
|
||||
ui.selectable_value(
|
||||
&mut self.palette,
|
||||
palette,
|
||||
palette.to_string(),
|
||||
);
|
||||
}
|
||||
});
|
||||
});
|
||||
TableBuilder::new(ui)
|
||||
.columns(Column::remainder(), 4)
|
||||
.body(|mut body| {
|
||||
let palette = self.load_palette_colors();
|
||||
body.row(30.0, |mut row| {
|
||||
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));
|
||||
ui.painter().rect_filled(
|
||||
rect,
|
||||
0.0,
|
||||
Color32::RED * Color32::from_gray(color),
|
||||
);
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
ui.section("Display", |ui| {
|
||||
ui.horizontal(|ui| {
|
||||
ui.label("Scale");
|
||||
ui.spacing_mut().slider_width = ui.available_width();
|
||||
let slider = Slider::new(&mut self.scale, 1.0..=10.0)
|
||||
.step_by(1.0)
|
||||
.show_value(false);
|
||||
ui.add(slider);
|
||||
});
|
||||
ui.checkbox(&mut self.show_grid, "Show grid");
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
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 = CharDataResource::CharacterData {
|
||||
palette: self.palette,
|
||||
};
|
||||
let image = Image::new(resource.to_uri())
|
||||
.fit_to_original_size(self.scale)
|
||||
.tint(Color32::RED)
|
||||
.texture_options(TextureOptions::NEAREST)
|
||||
.sense(Sense::click());
|
||||
let res = ui.add(image);
|
||||
if res.clicked() {
|
||||
if let Some(click_pos) = res.interact_pointer_pos() {
|
||||
let fixed_pos = (click_pos - start_pos) / self.scale;
|
||||
let x = (fixed_pos.x / 8.0) as usize;
|
||||
let y = (fixed_pos.y / 8.0) as usize;
|
||||
self.index = (y * 16) + x;
|
||||
self.index_str = self.index.to_string();
|
||||
}
|
||||
}
|
||||
let painter = ui.painter_at(res.rect);
|
||||
if self.show_grid {
|
||||
let stroke = ui.style().visuals.widgets.noninteractive.fg_stroke;
|
||||
for x in (1..16).map(|i| (i as f32) * 8.0 * self.scale) {
|
||||
let p1 = res.rect.min + (x, 0.0).into();
|
||||
let p2 = res.rect.min + (x, 128.0 * 8.0 * self.scale).into();
|
||||
painter.line(vec![p1, p2], stroke);
|
||||
}
|
||||
for y in (1..128).map(|i| (i as f32) * 8.0 * self.scale) {
|
||||
let p1 = res.rect.min + (0.0, y).into();
|
||||
let p2 = res.rect.min + (16.0 * 8.0 * self.scale, y).into();
|
||||
painter.line(vec![p1, p2], stroke);
|
||||
}
|
||||
}
|
||||
// draw box around selected
|
||||
let x1 = (self.index % 16) as f32 * 8.0 * self.scale;
|
||||
let x2 = x1 + (8.0 * self.scale);
|
||||
let y1 = (self.index / 16) as f32 * 8.0 * self.scale;
|
||||
let y2 = y1 + (8.0 * self.scale);
|
||||
painter.line(
|
||||
vec![
|
||||
(res.rect.min + (x1, y1).into()),
|
||||
(res.rect.min + (x2, y1).into()),
|
||||
(res.rect.min + (x2, y2).into()),
|
||||
(res.rect.min + (x1, y2).into()),
|
||||
(res.rect.min + (x1, y1).into()),
|
||||
],
|
||||
ui.style().visuals.widgets.active.fg_stroke,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
impl AppWindow for CharacterDataWindow {
|
||||
fn viewport_id(&self) -> ViewportId {
|
||||
ViewportId::from_hash_of(format!("chardata-{}", self.sim_id))
|
||||
}
|
||||
|
||||
fn sim_id(&self) -> SimId {
|
||||
self.sim_id
|
||||
}
|
||||
|
||||
fn initial_viewport(&self) -> ViewportBuilder {
|
||||
ViewportBuilder::default()
|
||||
.with_title(format!("Character 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.horizontal_top(|ui| {
|
||||
StripBuilder::new(ui)
|
||||
.size(Size::relative(0.3))
|
||||
.size(Size::remainder())
|
||||
.horizontal(|mut strip| {
|
||||
strip.cell(|ui| {
|
||||
ScrollArea::vertical().show(ui, |ui| self.show_form(ui));
|
||||
});
|
||||
strip.cell(|ui| {
|
||||
ScrollArea::both().show(ui, |ui| self.show_chardata(ui));
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
#[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));
|
||||
}
|
||||
|
||||
impl UiExt for Ui {
|
||||
fn section(&mut self, title: impl Into<String>, add_contents: impl FnOnce(&mut Ui)) {
|
||||
let mut frame = Frame::group(self.style());
|
||||
frame.outer_margin.top += 10.0;
|
||||
frame.inner_margin.top += 2.0;
|
||||
let res = frame.show(self, add_contents);
|
||||
let text = RichText::new(title).background_color(self.style().visuals.panel_fill);
|
||||
let old_rect = res.response.rect;
|
||||
let mut text_rect = old_rect;
|
||||
text_rect.min.x += 6.0;
|
||||
let new_rect = self
|
||||
.allocate_new_ui(UiBuilder::new().max_rect(text_rect), |ui| ui.label(text))
|
||||
.response
|
||||
.rect;
|
||||
self.allocate_space((old_rect.max - new_rect.max) - (old_rect.min - new_rect.min));
|
||||
}
|
||||
}
|
|
@ -1,16 +0,0 @@
|
|||
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],
|
||||
]
|
||||
}
|
Loading…
Reference in New Issue