-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocessor.py
More file actions
418 lines (403 loc) · 16.3 KB
/
processor.py
File metadata and controls
418 lines (403 loc) · 16.3 KB
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
import copy
import memory
from instruction import Instruction
class Processor(object):
'''
It defines a 5-stage pipelined MIPS-lite 32 architecture processor. It has run() and print_stat functions
which lets the user run a complete trace of instructions and print processor stats. This class
supports forwarding and non-forwarding of instructions between EX and Memory stages by setting a forwarding flag.
Inputs:
memory_image: object of class Memory(holds the trace file)
forwarding: True if full-forwarding required. Default is False
debug: Prints all the instructions in each stage of pipeline. Default is False.
'''
def __init__(self,memory_image,forwarding=False,debug=False):
'''
Instantiate a new processor.
'''
self.mem_image = copy.deepcopy(memory_image)
self.original_mem = memory_image
self.forwarding = forwarding
self.debug=debug
self.frwrd_dict = dict()
self.regs = memory.registers()
self.data_list = [0,Instruction(None),Instruction(None),Instruction(None),Instruction(None)]
self.pc = 0
self.arith_opcode = ['Add','Addi','Sub','Subi','Mul','Muli']
self.logic_opcode = ['Or','Ori','And','Andi','Xor','Xori']
self.ld_opcode = ['Ldw','Stw']
self.ctrl_opcode = ['Bz','Beq','Jr','Halt']
self.r_type = ['Add','Sub','Mul','Or','And','Xor']
self.reg_change={}
self.ari_count = 0
self.logic_count = 0
self.ctrl_count = 0
self.ld_count = 0
self.stall_cycle=0
self.num_instructions = 0
self.hazards = 0
self.st_count = []
self.Halt = False
self.cycles=0
def run(self,):
'''
Runs the complete trace of instructions
'''
self.cycles = 0
while self.data_list[4].opcode!='Halt':
self.Write_back()
self.Memory_op()
self.Execute()
self.Instruction_decode()
self.Fetch()
self.data_list[0]=self.pc
self.cycles += 1
self.cycles += 1
return
def print_stats(self,):
'''
Prints all the stats of the processor.
'''
print(" \n ")
print("*"*10+" Forwarding: "+str(self.forwarding)+" "+"*"*10)
print(" \n ")
print("_"*10+"PC, Memory and Register state"+"_"*10)
print(" Program counter state: ",self.pc)
self.print_mem()
self.print_reg()
print(" \n ")
print("_"*10+"Instruction counts:"+"_"*10)
print(" Total number of instructions: ",self.num_instructions)
print(" Arithmetic instructions: ",self.ari_count)
print(" Logical instructions: ",self.logic_count)
print(" Memory access instructions: ",self.ld_count)
print(" Control transfer instructions: ",self.ctrl_count)
print(" \n ")
print("_"*10+"Timing output"+"_"*10)
print("Number of cycles required: ",self.cycles)
if not self.forwarding:
print("Number of average stalls required: ", sum(self.st_count)/self.hazards)
print("Total stalls: ", sum(self.st_count))
print("Number of hazards occurred: ", self.hazards)
print(" \n ")
return
def print_mem(self,):
keys = [k for k in self.original_mem.mem_trace if self.mem_image.mem_trace[k] != self.original_mem.mem_trace[k]]
dict1 = dict()
for k in keys:
dict1[k] = self.mem_image.mem_trace[k]
print(" Final memory state: ",dict1)
def print_reg(self,):
print(" Final register state: ",self.reg_change)
def _check_target(self,instr):
'''
Checks stalls in the Instruction Decode stage.
Input: instr: object of class Instruction
'''
if instr.opcode=='Halt':
return
if self.forwarding and ((instr.type=='R') or (instr.opcode in ['Beq','Stw'])):
for obj in self.data_list[3:]:
if obj.type=='I' and obj.opcode!='Ldw':
if obj.reg_rt==instr.reg_rs:
instr.rs = obj.rt
instr.frwd_rs = True
elif obj.reg_rt==instr.reg_rt:
instr.rt = obj.rt
instr.frwd_rt = True
elif obj.reg_rt==instr.reg_rs==instr.reg_rt:
instr.rs = obj.rt
instr.frwd_rs = True
instr.rt = obj.rt
instr.frwd_rt = True
elif obj.type=='R':
if obj.reg_rd==instr.reg_rs:
instr.rs = obj.rd
instr.frwd_rs = True
elif obj.reg_rd==instr.reg_rt:
instr.rt = obj.rd
instr.frwd_rt = True
elif obj.reg_rd==instr.reg_rs==instr.reg_rt:
instr.rs = obj.rd
instr.frwd_rs = True
instr.rt = obj.rd
instr.frwd_rt = True
elif obj.opcode=='Ldw':
if obj.reg_rt==instr.reg_rs:
if self.data_list[3:].index(obj)==1:
instr.rs = obj.rt
instr.frwd_rs = True
else:
self.stall_cycle = len(self.data_list[3:]) - self.data_list[3:].index(obj)-1
return
elif obj.reg_rt==instr.reg_rt:
if self.data_list[3:].index(obj)==1:
instr.rt = obj.rt
instr.frwd_rt = True
else:
self.stall_cycle = len(self.data_list[3:]) - self.data_list[3:].index(obj)-1
return
elif obj.reg_rt==instr.reg_rs==instr.reg_rt:
if self.data_list[3:].index(obj)==1:
instr.rs = obj.rt
instr.frwd_rs = True
instr.rt = obj.rt
instr.frwd_rt = True
else:
self.stall_cycle = len(self.data_list[3:]) - self.data_list[3:].index(obj)-1
return
elif self.forwarding and instr.type=='I':
for obj in self.data_list[3:]:
if obj.type=='I'and obj.opcode!='Ldw':
if obj.reg_rt==instr.reg_rs:
instr.rs = obj.rt
instr.frwd_rs =True
break
elif obj.type=='R':
if obj.reg_rd==instr.reg_rs:
instr.rs = obj.rd
instr.frwd_rs =True
break
elif obj.opcode=='Ldw':
if obj.reg_rt==instr.reg_rs:
if self.data_list[3:].index(obj)==1:
instr.rs = obj.rt
instr.frwd_rs = True
else:
self.stall_cycle = len(self.data_list[3:]) - self.data_list[3:].index(obj)-1
return
elif (instr.type=='R') or (instr.opcode in ['Beq','Stw']):
for obj in self.data_list[3:]:
if obj.type=='I':
if obj.reg_rt in [instr.reg_rs,instr.reg_rt]:
self.stall_cycle = len(self.data_list[3:]) - self.data_list[3:].index(obj)
return
elif obj.type=='R':
if obj.reg_rd in [instr.reg_rs,instr.reg_rt]:
self.stall_cycle = len(self.data_list[3:]) - self.data_list[3:].index(obj)
return
elif instr.type=='I':
for obj in self.data_list[3:]:
if obj.type=='I':
if obj.reg_rt==instr.reg_rs:
if self.forwarding:
instr.rs = obj.rt
instr.frwd_rs =True
break
else:
self.stall_cycle = len(self.data_list[3:]) - self.data_list[3:].index(obj)
return
elif obj.type=='R':
if obj.reg_rd==instr.reg_rs:
if self.forwarding:
instr.rs = obj.rd
instr.frwd_rs =True
break
else:
self.stall_cycle = len(self.data_list[3:]) - self.data_list[3:].index(obj)
return
return
def _getSignedNum(self,num, bitLength):
'''
Returns signed number.
Inputs: num: number
bitlength: number of bits for signed number
'''
mask = (2 ** bitLength) - 1
if num & (1 << (bitLength - 1)):
return num | ~mask
else:
return num & mask
def Fetch(self,):
'''
Instruction Fetch stage of the processor. Fetches address of instruction from the program counter.
'''
if self.debug:
print("In IF stage: ",self.data_list[0])
if self.Halt:
return
if self.data_list[0]==None:
self.data_list[1] = Instruction(None)
return
if self.stall_cycle>0:
self.stall_cycle = self.stall_cycle - 1
if self.debug:
print('stall cycles in IF: ',self.stall_cycle)
return
for inst in self.data_list[1:]:
if (self.data_list[0] == inst.x_addr) and inst.opcode=='Stw':
if self.forwarding:
self.data_list[1] = Instruction(hex(inst.rt)[2:])
self.pc +=4
return
else:
self.hazards += 1
self.stall_cycle = len(self.data_list[1:])-self.data_list[1:].index(inst)
self.st_count.append(self.stall_cycle)
if self.stall_cycle>0:
if self.debug:
print('stall cycles in IF: ',self.stall_cycle)
return
instr = self.mem_image.read_word(self.data_list[0])
self.data_list[1] = Instruction(instr)
self.pc += 4
return
def Instruction_decode(self,):
'''
Instruction Decode stage of the processor. Decodes the instruction from the IF stage.
'''
instr = self.data_list[1]
instr.decode()
if self.debug:
print("In ID stage: ",instr)
if instr.opcode==None:
self.data_list[2] = instr
return
if self.Halt:
self.data_list[2] = Instruction(None)
return
if self.stall_cycle>0:
if self.debug:
print('stall cycles in ID: ',self.stall_cycle)
self.data_list[2] = Instruction(None)
return
self._check_target(instr)
if self.stall_cycle>0:
if self.debug:
print('stall cycles in ID: ',self.stall_cycle)
self.hazards += 1
self.st_count.append(self.stall_cycle)
self.data_list[2] = Instruction(None)
return
if instr.opcode in self.arith_opcode:
self.ari_count += 1
elif instr.opcode in self.logic_opcode:
self.logic_count += 1
elif instr.opcode in self.ctrl_opcode:
self.ctrl_count += 1
elif instr.opcode in self.ld_opcode:
self.ld_count += 1
if instr.opcode=='Halt':
self.Halt = True
self.data_list[2] = instr
return
if instr.frwd_rs==False:
instr.rs = self.regs.read_reg(instr.reg_rs) if instr.opcode in self.logic_opcode else self._getSignedNum(self.regs.read_reg(instr.reg_rs),32)
if instr.opcode in ['Jr','Bz']:
self.data_list[2] = instr
return
if instr.frwd_rt==False:
if instr.opcode in self.r_type:
instr.rt = self._getSignedNum(self.regs.read_reg(instr.reg_rt), 32)
else:
if instr.opcode in ['Stw','Beq']:
instr.rt = self.regs.read_reg(instr.reg_rt)
self.data_list[2] = instr
return
def Execute(self,):
'''
Instruction Execute stage of the processor. It executes the instruction.
'''
instr = self.data_list[2]
if self.debug:
print("In EX stage: ",instr)
if instr.opcode==None:
self.num_instructions = self.num_instructions
self.data_list[3] = instr
return
else:
self.num_instructions += 1
if instr.opcode=='Halt':
self.data_list[3] = instr
return
if self.Halt:
return
if instr.opcode=='Jr':
self.pc = instr.rs
self.data_list[1] = Instruction(None)
self.data_list[0] = None
self.data_list[3] = instr
return
if instr.opcode=='Bz':
if instr.rs==0:
self.pc = self.pc - 8 + 4*instr.x_addr
self.data_list[1] = Instruction(None)
self.data_list[0] = None
self.data_list[3] = instr
return
if instr.opcode=='Beq':
if instr.rs==instr.rt:
self.pc = self.pc - 8 + 4*instr.imm
self.data_list[1] = Instruction(None)
self.data_list[0] = None
self.data_list[3] = instr
return
if instr.opcode=='Add':
instr.rd = self._getSignedNum(instr.rs + instr.rt,32)
elif instr.opcode=='Addi':
instr.rt = self._getSignedNum(instr.rs + instr.imm,32)
elif instr.opcode=='Sub':
instr.rd = self._getSignedNum(instr.rs - instr.rt,32)
elif instr.opcode=='Subi':
instr.rt = self._getSignedNum(instr.rs - instr.imm,32)
elif instr.opcode=='Mul':
instr.rd = self._getSignedNum(instr.rs * instr.rt,32)
elif instr.opcode=='Muli':
instr.rt = self._getSignedNum(instr.rs * instr.imm,32)
elif instr.opcode=='Or':
instr.rd = instr.rs | instr.rt
elif instr.opcode=='Ori':
instr.rt = instr.rs | instr.imm
elif instr.opcode=='And':
instr.rd = instr.rs & instr.rt
elif instr.opcode=='Andi':
instr.rt = instr.rs & instr.imm
elif instr.opcode=='Xor':
instr.rd = instr.rs ^ instr.rt
elif instr.opcode=='Xori':
instr.rt = instr.rs ^ instr.imm
elif instr.opcode=='Ldw':
instr.x_addr = self._getSignedNum(instr.rs + instr.imm,32)
elif instr.opcode=='Stw':
instr.x_addr = self._getSignedNum(instr.rs + instr.imm,32)
self.data_list[3] = instr
return
def Memory_op(self,):
'''
Memory stage of the processor. It loads or stores data from the memory.
'''
instr = self.data_list[3]
if self.debug:
print('In Mem stage: ',instr)
if instr.opcode==None:
self.data_list[4] = instr
return
if instr.opcode=='Ldw':
addr = instr.x_addr
instr.rt = int('{0:032b}'.format(int.from_bytes(bytes.fromhex(self.mem_image.read_word(addr)),byteorder='big',signed=True)),2)
elif instr.opcode=='Stw':
addr = instr.x_addr
data = instr.rt
self.mem_image.write_word(addr,data)
self.data_list[4] = instr
return
def Write_back(self,):
'''
Write Back stage of the processor. It writes the generated data back to the registers.
'''
instr = self.data_list[4]
if self.debug:
print('In WB stage: ',instr)
if instr.opcode==None:
pass
return
if (instr.opcode in ['Stw','Jr','Halt','Bz','Beq']):
pass
else:
if instr.opcode in self.r_type:
self.reg_change['R'+str(instr.reg_rd)] = instr.rd
self.regs.write_reg(instr.reg_rd,instr.rd)
else:
self.reg_change['R'+str(instr.reg_rt)] = instr.rt
self.regs.write_reg(instr.reg_rt,instr.rt)
return