#include "bool.h" #define BASE_ADDRESS 0x00400000 /* *** Cache information *** The following three variables specify the cache parameters. blocksize is the number of words in a cache block. numsets is the number of sets in the cache that are involved in direct mapping; if the numsets is 1, the cache is fully associative. associativity specifies the associativity of the cache; if this is N, the cache is N-way associative. For a completely direct-mapped cache, associativity is 1. blocksize, numsets and associativity are powers of 2. The cache is implemented as an array of numsets sets. Each set is implemented as an array of associativity entries. Each entry is implemented as a struct that contains a tag and a block, i.e. an array of words. Searches in an associative cache should be implemented as linear searches. and when an entry is to be removed from an associative cache, it should be the least recently accessed. The cache should be write-through; that is, a write to a cached element should be accompanied by a write to the corresponding memory location. */ struct block { int lastUsedTime; /* See the accessCount variable at the top of proj5.c */ bool isValid; /* A simple valid flag for this block */ unsigned int tag; /* The tag bits. Note that the least significant bit of the tag, should be in the least significant bit of this field */ int * words; /* An array/pointer to the actual words which are in this cache block */ }; typedef struct block * Block; struct set { struct block * blocks; /* An array/pointer to the blocks (one per way remember) in this (fully associative) cache set. */ }; typedef struct set * Set; struct cache { struct set * sets; /* An array/pointer to all of the cache sets. */ int associativity; /* if this is N, the cache is N-way associative (a power of 2) */ int nsets; /* number of sets in the cache (a power of 2) */ int blocksize; /* number of words in a cache block (a power of 2) */ bool printingCache, debugging; }; typedef struct cache * Cache; struct memory { int * ram; int maxNumInstrs; int maxNumData; bool printingMemory, debugging; Cache cache; }; typedef struct memory * Memory; int contents(Memory mipsMem, unsigned int address); void store(Memory mipsMem, unsigned int address, int contents); struct memory * newMemory (int maxNumInstrs, int maxNumData, int associativity, int nsets, int blocksize, bool printingMemory, bool printingCache, bool debugging); void printMemory(Memory mipsMem, int changedMem); int toIndex (unsigned int address); Cache newCache (int associativity, int nsets, int blocksize, bool printingCache, bool debugging); bool cacheContains (Cache cache, unsigned int address); int cacheContents (Cache cache, unsigned int address); void updateCacheContents (Cache cache, unsigned int address, int contents); void addEntry (Cache cache, Memory mipsMem, unsigned int address); void printCache(Cache cache);