summaryrefslogtreecommitdiff
path: root/CONTRIBUTING.md
blob: d43217a5f62c0eacb8836afab48467449de461c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# Decompiling

Code starts out in `asm/`. When decompiled to C, it goes into `src/`. The goal is to decompile all the code.

Some of the code in `asm/` is handwritten assembly. It can't and shouldn't be decompiled. It's already commented, so there's no further work to do on these files.
Check `linker.ld` and ignore anything under the "handwritten assembly" section.

The rest of the `.s` files in `asm/` are fair game.

The basic decompilation process is:
* Choose a file in `asm/`, i.e. `asm/x.s`. Create a C file called `src/x.c`.
* Translate the first function in `asm/x.s` to C in `src/x.c`.
* `make`, and tweak the function until it matches.
* Clean up the code and comment.
* Repeat for each function until `asm/x.s` is empty.


# For example, let's decompile `asm/evilSpirit.s`.


## 1. Create `src/evilSpirit.c`

```c
#include "global.h"
```

`global.h` contains typedefs for GBA programming and more.
It must be the first include in the file. Other includes will assume you have included it.


## 2. Include it in the rom

Include `src/evilSpirit.c` in the rom by adding `src/evilSpirit.o` to `linker.ld`:
```diff
	asm/room.o(.text);
	asm/code_08080974.o(.text);
+	src/evilSpirit.o(.text);
	asm/evilSpirit.o(.text);
	asm/houseDoorExterior.o(.text);

```
Do not remove `asm/evilSpirit.o(.text)`. We want both `src/evilSpirit.c` and `asm/evilSpirit.s` in the rom.


## 3. Translate the function to C

Take the first function in `asm/evilSpirit.s`. Either comment it out or remove it, whichever is easier.

```asm
	thumb_func_start sub_08086284
sub_08086284: @ 0x08086284
	push {r4, lr}
	adds r4, r0, #0
	ldr r1, _080862B4 @ =gUnk_08120668
	ldrb r0, [r4, #0xc]
	lsls r0, r0, #2
	adds r0, r0, r1
	ldr r1, [r0]
	adds r0, r4, #0
	bl _call_via_r1
	adds r1, r4, #0
	adds r1, #0x41
	movs r0, #0
	strb r0, [r1]
	adds r0, r4, #0
	adds r0, #0x76
	ldrh r1, [r0]
	adds r0, #4
	ldrh r2, [r0]
	adds r0, r4, #0
	movs r3, #0
	bl SetAffineInfo
	pop {r4, pc}
	.align 2, 0
_080862B4: .4byte gUnk_08120668
```
---

Then, start translating the code to `src/evilSpirit.c`, bit by bit:

```asm
	push {r4, lr}
	adds r4, r0, #0
```
```c
	void sub_08086284(u8* r0) {
```
---
```asm
        ldr     r1, _080862B4 @ =gUnk_08120668
        ldrb    r0, [r4, #0xc]
        lsl     r0, r0, #0x2
        add     r0, r0, r1
        ldr     r1, [r0]
        add     r0, r4, #0
        bl      _call_via_r1
```
```c
	gUnk_08120668[*(u8 *)(r0 + 0xc)](r0);
```
---

---
```asm
        add     r1, r4, #0
        add     r1, r1, #0x41
        mov     r0, #0
        strb    r0, [r1]
```
```c
    *(u8 *)(r0 + 0x41) = 0;
```
---
```asm
        add     r0, r4, #0
        add     r0, r0, #0x76
        ldrh    r1, [r0]
        add     r0, r0, #0x4
        ldrh    r2, [r0]
        add     r0, r4, #0
        mov     r3, #0
        bl      SetAffineInfo
```
```c
	SetAffineInfo(r0, *(u16 *)(r0 + 0x76), *(u16 *)(r0 + 0x7a), 0);
```
---
```asm
        pop     {r4, pc}
```
```c
	return;
```
The type signature of the function depends on the return type. Return values are stored in r0,
so pay attention to how the assembly treats this register toward the end of the function.
ex:
* `add r0, r4, #0`

  `pop {r4, pc}`

The compiler chose to move a value into r0 here; the most likely explanation is that it's returning something.

You will need to look at the caller and the function prologue to determine the exact type if not void.

