Moved some constants from isa_traits.hh to the reg file headers.
[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 "sim/serialize.hh"
35
36 #include <string.h>
37
38 using namespace SparcISA;
39 using namespace std;
40
41 class Checkpoint;
42
43 string SparcISA::getIntRegName(RegIndex index)
44 {
45 static std::string intRegName[NumIntArchRegs] =
46 {"g0", "g1", "g2", "g3", "g4", "g5", "g6", "g7",
47 "o0", "o1", "o2", "o3", "o4", "o5", "o6", "o7",
48 "l0", "l1", "l2", "l3", "l4", "l5", "l6", "l7",
49 "i0", "i1", "i2", "i3", "i4", "i5", "i6", "i7"};
50 return intRegName[index];
51 }
52
53 int IntRegFile::flattenIndex(int reg)
54 {
55 int flatIndex = offset[reg >> FrameOffsetBits]
56 | (reg & FrameOffsetMask);
57 DPRINTF(Sparc, "Flattened index %d into %d.\n", reg, flatIndex);
58 return flatIndex;
59 }
60
61 void IntRegFile::clear()
62 {
63 int x;
64 for (x = 0; x < MaxGL; x++)
65 memset(regGlobals[x], 0, sizeof(IntReg) * RegsPerFrame);
66 for(int x = 0; x < 2 * NWindows; x++)
67 memset(regSegments[x], 0, sizeof(IntReg) * RegsPerFrame);
68 }
69
70 IntRegFile::IntRegFile()
71 {
72 offset[Globals] = 0;
73 regView[Globals] = regGlobals[0];
74 setCWP(0);
75 clear();
76 }
77
78 IntReg IntRegFile::readReg(int intReg)
79 {
80 IntReg val;
81 if(intReg < NumIntArchRegs)
82 val = regView[intReg >> FrameOffsetBits][intReg & FrameOffsetMask];
83 else if((intReg -= NumIntArchRegs) < NumMicroIntRegs)
84 val = microRegs[intReg];
85 else
86 panic("Tried to read non-existant integer register %d, %d\n", NumIntArchRegs + NumMicroIntRegs + intReg, intReg);
87
88 DPRINTF(Sparc, "Read register %d = 0x%x\n", intReg, val);
89 return val;
90 }
91
92 void IntRegFile::setReg(int intReg, const IntReg &val)
93 {
94 if(intReg)
95 {
96 DPRINTF(Sparc, "Wrote register %d = 0x%x\n", intReg, val);
97 if(intReg < NumIntArchRegs)
98 regView[intReg >> FrameOffsetBits][intReg & FrameOffsetMask] = val;
99 else if((intReg -= NumIntArchRegs) < NumMicroIntRegs)
100 microRegs[intReg] = val;
101 else
102 panic("Tried to set non-existant integer register\n");
103 }
104 }
105
106 //This doesn't effect the actual CWP register.
107 //It's purpose is to adjust the view of the register file
108 //to what it would be if CWP = cwp.
109 void IntRegFile::setCWP(int cwp)
110 {
111 int index = ((NWindows - cwp) % NWindows) * 2;
112 offset[Outputs] = FrameOffset + (index * RegsPerFrame);
113 offset[Locals] = FrameOffset + ((index+1) * RegsPerFrame);
114 offset[Inputs] = FrameOffset +
115 (((index+2) % (NWindows * 2)) * RegsPerFrame);
116 regView[Outputs] = regSegments[index];
117 regView[Locals] = regSegments[index+1];
118 regView[Inputs] = regSegments[(index+2) % (NWindows * 2)];
119
120 DPRINTF(Sparc, "Changed the CWP value to %d\n", cwp);
121 }
122
123 void IntRegFile::setGlobals(int gl)
124 {
125 DPRINTF(Sparc, "Now using %d globals\n", gl);
126
127 regView[Globals] = regGlobals[gl];
128 offset[Globals] = RegGlobalOffset + gl * RegsPerFrame;
129 }
130
131 void IntRegFile::serialize(std::ostream &os)
132 {
133 unsigned int x;
134 for(x = 0; x < MaxGL; x++)
135 SERIALIZE_ARRAY(regGlobals[x], RegsPerFrame);
136 for(x = 0; x < 2 * NWindows; x++)
137 SERIALIZE_ARRAY(regSegments[x], RegsPerFrame);
138 SERIALIZE_ARRAY(microRegs, NumMicroIntRegs);
139 }
140
141 void IntRegFile::unserialize(Checkpoint *cp, const std::string &section)
142 {
143 unsigned int x;
144 for(x = 0; x < MaxGL; x++)
145 UNSERIALIZE_ARRAY(regGlobals[x], RegsPerFrame);
146 for(unsigned int x = 0; x < 2 * NWindows; x++)
147 UNSERIALIZE_ARRAY(regSegments[x], RegsPerFrame);
148 UNSERIALIZE_ARRAY(microRegs, NumMicroIntRegs);
149 }