-
Notifications
You must be signed in to change notification settings - Fork 3
/
RomFrame.py
283 lines (283 loc) · 13.2 KB
/
RomFrame.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
import wx
import os
import time
import threading
from RomUtils import RomUtils
class RomFrame(wx.Frame):
def __init__(self):
self.endcallback = None
self.ser = None
self.amiga = None
self.execlib = None
self.doslib = None
self.snip = None
self.abort = threading.Event()
self.wantclose = False
self.busy = False
self.done = False
self.stepsize = 0x8000
self.timerperiod = 50
super().__init__(None, id=wx.ID_ANY, title=u"amigaXfer ROM Tool", pos=wx.DefaultPosition, size=wx.Size(500, 250), style=wx.DEFAULT_FRAME_STYLE | wx.TAB_TRAVERSAL)
self.SetSizeHints(wx.DefaultSize, wx.DefaultSize)
self.SetBackgroundColour(wx.SystemSettings.GetColour(wx.SYS_COLOUR_3DLIGHT))
bSizer10 = wx.BoxSizer(wx.VERTICAL)
wSizer15 = wx.WrapSizer(wx.HORIZONTAL, wx.WRAPSIZER_DEFAULT_FLAGS)
self.m_versionmsg = wx.StaticText(self, wx.ID_ANY, u"Version", wx.DefaultPosition, wx.DefaultSize, 0)
self.m_versionmsg.Wrap(-1)
wSizer15.Add(self.m_versionmsg, 0, wx.ALL | wx.ALIGN_CENTER_VERTICAL, 5)
self.m_version = wx.TextCtrl(self, wx.ID_ANY, u"0", wx.DefaultPosition, wx.DefaultSize, wx.TE_READONLY)
self.m_version.SetFont(wx.Font(wx.NORMAL_FONT.GetPointSize(), wx.FONTFAMILY_TELETYPE, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, False, wx.EmptyString))
maxlen = 5
self.m_version.SetMaxLength(maxlen)
self.m_version.SetInitialSize(self.m_version.GetSizeFromTextSize(self.m_version.GetTextExtent("A" * maxlen)))
wSizer15.Add(self.m_version, 0, wx.ALL | wx.ALIGN_CENTER_VERTICAL, 5)
self.m_revisionmsg = wx.StaticText(self, wx.ID_ANY, u"Revision", wx.DefaultPosition, wx.DefaultSize, 0)
self.m_revisionmsg.Wrap(-1)
wSizer15.Add(self.m_revisionmsg, 0, wx.ALL | wx.ALIGN_CENTER_VERTICAL, 5)
self.m_revision = wx.TextCtrl(self, wx.ID_ANY, u"0", wx.DefaultPosition, wx.DefaultSize, wx.TE_READONLY)
self.m_revision.SetFont(wx.Font(wx.NORMAL_FONT.GetPointSize(), wx.FONTFAMILY_TELETYPE, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, False, wx.EmptyString))
maxlen = 5
self.m_revision.SetMaxLength(maxlen)
self.m_revision.SetInitialSize(self.m_revision.GetSizeFromTextSize(self.m_version.GetTextExtent("A" * maxlen)))
wSizer15.Add(self.m_revision, 0, wx.ALL | wx.ALIGN_CENTER_VERTICAL, 5)
bSizer10.Add(wSizer15, 0, wx.EXPAND, 5)
wSizer16 = wx.WrapSizer(wx.HORIZONTAL, wx.WRAPSIZER_DEFAULT_FLAGS)
self.m_addrmsg = wx.StaticText(self, wx.ID_ANY, u"Address", wx.DefaultPosition, wx.DefaultSize, 0)
self.m_addrmsg.Wrap(-1)
wSizer16.Add(self.m_addrmsg, 0, wx.ALL | wx.ALIGN_CENTER_VERTICAL, 5)
self.m_addr = wx.TextCtrl(self, wx.ID_ANY, u"00000000", wx.DefaultPosition, wx.DefaultSize, wx.TE_READONLY)
self.m_addr.SetFont(wx.Font(wx.NORMAL_FONT.GetPointSize(), wx.FONTFAMILY_TELETYPE, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, False, wx.EmptyString))
maxlen = 8
self.m_addr.SetMaxLength(maxlen)
self.m_addr.SetInitialSize(self.m_addr.GetSizeFromTextSize(self.m_version.GetTextExtent("A" * maxlen)))
wSizer16.Add(self.m_addr, 0, wx.ALL | wx.ALIGN_CENTER_VERTICAL, 5)
self.m_sizemsg = wx.StaticText(self, wx.ID_ANY, u"Size", wx.DefaultPosition, wx.DefaultSize, 0)
self.m_sizemsg.Wrap(-1)
wSizer16.Add(self.m_sizemsg, 0, wx.ALL | wx.ALIGN_CENTER_VERTICAL, 5)
self.m_size = wx.TextCtrl(self, wx.ID_ANY, u"00000", wx.DefaultPosition, wx.DefaultSize, wx.TE_READONLY)
self.m_size.SetFont(wx.Font(wx.NORMAL_FONT.GetPointSize(), wx.FONTFAMILY_TELETYPE, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, False, wx.EmptyString))
maxlen = 5
self.m_size.SetMaxLength(maxlen)
self.m_size.SetInitialSize(self.m_size.GetSizeFromTextSize(self.m_version.GetTextExtent("A" * maxlen)))
wSizer16.Add(self.m_size, 0, wx.ALL | wx.ALIGN_CENTER_VERTICAL, 5)
bSizer10.Add(wSizer16, 0, wx.EXPAND, 5)
self.m_targetmsg = wx.StaticText(self, wx.ID_ANY, u"Target file", wx.DefaultPosition, wx.DefaultSize, 0)
self.m_targetmsg.Wrap(-1)
bSizer10.Add(self.m_targetmsg, 0, wx.ALL | wx.ALIGN_CENTER_HORIZONTAL, 5)
self.m_target = wx.FilePickerCtrl(self, wx.ID_ANY, wx.EmptyString, u"Select a file", u"*.rom", wx.DefaultPosition, wx.DefaultSize, wx.FLP_OVERWRITE_PROMPT | wx.FLP_SAVE | wx.FLP_USE_TEXTCTRL)
bSizer10.Add(self.m_target, 0, wx.ALL | wx.EXPAND, 5)
self.m_progress = wx.Gauge(self, wx.ID_ANY, 100, wx.DefaultPosition, wx.DefaultSize, wx.GA_HORIZONTAL)
self.m_progress.SetValue(0)
bSizer10.Add(self.m_progress, 0, wx.ALL | wx.EXPAND, 5)
wSizer17 = wx.WrapSizer(wx.HORIZONTAL, wx.WRAPSIZER_DEFAULT_FLAGS)
self.m_status = wx.TextCtrl(self, wx.ID_ANY, u"Init", wx.DefaultPosition, wx.DefaultSize, wx.TE_READONLY)
self.m_status.SetFont(wx.Font(wx.NORMAL_FONT.GetPointSize(), wx.FONTFAMILY_TELETYPE, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, False, wx.EmptyString))
maxlen = 9
self.m_status.SetMaxLength(maxlen)
self.m_status.SetInitialSize(self.m_status.GetSizeFromTextSize(self.m_version.GetTextExtent("A" * maxlen)))
wSizer17.Add(self.m_status, 0, wx.ALL | wx.ALIGN_CENTER_VERTICAL, 5)
wSizer17.Add((0, 0), 1, wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_CENTER_HORIZONTAL, 5)
self.m_timermsg = wx.StaticText(self, wx.ID_ANY, u"Time", wx.DefaultPosition, wx.DefaultSize, 0)
self.m_timermsg.Wrap(-1)
wSizer17.Add(self.m_timermsg, 0, wx.ALL | wx.ALIGN_CENTER_VERTICAL, 5)
self.m_timer = wx.TextCtrl(self, wx.ID_ANY, u" 0.0s", wx.DefaultPosition, wx.DefaultSize, wx.TE_READONLY)
self.m_timer.SetFont(wx.Font(wx.NORMAL_FONT.GetPointSize(), wx.FONTFAMILY_TELETYPE, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, False, wx.EmptyString))
maxlen = 7
self.m_timer.SetMaxLength(maxlen)
self.m_timer.SetInitialSize(self.m_timer.GetSizeFromTextSize(self.m_timer.GetTextExtent("A" * maxlen)))
wSizer17.Add(self.m_timer, 0, wx.ALL, 5)
self.m_abort = wx.Button(self, wx.ID_ANY, u"Stop", wx.DefaultPosition, wx.DefaultSize, 0)
self.m_abort.Hide()
self.m_abort.Bind(wx.EVT_BUTTON, self.onAbortPressed)
wSizer17.Add(self.m_abort, 0, wx.ALL | wx.ALIGN_CENTER_VERTICAL, 5)
self.m_dump = wx.Button(self, wx.ID_ANY, u"Dump", wx.DefaultPosition, wx.DefaultSize, 0)
wSizer17.Add(self.m_dump, 0, wx.ALL | wx.ALIGN_CENTER_VERTICAL, 5)
self.m_exit = wx.Button(self, wx.ID_ANY, u"Exit", wx.DefaultPosition, wx.DefaultSize, 0)
wSizer17.Add(self.m_exit, 0, wx.ALL | wx.ALIGN_CENTER_VERTICAL, 5)
bSizer10.Add(wSizer17, 1, wx.EXPAND, 5)
self.SetSizer(bSizer10)
self.Layout()
self.Fit()
self.Centre(wx.BOTH)
self.m_exit.Bind(wx.EVT_BUTTON, self.onExitPressed)
self.m_dump.Bind(wx.EVT_BUTTON, self.onDumpPressed)
return
def UpdateStatus(self, status):
self.m_status.ChangeValue(status)
return
def UpdateProgressValue(self, value):
self.m_progress.SetValue(value)
return
def UpdateProgressRange(self, value):
self.m_progress.SetRange(value)
return
def UpdateProgressPulse(self):
self.m_progress.Pulse()
return
def UpdateProgressDone(self):
maxval = self.m_progress.GetRange()
self.m_progress.SetValue(maxval)
return
def Enablement(self, enable):
if not self.done:
self.m_target.Enable(enable)
self.m_dump.Enable(enable)
self.m_exit.Enable(enable)
return
def onCloseSetup(self, event):
if event.CanVeto():
event.Veto()
return
def onClose(self, event):
if event.CanVeto():
event.Veto()
if self.wantclose:
return
if self.busy:
self.wantclose = True
wx.CallAfter(self.Abort)
return
self.UpdateStatus("UserClose")
self.CleanUp()
return
def onTimer(self, event):
value = time.monotonic() - self.timerbase
self.m_timer.ChangeValue(f"{value:6.1F}s")
return
def onExitPressed(self, event):
self.wantclose = True
wx.CallAfter(self.UpdateStatus, "CleanUp")
wx.CallAfter(self.CleanUp)
return
def onAbortPressed(self, event):
self.Abort()
return
def Abort(self):
self.m_abort.Enable(False)
self.abort.set()
return
def onDumpPressed(self, event):
self.Enablement(False)
self.abort.clear()
self.busy = True
self.m_abort.Enable(True)
self.m_dump.Hide()
self.m_abort.Show()
self.Layout()
self.UpdateStatus("Start")
localpath = self.m_target.GetPath()
debug = False
if not localpath:
wx.CallAfter(self.UpdateStatus, "File?")
print("No target file selected.")
wx.CallAfter(self.Stop)
return
#FIXME: Ugly test. No better way?
if os.path.exists(localpath):
if os.path.isfile(localpath):
if not os.access(localpath, os.W_OK):
wx.CallAfter(self.UpdateStatus, "FilePerm?")
print("Target is not writable.")
wx.CallAfter(self.Stop)
return
else:
wx.CallAfter(self.UpdateStatus, "FileNot?")
print("Target is not a file.")
wx.CallAfter(self.Stop)
return
else:
localpathdir = os.path.dirname(localpath)
if os.path.exists(localpathdir):
if not os.access(localpathdir, os.W_OK):
wx.CallAfter(self.UpdateStatus, "DirPerm?")
print("Target directory is not writable.")
wx.CallAfter(self.Stop)
return
else:
wx.CallAfter(self.UpdateStatus, "FileDir?")
print("Target directory does not exist.")
wx.CallAfter(self.Stop)
return
self.busy = True
self.timerbase = time.monotonic()
self.timer.Start(self.timerperiod)
threading.Thread(target=self.DumpWorker, args=(localpath, debug)).start()
return
def DumpWorker(self, localpath, debug):
romdump = bytearray()
romaddr = self.romutil.getaddr()
length = self.romutil.getsize()
blocks = length // self.stepsize
wx.CallAfter(self.UpdateProgressRange, blocks)
block = 0
for addr in range(romaddr, romaddr+length, self.stepsize):
wx.CallAfter(self.UpdateStatus, f"{addr:x}")
wx.CallAfter(self.UpdateProgressValue, block)
print(f"read+crc size: {hex(self.stepsize)}, addr: {hex(addr)}.", end='\r', flush=True)
blockdump = self.snip.verifiedreadmem(addr, self.stepsize)
romdump += blockdump
block += 1
if self.abort.is_set():
wx.CallAfter(self.UpdateStatus, "UserStop.")
print("\nUser stopped.")
wx.CallAfter(self.Stop)
return
wx.CallAfter(self.UpdateStatus, "Write")
print(f"\nWriting ROM dump to {localpath}.")
with open(localpath, "wb") as fh:
fh.write(romdump)
self.done = True
print("Done.")
wx.CallAfter(self.UpdateStatus, "Done.")
wx.CallAfter(self.Stop)
return
def Stop(self):
self.timer.Stop()
self.busy = False
self.m_abort.Hide()
self.m_dump.Show()
self.Layout()
if self.abort.is_set():
wx.CallAfter(self.UpdateProgressValue, 0)
else:
wx.CallAfter(self.UpdateProgressDone)
if self.wantclose:
self.UpdateStatus("UserClose")
self.CleanUp()
self.Enablement(True)
return
def RomSetup(self, endcallback, ser, amiga, execlib, snip):
self.Bind(wx.EVT_CLOSE, self.onCloseSetup)
self.m_status.ChangeValue(u'Setup')
self.endcallback = endcallback
self.ser = ser
self.amiga = amiga
self.execlib = execlib
self.snip = snip
self.done = False
self.wantclose = False
self.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.onTimer, self.timer)
self.Enablement(False)
threading.Thread(target=self.RomSetupWorker).start()
return
def RomSetupWorker(self):
romutil = RomUtils(debugger=self.amiga)
self.romutil = romutil
wx.CallAfter(self.RomSetupDone)
return
def RomSetupDone(self):
self.m_version.ChangeValue(f"{self.romutil.getversion()}")
self.m_revision.ChangeValue(f"{self.romutil.getrevision()}")
self.m_addr.ChangeValue(f"{self.romutil.getaddr():x}")
self.m_size.ChangeValue(f"{self.romutil.getsize()//1024}K")
wx.CallAfter(self.UpdateProgressValue, 0)
wx.CallAfter(self.UpdateStatus, "Ready.")
wx.CallAfter(self.Enablement, True)
self.Unbind(wx.EVT_CLOSE, handler=self.onCloseSetup)
self.Bind(wx.EVT_CLOSE, self.onClose)
return
def CleanUp(self):
self.Enablement(False)
wx.CallAfter(self.endcallback)
return