2 * Copyright (c) 2004-2005 The Regents of The University of Michigan
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.
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.
28 * Authors: Erik Hallnor
33 * Declaration of a memory trace CPU object for optimal caches. Uses a memory
34 * trace to access a fully associative cache with optimal replacement.
37 #ifndef __CPU_TRACE_OPT_CPU_HH__
38 #define __CPU_TRACE_OPT_CPU_HH__
42 #include "mem/mem_req.hh" // for MemReqPtr
43 #include "sim/eventq.hh" // for Event
44 #include "sim/sim_object.hh"
46 // Forward Declaration
50 * A CPU object to simulate a fully-associative cache with optimal replacement.
52 class OptCPU : public SimObject
57 typedef std::vector<RefIndex> L3Table;
58 typedef std::vector<L3Table> L2Table;
59 typedef std::vector<L2Table> L1Table;
62 * Event to call OptCPU::tick
64 class TickEvent : public Event
67 /** The associated CPU */
72 * Construct this event;
77 * Call the tick function.
82 * Return a string description of this event.
84 const char *description() const;
96 /** Reference Information, per set. */
97 std::vector<std::vector<RefInfo> > refInfo;
99 /** Lookup table to track blocks in the cache heap */
103 * Return the correct value in the lookup table.
105 RefIndex lookupValue(Addr addr)
107 int l1_index = (addr >> 32) & 0x0f;
108 int l2_index = (addr >> 16) & 0xffff;
109 int l3_index = addr & 0xffff;
110 assert(l1_index == addr >> 32);
111 return lookupTable[l1_index][l2_index][l3_index];
115 * Set the value in the lookup table.
117 void setValue(Addr addr, RefIndex index)
119 int l1_index = (addr >> 32) & 0x0f;
120 int l2_index = (addr >> 16) & 0xffff;
121 int l3_index = addr & 0xffff;
122 assert(l1_index == addr >> 32);
123 lookupTable[l1_index][l2_index][l3_index]=index;
127 * Initialize the lookup table to the given value.
129 void initTable(Addr addr, RefIndex index);
131 void heapSwap(int set, int a, int b) {
132 RefIndex tmp = cacheHeap[a];
133 cacheHeap[a] = cacheHeap[b];
136 setValue(refInfo[set][cacheHeap[a]].addr, a);
137 setValue(refInfo[set][cacheHeap[b]].addr, b);
140 int heapLeft(int index) { return index + index + 1; }
141 int heapRight(int index) { return index + index + 2; }
142 int heapParent(int index) { return (index - 1) >> 1; }
144 RefIndex heapRank(int set, int index) {
145 return refInfo[set][cacheHeap[index]].nextRefTime;
148 void heapify(int set, int start){
149 int left = heapLeft(start);
150 int right = heapRight(start);
152 if (left < assoc && heapRank(set, left) > heapRank(set, start)) {
155 if (right < assoc && heapRank(set, right) > heapRank(set, max)) {
160 heapSwap(set, start, max);
165 void verifyHeap(int set, int start) {
166 int left = heapLeft(start);
167 int right = heapRight(start);
170 assert(heapRank(set, start) >= heapRank(set, left));
171 verifyHeap(set, left);
174 assert(heapRank(set, start) >= heapRank(set, right));
175 verifyHeap(set, right);
179 void processRankIncrease(int set, int start) {
180 int parent = heapParent(start);
181 while (start > 0 && heapRank(set,parent) < heapRank(set,start)) {
182 heapSwap(set, parent, start);
184 parent = heapParent(start);
188 void processSet(int set);
190 static const RefIndex InfiniteRef = 0x7fffffff;
192 /** Memory reference trace. */
193 MemTraceReader *trace;
195 /** Cache heap for replacement. */
196 std::vector<RefIndex> cacheHeap;
198 /** The number of blocks in the cache. */
211 * Construct a OptCPU object.
213 OptCPU(const std::string &name,
214 MemTraceReader *_trace,
220 * Perform the optimal replacement simulation.
225 #endif // __CPU_TRACE_OPT_CPU_HH__