From 5de448917af22ab6af62f9ae615e302dcd139c4b Mon Sep 17 00:00:00 2001 From: Alex Janka Date: Sun, 19 Mar 2023 10:26:56 +1100 Subject: [PATCH] change from inline to where for generics --- lib/src/connect/mod.rs | 12 ++++++-- lib/src/lib.rs | 18 +++++++++-- .../processor/instructions/instructions.rs | 6 +++- lib/src/processor/instructions/primitives.rs | 6 +++- lib/src/processor/memory.rs | 30 +++++++++++++++---- lib/src/processor/memory/mmio/gpu.rs | 26 ++++++++++------ lib/src/processor/mod.rs | 24 ++++++++++++--- lib/src/processor/opcodes.rs | 6 +++- 8 files changed, 100 insertions(+), 28 deletions(-) diff --git a/lib/src/connect/mod.rs b/lib/src/connect/mod.rs index 4b09ad2..50ab942 100644 --- a/lib/src/connect/mod.rs +++ b/lib/src/connect/mod.rs @@ -71,7 +71,11 @@ pub trait PocketCamera { } #[non_exhaustive] -pub struct EmulatorOptions + Clone, R: Renderer> { +pub struct EmulatorOptions +where + ColourFormat: From + Clone, + R: Renderer, +{ pub(crate) window: R, pub(crate) tile_window: Option, pub(crate) rom: RomFile, @@ -84,8 +88,10 @@ pub struct EmulatorOptions + Clone, R: Renderer, } -impl + Clone, R: Renderer> - EmulatorOptions +impl EmulatorOptions +where + ColourFormat: From + Clone, + R: Renderer, { pub fn new(window: R, rom: RomFile, output: AudioOutput) -> Self { Self { diff --git a/lib/src/lib.rs b/lib/src/lib.rs index 22367b8..4877029 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -30,12 +30,20 @@ static VERBOSE: OnceCell = OnceCell::new(); pub const WIDTH: usize = 160; pub const HEIGHT: usize = 144; -pub struct EmulatorCore + Clone, R: Renderer> { +pub struct EmulatorCore +where + ColourFormat: From + Clone, + R: Renderer, +{ receiver: Receiver, cpu: Cpu, } -impl + Clone, R: Renderer> EmulatorCore { +impl EmulatorCore +where + ColourFormat: From + Clone, + R: Renderer, +{ pub fn init( receiver: Receiver, mut options: EmulatorOptions, @@ -153,7 +161,11 @@ impl + Clone, R: Renderer> EmulatorCore } } - pub fn get_save_state(&self) -> CpuSaveState { + pub fn get_save_state(&self) -> CpuSaveState + where + ColourFormat: From + Clone, + R: Renderer, + { CpuSaveState::create(&self.cpu) } diff --git a/lib/src/processor/instructions/instructions.rs b/lib/src/processor/instructions/instructions.rs index 98d7d2d..edbba15 100644 --- a/lib/src/processor/instructions/instructions.rs +++ b/lib/src/processor/instructions/instructions.rs @@ -4,7 +4,11 @@ use crate::{ util::{clear_bit, get_bit, set_bit}, }; -impl + Clone, R: Renderer> Cpu { +impl Cpu +where + ColourFormat: From + Clone, + R: Renderer, +{ pub(crate) fn and(&mut self, first: u8, second: u8) -> u8 { let result = first & second; self.set_or_clear_flag(Flags::Zero, result == 0x0); diff --git a/lib/src/processor/instructions/primitives.rs b/lib/src/processor/instructions/primitives.rs index 462e0d9..d3f2472 100644 --- a/lib/src/processor/instructions/primitives.rs +++ b/lib/src/processor/instructions/primitives.rs @@ -5,7 +5,11 @@ use crate::{ }; use std::ops::{BitAnd, BitOr}; -impl + Clone, R: Renderer> Cpu { +impl Cpu +where + ColourFormat: From + Clone, + R: Renderer, +{ pub(crate) fn pop_word(&mut self) -> u16 { let mut word: u16 = 0x0; word.set_low(self.memory.get(self.reg.sp)); diff --git a/lib/src/processor/memory.rs b/lib/src/processor/memory.rs index 3f2a446..c56b540 100644 --- a/lib/src/processor/memory.rs +++ b/lib/src/processor/memory.rs @@ -22,7 +22,11 @@ pub(crate) mod rom; pub(crate) type Address = u16; -pub struct Memory + Clone, R: Renderer> { +pub struct Memory +where + ColourFormat: From + Clone, + R: Renderer, +{ bootrom: Option>, rom: Rom, ram: [u8; 8192], @@ -40,7 +44,11 @@ pub struct Memory + Clone, R: Renderer> #[serde_with::serde_as] #[derive(Serialize, Deserialize)] -pub struct MemorySaveState + Clone, R: Renderer> { +pub struct MemorySaveState +where + ColourFormat: From + Clone, + R: Renderer, +{ rom: RomSaveState, #[serde_as(as = "[_; 8192]")] ram: [u8; 8192], @@ -57,8 +65,10 @@ pub struct MemorySaveState + Clone, R: Renderer + Clone, R: Renderer> - MemorySaveState +impl MemorySaveState +where + ColourFormat: From + Clone, + R: Renderer, { pub fn create(memory: &Memory) -> Self { Self { @@ -78,7 +88,11 @@ impl + Clone, R: Renderer> } } -impl + Clone, R: Renderer> Memory { +impl Memory +where + ColourFormat: From + Clone, + R: Renderer, +{ pub fn init( bootrom: Option>, rom: Rom, @@ -300,7 +314,11 @@ impl + Clone, R: Renderer> Memory + Clone, R: Renderer> Cpu { +impl Cpu +where + ColourFormat: From + Clone, + R: Renderer, +{ pub fn increment_timers(&mut self, machine_cycles: u8) { let steps = (machine_cycles as usize) * 4; diff --git a/lib/src/processor/memory/mmio/gpu.rs b/lib/src/processor/memory/mmio/gpu.rs index 37e1a8c..5734e28 100644 --- a/lib/src/processor/memory/mmio/gpu.rs +++ b/lib/src/processor/memory/mmio/gpu.rs @@ -23,8 +23,8 @@ mod types; const TILE_WINDOW_WIDTH: usize = 16 * 8; const TILE_WINDOW_HEIGHT: usize = 24 * 8; -pub struct Gpu, R: Renderer> { - pub buffer: Vec, +pub struct Gpu, R: Renderer> { + pub buffer: Vec, pub vram: Vram, pub oam: Oam, pub window: R, @@ -34,7 +34,7 @@ pub struct Gpu, R: Renderer> { mode_clock: usize, scanline: u8, lyc: u8, - tile_window: Option>, + tile_window: Option>, window_lc: u8, has_window_been_enabled: bool, bg_palette: Palette, @@ -48,8 +48,8 @@ pub struct Gpu, R: Renderer> { } #[derive(Serialize, Deserialize)] -pub struct GpuSaveState, R: Renderer> { - buffer: Vec, +pub struct GpuSaveState, R: Renderer> { + buffer: Vec, vram: Vram, oam: Oam, is_bg_zero: Vec, @@ -72,8 +72,12 @@ pub struct GpuSaveState, R: Renderer> { spooky: PhantomData, } -impl + Clone, R: Renderer> GpuSaveState { - pub fn create(gpu: &Gpu) -> Self { +impl GpuSaveState +where + ColourFormat: From + Clone, + R: Renderer, +{ + pub fn create(gpu: &Gpu) -> Self { Self { buffer: gpu.buffer.clone(), vram: gpu.vram, @@ -99,7 +103,11 @@ impl + Clone, R: Renderer> GpuSaveState } } -impl + Clone, R: Renderer> Gpu { +impl Gpu +where + ColourFormat: From + Clone, + R: Renderer, +{ pub fn new(window: R, tile_window_renderer: Option) -> Self { let tile_window = if let Some(mut tile_window_renderer) = tile_window_renderer { tile_window_renderer.prepare(TILE_WINDOW_WIDTH, TILE_WINDOW_HEIGHT); @@ -135,7 +143,7 @@ impl + Clone, R: Renderer> Gpu { } pub fn from_save_state( - state: GpuSaveState, + state: GpuSaveState, window: R, tile_window_renderer: Option, ) -> Self { diff --git a/lib/src/processor/mod.rs b/lib/src/processor/mod.rs index 3ccc859..2a40404 100644 --- a/lib/src/processor/mod.rs +++ b/lib/src/processor/mod.rs @@ -23,7 +23,11 @@ pub(crate) enum Direction { Right, } -pub struct Cpu + Clone, R: Renderer> { +pub struct Cpu +where + ColourFormat: From + Clone, + R: Renderer, +{ pub memory: Memory, pub reg: Registers, pub last_instruction: u8, @@ -33,7 +37,11 @@ pub struct Cpu + Clone, R: Renderer> { } #[derive(Serialize, Deserialize)] -pub struct CpuSaveState + Clone, R: Renderer> { +pub struct CpuSaveState +where + ColourFormat: From + Clone, + R: Renderer, +{ memory: MemorySaveState, reg: Registers, last_instruction: u8, @@ -42,7 +50,11 @@ pub struct CpuSaveState + Clone, R: Renderer + Clone, R: Renderer> CpuSaveState { +impl CpuSaveState +where + ColourFormat: From + Clone, + R: Renderer, +{ pub fn create(cpu: &Cpu) -> Self { Self { memory: MemorySaveState::create(&cpu.memory), @@ -55,7 +67,11 @@ impl + Clone, R: Renderer> CpuSaveState } } -impl + Clone, R: Renderer> Cpu { +impl Cpu +where + ColourFormat: From + Clone, + R: Renderer, +{ pub fn new(mut memory: Memory, run_bootrom: bool) -> Self { if !run_bootrom { memory.cpu_ram_init(); diff --git a/lib/src/processor/opcodes.rs b/lib/src/processor/opcodes.rs index d2e7012..42e8d02 100644 --- a/lib/src/processor/opcodes.rs +++ b/lib/src/processor/opcodes.rs @@ -9,7 +9,11 @@ use crate::{ use super::memory::mmio::gpu::Colour; -impl + Clone, R: Renderer> Cpu { +impl Cpu +where + ColourFormat: From + Clone, + R: Renderer, +{ pub fn run_opcode(&mut self, opcode: u8) -> u8 { match opcode { 0x00 => {