Skip to content

Commit

Permalink
Merge pull request #58 from YushiOMOTE/add-channel-1-delay
Browse files Browse the repository at this point in the history
Add channel 1 delay test
  • Loading branch information
YushiOMOTE authored Aug 2, 2024
2 parents 9c38251 + be1bb7b commit 828e552
Show file tree
Hide file tree
Showing 4 changed files with 208 additions and 20 deletions.
22 changes: 13 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,19 @@ Test status of [Blargg's Gameboy hardware test ROMs](https://github.com/retrio/g

Test status of [Same Suite](https://github.com/YushiOMOTE/SameSuite/tree/430ab7f68fc612e005ed5586990dfec0ea7a9ce5)

* [x] `apu/div_write_trigger.gb`
* [x] `apu/div_write_trigger_10.gb`
* [x] `apu/div_write_trigger_volume.gb`
* [x] `apu/div_write_trigger_volume_10.gb`
* [x] `apu/div_trigger_volume_10.gb`
* [x] `apu/channel_4/channel_4_lfsr.gb`
* [x] `apu/channel_4/channel_4_lfsr15.gb`
* [x] `apu/channel_4/channel_4_lfsr_7_15.gb`
* [x] `apu/channel_4/channel_4_lfsr_15_7.gb`
* APU
* [x] `apu/div_write_trigger.gb`
* [x] `apu/div_write_trigger_10.gb`
* [x] `apu/div_write_trigger_volume.gb`
* [x] `apu/div_write_trigger_volume_10.gb`
* [x] `apu/div_trigger_volume_10.gb`
* Channel 1
* [x] `apu/channel_1/channel_1_delay.gb`
* Channel 4
* [x] `apu/channel_4/channel_4_lfsr.gb`
* [x] `apu/channel_4/channel_4_lfsr15.gb`
* [x] `apu/channel_4/channel_4_lfsr_7_15.gb`
* [x] `apu/channel_4/channel_4_lfsr_15_7.gb`

## Projects

Expand Down
54 changes: 43 additions & 11 deletions core/src/apu/tone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub struct Tone {
freq: Freq,
dac: Dac,
index: usize,
after_power_on: bool,
}

#[bitfield(u8)]
Expand Down Expand Up @@ -107,6 +108,7 @@ impl Tone {
freq: Freq::default(),
dac: Dac::new(),
index: 0,
after_power_on: true,
}
}

Expand Down Expand Up @@ -202,17 +204,17 @@ impl Tone {
self.length_counter
.update(self.nr14.trigger(), self.nr14.enable_length());

if let Some(sweep) = self.sweep.as_mut() {
sweep.trigger(
self.freq.value(),
self.nr10.freq(),
self.nr10.subtract(),
self.nr10.shift(),
);
}

if self.nr14.trigger() {
self.reload_timer();
if let Some(sweep) = self.sweep.as_mut() {
sweep.trigger(
self.freq.value(),
self.nr10.freq(),
self.nr10.subtract(),
self.nr10.shift(),
);
}

self.load_initial_timer();
self.envelope
.update(self.nr12.init(), self.nr12.count(), self.nr12.increase());
}
Expand All @@ -227,6 +229,8 @@ impl Tone {
sweep.power_on();
}
self.length_counter.power_on();

self.after_power_on = true;
}

pub fn power_off(&mut self) {
Expand Down Expand Up @@ -279,9 +283,17 @@ impl Tone {
self.reload_timer();

self.index = (self.index + 1) % 8;

self.after_power_on = false;
}

fn write_amp(&mut self) {
// Duty Waveform Ratio
// -------------------------
// 0 00000001 12.5%
// 1 10000001 25%
// 2 10000111 50%
// 3 01111110 75%
let wave = match self.nr11.wave_duty() {
0 => [0, 0, 0, 0, 0, 0, 0, 1],
1 => [1, 0, 0, 0, 0, 0, 0, 1],
Expand All @@ -290,7 +302,27 @@ impl Tone {
_ => unreachable!(),
};

self.dac.write(wave[self.index] * self.envelope.amp());
// Just after powering on,
// the first duty step of the square waves after they are triggered for the first time is played
// as if it were 0. Also, the square duty sequence clocking is disabled until the first trigger.
let sample = if self.after_power_on && self.index == 0 {
0
} else {
wave[self.index]
};

self.dac.write(if self.is_active() {
sample * self.envelope.amp()
} else {
0
});
}

fn load_initial_timer(&mut self) {
// It takes (sample length + 2) ticks from the moment channel 1 is
// enabled until PCM12 is affected.
self.timer.reset();
self.timer.set_interval(self.timer_interval() + 2);
}

fn reload_timer(&mut self) {
Expand Down
Loading

0 comments on commit 828e552

Please sign in to comment.