Register - Login
Views: 95465109
Main - Memberlist - Active users - Calendar - Wiki - IRC Chat - Online users
Ranks - Rules/FAQ - Stats - Latest Posts - Color Chart - Smilies
10-20-18 01:47:24 PM

Jul - SM64 Hacking (Archive) - Correctly Compilling C For Usage With N64 New poll - New thread - New reply
Pages: 1 2Next newer thread | Next older thread
Casis777
User
Level: 10


Posts: 2/19
EXP: 4195
For next: 219

Since: 10-10-11


Since last post: 5.0 years
Last activity: 5.0 years

Posted on 01-17-12 07:29:58 PM (last edited by Casis777 at 01-17-12 08:41 PM) Link | Quote
There seems to be a problem with the way the mips-gcc compiler is compiling my code (and others) into MIPS assembly to be translated into a gameshark code for the nemu emulator. Here is what I have done...

*I'm running on Windows Vista 32-bit
*I'm running the MinGW compiler
*I have MSYS and all Paths correctly configured
*I'm using the MIPS-GCC compiler and other tools provided in this tutorial: link
*I have libraries from that google codesite downloaded
*I am modifying/using this linker script just to generate hacks: link
*I am modifying/using this linker script to generate overlays (or actors): link

Now, I have programmed a few basic hacks but none of them seem to be compiling correctly. Finally, I decided to try and compile a hack I know should run. Of course... it failed. Then, I decided to take spinout's bombarrow hack and compile that. Now, he had a compiled GS code of what it should look like, so when I compared the GS code that I had with his, I had finally come to the conclusion that it was a problem with the compiler or the linker (probably the compiler). Frauber, I know that you've compiled hacks made in C before, could you possibly know what the problem is?

By the way, here is the GS code I got after compiling the bombarrows hack and here is spinout's original GS code... As you can see, they begin to differ around the first couple of GS code lines. This leads me to believe it's either the compiler's, linker's, or objcopy's fault. Also, here is the source code for spinout's bombarrows for others to compile.

Should I try using this linker script instead? It seems to be exactly the same as the original linker script I was using except that it has fewer comments. And what about trying a different compiler? I found another mips-gcc compiler that was packaged with Galatea.

I know that this topic has to do with a OoT debug hack, but I also wish to know what is the problem so that way I may generate hacks for SM64

Lyskar
12210
-The Chaos within trumps the Chaos without-
Level: 185


Posts: 10793/12211
EXP: 86688050
For next: 1071685

Since: 07-03-07

From: 52-2-88-7

Since last post: 3.0 years
Last activity: 3.0 years

Posted on 01-17-12 08:02:11 PM Link | Quote
Normally I would trash this, but there is nowhere else in this forum to put this information, and it is something legitimately interesting, so I shall let it stay. But like all exceptions, this means this will be the topic for compiling C for use with N64--I won't be accepting an armada of other similar topics or just let any topic in just because of this one.
Casis777
User
Level: 10


Posts: 3/19
EXP: 4195
For next: 219

Since: 10-10-11


Since last post: 5.0 years
Last activity: 5.0 years

Posted on 01-17-12 08:47:22 PM (last edited by Casis777 at 01-17-12 08:47 PM) Link | Quote
Thanks, Metal_Man. I wasn't sure if I would be allowed to post this thread or not because of the following rule read in the sticky: SM64 Forum Info- Don't Make New Topics--



Do not post a thread unless it is one of the following:

A well-phrased Question not answered or addressed in another topics

messiaen
Catgirl
Level: 65


Posts: 1037/1085
EXP: 2256656
For next: 78972

Since: 11-20-07


Since last post: 4.0 years
Last activity: 3.0 years

Posted on 01-18-12 02:37:29 AM Link | Quote
I feel like helping since there's great value in learning how to set this up. If you got anything to compile, then you are on the right path.

Dunno about spinout stuff, but this file should help you. The subdirectory labelled 'c' contains multiple examples of SM64 hacks done in C. Use mario64.c as reference for Makefile and linker script. If I recall correctly, some of the CFLAGS have been changed from the example hacks.

A few important points:

1) Hook.txt is the hack entry-point. It replaces something useless in Mario's behavior with JAL 0x80400000, so your code will ONLY BE CALLED when Mario can be controlled (but not during menus, cutscenes and pauses). This is a quick cheapo to test hacks easily as Nemu64 cheats. If you want to inject your hack into the ROM, you'll need a few more steps, I should probably write a guide on that (fortunately its simpler with ROMs already modified by the OBJ Importer).

