Homebrew The Truth behind GBA emulation

  • Thread starter Thread starter Normmatt
  • Start date Start date
  • Views Views 39,864
  • Replies Replies 168
  • Likes Likes 1
Status
Not open for further replies.
I (along with Lordus) have been thinking about all of this for a while to see if there was any way it could work out. We ended up asking sgstair, who actually did present us with something that could be feasible. So I have to take back what I said, I was wrong to throw it down so easily.

Then again, it's uncertain how well it'd work. There would probably be mild to serious compatibility issues. To paraphrase sgstair, "if such a thing worked it'd by thanks to nothing but luck."

Here's the way it works: when the MPU raises a prefetch abort code code is loaded into RAM for that block of ROM, and then the PC is changed to this block in RAM, for a small block size. This is then protected by another MPU region which will trap any branches or PC-relative references outside the block. When it exits the block, the block region window is changed to fit the block it moves to, or it's loaded into memory if it's not there (eventually evicting something that is there). For those familiar with translation lookaside buffers, this is basically using the MPU to create a 1-entry version. This part covers code virtualization and some data virtualization.

Other data accesses that are not PC-relative to the code block loaded in would end up being trapped (including those to indirect addresses back on ROM). There wouldn't be a data TLB, per se, so these would all have to be virtualized entirely for each access.

All I/O accesses would have to be trapped; you can't single them out because the granularity on the MPU isn't low enough for that. DMA would have to be emulated and redirected where possible.

The BIOS would be loaded in ITCM at the beginning. It'd probably benefit from some customization to make memory transfers faster.

The block size to choose is not an easy thing and I'm sure different games would work better with different things. Large block size means more code will be kept resident at a time (less TLB misses), but will mean that less of the working set will be captured due to internal fragmentation, and less code will be mappable overall due to MPU segment/size restrictions. For this to have any hope at working well I'm sure it'd have to be tweaked per-game for many case.

Positives:
- When code does run uninterrupted it'd be fast, assuming that it stays in icache enough of the time. Not only is DS's ARM9 4x the CPU speed of GBA's ARM7, it also has faster load/store instructions.
- Not a lot of effort would be needed to get this off the ground at first, it's not like writing an emulator.
- It might be possible to use the two rendering engines on both screens to capture + scale the output to fullscreen, although it'd lag a screen behind and it'd look pretty ugly anyway since it wouldn't be filtered.
- Could hack in things like savestates, rapidfire, screenshots, standard menu options, etc

