/*_ format83.c Thu Sep 17 1987 Modified by: Erik Lindberg */ /* This program is released to the public domain. It can be used for any purpose whatsoever. If you can get some poor sucker to pay money for it, tell me your secret. Prints a file to stdout in Intel HEX 83 format. I wrote this to be easy to understand, modify, and *portable*. If you want elegant, you can get there from here. Modified may 25 2005, Theo Verelst, http://theover.tripod.com */ #include #include #include #define REC 0x10 /* Size of a record. */ char *line, buffer[128]; FILE *infile; extern char hex(); void puthex(); int main(argc,argv) int argc; char *argv[]; { int c=1, address=0; int sum, i; int pagecount = 0x0010; /* Start at 2d megabyte in flash */ i=0; if (!(infile = fopen(argv[++i],"rb"))) { fprintf(stderr, "Error on open of file %s\n",argv[i]); exit(1); } while (c != EOF) { sum = 0; if ((address/REC) == 0) { sum += pagecount >> 8; /* Checksum high address byte.*/ sum += pagecount & 0xff;/* Checksum low address byte.*/ sum += 2; /* Checksum record byte count.*/ sum += 4; /* type */ putchar(':'); puthex(2,2); /* Byte count. */ puthex(0,4); /* Do address */ puthex(4,2); /* Record type. */ puthex(pagecount,4); /* Do address and increment */ puthex(0-sum,2); /* Checksum is 1byte 2's comp.*/ printf("\n"); sum = 0; } line = buffer; for (i=0; i>4); *line++ = hex(c); sum += c; /* Checksum each character. */ } if (i) { sum += address >> 8; /* Checksum high address byte.*/ sum += address & 0xff; /* Checksum low address byte.*/ sum += i; /* Checksum record byte count.*/ sum += 2; /* type */ line = buffer; /* Now output the line! */ putchar(':'); puthex(i,2); /* Byte count. */ puthex(address,4); /* Do address and increment */ address += i; /* by bytes in record. */ address &= 0x0FFFF; puthex(0,2); /* Record type. */ for(i*=2;i;i--) /* Then the actual data. */ putchar(*line++); puthex(0-sum,2); /* Checksum is 1byte 2's comp.*/ printf("\n"); } if ((address/REC) == 0) pagecount++; } printf(":00000001FF\n"); /* End record. */ return 0; } /* Return ASCII hex character for binary value. */ char hex(c) int c; { if((c &= 0x000f)<10) c += '0'; else c += 'A'-10; return((char) c); } /* Put specified number of digits in ASCII hex. */ void puthex(val,digits) int val,digits; { if (--digits) puthex(val>>4,digits); putchar(hex(val & 0x0f)); }