• Our software update is now concluded. You will need to reset your password to log in. In order to do this, you will have to click "Log in" in the top right corner and then "Forgot your password?".
  • Welcome to PokéCommunity! Register now and join one of the best fan communities on the 'net to talk Pokémon and more! We are not affiliated with The Pokémon Company or Nintendo.

Development: Decompilation of Ruby and Sapphire in C

camthesaxman

reverse engineer of teh pokeymanz
23
Posts
7
Years
  • Age 28
  • Seen Feb 16, 2018
Hi everyone!

I'm currently working on this project to decompile Pokemon Ruby and Sapphire into matching C code. When this project becomes complete enough, it will be a great resource for hackers since you can easily edit png sprites and modify the game's code using high level C instead of assembly. Pokemon Ruby was originally written in C, and built using the open-source GCC 2.9 compiler. Our basic procedure is taking a piece of disassembled code, rewriting it in C, compiling it with GCC 2.9, and then verifying that the compiled code matches exactly, byte for byte, what is in the ROM. Since the code that we wrote compiles to the same assembly as the original ROM, our code is extremely similar to what is in Game Freak's original source code. We currently have 20% of the code decompiled, which is a significant portion, and all of the sprites and text have been dumped. If anyone knows ARM assembly and C programming, feel free to contribute and speed up this process. If you have any questions, post here or ask on the #pret IRC channel on freenode.net.

For example, here is the function that generates a wild Pokemon.
Code:
static bool8 GenerateWildMon(struct WildPokemonInfo *wildMonInfo, u8 area, bool8 checkRepel)
{
    u8 wildMonIndex = 0;
    u8 level;

    switch (area)
    {
    case 0:
        wildMonIndex = ChooseWildMonIndex_Land();
        break;
    case 1:
        wildMonIndex = ChooseWildMonIndex_Water();
        break;
    case 2:
        wildMonIndex = ChooseWildMonIndex_Water();
        break;
    }
    level = ChooseWildMonLevel(&wildMonInfo->wildPokemon[wildMonIndex]);
    if (checkRepel == TRUE && RepelCheck(level) == FALSE)
        return FALSE;
    else
    {
        CreateWildMon(wildMonInfo->wildPokemon[wildMonIndex].species, level);
        return TRUE;
    }
}
 

Le pug

Creator of Pokémon: Discovery / Fat Kid
870
Posts
10
Years
This is a great contribution to the community and I wish more people of similar skills did the same for other ROMs like Emerald as well. Keep up the great work and let me know when you finish, I'll definitely donate for completing such a beast of a project!
: D
 
1
Posts
7
Years
  • Age 25
  • Seen Feb 15, 2017
How long did it take to get to where it is currently, and have the other games (Pokemon Red, Firered, etc) been completely converted yet?
 
36
Posts
14
Years
  • Seen Oct 19, 2023
How long did it take to get to where it is currently, and have the other games (Pokemon Red, Firered, etc) been completely converted yet?

Pokemon Red has been completely disassembled along with Crystal, google pokered and pokecrystal and you'll find the git repository. As far as I can see, most efforts are currently focused on Ruby.

The pret (Pokémon Reverse Engineering Team) also has (incomplete as of yet) repositories for:
emerald
yellow
pinball
tcg
stadium
puzzle league

They currently don't have a disassemble repository for Fire Red but I think it is because Shiny Quagsire already started one called DisFire, though it hasn't been updated in a while.
 
Pokemon Red has been completely disassembled along with Crystal, google pokered and pokecrystal and you'll find the git repository. As far as I can see, most efforts are currently focused on Ruby.

The pret (Pokémon Reverse Engineering Team) also has (incomplete as of yet) repositories for:
emerald
yellow
pinball
tcg
stadium
puzzle league

They currently don't have a disassemble repository for Fire Red but I think it is because Shiny Quagsire already started one called DisFire, though it hasn't been updated in a while.

Yellow is actually complete. There are completed ones for Red, Yellow, and Crystal.
 
Last edited:

Shiny Quagsire

I'm Still Alive, Elsewhere
697
Posts
14
Years
Pokemon Red has been completely disassembled along with Crystal, google pokered and pokecrystal and you'll find the git repository. As far as I can see, most efforts are currently focused on Ruby.

The pret (Pokémon Reverse Engineering Team) also has (incomplete as of yet) repositories for:
emerald
yellow
pinball
tcg
stadium
puzzle league

They currently don't have a disassemble repository for Fire Red but I think it is because Shiny Quagsire already started one called DisFire, though it hasn't been updated in a while.
Yeah I started it, but lost time/interest because disassembly unfortunately isn't the most exciting thing. Would probably be easier to redo it anyhow for armips or similar, and also use macros for the script commands as opposed to assembly bits.
 

Marky Vigoroth

...I just liked the Skarmory icon.
171
Posts
8
Years
Honestly, seeing the C code has me feel so much better. The idea of using .png sprites only makes me feel more better. I eagerly await the completion of this disassembly.

I hope that there would be comments in the code, too.

PS I am hoping that someone finishes the Emerald and FireRed/LeafGreen ones, too.
 
Last edited:
755
Posts
7
Years
  • Age 33
  • NY
  • Seen Oct 9, 2023
How did you discover which compiler Game Freak used? Are there "tells" in the form of its generated asm, or was there a debug "GCC 2.9" string left in the ROM?

