forked from stardot/beebem-mac
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisc8271.cc
executable file
·1938 lines (1627 loc) · 58.6 KB
/
disc8271.cc
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
/****************************************************************************/
/* Beebem - (c) David Alan Gilbert 1994 */
/* ------------------------------------ */
/* This program may be distributed freely within the following restrictions:*/
/* */
/* 1) You may not charge for this program or for any part of it. */
/* 2) This copyright message must be distributed with all copies. */
/* 3) This program must be distributed complete with source code. Binary */
/* only distribution is not permitted. */
/* 4) The author offers no warrenties, or guarentees etc. - you use it at */
/* your own risk. If it messes something up or destroys your computer */
/* thats YOUR problem. */
/* 5) You may use small sections of code from this program in your own */
/* applications - but you must acknowledge its use. If you plan to use */
/* large sections then please ask the author. */
/* */
/* If you do not agree with any of the above then please do not use this */
/* program. */
/* Please report any problems to the author at [email protected] */
/****************************************************************************/
/* 8271 disc emulation - David Alan Gilbert 4/12/94 */
/* Mike Wyatt 30/8/97 - Added disc write and format support */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "6502core.h"
#include "disc8271.h"
#include "main.h"
#include "beebmem.h"
#include "disc1770.h"
#include "uefstate.h"
#include "beebsound.h"
extern int TorchTube;
unsigned char Disc8271Enabled=1;
int Disc8271Trigger; /* Cycle based time Disc8271Trigger */
static unsigned char ResultReg;
static unsigned char StatusReg;
static unsigned char DataReg;
static unsigned char Internal_Scan_SectorNum;
static unsigned int Internal_Scan_Count; /* Read as two bytes */
static unsigned char Internal_ModeReg;
static unsigned char Internal_CurrentTrack[2]; /* 0/1 for surface number */
static unsigned char Internal_DriveControlOutputPort;
static unsigned char Internal_DriveControlInputPort;
static unsigned char Internal_BadTracks[2][2]; /* 1st subscript is surface 0/1 and second subscript is badtrack 0/1 */
static int DriveHeadPosition[2]={0};
static bool DriveHeadLoaded=false;
static bool DriveHeadUnloadPending=false;
static int ThisCommand;
static int NParamsInThisCommand;
static int PresentParam; /* From 0 */
static unsigned char Params[16]; /* Wildly more than we need */
static bool Selects[2]; /* Drive selects */
static bool Writeable[2]={false,false}; /* True if the drives are writeable */
static int FirstWriteInt; /* Indicates the start of a write operation */
static int NextInterruptIsErr; /* none 0 causes error and drops this value into result reg */
#define TRACKSPERDRIVE 80
/* Note Head select is done from bit 5 of the drive output register */
#define CURRENTHEAD ((Internal_DriveControlOutputPort>>5) & 1)
/* Note: reads/writes one byte every 80us */
#define TIMEBETWEENBYTES (160)
typedef struct {
struct {
unsigned int CylinderNum:7;
unsigned int RecordNum:5;
unsigned int HeadNum:1;
unsigned int PhysRecLength;
} IDField;
unsigned int Deleted:1; /* If non-zero the sector is deleted */
unsigned char *Data;
} SectorType;
typedef struct {
int LogicalSectors; /* Number of sectors stated in format command */
int NSectors; /* i.e. the number of records we have - not anything physical */
SectorType *Sectors;
int Gap1Size,Gap3Size,Gap5Size; /* From format command */
} MyTrackType;
/* All data on the disc - first param is drive number, then head. then physical track id */
MyTrackType DiscStore[2][2][TRACKSPERDRIVE];
/* File names of loaded disc images */
static char FileNames[2][256];
/* Number of sides of loaded disc images */
static int NumHeads[2];
static bool SaveTrackImage(int DriveNum, int HeadNum, int TrackNum);
static void DriveHeadScheduleUnload(void);
typedef void (*CommandFunc)(void);
#define UPDATENMISTATUS if (StatusReg & 8) NMIStatus |=1<<nmi_floppy; else NMIStatus &= ~(1<<nmi_floppy);
/*--------------------------------------------------------------------------*/
static struct {
int TrackAddr;
int CurrentSector;
int SectorLength; /* In bytes */
int SectorsToGo;
SectorType *CurrentSectorPtr;
MyTrackType *CurrentTrackPtr;
int ByteWithinSector; /* Next byte in sector or ID field */
} CommandStatus;
/*--------------------------------------------------------------------------*/
typedef struct {
unsigned char CommandNum;
unsigned char Mask; /* Mask command with this before comparing with CommandNum - allows drive ID to be removed */
int NParams; /* Number of parameters to follow */
CommandFunc ToCall; /* Called after all paameters have arrived */
CommandFunc IntHandler; /* Called when interrupt requested by command is about to happen */
const char *Ident; /* Mainly for debugging */
} PrimaryCommandLookupType;
static void SetMotor(char Drive,bool State) {
if (Drive==0)
LEDs.Disc0=State;
else
LEDs.Disc1=State;
}
/*--------------------------------------------------------------------------*/
/* For appropriate commands checks the select bits in the command code and */
/* selects the appropriate drive. */
static void DoSelects(void) {
Selects[0]=(ThisCommand & 0x40)!=0;
Selects[1]=(ThisCommand & 0x80)!=0;
Internal_DriveControlOutputPort&=0x3f;
if (Selects[0]) Internal_DriveControlOutputPort|=0x40;
if (Selects[1]) Internal_DriveControlOutputPort|=0x80;
}; /* DoSelects */
/*--------------------------------------------------------------------------*/
static void NotImp(const char *NotImpCom) {
#ifdef WIN32
char errstr[200];
sprintf(errstr,"Disc operation '%s' not supported", NotImpCom);
MessageBox(GETHWND,errstr,"BBC Emulator",MB_OK|MB_ICONERROR);
#else
fprintf(stderr, "%s has not been implemented in disc8271 - sorry\n", NotImpCom);
exit(0);
#endif
}; /* NotImp */
/*--------------------------------------------------------------------------*/
/* Load the head - ignore for the moment */
static void DoLoadHead(void) {
}; /* DoLoadHead */
/*--------------------------------------------------------------------------*/
/* Initialise our disc structures */
static void InitDiscStore(void) {
int head,track,drive;
MyTrackType blank={0,0,NULL,0,0,0};
for(drive=0;drive<2;drive++)
for(head=0;head<2;head++)
for(track=0;track<TRACKSPERDRIVE;track++)
DiscStore[drive][head][track]=blank;
}; /* InitDiscStore */
/*--------------------------------------------------------------------------*/
/* Given a logical track number accounts for bad tracks */
static int SkipBadTracks(int Unit, int trackin) {
int offset=0;
if (!TorchTube) // If running under Torch Z80, ignore bad tracks
{
if (Internal_BadTracks[Unit][0]<=trackin) offset++;
if (Internal_BadTracks[Unit][1]<=trackin) offset++;
}
return(trackin+offset);
}; /* SkipBadTracks */
/*--------------------------------------------------------------------------*/
/* Returns a pointer to the data structure for a particular track. You */
/* pass the logical track number, it takes into account bad tracks and the */
/* drive select and head select etc. It always returns a valid ptr - if */
/* there aren't that many tracks then it uses the last one. */
/* The one exception!!!! is that if no drives are selected it returns NULL */
static MyTrackType *GetTrackPtr(int LogicalTrackID) {
int UnitID=-1;
if (Selects[0]) UnitID=0;
if (Selects[1]) UnitID=1;
if (UnitID<0) return(NULL);
LogicalTrackID=SkipBadTracks(UnitID,LogicalTrackID);
if (LogicalTrackID>=TRACKSPERDRIVE) LogicalTrackID=TRACKSPERDRIVE-1;
return(&(DiscStore[UnitID][CURRENTHEAD][LogicalTrackID]));
}; /* GetTrackPtr */
/*--------------------------------------------------------------------------*/
/* Returns a pointer to the data structure for a particular sector. Returns */
/* NULL for Sector not found. Doesn't check cylinder/head ID */
static SectorType *GetSectorPtr(MyTrackType *Track, int LogicalSectorID, int FindDeleted) {
int CurrentSector;
if (Track->Sectors==NULL) return(NULL);
for(CurrentSector=0;CurrentSector<Track->NSectors;CurrentSector++)
if ((Track->Sectors[CurrentSector].IDField.RecordNum==LogicalSectorID) && ((!Track->Sectors[CurrentSector].Deleted) || (!FindDeleted)))
return(&(Track->Sectors[CurrentSector]));
return(NULL);
}; /* GetSectorPtr */
/*--------------------------------------------------------------------------*/
/* Returns true if the drive signified by the current selects is ready */
static bool CheckReady(void) {
return Selects[0] || Selects[1];
}; /* CheckReady */
/*--------------------------------------------------------------------------*/
/* Cause an error - pass err num */
static void DoErr(int ErrNum) {
SetTrigger(50,Disc8271Trigger); /* Give it a bit of time */
NextInterruptIsErr=ErrNum;
StatusReg=0x80; /* Command is busy - come back when I have an interrupt */
UPDATENMISTATUS;
}; /* DoErr */
/*--------------------------------------------------------------------------*/
/* Checks a few things in the sector - returns true if OK */
static int ValidateSector(SectorType *SecToVal,int Track,int SecLength) {
if (SecToVal->IDField.CylinderNum!=Track) return(0);
if (SecToVal->IDField.PhysRecLength!=SecLength) return(0);
return(1);
}; /* ValidateSector */
/*--------------------------------------------------------------------------*/
static void DoVarLength_ScanDataCommand(void) {
DoSelects();
NotImp("DoVarLength_ScanDataCommand");
};
/*--------------------------------------------------------------------------*/
static void DoVarLength_ScanDataAndDeldCommand(void) {
DoSelects();
NotImp("DoVarLength_ScanDataAndDeldCommand");
};
/*--------------------------------------------------------------------------*/
static void Do128ByteSR_WriteDataCommand(void) {
DoSelects();
NotImp("Do128ByteSR_WriteDataCommand");
};
/*--------------------------------------------------------------------------*/
static void DoVarLength_WriteDataCommand(void) {
int Drive=-1;
DoSelects();
DoLoadHead();
if (!CheckReady()) {
DoErr(0x10); /* Drive not ready */
return;
};
if (Selects[0]) Drive=0;
if (Selects[1]) Drive=1;
if (!Writeable[Drive]) {
DoErr(0x12); /* Drive write protected */
return;
};
Internal_CurrentTrack[Drive]=Params[0];
CommandStatus.CurrentTrackPtr=GetTrackPtr(Params[0]);
if (CommandStatus.CurrentTrackPtr==NULL) {
DoErr(0x10);
return;
};
CommandStatus.CurrentSectorPtr=GetSectorPtr(CommandStatus.CurrentTrackPtr,Params[1],0);
if (CommandStatus.CurrentSectorPtr==NULL) {
DoErr(0x1e); /* Sector not found */
return;
};
CommandStatus.TrackAddr=Params[0];
CommandStatus.CurrentSector=Params[1];
CommandStatus.SectorsToGo=Params[2] & 31;
CommandStatus.SectorLength=1<<(7+((Params[2] >> 5) & 7));
if (ValidateSector(CommandStatus.CurrentSectorPtr,CommandStatus.TrackAddr,CommandStatus.SectorLength)) {
CommandStatus.ByteWithinSector=0;
SetMotor(Drive,true);
SetTrigger(TIMEBETWEENBYTES,Disc8271Trigger);
StatusReg=0x80; /* Command busy */
UPDATENMISTATUS;
CommandStatus.ByteWithinSector=0;
FirstWriteInt=1;
} else {
DoErr(0x1e); /* Sector not found */
};
};
/*--------------------------------------------------------------------------*/
static void WriteInterrupt(void) {
int LastByte=0;
if (CommandStatus.SectorsToGo<0) {
StatusReg=0x18; /* Result and interrupt */
if (Selects[0])
SetMotor(0,false);
if (Selects[1])
SetMotor(1,false);
UPDATENMISTATUS;
return;
};
if (!FirstWriteInt)
CommandStatus.CurrentSectorPtr->Data[CommandStatus.ByteWithinSector++]=DataReg;
else
FirstWriteInt=0;
ResultReg=0;
if (CommandStatus.ByteWithinSector>=CommandStatus.SectorLength) {
CommandStatus.ByteWithinSector=0;
if (--CommandStatus.SectorsToGo) {
CommandStatus.CurrentSector++;
CommandStatus.CurrentSectorPtr=GetSectorPtr(CommandStatus.CurrentTrackPtr,CommandStatus.CurrentSector,0);
if (CommandStatus.CurrentSectorPtr==NULL) {
DoErr(0x1e); /* Sector not found */
return;
}
} else {
/* Last sector done, write the track back to disc */
if (SaveTrackImage(Selects[0] ? 0 : 1, CURRENTHEAD, CommandStatus.TrackAddr)) {
StatusReg=0x10;
UPDATENMISTATUS;
LastByte=1;
CommandStatus.SectorsToGo=-1; /* To let us bail out */
SetTrigger(0,Disc8271Trigger); /* To pick up result */
}
else {
DoErr(0x12);
}
};
};
if (!LastByte) {
StatusReg=0x8c; /* Command busy, */
UPDATENMISTATUS;
SetTrigger(TIMEBETWEENBYTES,Disc8271Trigger);
};
}; /* WriteInterrupt */
/*--------------------------------------------------------------------------*/
static void Do128ByteSR_WriteDeletedDataCommand(void) {
DoSelects();
NotImp("Do128ByteSR_WriteDeletedDataCommand");
};
/*--------------------------------------------------------------------------*/
static void DoVarLength_WriteDeletedDataCommand(void) {
DoSelects();
NotImp("DoVarLength_WriteDeletedDataCommand");
};
/*--------------------------------------------------------------------------*/
static void Do128ByteSR_ReadDataCommand(void) {
DoSelects();
NotImp("Do128ByteSR_ReadDataCommand");
};
/*--------------------------------------------------------------------------*/
static void DoVarLength_ReadDataCommand(void) {
int Drive=-1;
DoSelects();
DoLoadHead();
if (!CheckReady()) {
DoErr(0x10); /* Drive not ready */
return;
};
if (Selects[0]) Drive=0;
if (Selects[1]) Drive=1;
Internal_CurrentTrack[Drive]=Params[0];
CommandStatus.CurrentTrackPtr=GetTrackPtr(Params[0]);
if (CommandStatus.CurrentTrackPtr==NULL) {
DoErr(0x10);
return;
};
CommandStatus.CurrentSectorPtr=GetSectorPtr(CommandStatus.CurrentTrackPtr,Params[1],0);
if (CommandStatus.CurrentSectorPtr==NULL) {
DoErr(0x1e); /* Sector not found */
return;
};
CommandStatus.TrackAddr=Params[0];
CommandStatus.CurrentSector=Params[1];
CommandStatus.SectorsToGo=Params[2] & 31;
CommandStatus.SectorLength=1<<(7+((Params[2] >> 5) & 7));
if (ValidateSector(CommandStatus.CurrentSectorPtr,CommandStatus.TrackAddr,CommandStatus.SectorLength)) {
CommandStatus.ByteWithinSector=0;
SetMotor(Drive,true);
SetTrigger(TIMEBETWEENBYTES,Disc8271Trigger);
StatusReg=0x80; /* Command busy */
UPDATENMISTATUS;
CommandStatus.ByteWithinSector=0;
} else {
DoErr(0x1e); /* Sector not found */
};
};
/*--------------------------------------------------------------------------*/
static void ReadInterrupt(void) {
int LastByte=0;
if (CommandStatus.SectorsToGo<0) {
StatusReg=0x18; /* Result and interrupt */
if (Selects[0])
SetMotor(0,false);
if (Selects[1])
SetMotor(1,false);
UPDATENMISTATUS;
return;
};
DataReg=CommandStatus.CurrentSectorPtr->Data[CommandStatus.ByteWithinSector++];
/*cerr << "ReadInterrupt called - DataReg=0x" << hex << int(DataReg) << dec << "ByteWithinSector=" << CommandStatus.ByteWithinSector << "\n"; */
/* DumpAfterEach=1; */
ResultReg=0;
if (CommandStatus.ByteWithinSector>=CommandStatus.SectorLength) {
CommandStatus.ByteWithinSector=0;
/* I don't know if this can cause the thing to step - I presume not for the moment */
if (--CommandStatus.SectorsToGo) {
CommandStatus.CurrentSector++;
if (CommandStatus.CurrentSectorPtr=GetSectorPtr(CommandStatus.CurrentTrackPtr,CommandStatus.CurrentSector,0),
CommandStatus.CurrentSectorPtr==NULL) {
DoErr(0x1e); /* Sector not found */
return;
}/* else cerr << "all ptr for sector " << CommandStatus.CurrentSector << "\n"*/;
} else {
/* Last sector done */
StatusReg=0x9c;
UPDATENMISTATUS;
LastByte=1;
CommandStatus.SectorsToGo=-1; /* To let us bail out */
SetTrigger(TIMEBETWEENBYTES,Disc8271Trigger); /* To pick up result */
};
};
if (!LastByte) {
StatusReg=0x8c; /* Command busy, */
UPDATENMISTATUS;
SetTrigger(TIMEBETWEENBYTES,Disc8271Trigger);
};
}; /* ReadInterrupt */
/*--------------------------------------------------------------------------*/
static void Do128ByteSR_ReadDataAndDeldCommand(void) {
DoSelects();
NotImp("Do128ByteSR_ReadDataAndDeldCommand");
};
/*--------------------------------------------------------------------------*/
static void DoVarLength_ReadDataAndDeldCommand(void) {
/* Use normal read command for now - deleted data not supported */
DoVarLength_ReadDataCommand();
};
/*--------------------------------------------------------------------------*/
static void DoReadIDCommand(void) {
int Drive=-1;
DoSelects();
DoLoadHead();
if (!CheckReady()) {
DoErr(0x10); /* Drive not ready */
return;
};
if (Selects[0]) Drive=0;
if (Selects[1]) Drive=1;
Internal_CurrentTrack[Drive]=Params[0];
CommandStatus.CurrentTrackPtr=GetTrackPtr(Params[0]);
if (CommandStatus.CurrentTrackPtr==NULL) {
DoErr(0x10);
return;
};
CommandStatus.CurrentSectorPtr=GetSectorPtr(CommandStatus.CurrentTrackPtr,0,0);
if (CommandStatus.CurrentSectorPtr==NULL) {
DoErr(0x1e); /* Sector not found */
return;
};
CommandStatus.TrackAddr=Params[0];
CommandStatus.CurrentSector=0;
CommandStatus.SectorsToGo=Params[2];
CommandStatus.ByteWithinSector=0;
SetMotor(Drive,true);
SetTrigger(TIMEBETWEENBYTES,Disc8271Trigger);
StatusReg=0x80; /* Command busy */
UPDATENMISTATUS;
CommandStatus.ByteWithinSector=0;
};
/*--------------------------------------------------------------------------*/
static void ReadIDInterrupt(void) {
int LastByte=0;
if (CommandStatus.SectorsToGo<0) {
StatusReg=0x18; /* Result and interrupt */
UPDATENMISTATUS;
if (Selects[0])
SetMotor(0,false);
if (Selects[1])
SetMotor(1,false);
return;
};
if (CommandStatus.ByteWithinSector==0)
DataReg=CommandStatus.CurrentSectorPtr->IDField.CylinderNum;
else if (CommandStatus.ByteWithinSector==1)
DataReg=CommandStatus.CurrentSectorPtr->IDField.HeadNum;
else if (CommandStatus.ByteWithinSector==2)
DataReg=CommandStatus.CurrentSectorPtr->IDField.RecordNum;
else
DataReg=1; /* 1=256 byte sector length */
CommandStatus.ByteWithinSector++;
ResultReg=0;
if (CommandStatus.ByteWithinSector>=4) {
CommandStatus.ByteWithinSector=0;
if (--CommandStatus.SectorsToGo) {
CommandStatus.CurrentSector++;
CommandStatus.CurrentSectorPtr=GetSectorPtr(CommandStatus.CurrentTrackPtr,
CommandStatus.CurrentSector,0);
if (CommandStatus.CurrentSectorPtr==NULL) {
DoErr(0x1e); /* Sector not found */
return;
}
} else {
/* Last sector done */
StatusReg=0x9c;
UPDATENMISTATUS;
LastByte=1;
CommandStatus.SectorsToGo=-1; /* To let us bail out */
SetTrigger(TIMEBETWEENBYTES,Disc8271Trigger); /* To pick up result */
};
};
if (!LastByte) {
StatusReg=0x8c; /* Command busy, */
UPDATENMISTATUS;
SetTrigger(TIMEBETWEENBYTES,Disc8271Trigger);
};
}; /* ReadIDInterrupt */
/*--------------------------------------------------------------------------*/
static void Do128ByteSR_VerifyDataAndDeldCommand(void) {
DoSelects();
NotImp("Do128ByteSR_VerifyDataAndDeldCommand");
};
/*--------------------------------------------------------------------------*/
static void DoVarLength_VerifyDataAndDeldCommand(void) {
int Drive=-1;
DoSelects();
if (!CheckReady()) {
DoErr(0x10); /* Drive not ready */
return;
};
if (Selects[0]) Drive=0;
if (Selects[1]) Drive=1;
Internal_CurrentTrack[Drive]=Params[0];
CommandStatus.CurrentTrackPtr=GetTrackPtr(Params[0]);
if (CommandStatus.CurrentTrackPtr==NULL) {
DoErr(0x10);
return;
};
CommandStatus.CurrentSectorPtr=GetSectorPtr(CommandStatus.CurrentTrackPtr,Params[1],0);
if (CommandStatus.CurrentSectorPtr==NULL) {
DoErr(0x1e); /* Sector not found */
return;
};
StatusReg=0x80; /* Command busy */
UPDATENMISTATUS;
SetMotor(Drive,true);
SetTrigger(100,Disc8271Trigger); /* A short delay to causing an interrupt */
};
/*--------------------------------------------------------------------------*/
static void VerifyInterrupt(void) {
StatusReg=0x18; /* Result with interrupt */
UPDATENMISTATUS;
if (Selects[0])
SetMotor(0,false);
if (Selects[1])
SetMotor(1,false);
ResultReg=0; /* All OK */
};
/*--------------------------------------------------------------------------*/
static void DoFormatCommand(void) {
int Drive=-1;
DoSelects();
DoLoadHead();
if (!CheckReady()) {
DoErr(0x10); /* Drive not ready */
return;
};
if (Selects[0]) Drive=0;
if (Selects[1]) Drive=1;
if (!Writeable[Drive]) {
DoErr(0x12); /* Drive write protected */
return;
};
Internal_CurrentTrack[Drive]=Params[0];
CommandStatus.CurrentTrackPtr=GetTrackPtr(Params[0]);
if (CommandStatus.CurrentTrackPtr==NULL) {
DoErr(0x10);
return;
};
CommandStatus.CurrentSectorPtr=GetSectorPtr(CommandStatus.CurrentTrackPtr,0,0);
if (CommandStatus.CurrentSectorPtr==NULL) {
DoErr(0x1e); /* Sector not found */
return;
};
CommandStatus.TrackAddr=Params[0];
CommandStatus.CurrentSector=0;
CommandStatus.SectorsToGo=Params[2] & 31;
CommandStatus.SectorLength=1<<(7+((Params[2] >> 5) & 7));
if (CommandStatus.SectorsToGo==10 && CommandStatus.SectorLength==256) {
CommandStatus.ByteWithinSector=0;
SetMotor(Drive,true);
SetTrigger(TIMEBETWEENBYTES,Disc8271Trigger);
StatusReg=0x80; /* Command busy */
UPDATENMISTATUS;
CommandStatus.ByteWithinSector=0;
FirstWriteInt=1;
} else {
DoErr(0x1e); /* Sector not found */
};
};
/*--------------------------------------------------------------------------*/
static void FormatInterrupt(void) {
int i;
int LastByte=0;
if (CommandStatus.SectorsToGo<0) {
StatusReg=0x18; /* Result and interrupt */
UPDATENMISTATUS;
if (Selects[0])
SetMotor(0,false);
if (Selects[1])
SetMotor(1,false);
return;
};
if (!FirstWriteInt) {
/* Ignore the ID data for now - just count the bytes */
CommandStatus.ByteWithinSector++;
}
else
FirstWriteInt=0;
ResultReg=0;
if (CommandStatus.ByteWithinSector>=4) {
/* Fill sector with 0xe5 chars */
for (i=0; i<256; ++i)
CommandStatus.CurrentSectorPtr->Data[i]=(unsigned char)0xe5;
CommandStatus.ByteWithinSector=0;
if (--CommandStatus.SectorsToGo) {
CommandStatus.CurrentSector++;
CommandStatus.CurrentSectorPtr=GetSectorPtr(CommandStatus.CurrentTrackPtr,CommandStatus.CurrentSector,0);
if (CommandStatus.CurrentSectorPtr==NULL) {
DoErr(0x1e); /* Sector not found */
return;
}
} else {
/* Last sector done, write the track back to disc */
if (SaveTrackImage(Selects[0] ? 0 : 1, CURRENTHEAD, CommandStatus.TrackAddr)) {
StatusReg=0x10;
UPDATENMISTATUS;
LastByte=1;
CommandStatus.SectorsToGo=-1; /* To let us bail out */
SetTrigger(0,Disc8271Trigger); /* To pick up result */
} else {
DoErr(0x12);
}
};
};
if (!LastByte) {
StatusReg=0x8c; /* Command busy, */
UPDATENMISTATUS;
SetTrigger(TIMEBETWEENBYTES * 256,Disc8271Trigger);
};
}; /* FormatInterrupt */
/*--------------------------------------------------------------------------*/
static void DoSeekInt(void) {
StatusReg=0x18; /* Result with interrupt */
UPDATENMISTATUS;
if (Selects[0])
SetMotor(0,false);
if (Selects[1])
SetMotor(1,false);
ResultReg=0; /* All OK */
};
/*--------------------------------------------------------------------------*/
static void DoSeekCommand(void) {
int Drive=-1;
DoSelects();
DoLoadHead();
if (Selects[0]) Drive=0;
if (Selects[1]) Drive=1;
if (Drive<0) {
DoErr(0x10);
return;
};
Internal_CurrentTrack[Drive]=Params[0];
StatusReg=0x80; /* Command busy */
UPDATENMISTATUS;
SetMotor(Drive,true);
SetTrigger(100,Disc8271Trigger); /* A short delay to causing an interrupt */
};
/*--------------------------------------------------------------------------*/
static void DoReadDriveStatusCommand(void) {
bool Track0 = false;
bool WriteProt = false;
if (ThisCommand & 0x40) {
Track0=(Internal_CurrentTrack[0]==0);
WriteProt=(!Writeable[0]);
};
if (ThisCommand & 0x80) {
Track0=(Internal_CurrentTrack[1]==0);
WriteProt=(!Writeable[1]);
};
ResultReg=0x80 | (Selects[1]?0x40:0) | (Selects[0]?0x4:0) | (Track0?2:0) | (WriteProt?8:0);
StatusReg|=0x10; /* Result */
UPDATENMISTATUS;
};
/*--------------------------------------------------------------------------*/
static void DoSpecifyCommand(void) {
/* Should set stuff up here */
};
/*--------------------------------------------------------------------------*/
static void DoWriteSpecialCommand(void) {
DoSelects();
switch(Params[0]) {
case 0x06:
Internal_Scan_SectorNum=Params[1];
break;
case 0x14:
Internal_Scan_Count&=0xff;
Internal_Scan_Count|=Params[1]<<8;
break;
case 0x13:
Internal_Scan_Count&=0xff00;
Internal_Scan_Count|=Params[1];
break;
case 0x12:
Internal_CurrentTrack[0]=Params[1];
break;
case 0x1a:
Internal_CurrentTrack[1]=Params[1];
break;
case 0x17:
Internal_ModeReg=Params[1];
break;
case 0x23:
Internal_DriveControlOutputPort=Params[1];
Selects[0]=(Params[1] & 0x40)!=0;
Selects[1]=(Params[1] & 0x80)!=0;
break;
case 0x22:
Internal_DriveControlInputPort=Params[1];
break;
case 0x10:
Internal_BadTracks[0][0]=Params[1];
break;
case 0x11:
Internal_BadTracks[0][1]=Params[1];
break;
case 0x18:
Internal_BadTracks[1][0]=Params[1];
break;
case 0x19:
Internal_BadTracks[1][1]=Params[1];
break;
default:
/* cerr << "Write to bad special register\n"; */
return;
break;
}; /* Special register number switch */
}; /* DoWriteSpecialCommand */
/*--------------------------------------------------------------------------*/
static void DoReadSpecialCommand(void) {
DoSelects();
switch(Params[0]) {
case 0x06:
ResultReg=Internal_Scan_SectorNum;
break;
case 0x14:
ResultReg=(Internal_Scan_Count>>8) & 255;
break;
case 0x13:
ResultReg=Internal_Scan_Count & 255;
break;
case 0x12:
ResultReg=Internal_CurrentTrack[0];
break;
case 0x1a:
ResultReg=Internal_CurrentTrack[1];
break;
case 0x17:
ResultReg=Internal_ModeReg;
break;
case 0x23:
ResultReg=Internal_DriveControlOutputPort;
break;
case 0x22:
ResultReg=Internal_DriveControlInputPort;
break;
case 0x10:
ResultReg=Internal_BadTracks[0][0];
break;
case 0x11:
ResultReg=Internal_BadTracks[0][1];
break;
case 0x18:
ResultReg=Internal_BadTracks[1][0];
break;
case 0x19:
ResultReg=Internal_BadTracks[1][1];
break;
default:
/* cerr << "Read of bad special register\n"; */
return;
break;
}; /* Special register number switch */
StatusReg |= 16; /* Result reg full */
UPDATENMISTATUS;
};
/*--------------------------------------------------------------------------*/
static void DoBadCommand(void) {
};
/*--------------------------------------------------------------------------*/
/* The following table is used to parse commands from the command number written into
the command register - it can't distinguish between subcommands selected from the
first parameter */
static PrimaryCommandLookupType PrimaryCommandLookup[]={
{0x00, 0x3f, 3, DoVarLength_ScanDataCommand, NULL, "Scan Data (Variable Length/Multi-Record)"},
{0x04, 0x3f, 3, DoVarLength_ScanDataAndDeldCommand, NULL, "Scan Data & deleted data (Variable Length/Multi-Record)"},
{0x0a, 0x3f, 2, Do128ByteSR_WriteDataCommand, NULL, "Write Data (128 byte/single record)"},
{0x0b, 0x3f, 3, DoVarLength_WriteDataCommand, WriteInterrupt, "Write Data (Variable Length/Multi-Record)"},
{0x0e, 0x3f, 2, Do128ByteSR_WriteDeletedDataCommand, NULL, "Write Deleted Data (128 byte/single record)"},
{0x0f, 0x3f, 3, DoVarLength_WriteDeletedDataCommand, NULL, "Write Deleted Data (Variable Length/Multi-Record)"},
{0x12, 0x3f, 2, Do128ByteSR_ReadDataCommand, NULL, "Read Data (128 byte/single record)"},
{0x13, 0x3f, 3, DoVarLength_ReadDataCommand, ReadInterrupt, "Read Data (Variable Length/Multi-Record)"},
{0x16, 0x3f, 2, Do128ByteSR_ReadDataAndDeldCommand, NULL, "Read Data & deleted data (128 byte/single record)"},
{0x17, 0x3f, 3, DoVarLength_ReadDataAndDeldCommand, ReadInterrupt, "Read Data & deleted data (Variable Length/Multi-Record)"},
{0x1b, 0x3f, 3, DoReadIDCommand, ReadIDInterrupt, "ReadID" },
{0x1e, 0x3f, 2, Do128ByteSR_VerifyDataAndDeldCommand, NULL, "Verify Data and Deleted Data (128 byte/single record)"},
{0x1f, 0x3f, 3, DoVarLength_VerifyDataAndDeldCommand, VerifyInterrupt, "Verify Data and Deleted Data (Variable Length/Multi-Record)"},
{0x23, 0x3f, 5, DoFormatCommand, FormatInterrupt, "Format"},
{0x29, 0x3f, 1, DoSeekCommand, DoSeekInt, "Seek"},
{0x2c, 0x3f, 0, DoReadDriveStatusCommand, NULL, "Read drive status"},
{0x35, 0xff, 4, DoSpecifyCommand, NULL, "Specify" },
{0x3a, 0x3f, 2, DoWriteSpecialCommand, NULL, "Write special registers" },
{0x3d, 0x3f, 1, DoReadSpecialCommand, NULL, "Read special registers" },
{0, 0, 0, DoBadCommand, NULL, "Unknown command"} /* Terminator due to 0 mask matching all */
}; /* PrimaryCommandLookup */
/*--------------------------------------------------------------------------*/
/* returns a pointer to the data structure for the given command */
/* If no matching command is given, the pointer points to an entry with a 0 */
/* mask, with a sensible function to call. */
static PrimaryCommandLookupType *CommandPtrFromNumber(int CommandNumber) {
PrimaryCommandLookupType *presptr=PrimaryCommandLookup;
for(;presptr->CommandNum!=(presptr->Mask & CommandNumber);presptr++);
return(presptr);
}; /* CommandPtrFromNumber */
/*--------------------------------------------------------------------------*/
/* Address is in the range 0-7 - with the fe80 etc stripped out */
int Disc8271_read(int Address) {
int Value=0;
if (!Disc8271Enabled)
return 0xFF;
switch (Address) {
case 0:
/*cerr << "8271 Status register read (0x" << hex << int(StatusReg) << dec << ")\n"; */
Value=StatusReg;
break;
case 1:
/*cerr << "8271 Result register read (0x" << hex << int(ResultReg) << dec << ")\n"; */
StatusReg &=~0x18; /* Clear interrupt request and result reg full flag*/
UPDATENMISTATUS;
Value=ResultReg;
ResultReg=0; /* Register goes to 0 after its read */
break;
case 4:
/*cerr << "8271 data register read\n"; */
StatusReg &= ~0xc; /* Clear interrupt and non-dma request - not stated but DFS never looks at result reg!*/
UPDATENMISTATUS;
Value=DataReg;
break;