-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path8080.py
65 lines (51 loc) · 1.78 KB
/
8080.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
from state import State
from instructions import instructions
class i8080(object):
def __init__(self):
self._state = State()
def load(self, filename):
with open(filename, "rb") as f:
data = f.read()
self._state.memory().write(0, list(ord(x) for x in data))
def state(self):
return self._state
def next_instruction(self):
opcode = self._state.memory().read_byte(self._state.registers().ip())
try:
try:
instruction = instructions[opcode]
except:
raise NotImplementedError("Instruction {} not implemented".format(hex(opcode)))
self._state.registers().increment_ip(instruction["length"])
instruction['operation'](self._state)
return True
except NotImplementedError as e:
print "n: {}".format(self.c)
self._state.dump_state()
raw_input()
raise NotImplementedError(e)
return False
def run(self):
self.c = 0
while True:
#raw_input()
self.process_interrupt()
self.c += 1
if not self.next_instruction():
break
if self.c == 1001:
print self.state().cycle_count
raw_input()
def process_interrupt(self):
if self.state().cycle_count > 16667:
self.state().cycle_count -= 16667
if self.state().last_interrupt == 0x10:
self.state().last_interrupt = 0x08
self.state().draw_screen()
else:
self.state().last_interrupt = 0x10
if self.state().IE:
self.state().cause_interrupt()
machine = i8080()
machine.load("invaders")
machine.run()