forked from mattbatwings/BatPU-2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassembler.py
More file actions
180 lines (150 loc) · 7.45 KB
/
assembler.py
File metadata and controls
180 lines (150 loc) · 7.45 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
def assemble(assembly_filename, output_filename):
assembly_file = open(assembly_filename, 'r')
machine_code_file = open(output_filename, 'w')
lines = (line.strip() for line in assembly_file)
# Remove comments and blanklines
for comment_symbol in ['/', ';', '#']:
lines = [line.split(comment_symbol)[0] for line in lines]
lines = [line for line in lines if line.strip()]
# Populate symbol table
symbols = {}
opcodes = ['nop', 'hlt', 'add', 'sub', 'nor', 'and', 'xor', 'rsh', 'ldi', 'adi', 'jmp', 'brh', 'cal', 'ret', 'lod', 'str']
for index, symbol in enumerate(opcodes):
symbols[symbol] = index
registers = ['r0', 'r1', 'r2', 'r3', 'r4', 'r5', 'r6', 'r7', 'r8', 'r9', 'r10', 'r11', 'r12', 'r13', 'r14', 'r15']
for index, symbol in enumerate(registers):
symbols[symbol] = index
conditions1 = ['eq', 'ne', 'ge', 'lt']
conditions2 = ['=', '!=', '>=', '<']
conditions3 = ['z', 'nz', 'c', 'nc']
conditions4 = ['zero', 'notzero', 'carry', 'notcarry']
for index, symbol in enumerate(conditions1):
symbols[symbol] = index
for index, symbol in enumerate(conditions2):
symbols[symbol] = index
for index, symbol in enumerate(conditions3):
symbols[symbol] = index
for index, symbol in enumerate(conditions4):
symbols[symbol] = index
ports = ['pixel_x', 'pixel_y', 'draw_pixel', 'clear_pixel', 'load_pixel', 'buffer_screen', 'clear_screen_buffer',
'write_char', 'buffer_chars', 'clear_chars_buffer', 'show_number', 'clear_number', 'signed_mode', 'unsigned_mode', 'rng', 'controller_input']
for index, symbol in enumerate(ports):
symbols[symbol] = index + 240
for i in range(256):
symbols[f'"{chr(i)}"'] = i
symbols[f"'{chr(i)}'"] = i
# Extract definitions and labels
def is_definition(word):
return word == 'define'
def is_label(word):
return word[0] == '.'
pc = 0
instructions = []
# Generate machine code
def resolve(word):
if word[0] in '-0123456789':
return int(word, 0)
if symbols.get(word) is None:
exit(f'Could not resolve {word}')
return symbols[word]
should_exit = 0
exit_mess = ""
for index, line in enumerate(lines):
words = [word.lower() for word in line.split()]
if is_definition(words[0]):
if(words[1] in symbols):
# Accumulate all duplicate definitions as the error message
exit_mess += f'Duplicate definition \'{words[1]}\' at line {index} before instruction {"%04d"%pc}!!\n'
# Instruction index is included because Ado's VM shows only the instruction indices
should_exit |= 2 # Signal dup. definitions
symbols[words[1]] = resolve(words[2]) # Support for any number fmt. as long as it has its base identifier
elif is_label(words[0]):
if(words[0] in symbols):
# Accumulate all duplicate labels as the error message
exit_mess += f'Duplicate label \'{words[0]}\' at line {index} before instruction {"%04d"%pc}!!\n'
# Instruction index is included because Ado's VM shows only the instruction indices
should_exit |= 1 # Signal dup. labels
symbols[words[0]] = pc
if len(words) > 1:
pc += 1
instructions.append(words[1:])
else:
pc += 1
instructions.append(words)
# Send them all at once
if(should_exit > 0):
exit_mess += ("Error: Duplicate labels.\n" * ((should_exit & 1) != 0)) + ("Error: Duplicate definitions.\n" * ((should_exit & 2) != 0))
exit(exit_mess)
for pc, words in enumerate(instructions):
# Resolve pseudo-instructions
if words[0] == 'cmp':
words = ['sub', words[1], words[2], registers[0]] # sub A B r0
elif words[0] == 'mov':
words = ['add', words[1], registers[0], words[2], ] # add A r0 dest
elif words[0] == 'lsh':
words = ['add', words[1], words[1], words[2]] # add A A dest
elif words[0] == 'inc':
words = ['adi', words[1], '1'] # adi dest 1
elif words[0] == 'dec':
words = ['adi', words[1], '-1'] # adi dest -1
elif words[0] == 'not':
words = ['nor', words[1], registers[0], words[2]] # nor A r0 dest
# lod/str optional offset
if words[0] in ['lod', 'str'] and len(words) == 3:
words.append('0')
# space special case
if words[-1] in ['"', "'"] and words[-2] in ['"', "'"]:
words = words[:-1]
words[-1] = "' '"
# Begin translation
opcode = words[0]
machine_code = symbols[opcode] << 12
words = [resolve(word) for word in words]
# Number of operands check
if opcode in ['nop', 'hlt', 'ret'] and len(words) != 1:
exit(f'Incorrect number of operands for {opcode} on line {pc}')
if opcode in ['jmp', 'cal'] and len(words) != 2:
exit(f'Incorrect number of operands for {opcode} on line {pc}')
if opcode in ['rsh', 'ldi', 'adi', 'brh'] and len(words) != 3:
exit(f'Incorrect number of operands for {opcode} on line {pc}')
if opcode in ['add', 'sub', 'nor', 'and', 'xor', 'lod', 'str'] and len(words) != 4:
exit(f'Incorrect number of operands for {opcode} on line {pc}')
# Reg A
if opcode in ['add', 'sub', 'nor', 'and', 'xor', 'rsh', 'ldi', 'adi', 'lod', 'str']:
if words[1] != (words[1] % (2 ** 4)):
exit(f'Invalid reg A for {opcode} on line {pc}')
machine_code |= (words[1] << 8)
# Reg B
if opcode in ['add', 'sub', 'nor', 'and', 'xor', 'lod', 'str']:
if words[2] != (words[2] % (2 ** 4)):
exit(f'Invalid reg B for {opcode} on line {pc}')
machine_code |= (words[2] << 4)
# Reg C
if opcode in ['add', 'sub', 'nor', 'and', 'xor', 'rsh']:
if words[-1] != (words[-1] % (2 ** 4)):
exit(f'Invalid reg C for {opcode} on line {pc}')
machine_code |= words[-1]
# Immediate
if opcode in ['ldi', 'adi']:
if words[2] < -128 or words[2] > 255: # 2s comp [-128, 127] or uint [0, 255]
exit(f'Invalid immediate for {opcode} on line {pc}')
machine_code |= words[2] & (2 ** 8 - 1)
# Instruction memory address
if opcode in ['jmp', 'brh', 'cal']:
if words[-1] != (words[-1] % (2 ** 10)):
exit(f'Invalid instruction memory address for {opcode} on line {pc}')
machine_code |= words[-1]
# Condition
if opcode in ['brh']:
if words[1] != (words[1] % (2 ** 2)):
exit(f'Invalid condition for {opcode} on line {pc}')
machine_code |= (words[1] << 10)
# Offset
if opcode in ['lod', 'str']:
if words[3] < -8 or words[3] > 7: # 2s comp [-8, 7]
exit(f'Invalid offset for {opcode} on line {pc}')
machine_code |= words[3] & (2 ** 4 - 1)
as_string = bin(machine_code)[2:].rjust(16, '0')
machine_code_file.write(f'{as_string}\n')
if __name__ == '__main__':
assemble('test.as', 'test.mc')