-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
562 lines (478 loc) · 16.6 KB
/
main.py
File metadata and controls
562 lines (478 loc) · 16.6 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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
from collections import deque
PC = 0
number_of_registers = 0
instruction_window_size = 0
program = []
finished = False
class Instruction(object):
def __init__(self, adrs, op, dest, s1, s2=None):
self.adress = adrs
self.opname = op
self.destination = dest
self.source1 = s1
self.source2 = s2
def toString(self):
st = ''
if self.opname == 'LD':
st = self.opname + ' ' + str(self.destination) + ', ' + str(self.source1)
elif self.opname =='BGE':
st = self.opname + ' ' + str(self.source1) + ', ' + str(self.source2) + ', ' + str(self.destination)
else:
st = self.opname + ' ' + self.destination + ', ' + str(self.source1) + ', ' + str(self.source2)
return st
class InstructionQueue(object):
def __init__(self, window_size):
self.instructions = deque()
self.window_size = window_size
def add(self, instruction):
self.instructions.append(instruction)
def isEmpty(self):
if len(self.instructions) == 0:
return True
else:
return False
def isFull(self):
if len(self.instructions) == self.window_size:
return True
else:
return False
def head(self):
return self.instructions[0]
def pop(self):
return self.instructions.popleft()
def clear(self):
self.instructions = deque()
def toString(self):
st = "Instruction Window\n"
for i in self.instructions:
st += i.toString() + '\n'
return st
class ReservationStation(object):
def __init__(self):
self.opName = ''
self.busy = False
self.Vj = None
self.Vk = None
self.Qj = None
self.Qk = None
self.destination = None
self.cyclesRemained = None
def clear(self):
self.opName = ''
self.busy = False
self.Vj = None
self.Vk = None
self.Qj = None
self.Qk = None
self.destination = None
self.cyclesRemained = None
def execute(self):
if self.opName == 'ADD':
return self.Vj + self.Vk
elif self.opName == 'SUB':
return self.Vj - self.Vk
elif self.opName == 'MUL':
return self.Vj * self.Vk
elif self.opName == 'DIV':
return self.Vj / self.Vk
elif self.opName == 'LD':
return self.Vj
def toString(self):
st = ' '
if self.busy:
st = self.opName + ' '
if self.opName == 'LD':
st += ' '
if self.Vj is None:
st += self.Qj + ' '
else:
st += str(self.Vj) + ' '
if self.Vk is None:
if self.Qk is None:
st += '- '
else:
st += self.Qk + ' '
else:
st += str(self.Vk) + ' '
st += self.destination
return st
class RegisterFile(object):
def __init__(self, num_of_registers):
self.size = num_of_registers
self.registerList = [Register(i) for i in range(num_of_registers)]
def flush(self,list):
for register in self.registerList:
for name in list:
if register.robId == name:
register.clear()
def toString(self):
st = 'Registers\n'
for reg in self.registerList:
st += reg.toString() + '\n'
return st
class Register(object):
def __init__(self, i):
self.name = 'R' + str(i)
self.value = None
self.robId = None
self.busy = False
def clear(self):
self.robId = None
self.busy = False
def toString(self):
st = self.name + ': '
if self.value is None:
st += '- '
else:
st += str(self.value) + ' '
if self.robId is None:
st += '-'
else:
st += self.robId
return st
class ReorderBuffer(object):
def __init__(self, size=5):
self.size = size
self.head = 0
self.tail = -1
self.list = [ReorderBufferEntry(i) for i in range(self.size)]
self.numelements = 0
def isFull(self):
if self.numelements == self.size:
return True
else:
return False
def createRoBEntry(self, i):
index = self.tail + 1
index %= self.size
self.list[index].opname = i.opname
self.list[index].destination = i.destination
self.list[index].busy = True
self.tail += 1
self.tail %= self.size
self.numelements += 1
return self.list[index].name
def gethead(self):
return self.list[self.head]
def pop(self):
self.list[self.head].clear()
self.head += 1
self.head %= self.size
self.numelements -= 1
def flush(self, destination):
index = 0
for entry in self.list:
if entry.name == destination:
index = self.list.index(entry)
break
listtoremove = []
while (self.tail - index) % self.size != 0:
index += 1
index %= self.size
listtoremove.append(self.list[index].name)
self.list[index].clear()
self.tail -= len(listtoremove)
self.tail %= self.size
self.numelements -= len(listtoremove)
return listtoremove
def toString(self):
st = 'Reorder Buffer\n'
i = 0
for entry in self.list:
st += entry.toString()
if i == self.head:
st += ' (H)'
if i == self.tail:
st += ' (T)'
st += '\n'
i += 1
return st
def findlastentry(self, register):
index = self.tail
for i in range(ROB.numelements):
if self.list[index].destination == register.name:
return self.list[index]
index -= 1
index %= self.size
return None
class ReorderBufferEntry(object):
def __init__(self, i):
self.name = 'ROB' + str(i)
self.opname = ''
self.busy = False
self.ready = False
self.destination = ''
self.value = None
def clear(self):
self.opname = ''
self.busy = False
self.ready = False
self.destination = ''
self.value = None
def toString(self):
st = self.name + ': '
if self.ready:
st += self.opname + ' '
if self.opname == 'LD':
st += ' '
st += self.destination + ' ' + str(self.value)
elif self.busy:
st += self.opname + ' '
if self.opname == 'LD':
st += ' '
st += self.destination + ' -'
return st
class FunctionalUnit(object):
def __init__(self, supportedIns, cyclenums):
self.supportedInstructions = supportedIns
self.cycles = cyclenums
self.reservationStation = ReservationStation()
def clear (self):
self.reservationStation = ReservationStation()
class CommonDataBus(object):
def __init__(self):
self.value = None
self.address = None
self.busy = False
def toString(self):
st ='Common Data Bus\n'
if self.busy:
st += str(self.value) + ' ' + self.address
return st
class FunctionalUnits(object):
def __init__(self):
self.fuList = []
def add(self, fu):
self.fuList.append(fu)
def findAvailable(self, opname):
for funtionalunit in self.fuList:
if not funtionalunit.reservationStation.busy:
if opname in funtionalunit.supportedInstructions:
return funtionalunit
return False
def isAvailable(self, opname):
for fu in self.fuList:
if opname in fu.supportedInstructions:
if not fu.reservationStation.busy:
return True
return False
def flush(self,list):
for fu in self.fuList:
for robId in list:
rs = fu.reservationStation
if rs.Qj == robId or rs.Qk == robId or rs.destination == robId:
fu.clear()
def toString(self):
st = 'Reservation Stations\n'
i = 0
for fu in self.fuList:
st += 'RS' + str(i) + ': ' + fu.reservationStation.toString() + '\n'
i += 1
return st
def read_parameters_file():
global number_of_registers, instruction_window_size
parameters_file = open("Parameters.txt", 'r')
for line in parameters_file.readlines():
args = line.split()
if args[0] == 'number_of_registers:':
number_of_registers = int(args[1])
elif args[0] == 'instruction_window_size:':
instruction_window_size = int(args[1])
def read_units_file():
units_file = open("Units.txt", 'r')
for line in units_file.readlines():
args = line.split()
if args[0] == '#':
pass
elif len(args) == 0:
pass
else:
supported_instructions = args[1].split(',')
cycles = int(args[2])
FU.add(FunctionalUnit(supported_instructions, cycles))
def read_program_file():
program_file = open("Program.txt", 'r')
for line in program_file.readlines():
args = line.split()
if args[0] == '#':
pass
elif len(args) == 0:
pass
else:
adress = int(args[0].split(':')[0])
opname = args[1]
destination = args[2].split(',')[0]
source1 = args[3].split(',')[0]
if opname == 'LD':
instruction = Instruction(adress, opname, destination, source1)
elif opname == 'BGE':
instruction = Instruction(adress, opname, args[4], destination, source1)
else:
source2 = args[4]
instruction = Instruction(adress, opname, destination, source1, source2)
program.append(instruction)
def fetch():
global PC
while IQ.isFull() is False and int(PC) < 4 * len(program) and PC != -1:
for i in program:
if i.adress == PC:
IQ.add(i)
if i.opname == 'BGE':
PC = int(i.destination)
else:
PC = PC + 4
break
def issue():
if ROB.isFull() is False and PC != -1 and FU.isAvailable(IQ.head().opname):
inst = IQ.pop()
fu = FU.findAvailable(inst.opname)
find = False
for register in RF.registerList:
if register.name == inst.source1:
if register.busy:
h = ROB.findlastentry(register)
# for robEntry in ROB.list:
# if robEntry.name == h:
# if robEntry.ready:
if h is not None and h.ready:
fu.reservationStation.Vj = h.value
else:
fu.reservationStation.Qj = h.name
else:
fu.reservationStation.Vj = register.value
find = True
if not find:
fu.reservationStation.Vj = float(inst.source1)
find = False
for register in RF.registerList:
if register.name == inst.source2:
if register.busy:
h = ROB.findlastentry(register)
# for robEntry in ROB.list:
# if robEntry.name == h:
# if robEntry.ready:
if h is not None and h.ready:
fu.reservationStation.Vk = h.value
else:
fu.reservationStation.Qk = h.name
else:
fu.reservationStation.Vk = register.value
find = True
if not find and inst.source2 is not None:
fu.reservationStation.Vk = float(inst.source2)
b = ROB.createRoBEntry(inst)
if inst.destination != 'BGE':
for register in RF.registerList:
if register.name == inst.destination:
if not register.busy:
register.robId = b
register.busy = True
break
fu.reservationStation.destination = b
fu.reservationStation.busy = True
fu.reservationStation.opName = inst.opname
fu.reservationStation.cyclesRemained = fu.cycles
def execute():
for fu in FU.fuList:
rs = fu.reservationStation
if rs.busy and rs.Vj is not None:
if rs.opName == 'LD':
rs.cyclesRemained -= 1
elif rs.Vk is not None:
rs.cyclesRemained -= 1
def writeResult():
global PC
CDB.busy = False
for fu in FU.fuList:
rs = fu.reservationStation
if rs.cyclesRemained is not None and rs.busy is True:
if rs.cyclesRemained <= 0 and not CDB.busy:
rs.busy = False
if rs.opName != 'BGE':
result = rs.execute()
b = rs.destination
CDB.busy = True
CDB.value = result
CDB.address = b
for robentry in ROB.list:
if robentry.name == b:
robentry.value = result
robentry.ready = True
break
for fu2 in FU.fuList:
rs2 = fu2.reservationStation
if rs2.Qj == b:
rs2.Vj = result
rs2.Qj = None
if rs2.Qk == b:
rs2.Vk = result
rs2.Qk = None
fu.busy = False
rs.clear()
elif rs.opName == 'BGE':
if not rs.Vj >= rs.Vk:
listtoremove = ROB.flush(rs.destination)
FU.flush(listtoremove)
RF.flush(listtoremove)
IQ.clear()
PC = -1
for e in ROB.list:
if e.name == rs.destination:
e.ready = True
rs.clear()
def commit():
global PC, finished
head = ROB.gethead()
if head.ready:
if head.opname == 'BGE':
if PC == -1:
finished = True
ROB.pop()
else:
d = head.destination
for reg in RF.registerList:
if reg.name == d:
reg.value = head.value
reg.robId = None
reg.busy = False
index = ROB.head
for i in range(ROB.numelements-1):
index += 1
index %= ROB.size
if ROB.list[index].destination == reg.name:
reg.robId = ROB.list[index].name
reg.busy = True
break
ROB.pop()
def print_report():
global cycles
report_file = open("Report.txt", "a")
report_file.write("------------------------\n")
report_file.write("CYCLE " + str(cycles) + '\n\n')
report_file.write(IQ.toString() + '\n')
report_file.write(RF.toString() + '\n')
report_file.write(FU.toString() + '\n')
report_file.write(ROB.toString() + '\n')
report_file.write(CDB.toString() + '\n')
report_file.close()
FU = FunctionalUnits()
CDB = CommonDataBus()
read_units_file()
read_parameters_file()
read_program_file()
IQ = InstructionQueue(instruction_window_size)
RF = RegisterFile(number_of_registers)
ROB = ReorderBuffer(len(FU.fuList))
cycles = 0
rf = open("Report.txt", "w")
rf.close()
while not finished:
commit()
writeResult()
execute()
fetch()
issue()
print_report()
cycles += 1
if finished:
print('R0:', RF.registerList[0].value)