From 06f2daa56e0eb8e592c015b07dbeb787bb081e4d Mon Sep 17 00:00:00 2001
From: Ian C <ianc@noddybox.co.uk>
Date: Mon, 5 Jul 2021 21:37:37 +0000
Subject: Added TAP file loading.

---
 source/gui.c      |  2 ++
 source/main.c     | 16 ++++++++++---
 source/snap.c     | 67 +++++++++++++++++++++++++++++++++++--------------------
 source/snapshot.c |  1 -
 source/spec.c     |  4 ++--
 5 files changed, 60 insertions(+), 30 deletions(-)

(limited to 'source')

diff --git a/source/gui.c b/source/gui.c
index 604f910..94b7f94 100644
--- a/source/gui.c
+++ b/source/gui.c
@@ -59,6 +59,8 @@ typedef struct
 
 static FSEL_File fsel[FSEL_MAX_FILES];
 
+char last_dir[FILENAME_MAX] = "/";
+
 
 static void CheckPath(char *path)
 {
diff --git a/source/main.c b/source/main.c
index f096f9a..0610a6f 100644
--- a/source/main.c
+++ b/source/main.c
@@ -31,6 +31,7 @@
 #include "spec.h"
 #include "config.h"
 #include "snapshot.h"
+#include "snap.h"
 
 #ifndef DSSPEC_VERSION
 #define DSSPEC_VERSION "DEV " __TIME__ "/" __DATE__
@@ -61,7 +62,7 @@
 static const char *main_menu[]=
 	{
 	    "Reset Spectrum",
-	    "Select Tape",
+            "Load TAP File",
 	    "Configure",
 	    "Map Joypad to Keys",
 	    "Save Memory Snapshot",
@@ -76,7 +77,7 @@ static const char *main_menu[]=
 typedef enum
 {
     MenuReset,
-    MenuSelectTape,
+    MenuLoadTAP,
     MenuConfigure,
     MenuMapJoypad,
     MenuSaveSnapshot,
@@ -299,8 +300,17 @@ int main(int argc, char *argv[])
 				SPECReset(z80);
 				break;
 
-			    case MenuSelectTape:
+			    case MenuLoadTAP:
+                            {
+                                char file[FILENAME_MAX];
+
+                                if (GUI_FileSelect(last_dir, file, ".TAP"))
+                                {
+                                    TAPCloseTape();
+                                    TAPOpenTape(file);
+                                }
 				break;
+                            }
 
 			    case MenuConfigure:
 				GUI_Config();
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;
     }
diff --git a/source/snapshot.c b/source/snapshot.c
index e75a359..61cffc2 100644
--- a/source/snapshot.c
+++ b/source/snapshot.c
@@ -137,7 +137,6 @@ void SNAP_Save(Z80 *cpu, SnapshotType type)
 
 void SNAP_Load(Z80 *cpu, const char *optional_name, SnapshotType type)
 {
-    static char last_dir[FILENAME_MAX] = "/";
     char file[FILENAME_MAX];
     FILE *fp = NULL;
 
diff --git a/source/spec.c b/source/spec.c
index 5fa5060..e0698c2 100644
--- a/source/spec.c
+++ b/source/spec.c
@@ -306,7 +306,7 @@ static int EDCallback(Z80 *z80, Z80Val data)
 	    if (TAPLoad(z80->AF.b[Z80_HI_WORD],
 			&z80->IX.w,
 			&z80->DE.w,
-			SPECWriteSnap))
+			SPECWriteMemSNAP))
 	    {
 		z80->AF.b[Z80_LO_WORD] |= eZ80_Carry;
 		z80->BC.w=0xb001;
@@ -394,7 +394,7 @@ void SPECWriteMem(Z80 *z80, Z80Word addr, Z80Byte val)
 }
 
 
-void SPECWriteSnap(Z80Word addr, Z80Byte val)
+void SPECWriteMemSNAP(Z80Word addr, Z80Byte val)
 {
     if (addr>=ROMLEN)
     {
-- 
cgit v1.2.3