emuforge

The main processor

The main processor is a 65C816 in a chip that does much more. It also has the timer for interrupts, a multiplier and a divider, the reading of the pads, and DMA. The chip is called 5A22. This page says what the processor sees, how long it takes, and how it is interrupted.

The processor itself is in crates/snes-65816, which the SA-1 uses too. Everything else here is in crates/snesemu-core/src: bus.rs and math.rs. The architecture has the order of things in a cycle and in a frame.

It rests on
The instructions, and the kind and order of their cyclesWDC's W65C816S data sheet. Decimal numbers are from Bruce Clark's "Decimal Mode"
Which addresses wrapanomie's "Instruction Wrapping", Clark's "65C816 Opcodes" and the data sheet
When interrupts are sampled, how one begins, and WAIanomie's timing document
The console around itthe Super Famicom Development Wiki's "Timing", "Memory Mapping", "Registers" and "Open Bus", the SNESdev Wiki's "Timing", "MMIO registers", "Open bus", "Multiplication" and "Division", and fullsnes

The credits have their addresses.

The 65C816

It has 24 bits of address and 8 bits of data. A bank is 64 KB, and the highest 8 bits of an address are its number.

RegisterBits
A16the accumulator. With the M flag, instructions use its lower 8 bits, and the upper ones stay
X, Y16the indexes. With the X flag, they have 8 bits, and the upper ones become 0
S16the stack, which is always in bank 0
D16where the direct page starts, in bank 0. If its lowest 8 bits are not 0, a direct address takes a cycle more
PC, and PBR16 and 8the next instruction, and its bank. PC stays in its bank, and starts at its beginning again
DBR8the bank of data
P8the flags

It starts in emulation mode, where it works like a 6502. M and X are then always set, and the upper half of S is 1. Games leave this mode with CLC and XCE. Some addresses that wrap in a page for a 6502 also do in this mode, and those of the instructions that the 65C816 added do not.

The processor runs a whole instruction at a time, with one call to the bus for each of its cycles: a read, a read of a vector, a write, or a cycle inside, which uses no address. The bus moves time on in each of these calls. So the rest of the console is always at the right time for an access, even though the instruction cannot stop in the middle. Two bytes of an instruction are read together where nothing can happen in between, and waits are passed. One step is one instruction, the sequence that enters an interrupt, one byte of MVN or MVP, or one cycle while WAI or STP has stopped it.

What it sees

BanksAddressesWhat is there
$00-$3F, $80-$BF$0000-$1FFFthe first 8 KB of main memory
$2000-$5FFFregisters (below), and open bus between them
$6000-$FFFFthe cartridge, and its chip
$40-$7D, $C0-$FFallthe cartridge, and its chip
$7E-$7Fallall 128 KB of main memory
RegistersWhat they are for
$2100-$213Fthe picture
$2140-$217Fthe four ports to the sound side, again and again
$2180-$2183main memory through a port (below)
$2200-$3FFFa chip in the cartridge, if it has registers here
$4016, $4017the pads, bit by bit
$4200-$4206interrupts and the reading of the pads ($4200), WRIO ($4201), and what the multiplier and the divider start with
$4207-$420Athe dot and the line of the IRQ
$420B, $420Cwhich channels of DMA and HDMA are on
$420Dbit 0 makes some of the cartridge's memory faster
$4210-$4213the NMI's flag, the IRQ's flag, V-blank and H-blank, and RDIO
$4214-$4217the results of the divider and the multiplier
$4218-$421Fthe pads, as the console read them
$4300-$437Fthe eight channels of DMA

$2100 to $21FF are on a bus of their own with 8 bits of address, the B-bus. Everything else is on the A-bus. DMA moves bytes from one of these buses to the other.

Main memory through a port: $2181 to $2183 set an address of 17 bits. Each read or write of $2180 uses the byte there, and then the address goes up by 1. So DMA can get to main memory from the B-bus.

Open bus: where nothing answers, a read gives the last byte that was on the processor's bus. Some registers have only a few bits of their own, and their other bits are open bus too. The picture chips have two more of these.

RDIO ($4213) gives what was written to WRIO ($4201), as no device that is emulated pulls its lines low. When bit 7 of WRIO goes from 1 to 0, the picture chips latch their counters. That is one dot later than when $2137 is read at the same time.

How long an access takes

