emuforge

The Mega CD

The Mega CD is a second computer beside the Mega Drive, with a 68000, memory, a drive, and chips for pictures and sound. The two computers share only some registers, one interrupt, a window into the second one's memory, and 256 KB of memory that they hand to each other.

This page is about the hardware, as mdemu emulates it. What runs before a game, and what games call, is software, and in How the Mega CD BIOS works. Discs has the way of a disc's data into memory.

Nearly all of this hardware is ported from jgenesis (credits), "the base" from here on. What is said here is from mdemu's code and its comments, which name forum posts, test programs and data sheets as their sources. What they call a guess is one here too. The code is in crates/mdemu-core/src/mega_cd.rs and mega_cd/.

PartWhat it isSource in mega_cd/
Sub CPUa 68000 at 12.5 MHz, with 512 KB of PRG RAMbus.rs, and mega_cd.rs
Word RAM256 KB, which both 68000s usewordram.rs
Graphics chipscales and turns pictures in word RAMgraphics.rs
PCM chip (RF5C164)8 channels, and 64 KB of its own memoryrf5c164.rs, sound.rs
Drive (CDD) and decoder (CDC)read the disc. The decoder has a buffer of 16 KB, and DMAcdd.rs, cdc.rs
Memory for saves8 KB, and a cartridge with 128 KBbackup.rs
Registerswhat joins the two sidesmemory.rs, bus.rs, font.rs

What each 68000 sees

The main CPU is the 68000 of the console (the Mega Drive):

AddressWhat is there for the main CPU
$000000 to $01FFFFour BIOS, and 0 after its end
$020000 to $03FFFFone of the four banks of PRG RAM, of 128 KB each. Both repeat up to $1FFFFF
$200000 to $23FFFFword RAM, which repeats up to $3FFFFF
$400000 to $7FFFFFthe cartridge for saves (below)
$A12000 to $A1202Fregisters

Two words in the BIOS are not from it: the vector at $70, for level 4 (the horizontal interrupt). The first always reads as $FFFF, and the second is the register $A12006. So a game can choose a handler in the main CPU's own memory. Bit 5 of $A10001 is 0 when there is a Mega CD.

The sub CPU has only 20 address lines connected, so $FF8000 is the same as $F8000:

AddressWhat is there for the sub CPU
$00000 to $7FFFFPRG RAM, with its vectors at the start
$80000 to $BFFFFword RAM in 2M mode, and its bank as dots in 1M mode
$C0000 to $DFFFFits bank of word RAM in 1M mode. In 2M mode, it reads as 0
$E0000 to $E3FFFthe 8 KB for saves, at odd addresses. They repeat up to $EFFFF
$F0000 to $F3FFFthe PCM chip, at odd addresses. It repeats up to $F7FFF
$F8000 to $F81FFregisters, written as $FF8000 on from here. They repeat up to $FFFFF

TAS can write to memory on the sub CPU, and cannot on the main CPU.

Main CPUSub CPUWhat it is
$A12000$FF8000main: level 2, reset and bus request. Sub: two LEDs, and the drive's reset
$A12002$FF8002word RAM. Main: also write protection and the bank of PRG RAM
$A12004$FF8004, $FF8006the decoder. The main CPU only sees its state and where it sends data
$A12006the lower word of the vector of level 4
$A12008$FF8008the next word from the decoder
$FF800Awhere the decoder's DMA writes
$A1200C$FF800Cthe stopwatch. Only the sub CPU writes it
$A1200E$FF800Eflags: first the main CPU's byte, and then the sub CPU's
$A12010$FF8010eight command words. Only the main CPU writes them
$A12020$FF8020eight status words. Only the sub CPU writes them
$FF8030, $FF8032the timer, and which interrupts are on
$FF8034 to $FF804Bthe drive
$FF804C to $FF8057two colours and 16 bits go in, and 16 dots of four bits come out, such as for text
$FF8058 to $FF8067the graphics chip

