Difference between revisions of "Map decoding in President"

From Pin Eight
Jump to: navigation, search
Line 59: Line 59:
|||| ++++- Pattern number
|||| ++++- Pattern number
++++------ Height at which pattern is drawn
++++------ Height at which pattern is drawn
== Walls ==
== Walls ==

Revision as of 02:34, 10 May 2009

President's architecture is heavily inspired by that of Super Mario Bros. (SMB1).

President compared to SMB1

Like SMB1:

  • Map is composed of "objects".
  • Map is decoded to 8-bit metatile entries in a 2-screen-wide sliding window.
  • Layer of "clouds", or a repeating sequence of columns where each column has a 1x3-tile object at a given height.
  • Layer of "wall", or a 1x12-tile repeated object drawn on top of "clouds".
  • Four 64-entry metatile tables, one for each attribute value.

Unlike SMB1:

  • The camera can move freely in the sliding window.
  • Each map has a 32-entry directory containing the length of each screen's data.
  • The decoder can operate on either side of the sliding window.
  • When a block is destroyed in one of the 512 columns of a map, it sets a bit in a destruction buffer.
  • A lookup table supports translating objects in a destroyed column into destroyed objects.
  • A Markov chain supports efficient decoding of objects taller than one block.
  • Clouds can be drawn over wall, not just under it.
  • It's CHR RAM based; map types can replace tile data.
  • President is free software.

Decoding procedure

To decode column x of screen s

  1. Clear the column buffer
  2. Draw the clouds, if clouds in back are enabled
  3. Draw the wall, if wall is enabled
  4. Draw the clouds, if clouds in front are enabled
  5. If s > 0:
    • Seek to screen s - 1
    • x += 16
  6. Else:
    • Seek to screen s
  7. While x > 0:
    1. Draw all objects obj where obj.x <= x and obj.x + 16 >= x
    2. Seek to next screen
    3. x -= 16
  8. Apply Markov chain to column buffer
  9. Draw column to nametable transfer buffer
  10. Copy column to sliding window buffer
  11. Wait for vertical blank
  12. Copy nametable transfer buffer to VRAM
  • CPU RAM 12 bytes: Column decoding buffer


Each screen's map data has a 32-entry table giving the length in bytes of each screen's map data. When the decoder seeks to the previous or next screen, it follows the chain of lengths when modifying curMapPageDataStart.


A cloud pattern has

  • Number of screens (1 byte), value = 1 to 4
  • Metatiles making up each of 16 1x3 patterns (48 bytes)
  • Column entries (16 to 64 bytes, 1 byte for each column)

Format of each column entry:

7654 3210
|||| ||||
|||| ++++- Pattern number
++++------ Height at which pattern is drawn


A "wall" is a horizontal strip of identical tiles on which To be written.

Background objects

The decoder for a background object is called with (x - o.x, y, obj), and it fills the column buffer.

A background object can never be wider than 16 tiles, because then it could sit across more than the two screens that the decoder considers.

Sliding window

There are two 192-byte buffers, each holding 16x12 bytes, where each byte controls what metatile is placed there. This is used for redrawing and collision. Columns of even screens go in buffer 0; columns of odd screens go in buffer 1.

  • CPU RAM $0600-$06BF: Even screen buffer
  • CPU RAM $0700-$07BF: Odd screen buffer


Between the two buffers of the sliding window is a destruction buffer organized as a 512-bit array. When a block is destroyed, the corresponding bit turns on.

The map decoder has a set of four 64-entry tables defining which tiles become which when destroyed. For example, if table[0][$19] = $8B, then all tiles of attribute 0 and value $19 become attribute 2 and value $0B in a destroyed column. This happens before Markov.

  • CPU RAM $06C0-$06FF: Destruction buffer


There is a set of four 64-entry tables from each tile to which is For example, if table[0][$23] = $64, then the most common tile below a tile of attribute 0 and value $23 is attribute 1 and value $24. Metatiles of value 0 are replaced with the most common tile below.

Metatile to nametable

The top 2 bits of a metatile number are the attribute number. There is a set of four 64-entry lookup tables, each of 4 bytes, from each tile to the top left, bottom left, top right, and bottom right nametable entries making up a metatile. Which table is used depends on the metatile's attribute number.


To be written.

  • PPU RAM $0000-$0FFF: Background tiles
  • PPU RAM $1000-$1FFF: Sprite tiles