summaryrefslogtreecommitdiffstats
path: root/src/devices/base/hex_buffer.cpp
blob: f389c92f205c9830d03d99b13b3b7d02dce81f75 (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
/***************************************************************************
 *   Copyright (C) 2005-2007 Nicolas Hadacek <hadacek@kde.org>             *
 *             (C) 2003 by Alain Gibaud                                    *
 *                                                                         *
 *   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 "hex_buffer.h"

#include <tqtextstream.h>

#include "devices/base/generic_device.h"

//-----------------------------------------------------------------------------
const char * const HexBuffer::FORMATS[Nb_Formats] = {
  "inhx8m", /*"inhx8s", */"inhx16", "inhx32"
};

void HexBuffer::savePartial(TQTextStream &stream, Format format) const
{
  BitValue oldseg;
  const_iterator block = begin();
  int len;
  while ( fetchNextBlock(block, end(), &len) ) {
    // block found, write it
    BitValue seg = block.key() >> 15 ; // 2 * seg address
    if ( format==IHX32 && seg!=oldseg ) {
      char buf[50];
      BitValue check = 0x02 + 0x04 + seg.byte(1) + seg.byte(0);
      sprintf(buf, ":02000004%04X%02X\n", seg.toUInt(), check.twoComplement().byte(0));
      stream << buf;
      oldseg = seg;
    }
    writeHexBlock(stream, len, block, format);
  }
}

void HexBuffer::saveEnd(TQTextStream &stream) const
{
  stream << ":00000001FF\n";
}

/* Write one line of Intel-hex file
 * Original code source from Timo Rossi,
 * modified by Alain Gibaud to support large blocks write
 */
void HexBuffer::writeHexBlock(TQTextStream &stream, int reclen, // length (in words)
			  const_iterator& data, // pointer to 1st data word (incremented by function)
                          Format format)
{
    while ( reclen>HEXBLKSIZE ) {
      writeHexBlock(stream, HEXBLKSIZE, data, format);
      reclen -= HEXBLKSIZE;
    }
    if ( reclen<=0 ) return; /* Oops, block has just a HEXBLKSIZE * n size */

    char buf[20];
    BitValue check = 0x0;

    // line start
    uint loc =  data.key();
    switch (format) {
      case IHX8M:
      case IHX32:
        loc *= 2;
        sprintf(buf,  ":%02X%04X00", 2*reclen, loc & 0xFFFF);
        check += ((loc) & 0xff) + (((loc) >> 8) & 0xff) + 2*reclen;
        break;
      case IHX16:
        sprintf(buf, ":%02X%04X00", reclen, loc & 0xFFFF);
        check += (loc & 0xff) + ((loc >> 8) & 0xff) + reclen;
        break;
      case Nb_Formats: Q_ASSERT(false); break;
    }
    stream << buf;

    // data
    for (; reclen > 0; ++data, --reclen) {
      BitValue word =  data.data();
      switch (format) {
        case IHX8M:
        case IHX32:
          sprintf(buf, "%02X%02X", word.byte(0), word.byte(1));
          break;
        case IHX16:
          sprintf(buf, "%02X%02X", word.byte(1), word.byte(0));
          break;
        case Nb_Formats: Q_ASSERT(false); break;
      }
      stream << buf;
      check += word.byte(0) + word.byte(1);
    }

    // checksum, assumes 2-complement
    sprintf(buf, "%02X\n", check.twoComplement().byte(0));
    stream << buf;
}

/* -------------------------------------------------------------------------
    This routine detects the next block to output
    A block is a set of consecutive addresse words not
    containing 0xFFFFFFFF
    @return true if a block has been detected
    'it' is updated to point to the first address of the block
    '*len' contains the size of the block
*/
bool HexBuffer::fetchNextBlock(const_iterator& it, const const_iterator &end, int *len)
{
    uint startadr, curadr;
    // for( i = *start ; (i < MAXPICSIZE) && (Mem[i] == INVALID) ; ++i) ;
    // skip non-used words

    // Search block start
    while ( it!=end && !it.data().isInitialized() ) ++it;

    // if(i >= MAXPICSIZE ) return false ;
    if ( it==end ) return false;

    //for( *start = i ; (i < MAXPICSIZE) && (Mem[i] != INVALID) ; ++i) ;
    //*len = i - *start ;
    // search block end - a block may not cross a segment boundary
    const_iterator itt(it) ;
    for (curadr = startadr = itt.key(), ++itt; itt!=end; ++itt) {
      if ( itt.key()!=curadr+1 ) break;    // non contiguous addresses
      if ( !itt.data().isInitialized() ) break;   // unused word found
      if ( ((itt.key()) & 0xFFFF0000U)!=(curadr & 0xFFFF0000U) ) break; // cross segment boundary
      curadr = itt.key();
    }
    *len = curadr - startadr + 1 ;

    return *len != 0  ;
}

TQString HexBuffer::ErrorData::message() const
{
  switch (type) {
    case UnrecognizedFormat: return i18n("Unrecognized format (line %1).").tqarg(line);
    case UnexpectedEOF: return i18n("Unexpected end-of-file.");
    case UnexpectedEOL: return i18n("Unexpected end-of-line (line %1).").tqarg(line);
    case WrongCRC: return i18n("CRC mismatch (line %1).").tqarg(line);
  }
  Q_ASSERT(false);
  return TQString();
}

bool HexBuffer::load(TQTextStream &stream, TQStringList &errors)
{
  Format format;
  TQValueList<ErrorData> list = load(stream, format);
  if ( list.isEmpty() ) return true;
  errors.clear();
  for (uint i=0; i<uint(list.count()); i++) errors += list[i].message();
  return false;
}

