# SPDX-License-Identifier: MIT """The CD drive, seen from the gate array: the 10-nibble status and command exchange at 75 Hz, and a drive model behind it that reads the disc image (high-level: the drive's microcontroller program has never been dumped, handoff-brief.md). Sources: - Sega's hardware manual, p.32-33 ($FF8036: HOCK starts the exchange, D/M; INT4 when status 7 has been received; communication starts when command 9 is written). DOC. - KRIKzz, mcd-verificator ``notes/cdd-cmd-info.txt`` and ``notes/random notes.txt`` (prose of his work on real consoles): the status codes, the report ("toc") types and what each carries in the six info nibbles, the flags nibble (bit 0 = paused or stopped, bit 2 = data), the checksum position, the commands, seek "sets to pause and looped around nearest 8 sectors - 2. example: seek pos 250, loop range 240-247", play = seek + resume, resume without a seek "will start from sector 0x200". DOC (community, hardware findings). - KRIKzz's drive logs (``cd-log/``, "cdd communication logs from real hardware"): the status of each exchange answers the command sent in the one before; a request that arrives before the disc is ready is answered when it is; after power-on the drive reports "0F" (stopped, busy) for about 70 exchanges, then "9F" for about 100 before the first answer; a seek shows busy, reported positions and a play-in before it arrives, and a paused drive's loop shows busy at each jump back (the structure below, built 2026-11-24). The counts are FITTED to the Sonic CD boot log (``sonic-bios/boot-log.txt``); scripts/replay_cdd_log.py measures the model against every log. - The checksum, "~(sum of nibbles 0-8) & $F", fits every line of the logs (FITTED); ares 9408cb4 (``cdd.cpp``, ISC) computes the same (OBSERVED). - How long a seek or a play takes to arrive: FITTED to the 28 seeks and plays in KRIKzz's logs (scripts/cdd_seek_times.py; :func:`seek_exchanges`). Until 2026-11-23 this was ares's curve (OBSERVED), which missed the logs by 12 exchanges on average. """ from __future__ import annotations import math from .disc import AUDIO, PREGAP from .wiring import unplugged #: Sub CPU clocks per exchange: 1/75 s at 12.5 MHz, as a fraction (75 per 12,500,000). EXCHANGE_HZ = 75 SUB_HZ = 12_500_000 # Status codes (KRIKzz, cdd-cmd-info.txt; random notes.txt). DOC. STOPPED, PLAYING, SEEKING, SCANNING, PAUSED, TRAY_OPEN = 0x0, 0x1, 0x2, 0x3, 0x4, 0x5 READY, NO_DISC, LEAD_OUT = 0x9, 0xB, 0xC BUSY = 0xF # in nibble 1: no report this exchange #: Exchanges after the host clock first runs before the drive leaves "0F", then before it #: answers its first report ("9F"): FITTED to KRIKzz's Sonic CD boot log (lines 2-73, #: 74-173). SPIN_UP = 70 TOC_READ = 100 #: Samples of CD-DA in a sector: 2,352 bytes of 16-bit stereo. SAMPLES_PER_SECTOR = 588 #: The disc's geometry, for how many tracks a seek crosses: the program area starts at #: a radius of 25 mm, the track pitch is 1.6 um and the scanning velocity 1.2-1.4 m/s #: (ECMA-130, 2nd ed., the disc's dimensions and recording). DOC; 1.3 m/s, the middle, #: is a HYPOTHESIS (each pressing has its own, and a log gives no way to tell it). PROGRAM_RADIUS = 0.025 TRACK_PITCH = 1.6e-6 SCAN_VELOCITY = 1.3 #: Exchanges from a seek or play command to the status showing the drive there, as #: SEEK_BASE + SEEK_PER_DOUBLING x log2(1 + tracks crossed): FITTED (least squares) to #: the 21 plays in KRIKzz's real-drive logs that start from a known place (0 to 8,244 #: tracks; mean miss 3.2 exchanges, the largest 15). The logs show why it grows as a #: logarithm: the drive jumps, reports where it landed, jumps again about half as far, #: and plays the last ten or so sectors into the target. SEEK_BASE = 13.7 SEEK_PER_DOUBLING = 3.77 #: A seek to pause (command 4) shows "paused" later than a play shows "playing": the 5 #: such seeks in the logs arrive 1 to 33 exchanges after the formula, 16 the middle. #: FITTED, loosely. PAUSE_EXTRA = 16 #: A seek from a drive that is not yet at a place (ready after the TOC, or stopped) #: first reports busy for 12 exchanges (LL-mcd-tst/seek.txt) and arrives 6 to 23 after #: the formula in the 6 such logs. FITTED, loosely. START_EXTRA = 12 # The seek's visible structure (scripts/cdd_seek_times.py --trace prints every case): # # start from a drive not yet at a place, busy ("F") with the flags nibble reading # 1 (no data, not playing) whatever is under the pickup: 12 exchanges in the # 7 LL-mcd-tst logs and seek.txt, 9 to 18 in Sonic CD's boot. FITTED. # jumps a reported position, then one or two busy exchanges while the pickup # jumps again: two in 101 of the 177 gaps in the logs, one in 63, three or # more in 13; the ones mostly near the end (FITTED). Each report is about # half as far from the landing as the one before while it is far, then # steps of about three tracks (the logs: # 9,753 -> 9,927 -> 10,071 -> 10,104 -> 10,137 -> 10,170 in boot-log line 905). # The positions are modelled as halving: a HYPOTHESIS for the shape, the # reported values themselves are not matched. # play-in the pickup lands LAND_SHORT sectors before the target and plays in, one # reported position each exchange. # arrival the new state (paused or playing) is shown SHOWN_EARLY sectors before the # target, in every one of the logs' seeks that ends on a report 0 (33). # # The total from command to arrival stays :func:`seek_exchanges` plus the extras above; # the jumps fill what the start and the play-in leave of it. START_BUSY = START_EXTRA #: Sectors before the target where the pickup lands: 9 to 19 in the 34 cases of the #: logs, 14 the median (kept; 15, the three seeks to pause from rest, replays 7 fewer #: exchanges). FITTED. LAND_SHORT = 14 #: Sectors before the target at which the status first shows the new state. FITTED (33 #: of the 33 seeks and plays in the logs that end on a report 0). SHOWN_EARLY = 3 #: Exchanges from one reported position in the jumps to the next. FITTED (above). JUMP_GROUP = 3 # What a seeking drive shows this exchange. BLIND, HELD, SHOWN = 0, 1, 2 def _radius(lba: int) -> float: """The pickup's radius at ``lba``: at constant linear velocity the area read grows by velocity x pitch each second, 75 sectors (ECMA-130).""" sectors = max(lba + PREGAP, 0) return math.sqrt(PROGRAM_RADIUS**2 + sectors * SCAN_VELOCITY * TRACK_PITCH / (75 * math.pi)) def seek_exchanges(start: int, target: int) -> int: """Exchanges until a play from ``start`` to ``target`` shows "playing" (above).""" tracks = abs(_radius(target) - _radius(start)) / TRACK_PITCH return round(SEEK_BASE + SEEK_PER_DOUBLING * math.log2(1 + tracks)) def _digits(value: int) -> tuple[int, int]: return value // 10 % 10, value % 10 def _msf(frames: int) -> tuple[int, int, int, int, int, int]: """Six BCD nibbles of a time in frames (absolute: LBA + 150).""" frames = max(frames, 0) m, s, f = frames // 4500, (frames // 75) % 60, frames % 75 return (*_digits(m), *_digits(s), *_digits(f)) class CDDA: """The CD-DA output (socket 8): 44.1 kHz stereo, the fader applied. ``gain`` is a labelled assumption (no recording of the Sega CD's mix exists; validation.md).""" rate = 44_100.0 gain = 0.25 def __init__(self) -> None: self.samples: list[tuple[float, float]] = [] self.volume = 1.0 def take_samples(self, mclk: int) -> list[tuple[float, float]]: samples, self.samples = self.samples, [] return samples class CDD: def __init__(self, disc: object | None = None) -> None: self.disc = disc self.request = unplugged # the gate array's, set by the device self.decode = unplugged # the CDC's, set by the device self.status = [0] * 10 self.command = [0] * 10 self.audio = CDDA() self.lid_open = False self.power_on() @property def phase(self) -> str: return "open" if self.lid_open else ("closed" if self.disc else "empty") def power_on(self) -> None: self.host_clock = 0 self.first = False self.clocks = 0 # sub clocks x 75 into the current exchange self.idle_clocks = 0 self.state = STOPPED if self.disc else NO_DISC self.ready_in = SPIN_UP + TOC_READ if self.disc else 0 self.lba = -PREGAP self.target = 0 self.after_seek = PAUSED self.seek_start = 0 # busy exchanges of the start left self.seek_jumps = 0 # exchanges of the jumps left self.seek_held = 0 # busy exchanges left before the next reported position self.seek_play_in = 0 # exchanges of the play-in left self.seek_play_in_from = 0 self.seek_moved = False self.land = 0 self.loop_end = 0 # the last sector of the paused loop self.loop_held = 0 # busy exchanges left in the loop's jump back self.loop_resume: int | None = None # where the jump back lands self.loop_long = True # the next jump back shows busy twice self.seek_view = SHOWN self.report = BUSY self.pending_report: int | None = None self.pending_delay = 0 # busy exchanges before the pending report is shown self.report_track = 0 self.new_command: list[int] | None = None self.music = False self.subcode = [0] * 64 self.subcode_position = 0 def reset_host(self) -> None: """The periphery reset (RES0 written 0) and the forced reset: the exchange stops until HOCK is set again (p.32: "After the Power is ON, initial value is set to '0'"). The drive itself goes on (which units "the periphery" covers the manual does not list, p.22). HYPOTHESIS.""" self.host_clock = 0 self.clocks = 0 # -- the gate array's registers ------------------------------------------------ def control(self) -> int: """$FF8036: D/M at bit 8 (1 for data, "including STOP and PAUSE"), HOCK at bit 2; DTS and DRS read 0, an exchange taking no time here (p.32). DOC (bit positions derived, manual-registers.md).""" return (0 if self.music else 0x100) | self.host_clock << 2 def set_control(self, value: int) -> None: hock = (value >> 2) & 1 if hock and not self.host_clock: # "Changing '0' to '1' starts communication to the CDD" (p.32). DOC. The first # status comes one exchange later: presenting it at once (as ares raises INT4 # here, OBSERVED) makes your own BIOS (US v2.00W) read "0F" at once, call the # drive faulty and reset the periphery for ever. FITTED to that BIOS's boot. self.clocks = 0 self.first = True self.host_clock = hock def set_fader(self, value: int) -> None: """$FF8034 bits 14-4, FD10-FD00 (p.31, docs/manual-registers.md): the decay is "minimum when FD00-10 are %10000000000 and ... maximum when they are %00000000000", and from %00000000100 up it is "-20Log((FD09 FD08...FD03 FD02)/256)", "FD01-FD00 ... not used arithmetically": a gain of FD09-FD02 / 256, and 1 at %10000000000. DOC. Below %100 the formula's FD09-FD02 is 0: silence (derived). FD10 set with other bits the manual does not cover: 1 here (HYPOTHESIS). Not modelled: the change takes "22 us x adjusted depth" (p.31), and DEF1-0, the de-emphasis, "usually OFF".""" fade = (value >> 4) & 0x7FF self.audio.volume = 1.0 if fade & 0x400 else ((fade >> 2) & 0xFF) / 256 def send(self) -> None: """Command 9 written: the command is taken for the next exchange (p.33).""" self.new_command = list(self.command) def status_word(self, index: int) -> int: """$FF8038 + ``index``: status nibbles ``index`` and ``index`` + 1, one a byte (p.33). The drive's side of the host channel (docs/domains.md).""" status = self.status return status[index] << 8 | status[index + 1] def command_word(self, index: int) -> int: """$FF8042 + ``index``: command nibbles ``index`` and ``index`` + 1 as written.""" command = self.command return command[index] << 8 | command[index + 1] def write_command(self, index: int, data: int, upper: bool, lower: bool) -> None: """A write to $FF8042 + ``index``: a nibble a byte; writing nibble 9 sends the command: "When the SUB-CPU writes Transmission Command 9, communication with the CDD is started" (p.33). DOC.""" if upper: self.command[index] = (data >> 8) & 0xF if lower: self.command[index + 1] = data & 0xF if index + 1 == 9: self.send() def subcode_address(self) -> int: """$FF8068: STA6-1 at bits 6-1 (p.55; positions partly derived).""" return (self.subcode_position & 0x3F) << 1 def subcode_word(self, index: int) -> int: return self.subcode[index] # -- the lid ------------------------------------------------------------------- def open_lid(self) -> None: self.lid_open = True self.disc = None self.state = TRAY_OPEN def close_lid(self, disc: object) -> None: self.lid_open = False self.disc = disc self.state = STOPPED self.ready_in = SPIN_UP + TOC_READ self.lba = -PREGAP # -- time ------------------------------------------------------------------------ def run(self, clocks: int) -> None: if not self.host_clock: # The drive spins up whether or not the host is talking to it: it is powered # with the console. HYPOTHESIS. if self.ready_in: self.idle_clocks += clocks * EXCHANGE_HZ while self.idle_clocks >= SUB_HZ and self.ready_in: self.idle_clocks -= SUB_HZ self._advance() return self.clocks += clocks * EXCHANGE_HZ while self.clocks >= SUB_HZ: self.clocks -= SUB_HZ self.block() def block(self) -> None: """One exchange: the drive moves a sector, takes the command sent in the last exchange, and presents its status (INT4).""" self._advance() command, self.new_command = self.new_command, None if command is not None: self._execute(command) self._present() # -- the drive --------------------------------------------------------------------- def _tracks(self) -> list[object]: return self.disc.tracks if self.disc is not None else [] # type: ignore[attr-defined] def _track_index(self, lba: int) -> int: tracks = self._tracks() for i, track in enumerate(tracks): if lba < track.end: # type: ignore[attr-defined] return i return len(tracks) def _is_data(self, lba: int) -> bool: tracks = self._tracks() i = self._track_index(lba) return i < len(tracks) and tracks[i].kind != AUDIO # type: ignore[attr-defined] def _advance(self) -> None: if self.ready_in: self.ready_in -= 1 if self.ready_in <= TOC_READ and self.state == STOPPED: self.state = READY # spun up: "9F" while the TOC is read return state = self.state if state == SEEKING: self._seek_step() elif state == PAUSED: # The loop: the 8 sectors up to ``loop_end``, then a jump back that shows # busy for one exchange or two, alternately, so the loop takes 9 exchanges # and shows 8 sectors or 7 (seek.txt lines 233-300, read-pause-resume.txt # lines 302-348). FITTED. KRIKzz's prose, "seek pos 250, loop range # 240-247", agrees (DOC); the end is the arrival, 3 before the target, or # one past the sector shown when a play is paused. self.seek_view = SHOWN if self.loop_resume is not None: if self.loop_held: self.loop_held -= 1 self.seek_view = HELD return self.lba, self.loop_resume = self.loop_resume, None elif self.loop_end - 8 < self.lba < self.loop_end: self.lba += 1 else: held = 2 if self.loop_long else 1 self.loop_long = not self.loop_long self.loop_held = held - 1 self.loop_resume = self.loop_end - 8 + held self.seek_view = HELD return # The pickup goes on reading while it loops, so data blocks still reach the # decoder (the BIOS watches their headers while paused before it plays). # HYPOTHESIS, fitted to your own BIOS's boot. disc = self.disc if disc is not None and self._is_data(self.lba): self.decode(self.lba, self._data_sector(self.lba)) elif state in (PLAYING, SCANNING): self._play_sector() def _play_sector(self) -> None: disc = self.disc if disc is None: return if self.lba >= disc.end: # type: ignore[attr-defined] self.state = LEAD_OUT return sector = disc.read_sector(self.lba) # type: ignore[attr-defined] self._subcode() if self._is_data(self.lba): self.music = False self.decode(self.lba, self._data_sector(self.lba, sector)) else: self.music = True audio = self.audio volume = audio.volume out = audio.samples for i in range(0, 2352, 4): left = int.from_bytes(sector[i : i + 2], "little", signed=True) right = int.from_bytes(sector[i + 2 : i + 4], "little", signed=True) out.append((left * volume, right * volume)) self.lba += 1 def _data_sector(self, lba: int, sector: bytes | None = None) -> bytes: """A data block as the drive delivers it. Where the image has none (the first track's two-second pregap, which a BIN does not hold) the block is built: the 12-byte sync, the header (absolute time in BCD, mode 1) and zero data, as a Mode 1 pregap is recorded (ECMA-130 ยง14). DOC.""" if sector is None: sector = self.disc.read_sector(lba) # type: ignore[attr-defined] if any(sector[:16]): return sector frames = lba + PREGAP m, sec, f = frames // 4500, (frames // 75) % 60, frames % 75 bcd = [((v // 10) << 4) | (v % 10) for v in (m, sec, f)] head = b"\x00" + b"\xff" * 10 + b"\x00" + bytes(bcd) + b"\x01" return head + bytes(2352 - 16) def _subcode(self) -> None: """A sector's 98 subcode bytes go to the buffer at $FF8100 and INT6 follows "When 98 byte buffering is completed" (p.30). DOC. A BIN/CUE image holds no subcode, so the buffer is written with zeros and $FF8068's address moves on by the sector's 48 words: HYPOTHESIS (ares advances it by 49; OBSERVED).""" position = self.subcode_position for i in range(48): self.subcode[(position + i) & 0x3F] = 0 self.subcode_position = (position + 48) & 0x3F self.request(6) def _seek(self, lba: int, then: int) -> None: # The two extras do not add up: a seek to pause from a drive not yet at a place # arrives as late as the larger (the 3 such seeks in the logs: 32, 46, 53). extra = 0 if then == PAUSED: extra = PAUSE_EXTRA if self.state in (READY, STOPPED): extra = max(extra, START_EXTRA) exchanges = seek_exchanges(self.lba, lba) + extra # The exchange that takes the command is the seek's first (the hardware's count # in scripts/cdd_seek_times.py starts there): its status already shows "seeking". # The last is the arrival; at least one jump comes before it. A total too short # for the whole structure shortens the start, then the play-in. total = max(exchanges, 2) start = min(START_BUSY if self.state in (READY, STOPPED) else 0, total - 2) play_in = min(LAND_SHORT - SHOWN_EARLY, total - 2 - start) jumps = total - 1 - start - play_in self.seek_start = start self.seek_jumps = jumps self.seek_held = 0 self.seek_play_in = self.seek_play_in_from = play_in self.seek_moved = False self.seek_view = BLIND if start else SHOWN self.land = lba - SHOWN_EARLY - play_in self.target = lba self.after_seek = then self.state = SEEKING # Not modelled: a seek from rest shows the absolute time (report 0) unasked # (seek.txt; KRIKzz's notes: seek "ressets toc to 0 if runs from stop state", # DOC). Built on this structure it lowers the replay (2,997 -> 2,895, from rest # only or after every seek alike): while the model's arrivals miss the logs' # (seek.txt: 47 exchanges against 32), shown positions land where the hardware # is busy. To revisit when the arrivals do (docs/worklog.md, 2026-11-24). self._seek_step() def _seek_step(self) -> None: """One exchange of a seek (the structure above): what it shows goes in ``seek_view``, the pickup's place in ``lba``.""" if self.seek_start: self.seek_start -= 1 self.seek_view = BLIND elif self.seek_jumps: left = self.seek_jumps self.seek_jumps -= 1 if self.seek_held: self.seek_held -= 1 self.seek_view = HELD else: # The groups of JUMP_GROUP exchanges, the last one or two of two (and a # single report when one exchange is left), as the logs end them. group = 2 if left in (2, 4) else (1 if left == 1 else JUMP_GROUP) self.seek_held = group - 1 if self.seek_view == SHOWN and not self.seek_moved: # From a place, the first report is the next sector: the pickup is # still reading there (every such seek in the logs). FITTED. self.lba += 1 else: self.lba = self.land + (self.lba - self.land) // 2 self.seek_moved = True self.seek_view = SHOWN elif self.seek_play_in: # The play-in: lands, then a reported position every exchange. self.lba = self.land if self.seek_play_in == self.seek_play_in_from else self.lba + 1 self.seek_play_in -= 1 self.seek_view = SHOWN else: self.lba = self.loop_end = self.target - SHOWN_EARLY self.loop_resume, self.loop_long = None, True self.seek_view = SHOWN self.state = self.after_seek self.music = self.after_seek == PLAYING and not self._is_data(self.lba) def _execute(self, command: list[int]) -> None: if (~sum(command[:9]) & 0xF) != command[9]: return # a bad checksum: the logs (LL-bad-crc.txt) are the judge; ignored here code = command[0] if self.state in (NO_DISC, TRAY_OPEN) and code not in (0xC, 0xD): return if code == 0x0: return if code == 0x1: # Stop shows state 0 afterwards, not 9 (Sonic CD's boot-log.txt line 318: "0F" # for 13 exchanges, then "04..." when the BIOS asks; the 13 busy exchanges # are not modelled). FITTED: replay 3,038 -> 3,054 of 11,256. self.state = STOPPED self.music = False self.report = BUSY return if code == 0x2: self.pending_report = command[3] self.report_track = command[4] * 10 + command[5] # A track's start (report 5) asked in state 9 is answered after two busy # exchanges: 112 of the 121 such requests in KRIKzz's logs are answered in # the third exchange after the command. FITTED: replay 3,004 -> 3,038. Not # modelled: report 5 asked while playing or paused stops the drive (state 9) # and answers after about 20 (boot-log.txt line 251; KRIKzz's notes: # "this one results stop playing", DOC). self.pending_delay = 2 if command[3] == 5 and self.state == READY else 0 return if code in (0x3, 0x4): minute = command[2] * 10 + command[3] second = command[4] * 10 + command[5] frame = command[6] * 10 + command[7] lba = (minute * 60 + second) * 75 + frame - PREGAP self._seek(lba, PLAYING if code == 0x3 else PAUSED) return if code == 0x6: if self.state == PLAYING: # The loop ends one past the sector this exchange shows and the first # jump back shows busy once (read-pause-resume.txt lines 301-305). # FITTED (one log). self.target = self.lba self.loop_end, self.loop_long = self.lba + 1, False elif self.state != PAUSED: self.loop_end, self.loop_long = self.target - SHOWN_EARLY, True self.loop_resume = None self.seek_view = SHOWN self.state = PAUSED return if code == 0x7: if self.state in (READY, STOPPED): self.lba = 0x200 self.state = PLAYING return if code in (0x8, 0x9): self.state = SCANNING return if code == 0xD: self.open_lid() elif code == 0xC and self.lid_open: self.lid_open = False self.state = NO_DISC def _present(self) -> None: """The status of this exchange, and INT4. A request is answered in the first exchange the drive is ready for it; reports 0-2 (the positions and the track) go on being shown while the host sends no other, reports 3-5 are shown once and then "F" (busy): FITTED to KRIKzz's logs (sonic-bios/boot-log.txt lines 174-237).""" status = self.status if self.first: # The first exchange after HOCK is set reads "00.000000.0F": KRIKzz's log of # a real drive (BI-mcd-tst/bios-start.txt, line 1). FITTED (one log); your own # BIOS (US v2.00W) checks for exactly this and resets the periphery otherwise. self.first = False status[:] = [0] * 9 + [0xF] self.request(4) return state = self.state not_ready = bool(self.ready_in) or state in (NO_DISC, TRAY_OPEN) hidden = state in (SEEKING, PAUSED) and self.seek_view != SHOWN if not_ready or hidden: # A request that comes while the seek hides the position waits for the # next reported one (Sonic CD's boot: each of the BIOS's requests during a # seek is answered at the next report, boot-log.txt lines 190-219). FITTED. report = BUSY elif self.pending_report is not None and self.pending_delay: self.pending_delay -= 1 report = BUSY elif self.pending_report is not None: report = self.pending_report self.pending_report = None self.report = report if report <= 2 else BUSY else: report = self.report info = self._info(report) if report != BUSY else (BUSY, 0, 0, 0, 0, 0, 0) status[0] = state status[1:8] = info if report == 5: status[8] = self.report_track % 10 else: # The flags nibble reads 1 (no data) during a seek's start and while stopped # ("0F.000000.1F", boot-log.txt line 319). FITTED. blind = state == SEEKING and self.seek_view == BLIND or state == STOPPED data = not not_ready and not blind and self._is_data(self.lba) status[8] = (4 if data else 0) | (0 if state == PLAYING else 1) status[9] = ~sum(status[:9]) & 0xF self.request(4) def _info(self, report: int) -> tuple[int, ...]: tracks = self._tracks() lba = self.lba if report == 0: return (0, *_msf(lba + PREGAP)) if report == 1: i = self._track_index(lba) start = tracks[i].start if i < len(tracks) else lba # type: ignore[attr-defined] return (1, *_msf(abs(lba - start))) if report == 2: i = self._track_index(lba) number = tracks[i].number if i < len(tracks) else 0xAA # type: ignore[attr-defined] flags = (4 if self._is_data(lba) else 0) | (0 if self.state == PLAYING else 1) return (2, *_digits(number), 0, 1, flags, 0) if report == 3: return (3, *_msf(self.disc.end + PREGAP)) # type: ignore[attr-defined] if report == 4: first = tracks[0].number if tracks else 0 # type: ignore[attr-defined] last = tracks[-1].number if tracks else 0 # type: ignore[attr-defined] return (4, *_digits(first), *_digits(last), 0, 0) if report == 5: number = self.report_track for track in tracks: if track.number == number: # type: ignore[attr-defined] digits = list(_msf(track.start + PREGAP)) # type: ignore[attr-defined] if track.kind != AUDIO: # type: ignore[attr-defined] digits[4] |= 8 # "bit 7 in frame value is type of track" (KRIKzz) return (5, *digits) return (5, 0, 0, 0, 0, 0, 0) return (report & 0xF, 0, 0, 0, 0, 0, 0) __all__ = ["CDD", "CDDA"]