home *** CD-ROM | disk | FTP | other *** search
/ Euroscene 1 / Euroscene 1.iso / resource / adev11_12.lha / utils / mhex.c
Encoding:
C/C++ Source or Header  |  1992-12-10  |  3.8 KB  |  133 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4. #include <math.h>
  5.  
  6. #define ULONG unsigned long
  7. #define UBYTE unsigned char
  8. #define BOOL char
  9. #define TRUE 1
  10. #define FALSE 0
  11.  
  12. #define STRING_SIZE 100
  13.  
  14. UBYTE far data_blk[65536];
  15.  
  16. #define HEX(c) ((c>'9')?c-'A'+10:c-'0')
  17.  
  18. void stch_l(char *chr_ptr, ULONG *u_ptr) {
  19.   *u_ptr=0;
  20.   while (isxdigit(*chr_ptr)) {
  21.     *u_ptr=*u_ptr*16+HEX(*chr_ptr);
  22.     chr_ptr++;
  23.   }
  24. }
  25.  
  26. BOOL load(char *dest, char *src, ULONG *addr, ULONG *next_addr) {
  27.   UBYTE chksum, temp1, temp2, byte_count, i;
  28.  
  29.   chksum=byte_count=(HEX(src[0]))*16 + HEX(src[1]); src+=2;
  30.   temp1=(HEX(src[0]))*16 + HEX(src[1]);
  31.   temp2=(HEX(src[2]))*16 + HEX(src[3]); src+=4;
  32.   *addr=(long)temp1*256 + (long)temp2;
  33.   *next_addr=*addr+(long)byte_count-3;
  34.   chksum+=temp1+temp2;
  35.   for (i=0;i<byte_count-3;i++) {
  36.     temp1=(HEX(src[0]))*16 + HEX(src[1]); src+=2;
  37.     chksum+=temp1;
  38.     *(dest+(long)*addr+i)=temp1;
  39.   }
  40.   temp1=(HEX(src[0]))*16 + HEX(src[1]); src+=2;
  41.   chksum+=temp1;
  42.   if (chksum == 255) return(TRUE);
  43.   return(FALSE);
  44. }
  45.  
  46. void main(int argc, char **argv) {
  47.   FILE *file_ptr;
  48.   char string[STRING_SIZE];
  49.   ULONG addr,next_addr;
  50.   ULONG min_addr=65535,max_addr=0;
  51.   ULONG line_num=0;
  52.   ULONG i;
  53.  
  54.   if (argc!=3 || argc==2 && argv[1][0]=='?')
  55.     printf("USAGE: %s <source> <destination>\n",argv[0]);
  56.   else {
  57.     if (!(file_ptr=fopen(argv[1],"r")))
  58.       printf("Unable to open %s for input\n",argv[1]);
  59.     else {
  60.       for (i=0;i<65536;i++) {
  61.         data_blk[i]=0xff;
  62.       }
  63.       while (fgets(string,STRING_SIZE,file_ptr)) {
  64.         line_num++;
  65.         if (string[0]!='S')
  66.           printf("Not a Motorola Hex format file\n");
  67.         else {
  68.           switch (string[1]) {
  69.             case '0':
  70.               if (!load(string,&string[2],&addr,&next_addr))
  71.                 printf("format or checksum error in line %ld\n",line_num);
  72.               else {
  73.                 string[next_addr]='\0';
  74.                 printf("  Loading program %s\n",string);
  75.               }
  76.               break;
  77.             case '1':
  78.               if (!load(data_blk,&string[2],&addr,&next_addr))
  79.                 printf("format or checksum error in line %ld\n",line_num);
  80.               else {
  81.                 max_addr=max((long)max_addr,next_addr);
  82.                 min_addr=min((long)min_addr,addr);
  83.               }
  84.               break;
  85.             case '2':
  86.               printf("This program does not support 24 bit addresses\n");
  87.               break;
  88.             case '8':
  89.               printf("This program does not support 24 bit addresses\n");
  90.               break;
  91.             case '9':
  92.               if (!load(data_blk,&string[2],&addr,&next_addr))
  93.                 printf("format or checksum error in line %ld",line_num);
  94.               else {
  95.                 printf("  Start address = %ld\n",addr);
  96.               }
  97.               break;
  98.           }
  99.         }
  100.       }
  101.       fclose(file_ptr);
  102.       printf("  Address range = %ld - %ld\n",min_addr,max_addr-1);
  103.       while (TRUE) {
  104.         printf("What is the starting address of the EPROM (Hex) ? ");
  105.         gets(string);
  106.         stch_l(string,&addr);
  107.         if (addr>65535 || addr<0)
  108.           printf("Invalid address\n");
  109.         else break;
  110.       }
  111.       while (TRUE) {
  112.         printf("What is the ending address of the EPROM (Hex) [%d]? ",max_addr-1);
  113.         gets(string);
  114.         if (string[0] == '\0') break;
  115.         stch_l(string,&max_addr);
  116.         if (max_addr>65535 || max_addr<0)
  117.           printf("Invalid address\n");
  118.         else break;
  119.       }
  120.       if (!(file_ptr=fopen(argv[2],"wb")))
  121.         printf("Unable to open %s for output\n",argv[2]);
  122.       else {
  123.         while (max_addr-addr>=16384) {
  124.           fwrite(&data_blk[addr],16384,1,file_ptr);
  125.           addr+=16384;
  126.         }
  127.         fwrite(&data_blk[addr],max_addr-addr,1,file_ptr);
  128.       }
  129.       fclose(file_ptr);
  130.     }
  131.   }
  132. }
  133.