250 lines
7.1 KiB
Python
250 lines
7.1 KiB
Python
import os
|
|
import pickle
|
|
|
|
print("[asmTron by CyberCodeDragon]")
|
|
|
|
doc = {8:"""
|
|
0000 NOP
|
|
0001 LDA memory > A
|
|
0010 ADD A + memspace
|
|
0011 SUB A - memspace
|
|
0100 STA A > memory
|
|
0101 OUT A > OUT
|
|
0110 JMP Jump to adress
|
|
0111 LDI par > A
|
|
1000 JIF Jump if (JC)
|
|
1001 HLO H-mem[memspace] > A
|
|
1010 HST A > H-mem[memspace]
|
|
1011 INP IN > A
|
|
1100 JMA Jump to A
|
|
1111 HLT Halt
|
|
""", 16: """
|
|
0000 0000 NOP
|
|
0000 0001 LDA memory > A
|
|
0000 0010 ADD A + memspace
|
|
0000 0011 SUB A - memspace
|
|
0000 0100 STA A > memory
|
|
0000 0101 OUT A > OUT
|
|
0000 0110 JMP Jump to adress
|
|
0000 0111 LDI par > A
|
|
0000 1000 JIF Jump if (JC)
|
|
0000 1001 HLO H-mem[memspace] > A
|
|
0000 1010 HST A > H-mem[memspace]
|
|
0000 1011 INP IN > A
|
|
0000 1111 HLT Halt
|
|
"""}
|
|
|
|
#bits = int(raw_input("arch [8/16]> "))
|
|
bits = 8
|
|
|
|
pointer = 0
|
|
|
|
registerA = 0
|
|
out = 0
|
|
|
|
memory = []
|
|
|
|
for i in range(2**bits):
|
|
memory.append(0)
|
|
|
|
def toDec(binary):
|
|
try:
|
|
return int(binary, 2)
|
|
except:
|
|
return -1
|
|
|
|
def toBin(decimal):
|
|
return int(bin(decimal)[2:])
|
|
|
|
def padBin(binary,bit=bits):
|
|
return str(binary).zfill(bit)
|
|
|
|
def bin_input(display,bit=bits):
|
|
raw = raw_input(display)
|
|
if raw=="x":
|
|
return -1
|
|
raw = raw.replace(" ","")
|
|
ret = toDec(raw)
|
|
if ret>2**bit:
|
|
print("[!] input exceted maxial size for "+str(bit)+"-bit integer")
|
|
return bin_input(display)
|
|
if ret==-1:
|
|
print("[!] "+raw+" is not a valid binary number!")
|
|
return bin_input(display)
|
|
return ret
|
|
|
|
def parse(cmd,par,verbose=False,adress=0):
|
|
global registerA
|
|
global memory
|
|
global out
|
|
global pointer
|
|
v=verbose
|
|
cmd16 = cmd
|
|
cmd = cmd[-4:]
|
|
if cmd=="0000": #nop
|
|
if v: print padBin(toBin(adress),bit=bits/2)+": NOP"
|
|
elif cmd=="0001": #lda
|
|
registerA = memory[par]
|
|
if v: print padBin(toBin(adress),bit=bits/2)+": LDA memory["+padBin(toBin(par),bit=bits/2)+"] > Register A"
|
|
elif cmd=="0010": #add
|
|
registerA = (registerA + memory[par]) % 2**bits
|
|
if v: print padBin(toBin(adress))+": ADD Register A + memory["+padBin(toBin(par),bit=bits/2)+"]"
|
|
elif cmd=="0011": #sub
|
|
registerA = (registerA - memory[par])
|
|
if registerA<0:
|
|
registerA = 2**bits+registerA
|
|
if v: print padBin(toBin(adress),bit=bits/2)+": SUB Register A - memory["+padBin(toBin(par),bit=bits/2)+"]"
|
|
elif cmd=="0100": #sta
|
|
memory[par] = registerA
|
|
if v: print padBin(toBin(adress),bit=bits/2)+": STA Register A > memory["+padBin(toBin(par),bit=bits/2)+"]"
|
|
elif cmd=="0101": #out
|
|
out = registerA
|
|
print("[OUT] "+padBin(toBin(out)))
|
|
if v: print padBin(toBin(adress),bit=bits/2)+": OUT Register A > out"
|
|
elif cmd=="0110": #jmp
|
|
pointer = par - 1
|
|
if v: print padBin(toBin(adress),bit=bits/2)+": JMP "+padBin(toBin(par),bit=bits/2)
|
|
elif cmd=="0111": #ldi
|
|
registerA = par
|
|
if v: print padBin(toBin(adress),bit=bits/2)+": LDI "+padBin(toBin(par),bit=bits/2)+" > Register A"
|
|
elif cmd=="1000": #jif
|
|
if registerA>=(2**bits)/2:
|
|
pointer = par - 1
|
|
if v: print padBin(toBin(adress),bit=bits/2)+": JIF {TRUE} > "+padBin(toBin(par),bit=bits/2)
|
|
else:
|
|
if v: print padBin(toBin(adress),bit=bits/2)+": JIF {FALSE} > "+padBin(toBin(par),bit=bits/2)
|
|
elif cmd=="1001": #hlo
|
|
registerA = memory[memory[par]]
|
|
elif cmd=="1010": #hst
|
|
memory[memory[par]] = registerA
|
|
elif cmd=="1011": #inp
|
|
if v: print padBin(toBin(adress),bit=bits/2)+": INP input > Register A"
|
|
registerA = bin_input("[INPUT]> ")
|
|
elif cmd=="1100":
|
|
pointer = registerA - 1
|
|
elif cmd=="1111": #hlt
|
|
return -1
|
|
return True
|
|
|
|
if __name__=="__main__":
|
|
while True:
|
|
x=raw_input("#> ")
|
|
if x=="help" or x=="h":
|
|
print("Help Screen goes here")
|
|
elif x=="docs":
|
|
print(doc[bits])
|
|
elif x=="write" or x=="w":
|
|
adress=bin_input("adress> ")
|
|
value=bin_input("value> ")
|
|
try:
|
|
memory[adress] = value
|
|
except:
|
|
print("[!] Unknown error while writing")
|
|
elif x=="read" or x=="r":
|
|
adress=bin_input("adress> ")
|
|
print(padBin(toBin(memory[adress])))
|
|
elif x=="dump" or x=="d":
|
|
for m in range(len(memory)):
|
|
print(padBin(toBin(m),bit=bits/2)+": "+padBin(toBin(memory[m])))
|
|
elif x=="dump32" or x=="d32":
|
|
for m in range(32):
|
|
print(padBin(toBin(m),bit=bits/2)+": "+padBin(toBin(memory[m])))
|
|
elif x=="dumpSpace" or x=="ds":
|
|
begin=bin_input("adress> ")
|
|
length=bin_input("length> ")
|
|
for m in range(length):
|
|
print(padBin(toBin(begin+m),bit=bits/2)+": "+padBin(toBin(memory[begin+m])))
|
|
elif x=="run" or x=="u":
|
|
while True:
|
|
cmd = padBin(toBin(memory[pointer]))[:(bits/2)]
|
|
par = int(toDec(padBin(toBin(memory[pointer]))[-1*(bits/2):]))
|
|
if parse(cmd,par)==-1:
|
|
pointer = 0
|
|
break
|
|
pointer = (pointer + 1) % bits
|
|
elif x=="edit" or x=="e":
|
|
adress=bin_input("offset> ",bit=bits/2)
|
|
while True:
|
|
value=bin_input(padBin(toBin(adress),bit=bits/2)+": "+padBin(toBin(memory[adress]))+" > ")
|
|
if value==-1:
|
|
break
|
|
try:
|
|
memory[adress] = value
|
|
except:
|
|
print("[!] Unknown error while writing")
|
|
if adress==len(memory)-1:
|
|
adress = -1
|
|
print("[*] End of mem")
|
|
adress = adress + 1
|
|
elif x=="asm":
|
|
adress=bin_input("offset> ",bit=bits/2)
|
|
while True:
|
|
print("[!] Not yet implemented error")
|
|
break
|
|
toWrite=raw_input(padBin(toBin(adress),bit=bits/2)+" > ")
|
|
cmd,par = toWrite.split(" ")
|
|
if value==-1:
|
|
break
|
|
try:
|
|
memory[adress] = value
|
|
except:
|
|
print("[!] Unknown error while writing")
|
|
adress = adress + 1
|
|
elif x=="cls":
|
|
os.system("cls")
|
|
elif x=="clear":
|
|
pointer = 0
|
|
registerA = 0
|
|
out = 0
|
|
memory = []
|
|
for i in range(2**(bits/2)):
|
|
memory.append(0)
|
|
elif x=="step" or x=="s":
|
|
cmd = padBin(toBin(memory[pointer]))[:(bits/2)]
|
|
par = int(toDec(padBin(toBin(memory[pointer]))[-1*(bits/2):]))
|
|
if parse(cmd,par,verbose=True,adress=pointer)==-1:
|
|
pointer = 0
|
|
print("[*] Programm halted")
|
|
pointer = (pointer + 1) % bits
|
|
elif x=="debug":
|
|
while True:
|
|
cmd = padBin(toBin(memory[pointer]))[:(bits/2)]
|
|
par = int(toDec(padBin(toBin(memory[pointer]))[-1*(bits/2):]))
|
|
if parse(cmd,par,verbose=True,adress=pointer)==-1:
|
|
pointer = -1
|
|
print("[*] Programm halted")
|
|
break
|
|
pointer = (pointer + 1) % (2**bits/2)
|
|
elif x=="pointer":
|
|
print("POINTER: "+padBin(toBin(pointer),bit=bits/2))
|
|
elif x=="setPointer":
|
|
pointer = bin_input("pointer> ",bit=bits/2)
|
|
elif x=="register":
|
|
print("REGISTER: "+padBin(toBin(registerA)))
|
|
elif x=="setRegister":
|
|
registerA = bin_input("register> ")
|
|
elif x=="save":
|
|
name = raw_input("name> ")
|
|
with open(name+".asmTron", 'wb') as f:
|
|
pickle.dump([bits,memory],f)
|
|
elif x=="load":
|
|
print("[*] Avaible saves:")
|
|
for f in os.listdir("."):
|
|
if f[-8:]==".asmTron":
|
|
print("> "+f[:-8])
|
|
name = raw_input("name> ")
|
|
with open(name+".asmTron", "rb") as f:
|
|
newBits,memory = pickle.load(f)
|
|
if not newBits==bits:
|
|
print("[!] Error: Uncompatible arch: "+str(newBits))
|
|
exit()
|
|
if not pointer==0:
|
|
print("[*] Pointer has been set to "+str(pointer))
|
|
elif x=="code":
|
|
data=bin_input("code> ")
|
|
cmd = padBin(toBin(memory[pointer]))[:(bits/2)]
|
|
par = int(toDec(padBin(toBin(memory[pointer]))[-1*(bits/2):]))
|
|
parse(cmd,par,verbose=True,adress=0)
|
|
else:
|
|
print("[!] Error")
|