# SPDX-License-Identifier: MIT """Milestone 1 unit tests: each expectation is a statement of Sega's Mega-CD Hardware Manual (1991, docs/manual-registers.md, cited p.N) unless it says otherwise.""" from __future__ import annotations import pytest from segacd_python.gatearray import TICK from segacd_python.megacd import MegaCD from segacd_python.memory import WordRAM, cell_to_dot def device() -> MegaCD: cd = MegaCD(bytes(0x20000)) cd.reset(0, 0) return cd def main_word(cd: MegaCD, address: int) -> int: return cd.read_word(address, 0) def test_power_on_values() -> None: """p.21: $A12000 = $0002, $A12002 = $0001, $FF8002 = $0001, $A12006 = $FFFF.""" cd = device() assert main_word(cd, 0xA12000) == 0x0002 assert main_word(cd, 0xA12002) == 0x0001 assert cd.sub_read_word(0xFF8002) == 0x0001 assert main_word(cd, 0xA12006) == 0xFFFF def test_hint_vector_low_word() -> None: """p.58: the level-4 vector's low word is $A12006; the high word reads $FFFF (mcd-verificator's REG 2006 row, TEST ROM).""" bios = bytearray(0x20000) bios[0x70:0x74] = b"\x12\x34\x56\x78" cd = MegaCD(bytes(bios)) cd.reset(0, 0) cd.write_word(0xA12006, 0xABCD, 0) assert main_word(cd, 0x70) == 0xFFFF assert main_word(cd, 0x72) == 0xABCD def test_communication_directions() -> None: """p.28-29, p.59-60: CFM and the command words are the main CPU's, CFS and the status words the sub CPU's; each side reads both.""" cd = device() cd.write_byte(0xA1200E, 0x5A, 0, 0) cd.sub_write_byte(0xFF800F, 0xA5) assert main_word(cd, 0xA1200E) == 0x5AA5 assert cd.sub_read_word(0xFF800E) == 0x5AA5 cd.write_word(0xA12010, 0x1234, 0) cd.sub_write_word(0xFF8010, 0xFFFF) # read-only on the sub side assert cd.sub_read_word(0xFF8010) == 0x1234 cd.sub_write_word(0xFF8020, 0x4321) cd.write_word(0xA12020, 0, 0) # read-only on the main side assert main_word(cd, 0xA12020) == 0x4321 def test_prg_ram_window_needs_the_bus() -> None: """p.57: the main CPU reaches PRG RAM at $020000 by BK1-0 "When SBRQ=1 or SRES=0".""" cd = device() cd.write_word(0xA12002, 0x0080, 0) # BK = 2 cd.write_word(0x020000, 0xBEEF, 0) assert cd.prg[0x40000:0x40002] == b"\xbe\xef" def test_write_protect_units() -> None: """p.14, p.57: WP protects the sub CPU's PRG RAM in $200-byte units.""" cd = device() cd.write_byte(0xA12002, 0x02, 0, 0) # WP = 2: $0000-$03FF cd.sub_write_word(0x3FE, 0x1111) cd.sub_write_word(0x400, 0x2222) assert cd.sub_read_word(0x3FE) == 0 assert cd.sub_read_word(0x400) == 0x2222 def test_2m_hand_over() -> None: """p.23-25: main DMNA=1 gives the 2M RAM to the sub CPU (DMNA reads 1, RET 0); the sub CPU's RET=1 gives it back (RET 1, DMNA 0).""" cd = device() cd.write_word(0x200000, 0x1234, 0) cd.write_byte(0xA12003, 0x02, 0, 0) assert main_word(cd, 0xA12002) & 3 == 0b10 assert cd.sub_read_word(0x80000) == 0x1234 cd.sub_write_word(0x80002, 0x5678) cd.sub_write_byte(0xFF8003, 0x01) assert main_word(cd, 0xA12002) & 3 == 0b01 assert main_word(cd, 0x200002) == 0x5678 def test_2m_interleave_and_1m_banks() -> None: """p.11: in 2M address 0 is WORD-RAM 0's word 0 and address 2 WORD-RAM 1's word 0; p.24: in 1M RET=0 gives the main CPU WORD-RAM 0 and the sub CPU WORD-RAM 1.""" cd = device() cd.write_word(0x200000, 0xAAAA, 0) # bank 0, word 0 cd.write_word(0x200002, 0xBBBB, 0) # bank 1, word 0 cd.write_word(0x200004, 0xCCCC, 0) # bank 0, word 1 cd.sub_write_byte(0xFF8003, 0x04) # MODE = 1M, RET = 0 assert main_word(cd, 0xA12002) & 4 assert main_word(cd, 0x200000) == 0xAAAA assert main_word(cd, 0x200002) == 0xCCCC assert cd.sub_read_word(0xC0000) == 0xBBBB def test_dot_image_and_priority() -> None: """p.54: "$0C0000 contains the original data '0A', and $080000 contains the write data": $0507 gives $57 normally, $57 overwrite, $5A underwrite; $0B00 gives $B0, $BA, $BA.""" for priority, first, second in ((0, 0x57, 0xB0), (2, 0x57, 0xBA), (1, 0x5A, 0xBA)): for data, expected in ((0x0507, first), (0x0B00, second)): cd = device() cd.sub_write_byte(0xFF8003, 0x04 | priority << 3) # 1M, sub has bank 1 cd.sub_write_byte(0xC0000, 0x0A) cd.sub_write_word(0x80000, data) assert cd.sub_read_byte(0xC0000) == expected, (priority, hex(data)) def test_cell_image_address_table() -> None: """p.16: dots 8,9 at dot map $004 are at cell map $400; dots 512,513 ($100) at $004.""" for dot, cell in ((0x000, 0x000), (0x003, 0x003), (0x004, 0x400), (0x100, 0x004), (0x104, 0x404), (0x106, 0x406)): # fmt: skip assert cell_to_dot(0x20000 + cell) == dot def test_cell_image_regions() -> None: """p.17: the V-128 region's cell image at $230000 shows the dot map at $210000.""" assert cell_to_dot(0x30000) == 0x10000 assert cell_to_dot(0x30000 + 0x200) == 0x10004 # a column is 128 x 4 bytes def test_word_ram_bytes() -> None: ram = WordRAM() ram.write_word_1m(2, 1, 0x1234) assert ram.data[6:8] == b"\x12\x34" # bank 1 word 1 = physical word 3 def test_stopwatch_and_timer() -> None: """p.28: the stopwatch counts every 30.72 us and a sub write clears it; p.30: the timer gives INT3 every (n+1) counts.""" cd = device() cd.sub_write_word(0xFF8032, 0x0008) # IEN3 cd.sub_write_word(0xFF8030, 3) cd.gate.run(TICK * 3) assert cd.gate.ipl() == 0 cd.gate.run(TICK) assert cd.gate.ipl() == 3 assert cd.sub_read_word(0xFF800C) == 4 cd.sub_write_word(0xFF800C, 0) assert main_word(cd, 0xA1200C) == 0 assert cd.sub_read_word(0xFF8030) == 3 # the written value, not the count def test_level_2_from_the_main_cpu() -> None: """p.56: IFL2 written 1 generates INT level 2 when IEN2=1; IEN2 reads at bit 15.""" cd = device() cd.write_byte(0xA12000, 0x01, 0, 0) assert cd.gate.ipl() == 0 cd.sub_write_word(0xFF8032, 0x0004) assert main_word(cd, 0xA12000) & 0x8000 cd.write_byte(0xA12000, 0x01, 0, 0) assert cd.gate.ipl() == 2 def test_font_converter() -> None: """p.34: "When SC10-SC13=F and SC00-SC03=1, If SB0=0, Then FD00-FD03=1, If SB0=1, Then FD00-FD03=F"; SBF lands in bits 15-12 of $FF8050.""" cd = device() cd.sub_write_word(0xFF804C, 0x00F1) cd.sub_write_word(0xFF804E, 0x8001) assert cd.sub_read_word(0xFF8050) == 0xF111 assert cd.sub_read_word(0xFF8056) == 0x111F @pytest.mark.parametrize("bits", [0b00, 0b01, 0b10, 0b11]) def test_sres_holds_sbrq(bits: int) -> None: """p.21's power-on $0002 and p.56: with SRES=0 the bus request reads set.""" cd = device() cd.write_byte(0xA12001, bits, 0, 0) value = main_word(cd, 0xA12000) & 3 assert value == (bits if bits & 1 else 0b10) def test_level_2_reaches_the_sub_cpu_late() -> None: """IFL2's request reaches the sub CPU's IPL pins IFL2_LATENCY sub clocks after the main CPU's write (FITTED to mcd-verificator's IRQ TEST row, TEST ROM); it reads as requested at once.""" from segacd_python.gatearray import IFL2_LATENCY cd = device() cd.sub_write_word(0xFF8032, 0x0004) cd.write_byte(0xA12000, 0x01, 0, 0) now = cd.gate.now assert main_word(cd, 0xA12000) & 0x0100 assert cd.gate.ipl(now + IFL2_LATENCY - 1) == 0 assert cd.gate.ipl(now + IFL2_LATENCY) == 2 def test_sub_cpu_hangs_on_main_word_ram_until_given() -> None: """p.24: a sub-CPU access to 2M word RAM the main CPU owns hangs it; the held write completes when the RAM is given to the sub CPU (mcd-verificator's WORD RAM row, errors 20 and 21, TEST ROM).""" cd = device() cd.sub_write_word(0x080000, 0x1234) assert cd.sub_hung assert cd.wram.read_word_2m(0) == 0 cd.write_word(0xA12002, 0x0002, 0) # DMNA=1: the RAM to the sub CPU cd.sub_running = True cd.run_sub(cd.sub_clock + 1) assert not cd.sub_hung assert cd.wram.read_word_2m(0) == 0x1234 def test_forced_reset_ends_the_hang() -> None: cd = device() cd.sub_read_word(0x0C0000) assert cd.sub_hung cd.write_byte(0xA12001, 0x01, 0, 0) # SRES=1 cd.write_byte(0xA12001, 0x00, 0, 0) # SRES=0: forced reset assert not cd.sub_hung