2) The linker script (mario64.x) shouldn´t be modified, however there's something very important to take in account, , which is the start address definied by ".". In this case, it's 0x80400000, which is fine for Gameshark test hacks, however when inserting to the ROM this has to match the actual RAM destination. More on this on a later post.


SECTIONS {
. = 0x80400000 ;


3) Remember ALL your source files must begin with this:

asm volatile("la $gp, _gp");

This ensures $GP is a pointer to the .data section containing the variables used by your code, otherwise the first load/store operation will result in an exception and crash the game.

Hope this heps, feel free to ask any questions.
Casis777
User
Level: 10


Posts: 4/19
EXP: 4195
For next: 219

Since: 10-10-11


Since last post: 5.0 years
Last activity: 5.0 years

Posted on 01-18-12 07:01:51 PM (last edited by Casis777 at 01-18-12 07:02 PM) Link | Quote
I appreciate your help, messian, but I already know how to correctly use the linker scripts and how hooks work. Although, I haven't tried adding asm volatile("la $gp, _gp"); to the code yet...

Perhaps I could try tinkering around with the GCC optimization levels within the CFLAGS to see if I get different outputs. Using the -mabi=32 argument seems to help the compiled GS code a little.

Maybe it's because I'm running Vista 32-bit? Everyone seems to frown upon programming in a vista environment
messiaen
Catgirl
Level: 65


Posts: 1038/1085
EXP: 2256656
For next: 78972

Since: 11-20-07


Since last post: 4.0 years
Last activity: 3.0 years

Posted on 01-18-12 08:37:00 PM Link | Quote
Use objdump -d file.elf to get a disassembly of your compiled code, then it should be easier to figure out what's different.
Casis777
User
Level: 10


Posts: 7/19
EXP: 4195
For next: 219

Since: 10-10-11


Since last post: 5.0 years
Last activity: 5.0 years

Posted on 02-13-12 11:26:41 PM (last edited by Casis777 at 02-13-12 11:30 PM) Link | Quote
I just deleted my previous reply because I fixed what was the main problem with the hack-- the makefile. Thanks for the examples and help, messian! I'll try making a few example hacks for SM64 now that I've done what I wanted with OoT
messiaen
Catgirl
Level: 65


Posts: 1063/1085
EXP: 2256656
For next: 78972

Since: 11-20-07


Since last post: 4.0 years
Last activity: 3.0 years

Posted on 02-23-12 03:18:43 PM Link | Quote
Sent by Cajetan in a PM. Please post question in the appropriate threads, not in PMs, geeky stuff is the purpose of this forum.