TQValueList<HexBuffer::ErrorData> HexBuffer::load(TQTextStream &stream, Format &format)
{
  clear();
  format = Nb_Formats;
  TQValueList<HexBuffer::ErrorData> errors;
  load(stream, format, errors);
  if ( format==Nb_Formats ) format = IHX8M; // default
  return errors;
}

/* -------------------------------------------------------------------------
   Read a Intel HEX file of either INHX16 or INHX8M format type, detecting
   the format automagicly by the wordcount and length of the line
   Tested in 8 and 16 bits modes
   ------------------------------------------------------------------------ */
void HexBuffer::load(TQTextStream &stream, Format &format, TQValueList<ErrorData> &errors)
{
    uint addrH = 0; // upper 16 bits of 32 bits address (inhx32 format)
    uint line = 1;

    for (; !stream.atEnd(); line++) { // read each line
        TQString s = stream.readLine();
        if ( !s.startsWith(":") ) continue; // skip invalid intel hex line
        s = s.stripWhiteSpace(); // clean-up white spaces at end-of-line
        if ( s==":" ) continue; // skip empty line

        const char *p = s.latin1();
        p += 1; // skip ':'
        uint bytecount = (s.length()-11) / 2; // number of data bytes of this record

        // get the byte count, the address and the type for this line.
        uint count, addr, type;
        if ( sscanf(p, "%02X%04X%02X", &count , &addr, &type)!=3 ) {
          errors += ErrorData(line, UnrecognizedFormat);
          return;
        }
        p += 8;
        uint cksum = count + (addr >> 8) + (addr & 0xFF) + type;

        if( type==0x01 ) { // EOF field :00 0000 01 FF
            uint data;
            if ( sscanf(p, "%02X", &data)!=1 ) errors += ErrorData(line, UnexpectedEOL);
            else if ( ((cksum+data) & 0xFF)!=0 ) errors += ErrorData(line, WrongCRC);
            return;
        }

        if ( type==0x04 ) { // linear extended record (for 0x21xxxx, :02 0000 04 0021 D9)
            if( sscanf(p, "%04X", &addrH)!=1 ) {
              errors += ErrorData(line, UnrecognizedFormat); // bad address record
              return;
            }
            p += 4;
            cksum += (addrH & 0xFF);
            cksum += (addrH >> 8);
            if ( format==Nb_Formats || format==IHX8M ) format = IHX32;
            else if ( format!=IHX32 ) {
              errors += ErrorData(line, UnrecognizedFormat); // inconsistent format
              return;
            }
            uint data;
            if ( sscanf(p, "%02X", &data)!=1 ) errors += ErrorData(line, UnexpectedEOL);
            else if ( ((cksum+data) & 0xFF)!=0 ) errors += ErrorData(line, WrongCRC);
            //qDebug("new address high: %s", toHex(addrH<<16, 8).data());
            continue; // goto next record
        }

        /* Figure out if its INHX16 or INHX8M
            if count is a 16 bits words count => INHX16
            if count is a byte count => INHX8M or INHX32 */
        if ( bytecount==count ) {
            if ( format==Nb_Formats ) format = IHX8M;
            else if ( format!=IHX8M && format!=IHX32 ) {
              errors += ErrorData(line, UnrecognizedFormat); // inconsistent format
              return;
            }
            /* Processing a INHX8M line */
            /* Modified to be able to read fuses from hexfile created by C18 toolchain */
            /* changed by Tobias Schoene 9 April 2005, */
            /* modified by A.G, because low and hi bytes was swapped in Tobias's code , 8 may 2005
            */
            uint addrbase = ((addrH << 16) | addr);
            //qDebug("line %i: address %s", line, toHex(addrbase, 8).data());
            for (uint x = 0; x<count; x++) {
              uint data;
              if ( sscanf(p, "%02X", &data)!=1 ) {
                errors += ErrorData(line, UnexpectedEOL);
                break;
              }
              p += 2;
              // A.G: I suspect possible initialization problem
              // if block begins at odd address
              // because |= works on an uninitalized word
              // however, I  don't know if such a situation can occurs
              uint a = addrbase+x >> 1;
              BitValue value = (*this)[a];
              if ( addrbase+x & 1 ) insert(a, value.maskWith(0x00FF) | data << 8); // Odd addr => Hi byte
              else insert(a, value.maskWith(0xFF00) | data); // Low byte
              //if ( x==0 ) qDebug("fb@%s: %s", toHex(addrbase+x >> 1, 8).data(), toHex(fb[addrbase+x >> 1], 8).data());
              cksum += data;
            }
        } else if ( bytecount==count*2 ) {
            if ( format==Nb_Formats ) format = IHX16;
            else if ( format!=IHX16 ) {
                errors += ErrorData(line, UnrecognizedFormat); // inconsistent format
                return;
            }
            /* Processing a INHX16 line */
            for(uint x=0; x<count; x++) {
                uint datal, datah;
                if( sscanf(p, "%02X%02X", &datah, &datal)!=2 ) {
                  errors += ErrorData(line, UnexpectedEOL);
                  break;
                }
                p += 4;
                //qDebug("%s: %s", toHexLabel(addr+x, 4).latin1(), toHexLabel(datal | (datah << 8), 4).latin1());
                insert(addr+x, datal | (datah << 8));
                cksum += datah;
                cksum += datal;
            }
        } else {
            errors += ErrorData(line, UnrecognizedFormat); // Brrrr !! Strange format.
            return;
        }

        /* Process the checksum */
        uint data;
        if( sscanf(p, "%02X", &data)!=1 ) errors += ErrorData(line, UnexpectedEOL);
        else if( ((data + cksum) & 0xFF)!=0 ) errors += ErrorData(line, WrongCRC);
    }

    errors += ErrorData(line, UnexpectedEOF);
    return;
}