How the two 68000s talk

   MAIN CPU                                           SUB CPU
      |------ reset and bus request ------------------->|   it has no choice
      |------ level 2 --------------------------------->|   it needs to have it on
      |------ flags (1 byte), 8 command words --------->|   it has to look
      |<----- flags (1 byte), 8 status words -----------|   the main CPU has to look
      |<===== PRG RAM, in banks of 128 KB =============>|   main writes only while sub is held
      |<===== word RAM, 256 KB ========================>|   each in turn, or a bank each

The main CPU is in charge. Bit 0 of $A12001 lets the sub CPU run (1) or holds it in reset (0). Bit 1 takes its bus (1), so that it stands still. At power-on, it is in reset and has no bus. After a bus request, it goes on where it was. After a reset, it starts again with the stack and the start at 0 and 4 of PRG RAM. The bus is taken at once: bit 1 reads as it was written. The sub CPU can do none of this to the main CPU.

The console's reset button holds the sub CPU too. It resets the drive and the decoder, turns off the PCM chip, and clears what is in memory.rs, such as the flags, commands, masks and the timer. It changes neither memory nor who has word RAM, and does not stop the graphics chip.

PRG RAM: the main CPU chooses its bank with bits 7 and 6 of $A12003. It can only write while the sub CPU is held, in reset or without its bus. Other writes are lost, which also counts for the Z80: without that, Dungeon Explorer's Z80 writes over what the sub CPU uses. This code does not stop its reads. Write protection, the byte at $A12002, keeps the sub CPU from writing to the first $200 bytes for each 1 in it. It does not stop the main CPU or the decoder's DMA.

Flags, commands and status only keep values. They raise no interrupt, make nobody wait, and mean what the programs say. Each side only writes its own. A byte that is written to either half of the flags goes to the writer's own byte. The page about our BIOS has what it builds on them.

The only interrupt between the two is level 2, from the main CPU to the sub CPU. Bit 0 of the byte at $A12000 sends it (1) or takes it back (0). It reads as 1 until the sub CPU takes it. Bit 7 says whether the sub CPU has it on. Nothing goes the other way: the main CPU's interrupts only come from the VDP, so the sub CPU changes a flag, and the main CPU has to look.

Bits 1 to 6 of $FF8033 turn on the levels with the same numbers:

LevelFromWhat happens while it is off
6subcode, which is not emulated, so it never comes
5the decoder, for a sector or the end of a transfer. It has two masks of its ownit waits
4the drive, 75 times a secondit waits
3the timer, when it has counted downit waits
2the main CPUit is dropped when it is sent
1the graphics chip, when it is doneit is dropped. Turning it off also drops one that waits

The highest one that waits and is on comes first, when it is above the level in the sub CPU's SR. Taking it clears only that one. Its vector is at $60 plus four times the level, in PRG RAM. That level 2 is dropped is not from the base, which keeps it. It rests on Arslan Senki and on ares, and not on a test of the hardware (more).

The timer and the stopwatch both count every 1,536 cycles of 50 MHz, which is 30.72 microseconds. N in $FF8031 gives the first level 3 after N counts, and then one every N + 1, as loading N again takes a count. 0 gives none. The stopwatch counts up in 12 bits, and starts again after about 126 milliseconds. Both CPUs can read it.

An example: the sub CPU draws a picture

Word RAM is in 2M mode, and the main CPU has it. The sub CPU runs, with levels 1 and 2 on, and its flag is 0. The job's number and the flag's meaning are made up, as each game has its own.

  1. The main CPU writes the picture's parts to word RAM, from $200000 on, and the job's number to $A12010.
  2. It writes $02 to the byte at $A12003: DMNA, with bank 0 of PRG RAM. Word RAM is now the sub CPU's. $A12003 reads as $02, and what the main CPU reads in word RAM is 0.
  3. It writes $01 to the byte at $A12000. Level 2 waits. A word would also change the reset and the bus request in $A12001, and a byte does not.
  4. That write is done at the end of the main CPU's instruction. When the Mega CD runs next, the sub CPU finds level 2. Ten cycles later it takes it, so bit 0 at $A12000 is 0 again, and goes to the vector at $68.
  5. Its handler reads the job at $FF8010, puts 1 into its flag at $FF800F and returns. The main CPU sees the flag at $A1200F, so it knows that the job was taken.
  6. The sub CPU's main program starts the graphics chip, and level 1 tells it that the picture is done. It writes its answer to $FF8020.
  7. It writes $01 to the byte at $FF8003: RET, in 2M mode. Just before its next instruction, word RAM goes to the main CPU. Only then does it put 0 into its flag. So once the main CPU sees the 0, word RAM is its own.
  8. The main CPU has been reading $A1200F. It now reads the answer at $A12020, and lets the VDP copy the picture with DMA. DMA from word RAM is late by one word: first comes what was on the bus before. A game has to allow for that.

