First Commit of my working state
[simh.git] / PDP1 / pdp1_dt.c
1 /* pdp1_dt.c: 18b DECtape simulator
2
3 Copyright (c) 1993-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 dt Type 550/555 DECtape
27
28 21-Dec-06 RMS Added 16-channel SBS support
29 23-Jun-06 RMS Fixed conflict in ATTACH switches
30 Revised header format
31 16-Aug-05 RMS Fixed C++ declaration and cast problems
32 25-Jan-04 RMS Revised for device debug support
33 09-Jan-04 RMS Changed sim_fsize calling sequence, added STOP_OFFR
34 26-Oct-03 RMS Cleaned up buffer copy code
35 18-Oct-03 RMS Added DECtape off reel message, simplified timing
36 25-Apr-03 RMS Revised for extended file support
37 14-Mar-03 RMS Fixed variable size interaction with save/restore
38 17-Oct-02 RMS Fixed bug in end of reel logic
39 06-Oct-02 RMS Added device disable support
40 13-Aug-02 RMS Cloned from pdp18b_dt.c
41
42 18b DECtapes are represented in memory by fixed length buffer of 32b words.
43 Three file formats are supported:
44
45 18b/36b 256 words per block [256 x 18b]
46 16b 256 words per block [256 x 16b]
47 12b 129 words per block [129 x 12b]
48
49 When a 16b or 12b DECtape file is read in, it is converted to 18b/36b format.
50
51 DECtape motion is measured in 3b lines. Time between lines is 33.33us.
52 Tape density is nominally 300 lines per inch. The format of a DECtape (as
53 taken from the TD8E formatter) is:
54
55 reverse end zone 8192 reverse end zone codes ~ 10 feet
56 reverse buffer 200 interblock codes
57 block 0
58 :
59 block n
60 forward buffer 200 interblock codes
61 forward end zone 8192 forward end zone codes ~ 10 feet
62
63 A block consists of five 18b header words, a tape-specific number of data
64 words, and five 18b trailer words. All systems except the PDP-8 use a
65 standard block length of 256 words; the PDP-8 uses a standard block length
66 of 86 words (x 18b = 129 words x 12b).
67
68 Because a DECtape file only contains data, the simulator cannot support
69 write timing and mark track and can only do a limited implementation
70 of read all and write all. Read all assumes that the tape has been
71 conventionally written forward:
72
73 header word 0 0
74 header word 1 block number (for forward reads)
75 header words 2,3 0
76 header word 4 checksum (for reverse reads)
77 :
78 trailer word 4 checksum (for forward reads)
79 trailer words 3,2 0
80 trailer word 1 block number (for reverse reads)
81 trailer word 0 0
82
83 Write all writes only the data words and dumps the interblock words in the
84 bit bucket.
85
86 The Type 550 controller has a 4b unit select field, for units 1-8. The code
87 assumes that the GETUNIT macro returns a unit number in the range of 0-7,
88 with 8 represented as 0, and an invalid unit as -1.
89 */
90
91 #include "pdp1_defs.h"
92
93 #define DT_NUMDR 8 /* #drives */
94 #define UNIT_V_WLK (UNIT_V_UF + 0) /* write locked */
95 #define UNIT_V_8FMT (UNIT_V_UF + 1) /* 12b format */
96 #define UNIT_V_11FMT (UNIT_V_UF + 2) /* 16b format */
97 #define UNIT_WLK (1 << UNIT_V_WLK)
98 #define UNIT_8FMT (1 << UNIT_V_8FMT)
99 #define UNIT_11FMT (1 << UNIT_V_11FMT)
100 #define STATE u3 /* unit state */
101 #define LASTT u4 /* last time update */
102 #define DT_WC 030 /* word count */
103 #define DT_CA 031 /* current addr */
104 #define UNIT_WPRT (UNIT_WLK | UNIT_RO) /* write protect */
105
106 /* System independent DECtape constants */
107
108 #define DT_LPERMC 6 /* lines per mark track */
109 #define DT_BLKWD 1 /* blk no word in h/t */
110 #define DT_CSMWD 4 /* checksum word in h/t */
111 #define DT_HTWRD 5 /* header/trailer words */
112 #define DT_EZLIN (8192 * DT_LPERMC) /* end zone length */
113 #define DT_BFLIN (200 * DT_LPERMC) /* buffer length */
114 #define DT_BLKLN (DT_BLKWD * DT_LPERMC) /* blk no line in h/t */
115 #define DT_CSMLN (DT_CSMWD * DT_LPERMC) /* csum line in h/t */
116 #define DT_HTLIN (DT_HTWRD * DT_LPERMC) /* header/trailer lines */
117
118 /* 16b, 18b, 36b DECtape constants */
119
120 #define D18_WSIZE 6 /* word size in lines */
121 #define D18_BSIZE 256 /* block size in 18b */
122 #define D18_TSIZE 578 /* tape size */
123 #define D18_LPERB (DT_HTLIN + (D18_BSIZE * DT_WSIZE) + DT_HTLIN)
124 #define D18_FWDEZ (DT_EZLIN + (D18_LPERB * D18_TSIZE))
125 #define D18_CAPAC (D18_TSIZE * D18_BSIZE) /* tape capacity */
126 #define D11_FILSIZ (D18_CAPAC * sizeof (int16))
127
128 /* 12b DECtape constants */
129
130 #define D8_WSIZE 4 /* word size in lines */
131 #define D8_BSIZE 86 /* block size in 18b */
132 #define D8_TSIZE 1474 /* tape size */
133 #define D8_LPERB (DT_HTLIN + (D8_BSIZE * DT_WSIZE) + DT_HTLIN)
134 #define D8_FWDEZ (DT_EZLIN + (D8_LPERB * D8_TSIZE))
135 #define D8_CAPAC (D8_TSIZE * D8_BSIZE) /* tape capacity */
136
137 #define D8_NBSIZE ((D8_BSIZE * D18_WSIZE) / D8_WSIZE)
138 #define D8_FILSIZ (D8_NBSIZE * D8_TSIZE * sizeof (int16))
139
140 /* This controller */
141
142 #define DT_CAPAC D18_CAPAC /* default */
143 #define DT_WSIZE D18_WSIZE
144
145 /* Calculated constants, per unit */
146
147 #define DTU_BSIZE(u) (((u)->flags & UNIT_8FMT)? D8_BSIZE: D18_BSIZE)
148 #define DTU_TSIZE(u) (((u)->flags & UNIT_8FMT)? D8_TSIZE: D18_TSIZE)
149 #define DTU_LPERB(u) (((u)->flags & UNIT_8FMT)? D8_LPERB: D18_LPERB)
150 #define DTU_FWDEZ(u) (((u)->flags & UNIT_8FMT)? D8_FWDEZ: D18_FWDEZ)
151 #define DTU_CAPAC(u) (((u)->flags & UNIT_8FMT)? D8_CAPAC: D18_CAPAC)
152
153 #define DT_LIN2BL(p,u) (((p) - DT_EZLIN) / DTU_LPERB (u))
154 #define DT_LIN2OF(p,u) (((p) - DT_EZLIN) % DTU_LPERB (u))
155 #define DT_LIN2WD(p,u) ((DT_LIN2OF (p,u) - DT_HTLIN) / DT_WSIZE)
156 #define DT_BLK2LN(p,u) (((p) * DTU_LPERB (u)) + DT_EZLIN)
157 #define DT_QREZ(u) (((u)->pos) < DT_EZLIN)
158 #define DT_QFEZ(u) (((u)->pos) >= ((uint32) DTU_FWDEZ (u)))
159 #define DT_QEZ(u) (DT_QREZ (u) || DT_QFEZ (u))
160
161 /* Status register A */
162
163 #define DTA_V_UNIT 12 /* unit select */
164 #define DTA_M_UNIT 017
165 #define DTA_UNIT (DTA_M_UNIT << DTA_V_UNIT)
166 #define DTA_V_MOT 4 /* motion */
167 #define DTA_M_MOT 03
168 #define DTA_V_FNC 0 /* function */
169 #define DTA_M_FNC 07
170 #define FNC_MOVE 00 /* move */
171 #define FNC_SRCH 01 /* search */
172 #define FNC_READ 02 /* read */
173 #define FNC_WRIT 03 /* write */
174 #define FNC_RALL 05 /* read all */
175 #define FNC_WALL 06 /* write all */
176 #define FNC_WMRK 07 /* write timing */
177 #define DTA_STSTP (1u << (DTA_V_MOT + 1))
178 #define DTA_FWDRV (1u << DTA_V_MOT)
179 #define DTA_MODE 0 /* not implemented */
180 #define DTA_RW 077
181 #define DTA_GETUNIT(x) map_unit[(((x) >> DTA_V_UNIT) & DTA_M_UNIT)]
182 #define DT_UPDINT if (dtsb & (DTB_DTF | DTB_BEF | DTB_ERF)) \
183 dev_req_int (dt_sbs);
184
185 #define DTA_GETMOT(x) (((x) >> DTA_V_MOT) & DTA_M_MOT)
186 #define DTA_GETFNC(x) (((x) >> DTA_V_FNC) & DTA_M_FNC)
187
188 /* Status register B */
189
190 #define DTB_V_DTF 17 /* data flag */
191 #define DTB_V_BEF 16 /* block end flag */
192 #define DTB_V_ERF 15 /* error flag */
193 #define DTB_V_END 14 /* end of tape */
194 #define DTB_V_TIM 13 /* timing err */
195 #define DTB_V_REV 12 /* reverse */
196 #define DTB_V_GO 11 /* go */
197 #define DTB_V_MRK 10 /* mark trk err */
198 #define DTB_V_SEL 9 /* select err */
199 #define DTB_DTF (1u << DTB_V_DTF)
200 #define DTB_BEF (1u << DTB_V_BEF)
201 #define DTB_ERF (1u << DTB_V_ERF)
202 #define DTB_END (1u << DTB_V_END)
203 #define DTB_TIM (1u << DTB_V_TIM)
204 #define DTB_REV (1u << DTB_V_REV)
205 #define DTB_GO (1u << DTB_V_GO)
206 #define DTB_MRK (1u << DTB_V_MRK)
207 #define DTB_SEL (1u << DTB_V_SEL)
208 #define DTB_ALLERR (DTB_END | DTB_TIM | DTB_MRK | DTB_SEL)
209
210 /* DECtape state */
211
212 #define DTS_V_MOT 3 /* motion */
213 #define DTS_M_MOT 07
214 #define DTS_STOP 0 /* stopped */
215 #define DTS_DECF 2 /* decel, fwd */
216 #define DTS_DECR 3 /* decel, rev */
217 #define DTS_ACCF 4 /* accel, fwd */
218 #define DTS_ACCR 5 /* accel, rev */
219 #define DTS_ATSF 6 /* @speed, fwd */
220 #define DTS_ATSR 7 /* @speed, rev */
221 #define DTS_DIR 01 /* dir mask */
222 #define DTS_V_FNC 0 /* function */
223 #define DTS_M_FNC 07
224 #define DTS_OFR 7 /* "off reel" */
225 #define DTS_GETMOT(x) (((x) >> DTS_V_MOT) & DTS_M_MOT)
226 #define DTS_GETFNC(x) (((x) >> DTS_V_FNC) & DTS_M_FNC)
227 #define DTS_V_2ND 6 /* next state */
228 #define DTS_V_3RD (DTS_V_2ND + DTS_V_2ND) /* next next */
229 #define DTS_STA(y,z) (((y) << DTS_V_MOT) | ((z) << DTS_V_FNC))
230 #define DTS_SETSTA(y,z) uptr->STATE = DTS_STA (y, z)
231 #define DTS_SET2ND(y,z) uptr->STATE = (uptr->STATE & 077) | \
232 ((DTS_STA (y, z)) << DTS_V_2ND)
233 #define DTS_SET3RD(y,z) uptr->STATE = (uptr->STATE & 07777) | \
234 ((DTS_STA (y, z)) << DTS_V_3RD)
235 #define DTS_NXTSTA(x) (x >> DTS_V_2ND)
236
237 /* Operation substates */
238
239 #define DTO_WCO 1 /* wc overflow */
240 #define DTO_SOB 2 /* start of block */
241
242 /* Logging */
243
244 #define LOG_MS 001 /* move, search */
245 #define LOG_RW 002 /* read write */
246 #define LOG_BL 004 /* block # lblk */
247
248 #define ABS(x) (((x) < 0)? (-(x)): (x))
249
250 extern int32 M[];
251 extern int32 stop_inst;
252 extern UNIT cpu_unit;
253 extern int32 sim_switches;
254 extern int32 sim_is_running;
255 extern FILE *sim_deb;
256
257 int32 dtsa = 0; /* status A */
258 int32 dtsb = 0; /* status B */
259 int32 dtdb = 0; /* data buffer */
260 int32 dt_sbs = 0; /* SBS level */
261 int32 dt_ltime = 12; /* interline time */
262 int32 dt_dctime = 40000; /* decel time */
263 int32 dt_substate = 0;
264 int32 dt_logblk = 0;
265 int32 dt_stopoffr = 0;
266 static const int32 map_unit[16] = { /* Type 550 unit map */
267 -1, 1, 2, 3, 4, 5, 6, 7,
268 0, -1, -1, -1, -1, -1, -1, -1
269 };
270
271 t_stat dt_svc (UNIT *uptr);
272 t_stat dt_reset (DEVICE *dptr);
273 t_stat dt_attach (UNIT *uptr, char *cptr);
274 t_stat dt_detach (UNIT *uptr);
275 void dt_deselect (int32 oldf);
276 void dt_newsa (int32 newf);
277 void dt_newfnc (UNIT *uptr, int32 newsta);
278 t_bool dt_setpos (UNIT *uptr);
279 void dt_schedez (UNIT *uptr, int32 dir);
280 void dt_seterr (UNIT *uptr, int32 e);
281 int32 dt_comobv (int32 val);
282 int32 dt_csum (UNIT *uptr, int32 blk);
283 int32 dt_gethdr (UNIT *uptr, int32 blk, int32 relpos);
284
285 /* DT data structures
286
287 dt_dev DT device descriptor
288 dt_unit DT unit list
289 dt_reg DT register list
290 dt_mod DT modifier list
291 */
292
293 UNIT dt_unit[] = {
294 { UDATA (&dt_svc, UNIT_FIX+UNIT_ATTABLE+UNIT_DISABLE+
295 UNIT_ROABLE, DT_CAPAC) },
296 { UDATA (&dt_svc, UNIT_FIX+UNIT_ATTABLE+UNIT_DISABLE+
297 UNIT_ROABLE, DT_CAPAC) },
298 { UDATA (&dt_svc, UNIT_FIX+UNIT_ATTABLE+UNIT_DISABLE+
299 UNIT_ROABLE, DT_CAPAC) },
300 { UDATA (&dt_svc, UNIT_FIX+UNIT_ATTABLE+UNIT_DISABLE+
301 UNIT_ROABLE, DT_CAPAC) },
302 { UDATA (&dt_svc, UNIT_FIX+UNIT_ATTABLE+UNIT_DISABLE+
303 UNIT_ROABLE, DT_CAPAC) },
304 { UDATA (&dt_svc, UNIT_FIX+UNIT_ATTABLE+UNIT_DISABLE+
305 UNIT_ROABLE, DT_CAPAC) },
306 { UDATA (&dt_svc, UNIT_FIX+UNIT_ATTABLE+UNIT_DISABLE+
307 UNIT_ROABLE, DT_CAPAC) },
308 { UDATA (&dt_svc, UNIT_FIX+UNIT_ATTABLE+UNIT_DISABLE+
309 UNIT_ROABLE, DT_CAPAC) }
310 };
311
312 REG dt_reg[] = {
313 { ORDATA (DTSA, dtsa, 18) },
314 { ORDATA (DTSB, dtsb, 18) },
315 { ORDATA (DTDB, dtdb, 18) },
316 { FLDATA (DTF, dtsb, DTB_V_DTF) },
317 { FLDATA (BEF, dtsb, DTB_V_BEF) },
318 { FLDATA (ERF, dtsb, DTB_V_ERF) },
319 { DRDATA (LTIME, dt_ltime, 31), REG_NZ },
320 { DRDATA (DCTIME, dt_dctime, 31), REG_NZ },
321 { ORDATA (SUBSTATE, dt_substate, 2) },
322 { DRDATA (LBLK, dt_logblk, 12), REG_HIDDEN },
323 { URDATA (POS, dt_unit[0].pos, 10, T_ADDR_W, 0,
324 DT_NUMDR, PV_LEFT | REG_RO) },
325 { URDATA (STATT, dt_unit[0].STATE, 8, 18, 0,
326 DT_NUMDR, REG_RO) },
327 { URDATA (LASTT, dt_unit[0].LASTT, 10, 32, 0,
328 DT_NUMDR, REG_HRO) },
329 { FLDATA (STOP_OFFR, dt_stopoffr, 0) },
330 { DRDATA (SBSLVL, dt_sbs, 4), REG_HRO },
331 { NULL }
332 };
333
334 MTAB dt_mod[] = {
335 { MTAB_XTD|MTAB_VDV, 0, "SBSLVL", "SBSLVL",
336 &dev_set_sbs, &dev_show_sbs, (void *) &dt_sbs },
337 { UNIT_WLK, 0, "write enabled", "WRITEENABLED", NULL },
338 { UNIT_WLK, UNIT_WLK, "write locked", "LOCKED", NULL },
339 { UNIT_8FMT + UNIT_11FMT, 0, "18b", NULL, NULL },
340 { UNIT_8FMT + UNIT_11FMT, UNIT_8FMT, "12b", NULL, NULL },
341 { UNIT_8FMT + UNIT_11FMT, UNIT_11FMT, "16b", NULL, NULL },
342 { 0 }
343 };
344
345 DEBTAB dt_deb[] = {
346 { "MOTION", LOG_MS },
347 { "DATA", LOG_RW },
348 { "BLOCK", LOG_BL },
349 { NULL, 0 }
350 };
351
352 DEVICE dt_dev = {
353 "DT", dt_unit, dt_reg, dt_mod,
354 DT_NUMDR, 8, 24, 1, 8, 18,
355 NULL, NULL, &dt_reset,
356 NULL, &dt_attach, &dt_detach,
357 NULL, DEV_DISABLE | DEV_DEBUG, 0,
358 dt_deb, NULL, NULL
359 };
360
361 /* IOT routine */
362
363 int32 dt (int32 IR, int32 dev, int32 dat)
364 {
365 int32 pulse = (IR >> 6) & 037;
366 int32 fnc, mot, unum;
367 UNIT *uptr = NULL;
368
369 if (dt_dev.flags & DEV_DIS) /* disabled? */
370 return (stop_inst << IOT_V_REASON) | dat; /* stop if requested */
371 unum = DTA_GETUNIT (dtsa); /* get unit no */
372 if (unum >= 0) uptr = dt_dev.units + unum; /* get unit */
373
374 if (pulse == 003) { /* MSE */
375 if ((dtsa ^ dat) & DTA_UNIT) dt_deselect (dtsa); /* new unit? */
376 dtsa = (dtsa & ~DTA_UNIT) | (dat & DTA_UNIT);
377 dtsb = dtsb & ~(DTB_DTF | DTB_BEF | DTB_ERF | DTB_ALLERR);
378 }
379 if (pulse == 004) { /* MLC */
380 dtsa = (dtsa & ~DTA_RW) | (dat & DTA_RW); /* load dtsa */
381 dtsb = dtsb & ~(DTB_DTF | DTB_BEF | DTB_ERF | DTB_ALLERR);
382 fnc = DTA_GETFNC (dtsa); /* get fnc */
383 if ((uptr == NULL) || /* invalid? */
384 ((uptr->flags) & UNIT_DIS) || /* disabled? */
385 (fnc >= FNC_WMRK) || /* write mark? */
386 ((fnc == FNC_WRIT) && (uptr->flags & UNIT_WLK)) ||
387 ((fnc == FNC_WALL) && (uptr->flags & UNIT_WLK)))
388 dt_seterr (uptr, DTB_SEL); /* select err */
389 else dt_newsa (dtsa);
390 }
391 if (pulse == 005) { /* MRD */
392 dat = (dat & ~DMASK) | dtdb;
393 dtsb = dtsb & ~(DTB_DTF | DTB_BEF);
394 }
395 if (pulse == 006) { /* MWR */
396 dtdb = dat & DMASK;
397 dtsb = dtsb & ~(DTB_DTF | DTB_BEF);
398 }
399 if (pulse == 007) { /* MRS */
400 dtsb = dtsb & ~(DTB_REV | DTB_GO); /* clr rev, go */
401 if (uptr) { /* valid unit? */
402 mot = DTS_GETMOT (uptr->STATE); /* get motion */
403 if (mot & DTS_DIR) dtsb = dtsb | DTB_REV; /* rev? set */
404 if ((mot >= DTS_ACCF) || (uptr->STATE & 0777700))
405 dtsb = dtsb | DTB_GO; /* accel? go */
406 }
407 dat = (dat & ~DMASK) | dtsb;
408 }
409 DT_UPDINT;
410 return dat;
411 }
412
413 /* Unit deselect */
414
415 void dt_deselect (int32 oldf)
416 {
417 int32 old_unit, old_mot;
418 UNIT *uptr;
419
420 old_unit = DTA_GETUNIT (oldf); /* get unit no */
421 if (old_unit < 0) return; /* invalid? */
422 uptr = dt_dev.units + old_unit; /* get unit */
423 old_mot = DTS_GETMOT (uptr->STATE);
424 if (old_mot >= DTS_ATSF) /* at speed? */
425 dt_newfnc (uptr, DTS_STA (old_mot, DTS_OFR));
426 else if (old_mot >= DTS_ACCF) /* accelerating? */
427 DTS_SET2ND (DTS_ATSF | (old_mot & DTS_DIR), DTS_OFR);
428 return;
429 }
430
431 /* Command register change
432
433 1. If change in motion, stop to start
434 - schedule acceleration
435 - set function as next state
436 2. If change in motion, start to stop
437 - if not already decelerating (could be reversing),
438 schedule deceleration
439 3. If change in direction,
440 - if not decelerating, schedule deceleration
441 - set accelerating (other dir) as next state
442 - set function as next next state
443 4. If not accelerating or at speed,
444 - schedule acceleration
445 - set function as next state
446 5. If not yet at speed,
447 - set function as next state
448 6. If at speed,
449 - set function as current state, schedule function
450 */
451
452 void dt_newsa (int32 newf)
453 {
454 int32 new_unit, prev_mot, new_fnc;
455 int32 prev_mving, new_mving, prev_dir, new_dir;
456 UNIT *uptr;
457
458 new_unit = DTA_GETUNIT (newf); /* new unit */
459 if (new_unit < 0) return; /* invalid? */
460 uptr = dt_dev.units + new_unit;
461 if ((uptr->flags & UNIT_ATT) == 0) { /* new unit attached? */
462 dt_seterr (uptr, DTB_SEL); /* no, error */
463 return;
464 }
465 prev_mot = DTS_GETMOT (uptr->STATE); /* previous motion */
466 prev_mving = prev_mot != DTS_STOP; /* previous moving? */
467 prev_dir = prev_mot & DTS_DIR; /* previous dir? */
468 new_mving = (newf & DTA_STSTP) != 0; /* new moving? */
469 new_dir = (newf & DTA_FWDRV) != 0; /* new dir? */
470 new_fnc = DTA_GETFNC (newf); /* new function? */
471
472 if ((prev_mving | new_mving) == 0) return; /* stop to stop */
473
474 if (new_mving & ~prev_mving) { /* start? */
475 if (dt_setpos (uptr)) return; /* update pos */
476 sim_cancel (uptr); /* stop current */
477 sim_activate (uptr, dt_dctime - (dt_dctime >> 2)); /* sched accel */
478 DTS_SETSTA (DTS_ACCF | new_dir, 0); /* state = accel */
479 DTS_SET2ND (DTS_ATSF | new_dir, new_fnc); /* next = fnc */
480 return;
481 }
482
483 if (prev_mving & ~new_mving) { /* stop? */
484 if ((prev_mot & ~DTS_DIR) != DTS_DECF) { /* !already stopping? */
485 if (dt_setpos (uptr)) return; /* update pos */
486 sim_cancel (uptr); /* stop current */
487 sim_activate (uptr, dt_dctime); /* schedule decel */
488 }
489 DTS_SETSTA (DTS_DECF | prev_dir, 0); /* state = decel */
490 return;
491 }
492
493 if (prev_dir ^ new_dir) { /* dir chg? */
494 if ((prev_mot & ~DTS_DIR) != DTS_DECF) { /* !already stopping? */
495 if (dt_setpos (uptr)) return; /* update pos */
496 sim_cancel (uptr); /* stop current */
497 sim_activate (uptr, dt_dctime); /* schedule decel */
498 }
499 DTS_SETSTA (DTS_DECF | prev_dir, 0); /* state = decel */
500 DTS_SET2ND (DTS_ACCF | new_dir, 0); /* next = accel */
501 DTS_SET3RD (DTS_ATSF | new_dir, new_fnc); /* next next = fnc */
502 return;
503 }
504
505 if (prev_mot < DTS_ACCF) { /* not accel/at speed? */
506 if (dt_setpos (uptr)) return; /* update pos */
507 sim_cancel (uptr); /* cancel cur */
508 sim_activate (uptr, dt_dctime - (dt_dctime >> 2)); /* sched accel */
509 DTS_SETSTA (DTS_ACCF | new_dir, 0); /* state = accel */
510 DTS_SET2ND (DTS_ATSF | new_dir, new_fnc); /* next = fnc */
511 return;
512 }
513
514 if (prev_mot < DTS_ATSF) { /* not at speed? */
515 DTS_SET2ND (DTS_ATSF | new_dir, new_fnc); /* next = fnc */
516 return;
517 }
518
519 dt_newfnc (uptr, DTS_STA (DTS_ATSF | new_dir, new_fnc));/* state = fnc */
520 return;
521 }
522
523 /* Schedule new DECtape function
524
525 This routine is only called if
526 - the selected unit is attached
527 - the selected unit is at speed (forward or backward)
528
529 This routine
530 - updates the selected unit's position
531 - updates the selected unit's state
532 - schedules the new operation
533 */
534
535 void dt_newfnc (UNIT *uptr, int32 newsta)
536 {
537 int32 fnc, dir, blk, unum, newpos;
538 uint32 oldpos;
539
540 oldpos = uptr->pos; /* save old pos */
541 if (dt_setpos (uptr)) return; /* update pos */
542 uptr->STATE = newsta; /* update state */
543 fnc = DTS_GETFNC (uptr->STATE); /* set variables */
544 dir = DTS_GETMOT (uptr->STATE) & DTS_DIR;
545 unum = (int32) (uptr - dt_dev.units);
546 if (oldpos == uptr->pos) /* bump pos */
547 uptr->pos = uptr->pos + (dir? -1: 1);
548 blk = DT_LIN2BL (uptr->pos, uptr);
549
550 if (dir? DT_QREZ (uptr): DT_QFEZ (uptr)) { /* wrong ez? */
551 dt_seterr (uptr, DTB_END); /* set ez flag, stop */
552 return;
553 }
554 sim_cancel (uptr); /* cancel cur op */
555 dt_substate = DTO_SOB; /* substate = block start */
556 switch (fnc) { /* case function */
557
558 case DTS_OFR: /* off reel */
559 if (dir) newpos = -1000; /* rev? < start */
560 else newpos = DTU_FWDEZ (uptr) + DT_EZLIN + 1000; /* fwd? > end */
561 break;
562
563 case FNC_MOVE: /* move */
564 dt_schedez (uptr, dir); /* sched end zone */
565 if (DEBUG_PRI (dt_dev, LOG_MS)) fprintf (sim_deb, ">>DT%d: moving %s\n", unum, (dir?
566 "backward": "forward"));
567 return; /* done */
568
569 case FNC_SRCH: /* search */
570 if (dir) newpos = DT_BLK2LN ((DT_QFEZ (uptr)?
571 DTU_TSIZE (uptr): blk), uptr) - DT_BLKLN - DT_WSIZE;
572 else newpos = DT_BLK2LN ((DT_QREZ (uptr)?
573 0: blk + 1), uptr) + DT_BLKLN + (DT_WSIZE - 1);
574 if (DEBUG_PRI (dt_dev, LOG_MS)) fprintf (sim_deb, ">>DT%d: searching %s\n", unum,
575 (dir? "backward": "forward"));
576 break;
577
578 case FNC_WRIT: /* write */
579 case FNC_READ: /* read */
580 case FNC_RALL: /* read all */
581 case FNC_WALL: /* write all */
582 if (DT_QEZ (uptr)) { /* in "ok" end zone? */
583 if (dir) newpos = DTU_FWDEZ (uptr) - DT_WSIZE;
584 else newpos = DT_EZLIN + (DT_WSIZE - 1);
585 }
586 else {
587 newpos = ((uptr->pos) / DT_WSIZE) * DT_WSIZE;
588 if (!dir) newpos = newpos + (DT_WSIZE - 1);
589 }
590 if (DEBUG_PRI (dt_dev, LOG_RW) ||
591 (DEBUG_PRI (dt_dev, LOG_BL) && (blk == dt_logblk)))
592 fprintf (sim_deb, ">>DT%d: read all block %d %s%s\n",
593 unum, blk, (dir? "backward": "forward"),
594 ((dtsa & DTA_MODE)? " continuous": " "));
595 break;
596
597 default:
598 dt_seterr (uptr, DTB_SEL); /* bad state */
599 return;
600 }
601
602 if ((fnc == FNC_WRIT) || (fnc == FNC_WALL)) { /* write function? */
603 dtsb = dtsb | DTB_DTF; /* set data flag */
604 DT_UPDINT;
605 }
606 sim_activate (uptr, ABS (newpos - ((int32) uptr->pos)) * dt_ltime);
607 return;
608 }
609
610 /* Update DECtape position
611
612 DECtape motion is modeled as a constant velocity, with linear
613 acceleration and deceleration. The motion equations are as follows:
614
615 t = time since operation started
616 tmax = time for operation (accel, decel only)
617 v = at speed velocity in lines (= 1/dt_ltime)
618
619 Then:
620 at speed dist = t * v
621 accel dist = (t^2 * v) / (2 * tmax)
622 decel dist = (((2 * t * tmax) - t^2) * v) / (2 * tmax)
623
624 This routine uses the relative (integer) time, rather than the absolute
625 (floating point) time, to allow save and restore of the start times.
626 */
627
628 t_bool dt_setpos (UNIT *uptr)
629 {
630 uint32 new_time, ut, ulin, udelt;
631 int32 mot = DTS_GETMOT (uptr->STATE);
632 int32 unum, delta;
633
634 new_time = sim_grtime (); /* current time */
635 ut = new_time - uptr->LASTT; /* elapsed time */
636 if (ut == 0) return FALSE; /* no time gone? exit */
637 uptr->LASTT = new_time; /* update last time */
638 switch (mot & ~DTS_DIR) { /* case on motion */
639
640 case DTS_STOP: /* stop */
641 delta = 0;
642 break;
643
644 case DTS_DECF: /* slowing */
645 ulin = ut / (uint32) dt_ltime;
646 udelt = dt_dctime / dt_ltime;
647 delta = ((ulin * udelt * 2) - (ulin * ulin)) / (2 * udelt);
648 break;
649
650 case DTS_ACCF: /* accelerating */
651 ulin = ut / (uint32) dt_ltime;
652 udelt = (dt_dctime - (dt_dctime >> 2)) / dt_ltime;
653 delta = (ulin * ulin) / (2 * udelt);
654 break;
655
656 case DTS_ATSF: /* at speed */
657 delta = ut / (uint32) dt_ltime;
658 break;
659 }
660
661 if (mot & DTS_DIR) uptr->pos = uptr->pos - delta; /* update pos */
662 else uptr->pos = uptr->pos + delta;
663 if (((int32) uptr->pos < 0) ||
664 ((int32) uptr->pos > (DTU_FWDEZ (uptr) + DT_EZLIN))) {
665 detach_unit (uptr); /* off reel? */
666 uptr->STATE = uptr->pos = 0;
667 unum = (int32) (uptr - dt_dev.units);
668 if (unum == DTA_GETUNIT (dtsa)) /* if selected, */
669 dt_seterr (uptr, DTB_SEL); /* error */
670 return TRUE;
671 }
672 return FALSE;
673 }
674
675 /* Unit service
676
677 Unit must be attached, detach cancels operation
678 */
679
680 t_stat dt_svc (UNIT *uptr)
681 {
682 int32 mot = DTS_GETMOT (uptr->STATE);
683 int32 dir = mot & DTS_DIR;
684 int32 fnc = DTS_GETFNC (uptr->STATE);
685 int32 *fbuf = (int32 *) uptr->filebuf;
686 int32 blk, wrd, ma, relpos;
687 uint32 ba;
688
689 /* Motion cases
690
691 Decelerating - if next state != stopped, must be accel reverse
692 Accelerating - next state must be @speed, schedule function
693 At speed - do functional processing
694 */
695
696 switch (mot) {
697
698 case DTS_DECF: case DTS_DECR: /* decelerating */
699 if (dt_setpos (uptr)) /* upd pos; off reel? */
700 return IORETURN (dt_stopoffr, STOP_DTOFF);
701 uptr->STATE = DTS_NXTSTA (uptr->STATE); /* advance state */
702 if (uptr->STATE) /* not stopped? */
703 sim_activate (uptr, dt_dctime - (dt_dctime >> 2)); /* reversing */
704 return SCPE_OK;
705
706 case DTS_ACCF: case DTS_ACCR: /* accelerating */
707 dt_newfnc (uptr, DTS_NXTSTA (uptr->STATE)); /* adv state, sched */
708 return SCPE_OK;
709
710 case DTS_ATSF: case DTS_ATSR: /* at speed */
711 break; /* check function */
712
713 default: /* other */
714 dt_seterr (uptr, DTB_SEL); /* state error */
715 return SCPE_OK;
716 }
717
718 /* Functional cases
719
720 Move - must be at end zone
721 Search - transfer block number, schedule next block
722 Off reel - detach unit (it must be deselected)
723 */
724
725 if (dt_setpos (uptr)) /* upd pos; off reel? */
726 return IORETURN (dt_stopoffr, STOP_DTOFF);
727 if (DT_QEZ (uptr)) { /* in end zone? */
728 dt_seterr (uptr, DTB_END); /* end zone error */
729 return SCPE_OK;
730 }
731 blk = DT_LIN2BL (uptr->pos, uptr); /* get block # */
732
733 switch (fnc) { /* at speed, check fnc */
734
735 case FNC_MOVE: /* move */
736 dt_seterr (uptr, DTB_END); /* end zone error */
737 return SCPE_OK;
738
739 case DTS_OFR: /* off reel */
740 detach_unit (uptr); /* must be deselected */
741 uptr->STATE = uptr->pos = 0; /* no visible action */
742 break;
743
744 /* Search */
745
746 case FNC_SRCH: /* search */
747 if (dtsb & DTB_DTF) { /* DTF set? */
748 dt_seterr (uptr, DTB_TIM); /* timing error */
749 return SCPE_OK;
750 }
751 sim_activate (uptr, DTU_LPERB (uptr) * dt_ltime);/* sched next block */
752 dtdb = blk; /* store block # */
753 dtsb = dtsb | DTB_DTF; /* set DTF */
754 break;
755
756 /* Read and read all */
757
758 case FNC_READ: case FNC_RALL:
759 if (dtsb & DTB_DTF) { /* DTF set? */
760 dt_seterr (uptr, DTB_TIM); /* timing error */
761 return SCPE_OK;
762 }
763 sim_activate (uptr, DT_WSIZE * dt_ltime); /* sched next word */
764 relpos = DT_LIN2OF (uptr->pos, uptr); /* cur pos in blk */
765 if ((relpos >= DT_HTLIN) && /* in data zone? */
766 (relpos < (DTU_LPERB (uptr) - DT_HTLIN))) {
767 wrd = DT_LIN2WD (uptr->pos, uptr);
768 ba = (blk * DTU_BSIZE (uptr)) + wrd;
769 dtdb = fbuf[ba]; /* get tape word */
770 dtsb = dtsb | DTB_DTF; /* set flag */
771 }
772 else {
773 ma = (2 * DT_HTWRD) + DTU_BSIZE (uptr) - DT_CSMWD - 1;
774 wrd = relpos / DT_WSIZE; /* hdr start = wd 0 */
775 #if defined (OLD_TYPE550)
776 if ((wrd == 0) || /* skip 1st, last */
777 (wrd == ((2 * DT_HTWRD) + DTU_BSIZE (uptr) - 1))) break;
778 #endif
779 if ((fnc == FNC_READ) && /* read, skip if not */
780 (wrd != DT_CSMWD) && /* fwd, rev cksum */
781 (wrd != ma)) break;
782 dtdb = dt_gethdr (uptr, blk, relpos);
783 if (wrd == (dir? DT_CSMWD: ma)) /* at end csum? */
784 dtsb = dtsb | DTB_BEF; /* end block */
785 else dtsb = dtsb | DTB_DTF; /* else next word */
786 }
787 if (dir) dtdb = dt_comobv (dtdb);
788 break;
789
790 /* Write and write all */
791
792 case FNC_WRIT: case FNC_WALL:
793 if (dtsb & DTB_DTF) { /* DTF set? */
794 dt_seterr (uptr, DTB_TIM); /* timing error */
795 return SCPE_OK;
796 }
797 sim_activate (uptr, DT_WSIZE * dt_ltime); /* sched next word */
798 relpos = DT_LIN2OF (uptr->pos, uptr); /* cur pos in blk */
799 if ((relpos >= DT_HTLIN) && /* in data zone? */
800 (relpos < (DTU_LPERB (uptr) - DT_HTLIN))) {
801 wrd = DT_LIN2WD (uptr->pos, uptr);
802 ba = (blk * DTU_BSIZE (uptr)) + wrd;
803 if (dir) fbuf[ba] = dt_comobv (dtdb); /* get data word */
804 else fbuf[ba] = dtdb;
805 if (ba >= uptr->hwmark) uptr->hwmark = ba + 1;
806 if (wrd == (dir? 0: DTU_BSIZE (uptr) - 1))
807 dtsb = dtsb | DTB_BEF; /* end block */
808 else dtsb = dtsb | DTB_DTF; /* else next word */
809 }
810 else {
811 wrd = relpos / DT_WSIZE; /* hdr start = wd 0 */
812 #if defined (OLD_TYPE550)
813 if ((wrd == 0) || /* skip 1st, last */
814 (wrd == ((2 * DT_HTWRD) + DTU_BSIZE (uptr) - 1))) break;
815 #endif
816 if ((fnc == FNC_WRIT) && /* wr, skip if !csm */
817 (wrd != ((2 * DT_HTWRD) + DTU_BSIZE (uptr) - DT_CSMWD - 1)))
818 break;
819 dtsb = dtsb | DTB_DTF; /* set flag */
820 }
821 break;
822
823 default:
824 dt_seterr (uptr, DTB_SEL); /* impossible state */
825 break;
826 }
827
828 DT_UPDINT; /* update interrupts */
829 return SCPE_OK;
830 }
831
832 /* Utility routines */
833
834 /* Set error flag */
835
836 void dt_seterr (UNIT *uptr, int32 e)
837 {
838 int32 mot = DTS_GETMOT (uptr->STATE);
839
840 dtsa = dtsa & ~DTA_STSTP; /* clear go */
841 dtsb = dtsb | DTB_ERF | e; /* set error flag */
842 if (mot >= DTS_ACCF) { /* ~stopped or stopping? */
843 sim_cancel (uptr); /* cancel activity */
844 if (dt_setpos (uptr)) return; /* update position */
845 sim_activate (uptr, dt_dctime); /* sched decel */
846 DTS_SETSTA (DTS_DECF | (mot & DTS_DIR), 0); /* state = decel */
847 }
848 DT_UPDINT;
849 return;
850 }
851
852 /* Schedule end zone */
853
854 void dt_schedez (UNIT *uptr, int32 dir)
855 {
856 int32 newpos;
857
858 if (dir) newpos = DT_EZLIN - DT_WSIZE; /* rev? rev ez */
859 else newpos = DTU_FWDEZ (uptr) + DT_WSIZE; /* fwd? fwd ez */
860 sim_activate (uptr, ABS (newpos - ((int32) uptr->pos)) * dt_ltime);
861 return;
862 }
863
864 /* Complement obverse routine */
865
866 int32 dt_comobv (int32 dat)
867 {
868 dat = dat ^ 0777777; /* compl obverse */
869 dat = ((dat >> 15) & 07) | ((dat >> 9) & 070) |
870 ((dat >> 3) & 0700) | ((dat & 0700) << 3) |
871 ((dat & 070) << 9) | ((dat & 07) << 15);
872 return dat;
873 }
874
875 /* Checksum routine */
876
877 int32 dt_csum (UNIT *uptr, int32 blk)
878 {
879 int32 *fbuf = (int32 *) uptr->filebuf;
880 int32 ba = blk * DTU_BSIZE (uptr);
881 int32 i, csum, wrd;
882
883 csum = 0777777;
884 for (i = 0; i < DTU_BSIZE (uptr); i++) { /* loop thru buf */
885 wrd = fbuf[ba + i]; /* get word */
886 csum = csum + wrd; /* 1's comp add */
887 if (csum > 0777777) csum = (csum + 1) & 0777777;
888 }
889 return (csum ^ 0777777); /* 1's comp res */
890 }
891
892 /* Get header word */
893
894 int32 dt_gethdr (UNIT *uptr, int32 blk, int32 relpos)
895 {
896 int32 wrd = relpos / DT_WSIZE;
897
898 if (wrd == DT_BLKWD) return blk; /* fwd blknum */
899 if (wrd == DT_CSMWD) return 0777777; /* rev csum */
900 if (wrd == (2 * DT_HTWRD + DTU_BSIZE (uptr) - DT_CSMWD - 1)) /* fwd csum */
901 return (dt_csum (uptr, blk));
902 if (wrd == (2 * DT_HTWRD + DTU_BSIZE (uptr) - DT_BLKWD - 1)) /* rev blkno */
903 return dt_comobv (blk);
904 return 0; /* all others */
905 }
906
907 /* Reset routine */
908
909 t_stat dt_reset (DEVICE *dptr)
910 {
911 int32 i, prev_mot;
912 UNIT *uptr;
913
914 for (i = 0; i < DT_NUMDR; i++) { /* stop all drives */
915 uptr = dt_dev.units + i;
916 if (sim_is_running) { /* CAF? */
917 prev_mot = DTS_GETMOT (uptr->STATE); /* get motion */
918 if ((prev_mot & ~DTS_DIR) > DTS_DECF) { /* accel or spd? */
919 if (dt_setpos (uptr)) continue; /* update pos */
920 sim_cancel (uptr);
921 sim_activate (uptr, dt_dctime); /* sched decel */
922 DTS_SETSTA (DTS_DECF | (prev_mot & DTS_DIR), 0);
923 }
924 }
925 else {
926 sim_cancel (uptr); /* sim reset */
927 uptr->STATE = 0;
928 uptr->LASTT = sim_grtime ();
929 }
930 }
931 dtsa = dtsb = 0; /* clear status */
932 DT_UPDINT; /* reset interrupt */
933 return SCPE_OK;
934 }
935
936 /* IORS routine */
937
938 int32 dt_iors (void)
939 {
940 #if defined IOS_DTA
941 return ((dtsb & (DTB_ERF | DTB_DTF))? IOS_DTA: 0);
942 #else
943 return 0;
944 #endif
945 }
946
947 /* Attach routine
948
949 Determine 12b, 16b, or 18b/36b format
950 Allocate buffer
951 If 12b, read 12b format and convert to 18b in buffer
952 If 16b, read 16b format and convert to 18b in buffer
953 If 18b/36b, read data into buffer
954 */
955
956 t_stat dt_attach (UNIT *uptr, char *cptr)
957 {
958 uint16 pdp8b[D8_NBSIZE];
959 uint16 pdp11b[D18_BSIZE];
960 uint32 ba, sz, k, *fbuf;
961 int32 u = uptr - dt_dev.units;
962 t_stat r;
963
964 r = attach_unit (uptr, cptr); /* attach */
965 if (r != SCPE_OK) return r; /* error? */
966 if ((sim_switches & SIM_SW_REST) == 0) { /* not from rest? */
967 uptr->flags = uptr->flags & ~(UNIT_8FMT | UNIT_11FMT); /* default 18b */
968 if (sim_switches & SWMASK ('T')) /* att 12b? */
969 uptr->flags = uptr->flags | UNIT_8FMT;
970 else if (sim_switches & SWMASK ('S')) /* att 16b? */
971 uptr->flags = uptr->flags | UNIT_11FMT;
972 else if (!(sim_switches & SWMASK ('A')) && /* autosize? */
973 (sz = sim_fsize (uptr->fileref))) {
974 if (sz == D8_FILSIZ)
975 uptr->flags = uptr->flags | UNIT_8FMT;
976 else if (sz == D11_FILSIZ)
977 uptr->flags = uptr->flags | UNIT_11FMT;
978 }
979 }
980 uptr->capac = DTU_CAPAC (uptr); /* set capacity */
981 uptr->filebuf = calloc (uptr->capac, sizeof (uint32));
982 if (uptr->filebuf == NULL) { /* can't alloc? */
983 detach_unit (uptr);
984 return SCPE_MEM;
985 }
986 fbuf = (uint32 *) uptr->filebuf; /* file buffer */
987 printf ("%s%d: ", sim_dname (&dt_dev), u);
988 if (uptr->flags & UNIT_8FMT) printf ("12b format");
989 else if (uptr->flags & UNIT_11FMT) printf ("16b format");
990 else printf ("18b/36b format");
991 printf (", buffering file in memory\n");
992 if (uptr->flags & UNIT_8FMT) { /* 12b? */
993 for (ba = 0; ba < uptr->capac; ) { /* loop thru file */
994 k = fxread (pdp8b, sizeof (uint16), D8_NBSIZE, uptr->fileref);
995 if (k == 0) break;
996 for ( ; k < D8_NBSIZE; k++) pdp8b[k] = 0;
997 for (k = 0; k < D8_NBSIZE; k = k + 3) { /* loop thru blk */
998 fbuf[ba] = ((uint32) (pdp8b[k] & 07777) << 6) |
999 ((uint32) (pdp8b[k + 1] >> 6) & 077);
1000 fbuf[ba + 1] = ((uint32) (pdp8b[k + 1] & 077) << 12) |
1001 ((uint32) pdp8b[k + 2] & 07777);
1002 ba = ba + 2;
1003 } /* end blk loop */
1004 } /* end file loop */
1005 uptr->hwmark = ba;
1006 } /* end if */
1007 else if (uptr->flags & UNIT_11FMT) { /* 16b? */
1008 for (ba = 0; ba < uptr->capac; ) { /* loop thru file */
1009 k = fxread (pdp11b, sizeof (uint16), D18_BSIZE, uptr->fileref);
1010 if (k == 0) break;
1011 for ( ; k < D18_BSIZE; k++) pdp11b[k] = 0;
1012 for (k = 0; k < D18_BSIZE; k++)
1013 fbuf[ba++] = pdp11b[k];
1014 }
1015 uptr->hwmark = ba; /* end elif */
1016 }
1017 else uptr->hwmark = fxread (uptr->filebuf, sizeof (uint32),
1018 uptr->capac, uptr->fileref);
1019 uptr->flags = uptr->flags | UNIT_BUF; /* set buf flag */
1020 uptr->pos = DT_EZLIN; /* beyond leader */
1021 uptr->LASTT = sim_grtime (); /* last pos update */
1022 return SCPE_OK;
1023 }
1024
1025 /* Detach routine
1026
1027 Cancel in progress operation
1028 If 12b, convert 18b buffer to 12b and write to file
1029 If 16b, convert 18b buffer to 16b and write to file
1030 If 18b/36b, write buffer to file
1031 Deallocate buffer
1032 */
1033
1034 t_stat dt_detach (UNIT* uptr)
1035 {
1036 uint16 pdp8b[D8_NBSIZE];
1037 uint16 pdp11b[D18_BSIZE];
1038 uint32 ba, k, *fbuf;
1039 int32 u = uptr - dt_dev.units;
1040
1041 if (!(uptr->flags & UNIT_ATT)) return SCPE_OK; /* attached? */
1042 if (sim_is_active (uptr)) {
1043 sim_cancel (uptr);
1044 if ((u == DTA_GETUNIT (dtsa)) && (dtsa & DTA_STSTP)) {
1045 dtsb = dtsb | DTB_ERF | DTB_SEL | DTB_DTF;
1046 DT_UPDINT;
1047 }
1048 uptr->STATE = uptr->pos = 0;
1049 }
1050 fbuf = (uint32 *) uptr->filebuf; /* file buffer */
1051 if (uptr->hwmark && ((uptr->flags & UNIT_RO) == 0)) { /* any data? */
1052 printf ("%s%d: writing buffer to file\n", sim_dname (&dt_dev), u);
1053 rewind (uptr->fileref); /* start of file */
1054 if (uptr->flags & UNIT_8FMT) { /* 12b? */
1055 for (ba = 0; ba < uptr->hwmark; ) { /* loop thru file */
1056 for (k = 0; k < D8_NBSIZE; k = k + 3) { /* loop blk */
1057 pdp8b[k] = (fbuf[ba] >> 6) & 07777;
1058 pdp8b[k + 1] = ((fbuf[ba] & 077) << 6) |
1059 ((fbuf[ba + 1] >> 12) & 077);
1060 pdp8b[k + 2] = fbuf[ba + 1] & 07777;
1061 ba = ba + 2;
1062 } /* end loop blk */
1063 fxwrite (pdp8b, sizeof (uint16), D8_NBSIZE, uptr->fileref);
1064 if (ferror (uptr->fileref)) break;
1065 } /* end loop file */
1066 } /* end if 12b */
1067 else if (uptr->flags & UNIT_11FMT) { /* 16b? */
1068 for (ba = 0; ba < uptr->hwmark; ) { /* loop thru file */
1069 for (k = 0; k < D18_BSIZE; k++) /* loop blk */
1070 pdp11b[k] = fbuf[ba++] & 0177777;
1071 fxwrite (pdp11b, sizeof (uint16), D18_BSIZE, uptr->fileref);
1072 if (ferror (uptr->fileref)) break;
1073 } /* end loop file */
1074 } /* end if 16b */
1075 else fxwrite (uptr->filebuf, sizeof (uint32), /* write file */
1076 uptr->hwmark, uptr->fileref);
1077 if (ferror (uptr->fileref)) perror ("I/O error");
1078 } /* end if hwmark */
1079 free (uptr->filebuf); /* release buf */
1080 uptr->flags = uptr->flags & ~UNIT_BUF; /* clear buf flag */
1081 uptr->filebuf = NULL; /* clear buf ptr */
1082 uptr->flags = uptr->flags & ~(UNIT_8FMT | UNIT_11FMT); /* default fmt */
1083 uptr->capac = DT_CAPAC; /* default size */
1084 return detach_unit (uptr);
1085 }