I wish pokeemerald were getting the main focus as a Gen 3 disassembly—it has significant improvements over Ruby, like the Battle Frontier, that would be time-consuming to port even when the C conversion is done—but regardless, pokeruby is shaping up to be an impressive and useful project.
 

Marky Vigoroth

...I just liked the Skarmory icon.
171
Posts
8
Years
How did you discover which compiler Game Freak used? Are there "tells" in the form of its generated asm, or was there a debug "GCC 2.9" string left in the ROM?

I wish pokeemerald were getting the main focus as a Gen 3 disassembly—it has significant improvements over Ruby, like the Battle Frontier, that would be time-consuming to port even when the C conversion is done—but regardless, pokeruby is shaping up to be an impressive and useful project.

I hope that this disassembly would massively reduce the amount of disassembly required in Emerald. In fact, I wonder if the reason why Ruby and Sapphire were picked was because they take less resources than Emerald because Emerald is 'bigger.'
 
I hope that this disassembly would massively reduce the amount of disassembly required in Emerald. In fact, I wonder if the reason why Ruby and Sapphire were picked was because they take less resources than Emerald because Emerald is 'bigger.'
Much of Ruby's code is analogous to what's in Firered and Emerald. They're all built on the same engine, after all. The hope is that, once Ruby is finished, the Ruby disassembly alone will mostly complete the Firered and Emerald ones, with just the optimized/changed/added portions of the games needing to be done. In this way, working on the Ruby disassembly is working on the others. Much of what's under the hood is the same.
 

Bonnox

Time (wasting) Lord
47
Posts
8
Years
first of all I want to congratulate myself for the brave of starting this very important project. (i clicked the like button but unfortunately it doesn't get saved...)

But why are you trying to obtain the exact assembly? Maybe to maintain compatibility with the non-disassembled-yet code which may call the disassembled one? Once the whole code has been disassembled, can we improve or simplify it, for a more comfortable work?
Infact, some time ago I tried compiling some of these reversed projects (ruby, or maybe emerald, I don't recall), and it took me a quarter of hour to fix some little problems during compilation. I believe in the idea that every code ever shipped on the internet, should compile at the first try, if the requirements are fullfilled, obviuously. Unfortunately a lot of times it is not so...

Is it difficult to write code that, when compiled, has to be perfectly equal to another?

You can count me in. If you want, feel free to assign me some tasks, but please keep them light, becasue of school exams.
 
Last edited:

camthesaxman

reverse engineer of teh pokeymanz
23
Posts
7
Years
  • Age 28
  • Seen Feb 16, 2018
Sorry for not being very active. I've been busy with school and other things.

There actually are some significant differences with Fire Red, Leaf Green, and Emerald compared to Ruby and Sapphire. In Ruby, a lot of variables are allocated simply by picking some memory address and defining a macro that accesses an object at that address. In the later games, they actually implemented a malloc() function for that, and that causes lots of code differences all over the place. YamaArashi did try decompiling Emerald first, but found it difficult to get matching code for a lot of the basic functions. Ruby turned out to be easier to match for some reason, and it's a great deal smaller than Emerald. We did port over a few files to the pokeemerald repository, though.

@Deokishisu:
The German debug ROM doesn't have debug symbols in it, if that's what you mean. Those get stripped when the executable is converted to a GBA rom. A lot of the debug menus are still left in the retail versions, though they're inaccessible without hacking.

@Rangi
The official Nintendo SDK was leaked (you can find it on the internet if you search enough), and it includes the compiler.

@Bonnox:
Well, we're trying to ensure that our C code is correct, and behaves exactly the same as the original ROM. It's easy to ensure that if your C code generates the exact same asm as the original. C code doesn't have a one to one relationship with machine code. Compilers can optimize it and produce very different code that has the same behavior. What makes this doable is that this old version of GCC did not optimize very well and generates pretty crappy code that has some predictable patterns. It also uses -O2 optimizations, which doesn't perform any function inlining. It's a pretty tedious task to get the code to match. Sometimes I just give up after a while and leave a function as inline assembly, and move on to some other function. As far as building goes, it should work fine if you're using a recent version of DevKitArm with Windows or Linux, and carefully follow the instructions in the README. If you're on Linux, the agbcc compiler needs to be built as 32 bit. Mac OS X isn't supported, but someone did manage to build pokeruby on it.
 

Go! Squirtle

Squirtle master is here
60
Posts
7
Years
One question i want to know is how do you decompile ruby rom in into asm?
 
Last edited:

Go! Squirtle

Squirtle master is here
60
Posts
7
Years
Are you going to answer me. Please! Answer My questions i have posted a post on this thread about them quote post and answer my questions.
 

BluRose

blu rass
811
Posts
10
Years
One question i want to know is how do you decompile ruby rom in into asm?
Are you going to answer me. Please! Answer My questions i have posted a post on this thread about them quote post and answer my questions.
someone's impatient

a way to do it which i doubt is the way done is to do it in vba :P search around in options
a more fun way of doing it would be like no$gba's debugger
maybe they even have an idb for it? lol probably not but hey

the way they probably do it is using one of these
 

Go! Squirtle

Squirtle master is here
60
Posts
7
Years
someone's impatient

a way to do it which i doubt is the way done is to do it in vba :P search around in options
a more fun way of doing it would be like no$gba's debugger
maybe they even have an idb for it? lol probably not but hey

the way they probably do it is using one of these
Thanks! This is what need to know. But i want to know how they convert hex values into asm code .
 
Back
Top