summaryrefslogtreecommitdiffstats
path: root/src/devices/pic/prog/pic_prog_specific.cpp
blob: c8d958b37a79f216efcc9df2ac36ab42c245d499 (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
/***************************************************************************
 *   Copyright (C) 2005-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 "pic_prog_specific.h"

#include "common/global/global.h"

//-----------------------------------------------------------------------------
const char * const Pic::VOLTAGE_TYPE_LABELS[Nb_VoltageTypes] = {
  I18N_NOOP("Programmer Vpp"), I18N_NOOP("Target Vdd"), I18N_NOOP("Target Vpp")
};

const char * const Pic::TARGET_MODE_LABELS[Nb_TargetModes] = {
  I18N_NOOP("Stopped"), I18N_NOOP("Running"), I18N_NOOP("In Programming")
};

const char * const Pic::RESET_MODE_LABELS[Nb_ResetModes] = {
  I18N_NOOP("Reset Held"), I18N_NOOP("Reset Released")
};

//-----------------------------------------------------------------------------
uint Programmer::PicDeviceSpecific::findNonMaskStart(Pic::MemoryRangeType type, const Device::Array &data) const
{
  uint start = 0;
  for (; start<data.count(); start++)
    if ( data[start]!=device().mask(type) ) break;
  const_cast<PicDeviceSpecific *>(this)->log(Log::DebugLevel::Normal, TQString("start before align: %1").tqarg(start));
  uint align = device().nbWordsWriteAlignment(type);
  start -= start % align;
  const_cast<PicDeviceSpecific *>(this)->log(Log::DebugLevel::Normal, TQString("start after align: %1 (align=%2)").tqarg(start).tqarg(align));
  return start;
}

uint Programmer::PicDeviceSpecific::findNonMaskEnd(Pic::MemoryRangeType type, const Device::Array &data) const
{
  uint end = data.count()-1;
  for (; end>0; end--)
    if ( data[end]!=device().mask(type) ) break;
  const_cast<PicDeviceSpecific *>(this)->log(Log::DebugLevel::Normal, TQString("end before align: %1").tqarg(end));
  uint align = device().nbWordsWriteAlignment(type);
  if ( (end+1) % align ) end += align - (end+1) % align;
  const_cast<PicDeviceSpecific *>(this)->log(Log::DebugLevel::Normal, TQString("end after align: %1 (align=%2)").tqarg(end).tqarg(align));
  Q_ASSERT(end<data.count());
  return end;
}

bool Programmer::PicDeviceSpecific::read(Pic::MemoryRangeType type, Device::Array &data, const VerifyData *vdata)
{
  setPowerOn();
  bool ok = doRead(type, data, vdata);
  setPowerOff();
  return ok;
}

bool Programmer::PicDeviceSpecific::write(Pic::MemoryRangeType mtype, const Device::Array &data, bool force)
{
  setPowerOn();
  bool ok = doWrite(mtype, data, force);
  setPowerOff();
  return ok;
}

bool Programmer::PicDeviceSpecific::erase(bool isProtected)
{
  setPowerOn();
  bool ok = doErase(isProtected);
  setPowerOff();
  return ok;
}

bool Programmer::PicDeviceSpecific::eraseRange(Pic::MemoryRangeType type)
{
  setPowerOn();
  bool ok = doEraseRange(type);
  setPowerOff();
  return ok;
}

bool Programmer::PicDeviceSpecific::doEmulatedEraseRange(Pic::MemoryRangeType type)
{
  Pic::Memory memory(device());
  if ( !doWrite(type, memory.arrayForWriting(type), true) ) return false;
  if ( !canReadRange(type) ) return true;
  VerifyData vdata(BlankCheckVerify, memory);
  Device::Array data;
  return doRead(type, data, &vdata);
}

//-----------------------------------------------------------------------------
bool Programmer::PicHardware::compareWords(Pic::MemoryRangeType type, uint index, BitValue v, BitValue d, Programmer::VerifyActions actions)
{
  if ( v==d ) return true;
  uint inc = device().addressIncrement(type);
  Address address = device().range(type).start + inc * index;
  if ( actions & ::Programmer::BlankCheckVerify )
    log(Log::LineType::SoftError, i18n("Device memory is not blank (in %1 at address %2: reading %3 and expecting %4).")
      .tqarg(type.label()).tqarg(toHexLabel(address, device().nbCharsAddress()))
      .tqarg(toHexLabel(d, device().nbCharsWord(type))).tqarg(toHexLabel(v, device().nbCharsWord(type))));
  else log(Log::LineType::SoftError, i18n("Device memory does not match hex file (in %1 at address %2: reading %3 and expecting %4).")
      .tqarg(type.label()).tqarg(toHexLabel(address, device().nbCharsAddress()))
      .tqarg(toHexLabel(d, device().nbCharsWord(type))).tqarg(toHexLabel(v, device().nbCharsWord(type))));
  return false;
}

bool Programmer::PicHardware::verifyWord(uint i, BitValue word, Pic::MemoryRangeType type, const VerifyData &vdata)
{
  if ( !(vdata.actions & ::Programmer::IgnoreProtectedVerify) && vdata.protectedRanges.contains(i) ) return true; // protected
  BitValue v = static_cast<const Pic::Memory &>(vdata.memory).normalizedWord(type, i);
  BitValue d = static_cast<const Pic::Memory &>(vdata.memory).normalizeWord(type, i, word);
  if ( type==Pic::MemoryRangeType::Config ) {
    BitValue pmask = device().config()._words[i].pmask;
    v = v.clearMaskBits(pmask);
    d = d.clearMaskBits(pmask);
  }
  return compareWords(type, i, v, d, vdata.actions);
}