The gameboy has access to only 8192 bytes of working RAM (i.e. the variables in the game, the list of ennemies ect...) and 8192 bytes of Video RAM.
This may sound like a lot... generally knowing that a byte holds just 1 comma-less value between 0 and 255 (or between -128 and 127, depending on what you do with it), but then wait. Say we have 256 colors (which, seeing how many ugly GIF's the internet is cluttered with, who have just exactly this number of pixels) we could just generate a grid with colors (Some would say an image... depends on what you are painting on) 128*64 cells large. The Gameboy, as being a device with 160*144 pixels, clearly hasn't got enough with that.
To still render 160*144 pixel large picture, the Gameboy pulls out a few tricks.
First of all, did you ever wonder why the old big Gameboy could only display 4 different shades of gray? (look very closely... you will see. Unless you have messed-up with the contrast!) It is because he has just stored 4 pixels in 1 byte!
Indeed, a lot of old systems only stored a On & Off image of your favorite character. Like this:
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 | Total |
X | X | X | #14 | |||||
X | X | X | X | X | #145 | |||
X | X | X | X | X | X | X | #223 | |
X | X | X | X | X | X | #138 | ||
X | X | X | X | #212 | ||||
X | X | X | X | #46 | ||||
X | X | #10 | ||||||
X | X | #10 |
Here, the bytes are made by summing up the numbers on the top which are lit as pixels. Or, said more mathematically, we do look at the first pixel, add 1 to the number if it is lit, then multiply the number by 2 and shift the picture 1 pixel to the left. The Gameboy does the inverse to reconstruct the image.
So, we have just encoded a 8*8 pixel image in just 8 bytes instead of 64 bytes. But 1 pixel in this image can only be eighter fully lit or fully dark! To resolve this problem, the Gameboy desided to store 1 row in 2 bytes.
If the same pixel is Off in both bytes, then the pixel is White (or green). If it is On on both bytes, it is Black. But if it is On on the first byte and Off on the second, it is dark green and if it Off on the first byte and On on the second one it is light green.
Green | X | Dark Green | ||||||
Light Green | X | Black | ||||||
X | X |
The Gameboy stores 384 pairs of 8*8 images like that, 16 bytes per image, in a table 6072 bytes large.
There is another trick that the Gameboy uses everywhere.
Say we want to write "Hello World
". We could just assign the value representing those 3 "l
"s to one of the numeric values of a byte. Then, we write this byte, and then the next byte the numeric value of "o
".
This way, we don't have to store every single "l
" as 8 bytes for a picture, but only as 1 byte of a char.
The Gameboy stores 2 such char maps, 1 byte per char, for the background, 32 bytes long by 32 bytes large (i.e. 1024 bytes accross per page of text). Of course, it reads left to right, mapping each value to a shape in the shape map.
And if we load in the whole alphabet, we can start writing words!
As you might guess, this table is 32*8=
256 pixels long by 256 pixels large. This is to large for the screen of the Gameboy, so it is clipped; however the game may scroll it with some registers