-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsimple.py
232 lines (189 loc) · 7.15 KB
/
simple.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import numpy as np
import assembler
import random
program = assembler.program
register_file = np.zeros(16, dtype=int)
memory = np.zeros(100, dtype=int)
for x in range(0, 10):
memory[x] = 10 - x
pc = 0
def printRegisterFile() :
print ""
i = 0
for x in register_file:
print "r" + str(i) + ":[" + str(x) + "]"
i = i+1
# raw_input("Press Enter to continue...")
def printMemory(fromIndex, toIndex):
print ""
i = 0
for x in memory[fromIndex:toIndex]:
print "m" + "[" + str(i) + "]" + ":[" + str(x) + "]"
i = i+1
# raw_input("Press Enter to continue...")
def fetch():
instruction = program[pc]
return instruction
def decode(instruction):
decoded = {}
decoded["opcode"] = instruction["opcode"]
# print str(instruction["opcode"])
if instruction["opcode"] == "sys":
return instruction
if "arg1" in instruction:
# print str(instruction["arg1"])
decoded["arg1"] = int(instruction["arg1"][1:])
if "arg2" in instruction:
# print str(instruction["arg2"])
decoded["arg2"] = int(instruction["arg2"][1:])
if "arg3" in instruction:
# print str(instruction["arg3"])
decoded["arg3"] = int(instruction["arg3"][1:])
return decoded
def execute(instruction):
intermediate = {}
value = None;
type = None;
id = None;
if instruction["opcode"] == "add" :
intermediate["value"] = register_file[instruction["arg2"]] + register_file[instruction["arg3"]]
intermediate["type"] = "register"
intermediate["id"] = instruction["arg1"]
return intermediate
elif instruction["opcode"] == "addi":
intermediate["value"] = register_file[instruction["arg2"]] + instruction["arg3"]
intermediate["type"] = "register"
intermediate["id"] = instruction["arg1"]
return intermediate
elif instruction["opcode"] == "blth":
if register_file[instruction["arg1"]] < 0:
intermediate["type"] = "pc"
intermediate["value"] = instruction["arg2"]
else :
intermediate["type"] = "noop"
return intermediate
elif instruction["opcode"] == "be":
if register_file[instruction["arg1"]] == 0:
intermediate["type"] = "pc"
intermediate["value"] = instruction["arg2"]
else :
intermediate["type"] = "noop"
return intermediate
elif instruction["opcode"] == "bgth":
if register_file[instruction["arg1"]] > 0:
intermediate["type"] = "pc"
intermediate["value"] = instruction["arg2"]
else :
intermediate["type"] = "noop"
return intermediate
elif instruction["opcode"] == "blthe":
if register_file[instruction["arg1"]] <= 0:
intermediate["type"] = "pc"
intermediate["value"] = instruction["arg2"]
else :
intermediate["type"] = "noop"
return intermediate
elif instruction["opcode"] == "bgthe":
if register_file[instruction["arg1"]] >= 0:
intermediate["type"] = "pc"
intermediate["value"] = instruction["arg2"]
else :
intermediate["type"] = "noop"
return intermediate
elif instruction["opcode"] == "j":
intermediate["type"] = "pc"
intermediate["value"] = instruction["arg1"]
return intermediate
elif instruction["opcode"] == "bne":
if register_file[instruction["arg1"]] != 0:
intermediate["type"] = "pc"
intermediate["value"] = instruction["arg2"]
else :
intermediate["type"] = "noop"
return intermediate
elif instruction["opcode"] == "sub" :
intermediate["value"] = register_file[instruction["arg2"]] - register_file[instruction["arg3"]]
intermediate["type"] = "register"
intermediate["id"] = instruction["arg1"]
return intermediate
# elif instruction["opcode"] == "idiv" :
# intermediate["value"] = int(register_file[instruction["arg2"]] / register_file[instruction["arg3"]])
# intermediate["type"] = "register"
# intermediate["id"] = instruction["arg1"]
# return intermediate
# elif instruction["opcode"] == "imul" :
# intermediate["value"] = int(register_file[instruction["arg2"]] * register_file[instruction["arg3"]])
# intermediate["type"] = "register"
# intermediate["id"] = instruction["arg1"]
# return intermediate
elif instruction["opcode"] == "subi" :
intermediate["value"] = register_file[instruction["arg2"]] - instruction["arg3"]
intermediate["type"] = "register"
intermediate["id"] = instruction["arg1"]
return intermediate
elif instruction["opcode"] == "ldc" :
intermediate["type"] = "register"
intermediate["id"] = instruction["arg1"]
intermediate["value"] = instruction["arg2"]
return intermediate
elif instruction["opcode"] == "ldr" :
intermediate["type"] = "register"
intermediate["id"] = instruction["arg1"]
intermediate["value"] = memory[register_file[instruction["arg2"]]]
return intermediate
elif instruction["opcode"] == "sto" :
intermediate["type"] = "memory"
intermediate["id"] = register_file[instruction["arg1"]]
intermediate["value"] = register_file[instruction["arg2"]]
return intermediate
elif instruction["opcode"] == "sys" :
if instruction["arg1"] == 'r':
printRegisterFile()
elif instruction["arg1"] == 'm':
printMemory(int(instruction["arg2"]),int(instruction["arg3"]))
intermediate["type"] = "noop"
return intermediate
elif instruction["opcode"] == "cmp" :
value = register_file[instruction["arg2"]] - register_file[instruction["arg3"]]
compare = None;
if value > 0 : compare = 1
elif value == 0 : compare = 0
elif value < 0 : compare = -1
intermediate["type"] = "register"
intermediate["id"] = instruction["arg1"]
intermediate["value"] = compare
return intermediate
else:
print "Cannot find execute for" + str(instruction)
def write_back(intermediate):
global pc
# print intermediate
if intermediate["type"] == "memory":
memory[intermediate["id"]] = intermediate["value"]
pc = pc + 1
elif intermediate["type"] == "register":
register_file[intermediate["id"]] = intermediate["value"]
pc = pc + 1
elif intermediate["type"] == "pc":
pc = intermediate["value"]
elif intermediate["type"] == "noop":
pc = pc + 1
def run():
global pc
i = 0
ins = 0
while(True):
instruction = fetch()
instruction = decode(instruction)
# print instruction
intermediate = execute(instruction)
# print intermediate
write_back(intermediate)
i += 4
ins += 1
if pc >= len(program):
break
print "Total clock cycles: %d" % (i)
ipc = float(ins)/float(i)
print "Instructions per cycle: %.2f" % (ipc)
run()