Skip to content

Commit eb6ff94

Browse files
committed
table printing pending
1 parent eb0c246 commit eb6ff94

9 files changed

+77
-423
lines changed

Diff for: Makefile

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
CXX = g++
22
CXXFLAGS =
33
EXEC = cache
4-
OBJS = cache.o direct_mapped.o fully_associative_lru.o set_associative_lru.o set_associative_fifo.o
4+
OBJS = cache.o direct_mapped.o fully_associative_lru.o set_associative_lru.o set_associative_fifo.o fully_associative_fifo.o
55

66
${EXEC}: ${OBJS}
77
${CXX} ${CXXFLAGS} -o ${EXEC} ${OBJS}
@@ -19,5 +19,7 @@ set_associative_lru.o: set_associative_lru.cpp cache.hpp
1919

2020
set_associative_fifo.o: set_associative_fifo.cpp cache.hpp
2121

22+
fully_associative_fifo.o: fully_associative_fifo.cpp cache.hpp
23+
2224
clean:
2325
rm -f ${EXEC} ${OBJS}

Diff for: cache.cpp

+19-66
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,21 @@ void cache_impl::set_cache_parameters(int cache_size, int block_size, int set_wa
2323
block_offset_bits = log2(block_size*4);
2424
if(set_ways != 0){
2525
num_sets = num_lines/set_ways;
26+
cout<<"num sets :"<<num_sets<<endl;
2627
set_associative_cache_FIFO = vector<deque<cache>>(num_sets, deque<cache>(set_ways*block_size, cache(0,0,"")));
2728
}
2829
}
2930

3031
string decimal_to_hex_string(int decimal_value){
3132
stringstream ss;
3233
ss<< std::hex << decimal_value;
33-
return ss.str();
34+
string hex_string ="";
35+
for(int i=0; i<4; i++){
36+
if(decimal_value <= 15)
37+
hex_string += "0";
38+
hex_string += ss.str();
39+
}
40+
return hex_string+" ";
3441
}
3542

