Ejemplo n.º 1
0
 public Memory()
 {
     for (int i = 0; i < cacheSize; i++)
     {
         cache[i] = new BlockSet(setAssociative, blockSize);
     }
 }
Ejemplo n.º 2
0
        public int this[int i]
        {
            get
            {
                blockNumber = Convert.ToInt32(i / blockSize);
                cacheIndex = blockNumber % cacheSize;
                randomBlockIndex = rand.Next(setAssociative - 1); //Random Replacement Strategy
                dataIndex = i % blockSize;
                //Cache drawing http://i.imgur.com/UIgzmdn.jpg
                currentTag = blockNumber >> (int)Math.Log(cacheIndex, 2);

                currentBlockSet = cache[cacheIndex];

                bool hit = false;
                int hitIndex = 0;

                //Search for a tag match within the blockset
                for (int x = 0; x < setAssociative; x++)
                {
                    int tempTag = cache[cacheIndex].getTag(x);
                    if (tempTag == currentTag)
                    {
                        hit = true;
                        hitIndex = x;
                        break;
                    }
                    else
                    {
                        hit = false;
                    }
                }

                if (hit == true)
                {
                    //HIT - simply return data
                    hits++;
                    return currentBlockSet[hitIndex][dataIndex];
                }
                else
                {
                    //MISS - write data to random block within blockset
                    int[] newBlockData = currentBlockSet[randomBlockIndex];
                    newBlockData[dataIndex] = stack[i];

                    //If current block to be replaced is dirty, then write it to the stack
                    if (cache[cacheIndex].getDirty(randomBlockIndex) == true)
                    {
                        for (int index = 0; index < blockSize; index++)
                        {
                            stack[index] = cache[cacheIndex][randomBlockIndex][index];
                        }
                    }

                    cache[cacheIndex][randomBlockIndex] = newBlockData; //Random Replacement
                    cache[cacheIndex].setTag(randomBlockIndex, currentTag);
                    cache[cacheIndex].setValidBit(true); //Set valid bit of the BlockSet to be true
                    misses++;
                    return cache[cacheIndex][randomBlockIndex][dataIndex];
                }
            }
            set
            {
                blockNumber = Convert.ToInt32(i / blockSize);
                cacheIndex = blockNumber % cacheSize;
                randomBlockIndex = rand.Next(setAssociative - 1); //Random Replacement Strategy
                dataIndex = i % blockSize;
                //Cache drawing http://i.imgur.com/UIgzmdn.jpg
                currentTag = blockNumber >> (int)Math.Log(cacheIndex, 2);

                currentBlockSet = cache[cacheIndex];

                int[] blockToBeWritten = cache[cacheIndex][randomBlockIndex];
                blockToBeWritten[dataIndex] = value;

                bool hit = false;
                int hitIndex = 0;

                for (int x = 0; x < setAssociative; x++)
                {
                    int tempTag = cache[cacheIndex].getTag(x);
                    if (tempTag == currentTag)
                    {
                        hit = true;
                        hitIndex = x;
                        break;
                    }
                    else
                    {
                        hit = false;
                    }
                }

                if (hit == true)
                {
                    cache[cacheIndex][randomBlockIndex] = blockToBeWritten;
                    cache[cacheIndex].setDirty(randomBlockIndex, true);
                    hits++;
                }
                else
                {
                    misses++;
                    stack[i] = value;
                }
            }
        }
Ejemplo n.º 3
0
        public int this[int i]
        {
            get
            {
                blockNumber = Convert.ToInt32(i / blockSize);
                cacheIndex = blockNumber % cacheSize;
                randomBlockIndex = rand.Next(setAssociative - 1); //Random Replacement Strategy
                dataIndex = i % blockSize;
                //Cache drawing http://i.imgur.com/UIgzmdn.jpg
                currentTag = blockNumber >> (int)Math.Log(cacheIndex, 2);

                currentBlockSet = cache[cacheIndex];

                Console.WriteLine("--Trying to get stack[{0}]--", i);
                Console.WriteLine("blockNumber = {0}, cacheIndex = {1}, blockIndex = {2}", blockNumber, cacheIndex, randomBlockIndex, dataIndex, currentTag);
                Console.WriteLine("dataIndex = {0}, currentTag = {1}, validBit = {2}", dataIndex, currentTag, cache[cacheIndex].getValidBit());

                bool hit = false;
                int hitIndex = 0;

                //Search for a tag match within the blockset
                for (int x = 0; x < setAssociative; x++)
                {
                    int tempTag = cache[cacheIndex].getTag(x);
                    if (tempTag == currentTag)
                    {
                        hit = true;
                        hitIndex = x;
                        break;
                    }
                    else
                    {
                        hit = false;
                    }
                }

                if (hit == true)
                {
                    //HIT
                    Console.WriteLine("HIT! Got data: {0}", currentBlockSet[hitIndex][dataIndex]);
                    hits++;
                    return currentBlockSet[hitIndex][dataIndex];
                }
                else
                {
                    //MISS
                    int[] newBlockData = currentBlockSet[hitIndex];
                    newBlockData[dataIndex] = stack[i];
                    cache[cacheIndex][randomBlockIndex] = newBlockData; //Random Replacement
                    cache[cacheIndex].setTag(randomBlockIndex, currentTag);
                    cache[cacheIndex].setValidBit(true); //Set valid bit of the BlockSet to be true
                    Console.WriteLine("MISS! Got data: {0}", cache[cacheIndex][randomBlockIndex][dataIndex]);
                    misses++;
                    return cache[cacheIndex][randomBlockIndex][dataIndex];
                }
                //If the current tag is the same as the tag in the cache, then it's a hit

            }
            set
            {
                blockNumber = Convert.ToInt32(i / blockSize);
                cacheIndex = blockNumber % cacheSize;
                randomBlockIndex = rand.Next(setAssociative - 1); //Random Replacement Strategy
                dataIndex = i % blockSize;
                //Cache drawing http://i.imgur.com/UIgzmdn.jpg
                currentTag = blockNumber >> (int)Math.Log(cacheIndex, 2);

                currentBlockSet = cache[cacheIndex];

                int[] blockToBeWritten = cache[cacheIndex][randomBlockIndex];
                blockToBeWritten[dataIndex] = value;

                bool hit = false;
                int hitIndex = 0;

                for (int x = 0; x < setAssociative; x++)
                {
                    int tempTag = cache[cacheIndex].getTag(x);
                    if (tempTag == currentTag)
                    {
                        hit = true;
                        hitIndex = x;
                        break;
                    }
                    else
                    {
                        hit = false;
                    }
                }

                if (hit == true)
                {
                    cache[cacheIndex][randomBlockIndex] = blockToBeWritten;
                    cache[cacheIndex].setDirty(randomBlockIndex, true);
                    hits++;
                }
                else
                {
                    misses++;
                    stack[i] = value;
                }
            }
        }