-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCPU.java
More file actions
52 lines (48 loc) · 1.92 KB
/
CPU.java
File metadata and controls
52 lines (48 loc) · 1.92 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
/*
* CPU CLASS
* keeps track of hits and misses w each search
*/
class CPU {
private int hits;
private int misses;
private double ratio;
private Cache myCache;
public CPU(Cache mc){
hits = 0;
misses = 0;
ratio=0;
myCache = mc;
}
public void clear(){
myCache.clear(); //go to cache to clear
}
public void delBlock(int blockNum){
myCache.deleteBlock(blockNum); //go to cache to del block
}
public void search(Numb instrNum){ //instr num is base16 rn
long inty = instrNum.convertRToBase();
int potentialBlock = (int)((inty / myCache.getBlockSize()) % myCache.getNumBlocks());
int blockOfMM = (int)(inty / myCache.getBlockSize());
int offset = (int)(inty % myCache.getBlockSize());
Numb numInBase10 = new Numb(Long.toString(inty), 10, 16, 8);
boolean found = this.myCache.checkForInstr(potentialBlock, offset, numInBase10); //look in cache for specific instr
if (found){
this.hits += 1; //nothing needs to be brought in or changed, just increase misses.
}
else{
this.misses += 1; //incr misses
Numb[] nummies = new Numb[(int)myCache.getBlockSize()]; //nummies is the list of x instructions located within a block
int count = 0;
for (long i = inty-offset; i<(inty-offset+myCache.getBlockSize()); i++){ //populate nummies with the instructions of the block youre bringing in
Numb hexxyBaby = new Numb(Long.toString(i),10,16,8);
nummies[count] = hexxyBaby;
count++;
}
myCache.bringBlock(potentialBlock, nummies, blockOfMM); //brings block into cache
}
ratio = (double)hits/(hits+misses); //update the ratio
}
public String toString(){
return("H - " + ratio + "\n" + myCache);
}
}