HW6 - C++ Implementation

Supplement to the hw6 main assignment.

We'll bend standard practices in programming a bit so that we need to know as little as possible about Visual Studio. In particular, rather than creating new classes for things that might naturally be classes, we'll insert their concepts into the existing ICache and DCache classes. That lets us put all new code in the files that are already part of the Visual Studio project, so that we don't have to monkey with creating and inserting new project files.

In your C++ implementations of caches, the main data structure will be one or more arrays. Because, ideally, your code would be easily parameterized to model different cache organizations, we'll simply use a 2D array. That naturally models N-way set associative caches, and in the degenerate case (where the number of columns is 1), can model both direct mapped and fully associative as well. For example, for the DCache you might have this (which you'd insert inside the private section of the class declaration in each xCache.h file):

    enum    {NUMLINES=32, NUMSETS=1, BLOCKSIZE=1 };
    Cache_Line    theCache[NUMLINES][NUMSETS];
Here Cache_Line is a programmer defined type. For the reasons stated above, it turns out that in this assignment the easiest thing is to make it a struct, which you can think of as a methodless class in which all instance variables are public:
    typedef struct CacheLine_Type {
        bool        valid;
        bool        dirty;
        int         tag;
        int         data[BLOCKSIZE];
    } Cache_Line;
(Note: The ICache wouldn't need the dirty bit.) That is the definition of the type. It also goes inside the class declaration in each xCache.h file. It must come after the enum above and before the declaration of theCache.

Here's some code that you might put in the xCache reinitialize() method to initialize theCache. It shows you everything else there is to know about using a struct:

   for ( int line=0; line<NUMLINES; line++ ) {
    for ( int set=0; set<NUMSETS; set++ ) {
      theCache[line][set].valid = false;
      theCache[line][set].dirty = false;  
      theCache[line][set].tag = 0;        
      for ( int block=0; block<BLOCKSIZE; block++ ) {
           theCache[line][set].data[block] = 0;
      }
    }
   }
Note that I really should have just initialized the valid field, as the other initializations are irrelevant and misleading. They're in this example, though, just for completeness in showing you how to reference all the fields.