Merge ktlim@zizzer.eecs.umich.edu:/bk/m5
[gem5.git] / cpu / beta_cpu / btb.cc
1 #include <math.h>
2
3 #include "cpu/beta_cpu/btb.hh"
4 #include "base/trace.hh"
5
6 DefaultBTB::DefaultBTB(unsigned _numEntries,
7 unsigned _tagBits,
8 unsigned _instShiftAmt)
9 : numEntries(_numEntries),
10 tagBits(_tagBits),
11 instShiftAmt(_instShiftAmt)
12 {
13 // @todo Check to make sure num_entries is valid (a power of 2)
14
15 DPRINTF(Fetch, "BTB: Creating BTB object.\n");
16
17 btb = new BTBEntry[numEntries];
18
19 for (int i = 0; i < numEntries; ++i)
20 {
21 btb[i].valid = false;
22 }
23
24 idxMask = numEntries - 1;
25
26 tagMask = (1 << tagBits) - 1;
27
28 tagShiftAmt = instShiftAmt + (int)log2(numEntries);
29 }
30
31 inline
32 unsigned
33 DefaultBTB::getIndex(const Addr &inst_PC)
34 {
35 // Need to shift PC over by the word offset.
36 return (inst_PC >> instShiftAmt) & idxMask;
37 }
38
39 inline
40 Addr
41 DefaultBTB::getTag(const Addr &inst_PC)
42 {
43 return (inst_PC >> tagShiftAmt) & tagMask;
44 }
45
46 bool
47 DefaultBTB::valid(const Addr &inst_PC)
48 {
49 unsigned btb_idx = getIndex(inst_PC);
50
51 Addr inst_tag = getTag(inst_PC);
52
53 assert(btb_idx < numEntries);
54
55 if (btb[btb_idx].valid && inst_tag == btb[btb_idx].tag) {
56 return true;
57 } else {
58 return false;
59 }
60 }
61
62 // @todo Create some sort of return struct that has both whether or not the
63 // address is valid, and also the address. For now will just use addr = 0 to
64 // represent invalid entry.
65 Addr
66 DefaultBTB::lookup(const Addr &inst_PC)
67 {
68 unsigned btb_idx = getIndex(inst_PC);
69
70 Addr inst_tag = getTag(inst_PC);
71
72 assert(btb_idx < numEntries);
73
74 if (btb[btb_idx].valid && inst_tag == btb[btb_idx].tag) {
75 return btb[btb_idx].target;
76 } else {
77 return 0;
78 }
79 }
80
81 void
82 DefaultBTB::update(const Addr &inst_PC, const Addr &target)
83 {
84 unsigned btb_idx = getIndex(inst_PC);
85
86 assert(btb_idx < numEntries);
87
88 btb[btb_idx].valid = true;
89 btb[btb_idx].target = target;
90 btb[btb_idx].tag = getTag(inst_PC);
91 }