-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path8FLOPPY.C
788 lines (661 loc) · 19.6 KB
/
8FLOPPY.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
#include <stdlib.h>
#include <dos.h>
#include <mem.h>
#include <process.h>
#include "8format.h"
#include "isadma.h"
/* IRQ fired to acknowledge? */
volatile unsigned char nIRQTriggered = 0;
unsigned char nISRInstalled = 0;
unsigned char nDriveReady = 0;
/* "Current" CHS used for seeking */
unsigned char nCurrentTrack = 0;
unsigned char nCurrentHead = 0;
/* 8" floppy parameters have not yet been specified to the FDC */
unsigned char nNeedsReset = 0;
/* Sector size from bytes to 0-3 */
unsigned char ConvertSectorSize(unsigned int nSize)
{
switch(nSize)
{
case 128:
default:
return 0;
case 256:
return 1;
case 512:
return 2;
case 1024:
return 3;
}
}
/* Provide GAP and Gap3 lengths */
unsigned char GetGapLength(unsigned char nFormatting)
{
/* User-provided gap length for read/write */
if ((nFormatting == 0) && (nCustomGapLength != 0))
{
return nCustomGapLength;
}
/* User-provided GAP3 length for track format */
if ((nFormatting == 1) && (nCustomGap3Length != 0))
{
return nCustomGap3Length;
}
/* Try to autodetect. (values per NEC uPD765 datasheet, Table 3) */
switch(nPhysicalSectorSize)
{
case 128:
default:
return (nFormatting == 0) ? 7 : 0x1B;
case 256:
{
if (nUseFM == 1)
{
return (nFormatting == 0) ? 0x0E : 0x2A;
}
else
{
return (nFormatting == 0) ? 0x0E : 0x36;
}
}
case 512:
{
if (nUseFM == 1)
{
return (nFormatting == 0) ? 0x1B : 0x3A;
}
else
{
/* EXT3: return vanilla recommended value */
if (strcmp(sFormatType, "EXT3") == 0)
{
return (nFormatting == 0) ? 0x1B : 0x54;
}
else
{
/* These gaps a little lower, to cram a maximum of 16 512B sectors per track :) */
return (nFormatting == 0) ? 0x18 : 0x50;
}
}
}
case 1024:
{
if (nUseFM == 1)
{
return (nFormatting == 0) ? 0x47 : 0x8A;
}
else
{
return (nFormatting == 0) ? 0x35 : 0x74;
}
}
}
}
/* The old IRQ pointer */
void interrupt (*oldIrqISR)();
void interrupt newIrqISR()
{
/* Set triggered flag + inform the PIC to re-enable IRQs again */
asm cli
asm mov [nIRQTriggered],1
asm mov al,20h
asm out 20h, al
asm sti
}
/* Install IRQ ISR */
void InstallISR()
{
const unsigned char nINTVector = (nUseIRQ > 7) ? nUseIRQ+0x68 : nUseIRQ+8;
if (nISRInstalled != 1)
{
oldIrqISR = getvect(nINTVector);
setvect(nINTVector, newIrqISR);
nISRInstalled = 1;
}
}
void RemoveISR()
{
const unsigned char nINTVector = (nUseIRQ > 7) ? nUseIRQ+0x68 : nUseIRQ+8;
if (nISRInstalled == 1)
{
setvect(nINTVector, oldIrqISR);
}
}
/* Wait for controller response, with a 6 sec timeout */
unsigned char WaitForIRQ()
{
unsigned int nTimeout = 1200;
if (nISRInstalled != 1)
{
return 0;
}
while(nTimeout != 0)
{
if (nIRQTriggered == 1)
{
nIRQTriggered = 0;
return 1;
}
nTimeout--;
delay(5);
}
printf("\nFloppy drive controller failed to respond in time (IRQ %d timeout).\n"
"Check proper connection of the drive, disk presence, and if the FDC works okay.\n",
nUseIRQ);
Quit(EXIT_FAILURE);
return 0;
}
unsigned char FDDGetData()
{
unsigned int nTimeout = 1200;
while(nTimeout != 0)
{
/* Receive byte from FDC only if RQM=1 and direction is FDC->CPU */
if((inportb(nFDCBase + 4) & 0xC0) == 0xC0)
{
return inportb(nFDCBase + 5);
}
nTimeout--;
delay(5);
}
printf("\nFailed to get data from the floppy drive controller (RQM / DIO timeout).\n"
"Check proper connection of the drive, disk presence, and if the FDC works okay.\n");
Quit(EXIT_FAILURE);
return 0;
}
void FDDSendData(unsigned char nData)
{
unsigned int nTimeout = 1200;
while(nTimeout != 0)
{
/* Transmit a byte to FDC only if RQM=1 and direction is CPU->FDC */
if((inportb(nFDCBase + 4) & 0xC0) == 0x80)
{
outportb(nFDCBase + 5, nData);
return;
}
nTimeout--;
delay(5);
}
printf("\nFailed to send data to the floppy drive controller (RQM / DIO timeout).\n"
"Check proper connection of the drive, disk presence, and if the FDC works okay.\n");
Quit(EXIT_FAILURE);
}
void FDDSendCommand(unsigned char nCommand)
{
/* Use modifier to clear the MFM flag for specifying main commands */
(nUseFM == 1) ? FDDSendData(nCommand & 0xbf) : FDDSendData(nCommand);
}
void FDDHeadLoad()
{
/* "Motor on" (in 8" drives always on), select the drive and allow IRQ6 from controller */
if (nDriveReady != 1)
{
outportb(nFDCBase + 2, (nDriveNumber & 0x03) | (1 << (4 + nDriveNumber)) | 0x0C);
nDriveReady = 1;
/* Disable IRQ0 to prevent BIOS from turning the drive motor off automatically
Also mask IRQ1 (keyboard interrupts) */
{
unsigned char nIRQMask = inportb(0x21);
outportb(0x21, nIRQMask | 3);
}
}
}
void FDDHeadRetract()
{
if (nDriveReady == 1)
{
/* "Motor off", drive unselected */
outportb(nFDCBase + 2, (nDriveNumber & 0x03) | 0x0C);
nDriveReady = 0;
/* Enable IRQ0 & IRQ1 */
{
unsigned char nIRQMask = inportb(0x21);
outportb(0x21, nIRQMask & 0xfc);
}
}
}
void FDDSeek(unsigned char nTrack, unsigned char nHead)
{
unsigned char nIdx;
unsigned char nST0;
unsigned char nResultTrack;
/* Three retries */
for (nIdx = 0; nIdx < 3; nIdx++)
{
FDDSendCommand(0xf); /* 0xF Seek */
FDDSendData((nHead << 2) | nDriveNumber); /* Which drive and head */
FDDSendData(nTrack);
WaitForIRQ();
/* 0x8 Sense interrupt command after completion */
FDDSendCommand(8);
nST0 = FDDGetData(); /* ST0 status register byte */
nResultTrack = FDDGetData(); /* Current cylinder */
/* Seek ended successfully ? */
if(nST0 & 0x20)
{
/* UC error */
if(nST0 & 0x10)
{
continue;
}
else
{
/* Not on our track */
if (nResultTrack != nTrack)
{
continue;
}
else
{
/* Seek success */
nCurrentTrack = nTrack;
nCurrentHead = nHead;
/* A 50ms delay after each seek as a safety margin for all drives */
delay(50);
return;
}
}
}
/* Seek did not succeed */
else
{
continue;
}
}
printf("\nFailed seeking to track %u on head %u after 3 attempts.\n",
nTrack, nHead);
Quit(EXIT_FAILURE);
}
void FDDReset()
{
unsigned char nIdx;
InstallISR();
/* Controller reset */
outportb(nFDCBase + 2, 0);
delay(25);
outportb(nFDCBase + 2, 0x0c); /* IRQ allowed, motors off, no drive selected */
delay(25);
WaitForIRQ();
/* Check interrupt status 3x (a must, drive is in polling mode) */
for(nIdx = 0; nIdx < 4; nIdx++)
{
FDDSendCommand(8);
FDDGetData(); /* ST0, trashed */
FDDGetData(); /* current track, trashed */
}
/* Always set the 500kbps FDC communication data rate, with 500kHz clock timing.
Practical data rate on MFM (1 bit per 1 databit) is 500 kbps, and 250kbps on FM (2 bits per 1 databit)
This does absolutely nothing on XT without a HD-capable FDC. */
outportb(nFDCBase + 7, 0);
delay(25);
/* 0x3 Fix drive data command - load new mechanical values
Table data @ https://www.isdaman.com/alsos/hardware/fdc/floppy.htm */
nNeedsReset = 1;
FDDSendCommand(3);
/* 8 inch drives are old as the republic, so using very conservative stepper values:
8ms track-to-track step rate, 252ms head load time, 224ms unload time */
/* First byte is SRT (upper nibble) | HUT (lower nibble)
Step rate time (SRT): 8 ms (500kbps FDC comm rate: 8 << 4)
Head unload time (HUT): 224 ms (500kbps FDC comm rate: 14) */
FDDSendData(0x8e);
/* Second byte is HLT (upper 7 bits) | non-DMA mode flag (bit 0)
Head load time (HLT): 252 ms (500kbps FDC comm rate: 126 << 1)
Non-DMA mode: 0 (we use DMA) */
FDDSendData(0xfc);
}
void FDDCalibrate()
{
unsigned int nIdx;
unsigned char nST0;
unsigned char nTrack;
unsigned char nCalibrationError = 0;
FDDHeadLoad();
/* Three retries */
for (nIdx = 0; nIdx < 3; nIdx++)
{
if (nCalibrationError == 1)
{
FDDHeadRetract();
FDDReset();
FDDHeadLoad();
nCalibrationError = 0;
}
FDDSendCommand(7); /* 0x7 Recalibrate command */
FDDSendData(nDriveNumber); /* Which drive (0 to 3) */
WaitForIRQ();
/* 0x8 Sense interrupt command after completion */
FDDSendCommand(8);
nST0 = FDDGetData(); /* ST0 status register byte */
nTrack = FDDGetData(); /* Current track */
/* Seek ended successfully ? */
if(nST0 & 0x20)
{
/* UC error */
if(nST0 & 0x10)
{
nCalibrationError = 1;
continue;
}
else
{
/* Track not 0 after recal */
if (nTrack > 0)
{
nCalibrationError = 1;
continue;
}
else
{
/* Success */
return;
}
}
}
/* Seek did not succeed for whatever reason */
else
{
nCalibrationError = 1;
continue;
}
}
printf("\nFailed drive recalibration after 3 attempts.\n");
Quit(EXIT_FAILURE);
}
/* Called upon application exit */
void FDDResetBIOS()
{
/* Use BIOS INT13h to recalibrate the FDC before passing control to the OS.
This is important, as it loads BIOS defaults (from INT 1Eh) into the FDC. */
if (nNeedsReset != 1)
{
return;
}
/* If the previous operation failed, assume the FDC seized up dead! */
outportb(nFDCBase + 2, 0); /* Hard reset of the controller */
delay(25);
outportb(nFDCBase + 2, 0x0c);
/* Now use BIOS to recalibrate and load defaults to all drives (3 to 0) */
asm mov cx,4
blip:
asm xor ax,ax
asm mov dx,cx
asm dec dx
asm int 13h
asm loop blip
}
unsigned char DetectErrors(unsigned char nST0, unsigned char nST1, unsigned char nST2)
{
unsigned char nError = 0;
/* Any errors in the ST0 status register? */
if ((nST0 & 0xC0) != 0)
{
nError = 1;
printf("\n!ST0 0x%x", nST0);
/* Drive not ready error */
if ( ((nST0 & 0xC0) == 0xC0) || ((nST0 & 8) == 8) )
{
printf(" Not ready");
}
/* UC/equipment error */
if(nST0 & 0x10)
{
printf(" Drive fault");
}
}
/* Any errors in the ST1 status register ? */
if (nST1 > 0)
{
nError = 1;
printf("\n!ST1 0x%x", nST1);
if (nST1 & 0x80)
{
printf(" Sector count per track exceeded");
}
if (nST1 & 0x20)
{
printf(" Error in data");
}
if (nST1 & 0x10)
{
printf(" Timeout or data overrun");
}
if (nST1 & 4)
{
printf(" No data (sector not found)");
}
if (nST1 & 2)
{
printf("\nWrite protect error\n");
Quit(EXIT_FAILURE); /* No point of retrying the command, quit now */
}
if (nST1 & 1)
{
printf(" No address mark");
}
}
/* Errors in ST2 status register */
if (nST2 > 0)
{
nError = 1;
printf("\n!ST2 0x%x", nST2);
if (nST2 & 0x20)
{
printf(" CRC error in data field");
}
if (nST2 & 0x10)
{
printf(" Wrong track in ID address mark");
}
if (nST2 & 2)
{
printf(" Bad track or defective media");
}
if (nST2 & 1)
{
printf(" No deleted address mark");
}
}
/* Reset and recalibrate on error */
if (nError)
{
FDDHeadRetract();
FDDReset();
FDDCalibrate();
FDDSeek(nCurrentTrack, nCurrentHead);
}
return nError;
}
/* Formats a whole track on the active (seeked) track and head number */
void FDDFormat()
{
unsigned char nIdx;
/* Clear 8K DMA buffer */
memset(pDMABuffer, 0, 8*1024);
/* Three retries */
for (nIdx = 0; nIdx < 3; nIdx++)
{
unsigned char nIdx2 = 0;
unsigned char nIdx3 = 0;
unsigned char nST0;
unsigned char nST1;
unsigned char nST2;
/* Prepare 4-byte 'CHSN' format buffer for all sectors on track */
for(nIdx2 = 0; nIdx2 < nSectorsPerTrack; nIdx2++)
{
pDMABuffer[nIdx3++] = nCurrentTrack;
pDMABuffer[nIdx3++] = nCurrentHead;
pDMABuffer[nIdx3++] = nIdx2+1; /* Sector number (1-based) */
pDMABuffer[nIdx3++] = ConvertSectorSize(nPhysicalSectorSize); /* Sector size 0 to 3 */
}
/* Setup DMA */
PrepareDMABufferForTransfer(0, 4*nSectorsPerTrack);
/* Now send command */
FDDSendCommand(0x4D); /* 0x4d Format track */
FDDSendData((nCurrentHead << 2) | nDriveNumber); /* Which drive and head */
FDDSendData(ConvertSectorSize(nPhysicalSectorSize)); /* Sector size, 0 to 3 */
FDDSendData(nSectorsPerTrack); /* Last sector on track */
FDDSendData(GetGapLength(1)); /* Format GAP3 length */
FDDSendData(nFormatByte); /* Format fill byte */
WaitForIRQ();
/* Get status information */
nST0 = FDDGetData();
nST1 = FDDGetData();
nST2 = FDDGetData();
/* Track, head, sector number and size trashed */
FDDGetData();
FDDGetData();
FDDGetData();
FDDGetData();
if (DetectErrors(nST0, nST1, nST2) == 0)
{
/* No errors, success */
return;
}
/* Errors detected - next attempt */
printf("\n");
}
printf("\nFormatting failed after 3 attempts.\n");
Quit(EXIT_FAILURE);
}
/* Writes either a single sector on the active (seeked) track and head number, or whole track
Input: pre-set DMA buffer with proper size
sector number (1-based !), or 255 (0xff) for whole track */
void FDDWrite(unsigned char nSectorNo)
{
unsigned char nIdx;
/* Three write retries */
for (nIdx = 0; nIdx < 3; nIdx++)
{
unsigned char nST0;
unsigned char nST1;
unsigned char nST2;
/* Setup DMA */
PrepareDMABufferForTransfer(0, (nSectorNo != 0xff) ?
nPhysicalSectorSize :
nPhysicalSectorSize*nSectorsPerTrack);
/* Now send command */
FDDSendCommand(0x45); /* 0x45 Write sector */
FDDSendData((nCurrentHead << 2) | nDriveNumber); /* Which drive and head */
FDDSendData(nCurrentTrack); /* currently seeked track and head */
FDDSendData(nCurrentHead); /* lol redundant but needed */
FDDSendData((nSectorNo != 0xff) ? nSectorNo : 1); /* Which sector to write, or whole track from sector 1 */
FDDSendData(ConvertSectorSize(nPhysicalSectorSize)); /* Sector size, 0 to 3 */
FDDSendData((nSectorNo != 0xff) ? nSectorNo : nSectorsPerTrack); /* Write only one sector or whole track */
FDDSendData(GetGapLength(0)); /* Gap length */
FDDSendData((nPhysicalSectorSize == 128) ? (unsigned char)nPhysicalSectorSize : 0xff); /* Data transfer length */
WaitForIRQ();
/* Get status information */
nST0 = FDDGetData();
nST1 = FDDGetData();
nST2 = FDDGetData();
/* Track, head, sector number and size trashed */
FDDGetData();
FDDGetData();
FDDGetData();
FDDGetData();
if (DetectErrors(nST0, nST1, nST2) == 0)
{
/* No errors, success */
return;
}
/* Errors detected - next attempt */
printf("\n");
}
/* Format error message for single sector/whole track operation */
if (nSectorNo != 0xff)
{
printf("\nWrite operation of track %u, head %u, sector %u failed after 3 attempts.\n",
nCurrentTrack, nCurrentHead, nSectorNo);
}
else
{
printf("\nWrite operation of track %u on head %u failed after 3 attempts.\n",
nCurrentTrack, nCurrentHead);
}
Quit(EXIT_FAILURE);
}
/* To update the INT 1Eh BIOS diskette parameter table, execute 8TSR. */
void FDDWriteINT1Eh(unsigned char* pPath)
{
static unsigned char* pArguments[] = { NULL, "255", "255", "255", "255", "255", "255", "255", "255", NULL};
/* Here, just prepare the arguments to execute 8TSR just before the end in Quit() */
if (pPath == NULL)
{
/* Command line arguments:
drivenumber tracks heads sectorsize EOT RWgap DTL GAP3
All values decadic and unsigned char (0-255) */
sprintf(pArguments[1], "%u", nDriveNumber);
sprintf(pArguments[2], "%u", nTracks);
sprintf(pArguments[3], "%u", nHeads);
sprintf(pArguments[4], "%u", ConvertSectorSize(nPhysicalSectorSize));
sprintf(pArguments[5], "%u", nSectorsPerTrack);
sprintf(pArguments[6], "%u", GetGapLength(0));
sprintf(pArguments[7], "%u", (nPhysicalSectorSize == 128) ? 0x80 : 0xff);
sprintf(pArguments[8], "%u", GetGapLength(1));
nLaunch8TSR = 1;
return;
}
/* Overlay 8FORMAT with 8TSR. Called in Quit() */
pArguments[0] = pPath;
execv(pPath, pArguments);
}
/* Reads either a single sector from the active (seeked) track and head number, or whole track
Input: sector number (1-based !), or 255 (0xff) for whole track */
void FDDRead(unsigned char nSectorNo)
{
unsigned char nIdx;
/* Clear 8K DMA buffer */
memset(pDMABuffer, 0, 8*1024);
/* Three read retries */
for (nIdx = 0; nIdx < 3; nIdx++)
{
unsigned char nST0;
unsigned char nST1;
unsigned char nST2;
/* Setup DMA */
PrepareDMABufferForTransfer(1, (nSectorNo != 0xff) ?
nPhysicalSectorSize :
nPhysicalSectorSize*nSectorsPerTrack);
/* Now send command */
FDDSendCommand(0x46); /* 0x46 Read sector */
FDDSendData((nCurrentHead << 2) | nDriveNumber); /* Which drive and head */
FDDSendData(nCurrentTrack); /* currently seeked track and head */
FDDSendData(nCurrentHead); /* lol redundant but needed */
FDDSendData((nSectorNo != 0xff) ? nSectorNo : 1); /* Which sector to read, or whole track from sector 1 */
FDDSendData(ConvertSectorSize(nPhysicalSectorSize)); /* Sector size, 0 to 3 */
FDDSendData((nSectorNo != 0xff) ? nSectorNo : nSectorsPerTrack); /* Read only one sector or whole track */
FDDSendData(GetGapLength(0)); /* Gap length */
FDDSendData((nPhysicalSectorSize == 128) ? (unsigned char)nPhysicalSectorSize : 0xff); /* Data transfer length */
WaitForIRQ();
/* Get status information */
nST0 = FDDGetData();
nST1 = FDDGetData();
nST2 = FDDGetData();
/* Track, head, sector number and size trashed */
FDDGetData();
FDDGetData();
FDDGetData();
FDDGetData();
if (DetectErrors(nST0, nST1, nST2) == 0)
{
/* No errors, success */
return;
}
/* Errors detected - next attempt */
printf("\n");
}
/* Format error message for single sector/whole track operation */
if (nSectorNo != 0xff)
{
printf("\nRead of track %u, head %u, sector %u failed after 3 attempts.\n",
nCurrentTrack, nCurrentHead, nSectorNo);
}
else
{
printf("\nRead of track %u on head %u failed after 3 attempts.\n",
nCurrentTrack, nCurrentHead);
}
Quit(EXIT_FAILURE);
}