"""The SF-7000: its 8255, ROM switch, drive and uPD765 (src/sms_python/sf7000.py).""" from __future__ import annotations import pytest from sms_python.board import SF7000, Board from sms_python.cartridge import make_cartridge from sms_python.sf7000 import ( CPU_CLOCK, DISK_SIZE, INDEX_PULSE, REVOLUTION, SECTOR_SIZE, SECTORS, ) IPL = bytes(range(256)) * 32 # 8 KB whose bytes are their addresses' low bytes def blank_disk() -> bytearray: """A disk whose every byte names its track and sector: track * 16 + sector - 1.""" disk = bytearray(DISK_SIZE) for index in range(DISK_SIZE // SECTOR_SIZE): disk[index * SECTOR_SIZE : (index + 1) * SECTOR_SIZE] = bytes([index & 0xFF]) * SECTOR_SIZE return disk def machine(disk: bytearray | None = None) -> Board: """An SF-7000 as the IPL leaves it: mode $90, drive in use, motor on.""" board = Board(SF7000, None, make_cartridge(IPL, "rom"), audio=False, disk=disk) board.write_port(0xE7, 0x90) # port A in, B and C out board.write_port(0xE6, 0x0B) # RESET to the controller, motor off board.write_port(0xE6, 0x00) # reset released, /INUSE and /MOTOR ON low return board def wait(board: Board, condition, limit: int = 4 * REVOLUTION) -> int: """Run time forward until ``condition()``; the cycles it took.""" start = board.cycles while not condition(): board.cycles += 8 assert board.cycles - start < limit, "timed out" return board.cycles - start def command(board: Board, *values: int) -> None: for value in values: assert board.read_port(0xE0) & 0xC0 == 0x80 # RQM, processor to controller board.write_port(0xE1, value) def result(board: Board) -> list[int]: values = [] while board.read_port(0xE0) & 0xC0 == 0xC0: values.append(board.read_port(0xE1)) return values def pulse_tc(board: Board) -> None: board.write_port(0xE7, 0x05) # set PC2, as the IPL does ($1155) board.write_port(0xE7, 0x04) def read_sector(board: Board, track: int, sector: int, last: int = SECTORS) -> list[int]: command(board, 0x46, 0x00, track, 0x00, sector, 0x01, last, 0x0E, 0xFF) data = [] for _ in range(SECTOR_SIZE): wait(board, lambda: board.read_port(0xE0) & 0x80) assert board.read_port(0xE0) & 0xF0 == 0xF0 # RQM, to processor, executing, busy assert board.read_port(0xE4) & 1 # INT asks for each byte data.append(board.read_port(0xE1)) return data def test_rom_until_switched_out_and_ram_everywhere(): board = Board(SF7000, None, make_cartridge(IPL, "rom"), audio=False) # Port C is an input after reset and /ROM SEL reads active: the IPL is in. assert [board.read_byte(a) for a in (0x0001, 0x1FFF, 0x2001, 0x3FFF)] == [1, 0xFF, 1, 0xFF] board.write_byte(0x0001, 0xAA) # into the RAM beneath board.write_byte(0x4000, 0x55) board.write_byte(0xFFFF, 0x66) assert (board.read_byte(0x0001), board.read_byte(0x4000), board.read_byte(0xFFFF)) == ( 1, 0x55, 0x66, ) # fmt: skip board.write_port(0xE7, 0x90) # the mode word clears port C: still the ROM assert board.read_byte(0x0001) == 1 board.write_port(0xE7, 0x0D) # set PC6 (IPL $01BD) assert board.read_byte(0x0001) == 0xAA board.write_port(0xE7, 0x0C) assert board.read_byte(0x0001) == 1 def test_needs_its_ipl_and_a_whole_disk(): with pytest.raises(ValueError, match="IPL"): Board(SF7000, None, None) with pytest.raises(ValueError, match="163840"): Board(SF7000, None, make_cartridge(IPL, "rom"), disk=bytearray(1000)) def test_index_pulse_once_a_turn_with_the_motor_on(): board = machine(blank_disk()) assert board.read_port(0xE4) & 4 # the pulse begins as the motor starts high = wait(board, lambda: not board.read_port(0xE4) & 4) low = wait(board, lambda: board.read_port(0xE4) & 4) assert abs(high - INDEX_PULSE) <= 8 assert abs(high + low - REVOLUTION) <= 8 # 200 ms: the IPL accepts 180 to 220 board.write_port(0xE6, 0x02) # motor off board.cycles += REVOLUTION - INDEX_PULSE // 2 assert not board.read_port(0xE4) & 4 assert not machine(None).read_port(0xE4) & 4 # no disk, no hole def test_seek_takes_the_step_rate_and_interrupts(): board = machine(blank_disk()) command(board, 0x03, 0x60, 0x0B) # SPECIFY as the IPL's: 20 ms a step, non-DMA assert board.read_port(0xE0) == 0x80 command(board, 0x08) # SENSE INTERRUPT STATUS with nothing to report assert result(board) == [0x80] command(board, 0x0F, 0x00, 10) assert board.read_port(0xE0) == 0x81 # ready for a command, drive 0 seeking assert not board.read_port(0xE4) & 1 took = wait(board, lambda: board.read_port(0xE4) & 1) assert abs(took - 10 * 20 * CPU_CLOCK // 1000) <= 8 command(board, 0x08) assert result(board) == [0x20, 10] # seek end, cylinder 10 assert not board.read_port(0xE4) & 1 command(board, 0x04, 0x00) # SENSE DRIVE STATUS: ready, not on track 0 assert result(board) == [0x20] command(board, 0x07, 0x00) # RECALIBRATE wait(board, lambda: board.read_port(0xE4) & 1) command(board, 0x08) assert result(board) == [0x20, 0] command(board, 0x04, 0x00) assert result(board) == [0x30] def test_read_one_sector_then_terminal_count(): board = machine(blank_disk()) assert read_sector(board, 0, 3) == [2] * SECTOR_SIZE pulse_tc(board) assert board.read_port(0xE4) & 1 # INT for the result assert result(board) == [0x00, 0x00, 0x00, 0, 0, 4, 1] # the next sector assert not board.read_port(0xE4) & 1 assert board.read_port(0xE0) == 0x80 def test_bytes_come_at_the_disk_speed(): board = machine(blank_disk()) command(board, 0x46, 0x00, 0, 0x00, 1, 0x01, SECTORS, 0x0E, 0xFF) wait(board, lambda: board.read_port(0xE0) & 0x80) start = board.cycles for _ in range(SECTOR_SIZE): wait(board, lambda: board.read_port(0xE0) & 0x80) board.read_port(0xE1) # 255 byte times of 32 us: 250 kbit/s. assert abs(board.cycles - start - 255 * 32 * CPU_CLOCK // 1_000_000) <= 16 def test_reading_runs_on_to_the_end_of_the_track_without_terminal_count(): board = machine(blank_disk()) assert read_sector(board, 0, 15) == [14] * SECTOR_SIZE for _ in range(SECTOR_SIZE): # sector 16 follows by itself wait(board, lambda: board.read_port(0xE0) & 0x80) assert board.read_port(0xE1) == 15 wait(board, lambda: board.read_port(0xE0) & 0xC0 == 0xC0) assert result(board) == [0x40, 0x80, 0x00, 0, 0, 16, 1] # abnormal: end of cylinder def test_terminal_count_after_the_last_sector_names_the_next_cylinder(): board = machine(blank_disk()) read_sector(board, 0, 16) pulse_tc(board) assert result(board) == [0x00, 0x00, 0x00, 1, 0, 1, 1] def test_a_sector_that_is_not_there_is_no_data_after_two_index_holes(): board = machine(blank_disk()) command(board, 0x46, 0x00, 5, 0x00, 1, 0x01, SECTORS, 0x0E, 0xFF) # head is on track 0 took = wait(board, lambda: board.read_port(0xE0) & 0xC0 == 0xC0) assert REVOLUTION < took <= 2 * REVOLUTION + 8 assert result(board) == [0x40, 0x04, 0x00, 5, 0, 1, 1] def test_no_disk_is_not_ready(): board = machine(None) command(board, 0x46, 0x00, 0, 0x00, 1, 0x01, SECTORS, 0x0E, 0xFF) assert result(board) == [0x48, 0x00, 0x00, 0, 0, 1, 1] command(board, 0x0F, 0x00, 3) command(board, 0x08) assert result(board) == [0x68, 0] # abnormal, seek end, not ready command(board, 0x04, 0x00) assert result(board) == [0x10] def test_write_a_sector_and_read_it_back(): disk = blank_disk() board = machine(disk) command(board, 0x45, 0x00, 0, 0x00, 2, 0x01, SECTORS, 0x0E, 0xFF) for value in range(100): wait(board, lambda: board.read_port(0xE0) & 0x80) assert board.read_port(0xE0) & 0xF0 == 0xB0 # RQM, to controller, executing, busy board.write_port(0xE1, value) pulse_tc(board) # short of a sector: the rest is zeros assert result(board) == [0x00, 0x00, 0x00, 0, 0, 3, 1] assert disk[SECTOR_SIZE : 2 * SECTOR_SIZE] == bytes(range(100)) + bytes(156) assert read_sector(board, 0, 2)[:101] == [*range(100), 0] assert disk[:SECTOR_SIZE] == bytes(SECTOR_SIZE) and disk[2 * SECTOR_SIZE] == 2 def test_format_fills_the_track_and_read_id_finds_a_sector(): disk = blank_disk() board = machine(disk) command(board, 0x0F, 0x00, 1) wait(board, lambda: board.read_port(0xE4) & 1) command(board, 0x08) result(board) command(board, 0x4D, 0x00, 0x01, SECTORS, 0x2A, 0xE5) # the IPL's, but for the filler for sector in range(1, SECTORS + 1): for value in (1, 0, sector, 1): # C, H, R, N wait(board, lambda: board.read_port(0xE0) & 0x80) board.write_port(0xE1, value) wait(board, lambda: board.read_port(0xE0) & 0xC0 == 0xC0) assert result(board) == [0x00, 0x00, 0x00, 0, 0, 0, 1] track = SECTORS * SECTOR_SIZE assert disk[track : 2 * track] == b"\xe5" * track assert disk[track - 1] == 15 and disk[2 * track] == 32 # the neighbours are untouched command(board, 0x4A, 0x00) # READ ID wait(board, lambda: board.read_port(0xE0) & 0xC0 == 0xC0) status0, status1, status2, cylinder, head, sector, size = result(board) assert (status0, status1, status2, cylinder, head, size) == (0, 0, 0, 1, 0, 1) assert 1 <= sector <= SECTORS def test_reset_pin_abandons_a_command(): board = machine(blank_disk()) board.write_port(0xE1, 0x46) assert board.read_port(0xE0) == 0x90 # busy: a command is half in board.write_port(0xE7, 0x07) # set PC3: RESET board.write_port(0xE7, 0x06) assert board.read_port(0xE0) == 0x80