First Commit of my working state
[simh.git] / Ibm1130 / ibm1130_prt.c
1 /* ibm1130_prt.c: IBM 1130 line printer emulation
2
3 Based on the SIMH simulator package written by Robert M Supnik
4
5 Brian Knittel
6 Revision History
7
8 2006.12.06 - Moved CGI stuff out of this routine into cgi1130 main() module.
9
10 2006.07.06 - Made 1403 printer 132 columns wide, was 120 previously
11
12 2006.01.03 - Fixed bug in prt_attach, found and fixed by Carl Claunch. Detach followed
13 by reattach of 1403-mode printer left device permanently not-ready.
14
15 2004.11.08 - HACK for demo mode: in physical (-p) mode, multiple consecutive formfeeds are suppressed.
16 This lets us do a formfeed at the end of a job to kick the last page out
17 without getting another blank page at the beginning of the next job.
18
19 2003.12.02 - Added -p option for physical line printer output (flushes
20 output buffer after each line). When using a physical printer on
21 Windows, be sure to set printer to "send output directly to printer"
22 to disable spooling, otherwise nothing appears until printer is
23 detatched.
24
25 2003.11.25 - Changed magic filename for standard output to "(stdout)".
26
27 2002.09.13 - Added 1403 support. New file, taken from part of ibm1130_stddev.c
28
29 Note: The 1403 is much faster, even in emulation, because it takes much
30 less CPU power to run it. DMS doesn't use the WAIT command when waiting for
31 printer operations to complete, so it ends up burning LOTS of cpu cycles.
32 The 1403 printer doesn't require as many. HOWEVER: DMS must be loaded for the 1403,
33 and Fortran IOCS control cards must specify it.
34
35 The 1132 is still the default printer.
36
37 As written, we can't have two printers.
38
39 * (C) Copyright 2002, Brian Knittel.
40 * You may freely use this program, but: it offered strictly on an AS-IS, AT YOUR OWN
41 * RISK basis, there is no warranty of fitness for any purpose, and the rest of the
42 * usual yada-yada. Please keep this notice and the copyright in any distributions
43 * or modifications.
44 *
45 * This is not a supported product, but I welcome bug reports and fixes.
46 * Mail to simh@ibm1130.org
47 */
48
49 #include "ibm1130_defs.h"
50 #include <stdlib.h> /* needed for atexit, for cgi mode */
51
52 /***************************************************************************************
53 * 1132 PRINTER
54 ***************************************************************************************/
55
56 #define PRT1132_DSW_READ_EMITTER_RESPONSE 0x8000
57 #define PRT1132_DSW_SKIP_RESPONSE 0x4000
58 #define PRT1132_DSW_SPACE_RESPONSE 0x2000
59 #define PRT1132_DSW_CARRIAGE_BUSY 0x1000
60 #define PRT1132_DSW_PRINT_SCAN_CHECK 0x0800
61 #define PRT1132_DSW_NOT_READY 0x0400
62 #define PRT1132_DSW_PRINTER_BUSY 0x0200
63
64 #define PRT1132_DSW_CHANNEL_MASK 0x00FF /* 1132 printer DSW bits */
65 #define PRT1132_DSW_CHANNEL_1 0x0080
66 #define PRT1132_DSW_CHANNEL_2 0x0040
67 #define PRT1132_DSW_CHANNEL_3 0x0020
68 #define PRT1132_DSW_CHANNEL_4 0x0010
69 #define PRT1132_DSW_CHANNEL_5 0x0008
70 #define PRT1132_DSW_CHANNEL_6 0x0004
71 #define PRT1132_DSW_CHANNEL_9 0x0002
72 #define PRT1132_DSW_CHANNEL_12 0x0001
73
74 #define PRT1403_DSW_PARITY_CHECK 0x8000 /* 1403 printer DSW bits */
75 #define PRT1403_DSW_TRANSFER_COMPLETE 0x4000
76 #define PRT1403_DSW_PRINT_COMPLETE 0x2000
77 #define PRT1403_DSW_CARRIAGE_COMPLETE 0x1000
78 #define PRT1403_DSW_RING_CHECK 0x0400
79 #define PRT1403_DSW_SYNC_CHECK 0x0200
80 #define PRT1403_DSW_CH9 0x0010
81 #define PRT1403_DSW_CH12 0x0008
82 #define PRT1403_DSW_CARRIAGE_BUSY 0x0004
83 #define PRT1403_DSW_PRINTER_BUSY 0x0002
84 #define PRT1403_DSW_NOT_READY 0x0001
85
86 #define IS_ONLINE(u) (((u)->flags & (UNIT_ATT|UNIT_DIS)) == UNIT_ATT)
87
88 static t_stat prt1132_svc(UNIT *uptr);
89 static t_stat prt1403_svc(UNIT *uptr);
90 static t_stat prt_svc (UNIT *uptr);
91 static t_stat prt_reset (DEVICE *dptr);
92 static t_stat prt_attach (UNIT *uptr, char *cptr);
93 static t_stat prt_detach (UNIT *uptr);
94
95 static int16 PRT_DSW = 0; /* device status word */
96 static int32 prt_swait = 500; /* line skip wait */
97 static int32 prt_cwait = 1250; /* character rotation wait */
98 static int32 prt_fwait = 100; /* fast wait, for 1403 operations */
99 static int32 prt_twait = 50; /* transfer wait, for 1403 operations */
100 #define SKIPTARGET (uptr->u4) /* target for skip operation */
101
102 static t_bool formfed = FALSE; /* last line printed was a formfeed */
103
104 #define UNIT_V_FORMCHECK (UNIT_V_UF + 0) /* out of paper error */
105 #define UNIT_V_DATACHECK (UNIT_V_UF + 1) /* printer overrun error */
106 #define UNIT_V_SKIPPING (UNIT_V_UF + 2) /* printer skipping */
107 #define UNIT_V_SPACING (UNIT_V_UF + 3) /* printer is spacing */
108 #define UNIT_V_PRINTING (UNIT_V_UF + 4) /* printer printing */
109 #define UNIT_V_TRANSFERRING (UNIT_V_UF + 5) /* unit is transferring print buffer (1403 only) */
110 #define UNIT_V_1403 (UNIT_V_UF + 6) /* printer model is 1403 rather than 1132 */
111 #define UNIT_V_PARITYCHECK (UNIT_V_UF + 7) /* error flags for 1403 */
112 #define UNIT_V_RINGCHECK (UNIT_V_UF + 8)
113 #define UNIT_V_SYNCCHECK (UNIT_V_UF + 9)
114 #define UNIT_V_PHYSICAL_PTR (UNIT_V_UF + 10) /* this appears in ibm1130_gui as well */
115
116 #define UNIT_FORMCHECK (1u << UNIT_V_FORMCHECK)
117 #define UNIT_DATACHECK (1u << UNIT_V_DATACHECK)
118 #define UNIT_SKIPPING (1u << UNIT_V_SKIPPING)
119 #define UNIT_SPACING (1u << UNIT_V_SPACING)
120 #define UNIT_PRINTING (1u << UNIT_V_PRINTING)
121 #define UNIT_TRANSFERRING (1u << UNIT_V_TRANSFERRING)
122 #define UNIT_1403 (1u << UNIT_V_1403)
123 #define UNIT_PARITYCHECK (1u << UNIT_V_PARITYCHECK)
124 #define UNIT_RINGCHECK (1u << UNIT_V_RINGCHECK)
125 #define UNIT_SYNCCHECK (1u << UNIT_V_SYNCCHECK)
126 #define UNIT_PHYSICAL_PTR (1u << UNIT_V_PHYSICAL_PTR)
127
128 UNIT prt_unit[] = {
129 { UDATA (&prt_svc, UNIT_ATTABLE, 0) },
130 };
131
132 #define IS_1403(uptr) (uptr->flags & UNIT_1403) /* model test */
133 #define IS_1132(uptr) ((uptr->flags & UNIT_1403) == 0) /* model test */
134 #define IS_PHYSICAL(uptr) (uptr->flags & UNIT_PHYSICAL_PTR)
135
136 /* Parameter in the unit descriptor (1132 printer) */
137
138 #define CMD_NONE 0
139 #define CMD_SPACE 1
140 #define CMD_SKIP 2
141 #define CMD_PRINT 3
142
143 REG prt_reg[] = {
144 { HRDATA (PRTDSW, PRT_DSW, 16) }, /* device status word */
145 { DRDATA (STIME, prt_swait, 24), PV_LEFT }, /* line skip wait */
146 { DRDATA (CTIME, prt_cwait, 24), PV_LEFT }, /* character rotation wait */
147 { DRDATA (FTIME, prt_fwait, 24), PV_LEFT }, /* 1403 fast wait */
148 { DRDATA (TTIME, prt_twait, 24), PV_LEFT }, /* 1403 transfer wait */
149 { NULL } };
150
151 MTAB prt_mod[] = {
152 { UNIT_1403, 0, "1132", "1132", NULL }, /* model option */
153 { UNIT_1403, UNIT_1403, "1403", "1403", NULL },
154 { 0 } };
155
156 DEVICE prt_dev = {
157 "PRT", prt_unit, prt_reg, prt_mod,
158 1, 16, 16, 1, 16, 16,
159 NULL, NULL, &prt_reset,
160 NULL, prt_attach, prt_detach};
161
162 #define MAX_COLUMNS 120
163 #define MAX_OVPRINT 20
164 #define PRT1132_COLUMNS 120
165 #define PRT1403_COLUMNS 120 /* the 1130's version of the 1403 printed in 120 columns only (see Functional Characteristics) */
166
167 static char prtbuf[MAX_COLUMNS*MAX_OVPRINT];
168 static int nprint[MAX_COLUMNS], ncol[MAX_OVPRINT], maxnp;
169 static int prt_nchar, prt_row; /* current printwheel position, current page row */
170 static int prt_nnl; /* number of queued newlines */
171
172 #define CC_CHANNEL_1 0x0800 /* carriage control tape punch values */
173 #define CC_CHANNEL_2 0x0400
174 #define CC_CHANNEL_3 0x0200
175 #define CC_CHANNEL_4 0x0100
176 #define CC_CHANNEL_5 0x0080
177 #define CC_CHANNEL_6 0x0040 /* 7, 8, 10 and 11 are not used on 1132 printer */
178 #define CC_CHANNEL_7 0x0020
179 #define CC_CHANNEL_8 0x0010
180 #define CC_CHANNEL_9 0x0008
181 #define CC_CHANNEL_10 0x0004
182 #define CC_CHANNEL_11 0x0002
183 #define CC_CHANNEL_12 0x0001
184
185 #define CC_1403_BITS 0x0FFF /* all bits for 1403, most for 1132 */
186 #define CC_1132_BITS (CC_1403_BITS & ~(CC_CHANNEL_7|CC_CHANNEL_8|CC_CHANNEL_10|CC_CHANNEL_11))
187
188 #define PRT_PAGELENGTH 66
189
190 static int cctape[PRT_PAGELENGTH]; /* standard carriage control tape */
191
192 static struct tag_ccpunches { /* list of rows and punches on tape */
193 int row, channels;
194 }
195 ccpunches[] = {
196 2, CC_CHANNEL_1, /* channel 1 = top of form */
197 62, CC_CHANNEL_12 /* channel 12 = bottom of form */
198 },
199 cccgi[] = {
200 2, CC_CHANNEL_1 /* channel 1 = top of form; no bottom of form */
201 };
202
203 #include "ibm1130_prtwheel.h"
204
205 extern int32 sim_switches;
206
207 /* cc_format_1132 and cc_format_1403 - turn cctape bits into proper format for DSW or status read */
208
209 static int cc_format_1132 (int bits)
210 {
211 return ((bits & (CC_CHANNEL_1|CC_CHANNEL_2|CC_CHANNEL_3|CC_CHANNEL_4|CC_CHANNEL_5|CC_CHANNEL_6)) >> 4) |
212 ((bits & CC_CHANNEL_9) >> 3) |
213 (bits & CC_CHANNEL_12);
214 }
215
216 #define cc_format_1403(bits) (bits)
217
218 /* reset_prt_line - clear the print line following paper advancement */
219
220 static void reset_prt_line (void)
221 {
222 memset(nprint, 0, sizeof(nprint));
223 memset(ncol, 0, sizeof(ncol));
224 maxnp = 0;
225 }
226
227 /* save_1132_prt_line - fire hammers for character 'ch' */
228
229 static t_bool save_1132_prt_line (int ch)
230 {
231 int i, r, addr = 32;
232 int32 mask = 0, wd = 0;
233
234 for (i = 0; i < PRT1132_COLUMNS; i++) {
235 if (mask == 0) { /* fetch next word from memory */
236 mask = 0x8000;
237 wd = M[addr++];
238 }
239
240 if (wd & mask) { /* hammer is to fire in this column */
241 if ((r = nprint[i]) < MAX_OVPRINT) {
242 if (ncol[r] <= i) { /* we haven't moved this far yet */
243 if (ncol[r] == 0) /* first char in this row? */
244 memset(prtbuf+r*MAX_COLUMNS, ' ', PRT1132_COLUMNS); /* blank out the new row */
245 ncol[r] = i+1; /* remember new row length */
246 }
247 prtbuf[r*MAX_COLUMNS + i] = (char) ch; /* save the character */
248
249 nprint[i]++; /* remember max overprintings for this column */
250 maxnp = MAX(maxnp, nprint[i]);
251 }
252 }
253
254 mask >>= 1; /* prepare to examine next bit */
255 }
256
257 return wd & 1; /* return TRUE if the last word has lsb set, which means all bits had been set */
258 }
259
260 /* write_line - write collected line to output file. No need to trim spaces as the hammers
261 * are never fired for them, so ncol[r] is the last printed position on each line.
262 */
263
264 static void newpage (FILE *fd, t_bool physical_printer)
265 {
266 if (cgi)
267 fputs("<HR>\n", fd);
268 else if (! formfed) {
269 putc('\f', fd);
270 if (physical_printer) {
271 fflush(fd); /* send the ff out to the printer immediately */
272 formfed = TRUE; /* hack: inhibit consecutive ff's */
273 }
274 }
275 }
276
277 static void flush_prt_line (FILE *fd, int spacemode, t_bool physical_printer)
278 {
279 int r;
280
281 if (! (spacemode || maxnp)) /* nothing to do */
282 return;
283
284 prt_row = (prt_row+1) % PRT_PAGELENGTH; /* NEXT line */
285
286 if (spacemode && ! maxnp) { /* spacing only */
287 if (prt_row == 0 && prt_nnl) {
288 #ifdef _WIN32
289 if (! cgi)
290 putc('\r', fd); /* DOS/Windows: end with cr/lf */
291 #endif
292 putc('\n', fd); /* otherwise end with lf */
293 if (spacemode & UNIT_SKIPPING) /* add formfeed if we crossed page boundary while skipping */
294 newpage(fd, physical_printer);
295
296 prt_nnl = 0;
297 }
298 else {
299 prt_nnl++;
300 formfed = FALSE;
301 }
302
303 prt_unit->pos++; /* note something written */
304 return;
305 }
306
307 if (prt_nnl) { /* there are queued newlines */
308 while (prt_nnl > 0) { /* spit out queued newlines */
309 #ifdef _WIN32
310 if (! cgi)
311 putc('\r', fd); /* DOS/Windows: end with cr/lf */
312 #endif
313 putc('\n', fd); /* otherwise end with lf */
314 prt_nnl--;
315 }
316 }
317
318 for (r = 0; r < maxnp; r++) {
319 if (r > 0)
320 putc('\r', fd); /* carriage return between overprinted lines */
321
322 fxwrite(&prtbuf[r*MAX_COLUMNS], 1, ncol[r], fd);
323 }
324
325 reset_prt_line();
326
327 prt_unit->pos++; /* note something written */
328 prt_nnl++; /* queue a newline */
329
330 if (physical_printer) /* if physical printer, send buffered output to device */
331 fflush(fd);
332
333 formfed = FALSE; /* note that something is now on the page */
334 }
335
336 /* 1132 printer commands */
337
338 #define PRT_CMD_START_PRINTER 0x0080
339 #define PRT_CMD_STOP_PRINTER 0x0040
340 #define PRT_CMD_START_CARRIAGE 0x0004
341 #define PRT_CMD_STOP_CARRIAGE 0x0002
342 #define PRT_CMD_SPACE 0x0001
343
344 #define PRT_CMD_MASK 0x00C7
345
346 extern char * saywhere (int addr);
347
348 static void mytrace (int start, char *what)
349 {
350 char *where;
351
352 if ((where = saywhere(prev_IAR)) == NULL) where = "?";
353 trace_io("%s %s at %04x: %s\n", start ? "start" : "stop", what, prev_IAR, where);
354 }
355
356 /* xio_1132_printer - XIO command interpreter for the 1132 printer */
357
358 void xio_1132_printer (int32 iocc_addr, int32 func, int32 modify)
359 {
360 char msg[80];
361 UNIT *uptr = &prt_unit[0];
362
363 switch (func) {
364 case XIO_READ:
365 M[iocc_addr & mem_mask] = codewheel1132[prt_nchar].ebcdic << 8;
366
367 if ((uptr->flags & UNIT_PRINTING) == 0) /* if we're not printing, advance this after every test */
368 prt_nchar = (prt_nchar + 1) % WHEELCHARS_1132;
369 break;
370
371 case XIO_SENSE_DEV:
372 ACC = PRT_DSW;
373 if (modify & 0x01) { /* reset interrupts */
374 CLRBIT(PRT_DSW, PRT1132_DSW_READ_EMITTER_RESPONSE | PRT1132_DSW_SKIP_RESPONSE | PRT1132_DSW_SPACE_RESPONSE);
375 CLRBIT(ILSW[1], ILSW_1_1132_PRINTER);
376 }
377 break;
378
379 case XIO_CONTROL:
380 if (modify & PRT_CMD_START_PRINTER) {
381 SETBIT(uptr->flags, UNIT_PRINTING);
382 /* mytrace(1, "printing"); */
383 }
384
385 if (modify & PRT_CMD_STOP_PRINTER) {
386 CLRBIT(uptr->flags, UNIT_PRINTING);
387 /* mytrace(0, "printing"); */
388 }
389
390 if (modify & PRT_CMD_START_CARRIAGE) {
391 SETBIT(uptr->flags, UNIT_SKIPPING);
392 /* mytrace(1, "skipping"); */
393 }
394
395 if (modify & PRT_CMD_STOP_CARRIAGE) {
396 CLRBIT(uptr->flags, UNIT_SKIPPING);
397 /* mytrace(0, "skipping"); */
398 }
399
400 if (modify & PRT_CMD_SPACE) {
401 SETBIT(uptr->flags, UNIT_SPACING);
402 /* mytrace(1, "space"); */
403 }
404
405 sim_cancel(uptr);
406 if (uptr->flags & (UNIT_SKIPPING|UNIT_SPACING|UNIT_PRINTING)) { /* busy bits = doing something */
407 SETBIT(PRT_DSW, PRT1132_DSW_PRINTER_BUSY);
408 sim_activate(uptr, prt_cwait);
409 }
410 else
411 CLRBIT(PRT_DSW, PRT1132_DSW_PRINTER_BUSY);
412
413 if (uptr->flags & (UNIT_SKIPPING|UNIT_SPACING))
414 SETBIT(PRT_DSW, PRT1132_DSW_CARRIAGE_BUSY);
415 else
416 CLRBIT(PRT_DSW, PRT1132_DSW_CARRIAGE_BUSY);
417
418 if ((uptr->flags & (UNIT_SKIPPING|UNIT_SPACING)) == (UNIT_SKIPPING|UNIT_SPACING)) {
419 sprintf(msg, "1132 printer skip and space at same time?");
420 xio_error(msg);
421 }
422 break;
423
424 default:
425 sprintf(msg, "Invalid 1132 printer XIO function %x", func);
426 xio_error(msg);
427 }
428 }
429
430 #define SET_ACTION(u,a) {(u)->flags &= ~(UNIT_SKIPPING|UNIT_SPACING|UNIT_PRINTING|UNIT_TRANSFERRING); (u)->flags |= a;}
431
432 static t_stat prt_svc (UNIT *uptr)
433 {
434 return IS_1403(uptr) ? prt1403_svc(uptr) : prt1132_svc(uptr);
435 }
436
437 /* prt1132_svc - emulated timeout for 1132 operation */
438
439 static t_stat prt1132_svc (UNIT *uptr)
440 {
441 if (PRT_DSW & PRT1132_DSW_NOT_READY) { /* cancel operation if printer went offline */
442 SETBIT(uptr->flags, UNIT_FORMCHECK);
443 SET_ACTION(uptr, 0);
444 forms_check(TRUE); /* and turn on forms check lamp */
445 return SCPE_OK;
446 }
447
448 if (uptr->flags & UNIT_SPACING) {
449 flush_prt_line(uptr->fileref, UNIT_SPACING, IS_PHYSICAL(uptr));
450
451 CLRBIT(PRT_DSW, PRT1132_DSW_CHANNEL_MASK|PRT1132_DSW_PRINTER_BUSY|PRT1132_DSW_CARRIAGE_BUSY);
452 SETBIT(PRT_DSW, cc_format_1132(cctape[prt_row]) | PRT1132_DSW_SPACE_RESPONSE);
453 SETBIT(ILSW[1], ILSW_1_1132_PRINTER);
454 CLRBIT(uptr->flags, UNIT_SPACING); /* done with this */
455 calc_ints();
456 }
457
458 if (uptr->flags & UNIT_SKIPPING) {
459 do {
460 flush_prt_line(uptr->fileref, UNIT_SKIPPING, IS_PHYSICAL(uptr));
461 CLRBIT(PRT_DSW, PRT1132_DSW_CHANNEL_MASK);
462 SETBIT(PRT_DSW, cc_format_1132(cctape[prt_row]));
463 } while ((cctape[prt_row] & CC_1132_BITS) == 0); /* slew directly to a cc tape punch */
464
465 SETBIT(PRT_DSW, cc_format_1132(cctape[prt_row]) | PRT1132_DSW_SKIP_RESPONSE);
466 SETBIT(ILSW[1], ILSW_1_1132_PRINTER);
467 calc_ints();
468 }
469
470 if (uptr->flags & UNIT_PRINTING) {
471 if (! save_1132_prt_line(codewheel1132[prt_nchar].ascii)) { /* save previous printed line */
472 SETBIT(uptr->flags, UNIT_DATACHECK); /* buffer wasn't set in time */
473 SET_ACTION(uptr, 0);
474 print_check(TRUE); /* and turn on forms check lamp */
475 return SCPE_OK;
476 }
477
478 prt_nchar = (prt_nchar + 1) % WHEELCHARS_1132; /* advance print drum */
479
480 SETBIT(PRT_DSW, PRT1132_DSW_READ_EMITTER_RESPONSE); /* issue interrupt to tell printer to set buffer */
481 SETBIT(ILSW[1], ILSW_1_1132_PRINTER); /* we'll save the printed stuff just before next emitter response (later than on real 1130) */
482 calc_ints();
483 }
484
485 if (uptr->flags & (UNIT_SPACING|UNIT_SKIPPING|UNIT_PRINTING)) { /* still doing something */
486 SETBIT(PRT_DSW, PRT1132_DSW_PRINTER_BUSY);
487 sim_activate(uptr, prt_cwait);
488 }
489 else
490 CLRBIT(PRT_DSW, PRT1132_DSW_PRINTER_BUSY);
491
492 return SCPE_OK;
493 }
494
495 void save_1403_prt_line (int32 addr)
496 {
497 int i, j, r, ch, even = TRUE;
498 unsigned char ebcdic;
499 int32 wd;
500
501 for (i = 0; i < PRT1403_COLUMNS; i++) {
502 if (even) { /* fetch next word from memory */
503 wd = M[addr++];
504 ebcdic = (unsigned char) ((wd >> 8) & 0x7F);
505 even = FALSE;
506 }
507 else {
508 ebcdic = (unsigned char) (wd & 0x7F); /* use low byte of previously fetched word */
509 even = TRUE;
510 }
511
512 ch = ' '; /* translate ebcdic to ascii. Don't bother checking for parity errors */
513 for (j = 0; j < WHEELCHARS_1403; j++) {
514 if (codewheel1403[j].ebcdic == ebcdic) {
515 ch = codewheel1403[j].ascii;
516 break;
517 }
518 }
519
520 if (ch > ' ') {
521 if ((r = nprint[i]) < MAX_OVPRINT) {
522 if (ncol[r] <= i) { /* we haven't moved this far yet */
523 if (ncol[r] == 0) /* first char in this row? */
524 memset(prtbuf+r*MAX_COLUMNS, ' ', PRT1403_COLUMNS); /* blank out the new row */
525 ncol[r] = i+1; /* remember new row length */
526 }
527 prtbuf[r*MAX_COLUMNS + i] = (char) ch; /* save the character */
528
529 nprint[i]++; /* remember max overprintings for this column */
530 maxnp = MAX(maxnp, nprint[i]);
531 }
532 }
533 }
534 }
535
536 void xio_1403_printer (int32 iocc_addr, int32 func, int32 modify)
537 {
538 UNIT *uptr = &prt_unit[0];
539
540 switch (func) {
541 case XIO_INITW: /* print a line */
542 save_1403_prt_line(iocc_addr); /* put formatted line into our print buffer */
543
544 SETBIT(uptr->flags, UNIT_TRANSFERRING); /* schedule transfer complete interrupt */
545 SETBIT(PRT_DSW, PRT1403_DSW_PRINTER_BUSY);
546 sim_activate(uptr, prt_twait);
547 break;
548
549 case XIO_CONTROL: /* initiate single space */
550 if (uptr->flags & UNIT_SKIPPING) {
551 xio_error("1403 printer skip and space at same time?");
552 }
553 else {
554 SETBIT(uptr->flags, UNIT_SPACING);
555 SETBIT(PRT_DSW, PRT1403_DSW_CARRIAGE_BUSY);
556 sim_activate(uptr, prt_fwait);
557 }
558 break;
559
560 case XIO_WRITE: /* initiate skip */
561 if (uptr->flags & UNIT_SPACING) {
562 xio_error("1403 printer skip and space at same time?");
563 }
564 else {
565 SETBIT(uptr->flags, UNIT_SKIPPING);
566 SKIPTARGET = ReadW(iocc_addr) & CC_1403_BITS; /* get CC bits that we're to match */
567 SETBIT(PRT_DSW, PRT1403_DSW_CARRIAGE_BUSY);
568 sim_activate(uptr, prt_fwait);
569 }
570 break;
571
572 case XIO_SENSE_DEV: /* get device status word */
573 ACC = PRT_DSW;
574 if (modify & 0x01) { /* reset interrupts */
575 CLRBIT(PRT_DSW, PRT1403_DSW_PARITY_CHECK | PRT1403_DSW_TRANSFER_COMPLETE |
576 PRT1403_DSW_PRINT_COMPLETE | PRT1403_DSW_CARRIAGE_COMPLETE |
577 PRT1403_DSW_RING_CHECK | PRT1403_DSW_SYNC_CHECK);
578 CLRBIT(ILSW[4], ILSW_4_1403_PRINTER);
579 }
580 break;
581 }
582 }
583
584 static t_stat prt1403_svc(UNIT *uptr)
585 {
586 if (PRT_DSW & PRT1403_DSW_NOT_READY) { /* cancel operation if printer went offline */
587 SET_ACTION(uptr, 0);
588 forms_check(TRUE); /* and turn on forms check lamp */
589 }
590 else if (uptr->flags & UNIT_TRANSFERRING) { /* end of transfer */
591 CLRBIT(uptr->flags, UNIT_TRANSFERRING);
592 SETBIT(uptr->flags, UNIT_PRINTING); /* schedule "print complete" */
593
594 SETBIT(PRT_DSW, PRT1403_DSW_TRANSFER_COMPLETE); /* issue transfer complete interrupt */
595 SETBIT(ILSW[4], ILSW_4_1403_PRINTER);
596 }
597 else if (uptr->flags & UNIT_PRINTING) {
598 CLRBIT(uptr->flags, UNIT_PRINTING);
599 CLRBIT(PRT_DSW, PRT1403_DSW_PRINTER_BUSY);
600
601 SETBIT(PRT_DSW, PRT1403_DSW_PRINT_COMPLETE);
602 SETBIT(ILSW[4], ILSW_4_1403_PRINTER); /* issue print complete interrupt */
603 }
604 else if (uptr->flags & UNIT_SKIPPING) {
605 do { /* find line with exact match of tape punches */
606 flush_prt_line(uptr->fileref, UNIT_SKIPPING, IS_PHYSICAL(uptr));
607 } while (cctape[prt_row] != SKIPTARGET); /* slew directly to requested cc tape punch */
608
609 CLRBIT(uptr->flags, UNIT_SKIPPING); /* done with this */
610 CLRBIT(PRT_DSW, PRT1403_DSW_CARRIAGE_BUSY);
611
612 SETBIT(PRT_DSW, PRT1403_DSW_CARRIAGE_COMPLETE);
613 SETBIT(ILSW[4], ILSW_4_1403_PRINTER);
614 }
615 else if (uptr->flags & UNIT_SPACING) {
616 flush_prt_line(uptr->fileref, UNIT_SPACING, IS_PHYSICAL(uptr));
617
618 CLRBIT(uptr->flags, UNIT_SPACING); /* done with this */
619 CLRBIT(PRT_DSW, PRT1403_DSW_CARRIAGE_BUSY);
620
621 SETBIT(PRT_DSW, PRT1403_DSW_CARRIAGE_COMPLETE);
622 SETBIT(ILSW[4], ILSW_4_1403_PRINTER);
623 }
624
625 if (uptr->flags & (UNIT_PRINTING|UNIT_SKIPPING|UNIT_SPACING|UNIT_TRANSFERRING))
626 sim_activate(uptr, prt_fwait);
627
628 CLRBIT(PRT_DSW, PRT1403_DSW_CH9|PRT1403_DSW_CH12); /* set the two CC bits in the DSW */
629 if (cctape[prt_row] & CC_CHANNEL_9)
630 SETBIT(PRT_DSW, PRT1403_DSW_CH9);
631 if (cctape[prt_row] & CC_CHANNEL_12)
632 SETBIT(PRT_DSW, PRT1403_DSW_CH12);
633
634 calc_ints();
635 return SCPE_OK;
636 }
637
638 /* delete_cmd - SCP command to delete a file */
639
640 static t_stat delete_cmd (int flag, char *cptr)
641 {
642 char gbuf[CBUFSIZE];
643 int status;
644
645 cptr = get_glyph (cptr, gbuf, 0); /* get next glyph */
646 if (*gbuf == 0) return SCPE_2FARG;
647 if (*cptr != 0) return SCPE_2MARG; /* now eol? */
648
649 status = remove(gbuf); /* delete the file */
650
651 if (status != 0 && errno != ENOENT) /* print message if failed and file exists */
652 perror(gbuf);
653
654 return SCPE_OK;
655 }
656
657 /* prt_reset - reset emulated printer */
658
659 static t_stat prt_reset (DEVICE *dptr)
660 {
661 UNIT *uptr = &prt_unit[0];
662 int i;
663
664 /* add a DELETE filename command so we can be sure to have clean listings */
665 register_cmd("DELETE", &delete_cmd, 0, "del{ete} filename remove file\n");
666
667 sim_cancel(uptr);
668
669 memset(cctape, 0, sizeof(cctape)); /* copy punch list into carriage control tape image */
670
671 if (cgi) {
672 for (i = 0; i < (sizeof(cccgi)/sizeof(cccgi[0])); i++)
673 cctape[cccgi[i].row-1] |= cccgi[i].channels;
674 }
675 else
676 for (i = 0; i < (sizeof(ccpunches)/sizeof(ccpunches[0])); i++)
677 cctape[ccpunches[i].row-1] |= ccpunches[i].channels;
678
679 prt_nchar = 0;
680 prt_row = 0;
681 prt_nnl = 0;
682
683 CLRBIT(uptr->flags, UNIT_FORMCHECK|UNIT_DATACHECK|UNIT_PRINTING|UNIT_SPACING|UNIT_SKIPPING|
684 UNIT_TRANSFERRING|UNIT_PARITYCHECK|UNIT_RINGCHECK|UNIT_SYNCCHECK);
685
686 if (IS_1132(uptr)) {
687 CLRBIT(ILSW[1], ILSW_1_1132_PRINTER);
688 PRT_DSW = cc_format_1132(cctape[prt_row]);
689 if (! IS_ONLINE(uptr))
690 SETBIT(PRT_DSW, PRT1132_DSW_NOT_READY);
691 }
692 else {
693 CLRBIT(ILSW[4], ILSW_4_1403_PRINTER);
694 PRT_DSW = 0;
695 if (cctape[prt_row] & CC_CHANNEL_9)
696 SETBIT(PRT_DSW, PRT1403_DSW_CH9);
697 if (cctape[prt_row] & CC_CHANNEL_12)
698 SETBIT(PRT_DSW, PRT1403_DSW_CH12);
699 if (! IS_ONLINE(uptr))
700 SETBIT(PRT_DSW, PRT1403_DSW_NOT_READY);
701 }
702
703 SET_ACTION(uptr, 0);
704 calc_ints();
705 reset_prt_line();
706
707 forms_check(FALSE);
708 return SCPE_OK;
709 }
710
711 static t_stat prt_attach (UNIT *uptr, char *cptr)
712 {
713 t_stat rval;
714 /* assume failure */
715 SETBIT(PRT_DSW, IS_1132(uptr) ? PRT1132_DSW_NOT_READY : PRT1403_DSW_NOT_READY);
716 formfed = FALSE;
717
718 if (uptr->flags & UNIT_ATT) {
719 if ((rval = prt_detach(uptr)) != SCPE_OK) {
720 return rval;
721 }
722 }
723
724 if (sim_switches & SWMASK('P')) /* set physical (unbuffered) printer flag */
725 SETBIT(uptr->flags, UNIT_PHYSICAL_PTR);
726 else
727 CLRBIT(uptr->flags, UNIT_PHYSICAL_PTR);
728
729 sim_cancel(uptr);
730
731 if (strcmp(cptr, "(stdout)") == 0) { /* connect printer to stdout */
732 if (uptr -> flags & UNIT_DIS) return SCPE_UDIS; /* disabled? */
733 uptr->filename = calloc(CBUFSIZE, sizeof(char));
734 strcpy(uptr->filename, "(stdout)");
735 uptr->fileref = stdout;
736 SETBIT(uptr->flags, UNIT_ATT);
737 uptr->pos = 0;
738 }
739 else {
740 if ((rval = attach_unit(uptr, quotefix(cptr))) != SCPE_OK)
741 return rval;
742 }
743
744 fseek(uptr->fileref, 0, SEEK_END); /* if we opened an existing file, append to it */
745 uptr->pos = ftell(uptr->fileref);
746
747 if (IS_1132(uptr)) {
748 CLRBIT(ILSW[1], ILSW_1_1132_PRINTER);
749 CLRBIT(uptr->flags, UNIT_FORMCHECK|UNIT_DATACHECK);
750 }
751 else {
752 CLRBIT(ILSW[4], ILSW_4_1403_PRINTER);
753 CLRBIT(uptr->flags, UNIT_PARITYCHECK|UNIT_RINGCHECK|UNIT_SYNCCHECK);
754 }
755
756 SET_ACTION(uptr, 0);
757 calc_ints();
758
759 prt_nchar = 0;
760 prt_nnl = 0;
761 prt_row = 0;
762 reset_prt_line();
763
764 if (IS_1132(uptr)) {
765 PRT_DSW = (PRT_DSW & ~PRT1132_DSW_CHANNEL_MASK) | cc_format_1132(cctape[prt_row]);
766
767 if (IS_ONLINE(uptr))
768 CLRBIT(PRT_DSW, PRT1132_DSW_NOT_READY);
769 }
770 else {
771 CLRBIT(PRT_DSW, PRT1403_DSW_CH9 | PRT1403_DSW_CH12);
772 if (cctape[prt_row] & CC_CHANNEL_9)
773 SETBIT(PRT_DSW, PRT1403_DSW_CH9);
774 if (cctape[prt_row] & CC_CHANNEL_12)
775 SETBIT(PRT_DSW, PRT1403_DSW_CH12);
776
777 if (IS_ONLINE(uptr))
778 CLRBIT(PRT_DSW, PRT1403_DSW_NOT_READY); /* fixed by Carl Claunch */
779 }
780
781 forms_check(FALSE);
782
783 return SCPE_OK;
784 }
785
786 static t_stat prt_detach (UNIT *uptr)
787 {
788 t_stat rval;
789
790 if (uptr->flags & UNIT_ATT)
791 flush_prt_line(uptr->fileref, TRUE, TRUE);
792
793 if (uptr->fileref == stdout) {
794 CLRBIT(uptr->flags, UNIT_ATT);
795 free(uptr->filename);
796 uptr->filename = NULL;
797 }
798 else if ((rval = detach_unit(uptr)) != SCPE_OK)
799 return rval;
800
801 sim_cancel(uptr);
802
803 if (IS_1132(uptr)) {
804 CLRBIT(ILSW[1], ILSW_1_1132_PRINTER);
805 CLRBIT(uptr->flags, UNIT_FORMCHECK|UNIT_DATACHECK);
806 SETBIT(PRT_DSW, PRT1132_DSW_NOT_READY);
807 }
808 else {
809 CLRBIT(ILSW[4], ILSW_4_1403_PRINTER);
810 SETBIT(PRT_DSW, PRT1403_DSW_NOT_READY);
811 }
812 SET_ACTION(uptr, 0);
813
814 calc_ints();
815
816 forms_check(FALSE);
817 return SCPE_OK;
818 }
819