Make SPARC checkpointing work
[gem5.git] / src / arch / sparc / intregfile.cc
1 /*
2 * Copyright (c) 2003-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 * Authors: Gabe Black
29 * Ali Saidi
30 */
31
32 #include "arch/sparc/intregfile.hh"
33 #include "base/trace.hh"
34 #include "base/misc.hh"
35 #include "sim/serialize.hh"
36
37 #include <string.h>
38
39 using namespace SparcISA;
40 using namespace std;
41
42 class Checkpoint;
43
44 string SparcISA::getIntRegName(RegIndex index)
45 {
46 static std::string intRegName[NumIntArchRegs] =
47 {"g0", "g1", "g2", "g3", "g4", "g5", "g6", "g7",
48 "o0", "o1", "o2", "o3", "o4", "o5", "o6", "o7",
49 "l0", "l1", "l2", "l3", "l4", "l5", "l6", "l7",
50 "i0", "i1", "i2", "i3", "i4", "i5", "i6", "i7"};
51 return intRegName[index];
52 }
53
54 int IntRegFile::flattenIndex(int reg)
55 {
56 int flatIndex = offset[reg >> FrameOffsetBits]
57 | (reg & FrameOffsetMask);
58 DPRINTF(Sparc, "Flattened index %d into %d.\n", reg, flatIndex);
59 return flatIndex;
60 }
61
62 void IntRegFile::clear()
63 {
64 int x;
65 for (x = 0; x < MaxGL; x++)
66 memset(regGlobals[x], 0, sizeof(IntReg) * RegsPerFrame);
67 for(int x = 0; x < 2 * NWindows; x++)
68 memset(regSegments[x], 0, sizeof(IntReg) * RegsPerFrame);
69 memset(regs, 0, sizeof(IntReg) * NumIntRegs);
70 }
71
72 IntRegFile::IntRegFile()
73 {
74 offset[Globals] = 0;
75 regView[Globals] = regGlobals[0];
76 setCWP(0);
77 clear();
78 }
79
80 IntReg IntRegFile::readReg(int intReg)
81 {
82 DPRINTF(Sparc, "Read register %d = 0x%x\n", intReg, regs[intReg]);
83 return regs[intReg];
84 /* XXX Currently not used. When used again regView/offset need to be
85 * serialized!
86 IntReg val;
87 if(intReg < NumIntArchRegs)
88 val = regView[intReg >> FrameOffsetBits][intReg & FrameOffsetMask];
89 else if((intReg -= NumIntArchRegs) < NumMicroIntRegs)
90 val = microRegs[intReg];
91 else
92 panic("Tried to read non-existant integer register %d, %d\n",
93 NumIntArchRegs + NumMicroIntRegs + intReg, intReg);
94
95 DPRINTF(Sparc, "Read register %d = 0x%x\n", intReg, val);
96 return val;
97 */
98 }
99
100 void IntRegFile::setReg(int intReg, const IntReg &val)
101 {
102 if(intReg)
103 {
104 DPRINTF(Sparc, "Wrote register %d = 0x%x\n", intReg, val);
105 regs[intReg] = val;
106 }
107 return;
108 /* XXX Currently not used. When used again regView/offset need to be
109 * serialized!
110 if(intReg)
111 {
112 DPRINTF(Sparc, "Wrote register %d = 0x%x\n", intReg, val);
113 if(intReg < NumIntArchRegs)
114 regView[intReg >> FrameOffsetBits][intReg & FrameOffsetMask] = val;
115 else if((intReg -= NumIntArchRegs) < NumMicroIntRegs)
116 microRegs[intReg] = val;
117 else
118 panic("Tried to set non-existant integer register\n");
119 } */
120 }
121
122 //This doesn't effect the actual CWP register.
123 //It's purpose is to adjust the view of the register file
124 //to what it would be if CWP = cwp.
125 void IntRegFile::setCWP(int cwp)
126 {
127 int index = ((NWindows - cwp) % NWindows) * 2;
128 if (index < 0)
129 panic("Index less than 0. cwp=%d nwin=%d\n", cwp, NWindows);
130 offset[Outputs] = FrameOffset + (index * RegsPerFrame);
131 offset[Locals] = FrameOffset + ((index+1) * RegsPerFrame);
132 offset[Inputs] = FrameOffset +
133 (((index+2) % (NWindows * 2)) * RegsPerFrame);
134 regView[Outputs] = regSegments[index];
135 regView[Locals] = regSegments[index+1];
136 regView[Inputs] = regSegments[(index+2) % (NWindows * 2)];
137
138 DPRINTF(Sparc, "Changed the CWP value to %d\n", cwp);
139 }
140
141 void IntRegFile::setGlobals(int gl)
142 {
143 DPRINTF(Sparc, "Now using %d globals\n", gl);
144
145 regView[Globals] = regGlobals[gl];
146 offset[Globals] = RegGlobalOffset + gl * RegsPerFrame;
147
148 if (regView[Globals] == regView[Inputs] ||
149 regView[Globals] == regView[Locals] ||
150 regView[Globals] == regView[Outputs] )
151 panic("Two register arrays set to the same thing!\n");
152 }
153
154 void IntRegFile::serialize(std::ostream &os)
155 {
156 SERIALIZE_ARRAY(regs, NumIntRegs);
157 SERIALIZE_ARRAY(microRegs, NumMicroIntRegs);
158
159 /* the below doesn't seem needed unless gabe makes regview work*/
160 unsigned int x;
161 for(x = 0; x < MaxGL; x++)
162 SERIALIZE_ARRAY(regGlobals[x], RegsPerFrame);
163 for(x = 0; x < 2 * NWindows; x++)
164 SERIALIZE_ARRAY(regSegments[x], RegsPerFrame);
165 }
166
167 void IntRegFile::unserialize(Checkpoint *cp, const std::string &section)
168 {
169 UNSERIALIZE_ARRAY(regs, NumIntRegs);
170 UNSERIALIZE_ARRAY(microRegs, NumMicroIntRegs);
171
172 /* the below doesn't seem needed unless gabe makes regview work*/
173 unsigned int x;
174 for(x = 0; x < MaxGL; x++)
175 UNSERIALIZE_ARRAY(regGlobals[x], RegsPerFrame);
176 for(unsigned int x = 0; x < 2 * NWindows; x++)
177 UNSERIALIZE_ARRAY(regSegments[x], RegsPerFrame);
178 }