-
Notifications
You must be signed in to change notification settings - Fork 0
/
sim_tape.c
5170 lines (4522 loc) · 207 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-2008, 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.
23-Jan-12 MP Added support for Logical EOT detection while positioning
05-Feb-11 MP Refactored to prepare for SIM_ASYNC_IO support
Added higher level routines:
sim_tape_wreomrw - erase remainder of tape & rewind
sim_tape_sprecsf - skip records
sim_tape_spfilef - skip files
sim_tape_sprecsr - skip records rev
sim_tape_spfiler - skip files rev
sim_tape_position - general purpose position
These routines correspond to natural tape operations
and will align better when physical tape support is
included here.
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
Public routines:
sim_tape_attach attach tape unit
sim_tape_detach detach tape unit
sim_tape_attach_help help routine for attaching tapes
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_wrtmk write tape mark
sim_tape_wreom erase remainder of tape
sim_tape_wreomrw erase remainder of tape & rewind
sim_tape_wrgap write erase gap
sim_tape_errecf erase record forward
sim_tape_errecr erase record reverse
sim_tape_sprecsf space records forward
sim_tape_spfilef space files forward
sim_tape_sprecsr space records reverse
sim_tape_spfiler space files reverse
sim_tape_position generalized position
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
sim_tape_error_text the textual description of a tape status
sim_tape_set_async enable asynchronous operation
sim_tape_clr_async disable asynchronous operation
aim_tape_test unit test routine
*/
#include "sim_defs.h"
#include "sim_tape.h"
#include "sim_scp_private.h"
#ifndef MAX
#define MAX(a,b) (((a) >= (b)) ? (a) : (b))
#endif
#ifndef MIN
#define MIN(a,b) (((a) <= (b)) ? (a) : (b))
#endif
#if defined SIM_ASYNCH_IO
#include <pthread.h>
#endif
static struct sim_tape_fmt {
const char *name; /* name */
int32 uflags; /* unit flags */
t_addr bot; /* bot test */
t_addr eom_remnant; /* potentially unprocessed data */
} fmts[] = {
{ "SIMH", 0, sizeof (t_mtrlnt) - 1, sizeof (t_mtrlnt) },
{ "E11", 0, sizeof (t_mtrlnt) - 1, sizeof (t_mtrlnt) },
{ "TPC", UNIT_RO, sizeof (t_tpclnt) - 1, sizeof (t_tpclnt) },
{ "P7B", 0, 0, 0 },
{ "AWS", 0, 0, 0 },
{ "TAR", UNIT_RO, 0, 0 },
{ "ANSI", UNIT_RO, 0, 0 },
{ "FIXED", UNIT_RO, 0, 0 },
{ "DOS11", UNIT_RO, 0, 0 },
{ NULL, 0, 0, 0 }
};
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 t_stat sim_tape_aws_wrdata (UNIT *uptr, uint8 *buf, t_mtrlnt bc);
static uint32 sim_tape_tpc_map (UNIT *uptr, t_addr *map, uint32 mapsize);
static t_stat sim_tape_validate_tape (UNIT *uptr);
static t_addr sim_tape_tpc_fnd (UNIT *uptr, t_addr *map);
static void sim_tape_data_trace (UNIT *uptr, const uint8 *data, size_t len, const char* txt, int detail, uint32 reason);
static t_stat tape_erase_fwd (UNIT *uptr, t_mtrlnt gap_size);
static t_stat tape_erase_rev (UNIT *uptr, t_mtrlnt gap_size);
struct tape_context {
DEVICE *dptr; /* Device for unit (access to debug flags) */
uint32 dbit; /* debugging bit for trace */
uint32 auto_format; /* Format determined dynamically */
uint8 *chunk_buf;
uint32 chunk_buf_size;
uint32 chunk_data_size;
uint32 chunk_offset;
#if defined SIM_ASYNCH_IO
t_bool asynch_io; /* Asynchronous Interrupt scheduling enabled */
int asynch_io_latency; /* instructions to delay pending interrupt */
pthread_mutex_t lock;
pthread_t io_thread; /* I/O Thread Id */
pthread_mutex_t io_lock;
pthread_cond_t io_cond;
pthread_cond_t io_done;
pthread_cond_t startup_cond;
int io_top;
uint8 *buf;
uint32 *bc;
uint32 *fc;
uint32 vbc;
uint32 max;
uint32 gaplen;
uint32 bpi;
uint32 *objupdate;
TAPE_PCALLBACK callback;
t_stat io_status;
#endif
};
#define tape_ctx up8 /* Field in Unit structure which points to the tape_context */
#if defined SIM_ASYNCH_IO
#define AIO_CALLSETUP \
struct tape_context *ctx = (struct tape_context *)uptr->tape_ctx; \
\
if (ctx == NULL) \
return sim_messagef (SCPE_IERR, "Bad Attach\n"); \
if ((callback == NULL) || !(ctx->asynch_io))
#define AIO_CALL(op, _buf, _bc, _fc, _max, _vbc, _gaplen, _bpi, _obj, _callback)\
if (ctx->asynch_io) { \
struct tape_context *ctx = \
(struct tape_context *)uptr->tape_ctx; \
\
pthread_mutex_lock (&ctx->io_lock); \
\
sim_debug_unit (ctx->dbit, uptr, \
"sim_tape AIO_CALL(op=%d, unit=%d)\n", op, (int)(uptr-ctx->dptr->units));\
\
if (ctx->callback) /* horrible mistake, stop */ \
SIM_SCP_ABORT ("AIO_CALL error"); \
ctx->io_top = op; \
ctx->buf = _buf; \
ctx->bc = _bc; \
ctx->fc = _fc; \
ctx->max = _max; \
ctx->vbc = _vbc; \
ctx->gaplen = _gaplen; \
ctx->bpi = _bpi; \
ctx->objupdate = _obj; \
ctx->callback = _callback; \
pthread_cond_signal (&ctx->io_cond); \
pthread_mutex_unlock (&ctx->io_lock); \
} \
else \
if (_callback) \
(_callback) (uptr, r);
#define TOP_DONE 0 /* close */
#define TOP_RDRF 1 /* sim_tape_rdrecf_a */
#define TOP_RDRR 2 /* sim_tape_rdrecr_a */
#define TOP_WREC 3 /* sim_tape_wrrecf_a */
#define TOP_WTMK 4 /* sim_tape_wrtmk_a */
#define TOP_WEOM 5 /* sim_tape_wreom_a */
#define TOP_WEMR 6 /* sim_tape_wreomrw_a */
#define TOP_WGAP 7 /* sim_tape_wrgap_a */
#define TOP_SPRF 8 /* sim_tape_sprecf_a */
#define TOP_SRSF 9 /* sim_tape_sprecsf_a */
#define TOP_SPRR 10 /* sim_tape_sprecr_a */
#define TOP_SRSR 11 /* sim_tape_sprecsr_a */
#define TOP_SPFF 12 /* sim_tape_spfilef */
#define TOP_SFRF 13 /* sim_tape_spfilebyrecf */
#define TOP_SPFR 14 /* sim_tape_spfiler */
#define TOP_SFRR 15 /* sim_tape_spfilebyrecr */
#define TOP_RWND 16 /* sim_tape_rewind_a */
#define TOP_POSN 17 /* sim_tape_position_a */
static void *
_tape_io(void *arg)
{
UNIT* volatile uptr = (UNIT*)arg;
struct tape_context *ctx = (struct tape_context *)uptr->tape_ctx;
/* Boost Priority for this I/O thread vs the CPU instruction execution
thread which in general won't be readily yielding the processor when
this thread needs to run */
sim_os_set_thread_priority (PRIORITY_ABOVE_NORMAL);
sim_debug_unit (ctx->dbit, uptr, "_tape_io(unit=%d) starting\n", (int)(uptr-ctx->dptr->units));
pthread_mutex_lock (&ctx->io_lock);
pthread_cond_signal (&ctx->startup_cond); /* Signal we're ready to go */
while (1) {
pthread_cond_wait (&ctx->io_cond, &ctx->io_lock);
if (ctx->io_top == TOP_DONE)
break;
pthread_mutex_unlock (&ctx->io_lock);
switch (ctx->io_top) {
case TOP_RDRF:
ctx->io_status = sim_tape_rdrecf (uptr, ctx->buf, ctx->bc, ctx->max);
break;
case TOP_RDRR:
ctx->io_status = sim_tape_rdrecr (uptr, ctx->buf, ctx->bc, ctx->max);
break;
case TOP_WREC:
ctx->io_status = sim_tape_wrrecf (uptr, ctx->buf, ctx->vbc);
break;
case TOP_WTMK:
ctx->io_status = sim_tape_wrtmk (uptr);
break;
case TOP_WEOM:
ctx->io_status = sim_tape_wreom (uptr);
break;
case TOP_WEMR:
ctx->io_status = sim_tape_wreomrw (uptr);
break;
case TOP_WGAP:
ctx->io_status = sim_tape_wrgap (uptr, ctx->gaplen);
break;
case TOP_SPRF:
ctx->io_status = sim_tape_sprecf (uptr, ctx->bc);
break;
case TOP_SRSF:
ctx->io_status = sim_tape_sprecsf (uptr, ctx->vbc, ctx->bc);
break;
case TOP_SPRR:
ctx->io_status = sim_tape_sprecr (uptr, ctx->bc);
break;
case TOP_SRSR:
ctx->io_status = sim_tape_sprecsr (uptr, ctx->vbc, ctx->bc);
break;
case TOP_SPFF:
ctx->io_status = sim_tape_spfilef (uptr, ctx->vbc, ctx->bc);
break;
case TOP_SFRF:
ctx->io_status = sim_tape_spfilebyrecf (uptr, ctx->vbc, ctx->bc, ctx->fc, ctx->max);
break;
case TOP_SPFR:
ctx->io_status = sim_tape_spfiler (uptr, ctx->vbc, ctx->bc);
break;
case TOP_SFRR:
ctx->io_status = sim_tape_spfilebyrecr (uptr, ctx->vbc, ctx->bc, ctx->fc);
break;
case TOP_RWND:
ctx->io_status = sim_tape_rewind (uptr);
break;
case TOP_POSN:
ctx->io_status = sim_tape_position (uptr, ctx->vbc, ctx->gaplen, ctx->bc, ctx->bpi, ctx->fc, ctx->objupdate);
break;
}
pthread_mutex_lock (&ctx->io_lock);
ctx->io_top = TOP_DONE;
pthread_cond_signal (&ctx->io_done);
sim_activate (uptr, ctx->asynch_io_latency);
}
pthread_mutex_unlock (&ctx->io_lock);
sim_debug_unit (ctx->dbit, uptr, "_tape_io(unit=%d) exiting\n", (int)(uptr-ctx->dptr->units));
return NULL;
}
/* This routine is called in the context of the main simulator thread before
processing events for any unit. It is only called when an asynchronous
thread has called sim_activate() to activate a unit. The job of this
routine is to put the unit in proper condition to digest what may have
occurred in the asynchronous thread.
Since tape processing only handles a single I/O at a time to a
particular tape device, we have the opportunity to possibly detect
improper attempts to issue multiple concurrent I/O requests. */
static void _tape_completion_dispatch (UNIT *uptr)
{
struct tape_context *ctx = (struct tape_context *)uptr->tape_ctx;
TAPE_PCALLBACK callback = ctx->callback;
sim_debug_unit (ctx->dbit, uptr, "_tape_completion_dispatch(unit=%d, top=%d, callback=%p)\n", (int)(uptr-ctx->dptr->units), ctx->io_top, ctx->callback);
if (ctx->io_top != TOP_DONE)
SIM_SCP_ABORT ("_tape_completion_dispatch()"); /* horribly wrong, stop */
if (ctx->asynch_io)
pthread_mutex_lock (&ctx->io_lock);
if (ctx->callback) {
ctx->callback = NULL;
if (ctx->asynch_io)
pthread_mutex_unlock (&ctx->io_lock);
callback (uptr, ctx->io_status);
}
else {
if (ctx->asynch_io)
pthread_mutex_unlock (&ctx->io_lock);
}
}
static t_bool _tape_is_active (UNIT *uptr)
{
struct tape_context *ctx = (struct tape_context *)uptr->tape_ctx;
if (ctx) {
sim_debug_unit (ctx->dbit, uptr, "_tape_is_active(unit=%d, top=%d)\n", (int)(uptr-ctx->dptr->units), ctx->io_top);
return (ctx->io_top != TOP_DONE);
}
return FALSE;
}
static t_bool _tape_cancel (UNIT *uptr)
{
struct tape_context *ctx = (struct tape_context *)uptr->tape_ctx;
if (ctx) {
sim_debug_unit (ctx->dbit, uptr, "_tape_cancel(unit=%d, top=%d)\n", (int)(uptr-ctx->dptr->units), ctx->io_top);
if (ctx->asynch_io) {
pthread_mutex_lock (&ctx->io_lock);
while (ctx->io_top != TOP_DONE)
pthread_cond_wait (&ctx->io_done, &ctx->io_lock);
pthread_mutex_unlock (&ctx->io_lock);
}
}
return FALSE;
}
#else
#define AIO_CALLSETUP \
if (uptr->tape_ctx == NULL) \
return sim_messagef (SCPE_IERR, "Bad Attach\n");
#define AIO_CALL(op, _buf, _fc, _bc, _max, _vbc, _gaplen, _bpi, _obj, _callback) \
if (_callback) \
(_callback) (uptr, r);
#endif
#define MIN_RECORD_SIZE 14 /* Mag tape records <14 bytes are considered noise */
#define MAX_RECORD_SIZE 65535 /* DEC tape controllers have a 16-bit byte count reg */
typedef struct VOL1 {
char type[3]; /* VOL */
char num; /* 1 */
char ident[6]; /* <ansi <a> characters blank padded > */
char accessibity; /* blank */
char reserved1[13]; /* */
char implement[13]; /* */
char owner[14]; /* */
char reserved2[28]; /* */
char standard; /* 1,3 or 4 */
} VOL1;
typedef struct HDR1 { /* Also EOF1, EOV1 */
char type[3]; /* HDR|EOF|EOV */
char num; /* 1 */
char file_ident[17]; /* filename */
char file_set[6]; /* label ident */
char file_section[4]; /* 0001 */
char file_sequence[4]; /* 0001 */
char generation_number[4]; /* 0001 */
char version_number[2]; /* 00 */
char creation_date[6]; /* cyyddd */
char expiration_date[6];
char accessibility; /* space */
char block_count[6]; /* 000000 */
char system_code[13]; /* */
char reserved[7]; /* blank */
} HDR1;
typedef struct HDR2 { /* Also EOF2, EOV2 */
char type[3]; /* HDR */
char num; /* 2 */
char record_format; /* F(fixed)|D(variable)|S(spanned) */
char block_length[5]; /* label ident */
char record_length[5]; /* */
char reserved_os1[21]; /* */
char carriage_control; /* A - Fortran CC, M - Record contained CC, space - CR/LF to be added */
char reserved_os2[13]; /* */
char buffer_offset[2]; /* */
char reserved_std[28]; /* */
} HDR2;
typedef struct HDR3 { /* Also EOF3, EOV3 */
char type[3]; /* HDR */
char num; /* 3 */
char rms_attributes[64]; /* 32 bytes of RMS attributes, converted to hex */
char reserved[12]; /* */
} HDR3;
typedef struct HDR4 { /* Also EOF4, EOV4 */
char type[3]; /* HDR */
char num; /* 4 */
char blank; /* blank */
char extra_name[62]; /* */
char extra_name_used[2]; /* 99 */
char unused[11];
} HDR4;
typedef struct TAPE_RECORD {
uint32 size;
uint8 data[1];
} TAPE_RECORD;
typedef struct MEMORY_TAPE {
uint32 ansi_type; /* ANSI-VMS, ANSI-RT11, ANSI-RSTS, ANSI-RSX11, etc. */
uint32 file_count; /* number of labeled files */
uint32 record_count; /* number of entries in the record array */
uint32 array_size; /* allocated size of records array */
uint32 block_size; /* tape block size */
TAPE_RECORD **records;
VOL1 vol1;
} MEMORY_TAPE;
const char HDR3_RMS_STREAM[] = "HDR3020002040000"
"0000000100000000"
"0000000002000000"
"0000000000000000"
"0000 ";
const char HDR3_RMS_STMLF[] = "HDR3020002050000"
"0000000100000000"
"0000000002000000"
"0000000000000000"
"0000 ";
const char HDR3_RMS_FIXED[] = "HDR3020000010000"
"0000000100000000"
"0000000002000000"
"0000000000000000"
"0000 ";
const char HDR3_RMS_VARRSX[] = "HDR300000A020000"
"0000000100000000"
"0000000000000000"
"0000000000000000"
"0000 ";
const char HDR3_RMS_FIXRSX[] = "HDR3020008010000"
"0000000100000000"
"0000000000000000"
"0000000000000000"
"0000 ";
static struct ansi_tape_parameters {
const char *name; /* operating system */
const char *system_code; /* */
t_bool nohdr2; /* no HDR2 records */
t_bool nohdr3; /* no HDR2 records */
t_bool fixed_text; /* */
char vol1_standard; /* 3 or 4 */
const char *hdr3_fixed; /* HDR3 template for Fixed format files */
const char *hdr3_lf_line_endings; /* HDR3 template for text with LF line ending files */
const char *hdr3_crlf_line_endings;/* HDR3 template for text with CRLF line ending files */
int skip_lf_line_endings;
int skip_crlf_line_endings;
t_bool y2k_date_bug;
t_bool zero_record_length;
char record_format;
char carriage_control;
} ansi_args[] = { /* code nohdr2 nohdr3 fixed_text lvl hdr3 for fixed hdr3 for lf hdr3 for crlf skLF CRLF Y2KDT 0RecLnt RFM CC*/
{"ANSI-VMS" , "DECFILE11A", FALSE, FALSE, FALSE, '3', HDR3_RMS_FIXED, HDR3_RMS_STMLF, HDR3_RMS_STREAM, 0, 0, FALSE, FALSE, 0, 0},
{"ANSI-RSX11" , "DECFILE11A", FALSE, FALSE, FALSE, '4', HDR3_RMS_FIXRSX, HDR3_RMS_VARRSX, HDR3_RMS_VARRSX, 1, 2, FALSE, FALSE, 0, 0},
{"ANSI-RT11" , "DECRT11A", TRUE, TRUE, TRUE, '3', NULL, NULL, NULL, 0, 0, FALSE, FALSE, 0, 0},
{"ANSI-RSTS" , "DECRSTS/E", FALSE, TRUE, TRUE, '3', NULL, NULL, NULL, 0, 0, TRUE, TRUE, 'U', 'M'},
{"ANSI-VAR" , "DECRSTS/E", FALSE, TRUE, FALSE, '3', NULL, NULL, NULL, 1, 2, TRUE, FALSE, 'D', ' '},
{NULL}
};
static MEMORY_TAPE *ansi_create_tape (const char *label, uint32 block_size, uint32 ansi_type);
static MEMORY_TAPE *memory_create_tape (void);
static void memory_free_tape (void *vtape);
static void sim_tape_add_ansi_entry (const char *directory,
const char *filename,
t_offset FileSize,
const struct stat *filestat,
void *context);
static t_bool memory_tape_add_block (MEMORY_TAPE *tape, uint8 *block, uint32 size);
typedef struct DOS11_HDR {
uint16 fname[2]; /* File name (RAD50 - 6 characters) */
uint16 ext; /* Extension (RAD50 - 3 characters) */
uint8 prog; /* Programmer # */
uint8 proj; /* Project # */
uint16 prot; /* Protection */
uint16 date; /* (year - 1970) * 1000 + day of year */
uint16 fname3; /* File name (RAD50 - 3 characters) */
} DOS11_HDR;
#define DOS11_PROT 0233
static void sim_tape_add_dos11_entry (const char *directory,
const char *filename,
t_offset FileSize,
const struct stat *filestat,
void *context);
static t_stat sim_export_tape (UNIT *uptr, const char *export_file);
static FILE *tape_open_and_check_file(const char *filename);
static int tape_classify_file_contents (FILE *f, size_t *max_record_size, t_bool *lf_line_endings, t_bool *crlf_line_endings);
/* Enable asynchronous operation */
t_stat sim_tape_set_async (UNIT *uptr, int latency)
{
#if !defined(SIM_ASYNCH_IO)
return sim_messagef (SCPE_NOFNC, "Tape: can't operate asynchronously\r\n");
#else
struct tape_context *ctx = (struct tape_context *)uptr->tape_ctx;
pthread_attr_t attr;
ctx->asynch_io = sim_asynch_enabled;
ctx->asynch_io_latency = latency;
if (ctx->asynch_io) {
pthread_mutex_init (&ctx->io_lock, NULL);
pthread_cond_init (&ctx->io_cond, NULL);
pthread_cond_init (&ctx->io_done, NULL);
pthread_cond_init (&ctx->startup_cond, NULL);
pthread_attr_init(&attr);
pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
pthread_mutex_lock (&ctx->io_lock);
pthread_create (&ctx->io_thread, &attr, _tape_io, (void *)uptr);
pthread_attr_destroy(&attr);
pthread_cond_wait (&ctx->startup_cond, &ctx->io_lock); /* Wait for thread to stabilize */
pthread_mutex_unlock (&ctx->io_lock);
pthread_cond_destroy (&ctx->startup_cond);
}
uptr->a_check_completion = _tape_completion_dispatch;
uptr->a_is_active = _tape_is_active;
uptr->cancel = _tape_cancel;
return SCPE_OK;
#endif
}
/* Disable asynchronous operation */
t_stat sim_tape_clr_async (UNIT *uptr)
{
#if !defined(SIM_ASYNCH_IO)
return SCPE_NOFNC;
#else
struct tape_context *ctx = (struct tape_context *)uptr->tape_ctx;
/* make sure device exists */
if (!ctx) return SCPE_UNATT;
if (ctx->asynch_io) {
pthread_mutex_lock (&ctx->io_lock);
ctx->asynch_io = FALSE;
pthread_cond_signal (&ctx->io_cond);
pthread_mutex_unlock (&ctx->io_lock);
pthread_join (ctx->io_thread, NULL);
pthread_mutex_destroy (&ctx->io_lock);
pthread_cond_destroy (&ctx->io_cond);
pthread_cond_destroy (&ctx->io_done);
}
return SCPE_OK;
#endif
}
t_stat sim_tape_set_chunk_mode (UNIT *uptr, uint32 chunk_size)
{
uptr->tape_chunk_size = chunk_size;
return SCPE_OK;
}
/*
This routine is called when the simulator stops and any time
the asynch mode is changed (enabled or disabled)
*/
static void _sim_tape_io_flush (UNIT *uptr)
{
#if defined (SIM_ASYNCH_IO)
struct tape_context *ctx = (struct tape_context *)uptr->tape_ctx;
sim_tape_clr_async (uptr);
if (sim_asynch_enabled)
sim_tape_set_async (uptr, ctx->asynch_io_latency);
#endif
if (MT_GET_FMT (uptr) < MTUF_F_ANSI)
fflush (uptr->fileref);
}
static const char *_sim_tape_format_name (UNIT *uptr)
{
int32 f = MT_GET_FMT (uptr);
if (f == MTUF_F_ANSI)
return ansi_args[MT_GET_ANSI_TYP (uptr)].name;
else
return fmts[f].name;
}
/* Attach tape unit */
t_stat sim_tape_attach (UNIT *uptr, CONST char *cptr)
{
DEVICE *dptr;
if ((dptr = find_dev_from_unit (uptr)) == NULL)
return SCPE_NOATT;
return sim_tape_attach_ex (uptr, cptr, ((dptr->flags & DEV_DEBUG) || (dptr->debflags != NULL)) ? MTSE_DBG_API : 0, 0);
}
t_stat sim_tape_attach_ex (UNIT *uptr, const char *cptr, uint32 dbit, int completion_delay)
{
struct tape_context *ctx;
uint32 objc;
DEVICE *dptr;
char gbuf[CBUFSIZE];
char export_file[CBUFSIZE] = "";
t_stat r;
t_bool auto_format = FALSE;
t_bool had_debug = (sim_deb != NULL);
uint32 starting_dctrl = uptr->dctrl;
int32 saved_switches = sim_switches;
MEMORY_TAPE *tape = NULL;
if ((dptr = find_dev_from_unit (uptr)) == NULL)
return SCPE_NOATT;
if (sim_switches & SWMASK ('F')) { /* format spec? */
cptr = get_glyph (cptr, gbuf, 0); /* get spec */
if (*cptr == 0) /* must be more */
return sim_messagef (SCPE_2FARG, "Missing Format specifier and/or filename to attach\n");
if (sim_tape_set_fmt (uptr, 0, gbuf, NULL) != SCPE_OK)
return sim_messagef (SCPE_ARG, "Invalid Tape Format: %s\n", gbuf);
sim_switches = sim_switches & ~(SWMASK ('F')); /* Record Format specifier already processed */
auto_format = TRUE;
}
if (sim_switches & SWMASK ('B')) { /* Record Size (blocking factor)? */
cptr = get_glyph (cptr, gbuf, 0); /* get spec */
if (*cptr == 0) /* must be more */
return sim_messagef (SCPE_2FARG, "Missing Record Size and/or filename to attach\n");
if ((MT_GET_FMT (uptr) != MTUF_F_TAR) && /* -B is honored for TAR, */
(MT_GET_FMT (uptr) != MTUF_F_FIXED) && /* FIXED, */
((MT_GET_FMT (uptr) == MTUF_F_ANSI) &&
((MT_GET_ANSI_TYP (uptr) != MTAT_F_VMS) && /* ANSI-VMS, */
(MT_GET_ANSI_TYP (uptr) != MTAT_F_RSX11) && /* ANSI-RSX11, */
(MT_GET_ANSI_TYP (uptr) != MTAT_F_VAR)))) { /* and ANSI-VAR only */
sim_messagef (SCPE_ARG, "The -B option is ignored for %s format\n", _sim_tape_format_name (uptr));
}
else {
uint32 recsize = (uint32) get_uint (gbuf, 10, MAX_RECORD_SIZE, &r);
if ((r != SCPE_OK) || (recsize < MIN_RECORD_SIZE))
return sim_messagef (SCPE_ARG, "Invalid Tape Record Size: %s\n", gbuf);
uptr->recsize = recsize;
}
sim_switches = sim_switches & ~(SWMASK ('B')); /* Record Blocking Factor */
}
if (sim_switches & SWMASK ('E')) /* On-disk tape image file must exist? */
if (MT_GET_FMT (uptr) >= MTUF_F_ANSI) /* Does not apply to MEMORY_TAPE images */
sim_messagef (SCPE_ARG, "The -E option is ignored for %s format\n", _sim_tape_format_name (uptr));
if (fmts[MT_GET_FMT (uptr)].uflags & UNIT_RO) /* Force ReadOnly attach for TPC, */
sim_switches |= SWMASK ('R'); /* TAR, ANSI, FIXED and DOS11 */
if (sim_switches & SWMASK ('X')) /* Export as SIMH? */
cptr = get_glyph_nc (cptr, export_file, 0); /* get export file spec */
switch (MT_GET_FMT (uptr)) {
case MTUF_F_ANSI:
{
const char *ocptr = cptr;
char label[CBUFSIZE] = "simh";
int file_errors = 0;
if ((MT_GET_ANSI_TYP (uptr) == MTAT_F_RT11) ||
(MT_GET_ANSI_TYP (uptr) == MTAT_F_RSTS))
uptr->recsize = 512;
if (uptr->recsize == 0)
uptr->recsize = 2048;
tape = ansi_create_tape (label, uptr->recsize, MT_GET_ANSI_TYP (uptr));
uptr->fileref = (FILE *)tape;
if (uptr->fileref == NULL)
return SCPE_MEM;
while (*cptr != 0) { /* do all mods */
uint32 initial_file_count = tape->file_count;
cptr = get_glyph_nc (cptr, gbuf, ','); /* get filename */
r = sim_dir_scan (gbuf, sim_tape_add_ansi_entry, tape);
if (r != SCPE_OK)
sim_messagef (SCPE_ARG, "file not found: %s\n", gbuf);
if (tape->file_count == initial_file_count)
++file_errors;
}
if ((tape->file_count > 0) && (file_errors == 0)) {
r = SCPE_OK;
memory_tape_add_block (tape, NULL, 0); /* Tape Mark */
/* RT-11 and RSTS write three tape marks at the end of an ANSI volume */
/* RSX-11 and VMS do not, but there is no harm in the extra tape mark */
memory_tape_add_block (tape, NULL, 0); /* Tape Mark */
uptr->flags |= UNIT_ATT;
uptr->filename = (char *)malloc (strlen (ocptr) + 1);
strcpy (uptr->filename, ocptr);
uptr->tape_eom = tape->record_count;
}
else {
r = SCPE_ARG;
memory_free_tape (uptr->fileref);
uptr->fileref = NULL;
cptr = ocptr;
}
}
break;
case MTUF_F_FIXED:
{
FILE *f;
struct stat statb;
size_t max_record_size;
t_bool lf_line_endings;
t_bool crlf_line_endings;
uint8 *block = NULL;
int error = FALSE;
static const uint8 ascii2ebcdic[128] = {
0000,0001,0002,0003,0067,0055,0056,0057,
0026,0005,0045,0013,0014,0015,0016,0017,
0020,0021,0022,0023,0074,0075,0062,0046,
0030,0031,0077,0047,0034,0035,0036,0037,
0100,0117,0177,0173,0133,0154,0120,0175,
0115,0135,0134,0116,0153,0140,0113,0141,
0360,0361,0362,0363,0364,0365,0366,0367,
0370,0371,0172,0136,0114,0176,0156,0157,
0174,0301,0302,0303,0304,0305,0306,0307,
0310,0311,0321,0322,0323,0324,0325,0326,
0327,0330,0331,0342,0343,0344,0345,0346,
0347,0350,0351,0112,0340,0132,0137,0155,
0171,0201,0202,0203,0204,0205,0206,0207,
0210,0211,0221,0222,0223,0224,0225,0226,
0227,0230,0231,0242,0243,0244,0245,0246,
0247,0250,0251,0300,0152,0320,0241,0007};
memset (&statb, 0, sizeof (statb));
tape = memory_create_tape ();
uptr->fileref = (FILE *)tape;
if (uptr->fileref == NULL)
return SCPE_MEM;
f = fopen (cptr, "rb");
if (f == NULL) {
r = sim_messagef (SCPE_OPENERR, "Can't open: %s - %s\n", cptr, strerror (errno));
break;
}
if (fstat (fileno (f), &statb) != 0) {
r = sim_messagef (SCPE_OPENERR, "Can't stat: %s - %s\n", cptr, strerror (errno));
fclose (f);
break;
}
r = SCPE_OK;
tape_classify_file_contents (f, &max_record_size, &lf_line_endings, &crlf_line_endings);
if (!lf_line_endings && !crlf_line_endings) { /* binary file? */
if (uptr->recsize == 0)
uptr->recsize = 512;
if ((statb.st_size % uptr->recsize) != 0) {
r = sim_messagef (SCPE_ARG, "Binary file data is not a multiple of the specifyed record size (%d)\n", (int)uptr->recsize);
fclose (f);
break;
}
tape->block_size = uptr->recsize;
block = (uint8 *)malloc (tape->block_size);
while (!feof (f) && !error) {
size_t data_read = fread (block, 1, tape->block_size, f);
if (data_read == tape->block_size)
error = memory_tape_add_block (tape, block, tape->block_size);
else {
if (data_read != 0) {
r = sim_messagef (SCPE_ARG, "Read %u bytes of data when expecting %u bytes\n", (uint32)data_read, (uint32)tape->block_size);
error = TRUE;
}
}
}
}
else { /* text file */
if (uptr->recsize == 0)
uptr->recsize = max_record_size;
if (uptr->recsize < max_record_size) {
r = sim_messagef (SCPE_ARG, "Text file: %s has lines longer than %d. Max Line Size: %d\n", cptr, (int)uptr->recsize, (int)max_record_size);
fclose (f);
break;
}
tape->block_size = uptr->recsize;
block = (uint8 *)calloc (1, tape->block_size + 3);
while (!feof (f) && !error) {
if (fgets ((char *)block, tape->block_size + 3, f)) {
size_t len = strlen ((char *)block);
while ((len > 0) &&
((block[len - 1] == '\r') || (block[len - 1] == '\n')))
--len;
memset (block + len, ' ', tape->block_size - len);
if (sim_switches & SWMASK ('C')) {
uint32 i;
for (i = 0; i < tape->block_size; i++)
block[i] = ascii2ebcdic[block[i]];
}
error = memory_tape_add_block (tape, block, tape->block_size);
}
else
error = ferror (f);
}
}
free (block);
fclose (f);
if (error)
r = sim_messagef (SCPE_IERR, "Error processing input file %s\n", cptr);
else {
memory_tape_add_block (tape, NULL, 0); /* Tape Mark */
memory_tape_add_block (tape, NULL, 0); /* Tape Mark */
uptr->flags |= UNIT_ATT;
uptr->filename = (char *)malloc (strlen (cptr) + 1);
strcpy (uptr->filename, cptr);
uptr->tape_eom = tape->record_count;
}
}
break;
case MTUF_F_DOS11:
{
const char *ocptr = cptr;
int file_errors = 0;
uptr->recsize = 512;
tape = memory_create_tape();
tape->block_size = uptr->recsize;
uptr->fileref = (FILE *)tape;
if (uptr->fileref == NULL)
return SCPE_MEM;
while (*cptr != 0) {
uint32 initial_file_count = tape->file_count;
cptr = get_glyph_nc (cptr, gbuf, ','); /* Get filename */
r = sim_dir_scan (gbuf, sim_tape_add_dos11_entry, tape);
if (r != SCPE_OK)
sim_messagef (SCPE_ARG, "file not found: %s\n", gbuf);
if (tape->file_count == initial_file_count)
++file_errors;
}
if ((tape->file_count > 0) && (file_errors == 0)) {
r = SCPE_OK;
memory_tape_add_block (tape, NULL, 0); /* Tape Mark */
/* RSX-11 and RSTS write three tape marks at the end of a DOS volume */
/* VMS does not, but there is no harm in the extra tape mark */
memory_tape_add_block (tape, NULL, 0); /* Tape Mark */
uptr->flags |= UNIT_ATT;
uptr->filename = (char *)malloc (strlen (ocptr) + 1);
strcpy (uptr->filename, ocptr);
uptr->tape_eom = tape->record_count;
}
else {
r = SCPE_ARG;
cptr = ocptr;
}
}
break;
case MTUF_F_TAR:
if (uptr->recsize == 0)
uptr->recsize = TAR_DFLT_RECSIZE; /* Apply default block size */
if ((uptr->recsize % 512) != 0)
return sim_messagef (SCPE_ARG, "TAR format block size of %u is not a multiple of 512\n", uptr->recsize);
sim_switches |= SWMASK ('E'); /* The TAR file must exist */
/* fall through */
default:
r = attach_unit (uptr, (CONST char *)cptr); /* attach unit */
break;
}
if (r != SCPE_OK) { /* error? */
if (MT_GET_FMT (uptr) >= MTUF_F_ANSI) {
r = sim_messagef (r, "Error opening %s format internal tape image generated from: '%s'\n", _sim_tape_format_name (uptr), cptr);
memory_free_tape (uptr->fileref);
uptr->fileref = NULL;
}
else
r = sim_messagef (r, "Error opening %s format tape image: '%s' - %s\n", _sim_tape_format_name (uptr), cptr, strerror(errno));
if (auto_format) /* format was specified at attach time? */
sim_tape_set_fmt (uptr, 0, "SIMH", NULL); /* restore default format */
uptr->recsize = 0;
uptr->tape_eom = 0;
return r;
}
if ((sim_switches & SWMASK ('D')) && !had_debug) {
sim_switches |= SWMASK ('E');
sim_switches &= ~(SWMASK ('D') | SWMASK ('R') | SWMASK ('F'));
sim_set_debon (0, "STDOUT");
sim_switches = saved_switches;
}
if (sim_switches & SWMASK ('D'))
uptr->dctrl = MTSE_DBG_STR | MTSE_DBG_DAT;
uptr->tape_ctx = ctx = (struct tape_context *)calloc(1, sizeof(struct tape_context));
ctx->dptr = dptr; /* save DEVICE pointer */
ctx->dbit = dbit; /* save debug bit */
ctx->auto_format = auto_format; /* save that we auto selected format */
switch (MT_GET_FMT (uptr)) { /* case on format */
case MTUF_F_TPC: /* TPC */
objc = sim_tape_tpc_map (uptr, NULL, 0); /* get # objects */
if (objc == 0) { /* tape empty? */
sim_tape_detach (uptr);
r = SCPE_FMT; /* yes, complain */
}
uptr->filebuf = calloc (objc + 1, sizeof (t_addr));
if (uptr->filebuf == NULL) { /* map allocated? */
sim_tape_detach (uptr);
r = SCPE_MEM; /* no, complain */
}
uptr->hwmark = objc + 1; /* save map size */
sim_tape_tpc_map (uptr, (t_addr *) uptr->filebuf, objc);/* fill map */
break;
case MTUF_F_TAR: /* TAR */
uptr->hwmark = (t_addr)sim_fsize (uptr->fileref);
break;
default:
break;
}
if (r == SCPE_OK) {
sim_tape_validate_tape (uptr);
sim_tape_rewind (uptr);
#if defined (SIM_ASYNCH_IO)
sim_tape_set_async (uptr, completion_delay);
#endif
uptr->io_flush = _sim_tape_io_flush;
}
if ((sim_switches & SWMASK ('D')) && !had_debug)
sim_set_deboff (0, NULL);
if (sim_switches & SWMASK ('D'))
uptr->dctrl = starting_dctrl;
if ((r == SCPE_OK) && (sim_switches & SWMASK ('X')))
r = sim_export_tape (uptr, export_file);
return r;
}
/* Detach tape unit */
t_stat sim_tape_detach (UNIT *uptr)
{
struct tape_context *ctx;
uint32 f;
t_bool auto_format = FALSE;