home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / hamradio / tnet_x1h.zip / MOTOROLA.C < prev    next >
Text File  |  1992-10-18  |  6KB  |  195 lines

  1. /* ************************************************************************
  2.  *
  3.  * Binary to hex converter for motorola format hex
  4.  *
  5.  * (C) By Dave Roberts G8KBB.
  6.  * This program is provided 'as is', with no warranty. It has been
  7.  * produced for use in self-tuition in amateur radio and is not
  8.  * to be used for commercial applications. It may be freely copied and
  9.  * used under these conditions.
  10.  *
  11.  * Invocation :
  12.  *
  13.  *    MOTOROLA infile outfile [ hex_start_address ]
  14.  *
  15.  * Return codes : AS detailed in the source. 0 for success, error
  16.  *                codes 1 - 5 errors as detailed below.
  17.  *
  18.  *************************************************************************/
  19.  
  20. #include <stdio.h>
  21.  
  22. unsigned Address;        /* effective address of data */
  23. unsigned char Buffer[32786];    /* buffer for i/p data from file */
  24. unsigned char *Ptr;        /* pointer into buffer for reading */
  25. unsigned Count;            /* number of bytes left in buffer */
  26. FILE *Fpin, *Fpout;        /* input & output file handles */
  27.  
  28. /* ***********************************************************************
  29.  * this is the main program.
  30.  * It exits with an error code of 0 if all went correctly.
  31.  * With 1 for a syntax error
  32.  *      2 for an input file open error
  33.  *      3 for an output file open error
  34.  *      4 for an input file read error
  35.  *      5 for an output file write error
  36.  * **********************************************************************/
  37.  
  38. main( argc, argv )
  39. char *argv[];
  40. {
  41.     int ecode;        /* error code on exit from main loop */
  42.     char *tmpptr;        /* scratch pointer for strtol() */
  43.     int cont;        /* flag to say stop looping */
  44.  
  45.     Address = 0;        /* address is zero unless user inputs one */
  46.     Count = 0;        /* mark buffer as empty */
  47.     ecode = 0;        /* error code will be zero if all goes well */
  48.     Ptr = Buffer;        /* point to start of buffer */
  49.     cont = 1;        /* flag that makes main loop loop ! */
  50.  
  51.     /* we need an input file & an output file.
  52.      * A start address is optional
  53.      * so need 2 or 3 parameters !
  54.      */
  55.     if( argc < 3 || argc > 4 )
  56.     {
  57.         printf( "Error - usage is :\n    motorola infile outfile [ start-address ]\n");
  58.         exit( 1 );
  59.     }
  60.  
  61.     /* OK, we seem to have the parameters - so let's open the input
  62.      * file in BINARY !!!! mode.
  63.      * Error & exit if cannot.
  64.      */
  65.     if( ( Fpin = fopen( argv[1], "rb"  ) ) == NULL )
  66.     {
  67.         perror("Input file error ");
  68.         exit( 2 );
  69.     }
  70.  
  71.     /* Next, lets open the output file for write, in default mode
  72.      * Error & exit if cannot
  73.      */
  74.     if( ( Fpout = fopen( argv[2], "w" ) ) == NULL )
  75.     {
  76.         perror("Output file error ");
  77.         exit( 3 );
  78.     }
  79.  
  80.     /* Now the optional start address.
  81.      * WARNING - being a lazy sod, just take the last 16 bits
  82.      */
  83.     if( argc == 4 )
  84.         Address = strtol( argv[3], &tmpptr, 16 );
  85.  
  86.     /* output the file header record
  87.      */
  88.     fprintf( Fpout, "S00600004844521b\n" );
  89.  
  90.     /* MAIN LOOP - keep doing this until there is no more data
  91.      * or until an error occurs.
  92.      *
  93.      * Read a buffer full if there is less than 16 bytes left in buffer
  94.      * Crash out if there is a read error.
  95.      * Dump either 16 bytes, or whatever is left to dump
  96.      * Crash out if write error
  97.      * Exit anyway if there was less than 16 bytes ( as we did not 
  98.      * read any more
  99.      */
  100.     while( cont )
  101.     {
  102.         if( Count < 16 )
  103.         {
  104.             if( !getdata() )
  105.             {
  106.                 perror("Input file ");
  107.                 ecode = 4;
  108.                 break;
  109.             }
  110.         }
  111.         if( Count < 16 )
  112.             cont = 0;
  113.         if( !dump( Count >= 16 ? 16 : Count ) )
  114.         {
  115.             perror("Output file ");
  116.             ecode = 5;
  117.             break;
  118.         }
  119.     }
  120.  
  121.     /* dump trailing line
  122.      * close files
  123.      * and exit.
  124.      */
  125.     fprintf( Fpout, "S9030000fc\n" );
  126.     fclose( Fpin );
  127.     fclose( Fpout );
  128.     return( ecode );
  129. }
  130.  
  131. /* ***********************************************************************
  132.  * This function reads data from the input file.
  133.  * Firstly, if there is still a bit of data left in the buffer, move
  134.  * it down to the bottom of the buffer.
  135.  * Then point to the start of the buffer so we handle that data first.
  136.  * Now try to fill the buffer up.
  137.  * If we read less than we expected, just check to see that the reason
  138.  * was end of file & if not die horribly.
  139.  * Now check for file read errors for additional ways to die.
  140.  * Finally, update the byte counter to reflect the new buffer size & return
  141.  * a return of 0 means error, 1 means all went well.
  142.  * ***********************************************************************/
  143. getdata()
  144. {
  145.     unsigned int i;        /* temp store used for bytes actually read */
  146.  
  147.     if( Count != 0 )
  148.         memmove( Buffer, Ptr, Count );
  149.     Ptr = Buffer;
  150.     i = fread( Buffer+Count, sizeof( char ), sizeof(Buffer)-Count, Fpin );
  151.     if( i != sizeof(Buffer)-Count && !feof( Fpin ) )
  152.         return( 0 );
  153.     if( ferror( Fpin ) )
  154.         return( 0 );
  155.     Count += i;
  156.     return( 1 );
  157. }
  158.  
  159. /* ************************************************************************
  160.  * Dump function for a line of data.
  161.  * This outputs a single line of (num) bytes of data.
  162.  * It checks each write for an error.
  163.  * Firstly, it sends the line preamble, count, address and type.
  164.  * Then it sends each byte in turn.
  165.  * Finally it computes the checksum & sends that too.
  166.  * A return of 0 means error, a return of 1 means all went well.
  167.  * ***********************************************************************/
  168. dump( num )
  169. unsigned num;
  170. {
  171.     int i;            /* counter for number of bytes processed */
  172.     unsigned char csum;    /* checksum value for line of data */
  173.  
  174.     if( num == 0 )
  175.         return( 1 );
  176.     fprintf( Fpout, "S1%02x%04x", num, Address );
  177.     if( ferror( Fpout ) )
  178.         return( 0 );
  179.     csum = num + ( (Address >> 8) & 0xff) + (Address & 0xff);
  180.     for( i=0; i<num; i++ )
  181.     {
  182.         fprintf( Fpout, "%02x", *Ptr );
  183.         if( ferror( Fpout ) )
  184.             return( 0 );
  185.         csum += *Ptr++;
  186.         Count--;
  187.         if( ! ++Address )
  188.             printf("WARNING. The address exceeded 0xffff.\n");
  189.     }
  190.     fprintf( Fpout, "%02x\n", (~csum) & 0xff );
  191.     if( ferror( Fpout ) )
  192.         return( 0 );
  193.     return( 1 );
  194. }
  195.