Skip to content

Commit

Permalink
wdc65816: emulate (direct,X) wraparound bug in emulation mode
Browse files Browse the repository at this point in the history
Manually cherry-picked ares commit ares-emulator/ares@be8fa76

Co-Authored-By: Adrian Siekierka <[email protected]>
  • Loading branch information
2 people authored and Screwtapello committed Mar 1, 2024
1 parent 4faca65 commit ccbe394
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 8 deletions.
8 changes: 4 additions & 4 deletions bsnes/processor/wdc65816/instructions-read.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ auto WDC65816::instructionIndexedIndirectRead8(alu8 op) -> void {
U.l = fetch();
idle2();
idle();
V.l = readDirect(U.l + X.w + 0);
V.h = readDirect(U.l + X.w + 1);
V.l = readDirectX(U.l + X.w, 0);
V.h = readDirectX(U.l + X.w, 1);
L W.l = readBank(V.w + 0);
alu(W.l);
}
Expand All @@ -123,8 +123,8 @@ auto WDC65816::instructionIndexedIndirectRead16(alu16 op) -> void {
U.l = fetch();
idle2();
idle();
V.l = readDirect(U.l + X.w + 0);
V.h = readDirect(U.l + X.w + 1);
V.l = readDirectX(U.l + X.w, 0);
V.h = readDirectX(U.l + X.w, 1);
W.l = readBank(V.w + 0);
L W.h = readBank(V.w + 1);
alu(W.w);
Expand Down
8 changes: 4 additions & 4 deletions bsnes/processor/wdc65816/instructions-write.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,17 @@ auto WDC65816::instructionIndexedIndirectWrite8() -> void {
U.l = fetch();
idle2();
idle();
V.l = readDirect(U.l + X.w + 0);
V.h = readDirect(U.l + X.w + 1);
V.l = readDirectX(U.l + X.w, 0);
V.h = readDirectX(U.l + X.w, 1);
L writeBank(V.w + 0, A.l);
}

auto WDC65816::instructionIndexedIndirectWrite16() -> void {
U.l = fetch();
idle2();
idle();
V.l = readDirect(U.l + X.w + 0);
V.h = readDirect(U.l + X.w + 1);
V.l = readDirectX(U.l + X.w, 0);
V.h = readDirectX(U.l + X.w, 1);
writeBank(V.w + 0, A.l);
L writeBank(V.w + 1, A.h);
}
Expand Down
7 changes: 7 additions & 0 deletions bsnes/processor/wdc65816/memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ auto WDC65816::writeDirect(uint address, uint8 data) -> void {
write(D.w + address & 0xffff, data);
}

auto WDC65816::readDirectX(uint address, uint offset) -> uint8 {
// The (direct,X) addressing mode has a bug in which the high byte is
// wrapped within the page if E = 1 and D&0xFF != 0.
if(EF && D.l) return read(((D.w + address) & 0xffff00) | ((D.w + address + offset) & 0xff));
else return readDirect(address + offset);
}

auto WDC65816::readDirectN(uint address) -> uint8 {
return read(D.w + address & 0xffff);
}
Expand Down
1 change: 1 addition & 0 deletions bsnes/processor/wdc65816/wdc65816.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ struct WDC65816 {
alwaysinline auto pushN(uint8 data) -> void;
alwaysinline auto readDirect(uint address) -> uint8;
alwaysinline auto writeDirect(uint address, uint8 data) -> void;
alwaysinline auto readDirectX(uint address, uint offset) -> uint8;
alwaysinline auto readDirectN(uint address) -> uint8;
alwaysinline auto readBank(uint address) -> uint8;
alwaysinline auto writeBank(uint address, uint8 data) -> void;
Expand Down

0 comments on commit ccbe394

Please sign in to comment.