How the Mega CD BIOS works
This page is the technical breakdown of mdemu's own BIOS: what runs when, where everything is in
memory, and how it reads the disc, plays music and keeps saves.
How the Mega CD BIOS was made says how each part was found and how sure it is.
The notes at the top of boot.rs have every call, with the games that show it.
This describes our code, and not Sega's. Games only show what they can see from outside. How the
real BIOS does the same things inside is not known.
The parts
A Mega CD has two 68000 processors. The console's has the picture, the controllers and the
sound chips of the Mega Drive. The sub CPU has the disc drive, the PCM sound and the graphics chip. Each
runs its own part of the game, so each has its own part of the BIOS:
| Part | Source | Where it is | Size |
| The console's | boot/main.s | From address 0 of the console | About 3.9 KB, of at most 8 KB |
| The sub CPU's | boot/sub.s | From address 0 of the sub CPU's memory, which the console copies it to from $2000 of the BIOS | About 4.7 KB of code, from $1000 on, after its vectors |
Both are written in 68000 assembly. tools/boot.sh assembles them to boot/boot.bin (17,100 bytes, much of it
empty), and the emulator has that file in it. Nothing is loaded from outside. It has no menu, and it does not check the security
code or the region of a disc.
From power-on to the game
| Step | The console | The sub CPU |
| 1 | Sets up the controllers, the video chip and its memory, the Z80 and the PSG, and clears its memory. Makes its table of jumps | Is held |
| 2 | Copies the sub CPU's part, puts the region's letter at $1F0, and lets it run | Starts at $1000. Makes its table of jumps, clears its variables and turns on levels 2, 4 and 5 |
| 3 | Waits for the sub CPU's flag to be 1 | Reads the disc's contents, and then its first 16 sectors to $10000. Sets its flag to 1 |
| 4 | Stops the sub CPU. Copies the game's two programs from there: to $FF0000, and to the sub CPU's $6000 | Is held |
| 5 | Protects the sub CPU's memory below $5400, sets its own flag ($A1200E) to 1 and lets the sub CPU run | Clears where the sectors were, and takes the game's entries from its header |
| 6 | Waits for the sub CPU's flag to be 0, and sets its own to $20 | Gives word RAM to the console, in one block, goes to two banks, sets its flag to 0, and waits for $20 |
| 7 | Waits 1,000 rounds of a loop, lets interrupts in and goes to $FFFF0000 | Calls the game's first entry, and then its second one again and again |
The two flags are one byte for each processor. Either can read both, and writes only its own. That is
how they wait for each other. Why $20, $FFFF0000 and the wait in step 7 are there is in
how it was made.
Where the programs are on the disc: the header has the console's program's start and size at
$30, and the sub CPU's at $40. The console gets everything from $200 to the end of its program. When
something is between the two programs, it gets that too. The sub CPU's program is taken from the start of its sector.
The sub CPU's entries: $18 of its program says where a table of words is. Each is a distance
from the table's start, and 0 ends it. The first entry runs once, the second is the game, and the third is called
at level 2. What the fourth is for is not known.
Where things are in memory
The console
| Address | What is there |
$000000 | Vectors. Interrupts and traps go to the table of jumps |
$0001A0 | Where the BIOS starts and ends (0 and $1FFFF) |
$000280 to $0003FF | The calls: a branch every 4 bytes. Those that are not known come back at once |
$000400 | Where it starts |
After the end of boot.bin, up to $01FFFF | $FF: The Terminator reads there |
$FF0000 | The game's program |
$FFFB80 | 64 colours that are to be sent to the video chip |
$FFFD00 | The stack, which grows down from here. The vector for it is $FFFFFD00, which is the same place: Microcosm needs that. The table of jumps starts here, and grows up |
$FFFD06, $FFFD0C, $FFFD12 | Jumps for level 6 (the picture), level 4 and level 2 |
$FFFD18 | Jumps for TRAP 0 to 15 |
$FFFDA8 | A jump that is called at each picture, which is a guess |
$FFFDAE | A jump for saves in a cartridge. Ours says that there is none |
$FFFDB4 | A copy of the video chip's 19 registers, as the words that set them |
$FFFDDE, $FFFDDF | Copies of both flags, for the exchange |
$FFFDE0, $FFFDF0 | 16 bytes for the sub CPU, and 16 from it |
$FFFE20 | The controllers: for each, what is held and what was just pressed |
$FFFE26 | Not 0 while a game waits for a picture |
$FFFE29 | Whether the colours have to be sent |
$FFFE46 to $FFFE4D | A fade: its first colour, how many less one, how dark they are, and where they are |
The sub CPU
| Address | What is there |
$000000 | Vectors |
$0001F0 | The region's letter: J, U or E |
$000634 | A branch to the decoder's interrupt code, which Flashback goes to |
$001000 | Our code |
$005400 | Our variables. It cannot write below here, so a game cannot overwrite the BIOS |
$005E80 | The stack, which grows down from here |
$005E80 to $005E9F | The drive's state, which $81 gives games (below). Some games read it here without asking |
$005F0A to $005FFF | The table of jumps, of six bytes each |
$006000 | The game's program |
$010000 | The disc's first 16 sectors, only at the start |
The table of jumps
Each jump is a JMP with an address, so six bytes. A game changes the BIOS by changing the address, and can
call the old one from its own code. Most of the vectors point here, and not at the code.
| Sub CPU | What it is |
$5F0A | Sets four jumps from another program's header |
$5F10 | Waits for the next level 2, which is a guess |
$5F16 | Saves, with a number in D0 |
$5F22 | The drive, with a number in D0 |
$5F28, $5F2E, $5F34, $5F3A | The game's four entries |
$5F40 to $5F75 | Errors |
$5F76 to $5F9F | Levels 1 to 7. Ours are level 2 ($5F7C), 4 ($5F88) and 5 ($5F8E) |
$5FA0 on | TRAP 0 to 15 |
An error without a handler of the game goes to a loop at a place of its own. exception_at in boot.rs turns that
place back into the exception's number, which is how the lab can say "exception 3" when a game crashes.
Interrupts
| Processor | Level | When | What our code does |
| Console | 6 | At each picture | Sends level 2 to the sub CPU, clears $FFFE26 and calls $FFFDA8. A game can put its own code here, with $368 or in the jump |
| Sub CPU | 2 | When the console sends it | Notes that a picture came. Then it waits about 200 cycles, and calls the game's third entry |
| Sub CPU | 4 | 75 times a second, from the drive | Takes the drive's status, makes the state for games, moves the volume, and sends a command |
| Sub CPU | 5 | For each block that the decoder has | Puts the wanted sector in the queue |
In the hardware, a level 2 that comes while the sub CPU has it off is dropped. This is a change to the
emulator and not the BIOS (see how it was made).
How the two processors talk
Games can use the flags and the 32 bytes of shared registers themselves. They can also call $344, the BIOS's
exchange, at each picture:
- It clears bit 1 of the console's flag. When the sub CPU does not run, or bit 0 of its flag is 0, it goes to step 5.
- It changes bit 0 of its copy of the console's flag. It then waits until bit 1 of the sub CPU's flag
shows that the last exchange was seen. It looks at most 32,768 times, which is our own choice.
- The 16 bytes at
$FFFDE0 go to $A12010, and are cleared.
- The 16 bytes at
$A12020 come to $FFFDF0.
- It sends level 2, and sets bit 1 of the console's flag.
The console's 16 bytes are four slots of four bytes: a command's number, and a value. $350, $35C and
$358 fill slots 0, 1 and 3. Slot 0 has numbers of $5F22, and the others are for the game's own program.
The drive
Commands
The drive takes a packet of ten digits of four bits each:
| Digit | What it is |
| 0 | The command. Our code sends 2 (a question), 3 (play or read from a time), 4 (go to a time), 6 (pause) and 7 (go on) |
| 1 | 0 |
| 2 to 7 | The time: minutes, seconds and sectors. A question has its number in digit 3, and a track from digit 4 on |
| 8 | 0 |
| 9 | What makes the sum of all ten end in 15 ($F) in its lowest four bits |
A call only puts its command in command and sets command_waits. Level 4 sends it the next time it comes, and $80
sets carry until then. With no command to send, and once the game's first entry came back, level 4 asks three questions in turn: the time on the
disc, the time in the track, and which track it is.
The drive's state
$81 gives the address of 32 bytes, at $5E80. Level 4 makes them new 75 times a second:
| Byte | What it is |
| 0 | 0 is stopped, 1 plays, 5 is paused, and 8 looks for a place. For data, it is 8 until the first sector waits. $20 is reading the contents |
| 1 | The same for data: 8 while the drive looks for it, and 1 while it reads. 5 after $18, and 0 for music |
| 2 to 6 | 0. Not known |
| 7 | The track, as a plain number, and only for music |
| 8 to 10 | The time on the disc: minutes, seconds and sectors, with two digits in each byte |
| 12 to 14 | The time in the track, in the same way |
| 16, 17 | The first and last track, as plain numbers. The last one is $5E91, which Lunar: The Silver Star reads. The first one is a guess |
| 20 to 22 | Where the disc ends, which is a guess |
| 24, 26 | The most that the volume can be, which is a guess, and the volume |
| 28 to 31 | The time and mode of the last sector that the game took |
The bytes that are not listed are 0, and are not known.
It does not only repeat what the drive says:
- Byte 0 is 8 as soon as a call sends the drive somewhere, and for the next three level 4s. After
that, it follows the drive, whose own answer comes later. Both times and the track are cleared then too.
- After
$02, it is 0 and not 5, until something else is asked for. The drive is only paused.
- After
$10, it is $20 for 12 level 4s. The contents were already read at the start, and are not read again.
The disc's contents
Before the game starts, the BIOS asks for the last track, the disc's end, and where each track starts. Data tracks are
marked in the highest bit. $11 to $13, $15 and $19 look up a track there. $83 gives its start
and kind. The last track is byte 17 of the drive's state, at $5E91: Lunar: The Silver Star compares each track with it before it plays music.
Music
$11, $12 and $13 send the drive to the start of a track. $11 plays on after it, $12 pauses at its
end, and $13 plays it again. For the last two, level 4 looks for the end. It first waits until
the drive says that it is in the track, and then until it says that it is in another one, or at the end of the disc.
The volume goes from 0 to $400. $85 sets it, and $86 moves it by a given amount at each level 4. It is kept as a number with 16 bits
for its fraction. The lower one of it and the most that it can be goes to the hardware's fader.
Data
$17 reads on from a sector until something stops it, and $20 reads a number of sectors:
- Look for it. The decoder is reset, and the drive plays from the wanted sector. Level 5 is turned on,
even when the game has it off. Blocks do not count for the next three level 4s, as they can be from before the drive
had the command.
- Find it. At each level 5, the BIOS takes the time in the block's header. A sector before the wanted one is left out. The
wanted one goes into a queue, with where it is in the decoder's memory, and the next one becomes the wanted one.
- Go back when one was missed. When the sector is after the wanted one, or four already wait, the drive
pauses and is sent back to the wanted one when the queue is empty. The decoder has 16 KB, which is not quite seven
sectors. Four leave room for one that is being sent and one that is being written.
- Offer it. A new sector also counts in
fresh. $8A and $8B only offer waiting less
fresh, and level 4 clears fresh.
- End. After the last sector, the drive pauses. The decoder still decodes, but does not write. Level 5
goes on, as Flashback waits for it.
Why step 4 is there: a game can put its own code at level 5, call ours from it, and then call $8A. Radical Rex
does. Without fresh, its level 5 took a sector that its main program was waiting for. In mdemu's tests, level 4
had cleared fresh again before the main program went on. So its main program still got the
sector, and games that take sectors in their level 5 still got those from before.
A game then takes a sector in three calls:
| Call | What it does |
$8B | Takes the first sector in the queue, and starts to send it. D0 has its time and mode. $FF8004 says where to send it: a processor reads it, or DMA copies it |
$8C | Only when the sub CPU reads it: copies its time and mode to A1, and its 2,048 bytes to A0 |
$8D | Waits for the DMA to end, if there is one, and ends the transfer. The sector's time and mode go to bytes 28 to 31 of the state |
Without DMA, a transfer has 2,052 bytes: the sector's time and mode, and its 2,048 bytes. With DMA, it has only the 2,048.
$89 stops the decoder and clears the queue, and $96 does nothing.
The picture
- Colours:
$2E4 copies them to $FFFB80, and sets $FFFE29. $2E8 then sends all 64 to the video chip.
- Fades:
$390 sets up a fade from black, and each $38C is one of its eight steps. Each $388 makes the colours darker,
and sets the zero flag when all are black.
- The video chip:
$2B0 sets registers from a list, and updates the copy at $FFFDB4. $2A0 clears the planes, the window and
the sprites' table where that copy says they are. $2B4 to $2CC fill
memory and draw tiles. $2D0 and $2D4 send data with DMA. $2D8 and $2DC show the picture or not.
- Packed tiles and maps:
$2F0 unpacks tiles to A2, and $2EC the same tiles to the video chip, where the game
set its address. $330 unpacks a map of tiles to A2. boot.rs says what each rests on.
- Waiting for a picture:
$308 sets $FFFE26 to 3 and waits until it is 0. Some games need bit 1, and Wakusei Woodstock needs bit 0. The game's level 6 handler has
to clear it when it replaced ours.
Saves
$5F16 keeps files in the 8 KB in the Mega CD, which has only odd addresses, from $FE0001 on:
| Where | What is there |
| Block 0 | Not used |
| Blocks 1 on | The files' data, one after another, with nothing between them |
| Below block 127, growing down | An entry of 16 bytes for each file: 11 for its name, a flag, its first block, and how many it has |
| Block 127 | 64 bytes that say that the memory is formatted: how many blocks are free (four times), how many files there are, and text |
Each block has 64 bytes. A file with a flag uses 32 bytes of data for each of its blocks, and one without it uses 64. The second is a
guess. Four entries fit in a block. The free blocks are 126 (all but block 0 and the last one), less the blocks that files have,
and less those for all entries and one more. So formatted memory has 125. When a file is deleted, the ones after it move down.
Two parts come from different places:
- Block 127's form, with its texts
SEGA_CD_ROM and RAM_CARTRIDGE___, comes from jgenesis (segacd-core/src/backupram.rs),
which mdemu's Mega CD hardware is a port of. mdemu did not work it out from games, and where jgenesis has it from
was not looked into. Our BIOS uses that form when it formats the memory, and looks for SEGA_CD_ROM. Where it keeps the number
of files there is our own.
- How files are kept is our own. So saves from other emulators and consoles cannot be used.
Saves in a cartridge
mdemu's hardware also has the 128 KB cartridge for saves, as jgenesis does. It is always there, and is formatted
and saved with the rest. Our BIOS does not use it. $FFFDAE says that there is none: carry is set, and D1 is 0.
A game that looks at the hardware itself could find it. Whether any game does that and then also
asks the BIOS was not looked into. Using the cartridge through the BIOS is still to be done.
Differences between regions
Our BIOS has two:
- The letter at
$1F0 is J, U or E.
- On a Japanese console,
$364 waits up to 185 pictures, and stops when the sub CPU first calls $5F22. To
look for that, it stops the sub CPU for a moment at each picture.
It never looks at which game runs. There is no list of games, and no code for one of them.
Savestates
The sub CPU's BIOS is in its memory, so a savestate has a copy of it. The console's memory has the addresses of BIOS code too.
With another boot.bin, those would be wrong. ID in boot.rs is a number made from all of boot.bin (its FNV-1a hash), and
each Mega CD savestate has it. A state with another number is refused, and nothing changes. So each change to the
BIOS makes older Mega CD savestates useless. Games' own saves are not affected.
How it is tested
- Programs for both processors (
boot/test.s and boot/more.s) run on made-up discs and call the BIOS as games do.
mega_cd/tests.rs checks what they got. No game is needed.
- 212 faults were planted in copies of the code, one at a time: a missing line, another number, or a wrong
order. The tests find all of them. The program that plants them is not published.
- Games: see how it was made.
What it does not do
- It has no menu. A game that goes back to the BIOS's menu shows nothing more.
- It does not change discs.
- It does not use the cartridge for saves.
- Some calls come back at once without doing anything, and parts of the drive's state are not known. Both are
listed here.
- Its times are not those of a real BIOS. Nobody has measured one for this.