-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
402 lines (330 loc) · 12.9 KB
/
main.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
import sys
from glob import glob
from libgb.cpu import CPU
from libgb.ppu import PPU
from libgb.cartridge import Cartridge
from libgb.bus import BUS_ACCESS_READ, BUS_ACCESS_WRITE, BUS_ACCESS_MEMREQ
from junk.window import Window
from junk import WINAPI
from junk.WINAPI import HWND, LPARAM, WPARAM, UINT, HDC, PAINTSTRUCT, pointer, BITMAPINFOHEADER, sizeof, byref, BITMAPINFOHEADER_4PAL, RECT, c_void_p
def buslog(*args, **kwargs): pass
#def buslog(*args, **kwargs): print(*args, **kwargs)
def main():
romdata = None
bootmeme = None
filename = None
if len(sys.argv) > 1:
filename = sys.argv[1]
if not filename:
#filename = glob('C:/Downloads/02-*.gb')[0]
#filename = glob('C:/Downloads/03-*.gb')[0]
filename = 'C:/Downloads/cpu_instrs.gb'
with open(filename, 'rb') as fi:
romdata = fi.read()
try:
with open('C:/Data/lolol/PicoGB/testrom.gb', 'rb') as fi:
bootmeme = fi.read(256)
except:
pass
mappertype = Cartridge.ClassFromHeuristics(romdata)
if not mappertype:
print("Mapper not supported")
sys.exit(1)
cart = mappertype(romdata)
cpu = CPU()
cpu.ResetClear()
cpu.RESET_STABLE = True
cpu.reg.PC = 0x100
cpubus = cpu.bus
if bootmeme:
cpu.ProtectOverlay = True
cpu.reg.PC = 0
ppu = PPU()
tmpwram = [0xFF] * 0x2000
tmpio = [0xFF] * 255
JOYP_RAW = 0
JOYP_PREV = 0xF
def update_JOYP():
nonlocal JOYP_PREV
P1 = tmpio[0]
if not (~P1 & 0x30):
return
jpsel = (P1 & 0x30) | 0xCF
if not (P1 & 0x10):
jpsel &= ~((JOYP_RAW >> 4) & 0xF)
if not (P1 & 0x20):
jpsel &= ~((JOYP_RAW >> 0) & 0xF)
tmpio[0] = jpsel
jpsel &= 0xF
if JOYP_PREV == 0xF and jpsel != 0xF:
cpu.IRQ_SetM(1 << 4)
JOYP_PREV = jpsel
tmpio[0x44] = 0x90
tmpio[0x41] = 0x81
tmpio[0x40] = 0x91
pause = False
autodump = False
count = 0
ppu_ticked = False
bipal = BITMAPINFOHEADER_4PAL()
bmi: BITMAPINFOHEADER = bipal.header
bmi.biSize = sizeof(bmi)
bipal.palette[0].value = 0x1F << 0
bipal.palette[1].value = 0x3F << 5
bipal.palette[2].value = 0x1F << 11
bipal.palette[3].value = 0
bmi.biPlanes = 1
bmi.biWidth = ppu.FRAMEBUF_STRIDE
bmi.biHeight = -ppu.FRAMEBUF_HEIGHT
bmi.biBitCount = 16
bmi.biSizeImage = abs(ppu.FRAMEBUF_STRIDE * ppu.FRAMEBUF_HEIGHT * 2)
#bmi.biCompression = 3 # BI_BITFIELDS
bmi.biCompression = 0 # BI_RGB
winscale = 4
fbpos = (0, 0, 176, 144)
#rekt = (0, 0, ppu.FRAMEBUF_STRIDE * winscale, ppu.FRAMEBUF_HEIGHT * winscale)
rekt = (0, 0, fbpos[2] * winscale, fbpos[3] * winscale)
window: Window = None # type: ignore
def wndproc(wnd: HWND, msg: UINT, wparam: WPARAM, lparam: LPARAM):
if msg == 0x0F:
pstr = PAINTSTRUCT()
lppstr = byref(pstr)
lpbmi = byref(bmi)
dc: HDC = WINAPI.BeginPaint(wnd, lppstr)
img_ptr = c_void_p(ppu.Framebuf.buffer_info()[0])
WINAPI.StretchDIBits\
(
dc,
rekt[0], rekt[1], rekt[2] - rekt[0], rekt[3] - rekt[1],
fbpos[0], ppu.FRAMEBUF_HEIGHT - fbpos[3] + fbpos[1], fbpos[2], fbpos[3],
img_ptr,
lpbmi,
0, # DIB_RGB_COLORS
0x00CC0020 # SRCCOPY
)
WINAPI.EndPaint(wnd, lppstr)
elif msg == 0x100 or msg == 0x101:
nbit = 0
if wparam == 88: # X
nbit = 1 << 0
elif wparam == 67: # C
nbit = 1 << 1
elif wparam == 8: # VK_BACK
nbit = 1 << 2
elif wparam == 13: # VK_RETURN
nbit = 1 << 3
elif wparam == 0x27: # VK_RIGHT
nbit = 1 << 4
elif wparam == 0x25: # VK_LEFT
nbit = 1 << 5
elif wparam == 0x26: # VK_UP
nbit = 1 << 6
elif wparam == 0x28: # VK_DOWN
nbit = 1 << 7
elif wparam == 81: # Q
WINAPI.PostQuitMessage(0)
if nbit:
nonlocal JOYP_RAW
if not (lparam & (1 << 31)): # type: ignore
JOYP_RAW |= nbit
else:
JOYP_RAW &= ~nbit
update_JOYP()
return Window.DefaultWndProc(wnd, msg, wparam, lparam)
#window = Window(wndproc, 168, 144)
#window = Window(wndproc, ppu.FRAMEBUF_STRIDE, ppu.FRAMEBUF_HEIGHT)
window = Window(wndproc, rekt[2] - rekt[0], rekt[3] - rekt[1])
#window.SetWindowPos(274, 474)
window.SetWindowPos(116, 300)
window.Show()
def dumpstate():
afstr = ''
afstr += 'Z' if cpu.reg.F & (1 << 7) else '-'
afstr += 'N' if cpu.reg.F & (1 << 6) else '-'
afstr += 'H' if cpu.reg.F & (1 << 5) else '-'
afstr += 'C' if cpu.reg.F & (1 << 4) else '-'
print("! State dump")
print("- BC: %04X" % cpu.reg.BC)
print("- DE: %04X" % cpu.reg.DE)
print("- HL: %04X" % cpu.reg.HL)
print("- AF: %02X%01Xx %s" % (cpu.reg.A, cpu.reg.F >> 4, afstr))
print("- PC: %04X" % cpu.reg.PC)
print("- SP: %04X" % cpu.reg.SP)
print("! Internals")
print("- IME: %u" % (cpu.IME))
print("- IE: %02X" % cpu.REG_IE)
print("- IF: %02X (<-- %02X <-- %02X) & %02X" % (cpu.REG_IF, cpu.REG_IF_DELAY1, cpu.REG_IF_DELAY2, cpu.REG_IF_CLR))
print("- ISR: %02X" % cpu.IRQ)
print("- DV: %02X" % (cpu.DV))
if cpu.bus.Access:
addrstr = ''
datastr = '--'
if cpu.bus.RD:
addrstr += " /RD"
datastr = "%02X" % cpu.bus.Data
if cpu.bus.WR:
addrstr += " /WR"
datastr = "%02X" % cpu.bus.Data
if cpu.bus.MemReq:
addrstr += " /MEMREQ"
addrstr = "%04X %s%s" % (cpu.bus.Address, datastr, addrstr)
print("- Bus: %s" % (addrstr))
else:
print("- Bus: ---- --")
if cpu.STATE_CB:
print("- $%02X! M%u" % (cpu.IR, cpu.STATE_IDX - 1))
else:
print("- $%02X M%u" % (cpu.IR, cpu.STATE_IDX - 1))
def DIV_update(new, old):
if tmpio[0x07] & 4:
bit = 1 << (7, 1, 3, 5)[tmpio[0x07]&3]
if not (new & bit) and not not (old & bit):
val = tmpio[0x05]
if val == 0xFF:
val = tmpio[0x06]
cpu.IRQ_SetM(1 << 2)
else:
val += 1
tmpio[0x05] = val
from msvcrt import kbhit
while True:
if cpu.reg.PC == 0x101:
#pause = True
pass
if count:
count -= 1
else:
count = 1048
window.Invalidate()
window.Update()
if not Window.LoopOnce():
break
if kbhit():
pause = True
if pause:
print("! Break !")
while True:
if autodump:
autodump = False
dumpstate()
line = input()
if line == "c":
pause = False
break
elif line == "i":
import code
code.InteractiveConsole(locals=locals()).interact()
elif line == "q":
WINAPI.PostQuitMessage(0)
count = 0
break
elif not line:
count = 0
autodump = True
break
else:
dumpstate()
cpu.StepPre()
cpubus.Reset()
DIV_update(cpu.DIV, cpu.DIV_PREV)
try:
status = cpu.Step()
except:
print("! Error at $%04X op $%02X M%u" % (cpu.reg.PC, cpu.IR, cpu.STATE_IDX - 1))
raise
if status != cpu.STATUS_RUNNING:
#pause = True
pass
#buslog("$%04X: %12s " % (cpu.reg.PC, CPU.ToString_Status(status)), end='')
busaccess = cpubus.Access
if busaccess:
assert busaccess & (BUS_ACCESS_READ | BUS_ACCESS_WRITE)
address = cpubus.Address
if busaccess & BUS_ACCESS_MEMREQ:
if busaccess & BUS_ACCESS_READ:
if address < 0x8000:
cart.OnRead(cpubus)
elif address < 0xA000:
ppu.TickT(3)
ppu.OnRead(cpubus)
ppu.TickT(1)
ppu_ticked = True
elif address < 0xC000:
cart.OnRead(cpubus)
else:
cpubus.SetData(tmpwram[address & 0x1FFF])
#buslog(("- Ext access [$%04X] --> $%02X" % (cpubus.Address, cpubus.Data)))
elif busaccess & BUS_ACCESS_WRITE:
#buslog(("- Ext access [$%04X] <-- $%02X" % (cpubus.Address, cpubus.Data)))
if address < 0x8000:
cart.OnWrite(cpubus)
elif address < 0xA000:
ppu.TickT(2)
ppu.OnWrite(cpubus)
ppu.TickT(2)
ppu_ticked = True
elif address < 0xC000:
cart.OnWrite(cpubus)
else:
tmpwram[address & 0x1FFF] = cpubus.Data
else:
if busaccess & BUS_ACCESS_READ:
if address >= 0xFF00:
if address == 0xFFFF:
cpubus.SetData(cpu.REG_IE)
elif address == 0xFF0F:
cpubus.SetData(cpu.REG_IF | 0xE0)
#pause = True
elif address == 0xFF04:
cpubus.SetData((cpu.DIV >> 6) & 0xFF)
elif address == 0xFF01 or address == 0xFF02:
pass
elif address == 0xFF00:
cpubus.SetData(tmpio[0])
elif address >= 0xFF40 and address < 0xFF50:
ppu.OnRead(cpubus)
else:
cpubus.SetData(tmpio[address & 0xFF])
elif bootmeme:
cpubus.SetData(bootmeme[address & 0xFF])
#buslog(("- Int access [$%04X] --> $%02X" % (cpubus.Address, cpubus.Data)))
elif busaccess & BUS_ACCESS_WRITE:
#buslog(("- Int access [$%04X] <-- $%02X" % (cpubus.Address, cpubus.Data)))
if address >= 0xFF00:
if address == 0xFFFF:
cpu.REG_IE = cpubus.Data # type: ignore
elif address == 0xFF0F:
cpu.IRQ_SetM(cpubus.Data & 0x1F)
cpu.IRQ_Clear(~cpubus.Data & 0x1F)
#pause = True
elif address == 0xFF04:
DIV_update(0, cpu.DIV)
cpu.DIV = 0
elif address >= 0xFF40 and address < 0xFF50:
ppu.OnWrite(cpubus)
elif address == 0xFF00:
tmpio[0] = cpubus.Data
update_JOYP()
else:
tmpio[address & 0xFF] = cpubus.Data
if address == 0xFF01:
sys.stdout.write(chr(cpubus.Data))
sys.stdout.flush()
elif address == 0xFF50:
cpu.ProtectOverlay = False
bootmeme = None
pass
elif cpubus.Address != 0xFFFF:
buslog("- Bus idle %04X" % cpubus.Address)
pass
else:
#buslog()
pass
if not ppu_ticked:
ppu.TickT(4)
else:
ppu_ticked = False
#TODO: inaccurate
cpu.IRQ_SetT(ppu.PendingIRQ(), 3)
if __name__ == '__main__':
main()