# SPDX-License-Identifier: MIT """Milestone 4 unit tests: the LC8951's registers (its design manual, Table 7 and §11) and the drive's exchange (KRIKzz's notes and logs).""" from __future__ import annotations from segacd_python.cdc import CDC from segacd_python.cdd import CDD, READY, SPIN_UP, TOC_READ from segacd_python.disc import AUDIO, MODE1, Track def test_ifstat_after_reset() -> None: """IFSTAT is active low with bit 4 "always returns a 1" (§11.3.1): idle = $FF.""" cdc = CDC() cdc.address = 1 assert cdc.read_register() == 0xFF assert cdc.address == 2 # AR increments after an access (§10) def test_five_bit_address_and_empty_registers() -> None: """KRIKzz (forum t=3166): AR with RS is five bits; registers 16-31 read $FF.""" cdc = CDC() cdc.address = 31 assert cdc.read_register() == 0xFF assert cdc.address == 0 def test_host_transfer_counts_bytes() -> None: """DBC counts bytes; a host transfer of DBC+1 bytes ends with EDT and DTEI.""" cdc = CDC() cdc.ram[0:4] = b"\x12\x34\x56\x78" cdc.set_destination(3) # sub CPU read cdc.address = 1 cdc.write_register(0x02) # IFCTRL: DOUTEN cdc.write_register(3) # DBCL: 4 bytes cdc.write_register(0) # DBCH cdc.write_register(0) # DACL cdc.write_register(0) # DACH cdc.write_register(0) # DTTRG assert cdc.dsr == 1 assert cdc.host_read(main=False) == 0x1234 assert cdc.host_read(main=False) == 0x5678 assert cdc.edt == 1 assert cdc.dtei == 1 class _Disc: def __init__(self) -> None: self.tracks = [Track(1, MODE1, 0, 0, 1000), Track(2, AUDIO, 1150, 1000, 5000)] self.end = 5000 def read_sector(self, lba: int) -> bytes: return bytes(2352) def test_drive_answers_once_ready() -> None: """KRIKzz's boot log: "0F" while spinning up, then "9F", then the answer to a request (report 4: first and last track); the checksum is ~sum of nibbles 0-8.""" drive = CDD(_Disc()) drive.command[:] = [2, 0, 0, 4, 0, 0, 0, 0, 0, 0] drive.command[9] = ~sum(drive.command[:9]) & 0xF drive.send() drive.block() assert drive.status[:2] == [0, 0xF] for _ in range(SPIN_UP): drive.block() assert drive.status[:2] == [READY, 0xF] for _ in range(TOC_READ - 1): drive.block() assert drive.status[:8] == [READY, 4, 0, 1, 0, 2, 0, 0] assert drive.status[9] == ~sum(drive.status[:9]) & 0xF drive.block() assert drive.status[1] == 0xF # shown once (the boot log, lines 174-176) def test_host_transfer_ends_when_last_word_is_latched() -> None: """EDT, "all the data has been transferred from the CDC" (p.26), and DTEI come when the last word reaches the gate array's host register, before the CPU reads it (mcd-verificator's CDC FLAGS row, ERROR 22 otherwise; TEST ROM).""" cdc = CDC() cdc.ram[0:4] = b"\x12\x34\x56\x78" cdc.set_destination(3) cdc.address = 1 for value in (0x02, 3, 0, 0, 0, 0): cdc.write_register(value) assert cdc.host_read(main=False) == 0x1234 assert cdc.edt == 1 and cdc.dtei == 1 and cdc.dsr == 1 assert cdc.host_read(main=False) == 0x5678 assert cdc.dsr == 0 def test_odd_byte_count_moves_whole_words() -> None: """A transfer of DBC+1 = 3 bytes moves one whole word; the odd byte stays behind (mcd-verificator's CDC DMA2 row, TEST ROM). The other CPU's register shows the latched word without taking it (CDC DMA3, TEST ROM).""" cdc = CDC() cdc.ram[0:4] = b"\x12\x34\x56\x78" cdc.set_destination(3) cdc.address = 1 for value in (0x02, 2, 0, 0, 0, 0): cdc.write_register(value) assert cdc.host_read(main=True) == 0x1234 # the main CPU peeks assert cdc.host_read(main=False) == 0x1234 assert cdc.dsr == 0 and cdc.edt == 1 def test_the_fader_follows_the_manuals_formula() -> None: """p.31: gain FD09-FD02 / 256 (FD01-FD00 unused), full at %10000000000, silent at 0.""" drive = CDD() drive.set_fader(0x400 << 4) assert drive.audio.volume == 1.0 drive.set_fader(0x200 << 4) assert drive.audio.volume == 0.5 drive.set_fader((0x200 | 3) << 4) # the two low bits change nothing assert drive.audio.volume == 0.5 drive.set_fader(3 << 4) assert drive.audio.volume == 0.0 def test_the_decoder_interrupt_is_a_pulse() -> None: """LC8951 design manual §12.6 and appendix E (p.63, p.108): with none of WRRQ, PRQ, QRQ set the INT pin is low "About 2750" us after a block; INT is a level, so a block that comes while an unacknowledged DTEI holds it brings no new request (CDC FLAGS, TEST ROM).""" cdc = CDC() requests = [] cdc.request = requests.append cdc.address = 1 cdc.write_register(0x60) # IFCTRL: DTEIEN, DECIEN cdc.address = 10 cdc.write_register(0x80) # CTRL0: DECEN cdc.decode(0, bytes(2352)) assert requests == [5] and cdc.deci == 1 cdc.run(2750 * 25 // 2 - 1) assert cdc.deci == 1 cdc.run(1) assert cdc.deci == 0 and not cdc.int_active cdc.decode(1, bytes(2352)) assert requests == [5, 5] # the pulse ended, so a new edge cdc.dtei = 1 cdc.run(2750 * 25 // 2) # this pulse ends with DTEI holding the pin cdc.decode(2, bytes(2352)) assert requests == [5, 5] def _send(drive: CDD, command: list[int]) -> None: drive.command[:] = command + [0] drive.command[9] = ~sum(command) & 0xF drive.send() def test_a_seek_from_rest_shows_its_structure() -> None: """KRIKzz's seek.txt (a real drive): a seek to 00:02:50 from rest shows busy with the flags nibble 1 for 12 exchanges, then reported positions with busy between, the play-in one sector an exchange, "paused" 3 sectors before the target, then the loop of 8 sectors with one or two busy exchanges at each jump back.""" drive = CDD(_Disc()) for _ in range(SPIN_UP + TOC_READ + 5): drive.block() _send(drive, [2, 0, 0, 0, 0, 0, 0, 0, 0]) # ask for the absolute time drive.block() _send(drive, [4, 0, 0, 0, 0, 2, 5, 0, 0]) # seek to 00:02:50, LBA 50 seen = [] for _ in range(140): drive.block() seen.append(list(drive.status)) assert all(s[0] == 2 and s[1] == 0xF and s[8] == 1 for s in seen[:12]) assert seen[12][1] != 0xF # the first position after the start arrival = next(i for i, s in enumerate(seen) if s[0] == 4) assert seen[arrival][1:8] == [0, 0, 0, 0, 2, 4, 7] # 00:02:47 assert seen[arrival - 1][1:8] == [0, 0, 0, 0, 2, 4, 6] # played in loop = seen[arrival + 1 : arrival + 19] assert [s[1] for s in loop[:3]] == [0xF, 0xF, 0] assert loop[2][6:8] == [4, 1] # back to 00:02:41 assert [s[1] == 0xF for s in loop].count(True) == 3 # 2 + 1 in two loops