NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

submit.cs (Brandon S Chow)
Home
Join Class
Logout
Feedback / Suggestions
cache.c Download
#include <stdio.h>
#include <stdlib.h>

typedef struct Set{
int flag;
int valid;
int tag;
}Set;

typedef struct Cache{

int MissedCount;
int AccessedCount;
int AccessTime;
int blocksize;
int cachesize;
int indexsize;
//int LRU[8];
int type;
int BlockPerSet;
int SetNum;
int offset;
Set *set1;
Set *set2;
Set *set3;
Set *set4;
Set *set5;
Set *set6;
Set *set7;
Set *set8;

}Cache;


// If you want to declare structs or extra helper functions, do so at the
// top of your cachefunctions.c. Do not add anything to this file.

int logFun(int x)
{
int xcopy = x;
int result = 0;
while(xcopy>>=1)
result++;
return result;
}

void *createAndInitialize(int blocksize, int cachesize, int type)
// You have a struct that contains all of the information for one cache.
// In this function, you create the cache and initialize it,
// returning a pointer to the struct. Because you are determining the struct,
// you return a void * to our main. Type 0 is a direct-mapped cache.
// Type 1 is a 2-way pseudo-associative cache. Type 2 is an 8-way
// set-associative cache.
{

int i;
int BlockPerSet;
Cache *cache = malloc(sizeof(struct Cache));

if(type == 0 || type ==2){
//direct map and 8 way
BlockPerSet = cachesize >> logFun(blocksize);
//printf("a. cache->BlockPerSet: %dn",cache->BlockPerSet);
//printf("cache->BlockPerSet: %dncachesize: %dnlogFun(blocksize): %dnblocksize: %dn",cache->BlockPerSet, cachesize, logFun(blocksize), blocksize);
}

else if(type ==1){
//two way
BlockPerSet = (cachesize >> logFun(blocksize))>>1;
//printf("b. cache->BlockPerSet: %dn",cache->BlockPerSet);
//Possibly here.
}

cache->set1 = malloc(sizeof(struct Set)*(BlockPerSet));
cache->set2 = malloc(sizeof(struct Set)*(BlockPerSet));
cache->set3 = malloc(sizeof(struct Set)*(BlockPerSet));
cache->set4 = malloc(sizeof(struct Set)*(BlockPerSet));
cache->set5 = malloc(sizeof(struct Set)*(BlockPerSet));
cache->set6 = malloc(sizeof(struct Set)*(BlockPerSet));
cache->set7 = malloc(sizeof(struct Set)*(BlockPerSet));
cache->set8 = malloc(sizeof(struct Set)*(BlockPerSet));

cache->MissedCount = 0;
cache->AccessedCount = 0;
cache->AccessTime = 0;
cache->blocksize = blocksize;
cache->cachesize = cachesize;
cache->type = type;


cache->indexsize = logFun(BlockPerSet);
//printf("afterlogFun(cache->BlockPerSet). cache->BlockPerSet: %dn",cache->BlockPerSet);
cache->offset = logFun(blocksize);

//printf("afterlogFun(blocksize). cache->BlockPerSet: %dn",cache->BlockPerSet);



for (i = 0; i < BlockPerSet; i++)
{
cache->set1[i].valid = 0;
cache->set2[i].valid = 0;
}
//printf("aftervaliditiesofset1and2. cache->BlockPerSet: %dn",cache->BlockPerSet);
if(cache->type==2)
{
cache->set3[i].valid = 0;
cache->set4[i].valid = 0;
cache->set5[i].valid = 0;
cache->set6[i].valid = 0;
cache->set7[i].valid = 0;
cache->set8[i].valid = 0;
}

cache->BlockPerSet = BlockPerSet;
//printf("aftervaliditiesof3-8. cache->BlockPerSet: %dn",cache->BlockPerSet);

/*cache->LRU[0] = 1;
cache->LRU[1] = 2; */

//printf("0. cache->BlockPerSet: %dn",cache->BlockPerSet);

return cache;

}


