summaryrefslogtreecommitdiffstats
path: root/src/progs/gpsim/base/gpsim_debug.cpp
blob: eca9841a071e8b3290bf904e822a825c1f57aae4 (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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/***************************************************************************
 *   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 "gpsim_debug.h"

#include <signal.h>
#include <tqregexp.h>

#include "devices/list/device_list.h"
#include "devices/pic/base/pic_register.h"
#include "coff/base/text_coff.h"
#include "progs/manager/debug_manager.h"

//----------------------------------------------------------------------------
GPSim::Debugger::Debugger(Programmer &programmer)
  : ::Debugger::PicBase(programmer), _nbBreakpoints(0)
{}

bool GPSim::Debugger::internalInit()
{
  if ( !hardware()->execute("processor pic" + device()->name().lower(), true) ) return false;
  if ( _inputType==PURL::Cod ) return hardware()->execute("load s " + _filename, true);
  Q_ASSERT( _inputType==PURL::Hex );
  return hardware()->execute("load h " + _filename, true);
}

bool GPSim::Debugger::internalRun()
{
  return hardware()->execute("run", false);
}

bool GPSim::Debugger::hardHalt()
{
  log(Log::LineType::Warning, i18n("Failed to halt target: kill process."));
  _programmer.disconnectHardware();
  return true;
}

bool GPSim::Debugger::softHalt(bool &success)
{
  success = hardware()->signal(SIGINT, true);
  return true;
}

bool GPSim::Debugger::internalStep()
{
  return hardware()->execute("step", true);
}

bool GPSim::Debugger::setBreakpoints(const TQValueList<Address> &list)
{
  for (uint i=0; i<_nbBreakpoints; i++)
    if ( !hardware()->execute("clear " + TQString::number(i), true) ) return false;
  for (uint i=0; i<uint(list.count()); i++)
    if ( !hardware()->execute("break e 0x" + toHex(list[i], nbChars(list[i].toUInt())), true) ) return false;
  _nbBreakpoints = list.count();
  return true;
}

bool GPSim::Debugger::internalReset()
{
  if ( _programmer.state()==::Programmer::Running && !halt() ) return false;
  return hardware()->execute("reset", true);
}

bool GPSim::Debugger::updateState()
{
  if ( hardware()->isReady() ) _programmer.setState(::Programmer::Halted);
  else _programmer.setState(::Programmer::Running);
  return true;
}

bool GPSim::Debugger::findRegExp(const TQStringList &lines, const TQString &pattern,
                                 const TQString &label, TQString &value) const
{
  TQRegExp rexp(pattern);
  uint i = 0;
  for (; i<uint(lines.count()); i++) {
    int offset = 0;
    for (;;) {
      offset = rexp.search(lines[i], offset, TQRegExp::CaretAtOffset);
      if ( offset==-1 || rexp.cap(1)==label ) break;
      offset += rexp.cap(0).length();
    }
    if ( offset!=-1 ) break;
  }
  if ( i==uint(lines.count()) ) return false;
  value = rexp.cap(2);
  return true;
}

bool GPSim::Debugger::readWreg(BitValue &value)
{
  // #### only known for version 4 and 11
  if ( hardware()->version()<=VersionData(0, 21, 7) || hardware()->version()>=VersionData(0, 22, 0) )
    return getRegister("W", value);
  TQStringList lines;
  if ( !hardware()->execute("dump s", true, &lines) ) return false;
  TQString w = (_coff->symbol("_WREG") ? "_WREG" : "W");
  TQString s;
  if ( !findRegExp(lines, "^\\s*[0-9A-Fa-f]+\\s+(\\w+)\\s*=\\s*([0-9A-Fa-f]+)", w, s) ) {
    log(Log::LineType::Error, i18n("Error reading register \"%1\"").tqarg(w));
    return false;
  }
  value = fromHex(s, 0);
  return true;
}

bool GPSim::Debugger::getRegister(const TQString &name, BitValue &value)
{
  TQStringList lines;
  TQRegExp r;
  if ( hardware()->version()<VersionData(0, 22, 0) ) {
    if ( !hardware()->execute("x " + name, true, &lines) ) return false;
    r.setPattern("\\w+\\s*[][\\w]+\\s*=\\s*(?:0x|)([0-9A-Fa-f]+)(?:\\W.*|)");
  } else {
    if ( !hardware()->execute(name, true, &lines) ) return false;
    r.setPattern("[^=]*=\\s*(?:0x|\\$)([0-9A-Fa-f]+)(?:\\W.*|)");
  }
  uint i = 0;
  for (; i<uint(lines.count()); i++)
    if ( r.exactMatch(lines[i]) ) break;
  if ( i==uint(lines.count()) ) {
    log(Log::LineType::Error, i18n("Error reading register \"%1\"").tqarg(name));
    return false;
  }
  value = fromHex(r.cap(1), 0);
  return true;
}

bool GPSim::Debugger::getRegister(Address address, BitValue &value)
{
  const Pic::RegistersData &rdata = device()->registersData();
  TQString name = toHex(address, rdata.nbCharsAddress());
  if ( hardware()->version()<VersionData(0, 22, 0) ) return getRegister("0x" + name, value);
  return getRegister(TQString("ramData[$%1]").tqarg(name), value);
}

bool GPSim::Debugger::readRegister(const Register::TypeData &data, BitValue &value)
{
  if ( data.type()==Register::Special ) {
    if ( data.name()=="WREG" ) return readWreg(value);
    if ( data.name()=="PC" ) return getRegister("pc", value);
    Q_ASSERT(false);
    return true;
  }
  TQString name = device()->registersData().sfrNames[data.address()];
  if ( name=="WREG" ) return readWreg(value);
  if ( !name.isEmpty() ) return getRegister(name.lower(), value);
  return getRegister(data.address(), value);
}

bool GPSim::Debugger::setRegister(const TQString &name, BitValue value)
{
  if ( hardware()->version()<VersionData(0, 22, 0) ) {
    log(Log::LineType::Warning, i18n("Writing registers is not supported by this version of gpsim"));
    return true;
  }
  const Pic::RegistersData &rdata = device()->registersData();
  TQString s = TQString("%1 = %2").tqarg(name).tqarg(toHexLabel(value, rdata.nbChars()));
  return hardware()->execute(s, true);
}

bool GPSim::Debugger::setRegister(Address address, BitValue value)
{
  const Pic::RegistersData &rdata = device()->registersData();
  TQString s = TQString("ramData[$%1]").tqarg(toHex(address, rdata.nbCharsAddress()));
  return setRegister(s, value);
}

bool GPSim::Debugger::writeRegister(const Register::TypeData &data, BitValue value)
{
  if ( data.type()==Register::Special ) {
    if ( data.name()=="WREG" ) return writeWreg(value);
    if ( data.name()=="PC" ) {
      log(Log::LineType::Warning, i18n("Writing PC is not supported by gpsim"));
      return true;
    }
    Q_ASSERT(false);
    return false;
  }
  const Pic::RegistersData &rdata = device()->registersData();
  TQString name = rdata.sfrNames[data.address()];
  if ( !name.isEmpty() ) return setRegister(name.lower(), value);
  return setRegister(data.address(), value);
}

bool GPSim::Debugger::writeWreg(BitValue value)
{
  return setRegister("W", value);
}

bool GPSim::Debugger::updatePorttqStatus(uint index, TQMap<uint, Device::PortBitData> &bits)
{
  for (uint i=0; i<Device::MAX_NB_PORT_BITS; i++) {
    if ( !device()->registersData().hasPortBit(index, i) ) continue;
    TQString name = device()->registersData().portName(index).lower() + TQString::number(i);
    TQStringList lines;
    if ( !hardware()->execute("symbol " + name, true, &lines) ) return false;
    TQString pattern = "^(\\w+)=([^\\s])+\\s*", value;
    if ( !findRegExp(lines, pattern, "bitState", value) || value.length()!=1 ) {
      log(Log::LineType::Error, i18n("Error reading state of IO bit: %1").tqarg(name));
      return false;
    }
    switch (value[0].latin1()) {
      case 'H':
      case '1': bits[i].state = Device::High; break;
      case 'L':
      case '0': bits[i].state = Device::Low; break;
      case 'W': bits[i].state = Device::WeakPullUp; break;
      case 'w': bits[i].state = Device::WeakPullDown; break;
      case 'Z': bits[i].state = Device::HighImpedance; break;
      case 'X': bits[i].state = Device::Unknown; break;
      default:
        bits[i].state = Device::Unknown;
        log(Log::LineType::Warning, i18n("Unknown state for IO bit: %1 (%2)").tqarg(name).tqarg(value));
        break;
    }
    if ( !findRegExp(lines, pattern, "Driving", value) || value.length()!=1 ) {
      log(Log::LineType::Error, i18n("Error reading driving state of IO bit: %1").tqarg(name));
      return false;
    }
    bits[i].driving = ( value[0]=='1' );
    if (bits[i].driving) {
      if ( !findRegExp(lines, pattern, "drivingState", value) || value.length()!=1 ) {
        log(Log::LineType::Error, i18n("Error reading driving state of IO bit: %1").tqarg(name));
        return false;
      }
      bits[i].drivingState = (value[0]=='0' ? Device::IoLow : Device::IoHigh);
      bits[i].drivenState = Device::IoUnknown;
    } else {
      if ( !findRegExp(lines, pattern, "drivenState", value) || value.length()!=1 ) {
        log(Log::LineType::Error, i18n("Error reading driven state of IO bit: %1").tqarg(name));
        return false;
      }
      bits[i].drivenState = (value[0]=='0' ? Device::IoLow : Device::IoHigh);
      bits[i].drivingState = Device::IoUnknown;
    }
  }
  return true;
}

//----------------------------------------------------------------------------
TQString GPSim::Group::statusLabel() const
{
  return i18n("GPSim (4MHz)"); // #### FIXME: add config
}

void GPSim::Group::initSupported()
{
  ProcessManager manager(0);
  if ( !manager.start() ) return;
  VersionData version;
  if ( !manager.getVersion(version) ) return;
  bool oldGpsim = ( version<VersionData(0, 21, 11) );
  if ( !manager.sendCommand("processor list", true) ) return;
  TQStringList devices = TQStringList::split(" ", manager.process().sout().join(" "));
  for (uint i=0; i<uint(devices.count()); i++) {
    TQString s = devices[i].upper();
    if ( s.startsWith("PIC") ) s = s.mid(3);
    const Pic::Data *data = static_cast<const Pic::Data *>(Device::lister().data(s));
    if (data) {
      if ( data->architecture()==Pic::Architecture::P18F && oldGpsim ) continue;
      addDevice(data->name(), data, ::Group::Support::Tested);
    }
  }
}

Programmer::Hardware *GPSim::Group::createHardware(::Programmer::Base &base, const ::Programmer::HardwareDescription &) const
{
  return new Hardware(base);
}

Programmer::DeviceSpecific *GPSim::Group::createDeviceSpecific(::Programmer::Base &base) const
{
  return new DeviceSpecific(base);
}