Skip to content

Instantly share code, notes, and snippets.

@Gamer92000
Created December 20, 2020 01:03
Show Gist options
  • Save Gamer92000/c7fb9b881de771a43801dabed838e8b0 to your computer and use it in GitHub Desktop.
Save Gamer92000/c7fb9b881de771a43801dabed838e8b0 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"]
def decode(instr):
OPC = (instr & 0b0111111000000000000000000) >> 18
A = (instr & 0b0000000111111000000000000) >> 12
B = (instr & 0b0000000000000111111000000) >> 6
C = (instr & 0b0000000000000000000111111)
if (OPC == 0): return "HALT"
OPC -= 1
op = OPC % 21
co = OPC // 21
cond = " "
if co == 1: cond = "+"
if co == 2: cond = "-"
opS = lookup[op]
aprev = "r"
bprev = "r"
cprev = "r"
if op in [1, 5, 7, 9]: cprev = "i"
if op == 18: bprev = "i"
stri = f"{cond} {opS.ljust(3)} {(aprev + str(A)).rjust(3)}, {(bprev + str(B)).rjust(3)}, {(cprev + str(C)).rjust(3)}"
# exceptions
if opS in ["lbl", "jup", "jdn"]:
if opS == "lbl": cprev = ""
stri = f"{cond} {opS.ljust(3)} {str(64*A + B).rjust(8)}, {(cprev + str(C)).rjust(3)}"
if opS == "cmp":
cc = A % 8
a = A // 8
opS += cmpluk[cc]
if a == 1: B,C = C,B
if a == 2: cprev = "i"
if a == 3: bprev = "i"
stri = f"{cond} {opS.ljust(8)} {(bprev + str(B)).rjust(3)}, {(cprev + str(C)).rjust(3)}"
if opS == ["mul", "sh/r"]:
# not neccessarry as not in rom
pass
return stri
bytesArr = []
with open("talkative-server-redacted.rom", "rb") as f:
byte = f.read(1)
while byte != b"":
bytesArr.append(byte.decode())
byte = f.read(1)
instArr = []
for i in range(0,len(bytesArr)-1, 4):
base = bytesArr[i] + bytesArr[i+1] + bytesArr[i+2] + bytesArr[i+3]
decoded = int.from_bytes(base64.b64decode(base), byteorder='big', signed=False)
decoded = decode(decoded)
print(decoded)
@Gamer92000
Copy link
Author

Disclaimer: This is not a full decompiler but just enough to get what is going on!

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