Vram banking in CGB

Discussion of programming and development for the original Game Boy and Game Boy Color.
Post Reply
DarkMoe
Posts: 70
Joined: Sat Jun 27, 2015 1:09 pm

Vram banking in CGB

Post by DarkMoe »

Hi, I've been trying to add color support to my GB emulator. DMG compatibility is very high now (some window issues mostly), but I'm having a hard time with color GB.

I already emulated the palette registers for bg and obj, but I can't draw anything yet.

This document for example (http://gbdev.gg8.se/wiki/articles/Video_Display), says:

BG Map Attributes (CGB Mode only)
In CGB Mode, an additional map of 32x32 bytes is stored in VRAM Bank 1 (each byte defines attributes for the corresponding tile-number map entry in VRAM Bank 0):
Bit 0-2 Background Palette number (BGP0-7)
Bit 3 Tile VRAM Bank number (0=Bank 0, 1=Bank 1)
Bit 4 Not used
Bit 5 Horizontal Flip (0=Normal, 1=Mirror horizontally)
Bit 6 Vertical Flip (0=Normal, 1=Mirror vertically)
Bit 7 BG-to-OAM Priority (0=Use OAM priority bit, 1=BG Priority)

I've just reached a tile in vram bank 1, which has a value of 0x08, that would be bit 3, but I can't get the meaning of "Tile VRAM Bank Number". What should I do with this bit ? If it's ON, it's bank 1, but I don't understand what should I do with it.

So I have my VRAM bank 0, which I guess is exactly the same as the DMG one, and this new VRAM bank 1 which has just attributes (metadata) for all the bank 0 tiles ?

What am I missing here ?

Thanks,
Shonumi
Posts: 342
Joined: Sun Jan 26, 2014 9:31 am

Re: Vram banking in CGB

Post by Shonumi »

Keep in mind, all of VRAM (0x8000 to 0x9FFF) will be on two banks. On the GBC, Background Map data is found on VRAM Bank 0 at 0x9800 through 0x9FFF. The attributes for each map entry are located in that same space on Bank 1. You seem to have gotten that much sorted out, so let's move on.

0x8000 through 0x97FF is where you find the actual tile data (i.e. the "dot data" that makes up the paletted tiles). Now, whenever you have to look up a map entry, and it tells you to look at VRAM Bank 0 or 1, every time you read from 0x8000 through 0x97FF, read from the specified VRAM Bank. It works like this:

Assume we are using 0x8000 through 0x8FFF as our Tile Pattern Table (keep it simple by using unsigned tile numbers...) and then say we start reading some random map entry. The map entry itself (found on VRAM Bank 0) is just a number (0 - 255) that will point us to the right address somewhere between 0x8000 through 0x8FFF. Say we pull the first map entry at 0x9800 and it was something like 0x17 from VRAM Bank 0. Now we need to look at 0x9800 in VRAM Bank 1 to get the BG attributes. Let's just assume the attributes flag tells us to look at VRAM Bank 1 for this example

Going forward, we have the map entry (0x17) and the VRAM Bank (0x1). We calculate the VRAM address to pull the tile data from by using the map entry, e.g. this formula:

Code: Select all

address = 0x8000 + (0x17 * 16)
Unsurprisingly, this is the same stuff you'd do on a regular DMG. So to pull the tile data from VRAM, we need start looking at 0x8170. But wait, remember that VRAM Bank bit from the BG attributes? That tells us which VRAM bank to look at when we access 0x8170 - 0x817F for the tile data. If it's 1, you read bytes VRAM Bank 1. If not, VRAM Bank 0. Pretty simple when you see what it's doing. Hope this clears it up for you :wink:
DarkMoe
Posts: 70
Joined: Sat Jun 27, 2015 1:09 pm

Re: Vram banking in CGB

Post by DarkMoe »

Brilliant ! I managed to make the CGB bios to work perfectly. Time to move to those new DMA features.

Thanks !
Shonumi
Posts: 342
Joined: Sun Jan 26, 2014 9:31 am

Re: Vram banking in CGB

Post by Shonumi »

Getting GBC VRAM banking correct is very important. In Pokemon Gold/Silver, for example, if done improperly, you end up turning Ecruteak City's famous Kimono Girls into something else entirely, like a dance troupe of middle-aged men. :D
Post Reply