-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
140 lines (94 loc) · 4.71 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
from sys import exit as sysexit
from os import environ, getcwd
from threading import Thread
from tkinter import Tk, filedialog
import pygame
import chip8
CHIP8 = chip8.CPU()
pygame.init()
root = Tk(); root.withdraw()
environ['SDL_VIDEO_CENTERED'] = '1'
width, height = 1280, 720
scaleMult = 12
screen = pygame.display.set_mode((width, height), pygame.HWSURFACE)
pygame.display.set_caption("CHIP-8")
clock = pygame.time.Clock()
emulatorClock = pygame.time.Clock()
emulationSurface = pygame.Surface((64*scaleMult, 32*scaleMult), pygame.HWSURFACE)
black, white = (0,0,0), (255,255,255)
bgcolor = (57,68,82)
mfont = pygame.font.SysFont("Calibri", 24)
keyReference = [pygame.K_x,
pygame.K_1, pygame.K_2, pygame.K_3, pygame.K_q, pygame.K_w, pygame.K_e, pygame.K_a, pygame.K_s, pygame.K_d, pygame.K_z, pygame.K_c, pygame.K_4, pygame.K_r, pygame.K_f, pygame.K_v]
UIbuttonsReference = [( pygame.Rect(50,450,120,50), (mfont.render("Open ROM", True, white, black), (55, 465)) ), ( pygame.Rect(190,450,120,50), (mfont.render("Reset ROM", True, white, black), (195, 465)) )]
keyButtonsReference = [( pygame.Rect(50,550,25,25), "1" ), ( pygame.Rect(80,550,25,25), "2" ), ( pygame.Rect(110,550,25,25), "3" ), ( pygame.Rect(140,550,25,25), "C" ),
( pygame.Rect(50,580,25,25), "4" ), ( pygame.Rect(80,580,25,25), "5" ), ( pygame.Rect(110,580,25,25), "6" ), ( pygame.Rect(140,580,25,25), "D" ),
( pygame.Rect(50,610,25,25), "7" ), ( pygame.Rect(80,610,25,25), "8" ), ( pygame.Rect(110,610,25,25), "9" ), ( pygame.Rect(140,610,25,25), "E" ),
( pygame.Rect(50,640,25,25), "A" ), ( pygame.Rect(80,640,25,25), "0" ), ( pygame.Rect(110,640,25,25), "B" ), ( pygame.Rect(140,640,25,25), "F" )]
def drawDebug():
s = pygame.Surface((300, 600), pygame.HWSURFACE)
s.fill(bgcolor)
s.blit(mfont.render(( "PC: " + "x" + hex(CHIP8.pc)[2::].upper() ), True, white, bgcolor), (10,0))
s.blit(mfont.render(( "Opcode: " + "x" + hex(CHIP8.opcode)[2::].upper() ), True, white, bgcolor), (10,20))
s.blit(mfont.render(( "I: " + "x" + hex(CHIP8.I)[2::].upper() ), True, white, bgcolor), (10,40))
for i in range(16):
s.blit(mfont.render( ("V" + hex(i)[-1].upper() + ": " + "x" + hex(CHIP8.V[i])[2::].upper() ), True, white, bgcolor), (10 + 120*(i//8), 80 + (i % 8)*20))
return s
def drawHandleUI():
if instructionAdvance:
screen.blit(mfont.render("Paused", True, (240, 15, 15), bgcolor), (50, 20))
for b in UIbuttonsReference:
pygame.draw.rect(screen, black, b[0])
if b[1]:
screen.blit(b[1][0], b[1][1])
for mb in keyButtonsReference:
tempbgcol = black if CHIP8.key[int(mb[1], 16)] == 0 else (80,80,80)
pygame.draw.rect(screen, tempbgcol, mb[0])
screen.blit(mfont.render(mb[1], True, white, tempbgcol), (mb[0].x + 6, mb[0].y + 1))
instructionAdvance = False
resetROM = False
def runEmulator():
global resetROM, instructionAdvance
while True:
if not instructionAdvance:
CHIP8.emulationCycle()
if resetROM:
CHIP8.reInit()
CHIP8.loadGame(ROMname)
resetROM = False
emulatorClock.tick(300)
ROMname = getcwd() + "\\roms\\" + "TETRIS"
CHIP8.loadGame(ROMname)
emulationThread = Thread(target=runEmulator)
emulationThread.start()
while True:
activekeys = pygame.key.get_pressed()
mousex, mousey = pygame.mouse.get_pos()
for event in pygame.event.get():
if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
pygame.quit()
emulationThread.join()
sysexit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
CHIP8.emulationCycle()
elif event.key == pygame.K_p:
instructionAdvance = not instructionAdvance
elif event.type == pygame.MOUSEBUTTONDOWN:
if UIbuttonsReference[0][0].collidepoint(mousex, mousey):
ROMname = filedialog.askopenfile().name
resetROM = True
elif UIbuttonsReference[1][0].collidepoint(mousex, mousey):
resetROM = True
for i in range(16):
CHIP8.key[i] = activekeys[keyReference[i]]
screen.fill(bgcolor)
emulationSurface.fill((0,0,0))
for pixelY in range(32):
for pixelX in range(64):
pygame.draw.rect(emulationSurface, (black if CHIP8.gfx[(pixelY * 64) + pixelX] == 0 else white), pygame.Rect(pixelX*scaleMult, pixelY*scaleMult, scaleMult, scaleMult))
screen.blit(emulationSurface, (50,50))
screen.blit(drawDebug(), (830, 50))
drawHandleUI()
pygame.display.update()
clock.tick(60)