Gameboy SRAM Copier

Discussion of programming and development for the original Game Boy and Game Boy Color.
Drenn
Posts: 7
Joined: Wed Jan 01, 2014 9:50 pm

Gameboy SRAM Copier

Post by Drenn »

I've made a little program which should allow you to copy a game's SRAM from one cartridge to another. I say "should", because I have not tested it yet on real hardware. My EMS 64M smartcard won't arrive for a few weeks. It works well in bgb, but there may be complications in real hardware. Most notably, the gameboy tends to reset itself when removing or inserting cartridges. But this doesn't happen every time.

The main use of this is to backup your save games to a gameboy flashcart. From there you can back up your save games to a PC. The only way to do this was with the mega memory card. This program should alleviate the need for that, though perhaps the memory card will turn out to be more reliable.

The procedure is as follows: as instructed, remove your flash cartridge and insert your retail game cartridge. Press A to copy the SRAM to the gameboy. Remove the game cartridge and re-insert your flashcart. Press B to write the SRAM to the flashcart.

Download it here, and if you try it, let me know if it works for you.

Technical details:

The average game can have as much as 32 kilobytes of SRAM. The Gameboy Color has 32 kilobytes of RAM... plus 127 free bytes tucked away at the end of the address space. I challenged myself to fit my SRAM read and write code into that space, and I succeeded, I think. One of the drawbacks of this is that I can't really do any sanity checks on the "receiver" cartridge.

The Gameboy Camera seems to have 128 kilobytes of SRAM, so this will only dump the first quarter. I'm not aware of any other games with this much SRAM, other than lsdj.

I've tested it with mbc1, 2, 3, and 5 games, just not on real hardware.
Drenn
Posts: 7
Joined: Wed Jan 01, 2014 9:50 pm

Re: Gameboy SRAM Copier

Post by Drenn »

From another thread:
nitro2k01 wrote:
Drenn wrote:
nitro2k01 wrote: You could also let the swapped Gameboy send the data over the link cable. (So the program would have two options, load into RAM and receive.)
It looks like the gameboy Camera has more sram than most ordinary games, more than can fit in Gameboy Color memory. So maybe the link cable idea is a good one. Either that or it can be dumped in 4 goes.

I got started on this, and aside from the above issue, it's actually working pretty nicely. I don't have my 64M yet, so I've been testing on bgb with its "load rom w/o reset" feature. But I think it would work on real hardware. I'll make a thread soon.

Edit: in its current form, this would let you dump the first several images in the gameboy camera.
The problem is that there's a risk the Gameboy resets every time you insert or remove a cartridge. This is because of reset line glitching, and on DMG to a lesser degree bus possibly glitching since the work RAM is on the same bus as the cartridge. You can cut the reset line inside the Gameboy, or on the cartridge, but some cartridges rely on the reset line to be pulled high for their operation, so you may want to fix this somehow. I've solved this by using a broken Mega Memory in passthrough mode as an adapter, where I disconnect the reset lines, and pull the second cart slot's reset line high.

One of those projects that I want to do, but may never finish, is a universal cart swapping dumper/flasher. So this thing would copy itself to RAM, and then you would swap the cartridge, and then you can dump ROM or SRAM data in various ways. This could include link port, audio (which would be decoded by an application on the PC) or for smaller amounts of data, work RAM.
Thanks for the info. Since this is meant for making savegame backups, it only needs to work once to be successful. Certain cartridges seem to reset a lot more than others, like my copy of Donkey Kong Country. It seems like this would be more of a nuisance than a problem.

I find it difficult to open things without breaking them, so I probably won't try anything with the reset lines.
Drenn
Posts: 7
Joined: Wed Jan 01, 2014 9:50 pm

Re: Gameboy SRAM Copier

Post by Drenn »

I finally got my EMS-64M flashcart. It turns out that the EMS 64M always resets the gameboy when it is inserted. So, this works for copying saved data to a cartridge, but not the other way around. I may work on a solution involving the link cable, as nitro2k01 suggested.
Lupus
Posts: 2
Joined: Sat Nov 14, 2015 1:29 pm

Re: Gameboy SRAM Copier

Post by Lupus »

