-
Notifications
You must be signed in to change notification settings - Fork 1
/
MCS4.py
78 lines (64 loc) · 2.16 KB
/
MCS4.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
import sys
import chips.i4001 as i4001, chips.i4002 as i4002, chips.i4004 as i4004
import chips.clock as clock
from hdl import *
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("ROM_FILE")
parser.add_argument("-d", "--debug", help="output debug information",
action="store_true")
parser.add_argument("-o", "--optimize", help="optimize for speed",
action="store_true")
parser.add_argument("-kb", "--key_buffer", help="initialize key buffer")
class MCS4:
def __init__(self):
global parser
self.ram_chip = 0 # This represents the currently active RAM chip
self.clock = clock.clock()
self.data = bus()
self.cm_rom = pwire()
self.cm_ram = pbus()
self.test = pwire()
self.CPU = i4004.i4004(self.clock, self.data, self.cm_rom, self.cm_ram, self.test)
self.PROM = []
self.RAM = [None, [], [], None, [], None, None, None, []]
self.SR = []
self.args = parser.parse_args()
def addROM(self, rom):
self.PROM.append(rom)
def addRAM(self, bank, ram):
idx = 1 << bank
self.RAM[idx].append(ram)
def addSR(self, sr):
self.SR.append(sr)
def program(self):
fh = open(self.args.ROM_FILE, 'r')
if self.PROM[0].program(fh) == 0:
sys.exit("ERROR: No instructions loaded!")
elif len(self.PROM) > 1:
for p in self.PROM[1:]:
p.program(fh)
# TODO: Make sure stdin is empty
fh.close()
def run(self, callback=None):
dump = self.args.debug
nb = 0
while True:
if callback is not None:
callback(nb)
for i in range(8):
self.clock.tick()
if i == 4 and dump:
self.dump(nb)
nb += 1
def dump(self, nb):
self.CPU.dump(nb)
self.RAM[1][0].dump()
self.RAM[1][1].dump()
for r in self.PROM:
r.dump()
print()
if len(self.SR):
for s in self.SR:
s.dump()
print()