Question

So for my computer architecture class I have to simulate a cache/memory relationship on C# and I'm just not sure how a cache actually stores data. I get the concept of a cache tag, but I really don't get the offset.

So say I have an RAM that holds 256 32bit integers. I want to have a caching system where I have a cache that holds 8 32bit integers. But then in those 32bit integers in the cache I need to add the tag and valid bit, which makes it roughly sized down to 26 bits or so. So how can I store the 32 bit data in that remaining 26 bits?

Here is the code I have right now, it's still a work in progress

class Memory
{
    public List<UInt32> instructions = new List<UInt32>();

    const int directCacheSize = 8;
    ulong[] directCache = new ulong[directCacheSize];

    private int[] stack = new int[256];

    public int this[int i]
    {
        get
        {
            int directMapIndex = i % directCacheSize;
            if (directCache[directMapIndex] == Convert.ToUInt64(stack[i]))
            {
                return Convert.ToInt32(directCache[directMapIndex] - 18446744069414584320);
            }
            else
            {
                directCache[directMapIndex] = (Convert.ToUInt64(i) << 32) + Convert.ToUInt64(stack[i]);
                return stack[i];
            }
        }
        set
        {
            stack[i] = value;
        }
    }
}
Was it helpful?

Solution

I've been trying to understand this mostly incoherent question and I think I've got it.

I was thinking originally that your fundamental error is that bits used to maintain the cache data structure bits are subtracted from the data size; that doesn't make any sense. They are added to the data size.

But then I realized that no, your fundamental error is that you have confused bits with bytes. You are subtracting six bits from 32 bytes to nonsensically get 26, but you should be adding six bits to 32 x 8 bits.

The error that is just plain confusing is that you also seem to have confused the offset into the data block with the data block itself. The data block stores the data. The offset identifies the location of the relevant data within the data block. The offset is part of an effective address, not a cache line!

You also seem to have forgotten about the dirty bit throughout.

So then a single block in my direct map cache would look like this: [tag 5bit][data 32bit]

No. The number of times that 32 appears in this problem has confused you deeply:

  • eight 32 bit words is 32 bytes
  • five bits can represent 32 possible tags
  • If you have 32 tags and 1024 bytes then each tag identifies 32 bytes

That's a lot of 32s and you've confused them terribly.

Start over.

Suppose you want a single line with 8 32 bit words in it, for a total of 32 bytes. What has to be in the single cache line?

  • the 32 bytes -- NOT BITS
  • a tag identifying where the 32 bytes came from
  • one validity bit
  • one dirty bit

If we assume that the 32 bytes can only be on 32-byte boundaries, and there are 1024 / 32 = 32 such boundaries, then for the tag we need log2(32) = 5 bits, so the total cache line size would be:

  • 32 bytes NOT BITS of data
  • 5 bits of tag
  • one validity bit
  • one dirty bit

Make sense?

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