emuforge

Architecture

A Super Nintendo has two processors that hardly know each other. One runs the game, and can stop for DMA and refresh. The other makes sound, with a clock and memory of its own. Two picture chips draw with a third set of memory, and some cartridges have a processor too. Most of this core's work is to show each part the others as they are at that time, while it runs as little as possible. This page says how. The pages about the hardware say what each part does.

The parts

CrateWhat it isWhat it needs from outside
crates/snes-65816the 65C816, for the main processor and for the SA-1a bus. Each call is one of its cycles
crates/snes-spc700the sound processor. It can stop after any cyclea bus. Each call is one of its cycles
crates/snes-dspthe S-DSP, which makes the sound, one of a sample's 32 steps at a timethe sound memory
crates/snes-ppuboth picture chips, with their memories, registers and the picturewhere the beam is, at each access
crates/snes-cx4, snes-gsu, snes-sa1chips in cartridges that run by themselves: the Cx4, the Super FX and the SA-1the time, and the cartridge's ROM and RAM, for each call
crates/snes-dsp1, snes-sdd1, snes-spc7110, snes-obc1, snes-srtcchips that mostly only do something when they are used: the DSP-1 (in part), the S-DD1, the SPC7110 with its clock, and the OBC1 and the S-RTCwhat it uses of the time, the ROM and the RAM, for each call
crates/snesemu-corethe console: time, the bus, DMA, interrupts, cartridges, controllers, the sound processor's timers and ports, and our own boot programa game and the buttons

apps/snesemu joins the core to forge's app, which has the window, the menu, the sound device and the gamepads. It also gives the core the player's time, for cartridges with a clock. apps/snesemu-lab runs the core without a window, with forge's lab. apps/snesemu-dev is for developers: it times the core, and compares builds frame by frame (Development). No crate in crates/ has unsafe, and none of them uses threads, files, the computer's clock or random numbers (but tests can read files). No other crate knows of the core. They know the console only as a bus, a time or a place in the picture, so each one can be tested alone. Only the SA-1's uses another one, for its 65C816.

Inside snesemu-core:

FileWhat is in it
console.rsloading a game, running a frame, what comes out, what a battery keeps, and savestates
bus/state.rswhat is in a savestate, and the checks of one that is loaded
bus.rswhat is at each address, time, interrupts, and the main processor's registers
dma.rsDMA and HDMA
math.rsthe multiplier and divider
cartridge.rs, chip.rscartridges, and how their chips meet the bus
input.rscontrollers
sound.rs, sound/timer.rs, boot.rsthe sound side, with its timers and our boot program

How it is joined

                                 the bus (bus.rs), which has the time
        +--------------+--------------+--------------+----------------+--------------+
        |              |              |              |                |              |
   main processor   128 KB of     both picture    DMA and HDMA,    the ports     the cartridge:
   (65C816)         main memory   chips (PPU)     the pads, the    $2140-$2143   ROM and RAM,
                                  VRAM, CGRAM,    multiplier and      |          and its chip
                                  OAM             the divider         |
                                                                   the sound side (sound.rs)
                                                                   SPC700, timers, S-DSP,
                                                                   64 KB of sound memory

DMA has no processor. It stops the main one, and uses the bus in its place. The cartridge keeps its ROM and RAM, and the bus lends both to the chip for each call. So a chip has no copy of them that could be old.

Clocks

All parts share the console's master clock, counted from when the console was turned on. It has 945,000,000 / 44 (about 21,477,273) clocks a second on an NTSC console, and 21,281,370 on a PAL one. A reset does not set it back.

PartIts clockOn NTSC
Main processor6, 8 or 12 master clocks for each cycle, depending on the address3.58, 2.68 or 1.79 MHz
Picture4 for each dot, and 6 for two dots of most lines. A line has 1,3645.37 MHz
DMA8 for each byte2.68 MB a second
Super FX1 or 2 for each cycle, as the game chooses21.48 or 10.74 MHz
SA-12 for each cycle10.74 MHz
Sound processor and S-DSPa crystal of its own: 1.024 MHz, with a sample in each 32 cycles5,632 cycles in 118,125 master clocks, about 1 in 20.97
Cx4a crystal of its own: 20 MHz176 cycles in 189 master clocks

