Skip to content

Commit 7703dac

Browse files
committed
run
1 parent a343bc9 commit 7703dac

11 files changed

+100724
-0
lines changed

project/AssocLevel.java

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
2+
public class AssocLevel
3+
{
4+
public Memory [] memorySlot;
5+
6+
public AssocLevel(int assoc, int blockSize)
7+
{
8+
// TODO Auto-generated constructor stub
9+
memorySlot = new Memory[assoc];
10+
for(int index = 0; index < assoc; index++)
11+
{
12+
memorySlot[index] = new Memory();
13+
memorySlot[index].memory = new int[blockSize];
14+
for(int i = 0; i < blockSize; i++)
15+
{
16+
memorySlot[index].memory[i] = 0;
17+
}
18+
}
19+
}
20+
21+
public void read(int index)
22+
{
23+
Memory temp = memorySlot[index];
24+
for(int spot = index; spot > 0; spot--)
25+
{
26+
memorySlot[spot] = memorySlot[spot-1];
27+
}
28+
memorySlot[0] = temp;
29+
}
30+
31+
public int search(long tag)
32+
{
33+
for(int find = 0; find < memorySlot.length; find++)
34+
{
35+
if(tag == memorySlot[find].tag)
36+
{
37+
return find;
38+
}
39+
if(!memorySlot[find].vaild)
40+
return -1;
41+
}
42+
return -1;
43+
}
44+
45+
public int findLRU()
46+
{
47+
for(int look = 0; look < memorySlot.length; look++)
48+
if(!memorySlot[look].vaild)
49+
return look;
50+
return memorySlot.length-1;
51+
}
52+
53+
54+
public void updateMemory(long tag, int lruIndex)
55+
{
56+
// TODO Auto-generated method stub
57+
Memory temp = memorySlot[lruIndex];
58+
for(int i = lruIndex; i > 0; i--)
59+
{
60+
memorySlot[i] = memorySlot[i -1];
61+
}
62+
memorySlot[0] = temp;
63+
memorySlot[0].tag = tag;
64+
memorySlot[0].vaild = true;
65+
memorySlot[0].dirty = false;
66+
}
67+
68+
69+
public long readUpdate() {
70+
// TODO Auto-generated method stub
71+
return memorySlot[0].tag;
72+
}
73+
74+
75+
public void write(int tagIndex)
76+
{
77+
// TODO Auto-generated method stub
78+
Memory temp = memorySlot[tagIndex];
79+
for(int spot = tagIndex; spot > 0; spot--)
80+
memorySlot[spot] = memorySlot[spot-1];
81+
memorySlot[0] = temp;
82+
memorySlot[0].dirty = true;
83+
memorySlot[0].vaild = true;
84+
}
85+
86+
public void writeUpdate() {
87+
// TODO Auto-generated method stub
88+
memorySlot[0].dirty = true;
89+
}
90+
91+
92+
public String toString()
93+
{
94+
String string = new String();
95+
for(int i = 0; i < memorySlot.length; i++)
96+
{
97+
string = string + Long.toHexString(memorySlot[i].tag);
98+
if(memorySlot[i].dirty)
99+
string = string + " D";
100+
string = string + " ";
101+
}
102+
string = string + "\n";
103+
return string;
104+
}
105+
106+
public static void main(String[] args) {
107+
// TODO Auto-generated method stub
108+
109+
}
110+
111+
}

project/Blocks.java

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
public class Blocks {
3+
4+
public long blocks;
5+
public boolean valid = true;
6+
7+
}

project/Cache.java