3643
void cache_impl::call_appropriate_cache(int addr){
@@ -41,7 +48,7 @@ void cache_impl::call_appropriate_cache(int addr){
4148
}
4249
case fully_associative:{
4350
if(repl_algo == FIFO)
44-
retrieve_value_from_fully_associative_cache_LRU(addr);
51+
retrieve_value_from_fully_associative_cache_FIFO(addr);
4552
else
4653
retrieve_value_from_fully_associative_cache_LRU(addr);
4754
break;
@@ -73,16 +80,13 @@ void cache_impl::print_appropriate_cache_state(){
7380
if(repl_algo == FIFO){
7481

7582
}else{
76-
for(int i=0; i<num_lines; i++){
77-
map<int, cache>::iterator it;
78-
if( (fully_associative_cache.find(i) != fully_associative_cache.end())){
79-
for(cache val: set_associative_cache[i]){
80-
cout<<i<<"\t\t"<<val.validBit<<"\t\t"<<val.tag<<"\t"<<val.data<<endl;
81-
}
82-
} else
83-
cout<<i<<endl;
83+
int k = 0;
84+
for(auto val: fully_associative_cache){
85+
cout<<k++<<"\t\t"<<val.second.validBit<<"\t\t"<<val.second.tag<<"\t"<<val.second.data<<endl;
8486
}
85-
}
87+
}
88+
89+
8690

8791
break;
8892
}
@@ -120,81 +124,30 @@ int main(){
120124
cout<<"Select Cache type: \n\n1.Direct Mapped\n2.Fully Associative\n3.Set Associative\n-->";
121125
cin>>temp;
122126
ch.c_type = (cache_type)temp;
127+
ch.set_ways = 0;
123128
if(ch.c_type != direct_mapped){
124129
cout<<"Select Replacement Algorithm\n1.FIFO\n2.LRU\n-->";
125130
cin>>temp;
126131
ch.repl_algo = (replacement_algo)temp;
127132
}
128133
if(ch.c_type == set_associative){
129134
cout<<"Enter the number of Ways\n-->";
130-
cin>>temp;
131-
ch.set_ways = (cache_type)temp;
135+
cin>>ch.set_ways;
132136
}
133137
ch.set_cache_parameters(ch.cache_size, ch.block_size, ch.set_ways);
134138
int i = 0;
139+
ch.ind = 0;
135140
while(i< ch.address.size())
136141
ch.call_appropriate_cache(ch.address[i++]);
137142

138143
ch.hit = 0; ch.miss=0; //reset after first iteration
139144

140-
for(int j= 1; j<400; j++){
145+
for(int j= 1; j<2; j++){
141146
i=0;
142147
while(i< ch.address.size())
143148
ch.call_appropriate_cache(ch.address[i++]);
144149
}
145150
ch.print_appropriate_cache_state();
146151
cout<<"Hits: "<<ch.hit<<" Misses :"<<ch.miss<<endl;
147-
148-
/*ch.hit = 0; ch.miss=0;
149-
cout<<"\n\nLRU Set Associative Cache\n";
150-
ch.set_cache_parameters(16, 1, 2);
151-
cout<<"Cache Size: 16 words, Block size : 1 word & 2 way set"<<endl;
152-
i=0;
153-
while(i< ch.address.size()){
154-
//cout<<ch.address[i]<<endl;
155-
ch.retrieve_value_from_set_associative_cache_LRU(ch.address[i++]);
156-
}
157-
ch.hit = 0; ch.miss=0;
158-
i=0;
159-
while(i< ch.address.size()){
160-
//cout<<ch.address[i]<<endl;
161-
ch.retrieve_value_from_set_associative_cache_LRU(ch.address[i++]);
162-
}
163-
cout<<"Set Number\tvalidBit\tTag\tdata"<<endl;
164-
for(int i=0; i<ch.num_sets; i++){
165-
if( (ch.set_associative_cache.find(i) != ch.set_associative_cache.end())){
166-
for(cache val: ch.set_associative_cache[i]){
167-
cout<<i<<"\t\t"<<val.validBit<<"\t\t"<<val.tag<<"\t"<<val.data<<endl;
168-
}
169-
} else
170-
cout<<i<<endl;
171-
}
172-
cout<<"Hits: "<<ch.hit<<" misses "<<ch.miss<<endl;
173-
174-
ch.hit = 0; ch.miss=0;
175-
cout<<"\n\nLRU Fully Associative Cache\n";
176-
ch.set_cache_parameters(16, 2, 0);
177-
cout<<"Cache Size: 16 words, Block size : 1 word "<<endl;
178-
i=0;
179-
while(i< ch.address.size()){
180-
//cout<<ch.address[i]<<endl;
181-
ch.retrieve_value_from_fully_associative_cache_LRU(ch.address[i++]);
182-
}
183-
ch.hit = 0; ch.miss=0;
184-
i=0;
185-
while(i< ch.address.size()){
186-
//cout<<ch.address[i]<<endl;
187-
ch.retrieve_value_from_fully_associative_cache_LRU(ch.address[i++]);
188-
}
189152

190-
/*cout<<"Set Number\tvalidBit\tTag\tdata"<<endl;
191-
for(int i=0; i<ch.num_sets; i++){
192-
if( (ch.fully_associative_cache.find(i) != ch.fully_associative_cache.end())){
193-
for(cache val: ch.set_associative_cache[i]){
194-
cout<<i<<"\t\t"<<val.validBit<<"\t\t"<<val.tag<<"\t"<<val.data<<endl;
195-
}
196-
} else
197-
cout<<i<<endl;
198-
}*/
199-
//cout<<"Hits: "<<ch.hit<<" misses "<<ch.miss<<endl;
200153
}

Diff for: cache.hpp

+7-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ enum cache_type {direct_mapped = 1, fully_associative = 2, set_associative = 3};
1010
string decimal_to_hex_string(int);
1111

1212
struct cache{
13-
int validBit;
13+
bool validBit;
1414
int tag;
1515
string data;
1616

@@ -27,6 +27,7 @@ class cache_impl{
2727
int cache_size; //in words
2828
int num_lines;
2929
int block_offset_bits;
30+
int word_size = 4;
3031
cache_type c_type;
3132
replacement_algo repl_algo;
3233

@@ -40,6 +41,10 @@ class cache_impl{
4041
vector<deque<cache>> set_associative_cache_FIFO;
4142

4243
map<int, cache> fully_associative_cache; // map<tag, cache>
44+
int ind;
45+
46+
deque<cache> fully_associative_cache_fifo;
47+
4348
list<int> fa_keys;
4449

4550
int miss = 0, hit = 0;
@@ -49,7 +54,7 @@ class cache_impl{
4954
void retrieve_value_from_set_associative_cache_LRU(int);
5055
void retrieve_value_from_set_associative_cache_FIFO(int);
5156
void retrieve_value_from_fully_associative_cache_LRU(int);
52-
//void retrieve_value_from_fully_associative_cache_FIFO(int);
57+
void retrieve_value_from_fully_associative_cache_FIFO(int);
5358
void call_appropriate_cache(int);
5459
void print_appropriate_cache_state();
5560
~cache_impl();

Diff for: direct_mapped.cpp

+3-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
void add_block_to_cache(map<int, cache> &, int, int, int, int);
44

55
void cache_impl::retrieve_value_from_direct_cache(int address){
6-
cout<<"Cache Size "<<cache_size<<" BLock Size "<<block_size<<endl;
6+
//cout<<"Cache Size "<<cache_size<<" BLock Size "<<block_size<<endl;
77
int mem_tag, mem_line, mem_block_offset;
88

99
int line_bits = log2(num_lines);
@@ -18,19 +18,16 @@ void cache_impl::retrieve_value_from_direct_cache(int address){
1818
if( (direct_cache.find(mem_line) != direct_cache.end()) && //address wrt line
1919
((direct_cache.find(mem_line)->second).tag == mem_tag) ) { // in that line, compare tag
2020
hit++;
21-
cout<<"Is a Hit\n";
21+
//cout<<"Is a Hit\n";
2222
} else {
23-
cout<<"Is a Miss\n";
23+
//cout<<"Is a Miss\n";
2424
miss++;
2525
add_block_to_cache(direct_cache, mem_tag, mem_line, address, block_size);
2626
}
2727

2828
}
2929

3030
void add_block_to_cache(map<int, cache> &direct_cache, int tag, int line, int address, int block_size){
31-
32-
//block.tag = tag;
33-
//block.validBit = 1;
3431
string data = decimal_to_hex_string(address);
3532
if(block_size==2)
3633
data += " " + decimal_to_hex_string(address + 4);

0 commit comments

Comments
 (0)