Changes to untemplate StaticInst and StaticInstPtr, change the isa to a namespace...
[gem5.git] / cpu / o3 / btb.cc
1 /*
2 * Copyright (c) 2004-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include "base/intmath.hh"
30 #include "base/trace.hh"
31 #include "cpu/o3/btb.hh"
32
33 using namespace TheISA;
34
35 DefaultBTB::DefaultBTB(unsigned _numEntries,
36 unsigned _tagBits,
37 unsigned _instShiftAmt)
38 : numEntries(_numEntries),
39 tagBits(_tagBits),
40 instShiftAmt(_instShiftAmt)
41 {
42 // @todo Check to make sure num_entries is valid (a power of 2)
43
44 DPRINTF(Fetch, "BTB: Creating BTB object.\n");
45
46 btb = new BTBEntry[numEntries];
47
48 for (int i = 0; i < numEntries; ++i)
49 {
50 btb[i].valid = false;
51 }
52
53 idxMask = numEntries - 1;
54
55 tagMask = (1 << tagBits) - 1;
56
57 tagShiftAmt = instShiftAmt + floorLog2(numEntries);
58 }
59
60 inline
61 unsigned
62 DefaultBTB::getIndex(const Addr &inst_PC)
63 {
64 // Need to shift PC over by the word offset.
65 return (inst_PC >> instShiftAmt) & idxMask;
66 }
67
68 inline
69 Addr
70 DefaultBTB::getTag(const Addr &inst_PC)
71 {
72 return (inst_PC >> tagShiftAmt) & tagMask;
73 }
74
75 bool
76 DefaultBTB::valid(const Addr &inst_PC)
77 {
78 unsigned btb_idx = getIndex(inst_PC);
79
80 Addr inst_tag = getTag(inst_PC);
81
82 assert(btb_idx < numEntries);
83
84 if (btb[btb_idx].valid && inst_tag == btb[btb_idx].tag) {
85 return true;
86 } else {
87 return false;
88 }
89 }
90
91 // @todo Create some sort of return struct that has both whether or not the
92 // address is valid, and also the address. For now will just use addr = 0 to
93 // represent invalid entry.
94 Addr
95 DefaultBTB::lookup(const Addr &inst_PC)
96 {
97 unsigned btb_idx = getIndex(inst_PC);
98
99 Addr inst_tag = getTag(inst_PC);
100
101 assert(btb_idx < numEntries);
102
103 if (btb[btb_idx].valid && inst_tag == btb[btb_idx].tag) {
104 return btb[btb_idx].target;
105 } else {
106 return 0;
107 }
108 }
109
110 void
111 DefaultBTB::update(const Addr &inst_PC, const Addr &target)
112 {
113 unsigned btb_idx = getIndex(inst_PC);
114
115 assert(btb_idx < numEntries);
116
117 btb[btb_idx].valid = true;
118 btb[btb_idx].target = target;
119 btb[btb_idx].tag = getTag(inst_PC);
120 }