src/emu/machine/68681.c
| r5571 | r5572 | |
| 2 | 2 | 68681 DUART |
| 3 | 3 | |
| 4 | 4 | Written by Mariusz Wojcieszek |
| 5 | Updated by Jonathan Gevaryahu AKA Lord Nightmare |
| 5 | 6 | */ |
| 6 | 7 | |
| 7 | 8 | #include "driver.h" |
| r5571 | r5572 | |
| 38 | 39 | #define STATUS_FIFO_FULL 0x02 |
| 39 | 40 | #define STATUS_RECEIVER_READY 0x01 |
| 40 | 41 | |
| 41 | | #define MODE_RX_INT_SELECT_BIT 6 |
| 42 | #define MODE_RX_INT_SELECT_BIT 0x40 |
| 42 | 43 | |
| 43 | 44 | #define RX_FIFO_SIZE 3 |
| 44 | 45 | |
| r5571 | r5572 | |
| 109 | 110 | |
| 110 | 111 | static void duart68681_update_interrupts(duart68681_state *duart68681) |
| 111 | 112 | { |
| 113 | /* update SR state and update interrupt ISR state for the following bits: |
| 114 | SRn: bits 7-4: handled elsewhere. |
| 115 | SRn: bit 3 (TxEMTn) (we can assume since we're not actually emulating the delay/timing of sending bits, that as long as TxRDYn is set, TxEMTn is also set since the transmit byte has 'already happened', therefore TxEMTn is always 1 assuming tx is enabled on channel n and the MSR2n mode is 0 or 2; in mode 1 it is explicitly zeroed, and mode 3 is undefined) |
| 116 | SRn: bit 2 (TxRDYn) (we COULD assume since we're not emulating delay and timing output, that as long as tx is enabled on channel n, TxRDY is 1 for channel n and the MSR2n mode is 0 or 2; in mode 1 it is explicitly zeroed, and mode 3 is undefined; however, tx_ready is already nicely handled for us elsewhere, so we can use that instead for now, though we may need to retool that code as well) |
| 117 | SRn: bit 1 (FFULLn) (this bit we actually emulate; if the recieve fifo for channel n is full, this bit is 1, otherwise it is 0. the recieve fifo should be three words long.) |
| 118 | SRn: bit 0 (RxRDYn) (this bit we also emulate; the bit is always asserted if the recieve fifo is not empty) |
| 119 | ISR: bit 7: Input Port change; this should be handled elsewhere, on the input port handler |
| 120 | ISR: bit 6: Delta Break B; this should be handled elsewhere, on the data recieve handler |
| 121 | ISR: bit 5: RxRDYB/FFULLB: this is handled here; depending on whether MSR1B bit 6 is 0 or 1, this bit holds the state of SRB bit 0 or bit 1 respectively |
| 122 | ISR: bit 4: TxRDYB: this is handled here; it mirrors SRB bit 2 |
| 123 | ISR: bit 3: Counter ready; this should be handled by the timer generator |
| 124 | ISR: bit 2: Delta Break A; this should be handled elsewhere, on the data recieve handler |
| 125 | ISR: bit 1: RxRDYA/FFULLA: this is handled here; depending on whether MSR1A bit 6 is 0 or 1, this bit holds the state of SRA bit 0 or bit 1 respectively |
| 126 | ISR: bit 0: TxRDYA: this is handled here; it mirrors SRA bit 2 |
| 127 | */ |
| 128 | UINT8 ch = 0; |
| 129 | //logerror("DEBUG: 68681 int check: upon func call, SRA is %02X, SRB is %02X, ISR is %02X\n", duart68681->channel[0].SR, duart68681->channel[1].SR, duart68681->ISR); |
| 130 | for (ch = 0; ch < 2; ch++) |
| 131 | { |
| 132 | //if ( duart68681->channel[ch].rx_enabled ) |
| 133 | //{ |
| 134 | if ( duart68681->channel[ch].rx_fifo_num > 0 ) |
| 135 | { |
| 136 | duart68681->channel[ch].SR |= STATUS_RECEIVER_READY; |
| 137 | } |
| 138 | else |
| 139 | { |
| 140 | duart68681->channel[ch].SR &= ~STATUS_RECEIVER_READY; |
| 141 | } |
| 142 | if ( duart68681->channel[ch].rx_fifo_num == RX_FIFO_SIZE ) |
| 143 | { |
| 144 | duart68681->channel[ch].SR |= STATUS_FIFO_FULL; |
| 145 | } |
| 146 | else |
| 147 | { |
| 148 | duart68681->channel[ch].SR &= ~STATUS_FIFO_FULL; |
| 149 | } |
| 150 | //} |
| 151 | //else |
| 152 | //{ |
| 153 | //duart68681->channel[ch].SR &= ~STATUS_RECEIVER_READY; |
| 154 | //duart68681->channel[ch].SR &= ~STATUS_FIFO_FULL; |
| 155 | //} |
| 156 | // Handle the TxEMT and TxRDY bits based on mode |
| 157 | switch( duart68681->channel[ch].MR2&0xC0) // what mode are we in? |
| 158 | { |
| 159 | case 0x00: // normal mode |
| 160 | if ( duart68681->channel[ch].tx_enabled ) |
| 161 | { |
| 162 | duart68681->channel[ch].SR |= STATUS_TRANSMITTER_EMPTY; |
| 163 | } |
| 164 | else |
| 165 | { |
| 166 | duart68681->channel[ch].SR &= ~STATUS_TRANSMITTER_EMPTY; |
| 167 | } |
| 168 | break; |
| 169 | case 0x40: // automatic echo mode |
| 170 | duart68681->channel[ch].SR &= ~STATUS_TRANSMITTER_EMPTY; |
| 171 | duart68681->channel[ch].SR &= ~STATUS_TRANSMITTER_READY; |
| 172 | break; |
| 173 | case 0x80: // local loopback mode |
| 174 | if ( duart68681->channel[ch].tx_enabled ) |
| 175 | { |
| 176 | duart68681->channel[ch].SR |= STATUS_TRANSMITTER_EMPTY; |
| 177 | } |
| 178 | else |
| 179 | { |
| 180 | duart68681->channel[ch].SR &= ~STATUS_TRANSMITTER_EMPTY; |
| 181 | } |
| 182 | break; |
| 183 | case 0xC0: // remote loopback mode |
| 184 | // write me, what the txrdy/txemt regs do for remote loopback mode is undocumented afaik, for now just clear both |
| 185 | duart68681->channel[ch].SR &= ~STATUS_TRANSMITTER_EMPTY; |
| 186 | duart68681->channel[ch].SR &= ~STATUS_TRANSMITTER_READY; |
| 187 | break; |
| 188 | } |
| 189 | // now handle the ISR bits |
| 190 | if ( duart68681->channel[ch].SR & STATUS_TRANSMITTER_READY ) |
| 191 | { |
| 192 | if (ch == 0) |
| 193 | duart68681->ISR |= INT_TXRDYA; |
| 194 | else |
| 195 | duart68681->ISR |= INT_TXRDYB; |
| 196 | } |
| 197 | else |
| 198 | { |
| 199 | if (ch == 0) |
| 200 | duart68681->ISR &= ~INT_TXRDYA; |
| 201 | else |
| 202 | duart68681->ISR &= ~INT_TXRDYB; |
| 203 | } |
| 204 | //logerror("DEBUG: 68681 int check: before reciever test, SR%c is %02X, ISR is %02X\n", (ch+0x41), duart68681->channel[ch].SR, duart68681->ISR); |
| 205 | if ( duart68681->channel[ch].MR1 & MODE_RX_INT_SELECT_BIT ) |
| 206 | { |
| 207 | if ( duart68681->channel[ch].SR & STATUS_FIFO_FULL ) |
| 208 | { |
| 209 | duart68681->ISR |= ((ch == 0) ? INT_RXRDY_FFULLA : INT_RXRDY_FFULLB); |
| 210 | } |
| 211 | else |
| 212 | { |
| 213 | duart68681->ISR &= ((ch == 0) ? ~INT_RXRDY_FFULLA : ~INT_RXRDY_FFULLB); |
| 214 | } |
| 215 | } |
| 216 | else |
| 217 | { |
| 218 | if ( duart68681->channel[ch].SR & STATUS_RECEIVER_READY ) |
| 219 | { |
| 220 | duart68681->ISR |= ((ch == 0) ? INT_RXRDY_FFULLA : INT_RXRDY_FFULLB); |
| 221 | } |
| 222 | else |
| 223 | { |
| 224 | duart68681->ISR &= ((ch == 0) ? ~INT_RXRDY_FFULLA : ~INT_RXRDY_FFULLB); |
| 225 | } |
| 226 | } |
| 227 | //logerror("DEBUG: 68681 int check: after reciever test, SR%c is %02X, ISR is %02X\n", (ch+0x41), duart68681->channel[ch].SR, duart68681->ISR); |
| 228 | } |
| 112 | 229 | if ( (duart68681->ISR & duart68681->IMR) != 0 ) |
| 113 | 230 | { |
| 114 | 231 | if ( duart68681->duart_config->irq_handler ) |
| 115 | 232 | { |
| 233 | LOG(( "68681: Interrupt line active (IMR & ISR = %02X)\n", (duart68681->ISR & duart68681->IMR) )); |
| 116 | 234 | duart68681->duart_config->irq_handler( duart68681->device, duart68681->IVR ); |
| 117 | 235 | } |
| 118 | 236 | } |
| r5571 | r5572 | |
| 138 | 256 | { |
| 139 | 257 | duart68681->channel[ch].MR2 = data; |
| 140 | 258 | } |
| 259 | duart68681_update_interrupts(duart68681); |
| 141 | 260 | }; |
| 142 | 261 | |
| 143 | 262 | static void duart68681_write_CSR(duart68681_state *duart68681, int ch, UINT8 data, UINT8 ACR) |
| r5571 | r5572 | |
| 171 | 290 | if ( BIT(data,1) ) |
| 172 | 291 | { |
| 173 | 292 | duart68681->channel[ch].rx_enabled = 0; |
| 174 | | duart68681->channel[ch].SR &= ~STATUS_RECEIVER_READY; |
| 293 | duart68681->channel[ch].SR &= ~STATUS_RECEIVER_READY; // datasheet explicitly says disabling rx does not affect the reciever ready line, which just indicates if the fifo has anything in it at all. |
| 175 | 294 | } |
| 176 | 295 | |
| 177 | 296 | if ( BIT(data,2) ) |
| r5571 | r5572 | |
| 179 | 298 | duart68681->channel[ch].tx_enabled = 1; |
| 180 | 299 | duart68681->channel[ch].tx_ready = 1; |
| 181 | 300 | duart68681->channel[ch].SR |= STATUS_TRANSMITTER_READY; |
| 301 | duart68681->channel[ch].SR |= STATUS_TRANSMITTER_EMPTY; |
| 182 | 302 | } |
| 183 | 303 | if ( BIT(data,3) ) |
| 184 | 304 | { |
| 185 | 305 | duart68681->channel[ch].tx_enabled = 0; |
| 186 | 306 | duart68681->channel[ch].tx_ready = 0; |
| 187 | 307 | duart68681->channel[ch].SR &= ~STATUS_TRANSMITTER_READY; |
| 308 | duart68681->channel[ch].SR &= ~STATUS_TRANSMITTER_EMPTY; |
| 188 | 309 | } |
| 189 | 310 | |
| 190 | 311 | switch( (data >> 4) & 0x07 ) |
| r5571 | r5572 | |
| 197 | 318 | case 2: /* Reset channel A receiver (disable receiver and flush fifo) */ |
| 198 | 319 | duart68681->channel[ch].rx_enabled = 0; |
| 199 | 320 | duart68681->channel[ch].SR &= ~STATUS_RECEIVER_READY; |
| 321 | duart68681->channel[ch].SR &= ~STATUS_OVERRUN_ERROR; // is this correct? |
| 200 | 322 | duart68681->channel[ch].rx_fifo_read_ptr = 0; |
| 201 | 323 | duart68681->channel[ch].rx_fifo_write_ptr = 0; |
| 202 | 324 | duart68681->channel[ch].rx_fifo_num = 0; |
| r5571 | r5572 | |
| 218 | 340 | { |
| 219 | 341 | duart68681->ISR &= ~INT_DELTA_BREAK_B; |
| 220 | 342 | } |
| 221 | | duart68681_update_interrupts(duart68681); |
| 222 | 343 | break; |
| 344 | /* TODO: case 6 and case 7 are start break and stop break respectively, which start or stop holding the TxDA or TxDB line low (space) after whatever data is in the buffer finishes transmitting (following the stop bit?), or after two bit-times if no data is being transmitted */ |
| 223 | 345 | default: |
| 224 | 346 | LOG(( "68681: Unhandled command (%x) in CR%d\n", (data >> 4) & 0x07, ch )); |
| 225 | 347 | break; |
| 226 | 348 | } |
| 349 | duart68681_update_interrupts(duart68681); |
| 227 | 350 | }; |
| 228 | 351 | |
| 229 | 352 | static UINT8 duart68681_read_rx_fifo(duart68681_state *duart68681, int ch) |
| r5571 | r5572 | |
| 243 | 366 | } |
| 244 | 367 | |
| 245 | 368 | duart68681->channel[ch].rx_fifo_num--; |
| 246 | | |
| 247 | | if ( duart68681->channel[ch].rx_fifo_num > 0 ) |
| 248 | | { |
| 249 | | duart68681->channel[ch].SR |= STATUS_RECEIVER_READY; |
| 250 | | if ( ch == 0 ) |
| 251 | | { |
| 252 | | duart68681->ISR |= INT_RXRDY_FFULLA; |
| 253 | | } |
| 254 | | else |
| 255 | | { |
| 256 | | duart68681->ISR |= INT_RXRDY_FFULLB; |
| 257 | | } |
| 258 | | } |
| 259 | | else |
| 260 | | { |
| 261 | | duart68681->channel[ch].SR &= ~STATUS_RECEIVER_READY; |
| 262 | | if ( ch == 0 ) |
| 263 | | { |
| 264 | | duart68681->ISR &= ~INT_RXRDY_FFULLA; |
| 265 | | } |
| 266 | | else |
| 267 | | { |
| 268 | | duart68681->ISR &= ~INT_RXRDY_FFULLB; |
| 269 | | } |
| 270 | | } |
| 271 | 369 | duart68681_update_interrupts(duart68681); |
| 272 | 370 | |
| 273 | 371 | return r; |
| r5571 | r5572 | |
| 312 | 410 | |
| 313 | 411 | period = ATTOTIME_IN_HZ(duart68681->channel[ch].baud_rate / 10 ); |
| 314 | 412 | timer_adjust_oneshot(duart68681->channel[ch].tx_timer, period, ch); |
| 413 | |
| 414 | // if local loopback is on, write the transmitted data as if a byte had been recieved |
| 415 | if ((duart68681->channel[ch].MR2&0xC0) == 0x80) |
| 416 | { |
| 417 | if ( duart68681->channel[ch].rx_fifo_num >= RX_FIFO_SIZE ) |
| 418 | { |
| 419 | LOG(( "68681: FIFO overflow\n" )); |
| 420 | duart68681->channel[ch].SR |= STATUS_OVERRUN_ERROR; |
| 421 | return; |
| 422 | } |
| 423 | duart68681->channel[ch].rx_fifo[duart68681->channel[ch].rx_fifo_write_ptr++] = data; |
| 424 | if ( duart68681->channel[ch].rx_fifo_write_ptr == RX_FIFO_SIZE ) |
| 425 | { |
| 426 | duart68681->channel[ch].rx_fifo_write_ptr = 0; |
| 427 | } |
| 428 | duart68681->channel[ch].rx_fifo_num++; |
| 429 | duart68681_update_interrupts(duart68681); |
| 430 | } |
| 315 | 431 | }; |
| 316 | 432 | |
| 317 | 433 | READ8_DEVICE_HANDLER(duart68681_r) |
| r5571 | r5572 | |
| 397 | 513 | case 0x0e: /* Start counter command */ |
| 398 | 514 | switch( (duart68681->ACR >> 4) & 0x07 ) |
| 399 | 515 | { |
| 516 | /* TODO: implement modes 0,1,2,4,5 */ |
| 517 | case 0x03: /* Counter, CLK/16 */ |
| 518 | { |
| 519 | attotime rate = ATTOTIME_IN_HZ(2*device->clock/(2*16*16*duart68681->CTR.w.l)); |
| 520 | timer_adjust_periodic(duart68681->duart_timer, rate, 0, rate); |
| 521 | } |
| 522 | break; |
| 523 | case 0x06: /* Timer, CLK/1 */ |
| 524 | { |
| 525 | attotime rate = ATTOTIME_IN_HZ(2*device->clock/(2*16*duart68681->CTR.w.l)); |
| 526 | timer_adjust_periodic(duart68681->duart_timer, rate, 0, rate); |
| 527 | } |
| 528 | break; |
| 400 | 529 | case 0x07: /* Timer, CLK/16 */ |
| 401 | 530 | { |
| 402 | 531 | //double hz; |
| r5571 | r5572 | |
| 410 | 539 | break; |
| 411 | 540 | case 0x0f: /* Stop counter command */ |
| 412 | 541 | duart68681->ISR &= ~INT_COUNTER_READY; |
| 542 | if (((duart68681->ACR >>4)& 0x07) < 4) // if in counter mode... |
| 543 | timer_adjust_oneshot(duart68681->duart_timer, attotime_never, 0); // shut down timer |
| 413 | 544 | duart68681_update_interrupts(duart68681); |
| 414 | 545 | break; |
| 415 | 546 | default: |
| r5571 | r5572 | |
| 446 | 577 | duart68681->ACR = data; |
| 447 | 578 | // bits 6-4: Counter/Timer Mode And Clock Source Select |
| 448 | 579 | // bits 3-0: IP3-0 Change-Of-State Interrupt Enable |
| 449 | | if (((data >> 4) & 0x07) != 0x07) |
| 580 | switch ((data >> 4) & 0x07) |
| 581 | { |
| 582 | case 0: case 1: case 2: case 4: case 5: // TODO: handle these cases! |
| 450 | 583 | logerror( "68681 (%s): Unhandled timer/counter mode %d\n", device->tag, (data >> 4) & 0x07); |
| 451 | | |
| 584 | break; |
| 585 | case 3: case 6: case 7: |
| 586 | break; |
| 587 | } |
| 452 | 588 | duart68681_write_CSR(duart68681, 0, duart68681->channel[0].CSR, data); |
| 453 | 589 | duart68681_write_CSR(duart68681, 1, duart68681->channel[1].CSR, data); |
| 590 | duart68681_update_interrupts(duart68681); // need to add ACR checking for IP delta ints |
| 454 | 591 | break; |
| 455 | 592 | case 0x05: /* IMR */ |
| 456 | 593 | duart68681->IMR = data; |
| r5571 | r5572 | |
| 504 | 641 | if ( duart68681->channel[ch].rx_fifo_num >= RX_FIFO_SIZE ) |
| 505 | 642 | { |
| 506 | 643 | LOG(( "68681: FIFO overflow\n" )); |
| 644 | duart68681->channel[ch].SR |= STATUS_OVERRUN_ERROR; |
| 507 | 645 | return; |
| 508 | 646 | } |
| 509 | 647 | duart68681->channel[ch].rx_fifo[duart68681->channel[ch].rx_fifo_write_ptr++] = data; |
| r5571 | r5572 | |
| 512 | 650 | duart68681->channel[ch].rx_fifo_write_ptr = 0; |
| 513 | 651 | } |
| 514 | 652 | duart68681->channel[ch].rx_fifo_num++; |
| 515 | | |
| 516 | | duart68681->channel[ch].SR |= STATUS_RECEIVER_READY; |
| 517 | | if ( BIT(duart68681->channel[ch].MR1,MODE_RX_INT_SELECT_BIT) == 0 ) |
| 518 | | { |
| 519 | | duart68681->ISR |= (ch == 0) ? INT_RXRDY_FFULLA : INT_RXRDY_FFULLB; |
| 520 | | } |
| 521 | | if ( duart68681->channel[ch].rx_fifo_num == RX_FIFO_SIZE ) |
| 522 | | { |
| 523 | | duart68681->channel[ch].SR |= STATUS_FIFO_FULL; |
| 524 | | if ( BIT(duart68681->channel[ch].MR1,MODE_RX_INT_SELECT_BIT) == 1 ) |
| 525 | | { |
| 526 | | duart68681->ISR |= (ch == 0) ? INT_RXRDY_FFULLA : INT_RXRDY_FFULLB; |
| 527 | | } |
| 528 | | } |
| 529 | 653 | duart68681_update_interrupts(duart68681); |
| 530 | 654 | } |
| 531 | 655 | }; |
| r5571 | r5572 | |
| 594 | 718 | memset(duart68681, 0, sizeof(duart68681_state)); |
| 595 | 719 | duart68681->duart_config = (const duart68681_config *)device->static_config; |
| 596 | 720 | duart68681->device = device; |
| 597 | | duart68681->IVR = 0x0f; |
| 721 | duart68681->IVR = 0x0f; /* Interrupt Vector Register */ |
| 722 | // "reset clears internal registers (SRA, SRB, IMR, ISR, OPR, OPCR) puts OP0-7 in the high state, stops the counter/timer, and puts channels a/b in the inactive state" |
| 723 | duart68681->channel[0].SR = 0; |
| 724 | duart68681->channel[1].SR = 0; |
| 725 | duart68681->IMR = 0; /* Interrupt Mask Register */ |
| 726 | duart68681->ISR = 0; /* Interrupt Status Register */ |
| 727 | duart68681->OPCR = 0; /* Output Port Conf. Register */ |
| 728 | duart68681->OPR = 0; /* Output Port Register */ |
| 729 | if (duart68681->duart_config->output_port_write) |
| 730 | duart68681->duart_config->output_port_write(duart68681->device, duart68681->OPR ^ 0xff); |
| 598 | 731 | |
| 599 | 732 | // allocate timers |
| 600 | 733 | duart68681->channel[0].tx_timer = timer_alloc(device->machine, tx_timer_callback, (void*)device); |