Address.hh revision 9362
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>
348946Sandreas.hansson@arm.com#include <iostream>
357002Snate@binkert.org
367002Snate@binkert.org#include "base/hashmap.hh"
378608Snilay@cs.wisc.edu#include "mem/ruby/common/TypeDefines.hh"
386145Snate@binkert.org
399362Snilay@cs.wisc.educonst uint32_t ADDRESS_WIDTH = 64; // address width in bytes
406145Snate@binkert.org
416145Snate@binkert.orgclass Address;
426145Snate@binkert.orgtypedef Address PhysAddress;
436145Snate@binkert.orgtypedef Address VirtAddress;
446145Snate@binkert.org
457039Snate@binkert.orgclass Address
467039Snate@binkert.org{
477039Snate@binkert.org  public:
487039Snate@binkert.org    Address()
497039Snate@binkert.org        : m_address(0)
507039Snate@binkert.org    { }
516145Snate@binkert.org
527039Snate@binkert.org    explicit
537039Snate@binkert.org    Address(physical_address_t address)
547039Snate@binkert.org        : m_address(address)
557039Snate@binkert.org    { }
566145Snate@binkert.org
577039Snate@binkert.org    Address(const Address& obj);
587039Snate@binkert.org    Address& operator=(const Address& obj);
596145Snate@binkert.org
607039Snate@binkert.org    void setAddress(physical_address_t address) { m_address = address; }
617039Snate@binkert.org    physical_address_t getAddress() const {return m_address;}
627039Snate@binkert.org    // selects bits inclusive
637039Snate@binkert.org    physical_address_t bitSelect(int small, int big) const;
647039Snate@binkert.org    physical_address_t bitRemove(int small, int big) const;
657039Snate@binkert.org    physical_address_t maskLowOrderBits(int number) const;
667039Snate@binkert.org    physical_address_t maskHighOrderBits(int number) const;
677039Snate@binkert.org    physical_address_t shiftLowOrderBits(int number) const;
686145Snate@binkert.org
698091Snilay@cs.wisc.edu    physical_address_t getLineAddress() const;
708091Snilay@cs.wisc.edu    physical_address_t getOffset() const;
718091Snilay@cs.wisc.edu    void makeLineAddress();
729362Snilay@cs.wisc.edu    void makePageAddress();
738091Snilay@cs.wisc.edu    void makeNextStrideAddress(int stride);
746145Snate@binkert.org
757039Snate@binkert.org    int getBankSetNum() const;
767039Snate@binkert.org    int getBankSetDist() const;
776145Snate@binkert.org
787039Snate@binkert.org    Index memoryModuleIndex() const;
796145Snate@binkert.org
807055Snate@binkert.org    void print(std::ostream& out) const;
817055Snate@binkert.org    void output(std::ostream& out) const;
827055Snate@binkert.org    void input(std::istream& in);
836145Snate@binkert.org
847039Snate@binkert.org    void
857039Snate@binkert.org    setOffset(int offset)
867039Snate@binkert.org    {
877039Snate@binkert.org        // first, zero out the offset bits
887039Snate@binkert.org        makeLineAddress();
897039Snate@binkert.org        m_address |= (physical_address_t) offset;
907039Snate@binkert.org    }
917039Snate@binkert.org
927039Snate@binkert.org  private:
937039Snate@binkert.org    physical_address_t m_address;
946145Snate@binkert.org};
956145Snate@binkert.org
967039Snate@binkert.orginline Address
977039Snate@binkert.orgline_address(const Address& addr)
986145Snate@binkert.org{
997039Snate@binkert.org    Address temp(addr);
1007039Snate@binkert.org    temp.makeLineAddress();
1017039Snate@binkert.org    return temp;
1026145Snate@binkert.org}
1036145Snate@binkert.org
1047039Snate@binkert.orginline bool
1057039Snate@binkert.orgoperator<(const Address& obj1, const Address& obj2)
1066145Snate@binkert.org{
1077039Snate@binkert.org    return obj1.getAddress() < obj2.getAddress();
1086145Snate@binkert.org}
1096145Snate@binkert.org
1107055Snate@binkert.orginline std::ostream&
1117055Snate@binkert.orgoperator<<(std::ostream& out, const Address& obj)
1126145Snate@binkert.org{
1137039Snate@binkert.org    obj.print(out);
1147055Snate@binkert.org    out << std::flush;
1157039Snate@binkert.org    return out;
1166145Snate@binkert.org}
1176145Snate@binkert.org
1187039Snate@binkert.orginline bool
1197039Snate@binkert.orgoperator==(const Address& obj1, const Address& obj2)
1206145Snate@binkert.org{
1217039Snate@binkert.org    return (obj1.getAddress() == obj2.getAddress());
1226145Snate@binkert.org}
1236145Snate@binkert.org
1247039Snate@binkert.orginline bool
1257039Snate@binkert.orgoperator!=(const Address& obj1, const Address& obj2)
1266145Snate@binkert.org{
1277039Snate@binkert.org    return (obj1.getAddress() != obj2.getAddress());
1287039Snate@binkert.org}
1296145Snate@binkert.org
1307039Snate@binkert.org// rips bits inclusive
1317039Snate@binkert.orginline physical_address_t
1327039Snate@binkert.orgAddress::bitSelect(int small, int big) const
1337039Snate@binkert.org{
1347039Snate@binkert.org    physical_address_t mask;
1357039Snate@binkert.org    assert((unsigned)big >= (unsigned)small);
1367039Snate@binkert.org
1377039Snate@binkert.org    if (big >= ADDRESS_WIDTH - 1) {
1387039Snate@binkert.org        return (m_address >> small);
1397039Snate@binkert.org    } else {
1407039Snate@binkert.org        mask = ~((physical_address_t)~0 << (big + 1));
1417039Snate@binkert.org        // FIXME - this is slow to manipulate a 64-bit number using 32-bits
1427039Snate@binkert.org        physical_address_t partial = (m_address & mask);
1437039Snate@binkert.org        return (partial >> small);
1447039Snate@binkert.org    }
1456145Snate@binkert.org}
1466145Snate@binkert.org
1477027SBrad.Beckmann@amd.com// removes bits inclusive
1487039Snate@binkert.orginline physical_address_t
1497039Snate@binkert.orgAddress::bitRemove(int small, int big) const
1507027SBrad.Beckmann@amd.com{
1517027SBrad.Beckmann@amd.com    physical_address_t mask;
1527027SBrad.Beckmann@amd.com    assert((unsigned)big >= (unsigned)small);
1537054Snate@binkert.org
1547027SBrad.Beckmann@amd.com    if (small >= ADDRESS_WIDTH - 1) {
1557027SBrad.Beckmann@amd.com        return m_address;
1567027SBrad.Beckmann@amd.com    } else if (big >= ADDRESS_WIDTH - 1) {
1577027SBrad.Beckmann@amd.com        mask = (physical_address_t)~0 >> small;
1587027SBrad.Beckmann@amd.com        return (m_address & mask);
1597027SBrad.Beckmann@amd.com    } else if (small == 0) {
1607027SBrad.Beckmann@amd.com        mask = (physical_address_t)~0 << big;
1617027SBrad.Beckmann@amd.com        return (m_address & mask);
1627027SBrad.Beckmann@amd.com    } else {
1637027SBrad.Beckmann@amd.com        mask = ~((physical_address_t)~0 << small);
1647027SBrad.Beckmann@amd.com        physical_address_t lower_bits = m_address & mask;
1657027SBrad.Beckmann@amd.com        mask = (physical_address_t)~0 << (big + 1);
1667027SBrad.Beckmann@amd.com        physical_address_t higher_bits = m_address & mask;
1677027SBrad.Beckmann@amd.com
1687027SBrad.Beckmann@amd.com        // Shift the valid high bits over the removed section
1697563SBrad.Beckmann@amd.com        higher_bits = higher_bits >> (big - small + 1);
1707027SBrad.Beckmann@amd.com        return (higher_bits | lower_bits);
1717027SBrad.Beckmann@amd.com    }
1727027SBrad.Beckmann@amd.com}
1737027SBrad.Beckmann@amd.com
1747039Snate@binkert.orginline physical_address_t
1757039Snate@binkert.orgAddress::maskLowOrderBits(int number) const
1766145Snate@binkert.org{
1776145Snate@binkert.org  physical_address_t mask;
1786145Snate@binkert.org
1796145Snate@binkert.org  if (number >= ADDRESS_WIDTH - 1) {
1807039Snate@binkert.org      mask = ~0;
1816145Snate@binkert.org  } else {
1827039Snate@binkert.org      mask = (physical_address_t)~0 << number;
1836145Snate@binkert.org  }
1846145Snate@binkert.org  return (m_address & mask);
1856145Snate@binkert.org}
1866145Snate@binkert.org
1877039Snate@binkert.orginline physical_address_t
1887039Snate@binkert.orgAddress::maskHighOrderBits(int number) const
1896145Snate@binkert.org{
1907039Snate@binkert.org    physical_address_t mask;
1916145Snate@binkert.org
1927039Snate@binkert.org    if (number >= ADDRESS_WIDTH - 1) {
1937039Snate@binkert.org        mask = ~0;
1947039Snate@binkert.org    } else {
1957039Snate@binkert.org        mask = (physical_address_t)~0 >> number;
1967039Snate@binkert.org    }
1977039Snate@binkert.org    return (m_address & mask);
1986145Snate@binkert.org}
1996145Snate@binkert.org
2007039Snate@binkert.orginline physical_address_t
2017039Snate@binkert.orgAddress::shiftLowOrderBits(int number) const
2026145Snate@binkert.org{
2037039Snate@binkert.org    return (m_address >> number);
2046145Snate@binkert.org}
2056145Snate@binkert.org
2069362Snilay@cs.wisc.eduAddress next_stride_address(const Address& addr, int stride);
2079362Snilay@cs.wisc.eduAddress page_address(const Address& addr);
2089362Snilay@cs.wisc.edu
2098946Sandreas.hansson@arm.com__hash_namespace_begin
2107039Snate@binkert.orgtemplate <> struct hash<Address>
2117039Snate@binkert.org{
2127039Snate@binkert.org    size_t
2137039Snate@binkert.org    operator()(const Address &s) const
2147039Snate@binkert.org    {
2157039Snate@binkert.org        return (size_t)s.getAddress();
2167039Snate@binkert.org    }
2177039Snate@binkert.org};
2188946Sandreas.hansson@arm.com__hash_namespace_end
2197039Snate@binkert.org
2206145Snate@binkert.orgnamespace std {
2217039Snate@binkert.orgtemplate <> struct equal_to<Address>
2227039Snate@binkert.org{
2237039Snate@binkert.org    bool
2247039Snate@binkert.org    operator()(const Address& s1, const Address& s2) const
2257039Snate@binkert.org    {
2267039Snate@binkert.org        return s1 == s2;
2277039Snate@binkert.org    }
2287039Snate@binkert.org};
2297811Ssteve.reinhardt@amd.com} // namespace std
2306145Snate@binkert.org
2317039Snate@binkert.org#endif // __MEM_RUBY_COMMON_ADDRESS_HH__
232