emuforge

The Super FX

The Super FX is a processor in the cartridge that is made for drawing. Star Fox and Yoshi's Island have one. Its full name is the Graphics Support Unit, or GSU. It runs the game's own program, and needs nothing else. It draws into RAM in the cartridge, in the format of tiles. The main processor then copies these to the picture's memory.

It is in crates/snes-gsu, and files below are in its src. How it meets the bus is in crates/snesemu-core/src/chip.rs. Nearly all of it rests on fullsnes, whose sections each module names. The one page that the SNESdev Wiki has is an overview. In one place, games show something other than fullsnes says: GETBH and GETBL.

There are three versions. The header does not say which one a cartridge has:

Here
MC1, the MARIO chipin the crate. The core never chooses it
GSU-1for games with up to 1 MB of ROM
GSU-2for games with more, which only the GSU-2 can have

That the size decides is a guess. fullsnes does not have the GSU-1's mirrors of registers or its version. So the mirrors are like the GSU-2's, and the version is 2 (the GSU-2's is 4), which are guesses too.

Time

It runs at 10.74 or 21.48 MHz, as bit 0 of CLSR says. Here, a cycle is two master clocks or one. On the console, the chip has its own crystal of 21.44 MHz. So it is about 0.2% fast here on NTSC, and 0.7% slow on PAL.

An instruction counts as done when it starts. So what it does to registers and RAM can be there earlier than on the chip, by at most the time of one instruction. Most of the time, the main processor cannot see that, as the chip has the RAM. STOP is the exception: it clears GO and asks for an interrupt exactly when it ends. An instruction only starts before the time up to which the chip runs. So how the time is cut into pieces changes nothing.

Cycles at 10.74 MHzAt 21.48 MHz
A byte of the program from the cache11
A byte from or to the ROM or the RAM35
A word from or to the RAM610
What the ROM buffer needs for its byte46
A slow multiplication of 8 bits1 more1 more
A multiplication of 16 bits7 more, or 3 with the fast multiplierthe same

fullsnes has a range of cycles for each instruction, and these numbers fit most of those ranges (timing.rs says how, and what does not fit). It says only "possibly" that all uses of memory take the same time for a byte, so that is a guess. That a byte of the program takes as long as any other at the faster speed is a guess too.

The processor

It has 16 registers of 16 bits (R0 to R15), and flags for zero, carry, sign and overflow. Some registers have another use too:

R1, R2the x and y of PLOT and RPIX
R4the lower half of a long product
R6, R7, R8what FMULT, LMULT and MERGE work with
R11where LINK keeps the address to return to
R12, R13LOOP counts R12 down, and jumps to R13
R14the address of a byte in the ROM. Changing it makes the ROM buffer read that byte while the program goes on
R15the address of the program. Changing it jumps
  • Prefixes change the next instruction. TO, WITH and FROM choose the registers for its result and its source, and ALT1, ALT2 and ALT3 give another instruction under the same number. They only last for one instruction, but branches keep them.
  • The byte after a jump always runs. While an instruction runs, the next byte is already being fetched. If that is an instruction with more bytes, these come from where the jump goes.
  • The code cache has 512 bytes, in 32 lines of 16 bytes. While the program runs in an area of 512 bytes from an address that CACHE or LJMP sets, each line is kept as it is run. What is in the cache runs in one cycle for each byte, and needs no ROM or RAM. After a jump out of a line, the rest of the line is loaded first. The main processor can also write a line, and it counts as loaded when its last byte is written.
  • Waits: a byte from the ROM and a store to the RAM take time while the program goes on. An instruction that needs the same memory waits for them. A read from the RAM also waits for the pixels.

Drawing

PLOT draws a pixel with the colour from COLOR or GETC at R1 and R2, and adds 1 to R1. The pixels are first collected in a cache of 8, which are beside each other. The cache is passed on when all 8 were drawn, or before a pixel somewhere else. It is also passed on by RPIX, which reads a pixel back. A second cache then writes them into the RAM while the program goes on, as one row of a tile. If not all 8 were drawn, the old bytes are read first, and only the new pixels change.

SCMR's lowest two bitsColoursBytes in a row of 8 pixels
042
1164
2 (reserved, so like 1: a guess)164
32568

SCBR says where the picture starts in the RAM, in steps of 1 KB. It is 128, 160 or 192 lines high, with tiles below each other. In a fourth mode, which is for objects, four squares of 16 × 16 tiles make a picture of 256 × 256 pixels. CMODE sets more: whether colour 0 is drawn, dithering, which half of a byte becomes the colour, whether the upper 4 bits of the colour stay, and the object mode.

Here, the RAM has the pixels as soon as the second cache has them, and only their time passes later. While the chip has the RAM, only its own program can see that, and it waits for them before it reads. If the chip loses the RAM or stops before that time is over, the main processor sees them earlier than on the chip.

GETBH and GETBL

GETB gets the byte from the ROM buffer. GETBH makes it the upper byte of the result, and GETBL the lower one. The other byte comes from a register. fullsnes says that it is from the one for the result (Dreg). It is from the one for the source (Sreg), as games showed. With Dreg, Dirt Racer, Dirt Trax FX, Doom and Yoshi's Island stop early or show a blank screen. With Sreg, all four go on. The number of different pictures in Star Fox, Vortex and Winter Gold stays the same. How this was found has the rest.

What the main processor sees

AddressGSU-1GSU-2
$00-$3F: $3000-$34FFregistersregisters
$00-$3F: $6000-$7FFFthe first 8 KB of the RAMthe same
$00-$3F: $8000-$FFFFthe ROM, as LoROMthe same
$40-$5Fthe ROM, as HiROMthe same
$70-$71up to 128 KB of RAMthe same
$80-$BFas $00-$3Fonly the registers and the first 8 KB of the RAM
$C0-$DF, $F0-$F1as $40-$5F and $70-$71nothing
the restnothingnothing

Both maps in the same ROM are the same bytes. Bank $40 has what banks $00 and $01 have in their upper halves. "Nothing" is what fullsnes has for known cartridges, and it is open bus.

RegisterName
$3000-$301FR0 to R15each is written with its lower byte first. That byte is kept in a latch, which all of them share. Writing the upper byte of R15 starts the program there
$3030, $3031SFRthe flags. Bit 5 is GO, which says that it runs. Bit 15 is the interrupt's flag, and reading $3031 clears it
$3034PBRthe program's bank
$3036ROMBRthe bank of the ROM buffer. It can only be read
$3037CFGRbit 7 keeps the interrupt off, and bit 5 chooses the fast multiplier
$3038SCBRwhere the picture starts
$3039CLSRthe speed
$303ASCMRthe picture's format, and who has the ROM (bit 4) and the RAM (bit 3)
$303BVCRthe version
$303CRAMBRthe bank of the RAM for the program. It can only be read
$303E, $303FCBRwhere the cached area starts. It can only be read
$3100-$32FFthe code cache

While it runs, the main processor can use only SFR and VCR, and it can write SCMR. All others read as 0 and cannot be written, which is a guess.

Who has the ROM and the RAM

SCMR says what the chip may use, and the chip has it only while it runs:

While the chip has it
The ROM (bit 4 of SCMR, and GO)the main processor reads fixed bytes in the whole ROM. They depend only on the last digit of the address. They are there so that interrupts have vectors: $0108 for the NMI, and $010C for the IRQ, in the main memory. The game puts a jump there. While a game uses the chip, its own program runs in main memory
The RAM (bit 3 of SCMR, and GO)the main processor's writes are lost, and its reads are open bus. No source says what happens, so this is a guess

The main processor can take either back while the program runs. It clears the bit, and the program waits when it next needs that memory. When it gets it back, it goes on. Here, an instruction starts only when it has all it needs, and one that has begun when a bit is cleared still ends. This is a simplification. A program in the cache that does not need the memory runs on.

The interrupt comes when STOP ends, unless bit 7 of CFGR keeps it off. It stays until $3031 is read. TIMEUP ($4211) does not show it, and does not end it.

When the core runs the chip: before its registers are used, before the ROM or the RAM that it has at that time is used, when it could next stop, and at the end of the frame. As any instruction can be followed by STOP, that next time is only a few clocks away while it runs. When its interrupt is kept off or has come, or when it waits for a memory, it needs nothing.

When it runs

So at its event, it runs ahead, at most 2,000 clocks. It does not end a STOP there. Until then, the main processor can only change it with its registers. Before most uses of them, a saved state, or the end of a frame, the chip and its RAM are put back as they were, and run to the exact time. Only reads of SFR's lower byte do not need that, as games ask for it again and again. After the first such read, the chip keeps what it had after each instruction. It stops keeping it when the game stops reading it (ahead.rs).

For example: one picture, step by step

Main processorThe chip
1has its program in main memory, as it will not have the ROM soon. It has jumps at $0108 and $010Cstopped
2writes the bank of the chip's program to PBR, where the picture is to SCBR, its speed to CLSR, and 0 to CFGR, so that the interrupt is onkeeps them
3writes the picture's format and both bits to SCMRstill has nothing, as it does not run
4writes the two bytes of the program's address to $301E and $301Fwith the second one, it sets GO and starts. It has the ROM and the RAM from now on. It first runs a NOP while it fetches its first byte
5does other things in main memory. Its NMI still works: both bytes of the vector are fixed ones, which lead to $0108loads its code into the cache line by line, reads data through R14, and plots. When it has drawn the last pixel, it uses RPIX so that its caches go to the RAM
6runs STOP, which takes as long as fetching one more byte. When it ends, GO is cleared. At that clock, the ROM and the RAM are the main processor's again, and the interrupt begins
7enters its IRQ. The vector is the ROM's own now. It reads $3031, which clears the interrupt's flag, and knows that the picture is donestopped
8in V-blank, it copies the tiles with DMA from $70:0000 to the picture's memory. It can start the next picture after that

A game can also do without the IRQ, and read SFR again and again until GO is 0. Each read runs the chip up to its own clock.

What is a guess, and what is not there

Each module marks its guesses. These matter the most:

  • All of its times beyond the ranges in fullsnes, and which of two waiting buffers goes first. The simplest rule is used: what waits starts when the bus is free.
  • Its state when it is turned on and after a reset is all 0.
  • What is at its other addresses is 0, which is STOP if it runs there. fullsnes has only the GSU-2's addresses. All versions use these here.
  • GO on a chip that runs changes only the flags. After STOP, the byte that was already fetched does not run.
  • A line that is only partly loaded is left empty by CACHE, LJMP and STOP.
  • Where a transparent pixel is drawn, nothing happens to the caches.
  • The fast multiplier at 21.48 MHz, which games must not use, works.
  • Some flags, such as the carry of the long multiplications and the sign of RPIX.
  • Not there: the MC1's STOP that hangs shortly after a write to the RAM, and the wrong byte that GETB reads when CACHE follows closely after a change of R14. fullsnes says that these exist, and not exactly when. BRAMR is for a second RAM for saves. No known cartridge has one, so it does nothing.

Stunt Race FX and Wild Trax only go on because TIMEUP does not show the chip's interrupt. Status has the results of all releases that were tried.

The instructions are tested with values that were worked out by hand from fullsnes' descriptions. PLOT and RPIX are compared with a second version in the tests, which puts one pixel at a time into the tiles from fullsnes' pictures of them. Other tests compare random runs in pieces of any length, and check that the next interrupt is never earlier than the chip says.

From commit 019fda0af379. Edit this page