The SA-1
The SA-1 is a second 65C816 in the cartridge, with memory and hardware of its own. Super Mario RPG and Kirby
Super Star have one. It runs at 10.74 MHz, where the main processor has 2.68 or 3.58 MHz in most
memory. Both run at the same time, and both can use the ROM and all of the cartridge's memory. It runs a
part of the game's own program, and needs nothing else.
It is in crates/snes-sa1, with the processor from crates/snes-65816. Files below are in its src. How it
meets the bus is in crates/snesemu-core/src/chip.rs. What is here rests on fullsnes ("SNES Cart SA-1 ..."),
the Super Famicom Development Wiki's two pages about it, and the SNESdev Wiki's "SA-1 Pinout"
(credits). fullsnes calls much of it unknown. Each module starts with its own
guesses.
What both processors see
| Banks | Addresses | Main processor | SA-1 |
$00-$3F, $80-$BF | $0000-$07FF | its main memory | I-RAM |
| $2200-$23FF | registers | registers |
| $3000-$37FF | I-RAM | I-RAM |
| $6000-$7FFF | 8 KB of BW-RAM, chosen by $2224 | 8 KB of BW-RAM or of the bitmap, chosen by $2225 |
| $8000-$FFFF | the ROM, as LoROM | the same |
$40-$4F | all | BW-RAM: 256 KB, four times. Tiles, during a conversion of type 1 | BW-RAM |
$60-$6F | all | | the bitmap |
$C0-$FF | all | the ROM, as HiROM | the same |
- I-RAM is 2 KB in the chip. The SA-1 also has it at
$0000, so its stack and its direct page are there.
- BW-RAM is the cartridge's RAM, up to 256 KB. A battery can keep it. The chip can also keep I-RAM with a
battery on some boards. The core does not do that. Its crate can.
- The ROM has up to 8 MB, in blocks of 1 MB.
$2220 to $2223 choose which block is in each of four
places of 1 MB in banks $C0-$FF. In the upper halves of banks $00-$3F and $80-$BF, there are four
places too. They have the same blocks if bit 7 of their register is set, and otherwise the first 4 MB. Both
processors see the same. Only the main one can change this. (The wiki's formulas for these lower banks leave
out half of the ROM. Its author says that they are not well researched. fullsnes' rule is used here.)
- The bitmap shows BW-RAM as one dot for each address. A dot has 4 bits, or 2 if bit 7 of
$223F is set.
The lowest bits are the leftmost dot. So the SA-1's program can change a single dot without touching its
neighbours. That the other bits of a byte read as 0 is a guess.
- Banks
$50-$5F and $70-$7D have nothing for the SA-1, and $50-$7D has nothing for the main
processor, where it is open bus.
What each one may write is in registers for each side. $2228 says how large the first part of BW-RAM is
that can be protected. $2226 (SBWE, for the main processor) and $2227 (CBWE, for the SA-1) turn on
writes to it. It is only protected while both are off. fullsnes says only of a part of this that it is "probably" so. If
each register is only for its own processor, Kirby's Dream Land 3 does not go
on. That they only matter for the protected part is a guess.
$2229 and $222A have a bit for each 256 bytes of I-RAM, each for its own side. Documents differ on what
that bit means. Here, as in fullsnes, a 1 allows writes. With the other meaning, which the wiki has, many
games with an SA-1 stop early. Protection is only for the processors, and not for DMA, which is a guess.
Registers only work for their own side, but for $2231 to $2237 of the DMA, which both can write. One that
the other side has reads as nothing is there: open bus for the main processor, and 0 for the SA-1.
Time
The SA-1 has a cycle in each 2 master clocks, which is 10.74 MHz. What one access takes is in one place
(timing.rs):
| Access | Master clocks |
| I-RAM, the ROM, registers, and cycles without an access | 2 |
| BW-RAM | 4. Documents have this for DMA only, so this is a guess for the processor |
| A memory that its own DMA also uses, if the DMA goes first | 2 more. How much it is, and that it is the same for each access, are guesses |
When both processors use the same memory, the SA-1 has to wait on the console. Documents name the three
cases (ROM, BW-RAM and I-RAM), and not how long that takes. The crate has a rule for this, which adds 2 clocks
to each access to a memory that the main processor uses. The core does not tell it what the main processor
uses. So here, the SA-1 never waits for it. The chip never makes the main processor wait.
When each one sees the other
The SA-1's processor runs whole instructions. An instruction that starts before the time up to which the chip
has to run can end later, by less than 46 clocks. The next call takes that into account, so it does not add
up. Everything else in the chip runs exactly up to the time. These rules keep both sides right:
- The main processor never sees anything early. If an instruction writes after the time that is reached,
its write is held. It is done when the chip gets there, at its own clock. That is true for memory,
registers, messages, interrupts and the start of a DMA. So the main processor sees each write at its exact
clock.
- The SA-1 can see a write of the main processor late, and never early. An instruction that has already
begun does not see it, and the next one does. That is less than 46 clocks. An interrupt can be one more
instruction late. Interrupts from the timer and from DMA come at their own time.
- All accesses have one order. Each processor's own ones are in the order of its program. If a program
waits for a flag and then reads what was written before that flag, it finds it. If two programs each set
their own flag first and then look at the other's, at least one of them sees the other.
- How the time is cut into pieces changes nothing.
What can go differently is a race within a single instruction, for example if one side uses TSB on a byte
that the other side writes. It is a race on the console too, and is a little wider here.
For example, the main processor reads a message at clock 1,000, and an instruction of the SA-1 that began
at clock 996 would write it at clock 1,002:
- The main processor's read asks the chip to run up to clock 1,000. The instruction starts, as 996 comes
before that.
- Its write is at 1,002. So it is held, and the main processor reads the old message.
- If it reads again at clock 1,030, the chip first does the held write, at 1,002. The main processor gets the
new message.
- If the main processor wrote a byte at clock 1,000, the instruction that began at 996 did not see it. The
next one does.
How they talk
A small message of 4 bits in each direction comes with each interrupt request:
| From | Written to | The other one reads | Its interrupt ends with |
| Main processor | CCNT ($2200): the message (bits 0-3), NMI (4), reset (5), wait (6) and IRQ (7) | CFR ($2301) | CIC ($220B) |
| SA-1 | SCNT ($2209): the message (bits 0-3), which vectors of the main processor are replaced (NMI 4, IRQ 6), and IRQ (7) | SFR ($2300) | SIC ($2202) |
CIE ($220A) and SIE ($2201) say which interrupts are on. One that is off still sets its flag, which is a
guess. For larger things, they use I-RAM and BW-RAM.
- The SA-1 starts in reset. The main processor writes its reset vector to
$2203 and $2204, and then
clears bit 5 of CCNT. The SA-1 starts at the next of its cycles.
- Reset and wait stop only its processor. Its timer and DMA go on, which is a guess.
- The SA-1's vectors come from registers that the main processor writes: the reset's, the NMI's and the
IRQ's (
$2203 to $2208). Those of BRK and COP are in the ROM. In emulation mode, BRK and the IRQ
have the same address, so BRK gets the IRQ's vector there. That follows from the address, and no document
says it.
- The SA-1 can replace two vectors of the main processor: the NMI's and the IRQ's in native mode, with
$220C to $220F. The bus asks the chip for each byte of a vector that the processor reads. The chip
cannot tell a vector from data, as the pin that says so (VPB) does not go to it. So the same four addresses
($00:FFEA, $FFEB, $FFEE and $FFEF) also give the new value to a normal read.
Their interrupts:
| To | From |
| The main processor (IRQ) | a request in SCNT, and the first tile of a conversion of type 1 |
| The SA-1 (IRQ) | a request in CCNT, its timer, and the end of its DMA |
| The SA-1 (NMI) | a request in CCNT. It is an edge, as for the main processor |
For example: a job for the SA-1, step by step
The SA-1 runs a loop that waits for a job with WAI. The IRQs for jobs are on in CIE, and its I flag is set.
The main processor has turned on the IRQ from the SA-1 in SIE:
| Main processor | SA-1 |
| 1 | writes what is to be done into I-RAM at $3000, and where the data is | waits in WAI. Its time passes at once, and nothing is run |
| 2 | writes $81 to $2200 (CCNT): an IRQ, with the message 1 | has its flag for this IRQ in CFR from this clock on |
| 3 | goes on with something else | WAI ends, as an input is active. I is set, so it goes on after it. It reads CFR, finds bit 7 and the message 1, and writes $80 to $220B (CIC) to clear the flag |
| 4 | | does its job, and writes the result into BW-RAM |
| 5 | | writes $82 to $2209 (SCNT): an IRQ, with the message 2. That is the write that the main processor sees at its exact clock |
| 6 | The bus looked at the chip when it could be ready at the earliest, and now sees its IRQ. The processor enters its IRQ after the instruction that sees it | goes back to its WAI |
| 7 | reads $4211 (TIMEUP), and finds that it was not the console's own timer. It reads SFR ($2300), finds bit 7 and message 2, and writes $80 to $2202 (SIC) | |
| 8 | reads the result in BW-RAM. Everything that was written before step 5 is there | |
A chip that could ask for an interrupt any time has to be looked at often. While its program runs and its
interrupt is on in SIE, each of its instructions could write to SCNT. Its first write could come after 3
cycles. So the core looks again every few clocks. If its interrupt is off, or it is stopped or in WAI, the
next time is much later, or never.
Its own hardware
- The multiplier and divider (
$2250 to $2254, read at $2306 to $230B): signed products of 16 × 16
bits, division of a signed by an unsigned number, and a sum of products with 40 bits. They take 5 cycles, or
6 for the sum. If a result is read earlier, it is the one before, which is a guess. A division by 0 gives 0
for both.
- The reader of bits (
$2258 to $225B, read at $230C and $230D): gives 16 bits from any bit of the
ROM, and goes on by 1 to 16 bits. It is for data with different lengths. The four blocks of the ROM do not
matter for it. That is fullsnes' "probably".
- The timer (
$2210 to $2215): one count for each 4 clocks. It counts dots and lines (341 dots, and 262
or 312 lines), or up to 18 bits. It can interrupt the SA-1 at a dot, a line or both. fullsnes calls the
meaning of what is written to it "totally unknown". Here, it is what the count is compared with. That is
fullsnes' more likely guess, and it is how the console's own timer works. This timer is not tied to the
picture.
- DMA (
$2230 to $2239): copies from the ROM, BW-RAM or I-RAM to I-RAM or BW-RAM. ROM to I-RAM takes 2
clocks for each byte, and everything else takes 4. Each byte moves at its own clock, and at the last one, CFR's flag
is set. The SA-1's processor stops until a DMA from the ROM is done. No document says so. Jumpin' Derby needs
it. Between BW-RAM and I-RAM, it runs on, which is a guess, and sees each byte from its clock on.
A bitmap becomes tiles
The SA-1 can change pictures that are bitmaps into the format of the picture's memory, in I-RAM in a buffer of
two tiles:
- Type 2: the SA-1's program writes dots to
$2240 to $224F, 8 for each row of a tile. A write to
$2247 or $224F makes a row. That is a guess, as is that it takes no time.
- Type 1: the main processor's DMA does it. While it is on, the main processor's reads of banks
$40-$4F do not give BW-RAM, but tiles:
| |
| 1 | The SA-1 makes a bitmap in BW-RAM. It writes $B0 to DCNT ($2230): DMA on, a conversion, type 1. Then it tells the main processor that it can start |
| 2 | The main processor writes where the bitmap is to $2232 to $2234. It writes the number of colours and the width to CDMA ($2231), and where the buffer is in I-RAM to $2235 and $2236. The write to $2236 starts it |
| 3 | The chip makes the first tile. It is ready after as long as a DMA would take to read it, and then SFR's bit 5 is set. A tile with 16 colours has 32 bytes, so this is 128 clocks. If bit 5 of SIE is set, the main processor has an IRQ then. The chip knows this time, so the core looks there |
| 4 | The main processor starts its DMA to $2118, from the address of the bitmap |
| 5 | The bus gives the chip each of the DMA's bytes, with its address and clock. The lower 18 bits of the address, divided by the size of a tile, say which tile it is. What is left says which byte of it. When a new tile begins, the chip converts it. The tiles go into the two halves of the buffer in turns. The DMA gets its byte from there |
| 6 | After the last byte, the main processor writes $80 to $2231. Then its reads of $40-$4F are BW-RAM again |
How an address becomes a tile is our guess, as the documents do not say. A row of tiles has as many bytes
as the 8 lines of the bitmap that it comes from. So the address in the bitmap and that of a tile are at the
start of a row at the same time. Only the main processor's banks $40-$4F change: it can use BW-RAM through
$6000-$7FFF as ever. The tiles after the first one take no time of their own. That is because a DMA at
BW-RAM's speed is twice as fast as the main processor's.
How the core uses it
- For registers, I-RAM and BW-RAM, the chip runs up to the clock of each access, by the main processor or DMA. Only reads while
its program waits do not need that.
- The ROM is read without running the chip. Only
$2220 to $2223 matter for it, and only the main
processor writes them. The four bytes of the vectors are different: the chip runs up to the clock first.
- At its next event and at the end of the frame, it also catches up.
- What costs nothing: when the chip is in reset, waits, or stopped with
STP, its processor does nothing.
In WAI, time passes at once, up to its next interrupt or the time that has to be reached.
- A loop that only waits is passed too. When its processor goes back and is exactly as it was, has written
nothing, and read nothing that changes by itself, it does the same again. This ends with any write from
outside, and before the timer's next flag (
waiting.rs).
What is a guess, and what is not there
The guesses above are marked. These are more:
$230E (VC), the chip's version, is 0, as no document has its value. $2261 and $2262, which games
write, do nothing. No document says what they are for.
- What has no known value when it is turned on is 0, and stays in a reset. A reset of the console stops
the processor, and keeps I-RAM.
- The overflow flag of the sum stays until the sum is set to 0. Operations that are started while one
still works are done after it.
- A DMA of 0 bytes moves nothing, and sets its flag at once. Another start ends the DMA that runs. So does
turning DMA off, or a conversion.
- A vector that a register gives takes the SA-1 one cycle for each byte.
- The refresh of the console does not stop the SA-1.
Status has the results of all releases that were tried.
Tests run random programs, with random accesses by the main processor, in pieces of any length. They compare
all of the state, also with the waits run in full, and check that the next event is never too late.
tests/waiting.rs has the rules for both processors: what one asks, the other one answers, and two programs
that keep each other out never run at the same time.