emuforge

Architecture

A Mega Drive has two processors. A Mega CD adds one, and a 32X adds two. With both, five processors of three kinds run at the same time, and each can only see some of what the others have. Most of this core's work is to let each one run at the right time, and to show it the others as they were then. This page says how. The pages about the hardware say what each part does.

The parts

PartWhat it is
crates/mdemu-corethe console and both add-ons. It has no window, sound device, gamepad, clock or files, and no unsafe
crates/md-m68000, md-z80, md-sh2the processors, each for any bus. The 68000's is used twice: for the console, and for the Mega CD's sub CPU
crates/md-bitsreading and changing single bits and bytes of numbers
crates/mdemu-discgives the core the sectors of a disc that forge opened, and tells discs apart
apps/mdemuwhat the Mega Drive is, for forge's standalone app (forge-app). The window, menu, sound device and gamepads are forge's
apps/mdemu-labthe compatibility lab, which runs games without a window
apps/mdemu-devruns the core for developers (Development)
apps/mdemu-libretromdemu as a libretro core: one line, which gives forge's adapter the app's core (Using mdemu)

A front end makes a MegaDrive with load or, for a disc, load_disc. Then it calls run_frame with the pads for each frame, and takes the picture and the sound that the frame made. The same settings, game and input give the same output on every machine. Its libretro core is forge's adapter over the app's core (apps/mdemu-libretro), which opens cartridges and discs as the app does. emuforge's shared interface for cores is not here yet (Status).

Inside mdemu-core:

FileWhat is in it
console.rsloading a game, running a frame, and savestates
bus.rswhat is at each address for the 68000 and the Z80, and what happens after each step
timing.rs, waits.rsthe clocks, what holds up which processor, and loops that only wait
vdp.rs, vdp/, video.rsthe picture
ym2612.rs, ym2612/, psg.rs, sound.rssound, and the mixer for all sources
cartridge.rs, cartridge/, svp.rs, svp/cartridges, with the SVP
s32x.rs, s32x/the 32X
mega_cd.rs, mega_cd/the Mega CD and its drive
input.rs, memory.rscontrollers, and the memory of both processors (the console)

How it is joined

                      the console's bus (bus.rs)
   +--------------+---------------+--------------+---------------+
   |              |               |              |               |
 68000          VDP, with        Z80 ------- YM2612       the cartridge slot
 64 KiB RAM     the PSG          8 KiB RAM                and the add-ons
                                                                 |
      +--------------------------+-------------------------------+
      |                          |                               |
  cartridge,                 32X (s32x.rs)                Mega CD (mega_cd.rs)
  or the SVP                 master SH-2, slave SH-2      sub 68000
                             SDRAM, frame buffers         program RAM, word RAM
                             VDP, PWM                     graphics chip, PCM, drive

An add-on belongs to the bus, and knows nothing of the console's processors. What the 68000 and the VDP's DMA read and write there goes through a few functions. The 32X needs the cartridge too, which its SH-2s can read. So the bus keeps the cartridge, and lends it to the 32X while that runs.

Clocks

The console's master clock is the time that all parts share. It has 53,693,175 cycles a second on NTSC, and 53,203,424 on PAL. Nearly every other clock is a part of it:

PartIts clockOn NTSC
680001 of 7 master cycles7.67 MHz
Z80 and PSG1 of 153.58 MHz
YM26121 of 42, which is 6 of the 68000's1.28 MHz
Both SH-2s3 for each cycle of the 6800023.01 MHz
The Mega CDa clock of its own, of 50 MHz50 MHz
The sub CPU1 of 4 of the Mega CD's12.5 MHz

The Mega CD's clock is not a part of the console's, so there is no whole number of its cycles in one of the console's. Each time that it runs, the remainder is kept exactly, and used the next time. So no time is lost or added, however long a game runs.

A step

The 68000 leads, one instruction at a time. It runs an instruction, which takes some cycles, and everything else is given the same time. This is a step. In each step, things happen in this order:

  1. The 68000 runs one whole instruction. It reads what it needs at once. Memory refresh and other waits are added to its time. When something holds it up, the step is a part of that wait, and no instruction runs.
  2. Its writes wait. They are kept until the end of the step.
  3. The Z80 runs until it has caught up with the 68000.
  4. The cartridge and the controllers' timers get the step's time.
  5. The VDP gets the step's time. It draws lines, raises interrupts, and copies with DMA from the cartridge, the 68000's memory or an add-on. At the end of a frame, all sound so far is mixed.
  6. The Mega CD gets the step's time. First its devices: the drive with its decoder and DMA, the timers, the graphics chip, and PCM. Then comes the sub CPU.
  7. The 32X gets it. First the slave SH-2, and then the master, then what else is in both chips, and at last the 32X's own VDP and PWM. If something happens in its VDP within the step, the step is split there.
  8. The 68000's writes happen now, first its bytes and then its words. At the end of a frame, the 32X's picture is then put together with the console's.

This is the order of the base. Two things follow from it:

  • What a part sees depends on this order. In step 3, the Z80 does not see what the 68000 writes in step 1. The sub CPU and the SH-2s do not see it either, until the next step.
  • No access is timed within an instruction. The 68000's reads are at the start of its step and its writes at the end (Status).

