src/emu/machine/z80pio.c
| r3449 | r3450 | |
| 1 | 1 | /*************************************************************************** |
| 2 | 2 | |
| 3 | | Z80 PIO implementation |
| 3 | Z80 PIO Parallel Input/Output Controller emulation |
| 4 | 4 | |
| 5 | | based on original version (c) 1997, Tatsuyuki Satoh |
| 6 | | |
| 7 | 5 | Copyright Nicola Salmoria and the MAME Team. |
| 8 | 6 | Visit http://mamedev.org for licensing and usage restrictions. |
| 9 | 7 | |
| r3449 | r3450 | |
| 18 | 16 | DEBUGGING |
| 19 | 17 | ***************************************************************************/ |
| 20 | 18 | |
| 21 | | #define LOG 0 |
| 19 | #define VERBOSE 0 |
| 22 | 20 | |
| 21 | #define LOGERROR if (VERBOSE) logerror |
| 22 | |
| 23 | 23 | /*************************************************************************** |
| 24 | 24 | CONSTANTS |
| 25 | 25 | ***************************************************************************/ |
| r3449 | r3450 | |
| 46 | 46 | PIO_PORT_COUNT |
| 47 | 47 | }; |
| 48 | 48 | |
| 49 | #define PIO_MODE_CONTROL_WORD 0x0f |
| 50 | #define PIO_IRQ_CONTROL_WORD 0x07 |
| 51 | #define PIO_IRQ_DISABLE_WORD 0x03 |
| 52 | |
| 49 | 53 | #define PIO_IRQ_MASK_FOLLOWS 0x10 |
| 50 | | #define PIO_IRQ_HIGH_LOW 0x20 |
| 51 | | #define PIO_IRQ_AND_OR 0x40 |
| 54 | #define PIO_IRQ_ACTIVE_HIGH 0x20 |
| 55 | #define PIO_IRQ_AND 0x40 |
| 52 | 56 | #define PIO_IRQ_ENABLE 0x80 |
| 53 | 57 | |
| 54 | 58 | /*************************************************************************** |
| r3449 | r3450 | |
| 65 | 69 | int state[PIO_PORT_COUNT]; /* control register state */ |
| 66 | 70 | int mode[PIO_PORT_COUNT]; /* mode control register */ |
| 67 | 71 | int irq_enable[PIO_PORT_COUNT]; /* interrupt enable flag */ |
| 68 | | int irq_pending[PIO_PORT_COUNT]; /* interrupt pending */ |
| 69 | 72 | int int_state[PIO_PORT_COUNT]; /* interrupt status */ |
| 70 | 73 | UINT8 enable[PIO_PORT_COUNT]; /* interrupt control word */ |
| 71 | 74 | UINT8 input[PIO_PORT_COUNT]; /* data input register */ |
| r3449 | r3450 | |
| 74 | 77 | UINT8 mask[PIO_PORT_COUNT]; /* mask register */ |
| 75 | 78 | UINT8 ddr[PIO_PORT_COUNT]; /* input/output select register */ |
| 76 | 79 | int strobe[PIO_PORT_COUNT]; /* strobe pulse */ |
| 80 | int match[PIO_PORT_COUNT]; /* control mode bit logic equation match */ |
| 77 | 81 | |
| 78 | 82 | /* timers */ |
| 79 | 83 | emu_timer *poll_timer; /* mode 3 poll timer */ |
| 84 | emu_timer *irq_timer; /* interrupt enable timer */ |
| 80 | 85 | }; |
| 81 | 86 | |
| 82 | 87 | /*************************************************************************** |
| r3449 | r3450 | |
| 111 | 116 | |
| 112 | 117 | for (channel = PIO_PORT_A; channel < PIO_PORT_COUNT; channel++) |
| 113 | 118 | { |
| 114 | | if (z80pio->irq_enable[channel]) |
| 119 | if (!(z80pio->irq_enable[channel])) |
| 115 | 120 | { |
| 116 | | if (z80pio->irq_pending[channel]) |
| 117 | | { |
| 118 | | if (LOG) logerror("Z80PIO \"%s\" Port %c Interrupt Request\n", device->tag, 'A' + channel); |
| 119 | | |
| 120 | | /* request interrupt */ |
| 121 | | z80pio->irq_pending[channel] = 0; |
| 122 | | z80pio->int_state[channel] |= Z80_DAISY_INT; |
| 123 | | break; |
| 124 | | } |
| 125 | | } |
| 126 | | else |
| 127 | | { |
| 128 | 121 | /* clear interrupt request */ |
| 129 | 122 | z80pio->int_state[channel] &= ~Z80_DAISY_INT; |
| 130 | 123 | } |
| r3449 | r3450 | |
| 136 | 129 | } |
| 137 | 130 | } |
| 138 | 131 | |
| 139 | | static void z80pio_set_ready_line(const device_config *device, int channel, int level) |
| 132 | static void z80pio_trigger_interrupt(const device_config *device, int channel) |
| 140 | 133 | { |
| 141 | 134 | z80pio_t *z80pio = get_safe_token(device); |
| 142 | 135 | |
| 143 | | if (LOG) logerror("Z80PIO \"%s\" Port %c Ready %u\n", device->tag, 'A' + channel, level); |
| 136 | if (z80pio->irq_enable[channel]) |
| 137 | { |
| 138 | z80pio->int_state[channel] |= Z80_DAISY_INT; |
| 139 | z80pio_check_interrupt(device); |
| 144 | 140 | |
| 141 | LOGERROR("Z80PIO \"%s\" Port %c : Interrupt Request\n", device->tag, 'A' + channel); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | static TIMER_CALLBACK( z80pio_irq_tick ) |
| 146 | { |
| 147 | const device_config *device = (device_config *) ptr; |
| 148 | z80pio_t *z80pio = get_safe_token(device); |
| 149 | |
| 150 | int channel = BIT(param, 1); |
| 151 | int state = BIT(param, 0); |
| 152 | |
| 153 | /* set interrupt enable */ |
| 154 | z80pio->irq_enable[channel] = state; |
| 155 | |
| 156 | LOGERROR("Z80PIO \"%s\" Port %c : Interrupt Enable %u\n", device->tag, 'A' + channel, state); |
| 157 | } |
| 158 | |
| 159 | static void z80pio_set_irq_enable(const device_config *device, int channel, int state) |
| 160 | { |
| 161 | z80pio_t *z80pio = get_safe_token(device); |
| 162 | |
| 163 | if (state) |
| 164 | { |
| 165 | /* enable interrupt on next _M1 */ |
| 166 | int param = (channel << 1) | state; |
| 167 | |
| 168 | timer_adjust_oneshot(z80pio->irq_timer, ATTOTIME_IN_HZ(z80pio->clock * 4), param); |
| 169 | } |
| 170 | else |
| 171 | { |
| 172 | /* disable interrupt immediately */ |
| 173 | z80pio->irq_enable[channel] = state; |
| 174 | |
| 175 | LOGERROR("Z80PIO \"%s\" Port %c : Interrupt Enable %u\n", device->tag, 'A' + channel, state); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | static void z80pio_ready_w(const device_config *device, int channel, int state) |
| 180 | { |
| 181 | z80pio_t *z80pio = get_safe_token(device); |
| 182 | |
| 183 | LOGERROR("Z80PIO \"%s\" Port %c : Ready %u\n", device->tag, 'A' + channel, state); |
| 184 | |
| 145 | 185 | if (channel == PIO_PORT_A) |
| 146 | 186 | { |
| 147 | 187 | if (z80pio->intf->on_ardy_changed) |
| 148 | 188 | { |
| 149 | | z80pio->intf->on_ardy_changed(device, level); |
| 189 | z80pio->intf->on_ardy_changed(device, state); |
| 150 | 190 | } |
| 151 | 191 | } |
| 152 | 192 | else |
| 153 | 193 | { |
| 154 | 194 | if (z80pio->intf->on_brdy_changed) |
| 155 | 195 | { |
| 156 | | z80pio->intf->on_brdy_changed(device, level); |
| 196 | z80pio->intf->on_brdy_changed(device, state); |
| 157 | 197 | } |
| 158 | 198 | } |
| 159 | 199 | } |
| 160 | 200 | |
| 161 | | static void z80pio_port_read(const device_config *device, int channel) |
| 201 | static UINT8 z80pio_port_r(const device_config *device, int channel) |
| 162 | 202 | { |
| 163 | 203 | z80pio_t *z80pio = get_safe_token(device); |
| 164 | 204 | UINT8 data = 0; |
| r3449 | r3450 | |
| 178 | 218 | } |
| 179 | 219 | } |
| 180 | 220 | |
| 181 | | //if (LOG) logerror("Z80PIO \"%s\" Port %c Read Data %02x\n", device->tag, 'A' + channel, data); |
| 221 | //LOGERROR("Z80PIO \"%s\" Port %c : Input Data '%02x'\n", device->tag, 'A' + channel, data); |
| 182 | 222 | |
| 183 | | z80pio->input[channel] = data; |
| 223 | return data; |
| 184 | 224 | } |
| 185 | 225 | |
| 186 | | static void z80pio_port_write(const device_config *device, int channel) |
| 226 | static void z80pio_port_w(const device_config *device, int channel) |
| 187 | 227 | { |
| 188 | 228 | z80pio_t *z80pio = get_safe_token(device); |
| 189 | 229 | UINT8 data = z80pio->output[channel]; |
| 190 | 230 | |
| 191 | 231 | if (z80pio->mode[channel] == PIO_MODE_CONTROL) |
| 192 | 232 | { |
| 193 | | /* mask out input pins */ |
| 233 | /* mask input pins */ |
| 194 | 234 | data &= ~z80pio->ddr[channel]; |
| 195 | 235 | } |
| 196 | 236 | |
| r3449 | r3450 | |
| 209 | 249 | } |
| 210 | 250 | } |
| 211 | 251 | |
| 212 | | if (LOG) logerror("Z80PIO \"%s\" Port %c Write Data %02x\n", device->tag, 'A' + channel, data); |
| 252 | //LOGERROR("Z80PIO \"%s\" Port %c : Output Data '%02x'\n", device->tag, 'A' + channel, data); |
| 213 | 253 | } |
| 214 | 254 | |
| 215 | | static void z80pio_set_mode(const device_config *device, int channel, int mode) |
| 255 | static void z80pio_set_mode(const device_config *device, int channel, UINT8 data) |
| 216 | 256 | { |
| 217 | 257 | z80pio_t *z80pio = get_safe_token(device); |
| 218 | 258 | |
| 219 | | if (LOG) logerror("Z80PIO \"%s\" Port %c Mode %u\n", device->tag, 'A' + channel, mode); |
| 259 | int mode = data >> 6; |
| 220 | 260 | |
| 221 | | /* clear interrupt */ |
| 222 | | z80pio->irq_enable[channel] = 0; |
| 223 | | z80pio->irq_pending[channel] = 0; |
| 224 | | z80pio->int_state[channel] = 0; |
| 225 | | |
| 226 | | z80pio_check_interrupt(device); |
| 227 | | |
| 228 | 261 | switch (mode) |
| 229 | 262 | { |
| 230 | 263 | case PIO_MODE_OUTPUT: |
| 264 | LOGERROR("Z80PIO \"%s\" Port %c : Mode %u (Byte Output)\n", device->tag, 'A' + channel, mode); |
| 265 | |
| 266 | /* set mode */ |
| 231 | 267 | z80pio->mode[channel] = mode; |
| 232 | 268 | |
| 233 | 269 | /* enable data output */ |
| 234 | | z80pio_port_write(device, channel); |
| 270 | z80pio_port_w(device, channel); |
| 235 | 271 | |
| 236 | 272 | /* assert ready line */ |
| 237 | | z80pio_set_ready_line(device, channel, 1); |
| 273 | z80pio_ready_w(device, channel, 1); |
| 238 | 274 | break; |
| 239 | 275 | |
| 240 | 276 | case PIO_MODE_INPUT: |
| 277 | LOGERROR("Z80PIO \"%s\" Port %c : Mode %u (Byte Input)\n", device->tag, 'A' + channel, mode); |
| 278 | |
| 279 | /* set mode */ |
| 241 | 280 | z80pio->mode[channel] = mode; |
| 242 | 281 | break; |
| 243 | 282 | |
| 244 | 283 | case PIO_MODE_BIDIRECTIONAL: |
| 245 | 284 | if (channel == PIO_PORT_A) |
| 246 | 285 | { |
| 286 | LOGERROR("Z80PIO \"%s\" Port %c : Mode %u (Bidirectional)\n", device->tag, 'A' + channel, mode); |
| 287 | |
| 288 | /* set mode */ |
| 247 | 289 | z80pio->mode[channel] = mode; |
| 248 | 290 | } |
| 249 | 291 | break; |
| 250 | 292 | |
| 251 | 293 | case PIO_MODE_CONTROL: |
| 294 | LOGERROR("Z80PIO \"%s\" Port %c : Mode %u (Bit Control)\n", device->tag, 'A' + channel, mode); |
| 295 | |
| 296 | /* set mode */ |
| 252 | 297 | z80pio->mode[channel] = mode; |
| 253 | 298 | |
| 254 | 299 | if ((channel == PIO_PORT_A) || (z80pio->mode[PIO_PORT_A] != PIO_MODE_BIDIRECTIONAL)) |
| 255 | 300 | { |
| 256 | 301 | /* clear ready line */ |
| 257 | | z80pio_set_ready_line(device, channel, 0); |
| 302 | z80pio_ready_w(device, channel, 0); |
| 258 | 303 | } |
| 304 | |
| 305 | /* disable interrupts until DDR is written */ |
| 306 | z80pio_set_irq_enable(device, channel, 0); |
| 307 | |
| 308 | /* set logic equation to false */ |
| 309 | z80pio->match[channel] = 0; |
| 310 | |
| 311 | /* next word is I/O control */ |
| 312 | z80pio->state[channel] = PIO_STATE_DDR; |
| 259 | 313 | break; |
| 260 | 314 | } |
| 261 | 315 | } |
| r3449 | r3450 | |
| 264 | 318 | CONTROL REGISTER READ/WRITE |
| 265 | 319 | ***************************************************************************/ |
| 266 | 320 | |
| 321 | READ8_DEVICE_HANDLER( lh0081_c_r ) |
| 322 | { |
| 323 | z80pio_t *z80pio = get_safe_token(device); |
| 324 | |
| 325 | return (z80pio->enable[PIO_PORT_A] & 0xc0) | (z80pio->enable[PIO_PORT_B] >> 4); |
| 326 | } |
| 327 | |
| 267 | 328 | READ8_DEVICE_HANDLER( z80pio_c_r ) |
| 268 | 329 | { |
| 269 | 330 | return 0; |
| r3449 | r3450 | |
| 279 | 340 | case PIO_STATE_NORMAL: |
| 280 | 341 | if (!BIT(data, 0)) |
| 281 | 342 | { |
| 343 | LOGERROR("Z80PIO \"%s\" Port %c : Interrupt Vector Word '%02x'\n", device->tag, 'A' + channel, z80pio->vector[channel]); |
| 344 | |
| 282 | 345 | /* load interrupt vector */ |
| 283 | 346 | z80pio->vector[channel] = data & 0xfe; |
| 284 | | |
| 285 | | if (LOG) logerror("Z80PIO \"%s\" Port %c Interrupt Vector %02x\n", device->tag, 'A' + channel, z80pio->vector[channel]); |
| 286 | 347 | } |
| 287 | | else if ((data & 0x0f) == 0x03) |
| 348 | else if ((data & 0x0f) == PIO_IRQ_DISABLE_WORD) |
| 288 | 349 | { |
| 350 | LOGERROR("Z80PIO \"%s\" Port %c : Interrupt Disable Word '%02x'\n", device->tag, 'A' + channel, data); |
| 351 | |
| 289 | 352 | /* set interrupt enable */ |
| 290 | 353 | z80pio->enable[channel] = (data & 0x80) | (z80pio->enable[channel] & 0x7f); |
| 291 | | z80pio->irq_enable[channel] = BIT(data, 7); |
| 292 | | z80pio_check_interrupt(device); |
| 293 | | |
| 294 | | if (LOG) logerror("Z80PIO \"%s\" Port %c Interrupt Enable %u\n", device->tag, 'A' + channel, BIT(data, 7)); |
| 354 | z80pio_set_irq_enable(device, channel, BIT(data, 7)); |
| 295 | 355 | } |
| 296 | | else if ((data & 0x0f) == 0x0f) |
| 356 | else if ((data & 0x0f) == PIO_MODE_CONTROL_WORD) |
| 297 | 357 | { |
| 298 | | /* select operating mode */ |
| 299 | | int mode = data >> 6; |
| 358 | LOGERROR("Z80PIO \"%s\" Port %c : Mode Control Word '%02x'\n", device->tag, 'A' + channel, data); |
| 300 | 359 | |
| 301 | | z80pio_set_mode(device, channel, mode); |
| 302 | | |
| 303 | | if (mode == PIO_MODE_CONTROL) |
| 304 | | { |
| 305 | | z80pio->state[channel] = PIO_STATE_DDR; |
| 306 | | } |
| 360 | /* set operating mode */ |
| 361 | z80pio_set_mode(device, channel, data); |
| 307 | 362 | } |
| 308 | | else if ((data & 0x0f) == 0x07) |
| 363 | else if ((data & 0x0f) == PIO_IRQ_CONTROL_WORD) |
| 309 | 364 | { |
| 365 | LOGERROR("Z80PIO \"%s\" Port %c : Interrupt Control Word '%02x'\n", device->tag, 'A' + channel, data); |
| 366 | |
| 310 | 367 | /* set interrupt control word */ |
| 311 | 368 | z80pio->enable[channel] = data & 0xf0; |
| 312 | 369 | |
| 313 | 370 | if (data & PIO_IRQ_MASK_FOLLOWS) |
| 314 | 371 | { |
| 315 | | /* mask follows */ |
| 372 | /* disable interrupts until mask is written */ |
| 373 | z80pio_set_irq_enable(device, channel, 0); |
| 374 | |
| 375 | /* next word is mask control */ |
| 316 | 376 | z80pio->state[channel] = PIO_STATE_MASK; |
| 317 | | |
| 318 | | /* clear pending interrupt */ |
| 319 | | z80pio->irq_pending[channel] = 0; |
| 320 | 377 | } |
| 321 | 378 | else |
| 322 | 379 | { |
| 323 | 380 | /* set interrupt enable */ |
| 324 | | z80pio->irq_enable[channel] = BIT(data, 7); |
| 381 | z80pio_set_irq_enable(device, channel, BIT(data, 7)); |
| 325 | 382 | } |
| 326 | | |
| 327 | | z80pio_check_interrupt(device); |
| 328 | | |
| 329 | | if (LOG) logerror("Z80PIO \"%s\" Port %c Interrupt Control %02x\n", device->tag, 'A' + channel, data); |
| 330 | 383 | } |
| 331 | 384 | break; |
| 332 | 385 | |
| 333 | 386 | case PIO_STATE_DDR: |
| 387 | LOGERROR("Z80PIO \"%s\" Port %c : I/O Control Word '%02x'\n", device->tag, 'A' + channel, data); |
| 388 | |
| 334 | 389 | /* set data direction */ |
| 335 | 390 | z80pio->ddr[channel] = data; |
| 336 | 391 | |
| 337 | 392 | /* set interrupt enable */ |
| 338 | | z80pio->irq_enable[channel] = BIT(z80pio->enable[channel], 7); |
| 339 | | z80pio_check_interrupt(device); |
| 393 | z80pio_set_irq_enable(device, channel, BIT(z80pio->enable[channel], 7)); |
| 340 | 394 | |
| 395 | /* next word is indeterminate */ |
| 341 | 396 | z80pio->state[channel] = PIO_STATE_NORMAL; |
| 342 | | |
| 343 | | if (LOG) logerror("Z80PIO \"%s\" Port %c Direction %02x\n", device->tag, 'A' + channel, data); |
| 344 | 397 | break; |
| 345 | 398 | |
| 346 | 399 | case PIO_STATE_MASK: |
| 400 | LOGERROR("Z80PIO \"%s\" Port %c : Mask Control Word '%02x'\n", device->tag, 'A' + channel, data); |
| 401 | |
| 347 | 402 | /* set interrupt mask */ |
| 348 | 403 | z80pio->mask[channel] = data; |
| 349 | 404 | |
| 350 | 405 | /* set interrupt enable */ |
| 351 | | z80pio->irq_enable[channel] = BIT(z80pio->enable[channel], 7); |
| 352 | | z80pio_check_interrupt(device); |
| 406 | z80pio_set_irq_enable(device, channel, BIT(z80pio->enable[channel], 7)); |
| 353 | 407 | |
| 408 | /* next word is indeterminate */ |
| 354 | 409 | z80pio->state[channel] = PIO_STATE_NORMAL; |
| 355 | | |
| 356 | | if (LOG) logerror("Z80PIO \"%s\" Port %c Mask %02x\n", device->tag, 'A' + channel, data); |
| 357 | 410 | break; |
| 358 | 411 | } |
| 359 | 412 | } |
| r3449 | r3450 | |
| 378 | 431 | data = z80pio->input[channel]; |
| 379 | 432 | |
| 380 | 433 | /* clear ready line */ |
| 381 | | z80pio_set_ready_line(device, channel, 0); |
| 434 | z80pio_ready_w(device, channel, 0); |
| 382 | 435 | |
| 383 | 436 | /* assert ready line */ |
| 384 | | z80pio_set_ready_line(device, channel, 1); |
| 437 | z80pio_ready_w(device, channel, 1); |
| 385 | 438 | break; |
| 386 | 439 | |
| 387 | 440 | case PIO_MODE_BIDIRECTIONAL: |
| 388 | 441 | data = z80pio->input[PIO_PORT_A] | (z80pio->output[PIO_PORT_A] & (~z80pio->ddr[PIO_PORT_A])); |
| 389 | 442 | |
| 390 | 443 | /* clear ready line */ |
| 391 | | z80pio_set_ready_line(device, 1, 0); |
| 444 | z80pio_ready_w(device, 1, 0); |
| 392 | 445 | |
| 393 | 446 | /* assert ready line */ |
| 394 | | z80pio_set_ready_line(device, 1, 1); |
| 447 | z80pio_ready_w(device, 1, 1); |
| 395 | 448 | break; |
| 396 | 449 | |
| 397 | 450 | case PIO_MODE_CONTROL: |
| 398 | | z80pio_port_read(device, channel); |
| 451 | z80pio->input[channel] = z80pio_port_r(device, channel); |
| 452 | |
| 399 | 453 | data = (z80pio->input[channel] & z80pio->ddr[channel]) | (z80pio->output[channel] & (~z80pio->ddr[channel])); |
| 400 | 454 | break; |
| 401 | 455 | } |
| 402 | 456 | |
| 403 | | if (LOG) logerror("Z80PIO \"%s\" Port %c Data Register Read %02x\n", device->tag, 'A' + channel, data); |
| 457 | LOGERROR("Z80PIO \"%s\" Port %c : Data Register Read '%02x'\n", device->tag, 'A' + channel, data); |
| 404 | 458 | |
| 405 | 459 | return data; |
| 406 | 460 | } |
| r3449 | r3450 | |
| 410 | 464 | z80pio_t *z80pio = get_safe_token( device ); |
| 411 | 465 | int channel = offset & 0x01; |
| 412 | 466 | |
| 413 | | if (LOG) logerror("Z80PIO \"%s\" Port %c Data Register Write %02x\n", device->tag, 'A' + channel, data); |
| 467 | LOGERROR("Z80PIO \"%s\" Port %c : Data Register Write '%02x'\n", device->tag, 'A' + channel, data); |
| 414 | 468 | |
| 415 | 469 | switch (z80pio->mode[channel]) |
| 416 | 470 | { |
| 417 | 471 | case PIO_MODE_OUTPUT: |
| 418 | 472 | /* clear ready line */ |
| 419 | | z80pio_set_ready_line(device, channel, 0); |
| 473 | z80pio_ready_w(device, channel, 0); |
| 420 | 474 | |
| 421 | 475 | /* output data */ |
| 422 | 476 | z80pio->output[channel] = data; |
| 423 | | z80pio_port_write(device, channel); |
| 477 | z80pio_port_w(device, channel); |
| 424 | 478 | |
| 425 | 479 | /* assert ready line */ |
| 426 | | z80pio_set_ready_line(device, channel, 1); |
| 480 | z80pio_ready_w(device, channel, 1); |
| 427 | 481 | break; |
| 428 | 482 | |
| 429 | 483 | case PIO_MODE_INPUT: |
| r3449 | r3450 | |
| 432 | 486 | |
| 433 | 487 | case PIO_MODE_BIDIRECTIONAL: |
| 434 | 488 | /* clear ready line */ |
| 435 | | z80pio_set_ready_line(device, PIO_PORT_A, 0); |
| 489 | z80pio_ready_w(device, channel, 0); |
| 436 | 490 | |
| 437 | 491 | /* latch output data */ |
| 438 | 492 | z80pio->output[channel] = data; |
| 439 | 493 | |
| 440 | | if (!z80pio->strobe[PIO_PORT_A]) |
| 494 | if (!z80pio->strobe[channel]) |
| 441 | 495 | { |
| 442 | 496 | /* output data if ASTB active */ |
| 443 | | z80pio_port_write(device, channel); |
| 497 | z80pio_port_w(device, channel); |
| 444 | 498 | } |
| 445 | 499 | |
| 446 | 500 | /* assert ready line */ |
| 447 | | z80pio_set_ready_line(device, PIO_PORT_A, 1); |
| 501 | z80pio_ready_w(device, channel, 1); |
| 448 | 502 | break; |
| 449 | 503 | |
| 450 | 504 | case PIO_MODE_CONTROL: |
| 451 | 505 | /* output data */ |
| 452 | 506 | z80pio->output[channel] = data; |
| 453 | | z80pio_port_write(device, channel); |
| 507 | z80pio_port_w(device, channel); |
| 454 | 508 | break; |
| 455 | 509 | } |
| 456 | 510 | } |
| r3449 | r3450 | |
| 472 | 526 | int bit; |
| 473 | 527 | int match = 0; |
| 474 | 528 | |
| 529 | int edge = (z80pio->enable[channel] & PIO_IRQ_ACTIVE_HIGH) ? 1 : 0; |
| 530 | |
| 531 | UINT8 mask = z80pio->mask[channel]; |
| 475 | 532 | UINT8 ddr = z80pio->ddr[channel]; |
| 476 | | UINT8 mask = z80pio->mask[channel]; |
| 477 | | UINT8 output = (z80pio->output[channel] & ~ddr); |
| 478 | | UINT8 old_data = (z80pio->input[channel] & z80pio->ddr[channel]) | output; |
| 479 | | UINT8 data; |
| 480 | | |
| 481 | | z80pio_port_read(device, channel); |
| 533 | UINT8 old_data = z80pio->input[channel] & ddr; |
| 534 | UINT8 data = z80pio_port_r(device, channel) & ddr; |
| 482 | 535 | |
| 483 | | data = (z80pio->input[channel] & ddr) | output; |
| 484 | | |
| 485 | 536 | for (bit = 0; bit < 8; bit++) |
| 486 | 537 | { |
| 487 | | if (!BIT(mask, bit)) |
| 538 | if (BIT(mask, bit)) continue; /* masked out */ |
| 539 | if (!BIT(ddr, bit)) continue; /* output bit */ |
| 540 | |
| 541 | if ((BIT(old_data, bit) ^ edge) && !(BIT(data, bit) ^ edge)) /* falling edge */ |
| 488 | 542 | { |
| 489 | | if (z80pio->enable[channel] & PIO_IRQ_HIGH_LOW) |
| 490 | | { |
| 491 | | if (!BIT(old_data, bit) && BIT(data, bit)) // rising edge |
| 492 | | { |
| 493 | | if (LOG) logerror("Z80PIO \"%s\" Port %c Rising Edge detected on bit %u\n", device->tag, 'A' + channel, bit); |
| 543 | /* logic equation is true, until proven otherwise */ |
| 544 | match = 1; |
| 494 | 545 | |
| 495 | | match = 1; |
| 496 | | |
| 497 | | if (!(z80pio->enable[channel] & PIO_IRQ_AND_OR)) // OR |
| 498 | | { |
| 499 | | break; |
| 500 | | } |
| 501 | | } |
| 502 | | else |
| 503 | | { |
| 504 | | if (z80pio->enable[channel] & PIO_IRQ_AND_OR) // AND |
| 505 | | { |
| 506 | | match = 0; |
| 507 | | break; |
| 508 | | } |
| 509 | | } |
| 510 | | } |
| 511 | | else |
| 512 | | { |
| 513 | | if (BIT(old_data, bit) && !BIT(data, bit)) // falling edge |
| 514 | | { |
| 515 | | if (LOG) logerror("Z80PIO \"%s\" Port %c Falling Edge detected on bit %u\n", device->tag, 'A' + channel, bit); |
| 516 | | |
| 517 | | match = 1; |
| 518 | | |
| 519 | | if (!(z80pio->enable[channel] & PIO_IRQ_AND_OR)) // OR |
| 520 | | { |
| 521 | | break; |
| 522 | | } |
| 523 | | } |
| 524 | | else |
| 525 | | { |
| 526 | | if (z80pio->enable[channel] & PIO_IRQ_AND_OR) // AND |
| 527 | | { |
| 528 | | match = 0; |
| 529 | | break; |
| 530 | | } |
| 531 | | } |
| 532 | | } |
| 546 | LOGERROR("Z80PIO \"%s\" Port %c : Edge Transition detected on bit %u, data %02x %02x\n", device->tag, 'A' + channel, bit, old_data, data); |
| 533 | 547 | } |
| 548 | else if (z80pio->enable[channel] & PIO_IRQ_AND) |
| 549 | { |
| 550 | /* all bits must match in AND mode, so logic equation is false */ |
| 551 | match = 0; |
| 552 | break; |
| 553 | } |
| 534 | 554 | } |
| 535 | 555 | |
| 536 | | if (match) |
| 556 | if (!z80pio->match[channel] && match) /* rising edge */ |
| 537 | 557 | { |
| 538 | | /* trigger interrupt */ |
| 539 | | z80pio->irq_pending[channel] = 1; |
| 558 | /* trigger interrupt only once */ |
| 559 | z80pio_trigger_interrupt(device, channel); |
| 560 | z80pio->input[channel] = data; |
| 561 | } |
| 540 | 562 | |
| 541 | | if (LOG) logerror("Z80PIO \"%s\" Port %c Interrupt Pending\n", device->tag, 'A' + channel); |
| 542 | | |
| 543 | | z80pio_check_interrupt(device); |
| 544 | | } |
| 563 | z80pio->match[channel] = match; |
| 545 | 564 | } |
| 546 | 565 | } |
| 547 | 566 | } |
| r3449 | r3450 | |
| 550 | 569 | STROBE STATE MANAGEMENT |
| 551 | 570 | ***************************************************************************/ |
| 552 | 571 | |
| 553 | | static void z80pio_strobe(const device_config *device, int channel, int level) |
| 572 | static void z80pio_strobe_bidirectional(const device_config *device, int channel, int state) |
| 554 | 573 | { |
| 555 | 574 | z80pio_t *z80pio = get_safe_token(device); |
| 556 | 575 | |
| 557 | | if (LOG) logerror("Z80PIO \"%s\" Port %c Strobe %u\n", device->tag, 'A' + channel, level); |
| 558 | | |
| 559 | | if (z80pio->mode[PIO_PORT_A] == PIO_MODE_BIDIRECTIONAL) |
| 576 | switch (channel) |
| 560 | 577 | { |
| 561 | | /* port A bidirectional mode */ |
| 562 | | |
| 563 | | switch (channel) |
| 578 | case PIO_PORT_A: |
| 579 | if (!state) /* low level */ |
| 564 | 580 | { |
| 565 | | case PIO_PORT_A: |
| 566 | | if (!level) |
| 567 | | { |
| 568 | | /* output data */ |
| 569 | | z80pio_port_write(device, PIO_PORT_A); |
| 570 | | } |
| 571 | | break; |
| 581 | /* output data */ |
| 582 | z80pio_port_w(device, PIO_PORT_A); |
| 583 | } |
| 584 | else if (!z80pio->strobe[channel] && state) /* rising edge */ |
| 585 | { |
| 586 | /* trigger interrupt */ |
| 587 | z80pio_trigger_interrupt(device, channel); |
| 572 | 588 | |
| 573 | | case PIO_PORT_B: |
| 574 | | if (!level) |
| 575 | | { |
| 576 | | /* input data */ |
| 577 | | z80pio_port_read(device, PIO_PORT_A); |
| 578 | | } |
| 579 | | else if (!z80pio->strobe[PIO_PORT_A] && level) // rising edge |
| 580 | | { |
| 581 | | /* generate interrupt */ |
| 582 | | z80pio->irq_pending[channel] = 1; |
| 583 | | z80pio_check_interrupt(device); |
| 589 | /* clear ready line */ |
| 590 | z80pio_ready_w(device, channel, 0); |
| 591 | } |
| 592 | break; |
| 584 | 593 | |
| 585 | | /* clear ready line */ |
| 586 | | z80pio_set_ready_line(device, channel, 0); |
| 594 | case PIO_PORT_B: |
| 595 | if (!state) /* low level */ |
| 596 | { |
| 597 | /* input data */ |
| 598 | z80pio->input[channel] = z80pio_port_r(device, PIO_PORT_A); |
| 599 | } |
| 600 | else if (!z80pio->strobe[channel] && state) /* rising edge */ |
| 601 | { |
| 602 | /* trigger interrupt */ |
| 603 | z80pio_trigger_interrupt(device, channel); |
| 587 | 604 | |
| 588 | | if (LOG) logerror("Z80PIO \"%s\" Port %c Interrupt Pending\n", device->tag, 'A' + channel); |
| 589 | | } |
| 590 | | break; |
| 605 | /* clear ready line */ |
| 606 | z80pio_ready_w(device, channel, 0); |
| 591 | 607 | } |
| 608 | break; |
| 592 | 609 | } |
| 593 | | else |
| 594 | | { |
| 595 | | switch (z80pio->mode[channel]) |
| 596 | | { |
| 597 | | case PIO_MODE_OUTPUT: |
| 598 | | if (!z80pio->strobe[channel] && level) // rising edge |
| 599 | | { |
| 600 | | /* generate interrupt */ |
| 601 | | z80pio->irq_pending[channel] = 1; |
| 602 | | z80pio_check_interrupt(device); |
| 603 | 610 | |
| 604 | | /* clear ready line */ |
| 605 | | z80pio_set_ready_line(device, channel, 0); |
| 611 | /* latch strobe state */ |
| 612 | z80pio->strobe[channel] = state; |
| 613 | } |
| 606 | 614 | |
| 607 | | if (LOG) logerror("Z80PIO \"%s\" Port %c Interrupt Pending\n", device->tag, 'A' + channel); |
| 608 | | } |
| 609 | | break; |
| 615 | static void z80pio_strobe(const device_config *device, int channel, int state) |
| 616 | { |
| 617 | z80pio_t *z80pio = get_safe_token(device); |
| 610 | 618 | |
| 611 | | case PIO_MODE_INPUT: |
| 612 | | if (!level) |
| 613 | | { |
| 614 | | /* strobe data in */ |
| 615 | | z80pio_port_read(device, channel); |
| 616 | | } |
| 617 | | else if (!z80pio->strobe[channel] && level) // rising edge |
| 618 | | { |
| 619 | | /* generate interrupt */ |
| 620 | | z80pio->irq_pending[channel] = 1; |
| 621 | | z80pio_check_interrupt(device); |
| 619 | switch (z80pio->mode[channel]) |
| 620 | { |
| 621 | case PIO_MODE_OUTPUT: |
| 622 | if (!z80pio->strobe[channel] && state) /* rising edge */ |
| 623 | { |
| 624 | /* trigger interrupt */ |
| 625 | z80pio_trigger_interrupt(device, channel); |
| 622 | 626 | |
| 623 | | /* clear ready line */ |
| 624 | | z80pio_set_ready_line(device, channel, 0); |
| 627 | /* clear ready line */ |
| 628 | z80pio_ready_w(device, channel, 0); |
| 629 | } |
| 630 | break; |
| 625 | 631 | |
| 626 | | if (LOG) logerror("Z80PIO \"%s\" Port %c Interrupt Pending\n", device->tag, 'A' + channel); |
| 627 | | } |
| 628 | | break; |
| 632 | case PIO_MODE_INPUT: |
| 633 | if (!state) /* low level */ |
| 634 | { |
| 635 | /* strobe data in */ |
| 636 | z80pio->input[channel] = z80pio_port_r(device, channel); |
| 637 | } |
| 638 | else if (!z80pio->strobe[channel] && state) /* rising edge */ |
| 639 | { |
| 640 | /* trigger interrupt */ |
| 641 | z80pio_trigger_interrupt(device, channel); |
| 629 | 642 | |
| 630 | | case PIO_MODE_CONTROL: |
| 631 | | /* ignore strobes */ |
| 632 | | break; |
| 643 | /* clear ready line */ |
| 644 | z80pio_ready_w(device, channel, 0); |
| 633 | 645 | } |
| 646 | break; |
| 647 | |
| 648 | case PIO_MODE_CONTROL: |
| 649 | /* ignore strobes */ |
| 650 | break; |
| 634 | 651 | } |
| 635 | 652 | |
| 636 | | z80pio->strobe[channel] = level; |
| 653 | /* latch strobe state */ |
| 654 | z80pio->strobe[channel] = state; |
| 637 | 655 | } |
| 638 | 656 | |
| 639 | | void z80pio_astb_w(const device_config *device, int level) |
| 657 | static void z80pio_strobe_w(const device_config *device, int channel, int state) |
| 640 | 658 | { |
| 641 | | z80pio_strobe(device, PIO_PORT_A, level); |
| 659 | z80pio_t *z80pio = get_safe_token(device); |
| 660 | |
| 661 | LOGERROR("Z80PIO \"%s\" Port %c : Strobe %u\n", device->tag, 'A' + channel, state); |
| 662 | |
| 663 | if (z80pio->mode[PIO_PORT_A] == PIO_MODE_BIDIRECTIONAL) |
| 664 | { |
| 665 | /* port A bidirectional mode */ |
| 666 | z80pio_strobe_bidirectional(device, channel, state); |
| 667 | } |
| 668 | else |
| 669 | { |
| 670 | z80pio_strobe(device, channel, state); |
| 671 | } |
| 642 | 672 | } |
| 643 | 673 | |
| 644 | | void z80pio_bstb_w(const device_config *device, int level) |
| 674 | void z80pio_astb_w(const device_config *device, int state) |
| 645 | 675 | { |
| 646 | | z80pio_strobe(device, PIO_PORT_B, level); |
| 676 | z80pio_strobe_w(device, PIO_PORT_A, state); |
| 647 | 677 | } |
| 648 | 678 | |
| 679 | void z80pio_bstb_w(const device_config *device, int state) |
| 680 | { |
| 681 | z80pio_strobe_w(device, PIO_PORT_B, state); |
| 682 | } |
| 683 | |
| 649 | 684 | /*************************************************************************** |
| 650 | 685 | DAISY CHAIN INTERFACE |
| 651 | 686 | ***************************************************************************/ |
| r3449 | r3450 | |
| 656 | 691 | int state = 0; |
| 657 | 692 | int channel; |
| 658 | 693 | |
| 659 | | if (LOG) logerror("Z80PIO \"%s\" IRQ STATE\n", device->tag); |
| 660 | | |
| 661 | 694 | /* loop over all channels */ |
| 662 | 695 | for (channel = PIO_PORT_A; channel < PIO_PORT_COUNT; channel++) |
| 663 | 696 | { |
| r3449 | r3450 | |
| 671 | 704 | state |= z80pio->int_state[channel]; |
| 672 | 705 | } |
| 673 | 706 | |
| 707 | LOGERROR("Z80PIO \"%s\" : Interrupt State %u\n", device->tag, state); |
| 708 | |
| 674 | 709 | return state; |
| 675 | 710 | } |
| 676 | 711 | |
| r3449 | r3450 | |
| 679 | 714 | z80pio_t *z80pio = get_safe_token( device ); |
| 680 | 715 | int channel; |
| 681 | 716 | |
| 682 | | if (LOG) logerror("Z80PIO \"%s\" IRQ ACK\n", device->tag); |
| 717 | LOGERROR("Z80PIO \"%s\" Interrupt Acknowledge\n", device->tag); |
| 683 | 718 | |
| 684 | 719 | /* loop over all channels */ |
| 685 | 720 | for (channel = PIO_PORT_A; channel < PIO_PORT_COUNT; channel++) |
| r3449 | r3450 | |
| 695 | 730 | } |
| 696 | 731 | } |
| 697 | 732 | |
| 698 | | if (LOG) logerror("z80pio_irq_ack: failed to find an interrupt to ack!"); |
| 733 | LOGERROR("z80pio_irq_ack: failed to find an interrupt to ack!"); |
| 699 | 734 | |
| 700 | 735 | return z80pio->vector[0]; |
| 701 | 736 | } |
| r3449 | r3450 | |
| 705 | 740 | z80pio_t *z80pio = get_safe_token( device ); |
| 706 | 741 | int channel; |
| 707 | 742 | |
| 708 | | if (LOG) logerror("Z80PIO \"%s\" IRQ RETI\n", device->tag); |
| 743 | LOGERROR("Z80PIO \"%s\" Return from Interrupt\n", device->tag); |
| 709 | 744 | |
| 710 | 745 | /* loop over all channels */ |
| 711 | 746 | for (channel = PIO_PORT_A; channel < PIO_PORT_COUNT; channel++) |
| r3449 | r3450 | |
| 721 | 756 | } |
| 722 | 757 | } |
| 723 | 758 | |
| 724 | | if (LOG) logerror("z80pio_irq_reti: failed to find an interrupt to clear IEO on!"); |
| 759 | LOGERROR("z80pio_irq_reti: failed to find an interrupt to clear IEO on!"); |
| 725 | 760 | } |
| 726 | 761 | |
| 727 | 762 | /*************************************************************************** |
| r3449 | r3450 | |
| 789 | 824 | z80pio->clock = intf->clock; |
| 790 | 825 | } |
| 791 | 826 | |
| 792 | | /* allocate poll timers */ |
| 827 | /* allocate poll timer */ |
| 793 | 828 | |
| 794 | 829 | z80pio->poll_timer = timer_alloc(z80pio_poll_tick, (void *)device); |
| 795 | 830 | timer_adjust_periodic(z80pio->poll_timer, attotime_zero, 0, ATTOTIME_IN_HZ(z80pio->clock / 16)); |
| 796 | 831 | |
| 832 | /* allocate interrupt enable timer */ |
| 833 | |
| 834 | z80pio->irq_timer = timer_alloc(z80pio_irq_tick, (void *)device); |
| 835 | |
| 797 | 836 | /* register for state saving */ |
| 798 | 837 | |
| 799 | 838 | state_save_combine_module_and_tag(unique_tag, "z80pio", device->tag); |
| r3449 | r3450 | |
| 801 | 840 | state_save_register_item_array(unique_tag, 0, z80pio->state); |
| 802 | 841 | state_save_register_item_array(unique_tag, 0, z80pio->mode); |
| 803 | 842 | state_save_register_item_array(unique_tag, 0, z80pio->irq_enable); |
| 804 | | state_save_register_item_array(unique_tag, 0, z80pio->irq_pending); |
| 805 | 843 | state_save_register_item_array(unique_tag, 0, z80pio->int_state); |
| 806 | 844 | state_save_register_item_array(unique_tag, 0, z80pio->enable); |
| 807 | 845 | state_save_register_item_array(unique_tag, 0, z80pio->input); |
| r3449 | r3450 | |
| 810 | 848 | state_save_register_item_array(unique_tag, 0, z80pio->mask); |
| 811 | 849 | state_save_register_item_array(unique_tag, 0, z80pio->ddr); |
| 812 | 850 | state_save_register_item_array(unique_tag, 0, z80pio->strobe); |
| 851 | state_save_register_item_array(unique_tag, 0, z80pio->match); |
| 813 | 852 | |
| 814 | 853 | return DEVICE_START_OK; |
| 815 | 854 | } |
| r3449 | r3450 | |
| 819 | 858 | z80pio_t *z80pio = get_safe_token(device); |
| 820 | 859 | int channel; |
| 821 | 860 | |
| 822 | | if (LOG) logerror("Z80PIO Reset\n"); |
| 861 | LOGERROR("Z80PIO \"%s\" : Reset\n", device->tag); |
| 823 | 862 | |
| 824 | 863 | for (channel = 0; channel < 2; channel++) |
| 825 | 864 | { |
| r3449 | r3450 | |
| 827 | 866 | z80pio->mask[channel] = 0xff; |
| 828 | 867 | |
| 829 | 868 | /* clear ready line */ |
| 830 | | z80pio_set_ready_line(device, channel, 0); |
| 869 | z80pio_ready_w(device, channel, 0); |
| 831 | 870 | |
| 832 | 871 | /* set mode 1 */ |
| 833 | 872 | z80pio->mode[channel] = PIO_MODE_INPUT; |
| r3449 | r3450 | |
| 838 | 877 | /* reset interrupt */ |
| 839 | 878 | z80pio->enable[channel] &= 0x7f; |
| 840 | 879 | z80pio->irq_enable[channel] = 0; |
| 841 | | z80pio->irq_pending[channel] = 0; |
| 842 | 880 | z80pio->int_state[channel] = 0; |
| 881 | z80pio->match[channel] = 0; |
| 843 | 882 | |
| 844 | 883 | /* reset output register */ |
| 845 | 884 | z80pio->output[channel] = 0; |
| r3449 | r3450 | |
| 882 | 921 | case DEVINFO_STR_CREDITS: info->s = "Copyright Nicola Salmoria and the MAME Team"; break; |
| 883 | 922 | } |
| 884 | 923 | } |
| 924 | |
| 925 | DEVICE_GET_INFO( z8420 ) |
| 926 | { |
| 927 | switch (state) |
| 928 | { |
| 929 | /* --- the following bits of info are returned as NULL-terminated strings --- */ |
| 930 | case DEVINFO_STR_NAME: info->s = "Zilog Z8420"; break; |
| 931 | case DEVINFO_STR_FAMILY: info->s = "Z80 PIO"; break; |
| 932 | |
| 933 | default: DEVICE_GET_INFO_CALL(z80pio); |
| 934 | } |
| 935 | } |
| 936 | |
| 937 | DEVICE_GET_INFO( lh0081 ) |
| 938 | { |
| 939 | switch (state) |
| 940 | { |
| 941 | /* --- the following bits of info are returned as NULL-terminated strings --- */ |
| 942 | case DEVINFO_STR_NAME: info->s = "Sharp LH0081"; break; |
| 943 | case DEVINFO_STR_FAMILY: info->s = "Z80 PIO"; break; |
| 944 | |
| 945 | default: DEVICE_GET_INFO_CALL(z80pio); |
| 946 | } |
| 947 | } |
| 948 | |
| 949 | DEVICE_GET_INFO( mk3881 ) |
| 950 | { |
| 951 | switch (state) |
| 952 | { |
| 953 | /* --- the following bits of info are returned as NULL-terminated strings --- */ |
| 954 | case DEVINFO_STR_NAME: info->s = "Mostek MK3881"; break; |
| 955 | case DEVINFO_STR_FAMILY: info->s = "Z80 PIO"; break; |
| 956 | |
| 957 | default: DEVICE_GET_INFO_CALL(z80pio); |
| 958 | } |
| 959 | } |