Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation of SA-1 BW-RAM protection #312

Merged
merged 1 commit into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading