Skip to content

Commit

Permalink
[#18] Introduce dispatchers.
Browse files Browse the repository at this point in the history
  • Loading branch information
kosarev committed Feb 19, 2021
1 parent 46a0f03 commit 2c39f22
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
13 changes: 6 additions & 7 deletions zx/_emulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from ._gui import ScreenWindow
from ._keyboard import KeyboardState
from ._keyboard import KEYS
from ._machine import Dispatcher
from ._machine import RunEvents
from ._machine import Spectrum48
from ._rzx import RZXFile
Expand Down Expand Up @@ -63,12 +64,10 @@ def __init__(self, speed_factor=1.0, profile=None, devices=None):
self.__events_to_signal = RunEvents.NO_EVENTS

# Don't even create the window on full throttle.
self.__devices = devices if devices is not None else []
if devices is None and self.__speed_factor is not None:
self.__devices = [ScreenWindow(self)]
devices = Dispatcher([ScreenWindow(self)])

# TODO: Eliminate the undescored version.
self.devices = self.__devices
self.devices = devices

self.__keyboard_state = KeyboardState()
self.set_on_input_callback(self.__on_input)
Expand Down Expand Up @@ -100,7 +99,7 @@ def _is_tape_paused(self):

def __pause_tape(self, is_paused=True):
self._tape_player.pause(is_paused)
self.notify_devices(TapeStateUpdated())
self.devices.notify(TapeStateUpdated())

def __unpause_tape(self):
self.__pause_tape(is_paused=False)
Expand Down Expand Up @@ -278,7 +277,7 @@ def __run_quantum(self, speed_factor=None):
creator_info = self.__playback_player.find_recording_info_chunk()

if True: # TODO
self.notify_devices(QuantumRun())
self.devices.notify(QuantumRun())

# TODO: For debug purposes.
'''
Expand Down Expand Up @@ -323,7 +322,7 @@ def __run_quantum(self, speed_factor=None):
self.render_screen()

pixels = self.get_frame_pixels()
self.notify_devices(ScreenUpdated(pixels))
self.devices.notify(ScreenUpdated(pixels))

self._tape_player.skip_rest_of_frame()
self._emulation_time.advance(1 / 50)
Expand Down
21 changes: 16 additions & 5 deletions zx/_machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,21 @@ def install_snapshot(self, snapshot):
setattr(self, field, value)


class Dispatcher(object):
def __init__(self, devices=None):
if devices is None:
devices = []

self.__devices = list(devices)

def __iter__(self):
yield from self.__devices

def notify(self, event):
for device in self:
device.on_event(event)


class Spectrum48(_Spectrum48Base, MachineState):
# Memory marks.
__NO_MARKS = 0
Expand Down Expand Up @@ -369,18 +384,14 @@ def __exit__(self, type, value, tb):
def stop(self):
raise EmulationExit()

def notify_devices(self, event):
for device in self.devices:
device.on_event(event)

@property
def paused(self):
return self.__paused

@paused.setter
def paused(self, value):
self.__paused = value
self.notify_devices(PauseStateUpdated())
self.devices.notify(PauseStateUpdated())

def set_breakpoints(self, addr, size):
self.mark_addrs(addr, size, self.__BREAKPOINT_MARK)
Expand Down

0 comments on commit 2c39f22

Please sign in to comment.