-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMNEMBLER.py
More file actions
681 lines (560 loc) · 19.4 KB
/
MNEMBLER.py
File metadata and controls
681 lines (560 loc) · 19.4 KB
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
import struct
import re
import json
import traceback
import sys
from SELOP import *
from util import *
MODE_RELATIVE = True
MODE_ABSOLUTE = False
SEL_INT_MAX = 0xffff
SYMBOLS = {}
EXTERNAL_SYMBOLS = {}
CONSTANTS = {}
EXPORTS = {}
MACROS = {}
DIRECT_LOAD = 0
MREF_LOAD = 1
SUB_LOAD = 2
SPECIAL_LOAD = 3
NOT_FORBIDDEN_CHARS = [ord(x) for x in list("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\r\n\b\x08[\\]^_ !\"#$%&'()*+,-./:;<=>?")]
FORBIDDEN_CHARS = []
for c in range(0,0xff):
if c not in NOT_FORBIDDEN_CHARS:
FORBIDDEN_CHARS.append(c)
def decompose_asm(l):
l = l.replace("\n","")
l = list(l)
ismacroinst = False
if len(l):
if l[0] == "*":
return (None,ismacroinst,None,False,None,None)
if len(l) > 5:
if l[4] == "M":
ismacroinst = True
l[4] = "\0"
if len(l) > 10:
l[9] = "\0"
in_quotes = False
qc = 0
for i in range(0, len(l)): #this matches the manual much better, doesnt actually mention quotes
if in_quotes:
if l[i] == "'":
qc -= 1
if qc == 0:
in_quotes = False
else:
if l[i] == "'":
qc += 1
if qc == 2:
in_quotes = True
if in_quotes == 0 and l[i] == " " and (i > 13):
l[i] = "\0"
break
(label, op, addridx, comment) = (None,None,None,None)
chunkdat = [x for x in "".join(l).split("\00")]
if len(chunkdat) == 4:
(label, op, addridx, comment) = chunkdat
elif len(chunkdat) == 3:
(label, op, addridx) = chunkdat
elif len(chunkdat) == 2:
(label, op) = chunkdat
elif len(chunkdat) == 1:
(label,) = chunkdat
if label.strip() == '':
label = None
else:
label = label.strip()
if comment:
comment = comment.lstrip()
indirect_bit = False
if op != None:
op = op.strip()
if op.strip() != "":
if op[-1] == "*":
op = op[:-1] #indirect instruction
indirect_bit = True
if addridx:
addridx = addridx.strip()
return (label,ismacroinst,op, indirect_bit, addridx, comment)
def get_unique_label():
n = 0
s = "_%d" % n
while s in SYMBOLS: #hacky but closer to what the actuall assembler seemed to do
n = n+1
s = "_%d" % n
if n > 999:
raise ValueError
return s
def asm_pass_1(filename):
program_listing = []
cur_offset_from_org = 0
supress_output = False
in_macro_name = None
ll = load_file(filename)
lnum = 0
addr_mode = MODE_ABSOLUTE
while(len(ll[lnum:])):
r_flag = False
x_flag = False
i_flag = False
handled = False
current_offset =0
l = ll[lnum]
lnum += 1
args = []
if 1 in [c in [ord(x) for x in l] for c in FORBIDDEN_CHARS]:
print("illegal caracters on line %s:%d fatal" % (filename,lnum))
exit()
if l.strip() != "":
(label,ismacroinst,op, indirect_bit, addridx, comment) = decompose_asm(l)
if op is not None or label is not None:
if in_macro_name != None:
if op == "EMAC":
in_macro_name = None
continue
else:
MACROS[in_macro_name].append(l)
else:
if ismacroinst:
if op in MACROS:
c = 0
n = 0
ulbl = get_unique_label()
local_labels = {}
if addridx != None:
params = addridx.split(",")
else:
params = []
for ml in MACROS[op]:
(mlabel,mismacroinst,mop,mindirect_bit, maddridx, mcomment) = decompose_asm(ml)
if mlabel is not None:
if mlabel[0] == "@":
local_labels[mlabel.strip()] = get_unique_label()
if "@" in ml:
m = re.findall("(@\d+)",ml)
for s in m:
ml = ml.replace(s, local_labels[s])
if "#" in ml:
m = re.findall("(#\d+)",ml)
for s in m:
ml = ml.replace(s, params[int(s[1:])-1])
ll.insert(lnum+c,ml)
c = c + 1
else:
print("macro %s not found" % op)
continue
else:
if label:
SYMBOLS[label] = ("int",cur_offset_from_org)
if op:
if op in PSEUDO_OPCODES:
if op == "REL":
addr_mode = MODE_RELATIVE
handled = True #fixme, this will eventually have consequences
continue
elif op == "ABS":
addr_mode = MODE_ABSOLUTE
continue
elif op == "BSS":
args = addridx.split(" ")
#FIXME, arg[1] should be an optional addres.. but.. i dont think i handle it at all
#am i expected to emit an ORG?
fooargs.append(args[0])
if len(args) > 1:
fooargs.append(args[1])
if comment:
fooargs.append("#%s"%comment)
for d in range(0,parsearg(cur_offset_from_org,SYMBOLS, args[0])(),2):
program_listing.append((lnum,cur_offset_from_org,op,indirect_bit,fooargs, LOADER_FORMATS[DIRECT_LOAD][1] | ( LOADER_BITMASKS["X_FLAG"] * x_flag ), lambda x=0:[x],supress_output))
fooargs = []
cur_offset_from_org += 1
handled = True
continue
elif op == "BES":
args = addridx.split(" ")
#FIXME, arg[1] should be an optional addres.. but.. i dont think i handle it at all
fooargs.append(args[0])
if len(args) > 1:
fooargs.append(args[1])
if comment:
fooargs.append("#%s"%comment)
for d in range(0,parsearg(cur_offset_from_org,SYMBOLS, args[0])(),2):
program_listing.append((lnum,cur_offset_from_org,op,indirect_bit,fooargs, LOADER_FORMATS[DIRECT_LOAD][1] | ( LOADER_BITMASKS["X_FLAG"] * x_flag ), lambda x=0:[x],supress_output))
SYMBOLS[label] = ("int",cur_offset_from_org) #adjust the label to point to the end of the block
fooargs = []
cur_offset_from_org += 1
handled = True
continue
elif op == "MACR":
in_macro_name = label
MACROS[in_macro_name] = []
handled = True
continue
elif op == "END":
if comment:
args.append("#%s"%comment)
program_listing.append((lnum,cur_offset_from_org,op,indirect_bit,args,0xe20000 , lambda : [0],supress_output))
handled = True
cur_offset_from_org += 1
continue
elif op == "NAME":
#program_listing.append((lnum,cur_offset_from_org,op,0xe20000 , lambda : [0]),supress_output)
continue
elif op == "NOLS":
supress_output = True
handled = True
continue
elif op == "MOR":
#lol
handled = True
continue
elif op == "LIST":
supress_output = False
handled = True
continue
elif op == "ORG":
r_flag = True
try:
# cur_offset_from_org = parsearg(cur_offset_from_org,SYMBOLS,addridx)() #not sure i mean to comment this out
cur_offset_from_org = 0
org_address = parsearg(cur_offset_from_org,SYMBOLS,addridx)()
args.append("'%06o" % org_address)
if comment:
args.append("#%s"%comment)
program_listing.append((lnum,org_address,op,indirect_bit,args, LOADER_FORMATS[LITERAL_LOAD][1] | ( LOADER_BITMASKS["X_FLAG"] * x_flag ) | ( LOADER_BITMASKS["R_FLAG"] * r_flag ) , lambda x=org_address, y=addridx: [parsearg(x,SYMBOLS,y)() ],supress_output))
handled = True
continue
except Exception as err:
print("****\n%s:%d generated the following error\n***" % (filename,lnum))
traceback.print_exc()
sys.exit(-1)
elif op in ["***", "ZZZ"]:
if len(addridx.split(",")) == 1:
val = addridx
elif len(addridx.split(",")) == 2:
(addr,idx) = addridx.split(",")
val = addr
args.append("'%06o" % val)
if int(idx):
idx = True
args.append("1")
if comment:
args.append("#%s"%comment)
program_listing.append((lnum,cur_offset_from_org,op,indirect_bit,args, (indirect_bit<<14), lambda x=cur_offset_from_org,y=val: [parsearg(x,SYMBOLS,y)()],supress_output))
handled = True
continue
elif op == "DATA":
r_flag = True
if "," not in addridx or addridx[:2] == "''":
lst = [addridx]
else:
lst = addridx.split(",")
for li in lst:
data = parsearg(cur_offset_from_org,SYMBOLS,li)()
if isinstance(data,list):
foobuff = []
if comment:
foobuff.append("#%s"%li + " " + comment)
else:
foobuff.append("#%s"%li + " ")
for d in range(0,len(data),2):
try:
r = (data[d] << 8) | (data[d+1])
except IndexError:
r = data[d]
program_listing.append((lnum,cur_offset_from_org,op,indirect_bit,["'%06o" % r]+foobuff, LOADER_FORMATS[DIRECT_LOAD][1] | ( LOADER_BITMASKS["X_FLAG"] * x_flag ), lambda x=r:[x],supress_output))
cur_offset_from_org += 1
foobuff = []
else:
args.append("'%06o" % data)
if comment:
args.append("#%s"%comment)
program_listing.append((lnum,cur_offset_from_org,op,indirect_bit,args,LOADER_FORMATS[DIRECT_LOAD][1] | ( LOADER_BITMASKS["X_FLAG"] * x_flag ), lambda x=data:[x],supress_output))
cur_offset_from_org += 1
handled = True
continue
elif op == "EQU":
try:
args.append(addridx)
SYMBOLS[label] = ("int",parsearg(cur_offset_from_org, SYMBOLS, addridx)()) #first pass only
except Exception as err:
print("****\n%s:%d generated the following error\n***" % (filename,lnum))
traceback.print_exc()
sys.exit(-1)
continue
elif op == "DAC": #not right fixme
idx = False
x_flag = True
if len(addridx.split(",")) == 1:
val = addridx
args.append(val)
elif len(addridx.split(",")) == 2: #fixme
(addr,idx) = addridx.split(",")
val = addr
args.append(val)
if int(idx):
idx = True
args.append("1")
if comment:
args.append("#%s"%comment)
program_listing.append((lnum,cur_offset_from_org,"DAC",indirect_bit, args, LOADER_FORMATS[MREF_LOAD][1] | ( LOADER_BITMASKS["R_FLAG"] * r_flag )|( LOADER_BITMASKS["X_FLAG"] * x_flag )|LOADER_BITMASKS["DAC"] ,lambda x=cur_offset_from_org,y=val:[parsearg(x,SYMBOLS,y)()],supress_output))
handled = True
cur_offset_from_org += 1
continue
elif op == "EAC": #not right either
daceac_bit = True
idx = False
x_flag= true
if len(addridx.split(",")) == 1:
val = addridx
args.append("'%06o" % val)
elif len(addridx.split(",")) == 2: #fixme too
(addr,idx) = addridx.split(",")
val = addr
args.append("'%06o" % val)
if int(idx):
idx = True
args.append("1")
if comment:
args.append("#%s"%comment)
program_listing.append((lnum,cur_offset_from_org,"EAC",args, LOADER_FORMATS[MREF_LOAD][1] | ( LOADER_BITMASKS["R_FLAG"] * r_flag )|( LOADER_BITMASKS["X_FLAG"] * x_flag )|LOADER_BITMASKS["EAC"] ,lambda x=cur_offset_from_org,y=val:[parsearg(x,SYMBOLS,y)()]),supress_output)
handled = True
continue
elif op in MREF_OPCODES:
addr = addridx
if indirect_bit:
i_flag = True
if addr[0] == "=":
x_flag = True
args.append(addr)
if comment:
args.append("#%s"%comment)
program_listing.append((lnum,cur_offset_from_org,op,indirect_bit,args,(MREF_OPCODES[op] << 17 ) | LOADER_FORMATS[LITERAL_LOAD][1]| ( LOADER_BITMASKS["X_FLAG"] * x_flag )| ( LOADER_BITMASKS["I_FLAG"] * i_flag )| ( LOADER_BITMASKS["R_FLAG"] * r_flag ), lambda x=cur_offset_from_org,y=addr[1:]: [parsearg(x,SYMBOLS,y)()],supress_output))
handled = True
else:
# print("indir",op)
r_flag = True
if len(addridx.split(",")) == 1:
addr = addridx
args.append(addr)
elif len(addridx.split(",")) == 2:
(addr,idx) = addridx.split(",")
args.append(addr)
if int(idx):
x_flag = True
args.append("1")
if comment:
args.append("#%s"%comment)
program_listing.append((lnum,cur_offset_from_org,op,indirect_bit,args, (MREF_OPCODES[op] << 17 ) | LOADER_FORMATS[MEMREF_LOAD][1] | ( LOADER_BITMASKS["X_FLAG"] * x_flag )| ( LOADER_BITMASKS["I_FLAG"] * i_flag )| ( LOADER_BITMASKS["R_FLAG"] * r_flag ), lambda x=cur_offset_from_org,y=addr: [parsearg(x,SYMBOLS,y)()],supress_output))
handled = True
cur_offset_from_org += 1
elif op in AUGMENTED_OPCODES:
shift_count = 0
if addridx and addridx.strip() != "":
try:
shift_count = parsearg(cur_offset_from_org,SYMBOLS,addridx)()
if shift_count:
args.append("'%o" % shift_count)
except Exception as err:
print("****\n%s:%d generated the following error\n***" % (filename,lnum))
traceback.print_exc()
sys.exit(-1)
opcode = (AUGMENTED_OPCODES[op][0] << 12) | (shift_count << 6) | AUGMENTED_OPCODES[op][1]
if comment:
args.append("#%s"%comment)
program_listing.append((lnum,cur_offset_from_org,op,indirect_bit,args, LOADER_FORMATS[DIRECT_LOAD][1], lambda y=opcode: [y],supress_output))
handled = True
cur_offset_from_org += 1
elif op in IO_OPCODES:
x_bit = False
map_bit = False
augment_code = 0
wait_bit = False
unit = addridx
index_bit = 0
if len(addridx.split(",")) == 2:
(unit, wait) = addridx.split(",")
args.append(unit)
if wait == "W":
wait_bit = True
args.append("W")
elif len(addridx.split(",")) == 3:
(unit, wait, index) = addridx.split(",")
args.append(unit)
if index == "1":
index_bit = True
args.append("1")
if wait == "W":
wait_bit = True
args.append("W")
opcode = (IO_OPCODES[op][0] << 12) | (index_bit << 11) | (indirect_bit << 10) | (map_bit << 9) | (wait_bit << 6) | (IO_OPCODES[op][1] << 7)
if comment:
args.append("#%s"%comment)
program_listing.append((lnum,cur_offset_from_org,op,indirect_bit,args, LOADER_FORMATS[DIRECT_LOAD][1] | ( LOADER_BITMASKS["X_FLAG"] * x_flag ) | opcode,lambda x=cur_offset_from_org,y=unit:[parsearg(x,SYMBOLS,y)()],supress_output))
handled = True
cur_offset_from_org += 1
elif op in INT_OPCODES:
augment_code = 0
if addridx:
try:
augment_code = parsearg(SYMBOLS,addridx) #fixme borken
except Exception as err:
print("****\%s:%d generated the following error" % (filename,lnum))
traceback.print_exc()
sys.exit(-1)
opcode = (INT_OPCODES[op] << 12) | augment_code
if comment:
args.append("#%s"%comment)
program_listing.append((lnum,cur_offset_from_org,op,indirect_bit,args, LOADER_FORMATS[LITERAL_LOAD][1] | ( LOADER_BITMASKS["X_FLAG"] * x_flag ),lambda x=opcode, y=augment_code, z=cur_offset_from_org:[parsearg(z,SYMBOLS, y)()|x],supress_output))
args.append("'%06o" % opcode)
handled = True
cur_offset_from_org += 1
else:
print("unhandled opcode [%s] on %s:%d in first pass.. you should fix that.. fatal" % (op,filename,lnum))
exit()
if handled == False:
program_listing.append((lnum,"ERROR",None,None,None,None,supress_output))
return (addr_mode,program_listing)
def asm_pass_2(object_code):
idx = 0
absfile = {}
current_addr = 0
org_addr = 0
absfile[org_addr] = []
while idx < len(object_code):
v,ol = object_code[idx]
fmt = (0xc00000 & v) >> 22
val = v & 0x3fffff
handled = False
if fmt == 0b00:
zeros = (val & 0x3f0000) >> 17
val = v & 0xffff
print("%06o\t" % val, ol)
absfile[org_addr].append((val,ol))
handled = True
idx += 1
current_addr+=1
elif fmt == 0b01:
# print("moo")
r = (val & 0x200000) >> 21
x = (val & 0x10000) >> 16
i = (val & 0x8000) >> 15
addr = (val & 0x7fff)
opcode = (val & 0x1e0000) >> 17
# map = True #i dont know why it is but it is
map = False
# print(addr)
# print(org_addr)
print("%06o\t" % ((opcode << 12) | (x << 11) | (i << 10) | (map << 9) |((addr + org_addr) & 0x1ff)), ol)
absfile[org_addr].append( ( (opcode << 12) | (x << 11) | (i << 10) | ((addr + org_addr) & 0x1ff),ol) )
handled = True
idx += 1
current_addr+=1
elif fmt == 0b10:
idx += 1
current_addr+=1
cd = (0xc00000 & full_tape[idx]) >> 22
if cd == 0x00:
print("sub def")
elif cd == 0x01:
print("sub call")
elif cd == 0x02:
print("common def")
elif cd == 0x03:
print("common req")
elif fmt == 0b11:
r = (val & 0x200000) >> 21
opcode = (val & 0x1e0000) >> 17
isliteral = val &0x10000
if(isliteral):
literal = (val & 0x3fff)
print("%06o\t" % literal, ol)
absfile[org_addr].append((literal,ol))
handled = True
else: #special action
address = (val & 0x7fff)
n = (val & 0x8000) >> 15
print("****",opcode)
if opcode==0:
print("set org")
current_addr = address
absfile[address] = []
org_addr = address
handled = True
elif opcode == 1:
print("end")
return absfile
handled = True
idx += 1
current_addr+=1
if not handled:
print("------\t",ol)
absfile[org_addr].append((0,ol))
return absfile
def load_file(filename):
f = open(filename)
ll = f.readlines()
f.close()
return ll
def write_symbols(filename):
fn = "%s.sym" % filename
print("writing symbols %s" % fn)
f = open(fn, "w")
f.write(json.dumps(SYMBOLS))
f.close()
filename = sys.argv[1]
(addr_mode,program_listing) = asm_pass_1(filename)
write_symbols(".".join(filename.split(".")[:-1]))
print("creating relocatable binary")
relocatable_file = []
for (lnum,cur_offset_from_org,op,indirect_bit,args,oparg,oparg_calc,supress) in program_listing:
if oparg != None:
for v in oparg_calc():
label = ""
for s,a in SYMBOLS.items():
if a[1] == cur_offset_from_org:
label = s
val = oparg | dec2twoscmplment(v)
if indirect_bit:
indir = "*"
else:
indir = " "
if not label:
label = " "
else:
label = label.ljust(4," ")
if len(args) and args[-1][0] == "#":
testbuff = "%s %s%s %s" % (label,op, indir, ",".join(args[:-1]))
buf = "*"+args[-1][1:]
testbuff += buf.rjust(27 + (len(buf) - len(testbuff))," ")
else:
testbuff = "%s %s%s %s" % (label,op, indir, ",".join(args))
outline = "%04x\t%08o\t%s\t\t\t" % (cur_offset_from_org,val,testbuff)
if not supress:
print(outline)
relocatable_file.append((val,outline))
else:
relocatable_file.append((0x0,"ERROR!"))
print(l)
if addr_mode == MODE_RELATIVE:
print("writing object binary")
fn = "%s.obj" % ".".join(filename.split(".")[:-1])
f = open(fn, "wb")
for val,line in relocatable_file:
f.write(struct.pack("3B", (val & 0xff0000) >> 16, (val & 0xff00) >> 8,(val & 0xff) ))
f.close()
else:
print("lol i dont really know what im doing")
print("running second pass on object output")
absolute_file = asm_pass_2(relocatable_file)
print("writing absolute binary")
# print (absolute_file)
for ml,absf in absolute_file.items():
if len(absf):
fn = "%s-ORG_%04x.bin" % (".".join(filename.split(".")[:-1]), ml)
f = open(fn, "wb")
for val,line in absf:
f.write(struct.pack(">H", val))
f.close()
print("uh.. we dont really support absolute mode right now")