Compare commits

..

No commits in common. "32b597a328f66e9b42456805ce579f50a51c651c" and "7112cd15e33fbe6fe2bb5f9f4e1d4985485861c9" have entirely different histories.

7 changed files with 203 additions and 206 deletions

View File

@ -64,29 +64,28 @@ impl BusIo for Apu {
fn write_byte(&mut self, addr: u16, byte: u8) { fn write_byte(&mut self, addr: u16, byte: u8) {
match addr & 0x00FF { match addr & 0x00FF {
0x10 if self.ctrl.enabled => self.ch1.set_sweep(byte), 0x10 => self.ch1.set_sweep(byte),
0x11 if self.ctrl.enabled => self.ch1.set_duty(byte), 0x11 => self.ch1.set_duty(byte),
0x12 if self.ctrl.enabled => self.ch1.set_envelope(byte), 0x12 => self.ch1.set_envelope(byte),
0x13 if self.ctrl.enabled => self.ch1.set_freq_lo(byte), 0x13 => self.ch1.set_freq_lo(byte),
0x14 if self.ctrl.enabled => self.ch1.set_freq_hi(byte), 0x14 => self.ch1.set_freq_hi(byte),
0x16 if self.ctrl.enabled => self.ch2.set_duty(byte), 0x16 => self.ch2.set_duty(byte),
0x17 if self.ctrl.enabled => self.ch2.set_envelope(byte), 0x17 => self.ch2.set_envelope(byte),
0x18 if self.ctrl.enabled => self.ch2.set_freq_lo(byte), 0x18 => self.ch2.set_freq_lo(byte),
0x19 if self.ctrl.enabled => self.ch2.set_freq_hi(byte), 0x19 => self.ch2.set_freq_hi(byte),
0x1A if self.ctrl.enabled => self.ch3.set_enabled(byte), 0x1A => self.ch3.set_enabled(byte),
0x1B if self.ctrl.enabled => self.ch3.set_len(byte), 0x1B => self.ch3.set_len(byte),
0x1C if self.ctrl.enabled => self.ch3.set_volume(byte), 0x1C => self.ch3.set_volume(byte),
0x1D if self.ctrl.enabled => self.ch3.set_freq_lo(byte), 0x1D => self.ch3.set_freq_lo(byte),
0x1E if self.ctrl.enabled => self.ch3.set_freq_hi(byte), 0x1E => self.ch3.set_freq_hi(byte),
0x20 if self.ctrl.enabled => self.ch4.set_len(byte), 0x20 => self.ch4.set_len(byte),
0x21 if self.ctrl.enabled => self.ch4.set_envelope(byte), 0x21 => self.ch4.set_envelope(byte),
0x22 if self.ctrl.enabled => self.ch4.set_poly(byte), 0x22 => self.ch4.set_poly(byte),
0x23 if self.ctrl.enabled => self.ch4.set_frequency(byte), 0x23 => self.ch4.set_freq_data(byte),
0x24 if self.ctrl.enabled => self.ctrl.set_channel(byte), 0x24 => self.ctrl.set_channel(byte),
0x25 if self.ctrl.enabled => self.ctrl.set_output(byte), 0x25 => self.ctrl.set_output(byte),
0x26 => self.set_status(byte), 0x26 => self.set_status(byte),
0x30..=0x3F => self.ch3.write_byte(addr, byte), 0x30..=0x3F => self.ch3.write_byte(addr, byte),
_ if !self.ctrl.enabled => {}
_ => eprintln!( _ => eprintln!(
"Wrote {:#04X} to unused IO register {:#06X} [APU]", "Wrote {:#04X} to unused IO register {:#06X} [APU]",
byte, addr byte, addr
@ -136,7 +135,7 @@ impl Apu {
self.sample_counter %= SM83_CLOCK_SPEED; self.sample_counter %= SM83_CLOCK_SPEED;
if let Some(ref mut prod) = self.prod { if let Some(ref mut prod) = self.prod {
if prod.available_block() { if prod.two_available() {
// Sample the APU // Sample the APU
let ch1_amplitude = self.ch1.amplitude(); let ch1_amplitude = self.ch1.amplitude();
let ch1_left = self.ctrl.output.ch1_left() as u8 as f32 * ch1_amplitude; let ch1_left = self.ctrl.output.ch1_left() as u8 as f32 * ch1_amplitude;
@ -173,21 +172,14 @@ impl Apu {
pub(crate) fn set_status(&mut self, byte: u8) { pub(crate) fn set_status(&mut self, byte: u8) {
self.ctrl.enabled = (byte >> 7) & 0x01 == 0x01; self.ctrl.enabled = (byte >> 7) & 0x01 == 0x01;
if self.ctrl.enabled {
// Frame Sequencer reset to Step 0
self.frame_seq_state = Default::default();
// Square Duty units are reset to first step
self.ch1.duty_pos = 0;
self.ch2.duty_pos = 0;
// Wave Channel's sample buffer reset to 0
}
if !self.ctrl.enabled { if !self.ctrl.enabled {
self.reset(); self.reset();
} else {
} }
self.ch1.enabled = self.ctrl.enabled;
self.ch2.enabled = self.ctrl.enabled;
self.ch3.enabled = self.ctrl.enabled;
self.ch4.enabled = self.ctrl.enabled;
} }
fn reset(&mut self) { fn reset(&mut self) {
@ -196,18 +188,15 @@ impl Apu {
self.ch1.sweep = Default::default(); self.ch1.sweep = Default::default();
self.ch1.duty = Default::default(); self.ch1.duty = Default::default();
self.ch1.envelope = Default::default(); self.ch1.envelope = Default::default();
self.ch1.freq_lo = Default::default(); self.ch1.freq_hi = Default::default(); // FIXME: What about frequency low?
self.ch1.freq_hi = Default::default();
self.ch2.duty = Default::default(); self.ch2.duty = Default::default();
self.ch2.envelope = Default::default(); self.ch2.envelope = Default::default();
self.ch2.freq_lo = Default::default();
self.ch2.freq_hi = Default::default(); self.ch2.freq_hi = Default::default();
self.ch3.enabled = Default::default(); self.ch3.enabled = Default::default();
self.ch3.len = Default::default(); self.ch3.len = Default::default();
self.ch3.volume = Default::default(); self.ch3.volume = Default::default();
self.ch3.freq_lo = Default::default();
self.ch3.freq_hi = Default::default(); self.ch3.freq_hi = Default::default();
self.ch4.len = Default::default(); self.ch4.len = Default::default();
@ -215,17 +204,15 @@ impl Apu {
self.ch4.poly = Default::default(); self.ch4.poly = Default::default();
self.ch4.freq = Default::default(); self.ch4.freq = Default::default();
self.ch2 = Default::default();
self.ch3 = Default::default();
self.ch4 = Default::default();
self.ctrl.channel = Default::default(); self.ctrl.channel = Default::default();
self.ctrl.output = Default::default(); self.ctrl.output = Default::default();
// Disable the rest of the channels
self.ch1.enabled = Default::default();
self.ch2.enabled = Default::default();
self.ch4.enabled = Default::default();
} }
fn clock_length(freq_hi: &FrequencyHigh, length_timer: &mut u16, enabled: &mut bool) { fn clock_length(freq_hi: &FrequencyHigh, length_timer: &mut u16, enabled: &mut bool) {
if freq_hi.length_disable() && *length_timer > 0 { if freq_hi.idk() && *length_timer > 0 {
*length_timer -= 1; *length_timer -= 1;
// Check in this scope ensures (only) the above subtraction // Check in this scope ensures (only) the above subtraction
@ -236,8 +223,8 @@ impl Apu {
} }
} }
fn clock_length_ch4(freq: &Ch4Frequency, length_timer: &mut u16, enabled: &mut bool) { fn clock_length_ch4(freq_data: &Ch4Frequency, length_timer: &mut u16, enabled: &mut bool) {
if freq.length_disable() && *length_timer > 0 { if freq_data.idk() && *length_timer > 0 {
*length_timer -= 1; *length_timer -= 1;
// Check in this scope ensures (only) the above subtraction // Check in this scope ensures (only) the above subtraction
@ -275,20 +262,23 @@ impl Apu {
} }
fn handle_sweep(&mut self) { fn handle_sweep(&mut self) {
if self.ch1.sweep_timer != 0 { if self.ch1.sweep_timer > 0 {
self.ch1.sweep_timer -= 1; self.ch1.sweep_timer -= 1;
} }
if self.ch1.sweep_timer == 0 { if self.ch1.sweep_timer == 0 {
let period = self.ch1.sweep.period(); self.ch1.sweep_timer = if self.ch1.sweep.period() != 0 {
self.ch1.sweep_timer = if period == 0 { 8 } else { period }; self.ch1.sweep.period()
} else {
8
};
if self.ch1.sweep_enabled && period != 0 { if self.ch1.sweep_enabled && self.ch1.sweep.period() != 0 {
let new_freq = self.ch1.calc_sweep_freq(); let new_freq = self.ch1.calc_sweep_freq();
if new_freq <= 2047 && self.ch1.sweep.shift_count() != 0 { if new_freq <= 2047 && self.ch1.sweep.shift_count() != 0 {
self.ch1.set_frequency(new_freq); self.ch1.set_frequency(new_freq);
self.ch1.shadow_freq = new_freq; self.ch1.shadow_freq = new_freq & 0x07FF;
let _ = self.ch1.calc_sweep_freq(); let _ = self.ch1.calc_sweep_freq();
} }
@ -381,7 +371,6 @@ impl SoundControl {
| (apu.ch3.enabled as u8) << 2 | (apu.ch3.enabled as u8) << 2
| (apu.ch2.enabled as u8) << 1 | (apu.ch2.enabled as u8) << 1
| apu.ch1.enabled as u8 | apu.ch1.enabled as u8
| 0x70
} }
} }
@ -442,8 +431,10 @@ impl Channel1 {
/// 0xFF10 | NR10 - Channel 1 Sweep Register /// 0xFF10 | NR10 - Channel 1 Sweep Register
pub(crate) fn set_sweep(&mut self, byte: u8) { pub(crate) fn set_sweep(&mut self, byte: u8) {
if self.enabled {
self.sweep = byte.into() self.sweep = byte.into()
} }
}
/// 0xFF11 | NR11 - Channel 1 Sound length / Wave pattern duty /// 0xFF11 | NR11 - Channel 1 Sound length / Wave pattern duty
pub(crate) fn duty(&self) -> u8 { pub(crate) fn duty(&self) -> u8 {
@ -452,9 +443,11 @@ impl Channel1 {
/// 0xFF11 | NR11 - Channel 1 Sound length / Wave pattern duty /// 0xFF11 | NR11 - Channel 1 Sound length / Wave pattern duty
pub(crate) fn set_duty(&mut self, byte: u8) { pub(crate) fn set_duty(&mut self, byte: u8) {
if self.enabled {
self.duty = byte.into(); self.duty = byte.into();
self.length_timer = 64 - self.duty.sound_length() as u16; self.length_timer = 64 - self.duty.sound_length() as u16;
} }
}
/// 0xFF12 | NR12 - Channel 1 Volume Envelope /// 0xFF12 | NR12 - Channel 1 Volume Envelope
pub fn envelope(&self) -> u8 { pub fn envelope(&self) -> u8 {
@ -463,13 +456,17 @@ impl Channel1 {
/// 0xFF12 | NR12 - Channel 1 Volume Envelope /// 0xFF12 | NR12 - Channel 1 Volume Envelope
pub(crate) fn set_envelope(&mut self, byte: u8) { pub(crate) fn set_envelope(&mut self, byte: u8) {
if self.enabled {
self.envelope = byte.into() self.envelope = byte.into()
} }
}
/// 0xFF13 | NR13 - Channel 1 Frequency low (lower 8 bits only) /// 0xFF13 | NR13 - Channel 1 Frequency low (lower 8 bits only)
pub(crate) fn set_freq_lo(&mut self, byte: u8) { pub(crate) fn set_freq_lo(&mut self, byte: u8) {
if self.enabled {
self.freq_lo = byte; self.freq_lo = byte;
} }
}
/// 0xFF14 | NR14 - Channel 1 Frequency high /// 0xFF14 | NR14 - Channel 1 Frequency high
pub(crate) fn freq_hi(&self) -> u8 { pub(crate) fn freq_hi(&self) -> u8 {
@ -478,6 +475,7 @@ impl Channel1 {
/// 0xFF14 | NR14 - Channel 1 Frequency high /// 0xFF14 | NR14 - Channel 1 Frequency high
pub(crate) fn set_freq_hi(&mut self, byte: u8) { pub(crate) fn set_freq_hi(&mut self, byte: u8) {
if self.enabled {
self.freq_hi = byte.into(); self.freq_hi = byte.into();
// If this bit is set, a trigger event occurs // If this bit is set, a trigger event occurs
@ -487,16 +485,18 @@ impl Channel1 {
self.current_volume = self.envelope.init_vol(); self.current_volume = self.envelope.init_vol();
// Sweep behaviour during trigger event // Sweep behaviour during trigger event
let sweep_period = self.sweep.period(); self.shadow_freq = self.frequency() & 0x07FF; // Mask should be redundant
let sweep_shift = self.sweep.shift_count(); self.sweep_timer = if self.sweep.period() != 0 {
self.shadow_freq = self.frequency(); self.sweep.period()
self.sweep_timer = if sweep_period == 0 { 8 } else { sweep_period }; } else {
8
};
if sweep_period != 0 || sweep_shift != 0 { if self.sweep.period() != 0 || self.sweep.shift_count() != 0 {
self.sweep_enabled = true; self.sweep_enabled = true;
} }
if sweep_shift != 0 { if self.sweep.shift_count() != 0 {
let _ = self.calc_sweep_freq(); let _ = self.calc_sweep_freq();
} }
@ -504,18 +504,17 @@ impl Channel1 {
if self.length_timer == 0 { if self.length_timer == 0 {
self.length_timer = 64; self.length_timer = 64;
} }
}
self.enabled = true;
} }
} }
fn calc_sweep_freq(&mut self) -> u16 { fn calc_sweep_freq(&mut self) -> u16 {
use SweepDirection::*; use SweepDirection::*;
let shifted_shadow_freq = self.shadow_freq >> self.sweep.shift_count();
let shadow_freq_shifted = self.shadow_freq >> self.sweep.shift_count();
let new_freq = match self.sweep.direction() { let new_freq = match self.sweep.direction() {
Increase => self.shadow_freq + shadow_freq_shifted, Increase => self.shadow_freq + shifted_shadow_freq,
Decrease => self.shadow_freq - shadow_freq_shifted, Decrease => self.shadow_freq - shifted_shadow_freq,
}; };
// Overflow check // Overflow check
@ -588,9 +587,11 @@ impl Channel2 {
/// 0xFF16 | NR21 - Channel 2 Sound length / Wave Pattern Duty /// 0xFF16 | NR21 - Channel 2 Sound length / Wave Pattern Duty
pub(crate) fn set_duty(&mut self, byte: u8) { pub(crate) fn set_duty(&mut self, byte: u8) {
if self.enabled {
self.duty = byte.into(); self.duty = byte.into();
self.length_timer = 64 - self.duty.sound_length() as u16; self.length_timer = 64 - self.duty.sound_length() as u16;
} }
}
/// 0xFF17 | NR22 - Channel 2 Volume ENvelope /// 0xFF17 | NR22 - Channel 2 Volume ENvelope
pub(crate) fn envelope(&self) -> u8 { pub(crate) fn envelope(&self) -> u8 {
@ -599,13 +600,17 @@ impl Channel2 {
/// 0xFF17 | NR22 - Channel 2 Volume ENvelope /// 0xFF17 | NR22 - Channel 2 Volume ENvelope
pub(crate) fn set_envelope(&mut self, byte: u8) { pub(crate) fn set_envelope(&mut self, byte: u8) {
if self.enabled {
self.envelope = byte.into() self.envelope = byte.into()
} }
}
/// 0xFF18 | NR23 - Channel 2 Frequency low (lower 8 bits only) /// 0xFF18 | NR23 - Channel 2 Frequency low (lower 8 bits only)
pub(crate) fn set_freq_lo(&mut self, byte: u8) { pub(crate) fn set_freq_lo(&mut self, byte: u8) {
if self.enabled {
self.freq_lo = byte; self.freq_lo = byte;
} }
}
/// 0xFF19 | NR24 - Channel 2 Frequency high /// 0xFF19 | NR24 - Channel 2 Frequency high
pub(crate) fn freq_hi(&self) -> u8 { pub(crate) fn freq_hi(&self) -> u8 {
@ -614,6 +619,7 @@ impl Channel2 {
/// 0xFF19 | NR24 - Channel 2 Frequency high /// 0xFF19 | NR24 - Channel 2 Frequency high
pub(crate) fn set_freq_hi(&mut self, byte: u8) { pub(crate) fn set_freq_hi(&mut self, byte: u8) {
if self.enabled {
self.freq_hi = byte.into(); self.freq_hi = byte.into();
if self.freq_hi.initial() { if self.freq_hi.initial() {
@ -625,8 +631,7 @@ impl Channel2 {
if self.length_timer == 0 { if self.length_timer == 0 {
self.length_timer = 64; self.length_timer = 64;
} }
}
self.enabled = true;
} }
} }
@ -659,21 +664,13 @@ pub(crate) struct Channel3 {
impl BusIo for Channel3 { impl BusIo for Channel3 {
fn read_byte(&self, addr: u16) -> u8 { fn read_byte(&self, addr: u16) -> u8 {
if self.enabled {
self.wave_ram[self.offset as usize]
} else {
self.wave_ram[(addr - Self::WAVE_RAM_START_ADDR) as usize] self.wave_ram[(addr - Self::WAVE_RAM_START_ADDR) as usize]
} }
}
fn write_byte(&mut self, addr: u16, byte: u8) { fn write_byte(&mut self, addr: u16, byte: u8) {
if self.enabled {
self.wave_ram[self.offset as usize] = byte;
} else {
self.wave_ram[(addr - Self::WAVE_RAM_START_ADDR) as usize] = byte; self.wave_ram[(addr - Self::WAVE_RAM_START_ADDR) as usize] = byte;
} }
} }
}
impl Channel3 { impl Channel3 {
const WAVE_RAM_START_ADDR: u16 = 0xFF30; const WAVE_RAM_START_ADDR: u16 = 0xFF30;
@ -695,9 +692,11 @@ impl Channel3 {
/// 0xFF1B | NR31 - Sound Length /// 0xFF1B | NR31 - Sound Length
pub(crate) fn set_len(&mut self, byte: u8) { pub(crate) fn set_len(&mut self, byte: u8) {
if self.enabled {
self.len = byte; self.len = byte;
self.length_timer = 256 - self.len as u16; self.length_timer = 256 - self.len as u16;
} }
}
/// 0xFF1C | NR32 - Channel 3 Volume /// 0xFF1C | NR32 - Channel 3 Volume
pub(crate) fn volume(&self) -> u8 { pub(crate) fn volume(&self) -> u8 {
@ -708,6 +707,7 @@ impl Channel3 {
pub(crate) fn set_volume(&mut self, byte: u8) { pub(crate) fn set_volume(&mut self, byte: u8) {
use Ch3Volume::*; use Ch3Volume::*;
if self.enabled {
self.volume = match (byte >> 5) & 0x03 { self.volume = match (byte >> 5) & 0x03 {
0b00 => Mute, 0b00 => Mute,
0b01 => Full, 0b01 => Full,
@ -716,11 +716,14 @@ impl Channel3 {
_ => unreachable!("{:#04X} is not a valid value for Channel3Volume", byte), _ => unreachable!("{:#04X} is not a valid value for Channel3Volume", byte),
}; };
} }
}
/// 0xFF1D | NR33 - Channel 3 Frequency low (lower 8 bits) /// 0xFF1D | NR33 - Channel 3 Frequency low (lower 8 bits)
pub(crate) fn set_freq_lo(&mut self, byte: u8) { pub(crate) fn set_freq_lo(&mut self, byte: u8) {
if self.enabled {
self.freq_lo = byte; self.freq_lo = byte;
} }
}
/// 0xFF1E | NR34 - Channel 3 Frequency high /// 0xFF1E | NR34 - Channel 3 Frequency high
pub(crate) fn freq_hi(&self) -> u8 { pub(crate) fn freq_hi(&self) -> u8 {
@ -729,6 +732,7 @@ impl Channel3 {
/// 0xFF1E | NR34 - Channel 3 Frequency high /// 0xFF1E | NR34 - Channel 3 Frequency high
pub(crate) fn set_freq_hi(&mut self, byte: u8) { pub(crate) fn set_freq_hi(&mut self, byte: u8) {
if self.enabled {
self.freq_hi = byte.into(); self.freq_hi = byte.into();
if self.freq_hi.initial() { if self.freq_hi.initial() {
@ -736,8 +740,7 @@ impl Channel3 {
if self.length_timer == 0 { if self.length_timer == 0 {
self.length_timer = 256; self.length_timer = 256;
} }
}
self.enabled = true;
} }
} }
@ -753,13 +756,13 @@ impl Channel3 {
} }
if self.freq_timer == 0 { if self.freq_timer == 0 {
self.freq_timer = (2048 - self.frequency()) * 2; self.freq_timer = (2048 - self.frequency()) * 4;
self.offset = (self.offset + 1) % (WAVE_PATTERN_RAM_LEN * 2) as u8; self.offset = (self.offset + 1) % 8;
} }
} }
fn read_sample(&self, index: u8) -> u8 { fn read_sample(&self, index: u8) -> u8 {
let i = (index / 2) as usize; let i = index as usize / 2;
if index % 2 == 0 { if index % 2 == 0 {
// We grab the high nibble on even indexes // We grab the high nibble on even indexes
@ -809,9 +812,11 @@ impl Channel4 {
/// 0xFF20 | NR41 - Channel 4 Sound Length /// 0xFF20 | NR41 - Channel 4 Sound Length
pub(crate) fn set_len(&mut self, byte: u8) { pub(crate) fn set_len(&mut self, byte: u8) {
if self.enabled {
self.len = byte & 0x3F; self.len = byte & 0x3F;
self.length_timer = 256 - self.len as u16; self.length_timer = 256 - self.len as u16;
} }
}
/// 0xFF21 | NR42 - Channel 4 Volume Envelope /// 0xFF21 | NR42 - Channel 4 Volume Envelope
pub(crate) fn envelope(&self) -> u8 { pub(crate) fn envelope(&self) -> u8 {
@ -820,8 +825,10 @@ impl Channel4 {
/// 0xFF21 | NR42 - Channel 4 Volume Envelope /// 0xFF21 | NR42 - Channel 4 Volume Envelope
pub(crate) fn set_envelope(&mut self, byte: u8) { pub(crate) fn set_envelope(&mut self, byte: u8) {
if self.enabled {
self.envelope = byte.into() self.envelope = byte.into()
} }
}
/// 0xFF22 | NR43 - Chanel 4 Polynomial Counter /// 0xFF22 | NR43 - Chanel 4 Polynomial Counter
pub(crate) fn poly(&self) -> u8 { pub(crate) fn poly(&self) -> u8 {
@ -830,8 +837,10 @@ impl Channel4 {
/// 0xFF22 | NR43 - Chanel 4 Polynomial Counter /// 0xFF22 | NR43 - Chanel 4 Polynomial Counter
pub(crate) fn set_poly(&mut self, byte: u8) { pub(crate) fn set_poly(&mut self, byte: u8) {
if self.enabled {
self.poly = byte.into(); self.poly = byte.into();
} }
}
/// 0xFF23 | NR44 - Channel 4 Counter / Consecutive Selector and Restart /// 0xFF23 | NR44 - Channel 4 Counter / Consecutive Selector and Restart
pub(crate) fn frequency(&self) -> u8 { pub(crate) fn frequency(&self) -> u8 {
@ -839,7 +848,8 @@ impl Channel4 {
} }
/// 0xFF23 | NR44 - Channel 4 Counter / Consecutive Selector and Restart /// 0xFF23 | NR44 - Channel 4 Counter / Consecutive Selector and Restart
pub(crate) fn set_frequency(&mut self, byte: u8) { pub(crate) fn set_freq_data(&mut self, byte: u8) {
if self.enabled {
self.freq = byte.into(); self.freq = byte.into();
if self.freq.initial() { if self.freq.initial() {
@ -854,8 +864,7 @@ impl Channel4 {
// LFSR behaviour during trigger event // LFSR behaviour during trigger event
self.lf_shift = 0x7FFF; self.lf_shift = 0x7FFF;
}
self.enabled = true;
} }
} }

View File

@ -43,17 +43,10 @@ impl<T> SampleProducer<T> {
self.inner.push(value) self.inner.push(value)
} }
pub(crate) fn available(&self) -> bool { #[inline]
pub(crate) fn two_available(&self) -> bool {
self.inner.slots() > 2 self.inner.slots() > 2
} }
pub(crate) fn available_block(&self) -> bool {
loop {
if self.inner.slots() > 2 {
break true;
}
}
}
} }
impl<T> std::fmt::Debug for SampleProducer<T> { impl<T> std::fmt::Debug for SampleProducer<T> {

View File

@ -178,12 +178,12 @@ pub(crate) mod ch4 {
pub struct Frequency(u8); pub struct Frequency(u8);
impl Debug; impl Debug;
_initial, _: 7; _initial, _: 7;
_length_disable, _: 6; // TODO: same as FrequencyHigh, figure out what this is _idk, _: 6; // TODO: same as FrequencyHigh, figure out what this is
} }
impl Frequency { impl Frequency {
pub(crate) fn length_disable(&self) -> bool { pub(crate) fn idk(&self) -> bool {
self._length_disable() self._idk()
} }
pub(crate) fn initial(&self) -> bool { pub(crate) fn initial(&self) -> bool {
@ -224,7 +224,7 @@ pub(crate) mod common {
pub struct FrequencyHigh(u8); pub struct FrequencyHigh(u8);
impl Debug; impl Debug;
_initial, _: 7; _initial, _: 7;
_length_disable, _: 6; // TODO: Figure out what the hell this is _idk, _: 6; // TODO: Figure out what the hell this is
pub freq_bits, set_freq_bits: 2, 0; pub freq_bits, set_freq_bits: 2, 0;
} }
@ -233,8 +233,8 @@ pub(crate) mod common {
self._initial() self._initial()
} }
pub(crate) fn length_disable(&self) -> bool { pub(crate) fn idk(&self) -> bool {
self._length_disable() self._idk()
} }
} }

View File

@ -66,7 +66,6 @@ impl Bus {
self.cartridge.as_ref()?.title() self.cartridge.as_ref()?.title()
} }
#[allow(dead_code)]
pub(crate) fn boot_mapped(&self) -> bool { pub(crate) fn boot_mapped(&self) -> bool {
self.boot.is_some() self.boot.is_some()
} }

View File

@ -33,11 +33,18 @@ pub fn run(
game_boy: &mut SM83, game_boy: &mut SM83,
gamepad: &mut Gilrs, gamepad: &mut Gilrs,
input: &WinitInputHelper, input: &WinitInputHelper,
target: Cycle, pending: Cycle,
) -> Cycle { ) -> Cycle {
let mut elapsed = Cycle::new(0); let mut elapsed = Cycle::new(0);
while elapsed < target { while elapsed < pending {
elapsed += run_unsynced(game_boy, gamepad, input);
}
elapsed
}
pub fn run_unsynced(game_boy: &mut SM83, gamepad: &mut Gilrs, input: &WinitInputHelper) -> Cycle {
if GAMEPAD_ENABLED { if GAMEPAD_ENABLED {
if let Some(event) = gamepad.next_event() { if let Some(event) = gamepad.next_event() {
joypad::handle_gamepad_input(game_boy.joypad_mut(), event); joypad::handle_gamepad_input(game_boy.joypad_mut(), event);
@ -45,27 +52,7 @@ pub fn run(
} }
joypad::handle_keyboard_input(game_boy.joypad_mut(), input); joypad::handle_keyboard_input(game_boy.joypad_mut(), input);
elapsed += game_boy.step(); game_boy.step()
}
elapsed
}
pub fn run_frame(game_boy: &mut SM83, gamepad: &mut Gilrs, input: &WinitInputHelper) -> Cycle {
let mut elapsed = Cycle::new(0);
while elapsed < CYCLES_IN_FRAME {
if GAMEPAD_ENABLED {
if let Some(event) = gamepad.next_event() {
joypad::handle_gamepad_input(game_boy.joypad_mut(), event);
}
}
joypad::handle_keyboard_input(game_boy.joypad_mut(), input);
elapsed += game_boy.step();
}
elapsed
} }
pub fn draw(ppu: &Ppu, frame: &mut [u8]) { pub fn draw(ppu: &Ppu, frame: &mut [u8]) {

View File

@ -1456,6 +1456,7 @@ impl Instruction {
} }
} }
#[inline]
fn unprefixed(byte: u8) -> Self { fn unprefixed(byte: u8) -> Self {
use Instruction::*; use Instruction::*;
@ -1572,6 +1573,7 @@ impl Instruction {
} }
} }
#[inline]
fn prefixed(byte: u8) -> Self { fn prefixed(byte: u8) -> Self {
use Instruction::*; use Instruction::*;
@ -1932,6 +1934,7 @@ mod table {
} }
} }
#[inline]
pub(crate) fn group1(code: u8) -> Group1RegisterPair { pub(crate) fn group1(code: u8) -> Group1RegisterPair {
use Group1RegisterPair::*; use Group1RegisterPair::*;
@ -1944,6 +1947,7 @@ mod table {
} }
} }
#[inline]
pub(crate) fn group2(code: u8) -> Group2RegisterPair { pub(crate) fn group2(code: u8) -> Group2RegisterPair {
use Group2RegisterPair::*; use Group2RegisterPair::*;
@ -1956,6 +1960,7 @@ mod table {
} }
} }
#[inline]
pub(crate) fn group3(code: u8) -> Group3RegisterPair { pub(crate) fn group3(code: u8) -> Group3RegisterPair {
use Group3RegisterPair::*; use Group3RegisterPair::*;
@ -1968,6 +1973,7 @@ mod table {
} }
} }
#[inline]
pub(crate) fn register(code: u8) -> Register { pub(crate) fn register(code: u8) -> Register {
use Register::*; use Register::*;
@ -1984,6 +1990,7 @@ mod table {
} }
} }
#[inline]
pub(crate) fn jump_cond(code: u8) -> JumpCondition { pub(crate) fn jump_cond(code: u8) -> JumpCondition {
use JumpCondition::*; use JumpCondition::*;
@ -1996,6 +2003,7 @@ mod table {
} }
} }
#[inline]
pub(crate) fn flag_instr(code: u8) -> Instruction { pub(crate) fn flag_instr(code: u8) -> Instruction {
use Instruction::*; use Instruction::*;
@ -2012,6 +2020,7 @@ mod table {
} }
} }
#[inline]
pub(crate) fn alu_reg_instr(alu_code: u8, reg_code: u8) -> Instruction { pub(crate) fn alu_reg_instr(alu_code: u8, reg_code: u8) -> Instruction {
use Instruction::*; use Instruction::*;
@ -2028,6 +2037,7 @@ mod table {
} }
} }
#[inline]
pub(crate) fn alu_imm_instr(code: u8) -> Instruction { pub(crate) fn alu_imm_instr(code: u8) -> Instruction {
use Instruction::*; use Instruction::*;
@ -2044,6 +2054,7 @@ mod table {
} }
} }
#[inline]
pub(crate) fn prefix_alu(alu_code: u8, reg_code: u8) -> Instruction { pub(crate) fn prefix_alu(alu_code: u8, reg_code: u8) -> Instruction {
use Instruction::*; use Instruction::*;

View File

@ -4,14 +4,14 @@ use gb::{AudioSPSC, Cycle, GB_HEIGHT, GB_WIDTH};
use gilrs::Gilrs; use gilrs::Gilrs;
use pixels::{PixelsBuilder, SurfaceTexture}; use pixels::{PixelsBuilder, SurfaceTexture};
use rodio::{OutputStream, Sink}; use rodio::{OutputStream, Sink};
use std::time::{Duration, Instant}; use std::time::Instant;
use winit::dpi::LogicalSize; use winit::dpi::LogicalSize;
use winit::event::{Event, VirtualKeyCode}; use winit::event::{Event, VirtualKeyCode};
use winit::event_loop::{ControlFlow, EventLoop}; use winit::event_loop::{ControlFlow, EventLoop};
use winit::window::{Window, WindowBuilder}; use winit::window::{Window, WindowBuilder};
use winit_input_helper::WinitInputHelper; use winit_input_helper::WinitInputHelper;
const WINDOW_SCALE: f64 = 2.0; const SCALE: f64 = 2.0;
fn main() -> Result<()> { fn main() -> Result<()> {
let app = App::new(crate_name!()) let app = App::new(crate_name!())
@ -38,20 +38,23 @@ fn main() -> Result<()> {
) )
.get_matches(); .get_matches();
// `rom` is a required value in every situation so this will
// always exist.
let rom_path = m let rom_path = m
.value_of("rom") .value_of("rom")
.expect("Required value 'rom' was provided"); .expect("Required value 'rom' was provided");
let mut game_boy = let mut game_boy =
gb::emu::init(m.value_of("boot"), rom_path).expect("Initialize DMG-01 Emulator"); gb::emu::init(m.value_of("boot"), rom_path).expect("Initialized DMG-01 Emulator");
let rom_title = gb::emu::rom_title(&game_boy); let cartridge_title = gb::emu::rom_title(&game_boy);
let mut gamepad = Gilrs::new().expect("Initialize Controller Support"); // Initialize Gamepad Support
let mut gamepad = Gilrs::new().expect("Initialized Gilrs for Controller Input");
// Initialize GUI // Initialize GUI
let event_loop = EventLoop::new(); let event_loop = EventLoop::new();
let mut input = WinitInputHelper::new(); let mut input = WinitInputHelper::new();
let window = create_window(&event_loop, rom_title)?; let window = create_window(&event_loop, cartridge_title)?;
let mut pixels = { let mut pixels = {
let size = window.inner_size(); let size = window.inner_size();
@ -62,20 +65,21 @@ fn main() -> Result<()> {
.build()? .build()?
}; };
// Initialize Audio
let spsc: AudioSPSC<f32> = Default::default(); let spsc: AudioSPSC<f32> = Default::default();
let (prod, cons) = spsc.init(); let (prod, cons) = spsc.init();
game_boy.apu_mut().set_producer(prod);
// Initialize Audio
let (_stream, stream_handle) = OutputStream::try_default().expect("Initialized Audio"); let (_stream, stream_handle) = OutputStream::try_default().expect("Initialized Audio");
let sink = Sink::try_new(&stream_handle)?; let sink = Sink::try_new(&stream_handle)?;
sink.append(cons); sink.append(cons);
game_boy.apu_mut().set_producer(prod);
std::thread::spawn(move || { std::thread::spawn(move || {
sink.sleep_until_end(); sink.sleep_until_end();
}); });
let mut start = Instant::now(); let mut now = Instant::now();
let frame_time = Duration::from_secs_f64(1.0 / 60.0); // 60 Hz on Host
let mut cycle_count: Cycle = Default::default(); let mut cycle_count: Cycle = Default::default();
event_loop.run(move |event, _, control_flow| { event_loop.run(move |event, _, control_flow| {
@ -100,17 +104,14 @@ fn main() -> Result<()> {
pixels.resize_surface(size.width, size.height); pixels.resize_surface(size.width, size.height);
} }
let mut diff = Instant::now() - start; let delta = now.elapsed().subsec_nanos();
while diff.subsec_nanos() < frame_time.subsec_nanos() { now = Instant::now();
if cycle_count < gb::emu::CYCLES_IN_FRAME {
cycle_count += gb::emu::run_frame(&mut game_boy, &mut gamepad, &input);
}
diff = Instant::now() - start; let pending = Cycle::new(delta / gb::emu::SM83_CYCLE_TIME.subsec_nanos());
} cycle_count += gb::emu::run(&mut game_boy, &mut gamepad, &input, pending);
start = Instant::now();
if cycle_count >= gb::emu::CYCLES_IN_FRAME { if cycle_count >= gb::emu::CYCLES_IN_FRAME {
// Draw Frame
cycle_count %= gb::emu::CYCLES_IN_FRAME; cycle_count %= gb::emu::CYCLES_IN_FRAME;
gb::emu::draw(game_boy.ppu(), pixels.get_frame()); gb::emu::draw(game_boy.ppu(), pixels.get_frame());
@ -137,10 +138,7 @@ fn create_window(event_loop: &EventLoop<()>, title: &str) -> Result<Window> {
fn create_window(event_loop: &EventLoop<()>, title: &str) -> Result<Window> { fn create_window(event_loop: &EventLoop<()>, title: &str) -> Result<Window> {
use winit::platform::windows::WindowBuilderExtWindows; use winit::platform::windows::WindowBuilderExtWindows;
let size = LogicalSize::new( let size = LogicalSize::new((GB_WIDTH as f64) * SCALE, (GB_HEIGHT as f64) * SCALE);
(GB_WIDTH as f64) * WINDOW_SCALE,
(GB_HEIGHT as f64) * WINDOW_SCALE,
);
Ok(WindowBuilder::new() Ok(WindowBuilder::new()
.with_title(title) .with_title(title)
.with_inner_size(size) .with_inner_size(size)