Cartridges
A cartridge can be more than a program: it can have banks, memory with a battery, a serial EEPROM,
or even a processor of its own. A file only has the program, so the core has to find out about the
rest. This page says how it does, and how that hardware works.
The code is in crates/mdemu-core/src/cartridge.rs, cartridge/, svp.rs and svp/, ported from
jgenesis ("the base"). How a region is chosen is mdemu's own.
Files
The core takes files from 512 bytes to 32 MB, and makes the same program from all of these formats.
So the app uses the same saves for them. Only the checksums below tell them apart:
| Format | How it is known |
| Plain | none of the others fits |
| With 512 bytes from a copier in front | its size is 512 more than a multiple of 1,024, and SEGA is where the header puts it (also in one of the next two formats) |
| Pairs of bytes swapped | ESAG instead of SEGA at $100 |
Interleaved, as many .smd files: in each 16 KB, the odd bytes come first, and then the even ones | its size is a multiple of 16 KB, there is no SEGA, and its letters are where interleaving puts them |
A file with less than 1 KB repeats, as a small cartridge's upper address lines are not connected.
The app opens .md, .gen, .smd, .bin and .32x files. From a .zip, it takes a file with
one of these endings. If there are several, the first of these endings counts, so a second chip's
ROM in the same .zip is not taken for the game. That part is
forge's (forge-app, files.rs), and its limit is 16 MiB. A
.zip can also have a disc.
| Address | What the core uses it for |
$100 | the console's name. It can say that there are banks or a 32X |
$180 | the product code. It can help to find the region, and says which wiring and chips it has (below) |
$1A0 | the addresses of the program, to know whether it has banks |
$1B0 | memory in the cartridge |
$1F0 | the region |
$3C0 | the header that a 32X uses |
A cartridge is for a 32X if its name or the 32X's header says so, or if the addresses in that
header can only be a 32X's. The 32X has more about it.
The region
A setting comes first. Without one, cartridge.rs goes by these rules:
EUROPE at $1F0 is Europe.
- Otherwise, the first three characters there can be the letters
U, J and E. Without any of
them, the first is read as a hexadecimal digit, with bit 2 for America, bit 0 for Japan and bit 3
for Europe.
- If that is Europe and another one, and the product code ends with
-50, it is Europe. This is
not done if the console's name is SEGA 32X, as one such cartridge for America and Japan has
that code. Of 1,418 releases, 25 are like this, and all of them were sold in Europe.
- If there is still more than one, America comes first, and then Japan.
- If the field says nothing, it is America.
Apart from step 3, the console's name changes nothing. Many European and Japanese cartridges have
GENESIS in it.
Banks
A plain cartridge is at its own addresses. Past its end is open bus. DMA can only read it up to
$3FFFFF here.
Large cartridges can have eight windows of 512 KB each. The first always shows the first 512 KB. The
odd addresses from $A130F3 to $A130FF choose a bank of 512 KB for each of the others. These
cartridges have SEGA SSF as their console's name. Some do not: those have more than 4 MB, and a
header that says that their program is at $000000-$3FFFFF.
Two kinds of cartridges have wires to other places:
- One of 512 KB shows each half four times, from
$000000 and from $100000. It is known from
its whole product code and its size (the second version of QuackShot).
- Triple Play Gold and Triple Play 96 have their third megabyte at
$300000, and memory at
$200000. They are known from their numbers and at least 3 MB.
Games that lock onto a cartridge work when both are in one file. Nothing puts two files
together. For such a file with Sonic & Knuckles (over 2 MB, by its product code), the memory is read
from the second game's header at 2 MB. The region is from the first header.
Memory and batteries
If $1B0 has RA, then a kind and $20, there is memory at the two addresses after that:
| Kind | Without a battery | With one |
| 16 bits | $A0 | $E0 |
| 8 bits, at even addresses | $B0 | $F0 |
| 8 bits, at odd addresses | $B8 | $F8 |
A word that is read from 8 bits is the same byte twice. A range that ends before it starts, or that
is larger than 2 MB, does not count as memory.
The memory and the program can have the same addresses. Bit 0 of $A130F1 says whether the memory
is seen there. It is seen from the start if it begins past the end of the program, as some games
never write to that register. An EEPROM and Triple Play's memory are seen from the start too.
The memory of five games is not in their headers. They are known from a checksum (CRC-32) of
their data, after a file's format was converted: four get 8 KB, and one gets 32 KB, at odd addresses from $200001 on.
What has a battery is kept, and so is an EEPROM. A save has only its bytes, and one with a different
size is not used. Without a save, all of it starts as 0, but for one EEPROM (below). The app writes
the file.
EEPROMs
An EEPROM keeps its contents without a battery. It has two lines: one for data and one for a clock.
The game moves them a bit at a time. The core knows five chips, all in cartridge/eeprom.rs:
| Chip | Size | What differs |
| X24C01 | 128 bytes | the address comes first |
| X24C02, X24C08, X24C16 | 256, 1,024 and 2,048 bytes | a byte for the device comes first. Bits 1 to 3 of it are the upper bits of the address |
| X24C64 | 8,192 bytes | that byte has to be $A0 to write or $A1 to read. Two bytes of address come after it for a write |
While the clock is high, data that falls starts a transfer, and data that rises ends one. The chip
takes each bit when the clock falls. After 8 bits comes one more clock for the answer. A read goes
on from byte to byte. A write goes round within 4, 16 or 32 bytes, as the chip's pages have that
size. A byte is there at once, as the time that a chip takes for a write is not emulated.
How the chip is found
A header does not say which chip there is, or which address and bit each of its lines has. So
the core has a list (cartridge/external/metadata.rs) from
a thread on gendev.spritesmind.net.
It identifies what is soldered to a board, and changes nothing in a game's program. It is only used
if the cartridge has none of the memory above:
- By serial number: the eight characters at
$183 are looked up in a table of 18.
- By a longer one: one game (Micro Machines 2) is known from eleven characters there.
- By checksum: three games are known from the CRC-32 of all of their data, as some cartridges'
numbers are not their own. It is the same for each format of a file.
Each entry is one of these five ways to connect the lines, and a chip:
| Cartridges | Data to the chip | Data from it | Clock | Chips |
| Sega's and Capcom's | $200001 bit 0 | the same | $200001 bit 1 | X24C01 |
| Electronic Arts' | $200000 bit 7 | the same | $200000 bit 6 | X24C01 |
| NBA Jam's | $200000 bit 0 | $200000 bit 1 | $200000 bit 1 | X24C02 |
| Acclaim's others | $200001 bit 0 | the same | $200000 bit 0 | X24C02, X24C16, X24C64 |
| Codemasters' | $300000 bit 0 | $380001 bit 7 | $300000 bit 1 | X24C08, X24C16 |
Micro Machines 2 supposedly does not use its chip unless it holds a certain text. So without a save,
that EEPROM starts with this text again and again.
A checksum is of the file as it is, and not of what the core made of it. So the eight games that
are known only from one get no memory or EEPROM if the file has another format, or if one byte
differs.
The SVP
One game, Virtua Racing, was sold with a processor in its cartridge (svp.rs). It is known from
one of its two numbers. The chip is a DSP called SSP1601 (svp/ssp1601.rs). It runs its program
from the cartridge, so it needs no ROM of its own. What is known about it is from notaz and Tasco
Deluxe's documentation.
68000 --- cartridge's program ($000000-$1FFFFF) --- DSP: X * Y * 2 = P, A (32 bits)
| | program memory, 1,024 words
+------ DRAM, 128 KB ($300000-$37FFFF) ------------+ RAM 0 and 1, 256 words each
| | stack, 6 places
+------ XST ($A15000) ------ one word -------------+
The DSP
It works with words of 16 bits. X and Y are always multiplied. Twice their product is in P, with
their signs. One instruction can add P to the accumulator A (32 bits) or subtract it, and get the
next X from RAM 0 and the next Y from RAM 1. Each RAM has three pointers that can move on by
themselves, also in a circle.
Its addresses are in words. A program can run in two places: its own memory is at $0000-$03FF,
and after that come the words of the cartridge's first 128 KB, at the same addresses. It starts at
$0400.
To reach other memory, it has five ports, PM0 to PM4. Each has one address for reads and another
one for writes, and goes on by itself after each access. The program puts an address and a mode into
a register (PMC), and then reads or writes the port without using the value. That sets it up. A step
can be 0, a power of two up to 32, or 128, in either direction, or 1 and 31 in turn. A port can also
write only the digits (4 bits) that are not 0, and leave the others.
| A port's address (in words) | What it reaches |
$000000-$0FFFFF | the cartridge's program, to read |
$180000-$18FFFF | DRAM |
$1C8000-$1C83FF | the DSP's program memory |
If neither of two bits in the DSP's status register is set, two of these are something else. PM0
says who wrote to XST (below), and PM3 is XST. For PM3 that is only supposed, as the game never uses
it with one of the bits set.
How it and the 68000 talk
They share DRAM and XST. DRAM is at $300000 for the 68000, four times. Nobody has to ask for it
first.
| Address | For the 68000 |
$A15000, $A15002 | XST, one word that both can read and write |
$A15004 | who wrote to it: bit 1 is the 68000, and bit 0 is the DSP |
$A15006 | $000A stops the DSP. Any other value lets it run |
When the 68000 reads $A15004, bit 0 is cleared. When the DSP reads its own copy, bit 1 is. So each
side can tell when the other has written something new. An exchange can go like this:
- At first, the DSP does not run its first instruction until the 68000 has written to XST.
- The 68000 writes what the DSP needs to DRAM, and then a word to XST. Bit 1 is set.
- The DSP reads who wrote to XST, which clears bit 1, and then the word. It gets what it needs
through a port, and writes its result to DRAM.
- The DSP writes a word to XST, which sets bit 0. The 68000 sees that at
$A15004, and bit 0 is
cleared.
- The 68000 starts a DMA from DRAM to the VDP. Each word of a DMA comes one read late
from this cartridge, so the game's source address is 2 higher than the first byte that it wants.
The DSP runs three instructions for each cycle of the 68000. That is about 23 million a second,
and the chip's speed is somewhere from 20 to 25 MHz. The code calls it a choice that is close
enough, as the game does not depend on the exact time. Neither of the two makes the other wait for
DRAM.
The DSP may be behind the 68000 while nothing outside can see it. It catches up before its memory
or registers are read by the 68000, through the Z80's window or by DMA. It also does so before
each write to the cartridge or its registers, and before its state is saved. The cartridge's program
cannot change, so reads of that do not need it. The DSP's waits can be passed
(Performance). The base skips one of them for this game and mdemu does not, so
one picture of 50 differs.
What is not emulated, or not known
- Two files as one cartridge, and cartridges with ports for controllers (J-Carts).
- Other ways to change banks. Only the kinds on this page are here.
- The exact speed of the SVP, its step of 128 where the program sets its own number, and what
its game does not use. An instruction that the documentation does not have does nothing.
- The reset button does not reset what is in a cartridge.
- A save made in a game was not tested in the app.
Status says what was run and compared.