[307] | 1 | #ifndef SOCLIB_CABA_MEM_CACHE_DIRECTORY_H |
---|
| 2 | #define SOCLIB_CABA_MEM_CACHE_DIRECTORY_H |
---|
| 3 | |
---|
| 4 | #include <inttypes.h> |
---|
| 5 | #include <systemc> |
---|
| 6 | #include <cassert> |
---|
[753] | 7 | #include <cstring> |
---|
[307] | 8 | #include "arithmetics.h" |
---|
| 9 | |
---|
| 10 | //#define RANDOM_EVICTION |
---|
| 11 | |
---|
| 12 | namespace soclib { namespace caba { |
---|
| 13 | |
---|
[836] | 14 | using namespace sc_core; |
---|
[307] | 15 | |
---|
[836] | 16 | //////////////////////////////////////////////////////////////////////// |
---|
| 17 | // A LRU entry |
---|
| 18 | //////////////////////////////////////////////////////////////////////// |
---|
| 19 | class LruEntry { |
---|
[307] | 20 | |
---|
| 21 | public: |
---|
| 22 | |
---|
[836] | 23 | bool recent; |
---|
[307] | 24 | |
---|
[836] | 25 | void init() |
---|
| 26 | { |
---|
| 27 | recent = false; |
---|
| 28 | } |
---|
[307] | 29 | |
---|
[836] | 30 | }; // end class LruEntry |
---|
[307] | 31 | |
---|
[836] | 32 | //////////////////////////////////////////////////////////////////////// |
---|
| 33 | // An Owner |
---|
| 34 | //////////////////////////////////////////////////////////////////////// |
---|
| 35 | class Owner{ |
---|
| 36 | |
---|
[307] | 37 | public: |
---|
[836] | 38 | // Fields |
---|
| 39 | bool inst; // Is the owner an ICache ? |
---|
| 40 | size_t srcid; // The SRCID of the owner |
---|
[307] | 41 | |
---|
[836] | 42 | //////////////////////// |
---|
| 43 | // Constructors |
---|
| 44 | //////////////////////// |
---|
| 45 | Owner(bool i_inst, |
---|
| 46 | size_t i_srcid) |
---|
| 47 | { |
---|
| 48 | inst = i_inst; |
---|
| 49 | srcid = i_srcid; |
---|
| 50 | } |
---|
[307] | 51 | |
---|
[836] | 52 | Owner(const Owner &a) |
---|
| 53 | { |
---|
| 54 | inst = a.inst; |
---|
| 55 | srcid = a.srcid; |
---|
| 56 | } |
---|
[307] | 57 | |
---|
[836] | 58 | Owner() |
---|
| 59 | { |
---|
| 60 | inst = false; |
---|
| 61 | srcid = 0; |
---|
| 62 | } |
---|
| 63 | // end constructors |
---|
[307] | 64 | |
---|
[836] | 65 | }; // end class Owner |
---|
[307] | 66 | |
---|
| 67 | |
---|
[836] | 68 | //////////////////////////////////////////////////////////////////////// |
---|
| 69 | // A directory entry |
---|
| 70 | //////////////////////////////////////////////////////////////////////// |
---|
| 71 | class DirectoryEntry { |
---|
[307] | 72 | |
---|
| 73 | typedef uint32_t tag_t; |
---|
| 74 | |
---|
| 75 | public: |
---|
| 76 | |
---|
[836] | 77 | bool valid; // entry valid |
---|
| 78 | bool is_cnt; // directory entry is in counter mode |
---|
| 79 | bool dirty; // entry dirty |
---|
| 80 | bool lock; // entry locked |
---|
| 81 | tag_t tag; // tag of the entry |
---|
| 82 | size_t count; // number of copies |
---|
| 83 | Owner owner; // an owner of the line |
---|
| 84 | size_t ptr; // pointer to the next owner |
---|
[307] | 85 | |
---|
| 86 | DirectoryEntry() |
---|
| 87 | { |
---|
[836] | 88 | valid = false; |
---|
| 89 | is_cnt = false; |
---|
| 90 | dirty = false; |
---|
| 91 | lock = false; |
---|
| 92 | tag = 0; |
---|
| 93 | count = 0; |
---|
| 94 | owner.inst = 0; |
---|
| 95 | owner.srcid = 0; |
---|
| 96 | ptr = 0; |
---|
[307] | 97 | } |
---|
| 98 | |
---|
| 99 | DirectoryEntry(const DirectoryEntry &source) |
---|
| 100 | { |
---|
[836] | 101 | valid = source.valid; |
---|
| 102 | is_cnt = source.is_cnt; |
---|
| 103 | dirty = source.dirty; |
---|
| 104 | lock = source.lock; |
---|
| 105 | tag = source.tag; |
---|
| 106 | count = source.count; |
---|
| 107 | owner = source.owner; |
---|
| 108 | ptr = source.ptr; |
---|
[307] | 109 | } |
---|
| 110 | |
---|
| 111 | ///////////////////////////////////////////////////////////////////// |
---|
| 112 | // The init() function initializes the entry |
---|
| 113 | ///////////////////////////////////////////////////////////////////// |
---|
| 114 | void init() |
---|
| 115 | { |
---|
[836] | 116 | valid = false; |
---|
| 117 | is_cnt = false; |
---|
| 118 | dirty = false; |
---|
| 119 | lock = false; |
---|
| 120 | count = 0; |
---|
[307] | 121 | } |
---|
| 122 | |
---|
| 123 | ///////////////////////////////////////////////////////////////////// |
---|
| 124 | // The copy() function copies an existing source entry to a target |
---|
| 125 | ///////////////////////////////////////////////////////////////////// |
---|
| 126 | void copy(const DirectoryEntry &source) |
---|
| 127 | { |
---|
[836] | 128 | valid = source.valid; |
---|
| 129 | is_cnt = source.is_cnt; |
---|
| 130 | dirty = source.dirty; |
---|
| 131 | lock = source.lock; |
---|
| 132 | tag = source.tag; |
---|
| 133 | count = source.count; |
---|
| 134 | owner = source.owner; |
---|
| 135 | ptr = source.ptr; |
---|
[307] | 136 | } |
---|
| 137 | |
---|
| 138 | //////////////////////////////////////////////////////////////////// |
---|
| 139 | // The print() function prints the entry |
---|
| 140 | //////////////////////////////////////////////////////////////////// |
---|
| 141 | void print() |
---|
| 142 | { |
---|
[836] | 143 | std::cout << "Valid = " << valid |
---|
| 144 | << " ; IS COUNT = " << is_cnt |
---|
| 145 | << " ; Dirty = " << dirty |
---|
| 146 | << " ; Lock = " << lock |
---|
| 147 | << " ; Tag = " << std::hex << tag << std::dec |
---|
| 148 | << " ; Count = " << count |
---|
| 149 | << " ; Owner = " << owner.srcid |
---|
| 150 | << " " << owner.inst |
---|
| 151 | << " ; Pointer = " << ptr << std::endl; |
---|
[307] | 152 | } |
---|
| 153 | |
---|
[836] | 154 | }; // end class DirectoryEntry |
---|
[307] | 155 | |
---|
[836] | 156 | //////////////////////////////////////////////////////////////////////// |
---|
| 157 | // The directory |
---|
| 158 | //////////////////////////////////////////////////////////////////////// |
---|
| 159 | class CacheDirectory { |
---|
[307] | 160 | |
---|
| 161 | typedef sc_dt::sc_uint<40> addr_t; |
---|
| 162 | typedef uint32_t data_t; |
---|
| 163 | typedef uint32_t tag_t; |
---|
| 164 | |
---|
| 165 | private: |
---|
| 166 | |
---|
| 167 | // Directory constants |
---|
[836] | 168 | size_t m_ways; |
---|
| 169 | size_t m_sets; |
---|
| 170 | size_t m_words; |
---|
| 171 | size_t m_width; |
---|
| 172 | uint32_t lfsr; |
---|
[307] | 173 | |
---|
| 174 | // the directory & lru tables |
---|
[836] | 175 | DirectoryEntry ** m_dir_tab; |
---|
| 176 | LruEntry ** m_lru_tab; |
---|
[307] | 177 | |
---|
| 178 | public: |
---|
| 179 | |
---|
| 180 | //////////////////////// |
---|
| 181 | // Constructor |
---|
| 182 | //////////////////////// |
---|
[836] | 183 | CacheDirectory( size_t ways, size_t sets, size_t words, size_t address_width) |
---|
[307] | 184 | { |
---|
[836] | 185 | m_ways = ways; |
---|
| 186 | m_sets = sets; |
---|
| 187 | m_words = words; |
---|
| 188 | m_width = address_width; |
---|
| 189 | lfsr = -1; |
---|
[307] | 190 | |
---|
[836] | 191 | m_dir_tab = new DirectoryEntry*[sets]; |
---|
| 192 | for (size_t i = 0; i < sets; i++ ) { |
---|
| 193 | m_dir_tab[i] = new DirectoryEntry[ways]; |
---|
| 194 | for (size_t j = 0; j < ways; j++) m_dir_tab[i][j].init(); |
---|
| 195 | } |
---|
| 196 | m_lru_tab = new LruEntry*[sets]; |
---|
| 197 | for (size_t i = 0; i < sets; i++) { |
---|
| 198 | m_lru_tab[i] = new LruEntry[ways]; |
---|
| 199 | for (size_t j = 0; j < ways; j++) m_lru_tab[i][j].init(); |
---|
| 200 | } |
---|
[307] | 201 | } // end constructor |
---|
| 202 | |
---|
| 203 | ///////////////// |
---|
| 204 | // Destructor |
---|
| 205 | ///////////////// |
---|
| 206 | ~CacheDirectory() |
---|
| 207 | { |
---|
[836] | 208 | for(size_t i = 0; i < m_sets; i++){ |
---|
| 209 | delete [] m_dir_tab[i]; |
---|
| 210 | delete [] m_lru_tab[i]; |
---|
| 211 | } |
---|
| 212 | delete [] m_dir_tab; |
---|
| 213 | delete [] m_lru_tab; |
---|
[307] | 214 | } // end destructor |
---|
| 215 | |
---|
| 216 | ///////////////////////////////////////////////////////////////////// |
---|
| 217 | // The read() function reads a directory entry. In case of hit, the |
---|
| 218 | // LRU is updated. |
---|
| 219 | // Arguments : |
---|
| 220 | // - address : the address of the entry |
---|
| 221 | // - way : (return argument) the way of the entry in case of hit |
---|
| 222 | // The function returns a copy of a (valid or invalid) entry |
---|
| 223 | ///////////////////////////////////////////////////////////////////// |
---|
[449] | 224 | DirectoryEntry read(const addr_t &address, size_t &way) |
---|
[307] | 225 | { |
---|
| 226 | |
---|
| 227 | #define L2 soclib::common::uint32_log2 |
---|
[499] | 228 | const size_t set = (size_t)(address >> (L2(m_words) + 2)) & (m_sets - 1); |
---|
| 229 | const tag_t tag = (tag_t)(address >> (L2(m_sets) + L2(m_words) + 2)); |
---|
[307] | 230 | #undef L2 |
---|
| 231 | |
---|
[836] | 232 | bool hit = false; |
---|
| 233 | for (size_t i = 0; i < m_ways; i++ ) |
---|
[499] | 234 | { |
---|
[836] | 235 | bool equal = (m_dir_tab[set][i].tag == tag); |
---|
[499] | 236 | bool valid = m_dir_tab[set][i].valid; |
---|
| 237 | hit = equal && valid; |
---|
[836] | 238 | if (hit) |
---|
| 239 | { |
---|
[499] | 240 | way = i; |
---|
| 241 | break; |
---|
| 242 | } |
---|
| 243 | } |
---|
[836] | 244 | if (hit) |
---|
[499] | 245 | { |
---|
| 246 | m_lru_tab[set][way].recent = true; |
---|
| 247 | return DirectoryEntry(m_dir_tab[set][way]); |
---|
[307] | 248 | } |
---|
[499] | 249 | else |
---|
| 250 | { |
---|
| 251 | return DirectoryEntry(); |
---|
| 252 | } |
---|
[307] | 253 | } // end read() |
---|
| 254 | |
---|
| 255 | ///////////////////////////////////////////////////////////////////// |
---|
[434] | 256 | // The inval function invalidate an entry defined by the set and |
---|
| 257 | // way arguments. |
---|
| 258 | ///////////////////////////////////////////////////////////////////// |
---|
[836] | 259 | void inval(const size_t &way, const size_t &set) |
---|
[434] | 260 | { |
---|
| 261 | m_dir_tab[set][way].init(); |
---|
| 262 | } |
---|
| 263 | |
---|
| 264 | ///////////////////////////////////////////////////////////////////// |
---|
[307] | 265 | // The read_neutral() function reads a directory entry, without |
---|
| 266 | // changing the LRU |
---|
| 267 | // Arguments : |
---|
| 268 | // - address : the address of the entry |
---|
| 269 | // The function returns a copy of a (valid or invalid) entry |
---|
| 270 | ///////////////////////////////////////////////////////////////////// |
---|
[836] | 271 | DirectoryEntry read_neutral(const addr_t &address, |
---|
| 272 | size_t * ret_way, |
---|
| 273 | size_t * ret_set ) |
---|
[307] | 274 | { |
---|
| 275 | |
---|
| 276 | #define L2 soclib::common::uint32_log2 |
---|
[836] | 277 | size_t set = (size_t) (address >> (L2(m_words) + 2)) & (m_sets - 1); |
---|
| 278 | tag_t tag = (tag_t) (address >> (L2(m_sets) + L2(m_words) + 2)); |
---|
[307] | 279 | #undef L2 |
---|
| 280 | |
---|
[836] | 281 | for (size_t way = 0; way < m_ways; way++) |
---|
[449] | 282 | { |
---|
[836] | 283 | bool equal = (m_dir_tab[set][way].tag == tag); |
---|
[449] | 284 | bool valid = m_dir_tab[set][way].valid; |
---|
[836] | 285 | if (equal and valid) |
---|
[449] | 286 | { |
---|
| 287 | *ret_set = set; |
---|
| 288 | *ret_way = way; |
---|
| 289 | return DirectoryEntry(m_dir_tab[set][way]); |
---|
| 290 | } |
---|
[307] | 291 | } |
---|
[449] | 292 | return DirectoryEntry(); |
---|
[307] | 293 | } // end read_neutral() |
---|
| 294 | |
---|
| 295 | ///////////////////////////////////////////////////////////////////// |
---|
| 296 | // The write function writes a new entry, |
---|
| 297 | // and updates the LRU bits if necessary. |
---|
| 298 | // Arguments : |
---|
| 299 | // - set : the set of the entry |
---|
| 300 | // - way : the way of the entry |
---|
| 301 | // - entry : the entry value |
---|
| 302 | ///////////////////////////////////////////////////////////////////// |
---|
[836] | 303 | void write(const size_t &set, |
---|
| 304 | const size_t &way, |
---|
| 305 | const DirectoryEntry &entry) |
---|
[307] | 306 | { |
---|
[836] | 307 | assert((set < m_sets) && "Cache Directory write : The set index is invalid"); |
---|
| 308 | assert((way < m_ways) && "Cache Directory write : The way index is invalid"); |
---|
[307] | 309 | |
---|
[836] | 310 | // update Directory |
---|
| 311 | m_dir_tab[set][way].copy(entry); |
---|
[307] | 312 | |
---|
[836] | 313 | // update LRU bits |
---|
| 314 | bool all_recent = true; |
---|
| 315 | for (size_t i = 0; i < m_ways; i++) |
---|
| 316 | { |
---|
| 317 | if (i != way) all_recent = m_lru_tab[set][i].recent && all_recent; |
---|
| 318 | } |
---|
| 319 | if (all_recent) |
---|
| 320 | { |
---|
| 321 | for (size_t i = 0; i < m_ways; i++) m_lru_tab[set][i].recent = false; |
---|
| 322 | } |
---|
| 323 | else |
---|
| 324 | { |
---|
| 325 | m_lru_tab[set][way].recent = true; |
---|
| 326 | } |
---|
[307] | 327 | } // end write() |
---|
| 328 | |
---|
| 329 | ///////////////////////////////////////////////////////////////////// |
---|
| 330 | // The print() function prints a selected directory entry |
---|
| 331 | // Arguments : |
---|
| 332 | // - set : the set of the entry to print |
---|
| 333 | // - way : the way of the entry to print |
---|
| 334 | ///////////////////////////////////////////////////////////////////// |
---|
| 335 | void print(const size_t &set, const size_t &way) |
---|
| 336 | { |
---|
[836] | 337 | std::cout << std::dec << " set : " << set << " ; way : " << way << " ; " ; |
---|
| 338 | m_dir_tab[set][way].print(); |
---|
[307] | 339 | } // end print() |
---|
| 340 | |
---|
| 341 | ///////////////////////////////////////////////////////////////////// |
---|
| 342 | // The select() function selects a directory entry to evince. |
---|
| 343 | // Arguments : |
---|
| 344 | // - set : (input argument) the set to modify |
---|
| 345 | // - way : (return argument) the way to evince |
---|
| 346 | ///////////////////////////////////////////////////////////////////// |
---|
| 347 | DirectoryEntry select(const size_t &set, size_t &way) |
---|
| 348 | { |
---|
[836] | 349 | assert((set < m_sets) |
---|
| 350 | && "Cache Directory : (select) The set index is invalid"); |
---|
[307] | 351 | |
---|
[489] | 352 | // looking for an empty slot |
---|
[836] | 353 | for (size_t i = 0; i < m_ways; i++) |
---|
[489] | 354 | { |
---|
[836] | 355 | if (not m_dir_tab[set][i].valid) |
---|
[489] | 356 | { |
---|
[836] | 357 | way = i; |
---|
[489] | 358 | return DirectoryEntry(m_dir_tab[set][way]); |
---|
| 359 | } |
---|
[307] | 360 | } |
---|
| 361 | |
---|
| 362 | #ifdef RANDOM_EVICTION |
---|
[489] | 363 | lfsr = (lfsr >> 1) ^ ((-(lfsr & 1)) & 0xd0000001); |
---|
| 364 | way = lfsr % m_ways; |
---|
| 365 | return DirectoryEntry(m_dir_tab[set][way]); |
---|
[307] | 366 | #endif |
---|
| 367 | |
---|
[489] | 368 | // looking for a not locked and not recently used entry |
---|
[836] | 369 | for (size_t i = 0; i < m_ways; i++) |
---|
[489] | 370 | { |
---|
[836] | 371 | if ((not m_lru_tab[set][i].recent) && (not m_dir_tab[set][i].lock)) |
---|
[489] | 372 | { |
---|
[836] | 373 | way = i; |
---|
[489] | 374 | return DirectoryEntry(m_dir_tab[set][way]); |
---|
| 375 | } |
---|
[307] | 376 | } |
---|
[489] | 377 | |
---|
| 378 | // looking for a locked not recently used entry |
---|
[836] | 379 | for (size_t i = 0; i < m_ways; i++) |
---|
[489] | 380 | { |
---|
[836] | 381 | if ((not m_lru_tab[set][i].recent) && (m_dir_tab[set][i].lock)) |
---|
[489] | 382 | { |
---|
[836] | 383 | way = i; |
---|
[489] | 384 | return DirectoryEntry(m_dir_tab[set][way]); |
---|
| 385 | } |
---|
[307] | 386 | } |
---|
[489] | 387 | |
---|
| 388 | // looking for a recently used entry not locked |
---|
[836] | 389 | for (size_t i = 0; i < m_ways; i++) |
---|
[489] | 390 | { |
---|
[836] | 391 | if ((m_lru_tab[set][i].recent) && (not m_dir_tab[set][i].lock)) |
---|
[489] | 392 | { |
---|
[836] | 393 | way = i; |
---|
[489] | 394 | return DirectoryEntry(m_dir_tab[set][way]); |
---|
| 395 | } |
---|
[307] | 396 | } |
---|
[489] | 397 | |
---|
| 398 | // select way 0 (even if entry is locked and recently used) |
---|
| 399 | way = 0; |
---|
| 400 | return DirectoryEntry(m_dir_tab[set][0]); |
---|
[307] | 401 | } // end select() |
---|
| 402 | |
---|
| 403 | ///////////////////////////////////////////////////////////////////// |
---|
[836] | 404 | // Global initialisation function |
---|
[307] | 405 | ///////////////////////////////////////////////////////////////////// |
---|
| 406 | void init() |
---|
| 407 | { |
---|
[836] | 408 | for (size_t set = 0; set < m_sets; set++) |
---|
[449] | 409 | { |
---|
[836] | 410 | for (size_t way = 0; way < m_ways; way++) |
---|
| 411 | { |
---|
| 412 | m_dir_tab[set][way].init(); |
---|
| 413 | m_lru_tab[set][way].init(); |
---|
| 414 | } |
---|
[307] | 415 | } |
---|
| 416 | } // end init() |
---|
| 417 | |
---|
[836] | 418 | }; // end class CacheDirectory |
---|
[307] | 419 | |
---|
[836] | 420 | /////////////////////////////////////////////////////////////////////// |
---|
| 421 | // A Heap Entry |
---|
| 422 | /////////////////////////////////////////////////////////////////////// |
---|
| 423 | class HeapEntry{ |
---|
[307] | 424 | |
---|
| 425 | public: |
---|
[836] | 426 | // Fields of the entry |
---|
| 427 | Owner owner; |
---|
| 428 | size_t next; |
---|
[307] | 429 | |
---|
[836] | 430 | //////////////////////// |
---|
| 431 | // Constructor |
---|
| 432 | //////////////////////// |
---|
| 433 | HeapEntry() |
---|
| 434 | :owner(false, 0) |
---|
| 435 | { |
---|
| 436 | next = 0; |
---|
| 437 | } // end constructor |
---|
[307] | 438 | |
---|
[836] | 439 | //////////////////////// |
---|
| 440 | // Constructor |
---|
| 441 | //////////////////////// |
---|
| 442 | HeapEntry(const HeapEntry &entry) |
---|
| 443 | { |
---|
| 444 | owner.inst = entry.owner.inst; |
---|
| 445 | owner.srcid = entry.owner.srcid; |
---|
| 446 | next = entry.next; |
---|
| 447 | } // end constructor |
---|
[307] | 448 | |
---|
[836] | 449 | ///////////////////////////////////////////////////////////////////// |
---|
| 450 | // The copy() function copies an existing source entry to a target |
---|
| 451 | ///////////////////////////////////////////////////////////////////// |
---|
| 452 | void copy(const HeapEntry &entry) |
---|
| 453 | { |
---|
| 454 | owner.inst = entry.owner.inst; |
---|
| 455 | owner.srcid = entry.owner.srcid; |
---|
| 456 | next = entry.next; |
---|
| 457 | } // end copy() |
---|
[307] | 458 | |
---|
[836] | 459 | //////////////////////////////////////////////////////////////////// |
---|
| 460 | // The print() function prints the entry |
---|
| 461 | //////////////////////////////////////////////////////////////////// |
---|
| 462 | void print() { |
---|
| 463 | std::cout |
---|
| 464 | << " -- owner.inst : " << std::dec << owner.inst << std::endl |
---|
| 465 | << " -- owner.srcid : " << std::dec << owner.srcid << std::endl |
---|
| 466 | << " -- next : " << std::dec << next << std::endl; |
---|
[307] | 467 | |
---|
[836] | 468 | } // end print() |
---|
[307] | 469 | |
---|
[836] | 470 | }; // end class HeapEntry |
---|
[307] | 471 | |
---|
[836] | 472 | //////////////////////////////////////////////////////////////////////// |
---|
| 473 | // The Heap |
---|
| 474 | //////////////////////////////////////////////////////////////////////// |
---|
| 475 | class HeapDirectory{ |
---|
| 476 | |
---|
[307] | 477 | private: |
---|
[836] | 478 | // Registers and the heap |
---|
| 479 | size_t ptr_free; |
---|
| 480 | bool full; |
---|
| 481 | HeapEntry * m_heap_tab; |
---|
[307] | 482 | |
---|
[836] | 483 | // Constants for debugging purpose |
---|
| 484 | size_t tab_size; |
---|
[307] | 485 | |
---|
| 486 | public: |
---|
[836] | 487 | //////////////////////// |
---|
| 488 | // Constructor |
---|
| 489 | //////////////////////// |
---|
| 490 | HeapDirectory(uint32_t size) { |
---|
| 491 | assert(size > 0 && "Memory Cache, HeapDirectory constructor : invalid size"); |
---|
| 492 | ptr_free = 0; |
---|
| 493 | full = false; |
---|
| 494 | m_heap_tab = new HeapEntry[size]; |
---|
| 495 | tab_size = size; |
---|
| 496 | } // end constructor |
---|
[307] | 497 | |
---|
[836] | 498 | ///////////////// |
---|
| 499 | // Destructor |
---|
| 500 | ///////////////// |
---|
| 501 | ~HeapDirectory() { |
---|
| 502 | delete [] m_heap_tab; |
---|
| 503 | } // end destructor |
---|
[307] | 504 | |
---|
[836] | 505 | ///////////////////////////////////////////////////////////////////// |
---|
| 506 | // Global initialisation function |
---|
| 507 | ///////////////////////////////////////////////////////////////////// |
---|
| 508 | void init() { |
---|
| 509 | ptr_free = 0; |
---|
| 510 | full = false; |
---|
| 511 | for (size_t i = 0; i< tab_size - 1; i++){ |
---|
| 512 | m_heap_tab[i].next = i + 1; |
---|
| 513 | } |
---|
| 514 | m_heap_tab[tab_size - 1].next = tab_size - 1; |
---|
| 515 | return; |
---|
[307] | 516 | } |
---|
| 517 | |
---|
[836] | 518 | ///////////////////////////////////////////////////////////////////// |
---|
| 519 | // The print() function prints a selected directory entry |
---|
| 520 | // Arguments : |
---|
| 521 | // - ptr : the pointer to the entry to print |
---|
| 522 | ///////////////////////////////////////////////////////////////////// |
---|
| 523 | void print(const size_t &ptr) { |
---|
| 524 | std::cout << "Heap, printing the entry : " << std::dec << ptr << std::endl; |
---|
| 525 | m_heap_tab[ptr].print(); |
---|
| 526 | } // end print() |
---|
[307] | 527 | |
---|
[836] | 528 | ///////////////////////////////////////////////////////////////////// |
---|
| 529 | // The print_list() function prints a list from selected directory entry |
---|
| 530 | // Arguments : |
---|
| 531 | // - ptr : the pointer to the first entry to print |
---|
| 532 | ///////////////////////////////////////////////////////////////////// |
---|
| 533 | void print_list(const size_t &ptr) { |
---|
| 534 | bool end = false; |
---|
| 535 | size_t ptr_temp = ptr; |
---|
| 536 | std::cout << "Heap, printing the list from : " << std::dec << ptr << std::endl; |
---|
| 537 | while (!end){ |
---|
| 538 | m_heap_tab[ptr_temp].print(); |
---|
| 539 | if (ptr_temp == m_heap_tab[ptr_temp].next) { |
---|
| 540 | end = true; |
---|
| 541 | } |
---|
| 542 | ptr_temp = m_heap_tab[ptr_temp].next; |
---|
| 543 | } |
---|
| 544 | } // end print_list() |
---|
[307] | 545 | |
---|
[836] | 546 | ///////////////////////////////////////////////////////////////////// |
---|
| 547 | // The is_full() function return true if the heap is full. |
---|
| 548 | ///////////////////////////////////////////////////////////////////// |
---|
| 549 | bool is_full() { |
---|
| 550 | return full; |
---|
| 551 | } // end is_full() |
---|
[307] | 552 | |
---|
[836] | 553 | ///////////////////////////////////////////////////////////////////// |
---|
| 554 | // The next_free_ptr() function returns the pointer |
---|
| 555 | // to the next free entry. |
---|
| 556 | ///////////////////////////////////////////////////////////////////// |
---|
| 557 | size_t next_free_ptr() { |
---|
| 558 | return ptr_free; |
---|
| 559 | } // end next_free_ptr() |
---|
[307] | 560 | |
---|
[836] | 561 | ///////////////////////////////////////////////////////////////////// |
---|
| 562 | // The next_free_entry() function returns |
---|
| 563 | // a copy of the next free entry. |
---|
| 564 | ///////////////////////////////////////////////////////////////////// |
---|
| 565 | HeapEntry next_free_entry() { |
---|
| 566 | return HeapEntry(m_heap_tab[ptr_free]); |
---|
| 567 | } // end next_free_entry() |
---|
[307] | 568 | |
---|
[836] | 569 | ///////////////////////////////////////////////////////////////////// |
---|
| 570 | // The write_free_entry() function modify the next free entry. |
---|
| 571 | // Arguments : |
---|
| 572 | // - entry : the entry to write |
---|
| 573 | ///////////////////////////////////////////////////////////////////// |
---|
| 574 | void write_free_entry(const HeapEntry &entry){ |
---|
| 575 | m_heap_tab[ptr_free].copy(entry); |
---|
| 576 | } // end write_free_entry() |
---|
[307] | 577 | |
---|
[836] | 578 | ///////////////////////////////////////////////////////////////////// |
---|
| 579 | // The write_free_ptr() function writes the pointer |
---|
| 580 | // to the next free entry |
---|
| 581 | ///////////////////////////////////////////////////////////////////// |
---|
| 582 | void write_free_ptr(const size_t &ptr){ |
---|
| 583 | assert((ptr < tab_size) && "HeapDirectory error : try to write a wrong free pointer"); |
---|
| 584 | ptr_free = ptr; |
---|
| 585 | } // end write_free_ptr() |
---|
[307] | 586 | |
---|
[836] | 587 | ///////////////////////////////////////////////////////////////////// |
---|
| 588 | // The set_full() function sets the full bit (to true). |
---|
| 589 | ///////////////////////////////////////////////////////////////////// |
---|
| 590 | void set_full() { |
---|
| 591 | full = true; |
---|
| 592 | } // end set_full() |
---|
[307] | 593 | |
---|
[836] | 594 | ///////////////////////////////////////////////////////////////////// |
---|
| 595 | // The unset_full() function unsets the full bit (to false). |
---|
| 596 | ///////////////////////////////////////////////////////////////////// |
---|
| 597 | void unset_full() { |
---|
| 598 | full = false; |
---|
| 599 | } // end unset_full() |
---|
[307] | 600 | |
---|
[836] | 601 | ///////////////////////////////////////////////////////////////////// |
---|
| 602 | // The read() function returns a copy of |
---|
| 603 | // the entry pointed by the argument |
---|
| 604 | // Arguments : |
---|
| 605 | // - ptr : the pointer to the entry to read |
---|
| 606 | ///////////////////////////////////////////////////////////////////// |
---|
| 607 | HeapEntry read(const size_t &ptr) { |
---|
| 608 | assert((ptr < tab_size) && "HeapDirectory error : try to write a wrong free pointer"); |
---|
| 609 | return HeapEntry(m_heap_tab[ptr]); |
---|
| 610 | } // end read() |
---|
[307] | 611 | |
---|
[836] | 612 | ///////////////////////////////////////////////////////////////////// |
---|
| 613 | // The write() function writes an entry in the heap |
---|
| 614 | // Arguments : |
---|
| 615 | // - ptr : the pointer to the entry to replace |
---|
| 616 | // - entry : the entry to write |
---|
| 617 | ///////////////////////////////////////////////////////////////////// |
---|
| 618 | void write(const size_t &ptr, const HeapEntry &entry) { |
---|
| 619 | assert((ptr < tab_size) && "HeapDirectory error : try to write a wrong free pointer"); |
---|
| 620 | m_heap_tab[ptr].copy(entry); |
---|
| 621 | } // end write() |
---|
[307] | 622 | |
---|
[836] | 623 | }; // end class HeapDirectory |
---|
| 624 | |
---|
| 625 | //////////////////////////////////////////////////////////////////////// |
---|
| 626 | // Cache Data |
---|
| 627 | //////////////////////////////////////////////////////////////////////// |
---|
| 628 | class CacheData |
---|
| 629 | { |
---|
[307] | 630 | private: |
---|
[836] | 631 | const uint32_t m_sets; |
---|
| 632 | const uint32_t m_ways; |
---|
| 633 | const uint32_t m_words; |
---|
[307] | 634 | |
---|
[836] | 635 | uint32_t *** m_cache_data; |
---|
[307] | 636 | |
---|
| 637 | public: |
---|
| 638 | |
---|
[836] | 639 | /////////////////////////////////////////////////////// |
---|
| 640 | CacheData(uint32_t ways, uint32_t sets, uint32_t words) |
---|
| 641 | : m_sets(sets), m_ways(ways), m_words(words) |
---|
| 642 | { |
---|
| 643 | m_cache_data = new uint32_t ** [ways]; |
---|
| 644 | for (size_t i = 0; i < ways; i++) |
---|
| 645 | { |
---|
| 646 | m_cache_data[i] = new uint32_t * [sets]; |
---|
| 647 | } |
---|
| 648 | for (size_t i = 0; i < ways; i++) |
---|
| 649 | { |
---|
| 650 | for (size_t j = 0; j < sets; j++) |
---|
| 651 | { |
---|
| 652 | m_cache_data[i][j] = new uint32_t[words]; |
---|
| 653 | // Init to avoid potential errors from memory checkers |
---|
| 654 | std::memset(m_cache_data[i][j], 0, sizeof(uint32_t) * words); |
---|
| 655 | } |
---|
| 656 | } |
---|
| 657 | } |
---|
| 658 | //////////// |
---|
| 659 | ~CacheData() |
---|
| 660 | { |
---|
| 661 | for (size_t i = 0; i < m_ways; i++) |
---|
| 662 | { |
---|
| 663 | for (size_t j = 0; j < m_sets; j++) |
---|
| 664 | { |
---|
| 665 | delete [] m_cache_data[i][j]; |
---|
| 666 | } |
---|
| 667 | } |
---|
| 668 | for (size_t i = 0; i < m_ways; i++) |
---|
| 669 | { |
---|
| 670 | delete [] m_cache_data[i]; |
---|
| 671 | } |
---|
| 672 | delete [] m_cache_data; |
---|
| 673 | } |
---|
| 674 | ////////////////////////////////////////// |
---|
| 675 | uint32_t read (const uint32_t &way, |
---|
| 676 | const uint32_t &set, |
---|
| 677 | const uint32_t &word) const |
---|
| 678 | { |
---|
| 679 | assert((set < m_sets) && "Cache data error: Trying to read a wrong set" ); |
---|
| 680 | assert((way < m_ways) && "Cache data error: Trying to read a wrong way" ); |
---|
| 681 | assert((word < m_words) && "Cache data error: Trying to read a wrong word"); |
---|
[307] | 682 | |
---|
[836] | 683 | return m_cache_data[way][set][word]; |
---|
| 684 | } |
---|
| 685 | ////////////////////////////////////////// |
---|
| 686 | void read_line(const uint32_t &way, |
---|
| 687 | const uint32_t &set, |
---|
| 688 | sc_core::sc_signal<uint32_t> * cache_line) |
---|
| 689 | { |
---|
| 690 | assert((set < m_sets) && "Cache data error: Trying to read a wrong set" ); |
---|
| 691 | assert((way < m_ways) && "Cache data error: Trying to read a wrong way" ); |
---|
[449] | 692 | |
---|
[836] | 693 | for (uint32_t word = 0; word < m_words; word++) { |
---|
| 694 | cache_line[word].write(m_cache_data[way][set][word]); |
---|
| 695 | } |
---|
| 696 | } |
---|
| 697 | ///////////////////////////////////////// |
---|
| 698 | void write (const uint32_t &way, |
---|
| 699 | const uint32_t &set, |
---|
| 700 | const uint32_t &word, |
---|
| 701 | const uint32_t &data, |
---|
| 702 | const uint32_t &be = 0xF) |
---|
| 703 | { |
---|
[307] | 704 | |
---|
[836] | 705 | assert((set < m_sets) && "Cache data error: Trying to write a wrong set" ); |
---|
| 706 | assert((way < m_ways) && "Cache data error: Trying to write a wrong way" ); |
---|
| 707 | assert((word < m_words) && "Cache data error: Trying to write a wrong word"); |
---|
| 708 | assert((be <= 0xF) && "Cache data error: Trying to write a wrong be"); |
---|
[307] | 709 | |
---|
[836] | 710 | if (be == 0x0) return; |
---|
[307] | 711 | |
---|
[836] | 712 | if (be == 0xF) |
---|
| 713 | { |
---|
| 714 | m_cache_data[way][set][word] = data; |
---|
| 715 | return; |
---|
| 716 | } |
---|
[307] | 717 | |
---|
[836] | 718 | uint32_t mask = 0; |
---|
| 719 | if (be & 0x1) mask = mask | 0x000000FF; |
---|
| 720 | if (be & 0x2) mask = mask | 0x0000FF00; |
---|
| 721 | if (be & 0x4) mask = mask | 0x00FF0000; |
---|
| 722 | if (be & 0x8) mask = mask | 0xFF000000; |
---|
[307] | 723 | |
---|
[836] | 724 | m_cache_data[way][set][word] = |
---|
| 725 | (data & mask) | (m_cache_data[way][set][word] & ~mask); |
---|
| 726 | } |
---|
| 727 | }; // end class CacheData |
---|
[307] | 728 | |
---|
| 729 | }} // end namespaces |
---|
| 730 | |
---|
| 731 | #endif |
---|
| 732 | |
---|
| 733 | // Local Variables: |
---|
| 734 | // tab-width: 4 |
---|
| 735 | // c-basic-offset: 4 |
---|
| 736 | // c-file-offsets:((innamespace . 0)(inline-open . 0)) |
---|
| 737 | // indent-tabs-mode: nil |
---|
| 738 | // End: |
---|
| 739 | |
---|
| 740 | // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 |
---|
| 741 | |
---|