First Commit of my working state
[simh.git] / HP2100 / hp2100_stddev.c
1 /* hp2100_stddev.c: HP2100 standard devices 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 ptr 12597A-002 paper tape reader interface
27 ptp 12597A-005 paper tape punch interface
28 tty 12531C buffered teleprinter interface
29 clk 12539C time base generator
30
31 25-Apr-08 JDB Changed TTY output wait from 100 to 200 for MSU BASIC
32 18-Apr-08 JDB Removed redundant control char handling definitions
33 14-Apr-08 JDB Changed TTY console poll to 10 msec. real time
34 Synchronized CLK with TTY if set for 10 msec.
35 Added UNIT_IDLE to TTY and CLK
36 09-Jan-08 JDB Fixed PTR trailing null counter for tape re-read
37 31-Dec-07 JDB Added IPTICK register to CLK to display CPU instr/tick
38 Corrected and verified ioCRS actions
39 28-Dec-06 JDB Added ioCRS state to I/O decoders
40 22-Nov-05 RMS Revised for new terminal processing routines
41 13-Sep-04 JDB Added paper tape loop mode, DIAG/READER modifiers to PTR
42 Added PV_LEFT to PTR TRLLIM register
43 Modified CLK to permit disable
44 15-Aug-04 RMS Added tab to control char set (from Dave Bryan)
45 14-Jul-04 RMS Generalized handling of control char echoing
46 (from Dave Bryan)
47 26-Apr-04 RMS Fixed SFS x,C and SFC x,C
48 Fixed SR setting in IBL
49 Fixed input behavior during typeout for RTE-IV
50 Suppressed nulls on TTY output for RTE-IV
51 Implemented DMA SRQ (follows FLG)
52 29-Mar-03 RMS Added support for console backpressure
53 25-Apr-03 RMS Added extended file support
54 22-Dec-02 RMS Added break support
55 01-Nov-02 RMS Revised BOOT command for IBL ROMs
56 Fixed bug in TTY reset, TTY starts in input mode
57 Fixed bug in TTY mode OTA, stores data as well
58 Fixed clock to add calibration, proper start/stop
59 Added UC option to TTY output
60 30-May-02 RMS Widened POS to 32b
61 22-Mar-02 RMS Revised for dynamically allocated memory
62 03-Nov-01 RMS Changed DEVNO to use extended SET/SHOW
63 29-Nov-01 RMS Added read only unit support
64 24-Nov-01 RMS Changed TIME to an array
65 07-Sep-01 RMS Moved function prototypes
66 21-Nov-00 RMS Fixed flag, buffer power up state
67 Added status input for ptp, tty
68 15-Oct-00 RMS Added dynamic device number support
69
70 The reader and punch, like most HP devices, have a command flop. The
71 teleprinter and clock do not.
72
73 Reader diagnostic mode simulates a tape loop by rewinding the tape image file
74 upon EOF. Normal mode EOF action is to supply TRLLIM nulls and then either
75 return SCPE_IOERR or SCPE_OK without setting the device flag.
76
77 To support CPU idling, the teleprinter interface (which doubles as the
78 simulator console) polls for input using a calibrated timer with a ten
79 millisecond period. Other polled-keyboard input devices (multiplexers and
80 the BACI card) synchronize with the console poll to ensure maximum available
81 idle time. The console poll is guaranteed to run, as the TTY device cannot
82 be disabled.
83
84 The clock (time base generator) autocalibrates. If the CLK is set to a ten
85 millisecond period (e.g., as under RTE), it is synchronized to the console
86 poll. Otherwise (e.g., as under DOS or TSB, which use 100 millisecond
87 periods), it runs asynchronously. If the specified clock frequency is below
88 10Hz, the clock service routine runs at 10Hz and counts down a repeat counter
89 before generating an interrupt. Autocalibration will not work if the clock
90 is running at 1Hz or less.
91
92 Clock diagnostic mode corresponds to inserting jumper W2 on the 12539C.
93 This turns off autocalibration and divides the longest time intervals down
94 by 10**3. The clk_time values were chosen to allow the diagnostic to
95 pass its clock calibration test.
96
97 References:
98 - 2748B Tape Reader Operating and Service Manual (02748-90041, Oct-1977)
99 - 12597A 8-Bit Duplex Register Interface Kit Operating and Service Manual
100 (12597-9002, Sep-1974)
101 - 12531C Buffered Teleprinter Interface Kit Operating and Service Manual
102 (12531-90033, Nov-1972)
103 - 12539C Time Base Generator Interface Kit Operating and Service Manual
104 (12539-90008, Jan-1975)
105 */
106
107 #include "hp2100_defs.h"
108
109 #define TTY_OUT_WAIT 200 /* TTY output wait */
110
111 #define UNIT_V_DIAG (TTUF_V_UF + 0) /* diag mode */
112 #define UNIT_V_AUTOLF (TTUF_V_UF + 1) /* auto linefeed */
113 #define UNIT_DIAG (1 << UNIT_V_DIAG)
114 #define UNIT_AUTOLF (1 << UNIT_V_AUTOLF)
115
116 #define PTP_LOW 0000040 /* low tape */
117 #define TM_MODE 0100000 /* mode change */
118 #define TM_KBD 0040000 /* enable keyboard */
119 #define TM_PRI 0020000 /* enable printer */
120 #define TM_PUN 0010000 /* enable punch */
121 #define TP_BUSY 0100000 /* busy */
122
123 #define CLK_V_ERROR 4 /* clock overrun */
124 #define CLK_ERROR (1 << CLK_V_ERROR)
125
126 extern uint32 PC, SR;
127 extern uint32 dev_cmd[2], dev_ctl[2], dev_flg[2], dev_fbf[2], dev_srq[2];
128
129 int32 ptr_stopioe = 0; /* stop on error */
130 int32 ptr_trlcnt = 0; /* trailer counter */
131 int32 ptr_trllim = 40; /* trailer to add */
132 int32 ptp_stopioe = 0;
133 int32 ttp_stopioe = 0;
134 int32 tty_buf = 0; /* tty buffer */
135 int32 tty_mode = 0; /* tty mode */
136 int32 tty_shin = 0377; /* tty shift in */
137 int32 tty_lf = 0; /* lf flag */
138 int32 clk_select = 0; /* clock time select */
139 int32 clk_error = 0; /* clock error */
140 int32 clk_ctr = 0; /* clock counter */
141 int32 clk_time[8] = { /* clock intervals */
142 155, 1550, 15500, 155000, 155000, 155000, 155000, 155000
143 };
144 int32 clk_tps[8] = { /* clock tps */
145 10000, 1000, 100, 10, 10, 10, 10, 10
146 };
147 int32 clk_rpt[8] = { /* number of repeats */
148 1, 1, 1, 1, 10, 100, 1000, 10000
149 };
150 uint32 clk_tick = 0; /* instructions per tick */
151
152 DEVICE ptr_dev, ptp_dev, tty_dev, clk_dev;
153 int32 ptrio (int32 inst, int32 IR, int32 dat);
154 t_stat ptr_svc (UNIT *uptr);
155 t_stat ptr_attach (UNIT *uptr, char *cptr);
156 t_stat ptr_reset (DEVICE *dptr);
157 t_stat ptr_boot (int32 unitno, DEVICE *dptr);
158 int32 ptpio (int32 inst, int32 IR, int32 dat);
159 t_stat ptp_svc (UNIT *uptr);
160 t_stat ptp_reset (DEVICE *dptr);
161 int32 ttyio (int32 inst, int32 IR, int32 dat);
162 t_stat tti_svc (UNIT *uptr);
163 t_stat tto_svc (UNIT *uptr);
164 t_stat tty_reset (DEVICE *dptr);
165 t_stat tty_set_opt (UNIT *uptr, int32 val, char *cptr, void *desc);
166 t_stat tty_set_alf (UNIT *uptr, int32 val, char *cptr, void *desc);
167 int32 clkio (int32 inst, int32 IR, int32 dat);
168 t_stat clk_svc (UNIT *uptr);
169 t_stat clk_reset (DEVICE *dptr);
170 int32 clk_delay (int32 flg);
171 t_stat tto_out (int32 c);
172 t_stat ttp_out (int32 c);
173
174 /* PTR data structures
175
176 ptr_dev PTR device descriptor
177 ptr_unit PTR unit descriptor
178 ptr_mod PTR modifiers
179 ptr_reg PTR register list
180 */
181
182 DIB ptr_dib = { PTR, 0, 0, 0, 0, 0, &ptrio };
183
184 UNIT ptr_unit = {
185 UDATA (&ptr_svc, UNIT_SEQ+UNIT_ATTABLE+UNIT_ROABLE, 0),
186 SERIAL_IN_WAIT
187 };
188
189 REG ptr_reg[] = {
190 { ORDATA (BUF, ptr_unit.buf, 8) },
191 { FLDATA (CMD, ptr_dib.cmd, 0) },
192 { FLDATA (CTL, ptr_dib.ctl, 0) },
193 { FLDATA (FLG, ptr_dib.flg, 0) },
194 { FLDATA (FBF, ptr_dib.fbf, 0) },
195 { FLDATA (SRQ, ptr_dib.srq, 0) },
196 { DRDATA (TRLCTR, ptr_trlcnt, 8), REG_HRO },
197 { DRDATA (TRLLIM, ptr_trllim, 8), PV_LEFT },
198 { DRDATA (POS, ptr_unit.pos, T_ADDR_W), PV_LEFT },
199 { DRDATA (TIME, ptr_unit.wait, 24), PV_LEFT },
200 { FLDATA (STOP_IOE, ptr_stopioe, 0) },
201 { ORDATA (DEVNO, ptr_dib.devno, 6), REG_HRO },
202 { NULL }
203 };
204
205 MTAB ptr_mod[] = {
206 { UNIT_DIAG, UNIT_DIAG, "diagnostic mode", "DIAG", NULL },
207 { UNIT_DIAG, 0, "reader mode", "READER", NULL },
208 { MTAB_XTD | MTAB_VDV, 0, "DEVNO", "DEVNO",
209 &hp_setdev, &hp_showdev, &ptr_dev },
210 { 0 }
211 };
212
213 DEVICE ptr_dev = {
214 "PTR", &ptr_unit, ptr_reg, ptr_mod,
215 1, 10, 31, 1, 8, 8,
216 NULL, NULL, &ptr_reset,
217 &ptr_boot, &ptr_attach, NULL,
218 &ptr_dib, DEV_DISABLE
219 };
220
221 /* PTP data structures
222
223 ptp_dev PTP device descriptor
224 ptp_unit PTP unit descriptor
225 ptp_mod PTP modifiers
226 ptp_reg PTP register list
227 */
228
229 DIB ptp_dib = { PTP, 0, 0, 0, 0, 0, &ptpio };
230
231 UNIT ptp_unit = {
232 UDATA (&ptp_svc, UNIT_SEQ+UNIT_ATTABLE, 0), SERIAL_OUT_WAIT
233 };
234
235 REG ptp_reg[] = {
236 { ORDATA (BUF, ptp_unit.buf, 8) },
237 { FLDATA (CMD, ptp_dib.cmd, 0) },
238 { FLDATA (CTL, ptp_dib.ctl, 0) },
239 { FLDATA (FLG, ptp_dib.flg, 0) },
240 { FLDATA (FBF, ptp_dib.fbf, 0) },
241 { FLDATA (SRQ, ptp_dib.srq, 0) },
242 { DRDATA (POS, ptp_unit.pos, T_ADDR_W), PV_LEFT },
243 { DRDATA (TIME, ptp_unit.wait, 24), PV_LEFT },
244 { FLDATA (STOP_IOE, ptp_stopioe, 0) },
245 { ORDATA (DEVNO, ptp_dib.devno, 6), REG_HRO },
246 { NULL }
247 };
248
249 MTAB ptp_mod[] = {
250 { MTAB_XTD | MTAB_VDV, 0, "DEVNO", "DEVNO",
251 &hp_setdev, &hp_showdev, &ptp_dev },
252 { 0 }
253 };
254
255 DEVICE ptp_dev = {
256 "PTP", &ptp_unit, ptp_reg, ptp_mod,
257 1, 10, 31, 1, 8, 8,
258 NULL, NULL, &ptp_reset,
259 NULL, NULL, NULL,
260 &ptp_dib, DEV_DISABLE
261 };
262
263 /* TTY data structures
264
265 tty_dev TTY device descriptor
266 tty_unit TTY unit descriptor
267 tty_reg TTY register list
268 tty_mod TTy modifiers list
269 */
270
271 #define TTI 0
272 #define TTO 1
273 #define TTP 2
274
275 DIB tty_dib = { TTY, 0, 0, 0, 0, 0, &ttyio };
276
277 UNIT tty_unit[] = {
278 { UDATA (&tti_svc, UNIT_IDLE | TT_MODE_UC, 0), POLL_WAIT },
279 { UDATA (&tto_svc, TT_MODE_UC, 0), TTY_OUT_WAIT },
280 { UDATA (&tto_svc, UNIT_SEQ | UNIT_ATTABLE | TT_MODE_8B, 0), SERIAL_OUT_WAIT }
281 };
282
283 REG tty_reg[] = {
284 { ORDATA (BUF, tty_buf, 8) },
285 { ORDATA (MODE, tty_mode, 16) },
286 { ORDATA (SHIN, tty_shin, 8), REG_HRO },
287 { FLDATA (CMD, tty_dib.cmd, 0), REG_HRO },
288 { FLDATA (CTL, tty_dib.ctl, 0) },
289 { FLDATA (FLG, tty_dib.flg, 0) },
290 { FLDATA (FBF, tty_dib.fbf, 0) },
291 { FLDATA (SRQ, tty_dib.srq, 0) },
292 { FLDATA (KLFP, tty_lf, 0), REG_HRO },
293 { DRDATA (KPOS, tty_unit[TTI].pos, T_ADDR_W), PV_LEFT },
294 { DRDATA (KTIME, tty_unit[TTI].wait, 24), REG_NZ + PV_LEFT },
295 { DRDATA (TPOS, tty_unit[TTO].pos, T_ADDR_W), PV_LEFT },
296 { DRDATA (TTIME, tty_unit[TTO].wait, 24), REG_NZ + PV_LEFT },
297 { DRDATA (PPOS, tty_unit[TTP].pos, T_ADDR_W), PV_LEFT },
298 { FLDATA (STOP_IOE, ttp_stopioe, 0) },
299 { ORDATA (DEVNO, tty_dib.devno, 6), REG_HRO },
300 { NULL }
301 };
302
303 MTAB tty_mod[] = {
304 { TT_MODE, TT_MODE_UC, "UC", "UC", &tty_set_opt },
305 { TT_MODE, TT_MODE_7B, "7b", "7B", &tty_set_opt },
306 { TT_MODE, TT_MODE_8B, "8b", "8B", &tty_set_opt },
307 { TT_MODE, TT_MODE_7P, "7p", "7P", &tty_set_opt },
308 { UNIT_AUTOLF, UNIT_AUTOLF, "autolf", "AUTOLF", &tty_set_alf },
309 { UNIT_AUTOLF, 0 , NULL, "NOAUTOLF", &tty_set_alf },
310 { MTAB_XTD | MTAB_VDV, 0, "DEVNO", "DEVNO",
311 &hp_setdev, &hp_showdev, &tty_dev },
312 { 0 }
313 };
314
315 DEVICE tty_dev = {
316 "TTY", tty_unit, tty_reg, tty_mod,
317 3, 10, 31, 1, 8, 8,
318 NULL, NULL, &tty_reset,
319 NULL, NULL, NULL,
320 &tty_dib, 0
321 };
322
323 /* CLK data structures
324
325 clk_dev CLK device descriptor
326 clk_unit CLK unit descriptor
327 clk_mod CLK modifiers
328 clk_reg CLK register list
329 */
330
331 DIB clk_dib = { CLK, 0, 0, 0, 0, 0, &clkio };
332
333 UNIT clk_unit = { UDATA (&clk_svc, UNIT_IDLE, 0) };
334
335 REG clk_reg[] = {
336 { ORDATA (SEL, clk_select, 3) },
337 { DRDATA (CTR, clk_ctr, 14) },
338 { FLDATA (CMD, clk_dib.cmd, 0), REG_HRO },
339 { FLDATA (CTL, clk_dib.ctl, 0) },
340 { FLDATA (FLG, clk_dib.flg, 0) },
341 { FLDATA (FBF, clk_dib.fbf, 0) },
342 { FLDATA (SRQ, clk_dib.srq, 0) },
343 { FLDATA (ERR, clk_error, CLK_V_ERROR) },
344 { BRDATA (TIME, clk_time, 10, 24, 8) },
345 { DRDATA (IPTICK, clk_tick, 24), PV_RSPC | REG_RO },
346 { ORDATA (DEVNO, clk_dib.devno, 6), REG_HRO },
347 { NULL }
348 };
349
350 MTAB clk_mod[] = {
351 { UNIT_DIAG, UNIT_DIAG, "diagnostic mode", "DIAG", NULL },
352 { UNIT_DIAG, 0, "calibrated", "CALIBRATED", NULL },
353 { MTAB_XTD | MTAB_VDV, 0, "DEVNO", "DEVNO",
354 &hp_setdev, &hp_showdev, &clk_dev },
355 { 0 }
356 };
357
358 DEVICE clk_dev = {
359 "CLK", &clk_unit, clk_reg, clk_mod,
360 1, 0, 0, 0, 0, 0,
361 NULL, NULL, &clk_reset,
362 NULL, NULL, NULL,
363 &clk_dib, DEV_DISABLE
364 };
365
366 /* Paper tape reader IO instructions */
367
368 int32 ptrio (int32 inst, int32 IR, int32 dat)
369 {
370 int32 dev;
371
372 dev = IR & I_DEVMASK; /* get device no */
373 switch (inst) { /* case on opcode */
374
375 case ioFLG: /* flag clear/set */
376 if ((IR & I_HC) == 0) { setFSR (dev); } /* STF */
377 break;
378
379 case ioSFC: /* skip flag clear */
380 if (FLG (dev) == 0) PC = (PC + 1) & VAMASK;
381 break;
382
383 case ioSFS: /* skip flag set */
384 if (FLG (dev) != 0) PC = (PC + 1) & VAMASK;
385 break;
386
387 case ioMIX: /* merge */
388 dat = dat | ptr_unit.buf;
389 break;
390
391 case ioLIX: /* load */
392 dat = ptr_unit.buf;
393 break;
394
395 case ioCRS: /* control reset */
396 /* action same as CLC */
397 case ioCTL: /* control clear/set */
398 if (IR & I_CTL) { /* CLC */
399 clrCMD (dev); /* clear cmd, ctl */
400 clrCTL (dev);
401 }
402 else { /* STC */
403 setCMD (dev); /* set cmd, ctl */
404 setCTL (dev);
405 sim_activate (&ptr_unit, ptr_unit.wait);
406 }
407 break;
408
409 default:
410 break;
411 }
412
413 if (IR & I_HC) { clrFSR (dev); } /* H/C option */
414 return dat;
415 }
416
417 /* Unit service */
418
419 t_stat ptr_svc (UNIT *uptr)
420 {
421 int32 dev, temp;
422
423 dev = ptr_dib.devno; /* get device no */
424 clrCMD (dev); /* clear cmd */
425 if ((ptr_unit.flags & UNIT_ATT) == 0) /* attached? */
426 return IORETURN (ptr_stopioe, SCPE_UNATT);
427 while ((temp = getc (ptr_unit.fileref)) == EOF) { /* read byte, error? */
428 if (feof (ptr_unit.fileref)) { /* end of file? */
429 if ((ptr_unit.flags & UNIT_DIAG) && (ptr_unit.pos > 0)) {
430 rewind (ptr_unit.fileref); /* rewind if loop mode */
431 ptr_unit.pos = 0;
432 }
433 else {
434 if (ptr_trlcnt >= ptr_trllim) { /* added all trailer? */
435 if (ptr_stopioe) { /* stop on error? */
436 printf ("PTR end of file\n");
437 return SCPE_IOERR;
438 }
439 else return SCPE_OK; /* no, just hang */
440 }
441 ptr_trlcnt++; /* count trailer */
442 temp = 0; /* read a zero */
443 break;
444 }
445 }
446 else { /* no, real error */
447 perror ("PTR I/O error");
448 clearerr (ptr_unit.fileref);
449 return SCPE_IOERR;
450 }
451 }
452 setFSR (dev); /* set flag */
453 ptr_unit.buf = temp & 0377; /* put byte in buf */
454 ptr_unit.pos = ftell (ptr_unit.fileref);
455
456 if (temp) /* character non-null? */
457 ptr_trlcnt = 0; /* clear trailing null counter */
458
459 return SCPE_OK;
460 }
461
462 /* Attach routine - clear the trailer counter */
463
464 t_stat ptr_attach (UNIT *uptr, char *cptr)
465 {
466 ptr_trlcnt = 0;
467 return attach_unit (uptr, cptr);
468 }
469
470 /* Reset routine - called from SCP, flags in DIB's */
471
472 t_stat ptr_reset (DEVICE *dptr)
473 {
474 ptr_dib.cmd = ptr_dib.ctl = 0; /* clear cmd, ctl */
475 ptr_dib.flg = ptr_dib.fbf = ptr_dib.srq = 1; /* set flg, fbf, srq */
476 ptr_unit.buf = 0;
477 sim_cancel (&ptr_unit); /* deactivate unit */
478 return SCPE_OK;
479 }
480
481 /* Paper tape reader bootstrap routine (HP 12992K ROM) */
482
483 const uint16 ptr_rom[IBL_LNT] = {
484 0107700, /*ST CLC 0,C ; intr off */
485 0002401, /* CLA,RSS ; skip in */
486 0063756, /*CN LDA M11 ; feed frame */
487 0006700, /* CLB,CCE ; set E to rd byte */
488 0017742, /* JSB READ ; get #char */
489 0007306, /* CMB,CCE,INB,SZB ; 2's comp */
490 0027713, /* JMP *+5 ; non-zero byte */
491 0002006, /* INA,SZA ; feed frame ctr */
492 0027703, /* JMP *-3 */
493 0102077, /* HLT 77B ; stop */
494 0027700, /* JMP ST ; next */
495 0077754, /* STA WC ; word in rec */
496 0017742, /* JSB READ ; get feed frame */
497 0017742, /* JSB READ ; get address */
498 0074000, /* STB 0 ; init csum */
499 0077755, /* STB AD ; save addr */
500 0067755, /*CK LDB AD ; check addr */
501 0047777, /* ADB MAXAD ; below loader */
502 0002040, /* SEZ ; E =0 => OK */
503 0027740, /* JMP H55 */
504 0017742, /* JSB READ ; get word */
505 0040001, /* ADA 1 ; cont checksum */
506 0177755, /* STA AD,I ; store word */
507 0037755, /* ISZ AD */
508 0000040, /* CLE ; force wd read */
509 0037754, /* ISZ WC ; block done? */
510 0027720, /* JMP CK ; no */
511 0017742, /* JSB READ ; get checksum */
512 0054000, /* CPB 0 ; ok? */
513 0027702, /* JMP CN ; next block */
514 0102011, /* HLT 11 ; bad csum */
515 0027700, /* JMP ST ; next */
516 0102055, /*H55 HALT 55 ; bad address */
517 0027700, /* JMP ST ; next */
518 0000000, /*RD 0 */
519 0006600, /* CLB,CME ; E reg byte ptr */
520 0103710, /* STC RDR,C ; start reader */
521 0102310, /* SFS RDR ; wait */
522 0027745, /* JMP *-1 */
523 0106410, /* MIB RDR ; get byte */
524 0002041, /* SEZ,RSS ; E set? */
525 0127742, /* JMP RD,I ; no, done */
526 0005767, /* BLF,CLE,BLF ; shift byte */
527 0027744, /* JMP RD+2 ; again */
528 0000000, /*WC 000000 ; word count */
529 0000000, /*AD 000000 ; address */
530 0177765, /*M11 -11 ; feed count */
531 0, 0, 0, 0, 0, 0, 0, 0, /* unused */
532 0, 0, 0, 0, 0, 0, 0, /* unused */
533 0000000 /*MAXAD -ST ; max addr */
534 };
535
536 t_stat ptr_boot (int32 unitno, DEVICE *dptr)
537 {
538 int32 dev;
539
540 dev = ptr_dib.devno; /* get device no */
541 if (ibl_copy (ptr_rom, dev)) return SCPE_IERR; /* copy boot to memory */
542 SR = (SR & IBL_OPT) | IBL_PTR | (dev << IBL_V_DEV); /* set SR */
543 return SCPE_OK;
544 }
545
546 /* Paper tape punch IO instructions */
547
548 int32 ptpio (int32 inst, int32 IR, int32 dat)
549 {
550 int32 dev;
551
552 dev = IR & I_DEVMASK; /* get device no */
553 switch (inst) { /* case on opcode */
554
555 case ioFLG: /* flag clear/set */
556 if ((IR & I_HC) == 0) { setFSR (dev); } /* STF */
557 break;
558
559 case ioSFC: /* skip flag clear */
560 if (FLG (dev) == 0) PC = (PC + 1) & VAMASK;
561 break;
562
563 case ioSFS: /* skip flag set */
564 if (FLG (dev) != 0) PC = (PC + 1) & VAMASK;
565 break;
566
567 case ioLIX: /* load */
568 dat = 0;
569 case ioMIX: /* merge */
570 if ((ptp_unit.flags & UNIT_ATT) == 0)
571 dat = dat | PTP_LOW; /* out of tape? */
572 break;
573
574 case ioOTX: /* output */
575 ptp_unit.buf = dat;
576 break;
577
578 case ioCRS: /* control reset */
579 /* action same as CLC */
580 case ioCTL: /* control clear/set */
581 if (IR & I_CTL) { /* CLC */
582 clrCMD (dev); /* clear cmd, ctl */
583 clrCTL (dev);
584 }
585 else { /* STC */
586 setCMD (dev); /* set cmd, ctl */
587 setCTL (dev);
588 sim_activate (&ptp_unit, ptp_unit.wait);
589 }
590 break;
591
592 default:
593 break;
594 }
595
596 if (IR & I_HC) { clrFSR (dev); } /* H/C option */
597 return dat;
598 }
599
600 /* Unit service */
601
602 t_stat ptp_svc (UNIT *uptr)
603 {
604 int32 dev;
605
606 dev = ptp_dib.devno; /* get device no */
607 clrCMD (dev); /* clear cmd */
608 setFSR (dev); /* set flag */
609 if ((ptp_unit.flags & UNIT_ATT) == 0) /* attached? */
610 return IORETURN (ptp_stopioe, SCPE_UNATT);
611 if (putc (ptp_unit.buf, ptp_unit.fileref) == EOF) { /* output byte */
612 perror ("PTP I/O error");
613 clearerr (ptp_unit.fileref);
614 return SCPE_IOERR;
615 }
616 ptp_unit.pos = ftell (ptp_unit.fileref); /* update position */
617 return SCPE_OK;
618 }
619
620 /* Reset routine */
621
622 t_stat ptp_reset (DEVICE *dptr)
623 {
624 ptp_dib.cmd = ptp_dib.ctl = 0; /* clear cmd, ctl */
625 ptp_dib.flg = ptp_dib.fbf = ptp_dib.srq = 1; /* set flg, fbf, srq */
626 ptp_unit.buf = 0;
627 sim_cancel (&ptp_unit); /* deactivate unit */
628 return SCPE_OK;
629 }
630
631 /* Terminal IO instructions */
632
633 int32 ttyio (int32 inst, int32 IR, int32 dat)
634 {
635 int32 dev;
636
637 dev = IR & I_DEVMASK; /* get device no */
638 switch (inst) { /* case on opcode */
639
640 case ioFLG: /* flag clear/set */
641 if ((IR & I_HC) == 0) { setFSR (dev); } /* STF */
642 break;
643
644 case ioSFC: /* skip flag clear */
645 if (FLG (dev) == 0) PC = (PC + 1) & VAMASK;
646 break;
647
648 case ioSFS: /* skip flag set */
649 if (FLG (dev) != 0) PC = (PC + 1) & VAMASK;
650 break;
651
652 case ioLIX: /* load */
653 dat = 0;
654 case ioMIX: /* merge */
655 dat = dat | tty_buf;
656 if (!(tty_mode & TM_KBD) && sim_is_active (&tty_unit[TTO]))
657 dat = dat | TP_BUSY;
658 break;
659
660 case ioOTX: /* output */
661 if (dat & TM_MODE) tty_mode = dat & (TM_KBD|TM_PRI|TM_PUN);
662 tty_buf = dat & 0377;
663 break;
664
665 case ioCRS: /* control reset */
666 clrCTL (dev); /* clear control */
667 setFSR (dev); /* set flag */
668 tty_mode = TM_KBD; /* set tty, clear print/punch */
669 tty_buf = 0; /* clear buffer */
670 tty_shin = 0377; /* input inactive */
671 tty_lf = 0; /* no lf pending */
672 break;
673
674 case ioCTL: /* control clear/set */
675 if (IR & I_CTL) { clrCTL (dev); } /* CLC */
676 else { /* STC */
677 setCTL (dev);
678 if (!(tty_mode & TM_KBD)) /* output? */
679 sim_activate (&tty_unit[TTO], tty_unit[TTO].wait);
680 }
681 break;
682
683 default:
684 break;
685 }
686
687 if (IR & I_HC) { clrFSR (dev); } /* H/C option */
688 return dat;
689 }
690
691 /* TTY input service routine.
692
693 The console input poll routine is scheduled with a ten millisecond period
694 using a calibrated timer, which is the source of event timing for all of the
695 keyboard polling routines. Synchronizing other keyboard polls with the
696 console poll ensures maximum idle time.
697
698 Several HP operating systems require a CR and LF sequence for line
699 termination. This is awkward on a PC, as there is no LF key (CTRL+J is
700 needed instead). We provide an AUTOLF mode to add a LF automatically to each
701 CR input. When this mode is set, entering CR will set a flag, which will
702 cause a LF to be supplied automatically at the next input poll.
703
704 The 12531C teleprinter interface and the later 12880A CRT interface provide a
705 clever mechanism to detect a keypress during output. This is used by DOS and
706 RTE to allow the user to interrupt lengthy output operations to enter system
707 commands.
708
709 Referring to the 12531C schematic, the terminal input enters on pin X
710 ("DATA FROM EIA COMPATIBLE DEVICE"). The signal passes through four
711 transistor inversions (Q8, Q1, Q2, and Q3) to appear on pin 12 of NAND gate
712 U104C. If the flag flip-flop is not set, the terminal input passes to the
713 (inverted) output of U104C and thence to the D input of the first of the
714 flip-flops forming the data register.
715
716 In the idle condition (no key pressed), the terminal input line is marking
717 (voltage negative), so in passing through a total of five inversions, a
718 logic one is presented at the serial input of the data register. During an
719 output operation, the register is parallel loaded and serially shifted,
720 sending the output data through the register to the device and -- this is
721 the crux -- filling the register with logic ones from U104C.
722
723 At the end of the output operation, the card flag is set, an interrupt
724 occurs, and the RTE driver is entered. The driver then does an LIA SC to
725 read the contents of the data register. If no key has been pressed during
726 the output operation, the register will read as all ones (octal 377). If,
727 however, any key was struck, at least one zero bit will be present. If the
728 register value doesn't equal 377, the driver sets the system "operator
729 attention" flag, which will cause DOS or RTE to output an asterisk prompt and
730 initiate a terminal read when the current output line is completed.
731 */
732
733 t_stat tti_svc (UNIT *uptr)
734 {
735 int32 c, dev;
736
737 uptr->wait = sim_rtcn_calb (POLL_RATE, TMR_POLL); /* calibrate poll timer */
738 sim_activate (uptr, uptr->wait); /* continue poll */
739
740 dev = tty_dib.devno; /* get device no */
741 tty_shin = 0377; /* assume inactive */
742 if (tty_lf) { /* auto lf pending? */
743 c = 012; /* force lf */
744 tty_lf = 0;
745 }
746 else {
747 if ((c = sim_poll_kbd ()) < SCPE_KFLAG) return c; /* no char or error? */
748 if (c & SCPE_BREAK) c = 0; /* break? */
749 else c = sim_tt_inpcvt (c, TT_GET_MODE (uptr->flags));
750 tty_lf = ((c & 0177) == 015) && (uptr->flags & UNIT_AUTOLF);
751 }
752 if (tty_mode & TM_KBD) { /* keyboard enabled? */
753 tty_buf = c; /* put char in buf */
754 uptr->pos = uptr->pos + 1;
755 setFSR (dev); /* set flag */
756 if (c) {
757 tto_out (c); /* echo? */
758 return ttp_out (c); /* punch? */
759 }
760 }
761 else tty_shin = c; /* no, char shifts in */
762 return SCPE_OK;
763 }
764
765 /* TTY output service routine */
766
767 t_stat tto_svc (UNIT *uptr)
768 {
769 int32 c, dev;
770 t_stat r;
771
772 c = tty_buf; /* get char */
773 tty_buf = tty_shin; /* shift in */
774 tty_shin = 0377; /* line inactive */
775 if ((r = tto_out (c)) != SCPE_OK) { /* output; error? */
776 sim_activate (uptr, uptr->wait); /* retry */
777 return ((r == SCPE_STALL)? SCPE_OK: r); /* !stall? report */
778 }
779 dev = tty_dib.devno; /* get device no */
780 setFSR (dev); /* set done flag */
781 return ttp_out (c); /* punch if enabled */
782 }
783
784 t_stat tto_out (int32 c)
785 {
786 t_stat r;
787
788 if (tty_mode & TM_PRI) { /* printing? */
789 c = sim_tt_outcvt (c, TT_GET_MODE (tty_unit[TTO].flags));
790 if (c >= 0) { /* valid? */
791 if (r = sim_putchar_s (c)) return r; /* output char */
792 tty_unit[TTO].pos = tty_unit[TTO].pos + 1;
793 }
794 }
795 return SCPE_OK;
796 }
797
798 t_stat ttp_out (int32 c)
799 {
800 if (tty_mode & TM_PUN) { /* punching? */
801 if ((tty_unit[TTP].flags & UNIT_ATT) == 0) /* attached? */
802 return IORETURN (ttp_stopioe, SCPE_UNATT);
803 if (putc (c, tty_unit[TTP].fileref) == EOF) { /* output char */
804 perror ("TTP I/O error");
805 clearerr (tty_unit[TTP].fileref);
806 return SCPE_IOERR;
807 }
808 tty_unit[TTP].pos = ftell (tty_unit[TTP].fileref);
809 }
810 return SCPE_OK;
811 }
812
813 /* TTY reset routine */
814
815 t_stat tty_reset (DEVICE *dptr)
816 {
817 tty_dib.cmd = tty_dib.ctl = 0; /* clear cmd, ctl */
818 tty_dib.flg = tty_dib.fbf = tty_dib.srq = 1; /* set flg, fbf, srq */
819 tty_mode = TM_KBD; /* enable input */
820 tty_buf = 0;
821 tty_shin = 0377; /* input inactive */
822 tty_lf = 0; /* no lf pending */
823 tty_unit[TTI].wait = POLL_WAIT; /* reset initial poll */
824 sim_rtcn_init (tty_unit[TTI].wait, TMR_POLL); /* init poll timer */
825 sim_activate (&tty_unit[TTI], tty_unit[TTI].wait); /* activate poll */
826 sim_cancel (&tty_unit[TTO]); /* cancel output */
827 return SCPE_OK;
828 }
829
830 t_stat tty_set_opt (UNIT *uptr, int32 val, char *cptr, void *desc)
831 {
832 int32 u = uptr - tty_dev.units;
833
834 if (u > TTO) return SCPE_NOFNC;
835 if ((u == TTI) && (val == TT_MODE_7P))
836 val = TT_MODE_7B;
837 tty_unit[u].flags = (tty_unit[u].flags & ~TT_MODE) | val;
838 return SCPE_OK;
839 }
840
841 t_stat tty_set_alf (UNIT *uptr, int32 val, char *cptr, void *desc)
842 {
843 int32 u = uptr - tty_dev.units;
844
845 if (u != TTI) return SCPE_NOFNC;
846 return SCPE_OK;
847 }
848
849 /* Synchronize polling.
850
851 Return an event time corresponding either with the amount of time remaining
852 in the current poll (mode = INITIAL) or the amount of time in a full poll
853 period (mode = SERVICE). If the former call is made when the device service
854 routine is started, then making the latter call during unit service will
855 ensure that the polls remain synchronized.
856 */
857
858 int32 sync_poll (POLLMODE poll_mode)
859 {
860 int32 poll_time;
861
862 if (poll_mode == INITIAL) {
863 poll_time = sim_is_active (&tty_unit[TTI]);
864
865 if (poll_time)
866 return poll_time;
867 else
868 return POLL_WAIT;
869 }
870 else
871 return tty_unit[TTI].wait;
872 }
873
874
875 /* Clock I/O instructions.
876
877 The time base generator (CLK) provides periodic interrupts from 100
878 microseconds to 1000 seconds. The CLK uses a calibrated timer to provide the
879 time base. For periods ranging from 1 to 1000 seconds, a 100 millisecond
880 timer is used, and 10 to 10000 ticks are counted before setting the device
881 flag to indicate that the period has expired.
882
883 If the period is set to ten milliseconds, the console poll timer is used
884 instead of an independent timer. This is to maximize the idle period.
885
886 In diagnostic mode, the clock period is set to the expected number of CPU
887 instructions, rather than wall-clock time, so that the diagnostic executes as
888 expected.
889 */
890
891 int32 clkio (int32 inst, int32 IR, int32 dat)
892 {
893 int32 dev;
894
895 dev = IR & I_DEVMASK; /* get device no */
896 switch (inst) { /* case on opcode */
897
898 case ioFLG: /* flag clear/set */
899 if ((IR & I_HC) == 0) { setFSR (dev); } /* STF */
900 break;
901
902 case ioSFC: /* skip flag clear */
903 if (FLG (dev) == 0) PC = (PC + 1) & VAMASK;
904 break;
905
906 case ioSFS: /* skip flag set */
907 if (FLG (dev) != 0) PC = (PC + 1) & VAMASK;
908 break;
909
910 case ioMIX: /* merge */
911 dat = dat | clk_error;
912 break;
913
914 case ioLIX: /* load */
915 dat = clk_error;
916 break;
917
918 case ioOTX: /* output */
919 clk_select = dat & 07; /* save select */
920 sim_cancel (&clk_unit); /* stop the clock */
921 clrCTL (dev); /* clear control */
922 break;
923
924 case ioCRS: /* control reset */
925 /* action same as CLC */
926 case ioCTL: /* control clear/set */
927 if (IR & I_CTL) { /* CLC */
928 clrCTL (dev); /* turn off clock */
929 sim_cancel (&clk_unit); /* deactivate unit */
930 }
931 else { /* STC */
932 setCTL (dev); /* set CTL */
933
934 if (clk_unit.flags & UNIT_DIAG) /* diag mode? */
935 clk_unit.flags = clk_unit.flags & ~UNIT_IDLE; /* not calibrated */
936 else
937 clk_unit.flags = clk_unit.flags | UNIT_IDLE; /* is calibrated */
938
939 if (!sim_is_active (&clk_unit)) { /* clock running? */
940 clk_tick = clk_delay (0); /* get tick count */
941
942 if ((clk_unit.flags & UNIT_DIAG) == 0) /* calibrated? */
943 if (clk_select == 2) /* 10 msec. interval? */
944 clk_tick = sync_poll (INITIAL); /* sync poll */
945 else
946 sim_rtcn_init (clk_tick, TMR_CLK); /* initialize timer */
947
948 sim_activate (&clk_unit, clk_tick); /* start clock */
949 clk_ctr = clk_delay (1); /* set repeat ctr */
950 }
951 clk_error = 0; /* clear error */
952 }
953 break;
954
955 default:
956 break;
957 }
958
959 if (IR & I_HC) { clrFSR (dev); } /* H/C option */
960 return dat;
961 }
962
963 /* CLK unit service.
964
965 As with the I/O handler, if the time base period is set to ten milliseconds,
966 the console poll timer is used instead of an independent timer.
967 */
968
969 t_stat clk_svc (UNIT *uptr)
970 {
971 int32 dev;
972
973 dev = clk_dib.devno; /* get device no */
974 if (!CTL (dev)) return SCPE_OK; /* CTL off? done */
975
976 if (clk_unit.flags & UNIT_DIAG) /* diag mode? */
977 clk_tick = clk_delay (0); /* get fixed delay */
978 else if (clk_select == 2) /* 10 msec period? */
979 clk_tick = sync_poll (SERVICE); /* sync poll */
980 else
981 clk_tick = sim_rtcn_calb (clk_tps[clk_select], TMR_CLK); /* calibrate delay */
982
983 sim_activate (uptr, clk_tick); /* reactivate */
984 clk_ctr = clk_ctr - 1; /* decrement counter */
985 if (clk_ctr <= 0) { /* end of interval? */
986 if (FLG (dev)) clk_error = CLK_ERROR; /* overrun? error */
987 else { setFSR (dev); } /* else set flag */
988 clk_ctr = clk_delay (1); /* reset counter */
989 }
990 return SCPE_OK;
991 }
992
993 /* Reset routine */
994
995 t_stat clk_reset (DEVICE *dptr)
996 {
997 clk_dib.cmd = clk_dib.ctl = 0; /* clear cmd, ctl */
998 clk_dib.flg = clk_dib.fbf = clk_dib.srq = 1; /* set flg, fbf, srq */
999 clk_error = 0; /* clear error */
1000 clk_select = 0; /* clear select */
1001 clk_ctr = 0; /* clear counter */
1002 sim_cancel (&clk_unit); /* deactivate unit */
1003 return SCPE_OK;
1004 }
1005
1006 /* Clock delay routine */
1007
1008 int32 clk_delay (int32 flg)
1009 {
1010 int32 sel = clk_select;
1011
1012 if ((clk_unit.flags & UNIT_DIAG) && (sel >= 4)) sel = sel - 3;
1013 if (flg) return clk_rpt[sel];
1014 else return clk_time[sel];
1015 }