summaryrefslogtreecommitdiff
path: root/source/snap.c
diff options
context:
space:
mode:
Diffstat (limited to 'source/snap.c')
-rw-r--r--source/snap.c150
1 files changed, 150 insertions, 0 deletions
diff --git a/source/snap.c b/source/snap.c
new file mode 100644
index 0000000..1ddb6b9
--- /dev/null
+++ b/source/snap.c
@@ -0,0 +1,150 @@
+/*
+
+ 3dsspec - Nintendo 3DS Sinclair Spectrum 48K emulator.
+
+ Copyright (C) 2021 Ian Cowburn
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License
+ as published by the Free Software Foundation; either version 2
+ of the License, or (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+
+ $Id: snap.c 4 2006-09-15 00:30:18Z ianc $
+
+*/
+#include "snap.h"
+
+/* ---------------------------------------- MACROS
+*/
+#ifndef TRUE
+#define TRUE 1
+#endif
+
+#ifndef FALSE
+#define FALSE 0
+#endif
+
+#define ROMLEN 0x4000
+
+
+/* ---------------------------------------- PRIVATE DATA
+*/
+static Z80Byte *tap_file;
+static int tap_len;
+static int tap_ptr;
+
+
+/* ---------------------------------------- PRIVATE FUNCTIONS
+*/
+static Z80Byte GetByte(void)
+{
+ Z80Byte ret=0;
+
+ if (tap_len)
+ {
+ ret=tap_file[tap_ptr];
+ tap_ptr=(tap_ptr+1)%tap_len;
+ }
+
+ return ret;
+}
+
+
+static Z80Word GetLSBWord(void)
+{
+ int c1,c2;
+
+ c1=GetByte();
+ c2=GetByte();
+
+ return c1+(c2<<8);
+}
+
+
+/* ---------------------------------------- INTERFACES
+*/
+void TAPSetTape(Z80Byte *tape, int len)
+{
+ tap_file = tape;
+ tap_len = len;
+ tap_ptr = 0;
+}
+
+
+int TAPLoad(Z80Byte id, Z80Word *addr, Z80Word *len, SNAP_Poke poke)
+{
+ Z80Word blen;
+ Z80Byte type,b,csum,tape_csum;
+
+ if (!tap_file)
+ {
+ return FALSE;
+ }
+
+ b=0;
+
+ blen=GetLSBWord();
+ type=GetByte();
+ csum=id;
+
+ /* Have we found the requested block?
+ */
+ if (id==type)
+ {
+ /* Knock of block type
+ */
+ blen--;
+
+ while(blen && *len)
+ {
+ b=GetByte();
+ csum^=b;
+
+ poke(*addr,b);
+
+ (*addr)++;
+ (*len)--;
+ blen--;
+ }
+
+ /* Get the checksum
+ */
+ if (blen)
+ {
+ tape_csum=GetByte();
+ blen--;
+ }
+ else
+ tape_csum=b;
+
+ /* In case we've been request less bytes than the block size
+ */
+ while(blen--)
+ GetByte();
+
+ /* Check the checksum
+ */
+ return csum==tape_csum;
+ }
+ else
+ {
+ /* If it's the wrong type, skip it
+ */
+ while(blen--)
+ GetByte();
+
+ return FALSE;
+ }
+}
+
+
+/* END OF FILE */