emuforge

The picture

Two chips, PPU1 and PPU2, draw the picture. They have three memories of their own, which are not on the processor's bus. A game fills them through a few registers, mostly with DMA in V-blank, and the chips then draw each line by themselves.

Both chips are one part here, crates/snes-ppu. Files without a folder are in its src. What is here rests on fullsnes, the Super Famicom Development Wiki (anomie's documents) and the SNESdev Wiki. A few things are from undisbeliever's and SnesLab's pages (credits). Each module names its pages. All guesses are in the last part, and lib.rs has its own list of them.

Memories

MemorySizeWhat it hasHow it changes
VRAM32,768 words (64 KB)tiles, and maps of tiles. In mode 7, one byte of each word is a map and the other one tileseach byte at once. Only in V-blank or with the screen off: otherwise, a write is lost, and the address moves all the same
CGRAM256 colours of 15 bits5 bits each for red, green and bluewith the second byte of a colour
OAM512 + 32 bytes128 objects: 4 bytes each, and then 2 bits for eachin the first 512 bytes, two bytes with the second one. In the other 32, at once

VRAM has its port at $2115 to $2119, and is read at $2139 and $213A:

  • $2116 and $2117 set the address of a word. Bit 15 is not connected. This also reads the word into a buffer, in V-blank or with the screen off.
  • $2118 and $2119 write the lower and the upper byte. $2139 and $213A read these bytes of the buffer. When a read moves the address, the buffer is first filled from the address before it moves. So the first word after a new address is read twice.
  • $2115 says which of both bytes moves the address (bit 7), by how much (bits 1-0: 1, 32, 128 or 128), and if the lowest 8, 9 or 10 bits of the address are turned left by three (bits 3-2). This way, tiles of 2, 4 or 8 bits can be filled in as if they were a bitmap.

CGRAM has its address at $2121, and colours are written at $2122 and read at $213B. Both use the same address and the same turn of first and second byte.

OAM has its address at $2102 and $2103, and bytes are written at $2104 and read at $2138. The address that was set is loaded again at clock 40 of the first line of V-blank, unless the screen is off. It is also loaded again by some writes of $2100.

Lines and frames

A line has 1,364 master clocks. A dot lasts 4 of them, but dots 323 and 327 last 6, so there are 340 dots. The 256 dots of the picture come out from dot 22 on, which is clocks 88 to 1,112.

NTSCPAL
Lines in a frame262312
With interlace263 in field 0313 in field 0
A line of another lengthwithout interlace, line 240 of field 1 has 1,360 clocks, and all of its dots last 4with interlace, line 311 of field 1 has 1,368 clocks, and so one dot more

The picture has lines 1 to 224, or 1 to 239 with overscan (SETINI, $2133). V-blank begins in line 225 or 240. The bus asks for this, for the number of lines and for the length of each one, when it needs them. So a change of SETINI counts from then on. V-blank does not end when overscan is turned on in it, but VRAM is locked again until line 240. The field changes with each frame, with or without interlace.

The architecture has a whole frame, and the main processor's page its interrupts.

How a dot is made

  1. Each layer gives a dot or none. Backgrounds look in their maps for the tile at that place, and in the tile for the dot's colour. Colour 0 is transparent. Objects were prepared before the line.
  2. The windows hide layers, each layer on its own, on the main screen, the sub screen or both.
  3. Two pictures are made, the main screen and the sub screen. Each has the layers that a register chooses for it ($212C and $212D). The dot in front wins, in the order of the mode. Where there is none, it is the backdrop: CGRAM's first colour.
  4. Colour math can add the sub screen or a fixed colour to the main screen, or subtract it.
  5. Brightness makes it darker, and the colour is in the picture. The sub screen's own dots are only seen in high resolution.

$2100 (INIDISP) turns the screen off with bit 7. It is then black. Bits 3 to 0 are the brightness. 0 is black, and with any other value each of red, green and blue becomes colour × (brightness + 1) / 16. The console does this in its analog output. Here the result is rounded down to 5 bits, which is a choice.

The modes

The lowest three bits of $2105 (BGMODE) choose one of eight modes. These are the numbers of colours in a dot of each background, and in which order things are, from the front to the back. S3 is an object of priority 3, and 1H is a tile of background 1 with its priority bit. 1L is one without.

ModeBG1BG2BG3BG4Order
04444S3 1H 2H S2 1L 2L S1 3H 4H S0 3L 4Leach background has 32 colours of its own in CGRAM
116164S3 1H 2H S2 1L 2L S1 3H S0 3Lwith bit 3 of BGMODE: 3H S3 1H 2H S2 1L 2L S1 S0 3L
21616S3 1H S2 2H S1 1L S0 2LBG3 has an offset for each column
325616as 2
42564as 2BG3 has an offset for each column
5164as 2512 dots
616S3 1H S2 S1 1L S0512 dots, and BG3 has an offset for each column
7256S3 S2 2H S1 1L S0 2Lone turned layer. BG2 is there only with EXTBG

Backgrounds

  • A map has 32 × 32 tiles in 1,024 words. $2107 to $210A say where a background's map is, and if there are two maps beside each other, below each other, or four.
  • A word of the map has the tile's number (bits 9-0), its palette (12-10), its priority (13), and the flips to the left and right (14) and up and down (15). Tiles with 256 colours have no palette. They can have direct colour (bit 0 of $2130): then the 8 bits of a dot are 3 bits of red, 3 of green and 2 of blue. Each has one bit more from the palette.
  • Tiles are 8 × 8 dots, or 16 × 16 if BGMODE says so for this background (bits 7-4). A large one is made of four: the tile itself, the next one, and those 16 numbers after these two. $210B and $210C say where they are. A row of a tile has one word for each 2 bits of a dot, 8 words apart. The lower byte has the lower bit, and bit 7 of each byte is the left dot.
  • Scrolls have 10 bits, and are written twice: first the lower byte, and then the upper one. All of them share a byte that keeps the last write. The lowest three bits of a horizontal scroll come from the last write to a horizontal scroll, and the rest of its lower byte from the last write to any scroll. (Both wikis have this. fullsnes has an older rule.)
  • An offset for each column: in modes 2, 4 and 6, BG3's map has scrolls, and not tiles. Each column of 8 dots of BG1 and BG2 can get another horizontal scroll (in steps of 8 dots) and another vertical one from there. Bits 13 and 14 of the word say if it is for BG1 or BG2. The first column on the screen never has one. Modes 2 and 6 take the horizontal scrolls from one line of BG3, and the vertical ones from 8 lines below. Mode 4 has one entry for both, and bit 15 says which of both it is. BG3's own scroll chooses the line and the first column.
  • Mosaic ($2106) makes blocks of up to 16 × 16 dots that all show their upper left one. They count from the top and from the left of the picture. A new size waits for the next block. Mosaic in modes 5 and 6 makes both screens show the even dots, with any size, also 1.

High resolution

Modes 5 and 6 have 512 dots in a line. The sub screen shows the even ones and the main screen the odd ones, so there have to be layers on both. Their tiles are always 16 of these dots wide. With interlace, their backgrounds also have twice as many lines, one half in each field. $2133 can also turn on pseudo-hires in any mode. Then the sub screen's 256 dots are seen between those of the main screen.

A dot of the sub screen is to the left of its main dot. What is done to it, such as being made black or having math, is what was done to the main dot before it. It is mixed with that dot before its math. The sub dots are only masked by windows at their own place. If a line changes to high resolution in the middle, the dots that are already there are doubled.

Mode 7

Mode 7 has one map of 128 × 128 tiles with 256 colours, which makes 1,024 × 1,024 dots. It can be turned, scaled and tilted with a matrix of four numbers ($211B to $211E, with 8 bits after the point). A game that changes them from line to line, with HDMA or in other ways, makes the ground look like a plane that goes into the distance.

A word of VRAM is in two parts. Word row × 128 + column has the map's tile in its lower byte, and word tile × 64 + y × 8 + x has the tile's dot in its upper one.

For the dot x of a line y, the place in the map is, with the scroll, the centre and the matrix A, B, C and D:

X = A × (scroll X − centre X) + B × (scroll Y − centre Y) + B × y + centre X × 256 + A × x
Y = C × (scroll X − centre X) + D × (scroll Y − centre Y) + D × y + centre Y × 256 + C × x

The lowest 8 bits of X and Y are the part after the point. The hardware leaves out some bits, and games can see that:

  • Scroll minus centre has a sign and the lower 10 bits. The bits between them are lost. Scrolls and centres have 13 bits, with a sign.
  • The lowest 6 bits of the first three products are dropped. Those of the product with x are not.

$211A can flip the screen in both directions. It also says what is outside of the map: the map again, nothing, or tile 0 everywhere. With EXTBG (bit 6 of $2133), the same dots are also a second layer, BG2. It has 128 colours, and their highest bit is the priority of the dot. Mosaic for the lines of both layers is BG1's bit, and each has its own for the dots in a line. Direct colour is only for BG1.

All of mode 7's registers but $211A, and its own copies of the scrolls at $210D and $210E, are written twice. They share a byte that keeps the last write.

Objects

An object has four bytes in OAM: x, y, the tile, and a byte with the flips, its priority, its palette and the tile's highest bit. Two bits more are x's highest one and the choice between two sizes. x goes from -256 to 255. An object is shown one line lower than its y says, and it goes on at the top after line 255.

Bits 7-5 of $2101SmallLarge
08 × 816 × 16
18 × 832 × 32
28 × 864 × 64
316 × 1632 × 32
416 × 1664 × 64
532 × 3264 × 64
616 × 3232 × 64
716 × 3232 × 32

Objects have 16 colours, and use the second half of CGRAM. $2101 also says where their two tables of 256 tiles are. Within an object, the next column is in the same row of 16 tiles, and the next row in the same table, and both wrap there. An object that is twice as high as it is wide flips each of its square halves up and down on its own.

How the objects of a line are found, during the line before it (objects.rs):

  1. The chips look at all 128 objects, one in each two dots, and keep the first 32 that are on the next line. If there is one more, bit 6 of STAT77 (range over) is set at that moment. Each object that is partly in the picture counts. One at x = -256 cannot be seen, but counts too.
  2. They begin with the first object. That is object 0. If bit 7 of $2103 is set, it is the one that the OAM address is at. If the next byte is the last of an object's four, the line's number is added to it.
  3. Between the lines, they fetch the tiles, from the last object that was found to the first, and from left to right in each one. There can be 34 tiles of 8 dots. If there is one more, bit 7 of STAT77 (time over) is set, and the rest is left out. So the objects that lose tiles are those in front. Tiles outside of the picture are not fetched, but all those of an object at x = -256 are.

Where objects overlap, the first one that has a dot there is seen, whatever its priority is. Only then does its priority decide what is in front of it, among the backgrounds. So an object in front with a low priority can hide another one, and be hidden by a background itself.

Both flags stay until a frame starts with the screen on. $2133 can show objects with half as many lines, of which each field has every second one.

Windows

There are two windows, each with a left and a right edge ($2126 to $2129). Both edges are inside. If the left one is after the right one, there is no window.

Each of the four backgrounds, the objects and the colour window has its own settings ($2123 to $2125): whether each window counts, and whether it is turned inside out. If both count, $212A and $212B say how they are put together:

ValueInside is
0in one or both (OR)
1in both (AND)
2in exactly one (XOR)
3in both or in none (XNOR)

$212E and $212F say which layers a window hides, on the main and on the sub screen. The colour window does not hide anything. It says where colours are made black, and where there is no math.

Colour math

$2130 (CGWSEL), $2131 (CGADSUB) and the fixed colour ($2132) decide, for each dot of the main screen:

  1. Is it made black first? Bits 7-6 of CGWSEL say where: nowhere, outside of the colour window, inside, or everywhere.
  2. Does it have math? Bits 5-4 of CGWSEL say where not, with the same four choices. Bits 5-0 of CGADSUB say which layers have it: the backdrop, objects and the four backgrounds. Objects have it only with palettes 4 to 7.
  3. With what? With the fixed colour. If bit 1 of CGWSEL is set, it is the sub screen. Where that has only the backdrop, it is the fixed colour after all.
  4. Plus or minus? Bit 7 of CGADSUB subtracts. Red, green and blue each stay between 0 and 31.
  5. Is it halved? Bit 6 of CGADSUB halves the result. A sum is not limited before that. It is not halved where the dot was made black, or where bit 1 of CGWSEL is set and the fixed colour is used all the same.

For example, a dot is red 20, the sub screen has red 18 there, and CGADSUB adds. Without halving, 38 is limited to 31. With it, it is 19. If nothing is on the sub screen there and the fixed colour is red 4, the result is 24, and it is not halved.

Registers that are read

AddressName
$2134-$2136MPYa product of 24 bits with a sign: the 16 bits of $211B times the last byte that was written to $211C. It is there at once. While mode 7 draws, the multiplier is busy, and each half dot has another product
$2137SLHVreading it latches the counters, if bit 7 of WRIO ($4201) is 1. The value is open bus
$213C, $213DOPHCT, OPVCTthe latched dot and line, with 9 bits each. The first read has the lower byte, and the second one the highest bit
$213ESTAT77time over, range over, and the version of PPU1: 1
$213FSTAT78the field, whether the counters were latched, PAL, and the version of PPU2: 3. Reading it makes both counters start with their first byte again. It also clears the bit that says that they were latched, but not while WRIO's bit 7 is 0

The counters are also latched when bit 7 of WRIO goes from 1 to 0. If they are latched again, the new values replace the old ones, whether STAT78 was read or not.

Open bus: each chip keeps the last byte that it gave. Bits that a register of this chip does not have are from there. Some registers that can only be written give PPU1's byte when they are read: those from $2100 to $212F whose last digit is 4 to 6 or 8 to A. All others give the processor's open bus.

When it is drawn

The picture is drawn late, and only when something could tell. The core says at each access where the beam is. A write that would change the picture first brings it up to there. Then the rest of the line has the new value. Reads only do what they need:

WhatHow far it catches up
the counters, STAT78, VRAM and CGRAM when they are read, open busnot at all
MPYto the line, if mode 7 is on and the screen is too
STAT77it looks for objects up to the dot, until both flags are set. Earlier lines are drawn
OAM when it is read, and its addressto the line. A byte written to OAM also finishes the search for objects up to the dot, if the screen is on
the modes and addresses for VRAM and CGRAM, and the first byte of a colournot at all
a byte in VRAM and a finished colourto the dot, if it changes the memory
$2105 to $2114 and $2123 to $2132, but for mosaicto the dot, if the value changes
$211A to $2120the same, but only in mode 7. In other modes, a game uses them to multiply
$2101, the sizes of objectsit looks for objects up to the dot, if the value changes. Earlier lines are drawn
$2100, mosaic, $2133to the dot, if the value changes

With the screen off or in V-blank, changes of only the picture or the objects do not have to wait. At the end of the frame, the core catches up all that is left.

A write takes effect at the next dot that comes out. A dot that starts at the clock of the write already has the new value. So a game that changes a colour in the middle of a line gets the old one to the left of that place, and the new one to the right. For example, a write at clock 500 comes after the dots that started before clock 500. Dot x of the picture starts at clock 88 + 4x. These are dots 0 to 102, as dot 103 starts at clock 500 and has the new colour.

A line is drawn as a whole when nothing changes in it, layer by layer and 8 dots of a tile at a time (draw.rs). The other way draws one dot after the other, from front to back, with the documents' rules and none of the shortcuts (exact.rs). It shares only the registers, the order of layers and what an object is with the fast one. Console::set_exact uses it, and also looks for objects one by one and catches up at each access. tests/same.rs compares both, with random accesses at random times, random memory, all modes, both consoles, and copies in the middle of a line. The picture, all that a program can read and the state of the chips have to be the same. status.md has what was compared in games.

A line that has not changed is not drawn again. For each whole line, the PPU remembers the registers, the objects, and the parts of VRAM that it was drawn from. VRAM and CGRAM remember when they last changed. If all of it is as it was, the line in the last picture is right. Interlaced pictures always draw theirs, and a loaded state forgets all of them (unchanged.rs). A test compares this with the exact way.

Frames can be left out. The picture can stay as it is while the chips do all that a program can see, such as finding objects for STAT77. The core does not use this yet.

What is a guess

Documents say nothing about these, or differ. Each place in the code says more:

  • In the middle of a line, a register changes the dot that comes out at that moment. The console fetches the tiles of backgrounds 2 to 3 tiles earlier, and shows wrong ones for a moment when the screen is turned on. Neither of these is here. Mosaic looks at the left of its block again, with the new registers. SnesLab says that the hardware keeps that colour.
  • CGRAM and OAM while the picture is drawn are used at the address that the game set. The console uses another one, which is not known. VRAM's buffer keeps its value then.
  • $2100 (INIDISP): brightness changes at once. There is no wrong value for a moment from what was on the bus before.
  • The OAM address is loaded again when the screen is turned on, and also by any write of INIDISP in the first line of V-blank while the screen is off. Documents have one of these rules each, so both are here.
  • Objects are fetched at the start of their line, with OAM, VRAM and $2101 as they are then. The last line of the picture looks for none. If the first object is not 0, the time of the range over flag counts from that one. After lines with the screen off there are no objects, and never wrong ones.
  • Interlaced objects: the field chooses their rows, even if the picture is not interlaced.
  • Mosaic looks at its size at the start of a line. fullsnes has a counter of lines, which is here. The Super Famicom Development Wiki says that each write starts new blocks.
  • High resolution: one page has the main and the sub screen the other way around. Nothing is done to the first dot of a line.
  • Mode 7: which half dot has which product in MPY. EXTBG does nothing in other modes, where a console shows wrong dots.
  • Latching again without a read of STAT78 in between. The SNESdev Wiki says that the old values stay, and that this was not confirmed.
  • The two long dots. Some pages have four dots of 5 clocks in their place.
  • SETINI counts as it is when the core asks for the length of a line or a frame, or where V-blank begins. Where an interlaced picture's rows are is decided at the start of line 1. What overscan does to V-blank when it is turned on in line 225 is only here in part.
  • When the console is turned on, the three memories, and the registers that no document has, are 0.

Light guns are not there, so nothing but $2137 and WRIO latches the counters.

From commit 019fda0af379. Edit this page