On PAL, the sound processor has 102,400 cycles in 2,128,137 master clocks, and the Cx4 2,000,000 in the same time. These are fractions of whole numbers. The sound side keeps what is left of a cycle for the next time, and the Cx4 works out its cycle from the whole time. So no time is lost, however often or rarely a part catches up.

Two of these are choices, and not facts:

  • No two consoles have the same sound clock. Consoles that people measured are up to about 0.5% faster than the 1.024 MHz that the crystal says (sound.rs). A fixed ratio gives the same result on every machine.
  • The Super FX also has its own crystal, of 21.44 MHz. Here it uses the console's clock (crates/snes-gsu/src/lib.rs).

A cycle of the main processor

The main processor leads, one bus cycle at a time. It calls the bus for each of its cycles: to read, to write, or to do something inside. In each call, things happen in this order:

  1. What is due is done. The bus compares the clock with one time, the next at which anything is due. Most of the time nothing is, and this comparison is all.
  2. The cycle's 6, 8 or 12 clocks pass.
  3. The access happens, at the end of the cycle. When in a cycle it happens on the console is not documented, so that is a guess.

What is due is of two kinds:

What they areWhen they are done
Eventsa new line, the NMI's flag, the IRQ's flag, the start and end of the automatic reading of the pads, and the next time at which a cartridge's chip could change its interrupt. They do not take time. They only change what can be seenbefore a cycle. Also in the middle of one, before any of the console's registers is used, and before the processor looks at its interrupts
Pausesrefresh, HDMA and DMA. They stop the processoronly between two cycles

So an event that comes while a cycle runs is seen by the access at its end. A pause that comes then waits for the next cycle. Events are done in the order of their times, each as of its own time. An event that is found late still has the right clock: the next line starts where the last one ended, and not when somebody looked.

When several pauses are due, refresh comes before HDMA, and both come before a DMA that was started. They are looked for again after each one, as each takes time. A DMA looks before each of its bytes. So refresh and HDMA also come in the middle of one.

Interrupts are sampled just before the last cycle of each instruction. If one is there, the processor enters it after that cycle (the main processor).

A frame

An NTSC frame has 262 lines, and a PAL frame 312. With interlace, the first of two frames has one more. This is an NTSC frame without interlace, with the 224 lines of a normal picture. Clocks are from the start of the line, if nothing else is said:

LineClockWhat happens
00V-blank ends, and so does the NMI's flag. The picture chips start a new frame with the other field. STAT78 shows that field from clock 4 on, and the flags for too many objects are cleared, unless the screen is off
024HDMA begins the tables of all channels that are on
eachabout 536refresh stops the processor for 40 clocks. The first one is at 538. Each later one is at the multiple of 8 clocks after the last one that is nearest to 536, so they are at 534 and 538 in turns while lines have 1,364 clocks
each1,096HVBJOY says that H-blank begins. It lasts until clock 4 of the next line
0 to 2241,112HDMA sends what the next line needs
1 to 22488 to 1,112the 256 dots come out. Line 1 is the first row of the picture, and line 224 the last. Objects are looked for one line earlier, two dots for each one
2250V-blank begins, and the picture is finished. This is 306,900 clocks after the frame's start. There is no HDMA from here on
2252RDNMI's flag is set. If NMITIMEN turns it on, the main processor has an NMI
22540the address of the object memory is loaded again, unless the screen is off
225130 to 385if NMITIMEN turns it on, the automatic reading of the pads starts. It takes 4,224 clocks, so it ends in line 228
240in every second frame, this line has 1,360 clocks, and not 1,364
2611,364the frame ends, and line 0 follows

A game can turn on an IRQ at any dot and line (the main processor). With overscan, the picture has 239 lines and V-blank begins in line 240. Some of these times depend on registers while the frame runs: the picture says which.

What a front end sees

