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

Add description for out of bounds MBC ram access #594

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

alloncm
Copy link
Contributor

@alloncm alloncm commented Mar 22, 2025

This is based on:

Since there are many types of MBC's I added that this is for most of the MBC's

This is based on:
- Source code of the SameBoy emulator
- Dissassembling Pokemon Pinball which depends on this behavior
- Mbc1 schematics - https://www.devrs.com/gb/files/mbc1.gif
@nummacway
Copy link

Use "modulo max_ram_size" instead of "and (max_ram_size - 1)"?
Why does Pinball depend on it? It doesn't even have banked RAM.

@alloncm
Copy link
Contributor Author

alloncm commented Apr 5, 2025

Perhaps "depends" is not the best way to phrase it, what I actually saw is that Pokemon Pinball sets the ram bank to a non zero value and since it does not have banked ram, all accesses are clipped (according to the formula).
Failing to emulate this exact behavior causes the game to freeze at some point.

@nummacway
Copy link

What point? I want to try with a 32 KiB cart.

@alloncm
Copy link
Contributor Author

alloncm commented Apr 5, 2025

It depends on the way you are handling this case, originally I just ignored those out of bounds writes, (and returned 0 on reads) with this way it froze after trying to reload a save from sram (boot up -> save -> restart -> load save -> freeze).
I dont remember exactly if the load save is also done with the same non zero bank (I think it was bank 4) if it is it supposed to work with a 32kb cart, if not I think it will freeze.


## MBC Out Of Bounds RAM Access

On most of the MBC's in case an out of bounds address is accessed (selecting unmapped RAM bank),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any MBC on which this doesn't happen?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In MBC3, banks $08-$0C select the RTC, and in MBC7, bank $40 selects the accelerometer and EEPROM.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if you request banks $04-$07 or $0D-$0F?
I actually have everything you need to check this, I'm just lazy. 🦥

Copy link
Contributor

@CasualPokePlayer CasualPokePlayer Apr 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$04-$07 and $0D-$0F just map to "nothing", reads returning open bus and writes doing nothing. (also this is assuming MBC3, with MBC30 $04-$07 are just more RAM banks as expected)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I decided to not be lazy and tested this. $04-$07 and $0D-$0F return $FF on every address and are read-only. The high RAM bank nibble is ignored. So $4000 is effectively a 4-bit register.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 0xFF is actually just an open bus value: 0xFF would just be a usual open bus value if you're reading from somewhere else other than the cartridge bus (e.g. WRAM within the GBC, or just HRAM in all units). It can be understood as just being the same as reading SRAM while SRAM is disabled via the 0000-1FFF register.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$FF is a common SRAM open-bus value especially with some flashcarts, so this makes sense.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used a legitimate cartridge of Goldene Edition.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants