Audio: reduce the title's XMA read advance before adding it

ADVANCE_XMA_READ added the title's block count to the 5-bit read block
in u32 before taking it modulo the ring size, so a count near u32::MAX
overflowed (a panic in debug builds). The count is reduced modulo the
ring first; the result is the same for every count that did not
overflow.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
Manuel PérezandClaude Opus 5.5 committed 2026-11-24 01:21:41 +01:00
1 parent b9aff99b98
commit fba9d1d3a9
1 file changed
+9 -1
+9 -1
View File
@@ -471,7 +471,8 @@ impl Xma {
// The decoder writes the same words.
let mut slot = lock(&self.voices[index as usize]);
let size = context.field(0, 22, 5).max(1);
let read = (context.field(9, 0, 5) + blocks) % size;
// The count is the title's: reduced first, so the sum stays small.
let read = (context.field(9, 0, 5) + blocks % size) % size;
context.set_field(9, 0, 5, read);
if blocks > 0 {
context.set_field(1, 31, 1, 1);
@@ -1062,6 +1063,13 @@ mod tests {
assert_eq!(context.field(0, 20, 2), 0, "input used up");
assert_eq!(context.field(2, 26, 5), INPUT_INVALID);
// A count near `u32::MAX` wraps the read block like any other
// (3 + 0xffff_ffff blocks on a ring of four is block 2); the sum
// used to overflow.
context.set_field(9, 0, 5, 3);
submit(ADVANCE_XMA_READ, &[1, u32::MAX]);
assert_eq!(context.field(9, 0, 5), 2);
submit(DISABLE_XMA, &[1]);
assert_eq!(produced(&xma()), None);
(m.release)(acp);