/* movhex -- move load point of .HEX file */ /* copyright 1984 Michael M Rubenstein */ /* NOTE: This program only changes the address at which the program will */ /* be loaded. The code is not relocated. */ /* syntax: movhex [] */ #define PGMNAME "movhex" #define BUFSIZE (16*1024) #include #define MAXLINE 255 unsigned hexaddr(), atoh(); main(argc, argv) int argc; char *argv[]; { static unsigned reloc, addr; static FILE *infile, *outfile; char line[MAXLINE + 1]; if (argc < 2 || strcmp(argv[1], "//") == 0) error("syntax: movhex [ []]"); if (argc < 3) { infile = stdin; outfile = stdout; } else { infile = fopen(".hex", ""); if (copen(infile, argv[2], "r") == NULL) error("Cannot open input file."); outfile = fopen(".hex", ""); if (copen(outfile, (argc < 4) ? argv[2] : argv[3], "w") == NULL) error("Cannot open output file."); } if (fgets(line, MAXLINE, infile) != NULL) { if (strlen(line) < 7) error("Invalid HEX file input."); reloc = atoh(argv[1]) - hexaddr(line); do { if (strlen(line) >= 7 && strncmp(line, ":00", 3) != 0) setaddr(line, hexaddr(line) + reloc); setcksum(line); fputs(line, outfile); } while (fgets(line, MAXLINE, infile) != NULL); } fclose(infile); fclose(outfile); } /* get address in hex file line */ unsigned hexaddr(line) register char *line; { static char a[5] = {0, 0, 0, 0, 0}; static int i; if (strlen(line) < 7) return 0; line += 3; for (i = 0; i < 4; ++i) a[i] = *(line++); return atoh(a); } /* convert ASCII to HEX */ unsigned atoh(s) register char *s; { static unsigned u; u = 0; while (*s) u = (u << 4) + hex(*(s++)); return u; } /* convert an ASCII character to a HEX digit */ hex(c) register int c; { if (isdigit(c)) return c - '0'; if ((c = toupper(c)) >= 'A' && c <= 'F') return c + (10 - 'A'); error("Invalid HEX digit in string."); } /* set an address in a HEX file line to a new value */ setaddr(line, a) register char *line; register unsigned a; { static int i; line += 7; for (i = 4; i--;) { *--line = hexdig(a % 16); a /= 16; } } /* convert hex digit to ASCII */ hexdig(d) int d; { return (d < 10) ? (d + '0') : (d + ('A' - 10)); } /* set the checksum of the line */ setcksum(l) char *l; { static int n; static unsigned chksum; n = 16 * hex(*++l); n += hex(*++l); chksum = -n; n += 3; while (n--) { chksum -= 16 * hex(*++l); chksum -= hex(*++l); } *++l = hexdig((chksum >> 4) & 0x0f); *++l = hexdig(chksum & 0x0f); *++l = '\n'; *++l = '\0'; }