2 * Copyright (c) 1999 Mark D. Hill and David A. Wood
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.
29 #ifndef __MEM_RUBY_COMMON_ADDRESS_HH__
30 #define __MEM_RUBY_COMMON_ADDRESS_HH__
34 #include "base/hashmap.hh"
35 #include "mem/ruby/common/Global.hh"
36 #include "mem/ruby/system/MachineID.hh"
37 #include "mem/ruby/system/NodeID.hh"
38 #include "mem/ruby/system/System.hh"
40 const int ADDRESS_WIDTH = 64; // address width in bytes
43 typedef Address PhysAddress;
44 typedef Address VirtAddress;
54 Address(physical_address_t address)
58 Address(const Address& obj);
59 Address& operator=(const Address& obj);
61 void setAddress(physical_address_t address) { m_address = address; }
62 physical_address_t getAddress() const {return m_address;}
63 // selects bits inclusive
64 physical_address_t bitSelect(int small, int big) const;
65 physical_address_t bitRemove(int small, int big) const;
66 physical_address_t maskLowOrderBits(int number) const;
67 physical_address_t maskHighOrderBits(int number) const;
68 physical_address_t shiftLowOrderBits(int number) const;
71 getLineAddress() const
73 return bitSelect(RubySystem::getBlockSizeBits(), ADDRESS_WIDTH);
79 return bitSelect(0, RubySystem::getBlockSizeBits() - 1);
85 m_address = maskLowOrderBits(RubySystem::getBlockSizeBits());
88 // returns the next stride address based on line address
90 makeNextStrideAddress(int stride)
92 m_address = maskLowOrderBits(RubySystem::getBlockSizeBits())
93 + RubySystem::getBlockSizeBytes()*stride;
96 int getBankSetNum() const;
97 int getBankSetDist() const;
99 Index memoryModuleIndex() const;
101 void print(std::ostream& out) const;
102 void output(std::ostream& out) const;
103 void input(std::istream& in);
106 setOffset(int offset)
108 // first, zero out the offset bits
110 m_address |= (physical_address_t) offset;
114 physical_address_t m_address;
118 line_address(const Address& addr)
121 temp.makeLineAddress();
126 operator<(const Address& obj1, const Address& obj2)
128 return obj1.getAddress() < obj2.getAddress();
132 operator<<(std::ostream& out, const Address& obj)
140 operator==(const Address& obj1, const Address& obj2)
142 return (obj1.getAddress() == obj2.getAddress());
146 operator!=(const Address& obj1, const Address& obj2)
148 return (obj1.getAddress() != obj2.getAddress());
151 // rips bits inclusive
152 inline physical_address_t
153 Address::bitSelect(int small, int big) const
155 physical_address_t mask;
156 assert((unsigned)big >= (unsigned)small);
158 if (big >= ADDRESS_WIDTH - 1) {
159 return (m_address >> small);
161 mask = ~((physical_address_t)~0 << (big + 1));
162 // FIXME - this is slow to manipulate a 64-bit number using 32-bits
163 physical_address_t partial = (m_address & mask);
164 return (partial >> small);
168 // removes bits inclusive
169 inline physical_address_t
170 Address::bitRemove(int small, int big) const
172 physical_address_t mask;
173 assert((unsigned)big >= (unsigned)small);
175 if (small >= ADDRESS_WIDTH - 1) {
177 } else if (big >= ADDRESS_WIDTH - 1) {
178 mask = (physical_address_t)~0 >> small;
179 return (m_address & mask);
180 } else if (small == 0) {
181 mask = (physical_address_t)~0 << big;
182 return (m_address & mask);
184 mask = ~((physical_address_t)~0 << small);
185 physical_address_t lower_bits = m_address & mask;
186 mask = (physical_address_t)~0 << (big + 1);
187 physical_address_t higher_bits = m_address & mask;
189 // Shift the valid high bits over the removed section
190 higher_bits = higher_bits >> (big - small + 1);
191 return (higher_bits | lower_bits);
195 inline physical_address_t
196 Address::maskLowOrderBits(int number) const
198 physical_address_t mask;
200 if (number >= ADDRESS_WIDTH - 1) {
203 mask = (physical_address_t)~0 << number;
205 return (m_address & mask);
208 inline physical_address_t
209 Address::maskHighOrderBits(int number) const
211 physical_address_t mask;
213 if (number >= ADDRESS_WIDTH - 1) {
216 mask = (physical_address_t)~0 >> number;
218 return (m_address & mask);
221 inline physical_address_t
222 Address::shiftLowOrderBits(int number) const
224 return (m_address >> number);
228 Address::memoryModuleIndex() const
231 bitSelect(RubySystem::getBlockSizeBits() +
232 RubySystem::getMemorySizeBits(), ADDRESS_WIDTH);
236 // Index indexHighPortion =
237 // address.bitSelect(MEMORY_SIZE_BITS - 1,
238 // PAGE_SIZE_BITS + NUMBER_OF_MEMORY_MODULE_BITS);
239 // Index indexLowPortion =
240 // address.bitSelect(DATA_BLOCK_BITS, PAGE_SIZE_BITS - 1);
242 // Index index = indexLowPortion |
243 // (indexHighPortion << (PAGE_SIZE_BITS - DATA_BLOCK_BITS));
246 Round-robin mapping of addresses, at page size granularity
248 ADDRESS_WIDTH MEMORY_SIZE_BITS PAGE_SIZE_BITS DATA_BLOCK_BITS
251 -----------------------------------------------------------------------
252 | unused |xxxxxxxxxxxxxxx| |xxxxxxxxxxxxxxx| |
253 | |xxxxxxxxxxxxxxx| |xxxxxxxxxxxxxxx| |
254 -----------------------------------------------------------------------
255 indexHighPortion indexLowPortion
257 NUMBER_OF_MEMORY_MODULE_BITS
262 Address::print(std::ostream& out) const
265 out << "[" << hex << "0x" << m_address << "," << " line 0x"
266 << maskLowOrderBits(RubySystem::getBlockSizeBits()) << dec << "]"
271 namespace __hash_namespace {
272 template <> struct hash<Address>
275 operator()(const Address &s) const
277 return (size_t)s.getAddress();
280 /* namespace __hash_namespace */ }
283 template <> struct equal_to<Address>
286 operator()(const Address& s1, const Address& s2) const
291 /* namespace std */ }
293 #endif // __MEM_RUBY_COMMON_ADDRESS_HH__