How does a CD drive know where to look on a CD?

Solution 1:

It knows where it is on the CD the same way it knows where it is on a hard drive. There is a filesystem on the CD, the CD drive will go all the way in and start reading the first bytes of information from the first track (The MBR would be a analogus thing on a hard disk). Those bytes will tell the drive information like how big the CD is, how much is in use, who manufactured it (in the case of CD-Rs), where to look to start reading the directory structure. The directory stucture will tell what offsets the files are are at and then the drive can do the math based off of the header information it knows where to move the laser to read the data.

If you want the actuall tech specs of how you would build your own optical drive you would need to get a copy of one of the Rainbow Books (ever hear of red book audio for some of your old games?). These books are the official specs for defining what is a "CD". Each color is named after what the color of the manual the book was originally bound in when the spec was published.

Solution 2:

After reading some libcdio documentation, I think I understand how this system works.

First I'll talk a bit about floppy drives and RAM and then connect that to compact discs.

In RAM each byte has a number assigned to it. When we want to access data in that byte, we send the number to the processor and it puts the data in a register which we can later process (there's also the story of segments and offsets for x86, but I don't think it's relevant for this analogy). The system was more or less simple and it allows relatively easy access to memory.

Now on diskettes (and HDDs too) the disk is separated into concentric circles and they mark the tracks. Tracks which are parallelly placed above or below each-other form what is called a cylinder (remember, today's floppies are double sided, and on HDDs we can have several double sided disks). The disk is also divided into sectors. A sector is basically an angle whose vertex is in the center of the disk. Area bounded by track and sector is called block (although many people call it sector too). Another important thing is the head. As I said, today floppy disks have two sides and in HDD, each disk has two sides. On each side, there is a head which reads the data and each head has its own number.

When we want to get data from a floppy (or older HDD), we send the cylinder number, head number and sector number to the drive controller and it moves the head to appropriate location. Dimensions of disks are standardized and locations of tracks and sectors are too. We have a stepper motor which will move the head along the radius of the disk. The controller knows the distance between tracks and the current location of the head and will calculate by how many tracks it needs to move the head to get to the required track. When the head is moved to correct track, it will read the data on the diskette. Each block (or as some call it sector) there is some metadata written in addition to front data. The metadata has information on sector number and the CRC data. The sector number is read and the controller now knows where the head is actually on the disk and knows by how much to rotate disk to put head on the needed sector (or as some call it block). Since (on floppies and some very old HDDs) there is fixed amount of blocks on each track (and each block "take up" same angle), the controller can calculate the angle by which it needs to rotate the disk so that the needed block is next to the head. This method of getting the location is also called CHS form cylinder, head sector and is still used for diskettes. HDDs moved on to a more user-friendly method of addressing.

So now we're at logical block addressing. The idea is very simple. Instead of having programmer calculate address of data on the disk using CHS, we'll just assign a number to each block, just like each byte has its number in RAM, and access the data using that. HDD electronics do the LBA to CHS conversion internally and now we don't have to think too much about that.

Finally I've reached the compact disc area. First, I'll mention the main difference between compact disks and HDDs/floppies: The track on CD is a spiral and not a circle. Now that I've said that, I'll move a bit to CD history. Since LBA is so easy and simple to use (from OS and application programmer point of view, it's more complicated for drive firmware programmer). The esteemed scientists and engineers which were designing compact disk standards saw the beauty of LBA (which at that time was being implemented in SCSI, it won't become common-place on home computers for two more decades) and decided that they wanted something like that on the compact disc.

There seem to be several ways to get a block address on CD and I'll start with the simplest one: logical sector number (remember, terms like block, sector are often used as if they have same meaning). Using LSN, entire spiral is divided into 2352 byte blocks of main data (which are called sectors here). Each block has its number. Then there's the LBA addressing format. It's LSN-150. From what I've read, the 150 sectors are part of the lead-in of the track and that part of the disc is supposed to be empty.

I'll now explain what "frame" is. Basically each block is divided into 98 frames, which are basically a sort of smaller blocks. Each frame has 33 bytes of data. Of those 33 bytes, 24 are actual data expected by the player and nine are metadata. Eight bytes are used to provide error correction and one byte is used to provide so-called sub-channel data. Of those subchannels, P and Q are interesting to us because they actually contain the information about the location of the sector those frames are in. The Q channel has data about location in absolute time which is related to another block system which is next topic. The frame is then encoded on the disc using 8-to-14 modulation scheme and additional 27 bit word is added to the end. The word is specially made so that it's different from surrounding data and is used to separate two frames.

Now on to MSF (minute, second, frame) addressing. This addressing system came up because of the intended use of compact discs: digital audio. It must have seemed logical back then to measure space on disc by amount of time you could play sound using that space. As I said, 98 frames make block. There are 75 frames in a second using this addressing system. The interesting part is that here "frame" means block and not the frame I discussed above. Also there are of course 60 seconds in a minute. That's why we can sometimes see CDs marked as 74 minute or 80 minute discs.

I think I've finally come to the part which the question asked: How does CD drive know where to look. Well it works the same way as HDD and floppy. The general layout of the spiral is known (and drive can detect information about the particular disc itself when it's inserted) and the head moves to the location where it's expecting the data to be. Since stepper motors are used for that, drive knows exact location of the head and it can easily calculate by how much steps it needs to move in order to get to the expected location. It then rotates the disc so that the expected block is at the read head and we're ready. The drive gets address of the data on disc in LBA format from OS and it does the conversion internally using the addressing systems I described above. There's also the information in each block about its location which is helpful too.

Solution 3:

Someone who knows can probably give you a quick answer, I won't pretend I know, but with regard to a couple of places to start researching...

There are several different file systems available which may or may not provide different answers, the most common that I have seen are ISO9660, Joliet, and UDF which along with others are briefly explained in the disktype documentation at sourceforge. A deeper comparison can be easily accessed at the Wikipedia "Comparison of file systems" page. Or if you wanted to get much deeper into a particular one, let's say UDF then checking out where the specs are published would be the next place to go, in that case the Optical Storage Technology Association.

Which I had a definitive answer but here is some reading that may be interesting in any case.