VIP inspection tooling #4
			
				
			
		
		
		
	
							
								
								
									
										23
									
								
								src/app.rs
								
								
								
								
							
							
						
						
									
										23
									
								
								src/app.rs
								
								
								
								
							| 
						 | 
					@ -18,8 +18,8 @@ use crate::{
 | 
				
			||||||
    controller::ControllerManager,
 | 
					    controller::ControllerManager,
 | 
				
			||||||
    emulator::{EmulatorClient, EmulatorCommand, SimId},
 | 
					    emulator::{EmulatorClient, EmulatorCommand, SimId},
 | 
				
			||||||
    input::MappingProvider,
 | 
					    input::MappingProvider,
 | 
				
			||||||
 | 
					    memory::MemoryMonitor,
 | 
				
			||||||
    persistence::Persistence,
 | 
					    persistence::Persistence,
 | 
				
			||||||
    vram::VramLoader,
 | 
					 | 
				
			||||||
    window::{
 | 
					    window::{
 | 
				
			||||||
        AboutWindow, AppWindow, BgMapWindow, CharacterDataWindow, GameWindow, GdbServerWindow,
 | 
					        AboutWindow, AppWindow, BgMapWindow, CharacterDataWindow, GameWindow, GdbServerWindow,
 | 
				
			||||||
        InputWindow,
 | 
					        InputWindow,
 | 
				
			||||||
| 
						 | 
					@ -40,11 +40,11 @@ fn load_icon() -> anyhow::Result<IconData> {
 | 
				
			||||||
pub struct Application {
 | 
					pub struct Application {
 | 
				
			||||||
    icon: Option<Arc<IconData>>,
 | 
					    icon: Option<Arc<IconData>>,
 | 
				
			||||||
    wgpu: WgpuState,
 | 
					    wgpu: WgpuState,
 | 
				
			||||||
    vram: Arc<VramLoader>,
 | 
					 | 
				
			||||||
    client: EmulatorClient,
 | 
					    client: EmulatorClient,
 | 
				
			||||||
    proxy: EventLoopProxy<UserEvent>,
 | 
					    proxy: EventLoopProxy<UserEvent>,
 | 
				
			||||||
    mappings: MappingProvider,
 | 
					    mappings: MappingProvider,
 | 
				
			||||||
    controllers: ControllerManager,
 | 
					    controllers: ControllerManager,
 | 
				
			||||||
 | 
					    memory: MemoryMonitor,
 | 
				
			||||||
    persistence: Persistence,
 | 
					    persistence: Persistence,
 | 
				
			||||||
    viewports: HashMap<ViewportId, Viewport>,
 | 
					    viewports: HashMap<ViewportId, Viewport>,
 | 
				
			||||||
    focused: Option<ViewportId>,
 | 
					    focused: Option<ViewportId>,
 | 
				
			||||||
| 
						 | 
					@ -62,6 +62,7 @@ impl Application {
 | 
				
			||||||
        let persistence = Persistence::new();
 | 
					        let persistence = Persistence::new();
 | 
				
			||||||
        let mappings = MappingProvider::new(persistence.clone());
 | 
					        let mappings = MappingProvider::new(persistence.clone());
 | 
				
			||||||
        let controllers = ControllerManager::new(client.clone(), &mappings);
 | 
					        let controllers = ControllerManager::new(client.clone(), &mappings);
 | 
				
			||||||
 | 
					        let memory = MemoryMonitor::new(client.clone());
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            let mappings = mappings.clone();
 | 
					            let mappings = mappings.clone();
 | 
				
			||||||
            let proxy = proxy.clone();
 | 
					            let proxy = proxy.clone();
 | 
				
			||||||
| 
						 | 
					@ -70,10 +71,10 @@ impl Application {
 | 
				
			||||||
        Self {
 | 
					        Self {
 | 
				
			||||||
            icon,
 | 
					            icon,
 | 
				
			||||||
            wgpu,
 | 
					            wgpu,
 | 
				
			||||||
            vram: Arc::new(VramLoader::new(client.clone())),
 | 
					 | 
				
			||||||
            client,
 | 
					            client,
 | 
				
			||||||
            proxy,
 | 
					            proxy,
 | 
				
			||||||
            mappings,
 | 
					            mappings,
 | 
				
			||||||
 | 
					            memory,
 | 
				
			||||||
            controllers,
 | 
					            controllers,
 | 
				
			||||||
            persistence,
 | 
					            persistence,
 | 
				
			||||||
            viewports: HashMap::new(),
 | 
					            viewports: HashMap::new(),
 | 
				
			||||||
| 
						 | 
					@ -89,13 +90,7 @@ impl Application {
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        self.viewports.insert(
 | 
					        self.viewports.insert(
 | 
				
			||||||
            viewport_id,
 | 
					            viewport_id,
 | 
				
			||||||
            Viewport::new(
 | 
					            Viewport::new(event_loop, &self.wgpu, self.icon.clone(), window),
 | 
				
			||||||
                event_loop,
 | 
					 | 
				
			||||||
                &self.wgpu,
 | 
					 | 
				
			||||||
                self.icon.clone(),
 | 
					 | 
				
			||||||
                self.vram.clone(),
 | 
					 | 
				
			||||||
                window,
 | 
					 | 
				
			||||||
            ),
 | 
					 | 
				
			||||||
        );
 | 
					        );
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -210,11 +205,11 @@ impl ApplicationHandler<UserEvent> for Application {
 | 
				
			||||||
                self.open(event_loop, Box::new(about));
 | 
					                self.open(event_loop, Box::new(about));
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            UserEvent::OpenCharacterData(sim_id) => {
 | 
					            UserEvent::OpenCharacterData(sim_id) => {
 | 
				
			||||||
                let vram = CharacterDataWindow::new(sim_id);
 | 
					                let vram = CharacterDataWindow::new(sim_id, &mut self.memory);
 | 
				
			||||||
                self.open(event_loop, Box::new(vram));
 | 
					                self.open(event_loop, Box::new(vram));
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            UserEvent::OpenBgMap(sim_id) => {
 | 
					            UserEvent::OpenBgMap(sim_id) => {
 | 
				
			||||||
                let bgmap = BgMapWindow::new(sim_id);
 | 
					                let bgmap = BgMapWindow::new(sim_id, &mut self.memory);
 | 
				
			||||||
                self.open(event_loop, Box::new(bgmap));
 | 
					                self.open(event_loop, Box::new(bgmap));
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            UserEvent::OpenDebugger(sim_id) => {
 | 
					            UserEvent::OpenDebugger(sim_id) => {
 | 
				
			||||||
| 
						 | 
					@ -328,7 +323,6 @@ impl Viewport {
 | 
				
			||||||
        event_loop: &ActiveEventLoop,
 | 
					        event_loop: &ActiveEventLoop,
 | 
				
			||||||
        wgpu: &WgpuState,
 | 
					        wgpu: &WgpuState,
 | 
				
			||||||
        icon: Option<Arc<IconData>>,
 | 
					        icon: Option<Arc<IconData>>,
 | 
				
			||||||
        vram: Arc<VramLoader>,
 | 
					 | 
				
			||||||
        mut app: Box<dyn AppWindow>,
 | 
					        mut app: Box<dyn AppWindow>,
 | 
				
			||||||
    ) -> Self {
 | 
					    ) -> Self {
 | 
				
			||||||
        let ctx = Context::default();
 | 
					        let ctx = Context::default();
 | 
				
			||||||
| 
						 | 
					@ -350,7 +344,6 @@ impl Viewport {
 | 
				
			||||||
            s.visuals.menu_rounding = Default::default();
 | 
					            s.visuals.menu_rounding = Default::default();
 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
        egui_extras::install_image_loaders(&ctx);
 | 
					        egui_extras::install_image_loaders(&ctx);
 | 
				
			||||||
        ctx.add_image_loader(vram);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        let wgpu_config = egui_wgpu::WgpuConfiguration {
 | 
					        let wgpu_config = egui_wgpu::WgpuConfiguration {
 | 
				
			||||||
            present_mode: wgpu::PresentMode::AutoNoVsync,
 | 
					            present_mode: wgpu::PresentMode::AutoNoVsync,
 | 
				
			||||||
| 
						 | 
					@ -374,7 +367,7 @@ impl Viewport {
 | 
				
			||||||
        let (window, state) = create_window_and_state(&ctx, event_loop, &builder, &mut painter);
 | 
					        let (window, state) = create_window_and_state(&ctx, event_loop, &builder, &mut painter);
 | 
				
			||||||
        egui_winit::update_viewport_info(&mut info, &ctx, &window, true);
 | 
					        egui_winit::update_viewport_info(&mut info, &ctx, &window, true);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        app.on_init(painter.render_state().as_ref().unwrap());
 | 
					        app.on_init(&ctx, painter.render_state().as_ref().unwrap());
 | 
				
			||||||
        Self {
 | 
					        Self {
 | 
				
			||||||
            painter,
 | 
					            painter,
 | 
				
			||||||
            ctx,
 | 
					            ctx,
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -19,6 +19,7 @@ mod emulator;
 | 
				
			||||||
mod gdbserver;
 | 
					mod gdbserver;
 | 
				
			||||||
mod graphics;
 | 
					mod graphics;
 | 
				
			||||||
mod input;
 | 
					mod input;
 | 
				
			||||||
 | 
					mod memory;
 | 
				
			||||||
mod persistence;
 | 
					mod persistence;
 | 
				
			||||||
mod vram;
 | 
					mod vram;
 | 
				
			||||||
mod window;
 | 
					mod window;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,88 @@
 | 
				
			||||||
 | 
					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,
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										391
									
								
								src/vram.rs
								
								
								
								
							
							
						
						
									
										391
									
								
								src/vram.rs
								
								
								
								
							| 
						 | 
					@ -1,208 +1,109 @@
 | 
				
			||||||
use serde::{Deserialize, Serialize};
 | 
					 | 
				
			||||||
use std::{
 | 
					use std::{
 | 
				
			||||||
    collections::{hash_map::Entry, HashMap},
 | 
					    collections::{hash_map::Entry, HashMap},
 | 
				
			||||||
    fmt::Display,
 | 
					    hash::Hash,
 | 
				
			||||||
    sync::{Arc, Mutex},
 | 
					    sync::Mutex,
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
use tokio::sync::mpsc;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
use egui::{
 | 
					use egui::{
 | 
				
			||||||
    load::{ImageLoader, ImagePoll, LoadError},
 | 
					    epaint::ImageDelta,
 | 
				
			||||||
    ColorImage, Context, Vec2,
 | 
					    load::{LoadError, SizedTexture, TextureLoader, TexturePoll},
 | 
				
			||||||
 | 
					    ColorImage, Context, TextureHandle, TextureOptions,
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					use serde::{Deserialize, Serialize};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
use crate::emulator::{EmulatorClient, EmulatorCommand, SimId};
 | 
					pub trait VramResource: Sized + PartialEq + Eq + Hash {
 | 
				
			||||||
 | 
					    fn to_uri(&self) -> String;
 | 
				
			||||||
enum VramRequest {
 | 
					    fn from_uri(uri: &str) -> Option<Self>;
 | 
				
			||||||
    Load(String, VramResource),
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
enum VramResponse {
 | 
					impl<T: Serialize + for<'a> Deserialize<'a> + PartialEq + Eq + Hash> VramResource for T {
 | 
				
			||||||
    Loaded(String, ColorImage),
 | 
					    fn to_uri(&self) -> String {
 | 
				
			||||||
 | 
					        format!("vram://{}", serde_json::to_string(self).unwrap())
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub struct VramResource {
 | 
					    fn from_uri(uri: &str) -> Option<Self> {
 | 
				
			||||||
    sim: SimId,
 | 
					        let content = uri.strip_prefix("vram://")?;
 | 
				
			||||||
    kind: VramResourceKind,
 | 
					        serde_json::from_str(content).ok()
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
impl VramResource {
 | 
					pub trait VramImageLoader {
 | 
				
			||||||
    pub fn character_data(sim: SimId, palette: VramPalette) -> Self {
 | 
					    type Resource: VramResource;
 | 
				
			||||||
        Self {
 | 
					
 | 
				
			||||||
            sim,
 | 
					    fn id(&self) -> &str;
 | 
				
			||||||
            kind: VramResourceKind::CharacterData { palette },
 | 
					    fn add(&self, resource: &Self::Resource) -> Option<ColorImage>;
 | 
				
			||||||
        }
 | 
					    fn update<'a>(
 | 
				
			||||||
    }
 | 
					        &'a self,
 | 
				
			||||||
 | 
					        resources: impl Iterator<Item = &'a Self::Resource>,
 | 
				
			||||||
    pub fn character(sim: SimId, palette: VramPalette, index: usize) -> Self {
 | 
					    ) -> Vec<(&'a Self::Resource, ColorImage)>;
 | 
				
			||||||
        Self {
 | 
					}
 | 
				
			||||||
            sim,
 | 
					
 | 
				
			||||||
            kind: VramResourceKind::Character { palette, index },
 | 
					pub struct VramTextureLoader<T: VramImageLoader> {
 | 
				
			||||||
        }
 | 
					    id: String,
 | 
				
			||||||
    }
 | 
					    loader: Mutex<T>,
 | 
				
			||||||
 | 
					    cache: Mutex<HashMap<T::Resource, TextureHandle>>,
 | 
				
			||||||
    pub fn palette_color(sim: SimId, palette: VramPalette, index: usize) -> Self {
 | 
					}
 | 
				
			||||||
        Self {
 | 
					
 | 
				
			||||||
            sim,
 | 
					impl<T: VramImageLoader> VramTextureLoader<T> {
 | 
				
			||||||
            kind: VramResourceKind::PaletteColor { palette, index },
 | 
					    pub fn new(loader: T) -> Self {
 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    pub fn to_uri(&self) -> String {
 | 
					 | 
				
			||||||
        format!(
 | 
					 | 
				
			||||||
            "vram://{}:{}",
 | 
					 | 
				
			||||||
            self.sim.to_index(),
 | 
					 | 
				
			||||||
            serde_json::to_string(&self.kind).unwrap(),
 | 
					 | 
				
			||||||
        )
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    fn from_uri(uri: &str) -> Option<VramResource> {
 | 
					 | 
				
			||||||
        let uri = uri.strip_prefix("vram://")?;
 | 
					 | 
				
			||||||
        let (sim, uri) = match uri.split_at_checked(2)? {
 | 
					 | 
				
			||||||
            ("0:", rest) => (SimId::Player1, rest),
 | 
					 | 
				
			||||||
            ("1:", rest) => (SimId::Player2, rest),
 | 
					 | 
				
			||||||
            _ => return None,
 | 
					 | 
				
			||||||
        };
 | 
					 | 
				
			||||||
        let kind = serde_json::from_str(uri).ok()?;
 | 
					 | 
				
			||||||
        Some(Self { sim, kind })
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
#[derive(Serialize, Deserialize)]
 | 
					 | 
				
			||||||
enum VramResourceKind {
 | 
					 | 
				
			||||||
    Character { palette: VramPalette, index: usize },
 | 
					 | 
				
			||||||
    CharacterData { palette: VramPalette },
 | 
					 | 
				
			||||||
    PaletteColor { palette: VramPalette, index: usize },
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
impl VramResourceKind {
 | 
					 | 
				
			||||||
    fn size(&self) -> Option<Vec2> {
 | 
					 | 
				
			||||||
        match self {
 | 
					 | 
				
			||||||
            Self::Character { .. } => Some((8.0, 8.0).into()),
 | 
					 | 
				
			||||||
            Self::CharacterData { .. } => Some((8.0 * 16.0, 8.0 * 128.0).into()),
 | 
					 | 
				
			||||||
            Self::PaletteColor { .. } => Some((1.0, 1.0).into()),
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, 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,
 | 
					 | 
				
			||||||
        ]
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
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 VramLoader {
 | 
					 | 
				
			||||||
    cache: Mutex<HashMap<String, ImagePoll>>,
 | 
					 | 
				
			||||||
    source: Mutex<mpsc::UnboundedReceiver<VramResponse>>,
 | 
					 | 
				
			||||||
    sink: mpsc::UnboundedSender<VramRequest>,
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
impl VramLoader {
 | 
					 | 
				
			||||||
    pub fn new(client: EmulatorClient) -> Self {
 | 
					 | 
				
			||||||
        let (tx1, rx1) = mpsc::unbounded_channel();
 | 
					 | 
				
			||||||
        let (tx2, rx2) = mpsc::unbounded_channel();
 | 
					 | 
				
			||||||
        std::thread::spawn(move || {
 | 
					 | 
				
			||||||
            tokio::runtime::Builder::new_current_thread()
 | 
					 | 
				
			||||||
                .enable_all()
 | 
					 | 
				
			||||||
                .build()
 | 
					 | 
				
			||||||
                .unwrap()
 | 
					 | 
				
			||||||
                .block_on(async move {
 | 
					 | 
				
			||||||
                    let worker = VramLoadingWorker::new(rx2, tx1, client);
 | 
					 | 
				
			||||||
                    worker.run().await;
 | 
					 | 
				
			||||||
                })
 | 
					 | 
				
			||||||
        });
 | 
					 | 
				
			||||||
        Self {
 | 
					        Self {
 | 
				
			||||||
 | 
					            id: loader.id().to_string(),
 | 
				
			||||||
 | 
					            loader: Mutex::new(loader),
 | 
				
			||||||
            cache: Mutex::new(HashMap::new()),
 | 
					            cache: Mutex::new(HashMap::new()),
 | 
				
			||||||
            source: Mutex::new(rx1),
 | 
					 | 
				
			||||||
            sink: tx2,
 | 
					 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
impl ImageLoader for VramLoader {
 | 
					impl<T: VramImageLoader> TextureLoader for VramTextureLoader<T> {
 | 
				
			||||||
    fn id(&self) -> &str {
 | 
					    fn id(&self) -> &str {
 | 
				
			||||||
        concat!(module_path!(), "::VramLoader")
 | 
					        &self.id
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    fn load(
 | 
					    fn load(
 | 
				
			||||||
        &self,
 | 
					        &self,
 | 
				
			||||||
        _ctx: &Context,
 | 
					        ctx: &Context,
 | 
				
			||||||
        uri: &str,
 | 
					        uri: &str,
 | 
				
			||||||
 | 
					        texture_options: TextureOptions,
 | 
				
			||||||
        _size_hint: egui::SizeHint,
 | 
					        _size_hint: egui::SizeHint,
 | 
				
			||||||
    ) -> Result<ImagePoll, LoadError> {
 | 
					    ) -> Result<TexturePoll, LoadError> {
 | 
				
			||||||
        let Some(resource) = VramResource::from_uri(uri) else {
 | 
					        let Some(resource) = T::Resource::from_uri(uri) else {
 | 
				
			||||||
            return Err(LoadError::NotSupported);
 | 
					            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();
 | 
					        let mut cache = self.cache.lock().unwrap();
 | 
				
			||||||
        {
 | 
					        for (resource, updated_image) in loader.update(cache.keys()) {
 | 
				
			||||||
            let mut source = self.source.lock().unwrap();
 | 
					            if let Some(handle) = cache.get(resource) {
 | 
				
			||||||
            while let Ok(response) = source.try_recv() {
 | 
					                let delta = ImageDelta::full(updated_image, TextureOptions::NEAREST);
 | 
				
			||||||
                match response {
 | 
					                ctx.tex_manager().write().set(handle.id(), delta);
 | 
				
			||||||
                    VramResponse::Loaded(uri, image) => {
 | 
					 | 
				
			||||||
                        cache.insert(
 | 
					 | 
				
			||||||
                            uri,
 | 
					 | 
				
			||||||
                            ImagePoll::Ready {
 | 
					 | 
				
			||||||
                                image: Arc::new(image),
 | 
					 | 
				
			||||||
                            },
 | 
					 | 
				
			||||||
                        );
 | 
					 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					        match cache.entry(resource) {
 | 
				
			||||||
 | 
					            Entry::Occupied(entry) => {
 | 
				
			||||||
 | 
					                let texture = SizedTexture::from_handle(entry.get());
 | 
				
			||||||
 | 
					                Ok(TexturePoll::Ready { texture })
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        let poll = match cache.entry(uri.to_string()) {
 | 
					 | 
				
			||||||
            Entry::Occupied(entry) => entry.into_mut(),
 | 
					 | 
				
			||||||
            Entry::Vacant(entry) => {
 | 
					            Entry::Vacant(entry) => {
 | 
				
			||||||
                let pending = ImagePoll::Pending {
 | 
					                if let Some(image) = loader.add(entry.key()) {
 | 
				
			||||||
                    size: resource.kind.size(),
 | 
					                    let handle =
 | 
				
			||||||
                };
 | 
					                        entry.insert(ctx.load_texture(uri, image, TextureOptions::NEAREST));
 | 
				
			||||||
                let _ = self.sink.send(VramRequest::Load(uri.to_string(), resource));
 | 
					                    let texture = SizedTexture::from_handle(handle);
 | 
				
			||||||
                entry.insert(pending)
 | 
					                    Ok(TexturePoll::Ready { texture })
 | 
				
			||||||
 | 
					                } else {
 | 
				
			||||||
 | 
					                    Err(LoadError::Loading("could not load texture".into()))
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        };
 | 
					 | 
				
			||||||
        Ok(poll.clone())
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    fn forget(&self, uri: &str) {
 | 
					    fn forget(&self, uri: &str) {
 | 
				
			||||||
        self.cache.lock().unwrap().remove(uri);
 | 
					        if let Some(resource) = T::Resource::from_uri(uri) {
 | 
				
			||||||
 | 
					            self.cache.lock().unwrap().remove(&resource);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    fn forget_all(&self) {
 | 
					    fn forget_all(&self) {
 | 
				
			||||||
| 
						 | 
					@ -214,159 +115,7 @@ impl ImageLoader for VramLoader {
 | 
				
			||||||
            .lock()
 | 
					            .lock()
 | 
				
			||||||
            .unwrap()
 | 
					            .unwrap()
 | 
				
			||||||
            .values()
 | 
					            .values()
 | 
				
			||||||
            .map(|c| match c {
 | 
					            .map(|h| h.byte_size())
 | 
				
			||||||
                ImagePoll::Pending { .. } => 0,
 | 
					 | 
				
			||||||
                ImagePoll::Ready { image } => image.pixels.len() * size_of::<ColorImage>(),
 | 
					 | 
				
			||||||
            })
 | 
					 | 
				
			||||||
            .sum()
 | 
					            .sum()
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					 | 
				
			||||||
struct VramLoadingWorker {
 | 
					 | 
				
			||||||
    source: mpsc::UnboundedReceiver<VramRequest>,
 | 
					 | 
				
			||||||
    sink: mpsc::UnboundedSender<VramResponse>,
 | 
					 | 
				
			||||||
    client: EmulatorClient,
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
impl VramLoadingWorker {
 | 
					 | 
				
			||||||
    fn new(
 | 
					 | 
				
			||||||
        source: mpsc::UnboundedReceiver<VramRequest>,
 | 
					 | 
				
			||||||
        sink: mpsc::UnboundedSender<VramResponse>,
 | 
					 | 
				
			||||||
        client: EmulatorClient,
 | 
					 | 
				
			||||||
    ) -> Self {
 | 
					 | 
				
			||||||
        Self {
 | 
					 | 
				
			||||||
            source,
 | 
					 | 
				
			||||||
            sink,
 | 
					 | 
				
			||||||
            client,
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    async fn run(mut self) {
 | 
					 | 
				
			||||||
        while let Some(request) = self.source.recv().await {
 | 
					 | 
				
			||||||
            #[allow(irrefutable_let_patterns)]
 | 
					 | 
				
			||||||
            if let VramRequest::Load(uri, resource) = request {
 | 
					 | 
				
			||||||
                let Some(image) = self.load(resource).await else {
 | 
					 | 
				
			||||||
                    continue;
 | 
					 | 
				
			||||||
                };
 | 
					 | 
				
			||||||
                if self.sink.send(VramResponse::Loaded(uri, image)).is_err() {
 | 
					 | 
				
			||||||
                    return;
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    async fn load(&self, resource: VramResource) -> Option<ColorImage> {
 | 
					 | 
				
			||||||
        let sim = resource.sim;
 | 
					 | 
				
			||||||
        match resource.kind {
 | 
					 | 
				
			||||||
            VramResourceKind::Character { palette, index } => {
 | 
					 | 
				
			||||||
                self.load_character(sim, palette, index).await
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
            VramResourceKind::CharacterData { palette } => {
 | 
					 | 
				
			||||||
                self.load_character_data(sim, palette).await
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
            VramResourceKind::PaletteColor { palette, index } => {
 | 
					 | 
				
			||||||
                self.load_palette_color(sim, palette, index).await
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    async fn load_character(
 | 
					 | 
				
			||||||
        &self,
 | 
					 | 
				
			||||||
        sim: SimId,
 | 
					 | 
				
			||||||
        palette: VramPalette,
 | 
					 | 
				
			||||||
        index: usize,
 | 
					 | 
				
			||||||
    ) -> Option<ColorImage> {
 | 
					 | 
				
			||||||
        if index >= 2048 {
 | 
					 | 
				
			||||||
            return None;
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        let address = 0x00078000 + (index as u32 * 16);
 | 
					 | 
				
			||||||
        let (memory, palette) = tokio::join!(
 | 
					 | 
				
			||||||
            self.read_memory(sim, address, 16),
 | 
					 | 
				
			||||||
            self.load_palette_colors(sim, palette),
 | 
					 | 
				
			||||||
        );
 | 
					 | 
				
			||||||
        let palette = palette?;
 | 
					 | 
				
			||||||
        let mut buffer = vec![];
 | 
					 | 
				
			||||||
        for byte in memory? {
 | 
					 | 
				
			||||||
            for offset in (0..8).step_by(2) {
 | 
					 | 
				
			||||||
                let char = (byte >> offset) & 0x3;
 | 
					 | 
				
			||||||
                buffer.push(palette[char as usize]);
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        Some(ColorImage::from_gray([8, 8], &buffer))
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    async fn load_character_data(&self, sim: SimId, palette: VramPalette) -> Option<ColorImage> {
 | 
					 | 
				
			||||||
        let (memory, palette) = tokio::join!(
 | 
					 | 
				
			||||||
            self.read_memory(sim, 0x00078000, 16 * 2048),
 | 
					 | 
				
			||||||
            self.load_palette_colors(sim, palette),
 | 
					 | 
				
			||||||
        );
 | 
					 | 
				
			||||||
        let palette = palette?;
 | 
					 | 
				
			||||||
        let mut buffer = vec![0; 8 * 8 * 2048];
 | 
					 | 
				
			||||||
        for (i, byte) in memory?.into_iter().enumerate() {
 | 
					 | 
				
			||||||
            let bytes = [0, 2, 4, 6].map(|off| palette[(byte as usize >> off) & 0x3]);
 | 
					 | 
				
			||||||
            let char_index = i / 16;
 | 
					 | 
				
			||||||
            let in_char_pos = i % 16;
 | 
					 | 
				
			||||||
            let x = ((char_index % 16) * 8) + ((in_char_pos % 2) * 4);
 | 
					 | 
				
			||||||
            let y = ((char_index / 16) * 8) + (in_char_pos / 2);
 | 
					 | 
				
			||||||
            let write_index = (y * 16 * 8) + x;
 | 
					 | 
				
			||||||
            buffer[write_index..(write_index + 4)].copy_from_slice(&bytes);
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        Some(ColorImage::from_gray([8 * 16, 8 * 128], &buffer))
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    async fn load_palette_color(
 | 
					 | 
				
			||||||
        &self,
 | 
					 | 
				
			||||||
        sim: SimId,
 | 
					 | 
				
			||||||
        palette: VramPalette,
 | 
					 | 
				
			||||||
        index: usize,
 | 
					 | 
				
			||||||
    ) -> Option<ColorImage> {
 | 
					 | 
				
			||||||
        if index == 0 {
 | 
					 | 
				
			||||||
            return Some(ColorImage::from_gray([1, 1], &[0]));
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        if index > 3 {
 | 
					 | 
				
			||||||
            return None;
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        let shade = *self.load_palette_colors(sim, palette).await?.get(index)?;
 | 
					 | 
				
			||||||
        Some(ColorImage::from_gray([1, 1], &[shade]))
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    async fn load_palette_colors(&self, sim: SimId, palette: VramPalette) -> Option<[u8; 4]> {
 | 
					 | 
				
			||||||
        let offset = match palette {
 | 
					 | 
				
			||||||
            VramPalette::Generic => {
 | 
					 | 
				
			||||||
                return Some([0, 64, 128, 255]);
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
            VramPalette::Bg0 => 0,
 | 
					 | 
				
			||||||
            VramPalette::Bg1 => 2,
 | 
					 | 
				
			||||||
            VramPalette::Bg2 => 4,
 | 
					 | 
				
			||||||
            VramPalette::Bg3 => 6,
 | 
					 | 
				
			||||||
            VramPalette::Obj0 => 8,
 | 
					 | 
				
			||||||
            VramPalette::Obj1 => 10,
 | 
					 | 
				
			||||||
            VramPalette::Obj2 => 12,
 | 
					 | 
				
			||||||
            VramPalette::Obj3 => 14,
 | 
					 | 
				
			||||||
        };
 | 
					 | 
				
			||||||
        let (palettes, brightnesses) = tokio::join!(
 | 
					 | 
				
			||||||
            self.read_memory(sim, 0x0005f860, 16),
 | 
					 | 
				
			||||||
            self.read_memory(sim, 0x0005f824, 6),
 | 
					 | 
				
			||||||
        );
 | 
					 | 
				
			||||||
        let palette = *palettes?.get(offset)?;
 | 
					 | 
				
			||||||
        let brts = brightnesses?;
 | 
					 | 
				
			||||||
        let shades = [
 | 
					 | 
				
			||||||
            0,
 | 
					 | 
				
			||||||
            brts[0],
 | 
					 | 
				
			||||||
            brts[2],
 | 
					 | 
				
			||||||
            brts[0].saturating_add(brts[2]).saturating_add(brts[4]),
 | 
					 | 
				
			||||||
        ];
 | 
					 | 
				
			||||||
        Some([
 | 
					 | 
				
			||||||
            0,
 | 
					 | 
				
			||||||
            shades[(palette >> 2) as usize & 0x03],
 | 
					 | 
				
			||||||
            shades[(palette >> 4) as usize & 0x03],
 | 
					 | 
				
			||||||
            shades[(palette >> 6) as usize & 0x03],
 | 
					 | 
				
			||||||
        ])
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    async fn read_memory(&self, sim: SimId, address: u32, size: usize) -> Option<Vec<u8>> {
 | 
					 | 
				
			||||||
        let (tx, rx) = oneshot::channel();
 | 
					 | 
				
			||||||
        self.client
 | 
					 | 
				
			||||||
            .send_command(EmulatorCommand::ReadMemory(sim, address, size, vec![], tx));
 | 
					 | 
				
			||||||
        rx.await.ok()
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -22,7 +22,8 @@ pub trait AppWindow {
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    fn initial_viewport(&self) -> ViewportBuilder;
 | 
					    fn initial_viewport(&self) -> ViewportBuilder;
 | 
				
			||||||
    fn show(&mut self, ctx: &Context);
 | 
					    fn show(&mut self, ctx: &Context);
 | 
				
			||||||
    fn on_init(&mut self, render_state: &egui_wgpu::RenderState) {
 | 
					    fn on_init(&mut self, ctx: &Context, render_state: &egui_wgpu::RenderState) {
 | 
				
			||||||
 | 
					        let _ = ctx;
 | 
				
			||||||
        let _ = render_state;
 | 
					        let _ = render_state;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    fn on_destroy(&mut self) {}
 | 
					    fn on_destroy(&mut self) {}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -377,7 +377,7 @@ impl AppWindow for GameWindow {
 | 
				
			||||||
        toasts.show(ctx);
 | 
					        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 (screen, sink) = GameScreen::init(render_state);
 | 
				
			||||||
        let (message_sink, message_source) = mpsc::channel();
 | 
					        let (message_sink, message_source) = mpsc::channel();
 | 
				
			||||||
        self.client.send_command(EmulatorCommand::ConnectToSim(
 | 
					        self.client.send_command(EmulatorCommand::ConnectToSim(
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,5 +1,6 @@
 | 
				
			||||||
mod bgmap;
 | 
					mod bgmap;
 | 
				
			||||||
mod chardata;
 | 
					mod chardata;
 | 
				
			||||||
 | 
					mod utils;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub use bgmap::*;
 | 
					pub use bgmap::*;
 | 
				
			||||||
pub use chardata::*;
 | 
					pub use chardata::*;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -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 {
 | 
					pub struct BgMapWindow {
 | 
				
			||||||
    sim_id: SimId,
 | 
					    sim_id: SimId,
 | 
				
			||||||
 | 
					    loader: Option<BgMapLoader>,
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
impl BgMapWindow {
 | 
					impl BgMapWindow {
 | 
				
			||||||
    pub fn new(sim_id: SimId) -> Self {
 | 
					    pub fn new(sim_id: SimId, memory: &mut MemoryMonitor) -> Self {
 | 
				
			||||||
        Self { sim_id }
 | 
					        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))
 | 
					        ViewportId::from_hash_of(format!("bgmap-{}", self.sim_id))
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    fn sim_id(&self) -> SimId {
 | 
				
			||||||
 | 
					        self.sim_id
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    fn initial_viewport(&self) -> ViewportBuilder {
 | 
					    fn initial_viewport(&self) -> ViewportBuilder {
 | 
				
			||||||
        ViewportBuilder::default()
 | 
					        ViewportBuilder::default()
 | 
				
			||||||
            .with_title(format!("BG Map Data ({})", self.sim_id))
 | 
					            .with_title(format!("BG Map Data ({})", self.sim_id))
 | 
				
			||||||
            .with_inner_size((640.0, 480.0))
 | 
					            .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) {
 | 
					    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![]
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,17 +1,86 @@
 | 
				
			||||||
 | 
					use std::{fmt::Display, sync::Arc};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
use egui::{
 | 
					use egui::{
 | 
				
			||||||
    Align, CentralPanel, Color32, ComboBox, Context, Frame, Image, RichText, ScrollArea, Sense,
 | 
					    Align, CentralPanel, Color32, ColorImage, ComboBox, Context, Frame, Image, RichText,
 | 
				
			||||||
    Slider, TextEdit, TextureOptions, Ui, UiBuilder, Vec2, ViewportBuilder, ViewportId,
 | 
					    ScrollArea, Sense, Slider, TextEdit, TextureOptions, Ui, UiBuilder, Vec2, ViewportBuilder,
 | 
				
			||||||
 | 
					    ViewportId,
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
use egui_extras::{Column, Size, StripBuilder, TableBuilder};
 | 
					use egui_extras::{Column, Size, StripBuilder, TableBuilder};
 | 
				
			||||||
 | 
					use serde::{Deserialize, Serialize};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
use crate::{
 | 
					use crate::{
 | 
				
			||||||
    emulator::SimId,
 | 
					    emulator::SimId,
 | 
				
			||||||
    vram::{VramPalette, VramResource},
 | 
					    memory::{MemoryMonitor, MemoryView},
 | 
				
			||||||
 | 
					    vram::{VramImageLoader, VramResource as _, VramTextureLoader},
 | 
				
			||||||
    window::AppWindow,
 | 
					    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 {
 | 
					pub struct CharacterDataWindow {
 | 
				
			||||||
    sim_id: SimId,
 | 
					    sim_id: SimId,
 | 
				
			||||||
 | 
					    loader: Option<CharDataLoader>,
 | 
				
			||||||
 | 
					    brightness: MemoryView,
 | 
				
			||||||
 | 
					    palettes: MemoryView,
 | 
				
			||||||
    palette: VramPalette,
 | 
					    palette: VramPalette,
 | 
				
			||||||
    index: usize,
 | 
					    index: usize,
 | 
				
			||||||
    index_str: String,
 | 
					    index_str: String,
 | 
				
			||||||
| 
						 | 
					@ -20,9 +89,12 @@ pub struct CharacterDataWindow {
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
impl CharacterDataWindow {
 | 
					impl CharacterDataWindow {
 | 
				
			||||||
    pub fn new(sim_id: SimId) -> Self {
 | 
					    pub fn new(sim_id: SimId, memory: &mut MemoryMonitor) -> Self {
 | 
				
			||||||
        Self {
 | 
					        Self {
 | 
				
			||||||
            sim_id,
 | 
					            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,
 | 
					            palette: VramPalette::Generic,
 | 
				
			||||||
            index: 0,
 | 
					            index: 0,
 | 
				
			||||||
            index_str: "0".into(),
 | 
					            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())
 | 
					            let image = Image::new(resource.to_uri())
 | 
				
			||||||
                .maintain_aspect_ratio(true)
 | 
					                .maintain_aspect_ratio(true)
 | 
				
			||||||
                .tint(Color32::RED)
 | 
					                .tint(Color32::RED)
 | 
				
			||||||
| 
						 | 
					@ -114,18 +189,18 @@ impl CharacterDataWindow {
 | 
				
			||||||
                TableBuilder::new(ui)
 | 
					                TableBuilder::new(ui)
 | 
				
			||||||
                    .columns(Column::remainder(), 4)
 | 
					                    .columns(Column::remainder(), 4)
 | 
				
			||||||
                    .body(|mut body| {
 | 
					                    .body(|mut body| {
 | 
				
			||||||
 | 
					                        let palette = self.load_palette_colors();
 | 
				
			||||||
                        body.row(30.0, |mut row| {
 | 
					                        body.row(30.0, |mut row| {
 | 
				
			||||||
                            for index in 0..4 {
 | 
					                            for color in palette {
 | 
				
			||||||
                                let resource =
 | 
					 | 
				
			||||||
                                    VramResource::palette_color(self.sim_id, self.palette, index);
 | 
					 | 
				
			||||||
                                row.col(|ui| {
 | 
					                                row.col(|ui| {
 | 
				
			||||||
                                    let rect = ui.available_rect_before_wrap();
 | 
					                                    let rect = ui.available_rect_before_wrap();
 | 
				
			||||||
                                    let scale = rect.height() / rect.width();
 | 
					                                    let scale = rect.height() / rect.width();
 | 
				
			||||||
                                    let rect = rect.scale_from_center2(Vec2::new(scale, 1.0));
 | 
					                                    let rect = rect.scale_from_center2(Vec2::new(scale, 1.0));
 | 
				
			||||||
                                    let image = Image::new(resource.to_uri())
 | 
					                                    ui.painter().rect_filled(
 | 
				
			||||||
                                        .tint(Color32::RED)
 | 
					                                        rect,
 | 
				
			||||||
                                        .fit_to_exact_size(rect.max - rect.min);
 | 
					                                        0.0,
 | 
				
			||||||
                                    ui.put(rect, image);
 | 
					                                        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) {
 | 
					    fn show_chardata(&mut self, ui: &mut Ui) {
 | 
				
			||||||
        let start_pos = ui.cursor().min;
 | 
					        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())
 | 
					        let image = Image::new(resource.to_uri())
 | 
				
			||||||
            .fit_to_original_size(self.scale)
 | 
					            .fit_to_original_size(self.scale)
 | 
				
			||||||
            .tint(Color32::RED)
 | 
					            .tint(Color32::RED)
 | 
				
			||||||
| 
						 | 
					@ -210,6 +297,11 @@ impl AppWindow for CharacterDataWindow {
 | 
				
			||||||
            .with_inner_size((640.0, 480.0))
 | 
					            .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) {
 | 
					    fn show(&mut self, ctx: &Context) {
 | 
				
			||||||
        CentralPanel::default().show(ctx, |ui| {
 | 
					        CentralPanel::default().show(ctx, |ui| {
 | 
				
			||||||
            ui.horizontal_top(|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 {
 | 
					trait UiExt {
 | 
				
			||||||
    fn section(&mut self, title: impl Into<String>, add_contents: impl FnOnce(&mut Ui));
 | 
					    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],
 | 
				
			||||||
 | 
					    ]
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Loading…
	
		Reference in New Issue