Since it only used `pop {r4, pc}`, it's probably `void`.

---

Putting it all together, we get:
```c
void sub_08086284(u8 *r0) {
    gUnk_08120668[*(u8 *)(r0 + 0xc)](r0);
    *(u8 *)(r0 + 0x41) = 0;
    SetAffineInfo(r0, *(u16 *)(r0 + 0x76), *(u16 *)(r0 + 0x7a), 0);
    return;
}
```


## 4. Simplify and document

This line doesn't look quite right.

```c
	gUnk_08120668[*(u8 *)(r0 + 0xc)](r0);
```

What is `r0`? Since this function corresponds to an entity, we should first try to assign r0 to an `Entity` struct.
You can find out what this is with `git grep`:

```sh
git grep "Entity" include/
```
```grep
include/entity.h:typedef struct Entity
```

So it's a struct called `Entity`. Let's look in `entity.h`:

```c
typedef struct Entity_ {
    /*0x00*/ struct Entity_* prev;
    /*0x04*/ struct Entity_* next;
    /*0x08*/ u8 kind;
    /*0x09*/ u8 id;
    /*0x0a*/ u8 type;
    /*0x0b*/ u8 type2;
    /*0x0c*/ u8 action;
    /*0x0d*/ u8 subAction;
    ...
} Entity;
```
---

What's the 12th byte in this struct?
```c
    /*0x00*/ struct Entity_* prev;
    /*0x04*/ struct Entity_* next;
    ...
    /*0x0c*/ u8 action; <-
```

---

The 12th byte belongs to `action`. We can substitute this in by replacing r0's parameter type and adding in the member names.

```c
void sub_08086284(Entity *r0) {
    gUnk_08120668[r0->action](r0);
```

Much better.

---

```c
void sub_08086284(Entity *r0) {
    gUnk_08120668[r0->action](r0);
    r0->bitfield = 0;
    SetAffineInfo(r0, r0->field_0x76.HWORD, r0->field_0x7a.HWORD, 0);
    return;
}
```

The fields at the end of of `Entity` are general purpose. For this reason the fields are defined as unions so the proper data size may be loaded.
This isn't pretty, but right now we are just concerned with making the function match. Later on we can define these entity-specific fields.

## 5. Build

```sh
make
```
```
src/evilSpirit.c: In function `sub_08086284':
src/evilSpirit.c:4: syntax error before `*'
src/evilSpirit.c:5: `gUnk_08120668' undeclared (first use in this function)
src/evilSpirit.c:5: (Each undeclared identifier is reported only once for each function it appears in.)
src/evilSpirit.c:7: warning: implicit declaration of function `SetAffineInfo'
```

We got some errors. We need to tell the compiler what `gUnk_08120668`, `Entity`, and `SetAffineInfo` are.

We know `r0` is an `Entity`, which is from `entity.h`. We can declare this above the function:
```c
#include "entity.h"
```
What about `gUnk_08120668` and `SetAffineInfo`?
```c
extern void SetAffineInfo();
extern void (*gUnk_08120668[])(Entity *);
```
Now the compiler will look outside of this file for both of these. We don't have to set the size of `gUnk_08120668`, a function array, since it's size is irrelevant for now.

---

Now our file looks like this:
```c
#include "global.h"
#include "entity.h"

extern void SetAffineInfo();
extern void (*gUnk_08120668[])(Entity *);

void sub_08086284(Entity *r0) {
    gUnk_08120668[r0->action](r0);
    r0->bitfield = 0;
    SetAffineInfo(r0, r0->field_0x76.HWORD, r0->field_0x7a.HWORD, 0);
    return;
}
```

---

Build again, and we get:
```sh
make
```
```sha1sum
tmc.gba: OK
```

This means the function matches. Congratulations!

---

If it doesn't match, you will get:
```sha1sum
tmc.gba: FAILED
sha1sum: WARNING: 1 computed checksum did NOT match
```

---

If you forgot to remove the function from `asm/evilSpirit.s`, you will get this error:
```gcc
asm/evilSpirit.o: In function `sub_08086284':
(.text+0x0): multiple definition of `sub_08086284'
src/evilSpirit.o:(.text+0x0): first defined here
```