Frist of all I want to apologize for digging out such an old thread but I think this information might be usefull for other people since this thread was the only one I found using google about this specific topic.

This little tool did exactly what I wanted it to do. About the resets. The resets are not caused by the reset line glitching out (I tried putting tape over it). The problem is that some cardridges have parts that require a lot of power to start up (includig capacitors) and thous will drain the 5V line if plugged into the gameboy. During normal startup this isn't an issue since the gameboy cpu is also starting up. But if you plug these cardridges in while the gameboy is already running the voltage drops too low for the cpu to remain stable and the whole thing crashes. After the power goes back to 5V the cpu is restarting (looking like it was reset).

I don't think most people want to mess with their hardware but if you want to, here is how you can still use this tool in order to backup your gameboy savegames to a flashcard. Keep in mind that you can break your gameboy, cardridge and any other device included in the process if you don't know what you're doing. So in order to stabilize the power line you need to power the gameboy from an external source (since batteries can't handle the sudden voltage drop). I did a little misstake there by not realising that one of my cables was broken so my first attempt didn't work. I was trying to hook a power supply to the battery tray. This might work, but I don't know. The way I did it was by soldering 2 wires to my flashcard so I could access the 5V and GND line while the flashcard was plugged in. I used one of these more expensive power supplies that let's you adjust the voltage as you desire. Then I proceeded as normal, started the SRAM dumper, removed the flashcard (which at this point was still powered externally), put my old game cartridge into the gameboy, copied the SRAM into the gameboys RAM, removed the game cardridge and carefully put the flashcard back in place and copied the RAM back into the SRAM of my flashcard. This worked first try. If you happen to have a game that crashes your gameboy you might need two power supplies.

Just an additional note about removing flashcards. I don't know how each flashcard works but you might even need to keep the flashcard powered (as it did) while it's removed since it contains a little cpu that keeps track of the flashcards state and might not allow you to write to the flashcards SRAM if the flashcard gets reinserted since it assumes no game is running and thous SRAM access is not required.

I don't recommend you doing this if you don't feel confident about working with running circuits and soldering. Connecting the wrong things or using the wrong voltage by even a tiny bit will almost certainly kill your flashcard and gameboy.


You might as well just buy a cardridge reader but I didn't want to spend money on this for just one game. Thanks for this great tool and I hope this post will help some people in the future.
Drenn
Posts: 7
Joined: Wed Jan 01, 2014 9:50 pm

Re: Gameboy SRAM Copier

Post by Drenn »

Lupus wrote:Frist of all I want to apologize for digging out such an old thread but I think this information might be usefull for other people since this thread was the only one I found using google about this specific topic.

This little tool did exactly what I wanted it to do. About the resets. The resets are not caused by the reset line glitching out (I tried putting tape over it). The problem is that some cardridges have parts that require a lot of power to start up (includig capacitors) and thous will drain the 5V line if plugged into the gameboy. During normal startup this isn't an issue since the gameboy cpu is also starting up. But if you plug these cardridges in while the gameboy is already running the voltage drops too low for the cpu to remain stable and the whole thing crashes. After the power goes back to 5V the cpu is restarting (looking like it was reset).

I don't think most people want to mess with their hardware but if you want to, here is how you can still use this tool in order to backup your gameboy savegames to a flashcard. Keep in mind that you can break your gameboy, cardridge and any other device included in the process if you don't know what you're doing. So in order to stabilize the power line you need to power the gameboy from an external source (since batteries can't handle the sudden voltage drop). I did a little misstake there by not realising that one of my cables was broken so my first attempt didn't work. I was trying to hook a power supply to the battery tray. This might work, but I don't know. The way I did it was by soldering 2 wires to my flashcard so I could access the 5V and GND line while the flashcard was plugged in. I used one of these more expensive power supplies that let's you adjust the voltage as you desire. Then I proceeded as normal, started the SRAM dumper, removed the flashcard (which at this point was still powered externally), put my old game cartridge into the gameboy, copied the SRAM into the gameboys RAM, removed the game cardridge and carefully put the flashcard back in place and copied the RAM back into the SRAM of my flashcard. This worked first try. If you happen to have a game that crashes your gameboy you might need two power supplies.

