feat(sound): implement NR10

This commit is contained in:
Rekai Nyangadzayi Musuka 2021-06-14 23:51:37 -05:00
parent 6a7ff66274
commit e45c13f719
2 changed files with 59 additions and 0 deletions

View File

@ -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(),

View File

@ -141,6 +141,8 @@ impl From<SoundStatus> 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<u8> for Sweep {
fn from(byte: u8) -> Self {
Self(byte)
}
}
impl From<Sweep> for u8 {
fn from(sweep: Sweep) -> Self {
sweep.0
}
}
#[derive(Debug, Clone, Copy)]
pub(crate) enum SweepChange {
Additive = 0,
Subtractive = 1,
}
impl From<u8> 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<SweepChange> 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