Address.hh revision 8608
16145Snate@binkert.org/*
26239Snate@binkert.org * Copyright (c) 1999 Mark D. Hill and David A. Wood
36239Snate@binkert.org * All rights reserved.
46145Snate@binkert.org *
56239Snate@binkert.org * Redistribution and use in source and binary forms, with or without
66239Snate@binkert.org * modification, are permitted provided that the following conditions are
76239Snate@binkert.org * met: redistributions of source code must retain the above copyright
86239Snate@binkert.org * notice, this list of conditions and the following disclaimer;
96239Snate@binkert.org * redistributions in binary form must reproduce the above copyright
106239Snate@binkert.org * notice, this list of conditions and the following disclaimer in the
116239Snate@binkert.org * documentation and/or other materials provided with the distribution;
126239Snate@binkert.org * neither the name of the copyright holders nor the names of its
136239Snate@binkert.org * contributors may be used to endorse or promote products derived from
146239Snate@binkert.org * this software without specific prior written permission.
156145Snate@binkert.org *
166239Snate@binkert.org * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
176239Snate@binkert.org * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
186239Snate@binkert.org * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
196239Snate@binkert.org * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
206239Snate@binkert.org * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
216239Snate@binkert.org * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
226239Snate@binkert.org * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
236239Snate@binkert.org * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
246239Snate@binkert.org * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
256239Snate@binkert.org * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
266239Snate@binkert.org * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
276145Snate@binkert.org */
286145Snate@binkert.org
297039Snate@binkert.org#ifndef __MEM_RUBY_COMMON_ADDRESS_HH__
307039Snate@binkert.org#define __MEM_RUBY_COMMON_ADDRESS_HH__
316145Snate@binkert.org
328091Snilay@cs.wisc.edu#include <cassert>
336145Snate@binkert.org#include <iomanip>
347002Snate@binkert.org
357002Snate@binkert.org#include "base/hashmap.hh"
368608Snilay@cs.wisc.edu#include "mem/ruby/common/TypeDefines.hh"
376145Snate@binkert.org
386145Snate@binkert.orgconst int ADDRESS_WIDTH = 64; // address width in bytes
396145Snate@binkert.org
406145Snate@binkert.orgclass Address;
416145Snate@binkert.orgtypedef Address PhysAddress;
426145Snate@binkert.orgtypedef Address VirtAddress;
436145Snate@binkert.org
447039Snate@binkert.orgclass Address
457039Snate@binkert.org{
467039Snate@binkert.org  public:
477039Snate@binkert.org    Address()
487039Snate@binkert.org        : m_address(0)
497039Snate@binkert.org    { }
506145Snate@binkert.org
517039Snate@binkert.org    explicit
527039Snate@binkert.org    Address(physical_address_t address)
537039Snate@binkert.org        : m_address(address)
547039Snate@binkert.org    { }
556145Snate@binkert.org
567039Snate@binkert.org    Address(const Address& obj);
577039Snate@binkert.org    Address& operator=(const Address& obj);
586145Snate@binkert.org
597039Snate@binkert.org    void setAddress(physical_address_t address) { m_address = address; }
607039Snate@binkert.org    physical_address_t getAddress() const {return m_address;}
617039Snate@binkert.org    // selects bits inclusive
627039Snate@binkert.org    physical_address_t bitSelect(int small, int big) const;
637039Snate@binkert.org    physical_address_t bitRemove(int small, int big) const;
647039Snate@binkert.org    physical_address_t maskLowOrderBits(int number) const;
657039Snate@binkert.org    physical_address_t maskHighOrderBits(int number) const;
667039Snate@binkert.org    physical_address_t shiftLowOrderBits(int number) const;
676145Snate@binkert.org
688091Snilay@cs.wisc.edu    physical_address_t getLineAddress() const;
698091Snilay@cs.wisc.edu    physical_address_t getOffset() const;
708091Snilay@cs.wisc.edu    void makeLineAddress();
718091Snilay@cs.wisc.edu    void makeNextStrideAddress(int stride);
726145Snate@binkert.org
737039Snate@binkert.org    int getBankSetNum() const;
747039Snate@binkert.org    int getBankSetDist() const;
756145Snate@binkert.org
767039Snate@binkert.org    Index memoryModuleIndex() const;
776145Snate@binkert.org
787055Snate@binkert.org    void print(std::ostream& out) const;
797055Snate@binkert.org    void output(std::ostream& out) const;
807055Snate@binkert.org    void input(std::istream& in);
816145Snate@binkert.org
827039Snate@binkert.org    void
837039Snate@binkert.org    setOffset(int offset)
847039Snate@binkert.org    {
857039Snate@binkert.org        // first, zero out the offset bits
867039Snate@binkert.org        makeLineAddress();
877039Snate@binkert.org        m_address |= (physical_address_t) offset;
887039Snate@binkert.org    }
897039Snate@binkert.org
907039Snate@binkert.org  private:
917039Snate@binkert.org    physical_address_t m_address;
926145Snate@binkert.org};
936145Snate@binkert.org
947039Snate@binkert.orginline Address
957039Snate@binkert.orgline_address(const Address& addr)
966145Snate@binkert.org{
977039Snate@binkert.org    Address temp(addr);
987039Snate@binkert.org    temp.makeLineAddress();
997039Snate@binkert.org    return temp;
1006145Snate@binkert.org}
1016145Snate@binkert.org
1027039Snate@binkert.orginline bool
1037039Snate@binkert.orgoperator<(const Address& obj1, const Address& obj2)
1046145Snate@binkert.org{
1057039Snate@binkert.org    return obj1.getAddress() < obj2.getAddress();
1066145Snate@binkert.org}
1076145Snate@binkert.org
1087055Snate@binkert.orginline std::ostream&
1097055Snate@binkert.orgoperator<<(std::ostream& out, const Address& obj)
1106145Snate@binkert.org{
1117039Snate@binkert.org    obj.print(out);
1127055Snate@binkert.org    out << std::flush;
1137039Snate@binkert.org    return out;
1146145Snate@binkert.org}
1156145Snate@binkert.org
1167039Snate@binkert.orginline bool
1177039Snate@binkert.orgoperator==(const Address& obj1, const Address& obj2)
1186145Snate@binkert.org{
1197039Snate@binkert.org    return (obj1.getAddress() == obj2.getAddress());
1206145Snate@binkert.org}
1216145Snate@binkert.org
1227039Snate@binkert.orginline bool
1237039Snate@binkert.orgoperator!=(const Address& obj1, const Address& obj2)
1246145Snate@binkert.org{
1257039Snate@binkert.org    return (obj1.getAddress() != obj2.getAddress());
1267039Snate@binkert.org}
1276145Snate@binkert.org
1287039Snate@binkert.org// rips bits inclusive
1297039Snate@binkert.orginline physical_address_t
1307039Snate@binkert.orgAddress::bitSelect(int small, int big) const
1317039Snate@binkert.org{
1327039Snate@binkert.org    physical_address_t mask;
1337039Snate@binkert.org    assert((unsigned)big >= (unsigned)small);
1347039Snate@binkert.org
1357039Snate@binkert.org    if (big >= ADDRESS_WIDTH - 1) {
1367039Snate@binkert.org        return (m_address >> small);
1377039Snate@binkert.org    } else {
1387039Snate@binkert.org        mask = ~((physical_address_t)~0 << (big + 1));
1397039Snate@binkert.org        // FIXME - this is slow to manipulate a 64-bit number using 32-bits
1407039Snate@binkert.org        physical_address_t partial = (m_address & mask);
1417039Snate@binkert.org        return (partial >> small);
1427039Snate@binkert.org    }
1436145Snate@binkert.org}
1446145Snate@binkert.org
1457027SBrad.Beckmann@amd.com// removes bits inclusive
1467039Snate@binkert.orginline physical_address_t
1477039Snate@binkert.orgAddress::bitRemove(int small, int big) const
1487027SBrad.Beckmann@amd.com{
1497027SBrad.Beckmann@amd.com    physical_address_t mask;
1507027SBrad.Beckmann@amd.com    assert((unsigned)big >= (unsigned)small);
1517054Snate@binkert.org
1527027SBrad.Beckmann@amd.com    if (small >= ADDRESS_WIDTH - 1) {
1537027SBrad.Beckmann@amd.com        return m_address;
1547027SBrad.Beckmann@amd.com    } else if (big >= ADDRESS_WIDTH - 1) {
1557027SBrad.Beckmann@amd.com        mask = (physical_address_t)~0 >> small;
1567027SBrad.Beckmann@amd.com        return (m_address & mask);
1577027SBrad.Beckmann@amd.com    } else if (small == 0) {
1587027SBrad.Beckmann@amd.com        mask = (physical_address_t)~0 << big;
1597027SBrad.Beckmann@amd.com        return (m_address & mask);
1607027SBrad.Beckmann@amd.com    } else {
1617027SBrad.Beckmann@amd.com        mask = ~((physical_address_t)~0 << small);
1627027SBrad.Beckmann@amd.com        physical_address_t lower_bits = m_address & mask;
1637027SBrad.Beckmann@amd.com        mask = (physical_address_t)~0 << (big + 1);
1647027SBrad.Beckmann@amd.com        physical_address_t higher_bits = m_address & mask;
1657027SBrad.Beckmann@amd.com
1667027SBrad.Beckmann@amd.com        // Shift the valid high bits over the removed section
1677563SBrad.Beckmann@amd.com        higher_bits = higher_bits >> (big - small + 1);
1687027SBrad.Beckmann@amd.com        return (higher_bits | lower_bits);
1697027SBrad.Beckmann@amd.com    }
1707027SBrad.Beckmann@amd.com}
1717027SBrad.Beckmann@amd.com
1727039Snate@binkert.orginline physical_address_t
1737039Snate@binkert.orgAddress::maskLowOrderBits(int number) const
1746145Snate@binkert.org{
1756145Snate@binkert.org  physical_address_t mask;
1766145Snate@binkert.org
1776145Snate@binkert.org  if (number >= ADDRESS_WIDTH - 1) {
1787039Snate@binkert.org      mask = ~0;
1796145Snate@binkert.org  } else {
1807039Snate@binkert.org      mask = (physical_address_t)~0 << number;
1816145Snate@binkert.org  }
1826145Snate@binkert.org  return (m_address & mask);
1836145Snate@binkert.org}
1846145Snate@binkert.org
1857039Snate@binkert.orginline physical_address_t
1867039Snate@binkert.orgAddress::maskHighOrderBits(int number) const
1876145Snate@binkert.org{
1887039Snate@binkert.org    physical_address_t mask;
1896145Snate@binkert.org
1907039Snate@binkert.org    if (number >= ADDRESS_WIDTH - 1) {
1917039Snate@binkert.org        mask = ~0;
1927039Snate@binkert.org    } else {
1937039Snate@binkert.org        mask = (physical_address_t)~0 >> number;
1947039Snate@binkert.org    }
1957039Snate@binkert.org    return (m_address & mask);
1966145Snate@binkert.org}
1976145Snate@binkert.org
1987039Snate@binkert.orginline physical_address_t
1997039Snate@binkert.orgAddress::shiftLowOrderBits(int number) const
2006145Snate@binkert.org{
2017039Snate@binkert.org    return (m_address >> number);
2026145Snate@binkert.org}
2036145Snate@binkert.org
2046145Snate@binkert.orgclass Address;
2057002Snate@binkert.orgnamespace __hash_namespace {
2067039Snate@binkert.orgtemplate <> struct hash<Address>
2077039Snate@binkert.org{
2087039Snate@binkert.org    size_t
2097039Snate@binkert.org    operator()(const Address &s) const
2107039Snate@binkert.org    {
2117039Snate@binkert.org        return (size_t)s.getAddress();
2127039Snate@binkert.org    }
2137039Snate@binkert.org};
2147811Ssteve.reinhardt@amd.com} // namespace __hash_namespace
2157039Snate@binkert.org
2166145Snate@binkert.orgnamespace std {
2177039Snate@binkert.orgtemplate <> struct equal_to<Address>
2187039Snate@binkert.org{
2197039Snate@binkert.org    bool
2207039Snate@binkert.org    operator()(const Address& s1, const Address& s2) const
2217039Snate@binkert.org    {
2227039Snate@binkert.org        return s1 == s2;
2237039Snate@binkert.org    }
2247039Snate@binkert.org};
2257811Ssteve.reinhardt@amd.com} // namespace std
2266145Snate@binkert.org
2277039Snate@binkert.org#endif // __MEM_RUBY_COMMON_ADDRESS_HH__
228