-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulator.py
executable file
·71 lines (61 loc) · 2 KB
/
simulator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/python
__author__ = 'marcus'
import sys, re
from base import Memory
from process import FileProcess
from instructions import Instruction
from syscall import Syscall
from commands import Command
def usage():
print "Usage: simulator [mode] [input file]"
print "Modes available: -d (debug), -n (normal)"
sys.exit(0)
def debug():
print "Debug mode is ON!"
print ""
if __name__ == "__main__":
print "Welcome to Mips Simulator by Marcus Gabilheri"
inputFile = []
mem = Memory()
fProcess = FileProcess(mem)
if len(sys.argv) != 3: usage()
if sys.argv[1] == "-d":
debug = True
elif sys.argv[1] == "-n":
debug = False
else:
usage()
lines = [line.strip() for line in open(sys.argv[2])]
for l in lines:
if not l:
continue
l = re.sub(' +', ',', l)
l = re.sub('\t+', ',', l)
line = l.split(",")
arr = []
for i in line:
if i != '':
arr.append(i)
inputFile.append(arr)
fProcess.process(inputFile)
s_call = Syscall(mem.get_registers(), mem)
ins = Instruction(mem.get_registers(), mem, s_call)
com = Command(mem.get_registers(), mem, ins)
#debug = False
while True:
if debug:
command = raw_input("")
print "PC: " + hex(mem.get_registers().PC)
print "Instruction Word: " + hex(mem.get_val_in_address(mem.get_registers().PC))
line = command.split(" ")
if len(line) > 2:
if line[1] == "mem":
com.execute_command(line[0] + " " + line[1], line[2])
elif line[1] == 'reg':
com.execute_command(line[0] + " " + line[1], line[2])
elif line[1] == 'inst':
com.execute_command(line[0] + " " + line[1], mem.get_val_in_address(mem.get_registers().PC))
continue
else:
pass
ins.evaluate(mem.get_val_in_address(mem.get_registers().PC))