FotG 0 Newbie Poster

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

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.

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.

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.

If you're wondering the LRU is a doubly linked list that searches for the used value, deletes it if found and moves it to the front

Can someone tell me what Im doing wrong here?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.