| 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.cpp,v 1.14 2009-10-01 17:25:28 h-a-l-9000 Exp $ */ |
| 20 | |
| 21 | #include <string.h> |
| 22 | #include <ctype.h> |
| 23 | |
| 24 | #include "dosbox.h" |
| 25 | |
| 26 | #include "inout.h" |
| 27 | #include "pic.h" |
| 28 | #include "setup.h" |
| 29 | #include "bios.h" // SetComPorts(..) |
| 30 | #include "callback.h" // CALLBACK_Idle |
| 31 | |
| 32 | #include "serialport.h" |
| 33 | #include "directserial.h" |
| 34 | #include "serialdummy.h" |
| 35 | #include "softmodem.h" |
| 36 | #include "nullmodem.h" |
| 37 | |
| 38 | #include "cpu.h" |
| 39 | |
| 40 | #define LOG_SER(x) log_ser |
| 41 | |
| 42 | bool device_COM::Read(Bit8u * data,Bit16u * size) { |
| 43 | // DTR + RTS on |
| 44 | sclass->Write_MCR(0x03); |
| 45 | for (Bit16u i=0; i<*size; i++) |
| 46 | { |
| 47 | Bit8u status; |
| 48 | if(!(sclass->Getchar(&data[i],&status,true,1000))) { |
| 49 | *size=i; |
| 50 | return true; |
| 51 | } |
| 52 | } |
| 53 | return true; |
| 54 | } |
| 55 | |
| 56 | |
| 57 | bool device_COM::Write(Bit8u * data,Bit16u * size) { |
| 58 | // DTR + RTS on |
| 59 | sclass->Write_MCR(0x03); |
| 60 | for (Bit16u i=0; i<*size; i++) |
| 61 | { |
| 62 | if(!(sclass->Putchar(data[i],true,true,1000))) { |
| 63 | *size=i; |
| 64 | sclass->Write_MCR(0x01); |
| 65 | return false; |
| 66 | } |
| 67 | } |
| 68 | // RTS off |
| 69 | sclass->Write_MCR(0x01); |
| 70 | return true; |
| 71 | } |
| 72 | |
| 73 | bool device_COM::Seek(Bit32u * pos,Bit32u type) { |
| 74 | *pos = 0; |
| 75 | return true; |
| 76 | } |
| 77 | |
| 78 | bool device_COM::Close() { |
| 79 | return false; |
| 80 | } |
| 81 | |
| 82 | Bit16u device_COM::GetInformation(void) { |
| 83 | return 0x80A0; |
| 84 | } |
| 85 | |
| 86 | device_COM::device_COM(class CSerial* sc) { |
| 87 | sclass = sc; |
| 88 | SetName(serial_comname[sclass->idnumber]); |
| 89 | } |
| 90 | |
| 91 | device_COM::~device_COM() { |
| 92 | } |
| 93 | |
| 94 | |
| 95 | |
| 96 | // COM1 - COM4 objects |
| 97 | CSerial* serialports[4] ={0,0,0,0}; |
| 98 | |
| 99 | static Bitu SERIAL_Read (Bitu port, Bitu iolen) { |
| 100 | Bitu i; |
| 101 | Bitu retval; |
| 102 | Bitu index = port & 0x7; |
| 103 | switch(port&0xff8) { |
| 104 | case 0x3f8: i=0; break; |
| 105 | case 0x2f8: i=1; break; |
| 106 | case 0x3e8: i=2; break; |
| 107 | case 0x2e8: i=3; break; |
| 108 | default: return 0xff; |
| 109 | } |
| 110 | if(serialports[i]==0) return 0xff; |
| 111 | |
| 112 | switch (index) { |
| 113 | case RHR_OFFSET: |
| 114 | retval = serialports[i]->Read_RHR(); |
| 115 | break; |
| 116 | case IER_OFFSET: |
| 117 | retval = serialports[i]->Read_IER(); |
| 118 | break; |
| 119 | case ISR_OFFSET: |
| 120 | retval = serialports[i]->Read_ISR(); |
| 121 | break; |
| 122 | case LCR_OFFSET: |
| 123 | retval = serialports[i]->Read_LCR(); |
| 124 | break; |
| 125 | case MCR_OFFSET: |
| 126 | retval = serialports[i]->Read_MCR(); |
| 127 | break; |
| 128 | case LSR_OFFSET: |
| 129 | retval = serialports[i]->Read_LSR(); |
| 130 | break; |
| 131 | case MSR_OFFSET: |
| 132 | retval = serialports[i]->Read_MSR(); |
| 133 | break; |
| 134 | case SPR_OFFSET: |
| 135 | retval = serialports[i]->Read_SPR(); |
| 136 | break; |
| 137 | } |
| 138 | |
| 139 | #if SERIAL_DEBUG |
| 140 | const char* const dbgtext[]= |
| 141 | {"RHR","IER","ISR","LCR","MCR","LSR","MSR","SPR","DLL","DLM"}; |
| 142 | if(serialports[i]->dbg_register) { |
| 143 | if((index<2) && ((serialports[i]->LCR)&LCR_DIVISOR_Enable_MASK)) |
| 144 | index += 8; |
| 145 | serialports[i]->log_ser(serialports[i]->dbg_register, |
| 146 | "read 0x%2x from %s.",retval,dbgtext[index]); |
| 147 | } |
| 148 | #endif |
| 149 | return retval; |
| 150 | } |
| 151 | static void SERIAL_Write (Bitu port, Bitu val, Bitu) { |
| 152 | Bitu i; |
| 153 | Bitu index = port & 0x7; |
| 154 | switch(port&0xff8) { |
| 155 | case 0x3f8: i=0; break; |
| 156 | case 0x2f8: i=1; break; |
| 157 | case 0x3e8: i=2; break; |
| 158 | case 0x2e8: i=3; break; |
| 159 | default: return; |
| 160 | } |
| 161 | if(serialports[i]==0) return; |
| 162 | |
| 163 | #if SERIAL_DEBUG |
| 164 | const char* const dbgtext[]={"THR","IER","FCR", |
| 165 | "LCR","MCR","!LSR","MSR","SPR","DLL","DLM"}; |
| 166 | if(serialports[i]->dbg_register) { |
| 167 | Bitu debugindex=index; |
| 168 | if((index<2) && ((serialports[i]->LCR)&LCR_DIVISOR_Enable_MASK)) |
| 169 | debugindex += 8; |
| 170 | serialports[i]->log_ser(serialports[i]->dbg_register, |
| 171 | "write 0x%2x to %s.",val,dbgtext[debugindex]); |
| 172 | } |
| 173 | #endif |
| 174 | switch (index) { |
| 175 | case THR_OFFSET: |
| 176 | serialports[i]->Write_THR (val); |
| 177 | return; |
| 178 | case IER_OFFSET: |
| 179 | serialports[i]->Write_IER (val); |
| 180 | return; |
| 181 | case FCR_OFFSET: |
| 182 | serialports[i]->Write_FCR (val); |
| 183 | return; |
| 184 | case LCR_OFFSET: |
| 185 | serialports[i]->Write_LCR (val); |
| 186 | return; |
| 187 | case MCR_OFFSET: |
| 188 | serialports[i]->Write_MCR (val); |
| 189 | return; |
| 190 | case MSR_OFFSET: |
| 191 | serialports[i]->Write_MSR (val); |
| 192 | return; |
| 193 | case SPR_OFFSET: |
| 194 | serialports[i]->Write_SPR (val); |
| 195 | return; |
| 196 | default: |
| 197 | serialports[i]->Write_reserved (val, port & 0x7); |
| 198 | } |
| 199 | } |
| 200 | #if SERIAL_DEBUG |
| 201 | void CSerial::log_ser(bool active, char const* format,...) { |
| 202 | if(active) { |
| 203 | // copied from DEBUG_SHOWMSG |
| 204 | char buf[512]; |
| 205 | buf[0]=0; |
| 206 | sprintf(buf,"%12.3f [% 7u] ",PIC_FullIndex(), SDL_GetTicks()); |
| 207 | va_list msg; |
| 208 | va_start(msg,format); |
| 209 | vsprintf(buf+strlen(buf),format,msg); |
| 210 | va_end(msg); |
| 211 | // Add newline if not present |
| 212 | Bitu len=strlen(buf); |
| 213 | if(buf[len-1]!='\n') strcat(buf,"\r\n"); |
| 214 | fputs(buf,debugfp); |
| 215 | } |
| 216 | } |
| 217 | #endif |
| 218 | |
| 219 | void CSerial::changeLineProperties() { |
| 220 | // update the event wait time |
| 221 | float bitlen; |
| 222 | |
| 223 | if(baud_divider==0) bitlen=(1000.0f/115200.0f); |
| 224 | else bitlen = (1000.0f/115200.0f)*(float)baud_divider; |
| 225 | bytetime=bitlen*(float)(1+5+1); // startbit + minimum length + stopbit |
| 226 | bytetime+= bitlen*(float)(LCR&0x3); // databits |
| 227 | if(LCR&0x4) bytetime+=bitlen; // stopbit |
| 228 | |
| 229 | #if SERIAL_DEBUG |
| 230 | const char* const dbgtext[]={"none","odd","none","even","none","mark","none","space"}; |
| 231 | log_ser(dbg_serialtraffic,"New COM parameters: baudrate %5.0f, parity %s, wordlen %d, stopbits %d", |
| 232 | 1.0/bitlen*1000.0f,dbgtext[(LCR&0x38)>>3],(LCR&0x3)+5,((LCR&0x4)>>2)+1); |
| 233 | #endif |
| 234 | updatePortConfig (baud_divider, LCR); |
| 235 | } |
| 236 | |
| 237 | static void Serial_EventHandler(Bitu val) { |
| 238 | Bitu serclassid=val&0x3; |
| 239 | if(serialports[serclassid]!=0) |
| 240 | serialports[serclassid]->handleEvent(val>>2); |
| 241 | } |
| 242 | |
| 243 | void CSerial::setEvent(Bit16u type, float duration) { |
| 244 | PIC_AddEvent(Serial_EventHandler,duration,(type<<2)|idnumber); |
| 245 | } |
| 246 | |
| 247 | void CSerial::removeEvent(Bit16u type) { |
| 248 | // TODO |
| 249 | PIC_RemoveSpecificEvents(Serial_EventHandler,(type<<2)|idnumber); |
| 250 | } |
| 251 | |
| 252 | void CSerial::handleEvent(Bit16u type) { |
| 253 | switch(type) { |
| 254 | case SERIAL_TX_LOOPBACK_EVENT: { |
| 255 | |
| 256 | #if SERIAL_DEBUG |
| 257 | log_ser(dbg_serialtraffic,loopback_data<0x10? |
| 258 | "tx 0x%02x (%u) (loopback)":"tx 0x%02x (%c) (loopback)", |
| 259 | loopback_data, loopback_data); |
| 260 | #endif |
| 261 | receiveByte (loopback_data); |
| 262 | ByteTransmitted (); |
| 263 | break; |
| 264 | } |
| 265 | case SERIAL_THR_LOOPBACK_EVENT: { |
| 266 | loopback_data=txfifo->probeByte(); |
| 267 | ByteTransmitting(); |
| 268 | setEvent(SERIAL_TX_LOOPBACK_EVENT,bytetime); |
| 269 | break; |
| 270 | } |
| 271 | case SERIAL_ERRMSG_EVENT: { |
| 272 | LOG_MSG("Serial%d: Errors: "\ |
| 273 | "Framing %d, Parity %d, Overrun RX:%d (IF0:%d), TX:%d, Break %d", |
| 274 | COMNUMBER, framingErrors, parityErrors, overrunErrors, |
| 275 | overrunIF0,txOverrunErrors, breakErrors); |
| 276 | errormsg_pending=false; |
| 277 | framingErrors=0; |
| 278 | parityErrors=0; |
| 279 | overrunErrors=0; |
| 280 | txOverrunErrors=0; |
| 281 | overrunIF0=0; |
| 282 | breakErrors=0; |
| 283 | break; |
| 284 | } |
| 285 | case SERIAL_RX_TIMEOUT_EVENT: { |
| 286 | rise(TIMEOUT_PRIORITY); |
| 287 | break; |
| 288 | } |
| 289 | default: handleUpperEvent(type); |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | /*****************************************************************************/ |
| 294 | /* Interrupt control routines **/ |
| 295 | /*****************************************************************************/ |
| 296 | void CSerial::rise (Bit8u priority) { |
| 297 | #if SERIAL_DEBUG |
| 298 | if(priority&TX_PRIORITY && !(waiting_interrupts&TX_PRIORITY)) |
| 299 | log_ser(dbg_interrupt,"tx interrupt on."); |
| 300 | if(priority&RX_PRIORITY && !(waiting_interrupts&RX_PRIORITY)) |
| 301 | log_ser(dbg_interrupt,"rx interrupt on."); |
| 302 | if(priority&MSR_PRIORITY && !(waiting_interrupts&MSR_PRIORITY)) |
| 303 | log_ser(dbg_interrupt,"msr interrupt on."); |
| 304 | if(priority&TIMEOUT_PRIORITY && !(waiting_interrupts&TIMEOUT_PRIORITY)) |
| 305 | log_ser(dbg_interrupt,"fifo rx timeout interrupt on."); |
| 306 | #endif |
| 307 | |
| 308 | waiting_interrupts |= priority; |
| 309 | ComputeInterrupts(); |
| 310 | } |
| 311 | |
| 312 | // clears the pending interrupt, triggers other waiting interrupt |
| 313 | void CSerial::clear (Bit8u priority) { |
| 314 | |
| 315 | #if SERIAL_DEBUG |
| 316 | if(priority&TX_PRIORITY && (waiting_interrupts&TX_PRIORITY)) |
| 317 | log_ser(dbg_interrupt,"tx interrupt off."); |
| 318 | if(priority&RX_PRIORITY && (waiting_interrupts&RX_PRIORITY)) |
| 319 | log_ser(dbg_interrupt,"rx interrupt off."); |
| 320 | if(priority&MSR_PRIORITY && (waiting_interrupts&MSR_PRIORITY)) |
| 321 | log_ser(dbg_interrupt,"msr interrupt off."); |
| 322 | if(priority&ERROR_PRIORITY && (waiting_interrupts&ERROR_PRIORITY)) |
| 323 | log_ser(dbg_interrupt,"error interrupt off."); |
| 324 | #endif |
| 325 | waiting_interrupts &= (~priority); |
| 326 | ComputeInterrupts(); |
| 327 | } |
| 328 | |
| 329 | void CSerial::ComputeInterrupts () { |
| 330 | |
| 331 | Bitu val = IER & waiting_interrupts; |
| 332 | |
| 333 | if (val & ERROR_PRIORITY) ISR = ISR_ERROR_VAL; |
| 334 | else if (val & TIMEOUT_PRIORITY) ISR = ISR_FIFOTIMEOUT_VAL; |
| 335 | else if (val & RX_PRIORITY) ISR = ISR_RX_VAL; |
| 336 | else if (val & TX_PRIORITY) ISR = ISR_TX_VAL; |
| 337 | else if (val & MSR_PRIORITY) ISR = ISR_MSR_VAL; |
| 338 | else ISR = ISR_CLEAR_VAL; |
| 339 | |
| 340 | if(val && !irq_active) |
| 341 | { |
| 342 | irq_active=true; |
| 343 | if(op2) { |
| 344 | PIC_ActivateIRQ(irq); |
| 345 | #if SERIAL_DEBUG |
| 346 | log_ser(dbg_interrupt,"IRQ%d on.",irq); |
| 347 | #endif |
| 348 | } |
| 349 | } else if((!val) && irq_active) { |
| 350 | irq_active=false; |
| 351 | if(op2) { |
| 352 | PIC_DeActivateIRQ(irq); |
| 353 | #if SERIAL_DEBUG |
| 354 | log_ser(dbg_interrupt,"IRQ%d off.",irq); |
| 355 | #endif |
| 356 | } |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | /*****************************************************************************/ |
| 361 | /* Can a byte be received? **/ |
| 362 | /*****************************************************************************/ |
| 363 | bool CSerial::CanReceiveByte() { |
| 364 | return !rxfifo->isFull(); |
| 365 | } |
| 366 | |
| 367 | /*****************************************************************************/ |
| 368 | /* A byte was received **/ |
| 369 | /*****************************************************************************/ |
| 370 | void CSerial::receiveByteEx (Bit8u data, Bit8u error) { |
| 371 | #if SERIAL_DEBUG |
| 372 | log_ser(dbg_serialtraffic,data<0x10 ? "\t\t\t\trx 0x%02x (%u)": |
| 373 | "\t\t\t\trx 0x%02x (%c)", data, data); |
| 374 | #endif |
| 375 | if (!(rxfifo->addb(data))) { |
| 376 | // Overrun error ;o |
| 377 | error |= LSR_OVERRUN_ERROR_MASK; |
| 378 | } |
| 379 | removeEvent(SERIAL_RX_TIMEOUT_EVENT); |
| 380 | if(rxfifo->getUsage()==rx_interrupt_threshold) rise (RX_PRIORITY); |
| 381 | else setEvent(SERIAL_RX_TIMEOUT_EVENT,bytetime*4.0f); |
| 382 | |
| 383 | if(error) { |
| 384 | // A lot of UART chips generate a framing error too when receiving break |
| 385 | if(error&LSR_RX_BREAK_MASK) error |= LSR_FRAMING_ERROR_MASK; |
| 386 | #if SERIAL_DEBUG |
| 387 | log_ser(dbg_serialtraffic,"with error: framing=%d,overrun=%d,break=%d,parity=%d", |
| 388 | (error&LSR_FRAMING_ERROR_MASK)>0,(error&LSR_OVERRUN_ERROR_MASK)>0, |
| 389 | (error&LSR_RX_BREAK_MASK)>0,(error&LSR_PARITY_ERROR_MASK)>0); |
| 390 | #endif |
| 391 | if(FCR&FCR_ACTIVATE) { |
| 392 | // error and FIFO active |
| 393 | if(!errorfifo->isFull()) { |
| 394 | errors_in_fifo++; |
| 395 | errorfifo->addb(error); |
| 396 | } |
| 397 | else { |
| 398 | Bit8u toperror=errorfifo->getTop(); |
| 399 | if(!toperror) errors_in_fifo++; |
| 400 | errorfifo->addb(error|toperror); |
| 401 | } |
| 402 | if(errorfifo->probeByte()) { |
| 403 | // the next byte in the error fifo has an error |
| 404 | rise (ERROR_PRIORITY); |
| 405 | LSR |= error; |
| 406 | } |
| 407 | } else { |
| 408 | // error and FIFO inactive |
| 409 | rise (ERROR_PRIORITY); |
| 410 | LSR |= error; |
| 411 | }; |
| 412 | if(error&LSR_PARITY_ERROR_MASK) { |
| 413 | parityErrors++; |
| 414 | }; |
| 415 | if(error&LSR_OVERRUN_ERROR_MASK) { |
| 416 | overrunErrors++; |
| 417 | if(!GETFLAG(IF)) overrunIF0++; |
| 418 | #if SERIAL_DEBUG |
| 419 | log_ser(dbg_serialtraffic,"rx overrun (IF=%d)", GETFLAG(IF)>0); |
| 420 | #endif |
| 421 | }; |
| 422 | if(error&LSR_FRAMING_ERROR_MASK) { |
| 423 | framingErrors++; |
| 424 | } |
| 425 | if(error&LSR_RX_BREAK_MASK) { |
| 426 | breakErrors++; |
| 427 | } |
| 428 | // trigger status window error notification |
| 429 | if(!errormsg_pending) { |
| 430 | errormsg_pending=true; |
| 431 | setEvent(SERIAL_ERRMSG_EVENT,1000); |
| 432 | } |
| 433 | } else { |
| 434 | // no error |
| 435 | if(FCR&FCR_ACTIVATE) { |
| 436 | errorfifo->addb(error); |
| 437 | } |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | void CSerial::receiveByte (Bit8u data) { |
| 442 | receiveByteEx(data,0); |
| 443 | } |
| 444 | |
| 445 | /*****************************************************************************/ |
| 446 | /* ByteTransmitting: Byte has made it from THR to TX. **/ |
| 447 | /*****************************************************************************/ |
| 448 | void CSerial::ByteTransmitting() { |
| 449 | if(sync_guardtime) { |
| 450 | //LOG_MSG("byte transmitting after guard"); |
| 451 | //if(txfifo->isEmpty()) LOG_MSG("Serial port: FIFO empty when it should not"); |
| 452 | sync_guardtime=false; |
| 453 | txfifo->getb(); |
| 454 | } //else LOG_MSG("byte transmitting"); |
| 455 | if(txfifo->isEmpty())rise (TX_PRIORITY); |
| 456 | } |
| 457 | |
| 458 | |
| 459 | /*****************************************************************************/ |
| 460 | /* ByteTransmitted: When a byte was sent, notify here. **/ |
| 461 | /*****************************************************************************/ |
| 462 | void CSerial::ByteTransmitted () { |
| 463 | if(!txfifo->isEmpty()) { |
| 464 | // there is more data |
| 465 | Bit8u data = txfifo->getb(); |
| 466 | #if SERIAL_DEBUG |
| 467 | log_ser(dbg_serialtraffic,data<0x10? |
| 468 | "\t\t\t\t\ttx 0x%02x (%u) (from buffer)": |
| 469 | "\t\t\t\t\ttx 0x%02x (%c) (from buffer)",data,data); |
| 470 | #endif |
| 471 | if (loopback) setEvent(SERIAL_TX_LOOPBACK_EVENT, bytetime); |
| 472 | else transmitByte(data,false); |
| 473 | if(txfifo->isEmpty())rise (TX_PRIORITY); |
| 474 | |
| 475 | } else { |
| 476 | #if SERIAL_DEBUG |
| 477 | log_ser(dbg_serialtraffic,"tx buffer empty."); |
| 478 | #endif |
| 479 | LSR |= LSR_TX_EMPTY_MASK; |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | /*****************************************************************************/ |
| 484 | /* Transmit Holding Register, also LSB of Divisor Latch (r/w) **/ |
| 485 | /*****************************************************************************/ |
| 486 | void CSerial::Write_THR (Bit8u data) { |
| 487 | // 0-7 transmit data |
| 488 | |
| 489 | if ((LCR & LCR_DIVISOR_Enable_MASK)) { |
| 490 | // write to DLL |
| 491 | baud_divider&=0xFF00; |
| 492 | baud_divider |= data; |
| 493 | changeLineProperties(); |
| 494 | } else { |
| 495 | // write to THR |
| 496 | clear (TX_PRIORITY); |
| 497 | |
| 498 | if((LSR & LSR_TX_EMPTY_MASK)) |
| 499 | { // we were idle before |
| 500 | //LOG_MSG("starting new transmit cycle"); |
| 501 | //if(sync_guardtime) LOG_MSG("Serial port internal error 1"); |
| 502 | //if(!(LSR & LSR_TX_EMPTY_MASK)) LOG_MSG("Serial port internal error 2"); |
| 503 | //if(txfifo->getUsage()) LOG_MSG("Serial port internal error 3"); |
| 504 | |
| 505 | // need "warming up" time |
| 506 | sync_guardtime=true; |
| 507 | // block the fifo so it returns THR full (or not in case of FIFO on) |
| 508 | txfifo->addb(data); |
| 509 | // transmit shift register is busy |
| 510 | LSR &= (~LSR_TX_EMPTY_MASK); |
| 511 | if(loopback) setEvent(SERIAL_THR_LOOPBACK_EVENT, bytetime/10); |
| 512 | else { |
| 513 | #if SERIAL_DEBUG |
| 514 | log_ser(dbg_serialtraffic,data<0x10? |
| 515 | "\t\t\t\t\ttx 0x%02x (%u) [FIFO=%2d]": |
| 516 | "\t\t\t\t\ttx 0x%02x (%c) [FIFO=%2d]",data,data,txfifo->getUsage()); |
| 517 | #endif |
| 518 | transmitByte (data,true); |
| 519 | } |
| 520 | } else { |
| 521 | // shift register is transmitting |
| 522 | if(!txfifo->addb(data)) { |
| 523 | // TX overflow |
| 524 | #if SERIAL_DEBUG |
| 525 | log_ser(dbg_serialtraffic,"tx overflow"); |
| 526 | #endif |
| 527 | txOverrunErrors++; |
| 528 | if(!errormsg_pending) { |
| 529 | errormsg_pending=true; |
| 530 | setEvent(SERIAL_ERRMSG_EVENT,1000); |
| 531 | } |
| 532 | } |
| 533 | } |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | |
| 538 | /*****************************************************************************/ |
| 539 | /* Receive Holding Register, also LSB of Divisor Latch (r/w) **/ |
| 540 | /*****************************************************************************/ |
| 541 | Bitu CSerial::Read_RHR () { |
| 542 | // 0-7 received data |
| 543 | if ((LCR & LCR_DIVISOR_Enable_MASK)) return baud_divider&0xff; |
| 544 | else { |
| 545 | Bit8u data=rxfifo->getb(); |
| 546 | if(FCR&FCR_ACTIVATE) { |
| 547 | Bit8u error=errorfifo->getb(); |
| 548 | if(error) errors_in_fifo--; |
| 549 | // new error |
| 550 | if(!rxfifo->isEmpty()) { |
| 551 | error=errorfifo->probeByte(); |
| 552 | if(error) { |
| 553 | LSR |= error; |
| 554 | rise(ERROR_PRIORITY); |
| 555 | } |
| 556 | } |
| 557 | } |
| 558 | // Reading RHR resets the FIFO timeout |
| 559 | clear (TIMEOUT_PRIORITY); |
| 560 | // RX int. is cleared if the buffer holds less data than the threshold |
| 561 | if(rxfifo->getUsage()<rx_interrupt_threshold)clear(RX_PRIORITY); |
| 562 | removeEvent(SERIAL_RX_TIMEOUT_EVENT); |
| 563 | if(!rxfifo->isEmpty()) setEvent(SERIAL_RX_TIMEOUT_EVENT,bytetime*4.0f); |
| 564 | return data; |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | /*****************************************************************************/ |
| 569 | /* Interrupt Enable Register, also MSB of Divisor Latch (r/w) **/ |
| 570 | /*****************************************************************************/ |
| 571 | // Modified by: |
| 572 | // - writing to it. |
| 573 | Bitu CSerial::Read_IER () { |
| 574 | // 0 receive holding register (byte received) |
| 575 | // 1 transmit holding register (byte sent) |
| 576 | // 2 receive line status (overrun, parity error, frame error, break) |
| 577 | // 3 modem status |
| 578 | // 4-7 0 |
| 579 | |
| 580 | if (LCR & LCR_DIVISOR_Enable_MASK) return baud_divider>>8; |
| 581 | else return IER&0x0f; |
| 582 | } |
| 583 | |
| 584 | void CSerial::Write_IER (Bit8u data) { |
| 585 | if ((LCR & LCR_DIVISOR_Enable_MASK)) { // write to DLM |
| 586 | baud_divider&=0xff; |
| 587 | baud_divider |= ((Bit16u)data)<<8; |
| 588 | changeLineProperties(); |
| 589 | } else { |
| 590 | // Retrigger TX interrupt |
| 591 | if (txfifo->isEmpty()&& (data&TX_PRIORITY)) |
| 592 | waiting_interrupts |= TX_PRIORITY; |
| 593 | |
| 594 | IER = data&0xF; |
| 595 | if((FCR&FCR_ACTIVATE)&&data&RX_PRIORITY) IER |= TIMEOUT_PRIORITY; |
| 596 | ComputeInterrupts(); |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | /*****************************************************************************/ |
| 601 | /* Interrupt Status Register (r) **/ |
| 602 | /*****************************************************************************/ |
| 603 | // modified by: |
| 604 | // - incoming interrupts |
| 605 | // - loopback mode |
| 606 | Bitu CSerial::Read_ISR () { |
| 607 | // 0 0:interrupt pending 1: no interrupt |
| 608 | // 1-3 identification |
| 609 | // 011 LSR |
| 610 | // 010 RXRDY |
| 611 | // 110 RX_TIMEOUT |
| 612 | // 001 TXRDY |
| 613 | // 000 MSR |
| 614 | // 4-7 0 |
| 615 | |
| 616 | if(IER&Modem_Status_INT_Enable_MASK) updateMSR(); |
| 617 | Bit8u retval = ISR; |
| 618 | |
| 619 | // clear changes ISR!! mean.. |
| 620 | if(ISR==ISR_TX_VAL) clear(TX_PRIORITY); |
| 621 | if(FCR&FCR_ACTIVATE) retval |= FIFO_STATUS_ACTIVE; |
| 622 | |
| 623 | return retval; |
| 624 | } |
| 625 | |
| 626 | #define BIT_CHANGE_H(oldv,newv,bitmask) (!(oldv&bitmask) && (newv&bitmask)) |
| 627 | #define BIT_CHANGE_L(oldv,newv,bitmask) ((oldv&bitmask) && !(newv&bitmask)) |
| 628 | |
| 629 | void CSerial::Write_FCR (Bit8u data) { |
| 630 | if(BIT_CHANGE_H(FCR,data,FCR_ACTIVATE)) { |
| 631 | // FIFO was switched on |
| 632 | errors_in_fifo=0; // should already be 0 |
| 633 | errorfifo->setSize(fifosize); |
| 634 | rxfifo->setSize(fifosize); |
| 635 | txfifo->setSize(fifosize); |
| 636 | } else if(BIT_CHANGE_L(FCR,data,FCR_ACTIVATE)) { |
| 637 | // FIFO was switched off |
| 638 | errors_in_fifo=0; |
| 639 | errorfifo->setSize(1); |
| 640 | rxfifo->setSize(1); |
| 641 | txfifo->setSize(1); |
| 642 | rx_interrupt_threshold=1; |
| 643 | } |
| 644 | FCR=data&0xCF; |
| 645 | if(FCR&FCR_CLEAR_RX) { |
| 646 | errors_in_fifo=0; |
| 647 | errorfifo->clear(); |
| 648 | rxfifo->clear(); |
| 649 | } |
| 650 | if(FCR&FCR_CLEAR_TX) txfifo->clear(); |
| 651 | if(FCR&FCR_ACTIVATE) { |
| 652 | switch(FCR>>6) { |
| 653 | case 0: rx_interrupt_threshold=1; break; |
| 654 | case 1: rx_interrupt_threshold=4; break; |
| 655 | case 2: rx_interrupt_threshold=8; break; |
| 656 | case 3: rx_interrupt_threshold=14; break; |
| 657 | } |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | /*****************************************************************************/ |
| 662 | /* Line Control Register (r/w) **/ |
| 663 | /*****************************************************************************/ |
| 664 | // signal decoder configuration: |
| 665 | // - parity, stopbits, word length |
| 666 | // - send break |
| 667 | // - switch between RHR/THR and baud rate registers |
| 668 | // Modified by: |
| 669 | // - writing to it. |
| 670 | Bitu CSerial::Read_LCR () { |
| 671 | // 0-1 word length |
| 672 | // 2 stop bits |
| 673 | // 3 parity enable |
| 674 | // 4-5 parity type |
| 675 | // 6 set break |
| 676 | // 7 divisor latch enable |
| 677 | return LCR; |
| 678 | } |
| 679 | |
| 680 | void CSerial::Write_LCR (Bit8u data) { |
| 681 | Bit8u lcr_old = LCR; |
| 682 | LCR = data; |
| 683 | if (((data ^ lcr_old) & LCR_PORTCONFIG_MASK) != 0) { |
| 684 | changeLineProperties(); |
| 685 | } |
| 686 | if (((data ^ lcr_old) & LCR_BREAK_MASK) != 0) { |
| 687 | if(!loopback) setBreak ((LCR & LCR_BREAK_MASK)!=0); |
| 688 | else { |
| 689 | // TODO: set loopback break event to reveiveError after |
| 690 | } |
| 691 | #if SERIAL_DEBUG |
| 692 | log_ser(dbg_serialtraffic,((LCR & LCR_BREAK_MASK)!=0) ? |
| 693 | "break on.":"break off."); |
| 694 | #endif |
| 695 | } |
| 696 | } |
| 697 | |
| 698 | /*****************************************************************************/ |
| 699 | /* Modem Control Register (r/w) **/ |
| 700 | /*****************************************************************************/ |
| 701 | // Set levels of RTS and DTR, as well as loopback-mode. |
| 702 | // Modified by: |
| 703 | // - writing to it. |
| 704 | Bitu CSerial::Read_MCR () { |
| 705 | // 0 -DTR |
| 706 | // 1 -RTS |
| 707 | // 2 -OP1 |
| 708 | // 3 -OP2 |
| 709 | // 4 loopback enable |
| 710 | // 5-7 0 |
| 711 | Bit8u retval=0; |
| 712 | if(dtr) retval|=MCR_DTR_MASK; |
| 713 | if(rts) retval|=MCR_RTS_MASK; |
| 714 | if(op1) retval|=MCR_OP1_MASK; |
| 715 | if(op2) retval|=MCR_OP2_MASK; |
| 716 | if(loopback) retval|=MCR_LOOPBACK_Enable_MASK; |
| 717 | return retval; |
| 718 | } |
| 719 | |
| 720 | void CSerial::Write_MCR (Bit8u data) { |
| 721 | // WARNING: At the time setRTSDTR is called rts and dsr members are still wrong. |
| 722 | if(data&FIFO_FLOWCONTROL) LOG_MSG("Warning: tried to activate hardware handshake."); |
| 723 | bool temp_dtr = data & MCR_DTR_MASK? true:false; |
| 724 | bool temp_rts = data & MCR_RTS_MASK? true:false; |
| 725 | bool temp_op1 = data & MCR_OP1_MASK? true:false; |
| 726 | bool temp_op2 = data & MCR_OP2_MASK? true:false; |
| 727 | bool temp_loopback = data & MCR_LOOPBACK_Enable_MASK? true:false; |
| 728 | if(loopback!=temp_loopback) { |
| 729 | if(temp_loopback) setRTSDTR(false,false); |
| 730 | else setRTSDTR(temp_rts,temp_dtr); |
| 731 | } |
| 732 | |
| 733 | if (temp_loopback) { // is on: |
| 734 | // DTR->DSR |
| 735 | // RTS->CTS |
| 736 | // OP1->RI |
| 737 | // OP2->CD |
| 738 | if(temp_dtr!=dtr && !d_dsr) { |
| 739 | d_dsr=true; |
| 740 | rise (MSR_PRIORITY); |
| 741 | } |
| 742 | if(temp_rts!=rts && !d_cts) { |
| 743 | d_cts=true; |
| 744 | rise (MSR_PRIORITY); |
| 745 | } |
| 746 | if(temp_op1!=op1 && !d_ri) { |
| 747 | // interrupt only at trailing edge |
| 748 | if(!temp_op1) { |
| 749 | d_ri=true; |
| 750 | rise (MSR_PRIORITY); |
| 751 | } |
| 752 | } |
| 753 | if(temp_op2!=op2 && !d_cd) { |
| 754 | d_cd=true; |
| 755 | rise (MSR_PRIORITY); |
| 756 | } |
| 757 | } else { |
| 758 | // loopback is off |
| 759 | if(temp_rts!=rts) { |
| 760 | // RTS difference |
| 761 | if(temp_dtr!=dtr) { |
| 762 | // both difference |
| 763 | |
| 764 | #if SERIAL_DEBUG |
| 765 | log_ser(dbg_modemcontrol,"RTS %x.",temp_rts); |
| 766 | log_ser(dbg_modemcontrol,"DTR %x.",temp_dtr); |
| 767 | #endif |
| 768 | setRTSDTR(temp_rts, temp_dtr); |
| 769 | } else { |
| 770 | // only RTS |
| 771 | |
| 772 | #if SERIAL_DEBUG |
| 773 | log_ser(dbg_modemcontrol,"RTS %x.",temp_rts); |
| 774 | #endif |
| 775 | setRTS(temp_rts); |
| 776 | } |
| 777 | } else if(temp_dtr!=dtr) { |
| 778 | // only DTR |
| 779 | #if SERIAL_DEBUG |
| 780 | log_ser(dbg_modemcontrol,"%DTR %x.",temp_dtr); |
| 781 | #endif |
| 782 | setDTR(temp_dtr); |
| 783 | } |
| 784 | } |
| 785 | // interrupt logic: if OP2 is 0, the IRQ line is tristated (pulled high) |
| 786 | if((!op2) && temp_op2) { |
| 787 | // irq has been enabled (tristate high -> irq level) |
| 788 | if(!irq_active) PIC_DeActivateIRQ(irq); |
| 789 | } else if(op2 && (!temp_op2)) { |
| 790 | if(!irq_active) PIC_ActivateIRQ(irq); |
| 791 | } |
| 792 | |
| 793 | dtr=temp_dtr; |
| 794 | rts=temp_rts; |
| 795 | op1=temp_op1; |
| 796 | op2=temp_op2; |
| 797 | loopback=temp_loopback; |
| 798 | } |
| 799 | |
| 800 | /*****************************************************************************/ |
| 801 | /* Line Status Register (r) **/ |
| 802 | /*****************************************************************************/ |
| 803 | // errors, tx registers status, rx register status |
| 804 | // modified by: |
| 805 | // - event from real serial port |
| 806 | // - loopback |
| 807 | Bitu CSerial::Read_LSR () { |
| 808 | Bitu retval = LSR & (LSR_ERROR_MASK|LSR_TX_EMPTY_MASK); |
| 809 | if(txfifo->isEmpty()) retval |= LSR_TX_HOLDING_EMPTY_MASK; |
| 810 | if(!(rxfifo->isEmpty()))retval |= LSR_RX_DATA_READY_MASK; |
| 811 | if(errors_in_fifo) retval |= FIFO_ERROR; |
| 812 | LSR &= (~LSR_ERROR_MASK); // clear error bits on read |
| 813 | clear (ERROR_PRIORITY); |
| 814 | return retval; |
| 815 | } |
| 816 | |
| 817 | void CSerial::Write_MSR (Bit8u val) { |
| 818 | d_cts = (val&MSR_dCTS_MASK)?true:false; |
| 819 | d_dsr = (val&MSR_dDSR_MASK)?true:false; |
| 820 | d_cd = (val&MSR_dCD_MASK)?true:false; |
| 821 | d_ri = (val&MSR_dRI_MASK)?true:false; |
| 822 | } |
| 823 | |
| 824 | /*****************************************************************************/ |
| 825 | /* Modem Status Register (r) **/ |
| 826 | /*****************************************************************************/ |
| 827 | // Contains status of the control input lines (CD, RI, DSR, CTS) and |
| 828 | // their "deltas": if level changed since last read delta = 1. |
| 829 | // modified by: |
| 830 | // - real values |
| 831 | // - write operation to MCR in loopback mode |
| 832 | Bitu CSerial::Read_MSR () { |
| 833 | Bit8u retval=0; |
| 834 | |
| 835 | if (loopback) { |
| 836 | |
| 837 | if (rts) retval |= MSR_CTS_MASK; |
| 838 | if (dtr) retval |= MSR_DSR_MASK; |
| 839 | if (op1) retval |= MSR_RI_MASK; |
| 840 | if (op2) retval |= MSR_CD_MASK; |
| 841 | |
| 842 | } else { |
| 843 | |
| 844 | updateMSR(); |
| 845 | if (cd) retval |= MSR_CD_MASK; |
| 846 | if (ri) retval |= MSR_RI_MASK; |
| 847 | if (dsr) retval |= MSR_DSR_MASK; |
| 848 | if (cts) retval |= MSR_CTS_MASK; |
| 849 | |
| 850 | } |
| 851 | // new delta flags |
| 852 | if(d_cd) retval|=MSR_dCD_MASK; |
| 853 | if(d_ri) retval|=MSR_dRI_MASK; |
| 854 | if(d_cts) retval|=MSR_dCTS_MASK; |
| 855 | if(d_dsr) retval|=MSR_dDSR_MASK; |
| 856 | |
| 857 | d_cd = false; |
| 858 | d_ri = false; |
| 859 | d_cts = false; |
| 860 | d_dsr = false; |
| 861 | |
| 862 | clear (MSR_PRIORITY); |
| 863 | return retval; |
| 864 | } |
| 865 | |
| 866 | /*****************************************************************************/ |
| 867 | /* Scratchpad Register (r/w) **/ |
| 868 | /*****************************************************************************/ |
| 869 | // Just a memory register. Not much to do here. |
| 870 | Bitu CSerial::Read_SPR () { |
| 871 | return SPR; |
| 872 | } |
| 873 | |
| 874 | void CSerial::Write_SPR (Bit8u data) { |
| 875 | SPR = data; |
| 876 | } |
| 877 | |
| 878 | /*****************************************************************************/ |
| 879 | /* Write_reserved **/ |
| 880 | /*****************************************************************************/ |
| 881 | void CSerial::Write_reserved (Bit8u data, Bit8u address) { |
| 882 | /*LOG_UART("Serial%d: Write to reserved register, value 0x%x, register %x", |
| 883 | COMNUMBER, data, address);*/ |
| 884 | } |
| 885 | |
| 886 | /*****************************************************************************/ |
| 887 | /* MCR Access: returns cirquit state as boolean. **/ |
| 888 | /*****************************************************************************/ |
| 889 | bool CSerial::getDTR () { |
| 890 | if(loopback) return false; |
| 891 | else return dtr; |
| 892 | } |
| 893 | |
| 894 | bool CSerial::getRTS () { |
| 895 | if(loopback) return false; |
| 896 | else return rts; |
| 897 | } |
| 898 | |
| 899 | /*****************************************************************************/ |
| 900 | /* MSR Access **/ |
| 901 | /*****************************************************************************/ |
| 902 | bool CSerial::getRI () { |
| 903 | return ri; |
| 904 | } |
| 905 | |
| 906 | bool CSerial::getCD () { |
| 907 | return cd; |
| 908 | } |
| 909 | |
| 910 | bool CSerial::getDSR () { |
| 911 | return dsr; |
| 912 | } |
| 913 | |
| 914 | bool CSerial::getCTS () { |
| 915 | return cts; |
| 916 | } |
| 917 | |
| 918 | void CSerial::setRI (bool value) { |
| 919 | if (value != ri) { |
| 920 | |
| 921 | #if SERIAL_DEBUG |
| 922 | log_ser(dbg_modemcontrol,"%RI %x.",value); |
| 923 | #endif |
| 924 | // don't change delta when in loopback mode |
| 925 | ri=value; |
| 926 | if(!loopback) { |
| 927 | if(value==false) d_ri=true; |
| 928 | rise (MSR_PRIORITY); |
| 929 | } |
| 930 | } |
| 931 | //else no change |
| 932 | } |
| 933 | void CSerial::setDSR (bool value) { |
| 934 | if (value != dsr) { |
| 935 | #if SERIAL_DEBUG |
| 936 | log_ser(dbg_modemcontrol,"DSR %x.",value); |
| 937 | #endif |
| 938 | // don't change delta when in loopback mode |
| 939 | dsr=value; |
| 940 | if(!loopback) { |
| 941 | d_dsr=true; |
| 942 | rise (MSR_PRIORITY); |
| 943 | } |
| 944 | } |
| 945 | //else no change |
| 946 | } |
| 947 | void CSerial::setCD (bool value) { |
| 948 | if (value != cd) { |
| 949 | #if SERIAL_DEBUG |
| 950 | log_ser(dbg_modemcontrol,"CD %x.",value); |
| 951 | #endif |
| 952 | // don't change delta when in loopback mode |
| 953 | cd=value; |
| 954 | if(!loopback) { |
| 955 | d_cd=true; |
| 956 | rise (MSR_PRIORITY); |
| 957 | } |
| 958 | } |
| 959 | //else no change |
| 960 | } |
| 961 | void CSerial::setCTS (bool value) { |
| 962 | if (value != cts) { |
| 963 | #if SERIAL_DEBUG |
| 964 | log_ser(dbg_modemcontrol,"CTS %x.",value); |
| 965 | #endif |
| 966 | // don't change delta when in loopback mode |
| 967 | cts=value; |
| 968 | if(!loopback) { |
| 969 | d_cts=true; |
| 970 | rise (MSR_PRIORITY); |
| 971 | } |
| 972 | } |
| 973 | //else no change |
| 974 | } |
| 975 | |
| 976 | /*****************************************************************************/ |
| 977 | /* Initialisation **/ |
| 978 | /*****************************************************************************/ |
| 979 | void CSerial::Init_Registers () { |
| 980 | // The "power on" settings |
| 981 | irq_active=false; |
| 982 | waiting_interrupts = 0x0; |
| 983 | |
| 984 | Bit32u initbps = 9600; |
| 985 | Bit8u bytesize = 8; |
| 986 | char parity = 'N'; |
| 987 | |
| 988 | Bit8u lcrresult = 0; |
| 989 | Bit16u baudresult = 0; |
| 990 | |
| 991 | IER = 0; |
| 992 | ISR = 0x1; |
| 993 | LCR = 0; |
| 994 | //MCR = 0xff; |
| 995 | loopback = true; |
| 996 | dtr=true; |
| 997 | rts=true; |
| 998 | op1=true; |
| 999 | op2=true; |
| 1000 | |
| 1001 | sync_guardtime=false; |
| 1002 | FCR=0xff; |
| 1003 | Write_FCR(0x00); |
| 1004 | |
| 1005 | |
| 1006 | LSR = 0x60; |
| 1007 | d_cts = true; |
| 1008 | d_dsr = true; |
| 1009 | d_ri = true; |
| 1010 | d_cd = true; |
| 1011 | cts = true; |
| 1012 | dsr = true; |
| 1013 | ri = true; |
| 1014 | cd = true; |
| 1015 | |
| 1016 | SPR = 0xFF; |
| 1017 | |
| 1018 | baud_divider=0x0; |
| 1019 | |
| 1020 | // make lcr: byte size, parity, stopbits, baudrate |
| 1021 | |
| 1022 | if (bytesize == 5) |
| 1023 | lcrresult |= LCR_DATABITS_5; |
| 1024 | else if (bytesize == 6) |
| 1025 | lcrresult |= LCR_DATABITS_6; |
| 1026 | else if (bytesize == 7) |
| 1027 | lcrresult |= LCR_DATABITS_7; |
| 1028 | else |
| 1029 | lcrresult |= LCR_DATABITS_8; |
| 1030 | |
| 1031 | switch(parity) |
| 1032 | { |
| 1033 | case 'N': |
| 1034 | case 'n': |
| 1035 | lcrresult |= LCR_PARITY_NONE; |
| 1036 | break; |
| 1037 | case 'O': |
| 1038 | case 'o': |
| 1039 | lcrresult |= LCR_PARITY_ODD; |
| 1040 | break; |
| 1041 | case 'E': |
| 1042 | case 'e': |
| 1043 | lcrresult |= LCR_PARITY_EVEN; |
| 1044 | break; |
| 1045 | case 'M': |
| 1046 | case 'm': |
| 1047 | lcrresult |= LCR_PARITY_MARK; |
| 1048 | break; |
| 1049 | case 'S': |
| 1050 | case 's': |
| 1051 | lcrresult |= LCR_PARITY_SPACE; |
| 1052 | break; |
| 1053 | } |
| 1054 | |
| 1055 | // baudrate |
| 1056 | if (initbps > 0) |
| 1057 | baudresult = (Bit16u) (115200 / initbps); |
| 1058 | else |
| 1059 | baudresult = 12; // = 9600 baud |
| 1060 | |
| 1061 | Write_MCR (0); |
| 1062 | Write_LCR (LCR_DIVISOR_Enable_MASK); |
| 1063 | Write_THR ((Bit8u) baudresult & 0xff); |
| 1064 | Write_IER ((Bit8u) (baudresult >> 8)); |
| 1065 | Write_LCR (lcrresult); |
| 1066 | updateMSR(); |
| 1067 | Read_MSR(); |
| 1068 | PIC_DeActivateIRQ(irq); |
| 1069 | } |
| 1070 | |
| 1071 | CSerial::CSerial(Bitu id, CommandLine* cmd) { |
| 1072 | idnumber=id; |
| 1073 | Bit16u base = serial_baseaddr[id]; |
| 1074 | |
| 1075 | irq = serial_defaultirq[id]; |
| 1076 | getBituSubstring("irq:",&irq, cmd); |
| 1077 | if (irq < 2 || irq > 15) irq = serial_defaultirq[id]; |
| 1078 | |
| 1079 | #if SERIAL_DEBUG |
| 1080 | dbg_serialtraffic = cmd->FindExist("dbgtr", false); |
| 1081 | dbg_modemcontrol = cmd->FindExist("dbgmd", false); |
| 1082 | dbg_register = cmd->FindExist("dbgreg", false); |
| 1083 | dbg_interrupt = cmd->FindExist("dbgirq", false); |
| 1084 | dbg_aux = cmd->FindExist("dbgaux", false); |
| 1085 | |
| 1086 | if(cmd->FindExist("dbgall", false)) { |
| 1087 | dbg_serialtraffic= |
| 1088 | dbg_modemcontrol= |
| 1089 | dbg_register= |
| 1090 | dbg_interrupt= |
| 1091 | dbg_aux= true; |
| 1092 | } |
| 1093 | |
| 1094 | |
| 1095 | if(dbg_serialtraffic|dbg_modemcontrol|dbg_register|dbg_interrupt|dbg_aux) |
| 1096 | debugfp=OpenCaptureFile("serlog",".serlog.txt"); |
| 1097 | else debugfp=0; |
| 1098 | |
| 1099 | if(debugfp == 0) { |
| 1100 | dbg_serialtraffic= |
| 1101 | dbg_modemcontrol= |
| 1102 | dbg_register= |
| 1103 | dbg_interrupt= |
| 1104 | dbg_aux= false; |
| 1105 | } else { |
| 1106 | std::string cleft; |
| 1107 | cmd->GetStringRemain(cleft); |
| 1108 | |
| 1109 | log_ser(true,"Serial%d: BASE %3x, IRQ %d, initstring \"%s\"\r\n\r\n", |
| 1110 | COMNUMBER,base,irq,cleft.c_str()); |
| 1111 | } |
| 1112 | #endif |
| 1113 | fifosize=16; |
| 1114 | |
| 1115 | errorfifo = new MyFifo(fifosize); |
| 1116 | rxfifo = new MyFifo(fifosize); |
| 1117 | txfifo = new MyFifo(fifosize); |
| 1118 | |
| 1119 | mydosdevice=new device_COM(this); |
| 1120 | DOS_AddDevice(mydosdevice); |
| 1121 | |
| 1122 | errormsg_pending=false; |
| 1123 | framingErrors=0; |
| 1124 | parityErrors=0; |
| 1125 | overrunErrors=0; |
| 1126 | txOverrunErrors=0; |
| 1127 | overrunIF0=0; |
| 1128 | breakErrors=0; |
| 1129 | |
| 1130 | for (Bitu i = 0; i <= 7; i++) { |
| 1131 | WriteHandler[i].Install (i + base, SERIAL_Write, IO_MB); |
| 1132 | ReadHandler[i].Install (i + base, SERIAL_Read, IO_MB); |
| 1133 | } |
| 1134 | } |
| 1135 | |
| 1136 | bool CSerial::getBituSubstring(const char* name,Bitu* data, CommandLine* cmd) { |
| 1137 | std::string tmpstring; |
| 1138 | if(!(cmd->FindStringBegin(name,tmpstring,false))) return false; |
| 1139 | const char* tmpchar=tmpstring.c_str(); |
| 1140 | if(sscanf(tmpchar,"%u",data)!=1) return false; |
| 1141 | return true; |
| 1142 | } |
| 1143 | |
| 1144 | CSerial::~CSerial(void) { |
| 1145 | DOS_DelDevice(mydosdevice); |
| 1146 | for(Bitu i = 0; i <= SERIAL_BASE_EVENT_COUNT; i++) |
| 1147 | removeEvent(i); |
| 1148 | }; |
| 1149 | bool CSerial::Getchar(Bit8u* data, Bit8u* lsr, bool wait_dsr, Bitu timeout) { |
| 1150 | double starttime=PIC_FullIndex(); |
| 1151 | // wait for DSR on |
| 1152 | if(wait_dsr) { |
| 1153 | while((!(Read_MSR()&0x20))&&(starttime>PIC_FullIndex()-timeout)) |
| 1154 | CALLBACK_Idle(); |
| 1155 | if(!(starttime>PIC_FullIndex()-timeout)) { |
| 1156 | #if SERIAL_DEBUG |
| 1157 | log_ser(dbg_aux,"Getchar status timeout: MSR 0x%x",Read_MSR()); |
| 1158 | #endif |
| 1159 | return false; |
| 1160 | } |
| 1161 | } |
| 1162 | // wait for a byte to arrive |
| 1163 | while((!((*lsr=Read_LSR())&0x1))&&(starttime>PIC_FullIndex()-timeout)) |
| 1164 | CALLBACK_Idle(); |
| 1165 | |
| 1166 | if(!(starttime>PIC_FullIndex()-timeout)) { |
| 1167 | #if SERIAL_DEBUG |
| 1168 | log_ser(dbg_aux,"Getchar data timeout: MSR 0x%x",Read_MSR()); |
| 1169 | #endif |
| 1170 | return false; |
| 1171 | } |
| 1172 | *data=Read_RHR(); |
| 1173 | |
| 1174 | #if SERIAL_DEBUG |
| 1175 | log_ser(dbg_aux,"Getchar read 0x%x",*data); |
| 1176 | #endif |
| 1177 | return true; |
| 1178 | } |
| 1179 | |
| 1180 | |
| 1181 | bool CSerial::Putchar(Bit8u data, bool wait_dsr, bool wait_cts, Bitu timeout) { |
| 1182 | |
| 1183 | double starttime=PIC_FullIndex(); |
| 1184 | // wait for it to become empty |
| 1185 | while(!(Read_LSR()&0x20)) { |
| 1186 | CALLBACK_Idle(); |
| 1187 | } |
| 1188 | // wait for DSR+CTS on |
| 1189 | if(wait_dsr||wait_cts) { |
| 1190 | if(wait_dsr||wait_cts) { |
| 1191 | while(((Read_MSR()&0x30)!=0x30)&&(starttime>PIC_FullIndex()-timeout)) |
| 1192 | CALLBACK_Idle(); |
| 1193 | } else if(wait_dsr) { |
| 1194 | while(!(Read_MSR()&0x20)&&(starttime>PIC_FullIndex()-timeout)) |
| 1195 | CALLBACK_Idle(); |
| 1196 | } else if(wait_cts) { |
| 1197 | while(!(Read_MSR()&0x10)&&(starttime>PIC_FullIndex()-timeout)) |
| 1198 | CALLBACK_Idle(); |
| 1199 | } |
| 1200 | if(!(starttime>PIC_FullIndex()-timeout)) { |
| 1201 | #if SERIAL_DEBUG |
| 1202 | log_ser(dbg_aux,"Putchar timeout: MSR 0x%x",Read_MSR()); |
| 1203 | #endif |
| 1204 | return false; |
| 1205 | } |
| 1206 | } |
| 1207 | Write_THR(data); |
| 1208 | |
| 1209 | #if SERIAL_DEBUG |
| 1210 | log_ser(dbg_aux,"Putchar 0x%x",data); |
| 1211 | #endif |
| 1212 | |
| 1213 | return true; |
| 1214 | } |
| 1215 | |
| 1216 | class SERIALPORTS:public Module_base { |
| 1217 | public: |
| 1218 | SERIALPORTS (Section * configuration):Module_base (configuration) { |
| 1219 | Bit16u biosParameter[4] = { 0, 0, 0, 0 }; |
| 1220 | Section_prop *section = static_cast <Section_prop*>(configuration); |
| 1221 | |
| 1222 | char s_property[] = "serialx"; |
| 1223 | for(Bitu i = 0; i < 4; i++) { |
| 1224 | // get the configuration property |
| 1225 | s_property[6] = '1' + i; |
| 1226 | Prop_multival* p = section->Get_multival(s_property); |
| 1227 | std::string type = p->GetSection()->Get_string("type"); |
| 1228 | CommandLine cmd(0,p->GetSection()->Get_string("parameters")); |
| 1229 | |
| 1230 | // detect the type |
| 1231 | if (type=="dummy") { |
| 1232 | serialports[i] = new CSerialDummy (i, &cmd); |
| 1233 | } |
| 1234 | #ifdef DIRECTSERIAL_AVAILIBLE |
| 1235 | else if (type=="directserial") { |
| 1236 | serialports[i] = new CDirectSerial (i, &cmd); |
| 1237 | if (!serialports[i]->InstallationSuccessful) { |
| 1238 | // serial port name was wrong or already in use |
| 1239 | delete serialports[i]; |
| 1240 | serialports[i] = NULL; |
| 1241 | } |
| 1242 | } |
| 1243 | #endif |
| 1244 | #if C_MODEM |
| 1245 | else if(type=="modem") { |
| 1246 | serialports[i] = new CSerialModem (i, &cmd); |
| 1247 | if (!serialports[i]->InstallationSuccessful) { |
| 1248 | delete serialports[i]; |
| 1249 | serialports[i] = NULL; |
| 1250 | } |
| 1251 | } |
| 1252 | else if(type=="nullmodem") { |
| 1253 | serialports[i] = new CNullModem (i, &cmd); |
| 1254 | if (!serialports[i]->InstallationSuccessful) { |
| 1255 | delete serialports[i]; |
| 1256 | serialports[i] = NULL; |
| 1257 | } |
| 1258 | } |
| 1259 | #endif |
| 1260 | else if(type=="disabled") { |
| 1261 | serialports[i] = NULL; |
| 1262 | } else { |
| 1263 | serialports[i] = NULL; |
| 1264 | LOG_MSG("Invalid type for serial%d",i+1); |
| 1265 | } |
| 1266 | if(serialports[i]) biosParameter[i] = serial_baseaddr[i]; |
| 1267 | } // for 1-4 |
| 1268 | BIOS_SetComPorts (biosParameter); |
| 1269 | } |
| 1270 | |
| 1271 | ~SERIALPORTS () { |
| 1272 | for (Bitu i = 0; i < 4; i++) |
| 1273 | if (serialports[i]) { |
| 1274 | delete serialports[i]; |
| 1275 | serialports[i] = 0; |
| 1276 | } |
| 1277 | } |
| 1278 | }; |
| 1279 | |
| 1280 | static SERIALPORTS *testSerialPortsBaseclass; |
| 1281 | |
| 1282 | void SERIAL_Destroy (Section * sec) { |
| 1283 | delete testSerialPortsBaseclass; |
| 1284 | testSerialPortsBaseclass = NULL; |
| 1285 | } |
| 1286 | |
| 1287 | void SERIAL_Init (Section * sec) { |
| 1288 | // should never happen |
| 1289 | if (testSerialPortsBaseclass) delete testSerialPortsBaseclass; |
| 1290 | testSerialPortsBaseclass = new SERIALPORTS (sec); |
| 1291 | sec->AddDestroyFunction (&SERIAL_Destroy, true); |
| 1292 | } |