home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2037 / loadhex.c < prev    next >
C/C++ Source or Header  |  1990-12-28  |  1KB  |  96 lines

  1. /*
  2.  
  3. Load .HEX format file into Z-80 memory.
  4.  
  5. */
  6.  
  7. #include <stdio.h>
  8. #include <strings.h>
  9. #include <ctype.h>
  10. #include "z80.h"
  11.  
  12. BYTE hexval(),hex_byte(),csum();
  13.  
  14. int getline(f,l)
  15. FILE *f;
  16. char *l;
  17. {
  18.   int c;
  19.  
  20.   while((c=getc(f))!=EOF && c!='\n')
  21.     *l++=c;
  22.   if (c==EOF)
  23.     return 1;
  24.   *l=0;
  25.   return 0;
  26. }
  27.  
  28. loadhex(f)
  29. FILE *f;
  30. {
  31.   char line[80],*ptr;
  32.   BYTE count,parse,i,cs;
  33.   WORD addr;
  34.  
  35.   while(!getline(f,line))
  36.   {
  37.     ptr=line;
  38.     if (index(ptr,':')==NULL)
  39.       continue;
  40.     ptr=index(ptr,':')+1;
  41.     if (cs=csum(ptr))
  42.     {
  43.       printf("Checksum error: %s\n",ptr);
  44.       continue;
  45.     }
  46.     count=hex_byte(ptr);
  47.     ptr+= 2;
  48.     addr=(hex_byte(ptr)<<8)|hex_byte(ptr+2);
  49.     ptr+= 4;
  50.     parse=hex_byte(ptr);
  51.     ptr+= 2;
  52.  
  53.     /* check parse byte if you want... */
  54.  
  55.     for(i=0;i<count;i++,ptr+= 2)
  56.     {
  57.       real_z80_mem[addr+i]=hex_byte(ptr);
  58.     }
  59.   }
  60.   
  61. }
  62.  
  63. BYTE hex_byte(c)
  64. char *c;
  65. {
  66.   return ((hexval(*c)<<4)|hexval(*(c+1)));
  67. }
  68.  
  69. BYTE hexval(c)
  70. char c;
  71. {
  72.   char *l;
  73.   static char digits[]="0123456789ABCDEF";
  74.  
  75.   if (islower(c))
  76.     c=toupper(c);
  77.   l=index(digits,c);
  78.   if (l==NULL)
  79.     return 255;
  80.   return l-digits;
  81. }
  82.  
  83. BYTE csum(l)
  84. char *l;
  85. {
  86.   BYTE csum=0;
  87.  
  88.   while(strlen(l))
  89.   {
  90.     csum+=(hex_byte(l)&0xff);
  91.     l+=2;
  92.   }
  93.  
  94.   return csum;
  95. }
  96.