summaryrefslogtreecommitdiff
path: root/KFloppy.HC
blob: 32f16759d7295993b4f577cae0d5fd03a21feaef (plain)
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
/*
    New, (hopefully) Less Messy Floppy Driver
    Copyright (C) 2025 Yoshi128k.
    Licensed under version 2 of the Do What The Fuck You Want To Public License
    See COPYING for details.
*/

Bool FDCIrq = FALSE;

U0 FDCDMAInit(U16 len)
{
  U64 buf_lo, buf_hi, page, cnt_lo, cnt_hi;

  buf_lo = &FDC_DMA & 0xFF;
  buf_hi = &FDC_DMA >> 8;
  page = &FDC_DMA >> 16;
  cnt_lo = (len - 1) & 0xFF;
  cnt_hi = (len - 1) >> 8;

  OutU8(0x0A, 6); // mask ch 0 and 2
  Sleep(1);
  OutU8(0x0C, -1); // reset flip flop
  Sleep(1);
  OutU8(0x04, buf_lo); // buf low byte
  Sleep(1);
  OutU8(0x04, buf_hi); // buf high byte
  Sleep(1);
  OutU8(0x0C, -1); // reset flip flop again
  Sleep(1);
  OutU8(0x05, cnt_lo); // cntr low byte
  Sleep(1);
  OutU8(0x05, cnt_hi); // cntr high byte
  Sleep(1);
  OutU8(0x81, 0); // page
  Sleep(1);
  OutU8(0x0A, 2); // unmask ch 0 and 2
  Sleep(1);
}

U0 FDCDMAPrepWrite()
{
  OutU8(0x0A, 6); // mask ch 0 and 2
  Sleep(1);
  OutU8(0x0B, 0x5A); // single xfer, addr inc, auto init, write, ch 2
  Sleep(1);
  OutU8(0x0A, 2); // unmask ch 0 and 2
  Sleep(1);
}

U0 FDCDMAPrepRead()
{
  OutU8(0x0A, 6); // mask ch 0 and 2
  Sleep(1);
  OutU8(0x0B, 0x56); // single xfer, addr inc, auto init, read, ch 2
  Sleep(1);
  OutU8(0x0A, 2); // unmask ch 0 and 2
  Sleep(1);
}

interrupt U0 FDCIrqHandler() {
  OutU8(0x20, 0x20); // EOI
  FDCIrq = TRUE;
}

U16 fdc_base = 0x03F0;

static U8 *fd_types[8] = {
  "none",
  "360kB 5.25\"",
  "1.2MB 5.25\"",
  "720kB 3.5\"",

  "1.44MB 3.5\"",
  "2.88MB 3.5\"",
  "unknown type",
  "unknown type"
};

U0 CMOSGetFloppyDrives()
{
  OutU8(0x70, 0x10);
  Sleep(1);

  U64 drives = InU8(0x71);

  AdamLog("Floppy Drive 0: %s\n", fd_types[drives >> 4]);
  AdamLog("Floppy Drive 1: %s\n", fd_types[drives & 0xF]);
}

U0 FDCSendCmd(U16 base, U8 cmd)
{
  // Send a command to the floppy controller

  // 60 sec timeout
  U64 i;
  for (i = 0; i < 600; i++) {
    Sleep(10);
    if (0x80 & InU8(base + FDC_MSR_DSR)) {
      OutU8(base + FDC_DATA, cmd);
      Sleep(1);
      return;
    }
  }
  AdamErr("FDC Command TimeOut");
}

U8 FDCReadData(U16 base)
{
  // Read data from the floppy controller

  // 60 sec timeout
  U64 i;
  for (i=0;i<600;i++) {
    Sleep(10);
    if (0x80 & InU8(base + FDC_MSR_DSR)) {
      return InU8(base + FDC_DATA);
    }
  }
  AdamErr("FDC Read TimeOut");
}

U0 FDCCheckInt(U16 base, U8 *st0, U8 *cyl)
{
  FDCSendCmd(base, FDC_SENSE_INTR);

  *st0 = FDCReadData(base);
  *cyl = FDCReadData(base);
}

I16 fdc_mtr_ticks = 0;
U8 fdc_mtr_state = FDC_MOTOR_OFF;

U0 FDCMotorOff(U16 base)
{
  OutU8(base + FDC_DOR, 0x0C);
  fdc_mtr_state = FDC_MOTOR_OFF;
}

