-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstall.py
212 lines (169 loc) · 6.86 KB
/
stall.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
if __name__ == '__main__':
# read instructions
inst = list()
with open('memory.txt', 'r') as f:
for line in f:
inst.append(line.split())
f = open('result.txt', 'w')
# variables
reg = [1 if i != 0 else 0 for i in range(32)]
mem = [1 for _ in range(32)]
dreg = {}
dmem = {}
IF = ''
ID = ''
EX = ''
MEM = ''
WB = ''
cycle = 0
stall = False
EXCtrl = ''
MEMCtrl = ''
WBCtrl = ''
current = 0
# process
while True:
# stall = False
# instructions after ID will not be affected
WB = MEM
MEM = EX
EX = ID if not stall else ''
# registers and memory process if no stall
if EX and not stall:
# store words from register to memory
if EX[0] == 'sw':
rs = int(EX[1].strip('$, '))
offset = int(EX[2][:EX[2].find('(')]) // 4 # 24($0) -> [0, 2)
base = int(EX[2][EX[2].find('$') + 1:EX[2].find(')')]) # 24($0) -> [3, 5)
mem[base + offset] = reg[rs]
# load words from memory to register
elif EX[0] == 'lw':
rs = int(EX[1].strip('$, '))
offset = int(EX[2][:EX[2].find('(')]) // 4 # 24($0) -> [0, 2)
base = int(EX[2][EX[2].find('$') + 1:EX[2].find(')')]) # 24($0) -> [3, 5)
reg[rs] = mem[base + offset]
# addition
elif EX[0] == 'add':
rd = int(EX[1].strip('$, '))
rs = int(EX[2].strip('$, '))
rt = int(EX[3].strip('$, '))
reg[rd] = reg[rs] + reg[rt]
# subtraction
elif EX[0] == 'sub':
rd = int(EX[1].strip('$, '))
rs = int(EX[2].strip('$, '))
rt = int(EX[3].strip('$, '))
reg[rd] = reg[rs] - reg[rt]
# condition
elif EX[0] == 'beq':
rs = int(EX[1].strip('$, '))
rt = int(EX[2].strip('$ ,'))
target = int(EX[3].strip('$, '))
if reg[rs] == reg[rt]:
ID = ''
current = inst.index(EX) + target + 1
IF = inst[current] if len(inst) > current else ''
current += 1
# if taken, we have to restore the values when beq is fetched
for i in range(1, len(reg)): # since $0 is always zero
if i in dreg.keys(): reg[i] = dreg[i]
elif reg[i] != 1: reg[i] = 1
dreg.clear()
for i in range(len(mem)):
if i in dmem.keys(): mem[i] = dmem[i]
elif mem[i] != 1: mem[i] = 1
dmem.clear()
# check if stall is needed
if not stall: ID = IF
if ID:
stall = False
if ID[0] == 'add' or ID[0] == 'sub':
rd = ID[1].strip(',')
rs = ID[2].strip(',')
rt = ID[3].strip(',')
# data hazard
# if WB and WB[0] != 'beq' and WB[0] != 'sw':
# if rs == WB[1].strip(',') or rt == WB[1].strip(','):
# stall = True
# if MEM and MEM[0] != 'beq' and MEM[0] != 'sw':
# if rs == MEM[1].strip(',') or rt == MEM[1].strip(','):
# stall = True
if EX and EX[0] == 'lw':
if rs == EX[1].strip(',') or rt == EX[1].strip(','):
stall = True
elif ID[0] == 'sw':
rs = ID[1].strip(',')
# data hazard
# if WB and WB[0] != 'beq' and WB[0] != 'sw':
# if rs == WB[1].strip(','):
# stall = True
# if MEM and MEM[0] != 'beq' and MEM[0] != 'sw':
# if rs == MEM[1].strip(','):
# stall = True
if EX and EX[0] == 'lw':
if rs == EX[1].strip(','):
stall = True
elif ID[0] == 'beq':
rs = ID[1].strip(',')
rt = ID[2].strip(',')
# if the value is different from the initial stage, we store it
for i in range(1, len(reg)): # since $0 is always zero
if reg[i] != 1:
dreg[i] = reg[i]
for i in range(len(mem)):
if mem[i] != 1:
dmem[i] = mem[i]
# data hazard
# if WB and WB[0] != 'beq' and WB[0] != 'sw':
# if rs == WB[1].strip(',') or rt == WB[1].strip(','):
# stall = True
if MEM and MEM[0] == 'lw':
if rs == MEM[1].strip(',') or rt == MEM[1].strip(','):
stall = True
if EX and EX[0] == 'lw':
if rs == EX[1].strip(',') or rt == EX[1].strip(','):
stall = True
# no stalls then move on
if not stall:
# EX = ID
# ID = IF
IF = inst[current] if len(inst) > current else ''
current += 1
# control signals
if EX:
if EX[0] == 'lw': EXCtrl = '01 010 11'
elif EX[0] == 'sw': EXCtrl = 'X1 001 0X'
elif EX[0] == 'add' or EX[0] == 'sub': EXCtrl = '10 000 10'
elif EX[0] == 'beq': EXCtrl = 'X0 100 0X'
if MEM:
if MEM[0] == 'lw': MEMCtrl = '010 11'
elif MEM[0] == 'sw': MEMCtrl = '001 0X'
elif MEM[0] == 'add' or MEM[0] == 'sub': MEMCtrl = '000 10'
elif MEM[0] == 'beq': MEMCtrl = '100 0X'
if WB:
if WB[0] == 'lw': WBCtrl = '11'
elif WB[0] == 'sw': WBCtrl = '0X'
elif WB[0] == 'add' or WB[0] == 'sub': WBCtrl = '10'
elif WB[0] == 'beq': WBCtrl = '0X'
# done
if not IF and not ID and not EX and not MEM and not WB:
f.write('\n')
f.write(f'需要花{cycle}個cycles')
f.write('\n')
f.write('\n')
# Since the example in the slide is too ugly, I chose to use a better form.
for i in range(len(reg)):
f.write(f'${i} {reg[i]}\n')
f.write('\n')
for i in range(len(mem)):
f.write(f'W{i} {mem[i]}\n')
break
# print results
cycle += 1
f.write(f"Cycle {cycle}\n")
if WB: f.write(f' {WB[0]}: WB {WBCtrl}\n')
if MEM: f.write(f' {MEM[0]}: MEM {MEMCtrl}\n')
if EX: f.write(f' {EX[0]}: EX {EXCtrl}\n')
if ID: f.write(f' {ID[0]}: ID\n')
if IF: f.write(f' {IF[0]}: IF\n')
f.close()