From 7fd6bf7f85b3558fe65012ca02426eb2c1a5a2d6 Mon Sep 17 00:00:00 2001 From: bladeoner Date: Mon, 6 Feb 2023 17:19:28 +0100 Subject: [PATCH] Snes9x - Fix dvision routine on SA-1 returns the wrong reminder (and quotient) on negative dividends (#1050) --- source/snes9x/sa1.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/source/snes9x/sa1.cpp b/source/snes9x/sa1.cpp index 9365a1f..62b06e7 100644 --- a/source/snes9x/sa1.cpp +++ b/source/snes9x/sa1.cpp @@ -563,8 +563,9 @@ void S9xSetSA1 (uint8 byte, uint32 address) { int16 dividend = (int16) SA1.op1; uint16 divisor = (uint16) SA1.op2; - uint16 remainder = (dividend >= 0) ? dividend % divisor : (dividend % divisor) + divisor; - uint16 quotient = (dividend - remainder) / divisor; + uint32 dividend_ext = dividend + (uint32)divisor * 65536; + uint16 remainder = dividend_ext % divisor; + uint16 quotient = dividend_ext / divisor; SA1.sum = (remainder << 16) | quotient; }