News: 11 March 2016 - Forum Rules
Current Moderators - DarkSol, KingMike, MathOnNapkins, Azkadellia, Danke

Author Topic: GB - when does Vblank-Interrupt occur  (Read 6718 times)

cret

  • Jr. Member
  • **
  • Posts: 75
    • View Profile
GB - when does Vblank-Interrupt occur
« on: February 22, 2014, 08:42:35 pm »
The title says it all.
Please don't tell me something like this:
Quote
The V-Blank interrupt occurs ~59.7 times a second
on a regular GB and ~61.1 times a second on a Super
GB (SGB). This interrupt occurs at the beginning of
the V-Blank period. During this period video
hardware is not using video ram so it may be freely
accessed. This period lasts approximately 1.1 ms.

I just want to know about the normal Gameboy, I need to know after how many CPU-CYCLES a Vblank-interrupt might going to be fired up.

Thanks
go r2, use debug. .... White hand was fainted

FAST6191

  • Hero Member
  • *****
  • Posts: 2591
    • View Profile
Re: GB - when does Vblank-Interrupt occur
« Reply #1 on: February 22, 2014, 08:59:00 pm »
For the GBA I would have pointed you at something like http://www.coranac.com/tonc/text/video.htm#sec-blanks but I lack something as nice for the GB/GBC.

However is it not just a multiple of those numbers and the clock speeds (4.194304MHz, to give an amount of about 4600 cycles for the vblank period itself and about 65600 for the rest of it, the sum being a complete end of vblank to end of vblank cycle)?

It gets slightly more complex as you are playing on a portable device and you get to tangle with things like http://nocash.emubase.de/pandocs.htm#reducingpowerconsumption

Drakon

  • Sr. Member
  • ****
  • Posts: 277
    • View Profile
    • 16 Bit Gamer
Re: GB - when does Vblank-Interrupt occur
« Reply #2 on: February 22, 2014, 10:04:28 pm »
To make the gb colorizer code word I learned to write v-blank waitloops.  I actually can't remember the exact code but you can find it in those hacks.  Duodream (very cool guy) who I met on irc was kind enough to explain it to me.  Vblank really annoying to work with.

Scio

  • Full Member
  • ***
  • Posts: 155
    • View Profile
Re: GB - when does Vblank-Interrupt occur
« Reply #3 on: February 22, 2014, 10:30:46 pm »
You can check V-Blank by comparing LY (FF44) with 144. Anything above 144 is V-Blank.

« Last Edit: February 22, 2014, 10:59:21 pm by Scio »

cret

  • Jr. Member
  • **
  • Posts: 75
    • View Profile
Re: GB - when does Vblank-Interrupt occur
« Reply #4 on: February 22, 2014, 10:59:42 pm »
I forgot to say why I need to know this: I'm working on a multi-arch emulator with a plugin-system. The main api is mostly done (screen is not, because I plan some awesome magic for this). I decided that the first supported arch should be gameboy. I get stuck on interrupts :( , because for some of them I don't know when they are going to be fired.


@Scio: Does that mean, that vblank is only fired if LY>144? How is LY set, only manually by the game, or does this happen automatically? Sounds interresting.
go r2, use debug. .... White hand was fainted

KingMike

  • Forum Moderator
  • Hero Member
  • *****
  • Posts: 6904
  • *sigh* A changed avatar. Big deal.
    • View Profile
