Skip to content

gba: add support for mGBA debugging#5443

Open
zowhoey wants to merge 1 commit into
tinygo-org:devfrom
zowhoey:mgba-debug-print
Open

gba: add support for mGBA debugging#5443
zowhoey wants to merge 1 commit into
tinygo-org:devfrom
zowhoey:mgba-debug-print

Conversation

@zowhoey
Copy link
Copy Markdown

@zowhoey zowhoey commented Jun 3, 2026

Implements putchar when mgbadebug build tag is set which outputs
text through the mGBA debugging output interface.

mGBA allows the games running in the emulator to output debug text
The process to do so is:

  1. set 0x4FFF780 to value of 0xC0DE
  2. write text in memory 0x4FFF600 to 0x4FFF700
  3. set 0x4FFF700 to the desired log level value from 0x100 (fatal) to
    0x104 (debug)

Once this is done mGBA will output the text in its logs view as well as
through stdout. (Setting the log level to fatal also produces a dialog
box with the text)

The text output in the CLI is prefixed with [DEBUG] GBA Debug: so I
modified the regex used for address matching in panic messages to be
able to read the address when the output line doesn't start with
panic.

After the change

func main() {
	println("hello world")
	// fake panic with an address in it for demo purposes
	panic("runtime error at 0x08000000: ")
}

results in output such as:
image

This makes debugging much easier as panic messages are shown and we can also do
debug prints.

Implements `putchar` when `mgbadebug` build tag is set which outputs
text through the mGBA debugging output interface.

mGBA allows the games running in the emulator to output debug text the
process to do so is:
1. set 0x4FFF780 to value of 0xC0DE
2. write text in memory 0x4FFF600 to 0x4FFF700
3. set 0x4FFF700 to the desired log level value from 0x100 (fatal) to
0x104 (debug)

Once this is done mGBA will output the text in its logs view as well as
through stdout. (Setting the log level to fatal also produces a dialog
box with the text)

The text output in the CLI is prefixed with `[DEBUG] GBA Debug: ` so I
modified the regex used for address matching in panic messages to be
able to read the address when the output line doesn't start with
`panic`.
@zowhoey zowhoey marked this pull request as ready for review June 3, 2026 14:13
@jwetzell
Copy link
Copy Markdown
Contributor

jwetzell commented Jun 4, 2026

Nice! I was looking at adding this too except I was going to add it to https://github.com/tinygo-org/tinygba . I like the ability to just use println though.

@zowhoey
Copy link
Copy Markdown
Author

zowhoey commented Jun 4, 2026

Hmm I didn't realize tinygba is a thing. I've been doing a fair bit of GBA development with tinygo slowly over several years (it's my fallback project when I get tired of everything else) so I have a fair bit of GBA functionality implemented.
No bitmap modes, no gamepak stuff, no DMA sound, but most other stuff is either fully or partially implemented. I'm not sure my code would mash well with the existing tinygba code, but I should probably look into publishing the code publicly as it could be useful for others.

I've had the code for mGBA debug printing in my code for at least 2 years as a custom log package. Eventually as the scale grew I started running into mysterious crashes very often that were really annoying to debug without seeing runtime panics, so having this code in the actual runtime has been pretty helpful.

@deadprogram
Copy link
Copy Markdown
Member

@zowhoey do please publish whatever code you have for gba. It would be amazing to have more support implemented, however we get it out there. If we can merge some of that into tinygba, great. If not, also fine. Just having a way to see working code that does things will be very helpful I'm sure!

@jwetzell
Copy link
Copy Markdown
Contributor

jwetzell commented Jun 4, 2026

@zowhoey nice! When I get bored of other things I've been messing with getting a good link cable API going mostly following this repo!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants