This commit is contained in:
parent
1978f1277c
commit
b02004f62e
|
|
@ -1 +1,68 @@
|
|||
#include "cache.h"
|
||||
Cache::Cache(char *argv[])
|
||||
{
|
||||
this->blockSize = atoi(argv[1]);
|
||||
this->size = atoi(argv[2]);
|
||||
this->Assoc = atoi(argv[3]);
|
||||
this->replicementPolicy = atoi(argv[4]);
|
||||
this->writePolicy = atoi(argv[5]);
|
||||
|
||||
if ((this->blockSize < 0) || !(this->blockSize ^ (this->blockSize - 1)))
|
||||
{
|
||||
throw "Para blockSize Err.";
|
||||
}
|
||||
if (this->size < 0)
|
||||
{
|
||||
throw "Para blockSize Err.";
|
||||
}
|
||||
if (this->Assoc < 1)
|
||||
{
|
||||
throw "Para blockSize Err.";
|
||||
}
|
||||
|
||||
if (this->replicementPolicy != LRU && this->replicementPolicy != LFU)
|
||||
{
|
||||
throw "Para blockSize Err.";
|
||||
}
|
||||
|
||||
if (this->writePolicy != WBWA && this->writePolicy != WTNA)
|
||||
{
|
||||
throw "Para blockSize Err.";
|
||||
}
|
||||
|
||||
this->offset = log2_floor(this->blockSize);
|
||||
this->index = log2_floor(this->size / (this->blockSize * this->assoc));
|
||||
this->tagBit = 32 - this->offset - this->index;
|
||||
|
||||
this->offsetMask = (1 << this->offset) - 1;
|
||||
this->indexMask = ((1 << this->index) - 1) << this->offset;
|
||||
|
||||
this->L1.resize(this->getMaxIndex());
|
||||
for (auto &s : L1 )
|
||||
s.block.resize(this->GetAssoc());
|
||||
}
|
||||
|
||||
Cache::~Cache()
|
||||
{
|
||||
}
|
||||
|
||||
uint32_t Cache::getMaxIndex(){
|
||||
return (1<<this->index);
|
||||
}
|
||||
uint32_t Cache::GetAssoc() {
|
||||
return this->assoc;
|
||||
}
|
||||
|
||||
CacheTIO Cache::Address2TIO(uint32_t addr)
|
||||
{
|
||||
uint32_t offset = addr & om;
|
||||
uint32_t index = (addr & im) >> o;
|
||||
uint32_t tag = addr >> (o + i);
|
||||
return {tag, index, offset};
|
||||
}
|
||||
|
||||
void Cache::writeCache(uint32_t address){
|
||||
|
||||
}
|
||||
void Cache::readCache(uint32_t address);
|
||||
void Cache::useCache(uint32_t address, int method);
|
||||
|
|
@ -1,24 +1,71 @@
|
|||
#pragma once
|
||||
#include "public.h"
|
||||
#include <iostream>
|
||||
#include <ostream>
|
||||
#include <vector>
|
||||
|
||||
class Cache
|
||||
{
|
||||
private:
|
||||
/* data */
|
||||
public:
|
||||
Cache(/* args */);
|
||||
~Cache();
|
||||
void writeCache(uint32_t address);
|
||||
void readCache(uint32_t address);
|
||||
struct CacheTIO {
|
||||
uint32_t tag, index, offset;
|
||||
};
|
||||
|
||||
struct Block{
|
||||
uint32_t tag = 0; // tag
|
||||
uint32_t cb = 0; // count block
|
||||
uint32_t lu = 0; // Last Used
|
||||
bool d = false; // dirty
|
||||
bool v = false; // valid
|
||||
bool operator<(const Block &t) {return cb < t.cb;}
|
||||
friend ostream& operator<<(ostream& os, Block&b) {
|
||||
os << "{tag:" << b.tag << ", Count-Block:" << b.cb << ", Last-Used:" << b.lu << ", Dirty:" << b.d << ", Valid:" << b.v << "}";
|
||||
return os;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
Cache::Cache(/* args */)
|
||||
{
|
||||
}
|
||||
struct Set {
|
||||
uint32_t countSet = 0; // count set
|
||||
vector<Block> block; // Blocks
|
||||
};
|
||||
|
||||
Cache::~Cache()
|
||||
class Cache
|
||||
{
|
||||
}
|
||||
private:
|
||||
//Param
|
||||
int blockSize; // Block Size
|
||||
int size; // Size
|
||||
int assoc; // Assoc
|
||||
int replicementPolicy; // Replicement Policy
|
||||
int writePolicy; // Write Policy: 1 -> WTNA, 0 ->
|
||||
|
||||
int tagBit; // tag bit
|
||||
int index; // index
|
||||
int offset; // offset
|
||||
|
||||
uint32_t offsetMask; // offset mask
|
||||
uint32_t indexMask; // index mask
|
||||
|
||||
//Count
|
||||
int readCount = 0; // Read Count
|
||||
int readMiss = 0; // Read Miss
|
||||
int writeCount = 0; // Write Count
|
||||
int writeMiss = 0; // Write Miss
|
||||
int writeBack = 0; // Write Back
|
||||
uint32_t lru = 0; // LRU Global Counter
|
||||
|
||||
// main cache
|
||||
vector<Set> L1;
|
||||
|
||||
// L2 cache
|
||||
Cache * L2;
|
||||
public:
|
||||
Cache(char *argv[]);
|
||||
~Cache();
|
||||
|
||||
uint32_t getMaxIndex();
|
||||
uint32_t getAssoc();
|
||||
|
||||
void writeCache(uint32_t address);
|
||||
void readCache(uint32_t address);
|
||||
void useCache(uint32_t address, int method);
|
||||
CacheTIO Address2TIO(uint32_t addr);
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,6 +1,27 @@
|
|||
|
||||
#include "world.h"
|
||||
#include "cache.h"
|
||||
#include "public.h"
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
hello_world(argc, argv);
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
try
|
||||
{
|
||||
if (argc != 7)
|
||||
{
|
||||
throw "Para Err."
|
||||
}
|
||||
|
||||
auto fileName = std::string(argv[6]);
|
||||
struct stat buffer;
|
||||
if (stat(("../traces/" + tf).c_str(), &buffer))
|
||||
{
|
||||
throw "traces file Err.";
|
||||
}
|
||||
|
||||
auto cache = new Cache(char *argv[]);
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,7 @@
|
|||
#pragma once
|
||||
|
||||
#define LRU 0
|
||||
#define LFU 1
|
||||
|
||||
#define WBWA 0
|
||||
#define WTNA
|
||||
|
|
@ -71,25 +71,8 @@ public:
|
|||
int WritePolicy() {return wp;}
|
||||
string TraceFile() {return tf;}
|
||||
|
||||
/**
|
||||
* @brief Extract Cache Parameter p
|
||||
*
|
||||
* @param argc
|
||||
* @param argv
|
||||
* @return
|
||||
* 1: Error (throw)
|
||||
* 0: Success
|
||||
*/
|
||||
int Extract(int argc, char* argv[]);
|
||||
|
||||
// ===== Simulator configuration =====
|
||||
// L1_BLOCKSIZE: 16
|
||||
// L1_SIZE: 16384
|
||||
// L1_ASSOC: 1
|
||||
// L1_REPLACEMENT_POLICY: 0
|
||||
// L1_WRITE_POLICY: 0
|
||||
// trace_file: gcc_trace.txt
|
||||
// ===================================
|
||||
friend ostream& operator<<(ostream &os, const CacheParam& cp){
|
||||
os << " ===== Simulator configuration =====\n";
|
||||
os << " L1_BLOCKSIZE:" << setw(22) << cp.bs << endl;
|
||||
|
|
@ -102,18 +85,10 @@ public:
|
|||
return os;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get HT
|
||||
* @return 0.25 + 2.5 * (s/ (512.0 * 1024)) + 0.025 * (bs/ 16.0) + 0.025 * as;
|
||||
*/
|
||||
double GetHT() {
|
||||
return 0.25 + 2.5 * (s/ (512.0 * 1024)) + 0.025 * (bs/ 16.0) + 0.025 * as;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get Miss Penalty
|
||||
* @return 20 + 0.5 * (bs / 16.0);
|
||||
*/
|
||||
double GetMP() {
|
||||
return 20 + 0.5 * (bs / 16.0);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue