Bläddra i källkod

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]>
Manuel Pérez 3 dagar sedan
förälder
incheckning
257d8bbab2
1 ändrade filer med 9 tillägg och 1 borttagningar
  1. 9 1
      crates/victory-hle/src/modules/audio.rs

+ 9 - 1
crates/victory-hle/src/modules/audio.rs

@@ -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);