Address.hh revision 7563:406e98960def
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 <iomanip>
33
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"
39
40const int ADDRESS_WIDTH = 64; // address width in bytes
41
42class Address;
43typedef Address PhysAddress;
44typedef Address VirtAddress;
45
46class Address
47{
48  public:
49    Address()
50        : m_address(0)
51    { }
52
53    explicit
54    Address(physical_address_t address)
55        : m_address(address)
56    { }
57
58    Address(const Address& obj);
59    Address& operator=(const Address& obj);
60
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;
69
70    physical_address_t
71    getLineAddress() const
72    {
73        return bitSelect(RubySystem::getBlockSizeBits(), ADDRESS_WIDTH);
74    }
75
76    physical_address_t
77    getOffset() const
78    {
79        return bitSelect(0, RubySystem::getBlockSizeBits() - 1);
80    }
81
82    void
83    makeLineAddress()
84    {
85        m_address = maskLowOrderBits(RubySystem::getBlockSizeBits());
86    }
87
88    // returns the next stride address based on line address
89    void
90    makeNextStrideAddress(int stride)
91    {
92        m_address = maskLowOrderBits(RubySystem::getBlockSizeBits())
93            + RubySystem::getBlockSizeBytes()*stride;
94    }
95
96    int getBankSetNum() const;
97    int getBankSetDist() const;
98
99    Index memoryModuleIndex() const;
100
101    void print(std::ostream& out) const;
102    void output(std::ostream& out) const;
103    void input(std::istream& in);
104
105    void
106    setOffset(int offset)
107    {
108        // first, zero out the offset bits
109        makeLineAddress();
110        m_address |= (physical_address_t) offset;
111    }
112
113  private:
114    physical_address_t m_address;
115};
116
117inline Address
118line_address(const Address& addr)
119{
120    Address temp(addr);
121    temp.makeLineAddress();
122    return temp;
123}
124
125inline bool
126operator<(const Address& obj1, const Address& obj2)
127{
128    return obj1.getAddress() < obj2.getAddress();
129}
130
131inline std::ostream&
132operator<<(std::ostream& out, const Address& obj)
133{
134    obj.print(out);
135    out << std::flush;
136    return out;
137}
138
139inline bool
140operator==(const Address& obj1, const Address& obj2)
141{
142    return (obj1.getAddress() == obj2.getAddress());
143}
144
145inline bool
146operator!=(const Address& obj1, const Address& obj2)
147{
148    return (obj1.getAddress() != obj2.getAddress());
149}
150
151// rips bits inclusive
152inline physical_address_t
153Address::bitSelect(int small, int big) const
154{
155    physical_address_t mask;
156    assert((unsigned)big >= (unsigned)small);
157
158    if (big >= ADDRESS_WIDTH - 1) {
159        return (m_address >> small);
160    } else {
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);
165    }
166}
167
168// removes bits inclusive
169inline physical_address_t
170Address::bitRemove(int small, int big) const
171{
172    physical_address_t mask;
173    assert((unsigned)big >= (unsigned)small);
174
175    if (small >= ADDRESS_WIDTH - 1) {
176        return m_address;
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);
183    } else {
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;
188
189        // Shift the valid high bits over the removed section
190        higher_bits = higher_bits >> (big - small + 1);
191        return (higher_bits | lower_bits);
192    }
193}
194
195inline physical_address_t
196Address::maskLowOrderBits(int number) const
197{
198  physical_address_t mask;
199
200  if (number >= ADDRESS_WIDTH - 1) {
201      mask = ~0;
202  } else {
203      mask = (physical_address_t)~0 << number;
204  }
205  return (m_address & mask);
206}
207
208inline physical_address_t
209Address::maskHighOrderBits(int number) const
210{
211    physical_address_t mask;
212
213    if (number >= ADDRESS_WIDTH - 1) {
214        mask = ~0;
215    } else {
216        mask = (physical_address_t)~0 >> number;
217    }
218    return (m_address & mask);
219}
220
221inline physical_address_t
222Address::shiftLowOrderBits(int number) const
223{
224    return (m_address >> number);
225}
226
227inline integer_t
228Address::memoryModuleIndex() const
229{
230    integer_t index =
231        bitSelect(RubySystem::getBlockSizeBits() +
232                  RubySystem::getMemorySizeBits(), ADDRESS_WIDTH);
233    assert (index >= 0);
234    return index;
235
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);
241    //
242    // Index index = indexLowPortion |
243    //     (indexHighPortion << (PAGE_SIZE_BITS - DATA_BLOCK_BITS));
244
245    /*
246      Round-robin mapping of addresses, at page size granularity
247
248ADDRESS_WIDTH    MEMORY_SIZE_BITS        PAGE_SIZE_BITS  DATA_BLOCK_BITS
249  |                    |                       |               |
250 \ /                  \ /                     \ /             \ /       0
251  -----------------------------------------------------------------------
252  |       unused        |xxxxxxxxxxxxxxx|       |xxxxxxxxxxxxxxx|       |
253  |                     |xxxxxxxxxxxxxxx|       |xxxxxxxxxxxxxxx|       |
254  -----------------------------------------------------------------------
255                        indexHighPortion         indexLowPortion
256                                        <------->
257                               NUMBER_OF_MEMORY_MODULE_BITS
258    */
259}
260
261inline void
262Address::print(std::ostream& out) const
263{
264    using namespace std;
265    out << "[" << hex << "0x" << m_address << "," << " line 0x"
266        << maskLowOrderBits(RubySystem::getBlockSizeBits()) << dec << "]"
267        << flush;
268}
269
270class Address;
271namespace __hash_namespace {
272template <> struct hash<Address>
273{
274    size_t
275    operator()(const Address &s) const
276    {
277        return (size_t)s.getAddress();
278    }
279};
280/* namespace __hash_namespace */ }
281
282namespace std {
283template <> struct equal_to<Address>
284{
285    bool
286    operator()(const Address& s1, const Address& s2) const
287    {
288        return s1 == s2;
289    }
290};
291/* namespace std */ }
292
293#endif // __MEM_RUBY_COMMON_ADDRESS_HH__
294