@@ -23,14 +23,21 @@ void cache_impl::set_cache_parameters(int cache_size, int block_size, int set_wa
23
23
block_offset_bits = log2 (block_size*4 );
24
24
if (set_ways != 0 ){
25
25
num_sets = num_lines/set_ways;
26
+ cout<<" num sets :" <<num_sets<<endl;
26
27
set_associative_cache_FIFO = vector<deque<cache>>(num_sets, deque<cache>(set_ways*block_size, cache (0 ,0 ," " )));
27
28
}
28
29
}
29
30
30
31
string decimal_to_hex_string (int decimal_value){
31
32
stringstream ss;
32
33
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+" " ;
34
41
}
35
42
36
43
void cache_impl::call_appropriate_cache (int addr){
@@ -41,7 +48,7 @@ void cache_impl::call_appropriate_cache(int addr){
41
48
}
42
49
case fully_associative:{
43
50
if (repl_algo == FIFO)
44
- retrieve_value_from_fully_associative_cache_LRU (addr);
51
+ retrieve_value_from_fully_associative_cache_FIFO (addr);
45
52
else
46
53
retrieve_value_from_fully_associative_cache_LRU (addr);
47
54
break ;
@@ -73,16 +80,13 @@ void cache_impl::print_appropriate_cache_state(){
73
80
if (repl_algo == FIFO){
74
81
75
82
}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;
84
86
}
85
- }
87
+ }
88
+
89
+
86
90
87
91
break ;
88
92
}
@@ -120,81 +124,30 @@ int main(){
120
124
cout<<" Select Cache type: \n\n 1.Direct Mapped\n 2.Fully Associative\n 3.Set Associative\n -->" ;
121
125
cin>>temp;
122
126
ch.c_type = (cache_type)temp;
127
+ ch.set_ways = 0 ;
123
128
if (ch.c_type != direct_mapped){
124
129
cout<<" Select Replacement Algorithm\n 1.FIFO\n 2.LRU\n -->" ;
125
130
cin>>temp;
126
131
ch.repl_algo = (replacement_algo)temp;
127
132
}
128
133
if (ch.c_type == set_associative){
129
134
cout<<" Enter the number of Ways\n -->" ;
130
- cin>>temp;
131
- ch.set_ways = (cache_type)temp;
135
+ cin>>ch.set_ways ;
132
136
}
133
137
ch.set_cache_parameters (ch.cache_size , ch.block_size , ch.set_ways );
134
138
int i = 0 ;
139
+ ch.ind = 0 ;
135
140
while (i< ch.address .size ())
136
141
ch.call_appropriate_cache (ch.address [i++]);
137
142
138
143
ch.hit = 0 ; ch.miss =0 ; // reset after first iteration
139
144
140
- for (int j= 1 ; j<400 ; j++){
145
+ for (int j= 1 ; j<2 ; j++){
141
146
i=0 ;
142
147
while (i< ch.address .size ())
143
148
ch.call_appropriate_cache (ch.address [i++]);
144
149
}
145
150
ch.print_appropriate_cache_state ();
146
151
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
- }
189
152
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;
200
153
}
0 commit comments