BankedArray.cc revision 10919
18839Sandreas.hansson@arm.com/*
28839Sandreas.hansson@arm.com * Copyright (c) 2012 Advanced Micro Devices, Inc.
38839Sandreas.hansson@arm.com * All rights reserved.
48839Sandreas.hansson@arm.com *
58839Sandreas.hansson@arm.com * Redistribution and use in source and binary forms, with or without
68839Sandreas.hansson@arm.com * modification, are permitted provided that the following conditions are
78839Sandreas.hansson@arm.com * met: redistributions of source code must retain the above copyright
88839Sandreas.hansson@arm.com * notice, this list of conditions and the following disclaimer;
98839Sandreas.hansson@arm.com * redistributions in binary form must reproduce the above copyright
108839Sandreas.hansson@arm.com * notice, this list of conditions and the following disclaimer in the
118839Sandreas.hansson@arm.com * documentation and/or other materials provided with the distribution;
128839Sandreas.hansson@arm.com * neither the name of the copyright holders nor the names of its
135335Shines@cs.fsu.edu * contributors may be used to endorse or promote products derived from
147897Shestness@cs.utexas.edu * this software without specific prior written permission.
154486Sbinkertn@umich.edu *
164486Sbinkertn@umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
174486Sbinkertn@umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
184486Sbinkertn@umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
194486Sbinkertn@umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
204486Sbinkertn@umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
214486Sbinkertn@umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
224486Sbinkertn@umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
234486Sbinkertn@umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
244486Sbinkertn@umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
254486Sbinkertn@umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
264486Sbinkertn@umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
274486Sbinkertn@umich.edu *
284486Sbinkertn@umich.edu * Author: Brad Beckmann
294486Sbinkertn@umich.edu *
304486Sbinkertn@umich.edu */
314486Sbinkertn@umich.edu
324486Sbinkertn@umich.edu#include "base/intmath.hh"
334486Sbinkertn@umich.edu#include "mem/ruby/structures/BankedArray.hh"
344486Sbinkertn@umich.edu#include "mem/ruby/system/System.hh"
354486Sbinkertn@umich.edu
364486Sbinkertn@umich.eduBankedArray::BankedArray(unsigned int banks, Cycles accessLatency,
374486Sbinkertn@umich.edu                         unsigned int startIndexBit, RubySystem *rs)
384486Sbinkertn@umich.edu    : m_ruby_system(rs)
394486Sbinkertn@umich.edu{
404486Sbinkertn@umich.edu    this->banks = banks;
417897Shestness@cs.utexas.edu    this->accessLatency = accessLatency;
428839Sandreas.hansson@arm.com    this->startIndexBit = startIndexBit;
434486Sbinkertn@umich.edu
446654Snate@binkert.org    if (banks != 0) {
456654Snate@binkert.org        bankBits = floorLog2(banks);
466654Snate@binkert.org    }
473102SN/A
483102SN/A    busyBanks.resize(banks);
496654Snate@binkert.org}
509036Sandreas.hansson@arm.com
514776Sgblack@eecs.umich.edubool
524776Sgblack@eecs.umich.eduBankedArray::tryAccess(int64 idx)
536654Snate@binkert.org{
542667SN/A    if (accessLatency == 0)
554776Sgblack@eecs.umich.edu        return true;
564776Sgblack@eecs.umich.edu
576654Snate@binkert.org    unsigned int bank = mapIndexToBank(idx);
586023Snate@binkert.org    assert(bank < banks);
598745Sgblack@eecs.umich.edu
606654Snate@binkert.org    if (busyBanks[bank].endAccess >= curTick()) {
616022Sgblack@eecs.umich.edu        if (!(busyBanks[bank].startAccess == curTick() &&
628745Sgblack@eecs.umich.edu            busyBanks[bank].idx == idx)) {
636654Snate@binkert.org            return false;
646022Sgblack@eecs.umich.edu        } else {
658745Sgblack@eecs.umich.edu            // We tried to allocate resources twice
666654Snate@binkert.org            // in the same cycle for the same addr
676022Sgblack@eecs.umich.edu            return true;
688745Sgblack@eecs.umich.edu        }
696654Snate@binkert.org    }
706116Snate@binkert.org
718745Sgblack@eecs.umich.edu    busyBanks[bank].idx = idx;
726691Stjones1@inf.ed.ac.uk    busyBanks[bank].startAccess = curTick();
736691Stjones1@inf.ed.ac.uk    busyBanks[bank].endAccess = curTick() +
748745Sgblack@eecs.umich.edu        (accessLatency-1) * m_ruby_system->clockPeriod();
754486Sbinkertn@umich.edu
765529Snate@binkert.org    return true;
771366SN/A}
781310SN/A
791310SN/Aunsigned int
809254SAndreas.Sandberg@arm.comBankedArray::mapIndexToBank(int64 idx)
819254SAndreas.Sandberg@arm.com{
829254SAndreas.Sandberg@arm.com    if (banks == 1) {
839254SAndreas.Sandberg@arm.com        return 0;
849254SAndreas.Sandberg@arm.com    }
859254SAndreas.Sandberg@arm.com    return idx % banks;
869254SAndreas.Sandberg@arm.com}
879254SAndreas.Sandberg@arm.com