Home / utk / cs130 / fa17 / lab09 / cache.ino
Directory Listing
cache.ino
la9ut_final.bmp
lab9.bmp
lab9_final.bmp
lab9ut.bmp
writeup.pdf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/////////////////////////////////////////////////////////////////////
//
// !!! DO NOT MODIFY ANY STRUCTURE / CLASS / ENUMERATION !!!
//
/////////////////////////////////////////////////////////////////////

struct Entry
{
  int epdata;
  int address;
  int value;
};

enum EvictPolicy
{
  EP_FIFO,
  EP_LRU,
  EP_LFU
};

class Cache
{
  const int mSets;
  const int mMask;
  const int mWays;
  const EvictPolicy mPolicy;
  Entry * const mEntries;
  
public:
  Cache(int _bits, int _ways, EvictPolicy _p);
  ~Cache();
  
  int FindSet(int _address) const;
  int GetNumSets() const;
  int GetNumWays() const;
  EvictPolicy GetPolicy() const;

  Entry *GetEntry(int _set, int _way);
  
  void Set(int _address, int _value);
  int Get(int _address);
} *cache;

/////////////////////////////////////////////////////////////////
//
// Add your Cache class' function definitions below
//
/////////////////////////////////////////////////////////////////














/////////////////////////////////////////////////////////////////////
//
// !!! DO NOT MODIFY setup() OR loop() BELOW !!!
//
/////////////////////////////////////////////////////////////////////

char heartbeat = 10;

void setup() {
  for (int i = 10;i <= 13;i++) {
    pinMode(i, OUTPUT);
    digitalWrite(i, HIGH);
  }
  Serial.begin(9600);
  cache = new Cache(0, 10, EP_FIFO);
  while (!Serial) delay(100);
  delay(1000);
  Serial.print("Created a default, fully-associative cache w/ 10 ways and FIFO eviction\n");
  Serial.print("Enter commands, Type 'h' for help and list of commands\n");
}

void loop() {
  int a, b, c;
  char cmd;
  char buf[255];
  EvictPolicy ep;
  if (Serial.available() > 0) {
    String input = Serial.readString();
    sscanf(input.c_str(), "%c", &cmd);
    switch (cmd) {
      case 'c': // r <bits> <ways> <policy>
        sscanf(input.c_str(), "%c %d %d %d", &cmd, &a, &b, &c);
        ep = (c == 1 ? EP_LRU : c == 2 ? EP_LFU : EP_FIFO);
        if (a < 0) {
          Serial.print("Invalid bits\n");
        }
        else if (b < 1) {
          Serial.print("Invalid ways\n");
        }
        else {
          delete cache;
          cache = new Cache(a, b, ep);
          sprintf(buf, "Created new cache %d sets, %d ways, %s eviction policy\n", 
            cache->GetNumSets(),
            cache->GetNumWays(),
            (ep == EP_FIFO ? "FIFO" : ep == EP_LRU ? "LRU" : "LFU"));
          Serial.print(buf);
        }
        break;
      case 's': // s <addr> <value>
        sscanf(input.c_str(), "%c %x %d", &cmd, &a, &b);
        cache->Set(a, b);
        sprintf(buf, "Set 0x%08x = %d\n", a, b);
        Serial.print(buf);
        break;
      case 'g': // g <addr>
        sscanf(input.c_str(), "%c %x", &cmd, &a);
        sprintf(buf, "Get 0x%08x = %d\n", a, cache->Get(a));
        Serial.print(buf);
        break;
      case 'e': // e <set> <way>
        sscanf(input.c_str(), "%c %d %d", &cmd, &a, &b);
        if (a < 0 || a >= cache->GetNumSets()) {
          Serial.print("Invalid set specified\n");
        }
        else if (b < 0 || b >= cache->GetNumWays()) {
          Serial.print("Invalid way specified\n");
        }
        else {
          const Entry *e = cache->GetEntry(a, b);
          sprintf(buf, "Entry: Set %d, Way %d\n  Tag: %d\n  Address: 0x%08x\n  Value: %d\n", a, b, e->epdata, e->address, e->value);
          Serial.print(buf);
        }
        break;
      case 'n':
        sprintf(buf, "This cache has %d sets\n", cache->GetNumSets());
        Serial.print(buf);
        break;
      case 'w':
        sprintf(buf, "This cache has %d ways\n", cache->GetNumWays());
        Serial.print(buf);
        break;
      case 'y': // y <set>
        sscanf(input.c_str(), "%c %d", &cmd, &a);
        if (a < 0 || a >= cache->GetNumSets()) {
          Serial.print("Invalid set\n");
        }
        else {
          sprintf(buf, "Set %d\n", a);
          Serial.print(buf);
          for (int w = 0;w < cache->GetNumWays();w++) {
            const Entry *e = cache->GetEntry(a, w);
            sprintf(buf, "  [Way %3d] Tag: %6d, Address: 0x%08x, Value: %d\n", w, e->epdata, e->address, e->value);
            Serial.print(buf);
          }
        }
        break;
      case 'f': // f <addr>
        sscanf(input.c_str(), "%c %x", &cmd, &a);
        sprintf(buf, "Address 0x%08x goes to set %d\n", a, cache->FindSet(a));
        Serial.print(buf);
        break;
      case 'p': // p
        do {
          EvictPolicy p = cache->GetPolicy();
          sprintf(buf, "This cache has %s eviction policy\n", (p == EP_FIFO ? "FIFO" : p == EP_LRU ? "LRU" : "LFU"));
          Serial.print(buf);
        } while (0);
        break;
      default:
        Serial.print("Usage: <command> [arguments]\n");
        Serial.print("Where <command> is:\n");
        Serial.print("  c <bits> <ways> <policy> - Create new cache\n");
        Serial.print("     <policy> is 0: FIFO, 1: LRU, 2: LFU\n");
        Serial.print("  s <addr> <value>         - Set <addr> to <value>\n");
        Serial.print("  g <addr>                 - Get the value at <addr>\n");
        Serial.print("  f <addr>                 - Get which set <addr> would go into\n");
        Serial.print("  y <set>                  - Get all of the way entries of a given set\n");
        Serial.print("  e <set> <way>            - Get the value at <set>[<way>]\n");
        Serial.print("  n                        - Get the number of sets for this cache\n");
        Serial.print("  w                        - Get the number of ways for this cache\n");
        Serial.print("  p                        - Get the evict policy for this cache\n");
        break;
        
    }
  }
  else {
    if (++heartbeat > 13)
      heartbeat = 10;
    for (int i = 10;i <= 13;i++) {
      digitalWrite(i, (heartbeat == i ? LOW : HIGH));
    }
  }
  delay(100);
}