First Commit of my working state
[simh.git] / HP2100 / hp2100_lps.c
1 /* hp2100_lps.c: HP 2100 12653A/2767 line printer simulator
2
3 Copyright (c) 1993-2007, 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 lps 12653A 2767 line printer
27 12566B microcircuit interface with loopback diagnostic connector
28
29 10-May-07 RMS Added UNIT_TEXT flag
30 11-Jan-07 JDB CLC cancels I/O event if DIAG (jumper W9 in "A" pos)
31 Added ioCRS state to I/O decoders
32 19-Nov-04 JDB Added restart when set online, etc.
33 Fixed col count for non-printing chars
34 01-Oct-04 JDB Added SET OFFLINE/ONLINE, POWEROFF/POWERON
35 Fixed status returns for error conditions
36 Fixed handling of non-printing characters
37 Fixed handling of characters after column 80
38 Improved timing model accuracy for RTE
39 Added fast/realistic timing
40 Added debug printouts
41 03-Jun-04 RMS Fixed timing (found by Dave Bryan)
42 26-Apr-04 RMS Fixed SFS x,C and SFC x,C
43 Implemented DMA SRQ (follows FLG)
44 25-Apr-03 RMS Revised for extended file support
45 24-Oct-02 RMS Added microcircuit test features
46 30-May-02 RMS Widened POS to 32b
47 03-Dec-01 RMS Changed DEVNO to use extended SET/SHOW
48 07-Sep-01 RMS Moved function prototypes
49 21-Nov-00 RMS Fixed flag, fbf power up state
50 Added command flop
51 15-Oct-00 RMS Added variable device number support
52
53 This module simulates two different devices. In "diagnostic mode," it
54 simulates a 12566B microcircuit interface card with a loopback connector and
55 the jumpers set as required for execution of the General Purpose Register
56 diagnostic. In non-diagnostic mode, it simulates a 12653A line printer
57 interface card and a 2767 line printer.
58
59 The 12566B interface with the loopback connector ties the device command
60 output to the device flag input. Setting control therefore causes device
61 flag to set almost immediately. Device command is active only during that
62 interim. Under simulation, the loopback occurs within the STC handler, and
63 CMD is never set.
64
65 The 2767 impact printer has a rotating drum with 80 columns of 64 raised
66 characters. ASCII codes 32 through 95 (SPACE through "_") form the print
67 repertoire. The printer responds to the control characters FF, LF, and CR.
68
69 The 80 columns are divided into four zones of 20 characters each that are
70 addressed sequentially. Received characters are buffered in a 20-character
71 memory. When the 20th printable character is received, the current zone is
72 printed, and the memory is reset. In the absence of print command
73 characters, a zone print operation will commence after each group of 20
74 printable characters is transmitted to the printer.
75
76 The print command characters have these actions:
77
78 * CR -- print the characters in the current zone, reset to zone 1, and clear
79 the buffer memory.
80 * LF -- same as CR, plus advances the paper one line.
81 * FF -- same as CR, plus advances the paper to the top of the next form.
82
83 The 2767 provides two status bits via the interface:
84
85 bit 15 -- printer not ready
86 bit 0 -- printer busy
87
88 The expected status returns are:
89
90 100001 -- power off or cable disconnected
91 100001 -- initial power on, then changes to 000001 within sixty
92 seconds of initial power on
93 000001 -- power on, paper unloaded or printer offline or not idle
94 000000 -- power on, paper loaded and printer online and idle
95
96 These simulator commands provide the listed printer states:
97
98 SET LPS POWEROFF --> power off or cable disconnected
99 SET LPS POWERON --> power on
100 SET LPS OFFLINE --> printer offline
101 SET LPS ONLINE --> printer online
102 ATT LPS <file> --> paper loaded
103 DET LPS --> paper out
104
105 References:
106 - 2767A Line Printer Operating and Service Manual (02767-90002, Oct-1973)
107 - 12566B, 12566B-001, 12566B-002, 12566B-003 Microcircuit Interface Kits
108 Operating and Service Manual (12566-90015, Apr-1976)
109
110 The following implemented behaviors have been inferred from secondary sources
111 (diagnostics, operating system drivers, etc.), due to absent or contradictory
112 authoritative information; future correction may be needed:
113
114 1. Paper out sets BUSY instead of NOT READY.
115 2. Print operation in progress sets BUSY instead of NOT READY.
116 3. Characters not in the print repertoire are replaced with blanks.
117 4. The 81st and succeeding characters overprint the current line.
118 */
119
120 #include "hp2100_defs.h"
121 #include "hp2100_cpu.h"
122
123 #define LPS_ZONECNT 20 /* zone char count */
124 #define LPS_PAGECNT 80 /* page char count */
125 #define LPS_PAGELNT 60 /* page line length */
126 #define LPS_FORMLNT 66 /* form line length */
127
128 /* Printer power states */
129
130 #define LPS_ON 0 /* power is on */
131 #define LPS_OFF 1 /* power is off */
132 #define LPS_TURNING_ON 2 /* power is turning on */
133
134 #define LPS_BUSY 0000001 /* busy status */
135 #define LPS_NRDY 0100000 /* not ready status */
136 #define LPS_PWROFF LPS_BUSY | LPS_NRDY /* power-off status */
137
138 #define UNIT_V_DIAG (UNIT_V_UF + 0) /* diagnostic mode */
139 #define UNIT_V_POWEROFF (UNIT_V_UF + 1) /* unit powered off */
140 #define UNIT_V_OFFLINE (UNIT_V_UF + 2) /* unit offline */
141 #define UNIT_DIAG (1 << UNIT_V_DIAG)
142 #define UNIT_POWEROFF (1 << UNIT_V_POWEROFF)
143 #define UNIT_OFFLINE (1 << UNIT_V_OFFLINE)
144
145 extern uint32 PC;
146 extern uint32 dev_cmd[2], dev_ctl[2], dev_flg[2], dev_fbf[2], dev_srq[2];
147 extern FILE *sim_deb;
148
149 int32 lps_ccnt = 0; /* character count */
150 int32 lps_lcnt = 0; /* line count */
151 int32 lps_stopioe = 0; /* stop on error */
152 int32 lps_sta = 0; /* printer status */
153 int32 lps_timing = 1; /* timing type */
154 uint32 lps_power = LPS_ON; /* power state */
155
156 /* Hardware timing:
157 (based on 1580 instr/msec) instr msec calc msec
158 ------------------------
159 - character transfer time : ctime = 2 2 us
160 - per-zone printing time : ptime = 55300 35 40
161 - per-line paper slew time : stime = 17380 11 13
162 - power-on ready delay time : rtime = 158000 100
163
164 NOTE: the printer acknowledges before the print motion has stopped to allow
165 for continuous slew, so the set times are a bit less than the calculated
166 operation time from the manual.
167
168 NOTE: the 2767 diagnostic checks completion times, so the realistic timing
169 must be used. Because simulator timing is in instructions, and because the
170 diagnostic uses the TIMER instruction (~1580 executions per millisecond) when
171 running on a 1000-E/F but a software timing loop (~400-600 executions per
172 millisecond) when running on anything else, realistic timings are decreased by
173 three-fourths when not executing on an E/F.
174 */
175
176 int32 lps_ctime = 0; /* char xfer time */
177 int32 lps_ptime = 0; /* zone printing time */
178 int32 lps_stime = 0; /* paper slew time */
179 int32 lps_rtime = 0; /* power-on ready time */
180
181 typedef int32 TIMESET[4]; /* set of controller times */
182
183 int32 *const lps_timers[] = { &lps_ctime, &lps_ptime, &lps_stime, &lps_rtime };
184
185 const TIMESET lps_times[2] = {
186 { 2, 55300, 17380, 158000 }, /* REALTIME */
187 { 2, 1000, 1000, 1000 } /* FASTTIME */
188 };
189
190 DEVICE lps_dev;
191 int32 lpsio (int32 inst, int32 IR, int32 dat);
192 t_stat lps_svc (UNIT *uptr);
193 t_stat lps_reset (DEVICE *dptr);
194 t_stat lps_restart (UNIT *uptr, int32 value, char *cptr, void *desc);
195 t_stat lps_poweroff (UNIT *uptr, int32 value, char *cptr, void *desc);
196 t_stat lps_poweron (UNIT *uptr, int32 value, char *cptr, void *desc);
197 t_stat lps_attach (UNIT *uptr, char *cptr);
198 t_stat lps_set_timing (UNIT *uptr, int32 val, char *cptr, void *desc);
199 t_stat lps_show_timing (FILE *st, UNIT *uptr, int32 val, void *desc);
200
201 /* LPS data structures
202
203 lps_dev LPS device descriptor
204 lps_unit LPS unit descriptor
205 lps_reg LPS register list
206 */
207
208 DIB lps_dib = { LPS, 0, 0, 0, 0, 0, &lpsio };
209
210 UNIT lps_unit = {
211 UDATA (&lps_svc, UNIT_SEQ+UNIT_ATTABLE+UNIT_DISABLE+UNIT_TEXT, 0)
212 };
213
214 REG lps_reg[] = {
215 { ORDATA (BUF, lps_unit.buf, 16) },
216 { ORDATA (STA, lps_sta, 16) },
217 { ORDATA (POWER, lps_power, 2), REG_RO },
218 { FLDATA (CMD, lps_dib.cmd, 0) },
219 { FLDATA (CTL, lps_dib.ctl, 0) },
220 { FLDATA (FLG, lps_dib.flg, 0) },
221 { FLDATA (FBF, lps_dib.fbf, 0) },
222 { FLDATA (SRQ, lps_dib.srq, 0) },
223 { DRDATA (CCNT, lps_ccnt, 7), PV_LEFT },
224 { DRDATA (LCNT, lps_lcnt, 7), PV_LEFT },
225 { DRDATA (POS, lps_unit.pos, T_ADDR_W), PV_LEFT },
226 { DRDATA (CTIME, lps_ctime, 24), PV_LEFT },
227 { DRDATA (PTIME, lps_ptime, 24), PV_LEFT },
228 { DRDATA (STIME, lps_stime, 24), PV_LEFT },
229 { DRDATA (RTIME, lps_rtime, 24), PV_LEFT },
230 { FLDATA (TIMING, lps_timing, 0), REG_HRO },
231 { FLDATA (STOP_IOE, lps_stopioe, 0) },
232 { ORDATA (DEVNO, lps_dib.devno, 6), REG_HRO },
233 { NULL }
234 };
235
236 MTAB lps_mod[] = {
237 { UNIT_DIAG, UNIT_DIAG, "diagnostic mode", "DIAG", NULL },
238 { UNIT_DIAG, 0, "printer mode", "PRINTER", NULL },
239 { UNIT_POWEROFF, UNIT_POWEROFF, "power off", "POWEROFF", lps_poweroff },
240 { UNIT_POWEROFF, 0, "power on", "POWERON", lps_poweron },
241 { UNIT_OFFLINE, UNIT_OFFLINE, "offline", "OFFLINE", NULL },
242 { UNIT_OFFLINE, 0, "online", "ONLINE", lps_restart },
243 { MTAB_XTD | MTAB_VDV, 0, NULL, "REALTIME",
244 &lps_set_timing, NULL, NULL },
245 { MTAB_XTD | MTAB_VDV, 1, NULL, "FASTTIME",
246 &lps_set_timing, NULL, NULL },
247 { MTAB_XTD | MTAB_VDV, 0, "TIMING", NULL,
248 NULL, &lps_show_timing, NULL },
249 { MTAB_XTD | MTAB_VDV, 0, "DEVNO", "DEVNO",
250 &hp_setdev, &hp_showdev, &lps_dev },
251 { 0 }
252 };
253
254 DEVICE lps_dev = {
255 "LPS", &lps_unit, lps_reg, lps_mod,
256 1, 10, 31, 1, 8, 8,
257 NULL, NULL, &lps_reset,
258 NULL, &lps_attach, NULL,
259 &lps_dib, DEV_DISABLE | DEV_DIS | DEV_DEBUG
260 };
261
262 /* IO instructions */
263
264 int32 lpsio (int32 inst, int32 IR, int32 dat)
265 {
266 int32 dev, sched;
267
268 dev = IR & I_DEVMASK; /* get device no */
269 switch (inst) { /* case on opcode */
270
271 case ioFLG: /* flag clear/set */
272 if ((IR & I_HC) == 0) { setFSR (dev); } /* STF */
273 break;
274
275 case ioSFC: /* skip flag clear */
276 if (FLG (dev) == 0) PC = (PC + 1) & VAMASK;
277 break;
278
279 case ioSFS: /* skip flag set */
280 if (FLG (dev) != 0) PC = (PC + 1) & VAMASK;
281 break;
282
283 case ioOTX: /* output */
284 if (DEBUG_PRS (lps_dev))
285 fprintf (sim_deb, ">>LPS OTx: Character %06o output\n", dat);
286 lps_unit.buf = dat;
287 break;
288
289 case ioLIX: /* load */
290 dat = 0; /* default sta = 0 */
291 case ioMIX: /* merge */
292 if ((lps_unit.flags & UNIT_DIAG) == 0) { /* real lpt? */
293 if (lps_power == LPS_ON) { /* power on? */
294 if (((lps_unit.flags & UNIT_ATT) == 0) || /* paper out? */
295 (lps_unit.flags & UNIT_OFFLINE) || /* offline? */
296 sim_is_active (&lps_unit)) lps_sta = LPS_BUSY;
297 else lps_sta = 0;
298 }
299 else lps_sta = LPS_PWROFF;
300 }
301 dat = dat | lps_sta; /* diag, rtn status */
302 if (DEBUG_PRS (lps_dev))
303 fprintf (sim_deb, ">>LPS LIx: Status %06o returned\n", dat);
304 break;
305
306 case ioCRS: /* control reset */
307 clrCTL (dev); /* clear control */
308 clrCMD (dev); /* clear command */
309 sim_cancel (&lps_unit); /* deactivate unit */
310 break;
311
312 case ioCTL: /* control clear/set */
313 if (IR & I_CTL) { /* CLC */
314 clrCTL (dev); /* clear control */
315 if (lps_unit.flags & UNIT_DIAG) { /* diagnostic mode? */
316 clrCMD (dev); /* clear command (jumper W9-A) */
317 if (IR & I_HC) /* clear flag too? */
318 sim_cancel (&lps_unit); /* prevent FLG/SRQ */
319 }
320 }
321 else { /* STC */
322 setCTL (dev); /* set ctl */
323 setCMD (dev); /* set cmd */
324 if (lps_unit.flags & UNIT_DIAG) { /* diagnostic? */
325 lps_sta = lps_unit.buf; /* loop back data */
326 sim_activate (&lps_unit, 2); /* schedule flag */
327 }
328 else { /* real lpt, sched */
329 if (DEBUG_PRS (lps_dev)) fprintf (sim_deb,
330 ">>LPS STC: Character %06o scheduled for line %d, column %d, ",
331 lps_unit.buf, lps_lcnt + 1, lps_ccnt + 1);
332 if ((lps_unit.buf != '\f') &&
333 (lps_unit.buf != '\n') &&
334 (lps_unit.buf != '\r')) { /* normal char */
335 lps_ccnt = lps_ccnt + 1; /* incr char counter */
336 if (lps_ccnt % LPS_ZONECNT == 0) /* end of zone? */
337 sched = lps_ptime; /* print zone */
338 else sched = lps_ctime; /* xfer char */
339 }
340 else { /* print cmd */
341 if (lps_ccnt % LPS_ZONECNT == 0) /* last zone printed? */
342 sched = lps_ctime; /* yes, so just char time */
343 else sched = lps_ptime; /* no, so print needed */
344 lps_ccnt = 0; /* reset char counter */
345 if (lps_unit.buf == '\n') { /* line advance */
346 lps_lcnt = (lps_lcnt + 1) % LPS_PAGELNT;
347 if (lps_lcnt > 0) sched = sched + lps_stime;
348 else sched = sched + /* allow for perf skip */
349 lps_stime * (LPS_FORMLNT - LPS_PAGELNT);
350 }
351 else if (lps_unit.buf == '\f') { /* form advance */
352 sched = sched + lps_stime * (LPS_FORMLNT - lps_lcnt);
353 lps_lcnt = 0;
354 }
355 }
356 sim_activate (&lps_unit, sched);
357 if (DEBUG_PRS (lps_dev))
358 fprintf (sim_deb, "time = %d\n", sched);
359 }
360 }
361 break;
362
363 default:
364 break;
365 }
366
367 if (IR & I_HC) { clrFSR (dev); } /* H/C option */
368 return dat;
369 }
370
371 /* Unit service */
372
373 t_stat lps_svc (UNIT *uptr)
374 {
375 int32 dev;
376 int32 c = uptr->buf & 0177;
377
378 if (lps_power == LPS_TURNING_ON) { /* printer warmed up? */
379 lps_power = LPS_ON; /* change state */
380 lps_restart (uptr, 0, NULL, NULL); /* restart I/O if hung */
381 if (DEBUG_PRS (lps_dev))
382 fputs (">>LPS svc: Power state is ON\n", sim_deb);
383 return SCPE_OK; /* done */
384 }
385 dev = lps_dib.devno; /* get dev no */
386 if (uptr->flags & UNIT_DIAG) { /* diagnostic? */
387 clrCMD (dev); /* clear cmd */
388 setFSR (dev); /* set flag, fbf */
389 return SCPE_OK; /* done */
390 }
391 if ((uptr->flags & UNIT_ATT) == 0) /* attached? */
392 return IORETURN (lps_stopioe, SCPE_UNATT);
393 else if (uptr->flags & UNIT_OFFLINE) /* offline? */
394 return IORETURN (lps_stopioe, STOP_OFFLINE);
395 else if (uptr->flags & UNIT_POWEROFF) /* powered off? */
396 return IORETURN (lps_stopioe, STOP_PWROFF);
397 clrCMD (dev); /* clear cmd */
398 setFSR (dev); /* set flag, fbf */
399 if (((c < ' ') || (c > '_')) && /* non-printing char? */
400 (c != '\f') && (c != '\n') && (c != '\r')) {
401 if (DEBUG_PRS (lps_dev))
402 fprintf (sim_deb, ">>LPS svc: Character %06o erased\n", c);
403 c = ' '; /* replace with blank */
404 }
405 if (lps_ccnt > LPS_PAGECNT) { /* 81st character? */
406 fputc ('\r', uptr->fileref); /* return to line start */
407 uptr->pos = uptr->pos + 1; /* update pos */
408 lps_ccnt = 1; /* reset char counter */
409 if (DEBUG_PRS (lps_dev))
410 fputs (">>LPS svc: Line wraparound to column 1\n", sim_deb);
411 }
412 fputc (c, uptr->fileref); /* "print" char */
413 uptr->pos = uptr->pos + 1; /* update pos */
414 if (DEBUG_PRS (lps_dev))
415 fprintf (sim_deb, ">>LPS svc: Character %06o printed\n", c);
416 if ((lps_lcnt == 0) && (c == '\n')) { /* LF did TOF? */
417 fputc ('\f', uptr->fileref); /* do perf skip */
418 uptr->pos = uptr->pos + 1; /* update pos */
419 if (DEBUG_PRS (lps_dev))
420 fputs (">>LPS svc: Perforation skip to TOF\n", sim_deb);
421 }
422 if (ferror (uptr->fileref)) {
423 perror ("LPS I/O error");
424 clearerr (uptr->fileref);
425 return SCPE_IOERR;
426 }
427 return SCPE_OK;
428 }
429
430 /* Reset routine - called from SCP, flags in DIB */
431
432 t_stat lps_reset (DEVICE *dptr)
433 {
434 lps_dib.cmd = lps_dib.ctl = 0; /* clear cmd, ctl */
435 lps_dib.flg = lps_dib.fbf = lps_dib.srq = 1; /* set flg, fbf, srq */
436 lps_sta = lps_unit.buf = 0;
437 lps_power = LPS_ON; /* power is on */
438 sim_cancel (&lps_unit); /* deactivate unit */
439 lps_set_timing (NULL, lps_timing, NULL, NULL); /* init timing set */
440 return SCPE_OK;
441 }
442
443 /* Restart I/O routine
444
445 If I/O is started via STC, and the printer is powered off, offline,
446 or out of paper, the CTL and CMD flip-flops will set, a service event
447 will be scheduled, and the service routine will be entered. If
448 STOP_IOE is not set, the I/O operation will "hang" at that point
449 until the printer is powered on, set online, or paper is supplied
450 (attached).
451
452 If a pending operation is "hung" when this routine is called, it is
453 restarted, which clears CTL and sets FBF and FLG, completing the
454 original I/O request.
455 */
456
457 t_stat lps_restart (UNIT *uptr, int32 value, char *cptr, void *desc)
458 {
459 if (lps_dib.cmd && lps_dib.ctl && !sim_is_active (uptr))
460 sim_activate (uptr, 0); /* reschedule I/O */
461 return SCPE_OK;
462 }
463
464 /* Printer power off */
465
466 t_stat lps_poweroff (UNIT *uptr, int32 value, char *cptr, void *desc)
467 {
468 lps_power = LPS_OFF; /* change state */
469 if (DEBUG_PRS (lps_dev)) fputs (">>LPS set: Power state is OFF\n", sim_deb);
470 return SCPE_OK;
471 }
472
473 /* Printer power on */
474
475 t_stat lps_poweron (UNIT *uptr, int32 value, char *cptr, void *desc)
476 {
477 if (lps_unit.flags & UNIT_DIAG) { /* diag mode? */
478 lps_power = LPS_ON; /* no delay */
479 if (DEBUG_PRS (lps_dev))
480 fputs (">>LPS set: Power state is ON\n", sim_deb);
481 }
482 else {
483 lps_power = LPS_TURNING_ON; /* change state */
484 lps_unit.flags |= UNIT_OFFLINE; /* set offline */
485 sim_activate (&lps_unit, lps_rtime); /* schedule ready */
486 if (DEBUG_PRS (lps_dev)) fprintf (sim_deb,
487 ">>LPS set: Power state is TURNING ON, scheduled time = %d\n",
488 lps_rtime );
489 }
490 return SCPE_OK;
491 }
492
493 /* Attach routine */
494
495 t_stat lps_attach (UNIT *uptr, char *cptr)
496 {
497 lps_ccnt = lps_lcnt = 0; /* top of form */
498 lps_restart (uptr, 0, NULL, NULL); /* restart I/O if hung */
499 return attach_unit (uptr, cptr);
500 }
501
502 /* Set printer timing
503
504 Realistic timing is factored, depending on CPU model, to account for the
505 timing method employed by the diagnostic. */
506
507 t_stat lps_set_timing (UNIT *uptr, int32 val, char *cptr, void *desc)
508 {
509 uint32 i, factor = 1;
510
511 lps_timing = (val != 0); /* determine choice */
512 if ((lps_timing == 0) && /* calc speed factor */
513 (UNIT_CPU_MODEL != UNIT_1000_E) &&
514 (UNIT_CPU_MODEL != UNIT_1000_F))
515 factor = 4;
516 for (i = 0; i < (sizeof (lps_timers) / sizeof (lps_timers[0])); i++)
517 *lps_timers[i] = lps_times[lps_timing][i] / factor; /* assign times */
518 return SCPE_OK;
519 }
520
521 /* Show printer timing */
522
523 t_stat lps_show_timing (FILE *st, UNIT *uptr, int32 val, void *desc)
524 {
525 if (lps_timing) fputs ("fast timing", st);
526 else fputs ("realistic timing", st);
527 return SCPE_OK;
528 }