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 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
|
/* dck.c: dock snapshot (Warajevo .DCK) handling routines
Copyright (c) 2003-2004 Darren Salt, Fredrick Meunier, Philip Kendall
$Id: dck.c 4724 2012-07-08 13:38:21Z 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:
Philip: philip-fuse@shadowmagic.org.uk
Darren: linux@youmustbejoking.demon.co.uk
Fred: fredm@spamcop.net
*/
#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 )
{
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;
}
settings_set_string( &settings_current.dck_file, filename );
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 );
}
static memory_page *
dck_get_memory_page( libspectrum_dck_bank bank, size_t index )
{
switch( bank ) {
case LIBSPECTRUM_DCK_BANK_HOME:
return timex_home[ index ];
break;
case LIBSPECTRUM_DCK_BANK_DOCK:
return &timex_dock[ index ];
break;
case LIBSPECTRUM_DCK_BANK_EXROM:
return &timex_exrom[ index ];
break;
default:
return NULL;
}
}
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;
}
utils_close_file( &file );
while( dck->dck[num_block] != NULL ) {
memory_page *page;
int i;
libspectrum_dck_bank dck_bank = dck->dck[num_block]->bank;
if( dck_bank != LIBSPECTRUM_DCK_BANK_HOME &&
dck_bank != LIBSPECTRUM_DCK_BANK_DOCK &&
dck_bank != LIBSPECTRUM_DCK_BANK_EXROM ) {
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++ ) {
libspectrum_byte *data;
int j;
switch( dck->dck[num_block]->access[i] ) {
case LIBSPECTRUM_DCK_PAGE_NULL:
break;
case LIBSPECTRUM_DCK_PAGE_ROM:
data = memory_pool_allocate( 0x2000 );
memcpy( data, dck->dck[num_block]->pages[i], 0x2000 );
for( j = 0; j < MEMORY_PAGES_IN_8K; j++ ) {
page = dck_get_memory_page( dck_bank, i * MEMORY_PAGES_IN_8K + j);
page->offset = j * MEMORY_PAGE_SIZE;
page->writable = 0;
page->save_to_snapshot = 1;
page->page = data + page->offset;
}
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_bank == LIBSPECTRUM_DCK_BANK_HOME && i>1 ) {
for( j = 0; j < MEMORY_PAGES_IN_8K; j++ ) {
page = dck_get_memory_page( dck_bank, i * MEMORY_PAGES_IN_8K + j);
if( dck->dck[num_block]->access[i] == LIBSPECTRUM_DCK_PAGE_RAM ) {
memcpy( page->page,
dck->dck[num_block]->pages[i] + j * MEMORY_PAGE_SIZE,
MEMORY_PAGE_SIZE );
} else {
memset( page->page, 0, MEMORY_PAGE_SIZE );
}
}
} else {
data = memory_pool_allocate( 0x2000 );
if( dck->dck[num_block]->access[i] == LIBSPECTRUM_DCK_PAGE_RAM ) {
memcpy( data, dck->dck[num_block]->pages[i], 0x2000 );
} else {
memset( data, 0, 0x2000 );
}
for( j = 0; j < MEMORY_PAGES_IN_8K; j++ ) {
page = dck_get_memory_page( dck_bank, i * MEMORY_PAGES_IN_8K + j);
page->offset = j * MEMORY_PAGE_SIZE;
page->writable = 1;
page->save_to_snapshot = 1;
page->page = data + page->offset;
}
}
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 );
}
|