Word RAM

The low byte of $A12002 and $FF8002 says who has it. Bit 2 is the mode: 0 is 2M, with one block that is handed over, and 1 is 1M, where each side has a bank. Only the sub CPU can change it.

BitIn 2M mode, it reads as 1 whenIn 1M mode, it reads as 1 when
0, RETthe main CPU has the blockthe main CPU has bank 1, and the sub CPU bank 0
1, DMNAthe sub CPU has the blockthe main CPU has asked for a swap

2M: each side can only give the block away, and never take it. The main CPU does it by writing DMNA = 1, and the sub CPU by writing RET = 1. A 0 does nothing to the block. What happens on the wrong side is not the same:

  • The main CPU reads 0, and what it writes is lost.
  • The sub CPU stops. Its instruction still ends. It reads what memory has at that time, and its writes are kept. It goes on when the main CPU writes DMNA = 1, and the kept writes are done first. Marko's Magic Football has wrong map graphics without that. The graphics chip and the decoder's DMA to word RAM wait too.

1M: each side always has a bank of 128 KB, and none of them waits for it. The main CPU's is at $200000, and the sub CPU's at $C0000. The main CPU asks for a swap by writing DMNA = 0, and the bit then reads as 1 until it gets one. The sub CPU answers by changing RET. Only a change swaps the banks and ends the request. It can also swap them without being asked. A bank is not half of the block. Bank 0 has its even words and bank 1 its odd ones, so a change of mode mixes them:

   block (2M):    word 0    word 1    word 2    word 3    word 4    word 5
   bank 0 (1M):   word 0              word 1              word 2
   bank 1 (1M):             word 0              word 1              word 2

Each write of the sub CPU sets the mode, RET and the priority mode together. RET chooses the banks in 2M mode too. DMNA = 1 and RET = 1 also give the block away in 1M mode. What the sub CPU writes there is done just before its next instruction. The code calls that a hack: it keeps Silpheed from a race that can crash it.

The sub CPU's dots ($80000 to $BFFFF, 1M): each address has one dot, in the lower four bits. So the 128 KB of its bank are 256 K dots, and it can change one alone. The priority mode, in bits 3 and 4 of $FF8003, says which dots are written: all (0), only where there is a 0 (1), or only dots that are not 0 (2). 3 is not valid and is the same as 0. It also counts for the graphics chip, in both modes, and not for what is written at $C0000.

The main CPU's cells ($220000 to $23FFFF, 1M): its bank is a picture of rows, 64 cells of 8 by 8 dots wide. Here it reads as cells, one below the other and then the next column. So what is sent to the VDP is already in the order of cells. There are five pictures: 32 cells high at $220000, 16 at $230000, 8 at $238000, and 4 at $23C000 and $23E000.

The graphics chip

It draws a picture line by line. Each line is a straight walk through a larger picture, the map, in any direction and at any speed. That is how it turns and scales. Everything that it uses is in word RAM:

  • The map is 256 or 4,096 dots wide and high. It has a word for each stamp of 16 by 16 or 32 by 32 dots: bits 0 to 10 are its number, 13 and 14 turn it by 0, 90, 180 or 270 degrees, and 15 flips it horizontally. The number, times 128 bytes, says where its dots are. The lowest two bits do not count for large stamps. Stamp 0 is empty, whatever is in memory.
  • The trace table has four words for each line: x and y of the start in eighths of a dot, and then how far x and y go for each dot, with a sign, in parts of 1/2,048.
  • The image buffer gets the dots, as the priority mode says. It has columns of cells, 1 to 32 cells high.
