-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathzxatasp.c
581 lines (446 loc) · 16.4 KB
/
zxatasp.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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
/* zxatasp.c: ZXATASP interface routines
Copyright (c) 2003-2008 Garry Lancaster and Philip Kendall
$Id: zxatasp.c 3707 2008-06-30 21:33:49Z 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: Philip Kendall <[email protected]>
*/
#include <config.h>
#include <string.h>
#include <libspectrum.h>
#include "debugger/debugger.h"
#include "ide.h"
#include "machine.h"
#include "memory.h"
#include "module.h"
#include "periph.h"
#include "settings.h"
#include "ui/ui.h"
#include "zxatasp.h"
/*
TBD: Allow memory size selection (128K/512K)
TBD: Should support for secondary channel be removed?
No software ever supported it, and v2.0+ boards don't have it.
*/
/* Debugger events */
static const char *event_type_string = "zxatasp";
static int page_event, unpage_event;
/* Private function prototypes */
static libspectrum_byte zxatasp_portA_read( libspectrum_word port,
int *attached );
static void zxatasp_portA_write( libspectrum_word port,
libspectrum_byte data );
static libspectrum_byte zxatasp_portB_read( libspectrum_word port,
int *attached );
static void zxatasp_portB_write( libspectrum_word port,
libspectrum_byte data );
static libspectrum_byte zxatasp_portC_read( libspectrum_word port,
int *attached );
static void zxatasp_portC_write( libspectrum_word port,
libspectrum_byte data );
static libspectrum_byte zxatasp_control_read( libspectrum_word port,
int *attached );
static void zxatasp_control_write( libspectrum_word port,
libspectrum_byte data );
static void zxatasp_resetports( void );
static void set_zxatasp_bank( int bank );
static void zxatasp_readide( libspectrum_ide_channel *chn,
libspectrum_ide_register idereg );
static void zxatasp_writeide( libspectrum_ide_channel *chn,
libspectrum_ide_register idereg );
/* Data */
const periph_t zxatasp_peripherals[] = {
{ 0x039f, 0x009f, zxatasp_portA_read, zxatasp_portA_write },
{ 0x039f, 0x019f, zxatasp_portB_read, zxatasp_portB_write },
{ 0x039f, 0x029f, zxatasp_portC_read, zxatasp_portC_write },
{ 0x039f, 0x039f, zxatasp_control_read, zxatasp_control_write },
};
const size_t zxatasp_peripherals_count =
sizeof( zxatasp_peripherals ) / sizeof( periph_t );
static libspectrum_byte zxatasp_control;
static libspectrum_byte zxatasp_portA;
static libspectrum_byte zxatasp_portB;
static libspectrum_byte zxatasp_portC;
static size_t current_page;
static libspectrum_ide_channel *zxatasp_idechn0;
static libspectrum_ide_channel *zxatasp_idechn1;
#define ZXATASP_PAGES 32
#define ZXATASP_PAGE_LENGTH 0x4000
static libspectrum_byte ZXATASPMEM[ ZXATASP_PAGES ][ ZXATASP_PAGE_LENGTH ];
static const size_t ZXATASP_NOT_PAGED = 0xff;
/* We're ignoring all mode bits and only emulating mode 0, basic I/O */
static const libspectrum_byte MC8255_PORT_C_LOW_IO = 0x01;
static const libspectrum_byte MC8255_PORT_B_IO = 0x02;
static const libspectrum_byte MC8255_PORT_C_HI_IO = 0x08;
static const libspectrum_byte MC8255_PORT_A_IO = 0x10;
static const libspectrum_byte MC8255_SETMODE = 0x80;
static const libspectrum_byte ZXATASP_IDE_REG = 0x07;
static const libspectrum_byte ZXATASP_RAM_BANK = 0x1f;
static const libspectrum_byte ZXATASP_IDE_WR = 0x08;
static const libspectrum_byte ZXATASP_IDE_RD = 0x10;
static const libspectrum_byte ZXATASP_IDE_PRIMARY = 0x20;
static const libspectrum_byte ZXATASP_RAM_LATCH = 0x40;
static const libspectrum_byte ZXATASP_RAM_DISABLE = 0x80;
static const libspectrum_byte ZXATASP_IDE_SECONDARY = 0x80;
#define ZXATASP_READ_PRIMARY( x ) \
( ( x & ( ZXATASP_IDE_PRIMARY | ZXATASP_RAM_LATCH | \
ZXATASP_IDE_RD | ZXATASP_IDE_WR ) ) == \
( ZXATASP_IDE_PRIMARY | ZXATASP_IDE_RD ) )
#define ZXATASP_WRITE_PRIMARY( x ) \
( ( x & ( ZXATASP_IDE_PRIMARY | ZXATASP_RAM_LATCH | \
ZXATASP_IDE_RD | ZXATASP_IDE_WR ) ) == \
( ZXATASP_IDE_PRIMARY | ZXATASP_IDE_WR ) )
#define ZXATASP_READ_SECONDARY( x ) \
( ( x & ( ZXATASP_IDE_SECONDARY | ZXATASP_RAM_LATCH | \
ZXATASP_IDE_RD | ZXATASP_IDE_WR ) ) == \
( ZXATASP_IDE_SECONDARY | ZXATASP_IDE_RD ) )
#define ZXATASP_WRITE_SECONDARY( x ) \
( ( x & ( ZXATASP_IDE_SECONDARY | ZXATASP_RAM_LATCH | \
ZXATASP_IDE_RD | ZXATASP_IDE_WR ) ) == \
( ZXATASP_IDE_SECONDARY | ZXATASP_IDE_WR ) )
static void zxatasp_reset( int hard_reset );
static void zxatasp_memory_map( void );
static void zxatasp_from_snapshot( libspectrum_snap *snap );
static void zxatasp_to_snapshot( libspectrum_snap *snap );
static module_info_t zxatasp_module_info = {
zxatasp_reset,
zxatasp_memory_map,
NULL,
zxatasp_from_snapshot,
zxatasp_to_snapshot,
};
/* Housekeeping functions */
int
zxatasp_init( void )
{
int error = 0;
zxatasp_idechn0 = libspectrum_ide_alloc( LIBSPECTRUM_IDE_DATA16 );
zxatasp_idechn1 = libspectrum_ide_alloc( LIBSPECTRUM_IDE_DATA16 );
ui_menu_activate( UI_MENU_ITEM_MEDIA_IDE_ZXATASP_MASTER_EJECT, 0 );
ui_menu_activate( UI_MENU_ITEM_MEDIA_IDE_ZXATASP_SLAVE_EJECT, 0 );
if( settings_current.zxatasp_master_file ) {
error = libspectrum_ide_insert( zxatasp_idechn0, LIBSPECTRUM_IDE_MASTER,
settings_current.zxatasp_master_file );
if( error ) return error;
ui_menu_activate( UI_MENU_ITEM_MEDIA_IDE_ZXATASP_MASTER_EJECT, 1 );
}
if( settings_current.zxatasp_slave_file ) {
error = libspectrum_ide_insert( zxatasp_idechn0, LIBSPECTRUM_IDE_SLAVE,
settings_current.zxatasp_slave_file );
if( error ) return error;
ui_menu_activate( UI_MENU_ITEM_MEDIA_IDE_ZXATASP_SLAVE_EJECT, 1 );
}
module_register( &zxatasp_module_info );
if( periph_register_paging_events( event_type_string, &page_event,
&unpage_event ) )
return 1;
return error;
}
int
zxatasp_end( void )
{
int error;
error = libspectrum_ide_free( zxatasp_idechn0 );
error = libspectrum_ide_free( zxatasp_idechn1 ) || error;
return error;
}
static void
zxatasp_reset( int hard_reset GCC_UNUSED )
{
if( !settings_current.zxatasp_active ) return;
machine_current->ram.romcs = 1;
set_zxatasp_bank( 0 ); current_page = 0;
machine_current->memory_map();
zxatasp_control = MC8255_SETMODE | MC8255_PORT_A_IO | MC8255_PORT_B_IO |
MC8255_PORT_C_HI_IO | MC8255_PORT_C_LOW_IO;
zxatasp_resetports();
libspectrum_ide_reset( zxatasp_idechn0 );
libspectrum_ide_reset( zxatasp_idechn1 );
}
int
zxatasp_insert( const char *filename, libspectrum_ide_unit unit )
{
char **setting;
ui_menu_item item;
switch( unit ) {
case LIBSPECTRUM_IDE_MASTER:
setting = &settings_current.zxatasp_master_file;
item = UI_MENU_ITEM_MEDIA_IDE_ZXATASP_MASTER_EJECT;
break;
case LIBSPECTRUM_IDE_SLAVE:
setting = &settings_current.zxatasp_slave_file;
item = UI_MENU_ITEM_MEDIA_IDE_ZXATASP_SLAVE_EJECT;
break;
default: return 1;
}
return ide_insert( filename, zxatasp_idechn0, unit, zxatasp_commit, setting,
item );
}
int
zxatasp_commit( libspectrum_ide_unit unit )
{
int error;
error = libspectrum_ide_commit( zxatasp_idechn0, unit );
return error;
}
int
zxatasp_eject( libspectrum_ide_unit unit )
{
char **setting;
ui_menu_item item;
switch( unit ) {
case LIBSPECTRUM_IDE_MASTER:
setting = &settings_current.zxatasp_master_file;
item = UI_MENU_ITEM_MEDIA_IDE_ZXATASP_MASTER_EJECT;
break;
case LIBSPECTRUM_IDE_SLAVE:
setting = &settings_current.zxatasp_slave_file;
item = UI_MENU_ITEM_MEDIA_IDE_ZXATASP_SLAVE_EJECT;
break;
default: return 1;
}
return ide_eject( zxatasp_idechn0, unit, zxatasp_commit, setting, item );
}
/* Port read/writes */
libspectrum_byte
zxatasp_portA_read( libspectrum_word port GCC_UNUSED, int *attached )
{
if( !settings_current.zxatasp_active ) return 0xff;
*attached = 1;
return zxatasp_portA;
}
static void
zxatasp_portA_write( libspectrum_word port GCC_UNUSED, libspectrum_byte data )
{
if( !settings_current.zxatasp_active ) return;
if( !( zxatasp_control & MC8255_PORT_A_IO ) ) zxatasp_portA = data;
}
libspectrum_byte
zxatasp_portB_read( libspectrum_word port GCC_UNUSED, int *attached )
{
if( !settings_current.zxatasp_active ) return 0xff;
*attached = 1;
return zxatasp_portB;
}
static void
zxatasp_portB_write( libspectrum_word port GCC_UNUSED, libspectrum_byte data )
{
if( !settings_current.zxatasp_active ) return;
if( !( zxatasp_control & MC8255_PORT_B_IO ) ) zxatasp_portB = data;
}
libspectrum_byte
zxatasp_portC_read( libspectrum_word port GCC_UNUSED, int *attached )
{
if( !settings_current.zxatasp_active ) return 0xff;
*attached = 1;
return zxatasp_portC;
}
static void
zxatasp_portC_write( libspectrum_word port GCC_UNUSED, libspectrum_byte data )
{
libspectrum_byte oldC = zxatasp_portC;
libspectrum_byte newC;
if( !settings_current.zxatasp_active ) return;
/* Determine new port C value, dependent on I/O modes */
newC = ( zxatasp_control & MC8255_PORT_C_LOW_IO )
? ( oldC & 0x0f ) : ( data & 0x0f );
newC |= ( zxatasp_control & MC8255_PORT_C_HI_IO )
? ( oldC & 0xf0 ) : ( data & 0xf0 );
/* Set the new port value */
zxatasp_portC = newC;
/* No action can occur if high part of port C is in input mode */
if( zxatasp_control & MC8255_PORT_C_HI_IO ) return;
/* Check for any I/O action */
if( ( ZXATASP_READ_PRIMARY( newC ) ) &
!( ZXATASP_READ_PRIMARY( oldC ) ) ) {
zxatasp_readide( zxatasp_idechn0, ( newC & ZXATASP_IDE_REG ) );
return;
}
if( ( ZXATASP_READ_SECONDARY( newC ) ) &
!( ZXATASP_READ_SECONDARY( oldC ) ) ) {
zxatasp_readide( zxatasp_idechn1, ( newC & ZXATASP_IDE_REG ) );
return;
}
if( ( ZXATASP_WRITE_PRIMARY( newC ) ) &
!( ZXATASP_WRITE_PRIMARY( oldC ) ) ) {
zxatasp_writeide( zxatasp_idechn0, ( newC & ZXATASP_IDE_REG ) );
return;
}
if( ( ZXATASP_WRITE_SECONDARY( newC ) ) &
!( ZXATASP_WRITE_SECONDARY( oldC ) ) ) {
zxatasp_writeide( zxatasp_idechn1, ( newC & ZXATASP_IDE_REG ) );
return;
}
if( newC & ZXATASP_RAM_LATCH ) {
int was_paged = machine_current->ram.romcs;
set_zxatasp_bank( newC & ZXATASP_RAM_BANK );
if( newC & ZXATASP_RAM_DISABLE ) {
machine_current->ram.romcs = 0;
current_page = ZXATASP_NOT_PAGED;
if( was_paged ) debugger_event( unpage_event );
} else {
machine_current->ram.romcs = 1;
current_page = newC & ZXATASP_RAM_BANK;
if( !was_paged ) debugger_event( page_event );
}
machine_current->memory_map();
return;
}
}
libspectrum_byte
zxatasp_control_read( libspectrum_word port GCC_UNUSED, int *attached )
{
if( !settings_current.zxatasp_active ) return 0xff;
*attached = 1;
return zxatasp_control;
}
static void
zxatasp_control_write( libspectrum_word port GCC_UNUSED, libspectrum_byte data )
{
if( !settings_current.zxatasp_active ) return;
if( data & MC8255_SETMODE ) {
/* Set the control word and reset the ports */
zxatasp_control = data;
zxatasp_resetports();
} else {
/* Set or reset a bit of port C */
libspectrum_byte bit = (data >> 1) & 7;
libspectrum_byte newC = zxatasp_portC;
if( data & 1 ) {
newC |= ( 1 << bit );
} else {
newC &= ~( 1 << bit );
}
zxatasp_portC_write( 0, newC );
}
}
static void
zxatasp_resetports( void )
{
/* In input mode, ports will return 0xff unless IDE is active,
which it won't be just after a reset. Output ports are always
reset to zero.
*/
zxatasp_portA = (zxatasp_control & MC8255_PORT_A_IO) ? 0xff : 0x00;
zxatasp_portB = (zxatasp_control & MC8255_PORT_B_IO) ? 0xff : 0x00;
zxatasp_portC = (zxatasp_control & MC8255_PORT_C_LOW_IO) ? 0x0f : 0x00;
zxatasp_portC |= (zxatasp_control & MC8255_PORT_C_HI_IO) ? 0xf0 : 0x00;
}
static void
set_zxatasp_bank( int bank )
{
memory_page *page;
size_t i, offset;
for( i = 0; i < 2; i++ ) {
page = &memory_map_romcs[i];
offset = i & 1 ? MEMORY_PAGE_SIZE : 0x0000;
page->page = &ZXATASPMEM[ bank ][ offset ];
page->writable = !settings_current.zxatasp_wp;
page->contended = 0;
page->page_num = bank;
page->offset = offset;
}
}
/* IDE access */
static void
zxatasp_readide(libspectrum_ide_channel *chn,
libspectrum_ide_register idereg)
{
libspectrum_byte dataHi, dataLo;
dataLo = libspectrum_ide_read( chn, idereg );
if( idereg == LIBSPECTRUM_IDE_REGISTER_DATA ) {
dataHi = libspectrum_ide_read( chn, idereg );
} else {
dataHi = 0xff;
}
if( zxatasp_control & MC8255_PORT_A_IO ) zxatasp_portA = dataLo;
if( zxatasp_control & MC8255_PORT_B_IO ) zxatasp_portB = dataHi;
}
static void
zxatasp_writeide(libspectrum_ide_channel *chn,
libspectrum_ide_register idereg)
{
libspectrum_byte dataHi, dataLo;
dataLo = ( zxatasp_control & MC8255_PORT_A_IO ) ? 0xff : zxatasp_portA;
dataHi = ( zxatasp_control & MC8255_PORT_B_IO ) ? 0xff : zxatasp_portB;
libspectrum_ide_write( chn, idereg, dataLo );
if( idereg == LIBSPECTRUM_IDE_REGISTER_DATA )
libspectrum_ide_write( chn, idereg, dataHi );
}
static void
zxatasp_memory_map( void )
{
int writable;
if( !settings_current.zxatasp_active ) return;
if( settings_current.zxatasp_wp && ( memory_map_romcs[0].page_num & 1 ) ) {
writable = 0;
} else {
writable = 1;
}
memory_map_romcs[0].writable = memory_map_romcs[1].writable = writable;
if( !settings_current.zxatasp_upload ) {
memory_map_read[0] = memory_map_romcs[0];
memory_map_read[1] = memory_map_romcs[1];
}
memory_map_write[0] = memory_map_romcs[0];
memory_map_write[1] = memory_map_romcs[1];
}
static void
zxatasp_from_snapshot( libspectrum_snap *snap )
{
size_t i, page;
if( !libspectrum_snap_zxatasp_active( snap ) ) return;
settings_current.zxatasp_active = 1;
settings_current.zxatasp_upload = libspectrum_snap_zxatasp_upload( snap );
settings_current.zxatasp_wp = libspectrum_snap_zxatasp_writeprotect( snap );
zxatasp_portA = libspectrum_snap_zxatasp_port_a ( snap );
zxatasp_portB = libspectrum_snap_zxatasp_port_b ( snap );
zxatasp_portC = libspectrum_snap_zxatasp_port_c ( snap );
zxatasp_control = libspectrum_snap_zxatasp_control( snap );
page = libspectrum_snap_zxatasp_current_page( snap );
if( page != ZXATASP_NOT_PAGED ) {
machine_current->ram.romcs = 1;
set_zxatasp_bank( page );
}
for( i = 0; i < libspectrum_snap_zxatasp_pages( snap ); i++ )
if( libspectrum_snap_zxatasp_ram( snap, i ) )
memcpy( ZXATASPMEM[ i ], libspectrum_snap_zxatasp_ram( snap, i ),
ZXATASP_PAGE_LENGTH );
machine_current->memory_map();
}
static void
zxatasp_to_snapshot( libspectrum_snap *snap )
{
size_t i;
libspectrum_byte *buffer;
if( !settings_current.zxatasp_active ) return;
libspectrum_snap_set_zxatasp_active( snap, 1 );
libspectrum_snap_set_zxatasp_upload( snap, settings_current.zxatasp_upload );
libspectrum_snap_set_zxatasp_writeprotect( snap,
settings_current.zxatasp_wp );
libspectrum_snap_set_zxatasp_port_a( snap, zxatasp_portA );
libspectrum_snap_set_zxatasp_port_b( snap, zxatasp_portB );
libspectrum_snap_set_zxatasp_port_c( snap, zxatasp_portC );
libspectrum_snap_set_zxatasp_control( snap, zxatasp_control );
libspectrum_snap_set_zxatasp_current_page( snap, current_page );
libspectrum_snap_set_zxatasp_pages( snap, ZXATASP_PAGES );
for( i = 0; i < ZXATASP_PAGES; i++ ) {
buffer = malloc( ZXATASP_PAGE_LENGTH * sizeof( libspectrum_byte ) );
if( !buffer ) {
ui_error( UI_ERROR_ERROR, "Out of memory at %s:%d", __FILE__, __LINE__ );
return;
}
memcpy( buffer, ZXATASPMEM[ i ], ZXATASP_PAGE_LENGTH );
libspectrum_snap_set_zxatasp_ram( snap, i, buffer );
}
}