?login_element?

Subversion Repositories NedoOS

Rev

Rev 630 | Blame | Compare with Previous | Last modification | View Log | Download

  1. /*
  2.         CASM Image Hex-to-Crl Converter
  3.  
  4.         Copyright (c) 1983 William C. Colley III
  5.  
  6.         Created 10 March 1983 by William C. Colley III
  7.  
  8. This utility converts the Intel hex image that results from the CASM/ASM
  9. process to a BDS C relocatable object image. CLOAD is necessary due to the
  10. CP/M LOAD.COM utility's inability to handle binary images having an origin
  11. address at anywhere other than 0100h.
  12.  
  13. To use the program, you do the following:
  14.  
  15.         A>cload <hexfile>[.hex] [-o <crlfile>[.crl]]
  16.  
  17. The optional -o option allows you to specify the name of the output file.  If
  18. you don't use -o, the output file will be named <hexfile>.crl.  The input file-
  19. name extension defaults to ".hex" if none is specified.  The output filename
  20. extension defaults to ".crl".
  21.  
  22. Compile & Link:
  23.         cc cload.c
  24.         clink cload -n
  25.  
  26. */
  27.  
  28. #include <stdio.h>
  29.  
  30. #define HARD    1
  31. #define SOFT    0
  32.  
  33. #define STACKSZ 1024
  34.  
  35. /*  These things are used to manipulate the input and output files.     */
  36.  
  37. char hexname[MAXLINE], crlname[MAXLINE];
  38. FILE *hex;  int crl;
  39.  
  40. /*  These things are used to manipulate the core buffer.                */
  41.  
  42. char *bbase, *bend, *bsize;
  43.  
  44. /*  These things are used to do the reading of the Intel hex file.      */
  45.  
  46. char bcnt, chks;
  47. union {
  48.     struct { char l, h; } b;
  49.     unsigned w;
  50. } addr;
  51.  
  52. /*  These things are reserved for the use of the main() function.       */
  53.  
  54. char *p;  int i;
  55.  
  56. /*  This thing is reserved for the use of the hgetc() function.         */
  57.  
  58. int j;
  59.  
  60. /*  This thing is reserved for the use of the hgetn() function.         */
  61.  
  62. int k;
  63.  
  64. main(argc,argv)
  65. int argc;
  66. char *argv[];
  67. {
  68.     char hgetc(), *endext(), *topofmem();
  69.  
  70.     puts("CASM Image Hex-to-Crl Converter -- v1.6\n");
  71.     puts("Copyright (c) 1983 William C. Colley III\n\n");
  72.  
  73.     for (p = hexname; --argc && p; ) {
  74.         if (**++argv == '-') p = (*++*argv == 'O' ? crlname : NULL);
  75.         else {
  76.             if (*p) p = NULL;
  77.             else {
  78.                 makename(p,*argv,p == hexname ? ".HEX" : ".CRL",SOFT);
  79.                 p = hexname;
  80.             }
  81.         }
  82.     }
  83.  
  84.     if (!p || !hexname[0]) {
  85.         puts("Usage:  A>cload hexname[.ext] [-o crlname[.ext]]\n");
  86.         exit();
  87.     }
  88.     if (!crlname[0]) makename(crlname,hexname,".CRL",HARD);
  89.  
  90.     if ((hex = fopen(hexname,"r")) == ERROR) die(NULL);
  91.     if ((crl = creat(crlname)) == ERROR) die(NULL);
  92.  
  93.     bbase = bsize = sbrk(5000);  bend = topofmem() - STACKSZ;
  94.  
  95.     for (;;) {
  96.         while ((i = getc(hex)) != ':')
  97.             if (i == ERROR || i == CPMEOF) badfile();
  98.         chks = 0;  bcnt = hgetc();  addr.b.h = hgetc();
  99.         addr.b.l = hgetc();  hgetc();
  100.         if (bcnt == 0) {
  101.             if (hgetc(),chks) badfile();
  102.             break;
  103.         }
  104.         for (p = bbase + addr.w - 0x100; bcnt; --bcnt) {
  105.             if (p > bend) die("Out of memory");
  106.             if (p > bsize) bsize = p;
  107.             *p++ = hgetc();
  108.         }
  109.         if (hgetc(),chks) badfile();
  110.     }
  111.  
  112.     i = (bsize - bbase + SECSIZ) / SECSIZ;
  113.  
  114.     if(write(crl,bbase,i) != i || fclose(hex) == ERROR || close(crl) == ERROR)
  115.         die(NULL);
  116.  
  117.     puts(crlname);
  118.     puts(" successfully generated.\n");
  119. }
  120.  
  121. makename(new,old,ext,flg)
  122. char *new, *old, *ext;
  123. int flg;
  124. {
  125.      while (*old) {
  126.         if (*old == '.') { strcpy(new,flg ? ext : old);  return; }
  127.         *new++ = *old++;
  128.     }
  129.     strcpy(new,ext);
  130. }
  131.  
  132. int hgetc()
  133. {
  134.     int j;
  135.  
  136.     j = hgetn() << 4;  chks += (j += hgetn());  return j;
  137. }
  138.  
  139. int hgetn()
  140. {
  141.     int k;
  142.  
  143.     if ((k = getc(hex)) >= '0' && k <= '9') return k - '0';
  144.     if (k >= 'A' && k <= 'F') return k - ('A' - 10);
  145.     badfile();
  146. }
  147.  
  148. badfile()
  149. {
  150.     die("Defective Intel hex file");
  151. }
  152.  
  153. die(msg)
  154. char *msg;
  155. {
  156.     /*puts(msg ? msg : errmsg(errno()));  putchar('\n');*/
  157.     if (msg) {
  158.         puts(msg);
  159.         putchar('\n');
  160.     }else printf("Error #%d\n",errno());
  161.     exit();
  162. }
  163.