From 2d0fcb51152e79efcd55c523b6adecf8a2aa6348 Mon Sep 17 00:00:00 2001 From: Ivan Kosarev Date: Sun, 31 Jan 2021 15:08:12 +0200 Subject: [PATCH] [#18] Move the most basic emulator methods to the machine class. --- zx/_emulator.py | 17 +++-------------- zx/_machine.py | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/zx/_emulator.py b/zx/_emulator.py index 333d1be..3ff7dfb 100644 --- a/zx/_emulator.py +++ b/zx/_emulator.py @@ -14,7 +14,6 @@ from ._data import SoundFile from ._device import DeviceEvent from ._error import Error -from ._except import EmulationExit from ._except import EmulatorException from ._file import parse_file from ._gui import ScreenWindow @@ -66,6 +65,9 @@ def __init__(self, speed_factor=1.0, profile=None, devices=None): if devices is None and self.__speed_factor is not None: self.__devices = [ScreenWindow(self)] + # TODO: Eliminate the undescored version. + self.devices = self.__devices + self.__keyboard_state = KeyboardState() self.set_on_input_callback(self.__on_input) @@ -79,16 +81,6 @@ def __init__(self, speed_factor=1.0, profile=None, devices=None): if self.__profile: self.set_breakpoints(0, 0x10000) - def __enter__(self): - return self - - def destroy(self): - for device in self.__devices: - device.destroy() - - def __exit__(self, type, value, tb): - self.destroy() - # TODO: Double-underscore or make public. def _is_paused(self): return self.__is_paused_flag @@ -117,9 +109,6 @@ def _save_snapshot_file(self, format, filename): image = snapshot f.write(image) - def stop(self): - raise EmulationExit() - # TODO: Double-underscore or make public. def _is_tape_paused(self): return self._tape_player.is_paused() diff --git a/zx/_machine.py b/zx/_machine.py index 1bc0af5..fa7c7d4 100644 --- a/zx/_machine.py +++ b/zx/_machine.py @@ -13,6 +13,7 @@ from ._data import MachineSnapshot from ._data import ProcessorSnapshot from ._emulatorbase import _Spectrum48Base +from ._except import EmulationExit from ._rom import get_rom_image from ._utils import make16 @@ -352,6 +353,19 @@ def __init__(self): # Install ROM. self.write(0x0000, get_rom_image(self.machine_kind)) + def destroy(self): + for device in self.devices: + device.destroy() + + def __enter__(self): + return self + + def __exit__(self, type, value, tb): + self.destroy() + + def stop(self): + raise EmulationExit() + def set_breakpoints(self, addr, size): self.mark_addrs(addr, size, self._BREAKPOINT_MARK)