CPU_simulation/main.py

106 lines
3.3 KiB
Python

import ast
def updateRAM(toupdate):
temp = ""
for i in toupdate:
temp = temp + i + ":" + toupdate[i] + "\n"
file = open("debugRAM", 'w')
file.write(temp)
file.close()
RAM = open("RAM", 'r')
RAM = RAM.read()
if RAM == "":
RAM = {}
for i in range(int('11111111', 2)):
RAM[str(i)] = "0x10000000"
updateRAM(RAM)
file = open("RAM", 'w')
file.write(str(RAM))
file.close()
exit()
else:
RAM = ast.literal_eval(RAM)
line = 0
registera = "0x10000000"
registerb = "0x10000000"
while True:
line = str(line)
command = RAM[line]
arg = RAM[str(int(line)+1)]
arg = arg[1:]
arg = arg[1:]
line = int(line)
line += 2
line = str(line)
print(command)
if command == "0x10010000":
registera = RAM[str(int(arg, 2))]
elif command == "0x10100000":
registerb = RAM[str(int(arg, 2))]
elif command == "0x10110000":
registera = RAM[str(int(RAM[str(int(arg, 2))], 2))]
elif command == "0x11111000":
registerb = RAM[str(int(RAM[str(int(arg, 2))], 2))]
elif command == "0x11010000":
RAM[str(int(arg, 2))] = registera
elif command == "0x11100000":
RAM[str(int(arg, 2))] = registerb
elif command == "0x11110000":
RAM[str(int(RAM[str(int(arg, 2))], 2))] = registera
elif command == "0x10000000":
RAM[str(int(RAM[str(int(arg, 2))], 2))] = registerb
elif command == "0x10001000":
registera = registerb
RAM[str(int(arg, 2))] = registera
elif command == "0x10010010":
registerb = registera
RAM[str(int(arg, 2))] = registera
elif command == "0x10011000":
if registera[0] == "1" and registerb[0] == "1":
RAM[str(int(arg, 2))] = "1x10000000"
else:
RAM[str(int(arg, 2))] = "0x10000000"
elif command == "0x10100000":
if registera[0] == "1" or registerb[0] == "1":
RAM[str(int(arg, 2))] = "1x10000000"
else:
RAM[str(int(arg, 2))] = "0x10000000"
elif command == "0x10101000":
if registera[0] == "1":
RAM[str(int(arg, 2))] = "0x10000000"
else:
RAM[str(int(arg, 2))] = "1x10000000"
elif command == "0x10110000":
if (registera[0] == "0" and registerb[0] == "0") or (registera[0] == "1" and registerb[0] == "1"):
RAM[str(int(arg, 2))] = "0x10000000"
else:
RAM[str(int(arg, 2))] = "1x10000000"
elif command == "0x10111000":
registera = RAM[str(int(arg, 2))]
elif command == "0x11000000":
RAM[str(int(arg, 2))] = bin(int(registera[2:], 2) + int(registerb[2:], 2))[2:]
elif command == "0x11001000":
line = int(arg, 2)
elif command == "0x11010000":
line = int(RAM[str(int(arg, 2))], 2)
elif command == "0x11011000":
if registera[0] == registerb[0]:
line = int(arg, 2)
elif command == "0x11100000":
if registera[0] > registerb[0]:
line = int(arg, 2)
elif command == "0x11101000":
if registera[0] < registerb[0]:
line = int(arg, 2)
elif command == "0x11100001":
print()
print("programm finished")
updateRAM(RAM)
exit()
updateRAM(RAM)
print()
print("programm finished")
updateRAM(RAM)