16145Snate@binkert.org/*
26145Snate@binkert.org * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
36145Snate@binkert.org * All rights reserved.
46145Snate@binkert.org *
56145Snate@binkert.org * Redistribution and use in source and binary forms, with or without
66145Snate@binkert.org * modification, are permitted provided that the following conditions are
76145Snate@binkert.org * met: redistributions of source code must retain the above copyright
86145Snate@binkert.org * notice, this list of conditions and the following disclaimer;
96145Snate@binkert.org * redistributions in binary form must reproduce the above copyright
106145Snate@binkert.org * notice, this list of conditions and the following disclaimer in the
116145Snate@binkert.org * documentation and/or other materials provided with the distribution;
126145Snate@binkert.org * neither the name of the copyright holders nor the names of its
136145Snate@binkert.org * contributors may be used to endorse or promote products derived from
146145Snate@binkert.org * this software without specific prior written permission.
156145Snate@binkert.org *
166145Snate@binkert.org * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
176145Snate@binkert.org * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
186145Snate@binkert.org * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
196145Snate@binkert.org * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
206145Snate@binkert.org * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
216145Snate@binkert.org * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
226145Snate@binkert.org * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
236145Snate@binkert.org * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
246145Snate@binkert.org * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
256145Snate@binkert.org * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
266145Snate@binkert.org * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
276145Snate@binkert.org */
286145Snate@binkert.org
296154Snate@binkert.org#include "mem/ruby/common/Address.hh"
3011108Sdavid.hashe@amd.com
3111108Sdavid.hashe@amd.com#include "mem/ruby/system/RubySystem.hh"
328091Snilay@cs.wisc.edu
3311025Snilay@cs.wisc.eduAddr
3411025Snilay@cs.wisc.edubitSelect(Addr addr, unsigned int small, unsigned int big)
358091Snilay@cs.wisc.edu{
3611025Snilay@cs.wisc.edu    assert(big >= small);
3711025Snilay@cs.wisc.edu
3811025Snilay@cs.wisc.edu    if (big >= ADDRESS_WIDTH - 1) {
3911025Snilay@cs.wisc.edu        return (addr >> small);
4011025Snilay@cs.wisc.edu    } else {
4111025Snilay@cs.wisc.edu        Addr mask = ~((Addr)~0 << (big + 1));
4211025Snilay@cs.wisc.edu        // FIXME - this is slow to manipulate a 64-bit number using 32-bits
4311025Snilay@cs.wisc.edu        Addr partial = (addr & mask);
4411025Snilay@cs.wisc.edu        return (partial >> small);
4511025Snilay@cs.wisc.edu    }
468091Snilay@cs.wisc.edu}
478091Snilay@cs.wisc.edu
4811025Snilay@cs.wisc.eduAddr
4911025Snilay@cs.wisc.edubitRemove(Addr addr, unsigned int small, unsigned int big)
508091Snilay@cs.wisc.edu{
5111025Snilay@cs.wisc.edu    assert(big >= small);
5211025Snilay@cs.wisc.edu
5311025Snilay@cs.wisc.edu    if (small >= ADDRESS_WIDTH - 1) {
5411025Snilay@cs.wisc.edu        return addr;
5511025Snilay@cs.wisc.edu    } else if (big >= ADDRESS_WIDTH - 1) {
5611025Snilay@cs.wisc.edu        Addr mask = (Addr)~0 >> small;
5711025Snilay@cs.wisc.edu        return (addr & mask);
5811025Snilay@cs.wisc.edu    } else if (small == 0) {
5911025Snilay@cs.wisc.edu        Addr mask = (Addr)~0 << big;
6011025Snilay@cs.wisc.edu        return (addr & mask);
6111025Snilay@cs.wisc.edu    } else {
6211025Snilay@cs.wisc.edu        Addr mask = ~((Addr)~0 << small);
6311025Snilay@cs.wisc.edu        Addr lower_bits = addr & mask;
6411025Snilay@cs.wisc.edu        mask = (Addr)~0 << (big + 1);
6511025Snilay@cs.wisc.edu        Addr higher_bits = addr & mask;
6611025Snilay@cs.wisc.edu
6711025Snilay@cs.wisc.edu        // Shift the valid high bits over the removed section
6811025Snilay@cs.wisc.edu        higher_bits = higher_bits >> (big - small + 1);
6911025Snilay@cs.wisc.edu        return (higher_bits | lower_bits);
7011025Snilay@cs.wisc.edu    }
718091Snilay@cs.wisc.edu}
728091Snilay@cs.wisc.edu
7311025Snilay@cs.wisc.eduAddr
7411025Snilay@cs.wisc.edumaskLowOrderBits(Addr addr, unsigned int number)
758091Snilay@cs.wisc.edu{
7611025Snilay@cs.wisc.edu  Addr mask;
7711025Snilay@cs.wisc.edu
7811025Snilay@cs.wisc.edu  if (number >= ADDRESS_WIDTH - 1) {
7911025Snilay@cs.wisc.edu      mask = ~0;
8011025Snilay@cs.wisc.edu  } else {
8111025Snilay@cs.wisc.edu      mask = (Addr)~0 << number;
8211025Snilay@cs.wisc.edu  }
8311025Snilay@cs.wisc.edu  return (addr & mask);
8411025Snilay@cs.wisc.edu}
8511025Snilay@cs.wisc.edu
8611025Snilay@cs.wisc.eduAddr
8711025Snilay@cs.wisc.edumaskHighOrderBits(Addr addr, unsigned int number)
8811025Snilay@cs.wisc.edu{
8911025Snilay@cs.wisc.edu    Addr mask;
9011025Snilay@cs.wisc.edu
9111025Snilay@cs.wisc.edu    if (number >= ADDRESS_WIDTH - 1) {
9211025Snilay@cs.wisc.edu        mask = ~0;
9311025Snilay@cs.wisc.edu    } else {
9411025Snilay@cs.wisc.edu        mask = (Addr)~0 >> number;
9511025Snilay@cs.wisc.edu    }
9611025Snilay@cs.wisc.edu    return (addr & mask);
9711025Snilay@cs.wisc.edu}
9811025Snilay@cs.wisc.edu
9911025Snilay@cs.wisc.eduAddr
10011025Snilay@cs.wisc.edushiftLowOrderBits(Addr addr, unsigned int number)
10111025Snilay@cs.wisc.edu{
10211025Snilay@cs.wisc.edu    return (addr >> number);
10311025Snilay@cs.wisc.edu}
10411025Snilay@cs.wisc.edu
10511025Snilay@cs.wisc.eduAddr
10611025Snilay@cs.wisc.edugetOffset(Addr addr)
10711025Snilay@cs.wisc.edu{
10811025Snilay@cs.wisc.edu    return bitSelect(addr, 0, RubySystem::getBlockSizeBits() - 1);
10911025Snilay@cs.wisc.edu}
11011025Snilay@cs.wisc.edu
11111025Snilay@cs.wisc.eduAddr
11211025Snilay@cs.wisc.edumakeLineAddress(Addr addr)
11311025Snilay@cs.wisc.edu{
11411025Snilay@cs.wisc.edu    return maskLowOrderBits(addr, RubySystem::getBlockSizeBits());
1158091Snilay@cs.wisc.edu}
1168091Snilay@cs.wisc.edu
1178091Snilay@cs.wisc.edu// returns the next stride address based on line address
11811025Snilay@cs.wisc.eduAddr
11911025Snilay@cs.wisc.edumakeNextStrideAddress(Addr addr, int stride)
1208091Snilay@cs.wisc.edu{
12111025Snilay@cs.wisc.edu    return maskLowOrderBits(addr, RubySystem::getBlockSizeBits())
12211025Snilay@cs.wisc.edu        + RubySystem::getBlockSizeBytes() * stride;
1238091Snilay@cs.wisc.edu}
12411118Snilay@cs.wisc.edu
12511118Snilay@cs.wisc.edustd::string
12611118Snilay@cs.wisc.eduprintAddress(Addr addr)
12711118Snilay@cs.wisc.edu{
12811118Snilay@cs.wisc.edu    std::stringstream out;
12911118Snilay@cs.wisc.edu    out << "[" << std::hex << "0x" << addr << "," << " line 0x"
13011118Snilay@cs.wisc.edu       << maskLowOrderBits(addr, RubySystem::getBlockSizeBits())
13111118Snilay@cs.wisc.edu       << std::dec << "]";
13211118Snilay@cs.wisc.edu    return out.str();
13311118Snilay@cs.wisc.edu}
134