1 /* sds_mt.c: SDS 940 magnetic tape simulator
3 Copyright (c) 2001-2006, Robert M. Supnik
5 Permission is hereby granted, free of charge, to any person obtaining a
6 copy of this software and associated documentation files (the "Software"),
7 to deal in the Software without restriction, including without limitation
8 the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 and/or sell copies of the Software, and to permit persons to whom the
10 Software is furnished to do so, subject to the following conditions:
12 The above copyright notice and this permission notice shall be included in
13 all copies or substantial portions of the Software.
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 ROBERT M SUPNIK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 Except as contained in this notice, the name of Robert M Supnik shall not be
23 used in advertising or otherwise to promote the sale, use or other dealings
24 in this Software without prior written authorization from Robert M Supnik.
26 mt 7 track magnetic tape
28 16-Feb-06 RMS Added tape capacity checking
29 07-Dec-04 RMS Added read-only file support
30 25-Apr-03 RMS Revised for extended file support
31 28-Mar-03 RMS Added multiformat support
32 28-Feb-03 RMS Revised for magtape library
34 Magnetic tapes are represented as a series of variable 8b records
37 32b record length in bytes - exact number
43 32b record length in bytes - exact number
45 If the byte count is odd, the record is padded with an extra byte
46 of junk. File marks are represented by a single record length of 0.
47 End of tape is two consecutive end of file marks.
53 #define MT_MAXFR (32768 * 4)
54 #define MT_NUMDR 8 /* number drives */
56 #define botf u3 /* bot tape flag */
57 #define eotf u4 /* eot tape flag */
59 extern uint32 xfr_req
;
60 extern int32 stop_invins
, stop_invdev
, stop_inviop
;
61 int32 mt_inst
= 0; /* saved instr */
62 int32 mt_eof
= 0; /* end of file */
63 int32 mt_gap
= 0; /* in gap */
64 int32 mt_skip
= 0; /* skip rec */
65 int32 mt_bptr
= 0; /* buf ptr */
66 int32 mt_blnt
= 0; /* buf length */
67 int32 mt_ctime
= 10; /* char time */
68 int32 mt_gtime
= 1000; /* gap time */
69 int32 mt_stopioe
= 1; /* stop on err */
70 uint8 mtxb
[MT_MAXFR
]; /* record buffer */
71 DSPT mt_tplt
[] = { /* template */
73 { MT_NUMDR
, DEV_MTS
},
74 { MT_NUMDR
, DEV_OUT
},
75 { MT_NUMDR
, DEV_MTS
+DEV_OUT
},
80 t_stat
mt_svc (UNIT
*uptr
);
81 t_stat
mt_reset (DEVICE
*dptr
);
82 t_stat
mt_boot (int32 unitno
, DEVICE
*dptr
);
83 t_stat
mt_attach (UNIT
*uptr
, char *cptr
);
84 t_stat
mt_detach (UNIT
*uptr
);
85 t_stat
mt_readrec (UNIT
*uptr
);
86 t_mtrlnt
mt_readbc (UNIT
*uptr
);
87 void mt_readend (UNIT
*uptr
);
88 t_stat
mt_wrend (uint32 dev
);
89 void mt_set_err (UNIT
*uptr
);
90 t_stat
mt (uint32 fnc
, uint32 inst
, uint32
*dat
);
92 static const char sds_to_bcd
[64] = {
93 012, 001, 002, 003, 004, 005, 006, 007,
94 010, 011, 012, 013, 014, 015, 016, 017,
95 060, 061, 062, 063, 064, 065, 066, 067,
96 070, 071, 072, 073, 074, 075, 076, 077,
97 040, 041, 042, 043, 044, 045, 046, 047,
98 050, 051, 052, 053, 054, 055, 056, 057,
99 020, 021, 022, 023, 024, 025, 026, 027,
100 030, 031, 032, 033, 034, 035, 036, 037
103 static const char bcd_to_sds
[64] = {
104 000, 001, 002, 003, 004, 005, 006, 007,
105 010, 011, 000, 013, 014, 015, 016, 017,
106 060, 061, 062, 063, 064, 065, 066, 067,
107 070, 071, 072, 073, 074, 075, 076, 077,
108 040, 041, 042, 043, 044, 045, 046, 047,
109 050, 051, 052, 053, 054, 055, 056, 057,
110 020, 021, 022, 023, 024, 025, 026, 027,
111 030, 031, 032, 033, 034, 035, 036, 037
114 /* MT data structures
116 mt_dev MT device descriptor
117 mt_unit MT unit descriptor
118 mt_reg MT register list
121 DIB mt_dib
= { CHAN_W
, DEV_MT
, XFR_MT0
, mt_tplt
, &mt
};
124 { UDATA (&mt_svc
, UNIT_ATTABLE
+ UNIT_ROABLE
+ UNIT_DISABLE
, 0) },
125 { UDATA (&mt_svc
, UNIT_ATTABLE
+ UNIT_ROABLE
+ UNIT_DISABLE
, 0) },
126 { UDATA (&mt_svc
, UNIT_ATTABLE
+ UNIT_ROABLE
+ UNIT_DISABLE
, 0) },
127 { UDATA (&mt_svc
, UNIT_ATTABLE
+ UNIT_ROABLE
+ UNIT_DISABLE
, 0) },
128 { UDATA (&mt_svc
, UNIT_ATTABLE
+ UNIT_ROABLE
+ UNIT_DISABLE
, 0) },
129 { UDATA (&mt_svc
, UNIT_ATTABLE
+ UNIT_ROABLE
+ UNIT_DISABLE
, 0) },
130 { UDATA (&mt_svc
, UNIT_ATTABLE
+ UNIT_ROABLE
+ UNIT_DISABLE
, 0) },
131 { UDATA (&mt_svc
, UNIT_ATTABLE
+ UNIT_ROABLE
+ UNIT_DISABLE
, 0) }
135 { BRDATA (BUF
, mtxb
, 8, 8, MT_MAXFR
) },
136 { DRDATA (BPTR
, mt_bptr
, 18), PV_LEFT
},
137 { DRDATA (BLNT
, mt_blnt
, 18), PV_LEFT
},
138 { FLDATA (XFR
, xfr_req
, XFR_V_MT0
) },
139 { ORDATA (INST
, mt_inst
, 24) },
140 { FLDATA (EOF
, mt_eof
, 0) },
141 { FLDATA (GAP
, mt_gap
, 0) },
142 { FLDATA (SKIP
, mt_skip
, 0) },
143 { DRDATA (CTIME
, mt_ctime
, 24), REG_NZ
+ PV_LEFT
},
144 { DRDATA (GTIME
, mt_gtime
, 24), REG_NZ
+ PV_LEFT
},
145 { URDATA (POS
, mt_unit
[0].pos
, 10, T_ADDR_W
, 0,
146 MT_NUMDR
, PV_LEFT
| REG_RO
) },
147 { URDATA (BOT
, mt_unit
[0].botf
, 10, 1, 0, MT_NUMDR
, REG_RO
) },
148 { URDATA (EOT
, mt_unit
[0].eotf
, 10, 1, 0, MT_NUMDR
, REG_RO
) },
149 { FLDATA (STOP_IOE
, mt_stopioe
, 0) },
154 { MTUF_WLK
, 0, "write enabled", "WRITEENABLED", NULL
},
155 { MTUF_WLK
, MTUF_WLK
, "write locked", "LOCKED", NULL
},
156 { MTAB_XTD
|MTAB_VUN
, 0, "FORMAT", "FORMAT",
157 &sim_tape_set_fmt
, &sim_tape_show_fmt
, NULL
},
158 { MTAB_XTD
|MTAB_VUN
, 0, "CAPACITY", "CAPACITY",
159 &sim_tape_set_capac
, &sim_tape_show_capac
, NULL
},
160 { MTAB_XTD
|MTAB_VDV
, 0, "CHANNEL", "CHANNEL",
161 &set_chan
, &show_chan
, NULL
},
166 "MT", mt_unit
, mt_reg
, mt_mod
,
167 MT_NUMDR
, 10, 31, 1, 8, 8,
168 NULL
, NULL
, &mt_reset
,
169 &mt_boot
, &mt_attach
, NULL
,
175 conn - inst = EOM0, dat = NULL
176 eom1 - inst = EOM1, dat = NULL
177 sks - inst = SKS, dat = ptr to result
178 disc - inst = device number, dat = NULL
179 wreor - inst = device number, dat = NULL
180 read - inst = device number, dat = ptr to data
181 write - inst = device number, dat = ptr to result
184 t_stat
mt (uint32 fnc
, uint32 inst
, uint32
*dat
)
186 int32 u
= inst
& MT_UNIT
; /* get unit */
187 UNIT
*uptr
= mt_dev
.units
+ u
; /* get unit ptr */
192 switch (fnc
) { /* case function */
194 case IO_CONN
: /* connect */
195 new_ch
= I_GETEOCH (inst
); /* get new chan */
196 if (new_ch
!= mt_dib
.chan
) return SCPE_IERR
; /* wrong chan? */
197 if (mt_gap
) { /* in gap? */
198 mt_gap
= 0; /* clr gap flg */
199 sim_cancel (uptr
); /* cancel timer */
201 else if (sim_is_active (uptr
)) CRETIOP
; /* busy? */
202 uptr
->eotf
= 0; /* clr eot flag */
203 mt_eof
= 0; /* clr eof flag */
204 mt_skip
= 0; /* clr skp flag */
205 mt_bptr
= mt_blnt
= 0; /* init buffer */
206 if ((inst
& DEV_MTS
)? (CHC_GETCPW (inst
) < 2): /* scn & cpw<3? */
207 (inst
& CHC_REV
)) return STOP_INVIOP
; /* rw & rev? */
208 mt_inst
= inst
; /* save inst */
209 if ((inst
& DEV_MTS
) && !(inst
&& DEV_OUT
)) /* scanning? */
210 chan_set_flag (mt_dib
.chan
, CHF_SCAN
); /* set chan flg */
211 xfr_req
= xfr_req
& ~XFR_MT0
; /* clr xfr flag */
212 sim_activate (uptr
, mt_gtime
); /* start timer */
215 case IO_EOM1
: /* EOM mode 1 */
216 new_ch
= I_GETEOCH (inst
); /* get new chan */
217 if (new_ch
!= mt_dib
.chan
) CRETIOP
; /* wrong chan? */
218 t
= inst
& 07670; /* get command */
219 if ((t
== 04010) && !sim_is_active (uptr
)) { /* rewind? */
220 sim_tape_rewind (uptr
); /* rewind unit */
221 uptr
->eotf
= 0; /* clr eot */
222 uptr
->botf
= 1; /* set bot */
224 else if ((t
== 03610) && sim_is_active (uptr
) &&/* skip rec? */
225 ((mt_inst
& DEV_OUT
) == 0)) mt_skip
= 1; /* set flag */
229 case IO_DISC
: /* disconnect */
230 sim_cancel (uptr
); /* no more xfr's */
231 if (inst
& DEV_OUT
) { /* write? */
232 if (r
= mt_wrend (inst
)) return r
; /* end record */
236 case IO_WREOR
: /* write eor */
237 chan_set_flag (mt_dib
.chan
, CHF_EOR
); /* set eor flg */
238 if (r
= mt_wrend (inst
)) return r
; /* end record */
239 mt_gap
= 1; /* in gap */
240 sim_activate (uptr
, mt_gtime
); /* start timer */
243 case IO_SKS
: /* SKS */
244 new_ch
= I_GETSKCH (inst
); /* get chan # */
245 if (new_ch
!= mt_dib
.chan
) return SCPE_IERR
; /* wrong chan? */
246 if ((inst
& (DEV_OUT
| DEV_MTS
)) == 0) { /* not sks 1n? */
247 t
= I_GETSKCND (inst
); /* get skip cond */
248 switch (t
) { /* case sks cond */
249 case 001: /* sks 1021n */
250 *dat
= 1; /* not magpak */
252 case 002: /* sks 1041n */
253 if (!(uptr
->flags
& UNIT_ATT
) || /* not ready */
254 sim_is_active (uptr
)) *dat
= 1;
256 case 004: /* sks 1101n */
257 if (!uptr
->eotf
) *dat
= 1; /* not EOT */
259 case 010: /* sks 1201n */
260 if (!uptr
->botf
) *dat
= 1; /* not BOT */
262 case 013: /* sks 12610 */
263 if (!mt_gap
) *dat
= 1; /* not in gap */
265 case 017: /* sks 13610 */
266 if (!mt_eof
) *dat
= 1; /* not EOF */
268 case 020: /* sks 1401n */
269 if (!sim_tape_wrp (uptr
)) *dat
= 1; /* not wrp */
271 case 031: /* sks 1621n */
272 case 033: /* sks 1661n */
273 *dat
= 1; /* not 556bpi */
274 case 035: /* sks 1721n */
275 break; /* not 800bpi */
280 case IO_READ
: /* read */
281 xfr_req
= xfr_req
& ~XFR_MT0
; /* clr xfr flag */
282 if (mt_blnt
== 0) { /* first read? */
283 r
= mt_readrec (uptr
); /* get data */
284 if ((r
!= SCPE_OK
) || (mt_blnt
== 0)) return r
; /* err, inv reclnt? */
286 uptr
->botf
= 0; /* off BOT */
287 if (mt_inst
& CHC_REV
) chr
= mtxb
[--mt_bptr
] & 077; /* get next rev */
288 else chr
= mtxb
[mt_bptr
++] & 077; /* get next fwd */
289 if (!(mt_inst
& CHC_BIN
)) chr
= bcd_to_sds
[chr
];/* bcd? */
290 *dat
= chr
& 077; /* give to chan */
291 if ((mt_inst
& CHC_REV
)? (mt_bptr
<= 0): /* rev or fwd, */
292 (mt_bptr
>= mt_blnt
)) mt_readend (uptr
); /* recd done? */
295 case IO_WRITE
: /* write */
296 uptr
->botf
= 0; /* off BOT */
298 xfr_req
= xfr_req
& ~XFR_MT0
; /* clr xfr flag */
299 if (!(mt_inst
& CHC_BIN
)) chr
= sds_to_bcd
[chr
];/* bcd? */
300 if (mt_bptr
< MT_MAXFR
) mtxb
[mt_bptr
++] = chr
; /* insert in buf */
312 t_stat
mt_svc (UNIT
*uptr
)
314 if (mt_gap
) { /* gap timeout */
315 mt_gap
= 0; /* clr gap flg */
316 chan_disc (mt_dib
.chan
); /* disc chan */
318 else if (mt_skip
) mt_readend (uptr
); /* skip record */
319 else { /* normal xfr */
320 xfr_req
= xfr_req
| XFR_MT0
; /* set xfr req */
321 sim_activate (uptr
, mt_ctime
); /* reactivate */
326 /* Read start (get new record) */
328 t_stat
mt_readrec (UNIT
*uptr
)
333 if ((uptr
->flags
& UNIT_ATT
) == 0) { /* attached? */
334 mt_set_err (uptr
); /* no, err, disc */
337 if (mt_inst
& CHC_REV
) /* reverse? */
338 st
= sim_tape_rdrecr (uptr
, mtxb
, &tbc
, MT_MAXFR
); /* read rec rev */
340 t_bool passed_eot
= sim_tape_eot (uptr
); /* passed EOT? */
341 st
= sim_tape_rdrecf (uptr
, mtxb
, &tbc
, MT_MAXFR
);
342 if (!passed_eot
&& sim_tape_eot (uptr
)) /* just passed eot? */
345 if (st
== MTSE_TMK
) { /* tape mark? */
346 mt_eof
= 1; /* set eof flag */
347 mtxb
[0] = mtxb
[1] = 017; /* EOR char */
348 mt_blnt
= 2; /* store 2 */
351 if (st
!= MTSE_OK
) { /* other error? */
352 mt_set_err (uptr
); /* err, disc */
353 if (st
== MTSE_IOERR
) return SCPE_IOERR
; /* IO error? */
354 if (st
== MTSE_INVRL
) return SCPE_MTRLNT
; /* inv rec lnt? */
355 if (st
== MTSE_EOM
) uptr
->eotf
= 1; /* eom? set eot */
358 mt_blnt
= tbc
; /* set buf lnt */
362 /* Read done (eof, end of record) */
364 void mt_readend (UNIT
*uptr
)
366 sim_cancel (uptr
); /* stop timer */
367 mt_skip
= 0; /* clr skp flg */
368 chan_set_flag (mt_dib
.chan
, CHF_EOR
); /* end record */
369 if (mt_eof
) chan_disc (mt_dib
.chan
); /* EOF? */
371 mt_gap
= 1; /* no, in gap */
372 sim_activate (uptr
, mt_gtime
); /* start timer */
377 /* Write complete (end of record or disconnect) */
379 t_stat
mt_wrend (uint32 dev
)
381 UNIT
*uptr
= mt_dev
.units
+ (dev
& MT_UNIT
);
385 sim_cancel (uptr
); /* no more xfr's */
386 if (mt_bptr
== 0) return SCPE_OK
; /* buf empty? */
387 if (!(uptr
->flags
& UNIT_ATT
)) { /* attached? */
388 mt_set_err (uptr
); /* no, err, disc */
391 if (sim_tape_wrp (uptr
)) { /* write lock? */
392 mt_set_err (uptr
); /* yes, err, disc */
395 if (dev
& DEV_MTS
) { /* erase? */
396 if (mt_inst
& CHC_REV
) /* reverse? */
397 sim_tape_sprecr (uptr
, &tbc
); /* backspace */
398 st
= sim_tape_wreom (uptr
); /* write eom */
401 t_bool passed_eot
= sim_tape_eot (uptr
); /* passed EOT? */
402 if ((mt_bptr
== 1) && (mtxb
[0] == 017) && /* wr eof? */
403 ((mt_inst
& 01670) == 00050))
404 st
= sim_tape_wrtmk (uptr
); /* write tape mark */
405 else st
= sim_tape_wrrecf (uptr
, mtxb
, mt_bptr
); /* write record */
406 if (!passed_eot
&& sim_tape_eot (uptr
)) /* just passed EOT? */
410 if (st
!= MTSE_OK
) mt_set_err (uptr
); /* error? */
411 if (st
== MTSE_IOERR
) return SCPE_IOERR
;
417 void mt_set_err (UNIT
*uptr
)
419 chan_set_flag (mt_dib
.chan
, CHF_EOR
| CHF_ERR
); /* eor, error */
420 chan_disc (mt_dib
.chan
); /* disconnect */
421 xfr_req
= xfr_req
& ~XFR_MT0
; /* clear xfr */
422 sim_cancel (uptr
); /* stop */
423 mt_bptr
= 0; /* buf empty */
428 t_stat
mt_reset (DEVICE
*dptr
)
432 chan_disc (mt_dib
.chan
); /* disconnect */
433 mt_eof
= 0; /* clear state */
437 mt_bptr
= mt_blnt
= 0;
438 xfr_req
= xfr_req
& ~XFR_MT0
; /* clr xfr flag */
439 for (i
= 0; i
< MT_NUMDR
; i
++) { /* deactivate */
440 sim_cancel (&mt_unit
[i
]);
441 sim_tape_reset (&mt_unit
[i
]);
447 /* Attach and detach routines */
449 t_stat
mt_attach (UNIT
*uptr
, char *cptr
)
453 r
= sim_tape_attach (uptr
, cptr
);
454 if (r
!= SCPE_OK
) return r
;
460 t_stat
mt_detach (UNIT
*uptr
)
462 uptr
->botf
= uptr
->eotf
= 0;
463 return sim_tape_detach (uptr
);
466 /* Boot routine - simulate FILL console command */
468 t_stat
mt_boot (int32 unitno
, DEVICE
*dptr
)
470 extern uint32 P
, M
[];
472 if (unitno
) return SCPE_ARG
; /* only unit 0 */
473 M
[0] = 077777771; /* -7B */
474 M
[1] = 007100000; /* LDX 0 */
475 M
[2] = 000203610; /* EOM 3610B */
476 M
[3] = 003200002; /* WIM 2 */
477 M
[4] = 000100002; /* BRU 2 */
478 P
= 1; /* start at 1 */