题
我有一个课程,我的教授写了一个模拟记忆写入L2缓存的方式。它有几个地方,我应该填满空白。我应该做的第一件事是清除每个缓存条目的有效位。他给了我们以下内容:
//number of cache entries (2^11)
#define L2_NUM_CACHE_ENTRIES (1<<11)
/***************************************************
This struct defines the structure of a single cache
entry in the L2 cache. It has the following fields:
v_d_tag: 32-bit unsigned word containing the
valid (v) bit at bit 31 (leftmost bit),
the dirty bit (d) at bit 30, and the tag
in bits 0 through 15 (the 16 rightmost bits)
cache_line: an array of 8 words, constituting a single
cache line.
****************************************************/
Typedef struct {
uint32_t v_d_tag;
uint32_t cache_line[WORDS_PER_CACHE_LINE];
} L2_CACHE_ENTRY;
//The L2 is just an array cache entries
L2_CACHE_ENTRY l2_cache[L2_NUM_CACHE_ENTRIES];
因此,据我了解,清除有效的位只是意味着将其设置为零。有效的位是v_d_tag的31位,所以我应该使用一个位掩码 - 我想按照“ v_d_tag = v_d_d_tag&0x80000000;”的行为做点什么?但是我不明白的是如何对每个缓存条目进行操作。我看到了缓存条目的数组(L2_CACHE),但是我看不出v_d_tag与此相关。
有人可以向我解释吗?
解决方案
Typedef结构在C ++中是多余的, #define
我看到,它们可能是静态的const int。
为了清除所有这些,您想做
for(int i = 0; i < L2_NUM_CACHE_ENTRIES; i++)
l2_cache[i].v_d_tag &= 0x80000000;
其他提示
结构以C方式定义,因为在C中,它是typedef声明结构的常见习语 struct L2_CACHE_ENTRY
在每个引用上。在C ++中不再需要此习语,因为 struct
标签将作为单个类型工作。
简而言之,在C ++中,您可以治疗
typedef struct {
uint32_t v_d_tag;
uint32_t cache_line[WORDS_PER_CACHE_LINE];
} L2_CACHE_ENTRY;
与
struct L2_CACHE_ENTRY{
uint32_t v_d_tag;
uint32_t cache_line[WORDS_PER_CACHE_LINE];
};
不隶属于 StackOverflow