Skip to content

Commit 880332c

Browse files
author
mejbah
committed
initial commit
0 parents  commit 880332c

File tree

5 files changed

+294
-0
lines changed

5 files changed

+294
-0
lines changed

Makefile

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
CC = g++
3+
LD = $(CC)
4+
TARGET = cache_sim
5+
6+
7+
SRCS = $(wildcard *.cpp)
8+
OBJS = $(patsubst %.cpp,%.o,$(SRCS))
9+
10+
#OBJECTS_AS := $(patsubst %.s,%.o,$(wildcard *.S))
11+
#OBJECTS_AS = lowlevellock.o
12+
all: $(TARGET) CSCOPE
13+
14+
$(TARGET) : $(OBJS)
15+
$(LD) -o $@ $^ $(LDFLAGS)
16+
%.o : %.cpp
17+
$(CC) $(CFLAGS) -c $<
18+
19+
CSCOPE:
20+
`find -name '*.c' -o -name '*.cpp' -o -name '*.h' -name '*.hh'> cscope.files`
21+
`cscope -b -q -k`
22+
23+
clean:
24+
rm -f $(TARGET) *.o
25+

cache.hh

+126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
#ifndef _CACHE_LINE_
2+
#define _CACHE_LINE_
3+
4+
5+
#define ADDR_SIZE 32
6+
#define CACHE_SIZE_KB 32
7+
#define BLOCK_SIZE 32
8+
#define ASSOC 4
9+
10+
11+
enum STATE { MODIFIED, EXCLUSIVE, SHARED, INVALID};
12+
13+
14+
// 19bit tag | 8bit set | 5bit block
15+
16+
17+
class CacheLine {
18+
19+
private:
20+
STATE state;
21+
unsigned int tag;
22+
bool valid;
23+
24+
25+
public:
26+
CacheLine(){
27+
tag = 0;
28+
state = INVALID;
29+
valid = false;
30+
}
31+
~CacheLine(){};
32+
33+
unsigned int getTag(){return tag;}
34+
void setTag(unsigned int _tag ) { tag = _tag; }
35+
36+
bool getValid(){ return valid; }
37+
void setValid(bool _valid){ valid = _valid; }
38+
39+
STATE getState(){ return state; }
40+
void setState( STATE _state ) { state =_state; }
41+
42+
};
43+
44+
45+
class CacheSet {
46+
47+
private:
48+
CacheLine _cache_line[ASSOC];
49+
int MRU; // most recent used bit for replacement
50+
//std::list<CacheLine>_cache_line;
51+
52+
public:
53+
CacheSet(){ //LRU = 0;
54+
};
55+
~CacheSet(){};
56+
57+
int getMRU() { return MRU; }
58+
59+
CacheLine* getLine(int assoc){return &_cache_line[assoc];}
60+
61+
CacheLine* access( unsigned int tag ){
62+
bool hit = false;
63+
for(int i=0; i<ASSOC; i++){
64+
//TODO: check valid
65+
if(_cache_line[i].getTag() == tag){
66+
fprintf(stderr, "Hit %d\n", i);
67+
hit = true;
68+
MRU = i;
69+
break;
70+
}
71+
}
72+
if(!hit){
73+
74+
if(MRU != (ASSOC-1)){
75+
MRU = MRU + 1;
76+
}
77+
else {
78+
MRU = 0;
79+
}
80+
fprintf(stderr, "Miss %d\n", MRU);
81+
_cache_line[MRU].setTag(tag);
82+
83+
}
84+
return &_cache_line[MRU];
85+
86+
}
87+
#if 0
88+
CacheLine* getLRU() { return _cache_line.back(); }
89+
90+
void check( unsigned int tag ){
91+
std::list<CacheLine>::iterator it;
92+
bool hit = false;
93+
for(it = _cache_line.begin(); it != _cache_line.end(); it++){
94+
if(it->getTag()== tag){
95+
hit = true;
96+
if(it != _cache_line.begin()){
97+
_cache_line.erase(it);
98+
_cache_line.push_front(it);
99+
}
100+
}
101+
}
102+
}
103+
#endif
104+
105+
};
106+
107+
108+
class CacheL1 {
109+
110+
private:
111+
CacheSet *sets;
112+
public:
113+
114+
CacheL1(){
115+
int no_of_blocks = (CACHE_SIZE_KB * 1024) / BLOCK_SIZE;
116+
int no_of_sets = no_of_blocks / ASSOC;
117+
sets = new CacheSet[no_of_sets];
118+
}
119+
120+
CacheSet* getSet( int set_index ){
121+
return &sets[set_index];
122+
}
123+
124+
125+
};
126+
#endif

