Line 7... |
Line 7... |
#include <stdlib.h>
|
#include <stdlib.h>
|
#include <string.h>
|
#include <string.h>
|
#include <stdarg.h>
|
#include <stdarg.h>
|
|
|
|
|
|
#define S1 1
|
|
#define S2 2
|
|
#define S3 3
|
|
|
|
|
void error(char *fmt, ...) {
|
void error(char *fmt, ...) {
|
va_list ap;
|
va_list ap;
|
|
|
va_start(ap, fmt);
|
va_start(ap, fmt);
|
printf("Error: ");
|
printf("Error: ");
|
Line 20... |
Line 25... |
exit(1);
|
exit(1);
|
}
|
}
|
|
|
|
|
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
|
int type;
|
|
unsigned int loadAddrMsk;
|
|
char *loadAddrStr;
|
|
char *infileStr;
|
|
char *outfileStr;
|
char *endptr;
|
char *endptr;
|
unsigned int loadAddr;
|
unsigned int loadAddr;
|
|
unsigned int startAddr;
|
FILE *infile;
|
FILE *infile;
|
FILE *outfile;
|
FILE *outfile;
|
int numBytes, i;
|
int numBytes, i;
|
int c;
|
int c;
|
unsigned char lineData[16];
|
unsigned char lineData[16];
|
unsigned int chksum;
|
unsigned int chksum;
|
|
|
if (argc != 4) {
|
if (argc != 5) {
|
printf("Usage: %s <load addr, hex> <input file> <output file>\n",
|
printf("Usage: %s -S1|-S2|-S3 <load addr, hex> ", argv[0]);
|
argv[0]);
|
printf("<input file> <output file>\n");
|
exit(1);
|
exit(1);
|
}
|
}
|
loadAddr = strtoul(argv[1], &endptr, 16);
|
if (strcmp(argv[1], "-S1") == 0) {
|
|
type = S1;
|
|
loadAddrMsk = 0x0000FFFF;
|
|
} else
|
|
if (strcmp(argv[1], "-S2") == 0) {
|
|
type = S2;
|
|
loadAddrMsk = 0x00FFFFFF;
|
|
} else
|
|
if (strcmp(argv[1], "-S3") == 0) {
|
|
type = S3;
|
|
loadAddrMsk = 0xFFFFFFFF;
|
|
} else {
|
|
error("exactly one of -S1, -S2, or -S3 must be specified");
|
|
}
|
|
loadAddrStr = argv[2];
|
|
infileStr = argv[3];
|
|
outfileStr = argv[4];
|
|
loadAddr = strtoul(loadAddrStr, &endptr, 16);
|
if (*endptr != '\0') {
|
if (*endptr != '\0') {
|
error("illegal load address %s", argv[1]);
|
error("illegal load address %s", loadAddrStr);
|
|
}
|
|
if (loadAddr & ~loadAddrMsk) {
|
|
error("load address too big");
|
}
|
}
|
infile = fopen(argv[2], "rb");
|
startAddr = loadAddr;
|
|
infile = fopen(infileStr, "rb");
|
if (infile == NULL) {
|
if (infile == NULL) {
|
error("cannot open input file %s", argv[2]);
|
error("cannot open input file %s", infileStr);
|
}
|
}
|
outfile = fopen(argv[3], "wt");
|
outfile = fopen(outfileStr, "wt");
|
if (outfile == NULL) {
|
if (outfile == NULL) {
|
error("cannot open output file %s", argv[3]);
|
error("cannot open output file %s", outfileStr);
|
}
|
}
|
|
fprintf(outfile, "S00600004844521B\n");
|
while (1) {
|
while (1) {
|
chksum = 0;
|
chksum = 0;
|
for (numBytes = 0; numBytes < 16; numBytes++) {
|
for (numBytes = 0; numBytes < 16; numBytes++) {
|
c = fgetc(infile);
|
c = fgetc(infile);
|
if (c == EOF) {
|
if (c == EOF) {
|
Line 59... |
Line 92... |
chksum += c;
|
chksum += c;
|
}
|
}
|
if (numBytes == 0) {
|
if (numBytes == 0) {
|
break;
|
break;
|
}
|
}
|
|
switch (type) {
|
|
case S1:
|
|
fprintf(outfile, "S1%02X%04X", numBytes + 3, loadAddr);
|
|
break;
|
|
case S2:
|
fprintf(outfile, "S2%02X%06X", numBytes + 4, loadAddr);
|
fprintf(outfile, "S2%02X%06X", numBytes + 4, loadAddr);
|
|
break;
|
|
case S3:
|
|
fprintf(outfile, "S3%02X%08X", numBytes + 5, loadAddr);
|
|
break;
|
|
}
|
for (i = 0; i < numBytes; i++) {
|
for (i = 0; i < numBytes; i++) {
|
fprintf(outfile, "%02X", lineData[i]);
|
fprintf(outfile, "%02X", lineData[i]);
|
}
|
}
|
chksum += numBytes + 4;
|
switch (type) {
|
chksum += ((loadAddr >> 0) & 0xFF) +
|
case S1:
|
|
chksum += numBytes + 3 +
|
|
((loadAddr >> 0) & 0xFF) +
|
|
((loadAddr >> 8) & 0xFF);
|
|
break;
|
|
case S2:
|
|
chksum += numBytes + 4 +
|
|
((loadAddr >> 0) & 0xFF) +
|
((loadAddr >> 8) & 0xFF) +
|
((loadAddr >> 8) & 0xFF) +
|
((loadAddr >> 16) & 0xFF);
|
((loadAddr >> 16) & 0xFF);
|
|
break;
|
|
case S3:
|
|
chksum += numBytes + 5 +
|
|
((loadAddr >> 0) & 0xFF) +
|
|
((loadAddr >> 8) & 0xFF) +
|
|
((loadAddr >> 16) & 0xFF) +
|
|
((loadAddr >> 24) & 0xFF);
|
|
break;
|
|
}
|
fprintf(outfile, "%02X\n", 0xFF - (chksum & 0xFF));
|
fprintf(outfile, "%02X\n", 0xFF - (chksum & 0xFF));
|
loadAddr += numBytes;
|
loadAddr += numBytes;
|
if (c == EOF) {
|
if (c == EOF) {
|
break;
|
break;
|
}
|
}
|
}
|
}
|
fprintf(outfile, "S804000000FB\n");
|
switch (type) {
|
|
case S1:
|
|
fprintf(outfile, "S903%04X", startAddr);
|
|
chksum = 3 +
|
|
((startAddr >> 0) & 0xFF) +
|
|
((startAddr >> 8) & 0xFF);
|
|
break;
|
|
case S2:
|
|
fprintf(outfile, "S804%06X", startAddr);
|
|
chksum = 4 +
|
|
((startAddr >> 0) & 0xFF) +
|
|
((startAddr >> 8) & 0xFF) +
|
|
((startAddr >> 16) & 0xFF);
|
|
break;
|
|
case S3:
|
|
fprintf(outfile, "S705%08X", startAddr);
|
|
chksum = 5 +
|
|
((startAddr >> 0) & 0xFF) +
|
|
((startAddr >> 8) & 0xFF) +
|
|
((startAddr >> 16) & 0xFF) +
|
|
((startAddr >> 24) & 0xFF);
|
|
break;
|
|
}
|
|
fprintf(outfile, "%02X\n", 0xFF - (chksum & 0xFF));
|
fclose(infile);
|
fclose(infile);
|
fclose(outfile);
|
fclose(outfile);
|
return 0;
|
return 0;
|
}
|
}
|
|
|
No newline at end of file
|
No newline at end of file
|