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
177
178
179
180
181
182
183
184
185
186
187
188
189
|
import sys
from string import Template
TWOWAY = {
"ir": "001",
"pm": "010",
"pc": "011",
"hr": "101",
"grx": "110",
}
# to bus: source
TB = TWOWAY | {
"ar": "100",
# "styrord": "111",
}
# from bus: destination
FB = TWOWAY | {
"asr": "111"
}
def compile(lines):
addr = 0
labels = {}
compiled = []
todo = []
for line in lines:
if not line:
# empty line, ignore
continue
if line.startswith(";"):
# comment
continue
if line.endswith(":"):
# label
labels[line.split(":")[0]] = addr
continue
if line.startswith("!"):
# inline
compiled.append(line[1:])
addr += 1
continue
for inst in line.split(", "):
alu = "0000"
tb = "000"
fb = "000"
s = "0"
p = "0"
lc = "00"
seq = "0000"
myadr = "0000000"
if "->" in inst:
# move a value
fr, to = inst.split("->")
# via bus
if fr in TB:
tb = TB[fr]
if to in FB:
fb = FB[to]
if "gr" in fr and len(fr) == 3:
tb = TB["grx"]
if "gr" in to and len(to) == 3:
fb = FB["grx"]
if to == "lc":
lc = "10"
# load alu
if to == "ar":
alu = "0001"
# alu addition
if "+" in fr:
if "+'" in fr:
# ignore flags
other = fr.split("+'")[1]
alu = "1000"
else:
other = fr.split("+")[1]
alu = "0100"
tb = TB[other]
# alu subtraction
if "-" in fr:
other = fr.split("-")[1]
tb = TB[other]
alu = "0101"
if "&" in fr:
other = fr.split("&")[1]
tb = TB[other]
alu = "0110"
if inst == "pc++":
p = "1"
if inst == "k1->upc":
seq = "0001"
if inst == "k2->upc":
seq = "0010"
if inst == "0->upc":
seq = "0011"
if inst == "halt":
seq = "1111"
if inst == "lc--":
lc = "01"
if inst == "lsr":
alu = "1101"
if "?" in inst:
cond, to = inst.split("? ")
if cond == "l=1":
seq = "1100"
else:
assert False
myadr = f"<{to}>"
if inst.startswith("b "):
seq = "0110" #TODO 0101?
myadr = "<{}>".format(inst.split("b ")[1])
compiled.append((inst, (alu, tb, fb, s, p, lc, seq, myadr)))
addr += 1
linked = []
for addr, line in enumerate(compiled):
if type(line) is str:
linked.append((addr, line))
continue
inst, (alu, tb, fb, s, p, lc, seq, myadr) = line
if myadr != "0000000":
for label, label_line in labels.items():
myadr = myadr.replace(f"<{label}>", str(bin(label_line)[2:]).zfill(7))
first = alu[0]
rest = alu[1:] + tb + fb + s + p + lc + seq + myadr
hs = "".join([f"{int(first):01x}"] + [f"{int(rest[i:i+4], 2):01x}" for i in range(0, len(rest), 4)])
linked.append((addr, hs))
if hs == "0000000":
todo.append(inst)
return ["{:02x}: {}".format(addr, line) for addr, line in linked], labels
"""
todo:
"""
print("todo")
print("\n".join(todo))
def write(prog, labels):
#with open("template.mia", "r") as f:
# template = Template(f.read())
ucode_pad = 0x80 - len(prog)
ucode = "\n".join(prog + [f"{len(prog)+n:02x}: 0000000" for n in range(ucode_pad)])
insts = [
"load",
"store",
"add",
"sub",
"and",
"lsr",
"bra",
"bne",
"halt",
"cmp",
"bge",
"beq",
]
inst_locations = "\n".join([f"{i:02x}: {labels[inst]:02x}" for i, inst in enumerate(insts)])
print("MyM:")
print(ucode)
print()
print("K1:")
print(inst_locations)
#print(template.safe_substitute(pm))
if __name__ == "__main__":
write(*compile([line.strip() for line in sys.stdin]))
|