Just an additional note about removing flashcards. I don't know how each flashcard works but you might even need to keep the flashcard powered (as it did) while it's removed since it contains a little cpu that keeps track of the flashcards state and might not allow you to write to the flashcards SRAM if the flashcard gets reinserted since it assumes no game is running and thous SRAM access is not required.

I don't recommend you doing this if you don't feel confident about working with running circuits and soldering. Connecting the wrong things or using the wrong voltage by even a tiny bit will almost certainly kill your flashcard and gameboy.


You might as well just buy a cardridge reader but I didn't want to spend money on this for just one game. Thanks for this great tool and I hope this post will help some people in the future.
I'm glad you found it helpful. Personally I'm a hardware noob and prefer to stay in the safe realm of software. One last observation I don't think I've mentioned in this thread, is that it doesn't seem to work on gameboy advance models. Without any hardware mods, the only way I can use this is to copy save data from my computer to a cartridge, using a gameboy color. Still helpful, but I'd need to make a link cable solution to make the other direction of transfer work.
User avatar
Dwedit
Posts: 4924
Joined: Fri Nov 19, 2004 7:35 pm
Contact:

Re: Gameboy SRAM Copier

Post by Dwedit »

Seems like the exact opposite of the GBA, where you can plug and unplug cartridges any time you want with no ill effects.
Here come the fortune cookies! Here come the fortune cookies! They're wearing paper hats!
Lupus
Posts: 2
Joined: Sat Nov 14, 2015 1:29 pm

Re: Gameboy SRAM Copier

Post by Lupus »

Dwedit wrote:Seems like the exact opposite of the GBA, where you can plug and unplug cartridges any time you want with no ill effects.
The GBA SP crashes as well if you plug in an Everdrive GB during normal operation. This comes down to the internal circuit not beeing designed for hot plugging and I would assume any device from that era will crash (maybe even custom retro builds). The Super Gameboy kept running for the most part but you can see a short flicker on the screen if you re-insert the flashcard (probably due to the same reason). The Gamecube thingy can detect if you remove the cardridge and will raise an error with no chance to switch cardridges like you would need in order to use this tool.

And I would also like to add that pluggin in a regular GBC power supply might do the job as well.
lidnariq
Posts: 11432
Joined: Sun Apr 13, 2008 11:12 am

Re: Gameboy SRAM Copier

Post by lidnariq »

Lupus wrote:This comes down to the internal circuit not being designed for hot plugging
That might be true for the SP, but it's definitely not true for the original GBA.

1- cartridge Vcc and Gnd are longer contacts than others. (Standard hotplug configuration.)
2- The hardware has a "cartridge inserted" interrupt.
3- the bypass capacitance on a GBA cart is only about 1.5µF, much smaller than the hundreds of µF on the mainboard

The thing about hot plugging is that you'll always be able to apply more load than it can support.
User avatar
mikejmoffitt
Posts: 1353
Joined: Sun May 27, 2012 8:43 pm

Re: Gameboy SRAM Copier

Post by mikejmoffitt »

Lupus wrote:
Dwedit wrote:Seems like the exact opposite of the GBA, where you can plug and unplug cartridges any time you want with no ill effects.
The GBA SP crashes as well if you plug in an Everdrive GB during normal operation. This comes down to the internal circuit not beeing designed for hot plugging and I would assume any device from that era will crash (maybe even custom retro builds). The Super Gameboy kept running for the most part but you can see a short flicker on the screen if you re-insert the flashcard (probably due to the same reason). The Gamecube thingy can detect if you remove the cardridge and will raise an error with no chance to switch cardridges like you would need in order to use this tool.

And I would also like to add that pluggin in a regular GBC power supply might do the job as well.
This isn't really correct. The GBA, GBASP, and Game Boy Player have a physical switch in the cartridge slot to detect a GBA vs GBC/DMG game. Changing it will cause a reset and mode switch. If you were to force the switch down, then you may have no issues.
nitro2k01
Posts: 252
Joined: Sat Aug 28, 2010 9:01 am

Re: Gameboy SRAM Copier

Post by nitro2k01 »

