-
Notifications
You must be signed in to change notification settings - Fork 1
/
assembler.py
866 lines (688 loc) · 34.2 KB
/
assembler.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
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
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
'''
Assembler: asm -> hex
Notes/Best Practices about ASM:
1. ALWAYS use LABELS - do not say GOTO 42
2. If you do not EXIT at the end of your program
the next lines in the giant hex file will run
3. The giant file.hex in /hex is a statically
linked hex file containing the assembled
hex from all assembly files in /asm in
alphabetical order
'''
import io
import json
import os
import re
import string
import sys
import time
import util
from termcolor import colored
LABELS_TO_PC = {}
COMMENT_SYMBOL = '//'
valid_opcodes_keywords = [
'LD', 'ADD', 'SUB', 'MUL', 'DIV', 'REM', 'CALL', 'CMP', 'LT', 'LTE', 'GT',
'GTE', 'GOTO', 'RETURN', 'PUSH', 'POP', 'EXIT', 'WAIT', 'SHT', 'RAND',
'INPUT', 'BLIT'
]
def error_msg(file_asm, line_idx, line, num_errors, msg):
print(f'\n{file_asm}:{line_idx}', colored('error:', 'red'), msg)
print(f'{line}')
print(colored('^'+'~'*(len(line)-1), 'green'))
num_errors += 1
return num_errors
def write_two_lines_to_hexfile(word0_first_half,
word0_second_half,
word1, hex_file_str):
# write to hex file
hex_file_str += word0_first_half
hex_file_str += word0_second_half
hex_file_str += '\n'
hex_file_str += word1
hex_file_str += '\n'
return hex_file_str
def compute_label_indices_from_file(file_asm, cumsum_hex_lines, num_errors):
'''line = code // comment'''
lines = util.return_lines_from_file(file_asm)
lines_for_program = [] # all but commentsa and blank lines
for line_idx, line in enumerate(lines):
first_comment_idx = line.find(COMMENT_SYMBOL)
comment = line[first_comment_idx+1:]
code = line[:first_comment_idx]
if first_comment_idx == -1:
comment = ''
code = line
# match label regex
if re.match(util.REGEX_LABEL_AND_COLON, code):
label = re.findall(util.REGEX_LABEL_AND_COLON, code)[0]
if label in LABELS_TO_PC.keys():
msg='label defined more than once'
num_errors = error_msg(
file_asm, line_idx, line, num_errors, msg
)
LABELS_TO_PC[label] = cumsum_hex_lines
elif re.match(util.REGEX_HEX_WITH_SPACE_BEFORE, code):
lines_for_program.append(code)
cumsum_hex_lines += 1 # eg 0XFF0000FF
elif not code.isspace() and code != '':
lines_for_program.append(code)
cumsum_hex_lines += 2
return lines_for_program, cumsum_hex_lines, num_errors
def split_hexfile_into_byte_array(giant_hex_file_str):
byte_arr = []
for word in giant_hex_file_str.split('\n'):
byte_arr += [
util.hex_to_int(word[:2]),
util.hex_to_int(word[2:4]),
util.hex_to_int(word[4:6]),
util.hex_to_int(word[6:])
]
return bytearray(byte_arr)
def validate_and_make_hexfile(file_asm, lines, num_errors):
hex_file_str = ''
extra_line_for_raw_hex = 0
# for line in lines:
for line_idx, line in enumerate(lines):
first_comment_idx = line.find(COMMENT_SYMBOL)
if first_comment_idx == -1:
comment = ''
code = line
else:
comment = line[first_comment_idx+1:]
code = line[:first_comment_idx]
# args in code
code_split = code.split(' ')
# filter \t and ' '
while '' in code_split:
code_split.remove('')
for idx in range(len(code_split)):
code_split[idx] = code_split[idx].replace('\t', '')
if len(code_split) > 0:
opcode = code_split[0]
args = code_split[1:]
word0_first_half = '0000'
word0_second_half = '0000'
word1 = '00000000'
valid_opcode = False
# raw data
if re.match(util.REGEX_HEX, opcode):
valid_opcode = False
if len(args) > 0:
msg='invalid hexcode'
num_errors = error_msg(
file_asm, line_idx, line, num_errors, msg
)
opcode_int = util.hex_to_int(opcode)
opcode_hex = util.int_to_hex(opcode_int)
hex_file_str += opcode_hex.zfill(8)
hex_file_str += '\n'
# valid opcode
elif opcode not in valid_opcodes_keywords:
msg=f'invalid opcode'
num_errors = error_msg(
file_asm, line_idx, line, num_errors, msg
)
elif opcode == 'GOTO':
valid_opcode = True
if len(args) < 1:
msg='missing argument'
num_errors = error_msg(
file_asm, line_idx, line, num_errors, msg
)
# GOTO LABEL
elif re.match(util.REGEX_LABEL, args[0]):
opcode_val = util.opcode_lookup_dict['GOTO']
if args[0] not in LABELS_TO_PC.keys():
msg='unknown label'
num_errors = error_msg(
file_asm, line_idx, line, num_errors, msg
)
else:
word1 = util.int_to_hex(LABELS_TO_PC[args[0]]).zfill(8)
word0_second_half = opcode_val.zfill(4)
# GOTO i
elif re.match(util.REGEX_INT, args[0]):
opcode_val = util.opcode_lookup_dict['GOTO']
word1 = util.int_to_hex(args[0]).zfill(8)
word0_second_half = opcode_val.zfill(4)
# GOTO R[i]
elif re.match(util.REGEX_LD_R_ONE, args[0]):
opcode_val = util.opcode_lookup_dict['GOTO R[i]']
word1 = util.int_to_hex(args[0][2:-1]).zfill(8)
word0_second_half = opcode_val.zfill(4)
elif opcode == 'LD':
# Validate
if len(args) < 2:
msg=f'requires 2 or more arguments after {opcode}'
num_errors = error_msg(
file_asm, line_idx, line, num_errors, msg
)
# LD ARRAY R[X] R[Y] W H (X,Y must be <= 255)
elif re.match(util.REGEX_ARRAY_LD, ' '.join(args)):
valid_opcode = True
opcode_val = util.opcode_lookup_dict['LD ARRAY TO VRAM']
all_args = re.findall(util.REGEX_ARRAY_LD, ' '.join(args))
label = all_args[0][0]
x_sprite = all_args[0][1]
y_sprite = all_args[0][2]
width_sprite = all_args[0][3]
height_sprite = all_args[0][4]
if label not in LABELS_TO_PC.keys():
msg='unknown label'
num_errors = error_msg(
file_asm, line_idx, line, num_errors, msg
)
label_idx = util.int_to_hex(LABELS_TO_PC[label]).zfill(4)
x_sprite = util.int_to_hex(x_sprite).zfill(2)
y_sprite = util.int_to_hex(y_sprite).zfill(2)
width_sprite = util.int_to_hex(width_sprite).zfill(2)
height_sprite = util.int_to_hex(height_sprite).zfill(2)
word0_first_half = label_idx
word0_second_half = opcode_val.zfill(4)
word1 = x_sprite + y_sprite + width_sprite + height_sprite
hex_file_str = write_two_lines_to_hexfile(
word0_first_half, word0_second_half,
word1, hex_file_str
)
# LD R[i] ARRAY (load PC of ARRAY to R[i])
elif re.match(util.REGEX_LD_LABEL_PC, ' '.join(args)):
valid_opcode = True
opcode_val = util.opcode_lookup_dict['LD ARRAY PC TO REGISTER']
word0_second_half = opcode_val.zfill(4)
# parse out args
all_args = re.findall(util.REGEX_LD_LABEL_PC, ' '.join(args))
ram_index = all_args[0][0]
label = all_args[0][1]
if label not in LABELS_TO_PC.keys():
msg='unknown label'
num_errors = error_msg(
file_asm, line_idx, line, num_errors, msg
)
label_idx = util.int_to_hex(LABELS_TO_PC[label]).zfill(4)
word0_first_half = label_idx
word1 = '0000' + util.int_to_hex(ram_index).zfill(4)
hex_file_str = write_two_lines_to_hexfile(
word0_first_half, word0_second_half,
word1, hex_file_str
)
# LD R[U] R[i] R[j] R[k] R[k] (i,j,k,l must be <= 255)
elif re.match(util.REGEX_REGISTER_ONLY_ARRAY_LD, ' '.join(args)):
valid_opcode = True
opcode_val = util.opcode_lookup_dict['LD REGISTERS TO VRAM']
all_args = re.findall(util.REGEX_REGISTER_ONLY_ARRAY_LD, ' '.join(args))
UVYZ = all_args[0][0]
x_sprite = all_args[0][1]
y_sprite = all_args[0][2]
width_sprite = all_args[0][3]
height_sprite = all_args[0][4]
UVYZ_digit = util.UVYZ_to_hex_digit[str(UVYZ)]
x_sprite = util.int_to_hex(x_sprite).zfill(2)
y_sprite = util.int_to_hex(y_sprite).zfill(2)
width_sprite = util.int_to_hex(width_sprite).zfill(2)
height_sprite = util.int_to_hex(height_sprite).zfill(2)
word0_first_half = UVYZ_digit + '000'
word0_second_half = opcode_val.zfill(4)
word1 = x_sprite + y_sprite + width_sprite + height_sprite
hex_file_str = write_two_lines_to_hexfile(
word0_first_half, word0_second_half,
word1, hex_file_str
)
# LD R[U] R[vram_idx] R[k] R[k] (k,l must be <= 255)
elif re.match(util.REGEX_REGISTER_ONLY_VRAM_IDX_ARRAY_LD, ' '.join(args)):
valid_opcode = True
opcode_val = util.opcode_lookup_dict['LD REGISTERS TO VRAM W VRAM INDEX']
all_args = re.findall(
util.REGEX_REGISTER_ONLY_VRAM_IDX_ARRAY_LD,
' '.join(args)
)
UVYZ = all_args[0][0]
idx_to_vram_idx = all_args[0][1]
width_sprite = all_args[0][2]
height_sprite = all_args[0][3]
UVYZ_digit = util.UVYZ_to_hex_digit[str(UVYZ)]
idx_to_vram_idx = util.int_to_hex(idx_to_vram_idx).zfill(4)
width_sprite = util.int_to_hex(width_sprite).zfill(2)
height_sprite = util.int_to_hex(height_sprite).zfill(2)
word0_first_half = UVYZ_digit + '000'
word0_second_half = opcode_val.zfill(4)
word1 = idx_to_vram_idx + width_sprite + height_sprite
hex_file_str = write_two_lines_to_hexfile(
word0_first_half, word0_second_half,
word1, hex_file_str
)
elif re.match(util.REGEX_LD_R_ONE, args[0]):
valid_opcode = True
# LD R[i] R[j]
if re.match(util.REGEX_LD_R_ONE, args[1]):
opcode_val = util.opcode_lookup_dict['REGISTER TO REGISTER LOAD']
word1 = util.int_to_hex(args[1][2:-1]).zfill(8)
# LD R[i] j
elif re.match(r'\d+', args[1]) or re.match(util.REGEX_HEX, args[1]):
opcode_val = util.opcode_lookup_dict['DIRECT LOAD']
word1 = util.int_to_hex(args[1]).zfill(8)
# LD R[i] R[U]
elif re.match(util.REGEX_UV_ONE, args[1]) and len(args) == 2:
opcode_val = util.opcode_lookup_dict['LD R[i] R[U]']
letters_arg0 = re.findall(util.REGEX_UV_ONE, args[1])
i = util.UVYZ_to_hex_digit[str(letters_arg0[0])]
word1 = i+'0000000'
word0_first_half = util.int_to_hex(args[0][2:-1]).zfill(4)
word0_second_half = opcode_val.zfill(4)
hex_file_str = write_two_lines_to_hexfile(
word0_first_half, word0_second_half,
word1, hex_file_str
)
elif re.match(util.REGEX_UV_ONE, args[0]) and len(args) == 2:
valid_opcode = True
letters_arg0 = re.findall(util.REGEX_UV_ONE, args[0])
i = util.UVYZ_to_hex_digit[str(letters_arg0[0])]
j = '0'
# LD R[U] R[V]
if re.match(util.REGEX_UV_ONE, args[1]):
opcode_val = util.opcode_lookup_dict['LD R[U] R[V]']
j = util.UVYZ_to_hex_digit[args[1][2:-1]]
# LD R[U] R[i]
elif re.match(r'R\[\d+]', args[1]):
opcode_val = util.opcode_lookup_dict['LD R[U] R[i]']
word1 = util.int_to_hex(args[1][2:-1]).zfill(8)
# LD R[U] i
elif re.match(r'\d+', args[1]):
opcode_val = util.opcode_lookup_dict['LD R[U] i']
word1 = util.int_to_hex(args[1]).zfill(8)
word0_first_half = i+j+'00'
word0_second_half = opcode_val.zfill(4)
hex_file_str = write_two_lines_to_hexfile(
word0_first_half, word0_second_half,
word1, hex_file_str
)
elif re.match(util.REGEX_UV_TWO, args[0]):
valid_opcode = True
letters_arg0 = re.findall(util.REGEX_UV_TWO, args[0])
i = util.UVYZ_to_hex_digit[str(letters_arg0[0][0])]
j = util.UVYZ_to_hex_digit[str(letters_arg0[0][1])]
k = '0'
l = '0'
# LD R[U:V] R[i]
if re.match(r'R\[\d+]', args[1]):
opcode_val = util.opcode_lookup_dict['LD R[U:V] R[i]']
word1 = util.int_to_hex(args[1][2:-1]).zfill(8)
# LD R[U:V] i
if re.match(r'\d+', args[1]):
opcode_val = util.opcode_lookup_dict['LD R[U:V] i']
word1 = util.int_to_hex(args[1]).zfill(8)
# LD R[U:V] R[Y]
elif re.match(util.REGEX_UV_ONE, args[1]):
opcode_val = util.opcode_lookup_dict['LD R[U:V] R[Y]']
k = re.findall(util.REGEX_UV_ONE, args[1])[0]
k = util.UVYZ_to_hex_digit[k]
# LD R[U:V] R[Y:Z]
elif re.match(util.REGEX_UV_TWO, args[1]):
opcode_val = util.opcode_lookup_dict['LD R[U:V] R[Y:Z]']
k_and_l = re.findall(util.REGEX_UV_TWO, args[1])
k = util.UVYZ_to_hex_digit[str(k_and_l[0][0])]
l = util.UVYZ_to_hex_digit[str(k_and_l[0][1])]
word0_first_half = i+j+k+l
word0_second_half = opcode_val.zfill(4)
hex_file_str = write_two_lines_to_hexfile(
word0_first_half, word0_second_half,
word1, hex_file_str
)
if not valid_opcode:
msg='invalid syntax'
num_errors = error_msg(
file_asm, line_idx, line, num_errors, msg
)
elif opcode == 'ADD':
valid_opcode = True
if len(args) < 2 or not re.match(util.REGEX_LD_R_ONE, args[0]):
msg=f'requires 2 or more arguments after {opcode}'
num_errors = error_msg(
file_asm, line_idx, line, num_errors, msg
)
elif re.match(util.REGEX_LD_R_ONE, args[1]):
opcode_val = util.opcode_lookup_dict['REGISTER TO REGISTER ADD']
word1 = util.int_to_hex(args[1][2:-1]).zfill(8)
else:
opcode_val = util.opcode_lookup_dict['DIRECT ADD']
word1 = util.int_to_hex(args[1]).zfill(8)
word0_first_half = util.int_to_hex(args[0][2:-1]).zfill(4)
word0_second_half = opcode_val.zfill(4)
elif opcode == 'SUB':
valid_opcode = True
if len(args) < 2 or not re.match(util.REGEX_LD_R_ONE, args[0]):
msg=f'requires 2 or more arguments after {opcode}'
num_errors = error_msg(
file_asm, line_idx, line, num_errors, msg
)
elif re.match(util.REGEX_LD_R_ONE, args[1]):
opcode_val = util.opcode_lookup_dict['REGISTER TO REGISTER SUBTRACT']
word1 = util.int_to_hex(args[1][2:-1]).zfill(8)
else:
opcode_val = util.opcode_lookup_dict['DIRECT SUBTRACT']
word1 = util.int_to_hex(args[1]).zfill(8)
word0_first_half = util.int_to_hex(args[0][2:-1]).zfill(4)
word0_second_half = opcode_val.zfill(4)
elif opcode == 'MUL':
valid_opcode = True
if len(args) < 2 or not re.match(util.REGEX_LD_R_ONE, args[0]):
msg=f'requires 2 or more arguments after {opcode}'
num_errors = error_msg(
file_asm, line_idx, line, num_errors, msg
)
elif re.match(util.REGEX_LD_R_ONE, args[1]):
opcode_val = util.opcode_lookup_dict['REGISTER TO REGISTER MULTIPLY']
word1 = util.int_to_hex(args[1][2:-1]).zfill(8)
else:
opcode_val = util.opcode_lookup_dict['DIRECT MULTIPLY']
word1 = util.int_to_hex(args[1]).zfill(8)
word0_first_half = util.int_to_hex(args[0][2:-1]).zfill(4)
word0_second_half = opcode_val.zfill(4)
elif opcode == 'DIV':
valid_opcode = True
if len(args) < 2 or not re.match(util.REGEX_LD_R_ONE, args[0]):
msg=f'requires 2 or more arguments after {opcode}'
num_errors = error_msg(
file_asm, line_idx, line, num_errors, msg
)
elif re.match(util.REGEX_LD_R_ONE, args[1]):
opcode_val = util.opcode_lookup_dict['REGISTER TO REGISTER DIVIDE']
word1 = util.int_to_hex(args[1][2:-1]).zfill(8)
else:
opcode_val = util.opcode_lookup_dict['DIRECT DIVIDE']
word1 = util.int_to_hex(args[1]).zfill(8)
# check if dividing by 0
if word1 == '00000000':
msg=f'attempting to divide by zero'
num_errors = error_msg(
file_asm, line_idx, line, num_errors, msg
)
word0_first_half = util.int_to_hex(args[0][2:-1]).zfill(4)
word0_second_half = opcode_val.zfill(4)
elif opcode == 'REM':
valid_opcode = True
if len(args) < 2 or not re.match(util.REGEX_LD_R_ONE, args[0]):
msg=f'requires 2 or more arguments after {opcode}'
num_errors = error_msg(
file_asm, line_idx, line, num_errors, msg
)
elif re.match(util.REGEX_LD_R_ONE, args[1]):
opcode_val = util.opcode_lookup_dict['REGISTER TO REGISTER REMAINDER']
word1 = util.int_to_hex(args[1][2:-1]).zfill(8)
else:
opcode_val = util.opcode_lookup_dict['DIRECT REMAINDER']
word1 = util.int_to_hex(args[1]).zfill(8)
# check if remainder by 0
if word1 == '00000000':
msg=f'attempting to divide by zero'
num_errors = error_msg(
file_asm, line_idx, line, num_errors, msg
)
word0_first_half = util.int_to_hex(args[0][2:-1]).zfill(4)
word0_second_half = opcode_val.zfill(4)
# ==
elif opcode == 'CMP':
valid_opcode = True
if len(args) < 2:
msg=f'requires 2 or more arguments after {opcode}'
num_errors = error_msg(
file_asm, line_idx, line, num_errors, msg
)
elif re.match(util.REGEX_LD_R_ONE, args[0]):
if re.match(util.REGEX_LD_R_ONE, args[1]):
opcode_val = util.opcode_lookup_dict['COMPARE REGISTER TO REGISTER']
word1 = util.int_to_hex(args[1][2:-1]).zfill(8)
word0_first_half = util.int_to_hex(args[0][2:-1]).zfill(4)
word0_second_half = opcode_val.zfill(4)
else:
opcode_val = util.opcode_lookup_dict['COMPARE REGISTER TO DIRECT']
word1 = util.int_to_hex(args[1]).zfill(8)
word0_first_half = util.int_to_hex(args[0][2:-1]).zfill(4)
word0_second_half = opcode_val.zfill(4)
elif re.match(util.REGEX_UV_ONE, args[0]):
letters_arg0 = re.findall(util.REGEX_UV_ONE, args[0])
i = util.UVYZ_to_hex_digit[str(letters_arg0[0])]
if re.match(r'\d+', args[1]):
opcode_val = util.opcode_lookup_dict['COMPARE UV TO DIRECT']
word1 = util.int_to_hex(args[1]).zfill(8)
word0_first_half = i + '000'
word0_second_half = opcode_val.zfill(4)
hex_file_str = write_two_lines_to_hexfile(
word0_first_half, word0_second_half,
word1, hex_file_str
)
# <
elif opcode == 'LT':
valid_opcode = True
if len(args) < 2 or not re.match(util.REGEX_LD_R_ONE, args[0]):
msg=f'requires 2 or more arguments after {opcode}'
num_errors = error_msg(
file_asm, line_idx, line, num_errors, msg
)
elif re.match(util.REGEX_LD_R_ONE, args[1]):
opcode_val = util.opcode_lookup_dict['LESS THAN REGISTER TO REGISTER']
word1 = util.int_to_hex(args[1][2:-1]).zfill(8)
else:
opcode_val = util.opcode_lookup_dict['LESS THAN REGISTER TO DIRECT']
word1 = util.int_to_hex(args[1]).zfill(8)
word0_first_half = util.int_to_hex(args[0][2:-1]).zfill(4)
word0_second_half = opcode_val.zfill(4)
# <=
elif opcode == 'LTE':
valid_opcode = True
if len(args) < 2 or not re.match(util.REGEX_LD_R_ONE, args[0]):
msg=f'requires 2 or more arguments after {opcode}'
num_errors = error_msg(
file_asm, line_idx, line, num_errors, msg
)
elif re.match(util.REGEX_LD_R_ONE, args[1]):
opcode_val = util.opcode_lookup_dict['LESS THAN OR EQUAL REGISTER TO REGISTER']
word1 = util.int_to_hex(args[1][2:-1]).zfill(8)
else:
opcode_val = util.opcode_lookup_dict['LESS THAN OR EQUAL REGISTER TO DIRECT']
word1 = util.int_to_hex(args[1]).zfill(8)
word0_first_half = util.int_to_hex(args[0][2:-1]).zfill(4)
word0_second_half = opcode_val.zfill(4)
# >
elif opcode == 'GT':
valid_opcode = True
if len(args) < 2 or not re.match(util.REGEX_LD_R_ONE, args[0]):
msg=f'requires 2 or more arguments after {opcode}'
num_errors = error_msg(
file_asm, line_idx, line, num_errors, msg
)
elif re.match(util.REGEX_LD_R_ONE, args[1]):
opcode_val = util.opcode_lookup_dict['STRICT GREATER THAN REGISTER TO REGISTER']
word1 = util.int_to_hex(args[1][2:-1]).zfill(8)
else:
opcode_val = util.opcode_lookup_dict['STRICT GREATER THAN REGISTER TO DIRECT']
word1 = util.int_to_hex(args[1]).zfill(8)
word0_first_half = util.int_to_hex(args[0][2:-1]).zfill(4)
word0_second_half = opcode_val.zfill(4)
# >=
elif opcode == 'GTE':
valid_opcode = True
if len(args) < 2 or not re.match(util.REGEX_LD_R_ONE, args[0]):
msg=f'requires 2 or more arguments after {opcode}'
num_errors = error_msg(
file_asm, line_idx, line, num_errors, msg
)
elif re.match(util.REGEX_LD_R_ONE, args[1]):
opcode_val = util.opcode_lookup_dict['GREATER THAN OR EQUAL REGISTER TO REGISTER']
word1 = util.int_to_hex(args[1][2:-1]).zfill(8)
else:
opcode_val = util.opcode_lookup_dict['GREATER THAN OR EQUAL REGISTER TO DIRECT']
word1 = util.int_to_hex(args[1]).zfill(8)
word0_first_half = util.int_to_hex(args[0][2:-1]).zfill(4)
word0_second_half = opcode_val.zfill(4)
elif opcode == 'CALL':
valid_opcode = True
if len(args) < 1:
msg=f'requires 1 or more arguments after {opcode}'
num_errors = error_msg(
file_asm, line_idx, line, num_errors, msg
)
# CALL LABEL
elif re.match(util.REGEX_LABEL, args[0]):
opcode_val = util.opcode_lookup_dict['CALL']
if args[0] not in LABELS_TO_PC.keys():
raise Exception('\nUnknown Label %s' %args[0])
word1 = util.int_to_hex(LABELS_TO_PC[args[0]]).zfill(8)
# CALL i
elif re.match(util.REGEX_INT, args[0]):
opcode_val = util.opcode_lookup_dict['CALL']
word1 = util.int_to_hex(args[0]).zfill(8)
# CALL R[i]
elif re.match(util.REGEX_LD_R_ONE, args[0]):
opcode_val = util.opcode_lookup_dict['CALL R[i]']
word1 = util.int_to_hex(args[0][2:-1]).zfill(8)
word0_second_half = opcode_val.zfill(4)
elif opcode == 'RETURN':
valid_opcode = True
if len(args) > 0:
msg=f'no args after {opcode}'
num_errors = error_msg(
file_asm, line_idx, line, num_errors, msg
)
opcode_val = util.opcode_lookup_dict['RETURN']
word0_second_half = opcode_val.zfill(4)
elif opcode in ('POP', 'PUSH'):
valid_opcode = True
if len(args) > 0:
msg=f'no args after {opcode}'
num_errors = error_msg(
file_asm, line_idx, line, num_errors, msg
)
opcode_val = util.opcode_lookup_dict[opcode]
word0_second_half = opcode_val.zfill(4)
elif opcode == 'BLIT':
valid_opcode = True
if len(args) > 0:
msg=f'no args after {opcode}'
num_errors = error_msg(
file_asm, line_idx, line, num_errors, msg
)
opcode_val = util.opcode_lookup_dict['BLIT']
word0_second_half = opcode_val.zfill(4)
elif opcode == 'RAND':
valid_opcode = True
opcode_val = util.opcode_lookup_dict[opcode]
word0_second_half = opcode_val.zfill(4)
if len(args) != 1:
msg=f'1 arg after {opcode}'
num_errors = error_msg(
file_asm, line_idx, line, num_errors, msg
)
elif re.match(r'R\[\d+]', args[0]):
word1 = util.int_to_hex(args[0][2:-1]).zfill(8)
else:
msg='invalid syntax'
num_errors = error_msg(
file_asm, line_idx, line, num_errors, msg
)
elif opcode == 'INPUT':
valid_opcode = True
opcode_val = util.opcode_lookup_dict[opcode]
word0_second_half = opcode_val.zfill(4)
if len(args) == 1 and re.match(util.REGEX_LD_R_ONE, args[0]):
ram_slot_idx = re.findall(r'R\[(\d+)]', args[0])[0]
word0_first_half = util.int_to_hex(ram_slot_idx).zfill(4)
else:
msg='invalid syntax'
num_errors = error_msg(
file_asm, line_idx, line, num_errors, msg
)
elif opcode == 'SHT': # shift right plus AND
valid_opcode = True
opcode_val = util.opcode_lookup_dict[opcode]
word0_second_half = opcode_val.zfill(4)
if len(args) == 3 and (re.match(util.REGEX_LD_R_ONE, args[0]) and
re.match(util.REGEX_LD_R_ONE, args[1]) and
re.match(r'\d+', args[2])):
ram_idx_from = re.findall(r'R\[(\d+)]', args[0])[0]
ram_idx_to = re.findall(r'R\[(\d+)]', args[1])[0]
bitshift = args[2]
word0_first_half = util.int_to_hex(ram_idx_to).zfill(4)
word1_first_half = util.int_to_hex(ram_idx_from).zfill(4)
word1_second_half = util.int_to_hex(bitshift).zfill(4)
word1 = word1_first_half + word1_second_half
else:
msg='invalid syntax'
num_errors = error_msg(
file_asm, line_idx, line, num_errors, msg
)
elif opcode == 'WAIT': # wait 17 ms
valid_opcode = True
opcode_val = util.opcode_lookup_dict[opcode]
word0_second_half = opcode_val.zfill(4)
if len(args) > 0:
msg=f'no args after {opcode}'
num_errors = error_msg(
file_asm, line_idx, line, num_errors, msg
)
elif opcode == 'EXIT':
valid_opcode = True
opcode_val = util.opcode_lookup_dict['EXIT']
word0_second_half = opcode_val.zfill(4)
if len(args) > 0:
msg=f'no args after {opcode}'
num_errors = error_msg(
file_asm, line_idx, line, num_errors, msg
)
# write lines for LD and CMP
if valid_opcode and opcode not in ['LD', 'CMP']:
hex_file_str = write_two_lines_to_hexfile(
word0_first_half, word0_second_half,
word1, hex_file_str
)
return hex_file_str, num_errors
if __name__ == '__main__':
staticallyLinked = (True if '-s' in sys.argv else False)
asm_filenames_in_argv = [f for f in sys.argv if f.endswith('.asm')]
all_files_in_asm_folder = sorted([f for f in os.listdir('./asm') if f.endswith('.asm')])
filename_where_pc_starts = asm_filenames_in_argv[0]
if staticallyLinked:
# gather all files in /asm folder
asm_files_for_compile = all_files_in_asm_folder
else:
asm_files_for_compile = sorted(asm_filenames_in_argv)
# check if asm filenames are valid
for filename in asm_files_for_compile:
if filename not in all_files_in_asm_folder:
print(f'"{filename}" is not a file in /asm folder.')
# gather all labels across hex files
num_errors = 0
cumsum_hex_lines = 0
giant_hex_file_str = ''
all_lines_for_programs = []
filename_and_lines_pairs = []
for asm_file in asm_files_for_compile:
# set where PC starts
if asm_file == filename_where_pc_starts:
where_PC_starts = cumsum_hex_lines
lines_for_program, cumsum_hex_lines, num_errors = compute_label_indices_from_file(
'asm/'+asm_file, cumsum_hex_lines, num_errors
)
filename_and_lines_pairs.append((asm_file, lines_for_program))
for item in filename_and_lines_pairs:
asm_file = item[0]
lines_for_program = item[1]
hex_file_str, num_errors = validate_and_make_hexfile(
asm_file, lines_for_program, num_errors
)
giant_hex_file_str += hex_file_str
# compiler messages
print('\n%s error%s generated' %(num_errors, ('s' if num_errors!=1 else '')))
if num_errors == 0:
print(colored('pass', 'green'))
else:
print(colored('fail', 'red'))
# convert hexfile to byte array
binary_format = split_hexfile_into_byte_array(giant_hex_file_str[:-1])
# write bytearray to disk
f = open('bin/file.bin', 'w+b')
f.write(binary_format)
f.close()
# write starting Program Counter (PC)
f = open('start_pc.txt', 'w')
f.write(str(where_PC_starts))
f.close()