-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathread_gb.py
137 lines (110 loc) · 3.99 KB
/
read_gb.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
#import threading
import time
import pygame
from threading import Thread
from graphics import PPU_THREAD
from interrupts import handle_interrupts, handle_timer
from memory_control import readMem
from opcodes import extra_opcodes, opcodes
from cpu import handle_opcode
from events import handle_events
log = 0
ppu = 1
A, B, C, D, E, H, L = 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
F = [0, 0, 0, 0, 0, 0, 0, 0]
SP = 0x0000
MEMORY = [0]*0x100
IME = False
HALT = 0
MAXCYCLE = 69905
if log:
disassembled_file = open("disassembled.txt", "w")
def runCode(pointer):
l=""
global A, B, C, D, E, F, H, L, SP, MEMORY, IME, HALT
#pygame
pygame.init()
pygame.display.set_caption("Gameboy Emulator")
#resizable
screen = pygame.display.set_mode((160*4, 144*4), pygame.RESIZABLE)
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((0, 0, 0))
# keys
keys={"LEFT": 1, "RIGHT": 1, "UP": 1, "DOWN": 1, "A": 1, "B": 1, "SELECT": 1, "START": 1}
# get current time
dt = time.time()
counter = 0
cycles = 0
boot = True
# spawn ppu thread
stop=False
ppu_thread = Thread(target=PPU_THREAD, args=(lambda: MEMORY,screen,lambda: stop))
ppu_thread.start()
while True:
keys,stop = handle_events(keys, MEMORY)
if stop==True:
ppu_thread.join()
pygame.quit()
if log:
disassembled_file.write(l)
disassembled_file.close()
return True
if time.time() - dt > 1.0/60.0:
if MEMORY[0xFF40] & 0x80:
counter = 0
MEMORY[0xFF44] = 0x90
MEMORY[0xFF0F] |= 0x01
dt = time.time()
cycles = 0
else:
if MEMORY[0xFF40] & 0x80:
counter += 1
if counter >= 0x90:
MEMORY[0xFF44] += 1
if MEMORY[0xFF44] >= 153:
MEMORY[0xFF44] = 0
if cycles >= MAXCYCLE:
continue
if len(MEMORY) <= pointer:
print(f"End of file reached, pointer: {pointer}")
break
code = readMem(MEMORY, pointer)
if not HALT:
ctime=time.time()
if log:
l+=f"PC: {pointer:04x}\t{opcodes[code]:<8} {extra_opcodes[readMem(MEMORY,pointer+1)] if opcodes[code]=='PREFCB' else '':<8}\tCode: {code:02x}\tPC+1: {readMem(MEMORY,pointer+1):02x}\tPC+2: {readMem(MEMORY,pointer+2):02x}"
A, B, C, D, E, H, L, F, SP, IME, MEMORY, pointer, cycle, HALT = handle_opcode(code,A,B,C,D,E,H,L,F,SP,IME,MEMORY,pointer,keys)
if log:
l+=f"\tAF: {A:02x}{int(''.join([str(x) for x in F[::-1]]),2):02x}\tBC: {B:02x}{C:02x}\tDE: {D:02x}{E:02x}\tHL: {H:02x}{L:02x}\tSP: {SP:04x}\tF: {F}\tIME: {IME}\tSP: {SP:04x}\tTime taken: {time.time()-ctime}\n"
if len(l)>10000:
disassembled_file.write(l)
l=""
else:
#print("Halting!")
cycle = 1
cycles += cycle
handle_timer(cycle, MEMORY)
pointer, MEMORY, IME, SP, HALT = handle_interrupts(MEMORY, pointer, IME, SP, HALT)
# print(f"F: {F}")
if pointer == 0x100 and boot:
boot = False
# copy the interupt table to the beginning of the memory
MEMORY[0:0x100] = game[0:0x100]
f = open("DMG_ROM.bin", "rb")
content = f.read()
f.close()
# print(content)
MEMORY[0x0:0x100] = content
#f = open("Dr. Mario (World).gb", "rb")
with open("config.txt", "r") as f:
config=f.read().splitlines()
# NOTE: should probably do this differently
f=open(config[0].split("=")[1], "rb")
game = f.read()
f.close()
MEMORY += game[0x100:0x8000]
if len(MEMORY) < 0x10000:
MEMORY += [0]*(0x10000-len(MEMORY))
if __name__=="__main__":
runCode(0x0)