-
Notifications
You must be signed in to change notification settings - Fork 25
/
disk.c
1865 lines (1604 loc) · 50.6 KB
/
disk.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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
** Oricutron
** Copyright (C) 2009-2014 Peter Gordon
**
** 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, version 2
** of the License.
**
** 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.
**
** WD17xx, Microdisc and Jasmin emulation
*/
/* The microdisc ROM has a bug where if no disk */
/* is inserted at boot, the disk detection routine */
/* has an unbalanced stack and there is a chance */
/* that it will end up executing garbage when you */
/* insert a disk. Euphoric has a fudge to work */
/* around this. It pretends a disk is in the drive */
/* but leaves the "read sector" command lingering */
/* until you insert a disk. Set this define to */
/* do the same fudge */
#define MICRODISC_FUDGE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "system.h"
#include "6502.h"
#include "via.h"
#include "8912.h"
#include "gui.h"
#include "disk.h"
#include "disk_pravetz.h"
#include "monitor.h"
#include "6551.h"
#include "machine.h"
#include "msgbox.h"
#include "filereq.h"
#ifdef WWW
#include <emscripten.h>
#endif
extern char diskfile[], diskpath[], filetmp[];
extern char telediskfile[], telediskpath[];
extern char pravdiskfile[], pravdiskpath[];
extern SDL_bool refreshdisks;
#define GENERAL_DISK_DEBUG 0
#define DEBUG_SECTOR_DUMP 0
#if DEBUG_SECTOR_DUMP
static char sectordumpstr[64];
static int sectordumpcount;
#endif
#ifdef MICRODISC_FUDGE
void microdisc_setintrq( void *md );
#endif
static Uint16 calc_crc( Uint16 crc, Uint8 value)
{
crc = ((unsigned char)(crc >> 8)) | (crc << 8);
crc ^= value;
crc ^= ((unsigned char)(crc & 0xff)) >> 4;
crc ^= (crc << 8) << 4;
crc ^= ((crc & 0xff) << 4) << 1;
return crc;
}
// Pop up disk image information for a couple of seconds
void disk_popup( struct machine *oric, int drive )
{
char tmp[40];
if( !oric->diskname[drive][0] )
{
do_popup( oric, "\x14\x15\x13" );
return;
}
sprintf( tmp, "\x14\x15""%d %-16s", drive, oric->diskname[drive] );
do_popup( oric, tmp );
}
// Free a disk image and clear the pointer to it
static void diskimage_free( struct machine *oric, struct diskimage **dimg )
{
char *dpath, *dfile;
if( !dimg ) return;
if( !(*dimg) ) return;
if( oric->type != MACH_TELESTRAT )
{
switch (oric->drivetype)
{
case DRV_PRAVETZ:
dpath = pravdiskpath;
dfile = pravdiskfile;
break;
default:
dpath = diskpath;
dfile = diskfile;
break;
}
}
else
{
dpath = telediskpath;
dfile = telediskfile;
}
if( (*dimg)->modified )
{
char modmsg[128];
sprintf( modmsg, "The disk in drive %d is modified.\nWould you like to save the changes?", (*dimg)->drivenum );
if( msgbox( oric, MSGBOX_YES_NO, modmsg ) )
{
sprintf( modmsg, "Save disk %d", (*dimg)->drivenum );
if( filerequester( oric, modmsg, dpath, dfile, FR_DISKSAVE ) )
{
joinpath( dpath, dfile );
diskimage_save( oric, filetmp, (*dimg)->drivenum );
}
}
}
if( (*dimg)->rawimage )
{
free( (*dimg)->rawimage );
(*dimg)->rawimage = NULL;
}
free( *dimg );
*dimg = NULL;
}
// Read a raw integer from a disk image file
static Uint32 diskimage_rawint( struct diskimage *dimg, Uint32 offset )
{
if( !dimg ) return 0;
if( ( !dimg->rawimage ) || ( dimg->rawimagelen < 4 ) ) return 0;
if( offset > (dimg->rawimagelen-4) ) return 0;
return (dimg->rawimage[offset+3]<<24)|(dimg->rawimage[offset+2]<<16)|(dimg->rawimage[offset+1]<<8)|dimg->rawimage[offset];
}
// Allocate and initialise a disk image structure
// If "rawimglen" isn't zero, it will also allocate
// ram for a raw disk image
static struct diskimage *diskimage_alloc( Uint32 rawimglen )
{
struct diskimage *dimg;
Uint8 *buf = NULL;
if( rawimglen )
{
// FIXME: this is temporary solution to allow
// low-level formatting up to 128 track per side
if( rawimglen < 128*2*6400+256 ) rawimglen = 128*2*6400+256;
buf = malloc( rawimglen );
if( !buf ) return NULL;
}
dimg = malloc( sizeof( struct diskimage ) );
if( !dimg )
{
free( buf );
return NULL;
}
dimg->drivenum = -1;
dimg->numtracks = 0;
dimg->numsides = 0;
dimg->geometry = 0;
dimg->cachedtrack = -1;
dimg->cachedside = -1;
dimg->numsectors = 0;
dimg->rawimage = buf;
dimg->rawimagelen = rawimglen;
dimg->modified = SDL_FALSE;
dimg->modified_time = 0;
return dimg;
}
// Eject a disk from a drive
void disk_eject( struct machine *oric, int drive )
{
diskimage_free( oric, &oric->wddisk.disk[drive] );
oric->wddisk.disk[drive] = NULL;
oric->pravetz.drv[drive].pimg = NULL;
oric->pravetz.drv[drive].byte = 0;
oric->pravetz.drv[drive].dirty = SDL_FALSE;
oric->pravetz.drv[drive].half_track = 0;
oric->diskname[drive][0] = 0;
disk_popup( oric, drive );
}
// Whenever a seek operation occurs, the track where the head ends up
// is "cached". The track is located within the raw image file, and
// all the sector address and data markers are found, and pointers
// are remembered for each.
void diskimage_cachetrack( struct diskimage *dimg, int track, int side )
{
Uint8 *ptr, *eot;
Uint32 sectorcount, n;
// If this track is already cached, don't waste time doing it again
if( ( dimg->cachedtrack == track ) &&
( dimg->cachedside == side ) )
return;
// reset cached info
for( n=0; n<32; n++ )
{
dimg->sector[n].id_ptr = NULL;
dimg->sector[n].data_ptr = NULL;
}
dimg->numsectors = 0;
dimg->cachedtrack = -1;
dimg->cachedside = -1;
if( side >= dimg->numsides )
return;
// Find the start and end locations of the track within the disk image
ptr = &dimg->rawimage[(side*dimg->numtracks+track)*6400+256];
eot = &ptr[6400];
// Scan through the track looking for sectors
sectorcount = 0;
while( ptr < eot )
{
// Search for ID mark
while( ((ptr+3)<eot) && !((ptr[0]==0xa1) && (ptr[1]==0xa1) && (ptr[2]==0xa1) && (ptr[3]==0xfe)) ) ptr++;
ptr += 3;
// Don't exceed the bounds of this track
if( ptr >= eot ) break;
// Store ID pointer
dimg->sector[sectorcount].id_ptr = ptr;
dimg->sector[sectorcount].data_ptr = NULL;
sectorcount++;
// Get N value
n = ptr[4];
// Skip ID field and CRC
ptr+=7;
// Search for data ID
while( (ptr<eot) && (ptr[0]!=0xfb) && (ptr[0]!=0xf8) ) ptr++;
if( ptr >= eot ) break;
// Store pointer
dimg->sector[sectorcount-1].data_ptr = ptr;
// Skip data field and ID
ptr += (1<<(n+7))+3;
}
if( 0 < sectorcount )
{
// Remember how many sectors we have successfully cached
dimg->numsectors = sectorcount;
dimg->cachedtrack = track;
dimg->cachedside = side;
}
}
// This saves a diskimage back to disk.
// Since the disk image is always kept in standard format, there is
// no processing of the image in this routine, it is just dumped from
// memory back to disk.
SDL_bool diskimage_save( struct machine *oric, char *fname, int drive )
{
FILE *f;
// Make sure there is a disk in the drive!
if( !oric->wddisk.disk[drive] ) return SDL_FALSE;
if( oric->drivetype == DRV_PRAVETZ )
disk_pravetz_write_image(&oric->pravetz.drv[drive]);
// Open the file for writing
f = fopen( fname, "wb" );
if( !f )
{
do_popup( oric, "\x14\x15Save failed" );
return SDL_FALSE;
}
// Dump it to disk
if( fwrite( oric->wddisk.disk[drive]->rawimage, oric->wddisk.disk[drive]->rawimagelen, 1, f ) != 1 )
{
fclose( f );
do_popup( oric, "\x14\x15Save failed" );
return SDL_FALSE;
}
// All done!
fclose( f );
// If we are not just overwriting the original file, remember the new filename
if( fname != oric->wddisk.disk[drive]->filename )
{
strncpy( oric->wddisk.disk[drive]->filename, fname, 4096+512-1 );
oric->wddisk.disk[drive]->filename[4096+512-1] = 0;
}
// The image in memory is no longer different to the last saved version
oric->wddisk.disk[drive]->modified = SDL_FALSE;
oric->wddisk.disk[drive]->modified_time = 0;
#ifdef WWW
// sync from memory state to persisted
EM_ASM(
FS.syncfs(function (err) {
assert(!err);
});
);
#endif
// Remember to update the GUI
refreshdisks = SDL_TRUE;
return SDL_TRUE;
}
// This routine "inserts" a disk image into a virtual drive
SDL_bool diskimage_load( struct machine *oric, char *fname, int drive )
{
FILE *f;
Uint32 len;
// Open the file
f = fopen( fname, "rb" );
if( !f ) return SDL_FALSE;
// The file exists, so eject any currently inserted disk
disk_eject( oric, drive );
// Determine the size of the disk image
fseek( f, 0, SEEK_END );
len = (int)ftell( f );
fseek( f, 0, SEEK_SET );
// Empty file!?
if( len <= 0 )
{
fclose( f );
return SDL_FALSE;
}
// Allocate a new disk image structure and space for the raw image
oric->wddisk.disk[drive] = diskimage_alloc( len );
if( !oric->wddisk.disk[drive] )
{
do_popup( oric, "\x14\x15""Out of memory" );
fclose( f );
return SDL_FALSE;
}
// Read the image file into memory
if( fread( oric->wddisk.disk[drive]->rawimage, len, 1, f ) != 1 )
{
fclose( f );
disk_eject( oric, drive );
do_popup( oric, "\x14\x15""Read error" );
return SDL_FALSE;
}
fclose( f );
if( oric->drivetype == DRV_PRAVETZ )
{
Uint16 t_idx;
Uint16 s_idx;
Uint16 sb_idx;
Uint16 tb_idx;
oric->pravetz.drv[drive].pimg = oric->wddisk.disk[drive];
if( drive >= 2 )
{
disk_eject( oric, drive );
do_popup( oric, "\x14\x15""Invalid drive number" );
return SDL_FALSE;
}
if( len != 143360 )
{
disk_eject( oric, drive );
do_popup( oric, "\x14\x15""Invalid pravetz disk image" );
return SDL_FALSE;
}
// Get some basic image info
for (t_idx = 0; t_idx < PRAV_TRACKS_PER_DISK; t_idx++)
{
tb_idx = 0;
for (s_idx = 0; s_idx < PRAV_SECTORS_PER_TRACK; s_idx++)
{
for (sb_idx = 0; sb_idx < PRAV_RAW_BYTES_PER_SECTOR; sb_idx++, tb_idx++)
{
oric->pravetz.drv[drive].image[t_idx][tb_idx] =
disk_pravetz_image_raw_byte(oric, drive, t_idx, s_idx, sb_idx);
}
}
while (tb_idx < PRAV_RAW_TRACK_SIZE)
{
oric->pravetz.drv[drive].image[t_idx][tb_idx] = 0xFF;
tb_idx++;
}
}
oric->pravetz.drv[drive].byte = 0;
oric->pravetz.drv[drive].half_track = 0;
}
else
{
// Check for the header ID
if( strncmp( (char *)oric->wddisk.disk[drive]->rawimage, "MFM_DISK", 8 ) != 0 )
{
disk_eject( oric, drive );
do_popup( oric, "\x14\x15""Invalid disk image" );
return SDL_FALSE;
}
// Get some basic image info
oric->wddisk.disk[drive]->drivenum = drive;
oric->wddisk.disk[drive]->numsides = diskimage_rawint( oric->wddisk.disk[drive], 8 );
oric->wddisk.disk[drive]->numtracks = diskimage_rawint( oric->wddisk.disk[drive], 12 );
oric->wddisk.disk[drive]->geometry = diskimage_rawint( oric->wddisk.disk[drive], 16 );
// Is the disk sane!?
if( ( oric->wddisk.disk[drive]->numsides < 1 ) ||
( oric->wddisk.disk[drive]->numsides > 2 ) )
{
disk_eject( oric, drive );
do_popup( oric, "\x14\x15""Invalid disk image" );
return SDL_FALSE;
}
}
// Nobody has written to this disk yet
oric->wddisk.disk[drive]->modified = SDL_FALSE;
oric->wddisk.disk[drive]->modified_time = 0;
// Remember the filename of the image for this drive
strncpy( oric->wddisk.disk[drive]->filename, fname, 4096+512-1 );
oric->wddisk.disk[drive]->filename[4096+512-1] = 0;
// Come up with a suitable short name for popups etc.
if( strlen( fname ) > 31 )
{
strncpy( oric->diskname[drive], &fname[strlen(fname)-31], 32 );
oric->diskname[drive][0] = 22;
} else {
strncpy( oric->diskname[drive], fname, 32 );
}
oric->diskname[drive][31] = 0;
// Do the popup
disk_popup( oric, drive );
// Mark the disk status icons as needing a refresh
refreshdisks = SDL_TRUE;
return SDL_TRUE;
};
// This routine does nothing. It is just used as a default for the callback routines
// and is usually replaced by the microdisc/jasmin/whatever implementations.
void wd17xx_dummy( void *nothing )
{
}
// Initialise a WD17xx controller instance
void wd17xx_init( struct wd17xx *wd )
{
wd->r_status = 0;
wd->r_track = 0;
wd->r_sector = 0;
wd->r_data = 0;
wd->c_drive = 0;
wd->c_side = 0;
wd->c_track = 0;
wd->c_sector = 0;
wd->last_step_in = SDL_FALSE;
wd->setintrq = wd17xx_dummy;
wd->clrintrq = wd17xx_dummy;
wd->intrqarg = NULL;
wd->setdrq = wd17xx_dummy;
wd->clrdrq = wd17xx_dummy;
wd->drqarg = NULL;
wd->currentop = COP_NUFFINK;
wd->delayedint = 0;
wd->delayeddrq = 0;
wd->distatus = -1;
wd->ddstatus = -1;
refreshdisks = SDL_TRUE;
}
// This routine emulates some cycles of disk activity.
void wd17xx_ticktock( struct wd17xx *wd, int cycles )
{
#ifdef MICRODISC_FUDGE
if ((wd->currentop == COP_READ_SECTORS_FUDGE) ||
(wd->currentop == COP_READ_SECTOR_FUDGE))
{
if (wd->disk[wd->c_drive])
{
wd->curroffs = 0;
wd->currsector = wd17xx_find_sector( wd, wd->r_sector );
if( !wd->currsector )
{
wd->r_status = WSF_RNF;
wd->clrdrq( wd->drqarg );
wd->setintrq( wd->intrqarg );
wd->currentop = COP_NUFFINK;
refreshdisks = SDL_TRUE;
#if GENERAL_DISK_DEBUG
dbg_printf( "DISK: Sector %d not found.", wd->r_sector );
#endif
}
else
{
wd->currseclen = 1<<(wd->currsector->id_ptr[4]+7);
wd->r_status = WSF_BUSY|WSF_NOTREADY;
wd->delayeddrq = 60;
wd->currentop = (wd->currentop == COP_READ_SECTORS_FUDGE) ? COP_READ_SECTORS : COP_READ_SECTOR;
wd->crc = 0xe295;
refreshdisks = SDL_TRUE;
#if DEBUG_SECTOR_DUMP
sectordumpcount = 0;
sectordumpstr[0] = 0;
#endif
}
}
}
#endif
// Is there a pending INTRQ?
if( wd->delayedint > 0 )
{
// Count down the INTRQ timer!
wd->delayedint -= cycles;
// Time to assert INTRQ?
if( wd->delayedint <= 0 )
{
// Yep! Stop timing.
wd->delayedint = 0;
// Need to update the status register?
if( wd->distatus != -1 )
{
// Yep. Do so.
wd->r_status = wd->distatus;
wd->distatus = -1;
}
// Assert INTRQ (this function pointer is set up by the microdisc/jasmin/whatever controller)
wd->setintrq( wd->intrqarg );
#if GENERAL_DISK_DEBUG
dbg_printf( "DISK: Delayed INTRQ" );
#endif
}
}
// Is there a pending DRQ?
if( wd->delayeddrq > 0 )
{
// Count down the DRQ timer!
wd->delayeddrq -= cycles;
// Time to assert DRQ?
if( wd->delayeddrq <= 0 )
{
// Yep! Stop timing.
wd->delayeddrq = 0;
// Need to update the status register?
if( wd->ddstatus != -1 )
{
// Yep. Do so.
wd->r_status = wd->ddstatus;
wd->ddstatus = -1;
}
// Assert DRQ
wd->r_status |= WSF_DRQ;
wd->setdrq( wd->drqarg );
}
}
}
// This routine seeks to the specified track. It is used by the SEEK and STEP commands.
void wd17xx_seek_track( struct wd17xx *wd, Uint8 track, SDL_bool dofudge )
{
// Is there a disk in the drive?
if( wd->disk[wd->c_drive] )
{
// Yes. If we are trying to seek to a non-existant track, just seek as far as we can
if( track >= wd->disk[wd->c_drive]->numtracks )
{
track = (wd->disk[wd->c_drive]->numtracks>0)?wd->disk[wd->c_drive]->numtracks-1:0;
wd->distatus = WSFI_HEADL|WSFI_SEEKERR;
}
else
{
wd->distatus = WSFI_HEADL|WSFI_PULSE;
}
// Cache the new track
diskimage_cachetrack( wd->disk[wd->c_drive], track, wd->c_side );
// Update our status
wd->c_track = track;
wd->c_sector = 0;
wd->r_track = track;
// Assert INTRQ in 20 cycles time and update the status accordingly
// (note: 20 cycles is waaaaaay faster than any real drive could seek. The actual
// delay would depend how far the head had to seek, and what stepping speed was
// currently set).
wd->delayedint = 20;
if( wd->c_track == 0 ) wd->distatus |= WSFI_TRK0;
#if GENERAL_DISK_DEBUG
dbg_printf( "DISK: At track %u (%u sectors)", track, wd->disk[wd->c_drive]->numsectors );
#endif
return;
}
// No disk in drive
// Set INTRQ because the operation has finished.
wd->setintrq( wd->intrqarg );
wd->r_track = 0;
#if GENERAL_DISK_DEBUG
dbg_printf( "DISK: Seek fail" );
#endif
#ifdef MICRODISC_FUDGE
// Euphoric style workaround for the sedoric bug...
if ((dofudge) && (wd->setintrq == microdisc_setintrq))
{
wd->r_status = 0x44;
return;
}
#endif
// Set error state
wd->r_status = WSF_NOTREADY|WSFI_SEEKERR;
}
// This routine looks for the sector with the specified ID in the current track.
// It returns NULL if there is no such sector, or a structure with pointers to
// the ID and data fields if the sector is found.
struct mfmsector *wd17xx_find_sector( struct wd17xx *wd, Uint8 secid )
{
int revs=0;
struct diskimage *dimg;
// Save some typing...
dimg = wd->disk[wd->c_drive];
// No disk image? No sectors!
if( !dimg ) return NULL;
// Make sure the current track is cached
diskimage_cachetrack( dimg, wd->c_track, wd->c_side );
// No sectors on this track? Someone needs to format their disk...
if( dimg->numsectors < 1 )
return NULL;
// We do this more realistically than we need to since this is not
// a super-accurate emulation (for now). Never mind. Lets go
// around the track up to two times.
while( revs < 2 )
{
// Move on to the next sector
wd->c_sector = (wd->c_sector+1)%dimg->numsectors;
// If we passed through the start of the track, set the pulse bit in the status register
if( !wd->c_sector )
{
revs++;
wd->r_status |= WSFI_PULSE;
}
// Found the required sector?
if( dimg->sector[wd->c_sector].id_ptr[3] == secid )
return &dimg->sector[wd->c_sector];
}
// The search failed :-(
#if GENERAL_DISK_DEBUG
dbg_printf( "Couldn't find sector %u", secid );
#endif
return NULL;
}
// This returns the first valid sector in the current track, or NULL if
// there aren't any sectors.
struct mfmsector *wd17xx_first_sector( struct wd17xx *wd )
{
struct diskimage *dimg;
dimg = wd->disk[wd->c_drive];
// No disk? no sector...
if( !dimg ) return NULL;
// Make sure the current track is cached
diskimage_cachetrack( dimg, wd->c_track, wd->c_side );
// No sectors?!
if( dimg->numsectors < 1 )
return NULL;
// We're at the first sector!
wd->c_sector = 0;
wd->r_status = WSFI_PULSE;
// Return the sector pointers
return &dimg->sector[wd->c_sector];
}
// Move on to the next sector
struct mfmsector *wd17xx_next_sector( struct wd17xx *wd )
{
struct diskimage *dimg;
dimg = wd->disk[wd->c_drive];
// No disk? No sectors!
if( !dimg ) return NULL;
// Make sure the current track is cached
diskimage_cachetrack( dimg, wd->c_track, wd->c_side );
// No sectors?
if( dimg->numsectors < 1 )
return NULL;
// Get the next sector number
wd->c_sector = (wd->c_sector+1)%dimg->numsectors;
// If we are at the start of the track, set the pulse bit
if( !wd->c_sector ) wd->r_status |= WSFI_PULSE;
// Return the sector pointers
return &dimg->sector[wd->c_sector];
}
// Perform a read operation on a WD17xx register
unsigned char wd17xx_read( struct wd17xx *wd, unsigned short addr )
{
// Which register?!
switch( addr )
{
case 0: // Status register
wd->clrintrq( wd->intrqarg ); // Reading the status register clears INTRQ
return wd->r_status;
case 1: // Track register
return wd->r_track;
case 2: // Sector register
return wd->r_sector;
case 3: // Data register
// What are we currently doing?
switch( wd->currentop )
{
#ifdef MICRODISC_FUDGE
case COP_READ_SECTOR_FUDGE:
case COP_READ_SECTORS_FUDGE:
return 0;
#endif
case COP_READ_SECTOR:
case COP_READ_SECTORS:
// We somehow started a sector read operation without a valid sector.
if( !wd->currsector )
{
// Abort.
wd->r_status &= ~WSF_DRQ;
wd->r_status |= WSF_RNF;
wd->clrdrq( wd->drqarg );
wd->currentop = COP_NUFFINK;
refreshdisks = SDL_TRUE;
break;
}
// If this is the first read of a read operation, remember the record type for later
if( wd->curroffs == 0 ) wd->sectype = (wd->currsector->data_ptr[wd->curroffs++]==0xf8)?WSFR_RECTYP:0x00;
// Get the next byte from the sector
wd->r_data = wd->currsector->data_ptr[wd->curroffs++];
wd->crc = calc_crc( wd->crc, wd->r_data );
// Clear any previous DRQ
wd->r_status &= ~WSF_DRQ;
wd->clrdrq( wd->drqarg );
#if DEBUG_SECTOR_DUMP
sprintf( §ordumpstr[sectordumpcount*2], "%02X", wd->r_data );
sectordumpstr[34+sectordumpcount] = ((wd->r_data>31)&&(wd->r_data<127)) ? wd->r_data : '.';
sectordumpcount++;
if( sectordumpcount >= 16 )
{
sectordumpstr[32] = ' ';
sectordumpstr[33] = '\'';
sectordumpstr[50] = '\'';
sectordumpstr[51] = 0;
dbg_printf( "%s", sectordumpstr );
sectordumpcount = 0;
}
#endif
// Has the whole sector been read?
if( wd->curroffs > wd->currseclen )
{
// If you want to do CRC checking, wd->crc should equal (wd->currsector->data_ptr[wd_curroffs]<<8)|wd->currsector->data_ptr[wd_curroffs+1] right here
#if DEBUG_SECTOR_DUMP
if( sectordumpcount )
{
sectordumpstr[33] = '\'';
sectordumpstr[34+sectordumpcount] = '\'';
sectordumpstr[35+sectordumpcount] = 0;
for( sectordumpcount*=2; sectordumpcount<33; sectordumpcount++ )
sectordumpstr[sectordumpcount*2] = ' ';
dbg_printf( "%s", sectordumpstr );
sectordumpcount = 0;
}
#endif
// We've got to the end of the current sector. IF it is a multiple sector
// operation, we need to move on!
if( wd->currentop == COP_READ_SECTORS )
{
#if GENERAL_DISK_DEBUG
dbg_printf( "Next sector..." );
#endif
// Get the next sector, and carry on!
wd->r_sector++;
wd->curroffs = 0;
wd->currsector = wd17xx_find_sector( wd, wd->r_sector );
wd->crc = 0xe295;
// If we hit the end of the track, thats fine, it just means the operation
// is finished.
if( !wd->currsector )
{
wd->delayedint = 20; // Assert INTRQ in 20 cycles time
wd->distatus = wd->sectype; // ...and when doing so, set the status to reflect the record type
wd->currentop = COP_NUFFINK; // No longer in the middle of an operation
wd->r_status &= (~WSF_DRQ); // Clear DRQ (no data to read)
wd->clrdrq( wd->drqarg );
refreshdisks = SDL_TRUE; // Turn off the disk LED in the status bar
break;
}
// We've got the next sector lined up. Assert DRQ in 180 cycles time (simulate a bit of a delay
// between sectors. Note that most of these values have been pulled out of thin air and might need
// adjusting for some pickier loaders).
wd->delayeddrq = 180;
break;
}
// Just reading one sector so..
wd->delayedint = 32; // INTRQ in a little while because we're finished
wd->distatus = wd->sectype; // Set the status accordingly
wd->currentop = COP_NUFFINK; // Finished the op
wd->r_status &= (~WSF_DRQ); // Clear DRQ (no more data)
wd->clrdrq( wd->drqarg );
refreshdisks = SDL_TRUE; // Turn off disk LED
} else {
wd->delayeddrq = 32; // More data ready. DRQ to let them know!
}
break;
case COP_READ_TRACK:
wd->r_data = wd->disk[wd->c_drive]->rawimage[(wd->c_side*wd->disk[wd->c_drive]->numtracks + wd->c_track)*6400+256 + wd->curroffs];
wd->curroffs++;
wd->r_status &= ~WSF_DRQ;
wd->clrdrq( wd->drqarg );
// Has the whole track been read?
if( wd->curroffs >= 6400 )
{
wd->delayedint = 20;
wd->distatus = 0;
wd->currentop = COP_NUFFINK;
refreshdisks = SDL_TRUE;
} else {
wd->delayeddrq = 32;
}
break;
case COP_READ_ADDRESS:
if( !wd->currsector )
{
wd->r_status &= ~WSF_DRQ;
wd->clrdrq( wd->drqarg );
wd->currentop = COP_NUFFINK;
refreshdisks = SDL_TRUE;
break;
}
if( wd->curroffs == 0 ) wd->r_sector = wd->currsector->id_ptr[1];
wd->r_data = wd->currsector->id_ptr[++wd->curroffs];
wd->r_status &= ~WSF_DRQ;
wd->clrdrq( wd->drqarg );
if( wd->curroffs >= 6 )
{
wd->delayedint = 20;
wd->distatus = 0;
wd->currentop = COP_NUFFINK;
refreshdisks = SDL_TRUE;
} else {
wd->delayeddrq = 32;
}
break;
}
return wd->r_data;
}
return 0; // ??
}
static SDL_bool last_step_in = SDL_FALSE;
void wd17xx_write( struct machine *oric, struct wd17xx *wd, unsigned short addr, unsigned char data )
{
switch( addr )
{
case 0: // Command register
wd->clrintrq( wd->intrqarg );
switch( data & 0xe0 )
{
case 0x00: // Restore or seek
switch( data & 0x10 )
{
case 0x00: // Restore (Type I)
#if GENERAL_DISK_DEBUG
dbg_printf( "DISK: (%04X) Restore", oric->cpu.pc-1 );
#endif
wd->r_status = WSF_BUSY;
if( data & 8 ) wd->r_status |= WSFI_HEADL;
wd17xx_seek_track( wd, 0, oric->cpu.calcpc == 0xe3a1 );
wd->currentop = COP_NUFFINK;
refreshdisks = SDL_TRUE;
break;
case 0x10: // Seek (Type I)
#if GENERAL_DISK_DEBUG
dbg_printf( "DISK: (%04X) Seek", oric->cpu.pc-1 );
#endif
wd->r_status = WSF_BUSY;
if( data & 8 ) wd->r_status |= WSFI_HEADL;
wd17xx_seek_track( wd, wd->r_data, SDL_FALSE );
wd->currentop = COP_NUFFINK;
refreshdisks = SDL_TRUE;
break;
}
break;
case 0x20: // Step (Type I)
#if GENERAL_DISK_DEBUG
dbg_printf( "DISK: (%04X) Step", oric->cpu.pc-1 );
#endif
wd->r_status = WSF_BUSY;
if( data & 8 ) wd->r_status |= WSFI_HEADL;
if( last_step_in )
wd17xx_seek_track( wd, wd->c_track+1, SDL_FALSE );
else
wd17xx_seek_track( wd, wd->c_track > 0 ? wd->c_track-1 : 0, SDL_FALSE );
wd->currentop = COP_NUFFINK;
refreshdisks = SDL_TRUE;
break;