Video
-----
The main GameBoy screen buffer (background) consists
of 256x256 pixels or 32x32 tiles (8x8 pixels each). Only
160x144 pixels can be displayed on the screen. Registers
SCROLLX and SCROLLY hold the coordinates of background to
be displayed in the left upper corner of the screen.
Background wraps around the screen (i.e. when part of it
goes off the screen, it appears on the opposite side.)
An area of VRAM known as Background Tile Map contains
the numbers of tiles to be displayed. It is organized as
32 rows of 32 bytes each. Each byte contains a number of
a tile to be displayed. Tile patterns are taken from the
Tile Data Table located either at $8000-8FFF or
$8800-97FF. In the first case, patterns are numbered with
unsigned numbers from 0 to 255 (i.e. pattern #0 lies at
address $8000). In the second case, patterns have signed
numbers from -128 to 127 (i.e. pattern #0 lies at address
$9000). The Tile Data Table address for the background
can be selected by setting the LCDC register.
There are two different Background Tile Maps. One is
located from $9800-9Bff. The other from $9C00-9FFF.
Only one of these can be viewed at any one time. The
currently displayed background can be selected by
setting the LCDC register.
Besides background, there is also a "window" overlaying
the background. The window is not scrollable i.e. it is
always displayed starting from its left upper corner. The
location of a window on the screen can be adjusted via
WNDPOSX and WNDPOSY registers. Screen coordinates of the
top left corner of a window are WNDPOSX-7,WNDPOSY. The
tile numbers for the window are stored in the Tile Data
Table. None of the windows tiles are ever transparent.
Both the Background and the window share the same Tile
Data Table.
Both background and window can be disabled or enabled
separately via bits in the LCDC register.
If the window is used and a scan line interrupt disables
it (either by writing to LCDC or by setting WX > 166)
and a scan line interrupt a little later on enables it
then the window will resume appearing on the screen at the
exact position of the window where it left off earlier.
This way, even if there are only 16 lines of useful graphics
in the window, you could display the first 8 lines at the
top of the screen and the next 8 lines at the bottom if
you wanted to do so.
WX may be changed during a scan line interrupt (to either
cause a graphic distortion effect or to disable the window
(WX>166) ) but changes to WY are not dynamic and won't
be noticed until the next screen redraw.
The tile images are stored in the Tile Pattern Tables.
Each 8x8 image occupies 16 bytes, where each 2 bytes
represent a line:
Tile: Image:
.33333.. .33333.. -> 01111100 -> $7C
22...22. 01111100 -> $7C
11...11. 22...22. -> 00000000 -> $00
2222222. <-- digits 11000110 -> $C6
33...33. represent 11...11. -> 11000110 -> $C6
22...22. color 00000000 -> $00
11...11. numbers 2222222. -> 00000000 -> $00
........ 11111110 -> $FE
33...33. -> 11000110 -> $C6
11000110 -> $C6
22...22. -> 00000000 -> $00
11000110 -> $C6
11...11. -> 11000110 -> $C6
00000000 -> $00
........ -> 00000000 -> $00
00000000 -> $00
As it was said before, there are two Tile Pattern Tables
at $8000-8FFF and at $8800-97FF. The first one can be used
for sprites, the background, and the window display. Its
tiles are numbered from 0 to 255. The second table can be
used for the background and the window display and its tiles
are numbered from -128 to 127.