home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 200-299 / ff287.lzh / DAsm / src / ftohex.c < prev    next >
C/C++ Source or Header  |  1989-12-06  |  3KB  |  181 lines

  1.  
  2. /*
  3.  *  FTOHEX.C
  4.  *
  5.  *  (c)Copyright 1988, Matthew Dillon, All Rights Reserved.
  6.  *
  7.  *  FTOHEX format infile [outfile]
  8.  *
  9.  *  format: format used when assembling (asm705/asm65)
  10.  *        1,2,3        -generate straight hex file
  11.  *
  12.  *  compilable on an ibm-pc or Amiga  _fmode is for Lattice C on the ibm,
  13.  *  is IGNORED by Aztec C on the Amiga.  Note that INT and CHAR are not
  14.  *  used as ibm's lattice C uses 16 bit ints and unsigned chars.  Change
  15.  *  as needed.    No guarentees for the IBMPC version.
  16.  */
  17.  
  18. #include <stdio.h>
  19. #ifdef LATTICE
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #define ARGS(args) args
  23. #else
  24. #define ARGS(args) ()
  25. #endif
  26.  
  27. #ifdef IBM
  28. typedef char ubyte;
  29. typedef unsigned uword;
  30. typedef int void;
  31. #else
  32. typedef unsigned char ubyte;
  33. typedef unsigned short uword;
  34. #endif
  35.  
  36. #define PERLINE 16
  37.  
  38. #ifndef LATTICE
  39. extern long ftell();
  40. extern void fseek();
  41. #endif
  42.  
  43. void exiterr ARGS((ubyte *));
  44. void convert ARGS((short, FILE *, FILE *));
  45. uword getwlh ARGS((FILE *));
  46. void puth ARGS((ubyte, FILE *));
  47.  
  48. uword _fmode = 0;
  49.  
  50. void
  51. main(ac, av)
  52. char *av[];
  53. int ac;
  54. {
  55.     short format;
  56.     FILE *infile;
  57.     FILE *outfile;
  58.  
  59.     _fmode = 0x8000;
  60.     if (ac < 3) {
  61.     puts("FTOHEX format infile [outfile]");
  62.     puts("format 1,2, or 3.  3=raw");
  63.     puts("(C)Copyright 1987 by Matthew Dillon, All Rights Reserved");
  64.     exit(1);
  65.     }
  66.     format = atoi(av[1]);
  67.     if (format < 1 || format > 3)
  68.     exiterr("specify infile format 1, 2, or 3");
  69.     infile = fopen(av[2], "r");
  70.     if (infile == NULL)
  71.     exiterr("unable to open input file");
  72.     outfile = (av[3]) ? fopen(av[3], "w") : stdout;
  73.     if (outfile == NULL)
  74.     exiterr("unable to open output file");
  75.     convert(format, infile, outfile);
  76.     fclose(infile);
  77.     fclose(outfile);
  78. }
  79.  
  80. void
  81. exiterr(str)
  82. ubyte *str;
  83. {
  84.     fputs(str, stderr);
  85.     fputs("\n", stderr);
  86.     exit(1);
  87. }
  88.  
  89. /*
  90.  *  Formats:
  91.  *
  92.  *  1:    origin (word:lsb,msb) + data
  93.  *  2:    origin (word:lsb,msb) + length (word:lsb,msb) + data  (repeat)
  94.  *  3:    data
  95.  *
  96.  *  Hex output:
  97.  *
  98.  *  :lloooo00(ll bytes hex code)cc      ll=# of bytes
  99.  *                    oooo=origin
  100.  *                    cc=invert of checksum all codes
  101.  */
  102.  
  103. void
  104. convert(format, in, out)
  105. short format;
  106. FILE *in;
  107. FILE *out;
  108. {
  109.     uword org = 0;
  110.     uword idx;
  111.     long len;
  112.     ubyte buf[256];
  113.  
  114.     if (format < 3)
  115.     org = getwlh(in);
  116.     if (format == 2) {
  117.     len = getwlh(in);
  118.     } else {
  119.     long begin = ftell(in);
  120.     fseek(in, 0L, 2);
  121.     len = ftell(in) - begin;
  122.     fseek(in, begin, 0);
  123.     }
  124.     for (;;) {
  125.     while (len > 0) {
  126.         register ubyte chk;
  127.         register short i;
  128.  
  129.         idx = (len > PERLINE) ? PERLINE : len;
  130.         fread(buf, idx, 1, in);
  131.         putc(':', out);
  132.         puth((ubyte)idx, out);
  133.         puth((ubyte)(org >> 8), out);
  134.         puth((ubyte)(org & 0xFF), out);
  135.         putc('0', out);
  136.         putc('0', out);
  137.         chk = idx + (org >> 8) + (org & 0xFF);
  138.         for (i = 0; i < idx; ++i) {
  139.         chk += buf[i];
  140.         puth(buf[i], out);
  141.         }
  142.         puth((ubyte)-chk, out);
  143.         putc('\r', out);
  144.         putc('\n', out);
  145.         len -= idx;
  146.         org += idx;
  147.     }
  148.     if (format == 2) {
  149.         org = getwlh(in);
  150.         if (feof(in))
  151.         break;
  152.         len = getwlh(in);
  153.     } else {
  154.         break;
  155.     }
  156.     }
  157.     fprintf(out, ":00000001FF\r\n");
  158. }
  159.  
  160. uword
  161. getwlh(in)
  162. FILE *in;
  163. {
  164.     uword result;
  165.  
  166.     result = getc(in);
  167.     result += getc(in) << 8;
  168.     return(result);
  169. }
  170.  
  171. void
  172. puth(c, out)
  173. ubyte c;
  174. FILE *out;
  175. {
  176.     static ubyte dig[] = { "0123456789ABCDEF" };
  177.     putc(dig[c>>4], out);
  178.     putc(dig[c&15], out);
  179. }
  180.  
  181.