Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ Madagascar*
ferris.png
331269-*
index.html?*

# Python bytecode
__pycache__/
*.pyc
25 changes: 22 additions & 3 deletions core/src/cpu/peripheral/apu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ const TICK_GRAN: u16 = 32;
/// About 804 stereo pairs are produced per Game Boy frame at 48 kHz.
/// Reserve some headroom so the hot audio path doesn't regrow this buffer.
const SAMPLE_BUFFER_CAPACITY_HINT: usize = 2048;

/// Max possible mixer output before normalization: 4 channels * level 15 * volume 8.
const MIXER_MAX: u32 = 4 * 15 * 8;

Expand Down Expand Up @@ -874,6 +875,18 @@ impl ApuPeripheral {
self.channel4.advance_frequency_noise(cycles);
}

/// Capacity we are willing to trust. Never the in-memory `cap` word alone:
/// `Vec::push` grows only when `len == cap`, so a `cap` corrupted UPWARD is
/// never equal to `len` and every push then runs past the real allocation.
/// `cap.min(HINT) <= cap` unconditionally, so this can never drop samples
/// from a correctly sized buffer.
#[inline(always)]
fn trusted_capacity(&self) -> usize {
self.sample_buffer
.capacity()
.min(SAMPLE_BUFFER_CAPACITY_HINT)
}

/// Downsample to 48 kHz and push stereo PCM pairs into `sample_buffer`.
///
/// The Pico runtime almost always calls this with `cycles` <= 4, so a
Expand All @@ -885,7 +898,7 @@ impl ApuPeripheral {
if cycles <= 4 {
self.sample_acc += sample_inc;
if self.sample_acc >= SAMPLE_PERIOD_NUM
&& self.sample_buffer.len() + 2 <= self.sample_buffer.capacity()
&& self.sample_buffer.len() + 2 <= self.trusted_capacity()
{
self.sample_acc -= SAMPLE_PERIOD_NUM;
let (left, right) = self.mix_sample();
Expand All @@ -899,8 +912,14 @@ impl ApuPeripheral {
let n_samples = acc / SAMPLE_PERIOD_NUM as u64;
self.sample_acc = (acc % SAMPLE_PERIOD_NUM as u64) as u32;
if n_samples != 0 {
let cap = self.sample_buffer.capacity();
let pushable = ((cap - self.sample_buffer.len()) / 2).min(n_samples as usize);
// `saturating_sub`, NOT `-`: if `len > cap` the plain
// subtraction underflows and `pushable` stops being bounded by
// capacity, spraying i16 samples past the allocation.
let pushable = (self
.trusted_capacity()
.saturating_sub(self.sample_buffer.len())
/ 2)
.min(n_samples as usize);
for _ in 0..pushable {
let (left, right) = self.mix_sample();
self.sample_buffer.push(left);
Expand Down
2 changes: 2 additions & 0 deletions platform/pico2w/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ std = []
# CPU overclock options (clk_sys target). Default boot is 300MHz; see main.rs.
oc-266 = []
oc-280 = []
# 288 MHz: one PLL grid step below 300 (FBDIV 120). Clean where 300 is not.
oc-288 = []
oc-300 = []
# Log FPS to RTT every 60 frames.
fps = []
Expand Down
Loading
Loading