1 /* slt.c: SLT data handling routines
2    Copyright (c) 2004-2016 Philip Kendall
3    Copyright (c) 2015 Stuart Brady
4    Copyright (c) 2015 Fredrick Meunier
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 "infrastructure/startup_manager.h"
33 #include "module.h"
34 #include "settings.h"
35 #include "slt.h"
36 #include "spectrum.h"
37 #include "ui/ui.h"
38 
39 /* .slt level data */
40 
41 static libspectrum_byte *slt[256];
42 static size_t slt_length[256];
43 
44 static libspectrum_byte *slt_screen;	/* The screenshot from the .slt file */
45 static int slt_screen_level;		/* The level of the screenshot.
46 					   Not used for anything AFAIK */
47 
48 static void slt_from_snapshot( libspectrum_snap *snap );
49 static void slt_to_snapshot( libspectrum_snap *snap );
50 
51 static module_info_t slt_module_info = {
52 
53   NULL,
54   NULL,
55   NULL,
56   slt_from_snapshot,
57   slt_to_snapshot,
58 
59 };
60 
61 static int
slt_init(void * context)62 slt_init( void *context )
63 {
64   module_register( &slt_module_info );
65 
66   return 0;
67 }
68 
69 void
slt_register_startup(void)70 slt_register_startup( void )
71 {
72   startup_manager_module dependencies[] = { STARTUP_MANAGER_MODULE_SETUID };
73   startup_manager_register( STARTUP_MANAGER_MODULE_SLT, dependencies,
74                             ARRAY_SIZE( dependencies ), slt_init, NULL, NULL );
75 }
76 
77 int
slt_trap(libspectrum_word address,libspectrum_byte level)78 slt_trap( libspectrum_word address, libspectrum_byte level )
79 {
80   size_t length;
81   libspectrum_byte *data;
82 
83   if( !settings_current.slt_traps ) return 0;
84 
85   if( slt_length[ level ] ) {
86 
87     length = slt_length[ level ];
88     data = slt[ level ];
89 
90     while( length-- ) writebyte( address++, *data++ );
91 
92   }
93 
94   return 0;
95 }
96 
97 static void
slt_from_snapshot(libspectrum_snap * snap)98 slt_from_snapshot( libspectrum_snap *snap )
99 {
100   size_t i;
101 
102   for( i=0; i<256; i++ ) {
103 
104     slt_length[i] = libspectrum_snap_slt_length( snap, i );
105 
106     if( slt_length[i] ) {
107 
108       slt[i] = memory_pool_allocate( slt_length[i] *
109 				     sizeof( libspectrum_byte ) );
110 
111       memcpy( slt[i], libspectrum_snap_slt( snap, i ),
112 	      libspectrum_snap_slt_length( snap, i ) );
113     }
114   }
115 
116   if( libspectrum_snap_slt_screen( snap ) ) {
117 
118     slt_screen = memory_pool_allocate( 6912 * sizeof( libspectrum_byte ) );
119 
120     memcpy( slt_screen, libspectrum_snap_slt_screen( snap ), 6912 );
121     slt_screen_level = libspectrum_snap_slt_screen_level( snap );
122   }
123 }
124 
125 static void
slt_to_snapshot(libspectrum_snap * snap)126 slt_to_snapshot( libspectrum_snap *snap )
127 {
128   size_t i;
129   libspectrum_byte *buffer;
130 
131   for( i=0; i<256; i++ ) {
132 
133     libspectrum_snap_set_slt_length( snap, i, slt_length[i] );
134 
135     if( slt_length[i] ) {
136 
137       buffer = libspectrum_new( libspectrum_byte, slt_length[i] );
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_new( libspectrum_byte, 6912 );
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