测试程序:可直接运行
#include <iostream>
using namespace std;
#define line 64
unsigned calc_address(unsigned head, int i, int j, int N,int bias)
{
return head + i * N * 16 + j * 16 + bias * 4;
}
unsigned calc_line(unsigned add, int block_size)
{
return (add / block_size) % line;
}
int isHit(unsigned cache[], unsigned r, unsigned a, int block_size)
{
a = a / block_size * block_size;
unsigned tmp = cache[r];
cache[r] = a;
return tmp == a;
}
void question_6_38(int block_size, unsigned add, const int N)
{
unsigned cache[line];
memset(cache, -1, sizeof(cache));
int hit = 0, miss = 0;
for (int i = 0; i < N; ++i)
for (int j = 0; j < N; ++j)
{
unsigned address = calc_address(add, i, j, N, 0);
unsigned r = calc_line(address, block_size);
hit += isHit(cache, r, address, block_size);
address = calc_address(add, i, j, N, 1);
r = calc_line(address, bl