Writing a Game Boy Emulator - Part 2

Introduction

In the last blog post I got the emulator to execute enough instructions of the boot ROM to load the tile data of the Nintendo logo. Now we want to draw the tiles in the correct order so that we can actually read Nintendo and no garbled mess.

The display

The tile data of the Nintendo logo starts at address $8000, there is also the possibility to change this to another address but this is of no concern for now as the DMG ROM uses only the first location. The Background Tile Table references these tiles and puts them on the screen in correct order. Its address starts at $9C00 and can also be changed, again not of interest for us for now. The Game Boys background consists of 32x32 tiles, therefore the Background Tile Table is organised as 32 rows of 32 bytes each. We just need to iterate through all of them and draw the corresponding tile at the correct location to the screen. The actual tiles that can be seen are 20 in width and 18 in height, this means there has to be some sort of scrolling capabilities. More of that later.

Now let's implement that:

complete gameboy logo

Awesome! We have a correctly rendered Nintendo logo. But wait! We actually shouldn't see anything.

It works. But why?

At first I was happy that the image was displayed, but on second thoughts - it shouldn't do that, right?

When you turn on your Game Boy the first thing you see is nothing, and only then the Nintendo logo scrolls in. So why do we see something here?

It all comes down to the fact that I didn't have scrolling implemented yet.

Like explained before the "game world" of the Game Boy is a 32x32 tiles big, that are 256x256 pixels.

But only a certain part of that is shown on the screen. The Game Boy screen displays only 20x18 tiles. Now, what I haven't told you yet is that the Game Boy (of course) supports scrolling. Two registers SCROLLX ($FF42) and SCROLLY ($FF43) contain the horizontal and vertical scroll position.

In my case I didn't yet have scrolling implemented and just showed the tiles starting from the top left corner, this equals to a SCROLLX and SCROLLY of zero. Coincidentally that is exactly where the logo is put.

At the start of the DMG ROM the value of the scroll register is set so that we can't see the Nintendo logo:

LD A,$64        ; $0056
LD D,A      ; $0058  set loop count, D=$64
LD ($FF00+$42),A    ; $0059  Set vertical scroll register

This value is then slowly decremented which results in the logo sliding in from the top.

So a happy coincidence on my side to be able to see the logo right away.

After implementing this (wasn't too much of a hassle) the scrolling works.

See you next time!