5 * th9x - http://code.google.com/p/th9x
6 * er9x - http://code.google.com/p/er9x
7 * gruvin9x - http://code.google.com/p/gruvin9x
9 * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
21 #include "hexinterface.h"
23 HexInterface::HexInterface(QTextStream
&stream
):
28 int HexInterface::getValueFromLine(const QString
&line
, int pos
, int len
)
31 int hex
= line
.mid(pos
,len
).toInt(&ok
, 16);
35 int HexInterface::load(uint8_t *data
, int maxsize
)
39 while (!stream
.atEnd()) {
40 QString line
= stream
.readLine();
42 if(line
.left(1)!=":") continue;
44 int byteCount
= getValueFromLine(line
,1);
45 int address
= getValueFromLine(line
,3,4);
46 int recType
= getValueFromLine(line
,7);
50 if(byteCount
<0 || address
<0 || recType
<0)
59 chkSum
-= address
& 0xFF;
60 chkSum
-= address
>> 8;
61 for(int i
=0; i
<byteCount
; i
++)
63 quint8 v
= getValueFromLine(line
,(i
*2)+9) & 0xFF;
68 quint8 retV
= getValueFromLine(line
,(byteCount
*2)+9) & 0xFF;
72 if (address
+offset
+ byteCount
<= maxsize
) {
73 if (recType
== 0x00) { //data record - ba holds record
74 memcpy(&data
[address
+offset
],ba
.data(),byteCount
);
75 result
= std::max(result
, address
+offset
+byteCount
);
87 bool HexInterface::save(const uint8_t * data
, const int size
)
92 if (addr
>(nextbank
*0x010000)-1) {
93 stream
<< iHEXExtRec(nextbank
) << "\n";
97 if ((size
- addr
) < llen
)
99 stream
<< iHEXLine(data
, addr
, llen
) << "\n";
102 stream
<< ":00000001FF\n"; // write EOF
106 QString
HexInterface::iHEXLine(const quint8
* data
, quint32 addr
, quint8 len
)
108 unsigned int bankaddr
;
109 bankaddr
=addr
&0xffff;
110 QString str
= QString(":%1%2000").arg(len
, 2, 16, QChar('0')).arg(bankaddr
, 4, 16, QChar('0')); //write start, bytecount (32), address and record type
112 chkSum
= -len
; //-bytecount; recordtype is zero
113 chkSum
-= bankaddr
& 0xFF;
114 chkSum
-= bankaddr
>> 8;
115 for (int j
= 0; j
< len
; j
++) {
116 str
+= QString("%1").arg(data
[addr
+ j
], 2, 16, QChar('0'));
117 chkSum
-= data
[addr
+ j
];
120 str
+= QString("%1").arg(chkSum
, 2, 16, QChar('0'));
121 return str
.toUpper(); // output to file and lf;
124 QString
HexInterface::iHEXExtRec(quint8 bank
)
126 QString str
= QString(":02000002"); //write record type 2 record
128 chkSum
= -2; //-bytecount; recordtype is zero
129 chkSum
-= 2; // type 2 record type
130 str
+= QString("%1000").arg((bank
&0x0f)<<4,1,16,QChar('0'));
131 chkSum
-= ((bank
&0x0f)<<4); // type 2 record type
132 str
+= QString("%1").arg(chkSum
, 2, 16, QChar('0'));
133 return str
.toUpper(); // output to file and lf;