-
Notifications
You must be signed in to change notification settings - Fork 0
/
as.py
executable file
·377 lines (318 loc) · 10.3 KB
/
as.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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#!/usr/bin/env python3
import sys
import struct
instructions = [
# index register instructions
('0011PPP0', 'FIN', ['P']),
('0110RRRR', 'INC', ['R']),
('1000RRRR', 'ADD', ['R']),
('1001RRRR', 'SUB', ['R']),
('1010RRRR', 'LD', ['R']),
('1011RRRR', 'XCH', ['R']),
# accumulator instructions
('11110000', 'CLB'),
('11110001', 'CLC'),
('11110010', 'IAC'),
('11110011', 'CMC'),
('11110100', 'CMA'),
('11110101', 'RAL'),
('11110110', 'RAR'),
('11110111', 'TCC'),
('11111000', 'DAC'),
('11111001', 'TCS'),
('11111010', 'STC'),
('11111011', 'DAA'),
('11111100', 'KBP'),
#('11111101', '')
#('11111110', '')
#('11111111', '')
# immediate instructions
('0010PPP0', 'FIM', ['P', 'D'], 'DDDDDDDD'),
('1101DDDD', 'LDM', ['D']),
# control instructions
('0100AAAA', 'JUN', ['A'], 'AAAAAAAA'),
('0011RRR1', 'JIN', ['R']),
('0001CCCC', 'JCN', ['C', 'A'], 'AAAAAAAA'),
('0111RRRR', 'ISZ', ['R', 'A'], 'AAAAAAAA'),
# subroutine linkage instructions
('0101AAAA', 'JMS', ['A'], 'AAAAAAAA'),
('1100DDDD', 'BBL', ['D']),
# nop
('00000000', 'NOP'),
# memory selection
('11111101', 'DCL'),
('0010PPP1', 'SRC', ['P']),
# i/o and RAM instructions
('11100000', 'WRM'),
('11100001', 'WMP'),
('11100010', 'WRR'),
('11100011', 'WPM'),
('11100100', 'WR0'),
('11100101', 'WR1'),
('11100110', 'WR2'),
('11100111', 'WR3'),
('11101000', 'SBM'),
('11101001', 'RDM'),
('11101010', 'RDR'),
('11101011', 'ADM'),
('11101100', 'RD0'),
('11101101', 'RD1'),
('11101110', 'RD2'),
('11101111', 'RD3'),
]
labels = {}
def str_to_mask(s, a):
mask = 0
base = 8
for i in range(8):
if s[7 - i] == a:
mask = mask | (1 << i)
base = min(base, i)
return mask, base
def desc_to_bin(desc, values={}):
fw_args = set()
sw_args = set()
inst_base = list(desc[0])
fw_str = desc[0]
relocs = []
# strip operand fields from instruction
# and determine operand ids referenced
# in the first word (in fw_args)
for i in range(len(fw_str)):
if fw_str[i] not in ['0', '1']:
inst_base[i] = '0'
fw_args.add(fw_str[i])
if len(desc) > 3:
# two-word instruction
sw_str = desc[3]
# determine operand ids referenced
# in the second word (in sw_args)
for i in range(len(sw_str)):
sw_args.add(sw_str[i])
sw_arg_layout = {}
fw_arg_layout = {}
# compute the mask and base for arguments
# in the second instruction word
for a in sw_args:
mask, base = str_to_mask(desc[3], a)
sw_arg_layout[a] = (mask, base)
# compute the mask and base for arguments
# in the first instruction word
for a in fw_args:
mask, base = str_to_mask(desc[0], a)
shift = 0
if a in sw_args:
# arguments that are split over the first
# and second instruction words have the
# low bits in the second word
shift = bin(sw_arg_layout[a][0]).count('1')
fw_arg_layout[a] = (mask, base, shift)
inst = int(''.join(inst_base), 2)
for a in fw_args:
mask, base, shift = fw_arg_layout[a]
print("fw arg {} {:08b} {}".format(a, mask, base))
#TODO: no default values
#value = values[a]
value = values.get(a, 0)
if isinstance(value, tuple) and (value[0] == 'UNKNOWN_LABEL'):
sym_name = value[1]
relocs.append([sym_name, 0, mask, base, shift])
else:
part = ((value >> shift) << base) & mask
print("part {:08b}".format(part))
inst = inst | part
if len(desc) > 3:
inst_sw = 0
# compute the second word for
# two word instructions
for a in sw_args:
mask, base = sw_arg_layout[a]
print("sw arg {} {:08b} {}".format(a, mask, base))
#TODO: no default values
#values = values[a]
value = values.get(a, 0)
if isinstance(value, tuple) and (value[0] == 'UNKNOWN_LABEL'):
sym_name = value[1]
relocs.append([sym_name, 1, mask, base, 0])
else:
part = (value << base) & mask
print("part {:08b}".format(part))
inst_sw = inst_sw | part
print("inst: {:02x} {:02x} - {:08b} {:08b}".format(inst, inst_sw, inst, inst_sw))
if len(relocs) > 0:
print("(pending relocations)")
return (inst, inst_sw), relocs
else:
print("inst: {:02x} - {:08b}".format(inst, inst))
if len(relocs) > 0:
print("(pending relocations)")
return (inst,), relocs
def get_desc_and_operands(inst):
op = inst[0]
desc = find_instruction(op)
assert desc is not None, 'unknown instruction ' + op
operand_count = len(inst) - 1
expected = len(desc[2]) if len(desc) >= 3 else 0
assert operand_count == expected, inst
operand_map = {}
for i in range(operand_count):
value = inst[i+1]
name = desc[2][i]
operand_map[name] = value
return desc, operand_map
def inst_to_bin(inst):
desc, operands = get_desc_and_operands(inst)
return desc_to_bin(desc, operands)
def parse_value(f):
print("reading ", f)
if f[0:2] == '0x':
v = int(f[2:], 16)
print("hex -> {:02x} = {}".format(v,v))
elif f[-1] == 'P' and f[0].isdigit():
# register pair symbols
v = int(f[0])
assert 0 <= v and v <= 7
print("pair -> ", v)
elif f in labels:
v = labels[f]
print("label -> ", v)
else:
try:
v = int(f)
print("int -> ", v)
except ValueError:
print(f, " is not an int. interpreting it as a label.")
v = ('UNKNOWN_LABEL', f)
return v
def assemble_line(line, loc=0):
print("assembling line...")
line = line.strip()
inst_start = 0
inst_end = len(line)
if '/' in line:
inst_end = line.index('/')
comment = line[inst_end:]
print("comment ", comment)
if ',' in line[:inst_end]:
label_end = line[:inst_end].index(',')
label = line[:label_end]
inst_start = label_end + 1
print("label ", label)
labels[label] = loc
if '=' in line[:inst_end]:
sym_end = line[:inst_end].index('=')
sym = line[:sym_end].strip()
value = int(line[sym_end+1:])
if sym == '':
new_loc = value
delta = new_loc - loc
assert delta >= 0, "origin can only move forward"
words = (0,) * delta
return words, []
else:
print('symbol {} = {}'.format(sym, value))
labels[sym] = value
return (), []
inst = line[inst_start:inst_end].strip()
print("instruction ", inst)
fields = inst.split(' ')
opcode = fields[0]
operands = [parse_value(f) for f in fields[1:]]
print("'{}'".format(opcode))
if opcode:
if opcode[0].isdigit():
# constant data
assert len(operands) == 0
words = (parse_value(opcode),)
relocs = []
else:
words, relocs = inst_to_bin([opcode] + operands)
print(['{:02x}'.format(s) for s in words])
return words, relocs
else:
print("no opcode")
return (), []
def find_instruction(op):
for inst in instructions:
if inst[1] == op:
return inst
return None
def find_instruction_by_bin(inst):
for desc in instructions:
c = int(desc[0], 2)
op_mask = desc_to_op_mask(desc)
with open('out.bin', 'wb') as f:
for o in range(16):
inst = o << 4
f.write(struct.pack('<B', inst));
print("all instructions:")
for desc in instructions:
print(desc[0], desc[1])
desc_to_bin(desc)
print()
print("test")
assert desc_to_bin(find_instruction('FIM'), {'P': 0, 'D': 255}) == ((0x20, 0xff), [])
assert inst_to_bin(['FIM', 0, 255]) == ((0x20, 0xff), [])
assert inst_to_bin(['JUN', 0x3e0]) == ((0x43, 0xe0), [])
assert inst_to_bin(['ADD', 1]) == ((0x81,), [])
assert inst_to_bin(['LDM', 3]) == ((0xd3,), [])
assert inst_to_bin(['JUN', 0x362]) == ((0x43, 0x62), [])
assert inst_to_bin(['FIM', 0, 4]) == ((0x20, 0x04), [])
assert inst_to_bin(['JCN', 6, 0x302]) == ((0x16, 0x02), [])
print("passed")
assemble_line('FOO, JCN 6 0x302 / comment')
assemble_line('FOO, 16 / comment')
print("demo")
desc_to_bin(find_instruction('JUN'), {'A': 0x3e0})
desc_to_bin(find_instruction('ADD'), {'R': 1})
desc_to_bin(find_instruction('LDM'), {'D': 3})
desc_to_bin(find_instruction('JUN'), {'A': 0x362})
desc_to_bin(find_instruction('FIM'), {'P': 0, 'D': 4})
desc_to_bin(find_instruction('JUN'), {'A': 0x370})
if len(sys.argv) > 1:
out = []
loc = 0
labels = {}
relocs = []
print("assembling ", sys.argv[1])
with open(sys.argv[1], 'r') as f_in:
for line in f_in.readlines():
print(line.strip())
words, line_relocs = assemble_line(line, loc)
if len(line_relocs) > 0:
print("reading {} relocs at {}".format(len(line_relocs), loc))
for r in line_relocs:
print(r)
r[1] += loc
print(r)
relocs.append(r)
for w in words:
out.append(w)
loc += 1
print(labels)
for i in range(len(out)):
w = out[i]
print("{:04x}: {:02x}".format(i, w))
print()
if len(relocs) > 0:
print('processing relocations for forward references')
for r in relocs:
symname, loc, mask, base, shift = r
if symname in labels:
value = labels[symname]
else:
print("symbol {} used in forward reference but not defined".format(symname))
assert False
word = out[loc]
part = ((value >> shift) << base) & mask
out[loc] = word | part
for i in range(len(out)):
w = out[i]
print("{:04x}: {:02x}".format(i, w))
if len(sys.argv) > 2:
output_name = sys.argv[2]
else:
output_name = sys.argv[1][:sys.argv[1].rfind(".")] + ".bin"
with open(output_name, 'wb') as f_out:
print("writing {} bytes to {}".format(len(out), output_name))
f_out.write(bytes(out))