第 11 章:Cache 一致性
学习目标
- 理解写策略
- 实现写回和写直达
- 处理脏数据
11.1 思考:写入怎么办?
a[0] = 10; // 写入 Cache
问题:内存中的数据要不要更新?
11.2 写直达(Write Through)
if (write_hit) begin
cache_data[index] <= write_data;
mem_write <= 1'b1; // 同时写内存
end
简单,但慢!
11.3 写回(Write Back)
reg dirty [255:0];
if (write_hit) begin
cache_data[index] <= write_data;
dirty[index] <= 1'b1; // 标记为脏
end
// 替换时才写回
if (replace && dirty[index])
mem_write <= 1'b1;
思考:哪种策略更好?