【cache】图解主存与cache的地址映射方式

教材上大段的文字清楚但不直观,老师讲的有点点快,自己课下做了点整理,如有纰漏,麻烦大家评论区指出。废话不多说,开始啦!

主存与cache的地址映射方式有3种

1.全相联映射

2.直接映射

3.组相联映射


1.全相联映射

适合小容量cache采用

5a37284cf8d544859f71213d07e67294.jpg

带全部地址一起保存

9e1b76f7532645bea06cb3b92ecc668a.jpg

主存(块)与cache(行)之间多对一,即cache一行中可以有多个主存的块。

2.直接映射

适合需要大容量cache的场合,主存(块)与cache(行)之间多对一,但是跟前面的区别在于!

1440d29e735e426e9d9bc8a466a1e1e2.jpg

  • mod:取余运算
  • m:cache中总行数
  • 为方便理解,先把主存分区,使每个区的块数等于cache的行数m

dd19c60439b145069de458a20f890518.jpg

缺点:每个主存块只有一个固定的行位置可存放

ps:若连续访问块号相距m整数倍的2个块,因2个块映射到同一cache行时,就会发生冲突。发生冲突时,要将原先存入的行换出去,可能过一会又要换进来。频繁的置换会使cache效率下降。

3.组相联映射

  • 将cache分成 u组,每组 v
  • 将cache中总行数:m=u*v
  • 分到哪一组?组号:q=j\mod u
  • j:主存块号,u*v :一维转成二维

ee2dcaa63cb1449b92f45fdf1f9d405e.jpg

对比直接映射和组相联映射来看看:

f52cd0f05b08492ba5871a2e217a358e.jpg

这个是组相联的 

  • 16
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以给你提供一些代码实现的思路和参考资料。 主存-Cache地址映射是计算机组成原理中的一个重要概念。在实现主存-Cache地址映射时,需要考虑以下几个方面: 1. 地址转换机制:主存-Cache地址映射需要实现地址转换机制,将主存地址转换为Cache地址。常见的地址转换机制包括直接映射、全相联映射和组相联映射。 2. 缓存替换算法:当Cache已满时,需要使用缓存替换算法,将新的数据写入到Cache中。常见的缓存替换算法包括最近最少使用算法(LRU)、先进先出算法(FIFO)和随机替换算法。 3. 缓存写回策略:当Cache中的数据被修改后,需要选择缓存写回策略。常见的缓存写回策略包括写回策略和写直达策略。 以下是一个简单的C++程序,实现了主存-Cache地址映射的基本功能,包括地址转换、缓存替换和缓存写回。缓存大小为4,使用直接映射地址转换机制和最近最少使用算法进行缓存替换,使用写回策略进行缓存写回。 ```cpp #include <iostream> #include <vector> #include <algorithm> using namespace std; // 定义Cache块结构体 struct CacheBlock { int tag; bool valid; bool dirty; vector<int> data; int accessTime; }; // 定义Cache结构体 struct Cache { int blockSize; int cacheSize; int blockNum; int tagBits; int indexBits; int offsetBits; vector<CacheBlock> blocks; // 构造函数 Cache(int blockSize, int cacheSize) { this->blockSize = blockSize; this->cacheSize = cacheSize; this->blockNum = cacheSize / blockSize; this->tagBits = 32 - __builtin_clz(blockSize) - __builtin_clz(blockNum); this->indexBits = __builtin_ctz(blockSize); this->offsetBits = 32 - this->tagBits - this->indexBits; this->blocks.resize(blockNum); for (int i = 0; i < blockNum; i++) { blocks[i].tag = -1; blocks[i].valid = false; blocks[i].dirty = false; blocks[i].data.resize(blockSize); blocks[i].accessTime = 0; } } // 地址转换函数 int addressToIndex(int address) { return (address >> offsetBits) & (blockNum - 1); } int addressToTag(int address) { return address >> (offsetBits + indexBits); } // 缓存替换函数 int replaceBlock(int index, int tag) { int replaceIndex = 0; int minAccessTime = INT_MAX; for (int i = index; i < blockNum; i += blockNum) { if (!blocks[i].valid) { replaceIndex = i; break; } if (blocks[i].accessTime < minAccessTime) { minAccessTime = blocks[i].accessTime; replaceIndex = i; } } if (blocks[replaceIndex].dirty) { // 缓存写回 cout << "Cache block " << replaceIndex << " is dirty, write back to memory" << endl; } blocks[replaceIndex].tag = tag; blocks[replaceIndex].valid = true; blocks[replaceIndex].dirty = false; blocks[replaceIndex].accessTime = 0; return replaceIndex; } // 缓存读取函数 int read(int address) { int index = addressToIndex(address); int tag = addressToTag(address); int offset = address & (blockSize - 1); if (blocks[index].valid && blocks[index].tag == tag) { // 命中Cache blocks[index].accessTime++; return blocks[index].data[offset]; } else { // 未命中Cache cout << "Cache miss!" << endl; int replaceIndex = replaceBlock(index, tag); // 从主存中读取数据到Cache cout << "Read data from memory to cache block " << replaceIndex << endl; for (int i = 0; i < blockSize; i++) { blocks[replaceIndex].data[i] = address + i; } blocks[replaceIndex].accessTime++; return blocks[replaceIndex].data[offset]; } } // 缓存写入函数 void write(int address, int value) { int index = addressToIndex(address); int tag = addressToTag(address); int offset = address & (blockSize - 1); if (blocks[index].valid && blocks[index].tag == tag) { // 命中Cache blocks[index].accessTime++; blocks[index].data[offset] = value; blocks[index].dirty = true; } else { // 未命中Cache cout << "Cache miss!" << endl; int replaceIndex = replaceBlock(index, tag); // 从主存中读取数据到Cache cout << "Read data from memory to cache block " << replaceIndex << endl; for (int i = 0; i < blockSize; i++) { blocks[replaceIndex].data[i] = address + i; } blocks[replaceIndex].accessTime++; blocks[replaceIndex].data[offset] = value; blocks[replaceIndex].dirty = true; } } }; int main() { Cache cache(4, 16); cout << "Read data from address 0: " << cache.read(0) << endl; cout << "Write data to address 8" << endl; cache.write(8, 123); cout << "Read data from address 4: " << cache.read(4) << endl; cout << "Write data to address 12" << endl; cache.write(12, 456); cout << "Read data from address 8: " << cache.read(8) << endl; return 0; } ``` 该程序实现了一个大小为16字节的Cache,每个Cache块大小为4字节,使用直接映射地址转换机制和最近最少使用算法进行缓存替换,使用写回策略进行缓存写回。程序输出结果如下: ``` Cache miss! Read data from memory to cache block 0 Read data from address 0: 0 Write data to address 8 Cache miss! Read data from memory to cache block 2 Read data from address 4: 4 Write data to address 12 Cache miss! Read data from memory to cache block 1 Read data from address 8: 123 ``` 希望以上信息能够对你有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值