-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathi7000_dsk.c
1885 lines (1723 loc) · 67.4 KB
/
i7000_dsk.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
/* i7090_disk.c: IBM 7090 Disk
Copyright (c) 2005-2016, Richard Cornwell
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
RICHARD CORNRWELL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Support for 1301/1302/2302 disks and 7238 drums
Disks are represented in files as follows:
Since these drives supported variable format for each cylinder the
format is represented as one track per cylinder as follows:
0 data
1 header
2 Home Address
3 end of track
These codes are packed 4 per byte and used to control read/write of
data.
After one format track per cylinder there is one record of bytes per
track data for each track. First bytes are home address 2, followed
by record address, and record data to cover number in format. All data
is stored with the top 2 bits as zero.
Limitiation of this are that the address field for each record can not
be more then 16 bytes.
*/
#include "i7000_defs.h"
#ifdef NUM_DEVS_DSK
#define UNIT_DSK UNIT_ATTABLE | UNIT_DISABLE | UNIT_FIX
#define FORMAT_OK (1 << (UNIT_V_LOCAL+0))
#define HA2_OK (1 << (UNIT_V_LOCAL+1))
#define CTSS_BOOT (1 << (UNIT_V_MODE))
/* Device status information stored in u5 */
#define DSKSTA_CMD 0x0000100 /* Unit has recieved a cmd */
#define DSKSTA_DATA 0x0000200 /* Unit has finished cmd */
#define DSKSTA_WRITE 0x0000400 /* Last command was a write */
#define DSKSTA_CHECK 0x0000800 /* Doing a write check */
#define DSKSTA_CMSK 0x00000ff /* Command mask */
#define DSKSTA_ARGMSK 0x0fff000 /* Command argument */
#define DSKSTA_ARGSHFT 12
#define DSKSTA_SCAN 0x1000000 /* Scanning for header */
#define DSKSTA_SKIP 0x2000000 /* Skiping record */
#define DSKSTA_XFER 0x4000000 /* Tranfser current record */
#define DSKSTA_DIRTY 0x8000000 /* Buffer needs to be written */
#define FMT_DATA 0 /* Data */
#define FMT_HDR 1 /* Header */
#define FMT_HA2 2 /* Home address 2 */
#define FMT_END 3 /* End of track */
/* Disk commands */
#define DNOP 0x00 /* Nop */
#define DREL 0x04 /* Release */
#define DEBM 0x08 /* Eight Bit mode */
#define DSBM 0x09 /* Six bit mode */
#define DSEK 0x80 /* Seek */
#define DVSR 0x82 /* Prepare to Verify single record */
#define DWRF 0x83 /* Prepare to Format */
#define DVTN 0x84 /* Prepare to Verify track no addr */
#define DVCY 0x85 /* Prepare to Verify Cyl */
#define DWRC 0x86 /* Prepare to Write Check */
#define DSAI 0x87 /* Set Access Inoperative */
#define DVTA 0x88 /* Prepare to Verify track addr */
#define DVHA 0x89 /* Prepare to Verify home addr */
/* Disk sense codes */ /*01234 */
#define STAT_SIXBIT 0x00004 /* Disk in 6bit more. */
#define EXPT_FILECHK 0x10010 /* File control check error */
#define EXPT_DSKCHK 0x10020 /* Disk storage error */
#define STAT_NOTRDY 0x10040 /* Disk no ready */
#define STAT_OFFLINE 0x10080 /* Disk offline */
#define DATA_PARITY 0x20100 /* Data parity error */
#define DATA_CHECK 0x20200 /* Compare error */
#define DATA_RESPONSE 0x20400 /* Response check */
#define PROG_INVADDR 0x40800 /* Invalid seek address */
#define PROG_NOREC 0x41000 /* No record found */
#define PROG_FMTCHK 0x42000 /* Format check */
#define PROG_INVCODE 0x44000 /* Invalid code */
#define PROG_INVSEQ 0x48000 /* Invalid sequence */
#define MAXTRACK 6020 /* Max size per track */
uint32 dsk_cmd(UNIT *, uint16, uint16);
t_stat dsk_srv(UNIT *);
t_stat dsk_boot(int32, DEVICE *);
void dsk_ini(UNIT *, t_bool);
t_stat dsk_reset(DEVICE *);
t_stat dsk_set_module(UNIT * uptr, int32 val, CONST char *cptr,
void *desc);
t_stat dsk_get_module(FILE * st, UNIT * uptr, int32 v,
CONST void *desc);
t_stat dsk_set_type(UNIT * uptr, int32 val, CONST char *cptr,
void *desc);
t_stat dsk_get_type(FILE * st, UNIT * uptr, int32 v,
CONST void *desc);
t_stat dsk_help (FILE *st, DEVICE *dptr, UNIT *uptr, int32 flag,
const char *cptr);
const char *dsk_description (DEVICE *dptr);
int disk_rblock(UNIT * uptr, int track);
int disk_wblock(UNIT * uptr);
void disk_posterr(UNIT * uptr, uint32 error);
void disk_cmderr(UNIT * uptr, uint32 error);
int disk_cmd(UNIT * uptr);
int disk_write(UNIT * uptr, uint8 data, int chan,
int eor);
int disk_read(UNIT * uptr, uint8 * data, int chan);
int disk_format(UNIT * uptr, FILE * f, int cyl,
UNIT * base);
int bcd_to_track(uint32 addr);
/* Data buffer for track */
uint8 dbuffer[NUM_DEVS_DSK * 4][MAXTRACK];
/* Format buffer for cylinder */
uint8 fbuffer[NUM_DEVS_DSK * 4][MAXTRACK / 4];
/* Currently loaded format record */
uint16 fmt_cyl[NUM_DEVS_DSK * 4];
/* Currently read in track in buffer */
uint16 dtrack[NUM_DEVS_DSK * 4];
/* Arm position */
uint16 arm_cyl[NUM_DEVS_DSK * 4];
uint32 sense[NUM_CHAN * 2];
uint32 sense_unit[NUM_CHAN * 2];
uint8 cmd_buffer[NUM_CHAN]; /* Command buffer per channel */
uint8 cmd_mod[NUM_CHAN]; /* Command module per channel */
uint32 cmd_option[NUM_CHAN]; /* Command option per channel */
uint16 cmd_count[NUM_CHAN]; /* Number of chars recieved */
#ifdef I7010
extern uint8 chan_seek_done[NUM_CHAN]; /* Seek finished flag */
extern uint8 chan_io_status[NUM_CHAN]; /* Channel status flags */
#endif
/* Macro to help build the disk size table */
#define DISK_DEF(name, cyl, cylpertrk, acc, charpertrk, overhd, mod, dr) \
{ name, cyl, cylpertrk, ((charpertrk/128) + 1) * 128, \
acc, (((charpertrk/128) + 1) * 128)/4, \
acc * cyl * ((((charpertrk/128) + 1) * 128)/4), \
overhd, mod, dr }
struct disk_t
{
const char *name; /* Type Name */
int cyl; /* Number of cylinders */
int track; /* Number of tracks/cylinder */
unsigned int bpt; /* Max bytes per track */
int arms; /* Number of access arms */
int fbpt; /* Number of format bytes per track */
int fmtsz; /* Format size */
int overhd; /* Number of characters overhead on HA/RA */
int mods; /* Number of modules */
int datarate; /* us per chars */
}
disk_type[] =
{
DISK_DEF("1301", 254, 40, 1, 2880, 4, 1, 15),
DISK_DEF("1301-2", 254, 40, 1, 2880, 4, 2, 15),
DISK_DEF("1302", 254, 40, 2, 5940, 7, 1, 10),
DISK_DEF("1302-2", 254, 40, 2, 5940, 7, 2, 10),
DISK_DEF("2302", 254, 40, 2, 5940, 7, 2, 10),
DISK_DEF("7238", 1, 404, 1, 3270, 4, 1, 10),
DISK_DEF("7238-2", 1, 404, 1, 3270, 4, 2, 10), {
NULL, 0}
};
int unit_bit[] = {
/*0 1 2 3 4 5 6 7 8 9 A B C D E F */
19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 30, 30, 30, 30, 30, 30,
9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 30, 30, 30, 30, 30, 30
};
#define DSKSTA_BSY 0x02 /* Controller busy. */
#define DSKSTA_EIGHT 0x04 /* Controller in 8 bit mode */
#ifdef I7090
#define CH1 4
#define CH2 6
#endif
#ifdef I7070
#define CH1 5
#define CH2 6
#endif
#ifdef I7080
#define CH1 5
#define CH2 6
#endif
#ifndef CH1
#define CH1 1
#endif
#ifndef CH2
#define CH2 2
#endif
UNIT dsk_unit[] = {
{UDATA(&dsk_srv, UNIT_S_CHAN(CH1) | UNIT_DSK, 0), 0, 0x000, 0},
{UDATA(&dsk_srv, UNIT_S_CHAN(CH1) | UNIT_DSK, 0), 0, 0x102, 0},
{UDATA(&dsk_srv, UNIT_S_CHAN(CH1) | UNIT_DSK, 0), 0, 0x204, 0},
{UDATA(&dsk_srv, UNIT_S_CHAN(CH1) | UNIT_DSK, 0), 0, 0x306, 0},
{UDATA(&dsk_srv, UNIT_S_CHAN(CH1) | UNIT_DSK, 0), 0, 0x408, 0},
{UDATA(&dsk_srv, UNIT_S_CHAN(CH2) | UNIT_DSK, 0), 0, 0x500, 0},
{UDATA(&dsk_srv, UNIT_S_CHAN(CH2) | UNIT_DSK, 0), 0, 0x602, 0},
{UDATA(&dsk_srv, UNIT_S_CHAN(CH2) | UNIT_DSK, 0), 0, 0x704, 0},
{UDATA(&dsk_srv, UNIT_S_CHAN(CH2) | UNIT_DSK, 0), 0, 0x806, 0},
{UDATA(&dsk_srv, UNIT_S_CHAN(CH2) | UNIT_DSK, 0), 0, 0x908, 0},
/* Second set for arm two of each unit */
{UDATA(&dsk_srv, UNIT_DIS, 0), 0, 0xff, 0},
{UDATA(&dsk_srv, UNIT_DIS, 0), 0, 0xff, 0},
{UDATA(&dsk_srv, UNIT_DIS, 0), 0, 0xff, 0},
{UDATA(&dsk_srv, UNIT_DIS, 0), 0, 0xff, 0},
{UDATA(&dsk_srv, UNIT_DIS, 0), 0, 0xff, 0},
{UDATA(&dsk_srv, UNIT_DIS, 0), 0, 0xff, 0},
{UDATA(&dsk_srv, UNIT_DIS, 0), 0, 0xff, 0},
{UDATA(&dsk_srv, UNIT_DIS, 0), 0, 0xff, 0},
{UDATA(&dsk_srv, UNIT_DIS, 0), 0, 0xff, 0},
{UDATA(&dsk_srv, UNIT_DIS, 0), 0, 0xff, 0},
/* Third set for arm one of second module */
{UDATA(&dsk_srv, UNIT_DIS, 0), 0, 0xff, 0},
{UDATA(&dsk_srv, UNIT_DIS, 0), 0, 0xff, 0},
{UDATA(&dsk_srv, UNIT_DIS, 0), 0, 0xff, 0},
{UDATA(&dsk_srv, UNIT_DIS, 0), 0, 0xff, 0},
{UDATA(&dsk_srv, UNIT_DIS, 0), 0, 0xff, 0},
{UDATA(&dsk_srv, UNIT_DIS, 0), 0, 0xff, 0},
{UDATA(&dsk_srv, UNIT_DIS, 0), 0, 0xff, 0},
{UDATA(&dsk_srv, UNIT_DIS, 0), 0, 0xff, 0},
{UDATA(&dsk_srv, UNIT_DIS, 0), 0, 0xff, 0},
{UDATA(&dsk_srv, UNIT_DIS, 0), 0, 0xff, 0},
/* Fourth set for arm two of second module */
{UDATA(&dsk_srv, UNIT_DIS, 0), 0, 0xff, 0},
{UDATA(&dsk_srv, UNIT_DIS, 0), 0, 0xff, 0},
{UDATA(&dsk_srv, UNIT_DIS, 0), 0, 0xff, 0},
{UDATA(&dsk_srv, UNIT_DIS, 0), 0, 0xff, 0},
{UDATA(&dsk_srv, UNIT_DIS, 0), 0, 0xff, 0},
{UDATA(&dsk_srv, UNIT_DIS, 0), 0, 0xff, 0},
{UDATA(&dsk_srv, UNIT_DIS, 0), 0, 0xff, 0},
{UDATA(&dsk_srv, UNIT_DIS, 0), 0, 0xff, 0},
{UDATA(&dsk_srv, UNIT_DIS, 0), 0, 0xff, 0},
{UDATA(&dsk_srv, UNIT_DIS, 0), 0, 0xff, 0},
};
MTAB dsk_mod[] = {
{FORMAT_OK, 0, 0, "NOFORMAT", NULL, NULL, NULL, "Format not allowed"},
{FORMAT_OK, FORMAT_OK, "FORMAT", "FORMAT", NULL, NULL, NULL,
"Format allowed"},
{HA2_OK, 0, 0, "NOHA2", NULL, NULL, NULL, "No writing of Home Address"},
{HA2_OK, HA2_OK, "HA2", "HA2", NULL, NULL, NULL,
"Allow writing of Home Address"},
#ifdef I7090
{CTSS_BOOT, 0, 0, "IBSYS", NULL, NULL, NULL, "IBSYS Boot Card"},
{CTSS_BOOT, CTSS_BOOT, "CTSS", "CTSS", NULL, NULL, NULL, "CTSS Boot Card"},
#endif
{MTAB_XTD | MTAB_VUN | MTAB_VALR, 0, "TYPE", "TYPE",
&dsk_set_type, &dsk_get_type, NULL, "Type of disk"},
{MTAB_XTD | MTAB_VUN | MTAB_VALR, 0, "MODULE", "MODULE",
&dsk_set_module, &dsk_get_module, NULL, "Module number"},
{MTAB_XTD | MTAB_VUN | MTAB_VALR, 0, "CHAN", "CHAN",
&set_chan, &get_chan, NULL, "Channel number"},
#ifndef I7010
{MTAB_XTD | MTAB_VUN | MTAB_VALR, 0, "SELECT", "SELECT",
&chan9_set_select, &chan9_get_select, NULL, "Unit select"},
#endif
{0}
};
DEVICE dsk_dev = {
"DK", dsk_unit, NULL /* Registers */ , dsk_mod,
NUM_DEVS_DSK, 8, 15, 1, 8, 8,
NULL, NULL, &dsk_reset, &dsk_boot, NULL, NULL,
&dsk_dib, DEV_DISABLE | DEV_DEBUG, 0, dev_debug,
NULL, NULL, &dsk_help, NULL, NULL, &dsk_description
};
uint32 dsk_cmd(UNIT * uptr, uint16 cmd, uint16 dev)
{
int u = (uptr->u3 >> 8) & 0xf;
int chan = UNIT_G_CHAN(dsk_unit[u].flags);
#ifdef I7010
int sel;
sel = (dsk_unit[u].flags & UNIT_SELECT) ? 1 : 0;
if (cmd & 0x100)
sense[(chan * 2) + sel] |= STAT_SIXBIT;
else
sense[(chan * 2) + sel] &= ~STAT_SIXBIT;
cmd_buffer[chan] = cmd & 0xff;
cmd_count[chan] = 2;
sim_debug(DEBUG_CHAN, &dsk_dev, "unit %d = cmd=%02x\n\r",
dev, cmd & 0xff);
#else
cmd_buffer[chan] = 0;
cmd_count[chan] = 0;
sim_debug(DEBUG_CHAN, &dsk_dev, "unit=%d cmd\n\r", dev);
#endif
cmd_option[chan] = 0;
cmd_mod[chan] = 0;
chan_clear(chan, DEV_SEL);
/* If device is not active start it working */
if (!sim_is_active(uptr))
sim_activate(uptr, us_to_ticks(50));
return SCPE_OK;
}
t_stat dsk_srv(UNIT * uptr)
{
int chan;
int sel;
int schan;
int dev = uptr->u3 & 0xff;
int u = (uptr->u3 >> 8) & 0xf;
struct disk_t *dsk = &disk_type[uptr->u4];
UNIT *base = &dsk_unit[u];
uint8 ch = 0;
int eor = 0;
chan = UNIT_G_CHAN(base->flags);
sel = (base->flags & UNIT_SELECT) ? 1 : 0;
schan = (chan * 2) + sel;
/* Make sure channel is talking to us */
if (sel != chan_test(chan, CTL_SEL))
goto seeking;
/* Channel has disconnected, abort current operation. */
if (chan_test(chan, DEV_DISCO)) {
int i;
/* Scan all units and stop them if they are on this channel */
for (i = 0; i < NUM_DEVS_DSK; i++) {
int xchan = UNIT_G_CHAN(dsk_unit[i].flags);
int j;
if (xchan != chan)
continue;
for (j = 3 * NUM_DEVS_DSK + i; j >= 0; j -= NUM_DEVS_DSK) {
disk_wblock(&dsk_unit[j]); /* Flush block */
if (dsk_unit[j].u5 & DSKSTA_CMD) {
dsk_unit[j].u5 &=
~(DSKSTA_CMD | DSKSTA_XFER | DSKSTA_SCAN | DSKSTA_DATA);
sim_cancel(&dsk_unit[j]);
}
}
}
chan_clear(chan, (DEV_DISCO | DEV_WEOR | DEV_SEL));
sim_debug(DEBUG_CHAN, &dsk_dev, "unit=%d disconnecting\n\r", dev);
if (uptr->wait > 0)
sim_activate(uptr, us_to_ticks(100));
return SCPE_OK;
}
/* Call channel proccess to make sure all date is available. */
chan_proc();
/* Handle sending sense data */
if (chan_test(chan, CTL_SNS)) {
chan9_clear_error(chan, sel);
switch(cmd_count[chan]) {
case 0:
sim_debug(DEBUG_SNS, &dsk_dev, "unit=%d chan sense=%05x\n", dev,
sense[schan]);
/* fall through */
case 1: case 2: case 3: case 4:
ch = (sense[schan] >> (4 * (4 - cmd_count[chan]))) & 0xF;
break;
case 9:
sim_debug(DEBUG_SNS, &dsk_dev, "unit=%d unit sense=%08x\n", dev,
sense_unit[schan]);
eor = DEV_REOR;
/* fall through */
case 5: case 6: case 7: case 8:
ch = (sense_unit[schan] >> (4 * (9 - cmd_count[chan]))) & 0xF;
break;
}
if (ch & 010)
ch ^= 030;
sim_debug(DEBUG_SNS, &dsk_dev, "unit=%d sense %d %02o\n\r", dev,
cmd_count[chan], ch);
cmd_count[chan]++;
switch(chan_write_char(chan, &ch, eor)) {
case DATA_OK:
if (eor == 0)
break;
case TIME_ERROR:
case END_RECORD:
sense[chan] &= STAT_SIXBIT;
sense_unit[chan] = 0;
chan_set(chan, CTL_END);
chan_clear(chan, DEV_SEL);
sim_debug(DEBUG_CHAN, &dsk_dev, "unit=%d sense eor\n\r", dev);
break;
}
sim_activate(uptr, us_to_ticks(20));
return SCPE_OK;
}
if (chan_test(chan, CTL_CNTL) && disk_cmd(uptr)) {
sim_activate(uptr, us_to_ticks(50));
return SCPE_OK;
}
/* Handle writing of data */
if (chan_test(chan, CTL_WRITE) && uptr->u5 & DSKSTA_CMD) {
/* Channel asking for end of record? */
if (chan_stat(chan, DEV_WEOR)) {
sim_debug(DEBUG_CHAN, &dsk_dev, "Disk chan %d -> weor\n\r", chan);
while (disk_write(uptr, 0x40, chan, 1) == 0) ;
disk_wblock(uptr); /* Flush block */
uptr->u5 &= ~(DSKSTA_SCAN | DSKSTA_XFER);
uptr->u5 &= ~(DSKSTA_SCAN | DSKSTA_XFER);
chan_set(chan, CTL_END|DEV_REOR);
sim_activate(uptr, us_to_ticks(100));
return SCPE_OK;
}
uptr->u5 |= DSKSTA_WRITE; /* Flag as write */
switch(chan_read_char(chan, &ch, 0)) {
case TIME_ERROR:
disk_posterr(uptr, DATA_RESPONSE);
sim_activate(uptr, us_to_ticks(100));
return SCPE_OK;
case END_RECORD:
/* If last word, fill with zeros until we get eor */
sim_debug(DEBUG_CHAN, &dsk_dev, "Disk chan %d eor\n\r",chan);
uptr->u5 |= DSKSTA_DATA;
while (disk_write(uptr, 0x40, chan, 1) == 0) ;
disk_wblock(uptr); /* Flush block */
uptr->u5 &= ~(DSKSTA_SCAN | DSKSTA_XFER);
chan_set(chan, CTL_END);
sim_activate(uptr, us_to_ticks(100));
break;
case DATA_OK:
eor = disk_write(uptr, ch, chan, 0);
uptr->u5 |= DSKSTA_DATA;
if (eor == 1) {
/* Handle end of record */
sim_debug(DEBUG_CHAN, &dsk_dev,
"Disk chan %d end of track\n\r", chan);
if ((uptr->u5 & DSKSTA_CMSK) == DVSR &&
(uptr->u5 & DSKSTA_XFER) == 0)
disk_posterr(uptr, PROG_NOREC);
uptr->u5 &= ~(DSKSTA_SCAN | DSKSTA_XFER);
chan_set(chan, DEV_REOR|CTL_END);
}
sim_activate(uptr, us_to_ticks(dsk->datarate));
return SCPE_OK;
}
}
/* Handle reading of data */
if (chan_test(chan, CTL_READ) && uptr->u5 & DSKSTA_CMD) {
/* Channel asking for end of record? */
if (chan_stat(chan, DEV_WEOR)) {
sim_debug(DEBUG_CHAN, &dsk_dev, "Disk chan %d -> weor\n\r", chan);
uptr->u5 &= ~(DSKSTA_SCAN | DSKSTA_XFER);
chan_set(chan, CTL_END|DEV_REOR);
sim_activate(uptr, us_to_ticks(100));
return SCPE_OK;
}
eor = disk_read(uptr, &ch, chan);
/* Check if we got error during read */
if (eor == -1) {
sim_activate(uptr, us_to_ticks(100));
return SCPE_OK;
}
switch(chan_write_char(chan, &ch, (eor)?DEV_REOR:0)) {
case TIME_ERROR:
/* Flag as timming error */
disk_posterr(uptr, DATA_RESPONSE);
break;
case END_RECORD:
/* Fill the buffer until end of record */
sim_debug(DEBUG_CHAN, &dsk_dev, "Disk chan %d eor\n\r", chan);
if ((uptr->u5 & DSKSTA_CMSK) == DVSR &&
(uptr->u5 & DSKSTA_XFER) == 0)
disk_posterr(uptr, PROG_NOREC);
uptr->u5 &= ~(DSKSTA_SCAN | DSKSTA_XFER);
chan_set(chan, CTL_END);
uptr->u5 |= DSKSTA_DATA;
break;
case DATA_OK:
uptr->u5 |= DSKSTA_DATA;
break;
}
sim_activate(uptr, us_to_ticks(dsk->datarate));
return SCPE_OK;
}
/* Handle read/write without a command */
if (chan_test(chan, CTL_WRITE|CTL_READ) &&
(uptr->u3 & 0xff) == cmd_mod[chan] &&
(uptr->u5 & (DSKSTA_DATA|DSKSTA_CMD)) == 0)
disk_posterr(uptr, PROG_INVSEQ);
seeking:
if (uptr->wait || uptr->u5 & DSKSTA_CMD)
sim_activate(uptr, us_to_ticks(100));
/* Handle seeking */
if (uptr->wait > 0) {
uptr->wait--;
if (uptr->wait == 0) {
sim_debug(DEBUG_EXP, &dsk_dev, "Seek done dev=%d\n", dev);
sense_unit[schan] |= 1 << unit_bit[dev];
#ifdef I7010
chan_seek_done[chan] = 1;
#else
chan9_set_attn(chan, sel);
#endif
}
}
return SCPE_OK;
}
/* Post a error on a given unit. */
void
disk_posterr(UNIT * uptr, uint32 error)
{
int chan;
int schan;
int sel;
int u = (uptr->u3 >> 8) & 0xf;
uptr = &dsk_unit[u];
uptr->u5 &= ~(DSKSTA_CMD | DSKSTA_XFER | DSKSTA_SCAN);
chan = UNIT_G_CHAN(uptr->flags);
sel = (uptr->flags & UNIT_SELECT) ? 1 : 0;
schan = (chan * 2) + sel;
sense[schan] |= error;
if (error != 0)
chan9_set_error(chan, SNS_UEND);
chan_set(chan, DEV_REOR|CTL_END);
sim_debug(DEBUG_DETAIL, &dsk_dev, "post err dev=%d err=%08x\n", u,error);
#ifdef I7010
if ((error & STAT_OFFLINE) == STAT_OFFLINE)
chan_io_status[chan] |= 01;
if ((error & STAT_NOTRDY) == STAT_NOTRDY)
chan_io_status[chan] |= 02;
if (error & (EXPT_FILECHK|EXPT_DSKCHK|DATA_PARITY|DATA_CHECK|
DATA_RESPONSE|PROG_INVADDR) & 0xFFFF)
chan_io_status[chan] |= 04;
if (error & (PROG_NOREC|PROG_FMTCHK|PROG_INVCODE|PROG_INVSEQ) & 0xffff)
chan_io_status[chan] |= 010;
#endif
}
/* Post error for command that could not be completed */
void
disk_cmderr(UNIT * uptr, uint32 error)
{
int chan;
int schan;
int sel;
int u = (uptr->u3 >> 8) & 0xf;
uptr = &dsk_unit[u];
uptr->u5 &= ~(DSKSTA_CMSK | DSKSTA_CMD | DSKSTA_CHECK | DSKSTA_WRITE);
chan = UNIT_G_CHAN(uptr->flags);
sel = (uptr->flags & UNIT_SELECT) ? 1 : 0;
schan = (chan * 2) + sel;
sense[schan] |= error;
if (error != 0)
chan9_set_error(chan, SNS_UEND);
chan_set(chan, DEV_REOR|CTL_END);
sim_debug(DEBUG_DETAIL, &dsk_dev, "cmd err dev=%d err=%08x\n", u,error);
#ifdef I7010
if ((error & STAT_OFFLINE) == STAT_OFFLINE)
chan_io_status[chan] |= 01;
if ((error & STAT_NOTRDY) == STAT_NOTRDY)
chan_io_status[chan] |= 02;
if (error & (EXPT_FILECHK|EXPT_DSKCHK|DATA_PARITY|DATA_CHECK|
DATA_RESPONSE|PROG_INVADDR) & 0xFFFF)
chan_io_status[chan] |= 04;
if (error & (PROG_NOREC|PROG_FMTCHK|PROG_INVCODE|PROG_INVSEQ) & 0xffff)
chan_io_status[chan] |= 010;
#endif
}
/* Process command */
int
disk_cmd(UNIT * uptr)
{
uint8 ch;
UNIT *base;
UNIT *up;
int chan;
int schan;
int sel;
int i;
int u;
int t;
int trk;
int cyl;
/* Get information on unit */
u = uptr - dsk_unit;
base = &dsk_unit[(uptr->u3 >> 8) & 0xf];
chan = UNIT_G_CHAN(base->flags);
sel = (base->flags & UNIT_SELECT) ? 1 : 0;
schan = (chan * 2) + sel;
/* Check if we have a command yet */
switch(chan_read_char(chan, &ch, 0)) {
case TIME_ERROR:
disk_cmderr(uptr, DATA_RESPONSE);
case END_RECORD:
return 0;
case DATA_OK:
sim_debug(DEBUG_DATA, &dsk_dev, "unit=%d data=%02o\n", u, ch);
break;
}
/* Place in command buffer */
switch(cmd_count[chan]) {
case 0:
case 1:
if (ch != 012)
cmd_buffer[chan] |= (ch & 0xf) << (4 * (1 - cmd_count[chan]));
break;
case 2:
case 3:
if (ch != 012)
cmd_mod[chan] |= (ch & 0xf) << (4 * (3 - cmd_count[chan]));
break;
case 4:
case 5:
case 6:
case 7:
if (ch != 012)
cmd_option[chan] |= (ch & 0xf) << (16 + (4 * (7 - cmd_count[chan])));
break;
case 8:
case 9:
cmd_option[chan] |= (ch & 0x3f) << (6 * (9 - cmd_count[chan]));
break;
}
/* Need at least two chars to determine command */
if (++cmd_count[chan] == 1)
return 1;
/* Check if we have enough digits */
switch(cmd_buffer[chan]) {
case DNOP: /* Nop */
case DREL: /* Release */
case DEBM: /* Eight Bit mode */
case DSBM: /* Six bit mode */
break; /* Yes */
case DSAI: /* Set Access Inoperative */
if (cmd_count[chan] <= 3)
return 1; /* Need more */
break;
case DSEK: /* Seek */
case DWRF: /* Prepare to Format */
case DVHA: /* Prepare to Verify home addr */
if (cmd_count[chan] <= 7)
return 1; /* Need more */
break;
case DVTA: /* Prepare to Verify track addr */
case DVTN: /* Prepare to Verify track no addr */
case DVCY: /* Prepare to Verify Cyl */
case DVSR: /* Prepare to Verify single record */
case DWRC: /* Prepare to Write Check */
if (cmd_count[chan] < 10)
return 1;
break;
}
/* Flag last item recieved */
chan_set(chan, DEV_REOR);
chan9_clear_error(chan, sel);
sim_debug(DEBUG_CMD, &dsk_dev, "unit=%d cmd=%02x %02x %04x %04o ",u,
cmd_buffer[chan], cmd_mod[chan], cmd_option[chan] >> 16,
cmd_option[chan] & 07777);
/* 40 36 32 2824 2016 6 0 */
/* 01 2 3 4 5 6 7 8 9 */
/* op | ac mod | t t | t t | HA2 | HA2 */
up = NULL;
sense[schan] &= STAT_SIXBIT;
switch (cmd_buffer[chan]) {
case DNOP: /* Nop */
case DREL: /* Release */
/* Should not happen, but if read or write, cpy will be
* expected so turn on command flag to catch disconnect */
sim_debug(DEBUG_CMD, &dsk_dev, "nop\n");
goto clear_drive;
case DEBM: /* Eight Bit mode */
/* Should not happen, but if read or write, cpy will be
* expected so turn on command flag to catch disconnect */
sim_debug(DEBUG_CMD, &dsk_dev, "eight bit mode\n");
sense[schan] &= ~STAT_SIXBIT;
goto clear_drive;
case DSBM: /* Six bit mode */
/* Should not happen, but if read or write, cpy will be
* expected so turn on command flag to catch disconnect */
sim_debug(DEBUG_CMD, &dsk_dev, "six bit mode\n");
sense[schan] |= STAT_SIXBIT;
clear_drive:
/* Scan all units and clear command */
for (i = 0; i < NUM_DEVS_DSK; i++) {
int xchan = UNIT_G_CHAN(dsk_unit[i].flags);
int j;
if (xchan != chan)
continue;
for (j = 3 * NUM_DEVS_DSK + i; j >= 0; j -= NUM_DEVS_DSK)
dsk_unit[j].u5 &= ~ DSKSTA_CMSK;
}
sim_activate(uptr, us_to_ticks(100));
return 1;
case DWRC: /* Prepare to Write Check */
sim_debug(DEBUG_CMD, &dsk_dev, "write check\n");
case DVSR: /* Prepare to Verify single record */
case DWRF: /* Prepare to Format */
case DVTN: /* Prepare to Verify track no addr */
case DVCY: /* Prepare to Verify Cyl */
case DVTA: /* Prepare to Verify track addr */
case DVHA: /* Prepare to Verify home addr */
case DSAI: /* Set Access Inoperative */
case DSEK: /* Seek */
break; /* Go find unit to operate on */
default:
sim_debug(DEBUG_CMD, &dsk_dev, " Unknown Command\n\r");
/* Flag as invalid command */
disk_cmderr(uptr, PROG_INVCODE);
return 1;
}
/* Find actual owner of this command */
for (i = 0; i < NUM_DEVS_DSK; i++) {
if ((dsk_unit[i].flags & (UNIT_SELECT | UNIT_CHAN)) !=
(base->flags & (UNIT_SELECT | UNIT_CHAN)))
continue;
/* Adjust for unit */
if ((0xff & dsk_unit[i].u3) == cmd_mod[chan])
up = &dsk_unit[i];
else if ((0xff & dsk_unit[i + NUM_DEVS_DSK].u3) == cmd_mod[chan])
up = &dsk_unit[i + NUM_DEVS_DSK];
else if ((0xff & dsk_unit[i + (NUM_DEVS_DSK * 2)].u3) == cmd_mod[chan])
up = &dsk_unit[i + (NUM_DEVS_DSK * 2)];
else if ((0xff & dsk_unit[i + (NUM_DEVS_DSK * 3)].u3) == cmd_mod[chan])
up = &dsk_unit[i + (NUM_DEVS_DSK * 3)];
else
continue;
/* Check if unit is busy */
if (cmd_buffer[chan] != DWRC && (up->u5 & DSKSTA_CMD || up->wait > 0)) {
sim_debug(DEBUG_CMD, &dsk_dev, "unit=%d busy\n", u);
if (up->wait > 5)
up->wait = 5;
disk_cmderr(uptr, STAT_NOTRDY);
return 1;
} else {
if (cmd_buffer[chan] == DWRC) {
if (up->u5 & DSKSTA_CMSK) {
up->u5 |= DSKSTA_CHECK;
up->u5 &= ~DSKSTA_DATA;
cmd_buffer[chan] = up->u5 & DSKSTA_CMSK;
} else {
disk_cmderr(up, PROG_INVSEQ);
return 0;
}
} else {
up->u5 &= ~(DSKSTA_CMSK|DSKSTA_CHECK|DSKSTA_WRITE|DSKSTA_DATA);
up->u5 |= cmd_buffer[chan];
}
break;
}
}
if (up == NULL) {
sim_debug(DEBUG_CMD, &dsk_dev, "invalid unit\n");
/* Flag as invalid command */
disk_cmderr(uptr, STAT_OFFLINE);
return 1;
}
/* Adjust to new unit */
u = up - dsk_unit;
base = &dsk_unit[(up->u3 >> 8) & 0xf];
chan = UNIT_G_CHAN(base->flags);
sel = (base->flags & UNIT_SELECT) ? 1 : 0;
schan = (chan * 2) + sel;
/* Clear unit attention */
sense_unit[schan] &= ~(1 << unit_bit[up->u3 & 0xff]);
/* Check if there is a unit here */
if ((base->flags & UNIT_ATT) == 0) {
disk_cmderr(uptr, STAT_OFFLINE);
return 1;
}
/* Find out track and cylinder of this operation */
trk = bcd_to_track(cmd_option[chan]);
if (chan_test(chan, CTL_PWRITE))
sim_debug(DEBUG_CMD, &dsk_dev, "write ");
if (chan_test(chan, CTL_PREAD))
sim_debug(DEBUG_CMD, &dsk_dev, "read ");
/* Do command */
switch (cmd_buffer[chan]) {
case DSAI: /* Set Access Inoperative */
detach_unit(base);
disk_cmderr(up, 0);
return 1;
case DSEK: /* Seek */
cyl = trk / disk_type[base->u4].track;
/* Check how far we have to move */
t = cyl - arm_cyl[u];
if (t < 0)
t = -t;
sim_debug(DEBUG_CMD, &dsk_dev,
"DSEK unit=%d %d cylinders to %d trk=%d\n", u, t, cyl, trk);
up->u5 &= ~(DSKSTA_CMSK | DSKSTA_CMD | DSKSTA_CHECK | DSKSTA_WRITE);
arm_cyl[u] = cyl; /* And cylinder */
if (cyl > disk_type[base->u4].cyl) {
disk_cmderr(up, PROG_INVADDR);
return 1;
}
/* Read in track buffer */
disk_rblock(up, trk);
/* Set up for seek wait */
up->wait = 0;
/* From documentation, it looks like seeks were a fixed time
* based on movement between cylinder groups */
if (t == 0) /* At cylinder, give attention */
up->wait = 2; /* Electronic select time */
else if (t > 50)
up->wait = (1800);
else if (t > 10)
up->wait = (1200);
else
up->wait = (300);
break;
case DWRF: /* Prepare to Format */
/* Verify ok to format */
if ((base->flags & FORMAT_OK) == 0) {
disk_cmderr(uptr, PROG_FMTCHK);
return 1;
}
cyl = trk / disk_type[base->u4].track;
/* Make sure positioned to correct track */
if (arm_cyl[u] != cyl) {
disk_cmderr(uptr, PROG_INVSEQ);
return 1;
}
/* Make sure head on valid cylinder */
if (cyl > disk_type[base->u4].cyl) {
disk_cmderr(up, PROG_INVADDR);
return 1;
}
fmt_cyl[u] = cyl;
sim_debug(DEBUG_CMD, &dsk_dev, "FMT unit=%d\n", u);
up->u5 |= DSKSTA_SCAN | DSKSTA_CMD | DSKSTA_WRITE; /* Flag as write */
up->u6 = 0;
chan_set(chan, DEV_SEL);
break;
case DVHA: /* Prepare to Verify home addr */
/* Verify HA2 ok to write */
if ((base->flags & HA2_OK) == 0) {
disk_cmderr(up, PROG_FMTCHK);
return 1;
}
/* fall through */
case DVTA: /* Prepare to Verify track addr */
case DVTN: /* Prepare to Verify track no addr */
case DVCY: /* Prepare to Verify Cyl */
switch (cmd_buffer[chan]) {
case DVTA:
sim_debug(DEBUG_CMD, &dsk_dev, "DVTA unit=%d ", u);
break;
case DVTN:
sim_debug(DEBUG_CMD, &dsk_dev, "DVTN unit=%d ", u);
break;
case DVCY:
sim_debug(DEBUG_CMD, &dsk_dev, "DVCY unit=%d ", u);
break;
case DVHA:
sim_debug(DEBUG_CMD, &dsk_dev, "DVHA unit=%d ", u);
break;
}
sim_debug(DEBUG_CMD, &dsk_dev, "trk=%d\n\r", trk);
/* Make sure head on valid cylinder */
if ((trk / disk_type[base->u4].track) > disk_type[base->u4].cyl) {
disk_cmderr(up, PROG_INVADDR);
return 1;
}
/* Make sure we have correct format for this cylinder in buffer */
disk_rblock(up, trk);
/* Start actual operations */
up->u5 |= DSKSTA_SCAN | DSKSTA_CMD;
up->u6 = 0;
chan_set(chan, DEV_SEL);
break;
case DVSR: /* Prepare to Verify single record */
/* Start actual operations */
up->u5 |= DSKSTA_SCAN | DSKSTA_CMD;
up->u6 = 0;
chan_set(chan, DEV_SEL);
break;
}
sim_activate(up, us_to_ticks(50));
return 0;
}
/* Print a format pattern */
void
print_format(UNIT * uptr)
{
struct disk_t *dsk = &disk_type[uptr->u4];
int i, j;
int u = uptr - dsk_unit;
int flag;
int lflag = -1;
sim_debug(DEBUG_DETAIL, &dsk_dev, "unit=%d (%s) format: ", u, dsk->name);
i = 0;
j = 0;
do {
flag = fbuffer[u][i / 4];
flag >>= (i & 03) * 2;
flag &= 03;
if (lflag != flag) {
if (j != 0) {
switch(lflag) {
case FMT_DATA:
sim_debug(DEBUG_DETAIL, &dsk_dev, "DA(%d) ", j);
break;
case FMT_HDR:
sim_debug(DEBUG_DETAIL, &dsk_dev, "RA(%d) ", j);
break;
case FMT_HA2:
sim_debug(DEBUG_DETAIL, &dsk_dev, "HA2(%d) ", j);
break;
}
}
j = 1;
lflag = flag;
} else {
j++;
}
i++;
} while (flag != FMT_END);
sim_debug(DEBUG_DETAIL, &dsk_dev, "total=%d\n", i);
}
int
disk_rblock(UNIT * uptr, int trk)
{
int u = uptr - dsk_unit;