-
Notifications
You must be signed in to change notification settings - Fork 1
/
ReadHexTest.c
125 lines (110 loc) · 3.02 KB
/
ReadHexTest.c
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
#include <stdio.h>
#define u8 unsigned char
#define u16 unsigned int
#define u32 unsigned long int
#define bool char
#define true 1
#define false 0
#define DATA_RECORD 0x00
#define EOF_RECORD 0x01
u8 read(FILE *pF) {
char nResult;
fscanf(pF, "%c", &nResult);
//printf("%c\n", nResult);
return nResult;
}
u8 hexVal(char nByte) {
//Returns hex value of ASCII byte, 0xFF if not valid
if ((nByte >= '0') && (nByte <= '9')) return (u8)(nByte - '0');
if ((nByte >= 'a') && (nByte <= 'f')) return (u8)(nByte - 'a' + 0x0A);
if ((nByte >= 'A') && (nByte <= 'F')) return (u8)(nByte - 'A' + 0x0A);
return 0xFF;
}
int parseIntelHex(FILE *pFile) {
u8 nReadData; //Raw byte
u8 nHexVal; //Interpreted nibble
u8 nData; //Interpreted byte
u8 nByteCount; //Line byte count
u16 nAddress; //Address
u8 nRecordType; //Record type
u8 nChecksum; //Checksum
u16 nDataIndex = 0; //Data buffer index
u8 nIndex = 0; //Line index
u16 nLineCount = 0;
bool bByteCount = false;
bool bAddress = false;
bool bRecordType = false;
bool bData = false;
bool bChecksum = false;
printf("Entering Parse Loop\n");
while(!feof(pFile)) { //While there's bytes available for reading
nReadData = read(pFile);
nHexVal = hexVal(nReadData);
if (nReadData == ':') {
bByteCount = true; //: indicates start of line, move to bytecount
nIndex = 0;
printf("Line %u:\n", nLineCount);
nLineCount++;
}
else if (nHexVal != 0xFF) { //If valid hex value
//Read byte count
if (bByteCount) { //If at the start of the line, read bytecount
//Byte count = MSB << 8 | LSB
nByteCount = ((nHexVal & 0x0F) << 8) | hexVal(read(pFile));
bByteCount = false;
bAddress = true;
printf("Byte count = %#x\n", nByteCount);
}
//Read address
else if (bAddress) { //If at the start of the address
//Address = MSB << 24 | 2SB << 16 | 3SB << 8 | LSB
nAddress = (((u16)(nHexVal) & 0x0F) << 8) |
((u16)(hexVal(read(pFile))) & 0x0F);
bAddress = false;
bRecordType = true;
printf("Address = %#x\n", nAddress);
}
//Read record type
else if (bRecordType) {
nRecordType = ((nHexVal & 0x0F) << 8) | hexVal(read(pFile));
bRecordType = false;
bData = true;
if (nRecordType == EOF_RECORD) {
printf("Record type = EOF\n");
return 0;
}
printf("Record type = data\n");
}
//Read data
else if (bData && (nRecordType == DATA_RECORD)) {
nIndex = 0;
while (nIndex < nByteCount) {
//if (nDataIndex >= *pDataLength) return -1;
nData = ((nHexVal & 0x0F) << 8) | hexVal(read(pFile));
//*(pDataBuffer + nDataIndex) = nData;
nDataIndex++;
nIndex++;
printf("%x", nData);
}
printf("\n");
bData = false;
bChecksum = true;
}
//Read checksum
else if (bChecksum) {
nChecksum = ((nHexVal & 0x0F) << 8) | hexVal(read(pFile));
printf("Checksum = %#x\n", nChecksum);
bChecksum = false;
}
}
}
printf("Exiting Parse Loop\n");
return 0;
}
int main(void) {
FILE *pFile;
pFile = fopen("test.hex","r");
parseIntelHex(pFile);
fclose(pFile);
return 0;
}