First Commit of my working state
[simh.git] / PDP1 / pdp1_lp.c
1 /* pdp1_lp.c: PDP-1 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 bused 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 lpt Type 62 line printer for the PDP-1
27
28 19-Jan-07 RMS Added UNIT_TEXT flag
29 21-Dec-06 RMS Added 16-channel SBS support
30 07-Sep-03 RMS Changed ioc to ios
31 23-Jul-03 RMS Fixed bugs in instruction decoding, overprinting
32 Revised to detect I/O wait hang
33 25-Apr-03 RMS Revised for extended file support
34 30-May-02 RMS Widened POS to 32b
35 13-Apr-01 RMS Revised for register arrays
36 */
37
38 #include "pdp1_defs.h"
39
40 #define BPTR_MAX 40 /* pointer max */
41 #define LPT_BSIZE (BPTR_MAX * 3) /* line size */
42 #define BPTR_MASK 077 /* buf ptr mask */
43
44 int32 lpt_spc = 0; /* print (0) vs spc */
45 int32 lpt_ovrpr = 0; /* overprint */
46 int32 lpt_stopioe = 0; /* stop on error */
47 int32 lpt_bptr = 0; /* buffer ptr */
48 int32 lpt_sbs = 0; /* SBS level */
49 char lpt_buf[LPT_BSIZE + 1] = { 0 };
50 static const unsigned char lpt_trans[64] = {
51 ' ','1','2','3','4','5','6','7','8','9','\'','~','#','V','^','<',
52 '0','/','S','T','U','V','W','X','Y','Z','"',',','>','^','-','?',
53 '@','J','K','L','M','N','O','P','Q','R','$','=','-',')','-','(',
54 '_','A','B','C','D','E','F','G','H','I','*','.','+',']','|','['
55 };
56
57 extern int32 ios, cpls, iosta;
58 extern int32 stop_inst;
59
60 t_stat lpt_svc (UNIT *uptr);
61 t_stat lpt_reset (DEVICE *dptr);
62
63 /* LPT data structures
64
65 lpt_dev LPT device descriptor
66 lpt_unit LPT unit
67 lpt_reg LPT register list
68 */
69
70 UNIT lpt_unit = {
71 UDATA (&lpt_svc, UNIT_SEQ+UNIT_ATTABLE+UNIT_TEXT, 0), SERIAL_OUT_WAIT
72 };
73
74 REG lpt_reg[] = {
75 { ORDATA (BUF, lpt_unit.buf, 8) },
76 { FLDATA (PNT, iosta, IOS_V_PNT) },
77 { FLDATA (SPC, iosta, IOS_V_SPC) },
78 { FLDATA (RPLS, cpls, CPLS_V_LPT) },
79 { DRDATA (BPTR, lpt_bptr, 6) },
80 { ORDATA (LPT_STATE, lpt_spc, 6), REG_HRO },
81 { FLDATA (LPT_OVRPR, lpt_ovrpr, 0), REG_HRO },
82 { DRDATA (POS, lpt_unit.pos, T_ADDR_W), PV_LEFT },
83 { DRDATA (TIME, lpt_unit.wait, 24), PV_LEFT },
84 { FLDATA (STOP_IOE, lpt_stopioe, 0) },
85 { BRDATA (LBUF, lpt_buf, 8, 8, LPT_BSIZE) },
86 { DRDATA (SBSLVL, lpt_sbs, 4), REG_HRO },
87 { NULL }
88 };
89
90 MTAB lpt_mod[] = {
91 { MTAB_XTD|MTAB_VDV, 0, "SBSLVL", "SBSLVL",
92 &dev_set_sbs, &dev_show_sbs, (void *) &lpt_sbs },
93 { 0 }
94 };
95
96 DEVICE lpt_dev = {
97 "LPT", &lpt_unit, lpt_reg, lpt_mod,
98 1, 10, 31, 1, 8, 8,
99 NULL, NULL, &lpt_reset,
100 NULL, NULL, NULL,
101 NULL, DEV_DISABLE
102 };
103
104 /* Line printer IOT routine */
105
106 int32 lpt (int32 inst, int32 dev, int32 dat)
107 {
108 int32 i;
109
110 if (lpt_dev.flags & DEV_DIS) /* disabled? */
111 return (stop_inst << IOT_V_REASON) | dat; /* stop if requested */
112 if ((inst & 07000) == 01000) { /* fill buf */
113 if (lpt_bptr < BPTR_MAX) { /* limit test ptr */
114 i = lpt_bptr * 3; /* cvt to chr ptr */
115 lpt_buf[i] = lpt_trans[(dat >> 12) & 077];
116 lpt_buf[i + 1] = lpt_trans[(dat >> 6) & 077];
117 lpt_buf[i + 2] = lpt_trans[dat & 077];
118 }
119 lpt_bptr = (lpt_bptr + 1) & BPTR_MASK;
120 return dat;
121 }
122 if ((inst & 07000) == 02000) { /* space */
123 iosta = iosta & ~IOS_SPC; /* space, clear flag */
124 lpt_spc = (inst >> 6) & 077; /* state = space n */
125 }
126 else if ((inst & 07000) == 00000) { /* print */
127 iosta = iosta & ~IOS_PNT; /* clear flag */
128 lpt_spc = 0; /* state = print */
129 }
130 else return (stop_inst << IOT_V_REASON) | dat; /* not implemented */
131 if (GEN_CPLS (inst)) { /* comp pulse? */
132 ios = 0; /* clear flop */
133 cpls = cpls | CPLS_LPT; /* request completion */
134 }
135 else cpls = cpls & ~CPLS_LPT;
136 sim_activate (&lpt_unit, lpt_unit.wait); /* activate */
137 return dat;
138 }
139
140 /* Unit service, printer is in one of three states
141
142 lpt_spc = 000 write buffer to file, set overprint
143 lpt_iot = 02x space command x, clear overprint
144 */
145
146 t_stat lpt_svc (UNIT *uptr)
147 {
148 int32 i;
149 static const char *lpt_cc[] = {
150 "\n",
151 "\n\n",
152 "\n\n\n",
153 "\n\n\n\n\n\n",
154 "\n\n\n\n\n\n\n\n\n\n\n",
155 "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n",
156 "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n",
157 "\f"
158 };
159
160 if (cpls & CPLS_LPT) { /* completion pulse? */
161 ios = 1; /* restart */
162 cpls = cpls & ~CPLS_LPT; /* clr pulse pending */
163 }
164 dev_req_int (lpt_sbs); /* req interrupt */
165 if (lpt_spc) { /* space? */
166 iosta = iosta | IOS_SPC; /* set flag */
167 if ((uptr->flags & UNIT_ATT) == 0) /* attached? */
168 return IORETURN (lpt_stopioe, SCPE_UNATT);
169 fputs (lpt_cc[lpt_spc & 07], uptr->fileref); /* print cctl */
170 uptr->pos = ftell (uptr->fileref); /* update position */
171 if (ferror (uptr->fileref)) { /* error? */
172 perror ("LPT I/O error");
173 clearerr (uptr->fileref);
174 return SCPE_IOERR;
175 }
176 lpt_ovrpr = 0; /* dont overprint */
177 }
178 else {
179 iosta = iosta | IOS_PNT; /* print */
180 if ((uptr->flags & UNIT_ATT) == 0) /* attached? */
181 return IORETURN (lpt_stopioe, SCPE_UNATT);
182 if (lpt_ovrpr) fputc ('\r', uptr->fileref); /* overprint? */
183 fputs (lpt_buf, uptr->fileref); /* print buffer */
184 uptr->pos = ftell (uptr->fileref); /* update position */
185 if (ferror (uptr->fileref)) { /* test error */
186 perror ("LPT I/O error");
187 clearerr (uptr->fileref);
188 return SCPE_IOERR;
189 }
190 lpt_bptr = 0;
191 for (i = 0; i <= LPT_BSIZE; i++) lpt_buf[i] = 0; /* clear buffer */
192 lpt_ovrpr = 1; /* set overprint */
193 }
194 return SCPE_OK;
195 }
196
197 /* Reset routine */
198
199 t_stat lpt_reset (DEVICE *dptr)
200 {
201 int32 i;
202
203 lpt_bptr = 0; /* clear buffer ptr */
204 for (i = 0; i <= LPT_BSIZE; i++) lpt_buf[i] = 0; /* clear buffer */
205 lpt_spc = 0; /* clear state */
206 lpt_ovrpr = 0; /* clear overprint */
207 cpls = cpls & ~CPLS_LPT;
208 iosta = iosta & ~(IOS_PNT | IOS_SPC); /* clear flags */
209 sim_cancel (&lpt_unit); /* deactivate unit */
210 return SCPE_OK;
211 }