DataBlock.hh revision 7039
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
297039Snate@binkert.org#ifndef __MEM_RUBY_COMMON_DATABLOCK_HH__
307039Snate@binkert.org#define __MEM_RUBY_COMMON_DATABLOCK_HH__
316145Snate@binkert.org
327002Snate@binkert.org#include <iomanip>
337002Snate@binkert.org#include <iostream>
347002Snate@binkert.org
357039Snate@binkert.org#include "mem/gems_common/Vector.hh"
366154Snate@binkert.org#include "mem/ruby/common/Global.hh"
376285Snate@binkert.org#include "mem/ruby/system/System.hh"
386145Snate@binkert.org
397039Snate@binkert.orgclass DataBlock
407039Snate@binkert.org{
417039Snate@binkert.org  public:
427039Snate@binkert.org    DataBlock()
437039Snate@binkert.org    {
447039Snate@binkert.org        alloc();
457039Snate@binkert.org    }
466145Snate@binkert.org
477039Snate@binkert.org    DataBlock(const DataBlock &cp)
487039Snate@binkert.org    {
497039Snate@binkert.org        m_data = new uint8[RubySystem::getBlockSizeBytes()];
507039Snate@binkert.org        memcpy(m_data, cp.m_data, RubySystem::getBlockSizeBytes());
517039Snate@binkert.org        m_alloc = true;
526762SBrad.Beckmann@amd.com    }
536285Snate@binkert.org
547039Snate@binkert.org    ~DataBlock()
557039Snate@binkert.org    {
567039Snate@binkert.org        if (m_alloc)
577039Snate@binkert.org            delete [] m_data;
587039Snate@binkert.org    }
596145Snate@binkert.org
607039Snate@binkert.org    DataBlock& operator=(const DataBlock& obj);
616285Snate@binkert.org
627039Snate@binkert.org    void assign(uint8* data);
636145Snate@binkert.org
647039Snate@binkert.org    void clear();
657039Snate@binkert.org    uint8 getByte(int whichByte) const;
667039Snate@binkert.org    const uint8* getData(int offset, int len) const;
677039Snate@binkert.org    void setByte(int whichByte, uint8 data);
687039Snate@binkert.org    void setData(uint8* data, int offset, int len);
697039Snate@binkert.org    void copyPartial(const DataBlock & dblk, int offset, int len);
707039Snate@binkert.org    bool equal(const DataBlock& obj) const;
717039Snate@binkert.org    void print(std::ostream& out) const;
727039Snate@binkert.org
737039Snate@binkert.org  private:
747039Snate@binkert.org    void alloc();
757039Snate@binkert.org    uint8* m_data;
767039Snate@binkert.org    bool m_alloc;
776145Snate@binkert.org};
786145Snate@binkert.org
797039Snate@binkert.orginline void
807039Snate@binkert.orgDataBlock::assign(uint8* data)
816285Snate@binkert.org{
827039Snate@binkert.org    if (m_alloc) {
837039Snate@binkert.org        delete [] m_data;
847039Snate@binkert.org    }
857039Snate@binkert.org    m_data = data;
867039Snate@binkert.org    m_alloc = false;
876285Snate@binkert.org}
886285Snate@binkert.org
897039Snate@binkert.orginline void
907039Snate@binkert.orgDataBlock::alloc()
916285Snate@binkert.org{
927039Snate@binkert.org    m_data = new uint8[RubySystem::getBlockSizeBytes()];
937039Snate@binkert.org    m_alloc = true;
947039Snate@binkert.org    clear();
956285Snate@binkert.org}
966285Snate@binkert.org
977039Snate@binkert.orginline void
987039Snate@binkert.orgDataBlock::clear()
996285Snate@binkert.org{
1007039Snate@binkert.org    memset(m_data, 0, RubySystem::getBlockSizeBytes());
1016285Snate@binkert.org}
1026285Snate@binkert.org
1037039Snate@binkert.orginline bool
1047039Snate@binkert.orgDataBlock::equal(const DataBlock& obj) const
1056285Snate@binkert.org{
1067039Snate@binkert.org    return !memcmp(m_data, obj.m_data, RubySystem::getBlockSizeBytes());
1076285Snate@binkert.org}
1086285Snate@binkert.org
1097039Snate@binkert.orginline void
1107039Snate@binkert.orgDataBlock::print(std::ostream& out) const
1116285Snate@binkert.org{
1127039Snate@binkert.org    using namespace std;
1137002Snate@binkert.org
1147039Snate@binkert.org    int size = RubySystem::getBlockSizeBytes();
1157039Snate@binkert.org    out << "[ ";
1167039Snate@binkert.org    for (int i = 0; i < size; i++) {
1177039Snate@binkert.org        out << setw(2) << setfill('0') << hex << "0x" << (int)m_data[i] << " ";
1187039Snate@binkert.org        out << setfill(' ');
1197039Snate@binkert.org    }
1207039Snate@binkert.org    out << dec << "]" << flush;
1216285Snate@binkert.org}
1226285Snate@binkert.org
1237039Snate@binkert.orginline uint8
1247039Snate@binkert.orgDataBlock::getByte(int whichByte) const
1256285Snate@binkert.org{
1267039Snate@binkert.org    return m_data[whichByte];
1276285Snate@binkert.org}
1286285Snate@binkert.org
1297039Snate@binkert.orginline const uint8*
1307039Snate@binkert.orgDataBlock::getData(int offset, int len) const
1316285Snate@binkert.org{
1327039Snate@binkert.org    assert(offset + len <= RubySystem::getBlockSizeBytes());
1337039Snate@binkert.org    return &m_data[offset];
1346285Snate@binkert.org}
1356285Snate@binkert.org
1367039Snate@binkert.orginline void
1377039Snate@binkert.orgDataBlock::setByte(int whichByte, uint8 data)
1386285Snate@binkert.org{
1396285Snate@binkert.org    m_data[whichByte] = data;
1406285Snate@binkert.org}
1416285Snate@binkert.org
1427039Snate@binkert.orginline void
1437039Snate@binkert.orgDataBlock::setData(uint8* data, int offset, int len)
1446285Snate@binkert.org{
1457039Snate@binkert.org    assert(offset + len <= RubySystem::getBlockSizeBytes());
1467039Snate@binkert.org    memcpy(&m_data[offset], data, len);
1476285Snate@binkert.org}
1486285Snate@binkert.org
1497039Snate@binkert.orginline void
1507039Snate@binkert.orgDataBlock::copyPartial(const DataBlock & dblk, int offset, int len)
1516285Snate@binkert.org{
1527039Snate@binkert.org    setData(&dblk.m_data[offset], offset, len);
1536285Snate@binkert.org}
1546145Snate@binkert.org
1557039Snate@binkert.orginline std::ostream&
1567039Snate@binkert.orgoperator<<(std::ostream& out, const DataBlock& obj)
1576145Snate@binkert.org{
1587039Snate@binkert.org    obj.print(out);
1597039Snate@binkert.org    out << std::flush;
1607039Snate@binkert.org    return out;
1616145Snate@binkert.org}
1626145Snate@binkert.org
1637039Snate@binkert.orginline bool
1647039Snate@binkert.orgoperator==(const DataBlock& obj1,const DataBlock& obj2)
1656145Snate@binkert.org{
1667039Snate@binkert.org    return obj1.equal(obj2);
1676145Snate@binkert.org}
1686145Snate@binkert.org
1697039Snate@binkert.org#endif // __MEM_RUBY_COMMON_DATABLOCK_HH__
170