First Commit of my working state
[simh.git] / PDP18B / pdp18b_rp.c
1 /* pdp18b_rp.c: RP15/RP02 disk pack simulator
2
3 Copyright (c) 1993-2005, 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
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) dat = dat | rp_sta; /* DPOSA */
229 else if (sb == 020) dat = dat | rp_stb; /* DPOSB */
230 }
231 if (pulse & 04) {
232 if (rp_busy) { /* busy? */
233 rp_updsta (0, STB_PGE);
234 return dat;
235 }
236 else if (sb == 000) { /* DPLA */
237 rp_da = dat & DMASK;
238 if (GET_SECT (rp_da) >= RP_NUMSC) rp_updsta (STA_NXS, 0);
239 if (GET_SURF (rp_da) >= RP_NUMSF) rp_updsta (STA_NXF, 0);
240 if (GET_CYL (rp_da) >= RP_NUMCY) rp_updsta (STA_NXC, 0);
241 }
242 else if (sb == 020) { /* DPCS */
243 rp_sta = rp_sta & ~(STA_HNF | STA_DON);
244 rp_stb = rp_stb & ~(STB_FME | STB_WPE | STB_LON | STB_WCE |
245 STB_TME | STB_PGE | STB_EOP);
246 rp_updsta (0, 0);
247 }
248 else if (sb == 040) rp_ma = dat & DMASK; /* DPCA */
249 else if (sb == 060) rp_wc = dat & DMASK; /* DPWC */
250 }
251 return dat;
252 }
253
254 /* IOT 64 */
255
256 int32 rp64 (int32 dev, int32 pulse, int32 dat)
257 {
258 int32 u, f, c, sb;
259 UNIT *uptr;
260
261 sb = pulse & 060;
262 if (pulse & 01) {
263 if (sb == 020) dat = IOT_SKP | dat; /* DPSN */
264 }
265 if (pulse & 02) {
266 if (sb == 000) /* DPOU */
267 dat = dat | rp_unit[GET_UNIT (rp_sta)].CYL;
268 else if (sb == 020) dat = dat | rp_da; /* DPOA */
269 else if (sb == 040) dat = dat | rp_ma; /* DPOC */
270 else if (sb == 060) dat = dat | rp_wc; /* DPOW */
271 }
272 if (pulse & 04) {
273 if (rp_busy) { /* busy? */
274 rp_updsta (0, STB_PGE);
275 return dat;
276 }
277 if (sb == 000) /* DPCF */
278 rp_sta = rp_sta & ~STA_RW;
279 else if (sb == 020) /* DPLZ */
280 rp_sta = rp_sta & (dat | ~STA_RW);
281 else if (sb == 040) /* DPLO */
282 rp_sta = rp_sta | (dat & STA_RW);
283 else if (sb == 060) /* DPLF */
284 rp_sta = (rp_sta & ~STA_RW) | (dat & STA_RW);
285 rp_sta = rp_sta & ~STA_DON; /* clear done */
286 u = GET_UNIT (rp_sta); /* get unit num */
287 uptr = rp_dev.units + u; /* select unit */
288 if ((rp_sta & STA_GO) && !sim_is_active (uptr)) {
289 f = uptr->FUNC = GET_FUNC (rp_sta); /* get function */
290 rp_busy = 1; /* set ctrl busy */
291 rp_sta = rp_sta & ~(STA_HNF | STA_DON); /* clear flags */
292 rp_stb = rp_stb & ~(STB_FME | STB_WPE | STB_LON | STB_WCE |
293 STB_TME | STB_PGE | STB_EOP | (1 << (STB_V_ATT0 - u)));
294 if (((uptr->flags & UNIT_ATT) == 0) || (f == FN_IDLE) ||
295 (f == FN_SEEK) || (f == FN_RECAL))
296 sim_activate (uptr, RP_MIN); /* short delay */
297 else {
298 c = GET_CYL (rp_da);
299 c = abs (c - uptr->CYL) * rp_swait; /* seek time */
300 sim_activate (uptr, MAX (RP_MIN, c + rp_rwait));
301 }
302 }
303 }
304 rp_updsta (0, 0);
305 return dat;
306 }
307
308 /* Unit service
309
310 If function = idle, clear busy
311 If seek or recal initial state, clear attention line, compute seek time,
312 put on cylinder, set second state
313 If unit not attached, give error
314 If seek or recal second state, set attention line, compute errors
315 Else complete data transfer command
316
317 The unit control block contains the function and cylinder for
318 the current command.
319 */
320
321 static int32 fill[RP_NUMWD] = { 0 };
322 t_stat rp_svc (UNIT *uptr)
323 {
324 int32 f, u, comp, cyl, sect, surf;
325 int32 err, pa, da, wc, awc, i;
326
327 u = (int32) (uptr - rp_dev.units); /* get drv number */
328 f = uptr->FUNC; /* get function */
329 if (f == FN_IDLE) { /* idle? */
330 rp_busy = 0; /* clear busy */
331 return SCPE_OK;
332 }
333
334 if ((f == FN_SEEK) || (f == FN_RECAL)) { /* seek or recal? */
335 rp_busy = 0; /* not busy */
336 cyl = (f == FN_SEEK)? GET_CYL (rp_da): 0; /* get cylinder */
337 sim_activate (uptr, MAX (RP_MIN, abs (cyl - uptr->CYL) * rp_swait));
338 uptr->CYL = cyl; /* on cylinder */
339 uptr->FUNC = FN_SEEK | FN_2ND; /* set second state */
340 rp_updsta (0, 0); /* update status */
341 return SCPE_OK;
342 }
343
344 if (f == (FN_SEEK | FN_2ND)) { /* seek done? */
345 rp_updsta (0, rp_stb | (1 << (STB_V_ATT0 - u))); /* set attention */
346 return SCPE_OK;
347 }
348
349 if ((uptr->flags & UNIT_ATT) == 0) { /* not attached? */
350 rp_updsta (STA_DON, STB_SUFU); /* done, unsafe */
351 return IORETURN (rp_stopioe, SCPE_UNATT);
352 }
353
354 if ((f == FN_WRITE) && (uptr->flags & UNIT_WPRT)) { /* write locked? */
355 rp_updsta (STA_DON | STA_WPE, 0); /* error */
356 return SCPE_OK;
357 }
358
359 if (GET_SECT (rp_da) >= RP_NUMSC) rp_updsta (STA_NXS, 0);
360 if (GET_SURF (rp_da) >= RP_NUMSF) rp_updsta (STA_NXF, 0);
361 if (GET_CYL (rp_da) >= RP_NUMCY) rp_updsta (STA_NXC, 0);
362 if (rp_sta & (STA_NXS | STA_NXF | STA_NXC)) { /* or bad disk addr? */
363 rp_updsta (STA_DON, STB_SUFU); /* done, unsafe */
364 return SCPE_OK;
365 }
366
367 pa = rp_ma & AMASK; /* get mem addr */
368 da = GET_DA (rp_da) * RP_NUMWD; /* get disk addr */
369 wc = 01000000 - rp_wc; /* get true wc */
370 if (((uint32) (pa + wc)) > MEMSIZE) { /* memory overrun? */
371 nexm = 1; /* set nexm flag */
372 wc = MEMSIZE - pa; /* limit xfer */
373 }
374 if ((da + wc) > RP_SIZE) { /* disk overrun? */
375 rp_updsta (0, STB_EOP); /* error */
376 wc = RP_SIZE - da; /* limit xfer */
377 }
378
379 err = fseek (uptr->fileref, da * sizeof (int), SEEK_SET);
380
381 if ((f == FN_READ) && (err == 0)) { /* read? */
382 awc = fxread (&M[pa], sizeof (int32), wc, uptr->fileref);
383 for ( ; awc < wc; awc++) M[pa + awc] = 0;
384 err = ferror (uptr->fileref);
385 }
386
387 if ((f == FN_WRITE) && (err == 0)) { /* write? */
388 fxwrite (&M[pa], sizeof (int32), wc, uptr->fileref);
389 err = ferror (uptr->fileref);
390 if ((err == 0) && (i = (wc & (RP_NUMWD - 1)))) {
391 fxwrite (fill, sizeof (int), i, uptr->fileref);
392 err = ferror (uptr->fileref);
393 }
394 }
395
396 if ((f == FN_WRCHK) && (err == 0)) { /* write check? */
397 for (i = 0; (err == 0) && (i < wc); i++) {
398 awc = fxread (&comp, sizeof (int32), 1, uptr->fileref);
399 if (awc == 0) comp = 0;
400 if (comp != M[pa + i]) rp_updsta (0, STB_WCE);
401 }
402 err = ferror (uptr->fileref);
403 }
404
405 rp_wc = (rp_wc + wc) & DMASK; /* final word count */
406 rp_ma = (rp_ma + wc) & DMASK; /* final mem addr */
407 da = (da + wc + (RP_NUMWD - 1)) / RP_NUMWD; /* final sector num */
408 cyl = da / (RP_NUMSC * RP_NUMSF); /* get cyl */
409 if (cyl >= RP_NUMCY) cyl = RP_NUMCY - 1;
410 surf = (da % (RP_NUMSC * RP_NUMSF)) / RP_NUMSC; /* get surface */
411 sect = (da % (RP_NUMSC * RP_NUMSF)) % RP_NUMSC; /* get sector */
412 rp_da = (cyl << DA_V_CYL) | (surf << DA_V_SURF) | (sect << DA_V_SECT);
413 rp_busy = 0; /* clear busy */
414 rp_updsta (STA_DON, 0); /* set done */
415
416 if (err != 0) { /* error? */
417 perror ("RP I/O error");
418 clearerr (uptr->fileref);
419 return IORETURN (rp_stopioe, SCPE_IOERR);
420 }
421 return SCPE_OK;
422 }
423
424 /* Update status */
425
426 void rp_updsta (int32 newa, int32 newb)
427 {
428 int32 f;
429 UNIT *uptr;
430
431 uptr = rp_dev.units + GET_UNIT (rp_sta);
432 rp_sta = (rp_sta & ~(STA_DYN | STA_ERR)) | newa;
433 rp_stb = (rp_stb & ~STB_DYN) | newb;
434 if (uptr->flags & UNIT_WPRT) rp_sta = rp_sta | STA_SUWP;
435 if ((uptr->flags & UNIT_ATT) == 0) rp_stb = rp_stb | STB_SUFU | STB_SUNR;
436 else if (sim_is_active (uptr)) {
437 f = (uptr->FUNC) & STA_M_FUNC;
438 if ((f == FN_SEEK) || (f == FN_RECAL))
439 rp_stb = rp_stb | STB_SUSU | STB_SUNR;
440 }
441 else if (uptr->CYL >= RP_NUMCY) rp_sta = rp_sta | STA_SUSI;
442 if ((rp_sta & STA_EFLGS) || (rp_stb & STB_EFLGS)) rp_sta = rp_sta | STA_ERR;
443 if (((rp_sta & (STA_ERR | STA_DON)) && (rp_sta & STA_IED)) ||
444 ((rp_stb & STB_ATTN) && (rp_sta & STA_IEA))) SET_INT (RP);
445 else CLR_INT (RP);
446 return;
447 }
448
449 /* Reset routine */
450
451 t_stat rp_reset (DEVICE *dptr)
452 {
453 int32 i;
454 UNIT *uptr;
455
456 rp_sta = rp_stb = rp_da = rp_wc = rp_ma = rp_busy = 0;
457 CLR_INT (RP);
458 for (i = 0; i < RP_NUMDR; i++) {
459 uptr = rp_dev.units + i;
460 sim_cancel (uptr);
461 uptr->CYL = uptr->FUNC = 0;
462 }
463 return SCPE_OK;
464 }
465
466 /* IORS routine */
467
468 int32 rp_iors (void)
469 {
470 return ((rp_sta & (STA_ERR | STA_DON)) || (rp_stb & STB_ATTN))? IOS_RP: 0;
471 }
472
473 /* Attach unit */
474
475 t_stat rp_attach (UNIT *uptr, char *cptr)
476 {
477 t_stat reason;
478
479 reason = attach_unit (uptr, cptr);
480 rp_updsta (0, 0);
481 return reason;
482 }
483
484 /* Detach unit */
485
486 t_stat rp_detach (UNIT *uptr)
487 {
488 t_stat reason;
489
490 reason = detach_unit (uptr);
491 rp_updsta (0, 0);
492 return reason;
493 }