Tuesday, May 2, 2017

An introduction to Mega Drive graphics, part 1

The chip responsible for the Mega Drive’s badass graphics is the Visual Display Processor, or VDP. If we want to get our game up on the screen, we first need to get acquainted with how it works.

Everything the VDP displays is built out of 8x8 pixel blocks commonly referred to as ‘tiles’. These are arranged in a grid to display background layers, or in various different shapes to display sprites. Tiles are indexed bitmaps with a color depth of four bits per pixel. In other words, each tile is 32 bytes long, and each nybble in a tile describes an index to a 16 color palette defined elsewhere.

Mega Drive colors are in 9-bit RGB with 3 bits per channel. This gives us a theoretical limit of 512 distinct colors, which is accurate for the most part. Color values are padded to 16 bits in the curious pattern 0000BBB0GGG0RRR0, so an easy way to write them is with a hex triplet of the form BGR, in which every digit is always even. For instance, $EEE is pure white.

The VDP can only read from its own address space, so tile and color data must be written to it through the VDP data port. Colors are written to a small bank called Color RAM, or CRAM. Tiles, on the other hand, are written to the VDP's main memory bank, called Video RAM, or VRAM.

In order to display a tile on the screen, its offset in VRAM must be inserted in a structure called a VDP pattern index and written to either the background or sprite table. These tables are also stored in VRAM, so right off the bat, not all VRAM space is available for holding tile data.

Since tiles are 32 bytes long, a properly aligned tile will always have an offset divisible by 32. VDP pattern indices take advantage of this by storing only the high 11 bits of a tile offset, freeing up five bits for other purposes. Bit 15 controls the tile's "priority", more on that later. Bits 12/11 control its vertical/horizontal flip. The VDP cannot display rotated tiles.

Bits 14 and 13 form an integer that defines which CRAM palette to use. This implies a limit of four palettes in CRAM which, due to this representation, are commonly referred to as palette lines 0-3. Four palettes of 16 colors each gives us a maximum of 64 colors on screen at any given moment.

Next, we'll look at one way Sonic games break this limit.

No comments:

Post a Comment