-
Notifications
You must be signed in to change notification settings - Fork 1
/
cache.h
96 lines (79 loc) · 2.42 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
/* Contains cache struct definitions & function prototypes
*
* To modify structure of cache, change values below
*/
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#define I_OFFSET_BITS 6
#define I_INDEX_BITS 14
#define I_TAG_BITS 12
#define I_ADDRESS_SIZE 32
#define D_OFFSET_BITS 6
#define D_INDEX_BITS 14
#define D_TAG_BITS 12
#define D_ADDRESS_SIZE 32
#define ICS (1 << I_INDEX_BITS) // Instruction cache set count
#define DCS (1 << D_INDEX_BITS) // Data cache set count
#define ICA 2 // Instruction cache associativity
#define DCA 4 // Data cache associativity
// Bits masking macros
#define I_INDEX_MASK (0x000FFFC0)
#define I_TAG_MASK (0xFFF00000)
#define i_index(x) ((x & I_INDEX_MASK) >> I_OFFSET_BITS)
#define i_tag(x) ((x & I_TAG_MASK) >> (I_OFFSET_BITS + I_INDEX_BITS))
#define d_address(t, i) (((t) << (D_INDEX_BITS + D_OFFSET_BITS)) + ((i) << D_OFFSET_BITS))
#define i_address(t, i) ((t) << ((I_INDEX_BITS + I_OFFSET_BITS)) + ((i) << I_OFFSET_BITS))
#define D_INDEX_MASK (0x000FFFC0)
#define D_TAG_MASK (0xFFF00000)
#define d_index(x) ((x & D_INDEX_MASK) >> D_OFFSET_BITS)
#define d_tag(x) ((x & D_TAG_MASK) >> (D_OFFSET_BITS + D_INDEX_BITS))
enum MODE_STATE {
SILENT = 0,
VERBOSE = 1,
DEBUG = 2};
// Data structure declarations
enum MESI_BITS {
MODIFIED = 0,
EXCLUSIVE = 1,
SHARED = 2,
INVALID = 3};
// representation of a single cache line
struct cache_line
{
enum MESI_BITS MESI;
uint8_t LRU;
uint32_t tag;
};
struct cache_set
{
struct cache_line* line;
};
// representation of a single cache
struct cache
{
long hits;
long misses;
long reads;
long writes;
int offset_bits;
int index_bits;
int tag_bits;
int associativity;
int set_count;
struct cache_set* set;
};
// global cache performance statistics
long hits, misses, reads, writes;
// General cache function prototypes
int allocate_cache(struct cache*, int way_cnt, int set_cnt, int tag_bits, int index_bits, int offset_bits);
int parse_input(int argc, char* argv[]);
void invalidate_cache(struct cache* cache);
int cache_check(struct cache* cache, int index, int tag);
int update_LRU(struct cache* cache, int index, int tag, int way);
int find_LRU(struct cache* cache, int index);
int find_victim(struct cache* cache, int index);
int display_cache(struct cache* cache);
int tag_mask(struct cache* cache, int address);
int index_mask(struct cache* cache, int address);