-
Notifications
You must be signed in to change notification settings - Fork 1
/
dck.c
184 lines (142 loc) · 4.75 KB
/
dck.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
/* dck.c: dock snapshot (Warajevo .DCK) handling routines
Copyright (c) 2003-2004 Darren Salt, Fredrick Meunier, Philip Kendall
$Id: dck.c 3703 2008-06-30 20:36:11Z pak21 $
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:
Philip: [email protected]
Darren: [email protected]
Fred: [email protected]
*/
#include <config.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <libspectrum.h>
#include "dck.h"
#include "machine.h"
#include "memory.h"
#include "settings.h"
#include "scld.h"
#include "ui/ui.h"
#include "utils.h"
#include "debugger/debugger.h"
/* Dock cart inserted? */
int dck_active = 0;
int
dck_insert( const char *filename )
{
int error;
if ( !( libspectrum_machine_capabilities( machine_current->machine ) &
LIBSPECTRUM_MACHINE_CAPABILITY_TIMEX_DOCK ) ) {
ui_error( UI_ERROR_ERROR, "This machine does not support the dock" );
return 1;
}
error = settings_set_string( &settings_current.dck_file, filename );
if( error ) return error;
machine_reset( 0 );
return 0;
}
void
dck_eject( void )
{
if ( !( libspectrum_machine_capabilities( machine_current->machine ) &
LIBSPECTRUM_MACHINE_CAPABILITY_TIMEX_DOCK ) ) {
ui_error( UI_ERROR_ERROR, "This machine does not support the dock" );
return;
}
if( settings_current.dck_file ) free( settings_current.dck_file );
settings_current.dck_file = NULL;
dck_active = 0;
ui_menu_activate( UI_MENU_ITEM_MEDIA_CARTRIDGE_DOCK_EJECT, 0 );
machine_reset( 0 );
}
int
dck_reset( void )
{
utils_file file;
size_t num_block = 0;
libspectrum_dck *dck;
int error;
dck_active = 0;
if( !settings_current.dck_file ) {
ui_menu_activate( UI_MENU_ITEM_MEDIA_CARTRIDGE_DOCK_EJECT, 0 );
return 0;
}
dck = libspectrum_dck_alloc();
error = utils_read_file( settings_current.dck_file, &file );
if( error ) { libspectrum_dck_free( dck, 0 ); return error; }
error = libspectrum_dck_read2( dck, file.buffer, file.length,
settings_current.dck_file );
if( error ) {
utils_close_file( &file ); libspectrum_dck_free( dck, 0 ); return error;
}
if( utils_close_file( &file ) ) {
libspectrum_dck_free( dck, 0 );
return 1;
}
while( dck->dck[num_block] != NULL ) {
memory_page **mem;
int i;
switch( dck->dck[num_block]->bank ) {
case LIBSPECTRUM_DCK_BANK_HOME:
mem = memory_map_home;
break;
case LIBSPECTRUM_DCK_BANK_DOCK:
mem = memory_map_dock;
break;
case LIBSPECTRUM_DCK_BANK_EXROM:
mem = memory_map_exrom;
break;
default:
ui_error( UI_ERROR_INFO, "Sorry, bank ID %i is unsupported",
dck->dck[num_block]->bank );
libspectrum_dck_free( dck, 0 );
return 1;
}
for( i = 0; i < 8; i++ ) {
switch( dck->dck[num_block]->access[i] ) {
case LIBSPECTRUM_DCK_PAGE_NULL:
break;
case LIBSPECTRUM_DCK_PAGE_ROM:
mem[i]->page = memory_pool_allocate( MEMORY_PAGE_SIZE );
if( !mem[i]->page ) return 1;
memcpy( mem[i]->page, dck->dck[num_block]->pages[i],
MEMORY_PAGE_SIZE );
mem[i]->writable = 0;
mem[i]->source = MEMORY_SOURCE_CARTRIDGE;
break;
case LIBSPECTRUM_DCK_PAGE_RAM_EMPTY:
case LIBSPECTRUM_DCK_PAGE_RAM:
/* Because the scr and snapshot code depends on the standard
memory map being in the RAM[] array, we just copy RAM
blocks from the HOME bank into the appropriate page; in
other cases, we allocate ourselves a new page to store the
contents in */
if( !(dck->dck[num_block]->bank == LIBSPECTRUM_DCK_BANK_HOME && i>1) ) {
mem[i]->page = memory_pool_allocate( MEMORY_PAGE_SIZE );
if( !mem[i]->page ) return 1;
mem[i]->writable = 1;
}
mem[i]->source = MEMORY_SOURCE_CARTRIDGE;
memcpy( mem[i]->page, dck->dck[num_block]->pages[i],
MEMORY_PAGE_SIZE );
break;
}
}
num_block++;
}
dck_active = 1;
/* Make the menu item to eject the cartridge active */
ui_menu_activate( UI_MENU_ITEM_MEDIA_CARTRIDGE_DOCK_EJECT, 1 );
return libspectrum_dck_free( dck, 0 );
}