Any clue on PPU emulation?

Discuss emulation of the Nintendo Entertainment System and Famicom.

Moderator: Moderators

User avatar
Quietust
Posts: 1920
Joined: Sun Sep 19, 2004 10:59 pm
Contact:

Re: Any on clue on PPU emulation?

Post by Quietust »

HastatusXXI wrote:However, I want to know if this is normal (PPU manages memory in a big-endian fashion, AFAIK), since the wiki states that the colour of a row of 8 pixels in a 8x8 tile is determined by using the corresponding byte of pattern table 0 as 0 bit values and the corresponding byte of pattern table 1 as 1 bit values and this is how I implemented my program.
According to the PPU pattern tables article on the wiki, the most significant bit of tile data (i.e. 0x80) represents the leftmost pixel of said tile, and the least significant bit (0x01) represents the rightmost pixel.
HastatusXXI wrote:Is YYCHR flipping the sprites prior to drawing them?
No, it is not, since a given tile can be interpreted as either sprite or background, and background tiles are never flipped.
Quietust, QMT Productions
P.S. If you don't get this note, let me know and I'll write you another.
HastatusXXI
Posts: 40
Joined: Sat Aug 25, 2018 7:21 am

Re: Any on clue on PPU emulation?

Post by HastatusXXI »

Okay, solved. My algorithm was flipping the pixels prior to drawing them. Thank you, Quietust.
HastatusXXI
Posts: 40
Joined: Sat Aug 25, 2018 7:21 am

Re: Any clue on PPU emulation?

Post by HastatusXXI »

Hello again, I'm making some progress here with the PPU. I'm using Nintendulator PPU and CPU memory maps dumps to comprehend image generation. I understand backgrounds now, but I need OAM dumps to practise everything concerning sprites. Is there any possibility to do this in Nintendulator? (I only see the option of dumping CPU and PPU, but nothing about OAM/SPR).
User avatar
Quietust
Posts: 1920
Joined: Sun Sep 19, 2004 10:59 pm
Contact:

Re: Any clue on PPU emulation?

Post by Quietust »

HastatusXXI wrote:Hello again, I'm making some progress here with the PPU. I'm using Nintendulator PPU and CPU memory maps dumps to comprehend image generation. I understand backgrounds now, but I need OAM dumps to practise everything concerning sprites. Is there any possibility to do this in Nintendulator? (I only see the option of dumping CPU and PPU, but nothing about OAM/SPR).
The "Dump PPU" button dumps everything related to the PPU: pattern tables, nametables, sprites, and palettes (in that order).
Quietust, QMT Productions
P.S. If you don't get this note, let me know and I'll write you another.
HastatusXXI
Posts: 40
Joined: Sat Aug 25, 2018 7:21 am

Re: Any clue on PPU emulation?

Post by HastatusXXI »

Quietust wrote:
HastatusXXI wrote:Hello again, I'm making some progress here with the PPU. I'm using Nintendulator PPU and CPU memory maps dumps to comprehend image generation. I understand backgrounds now, but I need OAM dumps to practise everything concerning sprites. Is there any possibility to do this in Nintendulator? (I only see the option of dumping CPU and PPU, but nothing about OAM/SPR).
The "Dump PPU" button dumps everything related to the PPU: pattern tables, nametables, sprites, and palettes (in that order).
Okay, I see. Thank you, Quietust.
HastatusXXI
Posts: 40
Joined: Sat Aug 25, 2018 7:21 am

Re: Any clue on PPU emulation?

Post by HastatusXXI »

I successfully rendered one frame of Donkey Kong (many things were hardcoded, such as the palettes). Now I'm trying to do it properly and, following the wiki article on PPU registers, I check PPUCTRL via the CPU dump at $2000 (I know that't not the way to do it, it's temporary). I found the value $A5, so the pattern table 0 is chosen, but pattern table 1 (I hardcoded this previously) should be chosen instead. Of course the rendered image is a mess.

What's the problem here?
User avatar
rainwarrior
Posts: 8734
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: Any clue on PPU emulation?

Post by rainwarrior »

Debugging Donkey Kong I see writes of $14 at the beginning of NMI and $94 when it finishes during gameplay frames. (The two writes disable then re-enable NMI so that it won't interrupt itself, that's a pattern a few Nintendo games use.) Where did you get $A5?
User avatar
koitsu
Posts: 4201
Joined: Sun Sep 19, 2004 9:28 pm
Location: A world gone mad

Re: Any clue on PPU emulation?

Post by koitsu »

I tried to use Mesen's Trace Logger feature to get me exactly what I needed (a log of writes to $2000), but it seems incredibly buggy/weird/busted in ways that are off-topic (I'll have to file a GitHub issue :-) ). So I stuck with a breakpoint on any writes to effective address $2000, with a conditional of value == $A5 -- nothing turned up from power-on onwards. I then changed the breakpoint to any write to effective address $2000. From power-on all the way into the attract mode (where the game plays by itself), these are the values you're going to see written to $2000 at varying points:

$10
$14
$90
$94

No other values were seen, at least not during the parts I tested.

$A5 sounds to me like either the wrong data being shown, ex. an opcode (that would be lda $zp), an internal value you might be using for PPU state, or maybe you reported to us the wrong value.
Sour
Posts: 891
Joined: Sun Feb 07, 2016 6:16 pm

Re: Any clue on PPU emulation?

Post by Sour »

koitsu wrote:I tried to use Mesen's Trace Logger feature to get me exactly what I needed (a log of writes to $2000), but it seems incredibly buggy/weird/busted in ways that are off-topic (I'll have to file a GitHub issue :-) )
Just tried on my end and it works as expected with the following condition: "address == $2000 && iswrite"
User avatar
koitsu
Posts: 4201
Joined: Sun Sep 19, 2004 9:28 pm
Location: A world gone mad

