Skip to content

Instantly share code, notes, and snippets.

@Gamer92000
Created December 20, 2020 01:25
Show Gist options
  • Save Gamer92000/99c0f47bccc3ecd54d03ed1b139ffd96 to your computer and use it in GitHub Desktop.
Save Gamer92000/99c0f47bccc3ecd54d03ed1b139ffd96 to your computer and use it in GitHub Desktop.
import base64
lookup = ["add", "add", "sub", "cmp", "or", "or", "xor", "xor", "and", "and", "sh/r", "shl", "shr", "ld", "st", "lbl", "jup", "jdn", "io", "mul"]
cmpluk = ["tr", "fa", "eq", "ne", "sl", "sg", "ul", "ug"]
class Instruction:
OPC = 0
A = 0
B = 0
C = 0
cond = 0
def __init__(self, string):
string = string.split(";")[0].strip()
if string.startswith("+"):
self.cond = 1
string = string.split("+")[1].strip()
if string.startswith("-"):
self.cond = 2
string = string.split("-")[1].strip()
OPCode = string.split(" ")[0]
if OPCode == "add":
self.A = int(string.split(" ")[1].split(",")[0][1:])
self.B = int(string.split(" ")[2].split(",")[0][1:])
self.C = int(string.split(" ")[3].split(",")[0][1:])
self.OPC = 0 + (1 if string.count("r")==2 else 0)
if OPCode == "sub":
self.A = int(string.split(" ")[1].split(",")[0][1:])
self.B = int(string.split(" ")[2].split(",")[0][1:])
self.C = int(string.split(" ")[3].split(",")[0][1:])
self.OPC = 2
if OPCode == "or":
self.A = int(string.split(" ")[1].split(",")[0][1:])
self.B = int(string.split(" ")[2].split(",")[0][1:])
self.C = int(string.split(" ")[3].split(",")[0][1:])
self.OPC = 4 + (1 if string.count("r")==2 else 0)
if OPCode == "xor":
self.A = int(string.split(" ")[1].split(",")[0][1:])
self.B = int(string.split(" ")[2].split(",")[0][1:])
self.C = int(string.split(" ")[3].split(",")[0][1:])
self.OPC = 6 + (1 if string.count("r")==2 else 0)
if OPCode == "and":
self.A = int(string.split(" ")[1].split(",")[0][1:])
self.B = int(string.split(" ")[2].split(",")[0][1:])
self.C = int(string.split(" ")[3].split(",")[0][1:])
self.OPC = 0o10 + (1 if string.count("r")==2 else 0)
if OPCode == "shl" and string.count("r") == 3:
self.A = int(string.split(" ")[1].split(",")[0][1:])
self.B = int(string.split(" ")[2].split(",")[0][1:])
self.C = int(string.split(" ")[3].split(",")[0][1:])
self.OPC = 0o13
if OPCode == "shr" and string.count("r") == 3:
self.A = int(string.split(" ")[1].split(",")[0][1:])
self.B = int(string.split(" ")[2].split(",")[0][1:])
self.C = int(string.split(" ")[3].split(",")[0][1:])
self.OPC = 0o14
if OPCode[:3] == "cmp":
self.OPC = 3
self.A = 0
s = string.split(" ", 1)[1]
self.A = cmpluk.index(string[3:5])
if "i" in s and "r" in s:
if s.index("r") < s.index("i"): self.A += 0o20
else: self.A += 0o30
self.B = int(string.split(" ")[1].split(",")[0][1:])
self.C = int(string.split(" ")[2].split(",")[0][1:])
if OPCode == "shl" and string.count("r") == 2:
self.OPC = 0o12
self.A = int(string.split(" ")[1].split(",")[0][1:])
self.B = int(string.split(" ")[2].split(",")[0][1:])
self.C = int(string.split(" ")[3].split(",")[0][1:])
if OPCode == "shr" and string.count("r") == 2:
self.OPC = 0o12
self.A = int(string.split(" ")[1].split(",")[0][1:])
self.B = int(string.split(" ")[2].split(",")[0][1:])
self.C = int(string.split(" ")[3].split(",")[0][1:]) + 0o10
if OPCode == "sar":
self.OPC = 0o12
self.A = int(string.split(" ")[1].split(",")[0][1:])
self.B = int(string.split(" ")[2].split(",")[0][1:])
self.C = int(string.split(" ")[3].split(",")[0][1:]) + 0o20
if OPCode == "rol":
self.OPC = 0o12
self.A = int(string.split(" ")[1].split(",")[0][1:])
self.B = int(string.split(" ")[2].split(",")[0][1:])
self.C = int(string.split(" ")[3].split(",")[0][1:]) + 0o30
if OPCode == "ld":
self.OPC = 0o15
self.A = int(string.split(" ")[1].split(",")[0][1:])
self.B = int(string.split("[")[1].split("+")[0].split("]")[0]) if "r" in string else 0
self.C = 0 if (not "+" in string) and "r" in string else int(string.split("[")[1].split("+")[-1].split("]")[0])
if OPCode == "st":
self.OPC = 0o16
self.A = int(string.split(" ")[2][1:])
self.B = int(string.split("[")[1].split("+")[0].split("]")[0]) if "r" in string else 0
self.C = 0 if (not "+" in string) and "r" in string else int(string.split("[")[1].split("+")[-1].split("]")[0])
if OPCode[:3] == "fmu":
self.OPC = 0o23
self.A = int(string.split(" ")[1].split(",")[0][1:])
self.B = int(string.split(" ")[2].split(",")[0][1:])
self.C = int(string.split("/")[1].split(" ")[0])
if OPCode[:3] == "fms":
self.OPC = 0o23
self.A = int(string.split(" ")[1].split(",")[0][1:])
self.B = int(string.split(" ")[2].split(",")[0][1:])
self.C = int(string.split("/")[1].split(" ")[0]) + 0o20
if OPCode == "lbl":
self.OPC = 0o17
self.A = int(string.split(" ")[1].split(",")[0])//64
self.B = int(string.split(" ")[1].split(",")[0])%64
self.C = int(string.split(" ")[2])
if OPCode == "jup":
self.OPC = 0o20
self.A = int(string.split(" ")[1].split(",")[0])//64
self.B = int(string.split(" ")[1].split(",")[0])%64
self.C = int(string.split(" ")[2][1:])
if OPCode == "jdn":
self.OPC = 0o21
self.A = int(string.split(" ")[1].split(",")[0])//64
self.B = int(string.split(" ")[1].split(",")[0])%64
self.C = int(string.split(" ")[2][1:])
if OPCode == "io":
self.OPC = 0o22
self.A = int(string.split(" ")[1].split(",")[0][1:])
self.B = int(string.split(" ")[2].split(",")[0][1:])
self.C = int(string.split(" ")[3].split(",")[0][1:])
def toBin(self):
OPC = 21*self.cond + self.OPC + 1
instr = OPC << 18
instr += self.A << 12
instr += self.B << 6
instr += self.C
return instr
@Gamer92000
Copy link
Author

Disclaimer: This compiler requires strict placement of spaces and does not include all special cases listed in the EMU 1.0 manual.
It is still capable of compiling all instructions though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment