Address.hh revision 8485:7a9a7f2a3d46
12221SN/A/*
22221SN/A * Copyright (c) 1999 Mark D. Hill and David A. Wood
32221SN/A * All rights reserved.
42221SN/A *
52221SN/A * Redistribution and use in source and binary forms, with or without
62221SN/A * modification, are permitted provided that the following conditions are
72221SN/A * met: redistributions of source code must retain the above copyright
82221SN/A * notice, this list of conditions and the following disclaimer;
92221SN/A * redistributions in binary form must reproduce the above copyright
102221SN/A * notice, this list of conditions and the following disclaimer in the
112221SN/A * documentation and/or other materials provided with the distribution;
122221SN/A * neither the name of the copyright holders nor the names of its
132221SN/A * contributors may be used to endorse or promote products derived from
142221SN/A * this software without specific prior written permission.
152221SN/A *
162221SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
172221SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
182221SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
192221SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
202221SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212221SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
222221SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232221SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242221SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252221SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
262221SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272665Ssaidi@eecs.umich.edu */
282665Ssaidi@eecs.umich.edu
292665Ssaidi@eecs.umich.edu#ifndef __MEM_RUBY_COMMON_ADDRESS_HH__
302221SN/A#define __MEM_RUBY_COMMON_ADDRESS_HH__
312221SN/A
323890Ssaidi@eecs.umich.edu#include <cassert>
333890Ssaidi@eecs.umich.edu#include <iomanip>
342221SN/A
354997Sgblack@eecs.umich.edu#include "base/hashmap.hh"
367678Sgblack@eecs.umich.edu#include "mem/ruby/common/Global.hh"
372221SN/A#include "mem/ruby/system/NodeID.hh"
382221SN/A
392221SN/Aconst int ADDRESS_WIDTH = 64; // address width in bytes
402221SN/A
412223SN/Aclass Address;
422221SN/Atypedef Address PhysAddress;
432221SN/Atypedef Address VirtAddress;
443415Sgblack@eecs.umich.edu
453415Sgblack@eecs.umich.educlass Address
462221SN/A{
474997Sgblack@eecs.umich.edu  public:
484997Sgblack@eecs.umich.edu    Address()
493573Sgblack@eecs.umich.edu        : m_address(0)
502221SN/A    { }
512221SN/A
523576Sgblack@eecs.umich.edu    explicit
533576Sgblack@eecs.umich.edu    Address(physical_address_t address)
543576Sgblack@eecs.umich.edu        : m_address(address)
553576Sgblack@eecs.umich.edu    { }
563576Sgblack@eecs.umich.edu
573576Sgblack@eecs.umich.edu    Address(const Address& obj);
583576Sgblack@eecs.umich.edu    Address& operator=(const Address& obj);
593576Sgblack@eecs.umich.edu
603576Sgblack@eecs.umich.edu    void setAddress(physical_address_t address) { m_address = address; }
613573Sgblack@eecs.umich.edu    physical_address_t getAddress() const {return m_address;}
623573Sgblack@eecs.umich.edu    // selects bits inclusive
633573Sgblack@eecs.umich.edu    physical_address_t bitSelect(int small, int big) const;
643573Sgblack@eecs.umich.edu    physical_address_t bitRemove(int small, int big) const;
653573Sgblack@eecs.umich.edu    physical_address_t maskLowOrderBits(int number) const;
663576Sgblack@eecs.umich.edu    physical_address_t maskHighOrderBits(int number) const;
673573Sgblack@eecs.umich.edu    physical_address_t shiftLowOrderBits(int number) const;
683573Sgblack@eecs.umich.edu
692221SN/A    physical_address_t getLineAddress() const;
707678Sgblack@eecs.umich.edu    physical_address_t getOffset() const;
717678Sgblack@eecs.umich.edu    void makeLineAddress();
722221SN/A    void makeNextStrideAddress(int stride);
732223SN/A
742223SN/A    int getBankSetNum() const;
752223SN/A    int getBankSetDist() const;
763576Sgblack@eecs.umich.edu
772221SN/A    Index memoryModuleIndex() const;
782221SN/A
793573Sgblack@eecs.umich.edu    void print(std::ostream& out) const;
803573Sgblack@eecs.umich.edu    void output(std::ostream& out) const;
812221SN/A    void input(std::istream& in);
823573Sgblack@eecs.umich.edu
833573Sgblack@eecs.umich.edu    void
842221SN/A    setOffset(int offset)
854695Sgblack@eecs.umich.edu    {
863573Sgblack@eecs.umich.edu        // first, zero out the offset bits
873573Sgblack@eecs.umich.edu        makeLineAddress();
883573Sgblack@eecs.umich.edu        m_address |= (physical_address_t) offset;
893576Sgblack@eecs.umich.edu    }
903576Sgblack@eecs.umich.edu
913576Sgblack@eecs.umich.edu  private:
923576Sgblack@eecs.umich.edu    physical_address_t m_address;
933573Sgblack@eecs.umich.edu};
943573Sgblack@eecs.umich.edu
953576Sgblack@eecs.umich.eduinline Address
963576Sgblack@eecs.umich.eduline_address(const Address& addr)
977678Sgblack@eecs.umich.edu{
987678Sgblack@eecs.umich.edu    Address temp(addr);
997678Sgblack@eecs.umich.edu    temp.makeLineAddress();
1007678Sgblack@eecs.umich.edu    return temp;
1013576Sgblack@eecs.umich.edu}
1023576Sgblack@eecs.umich.edu
1033576Sgblack@eecs.umich.eduinline bool
1043576Sgblack@eecs.umich.eduoperator<(const Address& obj1, const Address& obj2)
1053576Sgblack@eecs.umich.edu{
1063576Sgblack@eecs.umich.edu    return obj1.getAddress() < obj2.getAddress();
1073576Sgblack@eecs.umich.edu}
1083576Sgblack@eecs.umich.edu
1093576Sgblack@eecs.umich.eduinline std::ostream&
1103576Sgblack@eecs.umich.eduoperator<<(std::ostream& out, const Address& obj)
1113576Sgblack@eecs.umich.edu{
1123576Sgblack@eecs.umich.edu    obj.print(out);
1133576Sgblack@eecs.umich.edu    out << std::flush;
1143576Sgblack@eecs.umich.edu    return out;
1153576Sgblack@eecs.umich.edu}
1163576Sgblack@eecs.umich.edu
1173576Sgblack@eecs.umich.eduinline bool
1183576Sgblack@eecs.umich.eduoperator==(const Address& obj1, const Address& obj2)
1193576Sgblack@eecs.umich.edu{
1203576Sgblack@eecs.umich.edu    return (obj1.getAddress() == obj2.getAddress());
1213576Sgblack@eecs.umich.edu}
1223576Sgblack@eecs.umich.edu
1233576Sgblack@eecs.umich.eduinline bool
1243576Sgblack@eecs.umich.eduoperator!=(const Address& obj1, const Address& obj2)
1253576Sgblack@eecs.umich.edu{
1263576Sgblack@eecs.umich.edu    return (obj1.getAddress() != obj2.getAddress());
1273576Sgblack@eecs.umich.edu}
1283576Sgblack@eecs.umich.edu
1293576Sgblack@eecs.umich.edu// rips bits inclusive
1303576Sgblack@eecs.umich.eduinline physical_address_t
1313576Sgblack@eecs.umich.eduAddress::bitSelect(int small, int big) const
1323576Sgblack@eecs.umich.edu{
1333576Sgblack@eecs.umich.edu    physical_address_t mask;
1343576Sgblack@eecs.umich.edu    assert((unsigned)big >= (unsigned)small);
1353576Sgblack@eecs.umich.edu
1363576Sgblack@eecs.umich.edu    if (big >= ADDRESS_WIDTH - 1) {
1373576Sgblack@eecs.umich.edu        return (m_address >> small);
1383576Sgblack@eecs.umich.edu    } else {
1393573Sgblack@eecs.umich.edu        mask = ~((physical_address_t)~0 << (big + 1));
1403573Sgblack@eecs.umich.edu        // FIXME - this is slow to manipulate a 64-bit number using 32-bits
1413573Sgblack@eecs.umich.edu        physical_address_t partial = (m_address & mask);
1423573Sgblack@eecs.umich.edu        return (partial >> small);
1434695Sgblack@eecs.umich.edu    }
1442221SN/A}
1452221SN/A
1463576Sgblack@eecs.umich.edu// removes bits inclusive
1473576Sgblack@eecs.umich.eduinline physical_address_t
1483576Sgblack@eecs.umich.eduAddress::bitRemove(int small, int big) const
1493576Sgblack@eecs.umich.edu{
1503576Sgblack@eecs.umich.edu    physical_address_t mask;
1513576Sgblack@eecs.umich.edu    assert((unsigned)big >= (unsigned)small);
1523576Sgblack@eecs.umich.edu
1533576Sgblack@eecs.umich.edu    if (small >= ADDRESS_WIDTH - 1) {
1543576Sgblack@eecs.umich.edu        return m_address;
1553576Sgblack@eecs.umich.edu    } else if (big >= ADDRESS_WIDTH - 1) {
1563576Sgblack@eecs.umich.edu        mask = (physical_address_t)~0 >> small;
1573576Sgblack@eecs.umich.edu        return (m_address & mask);
1583573Sgblack@eecs.umich.edu    } else if (small == 0) {
1593573Sgblack@eecs.umich.edu        mask = (physical_address_t)~0 << big;
1602221SN/A        return (m_address & mask);
1612221SN/A    } else {
1624695Sgblack@eecs.umich.edu        mask = ~((physical_address_t)~0 << small);
1632221SN/A        physical_address_t lower_bits = m_address & mask;
1642221SN/A        mask = (physical_address_t)~0 << (big + 1);
1653576Sgblack@eecs.umich.edu        physical_address_t higher_bits = m_address & mask;
1663576Sgblack@eecs.umich.edu
1673576Sgblack@eecs.umich.edu        // Shift the valid high bits over the removed section
1683576Sgblack@eecs.umich.edu        higher_bits = higher_bits >> (big - small + 1);
1693576Sgblack@eecs.umich.edu        return (higher_bits | lower_bits);
1703576Sgblack@eecs.umich.edu    }
1713576Sgblack@eecs.umich.edu}
1723576Sgblack@eecs.umich.edu
1733576Sgblack@eecs.umich.eduinline physical_address_t
1743576Sgblack@eecs.umich.eduAddress::maskLowOrderBits(int number) const
1753576Sgblack@eecs.umich.edu{
1763576Sgblack@eecs.umich.edu  physical_address_t mask;
1773576Sgblack@eecs.umich.edu
1783576Sgblack@eecs.umich.edu  if (number >= ADDRESS_WIDTH - 1) {
1793576Sgblack@eecs.umich.edu      mask = ~0;
1803576Sgblack@eecs.umich.edu  } else {
1813576Sgblack@eecs.umich.edu      mask = (physical_address_t)~0 << number;
1823576Sgblack@eecs.umich.edu  }
1833576Sgblack@eecs.umich.edu  return (m_address & mask);
1843576Sgblack@eecs.umich.edu}
1853576Sgblack@eecs.umich.edu
1863576Sgblack@eecs.umich.eduinline physical_address_t
1873576Sgblack@eecs.umich.eduAddress::maskHighOrderBits(int number) const
1883576Sgblack@eecs.umich.edu{
1893576Sgblack@eecs.umich.edu    physical_address_t mask;
1903576Sgblack@eecs.umich.edu
1913576Sgblack@eecs.umich.edu    if (number >= ADDRESS_WIDTH - 1) {
1923576Sgblack@eecs.umich.edu        mask = ~0;
1933576Sgblack@eecs.umich.edu    } else {
1943576Sgblack@eecs.umich.edu        mask = (physical_address_t)~0 >> number;
1953576Sgblack@eecs.umich.edu    }
1963576Sgblack@eecs.umich.edu    return (m_address & mask);
1973576Sgblack@eecs.umich.edu}
1983576Sgblack@eecs.umich.edu
1993576Sgblack@eecs.umich.eduinline physical_address_t
2003576Sgblack@eecs.umich.eduAddress::shiftLowOrderBits(int number) const
2013576Sgblack@eecs.umich.edu{
2023576Sgblack@eecs.umich.edu    return (m_address >> number);
2034103Ssaidi@eecs.umich.edu}
2044103Ssaidi@eecs.umich.edu
2053576Sgblack@eecs.umich.educlass Address;
2063576Sgblack@eecs.umich.edunamespace __hash_namespace {
2073576Sgblack@eecs.umich.edutemplate <> struct hash<Address>
2083576Sgblack@eecs.umich.edu{
2093576Sgblack@eecs.umich.edu    size_t
2104997Sgblack@eecs.umich.edu    operator()(const Address &s) const
2114997Sgblack@eecs.umich.edu    {
2124997Sgblack@eecs.umich.edu        return (size_t)s.getAddress();
2134997Sgblack@eecs.umich.edu    }
2144997Sgblack@eecs.umich.edu};
2154997Sgblack@eecs.umich.edu} // namespace __hash_namespace
2164997Sgblack@eecs.umich.edu
2174997Sgblack@eecs.umich.edunamespace std {
2187678Sgblack@eecs.umich.edutemplate <> struct equal_to<Address>
2197678Sgblack@eecs.umich.edu{
2204997Sgblack@eecs.umich.edu    bool
2214997Sgblack@eecs.umich.edu    operator()(const Address& s1, const Address& s2) const
2223576Sgblack@eecs.umich.edu    {
2234997Sgblack@eecs.umich.edu        return s1 == s2;
2244997Sgblack@eecs.umich.edu    }
2254997Sgblack@eecs.umich.edu};
2264997Sgblack@eecs.umich.edu} // namespace std
2274997Sgblack@eecs.umich.edu
2284997Sgblack@eecs.umich.edu#endif // __MEM_RUBY_COMMON_ADDRESS_HH__
2294997Sgblack@eecs.umich.edu