Discs
This is the way of a disc's data, from a file on the computer to the memory of the
Mega CD, and from there to the game. It is about files and hardware.
How the Mega CD BIOS works says how the software uses that hardware,
and which calls games make.
.cue + .bin, .chd, or a .zip with either
| forge-disc: tracks, and raw sectors of 2,352 bytes by number
| mdemu-disc: gives them to the core
v
Inserted (disc.rs): a time on the disc becomes a sector's number
|
v
drive (cdd.rs): 75 sectors a second while it plays
| |
| any sector | a sector of music, sample by sample
v v
decoder (cdc.rs) volume --> resampler (sound.rs) --> mixer
16 KB buffer ^
| |
| one destination at a time |
+--> DMA --> PRG RAM |
+--> DMA --> word RAM ---> main CPU, or DMA of the VDP |
+--> DMA --> PCM RAM ----> PCM chip -------------------------+
+--> sub CPU reads $FF8008
+--> main CPU reads $A12008
The file
forge's forge-disc crate reads discs for all of emuforge's emulators, and knows no console. What
is here is as in the commit of forge that mdemu's Cargo.toml files name.
| File | What can be read |
.cue with its files | BINARY files. Tracks of AUDIO, MODE1/2352, MODE2/2352 and MODE1/2048 |
.chd | version 5, of a CD |
.zip | one with a cue sheet and its files, or else a CHD. If there are several, the name that sorts first |
A disc becomes a list of tracks and a list of parts. A track has its number, whether it is data
or sound, and three sectors: where the pause before it starts, where it starts, and where it ends. A
part says where some sectors are: in which file, at which byte, how far one is from the next, and in
which format. To read a sector, forge-disc looks up its part.
Every format comes out the same, as the 2,352 bytes of a raw sector:
- Music has 588 samples in stereo, of 16 bits, with the lowest byte first. A CHD has the highest
first, so they are swapped.
- Data with only its 2,048 bytes (
MODE1/2048) gets its sync bytes and its header again. Its
error correction is zeros.
- Pauses that are in no file are zeros. If the pause of a CHD's first track is not in the file,
it is taken to be the disc's first two seconds, which are not counted.
- Subcode in a CHD, 96 bytes after each sector, is not used.
A CHD is packed in hunks, and the last one that was read is kept.
What is checked: a disc has 1 to 99 tracks, whose numbers and places follow each other, and ends
before 100 minutes. Cue sheets have to have valid times, and an index 1 for each track. From a
file's name, only what is after its last slash is used, so a sheet cannot name a file in another
folder. A CHD's header is checked before room for its data is made.
From a ZIP, without unpacking it
A ZIP is never unpacked as a whole. But a packed file can only be unpacked from its start, and
the drive can go to any sector. So forge-disc goes through each packed file once when the disc is
opened. It keeps no data, but a copy of the unpacker's state every 2 MiB. To read a place later, it
goes on from the copy before it, or from where it is if that is nearer. This first pass also
compares the size and the CRC-32 with the ZIP's directory, and a file that differs is not opened.
A file that the ZIP only stores is read in place. It is read through once and checked in the
same way when the disc is opened. A cue sheet's files outside a ZIP have no such check. Cue sheets in a ZIP can have up to 1 MiB, and their files are found beside
them, also with other capital letters. Passwords and other ways to pack are refused.
Why the core opens no files
mdemu-core has a trait, Disc, with two functions: tracks, and read, which gets one raw
sector by its number. Where sectors come from is up to who uses the core. Here, that is a thin
type in crates/mdemu-disc, which the app and the lab share. So the core knows no files, and a test
can make up a disc. The same number has to give the same sector each time, as the same game and
input have to give the same output on every machine.
When a sector cannot be read, mdemu-disc writes that to the log, and the game gets one of
zeros. A disc is not in a savestate. The sector that the drive read last is, and so are all of the
console's memories.
Which disc it is
Its id is the SHA-1 of each track's number, kind and three places, and of the 2,048 bytes of
data of the first 16 sectors (disc_data in mdemu-disc). Those sectors have its header and its
first programs. A file's name and format do not count by themselves. But two images of a disc get
different ids if they have other places for its tracks or pauses. Space Ace has the same id from a
cue sheet and from a CHD. The app uses the id for saves and savestates, and the lab finds releases by it
(its README).
To identify a disc, the lab does not go through a packed cue sheet's files in a ZIP, as it only
needs the start. That finds no damage, so it opens the disc in the normal way to run it. A CHD in a
ZIP is always gone through, as its index can be at its end.
A Mega CD disc has SEGADISCSYSTEM at the start of its first sector's data. The core also
checks that its tracks start at 1, follow each other, and end early enough for two more seconds
after the last one.
Its region (mega_cd/disc.rs, console.rs) comes from the first of these that has one:
- The player's setting.
- The security code in the first sector. There are three, of different lengths. mdemu hashes
$156 bytes from $200 of the data on, as many as the shortest code has. It compares that with
three hashes, and none of the code is in mdemu. These hashes put 390 discs into three groups. The
groups are their regions, but for The Lawnmower Man (Europe), whose code is America's, and an
aftermarket disc.
- The region field of the disc's header, which is read as a cartridge's. This can
be wrong: Snatcher's American header says Japan.
- America.
The region sets whether the console is NTSC or PAL, what a game reads at $A10001, and the letter
that the BIOS has. mdemu's BIOS does not check the security code or the region.
A 32X is there when the header says SEGA 32X at $100. The Mega CD has more.
There is no list of games for any of this.
The drive
Time on the disc
The drive counts 75 sectors a second, and starts two seconds before the first sector in the
file. Sector 0 is at 00:02:00 (minutes, seconds, sectors), so a time is 150 more than its sector's
number. The first two seconds belong to the first track. After the last track, the drive goes on for
two seconds, as in the base. That was not compared with a console. Both are zeros, but sectors
beside a data track also have sync bytes and a header.
cdd.rs goes forward 44,100 times a second, once for each sample of a disc. 588 of these are one
tick, 1/75 of a second. At each tick, level 4 waits for the sub CPU, also without a new command.
While the drive plays, it reads a sector and gives it to the decoder. Its time then goes on by one.
Commands and answers
A command has ten digits, one in each byte from $FF8042 to $FF804B. Writing the last byte
sends it, and its answer is there at once, in ten bytes from $FF8038 on. They stay as they are
until the next command, even when the drive does something else. A program that wants to know more
has to ask again.
| Digit 0 | What it does |
| 0, or any other | gives the state. $B is not emulated either |
| 1 | stops the motor. At the next tick, the drive reads the contents |
| 2 | asks for what digit 3 says: 0 time on the disc, 1 time in the track, 2 track, 3 the disc's end, 4 first and last track, 5 where the track in digits 4 and 5 starts. For 5, it goes back to the contents, so playing ends |
| 3, 4 | go to the time in digits 2 to 7. 3 plays from there, and 4 pauses there |
| 6, 7 | pause, and go on to play. If 7 comes while the drive seeks, it plays when it is there, also after 4 |
| 8, 9 | forward and back by 100 sectors each tick, without sound or data |
$A | skips the number of "tracks" in digits 4 to 7, and pauses. Digit 3 says which way. Each counts as 15 sectors, which is not exact |
$C, $D | closes and opens the tray. The same disc is always in it |
The answer's first digit is the state: 0 stopped, 1 playing (and preparing to), 2 seeking, 3
scanning, 4 paused, 5 tray open, 7 a wrong command, 9 reading contents, $A skipping, $B no disc,
$C the disc's end, $D its start, and $E a moving tray. The second is what was asked for, or
$F while the drive seeks or skips, has no disc, or its tray is open or moving. When it is stopped,
all but the last are 0. Then comes the answer. The last one is such that the first nine and it end
in $F when they are added. The last digit of a command is not checked. A time with 60 seconds
or 75 sectors, or more, is a wrong command.
Seeking
How long it takes is not exact. The code says that a real seek does not simply take longer with
distance. Here it does, from a supposed 1.5 seconds across a disc of an hour:
-
The distance in sectors, times 113 and divided by 270,299, is rounded to how many ticks it takes.
A command 3 or 4 that moves takes at least 7 ticks, 93 milliseconds. The Japanese Cobra
Command needs that.
-
To play, it then takes 6 more ticks, 80 milliseconds, also where it was already in the right
place. The first sector comes at the tick after these. Time Gal stops at the start if this is
too short.
-
When a command ends playing, the drive reads one more sector at the next tick. The state
changes at once, but nothing else happens in that tick. Radical Rex crashes without it. So after
a command to seek, one more old sector can reach the decoder.
-
Command 7 during a seek makes the drive play when it is there, also when the seek was to end in
a pause. This is as in ares, and Final Fight CD's intro needs it.
The decoder
The decoder is an LC8951. Its 16 KB go around, so they have nearly seven raw sectors. A program puts
a register's number into $FF8005, and reads or writes that register at $FF8007. Each access goes
on to the next number, but for number 0.
| Number | When read | When written |
| 1 | interrupts and transfer. 0 means yes | bit 6: level 5 at a transfer's end. Bit 5: level 5 for a sector. Bit 1: transfers are on |
| 2, 3 | bytes left, less 1. At the end, the count is below 0 | how many bytes to send, less 1 (12 bits) |
| 4 to 7 | the last sector's header (time and mode), or its subheader | 4 and 5: where to send from. 6: start. 7: the end was seen |
| 8, 9 | where the last sector's header is in the buffer | where the next sector goes |
| 10, 11 | where the next sector goes | 10, bit 7: decode. Bit 2: write to the buffer. 11, bit 0: show subheaders |
| 12, 13 | errors, always none | where the last sector's header is |
| 14, 15 | reading 15 takes back the decoder's flag for a sector | 15 resets it |
What the drive reads, the decoder gets whole, at the tick, if it decodes. That can be a sector
of music too. It takes the header from bytes 12 to 15 and the subheader from 16 to 19. If it may
write, all 2,352 bytes go into the buffer. For the first one since writing was turned on, the place
in numbers 8 and 9 moves by 12, from the sync bytes to the header. Then it moves by 2,352 for each
sector. It finds and corrects no errors. Its registers always say that there are none.
Level 5 comes at each tick while it decodes, also when the drive has no sector. That is for the
sector flag, which goes away when number 15 is read, or by itself after 235 of a tick's 588 samples
(5.3 ms). When the sub CPU takes level 5, only the level is cleared. Snatcher, Batman Returns
and Robo Aleste need that. A transfer's end only brings level 5 if the last interrupt was cleared
in the decoder too. A sector brings none while a transfer's end that is on has not been cleared
there.
Where data goes
$FF8004, the upper byte, has the destination. Writing it sets the DMA address to 0 and clears
bit 15, so the address comes after it. Then writing number 6 starts. In the same word, bit 14 says
that a transfer runs, and bit 15 that the data has ended.
| Number | Where | How the DMA address in $FF800A counts | When it waits |
| 2 | the main CPU reads words at $A12008 | | for each read |
| 3 | the sub CPU reads words at $FF8008 | | for each read |
| 4 | PCM RAM, in the PCM chip's chosen bank | times 4, up to 4 KB | never |
| 5 | PRG RAM | times 8, up to 512 KB. Write protection does not stop it | while the sub CPU is in reset or without its bus |
| 7 | word RAM | times 8, up to 256 KB in 2M mode. In 1M mode, up to 128 KB of the sub CPU's bank | in 2M mode, while the main CPU has it |
| 0, 1, 6 | nowhere | | |
For 2 and 3, only the chosen CPU takes words, and a word is always there at once. The other one
sees the waiting word and does not take it. Writing there skips a word. Bit 15 is already 1 when the
last word waits, and bit 14 is 0 once it is taken.
DMA is fast on purpose: with each sample of the disc, it sends up to 128 bytes, but not in the
sample with the tick. The code calls that arbitrary: it only lets DMA end quickly. A sector of 2,048
bytes takes 16 samples, 0.36 milliseconds, if nothing holds it up. PRG RAM and word RAM get
whole words, so a last odd byte is left out. No processor is slowed down.
An example: one sector to word RAM
The drive is paused at 00:02:00. The program wants sector 16, which is at 00:02:16. Word RAM is
in 2M mode and the sub CPU has it, with levels 4 and 5 on. The sub CPU's program does all of this,
and a game can leave most of it to the BIOS.
- The decoder is set up: 15 to reset it, 0 in numbers 8, 9, 12 and 13, so the buffer starts at
0, and
$22 in number 1 (level 5 for sectors, and transfers on). Then it puts $84 in number 10
(decode, and write).
- The command is
3 0 0 0 0 2 1 6 0 3: play from 00:02:16. With its last digit, the drive
begins to seek, and its answer begins with 2 F. Level 5 comes at each tick now, but there is no
sector yet. So a program has to look at the header.
- Seven ticks to go there, and six to get ready. Level 4 comes at each one. The sector comes
at the 14th tick, 173 to 187 milliseconds after the command. The first tick can come at once or
after 13.3 milliseconds, so it is not always the same.
- The drive asks
Inserted for time 166. That is sector 16, so Disc::read is called with 16.
For MODE1/2352 from the start of a file, forge-disc reads 2,352 bytes from byte 16 x 2,352 on.
The drive's time is now 167.
- The decoder puts it into its buffer, at 0 to 2,351. The next sector goes to 2,352, and the
place of the last header is 12. The header has
00 02 16 01: its time in decimal digits, and
mode 1. Level 5 waits.
- The sub CPU takes level 5. It reads the header at numbers 4 to 7 to know that it is the
wanted sector, its place (12) at 8 and 9, and number 15 to take back the flag.
- It sets up the transfer: 7 in the byte at
$FF8004 for word RAM. That also sets the DMA
address to 0, which is its start. From number 2 on, it writes 2,047 (2,048 bytes, less 1) with
its low byte first. Then comes 16, also low byte first, as the data is after the header's 4
bytes. With number 6, it starts. All of this goes through $FF8007, as the number goes on by
itself.
- DMA writes the 2,048 bytes to
$80000 to $807FF of the sub CPU, in 0.36 milliseconds. If
the main CPU had word RAM, DMA would wait. At the end, bit 15 of $FF8004 is 1 and bit 14 is 0,
and the program writes number 7 to say that it has seen that. Level 5 did not come for it, as
$22 leaves that off.
- The sub CPU gives word RAM away with RET, and the main CPU finds the sector at
$200000. It
can use it, or send it to the VDP with DMA. The Mega CD has that part.
- 1/75 of a second later, sector 17 comes, if nothing stops the drive. The decoder puts it at
2,352, and its header is at 2,364. Sector 22 goes over the first 80 bytes of sector 16, so a
sector has to be taken within 6 ticks, 80 milliseconds.
At that pace, 75 sectors of 2,048 bytes come in a second: 153,600 bytes. When our BIOS misses a
sector, it sends the drive back, which is another seek.
Music
A raw sector of music is as long as a tick: 588 samples, left and right, of 16 bits. The drive reads
one at each tick, and plays it over the next one. There is sound when the sector that it read is
music and it still plays. So all of a track's last sector is heard, also before data, as in ares. In
the base, the last one before data is silent. Otherwise, it is silent, such as while it
seeks or scans. Bit 8 of $FF8036 is 0 when the sector that it read is music and the next one is
too, and 1 if not.
Volume goes from 0 to $400, in $FF8034. Higher values count as $400. It is 0 at power-on.
It does not jump there: it moves by one with each sample, so from silence to full takes 23
milliseconds. Below 4, a sample is multiplied by the volume divided by 1,024. From 4 on, it is
multiplied by the volume without its lowest two bits, again divided by 1,024. This is based on
documentation of the LC7883, as the code says. The whole word is taken as the volume, as in the
base. Whether that is right was not looked into.
Then mega_cd/sound.rs brings the samples to the mixer's rate, on a curve through four
samples. They are mixed at -7 dB, and added after the console's filters. The Mega CD
has that, with the PCM chip.
What is not there
Status has what was tested, with which games, and what else is open.
- Changing the disc. The tray opens and closes, and the same disc is still in it. So games on
several discs cannot go on to the next one.
- Subcode, with its interrupt (level 6).
- Errors on a disc and their correction. The decoder never says that there are any. A sector
that cannot be read from its file is zeros.
- Times of a real drive: for seeks, for the drive's reset, and for DMA.
- The drive's own clock. It follows the Mega CD's 50 MHz.
- A cartridge with a disc.
- Other files: cue sheets with
WAVE or other kinds of files, and other kinds of tracks.
- What a drive does with digits above 9 is not known. Here, a time that they make is used when
its minutes, seconds and sectors are all valid.