-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdivide.c
445 lines (351 loc) · 11.6 KB
/
divide.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/* divide.c: DivIDE interface routines
Copyright (c) 2005-2008 Matthew Westcott, Philip Kendall
$Id: divide.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:
E-mail: Philip Kendall <[email protected]>
*/
#include <config.h>
#include <libspectrum.h>
#include <string.h>
#include "debugger/debugger.h"
#include "ide.h"
#include "machine.h"
#include "module.h"
#include "periph.h"
#include "settings.h"
#include "ui/ui.h"
#include "divide.h"
/* Private function prototypes */
static libspectrum_byte divide_ide_read( libspectrum_word port, int *attached );
static void divide_ide_write( libspectrum_word port, libspectrum_byte data );
static void divide_control_write( libspectrum_word port, libspectrum_byte data );
static void divide_control_write_internal( libspectrum_byte data );
static void divide_page( void );
static void divide_unpage( void );
static libspectrum_ide_register port_to_ide_register( libspectrum_byte port );
/* Data */
const periph_t divide_peripherals[] = {
{ 0x00e3, 0x00a3, divide_ide_read, divide_ide_write },
{ 0x00ff, 0x00e3, NULL, divide_control_write },
};
const size_t divide_peripherals_count =
sizeof( divide_peripherals ) / sizeof( periph_t );
static const libspectrum_byte DIVIDE_CONTROL_CONMEM = 0x80;
static const libspectrum_byte DIVIDE_CONTROL_MAPRAM = 0x40;
int divide_automapping_enabled = 0;
int divide_active = 0;
static libspectrum_byte divide_control;
/* divide_automap tracks opcode fetches to entry and exit points to determine
whether DivIDE memory *would* be paged in at this moment if mapram / wp
flags allowed it */
static int divide_automap = 0;
static libspectrum_ide_channel *divide_idechn0;
static libspectrum_ide_channel *divide_idechn1;
#define DIVIDE_PAGES 4
#define DIVIDE_PAGE_LENGTH 0x2000
static libspectrum_byte divide_ram[ DIVIDE_PAGES ][ DIVIDE_PAGE_LENGTH ];
static libspectrum_byte divide_eprom[ DIVIDE_PAGE_LENGTH ];
static void divide_reset( int hard_reset );
static void divide_memory_map( void );
static void divide_enabled_snapshot( libspectrum_snap *snap );
static void divide_from_snapshot( libspectrum_snap *snap );
static void divide_to_snapshot( libspectrum_snap *snap );
static module_info_t divide_module_info = {
divide_reset,
divide_memory_map,
divide_enabled_snapshot,
divide_from_snapshot,
divide_to_snapshot,
};
/* Debugger events */
static const char *event_type_string = "divide";
static int page_event, unpage_event;
/* Housekeeping functions */
int
divide_init( void )
{
int error;
divide_idechn0 = libspectrum_ide_alloc( LIBSPECTRUM_IDE_DATA16 );
divide_idechn1 = libspectrum_ide_alloc( LIBSPECTRUM_IDE_DATA16 );
ui_menu_activate( UI_MENU_ITEM_MEDIA_IDE_DIVIDE_MASTER_EJECT, 0 );
ui_menu_activate( UI_MENU_ITEM_MEDIA_IDE_DIVIDE_SLAVE_EJECT, 0 );
if( settings_current.divide_master_file ) {
error = libspectrum_ide_insert( divide_idechn0, LIBSPECTRUM_IDE_MASTER,
settings_current.divide_master_file );
if( error ) return error;
ui_menu_activate( UI_MENU_ITEM_MEDIA_IDE_DIVIDE_MASTER_EJECT, 1 );
}
if( settings_current.divide_slave_file ) {
error = libspectrum_ide_insert( divide_idechn0, LIBSPECTRUM_IDE_SLAVE,
settings_current.divide_slave_file );
if( error ) return error;
ui_menu_activate( UI_MENU_ITEM_MEDIA_IDE_DIVIDE_SLAVE_EJECT, 1 );
}
module_register( ÷_module_info );
if( periph_register_paging_events( event_type_string, &page_event,
&unpage_event ) )
return 1;
return 0;
}
int
divide_end( void )
{
int error;
error = libspectrum_ide_free( divide_idechn0 );
error = libspectrum_ide_free( divide_idechn1 ) || error;
return error;
}
/* DivIDE does not page in immediately on a reset condition (we do that by
trapping PC instead); however, it needs to perform housekeeping tasks upon
reset */
static void
divide_reset( int hard_reset )
{
divide_active = 0;
if( !settings_current.divide_enabled ) return;
if( hard_reset ) {
divide_control = 0;
} else {
divide_control &= DIVIDE_CONTROL_MAPRAM;
}
divide_automap = 0;
divide_refresh_page_state();
libspectrum_ide_reset( divide_idechn0 );
libspectrum_ide_reset( divide_idechn1 );
}
int
divide_insert( const char *filename, libspectrum_ide_unit unit )
{
char **setting;
ui_menu_item item;
switch( unit ) {
case LIBSPECTRUM_IDE_MASTER:
setting = &settings_current.divide_master_file;
item = UI_MENU_ITEM_MEDIA_IDE_DIVIDE_MASTER_EJECT;
break;
case LIBSPECTRUM_IDE_SLAVE:
setting = &settings_current.divide_slave_file;
item = UI_MENU_ITEM_MEDIA_IDE_DIVIDE_SLAVE_EJECT;
break;
default: return 1;
}
return ide_insert( filename, divide_idechn0, unit, divide_commit, setting,
item );
}
int
divide_commit( libspectrum_ide_unit unit )
{
int error;
error = libspectrum_ide_commit( divide_idechn0, unit );
return error;
}
int
divide_eject( libspectrum_ide_unit unit )
{
char **setting;
ui_menu_item item;
switch( unit ) {
case LIBSPECTRUM_IDE_MASTER:
setting = &settings_current.divide_master_file;
item = UI_MENU_ITEM_MEDIA_IDE_DIVIDE_MASTER_EJECT;
break;
case LIBSPECTRUM_IDE_SLAVE:
setting = &settings_current.divide_slave_file;
item = UI_MENU_ITEM_MEDIA_IDE_DIVIDE_SLAVE_EJECT;
break;
default: return 1;
}
return ide_eject( divide_idechn0, unit, divide_commit, setting, item );
}
/* Port read/writes */
static libspectrum_ide_register
port_to_ide_register( libspectrum_byte port )
{
switch( port & 0xff ) {
case 0xa3:
return LIBSPECTRUM_IDE_REGISTER_DATA;
case 0xa7:
return LIBSPECTRUM_IDE_REGISTER_ERROR_FEATURE;
case 0xab:
return LIBSPECTRUM_IDE_REGISTER_SECTOR_COUNT;
case 0xaf:
return LIBSPECTRUM_IDE_REGISTER_SECTOR;
case 0xb3:
return LIBSPECTRUM_IDE_REGISTER_CYLINDER_LOW;
case 0xb7:
return LIBSPECTRUM_IDE_REGISTER_CYLINDER_HIGH;
case 0xbb:
return LIBSPECTRUM_IDE_REGISTER_HEAD_DRIVE;
default: /* 0xbf */
return LIBSPECTRUM_IDE_REGISTER_COMMAND_STATUS;
}
}
libspectrum_byte
divide_ide_read( libspectrum_word port, int *attached )
{
int ide_register;
if( !settings_current.divide_enabled ) return 0xff;
*attached = 1;
ide_register = port_to_ide_register( port );
return libspectrum_ide_read( divide_idechn0, ide_register );
}
static void
divide_ide_write( libspectrum_word port, libspectrum_byte data )
{
int ide_register;
if( !settings_current.divide_enabled ) return;
ide_register = port_to_ide_register( port );
libspectrum_ide_write( divide_idechn0, ide_register, data );
}
static void
divide_control_write( libspectrum_word port GCC_UNUSED, libspectrum_byte data )
{
int old_mapram;
if( !settings_current.divide_enabled ) return;
/* MAPRAM bit cannot be reset, only set */
old_mapram = divide_control & DIVIDE_CONTROL_MAPRAM;
divide_control_write_internal( data | old_mapram );
}
static void
divide_control_write_internal( libspectrum_byte data )
{
divide_control = data;
divide_refresh_page_state();
}
void
divide_set_automap( int state )
{
divide_automap = state;
divide_refresh_page_state();
}
void
divide_refresh_page_state( void )
{
if( divide_control & DIVIDE_CONTROL_CONMEM ) {
/* always paged in if conmem enabled */
divide_page();
} else if( settings_current.divide_wp
|| ( divide_control & DIVIDE_CONTROL_MAPRAM ) ) {
/* automap in effect */
if( divide_automap ) {
divide_page();
} else {
divide_unpage();
}
} else {
divide_unpage();
}
}
static void
divide_page( void )
{
divide_active = 1;
machine_current->ram.romcs = 1;
machine_current->memory_map();
debugger_event( page_event );
}
static void
divide_unpage( void )
{
divide_active = 0;
machine_current->ram.romcs = 0;
machine_current->memory_map();
debugger_event( unpage_event );
}
void
divide_memory_map( void )
{
int upper_ram_page;
if( !divide_active ) return;
/* low bits of divide_control register give page number to use in upper
bank; only lowest two bits on original 32K model */
upper_ram_page = divide_control & (DIVIDE_PAGES - 1);
if( divide_control & DIVIDE_CONTROL_CONMEM ) {
memory_map_romcs[0].page = divide_eprom;
memory_map_romcs[0].writable = !settings_current.divide_wp;
memory_map_romcs[1].page = divide_ram[ upper_ram_page ];
memory_map_romcs[1].writable = 1;
} else {
if( divide_control & DIVIDE_CONTROL_MAPRAM ) {
memory_map_romcs[0].page = divide_ram[3];
memory_map_romcs[0].writable = 0;
memory_map_romcs[1].page = divide_ram[ upper_ram_page ];
memory_map_romcs[1].writable = ( upper_ram_page != 3 );
} else {
memory_map_romcs[0].page = divide_eprom;
memory_map_romcs[0].writable = 0;
memory_map_romcs[1].page = divide_ram[ upper_ram_page ];
memory_map_romcs[1].writable = 1;
}
}
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
divide_enabled_snapshot( libspectrum_snap *snap )
{
if( libspectrum_snap_divide_active( snap ) )
settings_current.divide_enabled = 1;
}
static void
divide_from_snapshot( libspectrum_snap *snap )
{
size_t i;
if( !libspectrum_snap_divide_active( snap ) ) return;
settings_current.divide_wp =
libspectrum_snap_divide_eprom_writeprotect( snap );
divide_control_write_internal( libspectrum_snap_divide_control( snap ) );
if( libspectrum_snap_divide_eprom( snap, 0 ) ) {
memcpy( divide_eprom,
libspectrum_snap_divide_eprom( snap, 0 ), DIVIDE_PAGE_LENGTH );
}
for( i = 0; i < libspectrum_snap_divide_pages( snap ); i++ )
if( libspectrum_snap_divide_ram( snap, i ) )
memcpy( divide_ram[ i ], libspectrum_snap_divide_ram( snap, i ),
DIVIDE_PAGE_LENGTH );
if( libspectrum_snap_divide_paged( snap ) ) {
divide_page();
} else {
divide_unpage();
}
}
static void
divide_to_snapshot( libspectrum_snap *snap )
{
size_t i;
libspectrum_byte *buffer;
if( !settings_current.divide_enabled ) return;
libspectrum_snap_set_divide_active( snap, 1 );
libspectrum_snap_set_divide_eprom_writeprotect( snap,
settings_current.divide_wp );
libspectrum_snap_set_divide_paged( snap, divide_active );
libspectrum_snap_set_divide_control( snap, divide_control );
buffer = malloc( DIVIDE_PAGE_LENGTH * sizeof( libspectrum_byte ) );
if( !buffer ) {
ui_error( UI_ERROR_ERROR, "Out of memory at %s:%d", __FILE__, __LINE__ );
return;
}
memcpy( buffer, divide_eprom, DIVIDE_PAGE_LENGTH );
libspectrum_snap_set_divide_eprom( snap, 0, buffer );
libspectrum_snap_set_divide_pages( snap, DIVIDE_PAGES );
for( i = 0; i < DIVIDE_PAGES; i++ ) {
buffer = malloc( DIVIDE_PAGE_LENGTH * sizeof( libspectrum_byte ) );
if( !buffer ) {
ui_error( UI_ERROR_ERROR, "Out of memory at %s:%d", __FILE__, __LINE__ );
return;
}
memcpy( buffer, divide_ram[ i ], DIVIDE_PAGE_LENGTH );
libspectrum_snap_set_divide_ram( snap, i, buffer );
}
}