-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdmcache.cpp
More file actions
412 lines (308 loc) · 11.8 KB
/
dmcache.cpp
File metadata and controls
412 lines (308 loc) · 11.8 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
//
// dmcache.cpp
//
//
// Created by Donna Chow on 12/7/14.
//
//
/*
MAIN ERROR:
address for a block stored in memory is actually...
tag|linenumber|00
so...
0x002D w/ tag = 8 bits, line # = 6 bits, offset = 2 bits...
0000 0000 0010 1101
we get rid of offset....and then take
0000 0000 0010 11 . 00 <-- we add a 00 at the end!
Thus..... 0000 0000 0010 1100 = 44! We store at 44!
And to retrieve the block... we go in via offset since each block will have [4] w/in main memory
PROBLEM...
I kept storing the blocks via their 'tag' in their line so a lot of the times it got overwritten!
For example:
2104 and 21F6!
Both have same tags but different lines! One ends up getting overwrriten in main memory!
*/
#include <iomanip>
#include <math.h>
#include <iostream>
#include <fstream>
/*
fstream = stream class to let us read/write from and to files.
Writing to a file:
ofstream filename;
filename.open("name.txt");
filename << "Writing this into file.\n";
filename.close();
Reading from a file:
string line;
ifstream filevar("name.txt");
getline(filevar, line);
*/
using namespace std; //so we don't have to do std::cout and std::hex
/*
These are the individual lines w/in cache
2^6 = 64 lines in cache
Dirty Bit | Tag [block #] | Data
Line number = Index in Cache! ->6 bits
block_data = data w/in the particular block of main mem.
offset received from input = index.
*/
class line
{
public:
int dirty;
int block_data[4];
int tag;
int addressing;
line():dirty(0), tag(-1), addressing(0)
{
for(int i = 0; i < 4; i++)
{
block_data[i] = 0;
}
}
};
class memory_address
{
public:
int tag;
int line_number;
int offset;
int addressing; //location we're putting into our main memory
};
class block
{
public:
int words[4];
block()
{
for(int i = 0; i < 4; i++)
{
words[i] = 0;
}
}
};
/* Function declarations */
memory_address dec_binary(int address);
/* MEMORY ADDRESS CONVERSION
Converting a decimal to a 'binary'
25/2 = newnum, 25%2 = LSB
LSB -> MSB
2 bits for offset
6 bits for line number
8 bits for tag (aka block num)
*** C++ IS PASS BY VALUE SO WHEN WE DO address/2... it will not affect actual! ****
This is used for find offset, line number, and block number (tag) from input 16 bit memory address.
*/
memory_address dec_binary(int address)
{
memory_address temp;
//initialize all to 0 just incase 'address' reaches 0 already after looping...like tag.
temp.tag = 0;
int binary, twos;
int actual_dec = 0;
//new
int addressing_twos;
int addressing_temp = 0;
int addressing_counter = 2;
// offset 2 bits
for(int i = 0; i < 2; i++)
{
binary = address % 2;
twos = pow(2, i);
actual_dec = actual_dec + (twos * binary); //2^0 (1)...
address = address / 2;
}
temp.offset = actual_dec;
actual_dec = 0; //reset
//line number. 6 bits.
for(int i = 0; i < 6; i++)
{
binary = address % 2;
twos = pow(2, i);
actual_dec = actual_dec + (twos * binary);
//new
addressing_twos = pow(2,addressing_counter);
addressing_temp = addressing_temp + (addressing_twos*binary);
++addressing_counter;
address = address / 2;
}
temp.line_number = actual_dec;
actual_dec = 0;
int counter = 0;
//tag = 8 bits...counter = 7 here
while(address != 0) //if address already = 0 then tag = 0.
{
binary = address % 2;
twos = pow(2, counter);
actual_dec = actual_dec + (twos * binary);
++counter;
//new
addressing_twos = pow(2,addressing_counter);
addressing_temp = addressing_temp + (addressing_twos*binary);
++addressing_counter;
address = address / 2;
}
temp.tag = actual_dec;
//new
temp.addressing = addressing_temp;
return temp;
}
/* Reading from Command line:
argc = argument count
argv = argument vector
argv[1] = testfile
*/
int main(int argc, char** argv)
{
line cache[64]; //Cache size = 64 lines.
block main_memory[65536]; //index will be block #(tag) and block has array
memory_address input_address;
int address, read_write, data;
line checking;
int output_data, output_hit, output_dirty;
//new
int a = 0;
ifstream testing_input(argv[1]); //input file name = argv[1]
ofstream outfile("dm-out.txt");
outfile << hex << setfill('0') << uppercase;
/* file input line -> hex to decimal -> address
file input line -> hex to decimal -> read_write = 0/255
file input line -> hex to decimal -> data
*/
while(testing_input >> hex>> address >> read_write >> data)
{
input_address = dec_binary( address ); //returns tag, linenum, offset
a = input_address.addressing ;
/* if(read_write == 0)
{
cout<<"READ! ";
}
else
{
cout<<"WRITE! ";
}
cout<<hex<<input_address.tag<<" "<<input_address.line_number<<" "<<input_address.offset<<"\n";*/
// READ!
if(read_write == 0)
{
//go to the line in the cache and store temporarly for checking
checking = cache[ (input_address.line_number) ];
//HIT!!!
if( input_address.tag == checking.tag)
{
output_data = checking.block_data[ (input_address.offset) ];
output_hit = 1;
output_dirty = checking.dirty;
}
//MISS!!
if( input_address.tag != checking.tag)
{
// NOT DIRTY + MISS
//if line not dirty we can just replace with stuff from main memory.
if( checking.dirty == 0)
{
//setting tag of line to be the new tag (block num)
(cache[ (input_address.line_number)] ).tag = input_address.tag;
//new
(cache[ (input_address.line_number)] ).addressing = input_address.addressing;
//retrieve items from block w/in main memory.. aka mainmem[tag]
//replace whole lines
for(int i = 0; i < 4; i++)
{
(cache[ (input_address.line_number)]).block_data[i] = (main_memory[ a ]).words[i];
}
//Time to print the specific item now that we're done transferring!
output_data = (cache[ (input_address.line_number)]).block_data[ (input_address.offset)];
output_hit = 0; //MISS
output_dirty = 0;
}
// else DIRTY!
// NEED TO WRITE BACK BEFORE COPYING FORWARD.
else
{
//writing back
for(int i = 0; i < 4; i++)
{
(main_memory[ (cache[ (input_address.line_number)] ).addressing ]).words[i] = (cache[ (input_address.line_number) ]).block_data[i];
}
//replacing now with items in cache w/ those from main memory
for(int i = 0; i<4; i++)
{
(cache[ (input_address.line_number)]).block_data[i] = (main_memory[ a ]).words[i];
}
//change tag not that we've replaced!
//setting tag of line to be the new tag (block num)
(cache[ (input_address.line_number)] ).tag = input_address.tag;
//new
(cache[ (input_address.line_number)] ).addressing = input_address.addressing;
// no longer dirty! set dirty = 0
(cache[ (input_address.line_number)]).dirty = 0;
output_data = (cache[ (input_address.line_number)]).block_data[ (input_address.offset)];
output_hit = 0; //MISS
output_dirty = 0;
}
} //MISS
// THIS IS FOR TESTING/DEBUGGING! PRINTS OUT TO TERMINAL!
// cout<<"This is the input:\n"<<hex<<" "<<address<<" "<<read_write<<" "<<data<<"\n";
//cout<<"THIS IS THE OUTPUT:\n"<<hex<<output_data<<" ";
//cout<<output_hit<<" "<<output_dirty<<"\n----------\n";
// OUTPUT INTO DM-OUT.TXT !!!!!
outfile<<setw(2)<<(output_data & 0xFF)<<' '<<output_hit<<' '<<output_dirty <<endl;
} //IF READ
//WRITE!!!!
if( read_write == 255)
{
// HIT! We have the same block in the line as input for read!
if( input_address.tag == (cache[ (input_address.line_number) ]).tag)
{
(cache[ (input_address.line_number) ]).block_data[ (input_address.offset) ] = data;
//it's dirty now!
(cache[ (input_address.line_number) ]).dirty = 1;
}
//Else we just missed it! so we have to replace it in the cache
else
{
//else if it's dirty we have to write back first before we replace it in the cache
if( (cache[ (input_address.line_number) ]).dirty == 1)
{
//write back
for(int i = 0; i < 4; i++)
{
(main_memory[ (cache[ (input_address.line_number)] ).addressing ]).words[i] = (cache[ (input_address.line_number) ]).block_data[i];
}
//replace
(cache[ (input_address.line_number) ]).tag = input_address.tag;
//new
(cache[ (input_address.line_number)] ).addressing = input_address.addressing;
for(int i = 0; i < 4; i++)
{
(cache[ (input_address.line_number)]).block_data[i] = (main_memory[ a ]).words[i];
}
//WRITING NOW!
(cache[ (input_address.line_number) ]).block_data[(input_address.offset)] = data;
//ALREADY DIRTY! Don't need to set again!
} //dirty
//else not dirty so we just replace + write over
else
{
//replace
(cache[ (input_address.line_number) ]).tag = input_address.tag;
//new
(cache[ (input_address.line_number)] ).addressing = input_address.addressing;
for(int i = 0; i < 4; i++)
{
(cache[ (input_address.line_number)]).block_data[i] = (main_memory[ a ]).words[i];
}
//WRITING NOW!
(cache[ (input_address.line_number) ]).block_data[(input_address.offset)] = data;
//It's dirty now!
(cache[ (input_address.line_number) ]).dirty = 1;
}
}//miss
}//read
}
testing_input.close();
outfile.close();
return 0;
}