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
|
Credits to Petuh for this tutorial, and for Lunos for finding an archived version. This feature may also be directly pulled from Pyredrid's [mgba_printf](https://github.com/Pyredrid/pokeemerald/tree/mgba_printf) branch.
TODO: rewrite to be in line with the standards of other tutorials. As of now, this is (almost) identical to Petuh's original post.
This is for having an mGBA Debug printf function.
## 1. Getting the printf and mgba Files
First, download [this](https://cdn.discordapp.com/attachments/744575017051881492/744961612200542288/mGBA_Debug_printf.zip). Place the .c files in `src/` and the .h files in `include/`
```c
src/mgba.o(.text);
src/printf.o(.text);
```
and
```c
src/printf.o(.rodata);
```
to `ld_script.txt`.
## 2. Enabling MGBA logging
In `src/main.c` add `#include "mgba.h"`
Change the starting function to start mgba logging:
```diff
void AgbMain()
{
...
ResetBgs();
SetDefaultFontsPointer();
+ mgba_open();
InitHeap(gHeap, HEAP_SIZE);
...
```
## 3.
Add `#include "printf.h"` to `src/libisagbprn.c`.
```diff
...
#define AGB_PRINT_FLUSH_ADDR 0x9FE209D
-#define AGB_PRINT_STRUCT_ADDR 0x9FE20F8
+#define AGB_PRINT_STRUCT_ADDR (char*) 0x9FE20F8
#define AGB_PRINT_PROTECT_ADDR 0x9FE2FFE
#define WSCNT_DATA (WAITCNT_PHI_OUT_16MHZ | WAITCNT_WS0_S_2 | WAITCNT_WS0_N_4)
...
```
Then, search and replace `vsprintf` with `vsnprintf` in the file.
## 4. Printing ASCII Strings
In `gflib/string_util.h`, add `char *ConvertToAscii(const u8 *str);`.
Add `#include "malloc.h"` to `gflib/string_util.c`.
Then, add the following C function to `gflib/string_util.c`:
```c
char *ConvertToAscii(const u8 *str)
{
s32 i;
char * textBuffer = malloc(128);
for (i = 0; *str != EOS; i++, str++)
{
char modifiedCode = '?';
if(*str >= CHAR_a && *str <= CHAR_z)
{
modifiedCode = *str-(CHAR_A-'a'); // lower-case characters
}
else if(*str >= CHAR_A && *str <= CHAR_Z)
{
modifiedCode = *str-(CHAR_A-'A'); // upper-case characters
}
else if (*str >= CHAR_0 && *str <= CHAR_9)
{
modifiedCode = *str-(CHAR_0-'0'); // numbers
}
else if (*str == CHAR_SPACE)
{
modifiedCode = ' '; // space
}
textBuffer[i] = modifiedCode;
}
textBuffer[i] = 0;
return textBuffer;
}
```
## 5. Testing the code
Make sure you include
```c
#include "printf.h"
#include "mgba.h"
```
in whatever file you test with.
You should aldo include `#include "../gflib/string_util.h"`, if you want to print ASCII strings.
Let's test this in `wild_encounter.c`, add the following code:
```diff
static void CreateWildMon(u16 species, u8 level)
{
...
+ mgba_printf(MGBA_LOG_DEBUG, "%d", species);
CreateMonWithNature(&gEnemyParty[0], species, level, 32, PickWildMonNature());
}
```
This will print out the species number for the wild Pokémon about to be encountered.
If you want to print the species name, add `#include "data.h"` and instead use
`mgba_printf(MGBA_LOG_DEBUG, "%s", ConvertToAscii(gSpeciesNames[species]));`.
That is all.
TIP: Don't include this in release builds, because `printf` takes a lot of cycles.
|