/*-------------------------------------------------------------------* * File Name: s37tos28.c * * Author: Jonathan E. Flamm * Date: 6/4/93 * Description: /* SREC Format converter -- converts from s37 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 putc3BHex( unsigned int x ); unsigned int get1BHex( 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 ) { static char line[200]; unsigned int addr; unsigned int bytes; unsigned int addrBytes; unsigned int checksum; int i; if (argc != 3) { fprintf(stderr, "\nUsage: s37tos28 infile outfile\n"); fprintf(stderr, "Converts s37 srec to s28 format\n"); exit(1); } infile = fopen( (const char *)argv[1], "r" ); if ( infile == NULL) { fprintf(stderr, "Cannot open: %s\n",argv[1]); exit(1); } outfile = fopen( (const char *)argv[2], "w" ); if ( outfile == NULL) { fprintf(stderr, "Cannot open: %s\n",argv[2]); exit(1); } while ( fgets(line, 200, infile) ) { if ( line[0] != 'S') { fprintf(stderr, "Non S Record line in file\n"); exit(1); } switch (line[1]) { case '3': fputc('S', outfile); fputc('2', outfile); bytes = get1BHex( &line[2] ); putc1BHex( bytes - 1 ); addr = get4BHex( &line[4] ); if (addr & 0xff000000) { fprintf(stderr, "Address too large: %X\n",addr); exit(1); } putc3BHex( addr ); addrBytes = addr & 0xff; addrBytes += (addr >> 8) & 0xff; addrBytes += (addr >> 16) & 0xff; checksum = bytes - 1 + addrBytes; for (i = 0; i < (bytes - 5); i++ ) { fputc(line[12+(2*i)], outfile); fputc(line[12+(2*i)+1], outfile); checksum += get1BHex( &line[12+(2*i)] ); } checksum = 255 - (checksum % 256); putc1BHex( checksum ); fputc('\n', outfile); break; case '0': /* Header Record */ break; case '7': /* End Record */ fputc('S', outfile); fputc('8', outfile); putc1BHex( 4 ); addr = get4BHex( &line[4] ); putc3BHex( addr ); addrBytes = addr & 0xff; addrBytes += (addr >> 8) & 0xff; addrBytes += (addr >> 16) & 0xff; checksum = 4 + addrBytes; checksum = 255 - (checksum % 256); putc1BHex( checksum ); fputc('\n', outfile); break; default: fprintf(stderr, "Unknown record type '%c\n", line[1]); break; } /* end switch */ } } void putc1BHex( unsigned int x ) { 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 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; }