Discussion about software development for the old-school Gameboys, ranging from the "Gray brick" to Gameboy Color
(Launched in 2008)
You are not logged in.
some time ago i asked here: https://gbdev.gg8.se/forums/viewtopic.php?id=689 about the ways to optimize the c code, unfortunately, nobody replied
and in the other discussion here: https://gbdev.gg8.se/forums/viewtopic.p … 4089#p4089 nitro2k01 made some calculations for me (thank you so much!)
i was thinking how to simplify that kung fu and i discovered an easy way. this is all, i'm shure, is well-known, but for those who are new with gb, as i am, here are some pieces of code. they are pretty small to make a repo, so i just post them here:
bgb_emu.h:
#ifndef __BGB_EMU_INCLUDE #define __BGB_EMU_INCLUDE // lbl must be unique, message_text contains text and expressions to // evaluste, see bgb.html #define BGB_MESSAGE(lbl, message_text) \ __asm \ ld d, d \ jr lbl \ .dw 0x6464 \ .dw 0x0000 \ .ascii message_text \ lbl: \ __endasm #endif
bgb_debug_msg.c:
// 1. run bgb_debug_msg.gb in the BGB emulator // 2. open the internal BGB debugger pressing "ESC" // 3. open the debug messages window: "window->debug messages" // 4. reset the gameboy // you then see a message "NOP TIME: 2". 2 means 1 cycles of processor // running in double speed mode. so divide 2 by 2 and this is a cycle // count, that "nop" instruction between the messages had taken: 1 #include "bgb_emu.h" void main() { BGB_MESSAGE(1$, "%ZEROCLKS%"); __asm__("nop"); BGB_MESSAGE(2$, "NOP TIME: %-8+LASTCLKS%"); }
you can post any messages to "debug messages" window of a bgb emulator from your c-code, and you may also evaluate some values, how this is done in my example. the first parameter of a macro is a unique label (i don't know, how to generate them in a macro), and the second one is a text message. other features of this bgb magic are described in bgb.html which comes with the emulator. you may put any code between the macros and divide the result by 2 if the gb cpu speed is not double. this is how it looks like in gbg:
Last edited by toxa (2020-04-26 11:26:11)
Offline
this clock is affected by interrupts. the easiest way to eliminate this influence is to disable the interrupts, to do this you may use the __critical keyword:
void main() { __critical { BGB_MESSAGE(1$, "%ZEROCLKS%"); __asm__("nop"); BGB_MESSAGE(2$, "NOP TIME: %-8+LASTCLKS%"); } }
Last edited by toxa (2020-04-26 14:59:49)
Offline
Found a way to auto generate labels
#define ADD_DOLLARD(A) ADD_DOLLARD1 (A) #define ADD_DOLLARD1(A) A##$ #define BGB_MESSAGE(message_text) BGB_MESSAGE1(ADD_DOLLARD(__LINE__), message_text) #define BGB_MESSAGE1(lbl, message_text) \ __asm \ ld d, d \ jr lbl \ .dw 0x6464 \ .dw 0x0000 \ .ascii message_text \ lbl: \ __endasm
Offline
oh! that's another kind of a kung fu.
Offline
I have updated the macro in gbdk, now the code is like this
#include "bgb_emu.h" void main() { BGB_PROFILE_BEGIN(); __asm__("nop"); BGB_PROFILE_END(NOP TIME); }
I think it simplifies things. You can also use the old macro if you prefer
Last edited by Zalo (2020-05-16 04:59:27)
Offline
those messages are not only for profiling, but for a general purpose debugging:
<some code>
BGB_MESSAGE("wow! my program reached so far!")
<some other code>
Offline
I'm curious about what "double speed mode" means in this excerpt of the comments in bgb_debug_msg.c:
"...you then see a message 'NOP TIME: 2'. 2 means 1 cycles of processor running in double speed mode..."
Offline
"double speed mode" is a feature of Game Boy Color. enabling that feature makes CGB run faster (and also drain battery faster). see cgb.h cpu_fast();
Offline