-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
161 lines (150 loc) · 5.36 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
import cpu
import pygame
import sys
import time
processor = cpu.CPU()
processor.loadROM('roms/INVADERS')
scale = 10
pygame.init()
size = 64 * scale, 32 * scale
black = 0, 0, 0
surface = pygame.display.set_mode(size)
pygame.display.set_caption("Chip-8")
surface.fill(black)
pxArray = pygame.PixelArray(surface)
def videoRefresh(pxArray, cpu, scale):
array = pxArray
for i in range(32):
for j in range(8):
v_ram = cpu.memory[0xF00 + i * 8 + j]
for k in range(0, 8):
x = j * 8 + 7 - k
y = i
if (v_ram & 0x01) == 1:
array[(scale * x):(scale * (x + 1)), (scale * y):(scale * (y + 1))] = (255, 255, 255)
else:
array[scale * x:scale * (x + 1), scale * y: scale * (y + 1)] = (0, 0, 0)
v_ram >>= 1
tick = time.time()
run_tick = time.time()
# test()
#
# processor.RUN(200)
# print(processor.PC)
# exit(0)
# processor.RUN(186, False)
count = 0
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
processor.keyChanged = True
if event.key == pygame.K_0:
processor.lastKEY = 0
processor.keyboard[0] = 1
if event.key == pygame.K_1:
processor.lastKEY = 1
processor.keyboard[1] = 1
if event.key == pygame.K_2:
processor.lastKEY = 2
processor.keyboard[2] = 1
if event.key == pygame.K_3:
processor.lastKEY = 3
processor.keyboard[3] = 1
if event.key == pygame.K_4:
processor.lastKEY = 4
processor.keyboard[4] = 1
if event.key == pygame.K_5:
processor.lastKEY = 5
processor.keyboard[5] = 1
if event.key == pygame.K_6:
processor.lastKEY = 6
processor.keyboard[6] = 1
if event.key == pygame.K_7:
processor.lastKEY = 7
processor.keyboard[7] = 1
if event.key == pygame.K_8:
processor.lastKEY = 8
processor.keyboard[8] = 1
if event.key == pygame.K_9:
processor.lastKEY = 9
processor.keyboard[9] = 1
if event.key == pygame.K_a:
processor.lastKEY = 0xA
processor.keyboard[0xA] = 1
if event.key == pygame.K_b:
processor.lastKEY = 0xB
processor.keyboard[0xB] = 1
if event.key == pygame.K_c:
processor.lastKEY = 0xC
processor.keyboard[0xC] = 1
if event.key == pygame.K_d:
processor.lastKEY = 0xD
processor.keyboard[0xD] = 1
if event.key == pygame.K_e:
processor.lastKEY = 0xE
processor.keyboard[0xE] = 1
if event.key == pygame.K_f:
processor.lastKEY = 0xF
processor.keyboard[0xF] = 1
if event.type == pygame.KEYUP:
if event.key == pygame.K_0:
processor.lastKEY = 0
processor.keyboard[0] = 0
if event.key == pygame.K_1:
processor.lastKEY = 1
processor.keyboard[1] = 0
if event.key == pygame.K_2:
processor.lastKEY = 2
processor.keyboard[2] = 0
if event.key == pygame.K_3:
processor.lastKEY = 3
processor.keyboard[3] = 0
if event.key == pygame.K_4:
processor.lastKEY = 4
processor.keyboard[4] = 0
if event.key == pygame.K_5:
processor.lastKEY = 5
processor.keyboard[5] = 0
if event.key == pygame.K_6:
processor.lastKEY = 6
processor.keyboard[6] = 0
if event.key == pygame.K_7:
processor.lastKEY = 7
processor.keyboard[7] = 0
if event.key == pygame.K_8:
processor.lastKEY = 8
processor.keyboard[8] = 0
if event.key == pygame.K_9:
processor.lastKEY = 9
processor.keyboard[9] = 0
if event.key == pygame.K_a:
processor.lastKEY = 0xA
processor.keyboard[0xA] = 0
if event.key == pygame.K_b:
processor.lastKEY = 0xB
processor.keyboard[0xB] = 0
if event.key == pygame.K_c:
processor.lastKEY = 0xC
processor.keyboard[0xC] = 0
if event.key == pygame.K_d:
processor.lastKEY = 0xD
processor.keyboard[0xD] = 0
if event.key == pygame.K_e:
processor.lastKEY = 0xE
processor.keyboard[0xE] = 0
if event.key == pygame.K_f:
processor.lastKEY = 0xF
processor.keyboard[0xF] = 0
run_time = time.time() - run_tick
if run_time // (1.0 / 800) > 0:
processor.RUN()
run_tick = time.time()
past = time.time() - tick
add_timer = past // (1.0 / 60)
if add_timer > 0:
videoRefresh(pxArray, processor, scale)
processor.addTimer(int(add_timer))
pygame.display.update()
tick = time.time()