1 /* i1401_mt.c: IBM 1401 magnetic tape simulator
3 Copyright (c) 1993-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.
28 07-Jul-07 RMS Removed restriction on load-mode binary tape
29 28-Jun-07 RMS Revised read tape mark behavior based on real hardware
31 16-Feb-06 RMS Added tape capacity checking
32 15-Sep-05 RMS Yet another fix to load read group mark plus word mark
33 Added debug printouts (from Van Snyder)
34 26-Aug-05 RMS Revised to use API for write lock check
35 16-Aug-03 RMS End-of-record on load read works like move read
36 (verified on real 1401)
37 Added diagnostic read (space forward)
38 25-Apr-03 RMS Revised for extended file support
39 28-Mar-03 RMS Added multiformat support
40 15-Mar-03 RMS Fixed end-of-record on load read yet again
41 28-Feb-03 RMS Modified for magtape library
42 31-Oct-02 RMS Added error record handling
43 10-Oct-02 RMS Fixed end-of-record on load read writes WM plus GM
44 30-Sep-02 RMS Revamped error handling
45 28-Aug-02 RMS Added end of medium support
46 12-Jun-02 RMS End-of-record on move read preserves old WM under GM
48 03-Jun-02 RMS Modified for 1311 support
49 30-May-02 RMS Widened POS to 32b
50 22-Apr-02 RMS Added protection against bad record lengths
51 30-Jan-02 RMS New zero footprint tape bootstrap from Van Snyder
52 20-Jan-02 RMS Changed write enabled modifier
53 29-Nov-01 RMS Added read only unit support
54 18-Apr-01 RMS Changed to rewind tape before boot
55 07-Dec-00 RMS Widened display width from 6 to 8 bits to see record lnt
56 CEO Added tape bootstrap
57 14-Apr-99 RMS Changed t_addr to unsigned
58 04-Oct-98 RMS V2.4 magtape format
60 Magnetic tapes are represented as a series of variable 16b records
71 If the byte count is odd, the record is padded with an extra byte
72 of junk. File marks are represented by a byte count of 0.
75 #include "i1401_defs.h"
78 #define MT_NUMDR 7 /* #drives */
79 #define MT_MAXFR (MAXMEMSIZE * 2) /* max transfer */
81 uint8 dbuf
[MT_MAXFR
]; /* tape buffer */
83 extern uint8 M
[]; /* memory */
85 extern int32 BS
, iochk
;
89 t_stat
mt_reset (DEVICE
*dptr
);
90 t_stat
mt_boot (int32 unitno
, DEVICE
*dptr
);
91 t_stat
mt_map_status (t_stat st
);
92 UNIT
*get_unit (int32 unit
);
96 mt_dev MT device descriptor
98 mt_reg MT register list
99 mt_mod MT modifier list
103 { UDATA (NULL
, UNIT_DIS
, 0) }, /* doesn't exist */
104 { UDATA (NULL
, UNIT_DISABLE
+ UNIT_ATTABLE
+
105 UNIT_ROABLE
+ UNIT_BCD
, 0) },
106 { UDATA (NULL
, UNIT_DISABLE
+ UNIT_ATTABLE
+
107 UNIT_ROABLE
+ UNIT_BCD
, 0) },
108 { UDATA (NULL
, UNIT_DISABLE
+ UNIT_ATTABLE
+
109 UNIT_ROABLE
+ UNIT_BCD
, 0) },
110 { UDATA (NULL
, UNIT_DISABLE
+ UNIT_ATTABLE
+
111 UNIT_ROABLE
+ UNIT_BCD
, 0) },
112 { UDATA (NULL
, UNIT_DISABLE
+ UNIT_ATTABLE
+
113 UNIT_ROABLE
+ UNIT_BCD
, 0) },
114 { UDATA (NULL
, UNIT_DISABLE
+ UNIT_ATTABLE
+
115 UNIT_ROABLE
+ UNIT_BCD
, 0) }
119 { FLDATA (END
, ind
[IN_END
], 0) },
120 { FLDATA (ERR
, ind
[IN_TAP
], 0) },
121 { DRDATA (POS1
, mt_unit
[1].pos
, T_ADDR_W
), PV_LEFT
+ REG_RO
},
122 { DRDATA (POS2
, mt_unit
[2].pos
, T_ADDR_W
), PV_LEFT
+ REG_RO
},
123 { DRDATA (POS3
, mt_unit
[3].pos
, T_ADDR_W
), PV_LEFT
+ REG_RO
},
124 { DRDATA (POS4
, mt_unit
[4].pos
, T_ADDR_W
), PV_LEFT
+ REG_RO
},
125 { DRDATA (POS5
, mt_unit
[5].pos
, T_ADDR_W
), PV_LEFT
+ REG_RO
},
126 { DRDATA (POS6
, mt_unit
[6].pos
, T_ADDR_W
), PV_LEFT
+ REG_RO
},
131 { MTUF_WLK
, 0, "write enabled", "WRITEENABLED", NULL
},
132 { MTUF_WLK
, MTUF_WLK
, "write locked", "LOCKED", NULL
},
133 { MTAB_XTD
|MTAB_VUN
, 0, "FORMAT", "FORMAT",
134 &sim_tape_set_fmt
, &sim_tape_show_fmt
, NULL
},
135 { MTAB_XTD
|MTAB_VUN
, 0, "CAPACITY", "CAPACITY",
136 &sim_tape_set_capac
, &sim_tape_show_capac
, NULL
},
141 "MT", mt_unit
, mt_reg
, mt_mod
,
142 MT_NUMDR
, 10, 31, 1, 8, 8,
143 NULL
, NULL
, &mt_reset
,
144 &mt_boot
, &sim_tape_attach
, &sim_tape_detach
,
151 unit = unit character
152 mod = modifier character
157 t_stat
mt_func (int32 unit
, int32 mod
)
163 if ((uptr
= get_unit (unit
)) == NULL
) return STOP_INVMTU
; /* valid unit? */
164 if ((uptr
->flags
& UNIT_ATT
) == 0) return SCPE_UNATT
; /* attached? */
165 switch (mod
) { /* case on modifier */
167 case BCD_A
: /* diagnostic read */
168 if (DEBUG_PRS (mt_dev
)) fprintf (sim_deb
,
169 ">>MT%d: diagnostic read\n", unit
);
170 ind
[IN_END
] = 0; /* clear end of file */
171 st
= sim_tape_sprecf (uptr
, &tbc
); /* space fwd */
174 case BCD_B
: /* backspace */
175 if (DEBUG_PRS (mt_dev
)) fprintf (sim_deb
,
176 ">>MT%d: backspace\n", unit
);
177 ind
[IN_END
] = 0; /* clear end of file */
178 st
= sim_tape_sprecr (uptr
, &tbc
); /* space rev */
179 break; /* end case */
181 case BCD_E
: /* erase = nop */
182 if (DEBUG_PRS (mt_dev
)) fprintf (sim_deb
,
183 ">>MT%d: erase\n", unit
);
184 if (sim_tape_wrp (uptr
)) return STOP_MTL
;
187 case BCD_M
: /* write tapemark */
188 if (DEBUG_PRS (mt_dev
)) fprintf (sim_deb
,
189 ">>MT%d: write tape mark\n", unit
);
190 st
= sim_tape_wrtmk (uptr
); /* write tmk */
193 case BCD_R
: /* rewind */
194 if (DEBUG_PRS (mt_dev
)) fprintf (sim_deb
,
195 ">>MT%d: rewind\n", unit
);
196 sim_tape_rewind (uptr
); /* update position */
199 case BCD_U
: /* unload */
200 if (DEBUG_PRS (mt_dev
)) fprintf (sim_deb
,
201 ">>MT%d: rewind and unload\n", unit
);
202 sim_tape_rewind (uptr
); /* update position */
203 return detach_unit (uptr
); /* detach */
209 return mt_map_status (st
);
212 /* Read and write routines
215 unit = unit character
216 flag = normal, word mark, or binary mode
217 mod = modifier character
221 Fine point: after a read, the system writes a group mark just
222 beyond the end of the record. However, first it checks for a
223 GM + WM; if present, the GM + WM is not changed. Otherwise,
224 an MCW read sets a GM, preserving the current WM; while an LCA
225 read sets a GM and clears the WM.
228 t_stat
mt_io (int32 unit
, int32 flag
, int32 mod
)
236 if ((uptr
= get_unit (unit
)) == NULL
) return STOP_INVMTU
; /* valid unit? */
237 if ((uptr
->flags
& UNIT_ATT
) == 0) return SCPE_UNATT
; /* attached? */
241 case BCD_R
: /* read */
242 if (DEBUG_PRS (mt_dev
))
243 fprintf (sim_deb
, ">>MT%d: read from %d", unit
, BS
);
244 ind
[IN_TAP
] = ind
[IN_END
] = 0; /* clear error */
245 wm_seen
= 0; /* no word mk seen */
246 st
= sim_tape_rdrecf (uptr
, dbuf
, &tbc
, MT_MAXFR
); /* read rec */
247 if (st
== MTSE_RECE
) ind
[IN_TAP
] = 1; /* rec in error? */
248 else if (st
== MTSE_TMK
) { /* tape mark? */
249 ind
[IN_END
] = 1; /* set indicator */
250 tbc
= 1; /* one char read */
251 dbuf
[0] = BCD_TAPMRK
; /* BCD tapemark */
253 else if (st
!= MTSE_OK
) { /* stop on error */
254 if (DEBUG_PRS (mt_dev
))
255 fprintf (sim_deb
, ", stopped by status = %d\n", st
);
258 for (i
= 0; i
< tbc
; i
++) { /* loop thru buf */
259 if (M
[BS
] == (BCD_GRPMRK
+ WM
)) { /* GWM in memory? */
260 if (DEBUG_PRS (mt_dev
))
261 fprintf (sim_deb
, " to %d, stopped by GMWM\n", BS
);
263 if (ADDR_ERR (BS
)) { /* test for wrap */
264 BS
= BA
| (BS
% MAXMEMSIZE
);
267 return SCPE_OK
; /* done */
269 t
= dbuf
[i
]; /* get char */
270 if (!(flag
& MD_BIN
) && (t
== BCD_ALT
)) /* BCD mode alt blank? */
271 t
= BCD_BLANK
; /* real blank */
272 if (flag
& MD_WM
) { /* word mk mode? */
273 if ((t
== BCD_WM
) && (wm_seen
== 0)) /* WM char, none prev? */
274 wm_seen
= WM
; /* set flag */
276 M
[BS
] = wm_seen
| (t
& CHAR
); /* char + wm seen */
277 wm_seen
= 0; /* clear flag */
280 else M
[BS
] = (M
[BS
] & WM
) | (t
& CHAR
); /* preserve mem WM */
282 if (ADDR_ERR (BS
)) { /* check next BS */
283 BS
= BA
| (BS
% MAXMEMSIZE
);
287 if (M
[BS
] != (BCD_GRPMRK
+ WM
)) { /* not GM+WM at end? */
288 if (flag
& MD_WM
) M
[BS
] = BCD_GRPMRK
; /* LCA: clear WM */
289 else M
[BS
] = (M
[BS
] & WM
) | BCD_GRPMRK
; /* MCW: save WM */
291 if (DEBUG_PRS (mt_dev
))
292 fprintf (sim_deb
, " to %d, stopped by EOR\n", BS
);
294 if (ADDR_ERR (BS
)) { /* check final BS */
295 BS
= BA
| (BS
% MAXMEMSIZE
);
301 if (sim_tape_wrp (uptr
)) return STOP_MTL
; /* locked? */
302 if (M
[BS
] == (BCD_GRPMRK
+ WM
)) return STOP_MTZ
;/* eor? */
303 if (DEBUG_PRS (mt_dev
))
304 fprintf (sim_deb
, ">>MT%d: write from %d", unit
, BS
);
305 ind
[IN_TAP
] = ind
[IN_END
] = 0; /* clear error */
306 for (tbc
= 0; (t
= M
[BS
++]) != (BCD_GRPMRK
+ WM
); ) {
307 if ((t
& WM
) && (flag
& MD_WM
)) /* WM in wm mode? */
308 dbuf
[tbc
++] = BCD_WM
;
309 if (((t
& CHAR
) == BCD_BLANK
) && !(flag
& MD_BIN
))
310 dbuf
[tbc
++] = BCD_ALT
;
311 else dbuf
[tbc
++] = t
& CHAR
;
312 if (ADDR_ERR (BS
)) { /* check next BS */
313 BS
= BA
| (BS
% MAXMEMSIZE
);
317 if (DEBUG_PRS (mt_dev
)) fprintf (sim_deb
, " to %d\n", BS
- 1);
318 passed_eot
= sim_tape_eot (uptr
); /* passed EOT? */
319 st
= sim_tape_wrrecf (uptr
, dbuf
, tbc
); /* write record */
320 if (!passed_eot
&& sim_tape_eot (uptr
)) /* just passed EOT? */
322 if (ADDR_ERR (BS
)) { /* check final BS */
323 BS
= BA
| (BS
% MAXMEMSIZE
);
332 return mt_map_status (st
);
335 /* Get unit pointer from unit number */
337 UNIT
*get_unit (int32 unit
)
339 if ((unit
<= 0) || (unit
>= MT_NUMDR
)) return NULL
;
340 return mt_dev
.units
+ unit
;
343 /* Map tape status */
345 t_stat
mt_map_status (t_stat st
)
349 case MTSE_OK
: /* no error */
350 case MTSE_BOT
: /* reverse into BOT */
353 case MTSE_FMT
: /* illegal fmt */
356 case MTSE_UNATT
: /* not attached */
359 case MTSE_INVRL
: /* invalid rec lnt */
362 case MTSE_TMK
: /* end of file */
363 ind
[IN_END
] = 1; /* set end mark */
366 case MTSE_IOERR
: /* IO error */
367 ind
[IN_TAP
] = 1; /* set error */
368 if (iochk
) return SCPE_IOERR
;
371 case MTSE_RECE
: /* record in error */
372 case MTSE_EOM
: /* end of medium */
373 ind
[IN_TAP
] = 1; /* set error */
376 case MTSE_WRP
: /* write protect */
385 t_stat
mt_reset (DEVICE
*dptr
)
390 for (i
= 0; i
< MT_NUMDR
; i
++) { /* clear pos flag */
391 if (uptr
= get_unit (i
)) MT_CLR_PNU (uptr
);
393 ind
[IN_END
] = ind
[IN_TAP
] = 0; /* clear indicators */
397 /* Bootstrap routine */
399 t_stat
mt_boot (int32 unitno
, DEVICE
*dptr
)
401 extern int32 saved_IS
;
403 sim_tape_rewind (&mt_unit
[unitno
]); /* force rewind */
404 BS
= 1; /* set BS = 001 */
405 mt_io (unitno
, MD_WM
, BCD_R
); /* LDA %U1 001 R */