U0 FDCMotorPwrOffTimer()
{
  // You're supposed to run this function as a separate CTask. It will exit when the motor
  // exits the "wait" state (either because it has turned off or because something needs it to be on).
  // The driver only spawns this when the motor is moved from "on" to "wait".
  while (fdc_mtr_state == FDC_MOTOR_WAIT) {
    // Sanity check for if something wants the motor on at the last second
    if (fdc_mtr_state == FDC_MOTOR_ON) break;

    Sleep(500);

    fdc_mtr_ticks -= 50;
    if (fdc_mtr_ticks <= 0) {
      FDCMotorOff(fdc_base);
    }
    Yield; // Give control back to the scheduler
  }
}

U0 FDCMotorCtrl(U16 base, Bool onoff)
{
  U64 prev_mtr_state;
  if (onoff) {
    if (!fdc_mtr_state) {
      // Turn on motor
      OutU8(base + FDC_DOR, 0x1C);
      Sleep(500); // Wait 500 ms to allow drive to spin up
    }
    fdc_mtr_state = FDC_MOTOR_ON;
  } else {
    prev_mtr_state = fdc_mtr_state;
    if (fdc_mtr_state == FDC_MOTOR_WAIT) {
      AdamLog("FDC: Motor PowerOff Already Pending\n");
    }
    fdc_mtr_ticks = 300; // 3 sec timeout before motor turns off
    fdc_mtr_state = FDC_MOTOR_WAIT;
    // Only spawn the timer task of the motor is currently on; infinitely-looping tasks peg the CPU
    if (prev_mtr_state != FDC_MOTOR_WAIT) Spawn(&FDCMotorPwrOffTimer,,"FDCMotorPwrOffTimer");
  }
}

I8 FDCRecalibrate(U16 base)
{
  FDCIrq = FALSE;

  U64 i;
  U8 st0, cyl;

  FDCMotorCtrl(base, FDC_MOTOR_ON);

  for (i=0;i<10;i++) {
    FDCSendCmd(base, FDC_RECALIBRATE);
    FDCSendCmd(base, 0);

    // Wait for an interrupt, then get the status
    while (!FDCIrq) Yield;
    FDCCheckInt(base, &st0, &cyl);
    AdamLog("ST0: %d Cyl: %d\n",st0,cyl);

    if (st0 & 0xC0) {
      static U8 * status[4] = {0, "error", "invalid", "drive"};
      AdamLog("Calibration Status: %s\n", status[st0 >> 6]);
    }

    if (!cyl) {
      FDCMotorCtrl(base, FDC_MOTOR_OFF);
      return 0;
    }
  }

  AdamErr("FDC ReCalibrate TimeOut");
  FDCMotorCtrl(base, FDC_MOTOR_OFF);
  return -1;
}

I8 FDCReset(U16 base)
{
  FDCIrq = FALSE;

  OutU8(base + FDC_DOR, 0x00);
  Sleep(1);
  OutU8(base + FDC_DOR, 0x0C);
  Sleep(1);

  while (!FDCIrq) Yield;

  // Ignore this
  U64 i, lck;
  U8 st0, cyl;
  for (i=0;i<4;i++) {
    FDCCheckInt(base, &st0, &cyl);
  }

  // Set xfer rate to 500kbps
  OutU8(base + FDC_CCR_DIR, 0x00);
  Sleep(1);

  // Set the mechanical params and disable DMA (Terry didn't use it)
  FDCSendCmd(base, FDC_SPECIFY);
  FDCSendCmd(base, 0b10000000); // SRT = 8 (8 ms), HUT = 0 (256 ms)
  FDCSendCmd(base, 0b00011110); // HLT = 15 (30 ms), NDMA = 0 (DMA enabled)

  // Configure the FIFO
  FDCSendCmd(base, FDC_CONFIGURE);
  FDCSendCmd(base, 0x00); // 1st param is a 0
  FDCSendCmd(base, 0b01011011); // 2nd param: Implied seek on, FIFO on, Drive polling disabled, threshold = 12
  FDCSendCmd(base, 0x00); // 3rd param: write precomp = 0

  // Lock the configuration
  FDCSendCmd(base, 0x94);
  lck = FDCReadData(base); // Result: lock status

  // contingency
  if (FDCRecalibrate(base)) return -1;

  return 0;
}