Who can hold up whom

Processors do not only talk. They can also stop each other:

WhoHolds upHow
68000Z80it asks for the Z80's bus ($A11100), or holds it in reset ($A11200), which also resets the YM2612
VDP68000while DMA copies from the 68000's side, while a write waits for room in the FIFO, and while a read waits for its data
VDPZ80only when the Z80 uses the 68000's bus at that time. It then waits until the VDP lets it go
Z8068000each access through its window to the 68000's bus, or to the VDP, costs both of them time: 3 cycles for the Z80, and 9 or 10 for the 68000
68000itselfeach access to the Z80's bus costs it a cycle, and refreshing memory costs it some when it runs from the cartridge or from its own memory
68000SH-2sreset, and RV. While RV is set, an SH-2 that reads the cartridge waits. Both SH-2s then stay where they are until it is cleared
68000sub CPUreset, a request for its bus, and word RAM. If the sub CPU uses word RAM that the 68000 has (in 2M mode), it waits until the 68000 hands it over

The numbers of cycles are the base's. Some rest on test programs, and some are averages or estimates, which the comments in timing.rs say.

How they talk

Each pair of processors has its own places to meet:

BetweenWhereMore
68000 and Z80the Z80's memory, while the 68000 has its bus. The other way round, the Z80 has a window of 32 KiB into the 68000's addresses, but cannot read the 68000's own memoryThe console
68000 and VDPits ports and DMA. The VDP raises the interrupts of the 68000 and the Z80The picture
68000 and SVPshared memory on the cartridge, and registers at $A15000 that tell each side when the other has writtenCartridges
68000 and SH-2seight communication ports, interrupts, DMA through a FIFO, and the 32X's frame buffer, which one side can use at a timeThe 32X
master and slave SH-2the communication ports, SDRAM, and a serial line between the chips. Each has a cache of its ownThe 32X
68000 and sub CPUcommunication registers and flags, an interrupt to the sub CPU, word RAM, and the sub CPU's program RAM while it is stoppedThe Mega CD
the drive and everybodythe decoder's buffer, which a processor reads or DMA copies to one of several memoriesDiscs

When a step is too coarse

A step is not always enough. Two SH-2s can pass a value back and forth more quickly than that. If one writes to a port twice in a row, the other has to read the first value before it is overwritten. Brutal Unleashed: Above the Claw needs this, or it stops. So when an SH-2 reads or writes a communication port, the other one first runs up to the same time. Nothing else makes one run for the other: what one reads in SDRAM is what the other has written as far as it has run.

It is the same where a processor changes something in the middle of a step that decides what another part does:

  • Sound: before either processor writes to a sound chip, or the Z80 reads the YM2612, the chip is brought up to then (Sound).
  • The Mega CD: before the 68000 or DMA reads or writes any of its memory that the sub CPU can change, or uses its registers, the sub CPU catches up, and the devices too where that can make a difference.
  • The devices: before an instruction of the sub CPU sees or changes something of theirs that time changes, they get their time up to the end of that instruction's step.

What runs late

Giving every part a little time at each step is exact, but most of that time nothing happens that anyone can see. So many parts wait, and do all of it later in one go. They may never do that too late: a part says how long nothing about it can be seen, and whatever could change that makes it catch up first. This is now so for:

PartIt is left alone until
The VDPan interrupt, the Z80's interrupt line, the end of the picture, or an access to it
The YM2612 and the PSGsomebody writes to them (or reads the YM2612), or the frame ends
The SVPthe 68000 or DMA uses its memory or registers, a state is saved, or it is too far behind
The Mega CD's devicesone of them can have its next event, or somebody could see or change it
The sub CPUthe devices are due, or the 68000 or DMA could see or change what it does. That is at most 256 steps. One that only waits is left for longer, until something could change what it reads, or interrupt it
The 32X, with both SH-2sthe 68000 writes to it or to the cartridge, or reads its registers or frame buffer. At most 256 steps, and never past the end of a frame

The sub CPU and the 32X then still go through the same steps, one after the other, each with its own length. So their order is the one above, and what each processor sees of the others is the same.

Loops that only wait are passed, and not run. A game's processors often do nothing but ask whether something has happened yet. The 68000, the Z80, the SH-2s, the sub CPU and the SVP each have their own way to pass such a loop. When something that could end it happens, the processor is put where it would have been by then. No shortcut changes what comes out: Performance says why for each one, and how that was checked.

Savestates

A savestate is taken between two frames, and has sections for the console, the 68000, the Z80, the bus and everything on it. A 32X or a Mega CD adds its own. A state with other sections than the console has is refused.

  • Everything catches up before a state is saved, so what is late, or was found ahead, is not in it. A loaded console works that out again.
  • It has nothing of the game's file.
  • Values that no console can have are refused, such as a processor that is too far behind another, as catching up might then never end. A state that is refused changes nothing.
  • A Mega CD state names the boot code that it is from, as a copy of that code is in the sub CPU's memory. With other code, it is refused (Status).

From commit 1fd712ad794d. Edit this page