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.

281 lines
7.3 KiB

#!/bin/env python3
#compile RGPC1 Assembly
import sys
def getreg(g):
temp = g
if registers.get(instruction[1], 'fail') != 'fail':
temp = temp + registers.get(instruction[1]) + ';'
temp = temp.replace('a', '1')
else:
binified = format(int(instruction[1]), '08b')[:8].replace('-', '1')
temp = temp + binified + ';'
temp = temp.replace('a', '0')
if registers.get(instruction[2], 'fail') != 'fail':
temp = temp + registers.get(instruction[2]) + ';'
temp = temp.replace('b', '1')
else:
binified = format(int(instruction[2]), '08b')[:8].replace('-', '1')
temp = temp + binified + ';'
temp = temp.replace('b', '0')
return temp
def getjmp(g):
temp = g
if registers.get(instruction[1], 'fail') != 'fail':
temp = temp + registers.get(instruction[1]) + ';'
temp = temp.replace('a', '1')
else:
binified = format(int(instruction[1]), '08b')[:8].replace('-', '1')
temp = temp + binified + ';'
temp = temp.replace('a', '0')
if markers.get(instruction[2], 'fail') != 'fail':
temp = temp + format(int(markers.get(instruction[2])), '08b')
temp = temp.replace('a', '1')
else:
binified = format(int(instruction[2]), '08b')[:8].replace('-', '1')
temp = temp + binified + ';'
temp = temp.replace('a', '0')
return temp + ';'
verbose = 0
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 = 1
file = open(sys.argv[1])
fullfile = file.readlines()
file.close()
output = open(sys.argv[1].strip('.ra') + '.out', 'w+')
line = 0
markers = {}
registers = {
#normal
'acc': '01000000',
'a': '00100000',
'b': '01100000',
'c': '00010000',
#special
'zero': '00000000',
'full': '00000001',
'dbgo': '00000010',
'dbgi': '00000011',
'pgcr': '00000100'}
for i in fullfile:
line = line + 1
instruction = i.strip()
instruction = instruction.lower().split(' ')
if verbose == 1:
print(instruction)
if instruction[0][:1] == '!':
line = line - 1
markers[instruction[0][1:].strip('\n')] = line
if verbose == 1:
print("marker")
if verbose == 1:
print(markers)
for i in fullfile:
temp = None
binified = None
line = line + 1
instruction = i.strip()
instruction = instruction.lower().split(' ')
if verbose == 1:
print(instruction)
if instruction[0] == 'nul':
if verbose == 1:
print("nul")
output.write('00000000;00000000;00000000\n')
elif instruction[0] == 'nop':
if verbose == 1:
print("nop")
output.write('00101000\n')
elif instruction[0] == 'mov':
temp = '010000ab;'
temp = getreg(temp)
if verbose == 1:
print("mov")
print(temp)
output.write(temp + '\n')
elif instruction[0] == 'add':
temp = '11000yab;'
temp = getreg(temp)
if len(instruction) > 3:
if instruction[3] == 'not':
temp = temp.replace('y', '1')
else:
temp = temp.replace('y', '0')
if verbose == 1:
print("add")
print(temp)
output.write(temp + '\n')
elif instruction[0] == 'sub':
temp = '10100yab;'
temp = getreg(temp)
if len(instruction) > 3:
if instruction[3] == 'not':
temp = temp.replace('y', '1')
else:
temp = temp.replace('y', '0')
if verbose == 1:
print("sub")
print(temp)
output.write(temp + '\n')
elif instruction[0] == 'or':
temp = '11100yab;'
temp = getreg(temp)
if len(instruction) > 3:
if instruction[3] == 'not':
temp = temp.replace('y', '1')
else:
temp = temp.replace('y', '0')
if verbose == 1:
print("or")
print(temp)
output.write(temp + '\n')
elif instruction[0] == 'xor':
temp = '10010yab;'
temp = getreg(temp)
if len(instruction) > 3:
if instruction[3] == 'not':
temp = temp.replace('y', '1')
else:
temp = temp.replace('y', '0')
if verbose == 1:
print("xor")
print(temp)
output.write(temp + '\n')
elif instruction[0] == 'and':
temp = '11010yab;'
temp = getreg(temp)
if len(instruction) > 3:
if instruction[3] == 'not':
temp = temp.replace('y', '1')
else:
temp = temp.replace('y', '0')
if verbose == 1:
print("and")
print(temp)
output.write(temp + '\n')
elif instruction[0] == 'jmp':
temp = '011000a0;'
if markers.get(instruction[1], 'fail') != 'fail':
temp = temp + format(int(markers.get(instruction[1])), '08b')
temp = temp.replace('a', '1')
else:
binified = format(int(instruction[1]), '08b')[:8].replace('-', '1')
temp = temp + binified + ';'
temp = temp.replace('a', '0')
temp = temp + ';00000000;'
if verbose == 1:
print("jmp")
print(temp)
output.write(temp + '\n')
elif instruction[0] == 'jnz':
temp = '000100a0;'
temp = getjmp(temp)
if verbose == 1:
print("jnz")
print(temp)
output.write(temp + '\n')
elif instruction[0] == 'jgz':
temp = '010100a0;'
temp = getjmp(temp)
if verbose == 1:
print("jgz")
print(temp)
output.write(temp + '\n')
elif instruction[0] == 'jlz':
temp = '001100a0;'
temp = getjmp(temp)
if verbose == 1:
print("jlz")
print(temp)
output.write(temp + '\n')
elif instruction[0] == 'jez':
temp = '011100a0;'
temp = getjmp(temp)
if verbose == 1:
print("jez")
print(temp)
output.write(temp + '\n')
elif instruction[0] == 'dbl':
temp = '00001000;00000000;00000000;'
if verbose == 1:
print("dbl")
print(temp)
output.write(temp + '\n')
elif instruction[0] == 'dbb':
temp = '01001000;00000000;00000000;'
if verbose == 1:
print("dbb")
print(temp)
output.write(temp + '\n')
elif instruction[0] == 'hlt':
temp = '00000100;00000000;00000000;'
if verbose == 1:
print("hlt")
print(temp)
output.write(temp + '\n')
elif instruction[0] == '':
temp = '00000000;00000000;00000000;'
if verbose == 1:
print("newline")
print(temp)
output.write(temp + '\n')
elif instruction[0][:1] == '!':
if verbose == 1:
print("marker")
print("Ignoring this")
line = line - 1
pass
else:
print("Error!")
print("Line: " + str(line))
print("Content: " + i)
quit()
output.close()
print("All done!")
print("Data written to: " + sys.argv[1].strip('.ra') + '.out')