You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
190 lines
5.3 KiB
190 lines
5.3 KiB
#!/bin/env python3
|
|
import sys
|
|
|
|
def get_oreg(inst, regarg):
|
|
inst = inst.replace("outr", registers.get(regarg, '0000'))
|
|
return inst
|
|
|
|
def get_areg(inst, regarg):
|
|
inst = inst.replace("areg", registers.get(regarg, '0000'))
|
|
return inst
|
|
|
|
def get_breg(inst, regarg):
|
|
inst = inst.replace("breg", registers.get(regarg, '0000'))
|
|
return inst
|
|
|
|
def replace_args313(instout):
|
|
instout = get_oreg(instout, instruction[3])
|
|
instout = get_areg(instout, instruction[1])
|
|
instout = get_breg(instout, instruction[2])
|
|
return instout
|
|
|
|
verbose = False
|
|
if len(sys.argv) < 2:
|
|
print("Add arguments!")
|
|
quit()
|
|
if len(sys.argv) > 2:
|
|
if sys.argv[2] == 'v' or sys.argv[2] == '--verbose':
|
|
print("Running in verbose")
|
|
verbose = True
|
|
|
|
file = open(sys.argv[1], 'r')
|
|
fullfile = file.readlines()
|
|
file.close()
|
|
output = open(sys.argv[1].replace('.rasm', '.out'), 'w')
|
|
|
|
markers = {}
|
|
registers = {
|
|
'zero': '0000',
|
|
'g1': '0001',
|
|
'g2': '0010',
|
|
'g3': '0011',
|
|
'g4': '0100',
|
|
'outp': '0101',
|
|
'pgcr': '0110',
|
|
'g5': '0111',
|
|
'g6': '1000',
|
|
'gpio1': '1001',
|
|
'gpio2': '1010',
|
|
'gpio3': '1011',
|
|
'gpio4': '1100',
|
|
'g7': '1101',
|
|
'g8': '1110',
|
|
'rand': '1111'
|
|
}
|
|
|
|
print("Finding markers")
|
|
line = 0
|
|
for i in fullfile:
|
|
line = line + 1
|
|
instruction = i.strip()
|
|
instruction = instruction.lower().split(' ')
|
|
if verbose == True:
|
|
print(instruction)
|
|
|
|
if instruction[0][:1] == '!':
|
|
line = line - 1
|
|
markers[instruction[0][1:].strip('\n')] = line
|
|
if verbose == True:
|
|
print("marker")
|
|
|
|
if verbose == True:
|
|
print("Markers:")
|
|
print(markers)
|
|
|
|
print("Compiling source code")
|
|
line = 0
|
|
|
|
for i in fullfile:
|
|
temp = None
|
|
instout = None
|
|
binified = None
|
|
line = line + 1
|
|
instruction = i.strip()
|
|
instruction = instruction.lower().split(' ')
|
|
|
|
if verbose == True:
|
|
print(instruction)
|
|
|
|
if instruction[0] == '' or instruction[0][0] == "#" or instruction[0][0] == "!":
|
|
if verbose == True:
|
|
print(instruction)
|
|
pass
|
|
|
|
elif instruction[0] == 'ldi':
|
|
instout = "0110;outr;binified\n"
|
|
instout = get_oreg(instout, instruction[2])
|
|
binified = format(int(instruction[1]) & 0xffff, 'b')
|
|
instout = instout.replace("binified", binified)
|
|
if verbose == True:
|
|
print(instout)
|
|
output.write(instout)
|
|
|
|
elif instruction[0] == 'add':
|
|
instout = "0010;outr;areg;breg;0000;0000\n"
|
|
instout = replace_args313(instout)
|
|
if verbose == True:
|
|
print(instout)
|
|
output.write(instout)
|
|
|
|
elif instruction[0] == 'sub':
|
|
instout = "0011;outr;areg;breg;0000;0000\n"
|
|
instout = replace_args313(instout)
|
|
if verbose == True:
|
|
print(instout)
|
|
output.write(instout)
|
|
|
|
elif instruction[0] == 'mov':
|
|
instout = "0001;outr;areg;0000;0000;0000\n"
|
|
instout = get_oreg(instout, instruction[2])
|
|
instout = get_areg(instout, instruction[1])
|
|
if verbose == True:
|
|
print(instout)
|
|
output.write(instout)
|
|
|
|
elif instruction[0] == 'or':
|
|
instout = "0001;outr;areg;breg;0000;0000\n"
|
|
instout = replace_args313(instout)
|
|
if verbose == True:
|
|
print(instout)
|
|
output.write(instout)
|
|
|
|
elif instruction[0] == 'nor':
|
|
instout = "1001;outr;areg;breg;0000;0000\n"
|
|
instout = replace_args313(instout)
|
|
if verbose == True:
|
|
print(instout)
|
|
output.write(instout)
|
|
|
|
elif instruction[0] == 'and':
|
|
instout = "0100;outr;areg;breg;0000;0000\n"
|
|
instout = replace_args313(instout)
|
|
if verbose == True:
|
|
print(instout)
|
|
output.write(instout)
|
|
|
|
elif instruction[0] == 'nand':
|
|
instout = "1100;outr;areg;breg;0000;0000\n"
|
|
instout = replace_args313(instout)
|
|
if verbose == True:
|
|
print(instout)
|
|
output.write(instout)
|
|
|
|
elif instruction[0] == 'xor':
|
|
instout = "0101;outr;areg;breg;0000;0000\n"
|
|
instout = replace_args313(instout)
|
|
if verbose == True:
|
|
print(instout)
|
|
output.write(instout)
|
|
|
|
elif instruction[0] == 'xnor':
|
|
instout = "1101;outr;areg;breg;0000;0000\n"
|
|
instout = replace_args312(instout)
|
|
if verbose == True:
|
|
print(instout)
|
|
output.write(instout)
|
|
|
|
elif instruction[0] == 'jmp':
|
|
if markers.get(instruction[1], "0") != "0":
|
|
instout = "0110;0110;binified;0000;0000;0000\n"
|
|
binified = format(int(markers.get(instruction[1])) & 0xffff, 'b')
|
|
instout = instout.replace("binified", binified)
|
|
if verbose == True:
|
|
print(instout)
|
|
output.write(instout)
|
|
else:
|
|
print("Not added yet! you can only unconditionally jump to markers")
|
|
|
|
else:
|
|
print("Error! on line: " + str(line))
|
|
print(instruction)
|
|
if verbose != True:
|
|
exit(1)
|
|
|
|
# TODO Make the compiler do conditional jumps and jumps with immediate values
|
|
# also make sure the user knows you must let the compiler use a register for conditional jumps
|
|
|
|
|
|
output.close()
|
|
print("Done!")
|