RegisterWhat it is
$FF8058bit 15: it works. Bits 2 and 1: the large map and large stamps. Bit 0: the map repeats. If not, there are zeros around it
$FF805A, $FF805E, $FF8066where the map, the image buffer and the trace table are, divided by 4. Writing the last one starts the chip
$FF805C, $FF8060how many cells high the buffer is, less 1, and where in its first cell the picture starts
$FF8062, $FF8064how many dots the picture is wide (up to 511) and high (up to 255)

How long it takes is mostly a guess, as the code says. A word of word RAM takes 3 cycles of the sub CPU. A line of W dots needs 4 words for the trace, 2 for each dot (map and stamp), and 1 for every four dots that it writes. So a picture takes 3 x height x (4 + 2 x W + W / 4) cycles. With 256 by 160 dots, that is about 22 milliseconds.

The whole picture is drawn when the chip starts, and then that time passes. In 2M mode, neither happens while the main CPU has word RAM. At the end, bit 15 is 0, and level 1 comes if it is on. The height is 0 then. Real hardware counts it down as it goes.

Sound

The PCM chip makes a sample for each 384 cycles of the sub CPU, 32,552 a second. Each channel goes through its 64 KB in steps of 1/2,048 of a byte. A byte has a sign (1 is positive) and seven bits. $FF is not a sample: the channel goes on at its loop address. If there is another one there, it is silent, and what hardware does then is not known.

AddressWhat it is
$FF0001 to $FF000Dfor the chosen channel: volume, left and right (four bits each), speed and loop address (two bytes each), and the upper byte of its start
$FF000Fbit 7 turns the chip on. If bit 6 is 1, the lower bits choose a channel, and if not, one of 16 banks
$FF0011a bit for each channel. 0 is on when it is written, and 1 is on when it is read
$FF0021 to $FF003Fwhere each channel is. This can only be read
$FF2001 to $FF3FFF4 KB of its memory, in the chosen bank. While the chip is on, it can be written, but reads as 0

mega_cd/sound.rs brings the chip's sound and the disc's music, with 44,100 samples a second, to the mixer's rate, with a curve through four samples. The mixer has a sample every 1,008 cycles of the console's clock. Whole numbers keep the place, so nothing drifts. PCM goes through a low pass of 7,973 Hz. Next to the YM2612, it is mixed at -6 dB, and music at -7 dB. Both are added after the console's own filters. The numbers are the base's, and the mix was not compared with it.

Memory for saves

The sub CPU has 8 KB, at odd addresses from $FE0001 on. The main CPU has the cartridge's 128 KB, at odd addresses from $600001 on, which repeat up to $6FFFFF. $400001 gives its size: 4, for 8 KB times 2 to the power of 4. Bit 0 of $700001 lets it be written, and is 1 at power-on.

Both are always there, and both are in one save. Without a save, they start formatted, as some games (Popful Mail) fail badly if not. What files look like is up to software. Our BIOS uses the 8 KB and not the cartridge (more).

How they are kept in time

ClockCycles a secondWhat has it
The console's53,693,175 (NTSC), 53,203,424 (PAL)the console. The main CPU has a seventh of it
The Mega CD's50,000,000the Mega CD. The sub CPU has a quarter of it

The console's time is turned into the Mega CD's as a whole fraction. What is left over counts the next time, so they do not drift apart. The drive has its own clock in the hardware. Here, it follows the 50 MHz, which is easier.

Each instruction of the main CPU, or time that it stalls, is a step. The others then have the same time, in this order:

  1. The main CPU runs. It reads the Mega CD as it was after the last step. Its writes wait for the end of this one.
  2. The Z80 has that time. What it writes is done at once.
  3. The VDP has it. Its DMA also reads word RAM as it was after the last step.
  4. The Mega CD's devices have it: first the drive, with the decoder and its DMA, then the timer and the stopwatch, the graphics chip, and the PCM chip.
  5. The sub CPU has it. An instruction does everything at its start, and then its time passes. What does not fit into this step counts in the next ones.
  6. A 32X has it, if there is one. Then the main CPU's writes are done.

