From 16142ee99d68852ab90ce601d39ca9ef2cc77e2b Mon Sep 17 00:00:00 2001 From: Michel Heily Date: Thu, 16 Jan 2020 19:56:05 +0200 Subject: [PATCH] core: gba: Move VideoInterface & AudioInterface to GameBoyAdvance In preparation for savestates, this is needed to avoid (de)serializing Rc's Former-commit-id: c6dfe189d4f6104fc4b1b567529ec50714a2d74e --- src/core/gba.rs | 16 +++++++++++++--- src/core/gpu/mod.rs | 12 +++++------- src/core/sound/mod.rs | 19 +++++++++---------- 3 files changed, 27 insertions(+), 20 deletions(-) diff --git a/src/core/gba.rs b/src/core/gba.rs index 8e0a4f2..9e51b69 100644 --- a/src/core/gba.rs +++ b/src/core/gba.rs @@ -15,6 +15,9 @@ use super::super::{AudioInterface, InputInterface, VideoInterface}; pub struct GameBoyAdvance { pub sysbus: Box, pub cpu: Core, + + video_device: Rc>, + audio_device: Rc>, input_device: Rc>, cycles_to_next_event: usize, @@ -29,12 +32,17 @@ impl GameBoyAdvance { audio_device: Rc>, input_device: Rc>, ) -> GameBoyAdvance { - let gpu = Box::new(Gpu::new(video_device)); - let sound_controller = Box::new(SoundController::new(audio_device)); + let gpu = Box::new(Gpu::new()); + let sound_controller = Box::new(SoundController::new( + audio_device.borrow().get_sample_rate() as f32, + )); let io = IoDevices::new(gpu, sound_controller); GameBoyAdvance { cpu: cpu, sysbus: Box::new(SysBus::new(io, bios_rom, gamepak)), + + video_device: video_device, + audio_device: audio_device, input_device: input_device, cycles_to_next_event: 1, @@ -138,8 +146,10 @@ impl GameBoyAdvance { &mut self.sysbus, &mut irqs, &mut cycles_to_next_event, + &self.video_device, ); - io.sound.update(cycles, &mut cycles_to_next_event); + io.sound + .update(cycles, &mut cycles_to_next_event, &self.audio_device); self.cycles_to_next_event = cycles_to_next_event; io.intc.request_irqs(irqs); } diff --git a/src/core/gpu/mod.rs b/src/core/gpu/mod.rs index 19d6192..2326b00 100644 --- a/src/core/gpu/mod.rs +++ b/src/core/gpu/mod.rs @@ -184,8 +184,6 @@ type VideoDeviceRcRefCell = Rc>; #[derive(DebugStub)] pub struct Gpu { - #[debug_stub = "video handle"] - video_device: VideoDeviceRcRefCell, pub state: GpuState, /// how many cycles left until next gpu state ? @@ -221,10 +219,8 @@ pub struct Gpu { } impl Gpu { - pub fn new(video_device: VideoDeviceRcRefCell) -> Gpu { + pub fn new() -> Gpu { Gpu { - video_device: video_device, - dispcnt: DisplayControl(0x80), dispstat: DisplayStatus(0), bg: [ @@ -377,6 +373,7 @@ impl Gpu { completed: GpuState, sb: &mut SysBus, irqs: &mut IrqBitmask, + video_device: &VideoDeviceRcRefCell, ) { match self.state { HDraw => { @@ -419,7 +416,7 @@ impl Gpu { }; sb.io.dmac.notify_vblank(); - self.video_device.borrow_mut().render(&self.frame_buffer); + video_device.borrow_mut().render(&self.frame_buffer); self.cycles_left_for_current_state = CYCLES_SCANLINE; } } @@ -446,11 +443,12 @@ impl Gpu { sb: &mut SysBus, irqs: &mut IrqBitmask, cycles_to_next_event: &mut usize, + video_device: &VideoDeviceRcRefCell, ) { if self.cycles_left_for_current_state <= cycles { let overshoot = cycles - self.cycles_left_for_current_state; - self.on_state_completed(self.state, sb, irqs); + self.on_state_completed(self.state, sb, irqs, video_device); // handle the overshoot if overshoot < self.cycles_left_for_current_state { diff --git a/src/core/sound/mod.rs b/src/core/sound/mod.rs index d030401..abdeaa3 100644 --- a/src/core/sound/mod.rs +++ b/src/core/sound/mod.rs @@ -64,9 +64,6 @@ type AudioDeviceRcRefCell = Rc>; #[derive(DebugStub)] pub struct SoundController { - #[debug_stub = "AudioDeviceRcRefCell"] - audio_device: AudioDeviceRcRefCell, - sample_rate_to_cpu_freq: usize, // how many "cycles" are a sample? cycles: usize, // cycles count when we last provided a new sample. @@ -107,12 +104,9 @@ pub struct SoundController { } impl SoundController { - pub fn new(audio_device: AudioDeviceRcRefCell) -> SoundController { - let resampler = - CosineResampler::new(32768_f32, audio_device.borrow().get_sample_rate() as f32); + pub fn new(audio_device_sample_rate: f32) -> SoundController { + let resampler = CosineResampler::new(32768_f32, audio_device_sample_rate); SoundController { - audio_device: audio_device, - sample_rate_to_cpu_freq: 12345, cycles: 0, mse: false, @@ -321,7 +315,12 @@ impl SoundController { } } - pub fn update(&mut self, cycles: usize, cycles_to_next_event: &mut usize) { + pub fn update( + &mut self, + cycles: usize, + cycles_to_next_event: &mut usize, + audio_device: &AudioDeviceRcRefCell, + ) { self.cycles += cycles; while self.cycles >= self.cycles_per_sample { self.cycles -= self.cycles_per_sample; @@ -338,7 +337,7 @@ impl SoundController { } } - let mut audio = self.audio_device.borrow_mut(); + let mut audio = audio_device.borrow_mut(); self.resampler .push_sample((sample[0], sample[1]), &mut *audio); }