| 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: pic.cpp,v 1.44 2009-05-27 09:15:41 qbix79 Exp $ */ |
| 20 | |
| 21 | #include <list> |
| 22 | |
| 23 | #include "dosbox.h" |
| 24 | #include "inout.h" |
| 25 | #include "cpu.h" |
| 26 | #include "callback.h" |
| 27 | #include "pic.h" |
| 28 | #include "timer.h" |
| 29 | #include "setup.h" |
| 30 | |
| 31 | #define PIC_QUEUESIZE 512 |
| 32 | |
| 33 | struct IRQ_Block { |
| 34 | bool masked; |
| 35 | bool active; |
| 36 | bool inservice; |
| 37 | Bitu vector; |
| 38 | }; |
| 39 | |
| 40 | struct PIC_Controller { |
| 41 | Bitu icw_words; |
| 42 | Bitu icw_index; |
| 43 | Bitu masked; |
| 44 | |
| 45 | bool special; |
| 46 | bool auto_eoi; |
| 47 | bool rotate_on_auto_eoi; |
| 48 | bool single; |
| 49 | bool request_issr; |
| 50 | Bit8u vector_base; |
| 51 | }; |
| 52 | |
| 53 | Bitu PIC_Ticks=0; |
| 54 | Bitu PIC_IRQCheck; |
| 55 | Bitu PIC_IRQOnSecondPicActive; |
| 56 | Bitu PIC_IRQActive; |
| 57 | |
| 58 | |
| 59 | static IRQ_Block irqs[16]; |
| 60 | static PIC_Controller pics[2]; |
| 61 | static bool PIC_Special_Mode = false; //Saves one compare in the pic_run_irqloop |
| 62 | struct PICEntry { |
| 63 | float index; |
| 64 | Bitu value; |
| 65 | PIC_EventHandler pic_event; |
| 66 | PICEntry * next; |
| 67 | }; |
| 68 | |
| 69 | static struct { |
| 70 | PICEntry entries[PIC_QUEUESIZE]; |
| 71 | PICEntry * free_entry; |
| 72 | PICEntry * next_entry; |
| 73 | } pic_queue; |
| 74 | |
| 75 | static void write_command(Bitu port,Bitu val,Bitu iolen) { |
| 76 | PIC_Controller * pic=&pics[port==0x20 ? 0 : 1]; |
| 77 | Bitu irq_base=port==0x20 ? 0 : 8; |
| 78 | Bitu i; |
| 79 | static Bit16u IRQ_priority_table[16] = |
| 80 | { 0,1,2,8,9,10,11,12,13,14,15,3,4,5,6,7 }; |
| 81 | if (GCC_UNLIKELY(val&0x10)) { // ICW1 issued |
| 82 | if (val&0x04) E_Exit("PIC: 4 byte interval not handled"); |
| 83 | if (val&0x08) E_Exit("PIC: level triggered mode not handled"); |
| 84 | if (val&0xe0) E_Exit("PIC: 8080/8085 mode not handled"); |
| 85 | pic->single=(val&0x02)==0x02; |
| 86 | pic->icw_index=1; // next is ICW2 |
| 87 | pic->icw_words=2 + (val&0x01); // =3 if ICW4 needed |
| 88 | } else if (GCC_UNLIKELY(val&0x08)) { // OCW3 issued |
| 89 | if (val&0x04) E_Exit("PIC: poll command not handled"); |
| 90 | if (val&0x02) { // function select |
| 91 | if (val&0x01) pic->request_issr=true; /* select read interrupt in-service register */ |
| 92 | else pic->request_issr=false; /* select read interrupt request register */ |
| 93 | } |
| 94 | if (val&0x40) { // special mask select |
| 95 | if (val&0x20) pic->special = true; |
| 96 | else pic->special = false; |
| 97 | if (pics[0].special || pics[1].special) |
| 98 | PIC_Special_Mode = true; else |
| 99 | PIC_Special_Mode = false; |
| 100 | if (PIC_IRQCheck) { //Recheck irqs |
| 101 | CPU_CycleLeft += CPU_Cycles; |
| 102 | CPU_Cycles = 0; |
| 103 | } |
| 104 | LOG(LOG_PIC,LOG_NORMAL)("port %X : special mask %s",port,(pic->special)?"ON":"OFF"); |
| 105 | } |
| 106 | } else { // OCW2 issued |
| 107 | if (val&0x20) { // EOI commands |
| 108 | if (GCC_UNLIKELY(val&0x80)) E_Exit("rotate mode not supported"); |
| 109 | if (val&0x40) { // specific EOI |
| 110 | if (PIC_IRQActive==(irq_base+val-0x60U)) { |
| 111 | irqs[PIC_IRQActive].inservice=false; |
| 112 | PIC_IRQActive=PIC_NOIRQ; |
| 113 | for (i=0; i<=15; i++) { |
| 114 | if (irqs[IRQ_priority_table[i]].inservice) { |
| 115 | PIC_IRQActive=IRQ_priority_table[i]; |
| 116 | break; |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | // if (val&0x80); // perform rotation |
| 121 | } else { // nonspecific EOI |
| 122 | if (PIC_IRQActive<(irq_base+8)) { |
| 123 | irqs[PIC_IRQActive].inservice=false; |
| 124 | PIC_IRQActive=PIC_NOIRQ; |
| 125 | for (i=0; i<=15; i++){ |
| 126 | if(GCC_UNLIKELY(irqs[IRQ_priority_table[i]].inservice)) { |
| 127 | PIC_IRQActive=IRQ_priority_table[i]; |
| 128 | break; |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | // if (val&0x80); // perform rotation |
| 133 | } |
| 134 | } else { |
| 135 | if ((val&0x40)==0) { // rotate in auto EOI mode |
| 136 | if (val&0x80) pic->rotate_on_auto_eoi=true; |
| 137 | else pic->rotate_on_auto_eoi=false; |
| 138 | } else if (val&0x80) { |
| 139 | LOG(LOG_PIC,LOG_NORMAL)("set priority command not handled"); |
| 140 | } // else NOP command |
| 141 | } |
| 142 | } // end OCW2 |
| 143 | } |
| 144 | |
| 145 | static void write_data(Bitu port,Bitu val,Bitu iolen) { |
| 146 | PIC_Controller * pic=&pics[port==0x21 ? 0 : 1]; |
| 147 | Bitu irq_base=(port==0x21) ? 0 : 8; |
| 148 | Bitu i; |
| 149 | bool old_irq2_mask = irqs[2].masked; |
| 150 | switch(pic->icw_index) { |
| 151 | case 0: /* mask register */ |
| 152 | LOG(LOG_PIC,LOG_NORMAL)("%d mask %X",port==0x21 ? 0 : 1,val); |
| 153 | for (i=0;i<=7;i++) { |
| 154 | irqs[i+irq_base].masked=(val&(1<<i))>0; |
| 155 | if(port==0x21) { |
| 156 | if (irqs[i+irq_base].active && !irqs[i+irq_base].masked) PIC_IRQCheck|=(1 << (i+irq_base)); |
| 157 | else PIC_IRQCheck&=~(1 << (i+irq_base)); |
| 158 | } else { |
| 159 | if (irqs[i+irq_base].active && !irqs[i+irq_base].masked && !irqs[2].masked) PIC_IRQCheck|=(1 << (i+irq_base)); |
| 160 | else PIC_IRQCheck&=~(1 << (i+irq_base)); |
| 161 | } |
| 162 | } |
| 163 | if (machine==MCH_PCJR) { |
| 164 | /* irq6 cannot be disabled as it serves as pseudo-NMI */ |
| 165 | irqs[6].masked=false; |
| 166 | } |
| 167 | if(irqs[2].masked != old_irq2_mask) { |
| 168 | /* Irq 2 mask has changed recheck second pic */ |
| 169 | for(i=8;i<=15;i++) { |
| 170 | if (irqs[i].active && !irqs[i].masked && !irqs[2].masked) PIC_IRQCheck|=(1 << (i)); |
| 171 | else PIC_IRQCheck&=~(1 << (i)); |
| 172 | } |
| 173 | } |
| 174 | if (PIC_IRQCheck) { |
| 175 | CPU_CycleLeft+=CPU_Cycles; |
| 176 | CPU_Cycles=0; |
| 177 | } |
| 178 | break; |
| 179 | case 1: /* icw2 */ |
| 180 | LOG(LOG_PIC,LOG_NORMAL)("%d:Base vector %X",port==0x21 ? 0 : 1,val); |
| 181 | for (i=0;i<=7;i++) { |
| 182 | irqs[i+irq_base].vector=(val&0xf8)+i; |
| 183 | }; |
| 184 | if(pic->icw_index++ >= pic->icw_words) pic->icw_index=0; |
| 185 | else if(pic->single) pic->icw_index=3; /* skip ICW3 in single mode */ |
| 186 | break; |
| 187 | case 2: /* icw 3 */ |
| 188 | LOG(LOG_PIC,LOG_NORMAL)("%d:ICW 3 %X",port==0x21 ? 0 : 1,val); |
| 189 | if(pic->icw_index++ >= pic->icw_words) pic->icw_index=0; |
| 190 | break; |
| 191 | case 3: /* icw 4 */ |
| 192 | /* |
| 193 | 0 1 8086/8080 0 mcs-8085 mode |
| 194 | 1 1 Auto EOI 0 Normal EOI |
| 195 | 2-3 0x Non buffer Mode |
| 196 | 10 Buffer Mode Slave |
| 197 | 11 Buffer mode Master |
| 198 | 4 Special/Not Special nested mode |
| 199 | */ |
| 200 | pic->auto_eoi=(val & 0x2)>0; |
| 201 | |
| 202 | LOG(LOG_PIC,LOG_NORMAL)("%d:ICW 4 %X",port==0x21 ? 0 : 1,val); |
| 203 | |
| 204 | if ((val&0x01)==0) E_Exit("PIC:ICW4: %x, 8085 mode not handled",val); |
| 205 | if ((val&0x10)!=0) LOG_MSG("PIC:ICW4: %x, special fully-nested mode not handled",val); |
| 206 | |
| 207 | if(pic->icw_index++ >= pic->icw_words) pic->icw_index=0; |
| 208 | break; |
| 209 | default: |
| 210 | LOG(LOG_PIC,LOG_NORMAL)("ICW HUH? %X",val); |
| 211 | break; |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | |
| 216 | static Bitu read_command(Bitu port,Bitu iolen) { |
| 217 | PIC_Controller * pic=&pics[port==0x20 ? 0 : 1]; |
| 218 | Bitu irq_base=(port==0x20) ? 0 : 8; |
| 219 | Bitu i;Bit8u ret=0;Bit8u b=1; |
| 220 | if (pic->request_issr) { |
| 221 | for (i=irq_base;i<irq_base+8;i++) { |
| 222 | if (irqs[i].inservice) ret|=b; |
| 223 | b <<= 1; |
| 224 | } |
| 225 | } else { |
| 226 | for (i=irq_base;i<irq_base+8;i++) { |
| 227 | if (irqs[i].active) ret|=b; |
| 228 | b <<= 1; |
| 229 | } |
| 230 | if (irq_base==0 && (PIC_IRQCheck&0xff00)) ret |=4; |
| 231 | } |
| 232 | return ret; |
| 233 | } |
| 234 | |
| 235 | static Bitu read_data(Bitu port,Bitu iolen) { |
| 236 | Bitu irq_base=(port==0x21) ? 0 : 8; |
| 237 | Bitu i;Bit8u ret=0;Bit8u b=1; |
| 238 | for (i=irq_base;i<=irq_base+7;i++) { |
| 239 | if (irqs[i].masked) ret|=b; |
| 240 | b <<= 1; |
| 241 | } |
| 242 | return ret; |
| 243 | } |
| 244 | |
| 245 | |
| 246 | void PIC_ActivateIRQ(Bitu irq) { |
| 247 | if( irq < 8 ) { |
| 248 | irqs[irq].active = true; |
| 249 | if (!irqs[irq].masked) { |
| 250 | PIC_IRQCheck|=(1 << irq); |
| 251 | } |
| 252 | } else if (irq < 16) { |
| 253 | irqs[irq].active = true; |
| 254 | PIC_IRQOnSecondPicActive|=(1 << irq); |
| 255 | if (!irqs[irq].masked && !irqs[2].masked) { |
| 256 | PIC_IRQCheck|=(1 << irq); |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | void PIC_DeActivateIRQ(Bitu irq) { |
| 262 | if (irq<16) { |
| 263 | irqs[irq].active=false; |
| 264 | PIC_IRQCheck&=~(1 << irq); |
| 265 | PIC_IRQOnSecondPicActive&=~(1 << irq); |
| 266 | } |
| 267 | } |
| 268 | static inline bool PIC_startIRQ(Bitu i) { |
| 269 | /* irqs on second pic only if irq 2 isn't masked */ |
| 270 | if( i > 7 && irqs[2].masked) return false; |
| 271 | irqs[i].active = false; |
| 272 | PIC_IRQCheck&= ~(1 << i); |
| 273 | PIC_IRQOnSecondPicActive&= ~(1 << i); |
| 274 | CPU_HW_Interrupt(irqs[i].vector); |
| 275 | Bitu pic=(i&8)>>3; |
| 276 | if (!pics[pic].auto_eoi) { //irq 0-7 => pic 0 else pic 1 |
| 277 | PIC_IRQActive = i; |
| 278 | irqs[i].inservice = true; |
| 279 | } else if (GCC_UNLIKELY(pics[pic].rotate_on_auto_eoi)) { |
| 280 | E_Exit("rotate on auto EOI not handled"); |
| 281 | } |
| 282 | return true; |
| 283 | } |
| 284 | |
| 285 | void PIC_runIRQs(void) { |
| 286 | if (!GETFLAG(IF)) return; |
| 287 | if (GCC_UNLIKELY(!PIC_IRQCheck)) return; |
| 288 | if (GCC_UNLIKELY(cpudecoder==CPU_Core_Normal_Trap_Run)) return; |
| 289 | |
| 290 | static Bitu IRQ_priority_order[16] = |
| 291 | { 0,1,2,8,9,10,11,12,13,14,15,3,4,5,6,7 }; |
| 292 | static Bit16u IRQ_priority_lookup[17] = |
| 293 | { 0,1,2,11,12,13,14,15,3,4,5,6,7,8,9,10,16 }; |
| 294 | Bit16u activeIRQ = PIC_IRQActive; |
| 295 | if (activeIRQ == PIC_NOIRQ) activeIRQ = 16; |
| 296 | /* Get the priority of the active irq */ |
| 297 | Bit16u Priority_Active_IRQ = IRQ_priority_lookup[activeIRQ]; |
| 298 | |
| 299 | Bitu i,j; |
| 300 | /* j is the priority (walker) |
| 301 | * i is the irq at the current priority */ |
| 302 | |
| 303 | /* If one of the pics is in special mode use a check that cares for that. */ |
| 304 | if(!PIC_Special_Mode) { |
| 305 | for (j = 0; j < Priority_Active_IRQ; j++) { |
| 306 | i = IRQ_priority_order[j]; |
| 307 | if (!irqs[i].masked && irqs[i].active) { |
| 308 | if(GCC_LIKELY(PIC_startIRQ(i))) return; |
| 309 | } |
| 310 | } |
| 311 | } else { /* Special mode variant */ |
| 312 | for (j = 0; j<= 15; j++) { |
| 313 | i = IRQ_priority_order[j]; |
| 314 | if ( (j < Priority_Active_IRQ) || (pics[ ((i&8)>>3) ].special) ) { |
| 315 | if (!irqs[i].masked && irqs[i].active) { |
| 316 | /* the irq line is active. it's not masked and |
| 317 | * the irq is allowed priority wise. So let's start it */ |
| 318 | /* If started successfully return, else go for the next */ |
| 319 | if(PIC_startIRQ(i)) return; |
| 320 | } |
| 321 | } |
| 322 | } |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | void PIC_SetIRQMask(Bitu irq, bool masked) { |
| 327 | if(irqs[irq].masked == masked) return; /* Do nothing if mask doesn't change */ |
| 328 | bool old_irq2_mask = irqs[2].masked; |
| 329 | irqs[irq].masked=masked; |
| 330 | if(irq < 8) { |
| 331 | if (irqs[irq].active && !irqs[irq].masked) { |
| 332 | PIC_IRQCheck|=(1 << (irq)); |
| 333 | } else { |
| 334 | PIC_IRQCheck&=~(1 << (irq)); |
| 335 | } |
| 336 | } else { |
| 337 | if (irqs[irq].active && !irqs[irq].masked && !irqs[2].masked) { |
| 338 | PIC_IRQCheck|=(1 << (irq)); |
| 339 | } else { |
| 340 | PIC_IRQCheck&=~(1 << (irq)); |
| 341 | } |
| 342 | } |
| 343 | if(irqs[2].masked != old_irq2_mask) { |
| 344 | /* Irq 2 mask has changed recheck second pic */ |
| 345 | for(Bitu i=8;i<=15;i++) { |
| 346 | if (irqs[i].active && !irqs[i].masked && !irqs[2].masked) PIC_IRQCheck|=(1 << (i)); |
| 347 | else PIC_IRQCheck&=~(1 << (i)); |
| 348 | } |
| 349 | } |
| 350 | if (PIC_IRQCheck) { |
| 351 | CPU_CycleLeft+=CPU_Cycles; |
| 352 | CPU_Cycles=0; |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | static void AddEntry(PICEntry * entry) { |
| 357 | PICEntry * find_entry=pic_queue.next_entry; |
| 358 | if (GCC_UNLIKELY(find_entry ==0)) { |
| 359 | entry->next=0; |
| 360 | pic_queue.next_entry=entry; |
| 361 | } else if (find_entry->index>entry->index) { |
| 362 | pic_queue.next_entry=entry; |
| 363 | entry->next=find_entry; |
| 364 | } else while (find_entry) { |
| 365 | if (find_entry->next) { |
| 366 | /* See if the next index comes later than this one */ |
| 367 | if (find_entry->next->index > entry->index) { |
| 368 | entry->next=find_entry->next; |
| 369 | find_entry->next=entry; |
| 370 | break; |
| 371 | } else { |
| 372 | find_entry=find_entry->next; |
| 373 | } |
| 374 | } else { |
| 375 | entry->next=find_entry->next; |
| 376 | find_entry->next=entry; |
| 377 | break; |
| 378 | } |
| 379 | } |
| 380 | Bits cycles=PIC_MakeCycles(pic_queue.next_entry->index-PIC_TickIndex()); |
| 381 | if (cycles<CPU_Cycles) { |
| 382 | CPU_CycleLeft+=CPU_Cycles; |
| 383 | CPU_Cycles=0; |
| 384 | } |
| 385 | } |
| 386 | static bool InEventService = false; |
| 387 | static float srv_lag = 0; |
| 388 | |
| 389 | void PIC_AddEvent(PIC_EventHandler handler,float delay,Bitu val) { |
| 390 | if (GCC_UNLIKELY(!pic_queue.free_entry)) { |
| 391 | LOG(LOG_PIC,LOG_ERROR)("Event queue full"); |
| 392 | return; |
| 393 | } |
| 394 | PICEntry * entry=pic_queue.free_entry; |
| 395 | if(InEventService) entry->index = delay + srv_lag; |
| 396 | else entry->index = delay + PIC_TickIndex(); |
| 397 | |
| 398 | entry->pic_event=handler; |
| 399 | entry->value=val; |
| 400 | pic_queue.free_entry=pic_queue.free_entry->next; |
| 401 | AddEntry(entry); |
| 402 | } |
| 403 | |
| 404 | void PIC_RemoveSpecificEvents(PIC_EventHandler handler, Bitu val) { |
| 405 | PICEntry * entry=pic_queue.next_entry; |
| 406 | PICEntry * prev_entry; |
| 407 | prev_entry = 0; |
| 408 | while (entry) { |
| 409 | if (GCC_UNLIKELY((entry->pic_event == handler)) && (entry->value == val)) { |
| 410 | if (prev_entry) { |
| 411 | prev_entry->next=entry->next; |
| 412 | entry->next=pic_queue.free_entry; |
| 413 | pic_queue.free_entry=entry; |
| 414 | entry=prev_entry->next; |
| 415 | continue; |
| 416 | } else { |
| 417 | pic_queue.next_entry=entry->next; |
| 418 | entry->next=pic_queue.free_entry; |
| 419 | pic_queue.free_entry=entry; |
| 420 | entry=pic_queue.next_entry; |
| 421 | continue; |
| 422 | } |
| 423 | } |
| 424 | prev_entry=entry; |
| 425 | entry=entry->next; |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | void PIC_RemoveEvents(PIC_EventHandler handler) { |
| 430 | PICEntry * entry=pic_queue.next_entry; |
| 431 | PICEntry * prev_entry; |
| 432 | prev_entry=0; |
| 433 | while (entry) { |
| 434 | if (GCC_UNLIKELY(entry->pic_event==handler)) { |
| 435 | if (prev_entry) { |
| 436 | prev_entry->next=entry->next; |
| 437 | entry->next=pic_queue.free_entry; |
| 438 | pic_queue.free_entry=entry; |
| 439 | entry=prev_entry->next; |
| 440 | continue; |
| 441 | } else { |
| 442 | pic_queue.next_entry=entry->next; |
| 443 | entry->next=pic_queue.free_entry; |
| 444 | pic_queue.free_entry=entry; |
| 445 | entry=pic_queue.next_entry; |
| 446 | continue; |
| 447 | } |
| 448 | } |
| 449 | prev_entry=entry; |
| 450 | entry=entry->next; |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | |
| 455 | bool PIC_RunQueue(void) { |
| 456 | /* Check to see if a new milisecond needs to be started */ |
| 457 | CPU_CycleLeft+=CPU_Cycles; |
| 458 | CPU_Cycles=0; |
| 459 | if (CPU_CycleLeft<=0) { |
| 460 | return false; |
| 461 | } |
| 462 | /* Check the queue for an entry */ |
| 463 | Bits index_nd=PIC_TickIndexND(); |
| 464 | InEventService = true; |
| 465 | while (pic_queue.next_entry && (pic_queue.next_entry->index*CPU_CycleMax<=index_nd)) { |
| 466 | PICEntry * entry=pic_queue.next_entry; |
| 467 | pic_queue.next_entry=entry->next; |
| 468 | |
| 469 | srv_lag = entry->index; |
| 470 | (entry->pic_event)(entry->value); // call the event handler |
| 471 | |
| 472 | /* Put the entry in the free list */ |
| 473 | entry->next=pic_queue.free_entry; |
| 474 | pic_queue.free_entry=entry; |
| 475 | } |
| 476 | InEventService = false; |
| 477 | |
| 478 | /* Check when to set the new cycle end */ |
| 479 | if (pic_queue.next_entry) { |
| 480 | Bits cycles=(Bits)(pic_queue.next_entry->index*CPU_CycleMax-index_nd); |
| 481 | if (GCC_UNLIKELY(!cycles)) cycles=1; |
| 482 | if (cycles<CPU_CycleLeft) { |
| 483 | CPU_Cycles=cycles; |
| 484 | } else { |
| 485 | CPU_Cycles=CPU_CycleLeft; |
| 486 | } |
| 487 | } else CPU_Cycles=CPU_CycleLeft; |
| 488 | CPU_CycleLeft-=CPU_Cycles; |
| 489 | if (PIC_IRQCheck) PIC_runIRQs(); |
| 490 | return true; |
| 491 | } |
| 492 | |
| 493 | /* The TIMER Part */ |
| 494 | struct TickerBlock { |
| 495 | TIMER_TickHandler handler; |
| 496 | TickerBlock * next; |
| 497 | }; |
| 498 | |
| 499 | static TickerBlock * firstticker=0; |
| 500 | |
| 501 | |
| 502 | void TIMER_DelTickHandler(TIMER_TickHandler handler) { |
| 503 | TickerBlock * ticker=firstticker; |
| 504 | TickerBlock * * tick_where=&firstticker; |
| 505 | while (ticker) { |
| 506 | if (ticker->handler==handler) { |
| 507 | *tick_where=ticker->next; |
| 508 | delete ticker; |
| 509 | return; |
| 510 | } |
| 511 | tick_where=&ticker->next; |
| 512 | ticker=ticker->next; |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | void TIMER_AddTickHandler(TIMER_TickHandler handler) { |
| 517 | TickerBlock * newticker=new TickerBlock; |
| 518 | newticker->next=firstticker; |
| 519 | newticker->handler=handler; |
| 520 | firstticker=newticker; |
| 521 | } |
| 522 | |
| 523 | void TIMER_AddTick(void) { |
| 524 | /* Setup new amount of cycles for PIC */ |
| 525 | CPU_CycleLeft=CPU_CycleMax; |
| 526 | CPU_Cycles=0; |
| 527 | PIC_Ticks++; |
| 528 | /* Go through the list of scheduled events and lower their index with 1000 */ |
| 529 | PICEntry * entry=pic_queue.next_entry; |
| 530 | while (entry) { |
| 531 | entry->index -= 1.0; |
| 532 | entry=entry->next; |
| 533 | } |
| 534 | /* Call our list of ticker handlers */ |
| 535 | TickerBlock * ticker=firstticker; |
| 536 | while (ticker) { |
| 537 | TickerBlock * nextticker=ticker->next; |
| 538 | ticker->handler(); |
| 539 | ticker=nextticker; |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | /* Use full name to avoid name clash with compile option for position-independent code */ |
| 544 | class PIC_8259A: public Module_base { |
| 545 | private: |
| 546 | IO_ReadHandleObject ReadHandler[4]; |
| 547 | IO_WriteHandleObject WriteHandler[4]; |
| 548 | public: |
| 549 | PIC_8259A(Section* configuration):Module_base(configuration){ |
| 550 | /* Setup pic0 and pic1 with initial values like DOS has normally */ |
| 551 | PIC_IRQCheck=0; |
| 552 | PIC_IRQActive=PIC_NOIRQ; |
| 553 | PIC_Ticks=0; |
| 554 | Bitu i; |
| 555 | for (i=0;i<2;i++) { |
| 556 | pics[i].masked=0xff; |
| 557 | pics[i].auto_eoi=false; |
| 558 | pics[i].rotate_on_auto_eoi=false; |
| 559 | pics[i].request_issr=false; |
| 560 | pics[i].special=false; |
| 561 | pics[i].single=false; |
| 562 | pics[i].icw_index=0; |
| 563 | pics[i].icw_words=0; |
| 564 | } |
| 565 | for (i=0;i<=7;i++) { |
| 566 | irqs[i].active=false; |
| 567 | irqs[i].masked=true; |
| 568 | irqs[i].inservice=false; |
| 569 | irqs[i+8].active=false; |
| 570 | irqs[i+8].masked=true; |
| 571 | irqs[i+8].inservice=false; |
| 572 | irqs[i].vector=0x8+i; |
| 573 | irqs[i+8].vector=0x70+i; |
| 574 | } |
| 575 | irqs[0].masked=false; /* Enable system timer */ |
| 576 | irqs[1].masked=false; /* Enable Keyboard IRQ */ |
| 577 | irqs[2].masked=false; /* Enable second pic */ |
| 578 | irqs[8].masked=false; /* Enable RTC IRQ */ |
| 579 | if (machine==MCH_PCJR) { |
| 580 | /* Enable IRQ6 (replacement for the NMI for PCJr) */ |
| 581 | irqs[6].masked=false; |
| 582 | } |
| 583 | ReadHandler[0].Install(0x20,read_command,IO_MB); |
| 584 | ReadHandler[1].Install(0x21,read_data,IO_MB); |
| 585 | WriteHandler[0].Install(0x20,write_command,IO_MB); |
| 586 | WriteHandler[1].Install(0x21,write_data,IO_MB); |
| 587 | ReadHandler[2].Install(0xa0,read_command,IO_MB); |
| 588 | ReadHandler[3].Install(0xa1,read_data,IO_MB); |
| 589 | WriteHandler[2].Install(0xa0,write_command,IO_MB); |
| 590 | WriteHandler[3].Install(0xa1,write_data,IO_MB); |
| 591 | /* Initialize the pic queue */ |
| 592 | for (i=0;i<PIC_QUEUESIZE-1;i++) { |
| 593 | pic_queue.entries[i].next=&pic_queue.entries[i+1]; |
| 594 | } |
| 595 | pic_queue.entries[PIC_QUEUESIZE-1].next=0; |
| 596 | pic_queue.free_entry=&pic_queue.entries[0]; |
| 597 | pic_queue.next_entry=0; |
| 598 | } |
| 599 | ~PIC_8259A(){ |
| 600 | } |
| 601 | }; |
| 602 | |
| 603 | static PIC_8259A* test; |
| 604 | |
| 605 | void PIC_Destroy(Section* sec){ |
| 606 | delete test; |
| 607 | } |
| 608 | |
| 609 | void PIC_Init(Section* sec) { |
| 610 | test = new PIC_8259A(sec); |
| 611 | sec->AddDestroyFunction(&PIC_Destroy); |
| 612 | } |