-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsim_tape.c
2275 lines (1762 loc) · 109 KB
/
sim_tape.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
/* sim_tape.c: simulator tape support library
Copyright (c) 1993-2021, Robert M Supnik
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
ROBERT M SUPNIK 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.
Except as contained in this notice, the name of Robert M Supnik shall not be
used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from Robert M Supnik.
Ultimately, this will be a place to hide processing of various tape formats,
as well as OS-specific direct hardware access.
15-Dec-21 JDB Added extended SIMH format support
10-Oct-21 JDB Improved tape_erase_fwd corrupt image error checking
06-Oct-21 JDB Added sim_tape_erase global, tape_erase internal functions
27-Dec-18 JDB Added missing fall through comment in sim_tape_wrrecf
03-May-17 JDB Added support for erasing tape marks to sim_tape_errec[fr]
02-May-17 JDB Added error checks to sim_fseek calls
18-Jul-16 JDB Added sim_tape_errecf, sim_tape_errecr functions
12-Oct-15 JDB Fixed bug in sim_tape_rdlntf if gap buffer read ends at EOM
15-Dec-14 JDB Changed sim_tape_set_dens to check validity of density change
04-Nov-14 JDB Restrict sim_tape_set_fmt to unit unattached
31-Oct-14 JDB Fixed gap skip on reverse read
Fixed write EOM bug (should not update position)
Added set/show density functions, changed sim_tape_wrgap API
Buffered forward/reverse gap skip to improve execution time
22-Sep-14 JDB Added tape runaway support
08-Jun-08 JDB Fixed signed/unsigned warning in sim_tape_set_fmt
23-Jan-07 JDB Fixed backspace over gap at BOT
22-Jan-07 RMS Fixed bug in P7B format read reclnt rev (found by Rich Cornwell)
15-Dec-06 RMS Added support for small capacity tapes
30-Aug-06 JDB Added erase gap support
14-Feb-06 RMS Added variable tape capacity
23-Jan-06 JDB Fixed odd-byte-write problem in sim_tape_wrrecf
17-Dec-05 RMS Added write support for Paul Pierce 7b format
16-Aug-05 RMS Fixed C++ declaration and cast problems
02-May-05 RMS Added support for Pierce 7b format
28-Jul-04 RMS Fixed bug in writing error records (found by Dave Bryan)
RMS Fixed incorrect error codes (found by Dave Bryan)
05-Jan-04 RMS Revised for file I/O library
25-Apr-03 RMS Added extended file support
28-Mar-03 RMS Added E11 and TPC format support
The tape formats and functions implemented herein are described in separate
monographs titled, "SIMH Magtape Representation and Handling" and "Writing a
Simulator for the SIMH System," respectively.
Public routines:
sim_tape_attach attach tape unit
sim_tape_detach detach tape unit
sim_tape_rdrecf read tape record forward
sim_tape_rdrecr read tape record reverse
sim_tape_wrrecf write tape record forward
sim_tape_sprecf space tape record forward
sim_tape_sprecr space tape record reverse
sim_tape_wrmrk write private marker
sim_tape_wrtmk write tape mark
sim_tape_wreom erase remainder of tape
sim_tape_wrgap write erase gap
sim_tape_errecf erase record forward
sim_tape_errecr erase record reverse
sim_tape_erase erase a specified number of bytes
sim_tape_rewind rewind
sim_tape_reset reset unit
sim_tape_bot TRUE if at beginning of tape
sim_tape_eot TRUE if at or beyond end of tape
sim_tape_wrp TRUE if write protected
sim_tape_set_fmt set tape format
sim_tape_show_fmt show tape format
sim_tape_set_capac set tape capacity
sim_tape_show_capac show tape capacity
sim_tape_set_dens set tape density
sim_tape_show_dens show tape density
*/
#include "sim_defs.h"
#include "sim_tape.h"
struct sim_tape_fmt {
char *name; /* name */
int32 uflags; /* unit flags */
t_addr bot; /* bot test */
};
static const struct sim_tape_fmt fmts [] = { /* format table, indexed by MTUF_F number */
/* name uflags bot */
/* ------- ------------------- --------------------- */
{ "SIMH", MT_F_STD, sizeof (t_mtrlnt) - 1 }, /* 0 = MTUF_F_STD */
{ "E11", MT_F_E11, sizeof (t_mtrlnt) - 1 }, /* 1 = MTUF_F_E11 */
{ "TPC", MT_F_TPC | UNIT_RO, sizeof (t_tpclnt) - 1 }, /* 2 = MTUF_F_TPC */
{ "P7B", MT_F_P7B, 0 }, /* 3 = MTUF_F_P7B */
{ " ", MT_F_TDF | UNIT_RO, 0 }, /* 4 = MTUF_F_TDF (not implemented) */
{ "SIMH", MT_F_EXT, sizeof (t_mtrlnt) - 1 } /* 5 = MTUF_F_EXT */
};
#define FMT_COUNT (sizeof fmts / sizeof fmts [0]) /* count of format table entries */
static const uint32 bpi [] = { /* tape density table, indexed by MT_DENS constants */
0, /* 0 = MT_DENS_NONE -- density not set */
200, /* 1 = MT_DENS_200 -- 200 bpi NRZI */
556, /* 2 = MT_DENS_556 -- 556 bpi NRZI */
800, /* 3 = MT_DENS_800 -- 800 bpi NRZI */
1600, /* 4 = MT_DENS_1600 -- 1600 bpi PE */
6250 /* 5 = MT_DENS_6250 -- 6250 bpi GCR */
};
#define BPI_COUNT (sizeof (bpi) / sizeof (bpi [0])) /* count of density table entries */
static t_stat sim_tape_ioerr (UNIT *uptr);
static t_stat sim_tape_wrdata (UNIT *uptr, uint32 dat);
static uint32 sim_tape_tpc_map (UNIT *uptr, t_addr *map);
static t_addr sim_tape_tpc_fnd (UNIT *uptr, t_addr *map);
static t_stat tape_read (UNIT *uptr, uint8 *buffer, t_mtrlnt *class_count, t_mtrlnt bufsize, t_bool reverse);
static t_stat tape_erase (UNIT *uptr, t_mtrlnt byte_count);
static t_stat tape_erase_fwd (UNIT *uptr, t_mtrlnt gap_size);
static t_stat tape_erase_rev (UNIT *uptr, t_mtrlnt gap_size);
/* Attach tape unit */
t_stat sim_tape_attach (UNIT *uptr, char *cptr)
{
uint32 objc;
char gbuf[CBUFSIZE];
t_stat r;
if (sim_switches & SWMASK ('F')) { /* format spec? */
cptr = get_glyph (cptr, gbuf, 0); /* get spec */
if (*cptr == 0) /* must be more */
return SCPE_2FARG;
if (sim_tape_set_fmt (uptr, 0, gbuf, NULL) != SCPE_OK)
return SCPE_ARG;
}
r = attach_unit (uptr, cptr); /* attach unit */
if (r != SCPE_OK) /* error? */
return r;
switch (MT_GET_FMT (uptr)) { /* case on format */
case MTUF_F_TPC: /* TPC */
objc = sim_tape_tpc_map (uptr, NULL); /* get # objects */
if (objc == 0) { /* tape empty? */
sim_tape_detach (uptr);
return SCPE_FMT; /* yes, complain */
}
uptr->filebuf = calloc (objc + 1, sizeof (t_addr));
if (uptr->filebuf == NULL) { /* map allocated? */
sim_tape_detach (uptr);
return SCPE_MEM; /* no, complain */
}
uptr->hwmark = objc + 1; /* save map size */
sim_tape_tpc_map (uptr, (t_addr *) uptr->filebuf); /* fill map */
break;
default:
break;
}
sim_tape_rewind (uptr);
return SCPE_OK;
}
/* Detach tape unit */
t_stat sim_tape_detach (UNIT *uptr)
{
uint32 f = MT_GET_FMT (uptr);
t_stat r;
r = detach_unit (uptr); /* detach unit */
if (r != SCPE_OK)
return r;
switch (f) { /* case on format */
case MTUF_F_TPC: /* TPC */
if (uptr->filebuf) /* free map */
free (uptr->filebuf);
uptr->filebuf = NULL;
uptr->hwmark = 0;
break;
default:
break;
}
sim_tape_rewind (uptr);
return SCPE_OK;
}
/* Read record length forward (internal routine).
Inputs:
uptr = pointer to tape unit
bc = pointer to returned record length
Outputs:
status = operation status
exit condition tape position
------------------ -----------------------------------------------------
unit unattached unchanged
read error unchanged, PNU set if initial read
end of file/medium updated if a gap precedes, else unchanged and PNU set
tape mark updated
other marker updated
tape runaway updated
data record updated, sim_fread will read record forward
This routine is called to set up a record read or spacing in the forward
direction. On return, status is MTSE_OK if a data record, private marker, or
reserved marker was read, or an MTSE error code if a standard marker (e.g.
tape mark) was read, or an error occurred. The file is positioned at the
first byte of a data record, after a tape mark or private marker, or
otherwise as indicated above. In all cases, the successfully read marker or
data record length word is returned via the "bc" pointer.
When the extended SIMH format is enabled, then the variable addressed by the
"bc" parameter must be set on entry to a bitmap of the object classes to
return. Each of the classes is represented by its corresponding bit, i.e.,
bit 0 represents class 0, bit 1 for class 1, etc. The routine will return
only objects from the selected classes. Unselected class objects will be
ignored by skipping over them until the first selected class object is seen.
This allows a simulator to declare those classes it understands (e.g.,
standard classes 0 and 8, plus private classes 2 and 7) and those classes it
wishes to ignore. Erase gap markers are always skipped, and standard markers
are always returned, so specifying an empty bitmap will perform the
equivalent of a "space file forward," returning only when a tape mark or
EOM/EOF is encountered.
When standard SIMH format is enabled, standard classes 0 and 8 are
automatically selected, and the entry value addressed by "bc" is ignored.
The ANSI standards for magnetic tape recording (X3.22, X3.39, and X3.54) and
the equivalent ECMA standard (ECMA-62) specify a maximum erase gap length of
25 feet (7.6 meters). While gaps of any length may be written, gaps longer
than this are non-standard and may indicate that an unrecorded or erased tape
is being read.
If the tape density has been set via a previous "sim_tape_set_dens" call,
then the length is monitored when skipping over erase gaps. If the length
reaches 25 feet, motion is terminated, and MTSE_RUNAWAY status is returned.
Runaway status is also returned if an end-of-medium marker or the physical
end of file is encountered while spacing over a gap; however, MTSE_EOM is
returned if the tape is positioned at the EOM or EOF on entry.
If the density has not been set, then a gap of any length is skipped, and
MTSE_RUNAWAY status is never returned. In effect, erase gaps present in the
tape image file will be transparent to the caller.
Erase gaps are currently supported only in standard and extended SIMH tape
formats. Because gaps may be partially overwritten with data records, gap
metadata must be examined marker-by-marker. To reduce the number of file
read calls, a buffer of metadata elements is used. The buffer size is
initially established at 256 elements but may be set to any size desired. To
avoid a large read for the typical case where an erase gap is not present,
the first read is of a single metadatum marker. If that is a gap marker,
then additional buffered reads are performed.
The permissibility of data record lengths that are not multiples of the
metadatum size presents a difficulty when reading through gaps. If such an
"odd length" record is written over a gap, half of a gap marker will exist
immediately after the trailing record length.
This condition is detected when reading forward by the appearance of a
"reversed" marker. The value appears reversed because the value is made up
of half of one marker and half of the next. This is handled by seeking
forward two bytes to resync (it is illegal to overwrite and leave only two
bytes of gap, so at least one "whole" metadata marker will follow the
half-gap).
Implementation notes:
1. For programming convenience, erase gap processing is performed for both
SIMH and E11 tape formats, although the latter will never contain erase
gaps, as the "tape_erase_fwd" call takes no action for the E11 format.
2. The "runaway_counter" cannot decrement to zero (or below) in the presence
of an error that terminates the gap-search loop. Therefore, the test
after the loop exit need not check for error status, except to check
whether an EOM occurred while reading a gap.
3. The dynamic start/stop test of the HP 3000 magnetic tape diagnostic
heavily exercises the erase gap scanning code. Sample test execution
times for various buffer sizes on a 2 GHz host platform are:
buffer size execution time
(elements) (CPU seconds)
----------- --------------
1 7200
32 783
128 237
256 203
512 186
1024 171
4. Because an erase gap may precede the logical end-of-medium, represented
either by the physical end-of-file or by an EOM marker, the "position not
updated" flag is set only if the tape is positioned at the EOM when the
routine is entered. If at least one gap marker precedes the EOM, then
the PNU flag is not set. This ensures that a backspace-and-retry
sequence will work correctly in both cases.
5. When a data record length word is seen, a check is made to see if the
word is the last word in the metadata buffer. If it is, then the file
stream is correctly positioned to read the data, i.e., is positioned
immediately after the length word. If the word is somewhere within the
buffer, then the stream is repositioned to the location of the start of
the data.
6. A skipped data record may reside entirely within the metadata buffer.
However, the buffer consists of four-byte elements, and a data record may
not end on an element boundary. Rather than testing for this and
succeeding only half of the time, we unilaterally reposition the file
stream and invalidate the buffer to force a read.
7. A partial buffer read without a host I/O error occurs when the physical
EOF is reached. If the buffer contains only erase gap markers, i.e., the
tape image ends with a gap, then the next read will return zero because
the EOF flag is set. In this case, we could avoid this read by calling
the "feof" function first. We do not, because the common case -- entry
with the file positioned at EOF -- will not have the EOF flag set, as the
preceding "fseek" resets it, so the read call would be made anyway.
Thus, we would incur a small overhead on every call to save some overhead
on a rare corner-case.
*/
static t_stat sim_tape_rdlntf (UNIT *uptr, t_mtrlnt *bc)
{
const uint32 f = MT_GET_FMT (uptr); /* the tape format */
uint8 c;
t_bool all_eof;
t_mtrlnt sbc;
t_tpclnt tpcbc;
t_mtrlnt buffer [256]; /* local tape buffer */
uint32 bufcntr, bufcap; /* buffer counter and capacity */
uint32 classbit; /* bit representing the object class */
int32 runaway_counter, max_gap, sizeof_gap; /* bytes remaining before runaway and bytes per gap */
t_addr next_pos; /* next record position */
t_stat status = MTSE_OK; /* preset status return */
uint32 accept = MTB_STANDARD; /* preset the standard class acceptance set */
MT_CLR_PNU (uptr); /* clear the position-not-updated flag */
if ((uptr->flags & UNIT_ATT) == 0) /* if the unit is not attached */
return MTSE_UNATT; /* then quit with an error */
if (sim_fseek (uptr->fileref, uptr->pos, SEEK_SET)) { /* set the initial tape position; if it fails */
MT_SET_PNU (uptr); /* then set position not updated */
status = sim_tape_ioerr (uptr); /* and quit with I/O error status */
}
else switch (f) { /* otherwise the read method depends on the tape format */
case MTUF_F_EXT:
accept = (uint32) (*bc); /* get the set of acceptable classes */
/* fall through into the standard SIMH and E11 handler */
case MTUF_F_STD:
case MTUF_F_E11:
max_gap = 25 * 12 /* set the largest legal gap size in bytes */
* bpi [MT_DENS (uptr->dynflags)]; /* corresponding to 25 feet of tape */
if (max_gap == 0) { /* if tape density has not been not set */
sizeof_gap = 0; /* then disable runaway detection */
max_gap = INT_MAX; /* and allow gaps of any size */
}
else /* otherwise */
sizeof_gap = sizeof (t_mtrlnt); /* set the size of the gap */
runaway_counter = max_gap; /* initialize the runaway counter */
bufcntr = 0; /* force an initial read */
bufcap = 0; /* but of just one metadata marker */
do { /* loop until an object is accepted or an error occurs */
if (bufcntr == bufcap) { /* if the buffer is empty */
if (bufcap == 0) /* then if this is the initial read */
bufcap = 1; /* then start with just one marker */
else /* otherwise reset the capacity */
bufcap = sizeof (buffer) /* to the full size of the buffer */
/ sizeof (buffer [0]);
bufcap = sim_fread (buffer, sizeof (t_mtrlnt), /* fill the buffer */
bufcap, uptr->fileref); /* with tape metadata */
if (ferror (uptr->fileref)) { /* if a file I/O error occurred */
if (bufcntr == 0) /* then if this is the initial read */
MT_SET_PNU (uptr); /* then set position-not-updated */
status = sim_tape_ioerr (uptr); /* report the error and quit */
break;
}
else if (bufcap == 0 /* otherwise if positioned at the physical EOF */
|| buffer [0] == MTR_EOM) { /* or at the logical EOM */
if (bufcntr == 0) /* then if this is the initial read */
MT_SET_PNU (uptr); /* then set position not updated */
if (bufcap == 0) /* if an EOM marker was not read */
*bc = 0; /* then zero the marker value */
else /* otherwise */
*bc = MTR_EOM; /* store the EOM value */
status = MTSE_EOM; /* report the end-of-medium */
break; /* and quit */
}
else /* otherwise reset the index */
bufcntr = 0; /* to the start of the buffer */
}
*bc = buffer [bufcntr++]; /* store the metadata marker value */
if (*bc == MTR_EOM) { /* if an end-of-medium marker is seen */
status = MTSE_EOM; /* then report the end-of-medium */
break; /* and quit */
}
uptr->pos = uptr->pos + sizeof (t_mtrlnt); /* space over the marker */
if (*bc == MTR_TMK) { /* if the marker is a tape mark */
status = MTSE_TMK; /* then quit with tape mark status */
break;
}
else if (*bc == MTR_GAP) /* otherwise if the marker is a full gap */
runaway_counter -= sizeof_gap; /* then decrement the gap counter */
else if (*bc == MTR_FHGAP) { /* otherwise if the marker if a half gap */
uptr->pos = uptr->pos - sizeof (t_mtrlnt) / 2; /* then back up to resync */
if (sim_fseek (uptr->fileref, uptr->pos, SEEK_SET)) { /* set the tape position; if it fails */
status = sim_tape_ioerr (uptr); /* then quit with I/O error status */
break;
}
bufcntr = bufcap; /* mark the buffer as invalid to force a read */
runaway_counter -= sizeof_gap / 2; /* and decrement the gap counter by half */
}
else { /* otherwise it is not a known marker */
classbit = MTR_FB (*bc); /* so get the bit corresponding to the class */
next_pos = uptr->pos + MTR_RL (*bc) /* it it's a data record */
+ sizeof (t_mtrlnt); /* then preset to the end of the record */
if (f != MTUF_F_E11) /* if the format is not E11 */
next_pos += MTR_RL (*bc) & 1; /* then record sizes are an even number of bytes */
if (classbit & accept) { /* if the class is accepted */
if (classbit == MTB_SMARK) /* then if it's a SIMH-reserved marker */
status = MTSE_RESERVED; /* then return reserved status */
else if (classbit == MTB_PMARK) /* otherwise if it's a private marker */
status = MTSE_OK; /* then return successful status */
else if (bufcntr == bufcap /* otherwise if the record starts after the buffer */
|| sim_fseek (uptr->fileref, /* or repositioning to the start */
uptr->pos, SEEK_SET) == 0) /* of the data area succeeds */
uptr->pos = next_pos; /* then position past the record */
else /* otherwise the seek failed */
status = sim_tape_ioerr (uptr); /* so quit with I/O error status */
break; /* acceptance terminates the search */
}
else if (classbit & MTB_RECORDSET) { /* otherwise if ignoring a data record */
uptr->pos = next_pos; /* then position past the record */
if (sim_fseek (uptr->fileref, uptr->pos, SEEK_SET)) { /* set the new position; if it fails */
status = sim_tape_ioerr (uptr); /* then quit with I/O error status */
break;
}
bufcntr = bufcap; /* mark the buffer as invalid to force a read */
}
runaway_counter = max_gap; /* ignoring a marker or record resets the counter */
}
}
while (runaway_counter > 0); /* continue searching until runaway occurs */
if (sizeof_gap > 0 /* if gap detection is enabled */
&& (runaway_counter <= 0 /* and a tape runaway occurred */
|| status == MTSE_EOM /* or EOM/EOF was seen */
&& runaway_counter < max_gap)) /* while a gap was being skipped */
status = MTSE_RUNAWAY; /* then report it */
break; /* end of case */
case MTUF_F_TPC:
sim_fread (&tpcbc, sizeof (t_tpclnt), 1, uptr->fileref);
*bc = tpcbc; /* save rec lnt */
if (ferror (uptr->fileref)) { /* error? */
MT_SET_PNU (uptr); /* pos not upd */
status = sim_tape_ioerr (uptr);
}
else if (feof (uptr->fileref)) { /* eof? */
MT_SET_PNU (uptr); /* pos not upd */
status = MTSE_EOM;
}
else {
uptr->pos = uptr->pos + sizeof (t_tpclnt); /* spc over reclnt */
if (tpcbc == TPC_TMK) /* tape mark? */
status = MTSE_TMK;
else
uptr->pos = uptr->pos + ((tpcbc + 1) & ~1); /* spc over record */
}
break;
case MTUF_F_P7B:
for (sbc = 0, all_eof = 1; ; sbc++) { /* loop thru record */
sim_fread (&c, sizeof (uint8), 1, uptr->fileref);
if (ferror (uptr->fileref)) { /* error? */
MT_SET_PNU (uptr); /* pos not upd */
status = sim_tape_ioerr (uptr);
break;
}
else if (feof (uptr->fileref)) { /* eof? */
if (sbc == 0) /* no data? eom */
status = MTSE_EOM;
break; /* treat like eor */
}
else if ((sbc != 0) && (c & P7B_SOR)) /* next record? */
break;
else if ((c & P7B_DPAR) != P7B_EOF)
all_eof = 0;
}
if (status == MTSE_OK) {
*bc = sbc; /* save rec lnt */
sim_fseek (uptr->fileref, uptr->pos, SEEK_SET); /* for read */
uptr->pos = uptr->pos + sbc; /* spc over record */
if (all_eof) /* tape mark? */
status = MTSE_TMK;
}
break;
default:
status = MTSE_FMT;
}
return status;
}
/* Read record length reverse (internal routine).
Inputs:
uptr = pointer to tape unit
bc = pointer to returned record length
Outputs:
status = operation status
exit condition tape position
------------------ -------------------------------------------
unit unattached unchanged
read error unchanged, PNU set if initial read
beginning of tape unchanged
tape mark updated
other marker updated
tape runaway updated
data record updated, sim_fread will read record forward
This routine is called to set up a record read or spacing in the reverse
direction. On return, status is MTSE_OK if a data record, private marker, or
reserved marker was read, or an MTSE error code if a standard marker (e.g.
tape mark) was read, or an error occurred. The file is positioned at the
first byte of a data record, before a tape mark or private marker, or
otherwise as indicated above. In all cases, the successfully read marker or
data record length word is returned via the "bc" pointer.
When the extended SIMH format is enabled, then the variable addressed by the
"bc" parameter must be set on entry to a bitmap of the object classes to
return. Each of the classes is represented by its corresponding bit, i.e.,
bit 0 represents class 0, bit 1 for class 1, etc. The routine will return
only objects from the selected classes. Unselected class objects will be
ignored by skipping over them until the first selected class object is seen.
This allows a simulator to declare those classes it understands (e.g.,
standard classes 0 and 8, plus private classes 2 and 7) and those classes it
wishes to ignore. Erase gap markers are always skipped, and standard markers
are always returned, so specifying an empty bitmap will perform the
equivalent of a "space file forward," returning only when a tape mark or
EOM/EOF is encountered.
When standard SIMH format is enabled, standard classes 0 and 8 are
automatically selected, and the entry value addressed by "bc" is ignored.
The permissibility of data record lengths that are not multiples of the
metadatum size presents a difficulty when reading through gaps. If such an
"odd length" record is written over a gap, half of a gap marker will exist
immediately after the trailing record length.
Reading in reverse presents a more complex problem than reading forward
through gaps, because half of the marker is from the preceding trailing
record length marker and therefore could be any of a range of values.
However, that range is restricted by the SIMH tape specification to permit
unambiguous detection of the condition. The Class F assignments are:
F0000000 - FFFDFFFF Reserved for future use (available)
FFFE0000 - FFFFFFFD Reserved for erase gap interpretation
FFFFFFFE Erase gap (primary value)
FFFFFFFF End of medium
Values within the reserved erase-gap interpretation subrange are as follows:
FFFE0000 - FFFEFFFE Illegal (would be seen as full gap in reverse reads)
FFFEFFFF Interpret as half-gap in forward reads
FFFF0000 - FFFFFFFD Interpret as half-gap in reverse reads
A conforming writer will never write the illegal marker values, so that a
conforming reader will be able to recognize the half-gap marker values.
Implementation notes:
1. The "sim_fread" call cannot return 0 in the absence of an error
condition. The preceding "sim_tape_bot" test ensures that "pos" >= 4, so
"sim_fseek" will back up at least that far, so "sim_fread" will read at
least one element. If the call returns zero, an error must have
occurred, so the "ferror" call must succeed.
2. The "runaway_counter" cannot decrement to zero (or below) in the presence
of an error that terminates the gap-search loop. Therefore, the test
after the loop exit need not check for error status.
3. See the notes at "sim_tape_rdlntf" regarding the implementation of tape
runaway detection.
*/
static t_stat sim_tape_rdlntr (UNIT *uptr, t_mtrlnt *bc)
{
const uint32 f = MT_GET_FMT (uptr); /* the tape format */
uint8 c;
t_bool all_eof;
t_addr ppos;
t_mtrlnt sbc;
t_tpclnt tpcbc;
t_mtrlnt buffer [256]; /* local tape buffer */
uint32 bufcntr, bufcap; /* buffer counter and capacity */
uint32 classbit; /* bit representing the object class */
int32 runaway_counter, max_gap, sizeof_gap; /* bytes remaining before runaway and bytes per gap */
t_addr next_pos; /* next record position */
t_stat status = MTSE_OK; /* preset status return */
uint32 accept = MTB_STANDARD; /* preset the standard class acceptance set */
MT_CLR_PNU (uptr); /* clear the position-not-updated flag */
if ((uptr->flags & UNIT_ATT) == 0) /* if the unit is not attached */
return MTSE_UNATT; /* then quit with an error */
else if (sim_tape_bot (uptr)) /* otherwise if the unit is positioned at the BOT */
status = MTSE_BOT; /* then reading backward is not possible */
else switch (f) { /* otherwise the read method depends on the tape format */
case MTUF_F_EXT:
accept = (uint32) (*bc); /* get the set of acceptable classes */
/* fall through into the standard SIMH and E11 handler */
case MTUF_F_STD:
case MTUF_F_E11:
max_gap = 25 * 12 /* set the largest legal gap size in bytes */
* bpi [MT_DENS (uptr->dynflags)]; /* corresponding to 25 feet of tape */
if (max_gap == 0) { /* if tape density has not been not set */
sizeof_gap = 0; /* then disable runaway detection */
max_gap = INT_MAX; /* and allow gaps of any size */
}
else /* otherwise */
sizeof_gap = sizeof (t_mtrlnt); /* set the size of the gap */
runaway_counter = max_gap; /* initialize the runaway counter */
bufcntr = 0; /* force an initial read */
bufcap = 0; /* but of just one metadata marker */
ppos = uptr->pos; /* save the initial tape position */
do { /* loop until an object is accepted or an error occurs */
if (bufcntr == 0) { /* then if the buffer is empty */
if (sim_tape_bot (uptr)) { /* then if the search has backed into the BOT */
status = MTSE_BOT; /* then quit with an error */
break;
}
else if (bufcap == 0) /* otherwise if this is the initial read */
bufcap = 1; /* then start with just one marker */
else if (uptr->pos < sizeof (buffer)) /* otherwise if less than a full buffer remains */
bufcap = (uint32) uptr->pos /* then reduce the capacity accordingly */
/ sizeof (t_mtrlnt);
else /* otherwise reset the capacity */
bufcap = sizeof (buffer) /* to the full size of the buffer */
/ sizeof (buffer [0]);
sim_fseek (uptr->fileref, /* seek back to the location */
uptr->pos - bufcap * sizeof (t_mtrlnt), /* corresponding to the start */
SEEK_SET); /* of the buffer */
bufcntr = sim_fread (buffer, sizeof (t_mtrlnt), /* fill the buffer */
bufcap, uptr->fileref); /* with tape metadata */
if (ferror (uptr->fileref)) { /* if a file I/O error occurred */
if (uptr->pos == ppos) /* then if this is the initial read */
MT_SET_PNU (uptr); /* then set position not updated */
status = sim_tape_ioerr (uptr); /* report the error and quit */
break;
}
}
*bc = buffer [--bufcntr]; /* store the metadata marker value */
uptr->pos = uptr->pos - sizeof (t_mtrlnt); /* backspace over the marker */
if (*bc == MTR_TMK) { /* if the marker is a tape mark */
status = MTSE_TMK; /* then quit with tape mark status */
break;
}
else if (*bc == MTR_GAP) /* otherwise if the marker is a full gap */
runaway_counter -= sizeof_gap; /* then decrement the gap counter */
else if ((*bc & MTR_RHGAP) == MTR_RHGAP) { /* otherwise if the marker is a half gap */
uptr->pos = uptr->pos + sizeof (t_mtrlnt) / 2; /* then position forward to resync */
bufcntr = 0; /* mark the buffer as invalid to force a read */
runaway_counter -= sizeof_gap / 2; /* and decrement the gap counter by half */
}
else { /* otherwise it is not a known marker */
classbit = MTR_FB (*bc); /* so get the bit corresponding to the class */
next_pos = uptr->pos - MTR_RL (*bc) /* it it's a data record */
- sizeof (t_mtrlnt); /* then preset to the start of the record */
if (f != MTUF_F_E11) /* if the format is not E11 */
next_pos -= MTR_RL (*bc) & 1; /* then record sizes are an even number of bytes */
if (classbit & accept) { /* if the class is accepted */
if (classbit == MTB_SMARK) /* then if it's a SIMH-reserved marker */
status = MTSE_RESERVED; /* then return reserved status */
else if (classbit == MTB_PMARK) /* otherwise if it's a private marker */
status = MTSE_OK; /* then return successful status */
else if (sim_fseek (uptr->fileref, /* otherwise position to the start */
next_pos + sizeof (t_mtrlnt), /* of the data area */
SEEK_SET) == 0) /* and if the seek succeeds */
uptr->pos = next_pos; /* then position past the record */
else /* otherwise the seek failed */
status = sim_tape_ioerr (uptr); /* so quit with I/O error status */
break; /* acceptance terminates the search */
}
else if (classbit & MTB_RECORDSET) { /* otherwise if ignoring a data record */
uptr->pos = next_pos; /* then position before the record */
if (sim_fseek (uptr->fileref, uptr->pos, SEEK_SET)) { /* set the new position; if it fails */
status = sim_tape_ioerr (uptr); /* then quit with I/O error status */
break;
}
bufcntr = 0; /* mark the buffer as invalid to force a read */
}
runaway_counter = max_gap; /* ignoring a marker or record resets the counter */
}
}
while (runaway_counter > 0); /* continue searching until runaway occurs */
if (runaway_counter <= 0) /* if a tape runaway occurred */
status = MTSE_RUNAWAY; /* then report it */
break; /* end of case */
case MTUF_F_TPC:
ppos = sim_tape_tpc_fnd (uptr, (t_addr *) uptr->filebuf); /* find prev rec */
sim_fseek (uptr->fileref, ppos, SEEK_SET); /* position */
sim_fread (&tpcbc, sizeof (t_tpclnt), 1, uptr->fileref);
*bc = tpcbc; /* save rec lnt */
if (ferror (uptr->fileref)) /* error? */
status = sim_tape_ioerr (uptr);
else if (feof (uptr->fileref)) /* eof? */
status = MTSE_EOM;
else {
uptr->pos = ppos; /* spc over record */
if (*bc == MTR_TMK) /* tape mark? */
status = MTSE_TMK;
else
sim_fseek (uptr->fileref, uptr->pos + sizeof (t_tpclnt), SEEK_SET);
}
break;
case MTUF_F_P7B:
for (sbc = 1, all_eof = 1; (t_addr) sbc <= uptr->pos ; sbc++) {
sim_fseek (uptr->fileref, uptr->pos - sbc, SEEK_SET);
sim_fread (&c, sizeof (uint8), 1, uptr->fileref);
if (ferror (uptr->fileref)) { /* error? */
status = sim_tape_ioerr (uptr);
break;
}
else if (feof (uptr->fileref)) { /* eof? */
status = MTSE_EOM;
break;
}
else {
if ((c & P7B_DPAR) != P7B_EOF)
all_eof = 0;
if (c & P7B_SOR) /* start of record? */
break;
}
}
if (status == MTSE_OK) {
uptr->pos = uptr->pos - sbc; /* update position */
*bc = sbc; /* save rec lnt */
sim_fseek (uptr->fileref, uptr->pos, SEEK_SET); /* for read */
if (all_eof) /* tape mark? */
status = MTSE_TMK;
}
break;
default:
status = MTSE_FMT;
}
return status;
}
/* Read a data record or tape marker.
Read the data record or tape marker a the current tape position in the
indicated direction and return it via the supplied buffer or marker pointers,
respectively.
On entry, the "uptr" parameter points at the UNIT structure describing the
tape device, "buffer" points at a buffer large enough to receive a retrieved
data record, "class_count" points at a variable that receives the data record
length word containing the record class and length or the tape marker value,
"bufsize" indicates the size of the buffer in bytes, and "reverse" is TRUE if
the record is to be read in the reverse direction and FALSE if the record is
to be read in the forward direction.
If the tape format is extended SIMH, then "class_count" must point at a value
containing a bitmap of the desired record and marker classes to read. Each
class is represented by its corresponding bit. Classes present in the tape
image but not in the bitmap are skipped until an object of the specified
class is read. The standard markers (tape mark, etc.) are always read and
interpreted.
A successful read of a data record returns the data in the buffer and the
record class and length via the "class_count" parameter. A successful marker
read returns the marker value via the "class_count" parameter; the buffer is
not used.
For all other tape formats, the entry value indicated by "class_count" is
ignored, and all items supported by the specified format are returned.
Successful reads return data in the buffer and the record length via the
"class_count" parameter.
The result of the read is returned as the value of the function, as follows:
Status Condition
------------- ---------------------------------------------
MTSE_OK Successful read of a good data record
MTSE_RECE Successful read of a bad data record
MTSE_TMK Successful read of a tape mark
MTSE_RESERVED Successful read of a reserved marker
MTSE_UNATT The tape unit is not attached
MTSE_IOERR A host I/O error occurred
MTSE_FMT An invalid tape format is selected
MTSE_BOT Reading stopped at the beginning of the tape
MTSE_EOM Reading stopped at the end of the tape
MTSE_RUNAWAY Reading did not encounter any data
MTSE_INVRL The record is larger than the supplied buffer
or the record is incomplete
*/
static t_stat tape_read (UNIT *uptr, uint8 *buffer, t_mtrlnt *class_count, t_mtrlnt bufsize, t_bool reverse)
{
const uint32 f = MT_GET_FMT (uptr); /* the tape format */
t_mtrlnt cbc, rbc;
t_addr opos;
t_stat st;
cbc = *class_count; /* get the acceptance mask */
opos = uptr->pos; /* and save the original file position */
if (reverse) /* for a reverse read */
st = sim_tape_rdlntr (uptr, &cbc); /* get the preceding record length */
else /* otherwise */
st = sim_tape_rdlntf (uptr, &cbc); /* get the following record length */
if (st != MTSE_OK /* if the read failed */
|| (MTR_FB (cbc) & MTB_MARKERSET)) { /* or it returned a marker */
if (f == MTUF_F_EXT) /* then if the format is extended SIMH */
*class_count = cbc; /* then return the marker value */
return st; /* return the status */
}
rbc = MTR_RL (cbc); /* get the record length */
if (f == MTUF_F_EXT) /* if the format is extended SIMH */
*class_count = cbc; /* then return the class and length */
else /* otherwise */
*class_count = rbc; /* return just the length */
if (rbc > bufsize) /* if the record won't fit in the buffer */
st = MTSE_INVRL; /* then return invalid length status */
else { /* otherwise */
sim_fread (buffer, sizeof (uint8), rbc, uptr->fileref); /* read the data payload into the supplied buffer */
if (ferror (uptr->fileref)) /* if a host I/O error occurred */
st = sim_tape_ioerr (uptr); /* then return I/O error status */
else if (feof (uptr->fileref)) /* otherwise if the read was incomplete */
st = MTSE_INVRL; /* then report a record length error */
else if (f == MTUF_F_P7B) /* otherwise if the format is P7B */
buffer [0] = buffer [0] & P7B_DPAR; /* then strip the start-of-record flag */
}
if (st != MTSE_OK) { /* if the read failed */
MT_SET_PNU (uptr); /* then set the position not updated flag */
uptr->pos = opos; /* and restore the original position */
return st; /* and return the failure status */
}
else if (MTR_CF (cbc) == MTC_BAD) /* otherwise if a bad record was read */
return MTSE_RECE; /* then report a data error */
else /* otherwise */
return MTSE_OK; /* report a successful read */
}
/* Read record or marker forward.
Inputs:
uptr = pointer to tape unit
buf = pointer to buffer
bc = pointer to returned class/record length
max = maximum record size
Outputs:
status = operation status
*/
t_stat sim_tape_rdrecf (UNIT *uptr, uint8 *buf, t_mtrlnt *bc, t_mtrlnt max)
{
return tape_read (uptr, buf, bc, max, FALSE); /* read and return the next record or marker */
}
/* Read record or marker reverse.