BankedArray.cc revision 9105
1
2
3#include <vector>
4
5#include "base/intmath.hh"
6#include "mem/ruby/common/TypeDefines.hh"
7#include "mem/ruby/system/BankedArray.hh"
8#include "sim/eventq.hh"
9
10BankedArray::BankedArray(unsigned int banks, unsigned int accessLatency, unsigned int startIndexBit) :
11    EventManager(&mainEventQueue)
12{
13    this->banks = banks;
14    this->accessLatency = accessLatency;
15    this->startIndexBit = startIndexBit;
16
17    if (banks != 0) {
18        bankBits = floorLog2(banks);
19    }
20
21    busyBanks.resize(banks);
22}
23
24bool
25BankedArray::tryAccess(Index idx)
26{
27    if (accessLatency == 0)
28        return true;
29
30    unsigned int bank = mapIndexToBank(idx);
31    assert(bank < banks);
32
33    if (busyBanks[bank].scheduled()) {
34        if (!(busyBanks[bank].startAccess == curTick() && busyBanks[bank].idx == idx)) {
35            return false;
36        } else {
37            return true;  // We tried to allocate resources twice in the same cycle for the same addr
38        }
39    }
40
41    busyBanks[bank].idx = idx;
42    busyBanks[bank].startAccess = curTick();
43
44    // substract 1 so that next cycle the resource available
45    schedule(busyBanks[bank], curTick()+accessLatency-1);
46
47    return true;
48}
49
50unsigned int
51BankedArray::mapIndexToBank(Index idx)
52{
53    if (banks == 1) {
54        return 0;
55    }
56    return idx % banks;
57}
58