Need help with mmc5.cpp source, for custom modification

Discuss emulation of the Nintendo Entertainment System and Famicom.

Moderator: Moderators

infidelity
Posts: 339
Joined: Fri Mar 01, 2013 4:46 am

Need help with mmc5.cpp source, for custom modification

Post by infidelity » Tue Nov 12, 2019 5:32 am

I'm not fluent in c++, but I've been attempting to modify certain functions within the MMC5 mapper. So far I've been able to adjust the output size of the .sav file, but I need to adjust how many swappable wram pages are available. Register $5113 goes from 00-07, giving you 8 individual 2000 byte pages. But I need 32 pages (00-1F), and when you go higher than 07 within $5113, it repeats the other previous pages due to the bits involved.

Anyone with c++ knowledge that'd be willing to help would be greatly appreciated.

tepples
Posts: 21980
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: Need help with mmc5.cpp source, for custom modification

Post by tepples » Tue Nov 12, 2019 6:39 am

32 pages is 256 KiB of RAM. What application needs that much RAM?

infidelity
Posts: 339
Joined: Fri Mar 01, 2013 4:46 am

Re: Need help with mmc5.cpp source, for custom modification

Post by infidelity » Tue Nov 12, 2019 9:52 am

I'm creating a new project where the player can construct & save. I dont want to spoil any details as to what I'm trying to accomplish, but it works. I just need more available wram, which is why I've been trying to modify the mmc5.cpp file.

infidelity
Posts: 339
Joined: Fri Mar 01, 2013 4:46 am

Re: Need help with mmc5.cpp source, for custom modification

Post by infidelity » Tue Nov 12, 2019 11:01 am

So I'm hoping there's someone who can help me out with this.

calima
Posts: 1126
Joined: Tue Oct 06, 2015 10:16 am

Re: Need help with mmc5.cpp source, for custom modification

Post by calima » Tue Nov 12, 2019 12:07 pm

There's an "& 7" in a couple places around 5113 setting there. You'd change that to 31. But there may be many other places needing modification.

infidelity
Posts: 339
Joined: Fri Mar 01, 2013 4:46 am

Re: Need help with mmc5.cpp source, for custom modification

Post by infidelity » Tue Nov 12, 2019 12:13 pm

I'll edit that tonight. I know there's another section that looks to deal with the actual swapping of pages. They were "case"'s

case 0
case 1
case 2
case 4
case 8

I tried fiddling with that area but wasnt getting the correct results. Shouldn't have expected much since I can't write c++ :-/

User avatar
rainwarrior
Posts: 7816
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: Need help with mmc5.cpp source, for custom modification

Post by rainwarrior » Tue Nov 12, 2019 1:57 pm

We're talking about FCEUX's mmc5.cpp?

Check the GenMMC5_Init function at the bottom to set the save game size, in particular info->SaveGameLen needs to be set to the size of your WRAM for saving and loading to disk to work. You'll notice this is full of 3 special case hacks currently, so I guess you'll need another special case for 256k.

Secondly in MMC5_Init you'll need some way to specify WRAMSIZE... The iNES 2 header can specify WRAM size, and if info->ines2 is true it will show up in info->wram_size and/or info->battery_warm_size. You'll want to make MMC5_Init pay attention to that instead of just guessing it via CRC.

Go over all the uses of the actual bankswitch registers and see how the value is used to select a bank (search for uses of MMC5WRAM). It looks like & 7 is hardcoded in a bunch of locations, so... replace those with a mask variable I guess. Probably something to set in MMC5_Init to 7 generically, or something like (WRAMSIZE>>13)-1 when specified via iNES 2?

Also there's MMC5WRAMIndex which seems to map the smaller sizes to a 64k master. This needs to be expanded from 8 entries to 32, and BuildWRAMSizeTable should fill it with the values 0 to 31 for the 256k case, I guess?

There might be other stuff, too, but that's as far as I want to dig here speculatively. MMC5 is a big mapper implementation with a lot of historical hacky stuff in it. What you're asking can certainly be done, but it's not quite trivial.

infidelity
Posts: 339
Joined: Fri Mar 01, 2013 4:46 am

Re: Need help with mmc5.cpp source, for custom modification

Post by infidelity » Tue Nov 12, 2019 5:17 pm

Thank you rainwarrior, I'm going to start making adjustments/changes the best I can.

EDIT

So these are the changes I've made, but still no luck with swapping newer pages. I do however have the .sav outputting perfectly at 256kb.

