From f533290e855dcdb9d266f147716b10e9a6268058 Mon Sep 17 00:00:00 2001 From: Ember Cold Date: Wed, 21 Jul 2021 15:04:23 +0300 Subject: [PATCH] Handle DIV overflow in lightrec and the interpreter --- deps/lightrec/interpreter.c | 3 +++ libpcsxcore/psxinterpreter.c | 22 ++++++++++++++-------- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/deps/lightrec/interpreter.c b/deps/lightrec/interpreter.c index f586685c..ff609a45 100644 --- a/deps/lightrec/interpreter.c +++ b/deps/lightrec/interpreter.c @@ -801,6 +801,9 @@ static u32 int_special_DIV(struct interpreter *inter) if (rt == 0) { hi = rs; lo = (rs < 0) * 2 - 1; + } else if ((rs == 0x80000000) && (rt == 0xFFFFFFFF)) { + lo = rs; + hi = 0; } else { lo = rs / rt; hi = rs % rt; diff --git a/libpcsxcore/psxinterpreter.c b/libpcsxcore/psxinterpreter.c index d3e42e9f..db8e73b0 100644 --- a/libpcsxcore/psxinterpreter.c +++ b/libpcsxcore/psxinterpreter.c @@ -507,14 +507,20 @@ void psxSLTU() { if (!_Rd_) return; _rRd_ = _u32(_rRs_) < _u32(_rRt_); } // Rd * Format: OP rs, rt * *********************************************************/ void psxDIV() { - if (_i32(_rRt_) != 0) { - _i32(_rLo_) = _i32(_rRs_) / _i32(_rRt_); - _i32(_rHi_) = _i32(_rRs_) % _i32(_rRt_); - } - else { - _i32(_rLo_) = _i32(_rRs_) >= 0 ? 0xffffffff : 1; - _i32(_rHi_) = _i32(_rRs_); - } + if (!_i32(_rRt_)) { + _i32(_rHi_) = _i32(_rRs_); + if (_i32(_rRs_) & 0x80000000) { + _i32(_rLo_) = 1; + } else { + _i32(_rLo_) = 0xFFFFFFFF; + } + } else if (_i32(_rRs_) == 0x80000000 && _i32(_rRt_) == 0xFFFFFFFF) { + _i32(_rLo_) = 0x80000000; + _i32(_rHi_) = 0; + } else { + _i32(_rLo_) = _i32(_rRs_) / _i32(_rRt_); + _i32(_rHi_) = _i32(_rRs_) % _i32(_rRt_); + } } void psxDIVU() { -- 2.39.5