Re: Any clue on PPU emulation?

Post by koitsu »

Sour wrote:
koitsu wrote:I tried to use Mesen's Trace Logger feature to get me exactly what I needed (a log of writes to $2000), but it seems incredibly buggy/weird/busted in ways that are off-topic (I'll have to file a GitHub issue :-) )
Just tried on my end and it works as expected with the following condition: "address == $2000 && iswrite"
I'll file a GitHub Issues for the behaviours I see that are bizarre/weird. There's no issue with the conditionals, the problems pertain to other things.
HastatusXXI
Posts: 40
Joined: Sat Aug 25, 2018 7:21 am

Re: Any clue on PPU emulation?

Post by HastatusXXI »

I got the value $A5 from a random CPU dump of the title screen of Donkey Kong using Nintendulator. The value I'm giving to you comes from the dump at position $2000, not from watching a breakpoint. In fact, Nintendulator only shows $FF when I trace a breakpoint at $2000. I'm puzzled.
Last edited by HastatusXXI on Thu Sep 27, 2018 9:05 am, edited 1 time in total.
tepples
Posts: 22708
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: Any clue on PPU emulation?

Post by tepples »

If the last value written read or written on any PPU register ($2000-$2007 or their mirrors at $2008-$3FFF) is $A5, then reading a write-only PPU register (such as $2000) will return $A5. This behavior, which FCEUX calls "PPUGenLatch", is a result of bus capacitance within the PPU. Coincidentally, the last time we discussed it also involved Donkey Kong. It's possible that Nintendulator's memory viewer is emulating PPUGenLatch for the $2000-$3FFF space, but I haven't tested that.

As far as I'm aware, the only NES program that actually uses PPUGenLatch on purpose is my controller test ROM when it's trying to tell the difference between an NES and a Famicom based on $4016 open bus behavior. Existing games don't, and if future games do, it might just be to set the default language to Japanese or English or change how specialty controllers are read.
HastatusXXI
Posts: 40
Joined: Sat Aug 25, 2018 7:21 am

Re: Any clue on PPU emulation?

Post by HastatusXXI »

tepples wrote:If the last value written read or written on any PPU register ($2000-$2007 or their mirrors at $2008-$3FFF) is $A5, then reading a write-only PPU register (such as $2000) will return $A5. This behavior, which FCEUX calls "PPUGenLatch", is a result of bus capacitance within the PPU. Coincidentally, the last time we discussed it also involved Donkey Kong. It's possible that Nintendulator's memory viewer is emulating PPUGenLatch for the $2000-$3FFF space, but I haven't tested that.

As far as I'm aware, the only NES program that actually uses PPUGenLatch on purpose is my controller test ROM when it's trying to tell the difference between an NES and a Famicom based on $4016 open bus behavior. Existing games don't, and if future games do, it might just be to set the default language to Japanese or English or change how specialty controllers are read.
Does it mean I won't be able to get a PPU register value from a CPU dump from Nintendulator? There's no option to dump PPU registers and Quietust specified above that PPU dump contains pattern tables, nametables, OAM and palettes, but he didn't mention PPU registers at all, so I infered they would be available from the CPU memory map.

koitsu mentioned Mesen's Trace Logger. I'd rather use Nintendulator, since it's compatible with Wine. I'd try a Virtual Machine with Win 7 x64 if it's necessary (the one I have now runs Win Xp x32, but Mesen's emulator is not compatible with x86 arch).

Edit: Okay, I've seen Mesen is compatible with Linux.
Edit2: I can see the values koitsu gets for $2000 in the Trace Logger. However, all the PPU mapped registers stay $00, so I can't produce a dump to continue practising. How can I solve this? Is there any other way to get the value of the PPU registers?
Edit 3: Ok, I overlooked the paragraph on PPUGenLatch in the wiki article on PPU ports. So, the values in these registers start to decay about a frame after they've been written. Shouldn't it be possible to get a CPU dump just after the values have been written, either in Nintendulator or Mesen? Do they always dump the CPU memory map in a concrete instant of time?


SUMMARY: Is there any way I can dump the CPU memory map including the PPU registers values prior to decaying?
User avatar
rainwarrior
Posts: 8734
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: Any clue on PPU emulation?

Post by rainwarrior »

Is there any way I can dump the CPU memory map including the PPU registers values prior to decaying?
Most of the PPU state is visible in Mesen's debugger. There's a frame labelled "PPU Status" in there which has the state of $2000, $2001, the current address, and the current scanline/pixel.

In general debuggers will not return the state of write-only registers from a memory dump of CPU. There are different behaviours that may be implemented here (open bus, PPU bus storage, 00, FF, etc.) and this is not exactly consistent between emulators. Even with registers that can be read, a CPU memory view or dump may not show their contents, because reading from them may have side effects too. Again depends on the emulator, it's kind of a grey area what's appropriate to display for this kind of thing.
HastatusXXI
Posts: 40
Joined: Sat Aug 25, 2018 7:21 am

Re: Any clue on PPU emulation?

Post by HastatusXXI »

Hi, everyone.
I'm trying to resume this project, although at a slow pace...

I'm still trying to understand picture representation. I made a draft tool to represent a Donkey Kong frame from CPU and PPU dump files. Everything is drawn right expect from Mario and a flame, which look reversed on their half (see the attached image).

Any ideas on why this might be happening?

Image

Edit: since Peach is drawn properly I guess there's no trouble regarding my drawing algorithm, so ¿is there any bit for a particular sprite that sets it as reversed?
Edit2: I answer my own question: yes, bit 6 and 7 of byte 3 (attributes) in OAM are represent sprite flipping.

UPDATE: yes, that was the problem. Solved :)
Post Reply