DataBlock.cc revision 11307:bd7d06ea90f5
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
296145Snate@binkert.org#include "mem/ruby/common/DataBlock.hh"
306145Snate@binkert.org
316145Snate@binkert.org#include "mem/ruby/common/WriteMask.hh"
326145Snate@binkert.org#include "mem/ruby/system/RubySystem.hh"
336145Snate@binkert.org
346145Snate@binkert.orgDataBlock::DataBlock(const DataBlock &cp)
356145Snate@binkert.org{
366145Snate@binkert.org    m_data = new uint8_t[RubySystem::getBlockSizeBytes()];
376145Snate@binkert.org    memcpy(m_data, cp.m_data, RubySystem::getBlockSizeBytes());
386145Snate@binkert.org    m_alloc = true;
396145Snate@binkert.org}
406145Snate@binkert.org
416145Snate@binkert.orgvoid
426145Snate@binkert.orgDataBlock::alloc()
436145Snate@binkert.org{
446145Snate@binkert.org    m_data = new uint8_t[RubySystem::getBlockSizeBytes()];
456145Snate@binkert.org    m_alloc = true;
466145Snate@binkert.org    clear();
476145Snate@binkert.org}
486145Snate@binkert.org
496145Snate@binkert.orgvoid
506145Snate@binkert.orgDataBlock::clear()
516145Snate@binkert.org{
526145Snate@binkert.org    memset(m_data, 0, RubySystem::getBlockSizeBytes());
536145Snate@binkert.org}
546154Snate@binkert.org
556154Snate@binkert.orgbool
566154Snate@binkert.orgDataBlock::equal(const DataBlock& obj) const
576154Snate@binkert.org{
586154Snate@binkert.org    return !memcmp(m_data, obj.m_data, RubySystem::getBlockSizeBytes());
596154Snate@binkert.org}
606154Snate@binkert.org
616154Snate@binkert.orgvoid
626154Snate@binkert.orgDataBlock::copyPartial(const DataBlock &dblk, const WriteMask &mask)
636154Snate@binkert.org{
646154Snate@binkert.org    for (int i = 0; i < RubySystem::getBlockSizeBytes(); i++) {
656154Snate@binkert.org        if (mask.getMask(i, 1)) {
666154Snate@binkert.org            m_data[i] = dblk.m_data[i];
676145Snate@binkert.org        }
686145Snate@binkert.org    }
696145Snate@binkert.org}
706145Snate@binkert.org
716145Snate@binkert.orgvoid
726145Snate@binkert.orgDataBlock::atomicPartial(const DataBlock &dblk, const WriteMask &mask)
736145Snate@binkert.org{
746145Snate@binkert.org    for (int i = 0; i < RubySystem::getBlockSizeBytes(); i++) {
756145Snate@binkert.org        m_data[i] = dblk.m_data[i];
766145Snate@binkert.org    }
776145Snate@binkert.org    mask.performAtomic(m_data);
786145Snate@binkert.org}
796145Snate@binkert.org
806145Snate@binkert.orgvoid
816145Snate@binkert.orgDataBlock::print(std::ostream& out) const
826145Snate@binkert.org{
836145Snate@binkert.org    using namespace std;
846145Snate@binkert.org
856145Snate@binkert.org    int size = RubySystem::getBlockSizeBytes();
866145Snate@binkert.org    out << "[ ";
876145Snate@binkert.org    for (int i = 0; i < size; i++) {
886145Snate@binkert.org        out << setw(2) << setfill('0') << hex << "0x" << (int)m_data[i] << " ";
896145Snate@binkert.org        out << setfill(' ');
906145Snate@binkert.org    }
916145Snate@binkert.org    out << dec << "]" << flush;
926145Snate@binkert.org}
936145Snate@binkert.org
946145Snate@binkert.orgconst uint8_t*
956145Snate@binkert.orgDataBlock::getData(int offset, int len) const
966145Snate@binkert.org{
976145Snate@binkert.org    assert(offset + len <= RubySystem::getBlockSizeBytes());
986145Snate@binkert.org    return &m_data[offset];
996145Snate@binkert.org}
1006145Snate@binkert.org
1016145Snate@binkert.orguint8_t*
1026145Snate@binkert.orgDataBlock::getDataMod(int offset)
1036145Snate@binkert.org{
1046145Snate@binkert.org    return &m_data[offset];
1056145Snate@binkert.org}
1066145Snate@binkert.org
1076145Snate@binkert.orgvoid
1086145Snate@binkert.orgDataBlock::setData(const uint8_t *data, int offset, int len)
1096145Snate@binkert.org{
1106145Snate@binkert.org    assert(offset + len <= RubySystem::getBlockSizeBytes());
1116145Snate@binkert.org    memcpy(&m_data[offset], data, len);
1126145Snate@binkert.org}
1136145Snate@binkert.org
1146145Snate@binkert.orgDataBlock &
1156145Snate@binkert.orgDataBlock::operator=(const DataBlock & obj)
1166145Snate@binkert.org{
1176145Snate@binkert.org    memcpy(m_data, obj.m_data, RubySystem::getBlockSizeBytes());
1186145Snate@binkert.org    return *this;
1196145Snate@binkert.org}
1206145Snate@binkert.org