int accessCache(void *cache, int address){
//In this function, we access the cache with a particular address.
//If the address results in a hit, return 1. If it is a miss, return 0.

//printf("1. cache->BlockPerSet: %dn", cache->BlockPerSet);
Cache *newcache = (Cache*)cache;


//printf("2. newcache->BlockPerSet: %dn", newcache->BlockPerSet);

int index = (address >> newcache->offset) & (newcache->BlockPerSet-1);

//printf("2. newcache->BlockPerSet: %dn", newcache->BlockPerSet);

if(index>(newcache->BlockPerSet))
{
printf("Index: %dnnewcache->BlockPerSet: %dnnewcache->offset: %dnnewcache->BlockPerSet-1: %dn",index,newcache->BlockPerSet,newcache->offset,newcache->BlockPerSet-1);
printf("Segfault!n");
return 1;
}
int tag = address >> (newcache->offset + newcache->indexsize);
int type = newcache->type;
int valid_1 = newcache -> set1[index].valid;
int valid_2 = newcache -> set2[index].valid;
int tag_1 = newcache -> set1[index].tag;
int tag_2 = newcache -> set2[index].tag;

int valid_3,valid_4, valid_5,valid_6,valid_7, valid_8,
tag_3, tag_4, tag_5, tag_6, tag_7, tag_8;

if(type==2)
{
valid_3 = newcache -> set3[index].valid;
valid_4 = newcache -> set4[index].valid;
valid_5 = newcache -> set5[index].valid;
valid_6 = newcache -> set6[index].valid;
valid_7 = newcache -> set7[index].valid;
valid_8 = newcache -> set8[index].valid;
tag_3 = newcache -> set3[index].tag;
tag_4 = newcache -> set4[index].tag;
tag_5 = newcache -> set5[index].tag;
tag_6 = newcache -> set6[index].tag;
tag_7 = newcache -> set7[index].tag;
tag_8 = newcache -> set8[index].tag;
}

newcache->AccessedCount += 1;
newcache->AccessTime+=1;

//direct map
if (type == 0){
//if HIT
//printf("tag_1: %dntag: %dnvalid_1: %dnn",tag_1,tag,valid_1);
if ((valid_1 ==1)&&(tag_1 == tag))
{
//printf("We got a hitn");
return 1;
}

//if MISS
else
{
newcache -> AccessTime += 100;
newcache -> MissedCount ++;
newcache -> set1[index].tag = tag;
newcache -> set1[index].valid = 1;
return 0;
}
}
//2-associative
//if MISS
else if (type == 1){
if(valid_1 == 0 && valid_2 ==0)

{
newcache->AccessTime+=101;
newcache->MissedCount++;
newcache->set1[index].tag = tag;
//newcache->set2[index].tag = tag_1;
newcache->set1[index].valid = 1;
newcache->set2[index].valid = 1;
//newcache->set2[index].tag =-1;
return 0;
}

// if HIT
else if(tag_1 == tag || tag_2 == tag){
if (tag_2 == tag){
int tmp_tag = tag_1;
newcache->set1[index].tag = tag_2;
newcache->set2[index].tag = tmp_tag;
newcache->AccessTime+=1;
}
return 1;
}
//if other MISS
else if(valid_1==1 &&valid_2==1)
{
newcache->set2[index].tag=tag_1;
newcache->set1[index].tag=tag;
newcache->MissedCount++;
newcache->AccessTime+=101;
return 0;
}
}

// 8-associative
else if (type ==2){
//if MISS
if (valid_1 == 0)
{
newcache->set1[index].valid=1;
newcache->set2[index].valid=1;
newcache->set3[index].valid=1;
newcache->set4[index].valid=1;
newcache->set5[index].valid=1;
newcache->set6[index].valid=1;
newcache->set7[index].valid=1;
newcache->set8[index].valid=1;

//NOTE DIFFERENCE!
newcache->set1[index].tag = tag;
//END NOTICING!

newcache->set2[index].tag=0;
newcache->set3[index].tag=0;
newcache->set4[index].tag=0;
newcache->set5[index].tag=0;
newcache->set6[index].tag=0;
newcache->set7[index].tag=0;
newcache->set8[index].tag=0;
newcache->MissedCount++;
newcache->AccessTime+=105;
return 0;
}

else if(newcache->set1[index].tag==tag||newcache->set2[index].tag==tag||newcache->set3[index].tag==tag||newcache->set4[index].tag==tag||newcache->set5[index].tag==tag||newcache->set6[index].tag==tag||newcache->set7[index].tag==tag||newcache->set8[index].tag==tag)
{
newcache->AccessTime+=5;
return 1;
}
//Queue, push in top pop off bottom
else{
newcache->set1[index].tag = newcache->set2[index].tag;
newcache->set2[index].tag = newcache->set3[index].tag;
newcache->set3[index].tag = newcache->set4[index].tag;
newcache->set4[index].tag = newcache->set5[index].tag;
newcache->set5[index].tag = newcache->set6[index].tag;
newcache->set6[index].tag = newcache->set7[index].tag;
newcache->set7[index].tag = newcache->set8[index].tag;
newcache->set8[index].tag=tag;
newcache->MissedCount++;
newcache->AccessTime+=105;
return 0;
}

}

return 1;
}



int missesSoFar(void *cache){
//This returns the number of misses that have occurred so far
return ((Cache*)cache)->MissedCount;
}
int accessesSoFar(void *cache){
//This returns the number of accesses that have occurred so far

return ((Cache*)cache)->AccessedCount;

}
int totalAccessTime(void *cache){
//This returns the total number of cycles that all of the accesses
//have taken so far.
return ((Cache*)cache)->AccessTime;

}



/*
int powFun(int x)
{

return 1<<x;


}
*/
© 2014 Bryce Boe
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.