"""The console as a whole: frames, input, audio.""" from __future__ import annotations from sn76489_python import SN76489, SN76489AN from colecovision_python.audio import Mixer from colecovision_python.board import Board from colecovision_python.cartridge import Cartridge, cartridge_for from colecovision_python.controllers import HandController from colecovision_python.images import load_bios, load_image from colecovision_python.machine import NTSC, BoardOptions, Machine class ColecoVision: """A ColecoVision with two hand controllers plugged in. ``run_frame()`` runs until the VDP finishes a frame and returns it (a ``tms9918_python.Frame``: 192 rows of 256 colour indices); the frame's sound is then in ``take_audio()``. ``board`` is the :class:`~colecovision_python.board.Board` for anything finer. The sound chip is sn76489-python's ``SN76489AN`` row (the discrete TI part; the ColecoVision's is the SN76489A, the same die in the plastic package's letter: see docs/psg.md), not silenced at power-on (the core's provisional D5: the BIOS silences it at clock 100). ``audio=False`` leaves the chip out (sound writes are still waited for and logged). ``audio_rate`` is the output rate in Hz. """ def __init__( self, cartridge: Cartridge | bytes | str | None = None, *, machine: Machine = NTSC, bios: bytes | str | None = None, options: BoardOptions | None = None, audio: bool = True, audio_rate: int = 44_100, board_type: str | None = None, **board_keywords: object, ) -> None: if bios is None or isinstance(bios, str): bios = load_bios(machine.bios_sha1, bios) if isinstance(cartridge, str): cartridge = load_image(cartridge) if isinstance(cartridge, bytes | bytearray): cartridge = cartridge_for(bytes(cartridge), board_type) self.controllers = (HandController(), HandController()) board_keywords.setdefault("port1", self.controllers[0]) board_keywords.setdefault("port2", self.controllers[1]) if audio: board_keywords.setdefault("psg", SN76489(SN76489AN)) self.board = Board(bios, cartridge, machine=machine, options=options, **board_keywords) self.machine = machine self.frame = None self.mixer = Mixer(audio_rate) if self.board.psg is not None else None self._audio: list[float] = [] def run_frame(self): """Run to the end of the next frame and return it.""" board = self.board vdp = board.vdp step = board.step while True: step() frame = vdp.take_frame() if frame is not None: video = board.sockets.video self.frame = video.select(frame) if video is not None else frame if self.mixer is not None: self._collect_audio() return self.frame def _collect_audio(self) -> None: board = self.board board.run_psg(board.clock) source = board.sockets.audio expansion = None if source is not None: source.run(board.clock) expansion = source.take_samples() self._audio.extend(self.mixer.mix(board.psg.take_samples(), expansion)) def take_audio(self) -> list[float]: """Mono samples at ``audio_rate`` produced since the last call.""" samples, self._audio = self._audio, [] return samples def run_frames(self, count: int): frame = None for _ in range(count): frame = self.run_frame() return frame