BuildWRAMSizeTable(void)

Code: Select all

for (x = 0; x < 32; x++) {	//infidelity 11-12-19

case 0: MMC5WRAMIndex[x] = x; break; 						//0,1,2,3,4,5,6,7	//infidelity 11-12-19 (case 0 - case 32)
		case 1: MMC5WRAMIndex[x] = x; break; 						//0,1,2,3,4,5,6,7
		case 2: MMC5WRAMIndex[x] = x; break; 						//0,1,2,3,4,5,6,7
		case 3: MMC5WRAMIndex[x] = x; break; 						//0,1,2,3,4,5,6,7
		case 4: MMC5WRAMIndex[x] = x; break; 						//0,1,2,3,4,5,6,7
		case 5: MMC5WRAMIndex[x] = x; break; 						//0,1,2,3,4,5,6,7
		case 6: MMC5WRAMIndex[x] = x; break; 						//0,1,2,3,4,5,6,7
		case 7: MMC5WRAMIndex[x] = x; break; 						//0,1,2,3,4,5,6,7
		case 8: MMC5WRAMIndex[x] = x; break; 						//0,1,2,3,4,5,6,7
		case 9: MMC5WRAMIndex[x] = x; break; 						//0,1,2,3,4,5,6,7
		case 10: MMC5WRAMIndex[x] = x; break; 						//0,1,2,3,4,5,6,7
		case 11: MMC5WRAMIndex[x] = x; break; 						//0,1,2,3,4,5,6,7
		case 12: MMC5WRAMIndex[x] = x; break; 						//0,1,2,3,4,5,6,7
		case 13: MMC5WRAMIndex[x] = x; break; 						//0,1,2,3,4,5,6,7
		case 14: MMC5WRAMIndex[x] = x; break; 						//0,1,2,3,4,5,6,7
		case 15: MMC5WRAMIndex[x] = x; break; 						//0,1,2,3,4,5,6,7
		case 16: MMC5WRAMIndex[x] = x; break; 						//0,1,2,3,4,5,6,7
		case 17: MMC5WRAMIndex[x] = x; break; 						//0,1,2,3,4,5,6,7
		case 18: MMC5WRAMIndex[x] = x; break; 						//0,1,2,3,4,5,6,7
		case 19: MMC5WRAMIndex[x] = x; break; 						//0,1,2,3,4,5,6,7
		case 20: MMC5WRAMIndex[x] = x; break; 						//0,1,2,3,4,5,6,7
		case 21: MMC5WRAMIndex[x] = x; break; 						//0,1,2,3,4,5,6,7
		case 22: MMC5WRAMIndex[x] = x; break; 						//0,1,2,3,4,5,6,7
		case 23: MMC5WRAMIndex[x] = x; break; 						//0,1,2,3,4,5,6,7
		case 24: MMC5WRAMIndex[x] = x; break; 						//0,1,2,3,4,5,6,7
		case 25: MMC5WRAMIndex[x] = x; break; 						//0,1,2,3,4,5,6,7
		case 26: MMC5WRAMIndex[x] = x; break; 						//0,1,2,3,4,5,6,7
		case 27: MMC5WRAMIndex[x] = x; break; 						//0,1,2,3,4,5,6,7
		case 28: MMC5WRAMIndex[x] = x; break; 						//0,1,2,3,4,5,6,7
		case 29: MMC5WRAMIndex[x] = x; break; 						//0,1,2,3,4,5,6,7
		case 30: MMC5WRAMIndex[x] = x; break; 						//0,1,2,3,4,5,6,7
		case 31: MMC5WRAMIndex[x] = x; break; 						//0,1,2,3,4,5,6,7
		case 32: MMC5WRAMIndex[x] = x; break; 						//0,1,2,3,4,5,6,7
MMC5WRAM(uint32 A, uint32 V)

Code: Select all

V = MMC5WRAMIndex[V & 0x1F];	//infidelity 11-12-19
DECLFW(Mapper5_write)

Code: Select all

		case 0x5113:
			WRAMPage = V;
			MMC5WRAM(0x6000, V & 0x1F);	//infidelity 11-12-19
MMC5Synco(void)

Code: Select all

MMC5WRAM(0x6000, WRAMPage & 0x1F);	//infidelity 11-12-19
GenMMC5Init(CartInfo * info, int wsize, int battery)

Code: Select all

WRAM = (uint8*)FCEU_gmalloc(wsize * 262144);	//infidelity 11-12-19

			info->SaveGameLen[0] = 262144;	//infidelity 11-12-19
		else
			info->SaveGameLen[0] = 262144;	//infidelity 11-12-19

infidelity
Posts: 339
Joined: Fri Mar 01, 2013 4:46 am

Re: Need help with mmc5.cpp source, for custom modification

Post by infidelity » Wed Nov 13, 2019 5:38 am

Do my current changes look ok? Especially my changes to & 7 ?

Any further assistance would be great. I can't progress any further with this, not even sure if what I've done is correct.

infidelity
Posts: 339
Joined: Fri Mar 01, 2013 4:46 am

Re: Need help with mmc5.cpp source, for custom modification

Post by infidelity » Fri Nov 15, 2019 2:33 pm

Bump

User avatar
never-obsolete
Posts: 377
Joined: Wed Sep 07, 2005 9:55 am
Location: Phoenix, AZ

Re: Need help with mmc5.cpp source, for custom modification

Post by never-obsolete » Fri Nov 15, 2019 5:25 pm

I quickly looked it over but it looks like DetectMMC5WRAMSize should return 256 for your case.


In BuildWRAMSizeTable you only needed to add a case for when MMC5WRAMsize == 32, but hardcoding MMC5WRAMIndex[]to always be 0-31 should also work.

Code: Select all

WRAM = (uint8*)FCEU_gmalloc(wsize * 262144);
Unless wsize == 1, this will allocate too much space.
. That's just like, your opinion, man .

infidelity
Posts: 339
Joined: Fri Mar 01, 2013 4:46 am

Re: Need help with mmc5.cpp source, for custom modification

Post by infidelity » Sat Nov 16, 2019 2:27 pm

Thank you, you and zeromus said the same on detectwramsize. He assisted the rest of the way and now it's all set. Thanks everyone.

User avatar
rainwarrior
Posts: 7816
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: Need help with mmc5.cpp source, for custom modification

Post by rainwarrior » Sat Nov 16, 2019 9:17 pm

Had some down time today, and felt like looking at this a little more. After thinking about it, especially with all the people doing work on MMC5 recently, and new pinout discoveries, it seemed like it might be worth doing a more permanent solution that everyone can use rather than a private hack.

I added proper iNES 2 WRAM support for MMC5 to FCEUX with this pull request. Any interested reviews of the change are appreciated.

There's an appveyor build of that change if anyone needs an executable to try: FCEUX artifact with MMC5 iNES 2 WRAM support

Edit: Now merged into FCEUX main branch. Latest interim build will have it: Artifact

I've created a set of test ROMs that should cover potential cases: with or without iNES 2 header, with 0-7 bits of WRAM select, battery backed (s) or not battery backed (u). Source code is included.

The test results will show a grid of 128 values, each is the lowest $5113 value that can access that RAM bank. Banks that don't behave like RAM (e.g. open bus) will show as FF. It also sets the high bit if a previous save was successfully loaded, so you get 80 instead of 00, 81 instead of 01, etc. to check if saving works.

Edit: added a "2u1" ROM with a header that has 16k split as 8k saved + 8k unsaved.
Attachments
mmc5ramsize.zip
(38.72 KiB) Downloaded 70 times
Last edited by rainwarrior on Fri Nov 22, 2019 9:24 pm, edited 2 times in total.

lidnariq
Posts: 9403
Joined: Sun Apr 13, 2008 11:12 am
Location: Seattle

Re: Need help with mmc5.cpp source, for custom modification

Post by lidnariq » Sat Nov 16, 2019 10:05 pm

I'm unhappy about extending a mapper beyond what the real-world hardware could possibly support, but I don't think there's any use in my arguing about it...

User avatar
rainwarrior
Posts: 7816
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: Need help with mmc5.cpp source, for custom modification

Post by rainwarrior » Sun Nov 17, 2019 4:15 am

Well, in this case it's the same implementation either way. After this change, the only difference would be a 16 instead of a 128 in exactly one place in the code.

So... I don't particularly love theoretically impractical-for-hardware extensions like this, but in this particular case there is zero implementation burden for the emulator. Seems better to just quietly allow it to be supported than cause more hacky forks of FCEUX to floating around in the wild.

The wiki at least only documents the "real" stuff. At this point I don't think this hypothetical extension should even be mentioned there.

Post Reply