A front end makes a Console with load, which takes a game's file and the settings. These are which console it is (NTSC or PAL, or as the cartridge says), what every byte of main memory has at the start, and the player's time, for a cartridge with a clock. Then it calls run_frame with the buttons of both pads, and takes the picture and the sound:

  • run_frame runs until the next V-blank begins, and then to the end of that step of the main processor: an instruction with the pauses in it, or the sequence that enters an interrupt. So the game's own work in V-blank is in the next call. That is also where the automatic reading of the pads normally starts. So it reads the buttons of the call that it is in.
  • At its end, everything catches up: the picture, the sound side and the cartridge's chip.
  • picture gives rows of colours with 15 bits (5 each for red, green and blue), with the brightness already in them. It is 256 wide, or 512 if any line of the frame has high resolution, where the other lines have each pixel twice. It is 224 or 239 high, and twice that with interlace. Then every second row is from the frame before. The rows are 512 pixels apart.
  • sound gives 16-bit samples, left and right in turns, at 32,000 a second for each side. A frame has no fixed number of them: 532 or 533 on NTSC without interlace, and 640 on PAL. The first call starts at line 0, so it is shorter than a frame and has fewer.
  • frame_rate is a fraction: about 60.0988 frames a second on NTSC, and 50.0070 on PAL. It is for a picture without interlace.
  • battery is what a battery keeps, and load_battery puts it back. With a clock, that is more than the cartridge's RAM, and it has to come right after load.
  • save_state and load_state are for savestates.

The same game, settings and buttons always give the same picture and sound. The others of its functions are for tests and tools. emuforge's shared interface for cores and its other modes are not here yet (Status).

How they talk

Each pair of parts has its own places to meet. Where one is behind, it catches up first, so that each sees the other at the right time:

BetweenWhereWhat makes it right
Main processor and picture$2100 to $213F. The V-blank NMI comes from the bus, which asks the picture chips how long lines and frames arethey catch up, as far as the access needs it
Main processor and sound processorfour ports: $2140 to $2143 on one side, and $F4 to $F7 on the other. Each direction has its own four bytes, and there is no interruptthe sound side runs up to the clock of the access, and never past it
Sound processor and S-DSP64 KB of shared memory, and $F2 and $F3it is as if they ran in step: in each cycle, the S-DSP does its step first, and then the processor does its access. The S-DSP really does its steps later, but before anything that could tell (below)
Sound processor and timers$F0, $F1 and $FA to $FFthey are worked out at each use, from the time since the last one
Main processor and DMA$420B, $420C and $4300 to $437F. DMA stops the processorit runs in the processor's place, in the same time
DMA and everything on the B-bus$2100 to $21FF, one byte in 8 clocks: the picture, the sound ports, and main memory through $2180each byte is an access at its own clock, as the processor's are
Main processor and pads$4016 and $4017, or the 16 bits that the console readsthe reading is an event with its own start and end
Main processor and Cx4$6000 to $7FFF, the ROM and the RAM, and an IRQthe chip runs up to the clock of the access. A quiet one has nothing to do
Main processor and Super FX$3000 to $34FF, the ROM and the RAM, and an IRQ. Only one of them has the ROM or the RAM at a timeit runs up to that clock when its registers are used, and when the ROM or the RAM is used while it has it
Main processor and SA-1registers, 2 KB of shared memory, the cartridge's RAM, an IRQ in each direction, an NMI to the SA-1, and vectors that each can change for the other. Both can use everything at the same timeit runs up to that clock at each access, but for the ROM, which needs nothing. Its writes happen at their exact clock (why)
Main processor and DSP-1two registers, whose place depends on the cartridgeat each access, the chip looks at the clock, to know if it is still busy
Main processor and S-DD1$4800 to $480F, the ROM and the RAM. It also sees each write of the main processor to $4300 to $437Fnothing in it depends on the time
DMA and S-DD1each byte that a DMA takes from the A-bus for the B-bus. The S-DD1 is asked first, and can give a decompressed byte in place of the ROM'sit takes the same 8 clocks. HDMA never asks
Main processor, DMA and SPC7110$4800 to $4842, bank $50, the ROMs and the RAM. DMA reads it like the main processorresults are there at once. Only its clock depends on the time, and it catches up when it is used
Main processor and OBC1$6000 to $7FFF, with eight registers near the endnothing in it depends on the time
Main processor and S-RTC$2800 and $2801it catches up when it is used
Anything and the battery's memorya front end takes it after a framethe chip catches up at the end of the frame. A clock's part is only taken again when it has to be

The main processor and the sound processor can only wait for each other by asking again and again. A game that uploads sound does this thousands of times. The sound page goes through an upload from both sides.

What is left behind

Most parts are behind most of the time. Each catches up before something could show or change what it left out:

