chore(sound): implement channel 2 registers

This commit is contained in:
Rekai Nyangadzayi Musuka 2021-06-14 23:37:30 -05:00
parent f2513c6964
commit 6a7ff66274
2 changed files with 37 additions and 19 deletions

View File

@ -220,12 +220,15 @@ impl BusIo for Bus {
0x06 => self.timer.modulo, 0x06 => self.timer.modulo,
0x07 => self.timer.ctrl.into(), 0x07 => self.timer.ctrl.into(),
0x0F => self.interrupt_flag().into(), 0x0F => self.interrupt_flag().into(),
0x11 => self.sound.ch1.sound_duty.into(), 0x11 => self.sound.ch1.duty.into(),
0x12 => self.sound.ch1.vol_envelope.into(), 0x12 => self.sound.ch1.envelope.into(),
0x14 => self.sound.ch1.freq_hi.into(), 0x14 => self.sound.ch1.freq_hi.into(),
0x24 => self.sound.control.channel.into(), 0x16 => self.sound.ch2.duty.into(),
0x25 => self.sound.control.output.into(), 0x17 => self.sound.ch2.envelope.into(),
0x26 => self.sound.control.status.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(), 0x40 => self.ppu.ctrl.into(),
0x41 => self.ppu.stat.into(), 0x41 => self.ppu.stat.into(),
0x42 => self.ppu.pos.scroll_y, 0x42 => self.ppu.pos.scroll_y,
@ -238,7 +241,6 @@ impl BusIo for Bus {
0x49 => self.ppu.monochrome.obj_palette_1.into(), 0x49 => self.ppu.monochrome.obj_palette_1.into(),
0x4A => self.ppu.pos.window_y, 0x4A => self.ppu.pos.window_y,
0x4B => self.ppu.pos.window_x, 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), _ => unimplemented!("Unable to read {:#06X} in I/O Registers", addr),
} }
} }
@ -322,13 +324,17 @@ impl BusIo for Bus {
0x06 => self.timer.modulo = byte, 0x06 => self.timer.modulo = byte,
0x07 => self.timer.ctrl = byte.into(), 0x07 => self.timer.ctrl = byte.into(),
0x0F => self.set_interrupt_flag(byte), 0x0F => self.set_interrupt_flag(byte),
0x11 => self.sound.ch1.sound_duty = byte.into(), 0x11 => self.sound.ch1.duty = byte.into(),
0x12 => self.sound.ch1.vol_envelope = byte.into(), 0x12 => self.sound.ch1.envelope = byte.into(),
0x13 => self.sound.ch1.freq_lo = byte.into(), 0x13 => self.sound.ch1.freq_lo = byte.into(),
0x14 => self.sound.ch1.freq_hi = byte.into(), 0x14 => self.sound.ch1.freq_hi = byte.into(),
0x24 => self.sound.control.channel = byte.into(), 0x16 => self.sound.ch2.duty = byte.into(),
0x25 => self.sound.control.output = byte.into(), 0x17 => self.sound.ch2.envelope = byte.into(),
0x26 => self.sound.control.status = byte.into(), // FIXME: Should we control which bytes are written to here? 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(), 0x40 => self.ppu.ctrl = byte.into(),
0x41 => self.ppu.stat.update(byte), 0x41 => self.ppu.stat.update(byte),
0x42 => self.ppu.pos.scroll_y = byte, 0x42 => self.ppu.pos.scroll_y = byte,

View File

@ -1,9 +1,9 @@
use crate::instruction::Cycle;
use bitfield::bitfield; use bitfield::bitfield;
#[derive(Debug, Clone, Copy, Default)] #[derive(Debug, Clone, Copy, Default)]
pub(crate) struct Sound { pub(crate) struct Sound {
pub(crate) control: SoundControl, pub(crate) ctrl: SoundControl,
pub(crate) ch1: Channel1, pub(crate) ch1: Channel1,
pub(crate) ch2: Channel2,
} }
impl Sound { impl Sound {
@ -53,7 +53,7 @@ impl From<u8> for FrequencyHigh {
impl From<FrequencyHigh> for u8 { impl From<FrequencyHigh> for u8 {
fn from(freq: FrequencyHigh) -> Self { fn from(freq: FrequencyHigh) -> Self {
freq.0 freq.0 & 0x40 // Only bit 6 can be read
} }
} }
@ -142,12 +142,24 @@ impl From<SoundStatus> for u8 {
#[derive(Debug, Clone, Copy, Default)] #[derive(Debug, Clone, Copy, Default)]
pub(crate) struct Channel1 { pub(crate) struct Channel1 {
/// 0xFF11 | NR11 - Channel 1 Sound length / Wave pattern duty /// 0xFF11 | NR11 - Channel 1 Sound length / Wave pattern duty
pub(crate) sound_duty: SoundDuty, pub(crate) duty: SoundDuty,
/// 0xFF12 | NR12 - Channel 1 Volume Envelope /// 0xFF12 | NR12 - Channel 1 Volume Envelope
pub(crate) vol_envelope: VolumeEnvelope, pub(crate) envelope: VolumeEnvelope,
/// 0xFF13 | NR13 - Channel 1 Frequency Low (Lower 8 bits only) /// 0xFF13 | NR13 - Channel 1 Frequency low (lower 8 bits only)
pub(crate) freq_lo: FrequencyLow, 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, pub(crate) freq_hi: FrequencyHigh,
} }
@ -234,7 +246,7 @@ impl From<u8> for SoundDuty {
impl From<SoundDuty> for u8 { impl From<SoundDuty> for u8 {
fn from(duty: SoundDuty) -> Self { fn from(duty: SoundDuty) -> Self {
duty.0 duty.0 & 0xC0 // Only bits 7 and 6 can be read
} }
} }