-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmot2bin.c
316 lines (245 loc) · 10.2 KB
/
mot2bin.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
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
/*
---------------------------------------------------------------------------
mot2bin converts a Motorola hex file to binary.
---------------------------------------------------------------------------
*/
#define PROGRAM "mot2bin"
#define VERSION "2.5.1"
#include "common.h"
const char *Pgm_Name = PROGRAM;
int main (int argc, char *argv[]) {
// line inputted from file
char Line[MAX_LINE_SIZE];
// flag that a file was read
bool Fileread;
// cmd-line parameter #
char *p;
int result;
/* Application specific */
unsigned int First_Word, Address;
unsigned int Type;
unsigned int Exec_Address;
unsigned int temp;
unsigned int Record_Count, Record_Checksum;
byte Data_Str[MAX_LINE_SIZE];
fprintf (stdout,PROGRAM" v"VERSION", Copyright (C) 2017 Jacques Pelletier & contributors\n\n");
if (argc == 1)
usage();
strcpy(Extension, "bin"); // default is using a binary file extension
ParseOptions(argc, argv);
// When user enters input file name
// Assume last parameter is filename
GetFilename(Filename,argv[argc -1]);
// Just a normal file name
NoFailOpenInputFile (Filename);
PutExtension(Filename, Extension);
NoFailOpenOutputFile(Filename);
Fileread = true;
/*
When the HEX file is opened, the program will read it in 2 passes.
The first pass gets the highest and lowest addresses so that we can allocate the right size.
The second pass processes the hex data.
To begin, assume the lowest address is at the end of the memory.
While reading each records, subsequent addresses will lower this number.
At the end of the input file, this value will be the lowest address.
A similar assumption is made for highest address. It starts at the
beginning of memory. While reading each records, subsequent addresses will raise this number.
At the end of the input file, this value will be the highest address.
*/
Lowest_Address = (unsigned int)-1;
Highest_Address = 0;
Records_Start = 0;
Record_Nb = 0;
First_Word = 0;
// get highest and lowest addresses so that we can allocate the right size
do {
unsigned int i;
/* Read a line from input file. */
GetLine(Line,Filin);
Record_Nb++;
/* Remove carriage return/line feed at the end of line. */
i = strlen(Line);
if (--i != 0)
{
if (Line[i] == '\n') Line[i] = '\0';
p = (char *) Data_Str;
switch(Line[1])
{
case '0':
Nb_Bytes = 1; /* This is to fix the Highest_Address set to -1 when Nb_Bytes = 0 */
break;
/* 16 bits address */
case '1':
result = sscanf (Line,"S%1x%2x%4x",&Type,&Nb_Bytes,&First_Word);
if (result != 3) fprintf(stderr,"Error in line %d of hex file\n", Record_Nb);
/* Adjust Nb_Bytes for the number of data bytes */
Nb_Bytes = Nb_Bytes - 3;
break;
/* 24 bits address */
case '2':
result = sscanf (Line,"S%1x%2x%6x",&Type,&Nb_Bytes,&First_Word);
if (result != 3) fprintf(stderr,"Error in line %d of hex file\n", Record_Nb);
/* Adjust Nb_Bytes for the number of data bytes */
Nb_Bytes = Nb_Bytes - 4;
break;
/* 32 bits address */
case '3':
result = sscanf (Line,"S%1x%2x%8x",&Type,&Nb_Bytes,&First_Word);
if (result != 3) fprintf(stderr,"Error in line %d of hex file\n", Record_Nb);
/* Adjust Nb_Bytes for the number of data bytes */
Nb_Bytes = Nb_Bytes - 5;
break;
}
Phys_Addr = First_Word;
/* Set the lowest address as base pointer. */
if (Phys_Addr < Lowest_Address)
Lowest_Address = Phys_Addr;
/* Same for the top address. */
temp = Phys_Addr + Nb_Bytes -1;
if (temp > Highest_Address)
Highest_Address = temp;
}
}
while (!feof (Filin));
Allocate_Memory_And_Rewind();
Record_Nb = 0;
/* Read the file & process the lines. */
do /* repeat until EOF(Filin) */
{
int i;
Checksum = 0;
/* Read a line from input file. */
GetLine(Line,Filin);
Record_Nb++;
/* Remove carriage return/line feed at the end of line. */
i = strlen(Line);
if (--i != 0)
{
if (Line[i] == '\n') Line[i] = '\0';
/* Scan starting address and nb of bytes. */
/* Look at the record type after the 'S' */
Type = 0;
switch(Line[1])
{
case '0':
result = sscanf (Line,"S0%2x0000484452%2x",&Nb_Bytes,&Record_Checksum);
if (result != 2) fprintf(stderr,"Error in line %d of hex file\n", Record_Nb);
Checksum = Nb_Bytes + 0x48 + 0x44 + 0x52;
/* Adjust Nb_Bytes for the number of data bytes */
Nb_Bytes = 0;
break;
/* 16 bits address */
case '1':
result = sscanf (Line,"S%1x%2x%4x%s",&Type,&Nb_Bytes,&Address,Data_Str);
if (result != 4) fprintf(stderr,"Error in line %d of hex file\n", Record_Nb);
Checksum = Nb_Bytes + (Address >> 8) + (Address & 0xFF);
/* Adjust Nb_Bytes for the number of data bytes */
Nb_Bytes = Nb_Bytes - 3;
break;
/* 24 bits address */
case '2':
result = sscanf (Line,"S%1x%2x%6x%s",&Type,&Nb_Bytes,&Address,Data_Str);
if (result != 4) fprintf(stderr,"Error in line %d of hex file\n", Record_Nb);
Checksum = Nb_Bytes + (Address >> 16) + (Address >> 8) + (Address & 0xFF);
/* Adjust Nb_Bytes for the number of data bytes */
Nb_Bytes = Nb_Bytes - 4;
break;
/* 32 bits address */
case '3':
result = sscanf (Line,"S%1x%2x%8x%s",&Type,&Nb_Bytes,&Address,Data_Str);
if (result != 4) fprintf(stderr,"Error in line %d of hex file\n", Record_Nb);
Checksum = Nb_Bytes + (Address >> 24) + (Address >> 16) + (Address >> 8) + (Address & 0xFF);
/* Adjust Nb_Bytes for the number of data bytes */
Nb_Bytes = Nb_Bytes - 5;
break;
case '5':
result = sscanf (Line,"S%1x%2x%4x%2x",&Type,&Nb_Bytes,&Record_Count,&Record_Checksum);
if (result != 4) fprintf(stderr,"Error in line %d of hex file\n", Record_Nb);
Checksum = Nb_Bytes + (Record_Count >> 8) + (Record_Count & 0xFF);
/* Adjust Nb_Bytes for the number of data bytes */
Nb_Bytes = 0;
break;
case '7':
result = sscanf (Line,"S%1x%2x%8x%2x",&Type,&Nb_Bytes,&Exec_Address,&Record_Checksum);
if (result != 4) fprintf(stderr,"Error in line %d of hex file\n", Record_Nb);
Checksum = Nb_Bytes + (Exec_Address >> 24) + (Exec_Address >> 16) + (Exec_Address >> 8) + (Exec_Address & 0xFF);
Nb_Bytes = 0;
break;
case '8':
result = sscanf (Line,"S%1x%2x%6x%2x",&Type,&Nb_Bytes,&Exec_Address,&Record_Checksum);
if (result != 4) fprintf(stderr,"Error in line %d of hex file\n", Record_Nb);
Checksum = Nb_Bytes + (Exec_Address >> 16) + (Exec_Address >> 8) + (Exec_Address & 0xFF);
Nb_Bytes = 0;
break;
case '9':
result = sscanf (Line,"S%1x%2x%4x%2x",&Type,&Nb_Bytes,&Exec_Address,&Record_Checksum);
if (result != 4) fprintf(stderr,"Error in line %d of hex file\n", Record_Nb);
Checksum = Nb_Bytes + (Exec_Address >> 8) + (Exec_Address & 0xFF);
Nb_Bytes = 0;
break;
}
p = (char *) Data_Str;
/* If we're reading the last record, ignore it. */
switch (Type)
{
/* Data record */
case 1:
case 2:
case 3:
if (Nb_Bytes == 0)
{
fprintf(stderr,"0 byte length Data record ignored\n");
break;
}
Phys_Addr = Address;
p = ReadDataBytes(p);
/* Read the Checksum value. */
result = sscanf (p, "%2x",&Record_Checksum);
if (result != 1) fprintf(stderr,"Error in line %d of hex file\n", Record_Nb);
break;
case 5:
fprintf(stderr,"Record total: %d\n",Record_Count);
break;
case 7:
fprintf(stderr,"Execution Address (unused): %08X\n",Exec_Address);
break;
case 8:
fprintf(stderr,"Execution Address (unused): %06X\n",Exec_Address);
break;
case 9:
fprintf(stderr,"Execution Address (unused): %04X\n",Exec_Address);
break;
/* Ignore all other records */
default:;
}
Record_Checksum &= 0xFF;
/* Verify Checksum value. */
if (((Record_Checksum + Checksum) != 0xFF) && Enable_Checksum_Error)
{
fprintf(stderr,"Checksum error in record %d: should be %02X\n",Record_Nb, 255-Checksum);
Status_Checksum_Error = true;
}
}
}
while (!feof (Filin));
/*-----------------------------------------------------------------------------*/
fprintf(stdout,"Binary file start = %08X\n",Lowest_Address);
fprintf(stdout,"Records start = %08X\n",Records_Start);
fprintf(stdout,"Highest address = %08X\n",Highest_Address);
fprintf(stdout,"Pad Byte = %X\n", Pad_Byte);
WriteMemory();
#ifdef USE_FILE_BUFFERS
free (FilinBuf);
free (FiloutBuf);
#endif
fclose (Filin);
fclose (Filout);
if (Status_Checksum_Error && Enable_Checksum_Error)
{
fprintf(stderr,"Checksum error detected.\n");
return 1;
}
if (!Fileread)
usage();
return 0;
}