-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchip8.py
More file actions
176 lines (154 loc) · 5.65 KB
/
chip8.py
File metadata and controls
176 lines (154 loc) · 5.65 KB
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
#!/usr/bin/env python3
import sys
import os
import pyaudio
import wave
import threading
import time
import argparse
from modules.virtual_chip8 import Virtual_chip8
from PyQt5.QtWidgets import QApplication
from modules.gui import Gui
def main():
parser = create_parser()
parsed_args = parser.parse_args(sys.argv[1:])
start(parsed_args)
def create_parser():
parser = argparse.ArgumentParser(
description='Graphic version of chip8\n\
The list of keys:\n\
\r-d\n\
\r-r\n\
\r-m\n\
\r-s\n\
\r-ws')
parser.add_argument('path', type=str,
help='path to file')
parser.add_argument('-d', '--debug',
help='print command and counters info',
action='store_true')
parser.add_argument('-r', '--registers',
help='print registers info',
action='store_true')
parser.add_argument('-m', '--memory',
help='print memory info',
action='store_true')
parser.add_argument('-s', '--speed',
type=int,
help='change speed of execution in Hz:\
if value = 0 - without delay\
default = 1000Hz',
default=1000)
parser.add_argument('-ws', '--without_sound',
help='off the sound in games',
action='store_true')
return parser
def start(parsed_args):
vc8 = Virtual_chip8()
try:
with open(os.path.join(parsed_args.path), 'rb') as file:
load_memory(file, vc8)
except IOError:
sys.stderr.write('Not correct path or file is not supported {0}\n\
\rPlease check the correctness of the path\n\
\ryou entered, the presence of the file\n\
\rin the destination folder and the type\n\
\rof this file\n'
.format(parsed_args.path))
sys.exit(1)
app = QApplication(sys.argv)
gui = Gui(vc8)
thread_delay_timer = threading.Thread(target=tick_delay_timer,
args=(vc8,))
thread_execute = threading.Thread(target=execute,
args=(vc8, parsed_args))
thread_execute.start()
thread_delay_timer.start()
if not parsed_args.without_sound:
thread_sound_timer = threading.Thread(target=tick_sound_timer,
args=(vc8,))
thread_sound_timer.start()
sys.exit(app.exec_())
def execute(vc8, parsed_args):
wait_to_key_command = ('f', '0a')
prev_pc = vc8.pc
while vc8.pc < vc8.memory_limit and vc8.execution:
command = get_command(vc8)
tracing(vc8, command, parsed_args)
have_pressed_key = False
while ((command[2], command[4:]) == wait_to_key_command and
not have_pressed_key):
time.sleep(1 / vc8.speed)
for key in vc8.pressed_keys:
if vc8.pressed_keys[key]:
have_pressed_key = True
break
vc8.compare_and_execute(command)
if vc8.pc == prev_pc:
print('GAME OVER!')
time.sleep(5)
vc8.execution = False
sys.exit()
else:
prev_pc = vc8.pc
if parsed_args.speed > 0:
time.sleep(1 / parsed_args.speed)
def load_memory(file, vc8):
counter = 0
temp_num = ''
for line in file.readlines():
for num in line:
temp_num = hex(num)
if len(temp_num) < 4:
temp_num = '0x0' + temp_num[2]
vc8.memory[counter + vc8.shift] = temp_num
counter += 1
def get_command(vc8):
command = vc8.memory[vc8.pc] + vc8.memory[vc8.pc + 1]
command = command.replace('0x', '')
command = '0x' + command
return command
def tracing(vc8, command, parsed_args):
if parsed_args.debug:
print("PC: {0}, I: {1}, delay: {2}, sound: {3} command: {4}"
.format(vc8.pc, vc8.i, vc8.delay_timer, vc8.sound_timer,
command))
if parsed_args.registers:
print(vc8.registers)
if parsed_args.memory:
print(vc8.memory)
def tick_delay_timer(vc8):
while vc8.execution:
if vc8.delay_timer > 0:
vc8.delay_timer -= 1
time.sleep(1 / vc8.speed)
sys.exit()
def tick_sound_timer(vc8):
path = '.\\sound\\beep.wav'
try:
wf = wave.open(os.path.join(path), 'rb')
pa = pyaudio.PyAudio()
stream = pa.open(format=pa.get_format_from_width(wf.getsampwidth()),
channels=wf.getnchannels(),
rate=wf.getframerate(),
output=True)
data = wf.readframes(1024)
while vc8.execution:
if vc8.sound_timer > 0:
stream.write(data)
vc8.sound_timer -= 1
time.sleep(1 / vc8.speed)
stream.stop_stream()
stream.close()
pa.terminate()
sys.exit()
except IOError:
sys.stderr.write('Not correct path or file is not supported {0}\n\
\rPlease check the correctness of the path\n\
\ryou entered, the presence of the file\n\
\rin the destination folder and the type\n\
\rof this file\n'
.format(path))
sys.exit(1)
if __name__ == "__main__":
main()