1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 * Mupen64plus - interupt.c *
3 * Mupen64Plus homepage: http://code.google.com/p/mupen64plus/ *
4 * Copyright (C) 2002 Hacktarux *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the *
18 * Free Software Foundation, Inc., *
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
20 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
26 #define M64P_CORE_PROTOTYPES 1
27 #include "api/m64p_types.h"
28 #include "api/callbacks.h"
29 #include "api/m64p_vidext.h"
30 #include "api/vidext.h"
31 #include "memory/memory.h"
33 #include "main/main.h"
34 #include "main/savestates.h"
35 #include "main/cheat.h"
37 #include "plugin/plugin.h"
42 #include "exception.h"
44 #include "new_dynarec/new_dynarec.h"
47 #include "main/lirc.h"
52 static int vi_counter=0;
54 int interupt_unsafe_state = 0;
56 typedef struct _interupt_queue
60 struct _interupt_queue *next;
63 static interupt_queue *q = NULL;
65 static void clear_queue(void)
69 interupt_queue *aux = q->next;
75 /*static void print_queue(void)
78 //if (Count < 0x7000000) return;
79 DebugMessage(M64MSG_INFO, "------------------ 0x%x", (unsigned int)Count);
83 DebugMessage(M64MSG_INFO, "Count:%x, %x", (unsigned int)aux->count, aux->type);
88 static int SPECIAL_done = 0;
90 static int before_event(unsigned int evt1, unsigned int evt2, int type2)
92 if(evt1 - Count < 0x80000000)
94 if(evt2 - Count < 0x80000000)
96 if((evt1 - Count) < (evt2 - Count)) return 1;
101 if((Count - evt2) < 0x10000000)
106 if(SPECIAL_done) return 1;
119 void add_interupt_event(int type, unsigned int delay)
121 unsigned int count = Count + delay/**2*/;
123 interupt_queue *aux = q;
125 if(type == SPECIAL_INT /*|| type == COMPARE_INT*/) special = 1;
126 if(Count > 0x80000000) SPECIAL_done = 0;
128 if (get_event(type)) {
129 DebugMessage(M64MSG_WARNING, "two events of type 0x%x in interrupt queue", type);
134 q = (interupt_queue *) malloc(sizeof(interupt_queue));
138 next_interupt = q->count;
143 if(before_event(count, q->count, q->type) && !special)
145 q = (interupt_queue *) malloc(sizeof(interupt_queue));
149 next_interupt = q->count;
154 while (aux->next != NULL && (!before_event(count, aux->next->count, aux->next->type) || special))
157 if (aux->next == NULL)
159 aux->next = (interupt_queue *) malloc(sizeof(interupt_queue));
167 interupt_queue *aux2;
168 if (type != SPECIAL_INT)
169 while(aux->next != NULL && aux->next->count == count)
172 aux->next = (interupt_queue *) malloc(sizeof(interupt_queue));
180 void add_interupt_event_count(int type, unsigned int count)
182 add_interupt_event(type, (count - Count)/*/2*/);
185 static void remove_interupt_event(void)
187 interupt_queue *aux = q->next;
188 if(q->type == SPECIAL_INT) SPECIAL_done = 1;
191 if (q != NULL && (q->count > Count || (Count - q->count) < 0x80000000))
192 next_interupt = q->count;
197 unsigned int get_event(int type)
199 interupt_queue *aux = q;
200 if (q == NULL) return 0;
203 while (aux->next != NULL && aux->next->type != type)
205 if (aux->next != NULL)
206 return aux->next->count;
210 int get_next_event_type(void)
212 if (q == NULL) return 0;
216 void remove_event(int type)
218 interupt_queue *aux = q;
219 if (q == NULL) return;
227 while (aux->next != NULL && aux->next->type != type)
229 if (aux->next != NULL) // it's a type int
231 interupt_queue *aux2 = aux->next->next;
237 void translate_event_queue(unsigned int base)
240 remove_event(COMPARE_INT);
241 remove_event(SPECIAL_INT);
245 aux->count = (aux->count - Count)+base;
248 add_interupt_event_count(COMPARE_INT, Compare);
249 add_interupt_event_count(SPECIAL_INT, 0);
252 int save_eventqueue_infos(char *buf)
255 interupt_queue *aux = q;
258 *((unsigned int*)&buf[0]) = 0xFFFFFFFF;
263 memcpy(buf+len , &aux->type , 4);
264 memcpy(buf+len+4, &aux->count, 4);
268 *((unsigned int*)&buf[len]) = 0xFFFFFFFF;
272 void load_eventqueue_infos(char *buf)
276 while (*((unsigned int*)&buf[len]) != 0xFFFFFFFF)
278 int type = *((unsigned int*)&buf[len]);
279 unsigned int count = *((unsigned int*)&buf[len+4]);
280 add_interupt_event_count(type, count);
285 void init_interupt(void)
288 next_vi = next_interupt = 5000;
289 vi_register.vi_delay = next_vi;
292 add_interupt_event_count(VI_INT, next_vi);
293 add_interupt_event_count(SPECIAL_INT, 0);
296 void check_interupt(void)
298 if (MI_register.mi_intr_reg & MI_register.mi_intr_mask_reg)
299 Cause = (Cause | 0x400) & 0xFFFFFF83;
302 if ((Status & 7) != 1) return;
303 if (Status & Cause & 0xFF00)
307 q = (interupt_queue *) malloc(sizeof(interupt_queue));
314 interupt_queue* aux = (interupt_queue *) malloc(sizeof(interupt_queue));
317 aux->type = CHECK_INT;
320 next_interupt = Count;
324 void gen_interupt(void)
328 vi_counter = 0; // debug
332 if (!interupt_unsafe_state)
334 if (savestates_get_job() == savestates_job_load)
350 unsigned int dest = skip_jump;
353 if (q->count > Count || (Count - q->count) < 0x80000000)
354 next_interupt = q->count;
359 generic_jump_to(dest);
366 if (Count > 0x10000000) return;
367 remove_interupt_event();
368 add_interupt_event_count(SPECIAL_INT, 0);
375 cheat_apply_cheats(ENTRY_BOOT);
380 cheat_apply_cheats(ENTRY_VI);
390 // if paused, poll for input events
393 osd_render(); // draw Paused message in case gfx.updateScreen didn't do it
394 VidExt_GL_SwapBuffers();
406 if (vi_register.vi_v_sync == 0) vi_register.vi_delay = 500000;
407 else vi_register.vi_delay = ((vi_register.vi_v_sync + 1)*1500);
408 next_vi += vi_register.vi_delay;
409 if (vi_register.vi_status&0x40) vi_field=1-vi_field;
412 remove_interupt_event();
413 add_interupt_event_count(VI_INT, next_vi);
415 MI_register.mi_intr_reg |= 0x08;
416 if (MI_register.mi_intr_reg & MI_register.mi_intr_mask_reg)
417 Cause = (Cause | 0x400) & 0xFFFFFF83;
420 if ((Status & 7) != 1) return;
421 if (!(Status & Cause & 0xFF00)) return;
425 remove_interupt_event();
427 add_interupt_event_count(COMPARE_INT, Compare);
430 Cause = (Cause | 0x8000) & 0xFFFFFF83;
431 if ((Status & 7) != 1) return;
432 if (!(Status & Cause & 0xFF00)) return;
436 remove_interupt_event();
444 PIF_RAMb[0x3F] = 0x0;
445 remove_interupt_event();
446 MI_register.mi_intr_reg |= 0x02;
447 si_register.si_stat |= 0x1000;
448 if (MI_register.mi_intr_reg & MI_register.mi_intr_mask_reg)
449 Cause = (Cause | 0x400) & 0xFFFFFF83;
452 if ((Status & 7) != 1) return;
453 if (!(Status & Cause & 0xFF00)) return;
457 remove_interupt_event();
458 MI_register.mi_intr_reg |= 0x10;
459 pi_register.read_pi_status_reg &= ~3;
460 if (MI_register.mi_intr_reg & MI_register.mi_intr_mask_reg)
461 Cause = (Cause | 0x400) & 0xFFFFFF83;
464 if ((Status & 7) != 1) return;
465 if (!(Status & Cause & 0xFF00)) return;
469 if (ai_register.ai_status & 0x80000000) // full
471 unsigned int ai_event = get_event(AI_INT);
472 remove_interupt_event();
473 ai_register.ai_status &= ~0x80000000;
474 ai_register.current_delay = ai_register.next_delay;
475 ai_register.current_len = ai_register.next_len;
476 add_interupt_event_count(AI_INT, ai_event+ai_register.next_delay);
478 MI_register.mi_intr_reg |= 0x04;
479 if (MI_register.mi_intr_reg & MI_register.mi_intr_mask_reg)
480 Cause = (Cause | 0x400) & 0xFFFFFF83;
483 if ((Status & 7) != 1) return;
484 if (!(Status & Cause & 0xFF00)) return;
488 remove_interupt_event();
489 ai_register.ai_status &= ~0x40000000;
492 MI_register.mi_intr_reg |= 0x04;
493 if (MI_register.mi_intr_reg & MI_register.mi_intr_mask_reg)
494 Cause = (Cause | 0x400) & 0xFFFFFF83;
497 if ((Status & 7) != 1) return;
498 if (!(Status & Cause & 0xFF00)) return;
503 remove_interupt_event();
504 sp_register.sp_status_reg |= 0x203;
505 // sp_register.sp_status_reg |= 0x303;
507 if (!(sp_register.sp_status_reg & 0x40)) return; // !intr_on_break
508 MI_register.mi_intr_reg |= 0x01;
509 if (MI_register.mi_intr_reg & MI_register.mi_intr_mask_reg)
510 Cause = (Cause | 0x400) & 0xFFFFFF83;
513 if ((Status & 7) != 1) return;
514 if (!(Status & Cause & 0xFF00)) return;
518 remove_interupt_event();
519 dpc_register.dpc_status &= ~2;
520 dpc_register.dpc_status |= 0x81;
521 MI_register.mi_intr_reg |= 0x20;
522 if (MI_register.mi_intr_reg & MI_register.mi_intr_mask_reg)
523 Cause = (Cause | 0x400) & 0xFFFFFF83;
526 if ((Status & 7) != 1) return;
527 if (!(Status & Cause & 0xFF00)) return;
531 // Hardware Interrupt 2 -- remove interrupt event from queue
532 remove_interupt_event();
533 // setup r4300 Status flags: reset TS, and SR, set IM2
534 Status = (Status & ~0x00380000) | 0x1000;
535 Cause = (Cause | 0x1000) & 0xFFFFFF83;
536 /* the exception_general() call below will jump to the interrupt vector (0x80000180) and setup the
537 * interpreter or dynarec
542 // Non Maskable Interrupt -- remove interrupt event from queue
543 remove_interupt_event();
544 // setup r4300 Status flags: reset TS and SR, set BEV, ERL, and SR
545 Status = (Status & ~0x00380000) | 0x00500004;
547 // simulate the soft reset code which would run from the PIF ROM
549 // clear all interrupts, reset interrupt counters back to 0
553 // clear the audio status register so that subsequent write_ai() calls will work properly
554 ai_register.ai_status = 0;
555 // set ErrorEPC with the last instruction address
557 // reset the r4300 internal state
558 if (r4300emu != CORE_PURE_INTERPRETER)
560 // clear all the compiled instruction blocks and re-initialize
564 // adjust ErrorEPC if we were in a delay slot, and clear the delay_slot and dyna_interp flags
565 if(delay_slot==1 || delay_slot==3)
571 // set next instruction address to reset vector
572 last_addr = 0xa4000040;
573 generic_jump_to(0xa4000040);
577 DebugMessage(M64MSG_ERROR, "Unknown interrupt queue event type %.8X.", q->type);
578 remove_interupt_event();
583 if (r4300emu == CORE_DYNAREC) {
596 if (!interupt_unsafe_state)
598 if (savestates_get_job() == savestates_job_save)