-
Notifications
You must be signed in to change notification settings - Fork 7
/
wxatari.py
470 lines (406 loc) · 17.1 KB
/
wxatari.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
#!/usr/bin/env python
import os
import sys
import time
import wx
import wx.lib.newevent
try:
import wx.glcanvas as glcanvas
import OpenGL.GL as gl
HAS_OPENGL = True
except ImportError:
HAS_OPENGL = False
# FIXME: OpenGL on wx4 is segfaulting.
# update: Legacy OpenGL is segfaulting, GLSL seems to work.
#HAS_OPENGL = False
import numpy as np
# Include pyatari directory so that modules can be imported normally
import sys
module_dir = os.path.realpath(os.path.abspath(".."))
if module_dir not in sys.path:
sys.path.insert(0, module_dir)
import omnivore.emulators.atari8bit as a8
from omnivore.emulators.atari8bit.atari800 import Atari800
import omnivore.ui.screen as screen
akey = a8.akey
import logging
logging.basicConfig(level=logging.WARNING)
log = logging.getLogger(__name__)
#log.setLevel(logging.DEBUG)
class EmulatorControlBase(object):
def __init__(self, emulator):
self.emulator_panel = None
self.emulator = emulator
self.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.on_timer)
self.firsttime=True
self.refreshed=False
self.repeat=True
self.forceupdate=False
self.framerate = 1/60.0
self.tickrate = self.framerate
self.delay = self.tickrate * 1000 # wxpython delays are in milliseconds
self.last_update_time = 0.0
self.key_down = False
def on_key_down(self, evt):
log.debug("key down! key=%s mod=%s" % (evt.GetKeyCode(), evt.GetModifiers()))
key = evt.GetKeyCode()
mod = evt.GetModifiers()
if mod == wx.MOD_ALT or self.is_paused:
self.on_emulator_command_key(evt)
return
elif key == wx.WXK_F11:
self.on_emulator_command_key(evt)
return
else:
self.emulator.process_key_down_event(evt)
def on_key_up(self, evt):
log.debug("key up before evt=%s" % evt.GetKeyCode())
key=evt.GetKeyCode()
self.emulator.clear_keys()
evt.Skip()
def on_char(self, evt):
log.debug("on_char! char=%s, key=%s, raw=%s modifiers=%s" % (evt.GetUnicodeKey(), evt.GetKeyCode(), evt.GetRawKeyCode(), bin(evt.GetModifiers())))
mods = evt.GetModifiers()
char = evt.GetUnicodeKey()
if char > 0:
self.emulator.send_char(char)
else:
key = evt.GetKeyCode()
evt.Skip()
def show_audio(self):
#import binascii
#a = binascii.hexlify(self.emulator.audio)
#print np.where(self.emulator.audio > 0)
pass
# No really good solutions, especially cross-platform. In python 3, there's
# time.perf_counter, so maybe that it a thread will work where the thread
# generates wx Events that can be monitored.
if True:
def on_timer(self, evt):
now = time.time()
self.emulator.next_frame()
print(f"showing frame {self.emulator.status['frame_number']} {self.emulator.current_cpu_status}")
self.emulator_panel.show_frame()
self.show_audio()
after = time.time()
delta = after - now
if delta > self.framerate:
next_time = self.framerate * .8
elif delta < self.framerate:
next_time = self.framerate - delta
print(("now=%f show=%f delta=%f framerate=%f next_time=%f" % (now, after-now, delta, self.framerate, next_time)))
self.timer.StartOnce(next_time * 1000)
self.last_update_time = now
evt.Skip()
elif wx.Platform == "__WXGTK__":
def on_timer(self, evt):
if self.timer.IsRunning():
self.process_key_state()
now = time.time()
delta = now - self.last_update_time
print(("now=%f delta=%f framerate=%f" % (now, delta, self.framerate)))
if delta >= self.framerate:
self.emulator.next_frame()
print(("showing frame %d" % self.emulator.status['frame_number']))
self.emulator_panel.show_frame()
self.show_audio()
if delta > 2 * self.framerate:
self.emulator.next_frame()
print(("got extra frame %d" % self.emulator.status['frame_number']))
self.emulator_panel.show_frame()
self.show_audio()
self.last_update_time = now # + (delta % self.framerate)
else:
self.last_update_time += self.framerate
else:
print(("pausing a tick after frame %d" % self.emulator.status['frame_number']))
#self.last_update_time += self.tickrate
evt.Skip()
elif wx.Platform == "__WXMSW__":
# FIXME: settles on 120%
def on_timer(self, evt):
if self.timer.IsRunning():
self.process_key_state()
now = time.time()
if now > self.next_update_time:
delta = now - self.next_update_time
print(("now=%f next=%f delta=%f framerate=%f" % (now, self.next_update_time, delta, self.framerate)))
self.emulator.next_frame()
self.emulator_panel.show_frame()
self.show_audio()
# updating too slowly?
self.frame_delta += delta
delta = now - self.next_update_time
if delta > self.framerate:
self.emulator.next_frame()
print(("got extra frame %d" % self.emulator.status['frame_number']))
self.emulator_panel.show_frame()
self.show_audio()
self.next_update_time += self.framerate
self.next_update_time += self.framerate
else:
print(("pausing a tick after frame %d" % self.emulator.status['frame_number']))
#self.last_update_time += self.tickrate
evt.Skip()
def start_timer(self,repeat=False,delay=None,forceupdate=True):
if not self.timer.IsRunning():
self.repeat=repeat
if delay is not None:
self.delay=delay
self.forceupdate=forceupdate
self.last_update_time = time.time()
self.next_update_time = time.time() + self.framerate
self.frame_delta = 0.0
self.timer.Start(self.delay)
def stop_timer(self):
if self.timer.IsRunning():
self.timer.Stop()
def on_start(self, evt=None):
self.start_timer(repeat=True)
@property
def is_paused(self):
return not self.timer.IsRunning()
def on_pause(self, evt=None):
self.stop_timer()
class EmulatorFrame(EmulatorControlBase, wx.Frame):
parsed_args = []
options = {}
def __init__(self, bootfile=None, autostart=True):
wx.Frame.__init__(self, None, -1, "wxPython atari800 test", pos=(50,50),
size=(200,100), style=wx.DEFAULT_FRAME_STYLE)
EmulatorControlBase.__init__(self, Atari800())
self.CreateStatusBar()
menuBar = wx.MenuBar()
menu = wx.Menu()
self.id_load = wx.NewId()
item = menu.Append(self.id_load, "Load Image", "Load a disk image")
self.Bind(wx.EVT_MENU, self.on_menu, item)
menu.AppendSeparator()
item = menu.Append(wx.ID_EXIT, "E&xit\tCtrl-Q", "Exit demo")
self.Bind(wx.EVT_MENU, self.on_menu, item)
menuBar.Append(menu, "&File")
self.id_pause = wx.NewId()
self.id_coldstart = wx.NewId()
self.id_warmstart = wx.NewId()
self.id_start_debugging = wx.NewId()
self.id_debug_step = wx.NewId()
menu = wx.Menu()
self.pause_item = menu.Append(self.id_pause, "Pause\tCtrl-P", "Pause or resume the emulation")
self.Bind(wx.EVT_MENU, self.on_menu, self.pause_item)
item = menu.Append(self.id_start_debugging, "Start Debugging", "Enter monitor")
self.Bind(wx.EVT_MENU, self.on_menu, item)
menu.AppendSeparator()
item = menu.Append(self.id_debug_step, "Step", "Process one instruction")
self.Bind(wx.EVT_MENU, self.on_menu, item)
menu.AppendSeparator()
item = menu.Append(self.id_coldstart, "Cold Start", "Cold start (power switch off then on)")
self.Bind(wx.EVT_MENU, self.on_menu, item)
item = menu.Append(self.id_coldstart, "Warm Start", "Warm start (reset switch)")
self.Bind(wx.EVT_MENU, self.on_menu, item)
menuBar.Append(menu, "&Machine")
self.id_screen1x = wx.NewId()
self.id_screen2x = wx.NewId()
self.id_screen3x = wx.NewId()
self.id_glsl = wx.NewId()
self.id_opengl = wx.NewId()
self.id_unaccelerated = wx.NewId()
menu = wx.Menu()
item = menu.AppendRadioItem(self.id_glsl, "GLSL", "Use GLSL for scalable display")
self.Bind(wx.EVT_MENU, self.on_menu, item)
item = menu.AppendRadioItem(self.id_opengl, "OpenGL", "Use OpenGL for scalable display")
self.Bind(wx.EVT_MENU, self.on_menu, item)
item = menu.AppendRadioItem(self.id_unaccelerated, "Unaccelerated", "No OpenGL acceleration")
self.Bind(wx.EVT_MENU, self.on_menu, item)
menu.AppendSeparator()
item = menu.Append(self.id_screen1x, "Display 1x", "No magnification")
self.Bind(wx.EVT_MENU, self.on_menu, item)
item = menu.Append(self.id_screen2x, "Display 2x", "2x display")
self.Bind(wx.EVT_MENU, self.on_menu, item)
item = menu.Append(self.id_screen3x, "Display 3x", "3x display")
self.Bind(wx.EVT_MENU, self.on_menu, item)
menuBar.Append(menu, "&Screen")
self.SetMenuBar(menuBar)
self.Show(True)
self.Bind(wx.EVT_CLOSE, self.on_close_frame)
self.SetSize((800, 600))
self.emulator_panel = wx.Panel(self, -1)
self.cpu_status = wx.StaticText(self, -1)
self.box = wx.BoxSizer(wx.VERTICAL)
self.box.Add(self.emulator_panel, 1, wx.EXPAND)
self.box.Add(self.cpu_status, 0, wx.EXPAND)
self.SetSizer(self.box)
if self.options.unaccelerated or wx.Platform == "__WXMSW__":
control = screen.BitmapScreen
elif self.options.glsl and HAS_OPENGL:
control = screen.GLSLScreen
elif HAS_OPENGL:
control = screen.OpenGLScreen
else:
control = screen.BitmapScreen
self.set_display(control)
self.frame_cursor = -1
self.emulator.configure_emulator([])
if self.parsed_args:
self.emulator.boot_from_file(self.parsed_args[0])
if autostart:
wx.CallAfter(self.on_start, None)
def set_glsl(self):
self.set_display(a8.GLSLScreen)
def set_opengl(self):
self.set_display(a8.OpenGLScreen)
def set_unaccelerated(self):
self.set_display(a8.BitmapScreen)
def set_display(self, panel_cls):
paused = self.is_paused
self.on_pause()
old = self.emulator_panel
# Mac can occasionally fail to get an OpenGL context, so creation of
# the panel can fail. Attempting to work around by giving it more
# chances to work.
attempts = 3
while attempts > 0:
attempts -= 1
try:
self.emulator_panel = panel_cls(self, self.emulator)
attempts = 0
except wx.wxAssertionError:
log.error("Failed initializing OpenGL context. Trying %d more times" % attempts)
time.sleep(1)
self.box.Replace(old, self.emulator_panel)
old.Destroy()
print(self.emulator_panel)
self.Layout()
self.emulator_panel.SetFocus()
self.emulator_panel.Bind(wx.EVT_KEY_UP, self.on_key_up)
self.emulator_panel.Bind(wx.EVT_KEY_DOWN, self.on_key_down)
self.emulator_panel.Bind(wx.EVT_CHAR, self.on_char)
if not paused:
self.on_start()
def on_menu(self, evt):
id = evt.GetId()
if id == wx.ID_EXIT:
self.emulator.end_emulation()
self.timer.Stop()
self.Close(True)
elif id == self.id_load:
dlg = wx.FileDialog(self, "Choose a disk image", defaultDir = "", defaultFile = "", wildcard = "*.atr")
if dlg.ShowModal() == wx.ID_OK:
print(("Opening %s" % dlg.GetPath()))
filename = dlg.GetPath()
else:
filename = None
dlg.Destroy()
if filename:
self.emulator.load_disk(1, filename)
self.emulator.coldstart()
elif id == self.id_coldstart:
self.emulator.coldstart()
elif id == self.id_warmstart:
self.emulator.warmstart()
elif id == self.id_start_debugging:
self.pause()
elif id == self.id_debug_step:
self.emulator.debugger_step()
elif id == self.id_glsl:
self.set_glsl()
elif id == self.id_opengl:
self.set_opengl()
elif id == self.id_unaccelerated:
self.set_unaccelerated()
elif id == self.id_screen1x:
self.emulator_panel.set_scale(1)
elif id == self.id_screen2x:
self.emulator_panel.set_scale(2)
elif id == self.id_screen3x:
self.emulator_panel.set_scale(3)
elif id == self.id_pause:
if self.is_paused:
self.restart()
else:
self.pause()
def on_emulator_command_key(self, evt):
key = evt.GetKeyCode()
mod = evt.GetModifiers()
print(("emu key: %s %s" % (key, mod)))
if key == wx.WXK_LEFT:
if not self.is_paused:
self.pause()
else:
self.history_previous()
elif key == wx.WXK_RIGHT:
if not self.is_paused:
self.pause()
else:
self.history_next()
elif key == wx.WXK_SPACE or key == wx.WXK_F11:
if self.is_paused:
self.restart()
else:
self.pause()
def restart(self):
print("restart")
self.on_start()
self.pause_item.SetItemLabel("Pause")
if self.frame_cursor >= 0:
self.emulator.restore_history(self.frame_cursor)
self.frame_cursor = -1
self.SetStatusText("")
def pause(self):
print("pause")
self.on_pause()
self.pause_item.SetItemLabel("Resume")
self.emulator.save_history(True)
self.update_ui()
def history_previous(self):
print(f"frame_cursor: {self.frame_cursor} current: {self.emulator.current_frame_number}")
if self.frame_cursor < 0:
self.frame_cursor = self.emulator.current_frame_number
print(f"frame_cursor: {self.frame_cursor} current: {self.emulator.current_frame_number}")
try:
self.frame_cursor = self.emulator.get_previous_history(self.frame_cursor)
print(f"frame_cursor: {self.frame_cursor} current: {self.emulator.current_frame_number}")
#self.emulator.restore_history(frame_number)
except IndexError:
print(f"No previous frame before {self.frame_cursor}")
return
self.emulator_panel.show_frame(self.frame_cursor)
self.update_ui()
def history_next(self):
if self.frame_cursor < 0:
return
try:
self.frame_cursor = self.emulator.get_next_history(self.frame_cursor)
#self.emulator.restore_history(frame_number)
except IndexError:
self.frame_cursor = -1
self.emulator_panel.show_frame(self.frame_cursor)
self.update_ui()
def show_frame_number(self):
text = "Paused: %d frames, showing %d" % (self.emulator.current_frame_number, self.frame_cursor if self.frame_cursor > 0 else self.emulator.current_frame_number)
print(text)
self.SetStatusText(text)
def update_ui(self):
self.show_frame_number()
self.update_internals()
def update_internals(self):
a, p, sp, x, y, _, pc = self.emulator.cpu_state
#print(offsets)
text = "A=%02x X=%02x Y=%02x SP=%02x FLAGS=%02x PC=%04x" % (a, x, y, sp, p, pc)
self.cpu_status.SetLabel(text)
def on_close_frame(self, evt):
self.emulator.end_emulation()
evt.Skip()
if __name__ == '__main__':
# use argparse rather than sys.argv to handle the difference in being
# called as "python script.py" and "./script.py"
import argparse
parser = argparse.ArgumentParser(description='Atari800 WX Demo')
parser.add_argument("--unaccelerated", "--bitmap", "--slow", action="store_true", default=False, help="Use bitmap scaling instead of OpenGL")
parser.add_argument("--opengl", action="store_true", default=False, help="Use OpenGL scaling")
parser.add_argument("--glsl", action="store_true", default=False, help="Use GLSL scaling")
EmulatorFrame.options, EmulatorFrame.parsed_args = parser.parse_known_args()
app = wx.App(False)
frame = EmulatorFrame()
frame.Show()
app.MainLoop()