Gameboy Development Forum

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.

Ads

#1 2020-04-26 10:58:22

toxa
Member
Registered: 2020-02-13
Posts: 307

an easy way to profie your c-code with bgb emulator

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:

Code:

#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:

Code:

// 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:

https://d.radikal.ru/d39/2004/3a/d98dc5ed39d4.png

Last edited by toxa (2020-04-26 11:26:11)

Offline

 

#2 2020-04-26 11:11:48

toxa
Member
Registered: 2020-02-13
Posts: 307

Re: an easy way to profie your c-code with bgb emulator

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:

Code:

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

 

#3 2020-04-26 11:21:16

Zalo
Member
From: Spain
Registered: 2016-07-26
Posts: 103
Website

Re: an easy way to profie your c-code with bgb emulator

This is aawesome!!!!
So we just needed to read the docs? LOL

Offline

 

#4 2020-04-26 12:17:19

Zalo
Member
From: Spain
Registered: 2016-07-26
Posts: 103
Website

Re: an easy way to profie your c-code with bgb emulator

Found a way to auto generate labels

Code:

#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

 

#5 2020-04-26 12:28:57

toxa
Member
Registered: 2020-02-13
Posts: 307

Re: an easy way to profie your c-code with bgb emulator

oh! that's another kind of a kung fu.

Offline

 

#6 2020-05-16 04:58:55

Zalo
Member
From: Spain
Registered: 2016-07-26
Posts: 103
Website

Re: an easy way to profie your c-code with bgb emulator

I have updated the macro in gbdk, now the code is like this

Code:

#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

 

#7 2020-05-16 17:18:48

toxa
Member
Registered: 2020-02-13
Posts: 307

Re: an easy way to profie your c-code with bgb emulator

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

 

#8 2020-07-04 09:04:38

Matt
New member
From: State of Goiás, Brazil.
Registered: 2020-07-03
Posts: 4

Re: an easy way to profie your c-code with bgb emulator

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

 

#9 2020-07-04 17:48:22

toxa
Member
Registered: 2020-02-13
Posts: 307

Re: an easy way to profie your c-code with bgb emulator

"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

 

Board footer

Powered by PunBB
© Copyright 2002–2005 Rickard Andersson