feat: get Index offset address
This commit is contained in:
parent
a5febee25e
commit
9a545ccc37
11
main.cpp
11
main.cpp
@ -1,8 +1,6 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
// #include "block.h"
|
|
||||||
#include "simulator.h"
|
#include "simulator.h"
|
||||||
// using namespace std;
|
|
||||||
|
|
||||||
int main(){
|
int main(){
|
||||||
std::string temp;
|
std::string temp;
|
||||||
@ -26,5 +24,12 @@ int main(){
|
|||||||
std::cout << "index : " << indexSize << std::endl;
|
std::cout << "index : " << indexSize << std::endl;
|
||||||
std::cout << "tag : " << tagSize << std::endl;
|
std::cout << "tag : " << tagSize << std::endl;
|
||||||
|
|
||||||
CacheSim::Simulator(setSize, waySize, tagSize);
|
CacheSim::Simulator sim(machineBits, setSize, waySize, tagSize);
|
||||||
|
|
||||||
|
std::string action, address;
|
||||||
|
while(std::cin >> action >> address){
|
||||||
|
std::cout << "action: "<< action << " address: " << address << std::endl;
|
||||||
|
sim.read(address);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,9 +1,60 @@
|
|||||||
#include "simulator.h"
|
#include "simulator.h"
|
||||||
|
#include <sstream>
|
||||||
|
#include <bitset>
|
||||||
using CacheSim::Simulator;
|
using CacheSim::Simulator;
|
||||||
|
using std::stringstream;
|
||||||
|
using std::bitset;
|
||||||
|
|
||||||
Simulator::Simulator(short setSize, short waySize, short tagSize){
|
Simulator::Simulator(int machineBits, short setSize, short waySize, short tagSize){
|
||||||
this->setSize = setSize;
|
this->setSize = setSize;
|
||||||
|
this->tagSize = tagSize;
|
||||||
|
this->machineBits = machineBits;
|
||||||
for(int i=0; i<setSize; i++){
|
for(int i=0; i<setSize; i++){
|
||||||
this->sets.push_back(Set(waySize, tagSize));
|
this->sets.push_back(Set(waySize, tagSize));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool Simulator::read(string address){
|
||||||
|
vector<bool> addrBits = this->addressTranslate(address);
|
||||||
|
for(int i=0; i<addrBits.size(); i++){
|
||||||
|
std::cout << addrBits[i] << " ";
|
||||||
|
}
|
||||||
|
// std::cout << std::endl << addrBits.size() << std::endl;
|
||||||
|
short index = this->getIndex(addrBits);
|
||||||
|
// std::cout<<index<<std::endl;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Simulator::write(string address){
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
vector<bool> Simulator::addressTranslate(string address){
|
||||||
|
vector<bool> ans;
|
||||||
|
ans.reserve(this->tagSize);
|
||||||
|
|
||||||
|
stringstream ss;
|
||||||
|
ss << std::hex << address;
|
||||||
|
unsigned long long number;
|
||||||
|
ss >> number;
|
||||||
|
|
||||||
|
bitset<64> bits(number);
|
||||||
|
|
||||||
|
for(int i=this->machineBits-1; i>=0; i--){
|
||||||
|
ans.push_back(bits[i]);
|
||||||
|
}
|
||||||
|
return ans;
|
||||||
|
}
|
||||||
|
|
||||||
|
short Simulator::getIndex(vector<bool> addrBits){
|
||||||
|
short ans = 0 , base = 1;
|
||||||
|
// std::cout<<"hi"<<std::endl;
|
||||||
|
for(int i=(this->tagSize+log2(this->setSize)-1); i>=this->tagSize; i--){
|
||||||
|
// std::cout<<i<<std::endl;
|
||||||
|
ans += base * addrBits[i];
|
||||||
|
base *= 2;
|
||||||
|
}
|
||||||
|
// std::cout<<"bye"<<std::endl;
|
||||||
|
|
||||||
|
return ans;
|
||||||
|
}
|
||||||
12
simulator.h
12
simulator.h
@ -1,15 +1,25 @@
|
|||||||
#include "set.h"
|
#include "set.h"
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <iostream>
|
||||||
|
#include <math.h>
|
||||||
using CacheSim::Set;
|
using CacheSim::Set;
|
||||||
using std::vector;
|
using std::vector;
|
||||||
|
using std::string;
|
||||||
|
|
||||||
namespace CacheSim{
|
namespace CacheSim{
|
||||||
class Simulator
|
class Simulator
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
short setSize;
|
short setSize;
|
||||||
|
short tagSize;
|
||||||
|
int machineBits;
|
||||||
vector<Set> sets;
|
vector<Set> sets;
|
||||||
public:
|
public:
|
||||||
Simulator(short setSize, short waySize, short tagSize);
|
Simulator(int machineBits, short setSize, short waySize, short tagSize);
|
||||||
|
bool read(string address);
|
||||||
|
bool write(string address);
|
||||||
|
vector<bool> addressTranslate(string address);
|
||||||
|
short getIndex(vector<bool> addrBits);
|
||||||
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user