WhatMaster clocks
a cycle inside the processor, without an address6
$2000-$3FFF and $4200-$5FFF in banks $00-$3F and $80-$BF6
$4000-$41FF in these banks, which has $4016 and $401712
the upper halves of banks $80-$BF, and all of $C0-$FF, while bit 0 of $420D is set6
all else: main memory, $6000-$7FFF, and the rest of the cartridge8

So the same cartridge can be used in two ways: slowly in banks $00-$7D, and, if its board allows it, quickly in $80-$FF. The core does not look at what the board allows. It only follows $420D.

Refresh stops the processor for 40 clocks in each line, between two of its cycles (a frame). Main memory needs it. While a DMA runs, refresh adds its 40 clocks to it, which is a guess. This is the second version of the chip, which is what $4210 says too. Documents have no times for refresh and for the start of HDMA's tables but those of the first version, so these are used. That is a guess.

Interrupts

The processor looks at its two inputs just before the last cycle of each instruction. An NMI comes first. An IRQ only counts while the I flag is clear, as it is at that time. CLI, SEI, PLP, REP and SEP change this flag in their last cycle, so the old one still counts. RTI, BRK and COP change it earlier.

Vector (native mode)Vector (emulation mode)
NMI$FFEA$FFFA
IRQ$FFEE$FFFE
BRK$FFE6$FFFE
COP$FFE4$FFF4
reset$FFFC

All vectors are in bank 0. Entering an NMI or an IRQ takes 8 cycles in native mode, and 7 in emulation mode:

  1. It reads the next opcode, and does not use it. (The data sheet has a cycle inside the processor here. anomie's document corrects that for the SNES.)
  2. A cycle inside the processor.
  3. It pushes PBR (only in native mode), the upper byte of PC, the lower one, and P. In emulation mode, bit 4 of that P is 0.
  4. I is set, the D flag is cleared, and PBR is 0. Then it reads the two bytes of the vector.

Before the last of these cycles, it looks at its inputs again. For an NMI and an IRQ that is a guess, and so is that an NMI in the middle of a sequence does not change its vector. It also looks at its inputs when it enters a BRK or COP, as anomie's document says.

WAI waits until an input is active, whatever I is, and then takes 2 more cycles. With I set, the program goes on after it when an IRQ comes. What it does when an NMI's line is still low from one that was already entered is not documented. Here it goes on waiting, which is a guess. STP ends only with a reset.

The NMI and V-blank

The processor's NMI input is an edge. It is the moment at which two things become true together: the NMI's flag, which RDNMI ($4210) has in bit 7, and bit 7 of NMITIMEN ($4200). The bus keeps this edge until the processor enters the NMI.

  • The flag is set 2 clocks into the first line of V-blank, if the game allows NMIs or not.
  • It is cleared when RDNMI is read, and at the start of line 0.
  • If a game turns its NMI on in V-blank while the flag is still set, it gets one. If it turns it off and on again before it reads RDNMI, it gets another.
  • A read of RDNMI just when the flag is set is like any other here. That is a guess.

RDNMI's lower four bits are the chip's version, which is 2 here. HVBJOY ($4212) has V-blank in bit 7, H-blank in bit 6, and in bit 0 whether the automatic reading runs. This H-blank is from dot 274 to dot 1 of the next line.

The IRQ timer

Bits 4 and 5 of NMITIMEN choose when an IRQ comes. HTIME ($4207, $4208) and VTIME ($4209, $420A) have 9 bits each:

Bits 5 and 4An IRQ comes
0, 0never. This also clears the flag
0, 1in each line, at dot HTIME
1, 0at the start of line VTIME
1, 1in line VTIME at dot HTIME

The flag is set 14 + 4 × HTIME clocks after the start of the line. With HTIME 0, or without it, it is set 1,374 clocks after the start of the line before, which is usually 10 clocks into the line. So for HTIME 339 it comes 6 clocks into the next line. There is none for an HTIME above 339, or for a line that does not come. There is also none for HTIME 153 in the last line of a frame, or in the line with 1,360 clocks.

The IRQ stays until it is acknowledged. TIMEUP ($4211) has its flag in bit 7, and reading it clears the flag. A read in the first 4 clocks after it was set does not clear it. The documents say 4 to 8, so the 4 is a guess. A chip in the cartridge uses the same line, but TIMEUP shows only the console's own timer. "Timing" in the Super Famicom Development Wiki says that it shows the cartridge's line too, without clearing it. Games show otherwise. Only the chip's own register can end its interrupt.

For example: an IRQ in line 100

A game wants to change something in the middle of the picture. It writes 100 to VTIME and 0 to HTIME, clears the I flag and writes $30 to NMITIMEN. Its loop then waits. Line 100 starts 136,400 clocks after the start of the frame:

  1. At clock 10 of line 100, which is clock 136,410 of the frame, the flag is set. It is an event, so this does not stop anything.
  2. The instruction that runs then looks at its inputs before its last cycle. If that is at clock 10 or later, it sees the IRQ. If not, the next instruction does.
  3. The processor enters the IRQ: 8 cycles in native mode. The stack is in main memory, so each push takes 8 clocks. The cycle inside takes 6, and the other three read the cartridge, usually with 8. That is 62 clocks.
  4. The game's program reads TIMEUP, which clears the flag. Without this, the IRQ would come again as soon as the I flag is clear.
  5. The game writes the register that it wants to change. Reading TIMEUP and writing a register with 16 bits of address take 24 or 30 clocks each. So the write comes after clock 88, at which the first dot comes out, and the first dots of the line are still the old ones. For a clean edge, a game can wait for H-blank, choose a dot at the end of the line before, or let HDMA do it.

The multiplier and the divider

They are in the processor's chip, beside the processor. They need a fixed number of the processor's cycles, and not of clocks:

Starts with a write ofTakesResults
WRMPYA ($4202) × WRMPYB ($4203), 8 bits each$42038 cyclesRDMPY ($4216, $4217) has the product, and RDDIV ($4214, $4215) has WRMPYB
WRDIV ($4204, $4205, 16 bits) ÷ WRDIVB ($4206, 8 bits)$420616 cyclesRDDIV has the quotient, and RDMPY the remainder

Divided by 0, the quotient is $FFFF and the remainder is the dividend. A product is ready one cycle earlier for each of WRMPYA's upper bits that is 0. Nothing counts while they work. What a read gives is worked out from the number of cycles since the start. These are guesses (math.rs):

  • A product that is not ready has WRMPYB added for the lowest bits of WRMPYA, one bit for each cycle.
  • A division that is not ready still shows the results from before.
  • A start while one of them works does nothing. The documents say that it does not start again, and that it spoils the result, but not in which way.

The picture chips have another multiplier, which needs no time.

Turning on, and reset

When the console is turned onReset
Main memoryevery byte has the same value, 0 if the front end does not choose another one. A console has other values, so this is a guessstays
The processorin emulation mode with I set. D, DBR and PBR are 0. All that the chip does not set is 0 herethe same, but A, the lower bytes of X, Y and S, and some flags stay
NMITIMEN, the channels that are on, and bit 0 of $420D00
WRIO$FF$FF
HTIME and VTIME$1FF each, so they give no IRQ in a normal framestay
DMA's registersall bits are 1stay
The beamline 0 of field 0. That the first cycle begins at clock 0 is a guessline 0 of field 0. The master clock goes on

Then the processor runs its reset sequence: two cycles inside, three reads from the stack that move S down by 3, and the vector. That S moves by 3 follows from the data sheet's table, and was not measured by any source that was found.

What was tested, and what is open

The instructions agree with SingleStepTests' 65816 tests in all 5,080,400 cases: registers, memory, and the kind of each cycle, with the address and value of those that use the bus. How to run them says what these tests are. They have no interrupts. The crate's own tests have those, WAI, STP, the reset, the documents' examples and the number of cycles of each opcode. bus/tests.rs has the console's times, as the documents have them.

  • One question is open. In emulation mode, if D's lowest 8 bits are not 0, documents differ on where the second byte of a pointer for (d,X) is when the first is at the end of a page. Here it is in the next page, as in the data sheet. 264 of the cases depend on it. The notes of gilyon's test program say the other thing. None of these sources names a run on a console (address.rs).
  • WDM: its second cycle is inside the processor here, as in the tests. Clark says that it reads a byte.
  • Instructions that change a byte in memory have a cycle between the read and the write. That the SNES's processor writes nothing in it in emulation mode is a guess.
  • There are no other versions of the chip, and none of their faults, such as those with DMA and HDMA.
  • $4218 to $421F all change at the end of the reading, which is a guess.

From commit 019fda0af379. Edit this page