The 32X
The 32X goes between the console and the cartridge, and adds two fast processors, a second picture
and more sound. The new processors cannot reach anything in the console, so most of this page is
about how they and its 68000 talk. The port is from jgenesis ("the base"), but the boot code and the
SH-2s' bus are ours. The page says where the code has a guess or an estimate.
Mega Drive 32X
68000 ---+--- ports, CMD interrupt, FIFO ---+--- master SH-2 ---+
| | +--- SDRAM
Z80 -----+--- frame buffer and palette -----+--- slave SH-2 ----+
(window) | (one side at a time) |
+--- PWM sound --------------------+
+--- cartridge (RV) ---------------+
VDP -----------------> one picture <-------------- 32X VDP
| Part | What it has | Where it is |
| Two SH7604s, the master and the slave | each an SH-2 with a cache, DMA, division and timers | crates/md-sh2 |
| SDRAM | 256 KB, for both SH-2s | s32x/bus.rs |
| Registers | 8 communication ports, interrupts, and a FIFO that the 68000 fills | s32x/registers.rs |
| VDP | two frame buffers of 128 KB, and a palette of 256 colours | s32x/vdp.rs, s32x/vdp/registers.rs |
| PWM | two channels of sound | s32x/pwm.rs |
| Boot code | what each SH-2 starts with, and the 68000's vectors | s32x/boot.rs |
All but the first are in crates/mdemu-core/src. There, s32x.rs runs them and has what the 68000
sees, and bus.rs joins them to the console.
A cartridge gets a 32X (cartridge.rs) if $100 has SEGA 32X, if the 32X's header at $3C0
starts with MARS CHECK MODE, or if that header makes sense: the SH-2s' starts and vectors are all
in the cartridge or SDRAM, and the program is in the file and fits in SDRAM. A few are
not found. A disc gets a 32X with the name, or when a setting says that each disc has
one (the Mega CD).
What each processor sees
The 68000
| Address | Adapter off | Adapter on, RV = 0 | Adapter on, RV = 1 |
$000000 to $0000FF | cartridge | our vectors | our vectors |
$000100 to $3FFFFF | cartridge | nothing | cartridge |
$840000 to $85FFFF | nothing | frame buffer | frame buffer |
$860000 to $87FFFF | nothing | its overwrite image | its overwrite image |
$880000 to $8FFFFF | nothing | the cartridge's first 512 KB | nothing |
$900000 to $9FFFFF | nothing | the 1 MB of it that $A15104 chooses | nothing |
Turning the adapter on takes the program away from where it ran, so the code that does it has to
be elsewhere, such as in the console's memory. With a disc, $000000 to $7FFFFF stays the
Mega CD's. These do not depend on the adapter:
| Address | What it is |
$A130EC | MARS, which games look for |
$A15100 | bit 0 (ADEN) turns the adapter on. While bit 1 is 0, both SH-2s are held in reset. Bit 15 is FM |
$A15102 | bits 0 and 1: the CMD interrupt of the master and of the slave |
$A15104 | which of four parts of 1 MB is at $900000 |
$A15106 | bit 0 is RV, bit 2 turns a transfer through the FIFO on, and bit 7 says that the FIFO is full |
$A15108, $A1510C, $A15110, $A15112 | that transfer's source, destination and length, and the FIFO |
$A15120 to $A1512F | the communication ports |
$A15130 to $A1513F | PWM |
$A15180 to $A1518F, $A15200 to $A153FF | the 32X's VDP, and its palette |
The SH-2s
Both have the same map, but each has its own boot code. Where nothing is, they read 0, which is an
estimate. The last column does not count the instruction itself.
| Address | What it is | Cycles for a word |
0x00000000 to 0x00003FFF | boot code: 2 KB for the master, 1 KB for the slave, repeated | 1 |
0x00004000 to 0x0000403F | registers, the ports (at 0x4020) and PWM (at 0x4030) | 1 |
0x00004100 to 0x000043FF | the 32X's VDP, and its palette (at 0x4200) | 5 |
0x02000000 | the cartridge, up to 4 MB, read only | 8 |
0x04000000, 0x04020000 | the frame buffer, and its overwrite image | 5 to read, 1 or more to write |
0x06000000 to 0x0603FFFF | SDRAM | 10 to read, also a line of the cache, and 1 to write |
A longword takes as long as two words, but not in SDRAM. There, comments say 10 to 12 cycles to read
and 1 to 3 to write, and the code always takes the fewest. The frame buffer has a FIFO of four
writes, which can make an SH-2 wait.
An address's highest three bits say how an SH-2 gets there. With 0 (0x06000000), it goes
through its cache. With 1 (0x26000000), it goes around the cache, to the same memory. With 2, it
drops an entry of the cache, with its four lines. 3 and 6 reach the cache's tags and its data, and 7
the registers of the SH7604 itself.
How it starts
All of the boot code is ours (s32x/boot.rs). The console's ROMs were not used, and nor was a
disassembly of them. The credits say what it rests on.
The 68000 starts from the cartridge, as without a 32X. Both SH-2s start too, and wait until they see
ADEN, in bit 9 of 0x4000. Bit 8 there says that there is no cartridge. The words of the ports are
numbered from 0 to 7 here:
68000 (the cartridge's code) master slave
turns the adapter on sees that sees that
port 7 = 1, a marker waits for "M_OK"
copies the program to SDRAM
port 4 = the header's checksum
ports 0 and 1 = "M_OK"
waits for the checksum waits until port 7 is 0 ports 2 and 3 = "S_OK"
clears ports 4 to 7, 7 last waits until port 7 is 0
starts the game starts the game
The header says where the program is, where in SDRAM it goes and how long it is ($3D4, $3D8 and
$3DC), where the master and the slave start ($3E0 and $3E4), and where their vectors are
($3E8 and $3EC). The checksum is at $18E. Games wait for M_OK and S_OK too.
- The marker is there because a game's SH-2s can use these ports at once. Tempo's write over the
checksum, so neither may start before the 68000 is done with it. It comes before the copy, as
the 68000 does not always wait: in FIFA Soccer 96, the checksum is 0, as the port already is.
- Before its game starts, each SH-2 clears its interrupt of the reset button. Without that, Doom
and Star Wars Arcade stopped when they were reset just after they were turned on. It also empties
its cache and turns it on, as Pitfall: The Mayan Adventure needs it and does not do that.
The 68000's vectors 1 to 47 go to a table in the cartridge with six bytes for each, from
$880200 on. Only the one for the horizontal interrupt, at $70, can be written. Our code at $C0
writes D0's byte to where A1 points, with RV set and interrupts off. Some games call it to reach
$A130F1, the register for their save memory.
With a disc, its program puts a header and the SH-2s' program into the frame buffer, gives that
to the SH-2s, and writes _CD_ to ports 0 and 1. The master copies the program, both answer as
above, and the game starts. These answers, and that it starts then, are guesses. With them, all
seven discs that get this far go on.
A console is different. Its boot code takes another time, and starts only cartridges with the
maker's code, which ours does not look for. Ours also copies the checksum and does not add the
cartridge up, so one with a wrong checksum can start.
The reset button turns the adapter off, starts the 68000 again and interrupts both SH-2s. The
boot code runs again only after the 68000 holds the SH-2s in reset, through $A15100, and lets them
go.
How the processors talk
| From | To | Through |
| 68000 | an SH-2 | a port, the CMD interrupt, the FIFO, or the frame buffer |
| an SH-2 | 68000 | a port, clearing its CMD interrupt, or the frame buffer |
| an SH-2 | the other one | a port, SDRAM, the frame buffer, or the serial line, which a comment says that no game uses |
| Z80 | an SH-2 | what the 68000 sees of the 32X, a byte at a time, through its window of 32 KB |
An SH-2 cannot interrupt the 68000, whose interrupts all come from its own VDP. It cannot reach
the Z80 either, but the 68000 can pass something on, through the Z80's memory.
Communication ports
The eight words are plain memory. Everyone can read and write all of them, at $A15120 or
0x4020. Nothing says that one was written, and a second value replaces one that was not read. Two
things help. A longword's two words are written with nothing in between, so nobody sees half of
M_OK. Writing a byte leaves the other byte of its word as it is, so each can have its own
writer.
Interrupts
Each SH-2 has its own flags for these five, and its own masks, in bits 0 to 3 of 0x4000:
| Interrupt | Level | What raises it | Mask | Cleared by a write to |
| VRES | 14 | the reset button | none | 0x4014 |
| V | 12 | the start of the 32X VDP's vertical blanking | bit 3 | 0x4016 |
| H | 10 | that VDP, every n + 1 lines, with n from 0x4004 | bit 2 | 0x4018 |
| CMD | 8 | the 68000, through $A15102 | bit 1 | 0x401A |
| PWM | 6 | the PWM timer | bit 0 | 0x401C |
The highest one is taken if it is above the level in the SH-2's own status register. Its vector has
the number 64 plus half its level, so 68 for CMD. One from inside the SH7604 goes first only if its
level is higher. The masks do not all work in the same way:
- V and CMD are remembered while they are masked. V is forgotten when vertical blanking ends,
and cannot be cleared while it is masked. A comment says that this was seen on hardware, but not
whether the others are like that.
- H and PWM are lost if they come while they are masked. One that came stays until it is
cleared, also if it is masked after that.
- CMD is also an answer. When the SH-2 clears it, the 68000 reads its bit in
$A15102 as 0. The
68000 writes both bits at once, and a 0 takes an interrupt back.
H comes only for the lines of the picture and the one before them. With bit 7 of 0x4000 (HEN), it
comes in vertical blanking too. The SH-2s share HEN, 0x4004 and FM.
The FIFO
The 68000 can send a block of words, and an SH-2's DMA fetches them. The FIFO is two halves of four
words, and an SH-2 gets a half only when it is full. That turns on DREQ0, a line that asks
channel 0 of the DMA for a transfer. Both SH-2s see it, and the one whose channel is set up gets the
words. This is one way to send a block to the master:
- The 68000 writes the length, in words, to
$A15110. Its lowest two bits become 0. A source
and a destination can be written too, but the code only keeps them for the SH-2s to read.
- It turns the transfer on with bit 2 of
$A15106. Without that, words for the FIFO are
dropped. Turning it off empties the FIFO, and both sides start again with the first word of the
first half. The base only empties it, so a transfer that was stopped in the middle of a half puts
the next one out of step there. ares, which has one queue, empties all of it too.
- It writes 1 to
$A15102. If the masks allow CMD, the master goes to its handler, which
clears CMD at 0x401A.
- The handler sets up channel 0 (at
0xFFFFFF80) for when DREQ0 asks: the same number of
words, from 0x20004012, which stays, to SDRAM, where the address goes up. It turns on the
channel, and all DMA at 0xFFFFFFB0. Then it writes to a port that it is ready.
- The 68000 sees that, and writes words to
$A15112. Before each four, it looks at the full
bit. Nothing makes it wait: with the FIFO full, it would write over words that were not read.
- The fourth word fills a half, which turns DREQ0 on. The 68000 goes on with the other half.
- When the master next runs, its DMA moves these four words, and its instructions wait for
that. Each read takes a word out of the FIFO, and takes 1 off the length.
- The last word makes the length 0, which turns the transfer off in
$A15106. The channel has
counted to 0 too. It says that it is done, and interrupts the master if the game wants that.
- The master reads the block around its cache, from
0x26000000 on, or first drops what the
cache has of it. DMA does not change the cache.
Virtua Racing Deluxe sends 65 words for a length of 64, and crashes if the last one is moved.
The frame buffer: FM
FM says who can reach the frame buffer, the 32X's VDP and its palette: the 68000 (0) or both
SH-2s (1). Either side can change it at any time, in bit 15 of $A15100 or 0x4000. The other side
does not wait: its writes are lost, and it reads ones, which is an estimate for the SH-2s.
The cartridge: RV
The console's VDP can copy from the cartridge only at its usual address. With the adapter
on, the cartridge is there only while RV is set. It is then not at $880000 and $900000, so
the code that sets RV has to be somewhere else. If an SH-2 reads the cartridge with RV set, both
stop until it is clear. So until then they have to use SDRAM or what is in their caches. Their
watchdogs, the 32X's VDP and PWM go on. (With a disc, the console's VDP copies from the Mega CD.)
SDRAM and the caches
Nothing keeps the two caches the same. A write always goes to memory, and also changes the
writer's own cache if that has the line. The other cache keeps its old copy. So an SH-2 that wants
to see what the other one wrote reads around its cache, or first drops what it has. TAS, which tests
a byte and sets a bit in it, always reads from memory.
Some games need what only a cache has. WWF Raw writes to the cartridge, and Pitfall to near
0x00090000, where nothing is. Both then read the values back from the cache. Without it, graphics
in one's menus are missing, and objects in the other are not drawn until they are entirely on the
screen.
The picture
Programs only reach the back buffer, and the VDP draws the other one. Bit 0 of $A1518A
(0x410A) chooses which one is shown. The change comes when vertical blanking starts, or at once if
it already has, or if the mode is blank. Reading the bit says which one is shown now.
A buffer starts with a table: its word n says at which word line n starts. A line has 320 dots.
Bits 0 and 1 of $A15180 (0x4100) choose the mode:
| Mode | What a line has |
| 0, blank | nothing |
| 1, packed pixel | a byte for each dot, with the number of its colour in the palette |
| 2, direct colour | a word for each dot, with the colour itself |
| 3, run length | words with a number of dots, minus one, in the high byte, and the number of their colour in the low one |
There are 224 lines, or 240 with bit 6. In direct colour, 224 lines of their own need 71,680 words
and a buffer has 65,536, so some lines have to share. Bit 0 of $A15182 moves lines left by a dot,
but not those whose address ends in $FF. The VDP takes these settings over once a line, 76 dots
after horizontal blanking starts.
The overwrite image leaves bytes as they are where a 0 is written, so that a picture can be
drawn over another one. When a single byte is written, a 0 changes nothing at either address.
Auto fill fills up to 256 words when $A15188 is written. $A15186 has the address, and only
its low 8 bits count up. $A15184 has the number of words, minus one. The code fills all of them at
once, but bit 1 of $A1518A says for a while that the frame buffer is busy: 7 SH-2 cycles and 3 for
each word, as documentation appears to say. Mortal Kombat II needs that time, as its 68000 has
to interrupt the master before the fills are done. Metal Head needs the same bit for about 40 cycles
at the start of each horizontal blanking, when the memory is refreshed.
How two pictures become one
A colour has 5 bits each for red (the lowest), green and blue. Its bit 15 is its priority, and
bit 7 of $A15180 turns that around for all dots. If the result is 1, the dot is in front of the
console's. If it is 0, it only shows where the console's dot has the first colour of its palette. A
comment in vdp/colors.rs says that this was verified on hardware.
The pictures are put together when the console's frame is complete. With the adapter off, or with
only blank lines, there is only the console's. With a 32X, the levels of its colours are 8 lower, of
255, except for black.
The 32X's dots are as wide as the console's in H40. In H32, the console's 256 dots are wider,
and the 32X's picture starts a little further to the right. The picture is then 1,293 wide, so that
none is blended: the console's dots are 5 wide, and the 32X's are 4. It starts at 13, so only the
32X is there after 1,280.
PWM sound
Each side has a FIFO of three samples (s32x/pwm.rs). A sample is how wide a pulse is, in 12
bits. A fourth one pushes the oldest out. When none is left, the last one is used again. The 68000
has the same registers from $A15130 on:
| Register | What it is |
0x4030 | what each side plays (its own FIFO, the other one or nothing), the timer, and whether it turns on DREQ1 |
0x4032 | the cycle: the next samples are taken after one SH-2 cycle less than this, or after 4,095 if it is 0 |
0x4034, 0x4036, 0x4038 | the left FIFO, the right one, and both. They read as full (bit 15) and empty (bit 14) |
After 1 to 16 samples, the timer raises the PWM interrupt. It can also turn on DREQ1, which asks
channel 1 of an SH-2's DMA to move one unit. That turns DREQ1 off again. A longword at 0x4034 has
a sample for each side, so DMA can feed both sides. Both SH-2s see DREQ1 too. In 0x4030, the 68000
can only change what each side plays. If that is nothing on both sides, or if the cycle is 1, all of
this stands still.
A width of 0 becomes -1, and the widest one 1. A comment says that this is wrong: it should have
no sign, and maybe a filter after that. The mixer (sound.rs) takes the mean level over
each of its samples, which is 432 SH-2 cycles. It goes with the YM2612 and the PSG through the
console's filters, at -2 dB. That number is the base's, and was not measured.
The SH7604s
crates/md-sh2 takes one cycle for an instruction, or more for some (such as a branch that is
taken), and adds what its accesses take. Accesses to most of the registers below add 3 to 11. The
pipeline is not emulated.
| Part | What is emulated | File |
| Cache | 4 KB: four ways with 64 lines each, of 16 bytes. Only reads that miss fill a line. Which way that replaces comes from the order in which they were used. The mode with two ways changes only that choice. Programs can use the cache's data as memory | cache.rs |
| DMA | Two channels, with units of 1, 2, 4 or 16 bytes, by themselves or when DREQ0 or DREQ1 asks. The SH-2 stands still for them, where a comment says that it should only do so when both need memory. The order of priority that a game sets is not used, and transfers with a single address do nothing | dma.rs |
| Division | Of 32 or 64 bits by 32 bits, done at once. If the result does not fit, or the divisor is 0, it is the highest or lowest number, with a flag but no interrupt | divu.rs |
| Watchdog | A timer with an interrupt. It does not reset anything | wdt.rs |
| Free-running timer | It does not count. Its registers can be written, and all but one read as 0 | frt.rs |
| Serial | Bytes to the other SH-2, with an estimate of how long they take. Only a byte that came in can interrupt | sci.rs |
| Breakpoints | Their two addresses are kept, as After Burner Complete stores things there. Nothing stops at them | registers.rs |
How they are kept in time with each other
Everything counts cycles of the master clock, which has 53,693,175 a second on NTSC and 53,203,424
on PAL. The 68000 has a cycle for every 7 of them, and the Z80 for every 15. Each SH-2 has three
cycles for each of the 68000's, about 23.0 million a second on NTSC. A line of the picture is
3,420 cycles of the master clock for both VDPs.
The 68000 leads. A turn is one of its instructions, or a time in which it stalls:
- The 68000 runs. It reads what the 32X had after the last turn. Its writes wait for step 5.
- The Z80 catches up with it. Its reads and writes happen at once.
- The console's VDP catches up, unless nothing could see that. If it copies from the cartridge,
RV counts as it is now.
- The 32X gets the turn's time. It may run later, up to 256 turns behind, but goes through the
same turns one by one. It catches up before the 68000 writes to it or to the cartridge, or reads its
registers or frame buffer, before its sound is mixed, for a reset, and at the end of a frame.
- The 68000's writes happen. The SH-2s see them in the next turn.
- If the frame is complete, the two pictures are put together.
The 32X's time is cut into pieces. A piece ends with the turn, or before that when the 32X's VDP
takes its settings over, draws a line, can raise an H interrupt that an SH-2 allows, starts vertical
blanking or ends an auto fill. Each piece has this order:
slave SH-2 -> master SH-2 -> watchdogs and serial -> 32X VDP -> PWM
- Each SH-2 runs to the end of the piece. It stops after the instruction that gets there. If
that, or DMA before it, takes it further, it has less time in the next piece.
- Interrupts from the last three come after both SH-2s, which see them in the next piece. The
VDP's blanking bits also stay the same for a whole piece. PWM does not end one, so its interrupt
and DREQ1 can be later than they should be, by what is left of the piece.
- What the slave wrote is there for the master, also if the slave did it at a later time in the
piece. That is true for SDRAM, the frame buffer and all registers but the ports. What the master
writes is there for the slave in the next piece, or when the master had to catch up.
- The ports make an SH-2 catch up. Before one of them reads or writes a port, the other one runs
to the same time if it is behind. So the two see each other's values in the order of their times,
to within an instruction or the DMA that came with it. What the other one wrote to SDRAM until
then is there too. The 68000 and the Z80 do not catch up, and what they write only comes
between two turns.
- With RV set, reading the cartridge still gives its value, and both SH-2s can run to the end of
the piece. From then on, time passes and they do not run. This ends when RV is clear.
Brutal Unleashed: Above the Claw needs the ports to be like this. Its slave waits for a value
from the master, and then quickly writes a port twice. If the master does not read the first value
in between, the game stops.
Writes by both SH-2s share the frame buffer's FIFO. Apart from that, no processor waits here while
another one uses the 32X's memory. Performance says which work is left out,
such as loops that only wait, and that may not change what anything sees. The
architecture has the rest of the core.
What is not emulated or not known
Status says what was compared with the base, and has everything that is open.
Mainly:
- The boot code's time is not a console's, and how it starts a disc's program is in part a
guess. Why one reset of Knuckles' Chaotix failed in a test is not known.
- Waiting for memory is an estimate (above). Bit 13 of
$A1518A says when the palette can be
reached, but nothing stops other accesses. What an SH-2 reads where nothing is, or without FM, is
an estimate too.
- In the SH7604s, there are no free-running timer, resets by the watchdog, breakpoints,
interrupts for division or for bytes that were sent, low power in SLEEP, bus settings or address
errors.
- Some bits are only kept: bit 1 of
$A15106, as what it does is not known, and bits 0 and 8 of
$A1511A.
- How interlaced pictures work is not known. The 32X's lines are drawn into one field and then
copied to the other one.
- Nobody has listened to the PWM sound, and it was not compared with the base.