-
Notifications
You must be signed in to change notification settings - Fork 14
/
main.py
56 lines (41 loc) · 1.28 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
import sys
from intel4004_emu import translator
from intel4004_emu import executor
from intel4004_emu import consolex
class EnhancedExecutor(executor.Executor, consolex.Consolex):
def printRegs(self):
print(' '.join([str(r) for r in self.regs]))
print("acc=%d, cy=%d, ip=%d, dp=%d, cycles=%d" % (self.acc, self.cy, self.ip, self.dp, self.cycles))
def printMemory(self, rows, cols):
for row in range(rows):
for col in range(cols):
print "%X" % self.memory[row * cols + col],
print
def c_3ff(self):
self.printRegs()
def c_3fe(self):
self.printMemory(8, 32)
def c_3fd(self):
self.printMemory(4, 16)
def loadSource():
if len(sys.argv) < 2:
raise Exception('Source file should be specified!')
fileName = sys.argv[1]
f = open(fileName)
lines = f.readlines()
f.close()
return lines
def fetchState(cpu):
for i in range(2, len(sys.argv)):
cpu.regs[i - 2] = int(sys.argv[i])
def main():
try:
src = loadSource()
prg = translator.translate(src)
cpu = EnhancedExecutor()
fetchState(cpu)
cpu.run(prg)
except Exception as e:
sys.stderr.write("Error: %s\n" % e)
if __name__ == '__main__':
main()