First Commit of my working state
[simh.git] / ALTAIR / altair_sio.c
1 /* altair_sio: MITS Altair serial I/O card
2
3 Copyright (c) 1997-2005, Charles E. Owen
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 Charles E. Owen 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 Charles E. Owen.
25
26 These functions support a simulated MITS 2SIO interface card.
27 The card had two physical I/O ports which could be connected
28 to any serial I/O device that would connect to a current loop,
29 RS232, or TTY interface. Available baud rates were jumper
30 selectable for each port from 110 to 9600.
31
32 All I/O is via programmed I/O. Each each has a status port
33 and a data port. A write to the status port can select
34 some options for the device (0x03 will reset the port).
35 A read of the status port gets the port status:
36
37 +---+---+---+---+---+---+---+---+
38 | X X X X X X O I |
39 +---+---+---+---+---+---+---+---+
40
41 I - A 1 in this bit position means a character has been received
42 on the data port and is ready to be read.
43 O - A 1 in this bit means the port is ready to receive a character
44 on the data port and transmit it out over the serial line.
45
46 A read to the data port gets the buffered character, a write
47 to the data port writes the character to the device.
48 */
49
50 #include <stdio.h>
51
52 #include "altair_defs.h"
53
54 #define UNIT_V_ANSI (UNIT_V_UF + 0) /* ANSI mode */
55 #define UNIT_ANSI (1 << UNIT_V_ANSI)
56
57 t_stat sio_svc (UNIT *uptr);
58 t_stat sio_reset (DEVICE *dptr);
59 t_stat ptr_svc (UNIT *uptr);
60 t_stat ptr_reset (DEVICE *dptr);
61 t_stat ptp_svc (UNIT *uptr);
62 t_stat ptp_reset (DEVICE *dptr);
63
64 int32 ptr_stopioe = 0, ptp_stopioe = 0; /* stop on error */
65
66 /* 2SIO Standard I/O Data Structures */
67
68 UNIT sio_unit = { UDATA (&sio_svc, 0, 0), KBD_POLL_WAIT };
69
70 REG sio_reg[] = {
71 { ORDATA (DATA, sio_unit.buf, 8) },
72 { ORDATA (STAT, sio_unit.u3, 8) },
73 { NULL }
74 };
75
76 MTAB sio_mod[] = {
77 { UNIT_ANSI, 0, "TTY", "TTY", NULL },
78 { UNIT_ANSI, UNIT_ANSI, "ANSI", "ANSI", NULL },
79 { 0 }
80 };
81
82 DEVICE sio_dev = {
83 "2SIO", &sio_unit, sio_reg, sio_mod,
84 1, 10, 31, 1, 8, 8,
85 NULL, NULL, &sio_reset,
86 NULL, NULL, NULL
87 };
88
89 UNIT ptr_unit = { UDATA (&ptr_svc, UNIT_SEQ + UNIT_ATTABLE, 0), KBD_POLL_WAIT };
90
91 REG ptr_reg[] = {
92 { ORDATA (DATA, ptr_unit.buf, 8) },
93 { ORDATA (STAT, ptr_unit.u3, 8) },
94 { ORDATA (POS, ptr_unit.pos, T_ADDR_W) },
95 { NULL }
96 };
97
98 DEVICE ptr_dev = {
99 "PTR", &ptr_unit, ptr_reg, NULL,
100 1, 10, 31, 1, 8, 8,
101 NULL, NULL, &ptr_reset,
102 NULL, NULL, NULL
103 };
104
105 UNIT ptp_unit = { UDATA (&ptp_svc, UNIT_SEQ + UNIT_ATTABLE, 0), KBD_POLL_WAIT };
106
107 REG ptp_reg[] = {
108 { ORDATA (DATA, ptp_unit.buf, 8) },
109 { ORDATA (STAT, ptp_unit.u3, 8) },
110 { ORDATA (POS, ptp_unit.pos, T_ADDR_W) },
111 { NULL }
112 };
113
114 DEVICE ptp_dev = {
115 "PTP", &ptp_unit, ptp_reg, NULL,
116 1, 10, 31, 1, 8, 8,
117 NULL, NULL, &ptp_reset,
118 NULL, NULL, NULL
119 };
120
121 /* Service routines to handle simulator functions */
122
123 /* service routine - actually gets char & places in buffer */
124
125 int32 sio_svc (UNIT *uptr)
126 {
127 int32 temp;
128
129 sim_activate (&sio_unit, sio_unit.wait); /* continue poll */
130 if ((temp = sim_poll_kbd ()) < SCPE_KFLAG)
131 return temp; /* no char or error? */
132 sio_unit.buf = temp & 0377; /* Save char */
133 sio_unit.u3 |= 0x01; /* Set status */
134
135 /* Do any special character handling here */
136
137 sio_unit.pos++;
138 return SCPE_OK;
139 }
140
141
142 int32 ptr_svc (UNIT *uptr)
143 {
144 return SCPE_OK;
145 }
146
147 int32 ptp_svc (UNIT *uptr)
148 {
149 return SCPE_OK;
150 }
151
152
153 /* Reset routine */
154
155 int32 sio_reset (DEVICE *dptr)
156 {
157 sio_unit.buf = 0; /* Data */
158 sio_unit.u3 = 0x02; /* Status */
159 sim_activate (&sio_unit, sio_unit.wait); /* activate unit */
160 return SCPE_OK;
161 }
162
163
164 int32 ptr_reset (DEVICE *dptr)
165 {
166 ptr_unit.buf = 0;
167 ptr_unit.u3 = 0x02;
168 sim_cancel (&ptr_unit); /* deactivate unit */
169 return SCPE_OK;
170 }
171
172 int32 ptp_reset (DEVICE *dptr)
173 {
174 ptp_unit.buf = 0;
175 ptp_unit.u3 = 0x02;
176 sim_cancel (&ptp_unit); /* deactivate unit */
177 return SCPE_OK;
178 }
179
180
181 /* I/O instruction handlers, called from the CPU module when an
182 IN or OUT instruction is issued.
183
184 Each function is passed an 'io' flag, where 0 means a read from
185 the port, and 1 means a write to the port. On input, the actual
186 input is passed as the return value, on output, 'data' is written
187 to the device.
188 */
189
190 int32 sio0s(int32 io, int32 data)
191 {
192 if (io == 0) {
193 return (sio_unit.u3);
194 } else {
195 if (data == 0x03) { /* reset port! */
196 sio_unit.u3 = 0x02;
197 sio_unit.buf = 0;
198 sio_unit.pos = 0;
199 }
200 return (0);
201 }
202 }
203
204 int32 sio0d(int32 io, int32 data)
205 {
206 if (io == 0) {
207 sio_unit.u3 = sio_unit.u3 & 0xFE;
208 return (sio_unit.buf);
209 } else {
210 sim_putchar(data);
211 }
212 return 0;
213 }
214
215 /* Port 2 controls the PTR/PTP devices */
216
217 int32 sio1s(int32 io, int32 data)
218 {
219 if (io == 0) {
220 if ((ptr_unit.flags & UNIT_ATT) == 0) /* attached? */
221 return 0x02;
222 if (ptr_unit.u3 != 0) /* No more data? */
223 return 0x02;
224 return (0x03); /* ready to read/write */
225 } else {
226 if (data == 0x03) {
227 ptr_unit.u3 = 0;
228 ptr_unit.buf = 0;
229 ptr_unit.pos = 0;
230 ptp_unit.u3 = 0;
231 ptp_unit.buf = 0;
232 ptp_unit.pos = 0;
233 }
234 return (0);
235 }
236 }
237
238 int32 sio1d(int32 io, int32 data)
239 {
240 int32 temp;
241 UNIT *uptr;
242
243 if (io == 0) {
244 if ((ptr_unit.flags & UNIT_ATT) == 0) /* attached? */
245 return 0;
246 if (ptr_unit.u3 != 0)
247 return 0;
248 uptr = ptr_dev.units;
249 if ((temp = getc(uptr -> fileref)) == EOF) { /* end of file? */
250 ptr_unit.u3 = 0x01;
251 return 0;
252 }
253 ptr_unit.pos++;
254 return (temp & 0xFF);
255 } else {
256 uptr = ptp_dev.units;
257 putc(data, uptr -> fileref);
258 ptp_unit.pos++;
259 }
260 return 0;
261 }
262