1 /* snp.c: Routines for handling .snp snapshots
2    Copyright (c) 1998,2003 Philip Kendall
3 
4    $Id: snp.c 2890 2007-05-26 19:31:43Z zubzero $
5 
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10 
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15 
16    You should have received a copy of the GNU General Public License along
17    with this program; if not, write to the Free Software Foundation, Inc.,
18    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 
20    Author contact information:
21 
22    E-mail: philip-fuse@shadowmagic.org.uk
23 
24 */
25 
26 #include <config.h>
27 
28 #include "internals.h"
29 
30 #define SNP_TRAILER_LENGTH 31
31 
32 libspectrum_error
libspectrum_snp_read(libspectrum_snap * snap,const libspectrum_byte * buffer,size_t length)33 libspectrum_snp_read( libspectrum_snap *snap, const libspectrum_byte *buffer,
34 		      size_t length )
35 {
36   libspectrum_error error;
37 
38   /* Length must be at least 48K of RAM plus the 'trailer' */
39   if( length < 0xc000 + SNP_TRAILER_LENGTH ) {
40     libspectrum_print_error(
41       LIBSPECTRUM_ERROR_CORRUPT,
42       "libspectrum_snp_read: not enough bytes for a .snp file"
43     );
44     return LIBSPECTRUM_ERROR_CORRUPT;
45   }
46 
47   /* All .snp snaps are of the 48K machine */
48   libspectrum_snap_set_machine( snap, LIBSPECTRUM_MACHINE_48 );
49 
50   /* Get the RAM */
51   error = libspectrum_split_to_48k_pages( snap, buffer );
52   if( error != LIBSPECTRUM_ERROR_NONE ) return error;
53 
54   buffer += 0xc000;
55 
56   libspectrum_snap_set_f      ( snap, buffer[ 0] );
57   libspectrum_snap_set_a      ( snap, buffer[ 1] );
58   libspectrum_snap_set_out_ula( snap, buffer[ 2] );
59   /* Byte 3 not used */
60   libspectrum_snap_set_bc     ( snap, buffer[ 4] + buffer[ 5] * 0x100 );
61   libspectrum_snap_set_de     ( snap, buffer[ 6] + buffer[ 7] * 0x100 );
62   libspectrum_snap_set_hl     ( snap, buffer[ 8] + buffer[ 9] * 0x100 );
63   libspectrum_snap_set_pc     ( snap, buffer[10] + buffer[11] * 0x100 );
64   libspectrum_snap_set_sp     ( snap, buffer[12] + buffer[13] * 0x100 );
65   libspectrum_snap_set_ix     ( snap, buffer[14] + buffer[15] * 0x100 );
66   libspectrum_snap_set_iy     ( snap, buffer[16] + buffer[17] * 0x100 );
67   libspectrum_snap_set_iff1   ( snap, buffer[18] );
68   libspectrum_snap_set_iff2   ( snap, buffer[19] );
69   libspectrum_snap_set_im     ( snap, buffer[20] );
70   libspectrum_snap_set_r      ( snap, buffer[21] );
71   libspectrum_snap_set_i      ( snap, buffer[22] );
72   libspectrum_snap_set_f_     ( snap, buffer[23] );
73   libspectrum_snap_set_a_     ( snap, buffer[24] );
74   libspectrum_snap_set_bc_    ( snap, buffer[25] + buffer[26] * 0x100 );
75   libspectrum_snap_set_de_    ( snap, buffer[27] + buffer[28] * 0x100 );
76   libspectrum_snap_set_hl_    ( snap, buffer[29] + buffer[30] * 0x100 );
77 
78   return LIBSPECTRUM_ERROR_NONE;
79 }
80