Wednesday, January 22, 2014

Graphics: Tiles

All the graphics on the game boy are based on 8x8 tiles with 4 shades (2 bits per pixel), so 16 bytes each. The CPU uploads tile "character" data (pictures) to the video memory and then the background image (an optional overlay window) and sprites are all made by putting tiles together.

For an example let's look at the heart graphic from Super Mario Land:


It is stored using the following 16 bytes:

0000 6e6e bf93 bf83 ff83 7e46 3c2c 1818
 
translated to binary:

00000000
00000000

01101110
01101110

10111111
10010011

10111111
10010011

11111111
10010011

01000110
01111110

00111100
00101100

00011000
00011000

and the pixel shade is worked out using the following table:

0 white
0

1 light
0

0 dark
1

1 black
1

You can view the tiles inside a ROM image by converting every 16 bytes into image data this way, here are a couple of examples:

 

Boot ROMs

When you first turn on the game boy the Nintendo Logo will scroll down the screen and it chimes before the game starts, or if you don't have any game cartridge in it will show a black square like in this video:


What is happening when you first turn on the game boy is that it maps a "boot ROM" that's hard coded into the CPU over the memory range 0x0000-0x0100, it starts off by executing this program (which includes scrolling down the logo as well as initializing some CPU settings) before control flow is given over to the game cartridge itself.

A lot of specific information about the boot rom is described in Patent 5134391: System for preventing the use of an unauthorized external memory, but in particular at the very end of execution it maps the cartridge back over the range 0x0000-0x0100 so that a game can't read the boot ROM at all: That means you can't just create your own game cartridge that prints out the code inside the boot ROM.

In fact no one extracted the boot ROM from the game boy until neviksti in 2003 - 14 years after the Game Boy was released. This was done by decapping this chip and locating the part of the chip which actually encodes the boot ROM:

and then zooming in to it and reading off the individual bits by eye!


given that you can disassembly it from binary into assembly code that shows al the operations that the boot ROM does, you can read that commented here on the gbdev wiki and it really does follow the flow chart given on the patent almost by the letter:

The boot ROM for the game boy color was dumped in 2009 (almost a decade later) by a different technique using overclocking to skip that last instruction which locks out the boot ROM. You can read about that here: http://www.fpgb.org/?page_id=17.


The CPU

The CPU of the game boy handles almost everything except graphics, it does all the memory mapping and even sound synthesis. Here is a block diagram abstractly showing all the main components of the CPU:

Registers and Opcodes: The 8-bit registers come in pairs so that e.g. de and hl are often used as 16-bit numbers. There is a normal opcode table (with just a couple unused opcodes) and an extended opcode table for bit operations so just under 2x256 operations.

Memory Mapping: The CPU itself builds a memoy map that lets it address (using numbers from 0x0000 to 0xFFFF) various different chips like the two SRAM shown earlier as well as memory on the cartridge/game-pak inserted , there are also many specific addresses that act as hardware registers and this is how it interfaces with the serial port, lcd display, sound synthesis etc.

Interrupts: The CPU also supports interrupts via the interrupt controller. These  are triggered by special hardware events, during code execution the program-counter can jump to execute an interrupt routine to handle the event before returning back.

DMA: There is also a feature called Direct Memory Access which enables it to copy blocks of data around while executing other instructions.

Here are microscope photographs of one of these CPU chips decapped:

taken by Epop

and this fantastic image from tinytransistors (the link includes zooms of specific parts and descriptions of what they are)
Sources & Links:

Overview

Here are the main parts of the game boy unit:

The second block diagram (from patent 5134391) shows that the LCD controller is split into two parts.

and here is the main PCB from a real game boy:



You can see the main CPU in the middle, the two SRAM chips  used for display and work memory and I think the fourth chip amplifies the sound. At the bottom you can see where the cartridge (game pak) connector has been removed.

In the block diagram "6-pin subconnector" is the link cable port, here are pinouts for it and the cartridge connector


I think that the CPU itself outputs a stream of pixels which the two LCD chips (see the schematics) then decode into vertical and horizontal lines. You can see both those chips (covered in a glob) one behind the screen in this image, and the other on the flexible PCB below the screen:

Sources:

DMG-01

This post serves as a table of contents for the rest of the blog, whose focus is the working of the game boy and game boy color.
  1. Overview of the system
  2. The CPU
  3. Boot ROMs
  4. Graphics: Tiles
  5. Cartridges: MBCs
Collected some pictures of the unusual special cartridges that people have made or discovered: