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