MMC5 PRG RAM Bankswitching Confusion

This is an archive of a topic from NESdev BBS, taken in mid-October 2019 before a server upgrade.
View original topic
MMC5 PRG RAM Bankswitching Confusion
by on (#243117)
Hi peeps,

I'm current trying to get MMC5 implemented on my emu, I have Castlevania 3 working correctly (except the very top and bottom glitch on the intro while scrolling, but I think that's normally hidden by overscan?) and right now I'm trying to get Metal Slader Glory to work, so far it shows the intro, but when you press start it goes to nothing.

I think my issue is to do with my lack of understanding of how switching to RAM works since the information on the nesdev wiki is kinda vague on this.

My understanding is the cartridge has an internal 32k of RAM and the PRG Bank switching registers on 5114-5116 (with 5113 fixed to this RAM) can select to load from PRG RAM, which I presume points it at this internal RAM? But what about the program it selects at the same time? Does it just leave this RAM blank, or does the cartridge somehow know to copy the bank over to that are of the internal RAM?

Further to this, is the ram that can be used as a nametable/ordinary RAM separate to this internal 32kb?

Please correct me on any information I may be wrong on, none of the documentation seems overly clear on how all this works lol.

Thanks in advance.
Re: MMC5 PRG RAM Bankswitching Confusion
by on (#243118)
Metal Slader Glory has no extra PRG-RAM, and certainly not 32 KiB. In fact, from what I can tell, it does not access the $6000-$7FFF area at all. The only extra RAM it uses are the 1 KiB of additional MMC5 EXRAM at $5C00-$5FFF for music state data, which is enabled for that purpose by the game writing $02 to $5104 (use as ordinary RAM). The game uses PRG mode 3, e.g. 8 KiB banks, while Castlevania 3 uses PRG mode 2, i.e. one 16 KiB and two 8 KiB banks.

Edit: added $5104 notice.
Re: MMC5 PRG RAM Bankswitching Confusion
by on (#243119)
Thanks for your reply!

Okay so there's no internal ram, that clears that up, but in that case what is the meaning of the RAM/ROM switch in $5114-$5116? what happens differently? Surely it doesn't allow writing directly to the banks on the cartridge? Metal Slader definitely doesn't set this flag when loading some banks (meaning RAM mode)
Re: MMC5 PRG RAM Bankswitching Confusion
by on (#243120)
I would assume that if you set the PRG bank registers to RAM, and there is no RAM there, then you are reading/from writing to nowhere, i.e. to open bus.

Edit: I see that Metal Slader Glory does occasionally write $00 to $5115, turning $A000-$BFFF to open bus indeed.
Re: MMC5 PRG RAM Bankswitching Confusion
by on (#243121)
Okay so it needs open bus behaviour in that case? hmm alright.

How then do I detect if a cart has no ram? the NES header only says about save ram doesn't it?

and hypothetically speaking if there WAS RAM there, what would happen when switching to that mode?

Sorry for all the questions, i'm just trying to get my head around how all this works :P
Re: MMC5 PRG RAM Bankswitching Confusion
by on (#243122)
The NES 2.0 header's PRG-RAM and PRG-NVRAM fields tell you whether a cart has RAM, and how much. In the absence of a NES 2.0 header, you simply do not know. Short of using a hash table of all MMC5 games that contains the RAM size of all these games, emulating the maximum of 128 KiB works for all known games, as no known game relies on "address wrapping". In practice, no non-homebrew game uses more than 32 KiB (and I would be surprised to find a homebrew game that does), so just emulating a default of 32 KiB should be sufficient as well.

As far as I can see, the game does not need open bus there. It just writes a $00 value because it seems to be some kind of default value when the game does not need anything there, so it does not care if the emulator puts open bus or something else there.

If there was RAM there, then the $00 write to $5115 would map the first 8 KiB of that RAM to CPU $A000-$BFFF.
Re: MMC5 PRG RAM Bankswitching Confusion
by on (#243124)
So I take it MSG appears not to be another case of Low G Man syndrome. That game is notorious for requiring correct emulation of open bus.
Re: MMC5 PRG RAM Bankswitching Confusion
by on (#243125)
OK cool so in the case RAM does exist you just map the RAM space there, so the bank number is completely ignored?

MSG does a weird one for me, it tries to load bank 95 (integer) on $5115 which is obviously outside the number of banks on the cart but the ROM bit isn't set
Re: MMC5 PRG RAM Bankswitching Confusion
by on (#243126)
refraction wrote:
OK cool so in the case RAM does exist you just map the RAM space there, so the bank number is completely ignored?
No, it is applied. $00 means map 8 KiB PRG-RAM bank #0, $01 means 8 KiB PRG-RAM bank #1, and so on. If there is only 8 KiB of PRG-RAM, then of course bank #1 wraps back to #0, and if there is no PRG-RAM at all, then all PRG-RAM values become open bus.
refraction wrote:
MSG does a weird one for me, it tries to load bank 95 (integer) on $5115
I cannot reproduce that. The game might have already gone off the rails in your emulator for another reason by the time it seems to write that value.
Re: MMC5 PRG RAM Bankswitching Confusion
by on (#243127)
NewRisingSun wrote:
No, it is applied. $00 means map 8 KiB PRG-RAM bank #0, $01 means 8 KiB PRG-RAM bank #1, and so on. If there is only 8 KiB of PRG-RAM, then of course bank #1 wraps back to #0, and if there is no PRG-RAM at all, then all PRG-RAM values become open bus.

Wait so the PRG-RAM contains actual data? and that can be overwritten (if PRG-RAM protection is off)? Wouldn't that corrupt the game when it's next run? or is the data refreshed in this RAM when the cart is next booted? It's stuff like this I'm not clear on lol.

NewRisingSun wrote:
I cannot reproduce that. The game might have already gone off the rails in your emulator for another reason by the time it seems to write that value.


Hmm okay, I just checked again and it doesn't seem to be doing it anymore which is weird, I had some odd writes to like 4080 etc happening too which seem to have disappeared, not sure what I changed...

So now I've gotta work out why it black screens after the intro :/ Any common causes?
Edit: Seems if I hit start it comes up, but there's nothing on screen to tell me to do that..
Edit2: Looks like I'm missing the menu graphics and the boot text, just the intro is fine :/ Was sure my Nametable mapping is fine though :/

Thank you for your help so far, it has been quite enlightening!

Edit 3: Sorry guys, I fixed it. Turns out the changes I made to my scanline counter because MSG seemed to be wrong (this is before I had graphics) actually broke it! I now have intros, menus, etc :)

Thanks all for your help!