• Just a reminder that providing specifics on, sharing links to, or naming websites where ROMs can be accessed is against the rules. If your post has any of this information it will be removed.
  • Ever thought it'd be cool to have your art, writing, or challenge runs featured on PokéCommunity? Click here for info - we'd love to spotlight your work!
  • Our weekly protagonist poll is now up! Vote for your favorite Conquest protagonist in the poll by clicking here.
  • 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.

Using C language to hack Pokemon (3rd Gen)

tinix

PearlShipper & C Programmer
  • 86
    Posts
    15
    Years
    Hello everybody,

    Today I'm going to share my knowledge about how to use C for hacking pokemon roms instead of ASM.
    Ive been polishing this method for last few days.

    EDIT 16.8.2011: Added updated useful.h with header guard #define, remove .txt extension to use it

    Required Tools & Knowledge
    ----------------------------
    Spoiler:


    Compilation & Usage
    ------------------------------
    EDIT 16.8.2011: Added Optimisation Switches

    Spoiler:


    Action Time 1: Compiling, Using and Explanation of GetPokemonLevel.c
    --------------------------------------------------------------------
    Spoiler:


    Action Time 2: Calling Another Function (ASM or C)
    -----------------------------------------------
    Spoiler:


    That should be all regarding C & pokemon games :)
    If you have any questions post here.
    Do not copy without permission.


    Thanks to: HackMew for creating ASM compiler package and bat file

    Appendix: Pros and cons (if you think you have more say so in comments)
    Spoiler:
     
    Last edited:
    Very nice, I guess I thought the size of the routine would have been much larger from past experience. But with your options it appears as though it does perfectly fine. Props to you, my man. :)
     
    Oh, I forgot to attach zip with examples and it was on HDD I reformated >.< . Dont worry i still have files but i need to repack them.
     
    That would be useful. hahaha. XD { Failolol }
     
    Updated, ive noticed that I have forgotten zip just after i clicked submit
     
    I have tested it with two C routines(see example two), but i think it should be possible with any ASM routine in rom.
     
    I''m not the best in C...

    So is it possible to call a SWI function or other code from the ROM (for example the script handler)?

    __asm{ "MOV r0,24" }
    ^ Or something to that effect.
     


    __asm{ "MOV r0,24" }
    ^ Or something to that effect.

    Why not use function pointer to call other code?
    ie.

    void (*func) ();
    func = 0x8romoffset;
    func();
     
    SWI - Soft Ware Interrupt. It does things like force the screen to redraw, etc. There's not really a function for it.
     
    Yes!! Someone who uses Linux!

    Uh, I mean, great tutorial. Seems a bit complicated though, I think I'll stick to ASM. Low-Level is kinda fun, because it poses more of a challenge. Plus you can't hack existing routines with C Code too easily, if at all.

    Maybe once I up my C skills, I'll look back into this. :)
     

    SWI - Soft Ware Interrupt. It does things like force the screen to redraw, etc. There's not really a function for it.

    hmmm I, think libgba had function for raising SWI so it could be possible with a bit of work, i might look in it.

    Yes!! Someone who uses Linux!

    Uh, I mean, great tutorial. Seems a bit complicated though, I think I'll stick to ASM. Low-Level is kinda fun, because it poses more of a challenge. Plus you can't hack existing routines with C Code too easily, if at all.

    Maybe once I up my C skills, I'll look back into this. :)

    Thank you :)
    OT: I use linux and windows and my recent windows reinstalling screwed linux so I need to reinstall it >.<
     
    Last edited:
    hmmm I, think libgba had function for raising SWI so it could be possible with a bit of work, i might look in it.



    Thank you :)
    OT: I use linux and windows and my recent windows reinstalling screwed linux so I need to reinstall it >.<

    I've actually been wondering -- do you know of any libgba documentation? I looked on devkitpro's site, and couldn't find any. :\
     


    I've actually been wondering -- do you know of any libgba documentation? I looked on devkitpro's site, and couldn't find any. :\
    I have also bee looking trought interwebz and didnt find any, maybe you could try trial and error with examples?
     
    I guess. haha. Not much else choice we have, eh? XD
     
    -mthumb means create THUMB code, not ARM code, which is probably what you want
    -mthumb-interwork allows you to create calls between ARM and THUMB code
    Just sayin'

    Also, to account for the size of the assembled C code, that can be fixed with a simple optimisation switch (-O).
    -O0 : no optimisation
    -O1: optimise
    -O2: optimise more
    -O3: optimise even more
    -Os: optimise for size (This is basically -O2 , but only optimises when it can do so without increasing the physical code size)​
    For example, when I compiled and assembled your "GetPokemonLevel.c" the way you did, the output size was 44 bytes. With an optimisation switch the size is 20 bytes.
    Also, please use include guards on your header, it may help us if we use more than one C file...
     
    Last edited:
    -mthumb means create THUMB code, not ARM code, which is probably what you want
    -mthumb-interwork allows you to create calls between ARM and THUMB code
    Just sayin'

    I know what they do, also assembler batch file uses these so I left them in, just for compatibilty with batch file.


    Also, to account for the size of the assembled C code, that can be fixed with a simple optimisation switch (-O).
    -O0 : no optimisation
    -O1: optimise
    -O2: optimise more
    -O3: optimise even more
    -Os: optimise for size (This is basically -O2 , but only optimises when it can do so without increasing the physical code size)​
    For example, when I compiled and assembled your "GetPokemonLevel.c" the way you did, the output size was 44 bytes. With an optimisation switch the size is 20 bytes.

    Well I was thinking about this too and looked into arm-eabi-gcc --help if they are present and I didn't saw them here so I thought they aren't supported.


    Also, please use include guards on your header, it may help us if we use more than one C file...

    I forgot about this xP

    Anyway thanks for noticing, I will edit tutorial accordingly.

    EDIT: Updated first post.
     
    Last edited:
    hey, nice tutorial!

    I'm REALLY REALLY sorry for updating the thread after such HUGE amount of time, but, you know, bookmarks naturally age ; ) :3c

    May I ask some questions, please?
    I was wondering if I could use a library, so downloaded libtonc, but couldn't manage to get it working, because it has to be linked, something you definitely do only when compiling a game from scratch.
    I tried all sort of things, but didn't work for me.
    Do you know a good GBA library to just #include in my C file?
    yes, I know that including everything in a single file is bad, but I'm allergic to makefiles and such things :p
    If the C file remains short, everything works fine, but I remain limited to very poor things. If I begin to put toghether some H and C files made by me (to try to supply a library) in the main one, it is really probable that everything messes up and the ROM crashes upon calling the routine.
    Moreover, the GCC and tge AS are tricking me in some strange ways. >:)

    thanks in advance, goodbye ^^
     
    There unfortunately isn't (at least I assume so) a GBA library that will give you every definition in a single header file, because it's a huge mess design-wise. As long as you are using one source file only, you should also not be bothering with makefiles.

    Code:
    $ gcc file.c -mthumb -Ipath/to/library/headers -o file.o
    $ gcc file.o -o asm.bin
     
    There unfortunately isn't (at least I assume so) a GBA library that will give you every definition in a single header file, because it's a huge mess design-wise.

    so do you know how to link "professional" libraries in our simple C files?

    Code:
    $ gcc file.c -mthumb -Ipath/to/library/headers -o file.o
    $ gcc file.o -o asm.bin
    i personally use

    Code:
    gcc -S -mthumb -mthumb-interwork -O1 -Wall
    and then hackmew's thumb batch.
    so, you enlighted me! could I "skip a step" compiling with gcc? it would save me a lot of time, because the assembly output is always really messy, in comparison to what an human would be, so I developed some tools in java to try to patch things up. and I should really try doing so, maybe I will not experiment some of the strange issues GCC and AS are giving me, and MAYBE my naive method of including everything will work! (I doubt, maybe it's my code wrong. may I upload it to ask you to look at it, please?)

    thanks, now I will let this topic rest in peace and continue posting on another recent one wich I missed out.

    really thankyou, good morning.
     
    Back
    Top