PartIt can be behind untilWhy nothing can see it
Both picture chipsa register that could show the difference is read, one that changes the picture or the objects is written, or the frame endsthe frame's lines and the NMI come from the bus. The flags for too many objects, the address of the object memory, the product while mode 7 draws, and the field are the only things that change by themselves. Reading one of them brings that far enough
The sound sideone of its ports is used, or the frame endsfour bytes are all that both sides share, and nothing in it can interrupt the main processor
The S-DSP, behind the sound processorone of the registers that it changes is read, a write to a register could do something, memory that it could use is changed, its echo's memory is used, or the frame endsit says for some time ahead which memory that could be. That can be too much, and never too little
Its three timersone is read or changedonly the time, and what was written to them, decides what they count
The multiplier and the dividertheir result is readthey know the cycle in which they were started
A Cx4, a Super FX or an SA-1the main processor or DMA uses something that it could have changed, the time comes at which it could interrupt, or the frame endsall that it can do to the main processor goes through these. It says itself when it could next change its interrupt
A clock in a cartridge, and whether a DSP-1 is still busyit is used, or the frame endsthey have no interrupt, and only the time decides what they give

Being behind has to be the same as running step by step. Tests compare both with random accesses, for the picture, the sound side, the timers, the S-DSP, the Cx4, the Super FX, the SA-1, the DSP-1 and the clocks. Console::set_exact draws the picture dot by dot, with code of its own. It also runs the sound side cycle by cycle, with the S-DSP in step, and each step of the main processor. Cartridges' chips have no such switch. What was compared in games is on the page about performance.

How a chip in a cartridge is joined

A chip knows nothing of the core. It is plain data, without the ROM and the RAM. Each has the same few functions:

FunctionWhat it does
run to a timedoes all that comes up to that clock. An instruction that has begun by then is not cut, and each chip's page says what follows from that
read and writewhat the main processor and DMA see at an address, after running up to that time. No value means that nothing is there
interruptwhether it asks for one now
next eventthe first time at which that could change by itself. It can be too early, and never too late. Each chip tests that with random states
resetat a time up to which it ran

The core does the rest, in chip.rs and bus.rs:

  1. The kind of cartridge in the header says which chip it is. No list of games does.

  2. Its addresses become the chip's. The table of what is at each address has pages of 8 KB, and those that the chip answers for go to it. Where a page is the cartridge's ROM or RAM, it is used directly while the chip leaves that memory to the main processor. With a chip that has banks, the core finds such a page again after they have changed, when it is next read. Its registers between the console's own ($2200 to $3FFF) are found one by one, as these pages have both. So are those from $4380 on, as the S-DD1 and the SPC7110 have them. The rest depends on whether the chip has all of the cartridge behind it:

    • The Cx4, the Super FX, the SA-1, the S-DD1 and the SPC7110 have. Where the chip answers nothing, there is open bus. The cartridge's own LoROM or HiROM map does not count, as the chip can be behind, or keep the ROM or the RAM for itself. A test checks this for all pages and these chips.
    • The DSP-1, the OBC1 and the S-RTC have not. The cartridge's own map counts for everything else. With an OBC1, the map does not show the RAM, as the chip gives it at another place.
  3. Its next event is one of the bus's events. When it comes, the chip runs up to then, and says when the next one could be. A Super FX then runs ahead, at most 2,000 clocks, so that its next event is later (how). The core looks at the interrupt and the next event again after each access that could have changed them.

  4. Its interrupt and the console's own IRQ share one line. TIMEUP shows only the console's, and reading it clears only that.

  5. Only the SA-1 replaces vectors. The processor tells the bus when it reads a vector and not data. Then the SA-1 is asked first. The Cx4 and the Super FX give other bytes in place of the ROM, so nothing special is needed.

  6. Only the S-DD1 has to do with DMA. The bus tells it of writes to the DMA's registers, and DMA asks it for each byte.

  7. A clock adds its own part to what the battery keeps. It also says which address a game changes it through.

A game without a chip has no page and no event for one.

Chips from another crate

The DSP-1 to DSP-4 and the ST010 run a program of their maker's. What is public is only enough for a part of the DSP-1. So another crate can have these chips. It can also have another license, as the core does not depend on it and does not name it. It depends on the core, and implements NecChip from nec.rs. A front end gives its function to Console::load_with.

Who does it
Which chip a cartridge has, its place, and what the battery keepsthe core
What the chip does with its two registers, and with the ST010's memorythe other crate

