| 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: directserial.cpp,v 1.2 2009-09-26 09:15:19 h-a-l-9000 Exp $ */ |
| 20 | |
| 21 | #include "dosbox.h" |
| 22 | |
| 23 | #if C_DIRECTSERIAL |
| 24 | |
| 25 | #include "serialport.h" |
| 26 | #include "directserial.h" |
| 27 | #include "misc_util.h" |
| 28 | #include "pic.h" |
| 29 | |
| 30 | #include "libserial.h" |
| 31 | |
| 32 | /* This is a serial passthrough class. Its amazingly simple to */ |
| 33 | /* write now that the serial ports themselves were abstracted out */ |
| 34 | |
| 35 | CDirectSerial::CDirectSerial (Bitu id, CommandLine* cmd) |
| 36 | :CSerial (id, cmd) { |
| 37 | InstallationSuccessful = false; |
| 38 | comport = 0; |
| 39 | |
| 40 | rx_retry = 0; |
| 41 | rx_retry_max = 0; |
| 42 | |
| 43 | std::string tmpstring; |
| 44 | if(!cmd->FindStringBegin("realport:",tmpstring,false)) return; |
| 45 | |
| 46 | LOG_MSG ("Serial%d: Opening %s", COMNUMBER, tmpstring.c_str()); |
| 47 | if(!SERIAL_open(tmpstring.c_str(), &comport)) { |
| 48 | char errorbuffer[256]; |
| 49 | SERIAL_getErrorString(errorbuffer, sizeof(errorbuffer)); |
| 50 | LOG_MSG("Serial%d: Serial Port \"%s\" could not be opened.", |
| 51 | COMNUMBER, tmpstring.c_str()); |
| 52 | LOG_MSG("%s",errorbuffer); |
| 53 | return; |
| 54 | } |
| 55 | |
| 56 | #if SERIAL_DEBUG |
| 57 | dbgmsg_poll_block=false; |
| 58 | dbgmsg_rx_block=false; |
| 59 | #endif |
| 60 | |
| 61 | // rxdelay: How many milliseconds to wait before causing an |
| 62 | // overflow when the application is unresponsive. |
| 63 | if(getBituSubstring("rxdelay:", &rx_retry_max, cmd)) { |
| 64 | if(!(rx_retry_max<=10000)) { |
| 65 | rx_retry_max=0; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | CSerial::Init_Registers(); |
| 70 | InstallationSuccessful = true; |
| 71 | rx_state = D_RX_IDLE; |
| 72 | setEvent(SERIAL_POLLING_EVENT, 1); // millisecond receive tick |
| 73 | } |
| 74 | |
| 75 | CDirectSerial::~CDirectSerial () { |
| 76 | if(comport) SERIAL_close(comport); |
| 77 | // We do not use own events so we don't have to clear them. |
| 78 | } |
| 79 | |
| 80 | // CanReceive: true:UART part has room left |
| 81 | // doReceive: true:there was really a byte to receive |
| 82 | // rx_retry is incremented in polling events |
| 83 | |
| 84 | // in POLLING_EVENT: always add new polling event |
| 85 | // D_RX_IDLE + CanReceive + doReceive -> D_RX_WAIT , add RX_EVENT |
| 86 | // D_RX_IDLE + CanReceive + not doReceive -> D_RX_IDLE |
| 87 | // D_RX_IDLE + not CanReceive -> D_RX_BLOCKED, add RX_EVENT |
| 88 | |
| 89 | // D_RX_BLOCKED + CanReceive + doReceive -> D_RX_FASTWAIT, rem RX_EVENT |
| 90 | // rx_retry=0 , add RX_EVENT |
| 91 | // D_RX_BLOCKED + CanReceive + !doReceive -> D_RX_IDLE, rem RX_EVENT |
| 92 | // rx_retry=0 |
| 93 | // D_RX_BLOCKED + !CanReceive + doReceive + retry < max -> D_RX_BLOCKED, rx_retry++ |
| 94 | // D_RX_BLOCKED + !CanReceive + doReceive + retry >=max -> rx_retry=0 |
| 95 | |
| 96 | // to be continued... |
| 97 | |
| 98 | void CDirectSerial::handleUpperEvent(Bit16u type) { |
| 99 | switch(type) { |
| 100 | case SERIAL_POLLING_EVENT: { |
| 101 | setEvent(SERIAL_POLLING_EVENT, 1.0f); |
| 102 | // update Modem input line states |
| 103 | switch(rx_state) { |
| 104 | case D_RX_IDLE: |
| 105 | if(CanReceiveByte()) { |
| 106 | if(doReceive()) { |
| 107 | // a byte was received |
| 108 | rx_state=D_RX_WAIT; |
| 109 | setEvent(SERIAL_RX_EVENT, bytetime*0.9f); |
| 110 | } // else still idle |
| 111 | } else { |
| 112 | #if SERIAL_DEBUG |
| 113 | if(!dbgmsg_poll_block) { |
| 114 | log_ser(dbg_aux,"Directserial: block on polling."); |
| 115 | dbgmsg_poll_block=true; |
| 116 | } |
| 117 | #endif |
| 118 | rx_state=D_RX_BLOCKED; |
| 119 | // have both delays (1ms + bytetime) |
| 120 | setEvent(SERIAL_RX_EVENT, bytetime*0.9f); |
| 121 | } |
| 122 | break; |
| 123 | case D_RX_BLOCKED: |
| 124 | // one timeout tick |
| 125 | if(!CanReceiveByte()) { |
| 126 | rx_retry++; |
| 127 | if(rx_retry>=rx_retry_max) { |
| 128 | // it has timed out: |
| 129 | rx_retry=0; |
| 130 | removeEvent(SERIAL_RX_EVENT); |
| 131 | if(doReceive()) { |
| 132 | // read away everything |
| 133 | // this will set overrun errors |
| 134 | while(doReceive()); |
| 135 | rx_state=D_RX_WAIT; |
| 136 | setEvent(SERIAL_RX_EVENT, bytetime*0.9f); |
| 137 | } else { |
| 138 | // much trouble about nothing |
| 139 | rx_state=D_RX_IDLE; |
| 140 | } |
| 141 | } // else wait further |
| 142 | } else { |
| 143 | // good: we can receive again |
| 144 | #if SERIAL_DEBUG |
| 145 | dbgmsg_poll_block=false; |
| 146 | dbgmsg_rx_block=false; |
| 147 | #endif |
| 148 | removeEvent(SERIAL_RX_EVENT); |
| 149 | rx_retry=0; |
| 150 | if(doReceive()) { |
| 151 | rx_state=D_RX_FASTWAIT; |
| 152 | setEvent(SERIAL_RX_EVENT, bytetime*0.65f); |
| 153 | } else { |
| 154 | // much trouble about nothing |
| 155 | rx_state=D_RX_IDLE; |
| 156 | } |
| 157 | } |
| 158 | break; |
| 159 | |
| 160 | case D_RX_WAIT: |
| 161 | case D_RX_FASTWAIT: |
| 162 | break; |
| 163 | } |
| 164 | updateMSR(); |
| 165 | break; |
| 166 | } |
| 167 | case SERIAL_RX_EVENT: { |
| 168 | switch(rx_state) { |
| 169 | case D_RX_IDLE: |
| 170 | LOG_MSG("internal error in directserial"); |
| 171 | break; |
| 172 | |
| 173 | case D_RX_BLOCKED: // try to receive |
| 174 | case D_RX_WAIT: |
| 175 | case D_RX_FASTWAIT: |
| 176 | if(CanReceiveByte()) { |
| 177 | // just works or unblocked |
| 178 | rx_retry=0; // not waiting anymore |
| 179 | if(doReceive()) { |
| 180 | if(rx_state==D_RX_WAIT) setEvent(SERIAL_RX_EVENT, bytetime*0.9f); |
| 181 | else { |
| 182 | // maybe unblocked |
| 183 | rx_state=D_RX_FASTWAIT; |
| 184 | setEvent(SERIAL_RX_EVENT, bytetime*0.65f); |
| 185 | } |
| 186 | } else { |
| 187 | // didn't receive anything |
| 188 | rx_state=D_RX_IDLE; |
| 189 | } |
| 190 | } else { |
| 191 | // blocking now or still blocked |
| 192 | #if SERIAL_DEBUG |
| 193 | if(rx_state==D_RX_BLOCKED) { |
| 194 | if(!dbgmsg_rx_block) { |
| 195 | log_ser(dbg_aux,"Directserial: rx still blocked (retry=%d)",rx_retry); |
| 196 | dbgmsg_rx_block=true; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | else log_ser(dbg_aux,"Directserial: block on continued rx (retry=%d).",rx_retry); |
| 206 | #endif |
| 207 | setEvent(SERIAL_RX_EVENT, bytetime*0.65f); |
| 208 | rx_state=D_RX_BLOCKED; |
| 209 | } |
| 210 | |
| 211 | break; |
| 212 | } |
| 213 | updateMSR(); |
| 214 | break; |
| 215 | } |
| 216 | case SERIAL_TX_EVENT: { |
| 217 | // Maybe echo cirquit works a bit better this way |
| 218 | if(rx_state==D_RX_IDLE && CanReceiveByte()) { |
| 219 | if(doReceive()) { |
| 220 | // a byte was received |
| 221 | rx_state=D_RX_WAIT; |
| 222 | setEvent(SERIAL_RX_EVENT, bytetime*0.9f); |
| 223 | } |
| 224 | } |
| 225 | ByteTransmitted(); |
| 226 | updateMSR(); |
| 227 | break; |
| 228 | } |
| 229 | case SERIAL_THR_EVENT: { |
| 230 | ByteTransmitting(); |
| 231 | setEvent(SERIAL_TX_EVENT,bytetime*1.1f); |
| 232 | break; |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | bool CDirectSerial::doReceive() { |
| 238 | int value = SERIAL_getextchar(comport); |
| 239 | if(value) { |
| 240 | receiveByteEx((Bit8u)(value&0xff),(Bit8u)((value&0xff00)>>8)); |
| 241 | return true; |
| 242 | } |
| 243 | return false; |
| 244 | } |
| 245 | |
| 246 | // updatePortConfig is called when emulated app changes the serial port |
| 247 | // parameters baudrate, stopbits, number of databits, parity. |
| 248 | void CDirectSerial::updatePortConfig (Bit16u divider, Bit8u lcr) { |
| 249 | Bit8u parity = 0; |
| 250 | |
| 251 | switch ((lcr & 0x38)>>3) { |
| 252 | case 0x1: parity='o'; break; |
| 253 | case 0x3: parity='e'; break; |
| 254 | case 0x5: parity='m'; break; |
| 255 | case 0x7: parity='s'; break; |
| 256 | default: parity='n'; break; |
| 257 | } |
| 258 | |
| 259 | Bit8u bytelength = (lcr & 0x3)+5; |
| 260 | |
| 261 | // baudrate |
| 262 | Bitu baudrate; |
| 263 | if(divider==0) baudrate=115200; |
| 264 | else baudrate = 115200 / divider; |
| 265 | |
| 266 | // stopbits |
| 267 | Bit8u stopbits; |
| 268 | if (lcr & 0x4) { |
| 269 | if (bytelength == 5) stopbits = SERIAL_15STOP; |
| 270 | else stopbits = SERIAL_2STOP; |
| 271 | } else stopbits = SERIAL_1STOP; |
| 272 | |
| 273 | if(!SERIAL_setCommParameters(comport, baudrate, parity, stopbits, bytelength)) { |
| 274 | #if SERIAL_DEBUG |
| 275 | log_ser(dbg_aux,"Serial port settings not supported by host." ); |
| 276 | #endif |
| 277 | LOG_MSG ("Serial%d: Desired serial mode not supported (%d,%d,%c,%d)", |
| 278 | COMNUMBER, baudrate,bytelength,parity,stopbits); |
| 279 | } |
| 280 | CDirectSerial::setRTSDTR(getRTS(), getDTR()); |
| 281 | } |
| 282 | |
| 283 | void CDirectSerial::updateMSR () { |
| 284 | int new_status = SERIAL_getmodemstatus(comport); |
| 285 | |
| 286 | setCTS(new_status&SERIAL_CTS? true:false); |
| 287 | setDSR(new_status&SERIAL_DSR? true:false); |
| 288 | setRI(new_status&SERIAL_RI? true:false); |
| 289 | setCD(new_status&SERIAL_CD? true:false); |
| 290 | } |
| 291 | |
| 292 | void CDirectSerial::transmitByte (Bit8u val, bool first) { |
| 293 | if(!SERIAL_sendchar(comport, val)) |
| 294 | LOG_MSG("Serial%d: COM port error: write failed!", COMNUMBER); |
| 295 | if(first) setEvent(SERIAL_THR_EVENT, bytetime/8); |
| 296 | else setEvent(SERIAL_TX_EVENT, bytetime); |
| 297 | } |
| 298 | |
| 299 | |
| 300 | // setBreak(val) switches break on or off |
| 301 | void CDirectSerial::setBreak (bool value) { |
| 302 | SERIAL_setBREAK(comport,value); |
| 303 | } |
| 304 | |
| 305 | // updateModemControlLines(mcr) sets DTR and RTS. |
| 306 | void CDirectSerial::setRTSDTR(bool rts, bool dtr) { |
| 307 | SERIAL_setRTS(comport,rts); |
| 308 | SERIAL_setDTR(comport,dtr); |
| 309 | } |
| 310 | |
| 311 | void CDirectSerial::setRTS(bool val) { |
| 312 | SERIAL_setRTS(comport,val); |
| 313 | } |
| 314 | |
| 315 | void CDirectSerial::setDTR(bool val) { |
| 316 | SERIAL_setDTR(comport,val); |
| 317 | } |
| 318 | |
| 319 | #endif |