Skip to content

Commit

Permalink
BSNESv115+: apply patch to fix $4203 mul/div behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
Morilli committed Dec 11, 2022
1 parent 74b37ab commit 837592c
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
Binary file modified Assets/dll/bsnes.wbx.zst
Binary file not shown.
2 changes: 2 additions & 0 deletions waterbox/bsnescore/bsnes/sfc/cpu/cpu.hpp
Expand Up @@ -165,7 +165,9 @@ struct CPU : Processor::WDC65816, Thread, PPUcounter {

struct ALU {
uint mpyctr = 0;
uint mpylast = 0;
uint divctr = 0;
uint divlast = 0;
uint shift = 0;
} alu;

Expand Down
16 changes: 10 additions & 6 deletions waterbox/bsnescore/bsnes/sfc/cpu/io.cpp
Expand Up @@ -159,8 +159,10 @@ auto CPU::writeCPU(uint addr, uint8 data) -> void {
io.rddiv = io.wrmpyb << 8 | io.wrmpya;

if(!configuration.hacks.cpu.fastMath) {
alu.mpyctr = 8; //perform multiplication over the next eight cycles
alu.shift = io.wrmpyb;
if (!alu.mpylast) {
alu.mpyctr = 8; //perform multiplication over the next eight cycles
alu.shift = io.wrmpyb;
}
} else {
io.rdmpy = io.wrmpya * io.wrmpyb;
}
Expand All @@ -178,12 +180,14 @@ auto CPU::writeCPU(uint addr, uint8 data) -> void {
io.rdmpy = io.wrdiva;
if(alu.mpyctr || alu.divctr) return;

io.wrdivb = data;

if(!configuration.hacks.cpu.fastMath) {
alu.divctr = 16; //perform division over the next sixteen cycles
alu.shift = io.wrdivb << 16;
if (!alu.divlast) {
io.wrdivb = data;
alu.divctr = 16; //perform division over the next sixteen cycles
alu.shift = io.wrdivb << 16;
}
} else {
io.wrdivb = data;
if(io.wrdivb) {
io.rddiv = io.wrdiva / io.wrdivb;
io.rdmpy = io.wrdiva % io.wrdivb;
Expand Down
8 changes: 8 additions & 0 deletions waterbox/bsnescore/bsnes/sfc/cpu/timing.cpp
Expand Up @@ -143,20 +143,28 @@ auto CPU::scanline() -> void {
auto CPU::aluEdge() -> void {
if(alu.mpyctr) {
alu.mpyctr--;
if (!alu.mpyctr)
alu.mpylast = 1;
if(io.rddiv & 1) io.rdmpy += alu.shift;
io.rddiv >>= 1;
alu.shift <<= 1;
}
else
alu.mpylast = 0;

if(alu.divctr) {
alu.divctr--;
if (!alu.divctr)
alu.divlast = 1;
io.rddiv <<= 1;
alu.shift >>= 1;
if(io.rdmpy >= alu.shift) {
io.rdmpy -= alu.shift;
io.rddiv |= 1;
}
}
else
alu.divlast = 0;
}

auto CPU::dmaEdge() -> void {
Expand Down

0 comments on commit 837592c

Please sign in to comment.