| Maxim303 |
| quote: | Originally posted by lMIlk
YO MAXIM THAT TIMES TWO
quote:
Originally posted by davinox
Originally posted by drizzt81
ok, let's see..
I am sure all of you are interested in my retarded Combinatorics prject, so let me post the source code here:
yeah that's spam. u should be banned too. padding your post count is illegal. and it's being supported by mods? how gross. lemme throw up.
that project is weak..lemme post *my* old project, it's a direct and associative cpu cache simulator. hehe.. enjoy! (tru compiling and runnin if *really* bored)
***cache-sim.c***
code:
#include
#include
#include
/*enums for setting cache and rep types*/
enum {DIRECT, TWOWAY};
enum {RAND, LRU};
enum {INVALID, VALID};
enum {STATUS, TAG, DATA};
#define MEM_SIZE 4096
static int mem[MEM_SIZE]; /* 16KB main memory */
int csize, ctype, rtype;
struct cache {
int *direct[3];
/*int *tag;
int *data;
int *status;*/
} cache;
void cache_lookup(int addr)
{
int tag = (addr/csize);
int index = (addr%csize);
if (cache.direct[STATUS][index] == VALID)
printf("Cache Hit!\n");
else {
printf("Cache Miss!\n");
cache.direct[TAG][index] = tag; /*store into cache if miss*/
cache.direct[DATA][index] = mem[addr];
cache.direct[STATUS][index] = VALID; /*store and mark valid*/
}
printf("Memory Address: %d\n", addr);
printf("Cache Index: %d\n", index);
printf("Cache Tag: %d\n", tag);
printf("*****************************\n");
}
void cache_write(int addr, int data)
{
int tag = (addr/csize);
int index = (addr%csize);
if (cache.direct[STATUS][index] == VALID)
printf("Cache Hit!\n");
else
printf("Cache Miss!\n");
printf("Memory Address: %d\n", addr);
printf("Cache Index: %d\n", index);
printf("Cache Tag: %d\n", tag);
printf("*****************************\n");
cache.direct[TAG][index] = tag;
cache.direct[DATA][index] = data;
cache.direct[STATUS][index] = VALID;
mem[addr] = data; /*write through to memory*/
}
void dump_memory(char *fname)
{
FILE *mem_out;
const int apl = 3; /* number of mem addr contents printed per line */
int i;
if ((mem_out = fopen(fname, "w")) == NULL) {
perror(fname);
exit(EXIT_FAILURE);
}
for (i = 0; i < MEM_SIZE; i ++) {
if (!(i % apl) && i)
fprintf(mem_out, "\n");
fprintf(mem_out, "MEM[%d]=%-10d ", i, mem[i]);
}
}
#define BUF_SIZE 512
void sim(char *filename)
{
FILE *mem_access;
char line_buf[BUF_SIZE];
int match, addr, data;
if ((mem_access = fopen(filename, "r")) == NULL) {
perror(filename);
exit(EXIT_FAILURE);
}
while (fgets(line_buf, BUF_SIZE, mem_access) != NULL) {
match = sscanf(line_buf, "%u %u", &addr, &data);
if (match == 1) {
cache_lookup(addr);
} else if (match == 2) {
cache_write(addr, data);
} else {
fprintf(stderr, "Error: Invalid Line %s\n", line_buf);
exit(EXIT_FAILURE);
}
}
dump_memory("mem_dump");
}
int main(int argc, char **argv) {
char *memfile;
if (argc != 5) {
printf("Usage: cache-sim \n");
return EXIT_FAILURE;
}
csize = atoi(argv[1]);
if (strcmp(argv[2], "--direct") == 0)
ctype = DIRECT;
else if (strcmp(argv[2], "--2way") == 0)
ctype = TWOWAY;
else
printf("Invalid cache type\n");
if (strcmp(argv[3], "--rand") == 0)
rtype = RAND;
else if (strcmp(argv[3], "--lru") == 0)
rtype = LRU;
if (ctype == DIRECT) {
if ((cache.direct[TAG] = malloc(sizeof(int)*(csize))) == NULL)
exit(EXIT_FAILURE);
if ((cache.direct[DATA] = malloc(sizeof(int)*(csize))) == NULL)
exit(EXIT_FAILURE);
if ((cache.direct[STATUS] = malloc(sizeof(int)*(csize))) == NULL)
exit(EXIT_FAILURE);
}
memfile = argv[4];
sim(memfile);
return EXIT_SUCCESS;
}
***gen_access.c***
code:
#include
#include
#include
#include
#include
FILE *out_file;
#define LO_ADDR 0
#define HI_ADDR 4096
/* GEN_ACCESS
* Generate a memory access
* of the form: [data]
*/
static void gen_access(void);
int main(int argc, char **argv)
{
long i, num_accesses;
if (argc != 3) {
fprintf(stderr, "Command line: %s \n",
argv[0]);
exit(EXIT_FAILURE);
}
if ((out_file = fopen(argv[2], "w")) == NULL) {
perror("Error opening file");
exit(EXIT_FAILURE);
}
num_accesses = strtol(argv[1], NULL, 10);
srandom(time(NULL));
for (i = 0; i < num_accesses; i++)
gen_access();
return EXIT_SUCCESS;
}
void gen_access(void)
{
static int base, count;
long addr;
if (count == 0) {
base = random() % HI_ADDR;
count = 25 + (random() % 20); /*generate 25-35 mem accesses around base*/
}
addr = 10 - (random() % 20) + base;
if (addr < LO_ADDR)
addr = random() % 10;
if (addr >= HI_ADDR)
addr = base;
fprintf(out_file, "%lu", addr); /* the address */
if ((random() % 4) == 0)
fprintf(out_file, " %lu\n", random()); /* 25% of the accesses will be
* writes */
else
fprintf(out_file, "\n");
count--;
}
yeah, header files..
makefile:
CS_OBJS = cache-sim.o
CS_HFILES =
GEN_OBJS = gen_access.o
GEN_HFILES =
CC = gcc
FLAGS = -pedantic -O2 -Wall
%.o: %.c
$(CC) $(FLAGS) -c $<
all: cache-sim gen_access
cache-sim: $(CS_OBJS) $(CS_HFILES)
$(CC) $(FLAGS) -o cache-sim $(CS_OBJS)
gen_access: $(GEN_OBJS) $(GEN_HFILES)
$(CC) $(FLAGS) -o gen_access $(GEN_OBJS)
clean:
rm -fr *.o gen_access cache-sim core
cache test sequence:
503
520
517
514 1331704529
511
518
503
504
519
517
514
509
515
516
503 709403117
505
509
510
516
517
505
506
509
520
518 1365352847
520
508
518
514
513
510 1985531838
504 2142599432
506
513
507
232
232
242
241
241
238
246
241 1596812504
229 1434860694
230 1429976478
234
229 1554307710
245
244
235
237
242
248
229
244
239
230
245
236
230
229
246 584244135
2192 702465288
2199
2192
2210 1639432451
2198
2203
2207
2206
2197
2197
2206 166870671
2201
2200
2196
2194 1962719074
2202
2206 142799593
2194
2205 1758534999
2198
2208
2204
2194
2202
2202 274216585
2203
2201
2210
1168
1173
1169
1177
1183 1947044307
1166
1176
1173
1174
1173
1172
1176 1193720248
1181
1170 415125196
1181
1176
1174
1183
1172
1181
1168
1174
1182
1170
1169 414691673
1168
1184
1167 1767758641
1170
1169 464232718
1166
1185 359117980
1171 1278177093
1182
1168
1185 365136972
1169 633062792
1176
1184
1167 398932282
3995 132784035
3996 254988822
3984 614106802
3997
3986 2002635178
3997
3991
3985
3985
3994
3999
3990
3998 1641314374
3990
3993
3993
4002
3996
3992
3996
3983
3990 1600262841
3991
3999
3983
3989
3996
4002 609303961
3993
3991
3997 129832879
quote:
Originally posted by davinox
Originally posted by drizzt81
ok, let's see..
I am sure all of you are interested in my retarded Combinatorics prject, so let me post the source code here:
yeah that's spam. u should be banned too. padding your post count is illegal. and it's being supported by mods? how gross. lemme throw up.
that project is weak..lemme post *my* old project, it's a direct and associative cpu cache simulator. hehe.. enjoy! (tru compiling and runnin if *really* bored)
***cache-sim.c***
code:
#include
#include
#include
/*enums for setting cache and rep types*/
enum {DIRECT, TWOWAY};
enum {RAND, LRU};
enum {INVALID, VALID};
enum {STATUS, TAG, DATA};
#define MEM_SIZE 4096
static int mem[MEM_SIZE]; /* 16KB main memory */
int csize, ctype, rtype;
struct cache {
int *direct[3];
/*int *tag;
int *data;
int *status;*/
} cache;
void cache_lookup(int addr)
{
int tag = (addr/csize);
int index = (addr%csize);
if (cache.direct[STATUS][index] == VALID)
printf("Cache Hit!\n");
else {
printf("Cache Miss!\n");
cache.direct[TAG][index] = tag; /*store into cache if miss*/
cache.direct[DATA][index] = mem[addr];
cache.direct[STATUS][index] = VALID; /*store and mark valid*/
}
printf("Memory Address: %d\n", addr);
printf("Cache Index: %d\n", index);
printf("Cache Tag: %d\n", tag);
printf("*****************************\n");
}
void cache_write(int addr, int data)
{
int tag = (addr/csize);
int index = (addr%csize);
if (cache.direct[STATUS][index] == VALID)
printf("Cache Hit!\n");
else
printf("Cache Miss!\n");
printf("Memory Address: %d\n", addr);
printf("Cache Index: %d\n", index);
printf("Cache Tag: %d\n", tag);
printf("*****************************\n");
cache.direct[TAG][index] = tag;
cache.direct[DATA][index] = data;
cache.direct[STATUS][index] = VALID;
mem[addr] = data; /*write through to memory*/
}
void dump_memory(char *fname)
{
FILE *mem_out;
const int apl = 3; /* number of mem addr contents printed per line */
int i;
if ((mem_out = fopen(fname, "w")) == NULL) {
perror(fname);
exit(EXIT_FAILURE);
}
for (i = 0; i < MEM_SIZE; i ++) {
if (!(i % apl) && i)
fprintf(mem_out, "\n");
fprintf(mem_out, "MEM[%d]=%-10d ", i, mem[i]);
}
}
#define BUF_SIZE 512
void sim(char *filename)
{
FILE *mem_access;
char line_buf[BUF_SIZE];
int match, addr, data;
if ((mem_access = fopen(filename, "r")) == NULL) {
perror(filename);
exit(EXIT_FAILURE);
}
while (fgets(line_buf, BUF_SIZE, mem_access) != NULL) {
match = sscanf(line_buf, "%u %u", &addr, &data);
if (match == 1) {
cache_lookup(addr);
} else if (match == 2) {
cache_write(addr, data);
} else {
fprintf(stderr, "Error: Invalid Line %s\n", line_buf);
exit(EXIT_FAILURE);
}
}
dump_memory("mem_dump");
}
int main(int argc, char **argv) {
char *memfile;
if (argc != 5) {
printf("Usage: cache-sim \n");
return EXIT_FAILURE;
}
csize = atoi(argv[1]);
if (strcmp(argv[2], "--direct") == 0)
ctype = DIRECT;
else if (strcmp(argv[2], "--2way") == 0)
ctype = TWOWAY;
else
printf("Invalid cache type\n");
if (strcmp(argv[3], "--rand") == 0)
rtype = RAND;
else if (strcmp(argv[3], "--lru") == 0)
rtype = LRU;
if (ctype == DIRECT) {
if ((cache.direct[TAG] = malloc(sizeof(int)*(csize))) == NULL)
exit(EXIT_FAILURE);
if ((cache.direct[DATA] = malloc(sizeof(int)*(csize))) == NULL)
exit(EXIT_FAILURE);
if ((cache.direct[STATUS] = malloc(sizeof(int)*(csize))) == NULL)
exit(EXIT_FAILURE);
}
memfile = argv[4];
sim(memfile);
return EXIT_SUCCESS;
}
***gen_access.c***
code:
#include
#include
#include
#include
#include
FILE *out_file;
#define LO_ADDR 0
#define HI_ADDR 4096
/* GEN_ACCESS
* Generate a memory access
* of the form: [data]
*/
static void gen_access(void);
int main(int argc, char **argv)
{
long i, num_accesses;
if (argc != 3) {
fprintf(stderr, "Command line: %s \n",
argv[0]);
exit(EXIT_FAILURE);
}
if ((out_file = fopen(argv[2], "w")) == NULL) {
perror("Error opening file");
exit(EXIT_FAILURE);
}
num_accesses = strtol(argv[1], NULL, 10);
srandom(time(NULL));
for (i = 0; i < num_accesses; i++)
gen_access();
return EXIT_SUCCESS;
}
void gen_access(void)
{
static int base, count;
long addr;
if (count == 0) {
base = random() % HI_ADDR;
count = 25 + (random() % 20); /*generate 25-35 mem accesses around base*/
}
addr = 10 - (random() % 20) + base;
if (addr < LO_ADDR)
addr = random() % 10;
if (addr >= HI_ADDR)
addr = base;
fprintf(out_file, "%lu", addr); /* the address */
if ((random() % 4) == 0)
fprintf(out_file, " %lu\n", random()); /* 25% of the accesses will be
* writes */
else
fprintf(out_file, "\n");
count--;
}
yeah, header files..
makefile:
CS_OBJS = cache-sim.o
CS_HFILES =
GEN_OBJS = gen_access.o
GEN_HFILES =
CC = gcc
FLAGS = -pedantic -O2 -Wall
%.o: %.c
$(CC) $(FLAGS) -c $<
all: cache-sim gen_access
cache-sim: $(CS_OBJS) $(CS_HFILES)
$(CC) $(FLAGS) -o cache-sim $(CS_OBJS)
gen_access: $(GEN_OBJS) $(GEN_HFILES)
$(CC) $(FLAGS) -o gen_access $(GEN_OBJS)
clean:
rm -fr *.o gen_access cache-sim core
cache test sequence:
503
520
517
514 1331704529
511
518
503
504
519
517
514
509
515
516
503 709403117
505
509
510
516
517
505
506
509
520
518 1365352847
520
508
518
514
513
510 1985531838
504 2142599432
506
513
507
232
232
242
241
241
238
246
241 1596812504
229 1434860694
230 1429976478
234
229 1554307710
245
244
235
237
242
248
229
244
239
230
245
236
230
229
246 584244135
2192 702465288
2199
2192
2210 1639432451
2198
2203
2207
2206
2197
2197
2206 166870671
2201
2200
2196
2194 1962719074
2202
2206 142799593
2194
2205 1758534999
2198
2208
2204
2194
2202
2202 274216585
2203
2201
2210
1168
1173
1169
1177
1183 1947044307
1166
1176
1173
1174
1173
1172
1176 1193720248
1181
1170 415125196
1181
1176
1174
1183
1172
1181
1168
1174
1182
1170
1169 414691673
1168
1184
1167 1767758641
1170
1169 464232718
1166
1185 359117980
1171 1278177093
1182
1168
1185 365136972
1169 633062792
1176
1184
1167 398932282
3995 132784035
3996 254988822
3984 614106802
3997
3986 2002635178
3997
3991
3985
3985
3994
3999
3990
3998 1641314374
3990
3993
3993
4002
3996
3992
3996
3983
3990 1600262841
3991
3999
3983
3989
3996
4002 609303961
3993
3991
3997 129832879
quote:
Originally posted by davinox
Originally posted by drizzt81
ok, let's see..
I am sure all of you are interested in my retarded Combinatorics prject, so let me post the source code here:
yeah that's spam. u should be banned too. padding your post count is illegal. and it's being supported by mods? how gross. lemme throw up.
that project is weak..lemme post *my* old project, it's a direct and associative cpu cache simulator. hehe.. enjoy! (tru compiling and runnin if *really* bored)
***cache-sim.c***
code:
#include
#include
#include
/*enums for setting cache and rep types*/
enum {DIRECT, TWOWAY};
enum {RAND, LRU};
enum {INVALID, VALID};
enum {STATUS, TAG, DATA};
#define MEM_SIZE 4096
static int mem[MEM_SIZE]; /* 16KB main memory */
int csize, ctype, rtype;
struct cache {
int *direct[3];
/*int *tag;
int *data;
int *status;*/
} cache;
void cache_lookup(int addr)
{
int tag = (addr/csize);
int index = (addr%csize);
if (cache.direct[STATUS][index] == VALID)
printf("Cache Hit!\n");
else {
printf("Cache Miss!\n");
cache.direct[TAG][index] = tag; /*store into cache if miss*/
cache.direct[DATA][index] = mem[addr];
cache.direct[STATUS][index] = VALID; /*store and mark valid*/
}
printf("Memory Address: %d\n", addr);
printf("Cache Index: %d\n", index);
printf("Cache Tag: %d\n", tag);
printf("*****************************\n");
}
void cache_write(int addr, int data)
{
int tag = (addr/csize);
int index = (addr%csize);
if (cache.direct[STATUS][index] == VALID)
printf("Cache Hit!\n");
else
printf("Cache Miss!\n");
printf("Memory Address: %d\n", addr);
printf("Cache Index: %d\n", index);
printf("Cache Tag: %d\n", tag);
printf("*****************************\n");
cache.direct[TAG][index] = tag;
cache.direct[DATA][index] = data;
cache.direct[STATUS][index] = VALID;
mem[addr] = data; /*write through to memory*/
}
void dump_memory(char *fname)
{
FILE *mem_out;
const int apl = 3; /* number of mem addr contents printed per line */
int i;
if ((mem_out = fopen(fname, "w")) == NULL) {
perror(fname);
exit(EXIT_FAILURE);
}
for (i = 0; i < MEM_SIZE; i ++) {
if (!(i % apl) && i)
fprintf(mem_out, "\n");
fprintf(mem_out, "MEM[%d]=%-10d ", i, mem[i]);
}
}
#define BUF_SIZE 512
void sim(char *filename)
{
FILE *mem_access;
char line_buf[BUF_SIZE];
int match, addr, data;
if ((mem_access = fopen(filename, "r")) == NULL) {
perror(filename);
exit(EXIT_FAILURE);
}
while (fgets(line_buf, BUF_SIZE, mem_access) != NULL) {
match = sscanf(line_buf, "%u %u", &addr, &data);
if (match == 1) {
cache_lookup(addr);
} else if (match == 2) {
cache_write(addr, data);
} else {
fprintf(stderr, "Error: Invalid Line %s\n", line_buf);
exit(EXIT_FAILURE);
}
}
dump_memory("mem_dump");
}
int main(int argc, char **argv) {
char *memfile;
if (argc != 5) {
printf("Usage: cache-sim \n");
return EXIT_FAILURE;
}
csize = atoi(argv[1]);
if (strcmp(argv[2], "--direct") == 0)
ctype = DIRECT;
else if (strcmp(argv[2], "--2way") == 0)
ctype = TWOWAY;
else
printf("Invalid cache type\n");
if (strcmp(argv[3], "--rand") == 0)
rtype = RAND;
else if (strcmp(argv[3], "--lru") == 0)
rtype = LRU;
if (ctype == DIRECT) {
if ((cache.direct[TAG] = malloc(sizeof(int)*(csize))) == NULL)
exit(EXIT_FAILURE);
if ((cache.direct[DATA] = malloc(sizeof(int)*(csize))) == NULL)
exit(EXIT_FAILURE);
if ((cache.direct[STATUS] = malloc(sizeof(int)*(csize))) == NULL)
exit(EXIT_FAILURE);
}
memfile = argv[4];
sim(memfile);
return EXIT_SUCCESS;
}
***gen_access.c***
code:
#include
#include
#include
#include
#include
FILE *out_file;
#define LO_ADDR 0
#define HI_ADDR 4096
/* GEN_ACCESS
* Generate a memory access
* of the form: [data]
*/
static void gen_access(void);
int main(int argc, char **argv)
{
long i, num_accesses;
if (argc != 3) {
fprintf(stderr, "Command line: %s \n",
argv[0]);
exit(EXIT_FAILURE);
}
if ((out_file = fopen(argv[2], "w")) == NULL) {
perror("Error opening file");
exit(EXIT_FAILURE);
}
num_accesses = strtol(argv[1], NULL, 10);
srandom(time(NULL));
for (i = 0; i < num_accesses; i++)
gen_access();
return EXIT_SUCCESS;
}
void gen_access(void)
{
static int base, count;
long addr;
if (count == 0) {
base = random() % HI_ADDR;
count = 25 + (random() % 20); /*generate 25-35 mem accesses around base*/
}
addr = 10 - (random() % 20) + base;
if (addr < LO_ADDR)
addr = random() % 10;
if (addr >= HI_ADDR)
addr = base;
fprintf(out_file, "%lu", addr); /* the address */
if ((random() % 4) == 0)
fprintf(out_file, " %lu\n", random()); /* 25% of the accesses will be
* writes */
else
fprintf(out_file, "\n");
count--;
}
yeah, header files..
makefile:
CS_OBJS = cache-sim.o
CS_HFILES =
GEN_OBJS = gen_access.o
GEN_HFILES =
CC = gcc
FLAGS = -pedantic -O2 -Wall
%.o: %.c
$(CC) $(FLAGS) -c $<
all: cache-sim gen_access
cache-sim: $(CS_OBJS) $(CS_HFILES)
$(CC) $(FLAGS) -o cache-sim $(CS_OBJS)
gen_access: $(GEN_OBJS) $(GEN_HFILES)
$(CC) $(FLAGS) -o gen_access $(GEN_OBJS)
clean:
rm -fr *.o gen_access cache-sim core
cache test sequence:
503
520
517
514 1331704529
511
518
503
504
519
517
514
509
515
516
503 709403117
505
509
510
516
517
505
506
509
520
518 1365352847
520
508
518
514
513
510 1985531838
504 2142599432
506
513
507
232
232
242
241
241
238
246
241 1596812504
229 1434860694
230 1429976478
234
229 1554307710
245
244
235
237
242
248
229
244
239
230
245
236
230
229
246 584244135
2192 702465288
2199
2192
2210 1639432451
2198
2203
2207
2206
2197
2197
2206 166870671
2201
2200
2196
2194 1962719074
2202
2206 142799593
2194
2205 1758534999
2198
2208
2204
2194
2202
2202 274216585
2203
2201
2210
1168
1173
1169
1177
1183 1947044307
1166
1176
1173
1174
1173
1172
1176 1193720248
1181
1170 415125196
1181
1176
1174
1183
1172
1181
1168
1174
1182
1170
1169 414691673
1168
1184
1167 1767758641
1170
1169 464232718
1166
1185 359117980
1171 1278177093
1182
1168
1185 365136972
1169 633062792
1176
1184
1167 398932282
3995 132784035
3996 254988822
3984 614106802
3997
3986 2002635178
3997
3991
3985
3985
3994
3999
3990
3998 1641314374
3990
3993
3993
4002
3996
3992
3996
3983
3990 1600262841
3991
3999
3983
3989
3996
4002 609303961
3993
3991
3997 129832879
quote:
Originally posted by davinox
Originally posted by drizzt81
ok, let's see..
I am sure all of you are interested in my retarded Combinatorics prject, so let me post the source code here:
yeah that's spam. u should be banned too. padding your post count is illegal. and it's being supported by mods? how gross. lemme throw up.
that project is weak..lemme post *my* old project, it's a direct and associative cpu cache simulator. hehe.. enjoy! (tru compiling and runnin if *really* bored)
***cache-sim.c***
code:
#include
#include
#include
/*enums for setting cache and rep types*/
enum {DIRECT, TWOWAY};
enum {RAND, LRU};
enum {INVALID, VALID};
enum {STATUS, TAG, DATA};
#define MEM_SIZE 4096
static int mem[MEM_SIZE]; /* 16KB main memory */
int csize, ctype, rtype;
struct cache {
int *direct[3];
/*int *tag;
int *data;
int *status;*/
} cache;
void cache_lookup(int addr)
{
int tag = (addr/csize);
int index = (addr%csize);
if (cache.direct[STATUS][index] == VALID)
printf("Cache Hit!\n");
else {
printf("Cache Miss!\n");
cache.direct[TAG][index] = tag; /*store into cache if miss*/
cache.direct[DATA][index] = mem[addr];
cache.direct[STATUS][index] = VALID; /*store and mark valid*/
}
printf("Memory Address: %d\n", addr);
printf("Cache Index: %d\n", index);
printf("Cache Tag: %d\n", tag);
printf("*****************************\n");
}
void cache_write(int addr, int data)
{
int tag = (addr/csize);
int index = (addr%csize);
if (cache.direct[STATUS][index] == VALID)
printf("Cache Hit!\n");
else
printf("Cache Miss!\n");
printf("Memory Address: %d\n", addr);
printf("Cache Index: %d\n", index);
printf("Cache Tag: %d\n", tag);
printf("*****************************\n");
cache.direct[TAG][index] = tag;
cache.direct[DATA][index] = data;
cache.direct[STATUS][index] = VALID;
mem[addr] = data; /*write through to memory*/
}
void dump_memory(char *fname)
{
FILE *mem_out;
const int apl = 3; /* number of mem addr contents printed per line */
int i;
if ((mem_out = fopen(fname, "w")) == NULL) {
perror(fname);
exit(EXIT_FAILURE);
}
for (i = 0; i < MEM_SIZE; i ++) {
if (!(i % apl) && i)
fprintf(mem_out, "\n");
fprintf(mem_out, "MEM[%d]=%-10d ", i, mem[i]);
}
}
#define BUF_SIZE 512
void sim(char *filename)
{
FILE *mem_access;
char line_buf[BUF_SIZE];
int match, addr, data;
if ((mem_access = fopen(filename, "r")) == NULL) {
perror(filename);
exit(EXIT_FAILURE);
}
while (fgets(line_buf, BUF_SIZE, mem_access) != NULL) {
match = sscanf(line_buf, "%u %u", &addr, &data);
if (match == 1) {
cache_lookup(addr);
} else if (match == 2) {
cache_write(addr, data);
} else {
fprintf(stderr, "Error: Invalid Line %s\n", line_buf);
exit(EXIT_FAILURE);
}
}
dump_memory("mem_dump");
}
int main(int argc, char **argv) {
char *memfile;
if (argc != 5) {
printf("Usage: cache-sim \n");
return EXIT_FAILURE;
}
csize = atoi(argv[1]);
if (strcmp(argv[2], "--direct") == 0)
ctype = DIRECT;
else if (strcmp(argv[2], "--2way") == 0)
ctype = TWOWAY;
else
printf("Invalid cache type\n");
if (strcmp(argv[3], "--rand") == 0)
rtype = RAND;
else if (strcmp(argv[3], "--lru") == 0)
rtype = LRU;
if (ctype == DIRECT) {
if ((cache.direct[TAG] = malloc(sizeof(int)*(csize))) == NULL)
exit(EXIT_FAILURE);
if ((cache.direct[DATA] = malloc(sizeof(int)*(csize))) == NULL)
exit(EXIT_FAILURE);
if ((cache.direct[STATUS] = malloc(sizeof(int)*(csize))) == NULL)
exit(EXIT_FAILURE);
}
memfile = argv[4];
sim(memfile);
return EXIT_SUCCESS;
}
***gen_access.c***
code:
#include
#include
#include
#include
#include
FILE *out_file;
#define LO_ADDR 0
#define HI_ADDR 4096
/* GEN_ACCESS
* Generate a memory access
* of the form: [data]
*/
static void gen_access(void);
int main(int argc, char **argv)
{
long i, num_accesses;
if (argc != 3) {
fprintf(stderr, "Command line: %s \n",
argv[0]);
exit(EXIT_FAILURE);
}
if ((out_file = fopen(argv[2], "w")) == NULL) {
perror("Error opening file");
exit(EXIT_FAILURE);
}
num_accesses = strtol(argv[1], NULL, 10);
srandom(time(NULL));
for (i = 0; i < num_accesses; i++)
gen_access();
return EXIT_SUCCESS;
}
void gen_access(void)
{
static int base, count;
long addr;
if (count == 0) {
base = random() % HI_ADDR;
count = 25 + (random() % 20); /*generate 25-35 mem accesses around base*/
}
addr = 10 - (random() % 20) + base;
if (addr < LO_ADDR)
addr = random() % 10;
if (addr >= HI_ADDR)
addr = base;
fprintf(out_file, "%lu", addr); /* the address */
if ((random() % 4) == 0)
fprintf(out_file, " %lu\n", random()); /* 25% of the accesses will be
* writes */
else
fprintf(out_file, "\n");
count--;
}
yeah, header files..
makefile:
CS_OBJS = cache-sim.o
CS_HFILES =
GEN_OBJS = gen_access.o
GEN_HFILES =
CC = gcc
FLAGS = -pedantic -O2 -Wall
%.o: %.c
$(CC) $(FLAGS) -c $<
all: cache-sim gen_access
cache-sim: $(CS_OBJS) $(CS_HFILES)
$(CC) $(FLAGS) -o cache-sim $(CS_OBJS)
gen_access: $(GEN_OBJS) $(GEN_HFILES)
$(CC) $(FLAGS) -o gen_access $(GEN_OBJS)
clean:
rm -fr *.o gen_access cache-sim core
cache test sequence:
503
520
517
514 1331704529
511
518
503
504
519
517
514
509
515
516
503 709403117
505
509
510
516
517
505
506
509
520
518 1365352847
520
508
518
514
513
510 1985531838
504 2142599432
506
513
507
232
232
242
241
241
238
246
241 1596812504
229 1434860694
230 1429976478
234
229 1554307710
245
244
235
237
242
248
229
244
239
230
245
236
230
229
246 584244135
2192 702465288
2199
2192
2210 1639432451
2198
2203
2207
2206
2197
2197
2206 166870671
2201
2200
2196
2194 1962719074
2202
2206 142799593
2194
2205 1758534999
2198
2208
2204
2194
2202
2202 274216585
2203
2201
2210
1168
1173
1169
1177
1183 1947044307
1166
1176
1173
1174
1173
1172
1176 1193720248
1181
1170 415125196
1181
1176
1174
1183
1172
1181
1168
1174
1182
1170
1169 414691673
1168
1184
1167 1767758641
1170
1169 464232718
1166
1185 359117980
1171 1278177093
1182
1168
1185 365136972
1169 633062792
1176
1184
1167 398932282
3995 132784035
3996 254988822
3984 614106802
3997
3986 2002635178
3997
3991
3985
3985
3994
3999
3990
3998 1641314374
3990
3993
3993
4002
3996
3992
3996
3983
3990 1600262841
3991
3999
3983
3989
3996
4002 609303961
3993
3991
3997 129832879 |
hahaha... this used to give me nightmares... hope it does the same to all of you!!!
suffer, bi0tches :eyespop: :eyespop: :eyespop: :eyespop: :eyespop: :eyespop: :eyespop: :eyespop: :eyespop: |
|
|