/*-------------------------------------------------------------------* * File Name: bintos28.c * * Author: Jonathan E. Flamm * Date: 6/4/93 * Description: /* SREC Format converter -- converts from bin to s28 * * History: * 6/4/94 jef: Adapted from GNU code *-------------------------------------------------------------------* * * SREC Format is as follows: * * S
* * S starting character of a record * type 0 = header record * 1 = 2 byte address data record * 2 = 3 byte address data record * 3 = 4 byte address data record * 7 = 4 byte address termination record * 8 = 3 byte address termination record * 9 = 2 byte address termination record * * address - start address of data * data - data * checksum - sum of raw byte data in record from length * upwards modulo 256 and subtracted from 255 *-------------------------------------------------------------------*/ #include void putc1BHex( unsigned int x ); void putc2BHex( unsigned int x ); void putc3BHex( unsigned int x ); unsigned int get1BHex( char *p ); unsigned int get2BHex( char *p ); unsigned int get3BHex( char *p ); unsigned int get4BHex( char *p ); unsigned int htoi( char c ); char itoh( unsigned int x ); FILE *infile, *outfile; main( int argc, char **argv ) { unsigned int addr; unsigned int bytes; unsigned count; unsigned char line[0x10]; unsigned int addrBytes; unsigned int offset; unsigned int checksum; int i; int fd; if ( (argc != 3) && (argc != 4) ) { fprintf(stderr, "\nUsage: bintos28 infile outfile [hex offset]\n"); fprintf(stderr, "Converts bin to s28 format\n"); exit(1); } infile = fopen( (const char *)argv[1], "rb" ); if ( infile == NULL) { fprintf(stderr, "Cannot open: %s\n",argv[2]); exit(1); } outfile = fopen( (const char *)argv[2], "w" ); if ( outfile == NULL) { fprintf(stderr, "Cannot open: %s\n",argv[2]); exit(1); } if (argc == 4) sscanf( argv[3], "%X", &offset ); else offset = 0; fprintf( stdout, "offset = %X\n", offset ); bytes = 0x14; addr = offset; while ( (count = fread( line, 1, 0x10, infile )) == 0x10) { fputc('S', outfile); fputc('2', outfile); putc1BHex( bytes ); putc3BHex( addr ); addrBytes = addr & 0xff; addrBytes += (addr >> 8) & 0xff; addrBytes += (addr >> 16) & 0xff; checksum = bytes + addrBytes; addr += 0x10; for (i=0; i<0x10; i++) { putc1BHex( line[i] ); checksum += line[i]; } checksum = 255 - (checksum % 256); putc1BHex( checksum ); fputc('\n', outfile); } fprintf( stderr, "last count = %X\n", count ); if (count != 0) { fputc('S', outfile); fputc('2', outfile); putc1BHex( count + 4 ); putc3BHex( addr ); addrBytes = addr & 0xff; addrBytes += (addr >> 8) & 0xff; addrBytes += (addr >> 16) & 0xff; checksum = (count + 4) + addrBytes; addr += count; for (i=0; i> 8) & 0xff; addrBytes += (addr >> 16) & 0xff; checksum = 4 + addrBytes; putc1BHex( checksum ); fputc('\n', outfile); fprintf(stdout, "address span <%X,%X>\n", offset, addr); fprintf(stdout, "count=%d\n", addr - offset); fclose(outfile); } void putc1BHex( unsigned int x ) { fputc( itoh( (x >> 4) & 0xF ), outfile ); fputc( itoh(x & 0xF), outfile ); } void putc2BHex( unsigned int x ) { fputc( itoh((x >> 12) & 0xF), outfile ); fputc( itoh((x >> 8) & 0xF), outfile ); fputc( itoh((x >> 4) & 0xF), outfile ); fputc( itoh(x & 0xF), outfile ); } void putc3BHex( unsigned int x ) { fputc( itoh((x >> 20) & 0xF), outfile ); fputc( itoh((x >> 16) & 0xF), outfile ); fputc( itoh((x >> 12) & 0xF), outfile ); fputc( itoh((x >> 8) & 0xF), outfile ); fputc( itoh((x >> 4) & 0xF), outfile ); fputc( itoh(x & 0xF), outfile ); } unsigned int get1BHex( char *p ) { return ( htoi(*(p+1)) + ( htoi(*p) << 4 ) ); } unsigned int get2BHex( char *p ) { unsigned int x; x = (htoi(*(p+1)) + (htoi(*p) << 4 )) << 8; x += (htoi(*(p+3)) + (htoi(*(p+2)) << 4 )); return x; } unsigned int get3BHex( char *p ) { unsigned int x; x = (htoi(*(p+1)) + (htoi(*p) << 4 )) << 16; x += (htoi(*(p+3)) + (htoi(*(p+2)) << 4 )) << 8; x += (htoi(*(p+5)) + (htoi(*(p+4)) << 4 )); return x; } unsigned int get4BHex( char *p ) { unsigned int x; x = (htoi(*(p+1)) + (htoi(*p) << 4 )) << 24; x += (htoi(*(p+3)) + (htoi(*(p+2)) << 4 )) << 16; x += (htoi(*(p+5)) + (htoi(*(p+4)) << 4 )) << 8; x += (htoi(*(p+7)) + (htoi(*(p+6)) << 4 )); return x; } unsigned int htoi( char c ) { unsigned int val; if ( (c >= '0') && (c <= '9') ) val = c - '0'; else val = c - 'A' + 10; return val; } char itoh( unsigned int x ) { char val; if (x < 10) val = '0' + x; else val = 'A' + (x - 10); return val; }