Address.hh revision 9362:d7f4abbf52e3
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
39const uint32_t ADDRESS_WIDTH = 64; // address width in bytes
40
41class Address;
42typedef Address PhysAddress;
43typedef Address VirtAddress;
44
45class 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    int getBankSetNum() const;
76    int getBankSetDist() const;
77
78    Index memoryModuleIndex() const;
79
80    void print(std::ostream& out) const;
81    void output(std::ostream& out) const;
82    void input(std::istream& in);
83
84    void
85    setOffset(int offset)
86    {
87        // first, zero out the offset bits
88        makeLineAddress();
89        m_address |= (physical_address_t) offset;
90    }
91
92  private:
93    physical_address_t m_address;
94};
95
96inline Address
97line_address(const Address& addr)
98{
99    Address temp(addr);
100    temp.makeLineAddress();
101    return temp;
102}
103
104inline bool
105operator<(const Address& obj1, const Address& obj2)
106{
107    return obj1.getAddress() < obj2.getAddress();
108}
109
110inline std::ostream&
111operator<<(std::ostream& out, const Address& obj)
112{
113    obj.print(out);
114    out << std::flush;
115    return out;
116}
117
118inline bool
119operator==(const Address& obj1, const Address& obj2)
120{
121    return (obj1.getAddress() == obj2.getAddress());
122}
123
124inline bool
125operator!=(const Address& obj1, const Address& obj2)
126{
127    return (obj1.getAddress() != obj2.getAddress());
128}
129
130// rips bits inclusive
131inline physical_address_t
132Address::bitSelect(int small, int big) const
133{
134    physical_address_t mask;
135    assert((unsigned)big >= (unsigned)small);
136
137    if (big >= ADDRESS_WIDTH - 1) {
138        return (m_address >> small);
139    } else {
140        mask = ~((physical_address_t)~0 << (big + 1));
141        // FIXME - this is slow to manipulate a 64-bit number using 32-bits
142        physical_address_t partial = (m_address & mask);
143        return (partial >> small);
144    }
145}
146
147// removes bits inclusive
148inline physical_address_t
149Address::bitRemove(int small, int big) const
150{
151    physical_address_t mask;
152    assert((unsigned)big >= (unsigned)small);
153
154    if (small >= ADDRESS_WIDTH - 1) {
155        return m_address;
156    } else if (big >= ADDRESS_WIDTH - 1) {
157        mask = (physical_address_t)~0 >> small;
158        return (m_address & mask);
159    } else if (small == 0) {
160        mask = (physical_address_t)~0 << big;
161        return (m_address & mask);
162    } else {
163        mask = ~((physical_address_t)~0 << small);
164        physical_address_t lower_bits = m_address & mask;
165        mask = (physical_address_t)~0 << (big + 1);
166        physical_address_t higher_bits = m_address & mask;
167
168        // Shift the valid high bits over the removed section
169        higher_bits = higher_bits >> (big - small + 1);
170        return (higher_bits | lower_bits);
171    }
172}
173
174inline physical_address_t
175Address::maskLowOrderBits(int number) const
176{
177  physical_address_t mask;
178
179  if (number >= ADDRESS_WIDTH - 1) {
180      mask = ~0;
181  } else {
182      mask = (physical_address_t)~0 << number;
183  }
184  return (m_address & mask);
185}
186
187inline physical_address_t
188Address::maskHighOrderBits(int number) const
189{
190    physical_address_t mask;
191
192    if (number >= ADDRESS_WIDTH - 1) {
193        mask = ~0;
194    } else {
195        mask = (physical_address_t)~0 >> number;
196    }
197    return (m_address & mask);
198}
199
200inline physical_address_t
201Address::shiftLowOrderBits(int number) const
202{
203    return (m_address >> number);
204}
205
206Address next_stride_address(const Address& addr, int stride);
207Address page_address(const Address& addr);
208
209__hash_namespace_begin
210template <> struct hash<Address>
211{
212    size_t
213    operator()(const Address &s) const
214    {
215        return (size_t)s.getAddress();
216    }
217};
218__hash_namespace_end
219
220namespace std {
221template <> struct equal_to<Address>
222{
223    bool
224    operator()(const Address& s1, const Address& s2) const
225    {
226        return s1 == s2;
227    }
228};
229} // namespace std
230
231#endif // __MEM_RUBY_COMMON_ADDRESS_HH__
232