CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1
  1. #1
    Join Date
    Apr 2009
    Posts
    2

    hit/miss cache simulator

    I am attempting a cache read simulator that only counts hits and misses. it uses N-way associative cache, and LRU replacement. I have the correct values on what it needs to be, but no matter what I try, I cannot get the correct output.

    I have a block class
    Code:
    class FullyAblock
    	{
    	private:
    		LinkedList lru;
    		class fBlock
    			{
    			public:
    				int data;
    				bool valid;
    				fBlock() : valid(false) {}
    			};
    
    		fBlock area[256][256]; //                      x. [65536][1] or [256][256]
    		int maxBl,maxBy;	    //                  maxBl ^        maxBy   ^
    
    	public:
    		FullyAblock(int maxbl, int maxby) : maxBl(maxbl), maxBy(maxby)
    			{}
    
    		inline void add(int in)
    			{//fill the block from 0 to size-1
    			node *Lru;
    			int temp = in % maxBy;			//get its location
    			in -= temp;
    
    			for (int k = 0; k < maxBl; k++)
    				{//find the right area
    				if (!area[k][0].valid)
    					{
    					for (int i = 0; i < maxBy; i++)
    						{//fill the assiociative values
    						area[k][i].data = in;
    						in++;
    						}
    					area[k][0].valid = true;
    					lru.used(k);
    					return;
    					}
    				}// end k for
    
    			//cache is full add from lru
    			Lru = lru.getLRU();
    			lru.used(Lru->data);
    
    			for (int i = 0; i < maxBy; i++)
    				{//fill the assiociative values
    				area[Lru->data][i].data = in;
    				in++;
    				}
    			}
    
    		inline int getValue(int val)
    			{//find and get a value
    			int temp;
    			for (int k = 0; k < maxBl; k++)
    				{
    				for (int i = 0; i < maxBy; i++)
    					{
    					temp = area[k][i].data;
    					if (temp == val)
    						return temp;
    					}
    				}
    			//return -1 if not found
    			return -1;
    			}
    	};
    and a constructor that takes in the overall size in bytes of the cache, the block size in bytes, a counter, and associativity, which if isn't given is DMC, which is already working.

    the constructor initializes an array of the fully associative blocks, each array element pertaining to a set in the cache.

    Code:
    Cache(int _size, int blSize, HitMiss &hm, int assoc = 0) : size(_size), hCount(hm), blockSZ(blSize), associativity( assoc)
    
    {
    size /= blockSZ;
    sets = size/associativity;
    
    for (int i = 0 ; i < sets; i++)
           {
    	nWCache[i] = new FullyAblock(associativity,blockSZ);
           }
    }
    Then I have code thats argument is a byte address, which counts hits and misses.

    Code:
    void nWayRead(int in)
    	{ 
    	int where_= (in/blockSZ)%sets;
    	int temp;
    
    	temp = nWCache[where_]->getValue(in);
    	if (temp != -1)
    		hCount.hit(true);
    	else
    		{
    		hCount.hit(false);
    		nWCache[where_]->add(in);
    		}
    			
    	}
    I have been struggling with this program for awhile now and cant find my mistakes. My understanding is that the number of sets = (total size / block size) / associativity. And that the number of blocks in each set = associativity. And which set to add to =
    (the address / size of a block) mod number of sets.

    Can someone tell me what im doing wrong here?
    Last edited by FotG; May 8th, 2009 at 07:27 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured