-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstage.py
408 lines (273 loc) · 11.7 KB
/
stage.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
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
from config import *
from hazard import *
from icache import *
from dcache import *
import executable
class Stage:
def __init__(self, instruction):
self.instruction = instruction
self.hazard = Hazard()
class FetchStage(Stage):
def __init__(self, instruction):
Stage.__init__(self, instruction)
self.name = 'IF'
self.cache_hit, self.cycles = ICache.read(self.instruction.address)
def run(self, instruction):
STAGE['IF'] = BUSY
if not self.cache_hit and STAGE['DBUS'] == FREE and self.cycles > 0:
STAGE['IBUS'] = BUSY
if not self.cache_hit and MemoryStage.first_bus_access:
STAGE['IBUS'] = BUSY
STAGE['DBUS'] = FREE
MemoryStage.first_bus_access = False
if self.cache_hit or STAGE['IBUS'] == BUSY:
self.cycles -= 1
def next(self):
if self.cycles == 0:
STAGE['IBUS'] = FREE
if self.cycles <= 0 and REGISTER['FLUSH']:
STAGE['IF'] = FREE
STAGE['IBUS'] = FREE
REGISTER['FLUSH'] = False
return None
if self.cycles <= 0 and STAGE['ID'] == FREE:
STAGE['IF'] = FREE
return DecodeStage(self.instruction)
return self
class DecodeStage(Stage):
def __init__(self, instruction):
Stage.__init__(self, instruction)
self.name = 'ID'
def run(self, instruction):
STAGE['ID'] = BUSY
def next(self):
func_unit = self.instruction.functional_unit()
self._detect_hazard(func_unit)
if func_unit == 'NONE' and not self._is_hazard(func_unit):
self._execute_branch_instruction()
STAGE['ID'] = FREE
return None
if not self._is_hazard(func_unit):
STAGE['ID'] = FREE
return self._execution_stage(func_unit)
return self
def _execution_stage(self, func_unit):
if func_unit == 'FP_ADD':
return FPAddStage(self.instruction)
elif func_unit == 'FP_MUL':
return FPMulStage(self.instruction)
elif func_unit == 'FP_DIV':
return FPDivStage(self.instruction)
else:
return ExecuteStage(self.instruction)
def _execute_branch_instruction(self):
if self.instruction.name == 'J':
REGISTER['PC'] = self.instruction.immediate / 4
REGISTER['FLUSH'] = True
elif self.instruction.name == 'BEQ':
if REGISTER[self.instruction.src_reg[0]] == REGISTER[self.instruction.src_reg[1]]:
REGISTER['PC'] = self.instruction.immediate / 4
REGISTER['FLUSH'] = True
elif self.instruction.name == 'BNE':
if REGISTER[self.instruction.src_reg[0]] != REGISTER[self.instruction.src_reg[1]]:
REGISTER['PC'] = self.instruction.immediate / 4
REGISTER['FLUSH'] = True
def _detect_hazard(self, func_unit):
if not self._is_hazard(func_unit):
return
self.hazard.waw = False
if self.instruction.dest_reg != '' and REGISTER_STATUS[self.instruction.dest_reg] == BUSY:
self.hazard.waw = True
self.hazard.raw = False
for reg in self.instruction.src_reg:
if REGISTER_STATUS[reg] == BUSY:
self.hazard.raw = True
self.hazard.struct = False
if func_unit != 'NONE' and STAGE[func_unit] == BUSY:
self.hazard.struct = True
def _is_hazard(self, func_unit):
if self.instruction.dest_reg != '' and REGISTER_STATUS[self.instruction.dest_reg] == BUSY:
return True
for reg in self.instruction.src_reg:
if REGISTER_STATUS[reg] == BUSY:
return True
if func_unit != 'NONE' and STAGE[func_unit] == BUSY:
return True
return False
class ExecuteStage(Stage):
def __init__(self, instruction):
Stage.__init__(self, instruction)
self.name = 'EX'
def run(self, instruction):
if STAGE['IU'] != BUSY:
self._block_register()
self._execute()
STAGE['IU'] = BUSY
def next(self):
if STAGE['MEM'] == FREE:
STAGE['IU'] = FREE
return MemoryStage(self.instruction)
self.hazard.struct = True
return self
def _bus_hazard(self):
address = 0
if self.instruction.name in ['LW', 'L.D']:
address = int(self.instruction.offset) + REGISTER[self.instruction.src_reg[0]]
elif self.instruction.name in ['SW', 'S.D']:
address = int(self.instruction.offset) + REGISTER[self.instruction.src_reg[1]]
if self.instruction.name in ['LW', 'L.D', 'SW', 'S.D']:
if not DCache.is_hit(address) and STAGE['IBUS'] == BUSY:
return True
return False
def _execute(self):
instr = self.instruction
if instr.name == 'DADD':
REGISTER[instr.dest_reg] = REGISTER[instr.src_reg[0]] + REGISTER[instr.src_reg[1]]
elif instr.name == 'DADDI':
REGISTER[instr.dest_reg] = REGISTER[instr.src_reg[0]] + int(instr.immediate)
elif instr.name == 'DSUB':
REGISTER[instr.dest_reg] = REGISTER[instr.src_reg[0]] - REGISTER[instr.src_reg[1]]
elif instr.name == 'DSUBI':
REGISTER[instr.dest_reg] = REGISTER[instr.src_reg[0]] - int(instr.immediate)
elif instr.name == 'AND':
REGISTER[instr.dest_reg] = REGISTER[instr.src_reg[0]] & REGISTER[instr.src_reg[1]]
elif instr.name == 'ANDI':
REGISTER[instr.dest_reg] = REGISTER[instr.src_reg[0]] & int(instr.immediate)
elif instr.name == 'OR':
REGISTER[instr.dest_reg] = REGISTER[instr.src_reg[0]] | REGISTER[instr.src_reg[1]]
elif instr.name == 'ORI':
REGISTER[instr.dest_reg] = REGISTER[instr.src_reg[0]] | int(instr.immediate)
def _block_register(self):
if self.instruction.dest_reg != '':
REGISTER_STATUS[self.instruction.dest_reg] = BUSY
class MemoryStage(ExecuteStage):
first_bus_access = False
def __init__(self, instruction):
ExecuteStage.__init__(self, instruction)
self.stalled_for_ibus = False
self.first_word_hit = True
self.second_word_hit = True
self.first_word_cycles, self.second_word_cycles = self._calc_memory_cycles()
def run(self, instruction):
if not self.stalled_for_ibus:
if not self.first_word_hit:
MemoryStage.first_bus_access = True
self.stalled_for_ibus = True
if not self.second_word_hit and self.first_word_cycles == 0:
MemoryStage.first_bus_access = True
self.stalled_for_ibus = True
else:
MemoryStage.first_bus_access = False
if MemoryStage.first_bus_access and STAGE['IBUS'] == BUSY:
if not self.first_word_hit:
self.first_word_cycles -= 1
elif not self.second_word_hit:
self.second_word_cycles -= 1
if self.first_word_cycles == 0:
if not self.second_word_hit and STAGE['IBUS'] == FREE:
STAGE['DBUS'] = BUSY
if self.second_word_hit or STAGE['DBUS'] == BUSY:
self.second_word_cycles -= 1
STAGE['MEM'] = BUSY
if not self.first_word_hit and STAGE['IBUS'] == FREE and self.first_word_cycles > 0:
STAGE['DBUS'] = BUSY
if (self.first_word_hit or STAGE['DBUS'] == BUSY) and self.first_word_cycles > 0:
self.first_word_cycles -= 1
def next(self):
if not self.first_word_hit and self.first_word_cycles == 0 and self.second_word_hit:
STAGE['DBUS'] = FREE
if not self.second_word_hit and self.second_word_cycles == 0:
STAGE['DBUS'] = FREE
if self.second_word_cycles < 0:
self.hazard.struct = True
if (self.first_word_cycles + self.second_word_cycles) <= 0 and STAGE['WB'] == FREE:
STAGE['MEM'] = FREE
return WriteBackStage(self.instruction)
return self
def _calc_memory_cycles(self):
if self.instruction.name == 'LW':
address = int(self.instruction.offset) + REGISTER[self.instruction.src_reg[0]]
self.first_word_hit, REGISTER[self.instruction.dest_reg], cycles = DCache.read(address)
return cycles, 0
elif self.instruction.name == 'L.D':
address = int(self.instruction.offset) + REGISTER[self.instruction.src_reg[0]]
self.first_word_hit, word, first_word_read_time = DCache.read(address)
self.second_word_hit, word, second_word_read_time = DCache.read(address + 4)
return first_word_read_time, second_word_read_time
elif self.instruction.name == 'SW':
address = int(self.instruction.offset) + REGISTER[self.instruction.src_reg[1]]
self.first_word_hit, cycles = DCache.write(address, REGISTER[self.instruction.src_reg[0]])
return cycles, 0
elif self.instruction.name == 'S.D':
address = int(self.instruction.offset) + REGISTER[self.instruction.src_reg[1]]
self.first_word_hit, first_word_write_time = DCache.write(address, 0, False)
self.second_word_hit, second_word_write_time = DCache.write(address + 4, 0, False)
return first_word_write_time, second_word_write_time
return 1, 0
class FPAddStage(ExecuteStage):
def __init__(self, instruction):
ExecuteStage.__init__(self, instruction)
self.cycles = FP_ADD['CYCLES']
def run(self, instruction):
if self.cycles == FP_ADD['CYCLES']:
self._block_register()
STAGE['FP_ADD'] = BUSY
self.cycles -= 1
def next(self):
if self.cycles < 0:
self.hazard.struct = True
if FP_ADD['PIPELINED']:
STAGE['FP_ADD'] = FREE
if self.cycles <= 0 and STAGE['WB'] == FREE:
STAGE['FP_ADD'] = FREE
return WriteBackStage(self.instruction)
return self
class FPMulStage(ExecuteStage):
def __init__(self, instruction):
ExecuteStage.__init__(self, instruction)
self.cycles = FP_MUL['CYCLES']
def run(self, instruction):
if self.cycles == FP_MUL['CYCLES']:
self._block_register()
STAGE['FP_MUL'] = BUSY
self.cycles -= 1
def next(self):
if self.cycles < 0:
self.hazard.struct = True
if FP_MUL['PIPELINED']:
STAGE['FP_MUL'] = FREE
if self.cycles <= 0 and STAGE['WB'] == FREE:
STAGE['FP_MUL'] = FREE
return WriteBackStage(self.instruction)
return self
class FPDivStage(ExecuteStage):
def __init__(self, instruction):
ExecuteStage.__init__(self, instruction)
self.cycles = FP_DIV['CYCLES']
def run(self, instruction):
if self.cycles == FP_DIV['CYCLES']:
self._block_register()
STAGE['FP_DIV'] = BUSY
self.cycles -= 1
def next(self):
if self.cycles < 0:
self.hazard.struct = True
if FP_DIV['PIPELINED']:
STAGE['FP_DIV'] = FREE
if self.cycles <= 0 and STAGE['WB'] == FREE:
STAGE['FP_DIV'] = FREE
return WriteBackStage(self.instruction)
return self
class WriteBackStage(Stage):
def __init__(self, instruction):
Stage.__init__(self, instruction)
self.name = 'WB'
def run(self, instruction):
STAGE['WB'] = BUSY
def next(self):
STAGE['WB'] = FREE
self._unblock_register()
return None
def _unblock_register(self):
if self.instruction.dest_reg != '':
REGISTER_STATUS[self.instruction.dest_reg] = FREE