This says what each one can see:

  • The sub CPU sees what the main CPU wrote from the next step on, and the main CPU sees what the sub CPU had done by the end of the last step.
  • The sub CPU sees the devices at the end of the step, as they had all of its time first. So an interrupt can reach it early, by less than a step. When it starts the graphics chip, the chip's time begins after this step.
  • When it hands over word RAM while the graphics chip works, the chip still has this step's time with word RAM as it was.
  • A sub CPU that word RAM stops loses that time. The devices go on, but for the graphics chip.

What cannot be seen can be done later. The devices can be left while they only count. The sub CPU can be up to 256 steps behind, and is left for longer while it waits. The ends of these steps are kept, so that each instruction still sees the devices as they are at the end of its own step. Performance says why the result is the same. These bring them up to date:

WhatWhy
The drive's clock of 75 Hz, the timer's end, the graphics chip's end, or each of the disc's samples while the decoder can use DMAit can change something. All else that time changes is only brought up to date when it is used
The sub CPU uses something of the devices that time changes, such as PCM, the decoder or the stopwatchit sees them at the end of its step. If it can make them due sooner, its batch ends at the last step before that
The sub CPU starts the graphics chip, or hands over word RAM while it worksboth depend on when it does that
An interrupt that the sub CPU can take, or DMA to PRG RAMit can end a wait. First, the sub CPU is put where it would be
The main CPU reads or writes word RAM, or its DMA reads itboth processors use it. Only the sub CPU has to catch up
The main CPU reads PRG RAM through its windowthe same, and a wait is put where it is now, as it can write there
The main CPU writes a register from $A12000 on, or PRG RAM while the sub CPU is in resetthe sub CPU can wait for it, be held by it, or start with it
The main CPU reads a registerthe sub CPU may have written it. $A1200C is the stopwatch, so the devices catch up too. $A12008 takes the word, and the next one comes
Reset, loading a save, or making a savestateeverything has to be at the same time

A wait may call subroutines and write to PRG RAM, if its registers and all that it wrote are as they were the last time. It ends when something could change what it reads, or when an interrupt that is enabled comes. Between frames, all of this is up to date.

With a 32X

The disc decides: there is a 32X when its header has SEGA 32X at $100. Seven of the nine releases that need one say it. Slam City (USA) and Surgical Strike (Brazil) do not. With the setting "32X with discs" at "Always", each disc has one (Settings::disc_32x). It is not for all discs, as some look for a 32X: with one, Surgical Strike (Europe) asks whether to play it as a Sega CD game (A), or to insert disc 2 for the 32X (Start).

  • The Mega CD keeps its addresses, also when the 32X is on. The BIOS's vectors stay at 0, and not the 32X's. The 32X has MARS at $A130EC, its registers from $A15000 on, and, when it is on, its frame buffer from $840000 on.
  • There is no cartridge. Bit 8 of the SH-2s' register at $4000 says so, and our boot code for the 32X then waits for the disc's program.
  • Only the main CPU reaches both. The SH-2s and the sub CPU share nothing, so data goes from word RAM, or from the decoder, through the main CPU to the 32X.

This was seen in the seven discs: the main CPU puts a header of 56 bytes and then a program into the frame buffer, gives it to the SH-2s, and writes _CD_ from $A15120 on. Our boot code copies the program to where the header says. What it does next is a guess: it answers M_OK and S_OK, and starts the program. With that, all seven go on (s32x/boot.rs). None of it was compared with the base.

What is not emulated or not known

Status says what was tested. On this page:

  • Waits for memory: the sub CPU's accesses take no more time, whatever else uses that memory.
  • The graphics chip draws all at once, and its time is mostly a guess.
  • Level 2 while it is off needs a test on a console. Subcode and level 6 are not there.
  • The drive's clock bit, bit 2 of $FF8037, is kept and does nothing. Resetting the drive with $FF8001 takes no time, and the version in that register is 0.
  • Not there: changing discs, a cartridge's game with a disc, and a 32X for a disc that does not ask for one.

From commit 1fd712ad794d. Edit this page