cache_core.hh

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#ifndef _COCHE_CORE_H_
2+
3+
#include "cache.hh"
4+
#include "cache_util.hh"
5+
6+
class CacheCore{
7+
8+
private:
9+
CacheL1 *_cache;
10+
CacheCore(){};
11+
12+
public:
13+
static CacheCore& getInstance(){
14+
static char buf[sizeof(CacheCore)];
15+
static CacheCore *singleObject = new (buf) CacheCore();
16+
return *singleObject;
17+
}
18+
19+
void initialize(int cores ){
20+
_cache = new CacheL1[cores];
21+
}
22+
23+
void read( unsigned int address, int core ){
24+
access(address, core);
25+
26+
}
27+
void write( unsigned int address, int core){
28+
access(address, core);
29+
}
30+
void access( unsigned int address, int core){
31+
unsigned int setIndex = CacheUtil::getInstance().getSetIndex(address);
32+
unsigned int tag = CacheUtil::getInstance().getTag(address);
33+
bool hit = 0;
34+
fprintf(stderr, "setIndex %d\n", setIndex);
35+
CacheSet *set = _cache[core].getSet(setIndex);
36+
CacheLine *line = set->access(tag);
37+
38+
fprintf(stderr, "tag %u foundtag %u\n", tag, line->getTag());
39+
40+
}
41+
42+
43+
44+
45+
46+
};
47+
48+
49+
#endif

cache_util.hh

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#ifndef _CACHE_UTIL_
2+
#define _CACHE_UTIL_
3+
4+
#include "cache.hh"
5+
#include <unistd.h>
6+
#include <stdlib.h>
7+
#include <stdio.h>
8+
#include <string.h>
9+
#include <assert.h>
10+
#include <math.h>
11+
#include <new>
12+
13+
#define LOG2(x) ((int) rint((log((double) (x))) / (log(2.0))))
14+
15+
class CacheUtil{
16+
17+
private:
18+
int block_offset; //bits
19+
int set_index; //bits
20+
int tag; //bits
21+
22+
23+
CacheUtil(){}
24+
25+
public:
26+
#if 1
27+
static CacheUtil& getInstance(){
28+
static char buf[sizeof(CacheUtil)];
29+
static CacheUtil *theOneTrueObject = new (buf) CacheUtil();
30+
return *theOneTrueObject;
31+
}
32+
#endif
33+
//CacheUtil(){}
34+
35+
void initialize(){
36+
37+
int no_of_blocks = (CACHE_SIZE_KB * 1024) / BLOCK_SIZE;
38+
int no_of_sets = no_of_blocks / ASSOC;
39+
40+
block_offset = LOG2(BLOCK_SIZE);
41+
set_index = LOG2(no_of_sets);
42+
tag = ADDR_SIZE - (set_index + block_offset);
43+
44+
printf(" %d - %d - %d\n", tag, set_index, block_offset);
45+
46+
}
47+
48+
unsigned int getSetIndex( unsigned int address ){
49+
unsigned int setIndex = (address << tag) >> (tag+block_offset);
50+
return setIndex;
51+
}
52+
53+
unsigned int getTag( unsigned int address ){
54+
unsigned int tag = address >> (set_index+block_offset);
55+
return tag;
56+
}
57+
58+
};
59+
60+
61+
62+
63+
#endif

main.cpp

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include<stdio.h>
2+
3+
#include "cache_util.hh"
4+
#include "cache_core.hh"
5+
6+
int main(){
7+
8+
CacheUtil::getInstance().initialize();
9+
unsigned int val = 0xf000f001;
10+
//printf("addr %u set %u tag %u\n", val, CacheUtil::getInstance().getSetIndex(val), CacheUtil::getInstance().getTag(val));
11+
CacheCore::getInstance().initialize(1);
12+
13+
CacheCore::getInstance().read(val, 0);
14+
15+
CacheCore::getInstance().read(val, 0);
16+
17+
CacheCore::getInstance().read(val, 0);
18+
19+
val = 0xf000f101;
20+
21+
CacheCore::getInstance().read(val, 0);
22+
23+
val = 0xf010f101;
24+
25+
CacheCore::getInstance().read(val, 0);
26+
27+
val = 0xf010f001;
28+
29+
CacheCore::getInstance().read(val, 0);
30+
31+
}

0 commit comments

Comments
 (0)