DataBlock.cc revision 8090
16288Snate@binkert.org/*
26288Snate@binkert.org * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
36288Snate@binkert.org * All rights reserved.
46288Snate@binkert.org *
56288Snate@binkert.org * Redistribution and use in source and binary forms, with or without
66288Snate@binkert.org * modification, are permitted provided that the following conditions are
76288Snate@binkert.org * met: redistributions of source code must retain the above copyright
86288Snate@binkert.org * notice, this list of conditions and the following disclaimer;
96288Snate@binkert.org * redistributions in binary form must reproduce the above copyright
106288Snate@binkert.org * notice, this list of conditions and the following disclaimer in the
116288Snate@binkert.org * documentation and/or other materials provided with the distribution;
126288Snate@binkert.org * neither the name of the copyright holders nor the names of its
136288Snate@binkert.org * contributors may be used to endorse or promote products derived from
146288Snate@binkert.org * this software without specific prior written permission.
156288Snate@binkert.org *
166288Snate@binkert.org * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
176288Snate@binkert.org * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
186288Snate@binkert.org * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
196288Snate@binkert.org * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
206288Snate@binkert.org * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
216288Snate@binkert.org * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
226288Snate@binkert.org * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
236288Snate@binkert.org * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
246288Snate@binkert.org * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
256288Snate@binkert.org * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
266288Snate@binkert.org * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
276288Snate@binkert.org */
286145Snate@binkert.org
296154Snate@binkert.org#include "mem/ruby/common/DataBlock.hh"
308090Snilay@cs.wisc.edu#include "mem/ruby/system/System.hh"
318090Snilay@cs.wisc.edu
328090Snilay@cs.wisc.eduDataBlock::DataBlock(const DataBlock &cp)
338090Snilay@cs.wisc.edu{
348090Snilay@cs.wisc.edu    m_data = new uint8[RubySystem::getBlockSizeBytes()];
358090Snilay@cs.wisc.edu    memcpy(m_data, cp.m_data, RubySystem::getBlockSizeBytes());
368090Snilay@cs.wisc.edu    m_alloc = true;
378090Snilay@cs.wisc.edu}
388090Snilay@cs.wisc.edu
398090Snilay@cs.wisc.eduvoid
408090Snilay@cs.wisc.eduDataBlock::alloc()
418090Snilay@cs.wisc.edu{
428090Snilay@cs.wisc.edu    m_data = new uint8[RubySystem::getBlockSizeBytes()];
438090Snilay@cs.wisc.edu    m_alloc = true;
448090Snilay@cs.wisc.edu    clear();
458090Snilay@cs.wisc.edu}
468090Snilay@cs.wisc.edu
478090Snilay@cs.wisc.eduvoid
488090Snilay@cs.wisc.eduDataBlock::clear()
498090Snilay@cs.wisc.edu{
508090Snilay@cs.wisc.edu    memset(m_data, 0, RubySystem::getBlockSizeBytes());
518090Snilay@cs.wisc.edu}
528090Snilay@cs.wisc.edu
538090Snilay@cs.wisc.edubool
548090Snilay@cs.wisc.eduDataBlock::equal(const DataBlock& obj) const
558090Snilay@cs.wisc.edu{
568090Snilay@cs.wisc.edu    return !memcmp(m_data, obj.m_data, RubySystem::getBlockSizeBytes());
578090Snilay@cs.wisc.edu}
588090Snilay@cs.wisc.edu
598090Snilay@cs.wisc.eduvoid
608090Snilay@cs.wisc.eduDataBlock::print(std::ostream& out) const
618090Snilay@cs.wisc.edu{
628090Snilay@cs.wisc.edu    using namespace std;
638090Snilay@cs.wisc.edu
648090Snilay@cs.wisc.edu    int size = RubySystem::getBlockSizeBytes();
658090Snilay@cs.wisc.edu    out << "[ ";
668090Snilay@cs.wisc.edu    for (int i = 0; i < size; i++) {
678090Snilay@cs.wisc.edu        out << setw(2) << setfill('0') << hex << "0x" << (int)m_data[i] << " ";
688090Snilay@cs.wisc.edu        out << setfill(' ');
698090Snilay@cs.wisc.edu    }
708090Snilay@cs.wisc.edu    out << dec << "]" << flush;
718090Snilay@cs.wisc.edu}
728090Snilay@cs.wisc.edu
738090Snilay@cs.wisc.educonst uint8*
748090Snilay@cs.wisc.eduDataBlock::getData(int offset, int len) const
758090Snilay@cs.wisc.edu{
768090Snilay@cs.wisc.edu    assert(offset + len <= RubySystem::getBlockSizeBytes());
778090Snilay@cs.wisc.edu    return &m_data[offset];
788090Snilay@cs.wisc.edu}
798090Snilay@cs.wisc.edu
808090Snilay@cs.wisc.eduvoid
818090Snilay@cs.wisc.eduDataBlock::setData(uint8* data, int offset, int len)
828090Snilay@cs.wisc.edu{
838090Snilay@cs.wisc.edu    assert(offset + len <= RubySystem::getBlockSizeBytes());
848090Snilay@cs.wisc.edu    memcpy(&m_data[offset], data, len);
858090Snilay@cs.wisc.edu}
866145Snate@binkert.org
876285Snate@binkert.orgDataBlock &
886285Snate@binkert.orgDataBlock::operator=(const DataBlock & obj)
896145Snate@binkert.org{
907039Snate@binkert.org    if (this == &obj) {
917039Snate@binkert.org        // assert(false);
927039Snate@binkert.org    } else {
937039Snate@binkert.org        if (!m_alloc)
947039Snate@binkert.org            m_data = new uint8[RubySystem::getBlockSizeBytes()];
957039Snate@binkert.org        memcpy(m_data, obj.m_data, RubySystem::getBlockSizeBytes());
967039Snate@binkert.org        m_alloc = true;
977039Snate@binkert.org    }
987039Snate@binkert.org
997039Snate@binkert.org    return *this;
1006145Snate@binkert.org}
101