-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
87 lines (71 loc) · 1.83 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
import pyglet
import sys
import time
import multiprocessing
from hack import Hack
from utility import int_to_16_bit
window = pyglet.window.Window(512, 256)
def worker(cpu):
while True:
cpu.execute()
def update(dt):
while not from_cpu.empty():
i, val = from_cpu.get()
ram_list[i] = val
@window.event
def on_key_press(symbol, modifiers):
to_cpu.put(-1)
@window.event
def on_key_release(symbol, modifiers):
to_cpu.put(0)
@window.event
def on_draw():
batch = pyglet.graphics.Batch()
window.clear()
for x, v in enumerate(ram_list):
bin_val = int_to_16_bit(v)
pixel_count = 0
pixels = []
row = x // 32
row = 255 - row
col_offset = (x % 32) * 16
for i, b in enumerate(bin_val):
if b == '1':
pixels.extend([col_offset + i, row])
pixel_count += 1
if pixel_count:
batch.add(
pixel_count,
pyglet.gl.GL_POINTS,
None,
('v2i', pixels),
)
batch.draw()
@window.event
def on_close():
cpu_worker.terminate()
from_cpu.close()
to_cpu.close()
if __name__ == '__main__':
args = sys.argv[1:]
if not args:
print('Error: Rom file required')
sys.exit(1)
global cpu_worker, from_cpu, to_cpu, ram_list
ram_list = [0] * 8192
rom_file = args[0]
with open(rom_file, 'r+') as rm:
data = rm.read()
data = data.split('\n')
if '' in data:
data = []
from_cpu = multiprocessing.Queue()
to_cpu = multiprocessing.Queue()
cpu = Hack(data, fromq=to_cpu, toq=from_cpu)
cpu_worker = multiprocessing.Process(
target=worker,
args=(cpu,)
)
cpu_worker.start()
pyglet.clock.schedule_interval(update, 0.01)
pyglet.app.run()