-
Notifications
You must be signed in to change notification settings - Fork 16
/
bin2hex.c
316 lines (300 loc) · 9.3 KB
/
bin2hex.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
/*
*----------------------------------------------------------------------------
* File: bin2hex.c
*
* Author: Marek Karcz
*
* Date created: 3/8/2016
*
* Purpose: Convert binary file to memory image definition (plain text) file.
*----------------------------------------------------------------------------
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
const int DEBUG = 0;
const char *hdr1 = "; Created with BIN2HEX (C) Marek Karcz 2016. All rights reserved.\n";
char g_szInputFileName[256] = {0};
char g_szHexFileName[256] = {0};
int g_nStartAddr = 2048; /* $0800 */
int g_nExecAddr = 2048; /* $0800 */
int g_nSuppressAutoExec = 1;
int g_nSuppressAllZeroRows = 0;
int g_nConvert2IntelHex = 0;
void ScanArgs(int argc, char *argv[]);
void ConvertFile(void);
void Convert2IntelHex(void);
/*
*--------------------------------------------------------------------
* Method: Usage()
* Purpose: Print usage information/help.
* Arguments: char * - program name.
* Returns: n/a
*--------------------------------------------------------------------
*/
void Usage(char *prgn)
{
printf("\nProgram: %s\n Convert binary file to Intel HEX format.\nOR\n", prgn);
printf(" Convert binary file to memory image definition for MKBASIC (VM65) emulator.\n\n");
printf("Copyright: Marek Karcz 2016. All rights reserved.\n");
printf("Free for personal and educational use.\n\n");
printf("Usage:\n\n");
printf(" %s -f input -o output [-w addr] [-x exec] [[-s] [-z] | -i]\n\n", prgn);
printf("Where:\n\n");
printf(" input - binary file name\n");
printf(" output - output file name\n");
printf(" addr - starting address to load data (default: %d)\n", g_nStartAddr);
printf(" exec - address to auto-execute code from (default: %d)\n", g_nExecAddr);
printf(" -s - suppress auto-execute statement in output\n");
printf(" -z - suppress data blocks with 0-s only\n");
printf(" -i - convert to Intel HEX format\n");
printf(" NOTE: When this switch is used, addr, exec, -s, -z are ignored,\n");
printf(" addr = 0, exec is not set and data blocks with 0-s only\n");
printf(" are always suppressed.\n");
printf("\n");
}
/*
*--------------------------------------------------------------------
* Method: ScanArgs()
* Purpose: Scan/parse command line arguments and set internal
* flags and parameters.
* Arguments: int argc - # of command line arguments,
* char *argv[] - array of command line arguments.
* Returns: n/a
*--------------------------------------------------------------------
*/
void ScanArgs(int argc, char *argv[])
{
int n = 1;
while (n < argc)
{
if (strcmp(argv[n], "-f") == 0)
{
n++;
strcpy(g_szInputFileName,argv[n]);
}
else if (strcmp(argv[n],"-o") == 0)
{
n++;
strcpy(g_szHexFileName,argv[n]);
}
else if (strcmp(argv[n],"-w") == 0)
{
n++;
g_nStartAddr = atoi(argv[n]);
g_nExecAddr = g_nStartAddr;
g_nSuppressAutoExec = 0;
}
else if (strcmp(argv[n],"-x") == 0)
{
n++;
g_nExecAddr = atoi(argv[n]);
g_nSuppressAutoExec = 0;
}
else if (strcmp(argv[n],"-s") == 0)
{
g_nSuppressAutoExec = 1;
}
else if (strcmp(argv[n],"-z") == 0)
{
g_nSuppressAllZeroRows = 1;
}
else if (strcmp(argv[n],"-i") == 0)
{
g_nConvert2IntelHex = 1;
}
n++;
}
}
/*
*--------------------------------------------------------------------
* Method: ConvertFile()
* Purpose: Convert binary file to plain text memory definition
* file for MKBASIC (VM65) emulator.
* Arguments: n/a
* Returns: n/a
*--------------------------------------------------------------------
*/
void ConvertFile(void)
{
FILE *fpi = NULL;
FILE *fpo = NULL;
unsigned char bt[17];
char hex[80];
int i, addr, allzero, prev_allzero;
addr = g_nStartAddr;
printf("Processing...\n");
printf("Start address: $%04x\n", addr);
if (NULL != (fpi = fopen(g_szInputFileName,"rb")))
{
if (NULL != (fpo = fopen(g_szHexFileName,"w")))
{
time_t t = time(NULL);
struct tm *tm = localtime(&t);
char s[64] = {0};
strftime(s, sizeof(s), "; %c\n", tm);
fputs(hdr1, fpo);
fputs(s, fpo);
sprintf(hex, "ADDR\n$%04x\nORG\n$%04x\n", g_nExecAddr, addr);
if (DEBUG) printf("Adding line:\n%s\n", hex);
fputs(hex,fpo);
prev_allzero = 1;
while(0 == feof(fpi) && addr <= 0xFFFF)
{
memset(bt, 0, 17);
memset(hex, 0, 80);
if (DEBUG) printf("Reading input file...");
fread(bt, sizeof(char), 16, fpi);
if (DEBUG) printf("done.\n");
if (DEBUG) printf("Preparing hex string...\n");
allzero = 1;
for(i=0; i<16; i++)
{
if (DEBUG) printf("Code: %d\n", bt[i]);
if (*hex == 0) sprintf(hex, "$%02x", bt[i]);
else sprintf(hex, "%s $%02x", hex, bt[i]);
if (allzero && bt[i] > 0)
allzero = 0;
}
if (g_nSuppressAllZeroRows && prev_allzero && 0 == allzero) {
char buff[20];
sprintf (buff, "ORG\n$%04x\n", addr);
fputs(buff, fpo);
}
if (0 == g_nSuppressAllZeroRows
||
(g_nSuppressAllZeroRows && 0 == allzero)
)
{
sprintf(hex, "%s\n", hex);
if (DEBUG) printf("Adding line: %s", hex);
fputs(hex, fpo);
}
addr += 16;
prev_allzero = allzero;
}
if (0 == g_nSuppressAutoExec)
{
memset(hex, 80, sizeof(char));
sprintf(hex, "EXEC\n$%04x\n", g_nExecAddr);
if (DEBUG) printf("Adding line: %s", hex);
fputs(hex, fpo);
}
fclose(fpi);
fclose(fpo);
printf("Done.\n");
printf("End address: $%04x\n", (addr <= 0xFFFF) ? addr : 0xFFFF);
printf("Run address: $%04x\n", g_nExecAddr);
}
else
printf("ERROR: Unable to create output file.\n");
}
else
printf("ERROR: Unable to open input file.\n");
}
/*
*--------------------------------------------------------------------
* Method: Convert2IntelHex()
* Purpose: Convert binary file to Intel HEX format.
* Arguments: n/a
* Returns: n/a
*--------------------------------------------------------------------
*/
void Convert2IntelHex(void)
{
FILE *fpi = NULL;
FILE *fpo = NULL;
unsigned char bt[17], cksum;
char hex[80];
int i, allzero;
unsigned int addr;
addr = 0;
g_nSuppressAllZeroRows = 1;
g_nExecAddr = 0;
printf("Processing...\n");
printf("Start address: $%04x\n", addr);
if (NULL != (fpi = fopen(g_szInputFileName,"rb")))
{
if (NULL != (fpo = fopen(g_szHexFileName,"w")))
{
while(0 == feof(fpi) && addr <= 0xFFFF)
{
memset(bt, 0, 17);
memset(hex, 0, 80);
if (DEBUG) printf("Reading input file...");
fread(bt, sizeof(char), 16, fpi);
if (DEBUG) printf("done.\n");
if (DEBUG) printf("Preparing hex string...\n");
/* start the Intel HEX data record, all data blocks
generated by this program are 16-bytes long
*/
sprintf(hex, ":10%04x00", addr);
cksum = 0;
allzero = 1;
/* append data to record */
for(i=0; i<16; i++)
{
cksum += bt[i];
if (DEBUG) printf("Code: %d\n", bt[i]);
sprintf(hex, "%s%02x", hex, bt[i]);
if (allzero && bt[i]) allzero = 0;
}
cksum = ~cksum + 1; /* Two's complement of modulo 256 sum */
/* end record with check sum value */
sprintf(hex, "%s%02x", hex, cksum);
/* output only if non-zero data present in 16-byte block */
if (0 == g_nSuppressAllZeroRows
||
(g_nSuppressAllZeroRows && 0 == allzero)
)
{
sprintf(hex, "%s\n", hex);
if (DEBUG) printf("Adding line: %s", hex);
fputs(hex, fpo);
}
addr += 16;
}
/* add EOF */
hex[0] = 0;
strcpy(hex, ":00000001FF");
sprintf(hex, "%s\n", hex);
fputs(hex, fpo);
fclose(fpi);
fclose(fpo);
printf("Done.\n");
printf("End address: $%04x\n", (addr <= 0xFFFF) ? addr : 0xFFFF);
printf("Run address: $%04x\n", g_nExecAddr);
}
else
printf("ERROR: Unable to create output file.\n");
}
else
printf("ERROR: Unable to open input file.\n");
}
/*
*--------------------------------------------------------------------
* Method: main()
* Purpose: Main program loop/routine.
* Arguments: int argc - # of provided in command line arguments.
* char *argv[] - array of command line arguments.
* Returns: int - always 0.
*--------------------------------------------------------------------
*/
int main(int argc, char *argv[])
{
if (argc == 1)
Usage(argv[0]);
else {
ScanArgs(argc, argv);
if (*g_szInputFileName == 0 || *g_szHexFileName == 0)
Usage(argv[0]);
else {
if (0 == g_nConvert2IntelHex)
ConvertFile();
else
Convert2IntelHex();
}
}
return 0;
}