Originally posted by Cajetan
Okay another question(sorry for annoying you, but there isn't any other who knows stuff like that): How do i setup all stuff correctly?

Yes, i did following:
Installed MinGW, MSYS everything perfectly. I also got z64actor and z64teleport successfully compiled. Now i wanted to make my OWN code for SM64. Now comes the hard part for me: How do i setup everything correctly. In my folder is following:

m64helloworld.c
mario64.h
mario64.x
hook
makefile

Now with MSYS i did cd to that folder. Now i typed in: make
i get following errors:

"make: *** No rule to make target `mario64.o', needed by `mario64.elf'. Stop."

So, but i know mario64.o and mario64.elf get generated... i can't have them, because they must be later generated... So what am i doing wrong here?


I don't blame you, makefiles are very confusing.

Your makefile PRJNAME is probably mario64, so it expects your source file to be called mario64.c, unless you specify otherwise.
Cajetan
Member
Level: 20


Posts: 29/86
EXP: 40915
For next: 1524

Since: 08-06-11

From: Binary

Since last post: 3.0 years
Last activity: 1.0 years

Posted on 02-24-12 07:45:44 AM Link | Quote
Originally posted by messiaen
Sent by Cajetan in a PM. Please post question in the appropriate threads, not in PMs, geeky stuff is the purpose of this forum.

Originally posted by Cajetan
Okay another question(sorry for annoying you, but there isn't any other who knows stuff like that): How do i setup all stuff correctly?

Yes, i did following:
Installed MinGW, MSYS everything perfectly. I also got z64actor and z64teleport successfully compiled. Now i wanted to make my OWN code for SM64. Now comes the hard part for me: How do i setup everything correctly. In my folder is following:

m64helloworld.c
mario64.h
mario64.x
hook
makefile

Now with MSYS i did cd to that folder. Now i typed in: make
i get following errors:

"make: *** No rule to make target `mario64.o', needed by `mario64.elf'. Stop."

So, but i know mario64.o and mario64.elf get generated... i can't have them, because they must be later generated... So what am i doing wrong here?


I don't blame you, makefiles are very confusing.

Your makefile PRJNAME is probably mario64, so it expects your source file to be called mario64.c, unless you specify otherwise.


Well, it does compile now, but again a lot of errors:

$ make hlw
mips64-gcc -std=gnu99 -nodefaultlibs -march=vr4300 -mtune=vr4300 -mabi=32 -I. -I../include -fno-builtin -o3 -c -o hlw.o hlw.c
In file included from hlw.c:1:
explode.h:43: error: expected ')' before 'flags'
explode.h:56: error: expected ')' before '*' token
explode.h:57: error: expected ')' before '*' token
explode.h:58: error: expected ')' before '*' token
explode.h:59: error: expected ')' before '*' token
explode.h:60: error: expected ')' before 'obj_pointer'
explode.h:61: error: expected ')' before 'object1'
explode.h:62: error: expected ')' before 'dst'
explode.h:63: error: expected ')' before 'obj_ptr'
explode.h:64: error: expected ')' before 'argument'
explode.h:69: error: expected ')' before 'obj_pointer'
explode.h:70: error: expected ')' before 'model_ID'
explode.h:71: error: expected ')' before 'animation_index'
explode.h:72: error: expected ')' before 'argument'
explode.h:73: error: expected ')' before 'obj_pointer'
explode.h:74: error: expected ')' before 'behavior_segmented_pointer'
explode.h:75: error: expected ')' before 'obj_pointer'
explode.h:76: error: expected ')' before 'obj_pointer'
explode.h:79: error: expected ')' before 'mario_struct_pointer'
explode.h:81: error: expected ')' before '*' token
explode.h:85: error: expected ')' before 'layer'
explode.h:86: error: expected ')' before '*' token
explode.h:89: error: expected ')' before 'msg_ID'
explode.h:90: error: expected ')' before 'x_pos'
explode.h:91: error: expected ')' before 'x'
explode.h:92: error: expected ')' before 'arg'
explode.h:95: error: expected ')' before 'x'
explode.h:96: error: expected ')' before 'x'
explode.h:97: error: expected ')' before 'x'
explode.h:100: error: expected ')' before 'segmented_pointer'
explode.h:103: error: expected '=', ',', ';', 'asm' or '__attribute__' before '*' token
explode.h:112: error: expected specifier-qualifier-list before 'u16'
explode.h:119: error: expected specifier-qualifier-list before 'u32'
explode.h:125: error: expected declaration specifiers or '...' before 'u16'
explode.h:131: error: expected specifier-qualifier-list before 'u32'
explode.h:154: error: expected specifier-qualifier-list before 'u32'
explode.h:172: error: expected specifier-qualifier-list before 'u16'
explode.h:190: error: expected specifier-qualifier-list before 's16'
explode.h:203: error: expected specifier-qualifier-list before 'u32'
explode.h:220: error: expected specifier-qualifier-list before 's16'
explode.h:243: error: expected specifier-qualifier-list before 'u16'
explode.h:415: error: expected specifier-qualifier-list before 'u32'
hlw.c: In function '_start':
hlw.c:5: warning: implicit declaration of function 'PrintXY'
make: *** [hlw.o] Error 1"



I used now the updated explode.h, then i tried using the outdated(mario64.h) and get the same errors like up there. Is something wrong with the compiler?
messiaen
Catgirl
Level: 65


Posts: 1065/1085
EXP: 2256656
For next: 78972

Since: 11-20-07


Since last post: 4.0 years
Last activity: 3.0 years

Posted on 02-24-12 09:47:02 AM Link | Quote
Seems like you aren't including n64.h.

Use #include before anything else in your main .c file.

If that doesn't work, copy n64.h to the local directory and use #include "n64.h" instead.
Cajetan
Member
Level: 20


Posts: 30/86
EXP: 40915
For next: 1524

Since: 08-06-11

From: Binary

Since last post: 3.0 years
Last activity: 1.0 years

Posted on 02-24-12 09:51:43 AM (last edited by Cajetan at 02-24-12 09:59 AM) Link | Quote
Ok, the last help i need. Ok everything compiles now, but i get now a very short error:



$ make
mips64-gcc -std=gnu99 -nodefaultlibs -march=vr4300 -mtune=vr4300 -mabi=32 -I. -I../include -fno-builtin -o3 -c -o hlw.o hlw.c
mips64-ld -o hlw.elf hlw.o -L. -T hlw.x
d:\m64hack\c\n64tree-win32\root\bin\mips64-ld.exe:hlw.x:44: parse error
make: *** [hlw.elf] Error 1



I think that would be all then and compilation can be successful.

If the compilation goes successful next time, then i will write a tutorial how to get a successful compilation with your own code.

BTW, Hook is the hack entry point, so where do i actually hook my helloworld.c in?
messiaen
Catgirl
Level: 65


Posts: 1066/1085
EXP: 2256656
For next: 78972

Since: 11-20-07


Since last post: 4.0 years
Last activity: 3.0 years

Posted on 02-24-12 12:10:13 PM Link | Quote
Can't help if you don't post the contents of your hlw.x file. Make sure it's exactly the same as mario64.x and it will work fine.

The "hook" is what CALLS your code. In the examples provided, its called from Mario's Behavior, so whenever Mario is playing, your function (which by default is at 0x80400000) will be called.

The hook file is simply a Gameshark code that paches

JAL 0x80400000
NOP

to Mario Behavior, which is equivalente to a YourHack(); function call in C.

"Injecting" the hack into the ROM without using Gameshark codes is a bit more complicated.
Cajetan
Member
Level: 20


Posts: 31/86
EXP: 40915
For next: 1524

Since: 08-06-11

From: Binary

Since last post: 3.0 years
Last activity: 1.0 years

Posted on 02-24-12 01:04:53 PM (last edited by Cajetan at 02-24-12 01:13 PM) Link | Quote
Originally posted by messiaen
Can't help if you don't post the contents of your hlw.x file. Make sure it's exactly the same as mario64.x and it will work fine.

The "hook" is what CALLS your code. In the examples provided, its called from Mario's Behavior, so whenever Mario is playing, your function (which by default is at 0x80400000) will be called.

The hook file is simply a Gameshark code that paches

JAL 0x80400000
NOP

to Mario Behavior, which is equivalente to a YourHack(); function call in C.

"Injecting" the hack into the ROM without using Gameshark codes is a bit more complicated.


Ah Thank you, i found it out. It looks like i got a false mario64.x file. It compiles now everything. Thanks for your help, im going to write a tutorial how to do compile own codes without problems.
messiaen
Catgirl
Level: 65


Posts: 1068/1085
EXP: 2256656
For next: 78972

Since: 11-20-07


Since last post: 4.0 years
Last activity: 3.0 years

Posted on 02-24-12 01:40:34 PM Link | Quote
It would be nice if you could do it really step-by-step, with lots of pictures from all the steps (installing MSYS/MinGW, downloading the files, setting things up, etc).

I'm doing some modifications in the ROM Layout for the next version of the importer, so injecting new code will be a bit easier.
Vinnyboiler
Catgirl
Level: 63


Posts: 888/1028
EXP: 2071491
For next: 25661

Since: 12-27-07

From: London, England

Since last post: 1.0 years
Last activity: 223 days

Posted on 02-24-12 01:42:34 PM Link | Quote
Originally posted by Cajetan
Ah Thank you, i found it out. It looks like i got a false mario64.x file. It compiles now everything. Thanks for your help, im going to write a tutorial how to do compile own codes without problems.


Oh a tutorial actually sounds quite nice, I would absolute love it if you do.
Cajetan
Member
Level: 20


Posts: 32/86
EXP: 40915
For next: 1524

Since: 08-06-11

From: Binary

Since last post: 3.0 years
Last activity: 1.0 years

Posted on 02-24-12 02:43:08 PM Link | Quote
Originally posted by vinnyboiler
Originally posted by Cajetan
Ah Thank you, i found it out. It looks like i got a false mario64.x file. It compiles now everything. Thanks for your help, im going to write a tutorial how to do compile own codes without problems.


Oh a tutorial actually sounds quite nice, I would absolute love it if you do.


Yes, i will do. By the way, this is the successful Result i got from compiling:

Cajetan
Member
Level: 20


Posts: 33/86
EXP: 40915
For next: 1524

Since: 08-06-11

From: Binary

Since last post: 3.0 years
Last activity: 1.0 years

Posted on 02-24-12 05:55:16 PM (last edited by Cajetan at 02-24-12 05:55 PM) Link | Quote
So i wrote the tutorial... I don't know if you like it, but if anything is missing please report it:

http://sites.google.com/site/cajetan64/home/coding-in-n64-with-c
messiaen
Catgirl
Level: 65


Posts: 1070/1085
EXP: 2256656
For next: 78972

Since: 11-20-07


Since last post: 4.0 years
Last activity: 3.0 years

Posted on 02-25-12 12:45:24 PM (last edited by messiaen at 02-25-12 12:46 PM) Link | Quote
Please provide a link to the GCC N64 Toolchain setup tutorial". By the way, that tutorial was made by ZZT32.

Animations.h isn't needed, unless someone completes that with descriptions of all object animations.

Some possible tutorials you or someone else could try writing, or rather, guides for what you can learn from the examples. I strongly recommend reading all the basic tutorials on cprogramming.com. It's essential understanding struct pointers AND pointers-to-pointers syntax (you'll be using this to acess most structs).

1) Using pointers to alter things in RAM
- Reading/writing 8, 16 and 32-bit values (types u8, u16, u32 and floats)
- Converting simple Gameshark codes to C code

