summaryrefslogtreecommitdiffstats
path: root/src/devices/base/register.cpp
blob: dcd4c18464aac465c335df0df68b4930845a88cb (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/***************************************************************************
 *   Copyright (C) 2006 Nicolas Hadacek <[email protected]>                  *
 *                                                                         *
 *   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 "register.h"

//----------------------------------------------------------------------------
namespace Register
{
  List *_list = 0;
}
Register::List &Register::list()
{
  if ( _list==0 ) _list = new List;
  return *_list;
}

//----------------------------------------------------------------------------
Register::TypeData::TypeData(Address address, uint nbChars)
  : _nbChars(nbChars), _address(address)
{
  Q_ASSERT( address.isValid() && nbChars!=0 );
}
Register::TypeData::TypeData(const TQString &name, uint nbChars)
  : _nbChars(nbChars), _name(name)
{
  Q_ASSERT( !name.isEmpty() && nbChars!=0 );
}
Register::TypeData::TypeData(const TQString &name, Address address, uint nbChars)
  : _nbChars(nbChars), _address(address), _name(name)
{
  Q_ASSERT( address.isValid() && nbChars!=0 && !name.isEmpty() );
}

Register::Type Register::TypeData::type() const
{
  if ( !_address.isValid() ) {
    if ( _name.isEmpty() ) return Invalid;
    return Special;
  }
  if ( _name.isEmpty() ) return Regular;
  return Combined;
}

TQString Register::TypeData::toString() const
{
  return TQString("%1 %2 %3").tqarg(toLabel(_address)).tqarg(_nbChars).tqarg(_name);
}

Register::TypeData Register::TypeData::fromString(const TQString &s)
{
  TQStringList list = TQStringList::split(" ", s);
  if ( list.count()<2 || list.count()>3 ) return TypeData();
  bool ok;
  Address address = list[0].toUInt(&ok);
  if ( !ok ) return TypeData();
  uint nbChars = list[1].toUInt(&ok);
  if ( !ok || nbChars==0 || (nbChars%2)!=0 ) return TypeData();
  TQString name;
  if ( list.count()==3 ) name = list[2];
  if ( !address.isValid() ) {
    if ( name.isEmpty() ) return TypeData();
    return TypeData(name, nbChars);
  }
  if ( name.isEmpty() ) return TypeData(address, nbChars);
  return TypeData(name, address, nbChars);
}

//----------------------------------------------------------------------------
void Register::List::init()
{
  _regulars.clear();
  _specials.clear();
  _watched.clear();
  _portDatas.clear();
  delayedChanged();
}

void Register::List::setWatched(const TypeData &data, bool watched)
{
  if (watched) {
    if ( _watched.tqcontains(data) ) return;
    _watched.append(data);
  } else _watched.remove(data);
  delayedChanged();
}

void Register::List::clearWatched()
{
  _watched.clear();
  delayedChanged();
}

void Register::List::setValue(const TypeData &data, BitValue value)
{
  if ( !data.address().isValid() ) {
    _specials[data.name()].old = _specials[data.name()].current;
    _specials[data.name()].current = value;
  } else {
    Q_ASSERT( (data.nbChars()%2)==0 );
    uint nb = data.nbChars()/2;
    for (uint i=0; i<nb; i++) {
      Address address = data.address() + i;
      _regulars[address].old = _regulars[address].current;
      _regulars[address].current = value.byte(i);
    }
  }
  delayedChanged();
}

void Register::List::setPortData(uint index, const TQMap<uint, Device::PortBitData> &data)
{
  _portDatas[index].old = _portDatas[index].current;
  _portDatas[index].current = data;
  delayedChanged();
}

BitValue Register::List::value(const TypeData &data) const
{
  if ( !data.address().isValid() ) {
    if ( !_specials.tqcontains(data.name()) ) return BitValue();
    return _specials[data.name()].current;
  }
  Q_ASSERT( (data.nbChars()%2)==0 );
  uint nb = data.nbChars()/2;
  BitValue value = 0;
  for (int i=nb-1; i>=0; i--) {
    value <<= 8;
    BitValue v = _regulars[data.address() + i].current;
    if ( !v.isInitialized() ) return BitValue();
    value += v;
  }
  return value;
}

BitValue Register::List::oldValue(const TypeData &data) const
{
  if ( !data.address().isValid() ) {
    if ( !_specials.tqcontains(data.name()) ) return BitValue();
    return _specials[data.name()].old;
  }
  Q_ASSERT( (data.nbChars()%2)==0 );
  uint nb = data.nbChars()/2;
  BitValue value = 0;
  for (int i=nb-1; i>=0; i--) {
    value <<= 8;
    BitValue v = _regulars[data.address() + i].old;
    if ( !v.isInitialized() ) return BitValue();
    value += v;
  }
  return value;
}