46ac60c3627a61acef565df7effcbb4bab514ebe
1 /* h316_mt.c: H316/516 magnetic tape simulator
3 Copyright (c) 2003-2007, 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 516-4100 seven track magnetic tape
28 09-Jun-07 RMS Fixed bug in write without stop (from Theo Engel)
29 16-Feb-06 RMS Added tape capacity checking
30 26-Aug-05 RMS Revised to use API for write lock check
31 08-Feb-05 RMS Fixed error reporting from OCP (found by Philipp Hachtmann)
32 01-Dec-04 RMS Fixed bug in DMA/DMC support
34 Magnetic tapes are represented as a series of variable records
45 If the byte count is odd, the record is padded with an extra byte
46 of junk. File marks are represented by a byte count of 0.
49 #include "h316_defs.h"
52 #define MT_NUMDR 4 /* number of drives */
53 #define DB_N_SIZE 16 /* max data buf */
54 #define DBSIZE (1 << DB_N_SIZE) /* max data cmd */
55 #define FNC u3 /* function */
56 #define UST u4 /* unit status */
76 #define FNC_2ND 020 /* second state */
77 #define FNC_NOP (FNC_STOPW|FNC_2ND)
78 #define FNC_EOM 040 /* end of motion */
80 /* Status - unit.UST */
82 #define STA_BOT 0000002 /* beg of tape */
83 #define STA_EOT 0000001 /* end of tape */
85 extern int32 dev_int
, dev_enb
, chan_req
;
86 extern int32 stop_inst
;
88 uint32 mt_buf
= 0; /* data buffer */
89 uint32 mt_usel
= 0; /* unit select */
90 uint32 mt_busy
= 0; /* ctlr busy */
91 uint32 mt_mdirq
= 0; /* motion done int req */
92 uint32 mt_rdy
= 0; /* transfer ready (int) */
93 uint32 mt_err
= 0; /* error */
94 uint32 mt_eof
= 0; /* end of file */
95 uint32 mt_eor
= 0; /* transfer done */
96 uint32 mt_dma
= 0; /* DMA/DMC */
97 uint32 mt_xtime
= 16; /* transfer time */
98 uint32 mt_ctime
= 3000; /* start/stop time */
99 uint32 mt_stopioe
= 1; /* stop on I/O error */
100 uint8 mtxb
[DBSIZE
] = { 0 }; /* data buffer */
101 t_mtrlnt mt_ptr
= 0, mt_max
= 0; /* buffer ptrs */
103 int32
mtio (int32 inst
, int32 fnc
, int32 dat
, int32 dev
);
104 void mt_updint (uint32 rdy
, uint32 mdone
);
105 t_stat
mt_svc (UNIT
*uptr
);
106 t_stat
mt_reset (DEVICE
*dptr
);
107 t_stat
mt_attach (UNIT
*uptr
, char *cptr
);
108 t_stat
mt_detach (UNIT
*uptr
);
109 t_stat
mt_map_err (UNIT
*uptr
, t_stat st
);
110 void mt_wrwd (UNIT
*uptr
, uint32 dat
);
112 /* MT data structures
114 mt_dev MT device descriptor
116 mt_reg MT register list
117 mt_mod MT modifier list
120 DIB mt_dib
= { MT
, IOBUS
, MT_NUMDR
, &mtio
};
123 { UDATA (&mt_svc
, UNIT_ATTABLE
+ UNIT_ROABLE
+ UNIT_DISABLE
, 0) },
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) }
130 { ORDATA (BUF
, mt_buf
, 16) },
131 { ORDATA (USEL
, mt_usel
, 2) },
132 { FLDATA (BUSY
, mt_busy
, 0) },
133 { FLDATA (RDY
, mt_rdy
, 0) },
134 { FLDATA (ERR
, mt_err
, 0) },
135 { FLDATA (EOF
, mt_eof
, 0) },
136 { FLDATA (EOR
, mt_eor
, 0) },
137 { FLDATA (MDIRQ
, mt_mdirq
, 0) },
138 { FLDATA (DMA
, mt_dma
, 0) },
139 { FLDATA (INTREQ
, dev_int
, INT_V_MT
) },
140 { FLDATA (ENABLE
, dev_enb
, INT_V_MT
) },
141 { BRDATA (DBUF
, mtxb
, 8, 8, DBSIZE
) },
142 { DRDATA (BPTR
, mt_ptr
, DB_N_SIZE
+ 1) },
143 { DRDATA (BMAX
, mt_max
, DB_N_SIZE
+ 1) },
144 { DRDATA (CTIME
, mt_ctime
, 24), REG_NZ
+ PV_LEFT
},
145 { DRDATA (XTIME
, mt_xtime
, 24), REG_NZ
+ PV_LEFT
},
146 { URDATA (POS
, mt_unit
[0].pos
, 10, T_ADDR_W
, 0, MT_NUMDR
, PV_LEFT
) },
147 { URDATA (FNC
, mt_unit
[0].FNC
, 8, 8, 0, MT_NUMDR
, REG_HRO
) },
148 { URDATA (UST
, mt_unit
[0].UST
, 8, 2, 0, MT_NUMDR
, REG_HRO
) },
149 { ORDATA (CHAN
, mt_dib
.chan
, 5), REG_HRO
},
150 { FLDATA (STOP_IOE
, mt_stopioe
, 0) },
155 { MTUF_WLK
, 0, "write enabled", "WRITEENABLED", NULL
},
156 { MTUF_WLK
, MTUF_WLK
, "write locked", "LOCKED", NULL
},
157 { MTAB_XTD
|MTAB_VUN
, 0, "FORMAT", "FORMAT",
158 &sim_tape_set_fmt
, &sim_tape_show_fmt
, NULL
},
159 { MTAB_XTD
|MTAB_VUN
, 0, "CAPACITY", "CAPACITY",
160 &sim_tape_set_capac
, &sim_tape_show_capac
, NULL
},
161 { MTAB_XTD
|MTAB_VDV
, 0, NULL
, "IOBUS",
162 &io_set_iobus
, NULL
, NULL
},
163 { MTAB_XTD
|MTAB_VDV
, 0, NULL
, "DMC",
164 &io_set_dmc
, NULL
, NULL
},
165 { MTAB_XTD
|MTAB_VDV
, 0, NULL
, "DMA",
166 &io_set_dma
, NULL
, NULL
},
167 { MTAB_XTD
|MTAB_VDV
, 0, "CHANNEL", NULL
,
168 NULL
, &io_show_chan
, NULL
},
173 "MT", mt_unit
, mt_reg
, mt_mod
,
174 MT_NUMDR
, 10, 31, 1, 8, 8,
175 NULL
, NULL
, &mt_reset
,
176 NULL
, &mt_attach
, &mt_detach
,
182 int32
mtio (int32 inst
, int32 fnc
, int32 dat
, int32 dev
)
184 uint32 i
, u
= dev
& 03;
185 UNIT
*uptr
= mt_dev
.units
+ u
;
186 static uint8 wrt_fnc
[16] = { /* >0 = wr, 1 = chan op */
187 0, 0, 0, 0, 1, 1, 2, 0, 1, 0, 0, 0, 0, 0, 0, 0
190 switch (inst
) { /* case on opcode */
193 mt_updint (mt_rdy
, 0); /* clear motion intr */
194 mt_eof
= 0; /* clear eof */
195 switch (fnc
) { /* case on function */
197 case FNC_DMANM
: /* set DMA/DMC */
199 mt_usel
= u
; /* save unit select */
200 if (mt_dib
.chan
) mt_dma
= 1; /* set DMA if configured */
204 case FNC_IOBUS
: /* set IOBUS */
205 mt_usel
= u
; /* save unit select */
209 case FNC_STOPW
: /* stop write */
210 mt_usel
= u
; /* save unit select */
211 mt_updint (0, mt_mdirq
); /* clear ready */
212 if (wrt_fnc
[uptr
->FNC
& 017] == 1) /* writing? */
213 mt_eor
= 1; /* set transfer done */
216 default: /* motion command */
217 if (mt_busy
) return dat
; /* nop if ctlr busy */
218 mt_eor
= 0; /* clr transfer done */
219 mt_err
= 0; /* clr error */
220 mt_usel
= u
; /* save unit select */
221 if ((uptr
->flags
& UNIT_ATT
) == 0) /* not attached? */
222 return (((mt_stopioe
? SCPE_UNATT
: SCPE_OK
) << IOT_V_REASON
) | dat
);
223 if (sim_is_active (uptr
)) return dat
; /* nop if busy */
224 if (wrt_fnc
[fnc
] && sim_tape_wrp (uptr
))
225 return ((STOP_MTWRP
<< IOT_V_REASON
) | dat
);
229 for (i
= 0; i
< MT_NUMDR
; i
++) /* clear all EOT flags */
230 mt_unit
[i
].UST
= mt_unit
[i
].UST
& ~STA_EOT
;
231 sim_activate (uptr
, mt_ctime
); /* schedule */
236 case ioINA
: /* INA */
237 if (fnc
) return IOBADFNC (dat
); /* fnc 0 only */
238 if (mt_rdy
) { /* ready? */
239 mt_rdy
= 0; /* clear ready */
240 return IOSKIP (dat
| mt_buf
); /* ret buf, skip */
244 case ioOTA
: /* OTA */
245 if (fnc
) return IOBADFNC (dat
); /* fnc 0 only */
246 if (mt_rdy
) { /* ready? */
247 mt_rdy
= 0; /* clear ready */
248 mt_buf
= dat
; /* store buf */
249 return IOSKIP (dat
); /* skip */
254 uptr
= mt_dev
.units
+ mt_usel
; /* use saved unit sel */
257 case 000: /* ready */
258 if (mt_rdy
) return IOSKIP (dat
);
261 case 001: /* !busy */
262 if (!mt_busy
) return IOSKIP (dat
);
265 case 002: /* !error */
266 if (!mt_err
) return IOSKIP (dat
);
270 if (!(uptr
->UST
& STA_BOT
)) return IOSKIP (dat
);
273 case 004: /* !interrupting */
274 if (!TST_INTREQ (INT_MT
)) return IOSKIP (dat
);
278 if (!(uptr
->UST
& STA_EOT
)) return IOSKIP (dat
);
282 if (!mt_eof
) return IOSKIP (dat
);
285 case 007: /* !write prot */
286 if (!sim_tape_wrp (uptr
)) return IOSKIP (dat
);
289 case 011: /* operational */
290 if ((uptr
->flags
& UNIT_ATT
) &&
291 ((uptr
->FNC
& 017) != FNC_REW
)) return IOSKIP (dat
);
294 case 012: /* skip if !chan 2 */
297 case 013: /* skip if !auto */
300 case 014: /* !rewinding */
301 uptr
= mt_dev
.units
+ (dev
& 03); /* use specified unit */
302 if ((uptr
->FNC
& 017) != FNC_REW
) return IOSKIP (dat
);
307 case ioEND
: /* end of range */
308 mt_eor
= 1; /* transfer done */
317 If rewind done, reposition to start of tape, set status
318 else, do operation, set done, interrupt
320 Can't be write locked, can only write lock detached unit
323 t_stat
mt_svc (UNIT
*uptr
)
325 int32 ch
= mt_dib
.chan
- 1; /* DMA/DMC ch */
326 uint32 i
, c1
, c2
, c3
;
329 t_stat st
, r
= SCPE_OK
;
331 if ((uptr
->flags
& UNIT_ATT
) == 0) { /* offline? */
334 mt_updint (0, 1); /* cmd done */
335 return IORETURN (mt_stopioe
, SCPE_UNATT
);
338 passed_eot
= sim_tape_eot (uptr
); /* passed EOT? */
339 switch (uptr
->FNC
) { /* case on function */
341 case FNC_REW
: /* rewind (initial) */
342 mt_busy
= 0; /* ctlr not busy */
343 uptr
->FNC
= uptr
->FNC
| FNC_2ND
;
344 sim_activate (uptr
, mt_ctime
);
345 return SCPE_OK
; /* continue */
347 case FNC_REW
| FNC_2ND
: /* rewind done */
348 uptr
->pos
= 0; /* reposition file */
349 uptr
->UST
= STA_BOT
; /* set BOT */
350 uptr
->FNC
= FNC_NOP
; /* nop function */
351 for (i
= 0; i
< MT_NUMDR
; i
++) { /* last rewind? */
352 if ((mt_unit
[i
].FNC
& 017) == FNC_REW
) return SCPE_OK
;
354 mt_updint (mt_rdy
, 1); /* yes, motion done */
357 case FNC_WEOF
: /* write file mark */
358 if (st
= sim_tape_wrtmk (uptr
)) /* write tmk, err? */
359 r
= mt_map_err (uptr
, st
); /* map error */
360 break; /* sched end motion */
362 case FNC_FSR
: /* space fwd rec */
363 if (st
= sim_tape_sprecf (uptr
, &tbc
)) /* space fwd, err? */
364 r
= mt_map_err (uptr
, st
); /* map error */
365 break; /* sched end motion */
367 case FNC_BSR
: /* space rev rec */
368 if (st
= sim_tape_sprecr (uptr
, &tbc
)) /* space rev, err? */
369 r
= mt_map_err (uptr
, st
); /* map error */
370 break; /* sched end motion */
372 case FNC_FSF
: /* space fwd file */
373 while ((st
= sim_tape_sprecf (uptr
, &tbc
)) == MTSE_OK
) ;
374 r
= mt_map_err (uptr
, st
); /* map error */
375 break; /* sched end motion */
377 case FNC_BSF
: /* space rev file */
378 while ((st
= sim_tape_sprecr (uptr
, &tbc
)) == MTSE_OK
) ;
379 r
= mt_map_err (uptr
, st
); /* map error */
380 break; /* sched end motion */
382 case FNC_EOM
: /* end of motion */
383 uptr
->FNC
= FNC_NOP
; /* nop function */
384 mt_busy
= 0; /* not busy */
385 mt_updint (mt_rdy
, 1); /* end of motion */
386 return SCPE_OK
; /* done! */
388 case FNC_RBCD2
: case FNC_RBIN2
: case FNC_RBIN3
: /* read first */
389 mt_ptr
= 0; /* clr buf ptr */
390 st
= sim_tape_rdrecf (uptr
, mtxb
, &mt_max
, DBSIZE
); /* read rec */
391 if (st
!= MTSE_OK
) { /* error? */
392 r
= mt_map_err (uptr
, st
); /* map error */
393 break; /* sched end motion */
395 uptr
->FNC
= uptr
->FNC
| FNC_2ND
; /* next state */
396 sim_activate (uptr
, mt_xtime
); /* sched xfer */
399 case FNC_RBCD2
| FNC_2ND
: /* read, word */
400 case FNC_RBIN2
| FNC_2ND
:
401 case FNC_RBIN3
| FNC_2ND
:
402 if (mt_ptr
>= mt_max
) break; /* record done? */
403 c1
= mtxb
[mt_ptr
++] & 077; /* get 2 chars */
404 c2
= mtxb
[mt_ptr
++] & 077;
405 if (uptr
->FNC
== (FNC_RBCD2
| FNC_2ND
)) { /* BCD? */
406 if (c1
== 012) c1
= 0; /* change 12 to 0 */
407 if (c2
== 012) c2
= 0;
409 if (uptr
->FNC
== (FNC_RBIN3
| FNC_2ND
)) { /* read 3? */
410 if (mt_ptr
>= mt_max
) break; /* lose wd if not enuf */
411 c3
= mtxb
[mt_ptr
++] & 017; /* get 3rd char */
414 sim_activate (uptr
, mt_xtime
); /* no, sched word */
415 if (mt_eor
) return SCPE_OK
; /* xfer done? */
416 mt_buf
= (c1
<< 10) | (c2
<< 4) | c3
; /* pack chars */
417 if (mt_rdy
) mt_err
= 1; /* buf full? err */
418 mt_updint (1, mt_mdirq
); /* set ready */
419 if (mt_dma
) SET_CH_REQ (ch
); /* DMC/DMA? req chan */
420 return SCPE_OK
; /* continue */
422 case FNC_WBCD2
: case FNC_WBIN2
: case FNC_WBIN3
: /* write first */
423 mt_ptr
= 0; /* clear buf ptr */
424 mt_updint (1, mt_mdirq
); /* set ready */
425 if (mt_dma
) SET_CH_REQ (ch
); /* DMC/DMA? req chan */
426 uptr
->FNC
= uptr
->FNC
| FNC_2ND
; /* next state */
427 sim_activate (uptr
, mt_xtime
); /* sched xfer */
428 return SCPE_OK
; /* continue */
430 case FNC_WBCD2
| FNC_2ND
: /* write, word */
431 case FNC_WBIN2
| FNC_2ND
:
432 case FNC_WBIN3
| FNC_2ND
:
433 if (mt_eor
|| mt_rdy
) { /* done or no data? */
434 if (!mt_rdy
) mt_wrwd (uptr
, mt_buf
); /* write last word */
435 else mt_rdy
= 0; /* rdy must be clr */
436 if (mt_ptr
) { /* any data? */
437 if (st
= sim_tape_wrrecf (uptr
, mtxb
, mt_ptr
)) /* write, err? */
438 r
= mt_map_err (uptr
, st
); /* map error */
440 break; /* sched end motion */
442 mt_wrwd (uptr
, mt_buf
); /* write word */
443 sim_activate (uptr
, mt_xtime
); /* no, sched word */
444 mt_updint (1, mt_mdirq
); /* set ready */
445 if (mt_dma
) SET_CH_REQ (ch
); /* DMC/DMA? req chan */
446 return SCPE_OK
; /* continue */
448 default: /* unknown */
452 /* End of command, process error or schedule end of motion */
454 if (!passed_eot
&& sim_tape_eot (uptr
)) /* just passed EOT? */
455 uptr
->UST
= uptr
->UST
| STA_EOT
;
457 uptr
->FNC
= FNC_NOP
; /* nop function */
458 mt_busy
= 0; /* not busy */
459 mt_updint (mt_rdy
, 1); /* end of motion */
462 uptr
->FNC
= FNC_EOM
; /* sched end motion */
463 sim_activate (uptr
, mt_ctime
);
467 /* Write word to buffer */
469 void mt_wrwd (UNIT
*uptr
, uint32 dat
)
473 c1
= (dat
>> 10) & 077; /* get 2 chars */
474 c2
= (dat
>> 4) & 077;
475 if (uptr
->FNC
== (FNC_WBCD2
| FNC_2ND
)) { /* BCD? */
476 if (c1
== 0) c1
= 012; /* change 0 to 12 */
477 if (c2
== 0) c2
= 012;
479 if (mt_ptr
< DBSIZE
) mtxb
[mt_ptr
++] = c1
; /* store 2 char */
480 if (mt_ptr
< DBSIZE
) mtxb
[mt_ptr
++] = c2
;
481 if ((uptr
->FNC
== (FNC_WBIN3
| FNC_2ND
)) && /* write 3? */
482 (mt_ptr
< DBSIZE
)) mtxb
[mt_ptr
++] = mt_buf
& 017;
486 /* Map tape error status */
488 t_stat
mt_map_err (UNIT
*uptr
, t_stat st
)
492 case MTSE_FMT
: /* illegal fmt */
493 case MTSE_UNATT
: /* unattached */
494 mt_err
= 1; /* reject */
495 case MTSE_OK
: /* no error */
496 return SCPE_IERR
; /* never get here! */
498 case MTSE_TMK
: /* end of file */
499 mt_eof
= 1; /* eof */
502 case MTSE_INVRL
: /* invalid rec lnt */
506 case MTSE_IOERR
: /* IO error */
507 mt_err
= 1; /* error */
508 if (mt_stopioe
) return SCPE_IOERR
;
511 case MTSE_RECE
: /* record in error */
512 case MTSE_EOM
: /* end of medium */
513 mt_err
= 1; /* error */
516 case MTSE_BOT
: /* reverse into BOT */
517 uptr
->UST
= STA_BOT
; /* set status */
520 case MTSE_WRP
: /* write protect */
521 mt_err
= 1; /* error */
528 /* Update interrupts */
530 void mt_updint (uint32 rdy
, uint32 mdirq
)
532 mt_rdy
= rdy
; /* store new ready */
533 mt_mdirq
= mdirq
; /* store new motion irq */
534 if ((mt_rdy
&& !mt_dma
) || mt_mdirq
) SET_INT (INT_MT
); /* update int request */
535 else CLR_INT (INT_MT
);
541 t_stat
mt_reset (DEVICE
*dptr
)
546 mt_buf
= 0; /* clear state */
555 CLR_INT (INT_MT
); /* clear int, enb */
557 for (i
= 0; i
< MT_NUMDR
; i
++) { /* loop thru units */
558 uptr
= mt_dev
.units
+ i
;
559 sim_tape_reset (uptr
); /* reset tape */
560 sim_cancel (uptr
); /* cancel op */
561 uptr
->UST
= uptr
->pos
? 0: STA_BOT
; /* update status */
569 t_stat
mt_attach (UNIT
*uptr
, char *cptr
)
573 r
= sim_tape_attach (uptr
, cptr
); /* attach unit */
574 if (r
!= SCPE_OK
) return r
; /* update status */
581 t_stat
mt_detach (UNIT
* uptr
)
583 uptr
->UST
= 0; /* update status */
584 uptr
->FNC
= FNC_NOP
; /* nop function */
585 return sim_tape_detach (uptr
); /* detach unit */