Skip to content

Commit

Permalink
implementation of SA-1 BW-RAM protection
Browse files Browse the repository at this point in the history
Manually cherry-picked ares commit ares-emulator/ares@70f3610.

Co-Authored-By: absindx <59403574+absindx@users.noreply.github.com>
  • Loading branch information
Morilli and absindx committed Mar 1, 2024
1 parent 4faca65 commit 196a41c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
11 changes: 11 additions & 0 deletions bsnes/sfc/coprocessor/sa1/bwram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ auto SA1::BWRAM::writeCPU(uint address, uint8 data) -> void {
address = sa1.mmio.sbm * 0x2000 + (address & 0x1fff);
}

//note: BW-RAM protection works only when both SWEN and CWEN are disabled.
if(!sa1.mmio.swen && !sa1.mmio.cwen && (address & 0x3ffff) < 0x100 << sa1.mmio.bwp) return;
return write(address, data);
}

Expand Down Expand Up @@ -71,6 +73,15 @@ auto SA1::BWRAM::readLinear(uint address, uint8 data) -> uint8 {
}

auto SA1::BWRAM::writeLinear(uint address, uint8 data) -> void {
//note: BW-RAM protection works only when both SWEN and CWEN are disabled.
//this is required for Kirby's Dream Land 3 to work:
//* BWPA = 02 (protect 400000-4003ff)
//* SWEN = 80 (writes enabled)
//* CWEN = 00 (writes disabled)
//KDL3 proceeds to write to 4001ax and 40032x which must succeed.
//note: BWPA also affects SA-1 protection
if(!sa1.mmio.swen && !sa1.mmio.cwen && (address & 0x3ffff) < 0x100 << sa1.mmio.bwp) return;

return write(address, data);
}

Expand Down
10 changes: 9 additions & 1 deletion bsnes/sfc/coprocessor/sa1/io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,16 @@ auto SA1::writeIOCPU(uint address, uint8 data) -> void {
//(CCNT) SA-1 control
case 0x2200: {
if(mmio.sa1_resb && !(data & 0x20)) {
//reset SA-1 CPU (PC bank set to 0x00)
//reset SA-1 CPU (PC bank and data bank set to 0x00, clear STP status)
r.pc.d = mmio.crv;
r.b = 0x00;
r.stp = false;
//todo: probably needs a SA-1 CPU reset
//reset r.s, r.e, r.wai ...

//reset io status
//todo: reset timing is unknown, CIWP is set to 0 at reset
mmio.ciwp = 0x00;
}

mmio.sa1_irq = (data & 0x80);
Expand Down
4 changes: 2 additions & 2 deletions bsnes/sfc/coprocessor/sa1/memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ auto SA1::read(uint address) -> uint8 {
}

if((address & 0x40e000) == 0x006000 //00-3f,80-bf:6000-7fff
|| (address & 0xf00000) == 0x400000 //40-4f:0000-ffff
|| (address & 0xe00000) == 0x400000 //40-5f:0000-ffff
|| (address & 0xf00000) == 0x600000 //60-6f:0000-ffff
) {
step();
Expand Down Expand Up @@ -81,7 +81,7 @@ auto SA1::write(uint address, uint8 data) -> void {
}

if((address & 0x40e000) == 0x006000 //00-3f,80-bf:6000-7fff
|| (address & 0xf00000) == 0x400000 //40-4f:0000-ffff
|| (address & 0xe00000) == 0x400000 //40-5f:0000-ffff
|| (address & 0xf00000) == 0x600000 //60-6f:0000-ffff
) {
step();
Expand Down

0 comments on commit 196a41c

Please sign in to comment.