Debian Codegraph @

include/serialport.h

raw symbols SCIP
1 /*
2 * Copyright (C) 2002-2010 The DOSBox Team
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19 /* $Id: serialport.h,v 1.18 2009-09-25 23:40:48 h-a-l-9000 Exp $ */
20
21 #ifndef DOSBOX_SERIALPORT_H
22 #define DOSBOX_SERIALPORT_H
23
24 #ifndef DOSBOX_DOSBOX_H
25 #include "dosbox.h"
26 #endif
27 #ifndef DOSBOX_INOUT_H
28 #include "inout.h"
29 #endif
30 #ifndef DOSBOX_TIMER_H
31 #include "timer.h"
32 #endif
33 #ifndef DOSBOX_DOS_INC_H
34 #include "dos_inc.h"
35 #endif
36 #ifndef DOSBOX_PROGRAMS_H
37 #include "programs.h"
38 #endif
39
40 // set this to 1 for serial debugging in release mode
41 #define SERIAL_DBG_FORCED 0
42
43 #if (C_DEBUG || SERIAL_DBG_FORCED)
44 #define SERIAL_DEBUG 1
45 #endif
46
47 #if SERIAL_DEBUG
48 #include "hardware.h"
49 #endif
50
51 // Serial port interface
52
53 class MyFifo {
54 public:
55 MyFifo(Bitu maxsize_) {
56 maxsize=size=maxsize_;
57 pos=used=0;
58 data=new Bit8u[size];
59 }
60 ~MyFifo() {
61 delete[] data;
62 }
63 INLINE Bitu getFree(void) {
64 return size-used;
65 }
66 bool isEmpty() {
67 return used==0;
68 }
69 bool isFull() {
70 return (size-used)==0;
71 }
72
73 INLINE Bitu getUsage(void) {
74 return used;
75 }
76 void setSize(Bitu newsize)
77 {
78 size=newsize;
79 pos=used=0;
80 }
81 void clear(void) {
82 pos=used=0;
83 data[0]=0;
84 }
85
86 bool addb(Bit8u _val) {
87 Bitu where=pos+used;
88 if (where>=size) where-=size;
89 if(used>=size) {
90 // overwrite last byte
91 if(where==0) where=size-1;
92 else where--;
93 data[where]=_val;
94 return false;
95 }
96 data[where]=_val;
97 used++;
98 return true;
99 }
100 Bit8u getb() {
101 if (!used) return data[pos];
102 Bitu where=pos;
103 used--;
104 if(used) pos++;
105 if (pos>=size) pos-=size;
106 return data[where];
107 }
108 Bit8u getTop() {
109 Bitu where=pos+used;
110 if (where>=size) where-=size;
111 if(used>=size) {
112 if(where==0) where=size-1;
113 else where--;
114 }
115 return data[where];
116 }
117
118 Bit8u probeByte() {
119 return data[pos];
120 }
121 private:
122 Bit8u * data;
123 Bitu maxsize,size,pos,used;
124 };
125
126 class CSerial {
127 public:
128
129 #if SERIAL_DEBUG
130 FILE * debugfp;
131 bool dbg_modemcontrol; // RTS,CTS,DTR,DSR,RI,CD
132 bool dbg_serialtraffic;
133 bool dbg_register;
134 bool dbg_interrupt;
135 bool dbg_aux;
136 void log_ser(bool active, char const* format,...);
137 #endif
138
139 static bool getBituSubstring(const char* name,Bitu* data, CommandLine* cmd);
140
141 bool InstallationSuccessful;// check after constructing. If
142 // something was wrong, delete it right away.
143
144 // Constructor takes com port number (0-3)
145 CSerial(Bitu id, CommandLine* cmd);
146
147 virtual ~CSerial();
148
149 IO_ReadHandleObject ReadHandler[8];
150 IO_WriteHandleObject WriteHandler[8];
151
152 float bytetime; // how long a byte takes to transmit/receive in milliseconds
153 void changeLineProperties();
154 Bitu idnumber;
155
156 void setEvent(Bit16u type, float duration);
157 void removeEvent(Bit16u type);
158 void handleEvent(Bit16u type);
159 virtual void handleUpperEvent(Bit16u type)=0;
160
161 // defines for event type
162 #define SERIAL_TX_LOOPBACK_EVENT 0
163 #define SERIAL_THR_LOOPBACK_EVENT 1
164 #define SERIAL_ERRMSG_EVENT 2
165
166 #define SERIAL_TX_EVENT 3
167 #define SERIAL_RX_EVENT 4
168 #define SERIAL_POLLING_EVENT 5
169 #define SERIAL_THR_EVENT 6
170 #define SERIAL_RX_TIMEOUT_EVENT 7
171
172 #define SERIAL_BASE_EVENT_COUNT 7
173
174 #define COMNUMBER idnumber+1
175
176 Bitu irq;
177
178 // CSerial requests an update of the input lines
179 virtual void updateMSR()=0;
180
181 // Control lines from prepherial to serial port
182 bool getDTR();
183 bool getRTS();
184
185 bool getRI();
186 bool getCD();
187 bool getDSR();
188 bool getCTS();
189
190 void setRI(bool value);
191 void setDSR(bool value);
192 void setCD(bool value);
193 void setCTS(bool value);
194
195 // From serial port to prepherial
196 // set output lines
197 virtual void setRTSDTR(bool rts, bool dtr)=0;
198 virtual void setRTS(bool val)=0;
199 virtual void setDTR(bool val)=0;
200
201 // Register access
202 void Write_THR(Bit8u data);
203 void Write_IER(Bit8u data);
204 void Write_FCR(Bit8u data);
205 void Write_LCR(Bit8u data);
206 void Write_MCR(Bit8u data);
207 // Really old hardware seems to have the delta part of this register writable
208 void Write_MSR(Bit8u data);
209 void Write_SPR(Bit8u data);
210 void Write_reserved(Bit8u data, Bit8u address);
211
212 Bitu Read_RHR();
213 Bitu Read_IER();
214 Bitu Read_ISR();
215 Bitu Read_LCR();
216 Bitu Read_MCR();
217 Bitu Read_LSR();
218 Bitu Read_MSR();
219 Bitu Read_SPR();
220
221 // If a byte comes from loopback or prepherial, put it in here.
222 void receiveByte(Bit8u data);
223 void receiveByteEx(Bit8u data, Bit8u error);
224
225 // If an error was received, put it here (in LSR register format)
226 void receiveError(Bit8u errorword);
227
228 // depratched
229 // connected device checks, if port can receive data:
230 bool CanReceiveByte();
231
232 // when THR was shifted to TX
233 void ByteTransmitting();
234
235 // When done sending, notify here
236 void ByteTransmitted();
237
238 // Transmit byte to prepherial
239 virtual void transmitByte(Bit8u val, bool first)=0;
240
241 // switch break state to the passed value
242 virtual void setBreak(bool value)=0;
243
244 // change baudrate, number of bits, parity, word length al at once
245 virtual void updatePortConfig(Bit16u divider, Bit8u lcr)=0;
246
247 void Init_Registers();
248
249 bool Putchar(Bit8u data, bool wait_dtr, bool wait_rts, Bitu timeout);
250 bool Getchar(Bit8u* data, Bit8u* lsr, bool wait_dsr, Bitu timeout);
251
252
253 private:
254
255 DOS_Device* mydosdevice;
256
257 // I used this spec: st16c450v420.pdf
258
259 void ComputeInterrupts();
260
261 // a sub-interrupt is triggered
262 void rise(Bit8u priority);
263
264 // clears the pending sub-interrupt
265 void clear(Bit8u priority);
266
267 #define ERROR_PRIORITY 4 // overrun, parity error, frame error, break
268 #define RX_PRIORITY 1 // a byte has been received
269 #define TX_PRIORITY 2 // tx buffer has become empty
270 #define MSR_PRIORITY 8 // CRS, DSR, RI, DCD change
271 #define TIMEOUT_PRIORITY 0x10
272 #define NONE_PRIORITY 0
273
274 Bit8u waiting_interrupts; // these are on, but maybe not enabled
275
276 // 16C550
277 // read/write name
278
279 Bit16u baud_divider;
280 #define RHR_OFFSET 0 // r Receive Holding Register, also LSB of Divisor Latch (r/w)
281 // Data: whole byte
282 #define THR_OFFSET 0 // w Transmit Holding Register
283 // Data: whole byte
284 Bit8u IER; // r/w Interrupt Enable Register, also MSB of Divisor Latch
285 #define IER_OFFSET 1
286
287 bool irq_active;
288
289 #define RHR_INT_Enable_MASK 0x1
290 #define THR_INT_Enable_MASK 0x2
291 #define Receive_Line_INT_Enable_MASK 0x4
292 #define Modem_Status_INT_Enable_MASK 0x8
293
294 Bit8u ISR; // r Interrupt Status Register
295 #define ISR_OFFSET 2
296
297 #define ISR_CLEAR_VAL 0x1
298 #define ISR_FIFOTIMEOUT_VAL 0xc
299 #define ISR_ERROR_VAL 0x6
300 #define ISR_RX_VAL 0x4
301 #define ISR_TX_VAL 0x2
302 #define ISR_MSR_VAL 0x0
303 public:
304 Bit8u LCR; // r/w Line Control Register
305 private:
306 #define LCR_OFFSET 3
307 // bit0: word length bit0
308 // bit1: word length bit1
309 // bit2: stop bits
310 // bit3: parity enable
311 // bit4: even parity
312 // bit5: set parity
313 // bit6: set break
314 // bit7: divisor latch enable
315
316
317 #define LCR_BREAK_MASK 0x40
318 #define LCR_DIVISOR_Enable_MASK 0x80
319 #define LCR_PORTCONFIG_MASK 0x3F
320
321 #define LCR_PARITY_NONE 0x0
322 #define LCR_PARITY_ODD 0x8
323 #define LCR_PARITY_EVEN 0x18
324 #define LCR_PARITY_MARK 0x28
325 #define LCR_PARITY_SPACE 0x38
326
327 #define LCR_DATABITS_5 0x0
328 #define LCR_DATABITS_6 0x1
329 #define LCR_DATABITS_7 0x2
330 #define LCR_DATABITS_8 0x3
331
332 #define LCR_STOPBITS_1 0x0
333 #define LCR_STOPBITS_MORE_THAN_1 0x4
334
335 // Modem Control Register
336 // r/w
337 #define MCR_OFFSET 4
338 bool dtr; // bit0: DTR
339 bool rts; // bit1: RTS
340 bool op1; // bit2: OP1
341 bool op2; // bit3: OP2
342 bool loopback; // bit4: loop back enable
343
344 #define MCR_DTR_MASK 0x1
345 #define MCR_RTS_MASK 0x2
346 #define MCR_OP1_MASK 0x4
347 #define MCR_OP2_MASK 0x8
348 #define MCR_LOOPBACK_Enable_MASK 0x10
349 public:
350 Bit8u LSR; // r Line Status Register
351 private:
352
353 #define LSR_OFFSET 5
354
355 #define LSR_RX_DATA_READY_MASK 0x1
356 #define LSR_OVERRUN_ERROR_MASK 0x2
357 #define LSR_PARITY_ERROR_MASK 0x4
358 #define LSR_FRAMING_ERROR_MASK 0x8
359 #define LSR_RX_BREAK_MASK 0x10
360 #define LSR_TX_HOLDING_EMPTY_MASK 0x20
361 #define LSR_TX_EMPTY_MASK 0x40
362
363 #define LSR_ERROR_MASK 0x1e
364
365 // error printing
366 bool errormsg_pending;
367 Bitu framingErrors;
368 Bitu parityErrors;
369 Bitu overrunErrors;
370 Bitu txOverrunErrors;
371 Bitu overrunIF0;
372 Bitu breakErrors;
373
374
375 // Modem Status Register
376 // r
377 #define MSR_OFFSET 6
378 bool d_cts; // bit0: deltaCTS
379 bool d_dsr; // bit1: deltaDSR
380 bool d_ri; // bit2: deltaRI
381 bool d_cd; // bit3: deltaCD
382 bool cts; // bit4: CTS
383 bool dsr; // bit5: DSR
384 bool ri; // bit6: RI
385 bool cd; // bit7: CD
386
387 #define MSR_delta_MASK 0xf
388 #define MSR_LINE_MASK 0xf0
389
390 #define MSR_dCTS_MASK 0x1
391 #define MSR_dDSR_MASK 0x2
392 #define MSR_dRI_MASK 0x4
393 #define MSR_dCD_MASK 0x8
394 #define MSR_CTS_MASK 0x10
395 #define MSR_DSR_MASK 0x20
396 #define MSR_RI_MASK 0x40
397 #define MSR_CD_MASK 0x80
398
399 Bit8u SPR; // r/w Scratchpad Register
400 #define SPR_OFFSET 7
401
402
403 // For loopback purposes...
404 Bit8u loopback_data;
405 void transmitLoopbackByte(Bit8u val, bool value);
406
407 // 16C550 (FIFO)
408 public: // todo remove
409 MyFifo* rxfifo;
410 private:
411 MyFifo* txfifo;
412 MyFifo* errorfifo;
413 Bitu errors_in_fifo;
414 Bitu rx_interrupt_threshold;
415 Bitu fifosize;
416 Bit8u FCR;
417 bool sync_guardtime;
418 #define FIFO_STATUS_ACTIVE 0xc0 // FIFO is active AND works ;)
419 #define FIFO_ERROR 0x80
420 #define FCR_ACTIVATE 0x01
421 #define FCR_CLEAR_RX 0x02
422 #define FCR_CLEAR_TX 0x04
423 #define FCR_OFFSET 2
424 #define FIFO_FLOWCONTROL 0x20
425 };
426
427 extern CSerial* serialports[];
428 const Bit8u serial_defaultirq[] = { 4, 3, 4, 3 };
429 const Bit16u serial_baseaddr[] = {0x3f8,0x2f8,0x3e8,0x2e8};
430 const char* const serial_comname[]={"COM1","COM2","COM3","COM4"};
431
432 // the COM devices
433
434 class device_COM : public DOS_Device {
435 public:
436 // Creates a COM device that communicates with the num-th parallel port, i.e. is LPTnum
437 device_COM(class CSerial* sc);
438 ~device_COM();
439 bool Read(Bit8u * data,Bit16u * size);
440 bool Write(Bit8u * data,Bit16u * size);
441 bool Seek(Bit32u * pos,Bit32u type);
442 bool Close();
443 Bit16u GetInformation(void);
444 private:
445 CSerial* sclass;
446 };
447
448 #endif