The other crate is asked first. The core has its own DSP-1 for when there is none. All other chips of this kind are then refused by name. The chip gets the time with each access, and does its work up to then. It has no interrupt, so it is no event of the bus. Running it without an access changes nothing that can be seen.

A state says whose chip it has. It has the section NEC , with the name and version of the chip that made it, and only goes into the same one. A core without the other crate has the section DSP1 in a state of a game with a DSP-1, so that neither kind of state goes into the other kind of build. The tests have a made-up chip for all of this, with damaged states too.

It is not a feature of the core in Cargo. A feature is turned on for a whole build when one of its crates asks for it. With this crate, whoever wants the chips has to name it.

Doing nothing

What nesemu and mdemu found was part of the design from the start: not doing work saves the most, and making each step a little cheaper mostly does not help. The exact way came first, and it stays. Tests compare the shortcuts with the exact way. Performance explains each one, why it changes nothing, what was measured, and what did not help. In short:

  • One time to compare with. A cycle compares the clock with the time of whatever is due next, and looks at nothing else. Each part says when it could next be seen. A time that is too early only costs time. It is never too late, and whatever could bring it forward makes the core find it again.
  • A table of pages. Each 8 KB of addresses has an entry that says what is there, and another for how long an access takes. Only registers, and memory that a chip keeps for itself, go through code of their own.
  • The picture catches up in one go, over as many lines as there are, and a line is drawn as a whole where nothing changes in it. Objects, tiles, windows and colours are kept until what they were made from changes. A line that is as it was in the last picture is not drawn again. Writing the same value again to most registers, or the same data to memory, makes nothing catch up.
  • The sound side catches up in one go, with whole instructions, and the S-DSP makes many samples at once.
  • Waits are passed: those of the main processor, the sound processor and the SA-1. The times at which they could end are found, and nothing is run until then.
  • The timers, the multiplier and the divider are worked out when they are used. They do not count in each cycle.
  • Chips that run by themselves are left alone until they could interrupt, or until something that they could have changed is used. A Super FX runs ahead, and goes back when something could tell.
  • Nothing is allocated while a game runs.

Frames without a picture or sound are in the picture chips and the S-DSP, with their tests, but the core does not use them. Both can leave out what can only be seen or heard, and all that a game can see stays the same.

This is not built:

  • Waits for a register: the main processor's for V-blank and for a chip or the sound processor, and all of the Super FX's.
  • Reading all of an instruction's bytes at once. Two bytes of it are read together. More was slower (why).

State

All state is plain data, without references, threads or anything that is hidden on a stack. The sound processor can stop within an instruction, and what is between two of its cycles is in its fields. A Console can be copied between any two steps, and the copy goes on in the same way. Each processor, picture chip, sound chip and cartridge chip can also be compared. What was found ahead is not part of a comparison.

Savestates

A state has everything that can change, and nothing of the game's file. It can be made between any two steps of the main processor, and the console then goes on as without it. It has both processors, all memory with the cartridge's, the registers, latches and times of everything, and the cartridge's chip with its clock. It does not have the finished picture and the frame's sound. In the middle of a picture, or when it was interlaced, it has what later pictures need, so they are the same. Until the next frame is finished after loading, there is no sound, and the picture is black or, where the state has it, the last one.

  • Before a state is made, everything catches up to the same clock. So one moment always has the same state, however far behind each part was. Catching up changes nothing that follows.
  • What follows from the rest is not in it, such as what each page has and when something is next due. It is found again after loading.
  • Each part is a section with a version, which uses forge's forge-state. Tests record the layouts, so that a change is seen. A section that changes gets a new version, and the old one is still read. So far, all have their first version.
  • A state is checked before it is used. It has to be from the same kind of console, with the same map, sizes of ROM and RAM, and chip. No part may have more to catch up with than the next event allows. One that cannot be used changes nothing. It does not say which game it is from. forge's app adds that.

Tests save and load at random moments, with random programs, on both consoles and with each chip. They compare consoles that went on from a state with one that was never saved: step by step, in later frames, and by their own states. Others check that saving changes nothing that follows, also before a reset and with the exact picture, and that a state from another console or cartridge is refused. For both plain maps and each chip, states are damaged in many places. Each one is refused and changes nothing, or it runs. With games, only states of two were tried, in the app: they were loaded again after a restart.

From commit 019fda0af379. Edit this page