summaryrefslogtreecommitdiffstats
path: root/src/devices/mem24/mem24/mem24_memory.cpp
blob: aba1cbf2877c5c25860f034842b2c8ddfeeed9ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/***************************************************************************
 *   Copyright (C) 2006 Nicolas Hadacek <hadacek@kde.org>                  *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 ***************************************************************************/
#include "mem24_memory.h"

#include <tqfile.h>

#include "common/global/global.h"
#include "common/common/misc.h"

Mem24::Memory::Memory(const Data &data)
  : Device::Memory(data)
{
  fill(BitValue());
}

void Mem24::Memory::fill(BitValue value)
{
  _data = Device::Array(device().nbBytes());
  for (uint i=0; i<_data.count(); i++) _data[i] = value;
}

void Mem24::Memory::copyFrom(const Device::Memory &memory)
{
  Q_ASSERT( device().name()==memory.device().name() );
  _data = static_cast<const Memory &>(memory)._data;
}

Device::Array Mem24::Memory::arrayForWriting() const
{
  Device::Array data(_data.count());
  for (uint i=0; i<data.count(); i++) data[i] = _data[i].maskWith(0xFF);
  return data;
}

BitValue Mem24::Memory::byte(uint offset) const
{
  Q_ASSERT( _data.size()>offset );
  return _data[offset];
}

void Mem24::Memory::setByte(uint offset, BitValue value)
{
  Q_ASSERT( _data.size()>offset );
  Q_ASSERT( value<=0xFF );
  _data[offset] = value;
}

BitValue Mem24::Memory::checksum() const
{
  BitValue cs = 0x0000;
  for (uint i=0; i<_data.count(); i++) cs += _data[i];
  return cs.maskWith(0xFFFF);
}

//-----------------------------------------------------------------------------
void Mem24::Memory::savePartial(TQTextStream &stream, HexBuffer::Format format) const
{
  HexBuffer hb = toHexBuffer();
  hb.savePartial(stream, format);
}

//-----------------------------------------------------------------------------
HexBuffer Mem24::Memory::toHexBuffer() const
{
  HexBuffer hb;
  for (uint k=0; k<device().nbBytes(); k++) hb.insert(k, _data[k]);
  return hb;
}

void Mem24::Memory::fromHexBuffer(const HexBuffer &hb, WarningTypes &result,
                                      TQStringList &warnings, TQMap<uint, bool> &inRange)
{
  BitValue mask = 0xFF;
  for (uint k=0; k<device().nbBytes(); k++) {
    _data[k] = hb[k];
    if ( _data[k].isInitialized() ) {
      inRange[k] = true;
      if ( !(result & ValueTooLarge) && !_data[k].isInside(mask) ) {
        result |= ValueTooLarge;
        warnings += i18n("At least one word (at offset %1) is larger (%2) than the corresponding mask (%3).")
                    .arg(toHexLabel(k, 8)).arg(toHexLabel(_data[k], 8)).arg(toHexLabel(mask, 8));
      }
      _data[k] = _data[k].maskWith(mask);
    }
  }
}