I8 FDCSeek(U16 base, U8 cyli, U8 head)
{
  U64 i;
  U8 st0, cyl;
  FDCIrq = FALSE;

  FDCMotorCtrl(base, FDC_MOTOR_ON);

  for (i=0;i<10;i++) {
    // Attempt to move to given cyl
    // 1: X X X X X HD D1 D0
    // 2: Cyl No
    FDCSendCmd(base, FDC_SEEK);
    FDCSendCmd(base, head<<2);
    FDCSendCmd(base, cyli);

    while (!FDCIrq) Yield;
    FDCCheckInt(base, &st0, &cyl);
    AdamLog("ST0: %d Cyl: %d\n",st0,cyl);

    if (st0 & 0xC0) {
      static U8 * status[4] = {0, "error", "invalid", "drive"};
      AdamLog("Seek Status: %s\n", status[st0 >> 6]);
    }

    if (cyl == cyli) {
      FDCMotorCtrl(base, FDC_MOTOR_OFF);
      return 0;
    }
  }

  AdamErr("FDC Seek TimeOut");
  FDCMotorCtrl(base, FDC_MOTOR_OFF);
  return -1;
}

U0 FDCRead(U16 base, U8 cyl, U8 head, U8 sect, U8 trklen)
{
  // Read data from disk (single-track)
  static U8 flags;
  flags = 0x40; // Multi-track off, MFM modulation
  static U64 cmd;
  cmd = FDC_READ_DATA | flags;
  FDCIrq = FALSE;

  U64 st0, st1, st2, rcyl, rhd, rsect, rsectsize;

  FDCMotorCtrl(base, FDC_MOTOR_ON);

  AdamLog("Cmd: %d\nParams: %d %d %d %d %d %d %d %d\n",cmd,head<<2,cyl,head,sect,2,trklen,0x1B,0xFF);

  // Prepare the DMA controller
  AdamLog("Preparing DMA\n");
  FDCDMAInit(512*(trklen - sect + 1));
  FDCDMAPrepRead();

  FDCSendCmd(base, cmd);
  FDCSendCmd(base, head << 2); // Drive 0, specified head
  FDCSendCmd(base, cyl);
  FDCSendCmd(base, head);
  FDCSendCmd(base, sect);
  FDCSendCmd(base, 2); // Sector size = 2 = 512 bytes
  FDCSendCmd(base, trklen); // Track Length/Max Sector No.
  FDCSendCmd(base, 0x1B); // GAP3 Length = 27 (3.5")
  FDCSendCmd(base, 0xFF); // Data Length (irrelevant)

  // Wait for xfer to complete
  while (!FDCIrq) Yield;

  st0 = FDCReadData(base);
  st1 = FDCReadData(base);
  st2 = FDCReadData(base);
  rcyl = FDCReadData(base);
  rhd = FDCReadData(base);
  rsect = FDCReadData(base);
  rsectsize = FDCReadData(base);

  AdamLog("Read Results:\nST0: %d\nST1: %d\nST2: %d\nCyl: %d\nHead: %d\nSect: %d\nSect Size: %d\n",st0,st1,st2,rcyl,rhd,rsect,rsectsize);

  FDCMotorCtrl(base, FDC_MOTOR_OFF);
}


U0 FDCReadMulti(U16 base, U8 cyl, U8 head, U8 sect, U8 trklen)
{
  // Read data from disk (multi-track)
  static U8 flags;
  flags = 0xC0; // Multi-track on, MFM modulation
  static U64 cmd;
  cmd = FDC_READ_DATA | flags;
  FDCIrq = FALSE;

  U64 st0, st1, st2, rcyl, rhd, rsect, rsectsize;

  FDCMotorCtrl(base, FDC_MOTOR_ON);

  AdamLog("Cmd: %d\nParams: %d %d %d %d %d %d %d %d\n",cmd,head<<2,cyl,head,sect,2,trklen,0x1B,0xFF);

  // Prepare the DMA controller
  AdamLog("Preparing DMA\n");
  FDCDMAInit(512*(trklen - sect + 1)*2);
  FDCDMAPrepRead();

  FDCSendCmd(base, cmd);
  FDCSendCmd(base, head << 2); // Drive 0, specified head
  FDCSendCmd(base, cyl);
  FDCSendCmd(base, head);
  FDCSendCmd(base, sect);
  FDCSendCmd(base, 2); // Sector size = 2 = 512 bytes
  FDCSendCmd(base, trklen); // Track Length/Max Sector No.
  FDCSendCmd(base, 0x1B); // GAP3 Length = 27 (3.5")
  FDCSendCmd(base, 0xFF); // Data Length (irrelevant)

  // Wait for the xfer to complete
  while (!FDCIrq) Yield;

  st0 = FDCReadData(base);
  st1 = FDCReadData(base);
  st2 = FDCReadData(base);
  rcyl = FDCReadData(base);
  rhd = FDCReadData(base);
  rsect = FDCReadData(base);
  rsectsize = FDCReadData(base);

  AdamLog("Read Results:\nST0: %d\nST1: %d\nST2: %d\nCyl: %d\nHead: %d\nSect: %d\nSect Size: %d\n",st0,st1,st2,rcyl,rhd,rsect,rsectsize);

  FDCMotorCtrl(base, FDC_MOTOR_OFF);
}

