summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPyredrid <Pyredrid@users.noreply.github.com>2020-09-02 15:17:46 -0400
committerPyredrid <Pyredrid@users.noreply.github.com>2020-09-02 15:17:46 -0400
commit3ceaa9619bc229653250212e07ab38f29a1ef4b1 (patch)
tree7cd7aabef682d5e150634a316ed9dd28b30e79b6
parent79f441583f065f04990482bbc1f760ec7e2d05e6 (diff)
Updated printf in mGBA (markdown)
-rw-r--r--printf-in-mGBA.md77
1 files changed, 63 insertions, 14 deletions
diff --git a/printf-in-mGBA.md b/printf-in-mGBA.md
index d0d5944..d36ac27 100644
--- a/printf-in-mGBA.md
+++ b/printf-in-mGBA.md
@@ -1,21 +1,54 @@
-Credits to Petuh for this tutorial, and for Lunos for finding an archived version.
+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 identical to Petuh's original post.
+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.
-First, download [this](https://cdn.discordapp.com/attachments/744575017051881492/744961612200542288/mGBA_Debug_printf.zip). Pop the files into where they're supposed to go in src and include. Next, add
+
+## 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);
+src/mgba.o(.text);
+src/printf.o(.text);
```
and
```c
- src/printf.o(.rodata);
+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)
+...
```
-to `ld_script.txt`. Next, then add `#include "mgba.h" to main.c`, and in `AgbMain`, add `mgba_open();` below `SetDefaultFontsPointer();`.
-Now you are ready to change the `vsprintf` to `vsnprintf`. To do this, add `#include "printf.h"` to `libisagbprn.c`. In `#define AGB_PRINT_STRUCT_ADDR 0x9FE20F8`, change to `#define AGB_PRINT_STRUCT_ADDR (char*) 0x9FE20F8`. Then, change both `vsprintf` to `vsnprintf`.
-OPTIONAL: Printing to strings and testing
-Now is the printing to strings and testing part, both optional. If you want to print to string, add `#include "malloc.h"` to `string_util.c`. Then, add
+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)
{
@@ -46,15 +79,31 @@ char *ConvertToAscii(const u8 *str)
return textBuffer;
}
```
-(credits to Pidgey)
-In `string util.h`, add `char *ConvertToAscii(const u8 *str);`.
-Finally, the testing. make sure you include
+
+## 5. Testing the code
+Make sure you include
```c
#include "printf.h"
#include "mgba.h"
#include "gba/isagbprint.h"
```
-in whatever file you test with. optionally, you can include `#include "../gflib/string_util.h"`, and `#include "data.h"` if you want to get for example a species name. Let's test in `wild_encounter.c`. before `CreateMonWithNature(&gEnemyParty[0], species, level, 32, PickWildMonNature());`, add `mgba_printf(MGBA_LOG_DEBUG, "%s", ConvertToAscii(gSpeciesNames[species]));`. If you want to just get a number, you can do `mgba_printf(MGBA_LOG_DEBUG, "%d", species);`.
+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. \ No newline at end of file