#include "FunctionUsed.h"
using namespace std;
bool GetHitNum(char *address)
{
bool is_RD_data = false;
bool is_WT_data = false;
bool is_IR_fetch = false;
bool is_space = false;
bool hit = false;
switch(address[0])
{
case '0':is_RD_data = true;break;
case '1':is_WT_data = true;break;
case '2':is_IR_fetch = true;break;
case '\0':is_space = true;break;
default:
cout << "指令类型位:" <<address[0] << endl;
cout << "定义错误" << endl;return false;
}
temp = strtoul(address+2,NULL,16);
bitset<32> flags((long)temp);
hit = IsHit(flags);
/*Cache命中——读数据操作*/
if(hit && is_RD_data)
{
num_access++;
num_RD_data++;
num_RD_data_hit++;
num_hit++;
if(t_replace == LRU)
{
LruHitProcess();
}
}
/*Cache命中——写数据操作*/
else if(hit && is_WT_data)
{
num_access++;
num_WT_data++;
num_WT_data_hit++;
num_hit++;
cache_item[current_line][29] = true;
if(t_replace == LRU)
{
LruHitProcess();
}
}
/*Cache命中——取指令操作*/
else if(hit && is_IR_fetch)
{
num_access++;
num_IR_fetch++;
num_IR_fetch_hit++;
num_hit++;
if(t_replace == LRU)
{
LruHitProcess();
}
}
/*Cache未命中——读数据操作*/
else if((!hit) && is_RD_data)
{
num_access++;
num_RD_data++;
GetRead(flags);
if(t_replace == FIFO)
{
FifoUnhitSpace();
}
else if(t_replace == LRU)
{
LruUnhitSpace();
}
}
/*Cache未命中——写数据操作*/
else if((!hit) && is_WT_data)
{
num_access++;
num_WT_data++;
GetRead(flags);
cache_item[current_line][29] = true;
if(t_replace == FIFO)
{
FifoUnhitSpace();
}
else if(t_replace == LRU)
{
LruUnhitSpace();
}
}
/*Cache未命中——取指令操作*/
else if((!hit) && is_IR_fetch)
{
num_access++;
num_IR_fetch++;
GetRead(flags);
if(t_replace == FIFO)
{
FifoUnhitSpace();
}
else if(t_replace == LRU)
{
LruUnhitSpace();
}
}
else if(is_space)
{
num_space++;
}
else
{
cerr << "错误" << endl;
return false;
}
return true;
}
bool IsHit(bitset<32> flags)
{
bool ret = false;
/*直接映射方式下判断命中*/
if(t_assoc == direct_mapped)
{
bitset<32> flags_line;
for(j=0,i=(bit_block);i<(bit_block+bit_line);j++,i++) //判断在Cache中映射的块号
{
flags_line[j] = flags[i];
}
current_line = flags_line.to_ulong();
if(cache_item[current_line][30]==true) //判断hit位是否为真
{
ret = true;
for(i=31,j=28;i>(31ul-bit_tag);i--,j--) //判断标记是否相同,i:address,j:cache
{
if(flags[i] != cache_item[current_line][j])
{
ret = false;
break;
}
}
}
}
/*全相联映射方式下判断命中*/
else if(t_assoc == full_associative)
{
for(temp=0;temp<num_line;temp++) //比较判断全部的Cache块
{
if(cache_item[temp][30]==true) //判断hit位是否为真
{
ret = true;
for(i=31,j=28;i>(31ul-bit_tag);i--,j--) //判断标记是否相同,i:address,j:cache
{
if(flags[i] != cache_item[temp][j])
{
ret = false;
break;
}
}
}
if(ret == true)
{
current_line = temp;
break;
}
}
}
/*组相联映射方式下判断命中*/
else if(t_assoc == set_associative)
{
bitset<32> flags_set;
for(j=0,i=(bit_block);i<(bit_block+bit_set);j++,i++) //判断在cache多少组
{
flags_set[j] = flags[i];
}
current_set = flags_set.to_ulong();
for(temp=(current_set*cache_set);temp<((current_set+1)*cache_set);temp++) //判断该组在Cache中的块号范围
{
if(cache_item[temp][30]==true) //判断hit位是否为真
{
ret = true;
for(i=31,j=28;i>(31ul-bit_tag);i--,j--) //判断标记是否相同,i:address,j:cache
{
if(flags[i] != cache_item[temp][j])
{
ret = false;
break;
}
}
}
if(ret == true)
{
current_line = temp;
break;
}
}
}
return ret;
}
void GetRead(bitset<32> flags)
{
/*直接映射方式下主存块—>Cache块*/
if(t_assoc == direct_mapped)
{
if(cache_item[current_line][30] == false) //不命中
{
for(i=31,j=28;i>(31ul-bit_tag);i--,j--) //设置标记 (31为有效位 30为hit位 29为脏位)从28开始
{
cache_item[current_line][j] = flags[i];
}
cache_item[current_line][30] = true; //设置hit位为true
}
else
{
GetReplace(flags);
}
}
/*全相联映射方式下主存块—>Cache块*/
else if(t_assoc == full_associative)
{
bool space = false;
for(temp=0;temp<num_line;temp++)
{
if(cache_item[temp][30] == false) //find a space line
{
space = true;
break;
}
}
/*Cache未被填满*/
if(space == true)
{
current_line = temp;
for(i=31,j=28;i>(31ul-bit_tag);i--,j--) //设置标记
{
cache_item[current_line][j] = flags[i];
}
cache_item[current_line][30] = true; //设置hit位为true.
if(t_replace == FIFO)
{
FifoUnhitSpace();
}
else if(t_replace == LRU)
{
LruUnhitSpace();
}
}
/*Cache被填满*/
else
{
GetReplace(flags);
}
}
/*组相联映射方式下主存块—>Cache块*/
else if(t_assoc == set_associative)
{
bool space = false;
for(temp=(current_set*cache_set);temp<((current_set+1)*cache_set);temp++)
{
if(cache_item[temp][30] == false) //find a space line
{
space = true;
break;
}
}
/*Cache相关组未被填满*/
if(space == true)
{
current_line = temp;
for(i=31,j=28;i>(31ul-bit_tag);i--,j--) //设置标记
{
cache_item[current_line][j] = flags[i];
}
cache_item[current_line][30] = true; //设置hit位为true.
if(t_replace == FIFO)
{
FifoUnhitSpace();
}
else if(t_replace == LRU)
{
LruUnhitSpace();
}
}
/*Cache相关组被填满*/
else
{
GetReplace(flags);
}
}
}
void GetReplace(bitset<32> flags)
{
/*直接映射方式下块替换过程*/
if(t_assoc == direct_mapped)
{
}
/*全相联映射方式下块替换过程*/
else if(t_assoc == full_associative)
{
if(t_replace == Random)
{
current_line = rand()/(RAND_MAX/num_line+1); // 随机块号(0,num_line-1)
}
else if(t_re
评论0