Chr Bank problems

Discuss emulation of the Nintendo Entertainment System and Famicom.

Moderator: Moderators

Post Reply
User avatar
Anes
Posts: 702
Joined: Tue Dec 21, 2004 8:35 pm
Location: Mendoza, Argentina

Chr Bank problems

Post by Anes »

Hi all, i left emu deving but im taking it again.
I have problems with chr rom, i don't know why. I think my cpu emulation is ok (i checked against nestest.log).
Anyway here i go:

My chr c code emulation is like this:

Code: Select all

/* PPU */
PPUMEMMAP g_PpuMemMap;
PPUDATAMEMMAP g_PpuData;
pu8 g_ChrMem;

void MapPpuMem(int from, int to, pReadMemory pfReadMemory, pWriteMemory pfWriteMemory)
{
	int f = from >> 10, t = to >> 10;

	for (int i = f; i <= t; i++)
	{
		g_PpuMemMap.pReadPpuMemory[i] = pfReadMemory;
		g_PpuMemMap.pWritePpuMemory[i] = pfWriteMemory;
	}
}

/* SWAP MEMORY */
void SwapPpuMemory(int where, int to, int bank, int bank_size)
{
	pu8 chr_data = g_ChrMem + bank * bank_size;

	for (int i = where >> 10; i <= to >> 10; i++)
	{
		g_PpuData.pPpuDataMemory[i] = chr_data;
		chr_data += 0x0400;
	}
}

/* MEMORY REDIRECTION*/
u8 ReadPpuMemData(u16 addr)
{
	int index = addr >> 10;
	u8 data = g_PpuData.pPpuDataMemory[index][addr & 0x03FF];
	return data;
}

void WritePpuMemData(u16 addr, u8 data)
{
	g_PpuData.pPpuDataMemory[addr >> 10][addr & 0x03FF] = data;
}

u8 ReadPpuMem(u16 addr)
{
	u8 data = g_PpuMemMap.pReadPpuMemory[addr >> 10](addr);
	return data;
}

void WritePpuMem(u16 addr, u8 data)
{
	g_PpuMemMap.pWritePpuMemory[addr >> 10](addr, data);
}

void SetMemoryChrMem(pu8 chr_mem)
{
	g_ChrMem = chr_mem;
}
and the .h file is like:

Code: Select all

typedef struct tagPPUMEMMAP
{
	pReadMemory pReadPpuMemory[16];
	pWriteMemory pWritePpuMemory[16];
} PPUMEMMAP, *PPPUMEMMAP;

typedef struct tagPPUDATAMEMMAP
{
	pu8 pPpuDataMemory[16];
} PPUDATAMEMMAP, *PPPUDATAMEMMAP;

void MapPpuMem(int from, int to, pReadMemory pfReadMemory, pWriteMemory pfWriteMemory);
void SwapPpuMemory(int where, int to, int bank, int bank_size);
ReadMemory ReadPpuMemData;
WriteMemory WritePpuMemData;
ReadMemory ReadPpuMem;
WriteMemory WritePpuMem;
void SetMemoryChrMem(pu8 chr_mem);
Where the mem pointers are defined as:

Code: Select all

typedef unsigned __int8 u8;
typedef u8 * pu8;
typedef unsigned __int16 u16;
typedef u16 * pu16;

typedef void WriteMemory (u16,u8);
typedef  WriteMemory * pWriteMemory;
typedef u8 ReadMemory(u16);
typedef  ReadMemory * pReadMemory;
pu8 is a pointer to char.
Of course i call SetMemoryChrMem() with the appropiate pointer got it from the .nes rom.
I call SwapPpuMemory(0x0000, 0x1FFF, 0, 0x2000); i don't care now about mem swaping since it will get the beggining of chr rom of the ines file. That should be ok (i think).

I just want someone tell me if im doing something wrong, i load nestest.nes and shows me garbage on the screen.
Maybe im doing something wrong in rendering, i doing it pixel by pixel.

Anyway if someone can help me.
ANes
User avatar
Anes
Posts: 702
Joined: Tue Dec 21, 2004 8:35 pm
Location: Mendoza, Argentina

Re: Chr Bank problems

Post by Anes »

I found the error. As i suspected wasn't in this code. Anyway i wanted to know if it was right.
It seems it's ok.

Thxs in advance.
ANes
AWJ
Posts: 433
Joined: Mon Nov 10, 2008 3:09 pm

Re: Chr Bank problems

Post by AWJ »

You need some way to distinguish between RAM and ROM. ROM shouldn't change when software writes to it through the PPU.
User avatar
Anes
Posts: 702
Joined: Tue Dec 21, 2004 8:35 pm
Location: Mendoza, Argentina

Re: Chr Bank problems

Post by Anes »

Yes, that's right i know. I only wrote it cos i started from scratch.

Im hanged now in nestest "(indirect,X)" test thats throws me error 0x59 -> "STA didn't store the data where it was supposed to".
I have to clear that error :x I think it should be easy. I did it before, why not now?
ANes
User avatar
Dwedit
Posts: 4924
Joined: Fri Nov 19, 2004 7:35 pm
Contact:

Re: Chr Bank problems

Post by Dwedit »

Addresses defined as being in the zero page will generally stay in the zero page, even if they overflow to 0x100 after adding X or Y.
Full 16-bit addresses that aren't defined as being in the zero page will carry over to 0x100, even if their value is under 0x100.
Here come the fortune cookies! Here come the fortune cookies! They're wearing paper hats!
User avatar
Anes
Posts: 702
Joined: Tue Dec 21, 2004 8:35 pm
Location: Mendoza, Argentina

Re: Chr Bank problems

Post by Anes »

I have just fixed my cpu core with the thing you have just said.
nestest.nes throws me all OK now, but im worried about Blargg's instruction test now.
I run the rom singles and the only one that gives me OK is implied... WTF
ANes
User avatar
Dwedit
Posts: 4924
Joined: Fri Nov 19, 2004 7:35 pm
Contact:

Re: Chr Bank problems

Post by Dwedit »

zpg,x and zpg,y and (zpg,x) will wrap back to the zero page when doing the addition.
abs,x and abs,y will not, since they are full 16-bit values.
(zpg),y has one addition step where it adds 1 to read the second value of the pointer, that will wrap back to 00 if the value was FF. The part that adds Y is full 16-bit math and does not wrap back anywhere.
Here come the fortune cookies! Here come the fortune cookies! They're wearing paper hats!
User avatar
Anes
Posts: 702
Joined: Tue Dec 21, 2004 8:35 pm
Location: Mendoza, Argentina

Re: Chr Bank problems

Post by Anes »

Im aware of that Dwedit. My cpu is ok, what i forgot was that Blargg's rom singles also test the unofficial opcodes. Thing im not emulating right now.
ANes
Post Reply