diff --git a/src/bus.rs b/src/bus.rs index ca1bcf4..19bee14 100644 --- a/src/bus.rs +++ b/src/bus.rs @@ -220,12 +220,15 @@ impl BusIo for Bus { 0x06 => self.timer.modulo, 0x07 => self.timer.ctrl.into(), 0x0F => self.interrupt_flag().into(), - 0x11 => self.sound.ch1.sound_duty.into(), - 0x12 => self.sound.ch1.vol_envelope.into(), + 0x11 => self.sound.ch1.duty.into(), + 0x12 => self.sound.ch1.envelope.into(), 0x14 => self.sound.ch1.freq_hi.into(), - 0x24 => self.sound.control.channel.into(), - 0x25 => self.sound.control.output.into(), - 0x26 => self.sound.control.status.into(), + 0x16 => self.sound.ch2.duty.into(), + 0x17 => self.sound.ch2.envelope.into(), + 0x19 => self.sound.ch2.freq_hi.into(), + 0x24 => self.sound.ctrl.channel.into(), + 0x25 => self.sound.ctrl.output.into(), + 0x26 => self.sound.ctrl.status.into(), 0x40 => self.ppu.ctrl.into(), 0x41 => self.ppu.stat.into(), 0x42 => self.ppu.pos.scroll_y, @@ -238,7 +241,6 @@ impl BusIo for Bus { 0x49 => self.ppu.monochrome.obj_palette_1.into(), 0x4A => self.ppu.pos.window_y, 0x4B => self.ppu.pos.window_x, - 0x4D => 0x00, // Reading from this address is useful on the CGB only _ => unimplemented!("Unable to read {:#06X} in I/O Registers", addr), } } @@ -322,13 +324,17 @@ impl BusIo for Bus { 0x06 => self.timer.modulo = byte, 0x07 => self.timer.ctrl = byte.into(), 0x0F => self.set_interrupt_flag(byte), - 0x11 => self.sound.ch1.sound_duty = byte.into(), - 0x12 => self.sound.ch1.vol_envelope = byte.into(), + 0x11 => self.sound.ch1.duty = byte.into(), + 0x12 => self.sound.ch1.envelope = byte.into(), 0x13 => self.sound.ch1.freq_lo = byte.into(), 0x14 => self.sound.ch1.freq_hi = byte.into(), - 0x24 => self.sound.control.channel = byte.into(), - 0x25 => self.sound.control.output = byte.into(), - 0x26 => self.sound.control.status = byte.into(), // FIXME: Should we control which bytes are written to here? + 0x16 => self.sound.ch2.duty = byte.into(), + 0x17 => self.sound.ch2.envelope = byte.into(), + 0x18 => self.sound.ch2.freq_lo = byte.into(), + 0x19 => self.sound.ch2.freq_hi = byte.into(), + 0x24 => self.sound.ctrl.channel = byte.into(), + 0x25 => self.sound.ctrl.output = byte.into(), + 0x26 => self.sound.ctrl.status = byte.into(), // FIXME: Should we control which bytes are written to here? 0x40 => self.ppu.ctrl = byte.into(), 0x41 => self.ppu.stat.update(byte), 0x42 => self.ppu.pos.scroll_y = byte, diff --git a/src/sound.rs b/src/sound.rs index 74771f0..95cea93 100644 --- a/src/sound.rs +++ b/src/sound.rs @@ -1,9 +1,9 @@ -use crate::instruction::Cycle; use bitfield::bitfield; #[derive(Debug, Clone, Copy, Default)] pub(crate) struct Sound { - pub(crate) control: SoundControl, + pub(crate) ctrl: SoundControl, pub(crate) ch1: Channel1, + pub(crate) ch2: Channel2, } impl Sound { @@ -53,7 +53,7 @@ impl From for FrequencyHigh { impl From for u8 { fn from(freq: FrequencyHigh) -> Self { - freq.0 + freq.0 & 0x40 // Only bit 6 can be read } } @@ -142,12 +142,24 @@ impl From for u8 { #[derive(Debug, Clone, Copy, Default)] pub(crate) struct Channel1 { /// 0xFF11 | NR11 - Channel 1 Sound length / Wave pattern duty - pub(crate) sound_duty: SoundDuty, + pub(crate) duty: SoundDuty, /// 0xFF12 | NR12 - Channel 1 Volume Envelope - pub(crate) vol_envelope: VolumeEnvelope, - /// 0xFF13 | NR13 - Channel 1 Frequency Low (Lower 8 bits only) + pub(crate) envelope: VolumeEnvelope, + /// 0xFF13 | NR13 - Channel 1 Frequency low (lower 8 bits only) pub(crate) freq_lo: FrequencyLow, - /// 0xFF14 | NR14 - Channel 1 Frequency High + /// 0xFF14 | NR14 - Channel 1 Frequency high + pub(crate) freq_hi: FrequencyHigh, +} + +#[derive(Debug, Clone, Copy, Default)] +pub(crate) struct Channel2 { + /// 0xFF16 | NR21 - Channel 2 Sound length / Wave Pattern Duty + pub(crate) duty: SoundDuty, + /// 0xFF17 | NR22 - Channel 2 Volume ENvelope + pub(crate) envelope: VolumeEnvelope, + /// 0xFF18 | NR23 - Channel 2 Frequency low (lower 8 bits only) + pub(crate) freq_lo: FrequencyLow, + /// 0xFF19 | NR24 - Channel 2 Frequency high pub(crate) freq_hi: FrequencyHigh, } @@ -234,7 +246,7 @@ impl From for SoundDuty { impl From for u8 { fn from(duty: SoundDuty) -> Self { - duty.0 + duty.0 & 0xC0 // Only bits 7 and 6 can be read } }