-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathif2.c
197 lines (146 loc) · 4.7 KB
/
if2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/* if2.c: Interface II cartridge handling routines
Copyright (c) 2003 Darren Salt, Fredrick Meunier, Philip Kendall
Copyright (c) 2004 Fredrick Meunier
$Id: if2.c 3389 2007-12-03 12:54:17Z fredm $
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.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Author contact information:
E-mail: [email protected]
Darren: [email protected]
Fred: [email protected]
*/
#include <config.h>
#include <string.h>
#include "if2.h"
#include "machine.h"
#include "memory.h"
#include "module.h"
#include "periph.h"
#include "settings.h"
#include "ui/ui.h"
/* IF2 cart inserted? */
int if2_active = 0;
static void if2_reset( int hard_reset );
static void if2_memory_map( void );
static void if2_from_snapshot( libspectrum_snap *snap );
static void if2_to_snapshot( libspectrum_snap *snap );
static module_info_t if2_module_info = {
if2_reset,
if2_memory_map,
NULL,
if2_from_snapshot,
if2_to_snapshot,
};
int
if2_init( void )
{
module_register( &if2_module_info );
return 0;
}
int
if2_insert( const char *filename )
{
int error;
if ( !periph_interface2_active ) {
ui_error( UI_ERROR_ERROR,
"This machine does not support the Interface II" );
return 1;
}
error = settings_set_string( &settings_current.if2_file, filename );
if( error ) return error;
machine_reset( 0 );
return 0;
}
void
if2_eject( void )
{
if ( !periph_interface2_active ) {
ui_error( UI_ERROR_ERROR,
"This machine does not support the Interface II" );
return;
}
if( settings_current.if2_file ) free( settings_current.if2_file );
settings_current.if2_file = NULL;
machine_current->ram.romcs = 0;
ui_menu_activate( UI_MENU_ITEM_MEDIA_CARTRIDGE_IF2_EJECT, 0 );
machine_reset( 0 );
}
static void
if2_reset( int hard_reset GCC_UNUSED )
{
if2_active = 0;
if( !settings_current.if2_file ) {
ui_menu_activate( UI_MENU_ITEM_MEDIA_CARTRIDGE_IF2_EJECT, 0 );
return;
}
if ( !periph_interface2_active ) return;
machine_load_rom_bank( memory_map_romcs, 0, 0,
settings_current.if2_file,
NULL,
2 * MEMORY_PAGE_SIZE );
memory_map_romcs[0].source =
memory_map_romcs[1].source = MEMORY_SOURCE_CARTRIDGE;
machine_current->ram.romcs = 1;
if2_active = 1;
memory_romcs_map();
ui_menu_activate( UI_MENU_ITEM_MEDIA_CARTRIDGE_IF2_EJECT, 1 );
}
static void
if2_memory_map( void )
{
if( !if2_active ) return;
memory_map_read[0] = memory_map_write[0] = memory_map_romcs[0];
memory_map_read[1] = memory_map_write[1] = memory_map_romcs[1];
}
static void
if2_from_snapshot( libspectrum_snap *snap )
{
if( !libspectrum_snap_interface2_active( snap ) ) return;
if2_active = 1;
machine_current->ram.romcs = 1;
if( libspectrum_snap_interface2_rom( snap, 0 ) ) {
memory_map_romcs[0].offset = 0;
memory_map_romcs[0].page_num = 0;
memory_map_romcs[0].page =
memory_pool_allocate( 2 * MEMORY_PAGE_SIZE *
sizeof( libspectrum_byte ) );
if( !memory_map_romcs[0].page ) {
ui_error( UI_ERROR_ERROR, "Out of memory at %s:%d", __FILE__, __LINE__ );
return;
}
memcpy( memory_map_romcs[0].page,
libspectrum_snap_interface2_rom( snap, 0 ), 2 * MEMORY_PAGE_SIZE );
memory_map_romcs[1].offset = MEMORY_PAGE_SIZE;
memory_map_romcs[1].page_num = 0;
memory_map_romcs[1].page = memory_map_romcs[0].page + MEMORY_PAGE_SIZE;
memory_map_romcs[1].source =
memory_map_romcs[0].source = MEMORY_SOURCE_CARTRIDGE;
}
ui_menu_activate( UI_MENU_ITEM_MEDIA_CARTRIDGE_IF2_EJECT, 1 );
machine_current->memory_map();
}
static void
if2_to_snapshot( libspectrum_snap *snap )
{
libspectrum_byte *buffer;
if( !if2_active ) return;
libspectrum_snap_set_interface2_active( snap, 1 );
buffer = malloc( 0x4000 * sizeof( libspectrum_byte ) );
if( !buffer ) {
ui_error( UI_ERROR_ERROR, "Out of memory at %s:%d", __FILE__, __LINE__ );
return;
}
memcpy( buffer, memory_map_romcs[0].page, MEMORY_PAGE_SIZE );
memcpy( buffer + MEMORY_PAGE_SIZE, memory_map_romcs[1].page,
MEMORY_PAGE_SIZE );
libspectrum_snap_set_interface2_rom( snap, 0, buffer );
}