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-02-17 17:01:25

tigrou
New member
Registered: 2020-02-17
Posts: 3

How to use GB CPU usage meter in BGB ?

There is an option called "GB CPU usage meter" in BGB (press F11, go to "Debug" tab, last checkbox at bottom left). Is this a feature that allow to view how much CPU is spend executing code  (eg :draw or update) vs waiting for VSync ?

I don't know how to use it. It seems it is the green bar in the debugger near registers (since double clicking on it will disable "GB CPU usage meter" option). Anyway it's always green (even with some rom running in background)

Does anybody know how to use that feature ?

Offline

 

#2 2020-02-18 02:47:03

nitro2k01
Administrator
Registered: 2008-02-22
Posts: 244

Re: How to use GB CPU usage meter in BGB ?

This feature measures how much time is used for executing code vs how much CPU time is spent in low power mode using the HALT opcode. Time spent in the HALT mode is indicated with a dark green. Note however that many games don't use the HALT opcode, despite Nintendo's recommendations and in this case the meter will be solid green.

If you are developing your own software, you should generally put a HALT in your mainloop instead of an endless loop.

Code:

        xor     A       ; Clear A (A=0)
        ld      [rIF],A ; Clear the pending interrupt register
        inc     A       ; Increment A from 0 to 1
        ld      [rIE],A ; Enable the VBlank interrupt

        ei              ; Done with all initialization. Enable interrupts.
MainLoop:
        halt            ; Sleep until the next interrupt happens    

        jr  MainLoop

If you need to do other stuff in the mainloop, you could signal this from the VBlank interrupt handler.

Code:

        
        xor     A       ; Clear A. (A=0)
        ld      [rIF],A ; Clear the pending interrupt register.
        inc     A       ; Increment A from 0 to 1.
        ld      [rIE],A ; Enable the VBlank interrupt.

        ei              ; Done with all initialization. Enable interrupts.
MainLoop:
        halt            ; Sleep until the next interrupt happens.
        ld      A,[vblank_happened]
        dec     A       ; Check if A == 1. Also makes A == 0 if it was 1.
        jr      nz,MainLoop
        ld      [vblank_happened],A     ; Write back 0.

        ; Do other stuff.
        ; ...
        jr      MainLoop



VBlankHandler:
        ; Do other stuff.
        ; ...
        ld      A,1     ; Signal that a VBlank happened.
        ld      [vblank_happened],A
        POPA
        reti

There's also another secret meter that you can access by pressing ctrl+shift+9 while the game is running. (Not in the debugger. In the debugger it places a bookmark in the code instead.) The left part is always either black or pink and isn't part of the CPU meter. The meter has three parts: Yellow for normal code. Red for code that BGB was able to detect and skipped over, and could probably be replaced with a HALT loop. And black for time spent in HALT mode.


Blog: Gameboy Genius
"A journey of a thousand miles begins with one small step"
Old Chinese Proverb

Offline

 

#3 2020-02-18 04:36:06

tigrou
New member
Registered: 2020-02-17
Posts: 3

Re: How to use GB CPU usage meter in BGB ?

Thanks for your detailed answer.

The game I am debugging (SML3/WL1) simply waits for VBlank in a loop (by checking value of a flag set in VBlank interrupt handler).
That is why the BGB feature does not work.

I will maybe patch it and use HALT as you suggested. I want to check how much CPU game use to see how far it push GB to the limit.


EDIT : using your instructions, I was able to patch the ROM (and replace pooling with an HALT instruction)
It works really great. CPU usage during level gameplay is 60% in average, sometimes going up to 95% depending what is on the screen (but never reach 100%, probably to keep 60 fps)
When using doors, it goes as low as 10% (during fade in / out) then reach 100% for a short burst (probably to load new tiles to video memory)

Last edited by tigrou (2020-02-19 10:00:39)

Offline

 

#4 2023-11-26 15:01:54

tigrou
New member
Registered: 2020-02-17
Posts: 3

Re: How to use GB CPU usage meter in BGB ?

Here is how it was patched :

Code:

ROM0:02D1 ld a,(ff00+82) ;vsync
ROM0:02D3 and a
ROM0:02D4 jr z,02D1 ;wait for vsync

Replaced by:

Code:

ROM0:02D1 halt
ROM0:02D2 nop
ROM0:02D3 nop
ROM0:02D4 nop
ROM0:02D5 nop

When game is under stress (eg: lot of enemies on the screen), the code using HALT is noticeably slower than the one without (to the point music will start to slow down). I'm not sure why.

Using HALT, the logic is different from before : it wait for vsync to start, not for the end of the vsync handler.
It would be probably better to call HALT and then to wait for vsync end (as before).

Here is main game loop :

Code:

while(true) {
      update_gamelogic();
      if(line < 128) //max = 144 {
            wait_hsync_start();
            update_animatedtile();
      }
      update_sound();
      wait_vsync_done(); //replaced by HALT
      vsync = 0;
}

wait_vsync_done() {
    while(vsync == 0);
}

wait_hsync_start() {
    while(hsync == 1); //lcd stat (ff41)
    while(hsync == 0); //
}

vsync_handler() {
    update_scroll();
    update_backgroundtiles();
    update_oam();
    vsync = 1;
}

Last edited by tigrou (2024-02-23 11:10:13)

Offline

 

Board footer

Powered by PunBB
© Copyright 2002–2005 Rickard Andersson