From e45c13f719d61a9f5b07999f539766703bfaa1b3 Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Mon, 14 Jun 2021 23:51:37 -0500 Subject: [PATCH] feat(sound): implement NR10 --- src/bus.rs | 2 ++ src/sound.rs | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/src/bus.rs b/src/bus.rs index 19bee14..f43975b 100644 --- a/src/bus.rs +++ b/src/bus.rs @@ -220,6 +220,7 @@ impl BusIo for Bus { 0x06 => self.timer.modulo, 0x07 => self.timer.ctrl.into(), 0x0F => self.interrupt_flag().into(), + 0x10 => self.sound.ch1.sweep.into(), 0x11 => self.sound.ch1.duty.into(), 0x12 => self.sound.ch1.envelope.into(), 0x14 => self.sound.ch1.freq_hi.into(), @@ -324,6 +325,7 @@ impl BusIo for Bus { 0x06 => self.timer.modulo = byte, 0x07 => self.timer.ctrl = byte.into(), 0x0F => self.set_interrupt_flag(byte), + 0x10 => self.sound.ch1.sweep = byte.into(), 0x11 => self.sound.ch1.duty = byte.into(), 0x12 => self.sound.ch1.envelope = byte.into(), 0x13 => self.sound.ch1.freq_lo = byte.into(), diff --git a/src/sound.rs b/src/sound.rs index 95cea93..81de381 100644 --- a/src/sound.rs +++ b/src/sound.rs @@ -141,6 +141,8 @@ impl From for u8 { #[derive(Debug, Clone, Copy, Default)] pub(crate) struct Channel1 { + /// 0xFF10 | NR10 - Channel 1 Sweep Register + pub(crate) sweep: Sweep, /// 0xFF11 | NR11 - Channel 1 Sound length / Wave pattern duty pub(crate) duty: SoundDuty, /// 0xFF12 | NR12 - Channel 1 Volume Envelope @@ -151,6 +153,61 @@ pub(crate) struct Channel1 { pub(crate) freq_hi: FrequencyHigh, } +bitfield! { + pub struct Sweep(u8); + impl Debug; + time, set_time: 6, 4; + from into SweepChange, change, set_change: 3, 3; + shift_count, set_shift_count: 2, 0; +} + +impl Copy for Sweep {} +impl Clone for Sweep { + fn clone(&self) -> Self { + *self + } +} + +impl Default for Sweep { + fn default() -> Self { + Self(0) + } +} + +impl From for Sweep { + fn from(byte: u8) -> Self { + Self(byte) + } +} + +impl From for u8 { + fn from(sweep: Sweep) -> Self { + sweep.0 + } +} + +#[derive(Debug, Clone, Copy)] +pub(crate) enum SweepChange { + Additive = 0, + Subtractive = 1, +} + +impl From for SweepChange { + fn from(byte: u8) -> Self { + match byte & 0x01 { + 0b00 => Self::Additive, + 0b01 => Self::Subtractive, + _ => unreachable!("{:04X} is not a valid value for SweepChange", byte), + } + } +} + +impl From for u8 { + fn from(change: SweepChange) -> Self { + change as u8 + } +} + #[derive(Debug, Clone, Copy, Default)] pub(crate) struct Channel2 { /// 0xFF16 | NR21 - Channel 2 Sound length / Wave Pattern Duty