1 /* slt.c: SLT data handling routines
2    Copyright (c) 2004 Philip Kendall
3 
4    $Id: slt.c 4715 2012-06-07 03:32:59Z fredm $
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 <string.h>
29 
30 #include <libspectrum.h>
31 
32 #include "module.h"
33 #include "settings.h"
34 #include "slt.h"
35 #include "spectrum.h"
36 #include "ui/ui.h"
37 
38 /* .slt level data */
39 
40 static libspectrum_byte *slt[256];
41 static size_t slt_length[256];
42 
43 static libspectrum_byte *slt_screen;	/* The screenshot from the .slt file */
44 static int slt_screen_level;		/* The level of the screenshot.
45 					   Not used for anything AFAIK */
46 
47 static void slt_from_snapshot( libspectrum_snap *snap );
48 static void slt_to_snapshot( libspectrum_snap *snap );
49 
50 static module_info_t slt_module_info = {
51 
52   NULL,
53   NULL,
54   NULL,
55   slt_from_snapshot,
56   slt_to_snapshot,
57 
58 };
59 
60 void
slt_init(void)61 slt_init( void )
62 {
63   module_register( &slt_module_info );
64 }
65 
66 int
slt_trap(libspectrum_word address,libspectrum_byte level)67 slt_trap( libspectrum_word address, libspectrum_byte level )
68 {
69   size_t length;
70   libspectrum_byte *data;
71 
72   if( !settings_current.slt_traps ) return 0;
73 
74   if( slt_length[ level ] ) {
75 
76     length = slt_length[ level ];
77     data = slt[ level ];
78 
79     while( length-- ) writebyte( address++, *data++ );
80 
81   }
82 
83   return 0;
84 }
85 
86 static void
slt_from_snapshot(libspectrum_snap * snap)87 slt_from_snapshot( libspectrum_snap *snap )
88 {
89   size_t i;
90 
91   for( i=0; i<256; i++ ) {
92 
93     slt_length[i] = libspectrum_snap_slt_length( snap, i );
94 
95     if( slt_length[i] ) {
96 
97       slt[i] = memory_pool_allocate( slt_length[i] *
98 				     sizeof( libspectrum_byte ) );
99       if( !slt[i] ) {
100 	ui_error( UI_ERROR_ERROR, "Out of memory at %s:%d", __FILE__,
101 		  __LINE__ );
102 	return;
103       }
104 
105       memcpy( slt[i], libspectrum_snap_slt( snap, i ),
106 	      libspectrum_snap_slt_length( snap, i ) );
107     }
108   }
109 
110   if( libspectrum_snap_slt_screen( snap ) ) {
111 
112     slt_screen = memory_pool_allocate( 6912 * sizeof( libspectrum_byte ) );
113     if( !slt_screen ) {
114       ui_error( UI_ERROR_ERROR, "Out of memory at %s:%d", __FILE__, __LINE__ );
115       return;
116     }
117 
118     memcpy( slt_screen, libspectrum_snap_slt_screen( snap ), 6912 );
119     slt_screen_level = libspectrum_snap_slt_screen_level( snap );
120   }
121 }
122 
123 static void
slt_to_snapshot(libspectrum_snap * snap)124 slt_to_snapshot( libspectrum_snap *snap )
125 {
126   size_t i;
127   libspectrum_byte *buffer;
128 
129   for( i=0; i<256; i++ ) {
130 
131     libspectrum_snap_set_slt_length( snap, i, slt_length[i] );
132 
133     if( slt_length[i] ) {
134 
135       libspectrum_byte *buffer;
136 
137       buffer = libspectrum_malloc( slt_length[i] * sizeof(libspectrum_byte) );
138 
139       memcpy( buffer, slt[i], slt_length[i] );
140       libspectrum_snap_set_slt( snap, i, buffer );
141     }
142   }
143 
144   if( slt_screen ) {
145 
146     buffer = libspectrum_malloc( 6912 * sizeof( libspectrum_byte ) );
147 
148     memcpy( buffer, slt_screen, 6912 );
149     libspectrum_snap_set_slt_screen( snap, buffer );
150     libspectrum_snap_set_slt_screen_level( snap, slt_screen_level );
151   }
152 }
153