The Tilemap (also known as the Name Table) is used to display backgrounds.
It is an array of tiles stored in VRAM where each tile definition is 2 bytes in size and uses the following format:

BitsFeatureCommentHex Value
0 - 8Tile indexThis can range from 0 to 511$0 - $1FF
9Horizontal flipWhen set, the tile graphics will be flipped horizontally$200
10Vertical flipWhen set, the tile graphics will be flipped vertically$400
11PaletteWhen set, the tile will use the Sprite palette (CRAM $10-$1F)$800
12PriorityWhen set, the tile will be drawn on top of sprites$1000
13 - 15UnusedSome games use these bits as flags for collision and damage zones.$2000, $4000, $8000

The size of the Tilemap array depends on the display mode. When using the 192-line display modes the Tilemap is a 32x28 array, but when using the 224 or 240-line display modes it is 32x32 instead. The address of the Tilemap table is set using VDP Register 2 and is usually stored at $3800 in VRAM for 192-line modes, and $3700 for 224 or 240-line modes (avoiding clashes with the Sprite Table at $3F00).

The Tilemap is stored row by row, so the tile data for row 0 will be first, followed by the tile data for row 1.
As tiles are 2 bytes in size and rows are 32 tiles wide, each row is therefore 64 bytes long.
From this we can calculate the address of a tile at tile coordinates x and y by doing:

tile_address = $3800 + (tile_x * 2) + (tile_y * 64)

Scrolling

The Tilemap can be scrolled horizontally and vertically using the VDP Register 8 for horizontal scroll and VDP Register 9 for vertical scroll.

Horizontal Scrolling

As both the display and the Tilemap are 256 pixels wide (32 tiles * 8 pixels per tile = 256 pixels), glitches will be visible in the leftmost column of the Tilemap when the screen scrolls horizontally. These glitches can be covered up by setting bit 5 of VDP register 0 which will hide the leftmost column of the screen.
Horizontal scrolling can be disabled for the top two rows of tiles by setting bit 6 of VDP register 0, which can be used for status bars or similar.
The horizontal scroll register can be updated as the screen is being drawn, allowing for special effects where the horizontal scroll register is updated during the HBlank interrupt.

Vertical Scrolling

The Tilemap is always vertically bigger than the display, so vertical scrolling does not result in any glitches which need to be covered up.
Vertical scrolling can be disabled for the rightmost eight columns by setting bit 7 of VDP register 0, which can be used for status bars or similar.
The vertical scroll register is only read once - before the Tilemap is drawn - so it cannot be updated as the screen is drawn for special effects.

In the regular 192-line display mode the Tilemap is 224 pixels tall (28 tiles * 8 pixels per tile = 224 pixels). Values written to the vertical scroll register which are greater than 223 will wrap around, and are treated as if the scroll value was between 0 and 31. This means that setting the vertical scroll register to 240 is equivalent to setting the vertical scroll register to 16. When programming vertical scrolling, variables used to track the vertical scroll should therefore also wrap back around to 0 at 224 to avoid discontinuities in the scrolling.
In the 224 and 240-line display modes there is no such wrapping as the Tilemap is 256 pixels tall, so values from 0 to 255 are all valid.




Return to top
0.085s