First Commit of my working state
[simh.git] / SDS / sds_mt.c
1 /* sds_mt.c: SDS 940 magnetic tape simulator
2
3 Copyright (c) 2001-2006, Robert M. Supnik
4
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:
11
12 The above copyright notice and this permission notice shall be included in
13 all copies or substantial portions of the Software.
14
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.
21
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.
25
26 mt 7 track magnetic tape
27
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
33
34 Magnetic tapes are represented as a series of variable 8b records
35 of the form:
36
37 32b record length in bytes - exact number
38 byte 0
39 byte 1
40 :
41 byte n-2
42 byte n-1
43 32b record length in bytes - exact number
44
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.
48 */
49
50 #include "sds_defs.h"
51 #include "sim_tape.h"
52
53 #define MT_MAXFR (32768 * 4)
54 #define MT_NUMDR 8 /* number drives */
55 #define MT_UNIT 07
56 #define botf u3 /* bot tape flag */
57 #define eotf u4 /* eot tape flag */
58
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 */
72 { MT_NUMDR, 0 },
73 { MT_NUMDR, DEV_MTS },
74 { MT_NUMDR, DEV_OUT },
75 { MT_NUMDR, DEV_MTS+DEV_OUT },
76 { 0, 0 }
77 };
78
79 DEVICE mt_dev;
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);
91
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
101 };
102
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
112 };
113
114 /* MT data structures
115
116 mt_dev MT device descriptor
117 mt_unit MT unit descriptor
118 mt_reg MT register list
119 */
120
121 DIB mt_dib = { CHAN_W, DEV_MT, XFR_MT0, mt_tplt, &mt };
122
123 UNIT mt_unit[] = {
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) }
132 };
133
134 REG mt_reg[] = {
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) },
150 { NULL }
151 };
152
153 MTAB mt_mod[] = {
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 },
162 { 0 }
163 };
164
165 DEVICE mt_dev = {
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,
170 &mt_dib, DEV_DISABLE
171 };
172
173 /* Mag tape routine
174
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
182 */
183
184 t_stat mt (uint32 fnc, uint32 inst, uint32 *dat)
185 {
186 int32 u = inst & MT_UNIT; /* get unit */
187 UNIT *uptr = mt_dev.units + u; /* get unit ptr */
188 int32 t, new_ch;
189 uint8 chr;
190 t_stat r;
191
192 switch (fnc) { /* case function */
193
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 */
200 }
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 */
213 break;
214
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 */
223 }
224 else if ((t == 03610) && sim_is_active (uptr) &&/* skip rec? */
225 ((mt_inst & DEV_OUT) == 0)) mt_skip = 1; /* set flag */
226 else CRETINS;
227 break;
228
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 */
233 }
234 break;
235
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 */
241 break;
242
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 */
251 break;
252 case 002: /* sks 1041n */
253 if (!(uptr->flags & UNIT_ATT) || /* not ready */
254 sim_is_active (uptr)) *dat = 1;
255 break;
256 case 004: /* sks 1101n */
257 if (!uptr->eotf) *dat = 1; /* not EOT */
258 break;
259 case 010: /* sks 1201n */
260 if (!uptr->botf) *dat = 1; /* not BOT */
261 break;
262 case 013: /* sks 12610 */
263 if (!mt_gap) *dat = 1; /* not in gap */
264 break;
265 case 017: /* sks 13610 */
266 if (!mt_eof) *dat = 1; /* not EOF */
267 break;
268 case 020: /* sks 1401n */
269 if (!sim_tape_wrp (uptr)) *dat = 1; /* not wrp */
270 break;
271 case 031: /* sks 1621n */
272 case 033: /* sks 1661n */
273 *dat = 1; /* not 556bpi */
274 case 035: /* sks 1721n */
275 break; /* not 800bpi */
276 }
277 } /* end if */
278 break;
279
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? */
285 }
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? */
293 break;
294
295 case IO_WRITE: /* write */
296 uptr->botf = 0; /* off BOT */
297 chr = (*dat) & 077;
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 */
301 break;
302
303 default:
304 CRETINS;
305 }
306
307 return SCPE_OK;
308 }
309
310 /* Unit service */
311
312 t_stat mt_svc (UNIT *uptr)
313 {
314 if (mt_gap) { /* gap timeout */
315 mt_gap = 0; /* clr gap flg */
316 chan_disc (mt_dib.chan); /* disc chan */
317 }
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 */
322 }
323 return SCPE_OK;
324 }
325
326 /* Read start (get new record) */
327
328 t_stat mt_readrec (UNIT *uptr)
329 {
330 t_mtrlnt tbc;
331 t_stat st;
332
333 if ((uptr->flags & UNIT_ATT) == 0) { /* attached? */
334 mt_set_err (uptr); /* no, err, disc */
335 return SCPE_UNATT;
336 }
337 if (mt_inst & CHC_REV) /* reverse? */
338 st = sim_tape_rdrecr (uptr, mtxb, &tbc, MT_MAXFR); /* read rec rev */
339 else { /* no, fwd */
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? */
343 uptr->eotf = 1;
344 }
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 */
349 return SCPE_OK;
350 }
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 */
356 return SCPE_OK;
357 }
358 mt_blnt = tbc; /* set buf lnt */
359 return SCPE_OK;
360 }
361
362 /* Read done (eof, end of record) */
363
364 void mt_readend (UNIT *uptr)
365 {
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? */
370 else {
371 mt_gap = 1; /* no, in gap */
372 sim_activate (uptr, mt_gtime); /* start timer */
373 }
374 return;
375 }
376
377 /* Write complete (end of record or disconnect) */
378
379 t_stat mt_wrend (uint32 dev)
380 {
381 UNIT *uptr = mt_dev.units + (dev & MT_UNIT);
382 t_mtrlnt tbc;
383 t_stat st;
384
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 */
389 return SCPE_UNATT;
390 }
391 if (sim_tape_wrp (uptr)) { /* write lock? */
392 mt_set_err (uptr); /* yes, err, disc */
393 return SCPE_OK;
394 }
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 */
399 }
400 else {
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? */
407 uptr->eotf = 1;
408 }
409 mt_bptr = 0;
410 if (st != MTSE_OK) mt_set_err (uptr); /* error? */
411 if (st == MTSE_IOERR) return SCPE_IOERR;
412 return SCPE_OK;
413 }
414
415 /* Fatal error */
416
417 void mt_set_err (UNIT *uptr)
418 {
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 */
424 return;
425 }
426 /* Reset routine */
427
428 t_stat mt_reset (DEVICE *dptr)
429 {
430 int32 i;
431
432 chan_disc (mt_dib.chan); /* disconnect */
433 mt_eof = 0; /* clear state */
434 mt_gap = 0;
435 mt_skip = 0;
436 mt_inst = 0;
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]);
442 mt_unit[i].eotf = 0;
443 }
444 return SCPE_OK;
445 }
446
447 /* Attach and detach routines */
448
449 t_stat mt_attach (UNIT *uptr, char *cptr)
450 {
451 t_stat r;
452
453 r = sim_tape_attach (uptr, cptr);
454 if (r != SCPE_OK) return r;
455 uptr->botf = 1;
456 uptr->eotf = 0;
457 return SCPE_OK;
458 }
459
460 t_stat mt_detach (UNIT *uptr)
461 {
462 uptr->botf = uptr->eotf = 0;
463 return sim_tape_detach (uptr);
464 }
465
466 /* Boot routine - simulate FILL console command */
467
468 t_stat mt_boot (int32 unitno, DEVICE *dptr)
469 {
470 extern uint32 P, M[];
471
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 */
479 return SCPE_OK;
480 }