emuforge

Cartridges

A cartridge has the game in ROM, often some RAM whose contents a battery keeps, and sometimes a chip of its own. The console gives it 24 bits of address. What is where depends on the board in the cartridge, and the file of a game does not say which board that is. So this page is mostly about how snesemu finds out, and what it has to guess.

It is all in crates/snesemu-core/src/cartridge.rs. How a chip meets the bus is in chip.rs. What is here rests on the SNESdev Wiki's "ROM header", "ROM file formats" and "Memory map", and on fullsnes' sections about the header, LoROM and HiROM (credits).

No game is recognised, by its title, its checksum or a list. The map is chosen by what the ROM says of itself.

Files

  • A game is a file with the ROM from start to end, such as .sfc or .smc. forge's app also opens ZIPs.
  • A copier's header is left out. Copiers put 512 bytes in front of the ROM. A file has one if its size is 512 bytes more than a multiple of 1 KB. What these bytes say is not used.
  • A ROM has 32 KB to 8 MB. Anything else is refused, and so is a file in which nothing looks like a header.

The header

It has 64 bytes, and is at the end of the first 32 KB (LoROM) or 64 KB (HiROM) of the ROM, so that the processor sees it at $00:FFC0. ExHiROM's is 4 MB further on. The last 32 bytes are the processor's vectors:

In the headerAtWhat it hasWhat snesemu does with it
$00-$14$FFC0-$FFD4the titleit is shown. What is not ASCII becomes ?
$15$FFD5the map modeone of the signs below
$16$FFD6the kind of cartridgesays if there is RAM, a battery and a chip
$17$FFD7the ROM's size: 1 KB times 2 to this power, rounded upone of the signs below. The file's own size is used
$18$FFD8the RAM's size, in the same wayhow much RAM there is
$19$FFD9the countrychooses NTSC or PAL, if the settings do not
$1A$FFDAthe maker. $33 means that there are 16 more bytes before the headerwhere a Super FX's RAM size is
$1C-$1D$FFDCthe complement of the checksumone of the signs below
$1E-$1F$FFDEthe checksumone of the signs below
$3C-$3D$FFFCthe address at which the processor startsone of the signs below

PAL countries are codes $02 to $0C, $11 and $12. All others are NTSC, with Brazil. Codes that are not known are taken for NTSC, which is a guess. fullsnes does not say what $12 is. Here it is PAL: Tintin in Tibet (in English, Spanish and Swedish) has it, and says on an NTSC console that it is only for PAL.

Which map it is

For each of three maps, snesemu looks at where its header would be, and adds points for what looks real. Both documents list such signs. How much each one counts is our own choice:

SignPoints
The address at which the processor starts is below $8000this map is ruled out. Bank 0 has ROM only from there on
The checksum and its complement add up to $FFFF4
And the checksum is right8 more
The map mode's upper bits are right, and its lower four bits fit this map4, and otherwise -2
The ROM's size is from 8 to 13, and not smaller than the file1
The RAM's size is at most 71
The country's code is at most $141
The title has only letters that titles have (ASCII from $20 to $7E, and Japanese from $A1 to $DF)1
The first instruction is one that programs start with: SEI, CLC, SEC, STZ, JMP or JML3
The first instruction is BRK, COP, STP, WDM or $FF-6
Always-4

The map with the most points wins, if it has more than 0. When two have the same, it is the first of LoROM, HiROM and ExHiROM. The last row is there because the weak signs can all be there by chance. The map mode fits if its lower four bits are 0, 2 or 3 for LoROM, 1 or $A for HiROM, or 5 for ExHiROM.

A LoROM cartridge with 3 MB can have a slot for a data pack, and then its map is another one. fullsnes says how its header shows that: the game's code ($FFB2-$FFB5) starts with Z and has four letters, so $FFB5 is not a space, and the maker is $33. This is looked at after the map was chosen, and only with exactly 3 MB. Its map is below.

The checksum is the sum of all bytes of the ROM, with its smaller part repeated. The checksum's own four bytes count as $FF, $FF, 0 and 0.

For example, a game of 1 MB has its header at $7FC0 of the file. Its map mode is $20. At $7FC0, it gets 4 + 8 points for the checksum, 4 for the map mode, 4 for the four signs that count 1, and 3 for its first instruction, which is SEI. Less 4, that is 19. At $FFC0 of the file, there is only a part of the game. If the two bytes for the start make a number below $8000 there, HiROM is ruled out. If not, it is very unlikely to get more than a few points. The file ends before the place where ExHiROM would have its header.

What is where

MapROMSave memory (a guess for each)
LoROM32 KB in the upper half of each bank, up to 4 MB: $00-$7D and $80-$FF. In banks $40-$7D and $C0-$FF, the lower half has the same as the upper one, unless it has save memorythe lower halves of $70-$7D and $F0-$FF
HiROM64 KB in each of $40-$7D and $C0-$FF, up to 4 MB. The upper halves of $00-$3F and $80-$BF have those of the same 64 banks$6000-$7FFF of $20-$3F and $A0-$BF
ExHiROMas HiROM, up to 8 MB. $80-$FF has the first 4 MB, and $00-$7D has what comes after it$6000-$7FFF of $80-$BF
LoROM with a slot (3 MB)only the upper halves: the first 1 MB in $00-$1F, the second in $20-$3F and again in $A0-$BF, and the third in $80-$9F. fullsnes calls it "SpecialLoROM"as LoROM

