Skip to content

Commit

Permalink
Bus: IO: begin to convert the PPU I/O registers to the new system
Browse files Browse the repository at this point in the history
  • Loading branch information
fleroviux committed Mar 24, 2024
1 parent d3f9b6c commit 48b5957
Showing 1 changed file with 198 additions and 111 deletions.
309 changes: 198 additions & 111 deletions src/nba/src/bus/io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
namespace nba::core {

static constexpr bool AddressUsesNewIO(u32 address) {
return false;
return address < 0x04000020u;
}

static constexpr int GetAccessSize(u32 mask) {
Expand Down Expand Up @@ -102,7 +102,35 @@ template<u32 mask> u32 Bus::Hardware::ReadWord(u32 address) {
Log<Warn>("IO: unhandled {}-bit read from 0x{:08X}", access_size, access_address);
}; // @todo: does this *really* need to be in a lambda function?

auto& ppu_io = ppu.mmio;

switch(REG(address)) {
case REG(0x04000000u): {
if constexpr(mask & 0x000000FFu) value |= ppu_io.dispcnt.Read(0);
if constexpr(mask & 0x0000FF00u) value |= ppu_io.dispcnt.Read(1) << 8;
if constexpr(mask & 0x00FF0000u) value |= ppu_io.greenswap << 16;
return value;
}
case REG(0x04000004u): {
if constexpr(mask & 0x000000FFu) value |= ppu_io.dispstat.Read(0);
if constexpr(mask & 0x0000FF00u) value |= ppu_io.dispstat.Read(1) << 8;
if constexpr(mask & 0x00FF0000u) value |= ppu_io.vcount << 16;
return value;
}
case REG(0x04000008u): {
if constexpr(mask & 0x000000FFu) value |= ppu_io.bgcnt[0].Read(0);
if constexpr(mask & 0x0000FF00u) value |= ppu_io.bgcnt[0].Read(1) << 8;
if constexpr(mask & 0x00FF0000u) value |= ppu_io.bgcnt[1].Read(0) << 16;
if constexpr(mask & 0xFF000000u) value |= ppu_io.bgcnt[1].Read(1) << 24;
return value;
}
case REG(0x0400000Cu): {
if constexpr(mask & 0x000000FFu) value |= ppu_io.bgcnt[2].Read(0);
if constexpr(mask & 0x0000FF00u) value |= ppu_io.bgcnt[2].Read(1) << 8;
if constexpr(mask & 0x00FF0000u) value |= ppu_io.bgcnt[3].Read(0) << 16;
if constexpr(mask & 0xFF000000u) value |= ppu_io.bgcnt[3].Read(1) << 24;
return value;
}
default: {
Unhandled();
}
Expand All @@ -120,7 +148,66 @@ template<u32 mask> void Bus::Hardware::WriteWord(u32 address, u32 value) {
Log<Warn>("IO: unhandled {}-bit write to 0x{:08X} = 0x{:08X}", access_size, access_address, access_value);
}; // @todo: does this *really* need to be in a lambda function?

auto& ppu_io = ppu.mmio;

switch(REG(address)) {
case REG(0x04000000u): {
if constexpr(mask & 0x000000FFu) ppu_io.dispcnt.Write(0, (u8)(value >> 0));
if constexpr(mask & 0x0000FF00u) ppu_io.dispcnt.Write(1, (u8)(value >> 8));
if constexpr(mask & 0x00FF0000u) ppu_io.greenswap = (value >> 16) & 1;
break;
}
case REG(0x04000004u): {
if constexpr(mask & 0x000000FFu) ppu_io.dispstat.Write(0, (u8)(value >> 0));
if constexpr(mask & 0x0000FF00u) ppu_io.dispstat.Write(1, (u8)(value >> 8));
break;
}
case REG(0x04000008u): {
if constexpr(mask & 0x000000FFu) ppu_io.bgcnt[0].Write(0, (u8)(value >> 0));
if constexpr(mask & 0x0000FF00u) ppu_io.bgcnt[0].Write(1, (u8)(value >> 8));
if constexpr(mask & 0x00FF0000u) ppu_io.bgcnt[1].Write(0, (u8)(value >> 16));
if constexpr(mask & 0xFF000000u) ppu_io.bgcnt[1].Write(1, (u8)(value >> 24));
break;
}
case REG(0x0400000Cu): {
if constexpr(mask & 0x000000FFu) ppu_io.bgcnt[2].Write(0, (u8)(value >> 0));
if constexpr(mask & 0x0000FF00u) ppu_io.bgcnt[2].Write(1, (u8)(value >> 8));
if constexpr(mask & 0x00FF0000u) ppu_io.bgcnt[3].Write(0, (u8)(value >> 16));
if constexpr(mask & 0xFF000000u) ppu_io.bgcnt[3].Write(1, (u8)(value >> 24));
break;
}
case REG(0x04000010u): {
constexpr u32 lsw_mask = (mask >> 0) & 0x1FFu;
constexpr u32 msw_mask = (mask >> 16) & 0x1FFu;

if constexpr(mask & 0x0000FFFFu) ppu_io.bghofs[0] = ((value >> 0) & lsw_mask) | (ppu_io.bghofs[0] & ~lsw_mask);
if constexpr(mask & 0xFFFF0000u) ppu_io.bgvofs[0] = ((value >> 16) & msw_mask) | (ppu_io.bgvofs[0] & ~msw_mask);
break;
}
case REG(0x04000014u): {
constexpr u32 lsw_mask = (mask >> 0) & 0x1FFu;
constexpr u32 msw_mask = (mask >> 16) & 0x1FFu;

if constexpr(mask & 0x0000FFFFu) ppu_io.bghofs[1] = ((value >> 0) & lsw_mask) | (ppu_io.bghofs[1] & ~lsw_mask);
if constexpr(mask & 0xFFFF0000u) ppu_io.bgvofs[1] = ((value >> 16) & msw_mask) | (ppu_io.bgvofs[1] & ~msw_mask);
break;
}
case REG(0x04000018u): {
constexpr u32 lsw_mask = (mask >> 0) & 0x1FFu;
constexpr u32 msw_mask = (mask >> 16) & 0x1FFu;

if constexpr(mask & 0x0000FFFFu) ppu_io.bghofs[2] = ((value >> 0) & lsw_mask) | (ppu_io.bghofs[2] & ~lsw_mask);
if constexpr(mask & 0xFFFF0000u) ppu_io.bgvofs[2] = ((value >> 16) & msw_mask) | (ppu_io.bgvofs[2] & ~msw_mask);
break;
}
case REG(0x0400001Cu): {
constexpr u32 lsw_mask = (mask >> 0) & 0x1FFu;
constexpr u32 msw_mask = (mask >> 16) & 0x1FFu;

if constexpr(mask & 0x0000FFFFu) ppu_io.bghofs[3] = ((value >> 0) & lsw_mask) | (ppu_io.bghofs[3] & ~lsw_mask);
if constexpr(mask & 0xFFFF0000u) ppu_io.bgvofs[3] = ((value >> 16) & msw_mask) | (ppu_io.bgvofs[3] & ~msw_mask);
break;
}
default: {
Unhandled();
}
Expand All @@ -139,22 +226,22 @@ auto Bus::Hardware::ReadByte(u32 address) -> u8 {

switch(address) {
// PPU
case DISPCNT+0: return ppu_io.dispcnt.Read(0);
case DISPCNT+1: return ppu_io.dispcnt.Read(1);
case GREENSWAP+0: return ppu_io.greenswap;
case GREENSWAP+1: return 0;
case DISPSTAT+0: return ppu_io.dispstat.Read(0);
case DISPSTAT+1: return ppu_io.dispstat.Read(1);
case VCOUNT+0: return ppu_io.vcount & 0xFF;
case VCOUNT+1: return 0;
case BG0CNT+0: return ppu_io.bgcnt[0].Read(0);
case BG0CNT+1: return ppu_io.bgcnt[0].Read(1);
case BG1CNT+0: return ppu_io.bgcnt[1].Read(0);
case BG1CNT+1: return ppu_io.bgcnt[1].Read(1);
case BG2CNT+0: return ppu_io.bgcnt[2].Read(0);
case BG2CNT+1: return ppu_io.bgcnt[2].Read(1);
case BG3CNT+0: return ppu_io.bgcnt[3].Read(0);
case BG3CNT+1: return ppu_io.bgcnt[3].Read(1);
// case DISPCNT+0: return ppu_io.dispcnt.Read(0);
// case DISPCNT+1: return ppu_io.dispcnt.Read(1);
// case GREENSWAP+0: return ppu_io.greenswap;
// case GREENSWAP+1: return 0;
// case DISPSTAT+0: return ppu_io.dispstat.Read(0);
// case DISPSTAT+1: return ppu_io.dispstat.Read(1);
// case VCOUNT+0: return ppu_io.vcount & 0xFF;
// case VCOUNT+1: return 0;
// case BG0CNT+0: return ppu_io.bgcnt[0].Read(0);
// case BG0CNT+1: return ppu_io.bgcnt[0].Read(1);
// case BG1CNT+0: return ppu_io.bgcnt[1].Read(0);
// case BG1CNT+1: return ppu_io.bgcnt[1].Read(1);
// case BG2CNT+0: return ppu_io.bgcnt[2].Read(0);
// case BG2CNT+1: return ppu_io.bgcnt[2].Read(1);
// case BG3CNT+0: return ppu_io.bgcnt[3].Read(0);
// case BG3CNT+1: return ppu_io.bgcnt[3].Read(1);
case WININ+0: return ppu_io.winin.Read(0);
case WININ+1: return ppu_io.winin.Read(1);
case WINOUT+0: return ppu_io.winout.Read(0);
Expand Down Expand Up @@ -375,100 +462,100 @@ void Bus::Hardware::WriteByte(u32 address, u8 value) {

switch(address) {
// PPU
case DISPCNT+0: ppu_io.dispcnt.Write(0, value); break;
case DISPCNT+1: ppu_io.dispcnt.Write(1, value); break;
case GREENSWAP+0: ppu_io.greenswap = value & 1; break;
case GREENSWAP+1: break;
case DISPSTAT+0: ppu_io.dispstat.Write(0, value); break;
case DISPSTAT+1: ppu_io.dispstat.Write(1, value); break;
case BG0CNT+0: ppu_io.bgcnt[0].Write(0, value); break;
case BG0CNT+1: ppu_io.bgcnt[0].Write(1, value); break;
case BG1CNT+0: ppu_io.bgcnt[1].Write(0, value); break;
case BG1CNT+1: ppu_io.bgcnt[1].Write(1, value); break;
case BG2CNT+0: ppu_io.bgcnt[2].Write(0, value); break;
case BG2CNT+1: ppu_io.bgcnt[2].Write(1, value); break;
case BG3CNT+0: ppu_io.bgcnt[3].Write(0, value); break;
case BG3CNT+1: ppu_io.bgcnt[3].Write(1, value); break;
case BG0HOFS+0: {
ppu_io.bghofs[0] &= 0xFF00;
ppu_io.bghofs[0] |= value;
break;
}
case BG0HOFS+1: {
ppu_io.bghofs[0] &= 0x00FF;
ppu_io.bghofs[0] |= (value & 1) << 8;
break;
}
case BG0VOFS+0: {
ppu_io.bgvofs[0] &= 0xFF00;
ppu_io.bgvofs[0] |= value;
break;
}
case BG0VOFS+1: {
ppu_io.bgvofs[0] &= 0x00FF;
ppu_io.bgvofs[0] |= (value & 1) << 8;
break;
}
case BG1HOFS+0: {
ppu_io.bghofs[1] &= 0xFF00;
ppu_io.bghofs[1] |= value;
break;
}
case BG1HOFS+1: {
ppu_io.bghofs[1] &= 0x00FF;
ppu_io.bghofs[1] |= (value & 1) << 8;
break;
}
case BG1VOFS+0: {
ppu_io.bgvofs[1] &= 0xFF00;
ppu_io.bgvofs[1] |= value;
break;
}
case BG1VOFS+1: {
ppu_io.bgvofs[1] &= 0x00FF;
ppu_io.bgvofs[1] |= (value & 1) << 8;
break;
}
case BG2HOFS+0: {
ppu_io.bghofs[2] &= 0xFF00;
ppu_io.bghofs[2] |= value;
break;
}
case BG2HOFS+1: {
ppu_io.bghofs[2] &= 0x00FF;
ppu_io.bghofs[2] |= (value & 1) << 8;
break;
}
case BG2VOFS+0: {
ppu_io.bgvofs[2] &= 0xFF00;
ppu_io.bgvofs[2] |= value;
break;
}
case BG2VOFS+1: {
ppu_io.bgvofs[2] &= 0x00FF;
ppu_io.bgvofs[2] |= (value & 1) << 8;
break;
}
case BG3HOFS+0: {
ppu_io.bghofs[3] &= 0xFF00;
ppu_io.bghofs[3] |= value;
break;
}
case BG3HOFS+1: {
ppu_io.bghofs[3] &= 0x00FF;
ppu_io.bghofs[3] |= (value & 1) << 8;
break;
}
case BG3VOFS+0: {
ppu_io.bgvofs[3] &= 0xFF00;
ppu_io.bgvofs[3] |= value;
break;
}
case BG3VOFS+1: {
ppu_io.bgvofs[3] &= 0x00FF;
ppu_io.bgvofs[3] |= (value & 1) << 8;
break;
}
// case DISPCNT+0: ppu_io.dispcnt.Write(0, value); break;
// case DISPCNT+1: ppu_io.dispcnt.Write(1, value); break;
// case GREENSWAP+0: ppu_io.greenswap = value & 1; break;
// case GREENSWAP+1: break;
// case DISPSTAT+0: ppu_io.dispstat.Write(0, value); break;
// case DISPSTAT+1: ppu_io.dispstat.Write(1, value); break;
// case BG0CNT+0: ppu_io.bgcnt[0].Write(0, value); break;
// case BG0CNT+1: ppu_io.bgcnt[0].Write(1, value); break;
// case BG1CNT+0: ppu_io.bgcnt[1].Write(0, value); break;
// case BG1CNT+1: ppu_io.bgcnt[1].Write(1, value); break;
// case BG2CNT+0: ppu_io.bgcnt[2].Write(0, value); break;
// case BG2CNT+1: ppu_io.bgcnt[2].Write(1, value); break;
// case BG3CNT+0: ppu_io.bgcnt[3].Write(0, value); break;
// case BG3CNT+1: ppu_io.bgcnt[3].Write(1, value); break;
// case BG0HOFS+0: {
// ppu_io.bghofs[0] &= 0xFF00;
// ppu_io.bghofs[0] |= value;
// break;
// }
// case BG0HOFS+1: {
// ppu_io.bghofs[0] &= 0x00FF;
// ppu_io.bghofs[0] |= (value & 1) << 8;
// break;
// }
// case BG0VOFS+0: {
// ppu_io.bgvofs[0] &= 0xFF00;
// ppu_io.bgvofs[0] |= value;
// break;
// }
// case BG0VOFS+1: {
// ppu_io.bgvofs[0] &= 0x00FF;
// ppu_io.bgvofs[0] |= (value & 1) << 8;
// break;
// }
// case BG1HOFS+0: {
// ppu_io.bghofs[1] &= 0xFF00;
// ppu_io.bghofs[1] |= value;
// break;
// }
// case BG1HOFS+1: {
// ppu_io.bghofs[1] &= 0x00FF;
// ppu_io.bghofs[1] |= (value & 1) << 8;
// break;
// }
// case BG1VOFS+0: {
// ppu_io.bgvofs[1] &= 0xFF00;
// ppu_io.bgvofs[1] |= value;
// break;
// }
// case BG1VOFS+1: {
// ppu_io.bgvofs[1] &= 0x00FF;
// ppu_io.bgvofs[1] |= (value & 1) << 8;
// break;
// }
// case BG2HOFS+0: {
// ppu_io.bghofs[2] &= 0xFF00;
// ppu_io.bghofs[2] |= value;
// break;
// }
// case BG2HOFS+1: {
// ppu_io.bghofs[2] &= 0x00FF;
// ppu_io.bghofs[2] |= (value & 1) << 8;
// break;
// }
// case BG2VOFS+0: {
// ppu_io.bgvofs[2] &= 0xFF00;
// ppu_io.bgvofs[2] |= value;
// break;
// }
// case BG2VOFS+1: {
// ppu_io.bgvofs[2] &= 0x00FF;
// ppu_io.bgvofs[2] |= (value & 1) << 8;
// break;
// }
// case BG3HOFS+0: {
// ppu_io.bghofs[3] &= 0xFF00;
// ppu_io.bghofs[3] |= value;
// break;
// }
// case BG3HOFS+1: {
// ppu_io.bghofs[3] &= 0x00FF;
// ppu_io.bghofs[3] |= (value & 1) << 8;
// break;
// }
// case BG3VOFS+0: {
// ppu_io.bgvofs[3] &= 0xFF00;
// ppu_io.bgvofs[3] |= value;
// break;
// }
// case BG3VOFS+1: {
// ppu_io.bgvofs[3] &= 0x00FF;
// ppu_io.bgvofs[3] |= (value & 1) << 8;
// break;
// }
case BG2PA: ppu_io.bgpa[0] = (ppu_io.bgpa[0] & 0xFF00) | (value << 0); break;
case BG2PA+1: ppu_io.bgpa[0] = (ppu_io.bgpa[0] & 0x00FF) | (value << 8); break;
case BG2PB: ppu_io.bgpb[0] = (ppu_io.bgpb[0] & 0xFF00) | (value << 0); break;
Expand Down

0 comments on commit 48b5957

Please sign in to comment.