1 /* pdp18b_rp.c: RP15/RP02 disk pack simulator
2
3 Copyright (c) 1993-2008, 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 rp RP15/RP02 disk pack
27
28 14-Jan-04 RMS Revised IO device call interface
29 06-Feb-03 RMS Revised IOT decoding, fixed bug in initiation
30 05-Oct-02 RMS Added DIB, device number support
31 06-Jan-02 RMS Revised enable/disable support
32 29-Nov-01 RMS Added read only unit support
33 25-Nov-01 RMS Revised interrupt structure
34 Changed FLG to array
35 26-Apr-01 RMS Added device enable/disable support
36 14-Apr-99 RMS Changed t_addr to unsigned
37 29-Jun-96 RMS Added unit enable/disable support
38 */
39
40 #include "pdp18b_defs.h"
41
42 /* Constants */
43
44 #define RP_NUMWD 256 /* words/sector */
45 #define RP_NUMSC 10 /* sectors/surface */
46 #define RP_NUMSF 20 /* surfaces/cylinder */
47 #define RP_NUMCY 203 /* cylinders/drive */
48 #define RP_NUMDR 8 /* drives/controller */
49 #define RP_SIZE (RP_NUMCY * RP_NUMSF * RP_NUMSC * RP_NUMWD)
50 /* words/drive */
51
52 /* Unit specific flags */
53
54 #define UNIT_V_WLK (UNIT_V_UF + 0) /* hwre write lock */
55 #define UNIT_WLK (1u << UNIT_V_WLK)
56 #define UNIT_WPRT (UNIT_WLK | UNIT_RO) /* write protect */
57
58 /* Parameters in the unit descriptor */
59
60 #define CYL u3 /* current cylinder */
61 #define FUNC u4 /* function */
62
63 /* Status register A */
64
65 #define STA_V_UNIT 15 /* unit select */
66 #define STA_M_UNIT 07
67 #define STA_V_FUNC 12 /* function */
68 #define STA_M_FUNC 07
69 #define FN_IDLE 0
70 #define FN_READ 1
71 #define FN_WRITE 2
72 #define FN_RECAL 3
73 #define FN_SEEK 4
74 #define FN_RDALL 5
75 #define FN_WRALL 6
76 #define FN_WRCHK 7
77 #define FN_2ND 010 /* second state flag */
78 #define STA_IED 0004000 /* int enable done */
79 #define STA_IEA 0002000 /* int enable attn */
80 #define STA_GO 0001000 /* go */
81 #define STA_WPE 0000400 /* write lock error */
82 #define STA_NXC 0000200 /* nx cyl error */
83 #define STA_NXF 0000100 /* nx surface error */
84 #define STA_NXS 0000040 /* nx sector error */
85 #define STA_HNF 0000020 /* hdr not found */
86 #define STA_SUWP 0000010 /* sel unit wrt lock */
87 #define STA_SUSI 0000004 /* sel unit seek inc */
88 #define STA_DON 0000002 /* done */
89 #define STA_ERR 0000001 /* error */
90
91 #define STA_RW 0777000 /* read/write */
92 #define STA_EFLGS (STA_WPE | STA_NXC | STA_NXF | STA_NXS | \
93 STA_HNF | STA_SUSI) /* error flags */
94 #define STA_DYN (STA_SUWP | STA_SUSI) /* per unit status */
95 #define GET_UNIT(x) (((x) >> STA_V_UNIT) & STA_M_UNIT)
96 #define GET_FUNC(x) (((x) >> STA_V_FUNC) & STA_M_FUNC)
97
98 /* Status register B */
99
100 #define STB_V_ATT0 17 /* unit 0 attention */
101 #define STB_ATTN 0776000 /* attention flags */
102 #define STB_SUFU 0001000 /* sel unit unsafe */
103 #define STB_PGE 0000400 /* programming error */
104 #define STB_EOP 0000200 /* end of pack */
105 #define STB_TME 0000100 /* timing error */
106 #define STB_FME 0000040 /* format error */
107 #define STB_WCE 0000020 /* write check error */
108 #define STB_WPE 0000010 /* word parity error */
109 #define STB_LON 0000004 /* long parity error */
110 #define STB_SUSU 0000002 /* sel unit seeking */
111 #define STB_SUNR 0000001 /* sel unit not rdy */
112
113 #define STB_EFLGS (STB_SUFU | STB_PGE | STB_EOP | STB_TME | STB_FME | \
114 STB_WCE | STB_WPE | STB_LON ) /* error flags */
115 #define STB_DYN (STB_SUFU | STB_SUSU | STB_SUNR) /* per unit */
116
117 /* Disk address */
118
119 #define DA_V_SECT 0 /* sector */
120 #define DA_M_SECT 017
121 #define DA_V_SURF 5
122 #define DA_M_SURF 037
123 #define DA_V_CYL 10 /* cylinder */
124 #define DA_M_CYL 0377
125 #define GET_SECT(x) (((x) >> DA_V_SECT) & DA_M_SECT)
126 #define GET_SURF(x) (((x) >> DA_V_SURF) & DA_M_SURF)
127 #define GET_CYL(x) (((x) >> DA_V_CYL) & DA_M_CYL)
128 #define GET_DA(x) ((((GET_CYL (x) * RP_NUMSF) + GET_SURF (x)) * \
129 RP_NUMSC) + GET_SECT (x))
130
131 #define RP_MIN 2
132 #define MAX(x,y) (((x) > (y))? (x): (y))
133
134 extern int32 M[];
135 extern int32 int_hwre[API_HLVL+1], nexm;
136 extern UNIT cpu_unit;
137
138 int32 rp_sta = 0; /* status A */
139 int32 rp_stb = 0; /* status B */
140 int32 rp_ma = 0; /* memory address */
141 int32 rp_da = 0; /* disk address */
142 int32 rp_wc = 0; /* word count */
143 int32 rp_busy = 0; /* busy */
144 int32 rp_stopioe = 1; /* stop on error */
145 int32 rp_swait = 10; /* seek time */
146 int32 rp_rwait = 10; /* rotate time */
147
148 DEVICE rp_dev;
149 int32 rp63 (int32 dev, int32 pulse, int32 dat);
150 int32 rp64 (int32 dev, int32 pulse, int32 dat);
151 int32 rp_iors (void);
152 t_stat rp_svc (UNIT *uptr);
153 void rp_updsta (int32 newa, int32 newb);
154 t_stat rp_reset (DEVICE *dptr);
155 t_stat rp_attach (UNIT *uptr, char *cptr);
156 t_stat rp_detach (UNIT *uptr);
157
158 /* RP15 data structures
159
160 rp_dev RP device descriptor
161 rp_unit RP unit list
162 rp_reg RP register list
163 rp_mod RP modifier list
164 */
165
166 DIB rp_dib = { DEV_RP, 2, &rp_iors, { &rp63, &rp64 } };
167
168 UNIT rp_unit[] = {
169 { UDATA (&rp_svc, UNIT_FIX+UNIT_ATTABLE+UNIT_DISABLE, RP_SIZE) },
170 { UDATA (&rp_svc, UNIT_FIX+UNIT_ATTABLE+UNIT_DISABLE, RP_SIZE) },
171 { UDATA (&rp_svc, UNIT_FIX+UNIT_ATTABLE+UNIT_DISABLE, RP_SIZE) },
172 { UDATA (&rp_svc, UNIT_FIX+UNIT_ATTABLE+UNIT_DISABLE, RP_SIZE) },
173 { UDATA (&rp_svc, UNIT_FIX+UNIT_ATTABLE+UNIT_DISABLE, RP_SIZE) },
174 { UDATA (&rp_svc, UNIT_FIX+UNIT_ATTABLE+UNIT_DISABLE, RP_SIZE) },
175 { UDATA (&rp_svc, UNIT_FIX+UNIT_ATTABLE+UNIT_DISABLE, RP_SIZE) },
176 { UDATA (&rp_svc, UNIT_FIX+UNIT_ATTABLE+UNIT_DISABLE, RP_SIZE) }
177 };
178
179 REG rp_reg[] = {
180 { ORDATA (STA, rp_sta, 18) },
181 { ORDATA (STB, rp_stb, 18) },
182 { ORDATA (DA, rp_da, 18) },
183 { ORDATA (MA, rp_ma, 18) },
184 { ORDATA (WC, rp_wc, 18) },
185 { FLDATA (INT, int_hwre[API_RP], INT_V_RP) },
186 { FLDATA (BUSY, rp_busy, 0) },
187 { FLDATA (STOP_IOE, rp_stopioe, 0) },
188 { DRDATA (STIME, rp_swait, 24), PV_LEFT },
189 { DRDATA (RTIME, rp_rwait, 24), PV_LEFT },
190 { ORDATA (DEVNO, rp_dib.dev, 6), REG_HRO },
191 { NULL }
192 };
193
194 MTAB rp_mod[] = {
195 { UNIT_WLK, 0, "write enabled", "WRITEENABLED", NULL },
196 { UNIT_WLK, UNIT_WLK, "write locked", "LOCKED", NULL },
197 { MTAB_XTD|MTAB_VDV, 0, "DEVNO", "DEVNO", &set_devno, &show_devno },
198 { 0 }
199 };
200
201 DEVICE rp_dev = {
202 "RP", rp_unit, rp_reg, rp_mod,
203 RP_NUMDR, 8, 24, 1, 8, 18,
204 NULL, NULL, &rp_reset,
205 NULL, &rp_attach, &rp_detach,
206 &rp_dib, DEV_DISABLE
207 };
208
209 /* IOT routines */
210
rp63(int32 dev,int32 pulse,int32 dat)211 int32 rp63 (int32 dev, int32 pulse, int32 dat)
212 {
213 int32 sb = pulse & 060; /* subopcode */
214
215 rp_updsta (0, 0);
216 if (pulse & 01) {
217 if ((sb == 000) && /* DPSF */
218 ((rp_sta & (STA_DON | STA_ERR)) || (rp_stb & STB_ATTN)))
219 dat = IOT_SKP | dat;
220 else if ((sb == 020) && (rp_stb & STB_ATTN)) /* DPSA */
221 dat = IOT_SKP | dat;
222 else if ((sb == 040) && (rp_sta & STA_DON)) /* DPSJ */
223 dat = IOT_SKP | dat;
224 else if ((sb == 060) && (rp_sta & STA_ERR)) /* DPSE */
225 dat = IOT_SKP | dat;
226 }
227 if (pulse & 02) {
228 if (sb == 000) /* DPOSA */
229 dat = dat | rp_sta;
230 else if (sb == 020) /* DPOSB */
231 dat = dat | rp_stb;
232 }
233 if (pulse & 04) {
234 if (rp_busy) { /* busy? */
235 rp_updsta (0, STB_PGE);
236 return dat;
237 }
238 else if (sb == 000) { /* DPLA */
239 rp_da = dat & DMASK;
240 if (GET_SECT (rp_da) >= RP_NUMSC)
241 rp_updsta (STA_NXS, 0);
242 if (GET_SURF (rp_da) >= RP_NUMSF)
243 rp_updsta (STA_NXF, 0);
244 if (GET_CYL (rp_da) >= RP_NUMCY)
245 rp_updsta (STA_NXC, 0);
246 }
247 else if (sb == 020) { /* DPCS */
248 rp_sta = rp_sta & ~(STA_HNF | STA_DON);
249 rp_stb = rp_stb & ~(STB_FME | STB_WPE | STB_LON | STB_WCE |
250 STB_TME | STB_PGE | STB_EOP);
251 rp_updsta (0, 0);
252 }
253 else if (sb == 040) /* DPCA */
254 rp_ma = dat & DMASK;
255 else if (sb == 060) /* DPWC */
256 rp_wc = dat & DMASK;
257 }
258 return dat;
259 }
260
261 /* IOT 64 */
262
rp64(int32 dev,int32 pulse,int32 dat)263 int32 rp64 (int32 dev, int32 pulse, int32 dat)
264 {
265 int32 u, f, c, sb;
266 UNIT *uptr;
267
268 sb = pulse & 060;
269 if (pulse & 01) {
270 if (sb == 020) /* DPSN */
271 dat = IOT_SKP | dat;
272 }
273 if (pulse & 02) {
274 if (sb == 000) /* DPOU */
275 dat = dat | rp_unit[GET_UNIT (rp_sta)].CYL;
276 else if (sb == 020) /* DPOA */
277 dat = dat | rp_da;
278 else if (sb == 040) /* DPOC */
279 dat = dat | rp_ma;
280 else if (sb == 060) /* DPOW */
281 dat = dat | rp_wc;
282 }
283 if (pulse & 04) {
284 if (rp_busy) { /* busy? */
285 rp_updsta (0, STB_PGE);
286 return dat;
287 }
288 if (sb == 000) /* DPCF */
289 rp_sta = rp_sta & ~STA_RW;
290 else if (sb == 020) /* DPLZ */
291 rp_sta = rp_sta & (dat | ~STA_RW);
292 else if (sb == 040) /* DPLO */
293 rp_sta = rp_sta | (dat & STA_RW);
294 else if (sb == 060) /* DPLF */
295 rp_sta = (rp_sta & ~STA_RW) | (dat & STA_RW);
296 rp_sta = rp_sta & ~STA_DON; /* clear done */
297 u = GET_UNIT (rp_sta); /* get unit num */
298 uptr = rp_dev.units + u; /* select unit */
299 if ((rp_sta & STA_GO) && !sim_is_active (uptr)) {
300 f = uptr->FUNC = GET_FUNC (rp_sta); /* get function */
301 rp_busy = 1; /* set ctrl busy */
302 rp_sta = rp_sta & ~(STA_HNF | STA_DON); /* clear flags */
303 rp_stb = rp_stb & ~(STB_FME | STB_WPE | STB_LON | STB_WCE |
304 STB_TME | STB_PGE | STB_EOP | (1 << (STB_V_ATT0 - u)));
305 if (((uptr->flags & UNIT_ATT) == 0) || (f == FN_IDLE) ||
306 (f == FN_SEEK) || (f == FN_RECAL))
307 sim_activate (uptr, RP_MIN); /* short delay */
308 else {
309 c = GET_CYL (rp_da);
310 c = abs (c - uptr->CYL) * rp_swait; /* seek time */
311 sim_activate (uptr, MAX (RP_MIN, c + rp_rwait));
312 }
313 }
314 }
315 rp_updsta (0, 0);
316 return dat;
317 }
318
319 /* Unit service
320
321 If function = idle, clear busy
322 If seek or recal initial state, clear attention line, compute seek time,
323 put on cylinder, set second state
324 If unit not attached, give error
325 If seek or recal second state, set attention line, compute errors
326 Else complete data transfer command
327
328 The unit control block contains the function and cylinder for
329 the current command.
330 */
331
332 static int32 fill[RP_NUMWD] = { 0 };
rp_svc(UNIT * uptr)333 t_stat rp_svc (UNIT *uptr)
334 {
335 int32 f, u, comp, cyl, sect, surf;
336 int32 err, pa, da, wc, awc, i;
337
338 u = (int32) (uptr - rp_dev.units); /* get drv number */
339 f = uptr->FUNC; /* get function */
340 if (f == FN_IDLE) { /* idle? */
341 rp_busy = 0; /* clear busy */
342 return SCPE_OK;
343 }
344
345 if ((f == FN_SEEK) || (f == FN_RECAL)) { /* seek or recal? */
346 rp_busy = 0; /* not busy */
347 cyl = (f == FN_SEEK)? GET_CYL (rp_da): 0; /* get cylinder */
348 sim_activate (uptr, MAX (RP_MIN, abs (cyl - uptr->CYL) * rp_swait));
349 uptr->CYL = cyl; /* on cylinder */
350 uptr->FUNC = FN_SEEK | FN_2ND; /* set second state */
351 rp_updsta (0, 0); /* update status */
352 return SCPE_OK;
353 }
354
355 if (f == (FN_SEEK | FN_2ND)) { /* seek done? */
356 rp_updsta (0, rp_stb | (1 << (STB_V_ATT0 - u))); /* set attention */
357 return SCPE_OK;
358 }
359
360 if ((uptr->flags & UNIT_ATT) == 0) { /* not attached? */
361 rp_updsta (STA_DON, STB_SUFU); /* done, unsafe */
362 return IORETURN (rp_stopioe, SCPE_UNATT);
363 }
364
365 if ((f == FN_WRITE) && (uptr->flags & UNIT_WPRT)) { /* write locked? */
366 rp_updsta (STA_DON | STA_WPE, 0); /* error */
367 return SCPE_OK;
368 }
369
370 if (GET_SECT (rp_da) >= RP_NUMSC)
371 rp_updsta (STA_NXS, 0);
372 if (GET_SURF (rp_da) >= RP_NUMSF)
373 rp_updsta (STA_NXF, 0);
374 if (GET_CYL (rp_da) >= RP_NUMCY)
375 rp_updsta (STA_NXC, 0);
376 if (rp_sta & (STA_NXS | STA_NXF | STA_NXC)) { /* or bad disk addr? */
377 rp_updsta (STA_DON, STB_SUFU); /* done, unsafe */
378 return SCPE_OK;
379 }
380
381 pa = rp_ma & AMASK; /* get mem addr */
382 da = GET_DA (rp_da) * RP_NUMWD; /* get disk addr */
383 wc = 01000000 - rp_wc; /* get true wc */
384 if (((uint32) (pa + wc)) > MEMSIZE) { /* memory overrun? */
385 nexm = 1; /* set nexm flag */
386 wc = MEMSIZE - pa; /* limit xfer */
387 }
388 if ((da + wc) > RP_SIZE) { /* disk overrun? */
389 rp_updsta (0, STB_EOP); /* error */
390 wc = RP_SIZE - da; /* limit xfer */
391 }
392
393 err = fseek (uptr->fileref, da * sizeof (int), SEEK_SET);
394
395 if ((f == FN_READ) && (err == 0)) { /* read? */
396 awc = fxread (&M[pa], sizeof (int32), wc, uptr->fileref);
397 for ( ; awc < wc; awc++)
398 M[pa + awc] = 0;
399 err = ferror (uptr->fileref);
400 }
401
402 if ((f == FN_WRITE) && (err == 0)) { /* write? */
403 fxwrite (&M[pa], sizeof (int32), wc, uptr->fileref);
404 err = ferror (uptr->fileref);
405 if ((err == 0) && (i = (wc & (RP_NUMWD - 1)))) {
406 fxwrite (fill, sizeof (int), i, uptr->fileref);
407 err = ferror (uptr->fileref);
408 }
409 }
410
411 if ((f == FN_WRCHK) && (err == 0)) { /* write check? */
412 for (i = 0; (err == 0) && (i < wc); i++) {
413 awc = fxread (&comp, sizeof (int32), 1, uptr->fileref);
414 if (awc == 0)
415 comp = 0;
416 if (comp != M[pa + i])
417 rp_updsta (0, STB_WCE);
418 }
419 err = ferror (uptr->fileref);
420 }
421
422 rp_wc = (rp_wc + wc) & DMASK; /* final word count */
423 rp_ma = (rp_ma + wc) & DMASK; /* final mem addr */
424 da = (da + wc + (RP_NUMWD - 1)) / RP_NUMWD; /* final sector num */
425 cyl = da / (RP_NUMSC * RP_NUMSF); /* get cyl */
426 if (cyl >= RP_NUMCY)
427 cyl = RP_NUMCY - 1;
428 surf = (da % (RP_NUMSC * RP_NUMSF)) / RP_NUMSC; /* get surface */
429 sect = (da % (RP_NUMSC * RP_NUMSF)) % RP_NUMSC; /* get sector */
430 rp_da = (cyl << DA_V_CYL) | (surf << DA_V_SURF) | (sect << DA_V_SECT);
431 rp_busy = 0; /* clear busy */
432 rp_updsta (STA_DON, 0); /* set done */
433
434 if (err != 0) { /* error? */
435 perror ("RP I/O error");
436 clearerr (uptr->fileref);
437 return IORETURN (rp_stopioe, SCPE_IOERR);
438 }
439 return SCPE_OK;
440 }
441
442 /* Update status */
443
rp_updsta(int32 newa,int32 newb)444 void rp_updsta (int32 newa, int32 newb)
445 {
446 int32 f;
447 UNIT *uptr;
448
449 uptr = rp_dev.units + GET_UNIT (rp_sta);
450 rp_sta = (rp_sta & ~(STA_DYN | STA_ERR)) | newa;
451 rp_stb = (rp_stb & ~STB_DYN) | newb;
452 if (uptr->flags & UNIT_WPRT)
453 rp_sta = rp_sta | STA_SUWP;
454 if ((uptr->flags & UNIT_ATT) == 0)
455 rp_stb = rp_stb | STB_SUFU | STB_SUNR;
456 else if (sim_is_active (uptr)) {
457 f = (uptr->FUNC) & STA_M_FUNC;
458 if ((f == FN_SEEK) || (f == FN_RECAL))
459 rp_stb = rp_stb | STB_SUSU | STB_SUNR;
460 }
461 else if (uptr->CYL >= RP_NUMCY)
462 rp_sta = rp_sta | STA_SUSI;
463 if ((rp_sta & STA_EFLGS) || (rp_stb & STB_EFLGS))
464 rp_sta = rp_sta | STA_ERR;
465 if (((rp_sta & (STA_ERR | STA_DON)) && (rp_sta & STA_IED)) ||
466 ((rp_stb & STB_ATTN) && (rp_sta & STA_IEA)))
467 SET_INT (RP);
468 else CLR_INT (RP);
469 return;
470 }
471
472 /* Reset routine */
473
rp_reset(DEVICE * dptr)474 t_stat rp_reset (DEVICE *dptr)
475 {
476 int32 i;
477 UNIT *uptr;
478
479 rp_sta = rp_stb = rp_da = rp_wc = rp_ma = rp_busy = 0;
480 CLR_INT (RP);
481 for (i = 0; i < RP_NUMDR; i++) {
482 uptr = rp_dev.units + i;
483 sim_cancel (uptr);
484 uptr->CYL = uptr->FUNC = 0;
485 }
486 return SCPE_OK;
487 }
488
489 /* IORS routine */
490
rp_iors(void)491 int32 rp_iors (void)
492 {
493 return ((rp_sta & (STA_ERR | STA_DON)) || (rp_stb & STB_ATTN))? IOS_RP: 0;
494 }
495
496 /* Attach unit */
497
rp_attach(UNIT * uptr,char * cptr)498 t_stat rp_attach (UNIT *uptr, char *cptr)
499 {
500 t_stat reason;
501
502 reason = attach_unit (uptr, cptr);
503 rp_updsta (0, 0);
504 return reason;
505 }
506
507 /* Detach unit */
508
rp_detach(UNIT * uptr)509 t_stat rp_detach (UNIT *uptr)
510 {
511 t_stat reason;
512
513 reason = detach_unit (uptr);
514 rp_updsta (0, 0);
515 return reason;
516 }
517