Address.hh revision 8946
113531Sjairo.balart@metempsy.com/* 214227Sgiacomo.travaglini@arm.com * Copyright (c) 1999 Mark D. Hill and David A. Wood 314227Sgiacomo.travaglini@arm.com * All rights reserved. 414227Sgiacomo.travaglini@arm.com * 514227Sgiacomo.travaglini@arm.com * Redistribution and use in source and binary forms, with or without 614227Sgiacomo.travaglini@arm.com * modification, are permitted provided that the following conditions are 714227Sgiacomo.travaglini@arm.com * met: redistributions of source code must retain the above copyright 814227Sgiacomo.travaglini@arm.com * notice, this list of conditions and the following disclaimer; 914227Sgiacomo.travaglini@arm.com * redistributions in binary form must reproduce the above copyright 1014227Sgiacomo.travaglini@arm.com * notice, this list of conditions and the following disclaimer in the 1114227Sgiacomo.travaglini@arm.com * documentation and/or other materials provided with the distribution; 1214227Sgiacomo.travaglini@arm.com * neither the name of the copyright holders nor the names of its 1314227Sgiacomo.travaglini@arm.com * contributors may be used to endorse or promote products derived from 1413531Sjairo.balart@metempsy.com * this software without specific prior written permission. 1513531Sjairo.balart@metempsy.com * 1613531Sjairo.balart@metempsy.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1713531Sjairo.balart@metempsy.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1813531Sjairo.balart@metempsy.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1913531Sjairo.balart@metempsy.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 2013531Sjairo.balart@metempsy.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2113531Sjairo.balart@metempsy.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 2213531Sjairo.balart@metempsy.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2313531Sjairo.balart@metempsy.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2413531Sjairo.balart@metempsy.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2513531Sjairo.balart@metempsy.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 2613531Sjairo.balart@metempsy.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2713531Sjairo.balart@metempsy.com */ 2813531Sjairo.balart@metempsy.com 2913531Sjairo.balart@metempsy.com#ifndef __MEM_RUBY_COMMON_ADDRESS_HH__ 3013531Sjairo.balart@metempsy.com#define __MEM_RUBY_COMMON_ADDRESS_HH__ 3113531Sjairo.balart@metempsy.com 3213531Sjairo.balart@metempsy.com#include <cassert> 3313531Sjairo.balart@metempsy.com#include <iomanip> 3413531Sjairo.balart@metempsy.com#include <iostream> 3513531Sjairo.balart@metempsy.com 3613531Sjairo.balart@metempsy.com#include "base/hashmap.hh" 3713531Sjairo.balart@metempsy.com#include "mem/ruby/common/TypeDefines.hh" 3813531Sjairo.balart@metempsy.com 3913531Sjairo.balart@metempsy.comconst int ADDRESS_WIDTH = 64; // address width in bytes 4013531Sjairo.balart@metempsy.com 4113531Sjairo.balart@metempsy.comclass Address; 4213531Sjairo.balart@metempsy.comtypedef Address PhysAddress; 4313531Sjairo.balart@metempsy.comtypedef Address VirtAddress; 4413531Sjairo.balart@metempsy.com 4513531Sjairo.balart@metempsy.comclass Address 4613531Sjairo.balart@metempsy.com{ 4713531Sjairo.balart@metempsy.com public: 4813531Sjairo.balart@metempsy.com Address() 4913760Sjairo.balart@metempsy.com : m_address(0) 5013531Sjairo.balart@metempsy.com { } 5113531Sjairo.balart@metempsy.com 5213531Sjairo.balart@metempsy.com explicit 5313531Sjairo.balart@metempsy.com Address(physical_address_t address) 5413531Sjairo.balart@metempsy.com : m_address(address) 5513531Sjairo.balart@metempsy.com { } 5613760Sjairo.balart@metempsy.com 5713531Sjairo.balart@metempsy.com Address(const Address& obj); 5813531Sjairo.balart@metempsy.com Address& operator=(const Address& obj); 5913531Sjairo.balart@metempsy.com 6013531Sjairo.balart@metempsy.com void setAddress(physical_address_t address) { m_address = address; } 6113531Sjairo.balart@metempsy.com physical_address_t getAddress() const {return m_address;} 6213531Sjairo.balart@metempsy.com // selects bits inclusive 6313531Sjairo.balart@metempsy.com physical_address_t bitSelect(int small, int big) const; 6413531Sjairo.balart@metempsy.com physical_address_t bitRemove(int small, int big) const; 6513531Sjairo.balart@metempsy.com physical_address_t maskLowOrderBits(int number) const; 6613826Sgiacomo.travaglini@arm.com physical_address_t maskHighOrderBits(int number) const; 6713826Sgiacomo.travaglini@arm.com physical_address_t shiftLowOrderBits(int number) const; 6813760Sjairo.balart@metempsy.com 6913760Sjairo.balart@metempsy.com physical_address_t getLineAddress() const; 7013760Sjairo.balart@metempsy.com physical_address_t getOffset() const; 7113760Sjairo.balart@metempsy.com void makeLineAddress(); 7213760Sjairo.balart@metempsy.com void makeNextStrideAddress(int stride); 7313760Sjairo.balart@metempsy.com 7413760Sjairo.balart@metempsy.com int getBankSetNum() const; 7513760Sjairo.balart@metempsy.com int getBankSetDist() const; 7613760Sjairo.balart@metempsy.com 7713760Sjairo.balart@metempsy.com Index memoryModuleIndex() const; 7813760Sjairo.balart@metempsy.com 7913760Sjairo.balart@metempsy.com void print(std::ostream& out) const; 8013760Sjairo.balart@metempsy.com void output(std::ostream& out) const; 8113760Sjairo.balart@metempsy.com void input(std::istream& in); 8213760Sjairo.balart@metempsy.com 8313531Sjairo.balart@metempsy.com void 8413760Sjairo.balart@metempsy.com setOffset(int offset) 8513760Sjairo.balart@metempsy.com { 8613760Sjairo.balart@metempsy.com // first, zero out the offset bits 8713760Sjairo.balart@metempsy.com makeLineAddress(); 8813760Sjairo.balart@metempsy.com m_address |= (physical_address_t) offset; 8913760Sjairo.balart@metempsy.com } 9013760Sjairo.balart@metempsy.com 9113760Sjairo.balart@metempsy.com private: 9213760Sjairo.balart@metempsy.com physical_address_t m_address; 9313760Sjairo.balart@metempsy.com}; 9413760Sjairo.balart@metempsy.com 9513760Sjairo.balart@metempsy.cominline Address 9613760Sjairo.balart@metempsy.comline_address(const Address& addr) 9713760Sjairo.balart@metempsy.com{ 9813760Sjairo.balart@metempsy.com Address temp(addr); 9913760Sjairo.balart@metempsy.com temp.makeLineAddress(); 10013760Sjairo.balart@metempsy.com return temp; 10113760Sjairo.balart@metempsy.com} 10213760Sjairo.balart@metempsy.com 10313531Sjairo.balart@metempsy.cominline bool 10413760Sjairo.balart@metempsy.comoperator<(const Address& obj1, const Address& obj2) 10513760Sjairo.balart@metempsy.com{ 10613760Sjairo.balart@metempsy.com return obj1.getAddress() < obj2.getAddress(); 10713760Sjairo.balart@metempsy.com} 10813531Sjairo.balart@metempsy.com 10913760Sjairo.balart@metempsy.cominline std::ostream& 11013760Sjairo.balart@metempsy.comoperator<<(std::ostream& out, const Address& obj) 11113760Sjairo.balart@metempsy.com{ 11213760Sjairo.balart@metempsy.com obj.print(out); 11313531Sjairo.balart@metempsy.com out << std::flush; 11413760Sjairo.balart@metempsy.com return out; 11513760Sjairo.balart@metempsy.com} 11613760Sjairo.balart@metempsy.com 11713760Sjairo.balart@metempsy.cominline bool 11813760Sjairo.balart@metempsy.comoperator==(const Address& obj1, const Address& obj2) 11913531Sjairo.balart@metempsy.com{ 12013760Sjairo.balart@metempsy.com return (obj1.getAddress() == obj2.getAddress()); 12113760Sjairo.balart@metempsy.com} 12213760Sjairo.balart@metempsy.com 12313760Sjairo.balart@metempsy.cominline bool 12413760Sjairo.balart@metempsy.comoperator!=(const Address& obj1, const Address& obj2) 12513760Sjairo.balart@metempsy.com{ 12613531Sjairo.balart@metempsy.com return (obj1.getAddress() != obj2.getAddress()); 12713760Sjairo.balart@metempsy.com} 12813760Sjairo.balart@metempsy.com 12913760Sjairo.balart@metempsy.com// rips bits inclusive 13013760Sjairo.balart@metempsy.cominline physical_address_t 13113760Sjairo.balart@metempsy.comAddress::bitSelect(int small, int big) const 13213760Sjairo.balart@metempsy.com{ 13313760Sjairo.balart@metempsy.com physical_address_t mask; 13413760Sjairo.balart@metempsy.com assert((unsigned)big >= (unsigned)small); 13513760Sjairo.balart@metempsy.com 13613760Sjairo.balart@metempsy.com if (big >= ADDRESS_WIDTH - 1) { 13713760Sjairo.balart@metempsy.com return (m_address >> small); 13813760Sjairo.balart@metempsy.com } else { 13913760Sjairo.balart@metempsy.com mask = ~((physical_address_t)~0 << (big + 1)); 14013760Sjairo.balart@metempsy.com // FIXME - this is slow to manipulate a 64-bit number using 32-bits 14113760Sjairo.balart@metempsy.com physical_address_t partial = (m_address & mask); 14213531Sjairo.balart@metempsy.com return (partial >> small); 14313531Sjairo.balart@metempsy.com } 14413531Sjairo.balart@metempsy.com} 14513760Sjairo.balart@metempsy.com 14613531Sjairo.balart@metempsy.com// removes bits inclusive 14713760Sjairo.balart@metempsy.cominline physical_address_t 14813531Sjairo.balart@metempsy.comAddress::bitRemove(int small, int big) const 14913531Sjairo.balart@metempsy.com{ 15013760Sjairo.balart@metempsy.com physical_address_t mask; 15113531Sjairo.balart@metempsy.com assert((unsigned)big >= (unsigned)small); 15213760Sjairo.balart@metempsy.com 15313531Sjairo.balart@metempsy.com if (small >= ADDRESS_WIDTH - 1) { 15413531Sjairo.balart@metempsy.com return m_address; 15513531Sjairo.balart@metempsy.com } else if (big >= ADDRESS_WIDTH - 1) { 15613531Sjairo.balart@metempsy.com mask = (physical_address_t)~0 >> small; 15713531Sjairo.balart@metempsy.com return (m_address & mask); 15813531Sjairo.balart@metempsy.com } else if (small == 0) { 15913531Sjairo.balart@metempsy.com mask = (physical_address_t)~0 << big; 16013531Sjairo.balart@metempsy.com return (m_address & mask); 16113531Sjairo.balart@metempsy.com } else { 16213531Sjairo.balart@metempsy.com mask = ~((physical_address_t)~0 << small); 16313531Sjairo.balart@metempsy.com physical_address_t lower_bits = m_address & mask; 16413531Sjairo.balart@metempsy.com mask = (physical_address_t)~0 << (big + 1); 16513531Sjairo.balart@metempsy.com physical_address_t higher_bits = m_address & mask; 16613760Sjairo.balart@metempsy.com 16713760Sjairo.balart@metempsy.com // Shift the valid high bits over the removed section 16813760Sjairo.balart@metempsy.com higher_bits = higher_bits >> (big - small + 1); 16913760Sjairo.balart@metempsy.com return (higher_bits | lower_bits); 17013760Sjairo.balart@metempsy.com } 17113760Sjairo.balart@metempsy.com} 17213760Sjairo.balart@metempsy.com 17313760Sjairo.balart@metempsy.cominline physical_address_t 17413760Sjairo.balart@metempsy.comAddress::maskLowOrderBits(int number) const 17513760Sjairo.balart@metempsy.com{ 17613760Sjairo.balart@metempsy.com physical_address_t mask; 17713531Sjairo.balart@metempsy.com 17813760Sjairo.balart@metempsy.com if (number >= ADDRESS_WIDTH - 1) { 17913531Sjairo.balart@metempsy.com mask = ~0; 18013531Sjairo.balart@metempsy.com } else { 18113531Sjairo.balart@metempsy.com mask = (physical_address_t)~0 << number; 18213531Sjairo.balart@metempsy.com } 18313531Sjairo.balart@metempsy.com return (m_address & mask); 18413531Sjairo.balart@metempsy.com} 18513531Sjairo.balart@metempsy.com 18613760Sjairo.balart@metempsy.cominline physical_address_t 18713760Sjairo.balart@metempsy.comAddress::maskHighOrderBits(int number) const 18813760Sjairo.balart@metempsy.com{ 18913760Sjairo.balart@metempsy.com physical_address_t mask; 19013760Sjairo.balart@metempsy.com 19113531Sjairo.balart@metempsy.com if (number >= ADDRESS_WIDTH - 1) { 19213531Sjairo.balart@metempsy.com mask = ~0; 19313531Sjairo.balart@metempsy.com } else { 19413531Sjairo.balart@metempsy.com mask = (physical_address_t)~0 >> number; 19513531Sjairo.balart@metempsy.com } 19613531Sjairo.balart@metempsy.com return (m_address & mask); 19713760Sjairo.balart@metempsy.com} 19813760Sjairo.balart@metempsy.com 19913760Sjairo.balart@metempsy.cominline physical_address_t 20013760Sjairo.balart@metempsy.comAddress::shiftLowOrderBits(int number) const 20113760Sjairo.balart@metempsy.com{ 20213760Sjairo.balart@metempsy.com return (m_address >> number); 20313760Sjairo.balart@metempsy.com} 20413760Sjairo.balart@metempsy.com 20513760Sjairo.balart@metempsy.com__hash_namespace_begin 20613760Sjairo.balart@metempsy.comtemplate <> struct hash<Address> 20713760Sjairo.balart@metempsy.com{ 20813760Sjairo.balart@metempsy.com size_t 20913760Sjairo.balart@metempsy.com operator()(const Address &s) const 21013760Sjairo.balart@metempsy.com { 21113760Sjairo.balart@metempsy.com return (size_t)s.getAddress(); 21213760Sjairo.balart@metempsy.com } 21313760Sjairo.balart@metempsy.com}; 21413760Sjairo.balart@metempsy.com__hash_namespace_end 21513760Sjairo.balart@metempsy.com 21613531Sjairo.balart@metempsy.comnamespace std { 21713760Sjairo.balart@metempsy.comtemplate <> struct equal_to<Address> 21813760Sjairo.balart@metempsy.com{ 21913760Sjairo.balart@metempsy.com bool 22013760Sjairo.balart@metempsy.com operator()(const Address& s1, const Address& s2) const 22113760Sjairo.balart@metempsy.com { 22213760Sjairo.balart@metempsy.com return s1 == s2; 22313760Sjairo.balart@metempsy.com } 22413760Sjairo.balart@metempsy.com}; 22513760Sjairo.balart@metempsy.com} // namespace std 22613760Sjairo.balart@metempsy.com 22713760Sjairo.balart@metempsy.com#endif // __MEM_RUBY_COMMON_ADDRESS_HH__ 22813760Sjairo.balart@metempsy.com