| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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; |
| 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; |
| 142 |
|
| 143 |
|
| 144 |
|
| 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; |
| 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 |
|
| 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 |
|
| 179 |
virtual void updateMSR()=0; |
| 180 |
|
| 181 |
|
| 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 |
|
| 196 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 222 |
void receiveByte(Bit8u data); |
| 223 |
void receiveByteEx(Bit8u data, Bit8u error); |
| 224 |
|
| 225 |
|
| 226 |
void receiveError(Bit8u errorword); |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
bool CanReceiveByte(); |
| 231 |
|
| 232 |
|
| 233 |
void ByteTransmitting(); |
| 234 |
|
| 235 |
|
| 236 |
void ByteTransmitted(); |
| 237 |
|
| 238 |
|
| 239 |
virtual void transmitByte(Bit8u val, bool first)=0; |
| 240 |
|
| 241 |
|
| 242 |
virtual void setBreak(bool value)=0; |
| 243 |
|
| 244 |
|
| 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 |
|
| 258 |
|
| 259 |
void ComputeInterrupts(); |
| 260 |
|
| 261 |
|
| 262 |
void rise(Bit8u priority); |
| 263 |
|
| 264 |
|
| 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; |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
Bit16u baud_divider; |
| 280 |
#define RHR_OFFSET 0 // r Receive Holding Register, also LSB of Divisor Latch (r/w) |
| 281 |
|
| 282 |
#define THR_OFFSET 0 // w Transmit Holding Register |
| 283 |
|
| 284 |
Bit8u IER; |
| 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; |
| 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; |
| 305 |
private: |
| 306 |
#define LCR_OFFSET 3 |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 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 |
|
| 336 |
|
| 337 |
#define MCR_OFFSET 4 |
| 338 |
bool dtr; |
| 339 |
bool rts; |
| 340 |
bool op1; |
| 341 |
bool op2; |
| 342 |
bool loopback; |
| 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; |
| 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 |
|
| 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 |
|
| 376 |
|
| 377 |
#define MSR_OFFSET 6 |
| 378 |
bool d_cts; |
| 379 |
bool d_dsr; |
| 380 |
bool d_ri; |
| 381 |
bool d_cd; |
| 382 |
bool cts; |
| 383 |
bool dsr; |
| 384 |
bool ri; |
| 385 |
bool 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; |
| 400 |
#define SPR_OFFSET 7 |
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
Bit8u loopback_data; |
| 405 |
void transmitLoopbackByte(Bit8u val, bool value); |
| 406 |
|
| 407 |
|
| 408 |
public: |
| 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 |
|
| 433 |
|
| 434 |
class device_COM : public DOS_Device { |
| 435 |
public: |
| 436 |
|
| 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 |