-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMicro.java
316 lines (312 loc) · 21.1 KB
/
Micro.java
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
import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;
public class Micro {
static int startAddress;
static String[] instructions;
static ArrayList<Register> registers = new ArrayList<Register>();
static String[] cacheSpecifications;
static Cache cache;
static Memory memory = new Memory();
static ICache icache;
static TomasuloAlg tomasulo;
static Assembler assembler = new Assembler();
static Register pc = new Register("pc", 0);
@SuppressWarnings("resource")
public static void startSimulation() throws NumberFormatException,
Exception {
Scanner in = new Scanner(System.in);
System.out
.println("Please Enter the number of cache levels you want to simulate.");
int cacheLevels = in.nextInt();
switch (cacheLevels) {
case 1:
System.out
.println("Please Enter your specifications for the cache in the following format: "
+ "first level s,l,m, "
+ "first level hit_policy (1=write_throught 2=write_back),"
+ "first level miss policy (1=write allocate 2=write around),"
+ "first level cycles to access data,"
+ "first level memory access time (in cycles)");
cacheSpecifications = in.next().split(",");
cache = new Cache(Integer.parseInt(cacheSpecifications[0]),
Integer.parseInt(cacheSpecifications[1]),
Integer.parseInt(cacheSpecifications[2]), 0, 0, 0, 0, 0, 0,
Integer.parseInt(cacheSpecifications[5]), 0, 0,
Integer.parseInt(cacheSpecifications[6]),
Integer.parseInt(cacheSpecifications[3]),
Integer.parseInt(cacheSpecifications[4]), 0, 0, 0, 0,
cacheLevels, memory);
break;
case 2:
System.out
.println("Please Enter your specifications for the cache in the following format: "
+ "first level s,l,m, "
+ "first level hit_policy (1=write_throught 2=write_back),"
+ "first level miss policy (1=write allocate 2=write around),"
+ "first level cycles to access data,"
+ "memory access time (in cycles),"
+ "second level s,l,m, "
+ "second level hit_policy (1=write_throught 2=write_back),"
+ "second level miss policy (1=write allocate 2=write around),"
+ "second level cycles to access data");
cacheSpecifications = in.next().split(",");
cache = new Cache(Integer.parseInt(cacheSpecifications[0]),
Integer.parseInt(cacheSpecifications[1]),
Integer.parseInt(cacheSpecifications[2]),
Integer.parseInt(cacheSpecifications[7]),
Integer.parseInt(cacheSpecifications[8]),
Integer.parseInt(cacheSpecifications[9]), 0, 0, 0,
Integer.parseInt(cacheSpecifications[5]),
Integer.parseInt(cacheSpecifications[12]), 0,
Integer.parseInt(cacheSpecifications[6]),
Integer.parseInt(cacheSpecifications[3]),
Integer.parseInt(cacheSpecifications[4]),
Integer.parseInt(cacheSpecifications[10]),
Integer.parseInt(cacheSpecifications[11]), 0, 0,
cacheLevels, memory);
break;
case 3:
System.out
.println("Please Enter your specifications for the cache in the following format: "
+ "first level s,l,m, "
+ "first level hit_policy (1=write_throught 2=write_back),"
+ "first level miss policy (1=write allocate 2=write around),"
+ "first level cycles to access data,"
+ "memory access time (in cycles),"
+ "second level s,l,m, "
+ "second level hit_policy (1=write_throught 2=write_back),"
+ "second level miss policy (1=write allocate 2=write around),"
+ "second level cycles to access data,"
+ "third level s,l,m, "
+ "third level hit_policy (1=write_throught 2=write_back),"
+ "third level miss policy (1=write allocate 2=write around),"
+ "third level cycles to access data");
cacheSpecifications = in.next().split(",");
cache = new Cache(Integer.parseInt(cacheSpecifications[0]),
Integer.parseInt(cacheSpecifications[1]),
Integer.parseInt(cacheSpecifications[2]),
Integer.parseInt(cacheSpecifications[7]),
Integer.parseInt(cacheSpecifications[8]),
Integer.parseInt(cacheSpecifications[9]),
Integer.parseInt(cacheSpecifications[13]),
Integer.parseInt(cacheSpecifications[14]),
Integer.parseInt(cacheSpecifications[15]),
Integer.parseInt(cacheSpecifications[5]),
Integer.parseInt(cacheSpecifications[12]),
Integer.parseInt(cacheSpecifications[18]),
Integer.parseInt(cacheSpecifications[6]),
Integer.parseInt(cacheSpecifications[3]),
Integer.parseInt(cacheSpecifications[4]),
Integer.parseInt(cacheSpecifications[10]),
Integer.parseInt(cacheSpecifications[11]),
Integer.parseInt(cacheSpecifications[16]),
Integer.parseInt(cacheSpecifications[17]), cacheLevels,
memory);
break;
}
System.out
.println("Please Enter the number of instruction cache levels you want to simulate.");
int icacheLevels = in.nextInt();
switch (icacheLevels) {
case 1:
System.out
.println("Please Enter your specifications for the instruction cache in the following format: "
+ "first level s,l,m, "
+ "first level hit_policy (1=write_throught 2=write_back),"
+ "first level miss policy (1=write allocate 2=write around),"
+ "first level cycles to access data,"
+ "first level memory access time (in cycles)");
cacheSpecifications = in.next().split(",");
icache = new ICache(Integer.parseInt(cacheSpecifications[0]),
Integer.parseInt(cacheSpecifications[1]),
Integer.parseInt(cacheSpecifications[2]), 0, 0, 0, 0, 0, 0,
Integer.parseInt(cacheSpecifications[5]), 0, 0,
Integer.parseInt(cacheSpecifications[6]),
Integer.parseInt(cacheSpecifications[3]),
Integer.parseInt(cacheSpecifications[4]), 0, 0, 0, 0,
cacheLevels, memory);
break;
case 2:
System.out
.println("Please Enter your specifications for the instruction cache in the following format: "
+ "first level s,l,m, "
+ "first level hit_policy (1=write_throught 2=write_back),"
+ "first level miss policy (1=write allocate 2=write around),"
+ "first level cycles to access data,"
+ "memory access time (in cycles),"
+ "second level s,l,m, "
+ "second level hit_policy (1=write_throught 2=write_back),"
+ "second level miss policy (1=write allocate 2=write around),"
+ "second level cycles to access data");
cacheSpecifications = in.next().split(",");
icache = new ICache(Integer.parseInt(cacheSpecifications[0]),
Integer.parseInt(cacheSpecifications[1]),
Integer.parseInt(cacheSpecifications[2]),
Integer.parseInt(cacheSpecifications[7]),
Integer.parseInt(cacheSpecifications[8]),
Integer.parseInt(cacheSpecifications[9]), 0, 0, 0,
Integer.parseInt(cacheSpecifications[5]),
Integer.parseInt(cacheSpecifications[12]), 0,
Integer.parseInt(cacheSpecifications[6]),
Integer.parseInt(cacheSpecifications[3]),
Integer.parseInt(cacheSpecifications[4]),
Integer.parseInt(cacheSpecifications[10]),
Integer.parseInt(cacheSpecifications[11]), 0, 0,
cacheLevels, memory);
break;
case 3:
System.out
.println("Please Enter your specifications for the instruction cache in the following format: "
+ "first level s,l,m, "
+ "first level hit_policy (1=write_throught 2=write_back),"
+ "first level miss policy (1=write allocate 2=write around),"
+ "first level cycles to access data,"
+ "memory access time (in cycles),"
+ "second level s,l,m, "
+ "second level hit_policy (1=write_throught 2=write_back),"
+ "second level miss policy (1=write allocate 2=write around),"
+ "second level cycles to access data,"
+ "third level s,l,m, "
+ "third level hit_policy (1=write_throught 2=write_back),"
+ "third level miss policy (1=write allocate 2=write around),"
+ "third level cycles to access data");
cacheSpecifications = in.next().split(",");
icache = new ICache(Integer.parseInt(cacheSpecifications[0]),
Integer.parseInt(cacheSpecifications[1]),
Integer.parseInt(cacheSpecifications[2]),
Integer.parseInt(cacheSpecifications[7]),
Integer.parseInt(cacheSpecifications[8]),
Integer.parseInt(cacheSpecifications[9]),
Integer.parseInt(cacheSpecifications[13]),
Integer.parseInt(cacheSpecifications[14]),
Integer.parseInt(cacheSpecifications[15]),
Integer.parseInt(cacheSpecifications[5]),
Integer.parseInt(cacheSpecifications[12]),
Integer.parseInt(cacheSpecifications[18]),
Integer.parseInt(cacheSpecifications[6]),
Integer.parseInt(cacheSpecifications[3]),
Integer.parseInt(cacheSpecifications[4]),
Integer.parseInt(cacheSpecifications[10]),
Integer.parseInt(cacheSpecifications[11]),
Integer.parseInt(cacheSpecifications[16]),
Integer.parseInt(cacheSpecifications[17]), cacheLevels,
memory);
break;
}
System.out
.println("Please Enter your specifications for the hardware organization in the following format: "
+ "instruction buffer size, "
+ "number of load reservation stations"
+ "number of store reservation stations,"
+ "number of add/sub reservation stations,"
+ "number of nand reservation stations,"
+ "number of mul reservation stations,"
+ "number of addi reservation stations,"
+ "number of jmp/jal/ret reservation stations,"
+ "number of beq reservation stations,"
+ "number of ROB entries,"
+ "store cycles,"
+ "add/sub cycles,"
+ "nand cycles,"
+ "mul cycles,"
+ "addi cycles,"
+ "jmp/jal/ret cycles,"
+ "beq cycles");
String[] tomasuloSpecifications = in.next().split(",");
tomasulo = new TomasuloAlg(Integer.parseInt(tomasuloSpecifications[0]),
Integer.parseInt(tomasuloSpecifications[1]),
Integer.parseInt(tomasuloSpecifications[2]),
Integer.parseInt(tomasuloSpecifications[3]),
Integer.parseInt(tomasuloSpecifications[4]),
Integer.parseInt(tomasuloSpecifications[5]),
Integer.parseInt(tomasuloSpecifications[6]),
Integer.parseInt(tomasuloSpecifications[7]),
Integer.parseInt(tomasuloSpecifications[8]),
Integer.parseInt(tomasuloSpecifications[9]),
Integer.parseInt(tomasuloSpecifications[10]),
Integer.parseInt(tomasuloSpecifications[11]),
Integer.parseInt(tomasuloSpecifications[12]),
Integer.parseInt(tomasuloSpecifications[13]),
Integer.parseInt(tomasuloSpecifications[14]),
Integer.parseInt(tomasuloSpecifications[15]),
Integer.parseInt(tomasuloSpecifications[16]));
System.out
.println("Please enter the start address then write your program in the file.");
startAddress = in.nextInt();
instructions = assembler.assemble(new File("test.txt"));
for (int i = 0; i < instructions.length; i++) {
memory.writeData(Integer.toBinaryString(startAddress),
instructions[i]);
startAddress += 2;
System.err.println("ERROR"+Integer.toBinaryString(startAddress)+instructions[i]);
}
for (int q = 0; q < assembler.returnRegisters().size(); q++) {
if (!registers.contains(assembler.returnRegisters().get(q))) {
Register r = new Register(assembler.returnRegisters().get(q),
-1);
registers.add(r);
}
}
System.out
.println("Please enter the memory data required for your program in the form (value, address), if no data, enter (noData).");
String[] memData = in.next().split(",");
for (int d = 0; d < memData.length; d += 2) {
if (!memData[d].equals("noData"))
memory.writeData(memData[d + 1], memData[d]);
}
tomasulo.start(registers, icache, cache, memory, pc);
System.out.println("Number of cycles: " + tomasulo.cycles);
System.out.println("IPC: " + (instructions.length / tomasulo.cycles));
switch (cacheLevels) {
case 1:
System.out.println("Cache Hit ratio: "
+ (cache.hit / (cache.hit + cache.miss)));
break;
case 2:
System.out.println("Cache Hit ratio: ");
System.out.println("Level1: "
+ (cache.hit / (cache.hit + cache.miss)));
System.out.println("Level2: "
+ (cache.hit2 / (cache.hit2 + cache.miss2)));
break;
case 3:
System.out.println("Cache Hit ratio: ");
System.out.println("Level1: "
+ (cache.hit / (cache.hit + cache.miss)));
System.out.println("Level2: "
+ (cache.hit2 / (cache.hit2 + cache.miss2)));
System.out.println("Level3: "
+ (cache.hit3 / (cache.hit3 + cache.miss3)));
break;
}
switch (icacheLevels) {
case 1:
System.out.println("Instruction Cache Hit ratio: "
+ (icache.hit / (icache.hit + icache.miss)));
break;
case 2:
System.out.println("Instruction Cache Hit ratio: ");
System.out.println("Level1: "
+ (icache.hit / (icache.hit + icache.miss)));
System.out.println("Level2: "
+ (icache.hit2 / (icache.hit2 + icache.miss2)));
break;
case 3:
System.out.println("Instruction Cache Hit ratio: ");
System.out.println("Level1: "
+ (icache.hit / (icache.hit + icache.miss)));
System.out.println("Level2: "
+ (icache.hit2 / (icache.hit2 + icache.miss2)));
System.out.println("Level3: "
+ (icache.hit3 / (icache.hit3 + icache.miss3)));
break;
}
}
public static void main(String[] args) throws NumberFormatException,
Exception {
System.out
.println("*************************************************SIMULATION START*************************************************");
startSimulation();
}
}