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 >
Wrap
C/C++ Source or Header
|
1990-12-28
|
1KB
|
96 lines
/*
Load .HEX format file into Z-80 memory.
*/
#include <stdio.h>
#include <strings.h>
#include <ctype.h>
#include "z80.h"
BYTE hexval(),hex_byte(),csum();
int getline(f,l)
FILE *f;
char *l;
{
int c;
while((c=getc(f))!=EOF && c!='\n')
*l++=c;
if (c==EOF)
return 1;
*l=0;
return 0;
}
loadhex(f)
FILE *f;
{
char line[80],*ptr;
BYTE count,parse,i,cs;
WORD addr;
while(!getline(f,line))
{
ptr=line;
if (index(ptr,':')==NULL)
continue;
ptr=index(ptr,':')+1;
if (cs=csum(ptr))
{
printf("Checksum error: %s\n",ptr);
continue;
}
count=hex_byte(ptr);
ptr+= 2;
addr=(hex_byte(ptr)<<8)|hex_byte(ptr+2);
ptr+= 4;
parse=hex_byte(ptr);
ptr+= 2;
/* check parse byte if you want... */
for(i=0;i<count;i++,ptr+= 2)
{
real_z80_mem[addr+i]=hex_byte(ptr);
}
}
}
BYTE hex_byte(c)
char *c;
{
return ((hexval(*c)<<4)|hexval(*(c+1)));
}
BYTE hexval(c)
char c;
{
char *l;
static char digits[]="0123456789ABCDEF";
if (islower(c))
c=toupper(c);
l=index(digits,c);
if (l==NULL)
return 255;
return l-digits;
}
BYTE csum(l)
char *l;
{
BYTE csum=0;
while(strlen(l))
{
csum+=(hex_byte(l)&0xff);
l+=2;
}
return csum;
}