147 lines
3.2 KiB
C
147 lines
3.2 KiB
C
#include <stdio.h>
|
|
#include <getopt.h>
|
|
#include <string.h>
|
|
|
|
|
|
const char BIN2OBJIDSTRING[] = "$Id: bin2obj.c 8016 2007-11-01 14:24:42Z tlehnig $";
|
|
|
|
|
|
long long convertToHex(char *fn, unsigned long origin, FILE *outfile) {
|
|
FILE *datafile;
|
|
unsigned char data1, data2, data3, data4;
|
|
int res = 0;
|
|
long long count = 0;
|
|
|
|
datafile = fopen(fn, "r");
|
|
if (!datafile) {
|
|
printf("Datafile >%s< could not be opened, not writing data for this file\n", fn);
|
|
return -1;
|
|
}
|
|
|
|
printf("Converting file >%s< to .32.obj format at origin 0x%08lx (0x%08lx) ... ",
|
|
fn, origin >> 2, origin);
|
|
fprintf(outfile, "/origin %08lx\n", origin >> 2);
|
|
|
|
do {
|
|
data1 = 0;
|
|
data2 = 0;
|
|
data3 = 0;
|
|
data4 = 0;
|
|
|
|
res = fscanf(datafile, "%c%c%c%c", &data1, &data2, &data3, &data4);
|
|
if (res > 0) {
|
|
count += res;
|
|
|
|
fprintf(outfile, "%02x%02x%02x%02x", data4, data3, data2, data1);
|
|
|
|
if ((count % 16) == 0)
|
|
fprintf(outfile, "\n");
|
|
else
|
|
fprintf(outfile, " ");
|
|
}
|
|
|
|
} while (res > 0);
|
|
|
|
if ((count % 16) != 0) fprintf(outfile, "\n");
|
|
|
|
printf("done with %lli Bytes.\n", count);
|
|
|
|
fclose(datafile);
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
void print_help() {
|
|
printf("Usage: bin2obj [FLAGS] [OPTIONS]\n");
|
|
printf("\nFLAGS: -h, -v\n");
|
|
printf("-h Print this help\n");
|
|
printf("-v Print Version ID\n");
|
|
printf("\nOPTIONS: -m, -o\n");
|
|
printf("-m <mapfile> Defines mapfile to use for bin2obj\n");
|
|
printf("-o <outfile> Defines output file to use for bin2obj\n");
|
|
printf("\nbin2obj converts the binary files defined in the mapfile to a hex based textfile\n");
|
|
printf("used by MCEMU\n");
|
|
}
|
|
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
FILE *mapfile = NULL, *outfile = NULL;
|
|
unsigned long origin;
|
|
char datafn[255];
|
|
char outfn[255] = "output.obj";
|
|
char mapfn[255] = "load.map";
|
|
int res = 0;
|
|
unsigned long long count = 0;
|
|
long long thiscount = 0;
|
|
int retval = 0;
|
|
|
|
int c, doOptLoop = 1;
|
|
|
|
while (doOptLoop) {
|
|
c = getopt(argc, argv, "m:o:hv");
|
|
|
|
if (c == -1) {
|
|
doOptLoop = 0;
|
|
break;
|
|
}
|
|
|
|
switch (c) {
|
|
case 'h':
|
|
print_help();
|
|
return 0;
|
|
break;
|
|
case 'v':
|
|
printf("%s %s\n", argv[0], BIN2OBJIDSTRING);
|
|
return 0;
|
|
break;
|
|
case 'm':
|
|
printf("Mapfile: >%s<\n", optarg);
|
|
strncpy(mapfn, optarg, 255);
|
|
break;
|
|
case 'o':
|
|
printf("Outfile: >%s<\n", optarg);
|
|
strncpy(outfn, optarg, 255);
|
|
break;
|
|
default:
|
|
print_help();
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
|
|
mapfile = fopen(mapfn, "r");
|
|
if (!mapfile) {
|
|
printf("Mapfile >%s< not found, exiting.\n", mapfn);
|
|
return -1;
|
|
}
|
|
|
|
outfile = fopen(outfn, "w");
|
|
if (!outfile) {
|
|
printf("Outputfile >%s< could not be created, exiting\n", outfn);
|
|
return -1;
|
|
}
|
|
|
|
// res = fscanf(mapfile, "%lx %s\n", &origin, datafn);
|
|
|
|
while ((res = fscanf(mapfile, "%lx %s\n", &origin, datafn)) == 2) {
|
|
//printf("ReadMapFile origin: 0x%08lx, filename: >%s<\n", origin, datafn);
|
|
|
|
thiscount = convertToHex(datafn, origin, outfile);
|
|
if (thiscount < 0) {
|
|
retval = -1;
|
|
}
|
|
else count += thiscount;
|
|
|
|
}
|
|
|
|
fprintf(outfile, "/eof\n");
|
|
fclose(mapfile);
|
|
fclose (outfile);
|
|
|
|
printf("Total conversion: %lli Bytes\n", count);
|
|
|
|
return retval;
|
|
}
|