"""The desktop frontend's conversions, which need no window (pygame is not imported).""" from __future__ import annotations import zipfile from array import array from pathlib import Path import pytest from sms_python.frontend import DCBlocker, SoundBuffer, frame_bytes, machine_for def test_frame_bytes_are_opaque_bgra(): assert frame_bytes([[0x123456, 0xFFFFFF]]) == bytes([0x56, 0x34, 0x12, 0xFF]) + b"\xff" * 4 def test_dc_blocker_centres_a_held_level_on_zero(): dc = DCBlocker() first = array("h", dc.samples_s16([(1.0, 0.0)])) assert first[0] > 20_000 and first[1] == 0 # the step passes, one side at a time held = array("h", dc.samples_s16([(1.0, 0.0)] * 5000)) assert abs(held[-2]) < 10 # a constant level decays away def test_sound_buffer_pads_with_silence(): buffer = SoundBuffer() buffer.push(b"\x01\x02\x03") stream = memoryview(bytearray(b"\xaa" * 6)) buffer.fill(None, stream) assert bytes(stream) == b"\x01\x02\x03\x00\x00\x00" and len(buffer) == 0 def test_machine_from_the_file_or_the_image_in_its_zip(tmp_path: Path): archive = tmp_path / "game.zip" with zipfile.ZipFile(archive, "w") as z: z.writestr("game.gg", b"\x00" * 16) assert machine_for(archive) == "gamegear" assert machine_for(Path("x.sg")) == "sg1000" assert machine_for(Path("x.sms")) == "sms2" assert machine_for(Path("x.sc")) == "sc3000" assert machine_for(Path("x.sf")) == "sf7000" def test_window_runs_an_sc3000_and_an_sf7000_without_a_display(tmp_path: Path, monkeypatch): """The SC-3000's keyboard path and the SF-7000's disk path, on SDL's dummy drivers.""" pygame = pytest.importorskip("pygame") controller = pytest.importorskip("pygame._sdl2.controller") monkeypatch.setenv("SDL_VIDEODRIVER", "dummy") monkeypatch.setenv("SDL_AUDIODRIVER", "dummy") from sms_python.frontend import Frontend from sms_python.sf7000 import DISK_SIZE from sms_python.state import save_state program = bytes([0xF3, 0x18, 0xFE]) # DI; JR to itself cartridge = tmp_path / "program.sc" cartridge.write_bytes(program.ljust(0x2000, b"\xff")) disk = tmp_path / "disk.sf" disk.write_bytes(bytes(DISK_SIZE)) ipl = tmp_path / "ipl.rom" ipl.write_bytes(program.ljust(0x2000, b"\xff")) pygame.display.init() controller.init() try: computer = Frontend(pygame, cartridge, machine_for(cartridge), None, 1, False, False) computer.run(max_frames=2) assert computer.system.board.keyboard.keys == set() # F6 and F7: a state saved, run on from, and loaded. computer.saved = save_state(computer.system) at = computer.system.cycles computer.run(max_frames=3) computer._load_saved() assert computer.system.cycles == at # F8 held: back through the states the window kept as it ran. computer.run(max_frames=10) late = computer.system.cycles computer.rewinding = True computer.run(max_frames=4) assert computer.system.cycles < late station = Frontend(pygame, disk, machine_for(disk), ipl, 1, False, False) station.run(max_frames=2) assert station.system.board.disk_unit.fdc.disk == bytes(DISK_SIZE) assert disk.read_bytes() == bytes(DISK_SIZE) # the image file is not written finally: pygame.quit()