SAROO and Sega's RAM-cart timing: why the menu froze, and how it's fixed
Background: what "timing" means here
The Saturn talks to the cartridge over the A-Bus. The SCU register ASR0 sets how long each cart access may take. Sega's own RAM cart timing (technical bulletin #47) is 0x23301FF0: 3 wait states, and
no external wait. The cart must answer within a fixed time, about 140 ns, and the Saturn takes whatever is on the bus at that moment.
The SAROO firmware uses 0x38803880 instead: 8 wait states,
external wait on, which lets the cart hold the Saturn until its data is ready.
Why the SAROO needs that slower timing
The SAROO has no real RAM cart. Cart accesses go through the FPGA to 64 MB of SDRAM, which the STM32 microcontroller also uses for its own work. SDRAM can't answer instantly: it needs refresh cycles, and if the STM32 is in the middle of an access, the Saturn's request has to wait for it to finish. With external wait on, the FPGA simply holds the Saturn until the data is ready, so every read is correct.
At Sega's timing that safety net is gone. The stock FPGA couldn't meet the 3-wait deadline even when the SDRAM was idle. That's why the firmware patches 23 games so they can't switch to Sega's timing, and why some hacks that set it themselves don't work on a SAROO.
Step 1: an FPGA that answers in time ("change 7")
I reworked the FPGA's SDRAM path:
- Saturn requests are served before refresh and before the STM32.
- Refresh and STM32 accesses happen in gaps where the Saturn can't be waiting.
- The requested word goes straight to the bus.
- Write data is sampled later in the cycle.
Result on hardware: the RAMCART-Stress test disc passes 100 of 100 runs at Sega timing with 0 errors. That covers the 4 MB cart, code run from the cart, the 8 MB above it, and the extra 16 MB with AA24, all with CD loading going on at the same time.
Games run at Sega timing without the per-game patches: KOF96, Castlevania 4MB hack (with saves), Marvel Super Heroes and others.
Step 2: the menu still froze
With Sega timing everywhere, the SAROO
menu froze now and then, mainly when scrolling quickly through the game list. Testing narrowed it down:
- Renaming cover.bin stopped the freezes.
- Restoring it brought them back.
- It was not the CS1 half of the timing.
The cause: the SAROO menu runs from the cart, so every instruction is fetched from the same SDRAM. On every cursor move, the STM32 loaded the next cover into that SDRAM. Sooner or later an instruction fetch landed on an STM32 access already in progress. That access can't be interrupted, the answer came too late, the CPU ran a wrong instruction, and the menu froze.
Games aren't affected the same way. They run from Saturn work RAM, and the STM32 keeps CD data in its own RAM, so a game hardly ever competes with the STM32 for the SDRAM.
Step 3: fixing it in the firmware
- Debounced covers: a cover is only requested once the cursor has rested for about ¼ second, so fast scrolling no longer floods the SDRAM.
- The menu runs from Saturn work RAM. At power-on a small stub copies the menu into low work RAM at the safe timing, checks the copy word by word, then switches to Sega timing and starts it. Only the code games call after they start stays in the cart: game start, save emulation and game patches.
- Covers come through the CD data FIFO, not the SDRAM. A new STM32 command sends the cover through the FPGA's CD FIFO, the same path CD sectors take. The menu reads it into work RAM and draws it from there. In the game list the menu now doesn't touch the cart at all.
Tested on hardware:
- cold boot and boot animation;
- fast scrolling with covers, with no freezes;
- KOF96;
- Castlevania 4MB, including save and reload;
- the CD player;
- reset back to the menu.
All of this runs at Sega timing from power-on.
A fallback, if it's ever needed
I've also designed an FPGA version that keeps two copies of the cart memory in different SDRAM banks, so a Saturn read always has a free copy. In simulation it gives 0 errors in every test. It hasn't been flashed and isn't in the release; the firmware fix made it unnecessary for now.
Caveats
- Tested on one console (SAROO board marked 1.66). Other consoles may have less timing margin.
- The FPGA update has no verify and no fallback. Back up SS_SAVE.BIN / SS_MEMS.BIN first, and read the README.
- The new ssfirm.bin and mcuapp.bin must be installed together. With an older mcuapp.bin the menu shows no covers.