src/emu/machine/z80pio.c
| r3446 | r3447 | |
| 189 | 189 | z80pio_t *z80pio = get_safe_token(device); |
| 190 | 190 | UINT8 data = z80pio->output[channel]; |
| 191 | 191 | |
| 192 | if (z80pio->mode[channel] == PIO_MODE_CONTROL) |
| 193 | { |
| 194 | /* mask out input pins */ |
| 195 | data &= ~z80pio->ddr[channel]; |
| 196 | } |
| 197 | |
| 192 | 198 | if (channel == PIO_PORT_A) |
| 193 | 199 | { |
| 194 | 200 | if (z80pio->intf->port_a_w) |
| r3446 | r3447 | |
| 214 | 220 | if (LOG) logerror("Z80PIO Port %c Mode %u\n", 'A' + channel, mode); |
| 215 | 221 | |
| 216 | 222 | /* clear interrupt */ |
| 223 | z80pio->irq_enable[channel] = 0; |
| 217 | 224 | z80pio->irq_pending[channel] = 0; |
| 218 | 225 | z80pio->int_state[channel] = 0; |
| 219 | 226 | z80pio->match[channel] = 0; |
| 220 | 227 | |
| 228 | z80pio_check_interrupt(device); |
| 229 | |
| 221 | 230 | switch (mode) |
| 222 | 231 | { |
| 223 | 232 | case PIO_MODE_OUTPUT: |
| r3446 | r3447 | |
| 320 | 329 | z80pio->ddr[channel] = data; |
| 321 | 330 | z80pio->match[channel] = 0; |
| 322 | 331 | |
| 332 | /* set interrupt enable */ |
| 333 | z80pio->irq_enable[channel] = BIT(z80pio->enable[channel], 7); |
| 334 | z80pio_check_interrupt(device); |
| 335 | |
| 323 | 336 | z80pio->state[channel] = PIO_STATE_NORMAL; |
| 324 | 337 | |
| 325 | 338 | if (LOG) logerror("Z80PIO Port %c Direction %02x\n", 'A' + channel, data); |
| r3446 | r3447 | |
| 449 | 462 | |
| 450 | 463 | for (channel = PIO_PORT_A; channel < PIO_PORT_COUNT; channel++) |
| 451 | 464 | { |
| 452 | | if (z80pio->mode[channel] == PIO_MODE_CONTROL) |
| 465 | if ((z80pio->mode[channel] == PIO_MODE_CONTROL) && (z80pio->mask[channel] != 0xff)) |
| 453 | 466 | { |
| 467 | int bit; |
| 454 | 468 | int match = 0; |
| 469 | |
| 470 | UINT8 ddr = z80pio->ddr[channel]; |
| 471 | UINT8 mask = z80pio->mask[channel]; |
| 472 | UINT8 output = (z80pio->output[channel] & ~ddr); |
| 473 | UINT8 old_data = (z80pio->input[channel] & z80pio->ddr[channel]) | output; |
| 455 | 474 | UINT8 data; |
| 456 | | |
| 457 | | /* input data */ |
| 475 | |
| 458 | 476 | z80pio_port_read(device, channel); |
| 459 | 477 | |
| 460 | | data = z80pio->input[channel] & z80pio->ddr[channel]; |
| 478 | data = (z80pio->input[channel] & ddr) | output; |
| 461 | 479 | |
| 462 | | /* keep only relevant bits */ |
| 463 | | data &= ~z80pio->mask[channel]; |
| 480 | for (bit = 0; bit < 8; bit++) |
| 481 | { |
| 482 | if (!BIT(mask, bit)) |
| 483 | { |
| 484 | if (z80pio->enable[channel] & PIO_IRQ_HIGH_LOW) |
| 485 | { |
| 486 | if (!BIT(old_data, bit) && BIT(data, bit)) // rising edge |
| 487 | { |
| 488 | match = 1; |
| 464 | 489 | |
| 465 | | /* if active low, invert the bits */ |
| 466 | | if (!(z80pio->enable[channel] & PIO_IRQ_HIGH_LOW)) |
| 467 | | data ^= z80pio->mask[channel]; |
| 490 | if (!(z80pio->enable[channel] & PIO_IRQ_AND_OR)) // OR |
| 491 | { |
| 492 | break; |
| 493 | } |
| 494 | } |
| 495 | else |
| 496 | { |
| 497 | if (z80pio->enable[channel] & PIO_IRQ_AND_OR) // AND |
| 498 | { |
| 499 | match = 0; |
| 500 | break; |
| 501 | } |
| 502 | } |
| 503 | } |
| 504 | else |
| 505 | { |
| 506 | if (BIT(old_data, bit) && !BIT(data, bit)) // falling edge |
| 507 | { |
| 508 | match = 1; |
| 468 | 509 | |
| 469 | | /* if AND logic, interrupt if all bits are set */ |
| 470 | | if (z80pio->enable[channel] & PIO_IRQ_AND_OR) |
| 471 | | match = (data == z80pio->mask[channel]); |
| 472 | | /* otherwise, interrupt if at least one bit is set */ |
| 473 | | else |
| 474 | | match = (data != 0); |
| 510 | if (!(z80pio->enable[channel] & PIO_IRQ_AND_OR)) // OR |
| 511 | { |
| 512 | break; |
| 513 | } |
| 514 | } |
| 515 | else |
| 516 | { |
| 517 | if (z80pio->enable[channel] & PIO_IRQ_AND_OR) // AND |
| 518 | { |
| 519 | match = 0; |
| 520 | break; |
| 521 | } |
| 522 | } |
| 523 | } |
| 524 | } |
| 525 | } |
| 475 | 526 | |
| 476 | 527 | if (match) |
| 477 | 528 | { |
| 478 | | if (!z80pio->match[channel]) |
| 479 | | { |
| 480 | | /* trigger interrupt */ |
| 481 | | z80pio->match[channel] = 1; |
| 482 | | z80pio->irq_pending[channel] = 1; |
| 529 | /* trigger interrupt */ |
| 530 | z80pio->irq_pending[channel] = 1; |
| 483 | 531 | |
| 484 | | if (LOG) logerror("Z80PIO Port %c Interrupt Pending\n", 'A' + channel); |
| 532 | if (LOG) logerror("Z80PIO Port %c Interrupt Pending\n", 'A' + channel); |
| 485 | 533 | |
| 486 | | z80pio_check_interrupt(device); |
| 487 | | } |
| 534 | z80pio_check_interrupt(device); |
| 488 | 535 | } |
| 489 | | else |
| 490 | | { |
| 491 | | z80pio->match[channel] = 0; |
| 492 | | } |
| 493 | 536 | } |
| 494 | 537 | } |
| 495 | 538 | } |
| r3446 | r3447 | |
| 723 | 766 | /* allocate poll timers */ |
| 724 | 767 | |
| 725 | 768 | z80pio->poll_timer = timer_alloc(z80pio_poll_tick, (void *)device); |
| 726 | | timer_adjust_periodic(z80pio->poll_timer, attotime_zero, 0, ATTOTIME_IN_HZ(z80pio->clock)); |
| 769 | timer_adjust_periodic(z80pio->poll_timer, attotime_zero, 0, ATTOTIME_IN_HZ(z80pio->clock / 16)); |
| 727 | 770 | |
| 728 | 771 | /* register for state saving */ |
| 729 | 772 | |
| r3446 | r3447 | |
| 764 | 807 | /* set mode 1 */ |
| 765 | 808 | z80pio->mode[channel] = PIO_MODE_INPUT; |
| 766 | 809 | |
| 810 | /* set all inputs */ |
| 811 | z80pio->ddr[channel] = 0xff; |
| 812 | |
| 767 | 813 | /* reset interrupt */ |
| 768 | 814 | z80pio->enable[channel] &= 0x7f; |
| 769 | 815 | z80pio->irq_enable[channel] = 0; |