summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorwalkingeyerobot <mitch@thefoley.net>2020-09-03 23:30:34 -0400
committerwalkingeyerobot <mitch@thefoley.net>2020-09-03 23:30:34 -0400
commit3f9fe0d5dc14a1e901f213a43908aba1e09be77d (patch)
tree9a6a13df93d3892b3f8abcc9b970a44c910a7836
parentc4b5c0609cfe9f85c4c9039df8992920ff206dfb (diff)
clarifying some steps
-rw-r--r--printf-in-mGBA.md63
1 files changed, 41 insertions, 22 deletions
diff --git a/printf-in-mGBA.md b/printf-in-mGBA.md
index e51fa91..586f2bb 100644
--- a/printf-in-mGBA.md
+++ b/printf-in-mGBA.md
@@ -2,23 +2,25 @@ Credits to Petuh for this tutorial, and for Lunos for finding an archived versio
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.
+This is for having an mGBA Debug printf function. Note that calling printf takes a lot of cycles and **should NOT be included in release builds**.
## 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/`
+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/`. These files contain an implementation of printf and functions for communicating with mGBA.
+
+## 2. Add the new files to the build.
+We need tell the linker about the new files we just added. Open `ld_script.txt`. Find the section that begins with `.text :`. At the end of this section, add the following:
```c
src/mgba.o(.text);
src/printf.o(.text);
```
-and
+
+Next find the section that begins with `.rodata :`. At the end of this section, add the following:
```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:
+## 3. Initialize mGBA logging
+In order to print anything to the debugger, we need to first initialize it. Open `src/main.c`. At the top, add `#include "mgba.h"`. Then edit the starting function to start mGBA logging:
```diff
void AgbMain()
@@ -31,7 +33,7 @@ void AgbMain()
InitHeap(gHeap, HEAP_SIZE);
...
```
-## 3.
+## 4. ???
Add `#include "printf.h"` to `src/libisagbprn.c`.
```diff
@@ -45,10 +47,19 @@ Add `#include "printf.h"` to `src/libisagbprn.c`.
```
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`:
+## 5. Add the ability to print ASCII strings
+In order to print human-readable strings, we need to add a utility function to `gflib/string_util.h`. Open this file and add the following declaration at the end (above the `#endif`).
+```diff
+...
+void ConvertInternationalString(u8 *s, u8 language);
+void StripExtCtrlCodes(u8 *str);
+char *ConvertToAscii(const u8 *str);
+
+#endif // GUARD_STRING_UTIL_H
+
+```
+
+Now we need to add the implementation of `ConvertToAscii`. Open `gflib/string_util.c`. Add `#include "malloc.h"` to the top. Then, add the following C function to the end of the file:
```c
char *ConvertToAscii(const u8 *str)
@@ -137,16 +148,18 @@ char *ConvertToAscii(const u8 *str)
}
```
-## 5. Testing the code
-Make sure you include
+## 6. Testing the code
+Printf is now functioning! Let's test it out. We'll add a print statement every time we encounter a wild pokemon.
+
+Open `wild_encounter.c`. You'll need to add the following include statements at the top.
```c
#include "printf.h"
#include "mgba.h"
+#include "../gflib/string_util.h" // Only necessary if you want to print ASCII strings.
```
-in whatever file you test with.
-You should aldo include `#include "../gflib/string_util.h"`, if you want to print ASCII strings.
+**Note that you'll have to add those includes to any file you want to call `mgba_printf` from!**
-Let's test this in `wild_encounter.c`, add the following code:
+Now let's edit the function that's called whenever a wild Pokémon is encountered. At the end of that function, add the `mgba_printf` call as below:
```diff
static void CreateWildMon(u16 species, u8 level)
{
@@ -156,10 +169,16 @@ Let's test this in `wild_encounter.c`, add the following code:
}
```
-This will print out the species number for the wild Pokémon about to be encountered.
+This will print out the species number for the wild Pokémon about to be encountered. This is already useful, but we can do even better. Let's modify our statement to print the species name as well. We'll add two more includes to the top of the file:
+
+```
+#include "data.h" // for gSpeciesNames, which maps species number to species name.
+#include "../gflib/string_util.h" // for ConvertToAscii()
+```
+**Note that any time you want to call `ConvertToAscii` you'll need to `#include "../gflib/string_util.h"`!**
-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.
+Now we'll change our `mgba_printf` call to the following:
+`mgba_printf(MGBA_LOG_DEBUG, "%d %s", species, ConvertToAscii(gSpeciesNames[species]));`.
-TIP: Don't include this in release builds, because `printf` takes a lot of cycles. \ No newline at end of file
+## 7. Viewing the logs
+In order to view our logs, we'll need to use the [mGBA](https://mgba.io/) emulator. Open the log viewer by going to the "Tools" menu and selecting "View Logs...". Make sure the "Debug" checkbox is checked and you should see a message like this one when you encounter a wild Pokémon: `[DEBUG] GBA Debug: 286 POOCHYENA`. \ No newline at end of file