src/emu/machine/pic8259.c
| r3875 | r3876 | |
| 12 | 12 | **********************************************************************/ |
| 13 | 13 | |
| 14 | 14 | #include "driver.h" |
| 15 | | #include "memconv.h" |
| 16 | 15 | #include "machine/pic8259.h" |
| 17 | 16 | |
| 18 | 17 | #define IRQ_COUNT 8 |
| r3875 | r3876 | |
| 41 | 40 | pic8259_state_t state; |
| 42 | 41 | |
| 43 | 42 | UINT8 irq_lines; |
| 44 | | UINT8 in_service; |
| 45 | | UINT8 pending; |
| 43 | UINT8 esr; |
| 44 | UINT8 isr; |
| 45 | UINT8 irr; |
| 46 | 46 | UINT8 prio; |
| 47 | | UINT8 interrupt_mask; |
| 47 | UINT8 imr; |
| 48 | 48 | |
| 49 | 49 | UINT8 input; |
| 50 | | UINT32 special : 1; |
| 50 | UINT8 ocw3; |
| 51 | 51 | |
| 52 | 52 | /* ICW1 state */ |
| 53 | 53 | UINT32 level_trig_mode : 1; |
| r3875 | r3876 | |
| 91 | 91 | mask = 1 << irq; |
| 92 | 92 | |
| 93 | 93 | /* is this IRQ in service? */ |
| 94 | | if (pic8259->in_service & mask) |
| 94 | if (pic8259->isr & mask) |
| 95 | 95 | { |
| 96 | 96 | if (LOG_GENERAL) |
| 97 | 97 | logerror("pic8259_timerproc(): PIC IRQ #%d still in service\n", irq); |
| 98 | | return; |
| 98 | break; |
| 99 | 99 | } |
| 100 | 100 | |
| 101 | 101 | /* is this IRQ pending and enabled? */ |
| 102 | | if ((pic8259->state == STATE_READY) && (pic8259->pending & mask) && !(pic8259->interrupt_mask & mask)) |
| 102 | if ((pic8259->state == STATE_READY) && (pic8259->irr & mask) && !(pic8259->imr & mask)) |
| 103 | 103 | { |
| 104 | 104 | if (LOG_GENERAL) |
| 105 | 105 | logerror("pic8259_timerproc(): PIC triggering IRQ #%d\n", irq); |
| 106 | | if (pic8259->intf->set_int_line) |
| 106 | if ( ! ( pic8259->ocw3 & 0x04 ) && pic8259->intf->set_int_line) |
| 107 | 107 | pic8259->intf->set_int_line(device, 1); |
| 108 | 108 | return; |
| 109 | 109 | } |
| 110 | 110 | } |
| 111 | | if (pic8259->intf->set_int_line) |
| 111 | if ( ! ( pic8259->ocw3 & 0x04 ) && pic8259->intf->set_int_line) |
| 112 | 112 | pic8259->intf->set_int_line(device, 0); |
| 113 | 113 | } |
| 114 | 114 | |
| r3875 | r3876 | |
| 122 | 122 | void pic8259_set_irq_line(const device_config *device, int irq, int state) |
| 123 | 123 | { |
| 124 | 124 | pic8259_t *pic8259 = get_safe_token(device); |
| 125 | UINT8 old_irq_lines = pic8259->irq_lines; |
| 125 | 126 | |
| 126 | 127 | if (state) |
| 127 | 128 | { |
| 128 | 129 | /* setting IRQ line */ |
| 129 | | if (!(pic8259->irq_lines & (1 << irq))) |
| 130 | | { |
| 131 | | if (LOG_GENERAL) |
| 132 | | logerror("pic8259_set_irq_line(): PIC set IRQ line #%d\n", irq); |
| 130 | if (LOG_GENERAL) |
| 131 | logerror("pic8259_set_irq_line(): PIC set IRQ line #%d\n", irq); |
| 133 | 132 | |
| 134 | | pic8259->irq_lines |= 1 << irq; |
| 135 | | pic8259->pending |= 1 << irq; |
| 136 | | pic8259_set_timer(pic8259); |
| 137 | | } |
| 133 | pic8259->irq_lines |= 1 << irq; |
| 134 | |
| 135 | /* Set ESR bit if we see a 0 -> 1 transition */ |
| 136 | if ( ! ( old_irq_lines & ( 1 << irq ) ) ) |
| 137 | pic8259->esr |= ( 1 << irq ); |
| 138 | 138 | } |
| 139 | 139 | else |
| 140 | 140 | { |
| 141 | 141 | /* clearing IRQ line */ |
| 142 | | if (pic8259->irq_lines & (1 << irq)) |
| 143 | | { |
| 144 | | if (LOG_GENERAL) |
| 145 | | logerror("pic8259_set_irq_line(): PIC cleared IRQ line #%d\n", irq); |
| 142 | if (LOG_GENERAL) |
| 143 | logerror("pic8259_set_irq_line(): PIC cleared IRQ line #%d\n", irq); |
| 146 | 144 | |
| 147 | | pic8259->irq_lines &= ~(1 << irq); |
| 148 | | pic8259_set_timer(pic8259); |
| 149 | | } |
| 145 | pic8259->irq_lines &= ~(1 << irq); |
| 150 | 146 | } |
| 147 | |
| 148 | pic8259->irr = ( pic8259->level_trig_mode ) ? pic8259->esr & pic8259->irq_lines : pic8259->irq_lines; |
| 149 | pic8259_set_timer(pic8259); |
| 151 | 150 | } |
| 152 | 151 | |
| 153 | 152 | |
| r3875 | r3876 | |
| 163 | 162 | mask = 1 << irq; |
| 164 | 163 | |
| 165 | 164 | /* is this IRQ pending and enabled? */ |
| 166 | | if ((pic8259->pending & mask) && !(pic8259->interrupt_mask & mask)) |
| 165 | if ((pic8259->irr & mask) && !(pic8259->imr & mask)) |
| 167 | 166 | { |
| 168 | 167 | if (LOG_GENERAL) |
| 169 | 168 | logerror("pic8259_acknowledge(): PIC acknowledge IRQ #%d\n", irq); |
| 170 | | pic8259->pending &= ~mask; |
| 169 | pic8259->irr &= ~mask; |
| 170 | pic8259->esr &= ~mask; |
| 171 | 171 | if (!pic8259->auto_eoi) |
| 172 | | pic8259->in_service |= mask; |
| 172 | pic8259->isr |= mask; |
| 173 | pic8259_set_timer(pic8259); |
| 173 | 174 | if (pic8259->is_x86) { |
| 174 | 175 | /* For x86 mode*/ |
| 175 | 176 | return irq + pic8259->base; |
| r3875 | r3876 | |
| 194 | 195 | switch(offset) |
| 195 | 196 | { |
| 196 | 197 | case 0: /* PIC acknowledge IRQ */ |
| 197 | | if (pic8259->special) |
| 198 | if ( pic8259->ocw3 & 0x04 ) |
| 198 | 199 | { |
| 199 | | pic8259->special = 0; |
| 200 | | data = pic8259->input; |
| 200 | /* Polling mode */ |
| 201 | if ( pic8259->isr & ~pic8259->imr ) |
| 202 | { |
| 203 | int irq; |
| 204 | |
| 205 | pic8259_acknowledge( device ); |
| 206 | |
| 207 | for ( irq = 0; irq < IRQ_COUNT; irq++ ) |
| 208 | { |
| 209 | if ( ( 1 << irq ) & pic8259->isr & ~pic8259->imr ) |
| 210 | { |
| 211 | data = 0x80 | irq; |
| 212 | break; |
| 213 | } |
| 214 | } |
| 215 | } |
| 201 | 216 | } |
| 217 | else |
| 218 | { |
| 219 | switch ( pic8259->ocw3 & 0x03 ) |
| 220 | { |
| 221 | case 2: |
| 222 | data = pic8259->irr; |
| 223 | break; |
| 224 | case 3: |
| 225 | data = pic8259->isr & ~pic8259->imr; |
| 226 | break; |
| 227 | default: |
| 228 | data = 0x00; |
| 229 | break; |
| 230 | } |
| 231 | } |
| 202 | 232 | break; |
| 203 | 233 | |
| 204 | 234 | case 1: /* PIC mask register */ |
| 205 | | data = pic8259->interrupt_mask; |
| 235 | data = pic8259->imr; |
| 206 | 236 | break; |
| 207 | 237 | } |
| 208 | 238 | return data; |
| r3875 | r3876 | |
| 223 | 253 | if (LOG_ICW) |
| 224 | 254 | logerror("pic8259_w(): ICW1; data=0x%02X\n", data); |
| 225 | 255 | |
| 226 | | pic8259->interrupt_mask = 0x00; |
| 256 | pic8259->imr = 0x00; |
| 257 | pic8259->esr = 0x00; |
| 258 | pic8259->isr = 0x00; |
| 259 | pic8259->irr = 0x00; |
| 227 | 260 | pic8259->level_trig_mode = (data & 0x08) ? 1 : 0; |
| 228 | 261 | pic8259->vector_size = (data & 0x04) ? 1 : 0; |
| 229 | 262 | pic8259->cascade = (data & 0x02) ? 0 : 1; |
| r3875 | r3876 | |
| 239 | 272 | if (LOG_OCW) |
| 240 | 273 | logerror("pic8259_w(): OCW3; data=0x%02X\n", data); |
| 241 | 274 | |
| 242 | | switch (data & 0x03) |
| 243 | | { |
| 244 | | case 0x02: |
| 245 | | pic8259->special = 1; |
| 246 | | pic8259->input = pic8259->pending; |
| 247 | | break; |
| 248 | | case 0x03: |
| 249 | | pic8259->special = 1; |
| 250 | | pic8259->input = pic8259->in_service & ~pic8259->interrupt_mask; |
| 251 | | break; |
| 252 | | } |
| 275 | pic8259->ocw3 = data; |
| 253 | 276 | } |
| 254 | 277 | else if ((data & 0x18) == 0x00) |
| 255 | 278 | { |
| r3875 | r3876 | |
| 268 | 291 | case 0x20: |
| 269 | 292 | for (n = 0, mask = 1<<pic8259->prio; n < 8; n++, mask = (mask<<1) | (mask>>7)) |
| 270 | 293 | { |
| 271 | | if (pic8259->in_service & mask) |
| 294 | if (pic8259->isr & mask) |
| 272 | 295 | { |
| 273 | | pic8259->in_service &= ~mask; |
| 296 | pic8259->isr &= ~mask; |
| 274 | 297 | break; |
| 275 | 298 | } |
| 276 | 299 | } |
| r3875 | r3876 | |
| 278 | 301 | case 0x40: |
| 279 | 302 | break; |
| 280 | 303 | case 0x60: |
| 281 | | if( pic8259->in_service & mask ) |
| 304 | if( pic8259->isr & mask ) |
| 282 | 305 | { |
| 283 | | pic8259->in_service &= ~mask; |
| 306 | pic8259->isr &= ~mask; |
| 284 | 307 | } |
| 285 | 308 | break; |
| 286 | 309 | case 0x80: |
| r3875 | r3876 | |
| 289 | 312 | case 0xa0: |
| 290 | 313 | for (n = 0, mask = 1<<pic8259->prio; n < 8; n++, mask = (mask<<1) | (mask>>7)) |
| 291 | 314 | { |
| 292 | | if( pic8259->in_service & mask ) |
| 315 | if( pic8259->isr & mask ) |
| 293 | 316 | { |
| 294 | | pic8259->in_service &= ~mask; |
| 317 | pic8259->isr &= ~mask; |
| 295 | 318 | pic8259->prio = ++pic8259->prio & 7; |
| 296 | 319 | break; |
| 297 | 320 | } |
| r3875 | r3876 | |
| 301 | 324 | pic8259->prio = n & 7; |
| 302 | 325 | break; |
| 303 | 326 | case 0xe0: |
| 304 | | if( pic8259->in_service & mask ) |
| 327 | if( pic8259->isr & mask ) |
| 305 | 328 | { |
| 306 | | pic8259->in_service &= ~mask; |
| 307 | | pic8259->pending &= ~mask; |
| 329 | pic8259->isr &= ~mask; |
| 330 | pic8259->irr &= ~mask; |
| 308 | 331 | pic8259->prio = ++pic8259->prio & 7; |
| 309 | 332 | } |
| 310 | 333 | break; |
| r3875 | r3876 | |
| 358 | 381 | if (LOG_OCW) |
| 359 | 382 | logerror("pic8259_w(): OCW1; data=0x%02X\n", data); |
| 360 | 383 | |
| 361 | | pic8259->interrupt_mask = data; |
| 384 | pic8259->imr = data; |
| 362 | 385 | break; |
| 363 | 386 | } |
| 364 | 387 | break; |
| r3875 | r3876 | |
| 373 | 396 | |
| 374 | 397 | pic8259->intf = device->static_config; |
| 375 | 398 | |
| 399 | pic8259->timer = timer_alloc( device->machine, pic8259_timerproc, (void *)device ); |
| 400 | |
| 376 | 401 | return DEVICE_START_OK; |
| 377 | 402 | } |
| 378 | 403 | |
| r3875 | r3876 | |
| 380 | 405 | static DEVICE_RESET( pic8259 ) { |
| 381 | 406 | pic8259_t *pic8259 = get_safe_token(device); |
| 382 | 407 | |
| 383 | | pic8259->timer = timer_alloc(device->machine, pic8259_timerproc, (void *)device ); |
| 384 | | |
| 385 | 408 | pic8259->state = STATE_ICW1; /* It is unclear from the original code whether this is correct */ |
| 386 | 409 | pic8259->irq_lines = 0; |
| 387 | | pic8259->in_service = 0; |
| 388 | | pic8259->pending = 0; |
| 410 | pic8259->isr = 0; |
| 411 | pic8259->esr = 0; |
| 412 | pic8259->irr = 0; |
| 389 | 413 | pic8259->prio = 0; |
| 390 | | pic8259->interrupt_mask = 0; |
| 414 | pic8259->imr = 0; |
| 391 | 415 | pic8259->input = 0; |
| 392 | | pic8259->special = 0; |
| 416 | pic8259->ocw3 = 2; |
| 393 | 417 | pic8259->level_trig_mode = 0; |
| 394 | 418 | pic8259->vector_size = 0; |
| 395 | 419 | pic8259->cascade = 0; |