Banks $7E and $7F are always the console's main memory. The lower halves of banks $00-$3F and $80-$BF are the console's, up to $5FFF.

  • Memory that is smaller than its place repeats. The processor then sees the same byte at several addresses.
  • If a ROM's size is no power of two, the largest part that is one comes first. The rest then repeats until it is as large. For example, 3 MB are 2 MB and 1 MB. In the 4 MB of the map, the last 1 MB is there twice. If the rest is no power of two either, such as in 4 + 2 + 1, it repeats in the same way. That is a guess. The documents say that it is padded first, and differ on how.
  • The slot is always empty. A pack would be in $C0-$DF, and there is open bus. With this map, Derby Stallion 96 and Sound Novel Tsukuru show 410 and 362 different pictures in 3,600 frames (2dc121c, with simple presses). With the LoROM map, they showed one or two in 2,400.
  • Where the cartridge has nothing, there is open bus. Writes to the ROM do nothing.
  • Banks from $80 can be faster, if the game says so.

Where boards really have their save memory is not in the file. Older LoROM boards also have it in the upper halves of these banks. Most HiROM boards have it where it is here. A game whose board has it elsewhere finds no save memory there.

Save memory

  • Its size is 1 KB times 2 to the power in the header, from 1 to 7 (2 KB to 128 KB), if the lower four bits of the kind of cartridge are not 0. A game with an SA-1 can also have 8, which is 256 KB.
  • A Super FX has its RAM size in the bytes before the header, at $FFBD, if the maker is $33. Without that, or if it is not from 1 to 7, it has 32 KB. fullsnes has this for one early game. For others, it is a guess.
  • It is kept after the game ends if there is RAM and the lower four bits of the kind of cartridge are 2, 5, 6, 9 or $A. Only then does the core give it to the front end (Console::battery). Console::load_battery takes it back if it has the right size.
  • With a clock in the cartridge, the battery keeps that too (how).
  • When the console is turned on, all of its bytes are $FF, unless something was put back. A reset does not change it.

A battery with a clock

Two kinds of cartridge have a clock that goes on while the console is off: the RTC-4513 beside an SPC7110 and the S-RTC. The core has no clock of its own. A front end gives it one time when the console is turned on, in Settings::time: the whole seconds from 1970-01-01 00:00:00 to what the player's clock shows, without a time zone. The app takes it from the computer. From then on, only the master clock counts. The same game, settings and buttons then always give the same result. Without a time, it is 1970.

What a battery keepsBytes
The cartridge's RAMall of it
With an RTC-451324 more: its 16 registers, and the player's time that they are for (8, the lowest byte first)
With an S-RTC16 more: what the clock shows, and the player's time that it is for (8 bytes each, the lowest first). What it shows is counted in seconds from the year 1000 on

When it is put back, the clock moves by the time between the two. load_battery takes this from Settings::time, so it has to come right after the console is turned on. If the new time is before the one that was kept, the clock does not go back. A save that has only the RAM fits too, and leaves the clock. Data that does not fit changes nothing.

What the battery keeps for the clock only changes when it has to. A clock that goes on would be a new save each second. Both parts of it are from the same moment, so it stays right for as long as nobody changes the clock. It is only taken again after a write to $4840-$4842 (RTC-4513) or $2801 (S-RTC), and when the console is turned on, a battery is put back or a savestate is loaded.

While a game runs, the clock goes at the console's speed. So it stands still in a pause, and goes faster in fast forward. The crate for the RTC-4513 can also be told the time while it runs, which the core does not do.

Chips

The kind of cartridge ($FFD6) says which chip there is, if its lower four bits are 3 or more. The map mode has values for some chips too. A few games without a chip have these, so it is not used. The upper four bits are:

Upper bitsChipHere
0a DSP. The header does not say which of four. The rest of it doesthe DSP-1 is joined, in part. Another crate can have all four
1Super FXjoined
2OBC1joined
3SA-1joined
4S-DD1joined
5S-RTCjoined
$Fthe byte before the header ($FFBF) says which: $00 is the SPC7110, $01 the ST010 (1 MB of ROM) or the ST011 (512 KB), as in fullsnes' table of boards, $02 the ST018, and $10 the Cx4. For the SPC7110, a kind of $F9 says that there is a clock toothe SPC7110 and the Cx4 are joined

A game with a chip that is not joined is refused, with the chip's name. The architecture says how one is joined, and what is at the addresses for which the chip has nothing.

What is not there

  • ExLoROM. The documents have no map for it.
  • A cartridge or a data pack in a slot. The slot is always empty.
  • ST011 and ST018. DSP-2, DSP-3, DSP-4 and ST010 are only there with another crate. The DSP-1 is only there in part without it.
  • The Super Game Boy, the Satellaview and the Sufami Turbo.
  • The XBAND's modem.
  • What a board allows, such as fast access, is not checked.

Tests in cartridge.rs make ROMs of their own for each map, in many sizes, with and without a copier's header and a checksum. They also give each byte of a header every value, to see that a damaged file is never a problem.

From commit 019fda0af379. Edit this page