diff options
Diffstat (limited to 'source/snap.c')
-rw-r--r-- | source/snap.c | 67 |
1 files changed, 43 insertions, 24 deletions
diff --git a/source/snap.c b/source/snap.c index 1ddb6b9..e998ba2 100644 --- a/source/snap.c +++ b/source/snap.c @@ -21,6 +21,9 @@ $Id: snap.c 4 2006-09-15 00:30:18Z ianc $ */ +#include <stdlib.h> +#include <stdio.h> + #include "snap.h" /* ---------------------------------------- MACROS @@ -38,45 +41,57 @@ /* ---------------------------------------- PRIVATE DATA */ -static Z80Byte *tap_file; -static int tap_len; -static int tap_ptr; +static FILE *tapfile; /* ---------------------------------------- PRIVATE FUNCTIONS */ -static Z80Byte GetByte(void) +static Z80Byte GetTAPByte(void) { - Z80Byte ret=0; + int ret=0; - if (tap_len) + if (tapfile) { - ret=tap_file[tap_ptr]; - tap_ptr=(tap_ptr+1)%tap_len; + ret = fgetc(tapfile); + + if (ret == EOF) + { + ret = 0; + fclose(tapfile); + tapfile = NULL; + } } - return ret; + return (Z80Byte)ret; } -static Z80Word GetLSBWord(void) +static Z80Word GetTAPLSBWord(void) { int c1,c2; - c1=GetByte(); - c2=GetByte(); + c1=GetTAPByte(); + c2=GetTAPByte(); - return c1+(c2<<8); + return (Z80Word)(c1+(c2<<8)); } /* ---------------------------------------- INTERFACES */ -void TAPSetTape(Z80Byte *tape, int len) +void TAPOpenTape(const char *path) { - tap_file = tape; - tap_len = len; - tap_ptr = 0; + tapfile = fopen(path, "rb"); +} + + +void TAPCloseTape(void) +{ + if (tapfile) + { + fclose(tapfile); + tapfile = NULL; + } } @@ -85,15 +100,15 @@ int TAPLoad(Z80Byte id, Z80Word *addr, Z80Word *len, SNAP_Poke poke) Z80Word blen; Z80Byte type,b,csum,tape_csum; - if (!tap_file) + if (!tapfile) { return FALSE; } b=0; - blen=GetLSBWord(); - type=GetByte(); + blen=GetTAPLSBWord(); + type=GetTAPByte(); csum=id; /* Have we found the requested block? @@ -106,7 +121,7 @@ int TAPLoad(Z80Byte id, Z80Word *addr, Z80Word *len, SNAP_Poke poke) while(blen && *len) { - b=GetByte(); + b=GetTAPByte(); csum^=b; poke(*addr,b); @@ -120,16 +135,20 @@ int TAPLoad(Z80Byte id, Z80Word *addr, Z80Word *len, SNAP_Poke poke) */ if (blen) { - tape_csum=GetByte(); + tape_csum=GetTAPByte(); blen--; } else + { tape_csum=b; + } /* In case we've been request less bytes than the block size */ while(blen--) - GetByte(); + { + GetTAPByte(); + } /* Check the checksum */ @@ -140,7 +159,7 @@ int TAPLoad(Z80Byte id, Z80Word *addr, Z80Word *len, SNAP_Poke poke) /* If it's the wrong type, skip it */ while(blen--) - GetByte(); + GetTAPByte(); return FALSE; } |