Address.hh revision 8946
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 396145Snate@binkert.orgconst int 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(); 728091Snilay@cs.wisc.edu void makeNextStrideAddress(int stride); 736145Snate@binkert.org 747039Snate@binkert.org int getBankSetNum() const; 757039Snate@binkert.org int getBankSetDist() const; 766145Snate@binkert.org 777039Snate@binkert.org Index memoryModuleIndex() const; 786145Snate@binkert.org 797055Snate@binkert.org void print(std::ostream& out) const; 807055Snate@binkert.org void output(std::ostream& out) const; 817055Snate@binkert.org void input(std::istream& in); 826145Snate@binkert.org 837039Snate@binkert.org void 847039Snate@binkert.org setOffset(int offset) 857039Snate@binkert.org { 867039Snate@binkert.org // first, zero out the offset bits 877039Snate@binkert.org makeLineAddress(); 887039Snate@binkert.org m_address |= (physical_address_t) offset; 897039Snate@binkert.org } 907039Snate@binkert.org 917039Snate@binkert.org private: 927039Snate@binkert.org physical_address_t m_address; 936145Snate@binkert.org}; 946145Snate@binkert.org 957039Snate@binkert.orginline Address 967039Snate@binkert.orgline_address(const Address& addr) 976145Snate@binkert.org{ 987039Snate@binkert.org Address temp(addr); 997039Snate@binkert.org temp.makeLineAddress(); 1007039Snate@binkert.org return temp; 1016145Snate@binkert.org} 1026145Snate@binkert.org 1037039Snate@binkert.orginline bool 1047039Snate@binkert.orgoperator<(const Address& obj1, const Address& obj2) 1056145Snate@binkert.org{ 1067039Snate@binkert.org return obj1.getAddress() < obj2.getAddress(); 1076145Snate@binkert.org} 1086145Snate@binkert.org 1097055Snate@binkert.orginline std::ostream& 1107055Snate@binkert.orgoperator<<(std::ostream& out, const Address& obj) 1116145Snate@binkert.org{ 1127039Snate@binkert.org obj.print(out); 1137055Snate@binkert.org out << std::flush; 1147039Snate@binkert.org return out; 1156145Snate@binkert.org} 1166145Snate@binkert.org 1177039Snate@binkert.orginline bool 1187039Snate@binkert.orgoperator==(const Address& obj1, const Address& obj2) 1196145Snate@binkert.org{ 1207039Snate@binkert.org return (obj1.getAddress() == obj2.getAddress()); 1216145Snate@binkert.org} 1226145Snate@binkert.org 1237039Snate@binkert.orginline bool 1247039Snate@binkert.orgoperator!=(const Address& obj1, const Address& obj2) 1256145Snate@binkert.org{ 1267039Snate@binkert.org return (obj1.getAddress() != obj2.getAddress()); 1277039Snate@binkert.org} 1286145Snate@binkert.org 1297039Snate@binkert.org// rips bits inclusive 1307039Snate@binkert.orginline physical_address_t 1317039Snate@binkert.orgAddress::bitSelect(int small, int big) const 1327039Snate@binkert.org{ 1337039Snate@binkert.org physical_address_t mask; 1347039Snate@binkert.org assert((unsigned)big >= (unsigned)small); 1357039Snate@binkert.org 1367039Snate@binkert.org if (big >= ADDRESS_WIDTH - 1) { 1377039Snate@binkert.org return (m_address >> small); 1387039Snate@binkert.org } else { 1397039Snate@binkert.org mask = ~((physical_address_t)~0 << (big + 1)); 1407039Snate@binkert.org // FIXME - this is slow to manipulate a 64-bit number using 32-bits 1417039Snate@binkert.org physical_address_t partial = (m_address & mask); 1427039Snate@binkert.org return (partial >> small); 1437039Snate@binkert.org } 1446145Snate@binkert.org} 1456145Snate@binkert.org 1467027SBrad.Beckmann@amd.com// removes bits inclusive 1477039Snate@binkert.orginline physical_address_t 1487039Snate@binkert.orgAddress::bitRemove(int small, int big) const 1497027SBrad.Beckmann@amd.com{ 1507027SBrad.Beckmann@amd.com physical_address_t mask; 1517027SBrad.Beckmann@amd.com assert((unsigned)big >= (unsigned)small); 1527054Snate@binkert.org 1537027SBrad.Beckmann@amd.com if (small >= ADDRESS_WIDTH - 1) { 1547027SBrad.Beckmann@amd.com return m_address; 1557027SBrad.Beckmann@amd.com } else if (big >= ADDRESS_WIDTH - 1) { 1567027SBrad.Beckmann@amd.com mask = (physical_address_t)~0 >> small; 1577027SBrad.Beckmann@amd.com return (m_address & mask); 1587027SBrad.Beckmann@amd.com } else if (small == 0) { 1597027SBrad.Beckmann@amd.com mask = (physical_address_t)~0 << big; 1607027SBrad.Beckmann@amd.com return (m_address & mask); 1617027SBrad.Beckmann@amd.com } else { 1627027SBrad.Beckmann@amd.com mask = ~((physical_address_t)~0 << small); 1637027SBrad.Beckmann@amd.com physical_address_t lower_bits = m_address & mask; 1647027SBrad.Beckmann@amd.com mask = (physical_address_t)~0 << (big + 1); 1657027SBrad.Beckmann@amd.com physical_address_t higher_bits = m_address & mask; 1667027SBrad.Beckmann@amd.com 1677027SBrad.Beckmann@amd.com // Shift the valid high bits over the removed section 1687563SBrad.Beckmann@amd.com higher_bits = higher_bits >> (big - small + 1); 1697027SBrad.Beckmann@amd.com return (higher_bits | lower_bits); 1707027SBrad.Beckmann@amd.com } 1717027SBrad.Beckmann@amd.com} 1727027SBrad.Beckmann@amd.com 1737039Snate@binkert.orginline physical_address_t 1747039Snate@binkert.orgAddress::maskLowOrderBits(int number) const 1756145Snate@binkert.org{ 1766145Snate@binkert.org physical_address_t mask; 1776145Snate@binkert.org 1786145Snate@binkert.org if (number >= ADDRESS_WIDTH - 1) { 1797039Snate@binkert.org mask = ~0; 1806145Snate@binkert.org } else { 1817039Snate@binkert.org mask = (physical_address_t)~0 << number; 1826145Snate@binkert.org } 1836145Snate@binkert.org return (m_address & mask); 1846145Snate@binkert.org} 1856145Snate@binkert.org 1867039Snate@binkert.orginline physical_address_t 1877039Snate@binkert.orgAddress::maskHighOrderBits(int number) const 1886145Snate@binkert.org{ 1897039Snate@binkert.org physical_address_t mask; 1906145Snate@binkert.org 1917039Snate@binkert.org if (number >= ADDRESS_WIDTH - 1) { 1927039Snate@binkert.org mask = ~0; 1937039Snate@binkert.org } else { 1947039Snate@binkert.org mask = (physical_address_t)~0 >> number; 1957039Snate@binkert.org } 1967039Snate@binkert.org return (m_address & mask); 1976145Snate@binkert.org} 1986145Snate@binkert.org 1997039Snate@binkert.orginline physical_address_t 2007039Snate@binkert.orgAddress::shiftLowOrderBits(int number) const 2016145Snate@binkert.org{ 2027039Snate@binkert.org return (m_address >> number); 2036145Snate@binkert.org} 2046145Snate@binkert.org 2058946Sandreas.hansson@arm.com__hash_namespace_begin 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}; 2148946Sandreas.hansson@arm.com__hash_namespace_end 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