U0 FDCWrite(U16 base, U8 cyl, U8 head, U8 sect, U8 trklen)
{
  // Write data to disk (single-track)
  static U8 flags;
  flags = 0x40; // Multi-track off, MFM modulation
  static U64 cmd;
  cmd = FDC_WRITE_DATA | flags;
  FDCIrq = FALSE;

  U64 st0, st1, st2, rcyl, rhd, rsect, rsectsize;

  FDCMotorCtrl(base, FDC_MOTOR_ON);

  AdamLog("Cmd: %d\nParams: %d %d %d %d %d %d %d %d\n",cmd,head<<2,cyl,head,sect,2,trklen,0x1B,0xFF);

  // Prepare the DMA controller
  AdamLog("Preparing DMA\n");
  FDCDMAInit(512*(trklen - sect + 1));
  FDCDMAPrepWrite();

  FDCSendCmd(base, cmd);
  FDCSendCmd(base, head << 2); // Drive 0, specified head
  FDCSendCmd(base, cyl);
  FDCSendCmd(base, head);
  FDCSendCmd(base, sect);
  FDCSendCmd(base, 2); // Sector size = 2 = 512 bytes
  FDCSendCmd(base, trklen); // Track Length/Max Sector No.
  FDCSendCmd(base, 0x1B); // GAP3 Length = 27 (3.5")
  FDCSendCmd(base, 0xFF); // Data Length (irrelevant)

  // Wait for xfer to complete
  while (!FDCIrq) Yield;

  st0 = FDCReadData(base);
  st1 = FDCReadData(base);
  st2 = FDCReadData(base);
  rcyl = FDCReadData(base);
  rhd = FDCReadData(base);
  rsect = FDCReadData(base);
  rsectsize = FDCReadData(base);

  AdamLog("Read Results:\nST0: %d\nST1: %d\nST2: %d\nCyl: %d\nHead: %d\nSect: %d\nSect Size: %d\n",st0,st1,st2,rcyl,rhd,rsect,rsectsize);

  FDCMotorCtrl(base, FDC_MOTOR_OFF);
}


U0 FDCWriteMulti(U16 base, U8 cyl, U8 head, U8 sect, U8 trklen)
{
  // Write data to disk (multi-track)
  static U8 flags;
  flags = 0xC0; // Multi-track on, MFM modulation
  static U64 cmd;
  cmd = FDC_WRITE_DATA | flags;
  FDCIrq = FALSE;

  U64 st0, st1, st2, rcyl, rhd, rsect, rsectsize;

  FDCMotorCtrl(base, FDC_MOTOR_ON);

  AdamLog("Cmd: %d\nParams: %d %d %d %d %d %d %d %d\n",cmd,head<<2,cyl,head,sect,2,trklen,0x1B,0xFF);

  // Prepare the DMA controller
  AdamLog("Preparing DMA\n");
  FDCDMAInit(512*(trklen - sect + 1)*2);
  FDCDMAPrepRead();

  FDCSendCmd(base, cmd);
  FDCSendCmd(base, head << 2); // Drive 0, specified head
  FDCSendCmd(base, cyl);
  FDCSendCmd(base, head);
  FDCSendCmd(base, sect);
  FDCSendCmd(base, 2); // Sector size = 2 = 512 bytes
  FDCSendCmd(base, trklen); // Track Length/Max Sector No.
  FDCSendCmd(base, 0x1B); // GAP3 Length = 27 (3.5")
  FDCSendCmd(base, 0xFF); // Data Length (irrelevant)

  // Wait for the xfer to complete
  while (!FDCIrq) Yield;

  st0 = FDCReadData(base);
  st1 = FDCReadData(base);
  st2 = FDCReadData(base);
  rcyl = FDCReadData(base);
  rhd = FDCReadData(base);
  rsect = FDCReadData(base);
  rsectsize = FDCReadData(base);

  AdamLog("Read Results:\nST0: %d\nST1: %d\nST2: %d\nCyl: %d\nHead: %d\nSect: %d\nSect Size: %d\n",st0,st1,st2,rcyl,rhd,rsect,rsectsize);

  FDCMotorCtrl(base, FDC_MOTOR_OFF);
}