summaryrefslogtreecommitdiffstats
path: root/src/devices/pic/base/pic.cpp
blob: 75e6bf43c23c50cf7f9046e9d8ba93a4da50364f (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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/***************************************************************************
 *   Copyright (C) 2005 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 "pic.h"

#include "common/global/global.h"
#include "common/common/misc.h"
#include "common/global/purl.h"
#include "pic_register.h"
#include "pic_config.h"

//------------------------------------------------------------------------------
const Pic::ProgVoltageType::Data Pic::ProgVoltageType::DATA[Nb_Types] = {
  { "vpp",            0 },
  { "vdd_prog",       0 },
  { "vdd_prog_write", 0 }
};

const Pic::MemoryRangeType::Data Pic::MemoryRangeType::DATA[Nb_Types] = {
  { "code",               I18N_NOOP("Code memory"),         Writable },
  { "calibration",        I18N_NOOP("Calibration"),         Writable },
  { "user_ids",           I18N_NOOP("User IDs"),            Writable },
  { "device_id",          I18N_NOOP("Device ID"),           ReadOnly },
  { "config",             I18N_NOOP("Configuration Bits"),  Writable },
  { "eeprom",             I18N_NOOP("Data EEPROM"),         Writable },
  { "debug_vector",       I18N_NOOP("Debug Vector"),        Writable },
  { "hardware_stack",     I18N_NOOP("Hardware Stack"),      ReadOnly },
  { "calibration_backup", I18N_NOOP("Calibration Backup"),  Writable },
  { "program_executive",  I18N_NOOP("Program Executive"),   Writable }
};

const Pic::SelfWrite::Data Pic::SelfWrite::DATA[Nb_Types] = {
  { "yes", 0 },
  { "no",  0 }
};

const Pic::DeviceType::Data Pic::DeviceType::DATA[Nb_Types] = {
  { 0, I18N_NOOP("Normal") },
  { 0, I18N_NOOP("J")      },
  { 0, I18N_NOOP("K")      }
};

const Pic::Architecture::Data Pic::Architecture::DATA[Nb_Types] = {
//  name family_label nbBytesPC nbBytesWord packed nbBitsRegister registerBankLength
//  {Code, Cal, UserID, DevId, Conf, EEPROM, DebugVec, HardStack, CalBackup, Program Executive} randomAccess
  { "10X", I18N_NOOP("Baseline Family"),  0, 2, false,  8, 0x020, { 12, 12, 12, 12, 12,  8, 12,  0, 12,  0 }, false, SelfWrite::No,       DeviceType::Normal }, // 9, 10, 11 or 12-bit program counter
  { "16X", I18N_NOOP("Midrange Family"), 13, 2, false,  8, 0x080, { 14, 14, 14, 14, 14,  8, 14,  0, 14,  0 }, false, SelfWrite::Nb_Types, DeviceType::Normal }, // max eeprom: 256 words
  { "17C", I18N_NOOP("17C Family"),      16, 2, false,  8, 0x100, { 16,  0,  0,  0, 16,  8,  0,  0,  0,  0 }, true,  SelfWrite::No,       DeviceType::Normal },
  { "18C", I18N_NOOP("18C Family"),      21, 2, true,   8, 0x100, { 16,  8,  8,  8,  8,  8, 16,  0,  8,  0 }, true,  SelfWrite::No,       DeviceType::Normal },
  { "18F", I18N_NOOP("18F Family"),      21, 2, true,   8, 0x100, { 16,  8,  8,  8,  8,  8, 16,  0,  8,  0 }, true,  SelfWrite::Nb_Types, DeviceType::Normal },
  { "18J", I18N_NOOP("18J Family"),      21, 2, true,   8, 0x100, { 16,  8,  8,  8,  8,  8, 16,  0,  8,  0 }, true,  SelfWrite::Yes,      DeviceType::J      },
  { "24F", I18N_NOOP("24F Family"),      23, 4, false, 16, 0x800, { 24,  0,  0, 16, 24,  0, 24,  0,  0, 24 }, true,  SelfWrite::Yes,      DeviceType::J      },
  { "24H", I18N_NOOP("24H Family"),      23, 4, false, 16, 0x800, { 24,  0,  8, 16,  8,  0, 24,  0,  0, 24 }, true,  SelfWrite::Yes,      DeviceType::J      },
  { "30F", I18N_NOOP("30F Family"),      23, 4, false, 16, 0xA00, { 24,  0, 24, 16, 16, 16, 24,  0,  0, 24 }, true,  SelfWrite::Yes,      DeviceType::Normal }, // dsPIC: eeprom max = 2 kwords = 4 kbytes
  { "33F", I18N_NOOP("33F Family"),      23, 4, false, 16, 0x800, { 24,  0,  8, 16,  8,  0, 24,  0,  0, 24 }, true,  SelfWrite::Yes,      DeviceType::J      }
};

const Pic::Checksum::Algorithm::Data Pic::Checksum::Algorithm::DATA[Nb_Types] = {
  { "",      0 },
  { "XOR4",  0 },
  { "XNOR7", 0 },
  { "XNOR8", 0 }
};

const Pic::Feature::Data Pic::Feature::DATA[Nb_Types] = {
  { "ccp", I18N_NOOP("CCP")      },
  { "adc", I18N_NOOP("ADC")      },
  { "ssp", I18N_NOOP("SSP")      },
  { "lvd", I18N_NOOP("Low Voltage Detect") },
  { "usb", I18N_NOOP("USB")      },
  { "usart",           I18N_NOOP("USART")    },
  { "can",             I18N_NOOP("CAN")      },
  { "ecan",            I18N_NOOP("ECAN")     },
  { "ethernet",        I18N_NOOP("Ethernet") },
  { "lcd",             I18N_NOOP("LCD")      },
  { "motor_control",   I18N_NOOP("Motor Control") },
  { "motion_feedback", I18N_NOOP("Motion Feeback") },
  { "self_write",      I18N_NOOP("Self-Write") }
};

//-----------------------------------------------------------------------------
Pic::Data::Data()
  : Device::Data(new RegistersData(*this))
{
  FOR_EACH(ProgVoltageType, type) {
    _voltages[type].min = 0.0;
    _voltages[type].max = 0.0;
    _voltages[type].nominal = 0.0;
  }
  FOR_EACH(MemoryRangeType, type) {
    _ranges[type].properties = NotPresent;
    _ranges[type].start = 0;
    _ranges[type].end = 0;
    _ranges[type].hexFileOffset = 0;
  }
  _config = new Config(*this);
  _calibration.opcode = 0;
  _calibration.opcodeMask = 0;
}

Pic::Data::~Data()
{
  delete _config;
}

bool Pic::Data::isReadable(MemoryRangeType type) const
{
  return ( range(type).properties & Programmable );
}

bool Pic::Data::isWritable(MemoryRangeType type) const
{
  return ( (type.data().properties & Writable) && (range(type).properties & Programmable) );
}

uint Pic::Data::addressIncrement(MemoryRangeType type) const
{
  uint inc = _architecture.data().nbBytesWord;
  if ( _architecture.data().packed
       && ( type==MemoryRangeType::Code || type==MemoryRangeType::DebugVector ) ) return inc;
  return inc / 2;
}

uint Pic::Data::nbWords(MemoryRangeType type) const
{
  if ( !isPresent(type) ) return 0;
  return nbAddresses(type) / addressIncrement(type);
}

uint Pic::Data::nbAddresses(MemoryRangeType type) const
{
  if ( !isPresent(type) ) return 0;
  return (range(type).end - range(type).start + 1);
}

TQString Pic::Data::fname(Device::Special special) const
{
  TQString s = name();
  switch (special.type()) {
    case Device::Special::Normal: break;
    case Device::Special::LowPower:
      // assume name is of form "NNX..."
      s.insert(2, 'L');
      break;
    case Device::Special::LowVoltage:
      // assume name is of form "NNXN..."
      s.replace(2, 1, "LV");
      break;
    case Device::Special::HighVoltage:
      // assume name is of form "NNXN..."
      s.replace(2, 1, "HV");
      break;
    case Device::Special::Nb_Types: Q_ASSERT(false); break;
  }
  return s;
}

bool Pic::Data::matchId(BitValue rawId, Device::IdData &idata) const
{
  if ( !isPresent(MemoryRangeType::DeviceId) ) return false;
  TQMap<Device::Special, BitValue>::const_iterator it;
  for (it=_ids.begin(); it!=_ids.end(); ++it) {
    idata.special = it.key();
    BitValue nid = 0x0;
    switch (architecture().type()) {
      case Architecture::P10X:
      case Architecture::P16X:
      case Architecture::P17C:
      case Architecture::P18C:
      case Architecture::P18F:
      case Architecture::P18J:
        nid = rawId.clearMaskBits(0x1F);
        idata.revision = rawId.maskWith(0x1F);
        break;
      case Architecture::P24F:
        nid = (rawId >> 16).maskWith(0x3FFF);
        idata.revision = (rawId >> 6).maskWith(0x7);
        idata.minorRevision = rawId.maskWith(0x7);
        break;
      case Architecture::P30F:
        nid = (rawId >> 16).maskWith(0xFFFF);
        idata.revision = (rawId >> 6).maskWith(0x3F);
        idata.minorRevision = rawId.maskWith(0x3F);
        idata.process = (rawId >> 12).maskWith(0xF);
        break;
      case Architecture::P24H:
      case Architecture::P33F:
        nid = (rawId >> 16).maskWith(0xFFFF);
        idata.revision = rawId.maskWith(0xFFFF); // ??
        break;
      case Architecture::Nb_Types: Q_ASSERT(false); break;
    }
    if ( nid==it.data() ) return true;
  }
  return false;
}

TQStringList Pic::Data::idNames(const TQMap<TQString, Device::IdData> &ids) const
{
  TQStringList list;
  TQMap<TQString, Device::IdData>::const_iterator it;
  for (it=ids.begin(); it!=ids.end(); ++it) {
    switch (_architecture.type()) {
      case Architecture::P10X:
      case Architecture::P16X:
      case Architecture::P17C:
      case Architecture::P18C:
      case Architecture::P18F:
      case Architecture::P18J:
        list += i18n("%1 (rev. %2)").arg(it.key()).arg(toLabel(it.data().revision));
        break;
      case Architecture::P24F:
        list += i18n("%1 (rev. %2.%3)").arg(it.key()).arg(toLabel(it.data().revision)).arg(toLabel(it.data().minorRevision));
        break;
      case Architecture::P30F:
        list += i18n("%1 (proc. %2; rev. %3.%4)").arg(it.key()).arg(toLabel(it.data().process)).arg(toLabel(it.data().revision)).arg(toLabel(it.data().minorRevision));
        break;
      case Architecture::P24H:
      case Architecture::P33F:
        list += i18n("%1 (rev. %2)").arg(it.key()).arg(toLabel(it.data().revision));
        break;
      case Architecture::Nb_Types: Q_ASSERT(false); break;
    }
  }
  return list;
}

bool Pic::Data::checkCalibration(const Device::Array &data, TQString *message) const
{
  Q_ASSERT( nbWords(MemoryRangeType::Cal)==data.count() );
  for (uint i=0; i<data.count(); i++) {
    TQString address = toHexLabel(range(MemoryRangeType::Cal).start + i*addressIncrement(MemoryRangeType::Cal), nbCharsAddress());
    if ( data[i]==mask(MemoryRangeType::Cal) ) {
      if (message) *message = i18n("Calibration word at address %1 is blank.").arg(address);
      return false;
    }
  }
  if ( data.count()==1 ) {
    if ( data[0].maskWith(_calibration.opcodeMask)!=_calibration.opcode ) {
      if (message) *message = i18n("Calibration word is not a compatible opcode (%2).")
                              .arg(toHexLabel(_calibration.opcode, nbCharsWord(MemoryRangeType::Code)));
      return false;
    }
  }
  return true;
}

const Pic::RegistersData &Pic::Data::registersData() const
{
  return static_cast<const RegistersData &>(*_registersData);
}

bool Pic::Data::hasFeature(Feature feature, bool *unknown) const
{
  bool ok = ( registersData().nbBanks!=0 );
  if (unknown) *unknown = !ok;
  if (!ok) return false;
  switch (feature.type()) {
    case Feature::CCP: return registersData().sfrs.contains("CCP1CON");
    case Feature::ADC: return registersData().sfrs.contains("ADCON0");
    case Feature::SSP: return registersData().sfrs.contains("SSPCON");
    case Feature::LVD: return registersData().sfrs.contains("LVDCON");
    case Feature::USB: return registersData().sfrs.contains("UCON");
    case Feature::USART:
      return ( registersData().sfrs.contains("TXSTA")        // 16F
               || registersData().sfrs.contains("TXSTA1")    // 18F
               || registersData().sfrs.contains("U1MODE") ); // 30F
    case Feature::CAN: return registersData().sfrs.contains("CANCON") && !registersData().sfrs.contains("ECANCON");
    case Feature::ECAN: return registersData().sfrs.contains("ECANCON");
    case Feature::Ethernet: return registersData().sfrs.contains("ETHCON1");
    case Feature::LCD: return registersData().sfrs.contains("LCDCON");
    case Feature::MotorControl: return registersData().sfrs.contains("PWMCON0");
    case Feature::MotionFeedback: return registersData().sfrs.contains("CAP1CON");
    case Feature::SelfWrite: return _selfWrite==SelfWrite::Yes;
    case Feature::Nb_Types: Q_ASSERT(false); break;
  }
  return false;
}

Device::Array Pic::Data::gotoInstruction(Address address, bool withPageSelection) const
{
  Q_ASSERT( address<addressIncrement(MemoryRangeType::Code)*nbWords(MemoryRangeType::Code) );
  Device::Array a;
  switch (_architecture.type()) {
  case Architecture::P10X:
    if ( nbWords(MemoryRangeType::Code)>0x1FF && withPageSelection)
      a.append(0x4A3 | (address>0x1FF ? 0x100 : 0x000)); // bsf STATUS,PA0 or bcf STATUS,PA0
    a.append(0xA00 | (address.toUInt() & 0x1FF)); // goto
    break;
  case Architecture::P16X:
    if ( nbWords(MemoryRangeType::Code)>0x7FF && withPageSelection ) {
      if ( address<=0x7FF ) a.append(0x018A); // clrf PCLATH
      else {
        a.append(0x3000 | (address.toUInt() >> 8)); // movl high address
        a.append(0x008A); // movwf PCLATH
      }
    }
    a.append(0x2800 | (address.toUInt() & 0x7FF));
    break;
  case Architecture::P17C:
    a.append(0xC000 | (address.toUInt() & 0x1FFF));
    break;
  case Architecture::P18C:
  case Architecture::P18F:
  case Architecture::P18J:
    a.append(0xEF00 | ((address.toUInt()/2) & 0xFF));
    a.append(0xF000 | ((address.toUInt()/2) >> 8));
    break;
  case Architecture::P24F:
  case Architecture::P24H:
  case Architecture::P30F:
  case Architecture::P33F:
    a.append(0x040000 | (address.toUInt() & 0x00FFFE));
    a.append(0X000000 | (address.toUInt() >> 16));
    break;
  case Architecture::Nb_Types: Q_ASSERT(false); break;
  }
  return a;
}

bool Pic::Data::isGotoInstruction(BitValue instruction) const
{
  switch (_architecture.type()) {
  case Architecture::P10X: return ( instruction.maskWith(0xE00)==0xA00 );
  case Architecture::P16X: return ( instruction.maskWith(0xF800)==0x2800 );
  case Architecture::P17C: return ( instruction.maskWith(0xE000)==0xC000 );
  case Architecture::P18C:
  case Architecture::P18F:
  case Architecture::P18J: return ( instruction.maskWith(0xFF00)==0xEF00 );
  case Architecture::P24F:
  case Architecture::P24H:
  case Architecture::P30F:
  case Architecture::P33F: return ( instruction.maskWith(0xFF0000)==0x040000 );
  case Architecture::Nb_Types: Q_ASSERT(false); break;
  }
  return false;
}

uint Pic::Data::nbWordsWriteAlignment(MemoryRangeType type) const
{
  if ( type!=MemoryRangeType::Code ) return 1;
  return TQMAX(_nbWordsCodeWrite, uint(16));
}

//----------------------------------------------------------------------------
TQDataStream &operator <<(TQDataStream &s, const Pic::VoltageData &vd)
{
  s << vd.min << vd.max << vd.nominal;
  return s;
}
TQDataStream &operator >>(TQDataStream &s, Pic::VoltageData &vd)
{
  s >> vd.min >> vd.max >> vd.nominal;
  return s;
}

TQDataStream &operator <<(TQDataStream &s, const Pic::MemoryRangeData &mrd)
{
  s << TQ_UINT8(mrd.properties) << mrd.start << mrd.end << mrd.hexFileOffset;
  return s;
}
TQDataStream &operator >>(TQDataStream &s, Pic::MemoryRangeData &mrd)
{
  TQ_UINT8 properties;
  s >> properties >> mrd.start >> mrd.end >> mrd.hexFileOffset;
  mrd.properties = Pic::MemoryRangeProperties(properties);
  return s;
}

TQDataStream &operator <<(TQDataStream &s, const Pic::Checksum::Data &cd)
{
  s << cd.constant << cd.bbsize << cd.algorithm << cd.protectedMaskNames;
  s << cd.blankChecksum << cd.checkChecksum;
  return s;
}
TQDataStream &operator >>(TQDataStream &s, Pic::Checksum::Data &cd)
{
  s >> cd.constant >> cd.bbsize >> cd.algorithm >> cd.protectedMaskNames;
  s >> cd.blankChecksum >> cd.checkChecksum;
  return s;
}

TQDataStream &operator <<(TQDataStream &s, const Pic::CalibrationData &cd)
{
  s << cd.opcode << cd.opcodeMask;
  return s;
}
TQDataStream &operator >>(TQDataStream &s, Pic::CalibrationData &cd)
{
  s >> cd.opcode >> cd.opcodeMask;
  return s;
}

TQDataStream &Pic::operator <<(TQDataStream &s, const Pic::Data &data)
{
  s << static_cast<const Device::Data &>(data);
  s << data._architecture << data._ids << data._nbBitsPC;
  s << data._voltages << data._ranges;
  s << data._userIdRecommendedMask;
  s << *data._config;
  s << data._checksums;
  s << data._calibration;
  s << static_cast<const Pic::RegistersData &>(*data._registersData);
  s << data._nbWordsCodeWrite << data._nbWordsCodeRowErase;
  s << data._selfWrite;
  return s;
}
TQDataStream &Pic::operator >>(TQDataStream &s, Pic::Data &data)
{
  s >> static_cast<Device::Data &>(data);
  s >> data._architecture >> data._ids >> data._nbBitsPC;
  s >> data._voltages >> data._ranges;
  s >> data._userIdRecommendedMask;
  s >> *data._config;
  s >> data._checksums;
  s >> data._calibration;
  s >> static_cast<Pic::RegistersData &>(*data._registersData);
  s >> data._nbWordsCodeWrite >> data._nbWordsCodeRowErase;
  s >> data._selfWrite;
  return s;
}