881ef5d66c131100bcb1ff4900b0fe8574d3d839
[gem5.git] / src / mem / ruby / common / Address.hh
1 /*
2 * Copyright (c) 1999 Mark D. Hill and David A. Wood
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 #ifndef __MEM_RUBY_COMMON_ADDRESS_HH__
30 #define __MEM_RUBY_COMMON_ADDRESS_HH__
31
32 #include <cassert>
33 #include <iomanip>
34 #include <iostream>
35
36 #include "base/hashmap.hh"
37 #include "mem/ruby/common/TypeDefines.hh"
38
39 const uint32_t ADDRESS_WIDTH = 64; // address width in bytes
40
41 class Address;
42 typedef Address PhysAddress;
43 typedef Address VirtAddress;
44
45 class Address
46 {
47 public:
48 Address()
49 : m_address(0)
50 { }
51
52 explicit
53 Address(physical_address_t address)
54 : m_address(address)
55 { }
56
57 Address(const Address& obj);
58 Address& operator=(const Address& obj);
59
60 void setAddress(physical_address_t address) { m_address = address; }
61 physical_address_t getAddress() const {return m_address;}
62 // selects bits inclusive
63 physical_address_t bitSelect(int small, int big) const;
64 physical_address_t bitRemove(int small, int big) const;
65 physical_address_t maskLowOrderBits(int number) const;
66 physical_address_t maskHighOrderBits(int number) const;
67 physical_address_t shiftLowOrderBits(int number) const;
68
69 physical_address_t getLineAddress() const;
70 physical_address_t getOffset() const;
71 void makeLineAddress();
72 void makePageAddress();
73 void makeNextStrideAddress(int stride);
74
75 Index memoryModuleIndex() const;
76
77 void print(std::ostream& out) const;
78 void output(std::ostream& out) const;
79 void input(std::istream& in);
80
81 void
82 setOffset(int offset)
83 {
84 // first, zero out the offset bits
85 makeLineAddress();
86 m_address |= (physical_address_t) offset;
87 }
88
89 private:
90 physical_address_t m_address;
91 };
92
93 inline Address
94 line_address(const Address& addr)
95 {
96 Address temp(addr);
97 temp.makeLineAddress();
98 return temp;
99 }
100
101 inline bool
102 operator<(const Address& obj1, const Address& obj2)
103 {
104 return obj1.getAddress() < obj2.getAddress();
105 }
106
107 inline std::ostream&
108 operator<<(std::ostream& out, const Address& obj)
109 {
110 obj.print(out);
111 out << std::flush;
112 return out;
113 }
114
115 inline bool
116 operator==(const Address& obj1, const Address& obj2)
117 {
118 return (obj1.getAddress() == obj2.getAddress());
119 }
120
121 inline bool
122 operator!=(const Address& obj1, const Address& obj2)
123 {
124 return (obj1.getAddress() != obj2.getAddress());
125 }
126
127 // rips bits inclusive
128 inline physical_address_t
129 Address::bitSelect(int small, int big) const
130 {
131 physical_address_t mask;
132 assert((unsigned)big >= (unsigned)small);
133
134 if (big >= ADDRESS_WIDTH - 1) {
135 return (m_address >> small);
136 } else {
137 mask = ~((physical_address_t)~0 << (big + 1));
138 // FIXME - this is slow to manipulate a 64-bit number using 32-bits
139 physical_address_t partial = (m_address & mask);
140 return (partial >> small);
141 }
142 }
143
144 // removes bits inclusive
145 inline physical_address_t
146 Address::bitRemove(int small, int big) const
147 {
148 physical_address_t mask;
149 assert((unsigned)big >= (unsigned)small);
150
151 if (small >= ADDRESS_WIDTH - 1) {
152 return m_address;
153 } else if (big >= ADDRESS_WIDTH - 1) {
154 mask = (physical_address_t)~0 >> small;
155 return (m_address & mask);
156 } else if (small == 0) {
157 mask = (physical_address_t)~0 << big;
158 return (m_address & mask);
159 } else {
160 mask = ~((physical_address_t)~0 << small);
161 physical_address_t lower_bits = m_address & mask;
162 mask = (physical_address_t)~0 << (big + 1);
163 physical_address_t higher_bits = m_address & mask;
164
165 // Shift the valid high bits over the removed section
166 higher_bits = higher_bits >> (big - small + 1);
167 return (higher_bits | lower_bits);
168 }
169 }
170
171 inline physical_address_t
172 Address::maskLowOrderBits(int number) const
173 {
174 physical_address_t mask;
175
176 if (number >= ADDRESS_WIDTH - 1) {
177 mask = ~0;
178 } else {
179 mask = (physical_address_t)~0 << number;
180 }
181 return (m_address & mask);
182 }
183
184 inline physical_address_t
185 Address::maskHighOrderBits(int number) const
186 {
187 physical_address_t mask;
188
189 if (number >= ADDRESS_WIDTH - 1) {
190 mask = ~0;
191 } else {
192 mask = (physical_address_t)~0 >> number;
193 }
194 return (m_address & mask);
195 }
196
197 inline physical_address_t
198 Address::shiftLowOrderBits(int number) const
199 {
200 return (m_address >> number);
201 }
202
203 Address next_stride_address(const Address& addr, int stride);
204 Address page_address(const Address& addr);
205
206 __hash_namespace_begin
207 template <> struct hash<Address>
208 {
209 size_t
210 operator()(const Address &s) const
211 {
212 return (size_t)s.getAddress();
213 }
214 };
215 __hash_namespace_end
216
217 namespace std {
218 template <> struct equal_to<Address>
219 {
220 bool
221 operator()(const Address& s1, const Address& s2) const
222 {
223 return s1 == s2;
224 }
225 };
226 } // namespace std
227
228 #endif // __MEM_RUBY_COMMON_ADDRESS_HH__