Re: GB - when does Vblank-Interrupt occur
« Reply #5 on: February 23, 2014, 01:24:18 am »
I haven't done graphics-related GB hacking, but I'm guessing LY is a value returned by the video chip (and thus for programming purposes should assume it's changed automatically).
"My watch says 30 chickens" Google, 2018

tomaitheous

  • Hero Member
  • *****
  • Posts: 543
    • View Profile
    • PC Engine Dev
Re: GB - when does Vblank-Interrupt occur
« Reply #6 on: February 23, 2014, 10:41:28 am »
The title says it all.
Please don't tell me something like this:
I just want to know about the normal Gameboy, I need to know after how many CPU-CYCLES a Vblank-interrupt might going to be fired up.

Thanks

 If you're working on emulator related stuff (writing emulation code), this site is probably not the best place for that info.

 That said, just look at what you posted. The refresh rate of the screen is ~59.7 times a second. That's ~16.75ms per frame (the total time a frame takes, including vblank). Vblank is listed as last ~1.1ms. That pretty much tells you all that you need to know.

 The active display is 144 pixels. 144+vblank = 16.75ms. Vblank is ~1.1ms. Vblank is somewhere between 10 to 11 scanlines in length (which sounds about right, from memory). Vblank is going to happen every ~70,256 master clock cycles or 17,564 cpu cycles (the master clock is 4,194,304hz. Since all cpu cycles are exactly 1/4 the master clock, most emulators and docs list the 'cpu cycles' in the 1/4 format). The start of the display is 0, the end of the display is 144 (start of vblank). So, assuming vblank is 11 scanlines long - the vblank interrupt is fired ~16,317 cpu cycles after the start of every frame.

 If you want better precision than that, you'll need to look at some emulator source code or talk to an emulator author that knows the gameboy.


EDIT: The GB/C cribsheet says the vblank time is 10 scanlines. So there you go. I suggest you get that cribsheet (GBCribSheet000129.pdf)

Description Frequency/Time 1x Clocks 2x Clocks
Horizontal Scanline Time 108.7 μs 114 228
V-Blank Period (10 scanlines) 1087 μs 1140 2280
Mode 10 19.31 μs
Mode 11 41.37 μs to 70.69 μs
Mode 0 with 10 sprites per scanline 18.72 μs
Mode 0 with no sprites per scanline 48.64 μs
CPU Clock Speed 4.194/8.838 Mhz 1,048,576/sec 2,209,715/sec
Horizontal Sync Frequency 9198 Hz
Vertical Sync Frequency 59.73 Hz 17555/frame 36995/frame

Drenn

  • Jr. Member
  • **
  • Posts: 94
    • View Profile
Re: GB - when does Vblank-Interrupt occur
« Reply #7 on: February 23, 2014, 01:51:31 pm »
LY is set by the system. It represents the scanline currently being rendered. Apparently games can reset this value back to zero, which causes the system to start rendering scanline 0 again. Don't worry about that... I haven't yet found anything which relies on this behaviour.

I think the exact number of cycles per scanline (and thus, to vblank) can vary. I'll be using cycles in terms of the master clock (multiples of 4).

The numbers I use are: each line is rendered for 456 cycles, and then LY is incremented. Vblank is triggered once LY hits 144. When LY == 153, LY is reverted to 0.

There's some funny behaviour where scanline 0 lasts about twice as long as it should... I got this following information from examining bgb, though I haven't tested how accurate it is on hardware. After 456 cycles on scanline 0, the vblank period ends. Then it does scanline 0 "normally" and proceeds to scanline 1 after another 456 cycles. It's not so important to worry about this at first.

So you've got vblank lasting from LY=144 to LY=152, plus half of LY=0. Effectively, vblank lasts for 10 scanlines. Vblank starts at 144*456 = 65664 cycles from the start of the frame. It takes another 10*456 = 4560 until the next frame begins.

(144*456+10*456)*59.7 = 4192372.8 clocks per second, which is reasonably close to the correct value.
Zeldahacking.net: The hub for Zelda: Oracle of Ages and Seasons hacking. We have a discord!

cret

  • Jr. Member
  • **
  • Posts: 75
    • View Profile
Re: GB - when does Vblank-Interrupt occur
« Reply #8 on: February 25, 2014, 08:20:46 pm »
i don't think that the 1/4 format is a good idea, because stop has no specific cycle-length, and i didn't use it when I ported cycle-length to r2 http://runas-racer.com/foo/cycles3.png, and i've read that dma takes 671 cpu-cycles. thanks for your responses, it helped a lot.


But I still have questions:
would it be enough for the start just to display all stuff from the vram/ram, like i did in this demo: (1.07 the interresting part starts) http://runas-racer.com/foo/ramu2.mp4
« Last Edit: February 25, 2014, 10:05:32 pm by cret »
go r2, use debug. .... White hand was fainted

Drenn

  • Jr. Member
  • **
  • Posts: 94
    • View Profile
Re: GB - when does Vblank-Interrupt occur
« Reply #9 on: February 26, 2014, 02:46:41 pm »
i don't think that the 1/4 format is a good idea, because stop has no specific cycle-length, and i didn't use it when I ported cycle-length to r2 http://runas-racer.com/foo/cycles3.png, and i've read that dma takes 671 cpu-cycles. thanks for your responses, it helped a lot.


But I still have questions:
would it be enough for the start just to display all stuff from the vram/ram, like i did in this demo: (1.07 the interresting part starts) http://runas-racer.com/foo/ramu2.mp4
You'll want to get basic background & sprite rendering to work as soon as possible, if you want to test games effectively. Since you've established that it's writing to vram correctly, there's no real reason to put it off.
Except for the fact that it looks really slow, but maybe that's intentional.
Zeldahacking.net: The hub for Zelda: Oracle of Ages and Seasons hacking. We have a discord!

cret

  • Jr. Member
  • **
  • Posts: 75
    • View Profile
Re: GB - when does Vblank-Interrupt occur
« Reply #10 on: February 26, 2014, 10:14:40 pm »
yup, thats because I was mad when i created this output for testing. After each opcode I created a hash, and if it diffed from the previous, I redrawed everything. totally inefficient, but I removed all of this. I'm refactoring some memory stuff, added a plugin-system ...

In the end I want to use webgl, because it's just depending on 1 lib: libr, the radare-api, which itself has no special dependency (so it runs on linux, bsd, qnx, osx, android, ios, firefox-os, haiku, w32, w64, solaris).

this is mostly aimed to be a debugger-backend for a powerfull reverseengineering-framework.
go r2, use debug. .... White hand was fainted

cret

  • Jr. Member
  • **
  • Posts: 75
    • View Profile
Re: GB - when does Vblank-Interrupt occur
« Reply #11 on: April 02, 2014, 10:58:14 am »
hi, me again.
I've implemented somehow vblank-, timer- and joypad- interrupting and ly incrementing after approximately 456 cycles. I've got a new question: when does lcd-stat-interrupt occur? after incrementing ly?
go r2, use debug. .... White hand was fainted

Drenn

  • Jr. Member
  • **
  • Posts: 94
    • View Profile
Re: GB - when does Vblank-Interrupt occur
« Reply #12 on: April 02, 2014, 12:20:22 pm »
It can occur at various times depending on the LCD Status register.  For instance, if bits 3 and 4 are set, it will be triggered at every hblank and at vblank. You need to emulate the different "modes" per line in order to emulate the timing correctly.
Zeldahacking.net: The hub for Zelda: Oracle of Ages and Seasons hacking. We have a discord!

cret

  • Jr. Member
  • **
  • Posts: 75
    • View Profile
Re: GB - when does Vblank-Interrupt occur
« Reply #13 on: April 02, 2014, 03:55:30 pm »
Do I understand it right, that hblank means that ly gets increased?
go r2, use debug. .... White hand was fainted