+277
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,277 @@
1+
2+
public class Cache {
3+
4+
public boolean debug = false;
5+
6+
public AssocLevel [] memoryIndex;
7+
8+
public int LRU;
9+
int STATE = 0;
10+
int LOC = 1;
11+
int BLOCKNUMBER = 2;
12+
int set = 0;
13+
int assoc = 0;
14+
int blockSize = 0;
15+
long tag = 0;
16+
int index = 0;
17+
int blockSet = 0;
18+
int indexBits = 0;
19+
int blockBits = 0;
20+
int tagBits = 0;
21+
String cacheName;
22+
String debugNumber = new String();
23+
long instruction = 0;
24+
int number;
25+
boolean SBAccess = false;
26+
int blockNumber = 0;
27+
28+
29+
Cache nextLevel = null;
30+
31+
32+
33+
34+
public int memoryTraffic = 0;
35+
public int read = 0;
36+
public int write = 0;
37+
public int readMiss = 0;
38+
public int writeMiss = 0;
39+
public int writeBacks = 0;
40+
public int SBReadMiss = 0;
41+
public int SBRead = 0;
42+
43+
44+
45+
46+
47+
public Cache(int SetSize, int Assoc, int BlockSize, String name)
48+
{
49+
// TODO Auto-generated constructor stub
50+
memoryIndex = new AssocLevel[SetSize];
51+
set = SetSize;
52+
assoc = Assoc;
53+
blockSize = BlockSize;
54+
nextLevel = null;
55+
indexBits = log2(set);
56+
blockBits = log2(blockSize);
57+
tagBits = (int) (32 - indexBits - blockBits);
58+
cacheName = name;
59+
60+
for(int index = 0; index < SetSize; index++)
61+
{
62+
memoryIndex[index] = new AssocLevel(Assoc, BlockSize);
63+
}
64+
65+
}
66+
67+
68+
public int log2(int value)
69+
{
70+
return (short)(Math.log10(value)/Math.log10(2));
71+
}
72+
73+
74+
75+
public void controler(long[] result)
76+
{
77+
// TODO Auto-generated method stub
78+
long [] output = new long[2];
79+
instruction = (result[1] >> blockBits) << blockBits;
80+
output = processInput(instruction);
81+
tag = output[0];
82+
index = (int)output[1];
83+
if(result[0] == 2)
84+
{
85+
result[0] = 0;
86+
SBAccess = true;
87+
}
88+
89+
if(result[0] == 0)
90+
{
91+
read();
92+
}
93+
else
94+
{
95+
write();
96+
}
97+
}
98+
99+
public long [] processInput(long input)
100+
{
101+
long [] output = new long[2];
102+
output[1] = (long)(( input >> blockBits) & ((long)Math.pow(2, indexBits) - 1 ));
103+
output[0] = (long)(( input >> indexBits + blockBits) & (long)(Math.pow(2, tagBits) - 1 ));
104+
return output;
105+
}
106+
107+
108+
private void write()
109+
{
110+
int result;
111+
long evict;
112+
int lru;
113+
long [] input = new long[2];
114+
write++;
115+
result = memoryIndex[index].search(tag);
116+
debugDisplay(cacheName + " write : " + Long.toHexString(instruction) + "(tag "+ Long.toHexString(tag) +", index "+ index+ ")");
117+
if(result > -1)
118+
{
119+
debugDisplay(cacheName+" write hit");
120+
memoryIndex[index].write(result);
121+
debugDisplay(cacheName+ " update LRU");
122+
}
123+
else
124+
{
125+
debugDisplay(cacheName+ " miss");
126+
evict = recon();
127+
if(memoryIndex[index].memorySlot[assoc-1].dirty)
128+
{
129+
debugDisplay(cacheName+" victim: " + Long.toHexString(evict));
130+
writeBacks++;
131+
if(nextLevel == null)
132+
{
133+
memoryTraffic++;
134+
}
135+
else
136+
{
137+
input[0] = 1;
138+
input[1] = evict;
139+
nextLevel.controler(input);
140+
}
141+
142+
}
143+
else
144+
{
145+
debugDisplay(cacheName+" victim: none");
146+
147+
}
148+
writeMiss++;
149+
if(nextLevel == null)
150+
{
151+
memoryTraffic++;
152+
}
153+
else
154+
{
155+
input[0] = 0;
156+
input[1] = instruction;
157+
nextLevel.controler(input);
158+
}
159+
lru = memoryIndex[index].findLRU();
160+
memoryIndex[index].updateMemory(tag, lru);
161+
debugDisplay(cacheName + " Update LRU");
162+
memoryIndex[index].writeUpdate();
163+
}
164+
165+
}
166+
167+
168+
private void read()
169+
{
170+
int result;
171+
long evict;
172+
int lru;
173+
long [] input = new long[2];
174+
if(!SBAccess)
175+
read++;
176+
else
177+
SBRead++;
178+
debugDisplay(cacheName + " read : " + Long.toHexString(instruction) + "(tag "+ Long.toHexString(tag) +", index "+ index+ ")");
179+
result = memoryIndex[index].search(tag);
180+
if(result > -1)
181+
{
182+
debugDisplay(cacheName+" read hit");
183+
memoryIndex[index].read(result);
184+
debugDisplay(cacheName+ " update LRU");
185+
}
186+
else
187+
{
188+
debugDisplay(cacheName+ " miss");
189+
evict = recon();
190+
if(memoryIndex[index].memorySlot[assoc-1].dirty)
191+
{
192+
debugDisplay(cacheName+" victim: " + Long.toHexString(evict));
193+
writeBacks++;
194+
if(nextLevel == null)
195+
{
196+
memoryTraffic++;
197+
}
198+
else
199+
{
200+
input[0] = 1;
201+
input[1] = evict;
202+
nextLevel.controler(input);
203+
204+
}
205+
206+
}
207+
else
208+
{
209+
debugDisplay(cacheName+" victim: none");
210+
211+
}
212+
if(!SBAccess)
213+
{
214+
readMiss++;
215+
}
216+
else
217+
{
218+
SBReadMiss++;
219+
}
220+
221+
if(nextLevel == null)
222+
{
223+
memoryTraffic++;
224+
}
225+
else
226+
{
227+
input[0] = 0;
228+
input[1] = instruction;
229+
nextLevel.controler(input);
230+
}
231+
lru = memoryIndex[index].findLRU();
232+
memoryIndex[index].updateMemory(tag, lru);
233+
debugDisplay(cacheName + " Update LRU");
234+
}
235+
SBAccess = false;
236+
}
237+
238+
239+
240+
private long recon()
241+
{
242+
// TODO Auto-generated method stub
243+
return (index << blockBits) + ( memoryIndex[index].memorySlot[assoc-1].tag << (indexBits + blockBits));
244+
}
245+
246+
247+
public String toString()
248+
{
249+
String string = new String();
250+
for(int i = 0; i < memoryIndex.length; i++)
251+
{
252+
string = string + "Set " + i + ": ";
253+
string=string + memoryIndex[i].toString();
254+
}
255+
return string;
256+
}
257+
258+
259+
public void debugDisplay(Object object)
260+
{
261+
if(debug)
262+
{
263+
System.out.println(object);
264+
}
265+
}
266+
267+
268+
/**
269+
* @param args
270+
*/
271+
public static void main(String[] args) {
272+
// TODO Auto-generated method stub
273+
new Cache(16, 1, 4, "L");
274+
}
275+
276+
277+
}

0 commit comments

Comments
 (0)