First Commit of my working state
[simh.git] / I1401 / i1401_mt.c
1 /* i1401_mt.c: IBM 1401 magnetic tape simulator
2
3 Copyright (c) 1993-2007, 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 magtape
27
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
30 (found by Van Snyder)
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
47 (found by Van Snyder)
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
59
60 Magnetic tapes are represented as a series of variable 16b records
61 of the form:
62
63 32b byte count
64 byte 0
65 byte 1
66 :
67 byte n-2
68 byte n-1
69 32b byte count
70
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.
73 */
74
75 #include "i1401_defs.h"
76 #include "sim_tape.h"
77
78 #define MT_NUMDR 7 /* #drives */
79 #define MT_MAXFR (MAXMEMSIZE * 2) /* max transfer */
80
81 uint8 dbuf[MT_MAXFR]; /* tape buffer */
82
83 extern uint8 M[]; /* memory */
84 extern int32 ind[64];
85 extern int32 BS, iochk;
86 extern UNIT cpu_unit;
87 extern FILE *sim_deb;
88
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);
93
94 /* MT data structures
95
96 mt_dev MT device descriptor
97 mt_unit MT unit list
98 mt_reg MT register list
99 mt_mod MT modifier list
100 */
101
102 UNIT mt_unit[] = {
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) }
116 };
117
118 REG mt_reg[] = {
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 },
127 { NULL }
128 };
129
130 MTAB mt_mod[] = {
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 },
137 { 0 }
138 };
139
140 DEVICE mt_dev = {
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,
145 NULL, DEV_DEBUG
146 };
147
148 /* Function routine
149
150 Inputs:
151 unit = unit character
152 mod = modifier character
153 Outputs:
154 status = status
155 */
156
157 t_stat mt_func (int32 unit, int32 mod)
158 {
159 t_mtrlnt tbc;
160 UNIT *uptr;
161 t_stat st;
162
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 */
166
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 */
172 break;
173
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 */
180
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;
185 return SCPE_OK;
186
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 */
191 break;
192
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 */
197 return SCPE_OK;
198
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 */
204
205 default:
206 return STOP_INVM;
207 }
208
209 return mt_map_status (st);
210 }
211
212 /* Read and write routines
213
214 Inputs:
215 unit = unit character
216 flag = normal, word mark, or binary mode
217 mod = modifier character
218 Outputs:
219 status = status
220
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.
226 */
227
228 t_stat mt_io (int32 unit, int32 flag, int32 mod)
229 {
230 int32 t, wm_seen;
231 t_mtrlnt i, tbc;
232 t_stat st;
233 t_bool passed_eot;
234 UNIT *uptr;
235
236 if ((uptr = get_unit (unit)) == NULL) return STOP_INVMTU; /* valid unit? */
237 if ((uptr->flags & UNIT_ATT) == 0) return SCPE_UNATT; /* attached? */
238
239 switch (mod) {
240
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 */
252 }
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);
256 break;
257 }
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);
262 BS++; /* incr BS */
263 if (ADDR_ERR (BS)) { /* test for wrap */
264 BS = BA | (BS % MAXMEMSIZE);
265 return STOP_WRAP;
266 }
267 return SCPE_OK; /* done */
268 }
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 */
275 else {
276 M[BS] = wm_seen | (t & CHAR); /* char + wm seen */
277 wm_seen = 0; /* clear flag */
278 }
279 }
280 else M[BS] = (M[BS] & WM) | (t & CHAR); /* preserve mem WM */
281 if (!wm_seen) BS++;
282 if (ADDR_ERR (BS)) { /* check next BS */
283 BS = BA | (BS % MAXMEMSIZE);
284 return STOP_WRAP;
285 }
286 }
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 */
290 }
291 if (DEBUG_PRS (mt_dev))
292 fprintf (sim_deb, " to %d, stopped by EOR\n", BS);
293 BS++; /* adv BS */
294 if (ADDR_ERR (BS)) { /* check final BS */
295 BS = BA | (BS % MAXMEMSIZE);
296 return STOP_WRAP;
297 }
298 break;
299
300 case BCD_W:
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);
314 return STOP_WRAP;
315 }
316 }
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? */
321 ind[IN_END] = 1;
322 if (ADDR_ERR (BS)) { /* check final BS */
323 BS = BA | (BS % MAXMEMSIZE);
324 return STOP_WRAP;
325 }
326 break;
327
328 default:
329 return STOP_INVM;
330 }
331
332 return mt_map_status (st);
333 }
334
335 /* Get unit pointer from unit number */
336
337 UNIT *get_unit (int32 unit)
338 {
339 if ((unit <= 0) || (unit >= MT_NUMDR)) return NULL;
340 return mt_dev.units + unit;
341 }
342
343 /* Map tape status */
344
345 t_stat mt_map_status (t_stat st)
346 {
347 switch (st) {
348
349 case MTSE_OK: /* no error */
350 case MTSE_BOT: /* reverse into BOT */
351 break;
352
353 case MTSE_FMT: /* illegal fmt */
354 return SCPE_IERR;
355
356 case MTSE_UNATT: /* not attached */
357 return SCPE_UNATT;
358
359 case MTSE_INVRL: /* invalid rec lnt */
360 return SCPE_MTRLNT;
361
362 case MTSE_TMK: /* end of file */
363 ind[IN_END] = 1; /* set end mark */
364 break;
365
366 case MTSE_IOERR: /* IO error */
367 ind[IN_TAP] = 1; /* set error */
368 if (iochk) return SCPE_IOERR;
369 break;
370
371 case MTSE_RECE: /* record in error */
372 case MTSE_EOM: /* end of medium */
373 ind[IN_TAP] = 1; /* set error */
374 break;
375
376 case MTSE_WRP: /* write protect */
377 return STOP_MTL;
378 }
379
380 return SCPE_OK;
381 }
382
383 /* Reset routine */
384
385 t_stat mt_reset (DEVICE *dptr)
386 {
387 int32 i;
388 UNIT *uptr;
389
390 for (i = 0; i < MT_NUMDR; i++) { /* clear pos flag */
391 if (uptr = get_unit (i)) MT_CLR_PNU (uptr);
392 }
393 ind[IN_END] = ind[IN_TAP] = 0; /* clear indicators */
394 return SCPE_OK;
395 }
396
397 /* Bootstrap routine */
398
399 t_stat mt_boot (int32 unitno, DEVICE *dptr)
400 {
401 extern int32 saved_IS;
402
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 */
406 saved_IS = 1;
407 return SCPE_OK;
408 }