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
329208Snilay@cs.wisc.edu#include <inttypes.h>
339208Snilay@cs.wisc.edu
349181Spower.jg@gmail.com#include <cassert>
357002Snate@binkert.org#include <iomanip>
367002Snate@binkert.org#include <iostream>
377002Snate@binkert.org
3811307Santhony.gutierrez@amd.comclass WriteMask;
3911307Santhony.gutierrez@amd.com
407039Snate@binkert.orgclass DataBlock
417039Snate@binkert.org{
427039Snate@binkert.org  public:
437039Snate@binkert.org    DataBlock()
447039Snate@binkert.org    {
457039Snate@binkert.org        alloc();
467039Snate@binkert.org    }
476145Snate@binkert.org
488090Snilay@cs.wisc.edu    DataBlock(const DataBlock &cp);
496285Snate@binkert.org
507039Snate@binkert.org    ~DataBlock()
517039Snate@binkert.org    {
527039Snate@binkert.org        if (m_alloc)
537039Snate@binkert.org            delete [] m_data;
547039Snate@binkert.org    }
556145Snate@binkert.org
567039Snate@binkert.org    DataBlock& operator=(const DataBlock& obj);
576285Snate@binkert.org
589208Snilay@cs.wisc.edu    void assign(uint8_t *data);
596145Snate@binkert.org
607039Snate@binkert.org    void clear();
619208Snilay@cs.wisc.edu    uint8_t getByte(int whichByte) const;
629208Snilay@cs.wisc.edu    const uint8_t *getData(int offset, int len) const;
6311307Santhony.gutierrez@amd.com    uint8_t *getDataMod(int offset);
649208Snilay@cs.wisc.edu    void setByte(int whichByte, uint8_t data);
6510563Sandreas.hansson@arm.com    void setData(const uint8_t *data, int offset, int len);
6611307Santhony.gutierrez@amd.com    void copyPartial(const DataBlock &dblk, int offset, int len);
6711307Santhony.gutierrez@amd.com    void copyPartial(const DataBlock &dblk, const WriteMask &mask);
6811307Santhony.gutierrez@amd.com    void atomicPartial(const DataBlock & dblk, const WriteMask & mask);
697039Snate@binkert.org    bool equal(const DataBlock& obj) const;
707039Snate@binkert.org    void print(std::ostream& out) const;
717039Snate@binkert.org
727039Snate@binkert.org  private:
737039Snate@binkert.org    void alloc();
749208Snilay@cs.wisc.edu    uint8_t *m_data;
757039Snate@binkert.org    bool m_alloc;
766145Snate@binkert.org};
776145Snate@binkert.org
787039Snate@binkert.orginline void
799208Snilay@cs.wisc.eduDataBlock::assign(uint8_t *data)
806285Snate@binkert.org{
819181Spower.jg@gmail.com    assert(data != NULL);
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
899208Snilay@cs.wisc.eduinline uint8_t
907039Snate@binkert.orgDataBlock::getByte(int whichByte) const
916285Snate@binkert.org{
927039Snate@binkert.org    return m_data[whichByte];
936285Snate@binkert.org}
946285Snate@binkert.org
957039Snate@binkert.orginline void
969208Snilay@cs.wisc.eduDataBlock::setByte(int whichByte, uint8_t data)
976285Snate@binkert.org{
986285Snate@binkert.org    m_data[whichByte] = data;
996285Snate@binkert.org}
1006285Snate@binkert.org
1017039Snate@binkert.orginline void
1027039Snate@binkert.orgDataBlock::copyPartial(const DataBlock & dblk, int offset, int len)
1036285Snate@binkert.org{
1047039Snate@binkert.org    setData(&dblk.m_data[offset], offset, len);
1056285Snate@binkert.org}
1066145Snate@binkert.org
1077039Snate@binkert.orginline std::ostream&
1087039Snate@binkert.orgoperator<<(std::ostream& out, const DataBlock& obj)
1096145Snate@binkert.org{
1107039Snate@binkert.org    obj.print(out);
1117039Snate@binkert.org    out << std::flush;
1127039Snate@binkert.org    return out;
1136145Snate@binkert.org}
1146145Snate@binkert.org
1157039Snate@binkert.orginline bool
1167039Snate@binkert.orgoperator==(const DataBlock& obj1,const DataBlock& obj2)
1176145Snate@binkert.org{
1187039Snate@binkert.org    return obj1.equal(obj2);
1196145Snate@binkert.org}
1206145Snate@binkert.org
1217039Snate@binkert.org#endif // __MEM_RUBY_COMMON_DATABLOCK_HH__
122