-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsim_cache.java
More file actions
109 lines (102 loc) · 4.48 KB
/
sim_cache.java
File metadata and controls
109 lines (102 loc) · 4.48 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
import java.io.*;
import static java.lang.Math.log;
/* args hold the command line arguments
Example:-
sim_cache 32 8192 4 7 262144 8 gcc_trace.txt
args[0] = "32"
args[1] = "8192"
args[2] = "4"
... and so on
*/
public class sim_cache
{
public static void main(String[] args) {
cache_params params = new cache_params(); // check cache_params.java class for the definition of class cache_params
String trace_file; // Variable that holds trace file name
char rw; // Variable holds read/write type read from input file
long addr; // Variable holds the address read from input file
if (args.length !=7 ) // Checks if correct number of inputs have been given. Throw error and exit if wrong
{
System.out.println("Error: Expected inputs:7 Given inputs:" + args.length);
System.exit(0);
}
// Long.parseLong() converts string to long
params.block_size = Long.parseLong(args[0]);
params.l1_size = Long.parseLong(args[1]);
params.l1_assoc = Long.parseLong(args[2]);
params.vc_num_blocks = Long.parseLong(args[3]);
params.l2_size = Long.parseLong(args[4]);
params.l2_assoc = Long.parseLong(args[5]);
trace_file = args[6];
// Print params
System.out.printf(" ===== Simulator configuration =====%n"+
" BLOCKSIZE: %d%n"+
" L1_SIZE: %d%n"+
" L1_ASSOC: %d%n"+
" VC_NUM_BLOCKS: %d%n"+
" L2_SIZE: %d%n"+
" L2_ASSOC: %d%n"+
" trace_file: %s%n%n", params.block_size, params.l1_size, params.l1_assoc, params.vc_num_blocks, params.l2_size, params.l2_assoc, trace_file);
Memory memHeir=new Memory(params);
int block_offset =(int) (log(params.block_size)/log(2));
try (BufferedReader br = new BufferedReader(new FileReader(trace_file)))
{
String line;
while ((line = br.readLine()) != null) {
rw = line.charAt(0); // gets r/w char from String line
addr = Long.parseLong(line.substring(2), 16); // gets address from String line and converts to long. parseLong uses base 16
String binaryAddr=Long.toBinaryString(addr);
String prepend="";
if(binaryAddr.length()<32){
prepend= "0";
for(int x=1; x<32-binaryAddr.length();x++){
prepend+="0";
}
}
binaryAddr=prepend+binaryAddr;
binaryAddr=binaryAddr.substring(0,(binaryAddr.length()-block_offset));
if (rw == 'r') {
if(params.vc_num_blocks == 0){
if(params.l2_size == 0){
memHeir.readL1(binaryAddr);
}
else{
memHeir.readL1L2(binaryAddr);
}
}
else{
if(params.l2_size == 0){
memHeir.readL1(binaryAddr);
}
else{
memHeir.readL1L2(binaryAddr);
}
}
}
else if(rw == 'w') {
if(params.vc_num_blocks == 0){
if(params.l2_size == 0){
memHeir.writeL1(binaryAddr);
}
else{
memHeir.writeL1L2(binaryAddr);
}
}
else{
if(params.l2_size == 0){
memHeir.writeL1(binaryAddr);
}
else{
memHeir.writeL1L2(binaryAddr);
}
}
}
}
}
catch (IOException x) // Throw error if file I/O fails
{
System.err.format("IOException: %s%n", x);
}
memHeir.printStats();
}
}