mikejmoffitt wrote:This isn't really correct. The GBA, GBASP, and Game Boy Player have a physical switch in the cartridge slot to detect a GBA vs GBC/DMG game. Changing it will cause a reset and mode switch. If you were to force the switch down, then you may have no issues.
I will have to counter that with another "this isn't really correct". Try it yourself. Ejecting a GB cart from a GBA will mostly not create a reset, but will stay in GB mode. Changing the state of the switch itself will not cause a reset however. Try doing it manually with a screwdriver after removing a cart (either GB or GBA) and see for yourself. :)
What will usually cause a reset is inserting a cart.

What the switch will do under the hood as far as I understand is:
1) Change the system voltage.
2) Change a bit in a register somewhere. (iirc)
3) Trigger the FIQ (fast IRQ) which may or may not be handled by the running program.
4) Change the GBA BIOS behavior to switch into GBC, detected using 2.
So, in effect when a cartridge insertion resets the GBA, something on the cartridge bus must be causing the actual reset, while the BIOS then decides to enter or not enter GBC mode using its usual method, based on the switch position.
User avatar
Dwedit
Posts: 4924
Joined: Fri Nov 19, 2004 7:35 pm
Contact:

Re: Gameboy SRAM Copier

Post by Dwedit »

As far as I know, the switch is much lower-level than what you described, the GBA BIOS doesn't even get a chance to see it.
Here come the fortune cookies! Here come the fortune cookies! They're wearing paper hats!
nitro2k01
Posts: 252
Joined: Sat Aug 28, 2010 9:01 am

Re: Gameboy SRAM Copier

Post by nitro2k01 »

I remember going through the BIOS looking for references to writes to the GB mode switch bit (bit 3 of DISPCNT) and found code that checks for something (don't remember exactly what) then sets up the frame around the GB screen, and then enters GBC mode. When singlestepping this code, I noticed something. This code waited one or a couple of frames before setting up VRAM and entering GBC mode, at which point the screen is white. You can observe what is probably this code in action if you turn a GBA off and on quickly before the dirt lines disappear. The screen is forced to be fully white for a short period of time. I should probably revisit this and document this code.

It would be strange if the code for setting up a border around the GB screen was there in the BIOS if there was a completely different mechanism all in hardware. I seem to remember that someone played around with manually entering GBC mode from a GBA program (wasn't that you even, dwedit?) to make it do rotations and other weird stuff when you changed aspect ratio with L and R.
User avatar
Dwedit
Posts: 4924
Joined: Fri Nov 19, 2004 7:35 pm
Contact:

Re: Gameboy SRAM Copier

Post by Dwedit »

Yes, there is all that stuff in the BIOS that is polling for a GBC cartridge bit in a register somewhere, then it sets a certain video mode and fades the screen into that mode.
None of it ever runs, because the switch is immediate. GBA code never gets to see a GBC cartridge.

Then if you glitch from GBA into GBC mode, it can't read the cartridges, so it can't do anything useful.

They probably tried to make it work in software, failed, then gave up and used the switch.
Here come the fortune cookies! Here come the fortune cookies! They're wearing paper hats!
nitro2k01
Posts: 252
Joined: Sat Aug 28, 2010 9:01 am

Re: Gameboy SRAM Copier

Post by nitro2k01 »

Dwedit wrote:None of it ever runs, because the switch is immediate. GBA code never gets to see a GBC cartridge.
What makes you think the switch is immediate? Like I said, when I was debugging that code path in the GBA BIOS, I noticed that it showed a white screen for maybe 3 frames before setting up the letterbox video mode. That was what led me to look at the boot process on hardware and it seems that it, too, shows a white screen for x frames, which I had never thought about before that. This is at least an indication that the BIOS code is running.
Of course, GBA code never gets to access the actual GBC cartridge, only the switch state, but that's all it needs.
User avatar
Dwedit
Posts: 4924
Joined: Fri Nov 19, 2004 7:35 pm
Contact:

Re: Gameboy SRAM Copier

Post by Dwedit »

You can stick in a GB cartridge while running a multiboot mode program with all interrupts disabled, and it will reboot into GB mode and run the game (Warning, inserting GB cartridges into a running system may corrupt the saved game). No ARM code or GBA BIOS code is responsible for it switching modes.
Here come the fortune cookies! Here come the fortune cookies! They're wearing paper hats!
Post Reply