aboutsummaryrefslogtreecommitdiffstats
path: root/disk.c
blob: 1939780cea1d4cf8db50d86cb689e50cf022a4f2 (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
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
/*
 * ABC80/800 simulated disk
 */

#include "compiler.h"
#include "z80.h"
#include "abcio.h"
#include "hostfile.h"
#include "trace.h"

const char *disk_path = "abcdisk";

#define NOTTHERE 0
#define READONLY 0
#define INTERLEAVE 0

/* This is the interpretation of an "out" command */
enum out_state {
    disk_need_init,
    disk_k0,
    disk_k1,
    disk_k2,
    disk_k3,
    disk_upload,
    disk_download
};

/* Per-drive state */
struct drive_state {
    struct host_file *hf;
    char name[4];               /* Drive name */
    unsigned int sectors;
    uint8_t ilmsk, ilfac;       /* Interlacing parameters */
    bool disabled;		/* Explicitly disabled */
};

/* Per-controller state */
struct ctl_state {
    enum out_state state;
    uint8_t k[4];               /* Command bytes */
    unsigned int clustshift;
    unsigned int maxsectors;
    unsigned int c, h, s;
    unsigned int drives;        /* Number of drives present */
    bool newaddr;
    bool fmtdata_in_buf;	/* Use user-provided formatting data */
    bool trace_dump;	        /* If tracing, dump data buf after command */
    const char name[3];         /* Device type name with extra NUL */
    uint8_t ilmsk, ilfac;	/* Software interleaving parameters */
    int out_ptr;                /* Pointer within buffer for out data */
    int in_ptr;                 /* Pointer within buffer for in data */
    int error;			/* Error code */
    int notready_ctr;           /* How many times are we not ready? */
    struct drive_state drv[8];  /* Per-drive  */
    uint8_t buf[4][256];	/* 4 buffers @ 256 bytes */
};
#define NOT_READY 4		/* How many times to report not ready */

/*
 * DOSGEN depends on this value... and different DOSGEN
 * expect different values. If this value is wrong, DOSGEN
 * will spin forever on "testing sector..."
 */
#define OUT_OF_RANGE 0x21	/* Status code for an invalid sector */

static struct ctl_state mo_state = {
    .clustshift = 0,
    .maxsectors = 40 * 1 * 16,
    .c = 40, .h = 1, .s = 16,
    .fmtdata_in_buf = true,
    .ilmsk = 15,
    .ilfac = 7,
    .name = "mo"
};

static struct ctl_state mf_state = {
    .clustshift = 2,
    .maxsectors = 80 * 2 * 16,
    .c = 80, .h = 2, .s = 16,
    .name = "mf"
};

static struct ctl_state sf_state = {
    .clustshift = 2,
    .maxsectors = (77 * 2 - 1) * 26, /* Track 0, side 0 not used */
    .c = 77, .h = 2, .s = 26,
    .name = "sf"
};

static struct ctl_state hd_state = {
    .clustshift = 5,
    .newaddr = true,            /* Actually irrelevant for clustshift = 5 */
    .maxsectors = (239 * 32 - 1) * 32,     /* Maximum supported by UFD-DOS */
    .c = 238, .h = 16, .s = 64,
    .name = "hd"
};

static struct ctl_state *const sel_to_state[64] = {
    [36] = &hd_state,
    [44] = &mf_state,
    [45] = &mo_state,
    [46] = &sf_state,
};

static inline struct drive_state *cur_drv_mutable(struct ctl_state *state)
{
    return &state->drv[state->k[1] & 7];
}

static inline const struct drive_state *cur_drv(const struct ctl_state *state)
{
    return &state->drv[state->k[1] & 7];
}

static inline unsigned int cur_sector(const struct ctl_state *state)
{
    uint8_t k2 = state->k[2], k3 = state->k[3];

    if (state->newaddr)
        return (k2 << 8) + k3;
    else
        return (((k2 << 3) + (k3 >> 5)) << state->clustshift) + (k3 & 31);
}

/* Get physical sector number, after interleaving */
static inline unsigned int
virt2phys(const struct drive_state *drv, unsigned int sector)
{
    unsigned int ilmsk = drv->ilmsk;
    unsigned int ilfac = drv->ilfac;

    sector = (sector & ~ilmsk) | ((sector * ilfac) & ilmsk);
    return sector;
}

static inline unsigned int phys_sector(const struct ctl_state *state)
{
    return virt2phys(cur_drv(state), cur_sector(state));
}

static inline off_t file_pos(const struct ctl_state *state)
{
    return phys_sector(state) << 8;
}

static inline bool cur_sector_valid(const struct ctl_state *state)
{
    uint8_t k3 = state->k[3];

    if (!state->newaddr && ((k3 & 31) >> state->clustshift))
	return false;

    return cur_sector(state) < cur_drv(state)->sectors;
}

static inline bool file_pos_valid(const struct ctl_state *state)
{
    return file_pos(state) < cur_drv(state)->hf->filesize - 255;
}

static inline uint8_t *cur_buf(struct ctl_state *state)
{
    return state->buf[state->k[1] >> 6];
}

static void disk_reset_state(struct ctl_state *state)
{
    int i;

    state->state = disk_k0;
    state->error = 0;
    state->in_ptr = -1;
    state->out_ptr = 0;
    state->notready_ctr = NOT_READY;

    for (i = 0; i < 8; i++)
        flush_file(state->drv[i].hf);
}

static struct drive_state *
name_to_drive(const char *drive)
{
    int sel;
    struct ctl_state *state;
    unsigned int ndrive;

    /* All drive names are three letters long */
    if (strlen(drive) != 3)
	return NULL;

    ndrive = drive[2] - '0';
    if (ndrive > 7)
	return NULL;

    if (!memcmp("dr", drive, 2)) {
	/* DRx alias for MOx (matches "old DOS") */
	return &mo_state.drv[ndrive];
    }

    for (sel = 0; sel < 64; sel++) {
	state = sel_to_state[sel];
	if (!state)
	    continue;
	if (!memcmp(state->name, drive, 2))
	  return &state->drv[ndrive];
    }

    return NULL;		/* No such disk */
}

bool valid_drive_name(const char *drive)
{
    return name_to_drive(drive) != NULL;
}

int disk_mount(const char *drive, const char *filename)
{
    struct drive_state *drv = name_to_drive(drive);

    if (!drv)
	return -1;

    if (drv->hf)
	close_file(&drv->hf);

    if (!filename) {
	drv->disabled = true;
	return 0;
    } else {
	drv->disabled = false;
	drv->hf = open_host_file(HF_BINARY | HF_RETRY, NULL, filename, O_RDWR);
	return drv->hf ? 0 : -1;
    }
}

static void disk_init(struct ctl_state *state)
{
    int i;

    /* If any of these don't exist we simply report device not ready */
    for (i = 0; i < 8; i++) {
        struct drive_state *drv = &state->drv[i];
        unsigned int filesec;

        snprintf(drv->name, sizeof drv->name, "%-.2s%c", state->name, i + '0');

	if (!drv->disabled && !drv->hf && disk_path) {
	  /* Try open RDWR first, then RDONLY, but don't create */
	    drv->hf = open_host_file(HF_BINARY | HF_RETRY,
				     disk_path, drv->name, O_RDWR);
	}

	if (!drv->hf)
	    continue; /* File not present = drive not ready */

        state->drives++;

        /*
         * Smaller than the standard disk size?  Treat the sectors
         * beyond the end as bad.
         */
	filesec = drv->hf->filesize >> 8;
        drv->sectors = (filesec && filesec < state->maxsectors)
            ? filesec : state->maxsectors;

        /* Try to memory-map the file if it seems to be an actual file */
	if (filesec)
	  map_file(drv->hf, drv->sectors << 8);

	if (tracing(TRACE_DISK)) {
	    fprintf(tracef, "%s: initialized, sectors = %u (%u clusters), %s\n",
		    drv->name, drv->sectors, drv->sectors >> state->clustshift,
		    drv->hf->map ? "memory mapped" : "not mapped");
	}

	/* Interleaving parameters */
#if INTERLEAVE
	drv->ilfac = state->ilfac;
	drv->ilmsk = state->ilmsk;
#else
	drv->ilfac = drv->ilmsk = 0;
#endif
    }

    if (tracing(TRACE_DISK)) {
	fprintf(tracef, "%s: controller initialized with %u drives\n",
		state->name, state->drives);
    }

    disk_reset_state(state);
}

static void do_next_command(struct ctl_state *state)
{
    struct drive_state *drv = cur_drv_mutable(state);
    struct host_file *hf = drv->hf;
    uint8_t *buf = cur_buf(state);

    if (state->k[0] & 0x01) {
        /* READ SECTOR */
	if (!hf) {
	    state->error = 0x80; /* Device not ready */
	} else if (!cur_sector_valid(state)) {
	    if (tracing(TRACE_DISK)) {
		fprintf(tracef, "%s: read: disk sector out of range: %u/%u (cluster %u/%u)\n",
			drv->name, cur_sector(state), drv->sectors,
			cur_sector(state) >> state->clustshift,
			drv->sectors >> state->clustshift);
	    }
	    state->error = OUT_OF_RANGE;
	} else if (!file_pos_valid(state)) {
	    state->error = 0x08; /* CRC error(?) */
	} else {
	    if (hf->map) {
		memcpy(buf, hf->map + file_pos(state), 256);
	    } else {
		clearerr(hf->f);
		fseek(hf->f, file_pos(state), SEEK_SET);
		fread(buf, 1, 256, hf->f);
		if (ferror(hf->f))
		    state->error = 0x08; /* CRC error */
	    }
	}
        state->k[0] &= ~0x01;   /* Command done */
    }
    if (state->k[0] & 0x02) {
        /* SECTOR TO HOST */
	state->in_ptr = 0;
        state->state = disk_download;
        state->k[0] &= ~0x02;   /* Command done */
        return;
    }
    if (state->k[0] & 0x04) {
        /* SECTOR FROM HOST */
        state->state = disk_upload;
        state->out_ptr = 0;
        state->k[0] &= ~0x04;   /* Command done */
        return;
    }
    if (state->k[0] & 0x08) {
        /* WRITE SECTOR */
	if (!hf) {
	    state->error = 0x80; /* Not ready */
	} else if (!file_wrok(hf)) {
	    state->error = 0x40; /* Write protect */
	} else if (!cur_sector_valid(state)) {
	    state->error = OUT_OF_RANGE;
	    if (tracing(TRACE_DISK)) {
		fprintf(tracef, "%s: write: disk sector out of range: %u/%u (cluster %u/%u)\n",
			drv->name, cur_sector(state), drv->sectors,
			cur_sector(state) >> state->clustshift,
			drv->sectors >> state->clustshift);
	    }
	} else if (!file_pos_valid(state)) {
	    state->error = 0x08; /* CRC error(?) */
	} else {
	    if (hf->map) {
		memcpy(hf->map + file_pos(state), buf, 256);
	    } else {
		clearerr(hf->f);
		fseek(hf->f, file_pos(state), SEEK_SET);
		fwrite(buf, 1, 256, hf->f);
		if (ferror(hf->f))
		    state->error = 0x20; /* Write fault */
	    }
        }
        state->k[0] &= ~0x08;   /* Command done */
    }
    if (state->k[0] & 0x10 && state->k[1] & 0x08) {
	state->out_ptr = 0;
	/* FORMAT */
	if (!drv->hf) {
	    state->error = 0x80;	/* Not ready */
	} else if (!file_wrok(hf)) {
            state->error = 0x40;	/* Write protect */
	} else {
	    unsigned int s, c0, c1, s0, s1;
	    unsigned int cylsec = state->s * state->h;
	    uint8_t data[256];
	    unsigned int fmtsec, filesec;

	    /* Sector count produced by format */
	    fmtsec = state->maxsectors;

	    /* For non-MO-drives, this seems to be internally generated */
	    memset(data, 0x40, 256);

	    if (state->fmtdata_in_buf) {
		/*
		 * MO drives put the sector image in the buffers, for
		 * backwards compatibility and to support single density.
		 *
		 * Right before the F7 header CRC opcode is a density byte;
		 * 00 for single, and 01 for double.  The data begins after
		 * a byte of FB.
		 */
		bool single = false;
		const uint8_t *p, *ep;

		ep = state->buf[1];
		for (p = state->buf[0]+1; p < ep; p++) {
		    if (*p == 0xf7)
			single = (p[-1] == 0);
		    if (*p == 0xfb)
			break;
		}

		fmtsec >>= single;

		if (*p++ == 0xfb) { /* Data block found */
		    if (single) {
			/* Really two 128-byte sectors! */
			memcpy(data, p, 128);
			memcpy(data+128, p, 128);
		    } else {
			memcpy(data, p, 256);
		    }
		}
	    }

	    /*
	     * Adjust the size of the accessible device to the smallest
	     * of the physical file and the formatted size
	     */
	    filesec = drv->hf->filesize >> 8;
	    drv->sectors = (filesec && filesec < fmtsec) ? filesec : fmtsec;

	    /*
	     * k2 and k3 contain the first and last cylinder numbers to
	     * format, inclusively.  The last cylinder may be partial due
	     * to virtual remapping, e.g. for sf floppies.
	     */

	    c0 = state->k[2];
	    s0 = c0 * cylsec;
	    c1 = state->k[3] + 1;
	    s1 = c1 * cylsec;

	    if (tracing(TRACE_DISK)) {
		fprintf(tracef, "%s: formatting cyl %u..%u, sectors %u..%u\n",
			drv->name, c0, c1-1, s0, s1-1);
	    }

	    clearerr(hf->f);
	    state->error = 0;

	    for (s = s0; s < s1; s++) {
		unsigned int ps = virt2phys(drv, s);
		if (ps >= drv->sectors) {
		    state->error |= 0x02; /* Track 0/Lost data? */
		    break;
		} else if (hf->map) {
		    memcpy(hf->map + (ps << 8), data, 256);
		} else {
		    fseek(hf->f, ps << 8, SEEK_SET);
		    fwrite(data, 1, 256, hf->f);
		}
	    }
	    if (ferror(hf->f))
		state->error |= 0x20; /* Write fault */
	}
	state->k[1] &= ~0x08;
    }

    if (!(state->k[1] & 0x38))
	state->k[0] &= ~0x10;

    if (tracing(TRACE_DISK)) {
      if (state->trace_dump) {
	trace_dump(TRACE_DISK, drv->name, buf, 256);
	state->trace_dump = false;
      }
    }
    state->state = disk_k0;
}

static void disk_reset(uint8_t sel)
{
    struct ctl_state *state = sel_to_state[sel];

    if (state && state->state != disk_need_init)
	disk_reset_state(state);
}

static void disk_out(uint8_t sel, uint16_t port, uint8_t value)
{
    struct ctl_state *state = sel_to_state[sel];

    if (!state)
        return;                 /* Not a disk drive */

    if (tracing(TRACE_DISK)) {
	fprintf(tracef, "%s:  OUT %d/%d: %02x : ",
		state->name, sel, port, value);
	fprintf(tracef, "PC = %04X  BC = %04X  DE = %04X  HL = %04X\n",
		REG_PC, REG_BC, REG_DE, REG_HL);
    }

    if (state->state == disk_need_init)
        disk_init(state);

    if (!state->drives) {
	if (tracing(TRACE_DISK)) {
		fprintf(tracef, "%s: no drives present\n", state->name);
	}
	return;
    }

    switch (port) {
    case 0:
	state->notready_ctr = 0;

        switch (state->state) {
        case disk_k0:
        case disk_k1:
        case disk_k2:
            state->k[state->state - disk_k0] = value;
            state->state++;
            break;
        case disk_k3:
            state->k[3] = value;
            state->state = disk_k0;

            if (tracing(TRACE_DISK)) {
		uint16_t k = (state->k[1] << 8) + state->k[0];
		fprintf(tracef, "%s: cmd %02X %02X %02X %02X "
			"sect %u (phys %u, pos %"PRId64") "
			"buf %u :%s%s%s%s%s%s%s%s%s%s\n",
			cur_drv(state)->name,
			state->k[0], state->k[1], state->k[2], state->k[3],
			cur_sector(state), phys_sector(state),
			(int64_t)file_pos(state),
			k >> (6+8),
			(k & 0x01) ? " read" : "",
			(k & 0x02) ? " to_host" : "",
			(k & 0x04) ? " from_host" : "",
			(k & 0x08) ? " write" : "",
			((k & 0x0810) == 0x0810) ? " format" : "",
			((k & 0x1010) == 0x1010) ? " rdmark" : "",
			((k & 0x2010) == 0x2010) ? " ?cmd5" : "",
			(k & 0x20) ? " select_drive" : "",
			(k & 0x40) ? " motor_on" : "",
			(k & 0x80) ? " ?k7" : "");
		state->trace_dump |= (state->k[0] & 15) != 0;
	    }
	    state->error = 0;	/* Clear error */
	    do_next_command(state);
            break;
        case disk_upload:
            cur_buf(state)[state->out_ptr++] = value;
            if (state->out_ptr >= 256)
                do_next_command(state);
            break;
        case disk_download:
            break;
        case disk_need_init:
            abort();            /* Should never happen */
            break;
        }
        break;

    case 2:                    /* Start command */
    case 4:                    /* Reset */
	disk_reset_state(state);
        break;

    default:
        /* Nothing */
        break;
    }
}

static uint8_t disk_in(uint8_t sel, uint16_t port)
{
    struct ctl_state *state = sel_to_state[sel];
    uint8_t v = 0xff;

    if (!state)
        return 0xff;            /* Not a disk drive */

    if (state->state == disk_need_init)
        disk_init(state);

    if (!state->drives) {
	if (tracing(TRACE_DISK)) {
		fprintf(tracef, "%s: no drives present\n", state->name);
	}
        return 0xff;            /* No drives - controller not present */
    }

    switch (port) {
    case 0:
        if (state->in_ptr >= 0) {
	    state->notready_ctr = 0;
            v = state->buf[state->k[1] >> 6][state->in_ptr++];
            if (state->in_ptr >= 256) {
                state->in_ptr = -1;
                do_next_command(state);
            }
        } else {
            v = state->error;
        }
        break;

    case 1:
	v = (state->state == disk_k0) ? 0x80 : 0;
	v |= state->error ? 0 : 8;
	v |= state->notready_ctr ? 0 : 1;
	if (state->notready_ctr)
	    state->notready_ctr--;
	break;		       /* Primary status */
    default:
        v = 0xff;
	break;
    }

    if (tracing(TRACE_DISK)) {
        fprintf(tracef, "%s:  IN  %d/%d: %02x : error %02x ",
		state->name, sel, port, v, state->error);
        fprintf(tracef, "PC = %04X  BC = %04X  DE = %04X  HL = %04X\n",
                REG_PC, REG_BC, REG_DE, REG_HL);
    }
    return v;
}

static const struct abcbus_dev disk_dev = {
    .out = disk_out,
    .in  = disk_in,
    .reset = disk_reset,
    .portmask = 7
};

void disk_register_devices(void)
{
    unsigned int i;

    for (i = 0; i < 64; i++) {
	if (sel_to_state[i])
	    register_abcbus_dev(i, &disk_dev);
    }
}