Question

I have a program my professor wrote that simulates the way the memory writes to the L2 cache. It has a couple of places where I'm supposed to fill in the blank. The first thing I'm supposed to do is clear the valid bit of each cache entry. He has given us the following:

//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];

So, as I understand it, clearing the valid bit just means setting it to zero. The valid bit is bit 31 of v_d_tag, so I should be using a bitmask - I want to do something along the lines of, "v_d_tag = v_d_tag & 0x80000000;", I think? But what I don't understand is how I can go through and do that for each cache entry. I see the array of cache entries (l2_cache), but I don't see how the v_d_tag is related to that.

Can anybody explain it to me?

Was it helpful?

Solution

typedef struct is redundant in C++, as are the #define I'm seeing, they could be static const int.

In order to clear them all, you would want to do

for(int i = 0; i < L2_NUM_CACHE_ENTRIES; i++)
    l2_cache[i].v_d_tag &= 0x80000000;

OTHER TIPS

The struct is defined in a C fashion, since in C it is a common idiom to typedef declare a struct so that it can be used as a type without having to write struct L2_CACHE_ENTRY on every reference. This idiom is no longer needed in C++, since the struct tag will work as an individual type.

In short, in C++ you can treat

typedef struct {

uint32_t v_d_tag;

uint32_t cache_line[WORDS_PER_CACHE_LINE];

} L2_CACHE_ENTRY;

exactly the same as

struct L2_CACHE_ENTRY{

uint32_t v_d_tag;

uint32_t cache_line[WORDS_PER_CACHE_LINE];

};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top