2) Reading data from the game: current level, number of stars
- Using conditionals to enhance simpler codes
- Making your code execute only on a specific level, or on a minimum number of stars

3) Acessing the Mario struct
- Pointer-to-pointer struct syntax and acessing members, ie (*Struct)->member
- Checking current and previous action
- Reading JoyPad input using & and == operators.

4) Detecting current collision type Mario is stepping
- Adding action to existing collision types
- Creating custom collision types

5) Acessing the Object Struct
- Basic variables: action, position, collision spheres & interaction, x/y/z speed, distance from Mario or from other object
- Deactivating (killing) objects
- Changing action based on object timers
- Changing current animation
- Checking behaviors
- Spawning child objects
- Segmented vs RAM pointers (SegmentedToVirtual function)
- Iterating through the object linked list to find an specific object.

6) DMA
- Copying data from the ROM to RAM
Cajetan
Member
Level: 20


Posts: 34/86
EXP: 40915
For next: 1524

Since: 08-06-11

From: Binary

Since last post: 3.0 years
Last activity: 1.0 years

Posted on 02-26-12 08:31:23 AM Link | Quote
Thanks Messiaen. I will provide the link to the N64Toolchain and the restly things will be added too.
I will also add some exercises like spawn a child object or make a code that prints Hello World on the screen... I think that should help understanding how Super Mario 64 actually works and motivates others to write tutorials. I also will provide links to learn the C syntax.. it must be destiny... because i actually wanted to post the same link (http://www.cprogramming.com). It's a very good site and i learned from there all my basic knowledge of the C Syntax. It's great written and very understandable.

I hope you, me or someone else could update the header files and share it here.

ShenoxVII

(in all seriousness, you're a prick)
Level: 23


Posts: 69/111
EXP: 63333
For next: 4390

Since: 10-09-10


Since last post: 5.0 years
Last activity: 5.0 years

Posted on 02-29-12 06:18:27 PM Link | Quote
Lol hey tarek, haven't talked to you since I met you randomly on kaillera like a couple of years ago, and before that when we were hacking that kiddy game. If you are the person i think you are, German.

Btw, NICE tutorial, I love it thanks for doing this its a huge break through for progress in the sm64 hacking world, hopefully this will gain popularity of more complex hacks and now dealing with actual languages with the rom, so we can build off of it.

This, is great.
Pages: 1 2Next newer thread | Next older thread
Jul - SM64 Hacking (Archive) - Correctly Compilling C For Usage With N64 New poll - New thread - New reply




Rusted Logic

Acmlmboard - commit 5d36857 [2018-03-03]
©2000-2018 Acmlm, Xkeeper, Inuyasha, et al.

29 database queries, 14 query cache hits.
Query execution time: 0.184481 seconds
Script execution time: 0.037982 seconds
Total render time: 0.222463 seconds
Memory used: 786432