# tutorial Writing Your Own Code For Pokemon B2/W2 Romhacks-Development Kit For B2W2

## Recommended Posts

so i have this idea that i've been kicking around in my head probably since right after b2w2 came out in japan. i think this will be a set of games that is hacked for a good long time to come. this is quite likely the last set of main series releases on the ds. anyway, what if we could actually program for the games? i don't mean in assembly, but in C or C++. i know what i would have to do and exactly what i would have to write to create a nice, robust library for low-level manipulation of the game. for example, say you wanted to decrypt a party pokemon do something with one of its fields: (let's say the level for pkm party slot 3)

u32 slot3_lv = getPkmStat(getPartyPkmAddress(&PkmBlk, 3), PKM_LV, NULL);


you could do that instead of writing some assembler and compiling it with the gcc assembler from devkitarm, arm-none-eabi-as, and hoping you got the syntax right, all the pointers right, etc etc.

some of the things you could do:

-create your own set of overlays and load them like gamefreak using their overlay handler

-create your own data files and load them into memory to manipulate them with your own code

-add new fields and data to pkm files

-add new data to the save game

-add new stuff to happen at midnight

-tweak damage calculations, stat calculations, a lot of stuff without needing an AR

-hell, rewrite the graphics renderer if you're feeling really enterprising

my list of script commands is very nicely filled out and quite thorough- all from analyzed code, all completely correct. i've done everything on this list(barring a lame attempt at the last one). what i'm offering is a way to do it more easily through a library and higher-level code instead of assembly. something like this would be much more accessible. i have an IDA database absolutely packed with info that would be so useful to this along with quite a few pages in a notepad of stuff and even more kind of bouncing around in my head that i've never mentioned to anyone.

i have a script compiler partially done, a script decompiler partially done, and the source files for libPKM prepped, but not started. this has the potential to be a full devkit for bw2.

the reason i'm doing this and not just starting work directly is that i'm wondering how many people would actually use it. i can think of less than a half dozen people that even could use it. so.. would anyone out there use this? it would take hacking this game to a whole other level, but it's not worth doing if i'm going to spend a ton of time on it and no one is going to use it. i'd love it if people could get behind this a little bit because it has huge potential, but like i said i don't really know how many people can use it.

thoughts?

Edited by Bond697

##### Share on other sites

This is a level of freedom in hacking that I haven't seen seriously discussed since... Pokemon Brown. Wow.

But you're right, how many people who're into the Pokemon hacking hobby are also versed in C++? Not a lot, like you said. But, if we had a way to fundamentally change so many aspects long seen as off limits, don't you think more people would learn how to use a tool like this? The promise of abilities like this ought to draw a lot of curiosity. Sure, many will be totally inept at first, but the number of people who could do this sort of thing and do it well will undoubtedly increase.

Me, personally? The biggest thing I miss from hacking Gen 4 (which has opened up a lot recently, too) was changing type effectiveness. Gen 5 is better than Gen 4 in almost every way, other than that... if this kind of hacking is the only way to find this data in the ROM outside of RAM analysis (which I've also never done), I'd love to learn to use your research myself.

Another thing that would be AWESOME (but a ton of work) would be to create the return of time-of-day Pokemon encounters! Adding in new encounter tables for existing routes, allowing you to have some different Pokemon show up at night, just like in Gen 2 & 4. You could do the traditional kinds of encounters, like Hoothoot & Zubat flying around outdoors... maybe even Pokemon entering battle sleeping, like Gen 2. Or you could even take a cue from Monster Hunter and have stronger, more vicious opponents roaming the countryside at night, etc....

##### Share on other sites

I would love this.

##### Share on other sites
I would love this.

Me too

##### Share on other sites

ok, good. and that's a good point above about people learning for the sake of using this.

i'll work through the end of the year on the database(right now i have more then 3000 functions labelled, mostly with arguments, across the arm9, the scripting overlay, the big overworld overlay, and a couple of minor overlays) to get as much ready as possible then get started after the new year.

##### Share on other sites

FYI: this is still happening, but work is moving slowly. I had a very successful test last night of a very simple way of using builtin code from the game in user-made functions. It works great.

##### Share on other sites

Heheheh, exciting. Keep up the good work!

##### Share on other sites

Now I'll have even more reason to engross myself in learning C++.

##### Share on other sites

http://hack.thundaga.com/libmem.7z

to use this, throw mem.h into ..\devkitPro\libnds\include and libmem.a into ..\devkitPro\libnds\lib

then in the file you want to use the functions, add:

#include <mem.h>

finally, pop open your makefile for the project you're working on and look for this line:

LIBS := -lnds9

change it to:

LIBS := -lnds9 -lmem

and you're good to go. aside from the basic get, set, and swap, the memcopies and memsets are faster than what's in newlib, the stdlib implementation in devkitpro.

##### Share on other sites

so i've been working on this and i thought it would be good to put up a basic example of the kind of thing you'll have access to with this kit:

all the function definitions are straight from the arm9 and are normally used for allocating memory in-game. you'll have access to all the same stuff plus some inlined and macro functions.

by my count, if you add the 2 free blocks together here:

#define FREE1 0x23A8B04

#define FREE1_SIZE 0x374FC

#define FREE2 0x23FA800

#define FREE2_SIZE 0x5000

you end up with roughly 240kb free for your own data/code, which is quite a bit. this is especially so since you'll be able to move code and stuff in and out of ram via overlays.

i think what will happen is that i'll write a function that just allocates all the free memory into 2 main heaps that you can do things with, one around 0x5000 bytes and one around 0x374FC bytes. you won't have to use that function and you can write your own to partition up the memory even more, but it will be there as a. an example and b. for ease of use.

Edited by Bond697

##### Share on other sites

just another update on this:

i'm still going. i just finished the main file manipulation functions. here's a preview:

it's going pretty well.

Edited by Bond697

##### Share on other sites

I just bought a book about c++ and started learning. This is pretty interesting.

##### Share on other sites

the last few obstacles to this are finally gone. i was worried about getting devkitpro to produce binaries with nothing but the user-created code instead of a full nds binary, but a custom makefile and linker file solved that. my other concern was that the symbol file used with the makefile was causing all generated branches to be done in ARM, but all of b2w2 are in THUMB. i found a (thankfully simple) way to change that that i didn't think would work, but fortunately does. the only other even minor issue was using asserts. you can't use assert or sassert and there's no console, stdin, or stderr, so outputting text to the screen would be difficult to say the least unless i added it to the rom and did a whole lot of unnecessary work. so i found a compromise:

when you throw an assert (using special asserts i'll provide), the screen will turn that black and blue and ask you to reset like when there's a normal in-game error and you'll be able to go to 0x23DFB00 to view the text from the assert sitting in memory in ASCII. it works surprisingly well.

e: the asserts will look like this:

http://i.imgur.com/ZDWHsdu.png

Edited by Bond697

##### Share on other sites

still going. i've opened up almost the entire sdk for everyone to use. next will be the foundation system and finally gamefreak's functions and my own inlines to make them easier to use. then testing and we'll be good to go.

i gave this a quick test the other day. i threw the "libpkm" folder into the main "libnds" folder and started calling functions, declaring types, calling inline functions, etc from my code. after a few very enlightening errors, everything compiled perfectly and would have run without issue if i made an overlay out of it.

i'm also working on a feature similar to libnds where your code compiles as arm/thumb if you add that word to the filename.(i.e. mat.arm.cpp compiles as arm)

e:

the last feature is done. if you add "arm" or "thumb" to your sourcefile name, it will compile for that cpu state.

##### Share on other sites

Would you consider making this an open source project? I would be very interested in participating and contributing to this project. I'm new to this site and new to Pokémon hacking but I'm experienced in C, C++ and basically any programming and/or scripting language.

I would also love it for one if there was one tool out there that can run on a decent OS for once, if you could find the room for another developer I'd be more than happy to compile the project on Linux too (since haven't been using Windows since 2002).

BTW, since I'm new to this community, could any of you point me to a guide containing the rom structure of a Pokémon game? I've been looking to change all battles in fire-red to double battles (for reasons of strategy etc.) and I'd love to do this in one simple bash/python/awk script instead of having to manually alter every other training using existing tools. Basically I just want to know how a trainer s stored internally in a binary or hex rom file.

##### Share on other sites

i've always been the only person working on this because i've done all the reverse engineering myself and no one knows what i'm doing like i do. see here:

that said, this isn't the kind of project where i would want a lot of people working on it because you need to have at least some idea of how things work to add code. that said, i'd be fine working with one other person on it. i pm-ed you the latest set of code. have a look and see what you think. and if you know a lot about ld and gcc in general, that would be great. if you want to discuss it, i'm always on pp's irc.

if you're looking to edit gen 3 stuff like fire red, you probably want pokecommunity.

##### Share on other sites

i'm making real progress with this now, so i figured i would put up some of the headers for people to check out:

http://pastebin.com/b6zMY8rX

http://pastebin.com/0akk4EsT

http://pastebin.com/3xpB8Fda

http://pastebin.com/M1PVVfuQ

http://pastebin.com/e5X6fg6J

everything is tested and working nicely with libnds.

##### Share on other sites

Oh god this is amazing. Im definitely going to use it when it comes out.

By the way is this going to be independent of an original rom? Or you will need to compile your code to some specific binarys (like arm9.bin) and combine it with some resource files from the original rom (like narc files)?

##### Share on other sites
Oh god this is amazing. Im definitely going to use it when it comes out.

By the way is this going to be independent of an original rom? Or you will need to compile your code to some specific binarys (like arm9.bin) and combine it with some resource files from the original rom (like narc files)?

Start with the original ROM, and modify that. Writing code with this makes an overlay, which is loaded by the ROM.

##### Share on other sites
Oh god this is amazing. Im definitely going to use it when it comes out.

By the way is this going to be independent of an original rom? Or you will need to compile your code to some specific binarys (like arm9.bin) and combine it with some resource files from the original rom (like narc files)?

i'm not sure yet. what will probably happen is that i'll write some kind of special overlay loader/hack that will handle the initial loading/hijack. i have the binaries that are built from user-created code stripped and ready to insert. i'll probably create an overlay info table editor that will let you insert the overlays at the end of the list. then you can load them when you need them. what will basically happen is, i'll have the user define a variable at a certain address and if that address is non-zero, my loader will assume there's a hack present to be loaded, etc. i'm still working it out, but that's the basics.

##### Share on other sites

If I have learned anything from being part of the modding community for minecraft. If you give people a robust common language to work with in a game for modding and or hacking and they don't know it they will learn it. In fact doing this might bring pokemon rom hacking even farther into the mainstream. I personally haven't done any major modding or programming I know enough python to do some basic scripting. But I know that people will flock to something as robust as complete control over the game like having a C++ dev kit. Also I feel this might open the field up to much better tools and map editors if all they have to do is basically write the files out in a standardized format then just run it through a compiler. So instead of the people programming these tools having to do the really heavy work they can focus on making a strong good looking tool for editing the roms. Good luck with this massive undertaking.