The Mega Drive
The console has two processors that are not alike. A 68000 runs the game. A Z80 mostly makes sound.
They have separate memory and separate buses, with a door in each direction. This page says what is
behind each door, what it costs to go through, and how both processors hear of the picture.
Files are in crates/mdemu-core/src unless another place is named. Most of them are ported from
jgenesis ("the base"). The architecture says how the
processors are scheduled. The VDP, sound, cartridges, the
32X and the Mega CD have pages of their own.
cartridge, 32X or Mega CD Z80's memory (8 KB)
| |
68000 ------+------- 68000's bus -----[ door ]------ Z80's bus ------ Z80
| ^ | ^
memory (64 KB), VDP + PSG, controllers YM2612 |
| | |
| +--- H and V interrupts, to the 68000 |
+------- V interrupt, to the Z80 ----------------------+
Clocks and regions
Everything here runs from one master clock (video.rs, timing.rs). It has 53,693,175 cycles a
second on an NTSC console (America and Japan), and 53,203,424 on a PAL one (Europe). The 68000 has
one cycle for each 7 of them, and the Z80 one for each 15. That is about 7.67 and 3.58 MHz on NTSC.
A line of the picture lasts 3,420.
The region is the one that the cartridge, the disc or a setting says. A
game can read it at $A10001. Bit 7 is 1 on a console that is not Japanese, bit 6 is 1 on PAL, and
bit 5 is 1 without a Mega CD. The other bits are 0. Bits 0 to 3 being 0 stands for a first model.
What the 68000 sees
Its bus has 24 bits of address (bus.rs):
| Address | What is there |
$000000-$7FFFFF | the cartridge or part of the Mega CD. A 32X takes over part of it |
$800000-$9FFFFF | the 32X's picture memory and cartridge windows, while it is on |
$A00000-$A0FFFF | the Z80's $0000-$7FFF, twice, while the 68000 has the Z80's bus |
$A10000-$A1001F | region and controllers. An even address is the same as the odd one after it |
$A11100, $A11200 | who has the Z80's bus, and the Z80's reset line |
$A12000-$A12FFF | the Mega CD's registers |
$A13000-$A13FFF | the cartridge's registers for banks and memory, and the 32X's name |
$A15000-$A15FFF | the registers of the 32X, or of the SVP in a cartridge |
$C00000-$C0001F | the VDP, and the PSG at odd addresses from $C00011 to $C00017 |
$E00000-$FFFFFF | 64 KB of memory, again and again |
Where nothing answers, a read gives what was last on the bus (open bus), and a write does nothing.
Games depend on that: $A11100 has only one bit of its own, and two games do not start unless its
other bits are open bus.
TAS reads a byte and then writes it back, but on this bus the write never arrives. The RESET
instruction only takes its 132 cycles here, and resets nothing.
What the Z80 sees
| Address | What is there |
$0000-$3FFF | 8 KB of memory, twice |
$4000-$5FFF | the YM2612's four addresses, again and again |
$6000-$60FF | the window's bank register, which reads as $FF |
$7F00-$7F1F | the VDP, and the PSG at odd addresses from $7F11 to $7F17 |
$8000-$FFFF | a window of 32 KB into the 68000's addresses |
Other addresses and its input and output ports read as $FF. Its NMI line is not connected.
How the two talk
The 68000 goes to the Z80
When the console is turned on, the Z80 is held in reset and nobody asks for its bus. Both lines are
bit 8 of a word, or bit 0 of a byte at the even address:
| Register | Writing 1 | Writing 0 |
$A11100 | asks for the Z80's bus | gives it back |
$A11200 | lets the Z80 out of reset | holds it in reset |
The 68000 has the bus when it asks for it and the Z80 is not in reset. Then bit 8 of $A11100
reads as 0. So a game has to end the reset before it can load a program for the Z80. Without the
bus, reads at $A00000 give open bus, and writes are lost.
The Z80 looks at both lines only between two instructions. Reset makes PC, I and R 0 and turns its
interrupts off. In this core, the 68000 gets the bus at once, and does not wait for the end of
the Z80's instruction. All of that instruction's reads and writes happened at its start, so the rest
of it is only time.
Each access to the Z80's side takes one more cycle of the 68000, which the sound of one game needs.
That bus has 8 bits. A word that is read is the same byte twice, and only the high byte of a written
word arrives. The YM2612, the bank register and the VDP can be reached this way too.
Putting the Z80 in reset also resets the YM2612. One game needs it for its music to stop.
The Z80 goes to the 68000
The window's bank register has nine bits. A write to $6000 takes bit 0 and shifts it in from the
top. Nine writes set bits 15 to 23 of the address, the lowest first. The low 15 bits are those of
the Z80's address.
| Through the window | Read | Write |
the 68000's memory, $E00000-$FFFFFF | $FF: the Z80 cannot read it | arrives |
the Z80's side again, $A00000-$A0FFFF | $FF | lost |
| anything else | as for the 68000 | as for the 68000 |
An access costs both processors time, through the window or at $7F00-$7F1F, where the PSG is
too. The Z80 loses 3 of its cycles. The 68000 loses 10 of its own for seven accesses and then 9 for
three, which is 9.7 on average. These are averages from a test program, and not exact times of each
access (timing.rs). So a Z80 that plays samples from the cartridge slows the 68000 down with each
byte.
While the VDP holds the 68000's bus, such an access stops the Z80 until the bus is free. Here,
the access itself still happens at once, and the Z80 stops after it. The 68000 gets no extra wait
then. Whether that is right is not known, but effects of a demo need it. A Z80 that only uses its
own memory and the YM2612 runs on.
Interrupts
| Who gets it | When |
| 68000, level 6 (V) | at the start of the first line after the picture |
| 68000, level 4 (H) | near the end of the visible part of a line, when the VDP's counter runs out |
| Z80 | during all of the first line after the picture |
The VDP raises all three. The 68000 looks before each instruction whether the level is higher than
its status register allows. If it is, 10 cycles pass, and it acknowledges the interrupt. Saving its
place and jumping take 44 more. That is always 54, where a console takes 49 to 59, as the core does
not follow the slower clock that the console waits for. The handlers' addresses are at $70 (H) and
$78 (V) (md-m68000/src/core.rs).
The VDP does not look at which level was acknowledged. It clears the one that it asks for at
that moment. If V arrives during those 10 cycles of H, the H handler runs, and V is cleared. Then
the H handler runs again, and V is lost. The console has this fault, and so does the core. An
interrupt that is turned on while it is already pending comes one instruction later, which two games
need.
For the Z80, it is a level and not an event, and there is nothing to acknowledge. If its
interrupts are off for the whole line, it misses this one. If they are on again before the line
ends, it can get a second one. Modes 0 and 1 jump to $0038, as mode 0 always reads $FF as its
instruction.
Who waits for what
| Cause | Who waits |
| DMA from memory, until the last word was read | 68000 |
| A write to the VDP's full FIFO, until there is a place | 68000 |
| A read from the VDP while its FIFO has something, until it is empty | 68000 |
| Any of these, when the Z80 goes to the 68000's bus | also the Z80 |
| Memory refresh | 68000 |
For refresh, the core counts 128 of the 68000's cycles. Then it adds 2 if the program runs below
$800000, 3 if it runs from $E00000 on, and none elsewhere. If the 68000 was waiting anyway,
nothing is added, and while the VDP has the bus, the count stays at 0. Two games have broken
graphics without refresh, but it is an estimate. The cartridge and the memory are refreshed at
different times, and to follow that, the core would have to know when in an instruction each access
happens.
In the core, a 68000 instruction reads first, then its time passes, and then it writes. During
that time, the Z80 runs first and still sees the old values. The VDP and what is in the slot follow.
The 68000's writes come last. What the Z80 writes is there at once. The VDP may be behind while
nothing that it does can be seen, but it catches up before each access to its ports
(Performance).
An example: a sound for the Z80
What the Z80's memory means is up to each game. In this one, a program on the Z80 looks for a
command at $1F00:
- The 68000 writes
$0100 to $A11100. The Z80 still runs beside this instruction, and then the
write arrives.
- The 68000 reads
$A11100 until bit 8 is 0. Here that is so the first time, if the Z80 is not in
reset. The Z80 stops where its instruction ends.
- The 68000 writes the number of a sound to
$A01F00, for one more cycle. Now it is at $1F00 for
the Z80.
- The 68000 writes 0 to
$A11100, and the Z80 goes on with its next instruction.
- The Z80 reads the number at
$1F00 and puts 0 there. If the 68000 wants to know that it was
seen, it has to ask for the bus again.
- The Z80 waits until the YM2612 is not busy. It writes a register's number to
$4000 and a value
to $4001. The chip is first brought up to the Z80's time, so only the samples after that can
change (Sound).
- For a recorded sound, the Z80 sets the window's bank with nine writes to
$6000. It reads each
byte through the window and writes it to the YM2612's DAC. Each read costs both processors time.
During a DMA from memory, the next read stops the Z80, and the recording is late.
The 68000 can also write to the YM2612 itself, at $A04000, but it needs the bus for that, so the
Z80 stands still. It can write to the PSG at $C00011 without it.
Controllers
Port 1 has its data at $A10003, and port 2 at $A10005 (input.rs). $A10009 and $A1000B
choose which of their pins the console drives. The third port, at the back, never has anything in
it. A controller has more buttons than pins, so a game sets TH (bit 6) to choose what it reads.
A pressed button is 0:
| TH | Bit 5 | Bit 4 | Bits 3 to 0 |
| 1 | C | B | Right, Left, Down, Up |
| 0 | Start | A | 0, 0, Down, Up |
A controller with six buttons also counts how often TH goes from 0 to 1. After two times, bits 3
to 0 are all 0 when TH is 0, so a game knows what it is. After three times, they are Mode, X, Y
and Z when TH is 1, and all 1 when TH is 0. Everything else is as in the table. The fifth time
starts again, and so do 12,150 of the 68000's cycles without one. Those are about 1.58 ms, roughly
what a test program expects. A few games cannot read such a controller, so a setting gives them one
with three buttons.
Controllers do not drive TH. If a game makes it an input, it goes to 1 after 30 of the 68000's
cycles here. Two games say how long that may take: one needs it within about 70, and the other not
before about 15. An empty port reads as all 1s. Buttons stay the same for the whole frame.
Turning it on, and reset
When the console is turned on, all of its memory holds one value. It is 0 unless the core is told
another one, and on a real console, it is not always the same. The 68000 reads its stack pointer at
address 0, and where to start at address 4.
The reset button does much less. The 68000 starts again in the same way. The Z80 is in reset
again, and its bus is not asked for. The YM2612, a 32X and a Mega CD are reset. The memory of both
processors, the VDP with its memory, the PSG, the ports and what is in the cartridge stay as they
are (console.rs, bus.rs).
What is not emulated
- When in an instruction each access happens. The VDP's status and counters make up
for a part of that.
- External interrupts (level 2). The ports' bits for them only keep what is written.
- Serial data in the ports. The bytes to send only keep what is written, and other registers
read as 0.
- What stops a console for good: the Z80 at
$7F20-$7FFF, where it writes, or at $A00000
through its window. Comments in bus.rs say so, and the core goes on.
- Other devices: mice, light guns and adapters for more players.
- The Z80's interrupt mode 2 (it does what mode 1 does, but takes longer) and some of its flags.
Status has those, and the 68000's open questions.