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

Fix PRG bank shown in debugger #757

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
46 changes: 37 additions & 9 deletions src/cart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,25 @@ uint32 genieaddr[3];

CartInfo *currCartInfo;

static INLINE void setpageptr(int s, uint32 A, uint8 *p, int ram) {
// Reverse mapping of the address space back to bank numbers.
// Split into 2 KiB pages, like `Page`.
int PRGBankMapping[32];

static INLINE void setpageptr(int s, uint32 A, uint8 *p, int ram, int bank) {
uint32 AB = A >> 11;
int x;

if (p)
for (x = (s >> 1) - 1; x >= 0; x--) {
PRGIsRAM[AB + x] = ram;
Page[AB + x] = p - A;
PRGBankMapping[AB + x] = bank;
}
else
for (x = (s >> 1) - 1; x >= 0; x--) {
PRGIsRAM[AB + x] = 0;
Page[AB + x] = 0;
PRGBankMapping[AB + x] = bank;
}
}

Expand All @@ -104,6 +110,7 @@ void ResetCartMapping(void) {
Page[x] = nothing - x * 2048;
PRGptr[x] = CHRptr[x] = 0;
PRGsize[x] = CHRsize[x] = 0;
PRGBankMapping[x] = -1;
}
for (x = 0; x < 8; x++) {
MMC5SPRVPage[x] = MMC5BGVPage[x] = VPageR[x] = nothing - 0x400 * x;
Expand Down Expand Up @@ -140,6 +147,12 @@ void SetupCartCHRMapping(int chip, uint8 *p, uint32 size, int ram) {
CHRram[chip] = ram;
}

// Get the mapper-specific bank number for the given CPU address.
// Returns -1 when the page has not been mapped.
int GetPRGBank(uint16 address) {
return PRGBankMapping[address >> 11];
}

DECLFR(CartBR) {
return Page[A >> 11][A];
}
Expand All @@ -157,9 +170,20 @@ DECLFR(CartBROB) {
return Page[A >> 11][A];
}

// Get the cumulative size of PRG chips.
// I.e. the ROM file offset (without iNES header).
static uint32 PRGOffset(int chip) {
uint32 bank = 0;
for (int i = 0; i < chip; i ++) {
bank += PRGsize[i];
}
return bank;
}

void setprg2r(int r, uint32 A, uint32 V) {
V &= PRGmask2[r];
setpageptr(2, A, PRGptr[r] ? (&PRGptr[r][V << 11]) : 0, PRGram[r]);
int bank = PRGptr[r] ? (PRGOffset(r) / (1 << 11) + V) : -1;
setpageptr(2, A, PRGptr[r] ? (&PRGptr[r][V << 11]) : 0, PRGram[r], bank);
}

void setprg2(uint32 A, uint32 V) {
Expand All @@ -168,22 +192,24 @@ void setprg2(uint32 A, uint32 V) {

void setprg4r(int r, uint32 A, uint32 V) {
V &= PRGmask4[r];
setpageptr(4, A, PRGptr[r] ? (&PRGptr[r][V << 12]) : 0, PRGram[r]);
int bank = PRGptr[r] ? (PRGOffset(r) / (1 << 12) + V) : -1;
setpageptr(4, A, PRGptr[r] ? (&PRGptr[r][V << 12]) : 0, PRGram[r], bank);
}

void setprg4(uint32 A, uint32 V) {
setprg4r(0, A, V);
}

void setprg8r(int r, uint32 A, uint32 V) {
int bank = PRGptr[r] ? (PRGOffset(r) / (1 << 13) + (V & PRGmask8[r])) : -1;
if (PRGsize[r] >= 8192) {
V &= PRGmask8[r];
setpageptr(8, A, PRGptr[r] ? (&PRGptr[r][V << 13]) : 0, PRGram[r]);
setpageptr(8, A, PRGptr[r] ? (&PRGptr[r][V << 13]) : 0, PRGram[r], bank);
} else {
uint32 VA = V << 2;
int x;
for (x = 0; x < 4; x++)
setpageptr(2, A + (x << 11), PRGptr[r] ? (&PRGptr[r][((VA + x) & PRGmask2[r]) << 11]) : 0, PRGram[r]);
setpageptr(2, A + (x << 11), PRGptr[r] ? (&PRGptr[r][((VA + x) & PRGmask2[r]) << 11]) : 0, PRGram[r], bank);
}
}

Expand All @@ -192,15 +218,16 @@ void setprg8(uint32 A, uint32 V) {
}

void setprg16r(int r, uint32 A, uint32 V) {
int bank = PRGptr[r] ? (PRGOffset(r) / (1 << 14) + (V & PRGmask16[r])) : -1;
if (PRGsize[r] >= 16384) {
V &= PRGmask16[r];
setpageptr(16, A, PRGptr[r] ? (&PRGptr[r][V << 14]) : 0, PRGram[r]);
setpageptr(16, A, PRGptr[r] ? (&PRGptr[r][V << 14]) : 0, PRGram[r], bank);
} else {
uint32 VA = V << 3;
int x;

for (x = 0; x < 8; x++)
setpageptr(2, A + (x << 11), PRGptr[r] ? (&PRGptr[r][((VA + x) & PRGmask2[r]) << 11]) : 0, PRGram[r]);
setpageptr(2, A + (x << 11), PRGptr[r] ? (&PRGptr[r][((VA + x) & PRGmask2[r]) << 11]) : 0, PRGram[r], bank);
}
}

Expand All @@ -209,15 +236,16 @@ void setprg16(uint32 A, uint32 V) {
}

void setprg32r(int r, uint32 A, uint32 V) {
int bank = PRGptr[r] ? (PRGOffset(r) / (1 << 15) + (V & PRGmask32[r])) : -1;
if (PRGsize[r] >= 32768) {
V &= PRGmask32[r];
setpageptr(32, A, PRGptr[r] ? (&PRGptr[r][V << 15]) : 0, PRGram[r]);
setpageptr(32, A, PRGptr[r] ? (&PRGptr[r][V << 15]) : 0, PRGram[r], bank);
} else {
uint32 VA = V << 4;
int x;

for (x = 0; x < 16; x++)
setpageptr(2, A + (x << 11), PRGptr[r] ? (&PRGptr[r][((VA + x) & PRGmask2[r]) << 11]) : 0, PRGram[r]);
setpageptr(2, A + (x << 11), PRGptr[r] ? (&PRGptr[r][((VA + x) & PRGmask2[r]) << 11]) : 0, PRGram[r], bank);
}
}

Expand Down
1 change: 1 addition & 0 deletions src/cart.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ void ResetCartMapping(void);
void SetupCartPRGMapping(int chip, uint8 *p, uint32 size, int ram);
void SetupCartCHRMapping(int chip, uint8 *p, uint32 size, int ram);
void SetupCartMirroring(int m, int hard, uint8 *extra);
int GetPRGBank(uint16 address);

DECLFR(CartBROB);
DECLFR(CartBR);
Expand Down
12 changes: 5 additions & 7 deletions src/debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,6 @@ int GetPRGAddress(int A){

/**
* Returns the bank for a given offset.
* Technically speaking this function does not calculate the actual bank
* where the offset resides but the 0x4000 bytes large chunk of the ROM of the offset.
*
* @param offs The offset
* @return The bank of that offset or -1 if the offset is not part of the ROM.
Expand All @@ -298,13 +296,13 @@ int getBank(int offs)
{
//NSF data is easy to overflow the return on.
//Anything over FFFFF will kill it.
if (GameInfo && GameInfo->type == GIT_NSF) {
int addr = GetNesFileAddress(offs);

//GetNesFileAddress doesn't work well with Unif files
int addr = GetNesFileAddress(offs)-NES_HEADER_SIZE;
return addr != -1 ? (addr - NES_HEADER_SIZE) / 0x1000 : -1;
}

if (GameInfo && GameInfo->type==GIT_NSF)
return addr != -1 ? addr / 0x1000 : -1;
return addr != -1 ? addr / (1<<debuggerPageSize) : -1; //formerly, dividing by 0x4000
return ((offs >= 0x6000) && (offs <= 0xFFFF)) ? GetPRGBank(offs) : -1;
}

int GetNesFileAddress(int A){
Expand Down
7 changes: 5 additions & 2 deletions src/drivers/win/debugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -388,8 +388,11 @@ INT_PTR CALLBACK AddbpCallB(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lPara
sprintf(str, "%04X", (unsigned int)lParam);
SetDlgItemText(hwndDlg,IDC_ADDBP_ADDR_START,str);
// also set the condition to only break at this Bank
sprintf(str, "K==#%02X", getBank(lParam));
SetDlgItemText(hwndDlg, IDC_ADDBP_CONDITION, str);
auto bank = getBank(lParam);
if (bank > -1) {
sprintf(str, "K==#%02X", bank);
SetDlgItemText(hwndDlg, IDC_ADDBP_CONDITION, str);
}
}
}
break;
Expand Down