Negatives:
- 16MB ROMs have a potential hazard since they expect a 16MB ROM address space to be accessible and might accidentally read from or branch to main RAM in a PC-relative fashion, causing the game to break. Branches are much more likely to happen than loads of this nature, meaning it can be alleviated by making the RAM portion non-execute and holding all necessary code in the upper half of ITCM (if it'll fit, there's only 16KB). The downside is that it'll break games that have code in EWRAM (it's not common but they do exist)
- Things are worse for 32MB ROMs, since they can't fit in the RAM space at all. If they try to branch from the lower half of the ROM to the upper half they'll end up in the middle of IWRAM and crash.
- 1 TLB entry is very unreliable. GBA code tends to jump around all over the place so it'll miss a lot on function calls at whatnot. What's even worse is if you get caught in a tight loop that crosses the boundary of the segment; if this happens you can kiss performance goodbye.
- If the game accesses an array at an indirect address in ROM via the CPU as opposed to using DMA or the GBA BIOS call for this they'll be very slow, since every access will have to be trapped and emulated.
- Some indirect branches have to be trapped too. For instance, if the code goes uses a lookup table (like for implementing switches). Returns from interrupts should usually work okay since the return addresses were stored implicitely.
- Sound is really tricky. The ARM9 can't access the audio hardware, GBA games tend to stream audio from buffers in IWRAM, which must be mapped to the ARM9 and not the ARM7. So audio DMA writes will have to be trapped and sent to the ARM7 in some way (either directly or via buffers). There are usually two streams updating for stereo sound so this is a lot of overhead. Games that rely on sound DMA IRQs are going to do even worse; the IRQs will have to be faked (I THINK this is perfectly doable, but costs you even more). These games aren't that common but do exist (Sword of Mana comes to mind)
- Games that have heavy raster effects will usually write to I/O a few times per scanline and this will add a lot of overhead.
- Because the display hardware doesn't match up in terms of line count HDMA will have to be emulated using HIRQs or else the game will glitch out. This adds more overhead.
- If the game relies on vblank happening at a certain line counter value then it's going to be in for a surprise. Faking vblank by faking the IRQ then forcefully blanking the screen might work, but sounds risky. Enter: more work to do per HIRQ.
- To handle all these VMM IRQs the IRQ handler is going to be a bit more bloated than the standard GBA BIOS one; I suppose the clock speed difference makes up for this.
- Enabling cache on IWRAM and VRAM will cause some games to glitch out or break entirely (inconsistencies with DMA, self modifying code, etc). Unfortunately, the IWRAM and VRAM as seen by the DS's CPU is actually slower than it is on GBA - you can expect code that runs from it to be almost twice as slow. If the game requires it to be uncached (like Golden Sun and other Camelot games would) and executes in IWRAM a significant percentage of the time (like most GBA games do, 40% is what I measured over some games) then they're going to suffer for this.
- GBA ROMs in general might not be that friendly with cache because they weren't written with it in mind. They jump all over the place and might introduce way collisions. Cache misses are worse than going through RAM, but for non-sequential loads/stores DS is actually slower than GBA's, I'm talking GBA's ROM. It makes up for this somewhat by issuing an N32 fetch for two Thumb opcodes at once (essentially getting a MUCH faster S16 second fetch out of this, where GBA would need to do two N16's - if you consider this DS is a bit faster). If this happens to volatile data it's a much worse story, the DS's RAM is significantly slower than the GBA's EWRAM. On the other hand, if it does stay cached then things will be in pretty good shape.
- Some games might thrash the thing's ROM cache by bouncing around data. GBA games tend to have pretty small working sets though.

So that's pretty much my take on it. Do I think it might be possible? Perhaps. Do I think someone actually did it? Maybe, but I have some doubts. Do I think it's worth trying? Yes, from a technical standpoint it's pretty interesting and not a huge amount of work to get some results. Do I think it'd be a suitable overall replacement for running games off of slot 2? I doubt it.
 
I (along with Lordus) have been thinking about all of this for a while to see if there was any way it could work out. We ended up asking sgstair, who actually did present us with something that could be feasible. So I have to take back what I said, I was wrong to throw it down so easily.


To sum up you would basically be running in NDS mode and catching anything GBA-wise that NDS mode can't handle while managing a sliding window where the GBA code is executing from. The time it would take to get that working, and nicely, would be a waste.
 
ez flash expansion pack is less than 20 dollars, id actually be upset if someone wasted a lot of time on GBA emulation when its better SNES we need!

who cares about this emulation when most people have a gameboy advance already or slot 2 device, why bother? the "anonymous" developer of this exploit can keep his silly programming, and even so someone else will catch on and develop it for themselves now they know it's possible.

I don't see why any of you bothered to reply anyways. Because everything revolves around you huh? The programmers should just keep their 'silly' programming to themselves. Heck, why do we even need flash carts, I can just buy the games. You flash cart makers can keep your silly 'development' to yourselves!

Because programmers shouldn't be bother doing what they want to do. They should be doing what you tell them to. Y'know, because you've contributed so much to the NDS community...

wait a second...
unsure.gif
 
I don't see why any of you bothered to reply anyways. Because everything revolves around you huh? The programmers should just keep their 'silly' programming to themselves. Heck, why do we even need flash carts, I can just buy the games. You flash cart makers can keep your silly 'development' to yourselves!

Because programmers shouldn't be bother doing what they want to do. They should be doing what you tell them to. Y'know, because you've contributed so much to the NDS community...

wait a second...Â
unsure.gif

Amen brutha'! Tell it!!
 
I don't see why any of you bothered to reply anyways. Because everything revolves around you huh? The programmers should just keep their 'silly' programming to themselves. Heck, why do we even need flash carts, I can just buy the games. You flash cart makers can keep your silly 'development' to yourselves!

Because programmers shouldn't be bother doing what they want to do. They should be doing what you tell them to. Y'know, because you've contributed so much to the NDS community...

wait a second...Â
unsure.gif

how dramatic! its just an opinion, calm down.
 
I understand how this is possible, I was about to say the same thing (at posting time anyways) but why would any developer spend hours and hours of work on something that anyone can do with an EZV for about $20?
 
A "hypervisor" as he is defining it is very similar to an emulator, yet still different. With an Emulator, you are virtually creating all the processors, ram, etc. in an actual system. With a "hypervisor", you are simply translating code written for the GBA into the DS's language, this is possible because the hardware is so similar, just like the Gamecube and Wii, they really are practically the same system with only minor upgrages, which is why it can still run Gamecube games to begin with.
Actually I would say that the wii has some pretty significant upgrades from the GC, but they are essentially the same core. In the same way the DS and GBA are the same core. AKA Arm7 to Arm9, or PPC 446MHz to PPC 1800GHz, see my point?
 
A "hypervisor" as he is defining it is very similar to an emulator, yet still different. With an Emulator, you are virtually creating all the processors, ram, etc. in an actual system. With a "hypervisor", you are simply translating code written for the GBA into the DS's language, this

Summing stuff up in "baby language" is really silly. There is no "language translation" here, the DS can run the same opcodes the GBA does... The places shit is in memory and how it works is different in the NDS hence you would need something to "trap" stuff that isn't going to work. The bigger problem is that you don't have 256mbits worth of main ram to load a full size GBA cart into, so you're going to have to load fragments of it and use the CP15 or something to catch stuff that's gone out of bounds and fix the buffer window.

is possible because the hardware is so similar, just like the Gamecube and Wii, they really are practically the same system with only minor upgrages, which is why it can still run Gamecube games to begin with.

Or the fact that Nintendo is pretty good at reworking their systems down to single chips and embedding the whole lot into the next gen console for backward capability?...
 
Well, I'm still a first year student at Conestoga College in the Computer Engineering Technology course, but I have a few years of good programming experience under my belt and I'd be very interested in creating a .GBA file patcher. I understand fully the concept of emulation and it's flaws - it seems like swatting a mosquito with a sledgehammer to do something like (as an example):

think of something to say,
write it down,
type it into a computer,
open a web browser,
go to google language tools,
paste in your phrase,
translate it to Swahili on google's servers,
print out the resulting page,
then attempt to read Swahili to someone else


It makes so much more sense to learn Swahili and just speak it properly.

I was already contemplating the idea of a 'translator' before even reading this article - but reading it made me more interested. Could anybody point me in the direction of where to get started on a project like this? I'm well ahead in my studies and often can't see my girlfriend 'cause she's busy, so I have plenty of extra time to mess around with. I'd be interested in reading a very detailed spec sheet on GBA files and NDS files to see what the differences are, for starters.

Does anyone have such information? Or is this almost-non-existent info that only serious developers can get their hands on?

The way I see it is, you can write GBA and NDS games in ANSI C, so if you could take the compiled result, reverse engineer it, or even just mask specific patterns of code (for example if the code "do this" drew a dot on the screen for GBA and "do that" drew it on the NDS, you could simply try to find each reference to "do this" and replace it) and as for logic, I assume ARM9 can handle the same base logic as the ARM7, am I right? So all that REALLY needs any work done is a replacement graphics 'driver' and audio 'driver'.

Does anyone have any suggestions/hints/starter code/anything else helpful?


Thanks!

EDIT:

I'm also wondering - is there anyone out there knowledgeable enough to know about hacking the DS from a homebrew game? Is there such thing as full detailed spec sheets for the DS out there? I love hacking and stuff, so finding a way to trick the DS into loading the unaltered GBA ROM from the Slot1 card into GBA's RAM, then tricking the DS into switching into GBA mode without damaging the ROM in the memory would be awesome!
 
Sorry to butt in, but I just wanted to agree with all the helpful people who think DSi users can just buy an EZ Flash instead of having a coder making a new program for it.

Hey wait a sec...
unsure.gif
The DSi doesn't even have a slot 2! But it doesn't matter, because you guys have DS lites, and as we all know, the world revolves around you. Glad we got that out of the way.
 
browner87 said:
Well, I'm still a first year student at Conestoga College in the Computer Engineering Technology course, but I have a few years of good programming experience under my belt and I'd be very interested in creating a .GBA file patcher. I understand fully the concept of emulation and it's flaws - it seems like swatting a mosquito with a sledgehammer to do something like (as an example):

think of something to say,
write it down,
type it into a computer,
open a web browser,
go to google language tools,
paste in your phrase,
translate it to Swahili on google's servers,
print out the resulting page,
then attempt to read Swahili to someone else


It makes so much more sense to learn Swahili and just speak it properly.

I was already contemplating the idea of a 'translator' before even reading this article - but reading it made me more interested. Could anybody point me in the direction of where to get started on a project like this? I'm well ahead in my studies and often can't see my girlfriend 'cause she's busy, so I have plenty of extra time to mess around with. I'd be interested in reading a very detailed spec sheet on GBA files and NDS files to see what the differences are, for starters.

Does anyone have such information? Or is this almost-non-existent info that only serious developers can get their hands on?

The way I see it is, you can write GBA and NDS games in ANSI C, so if you could take the compiled result, reverse engineer it, or even just mask specific patterns of code (for example if the code "do this" drew a dot on the screen for GBA and "do that" drew it on the NDS, you could simply try to find each reference to "do this" and replace it) and as for logic, I assume ARM9 can handle the same base logic as the ARM7, am I right? So all that REALLY needs any work done is a replacement graphics 'driver' and audio 'driver'.

Does anyone have any suggestions/hints/starter code/anything else helpful?


Thanks!

EDIT:

I'm also wondering - is there anyone out there knowledgeable enough to know about hacking the DS from a homebrew game? Is there such thing as full detailed spec sheets for the DS out there? I love hacking and stuff, so finding a way to trick the DS into loading the unaltered GBA ROM from the Slot1 card into GBA's RAM, then tricking the DS into switching into GBA mode without damaging the ROM in the memory would be awesome!

GBATEK has a vast amount of information on the GBA and DS.

The DS is built off of the GBA just like the Wii is built off of the GC.

The size of the GBA game would be one of the main problems you would face. The DS has only 4M of RAM (minus the 288K for GBA and anything else you would have to use for the hypervisor). This would only allow a very small number of games (and mostly crap) to run. For homebrew I could see the reason for trying to do this but for games I'm not sure if the headaches would be worth it.

And that is, of course, if you could get it to copy the ROM to RAM, remap the addresses to the RAM and boot up in GBA mode with it. Very knowledgeable people have tried that and failed because of how the hardware switches off and/or changes modes depending on if it is running GBA or DS code.

@midna25:
It is speculated that at some point the DSi will allow GBA games to run from internal storage or SD. Honestly, how can you be pissed off that you can't run GBA games when you bought a DSi knowing that it didn't play GBA?
 
My first and most hopeful plan would be to simply find a way to re-write the .GBA file into a legitimate .NDS file. To be able to translate the implied graphics and audio drivers (I don't know exactly how all this works out - it's less than an hour away from midnight my time and I have strep throat, a cold, ear infection, and homework) is all that I see needing to be done. What I'd like to do is read some good yet not toooo technical documentation (to start with) about GBA and NDS ROMs. Just in my head I'm wondering why you can't just rename .GBA into .NDS. When someone can completely, precisely, and fully answer that question we have an answer right there. I assume a NDS ROM has more information in it than a GBA because there are two screens to be written to, and I believe that the DS is the first to have stereo sound, but the underlying logic of the game should be the exact same. So if someone can find a way to add any extra bit needed to specify drawing to the top screen vs. the only screen (as is the case of the GBA) and also simply insert some blank data (or, if you wish - some extra screen candy) to the touch screen and split the mono sound to go to each speaker.

I'm very tired right now (and sick) so I'll look at the link given above tomorrow - but it sounds good
smile.gif
 
browner87 said:
My first and most hopeful plan would be to simply find a way to re-write the .GBA file into a legitimate .NDS file. To be able to translate the implied graphics and audio drivers (I don't know exactly how all this works out - it's less than an hour away from midnight my time and I have strep throat, a cold, ear infection, and homework) is all that I see needing to be done. What I'd like to do is read some good yet not toooo technical documentation (to start with) about GBA and NDS ROMs. Just in my head I'm wondering why you can't just rename .GBA into .NDS. When someone can completely, precisely, and fully answer that question we have an answer right there. I assume a NDS ROM has more information in it than a GBA because there are two screens to be written to, and I believe that the DS is the first to have stereo sound, but the underlying logic of the game should be the exact same. So if someone can find a way to add any extra bit needed to specify drawing to the top screen vs. the only screen (as is the case of the GBA) and also simply insert some blank data (or, if you wish - some extra screen candy) to the touch screen and split the mono sound to go to each speaker.

I'm very tired right now (and sick) so I'll look at the link given above tomorrow - but it sounds good
smile.gif

From what I've read you want to edit the gba rom itself too redirect the gfx and audio, that is never going to work because there is hundreds of different opcode combination's that can be used to read/write to the gfx/audio registers, you need to trap the actual read and writes using the CP15 itself which will allow you to trap any reads to the gba slot and redirect them to main ram. This is by far no easy feet and is likely to be very slow.
 
with the dsi coming out and no support for any slot 2 device i think its better to convert the rom to nds format rather than buying an ez flash.
 
browner87 said:
My first and most hopeful plan would be to simply find a way to re-write the .GBA file into a legitimate .NDS file. To be able to translate the implied graphics and audio drivers (I don't know exactly how all this works out - it's less than an hour away from midnight my time and I have strep throat, a cold, ear infection, and homework) is all that I see needing to be done. What I'd like to do is read some good yet not toooo technical documentation (to start with) about GBA and NDS ROMs. Just in my head I'm wondering why you can't just rename .GBA into .NDS. When someone can completely, precisely, and fully answer that question we have an answer right there. I assume a NDS ROM has more information in it than a GBA because there are two screens to be written to, and I believe that the DS is the first to have stereo sound, but the underlying logic of the game should be the exact same. So if someone can find a way to add any extra bit needed to specify drawing to the top screen vs. the only screen (as is the case of the GBA) and also simply insert some blank data (or, if you wish - some extra screen candy) to the touch screen and split the mono sound to go to each speaker.

I'm very tired right now (and sick) so I'll look at the link given above tomorrow - but it sounds good
smile.gif


A DS rom is actually more like an ISO. It contains a header, the ARM9 binary, the ARM7 binary, a filename table, a FAT, title/icon data, overlays, etc. Whereas the GBA contains a small header and everything else is code or files stores in a FAT-like filesystem. What you are talking about is dynamic recompilation and hypervising. Or a conversion program that produces a DS runable binary. I think it would require a tremendous effort in order to achieve and many or most games would require the attention of a human touch in order to convert properly. Not to mention the addition of another CPU could prove problematic.

Also you would still have the 4M RAM limitation... The GBA could access anywhere up to 32M on a cart at any time while the DS loads the arm7.bin, arm9.bin at boot and never touches them again -- only loads files from the FAT after boot.

Not trying to put you off because I would love to see GBA games running in DS mode but I think you are getting over-zealous about your ideas for this project w/o knowing what it would require. But then again many people -- if they knew how tough things will become -- would never have started their work in the first place.
wink.gif


da_head said:
QUOTE(browner87 @ Jan 21 2009, 12:18 AM) Just in my head I'm wondering why you can't just rename .GBA into .NDS.
:facepalm:

+1
 
Lol such an old thread. Back then it is easy to imagine why no one would want to touch this concept... but with the introduction of the DSi (unheard of in early '08) more and more people are going to be searching for this illustrious GBA emulator...
 
QUOTE said:
@midna25:
It is speculated that at some point the DSi will allow GBA games to run from internal storage or SD. Honestly, how can you be pissed off that you can't run GBA games when IF you bought a DSi knowing that it didn't play GBA?
Not pissed off at all, I just happen to like being sarcastic. I plan on buying a DSi (lite, flashcart and EZ-Flash all stolen) and am just pointing out that this is a worthwhile venture in more ways than convenience. Also, I'm not stupid enough to import an out of region DSi when the US release date comes in 5 months.

Edit: Ahh, look above this post. Now that I pointed out the fact, people are agreeing with me. Lovely.
 
midna25 said:
QUOTE said:
@midna25:
It is speculated that at some point the DSi will allow GBA games to run from internal storage or SD. Honestly, how can you be pissed off that you can't run GBA games when IF you bought a DSi knowing that it didn't play GBA?
Not pissed off at all, I just happen to like being sarcastic. I plan on buying a DSi (lite, flashcart and EZ-Flash all stolen) and am just pointing out that this is a worthwhile venture in more ways than convenience. Also, I'm not stupid enough to import an out of region DSi when the US release date comes in 5 months.

Edit: Ahh, look above this post. Now that I pointed out the fact, people are agreeing with me. Lovely.

Ah... Sorry, didn't catch the sarcasm. It's hard to hear it on the net sometimes.
wink.gif


Now you started something...
 
Status
Not open for further replies.

Site & Scene News

Popular threads in this forum