-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.h
114 lines (86 loc) · 3 KB
/
cache.h
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
/*
Ducksim: Cache
*/
#ifndef _CACHE_H
#define _CACHE_H
#include <stdio.h>
#include <iostream>
#include <math.h>
#include <vector>
#include <iomanip>
#include <fstream>
#include "types.h"
#include "replacement_policies.h"
//using namespace std;
/* Cacheline */
struct CacheLine {
uint tag;
bool valid;
bool dirty;
int number_data_blocks; //in B
/*Some pointer to actual data block ==> PENDING....*/
};
/* CacheStats per cache */
struct CacheStatistics {
uint hits;
uint misses;
uint access; //total memory references
uint replacements; //total number of cache line replacements
uint write_backs; //total number of write backs
};
/*
* CacheLines is a collection of CacheLine objects, held in a vector.
*/
typedef vector<CacheLine> CacheLines;
/*
* Describes a cache
*/
class Cache {
public:
/*Default Constructor*/
//Cache(uint in_size, int in_associativity, int in_banks, int in_number_cache_lines, int in_number_data_blocks,
// ReplacementPolicy in_replacement_policy);
Cache(uint in_size, int in_associativity, int in_banks, int in_number_data_blocks, ReplacementPolicy in_replacement_policy);
/*Default Destructor*/
~Cache();
/*Reinitialize cache usually after a program is complete*/
void reinit_cache();
/*Cache Access with memory address and read/write condition => implement replacement + placement policy and do the bookeeping
Return true on hit*/
bool access(uint address, bool write);
/*Dump cache statistics on the Output Stream*/
void dump_stats(ostream& stream);
/*Dump cache settings on the Output Stream*/
void dump_settings(ostream& stream);
/*Dump current cache state on the Output Stream*/
void dump_cache(ostream& stream);
/*Run cache based on given input stream for #lines, -1 means complete run*/
bool run(istream& stream, int lines=-1, char *fin=NULL); //run cache from stream.. can do cin as well
/*Get back the virtual bank id to be replaced which is the index of the replacement_line*/
int get_replacement_line( CacheLines cache_replacement_lines, ReplacementLines replacement_lines);
/*Implement Replacement Policy*/
/*Get Miss Rate*/
double get_miss_rate(void);
/*Restore cache stat rates*/
void restore_rates(void);
private:
/*Configuration*/
uint size; //in B
int associativity; //should be to the power of 2
int number_virtual_banks; //keep track of virtual banks for implementing associativity
int banks; //just use 1 for now
int number_cache_lines; //total number
int number_cache_sets; //total number / associativity or virtual banks
int number_data_blocks; //per cache line
vector<CacheLines> virtual_banks; //bank = collection of cache lines that belong to different sets
/*Stats*/
CacheStatistics stats;
/*Replacement Policy + Stat for implementing Replacement Policy*/
ReplacementPolicy replacement_policy;
CacheReplacementStats *replacement_stats;
/* Memory Hiearchy */
Cache *lower_level;
Cache *upper_level;
/*need hash functions for associativity later: per bank*/
};
#endif