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);
130 return; // silently return...
135 q = (interupt_queue *) malloc(sizeof(interupt_queue));
139 next_interupt = q->count;
144 if(before_event(count, q->count, q->type) && !special)
146 q = (interupt_queue *) malloc(sizeof(interupt_queue));
150 next_interupt = q->count;
155 while (aux->next != NULL && (!before_event(count, aux->next->count, aux->next->type) || special))
158 if (aux->next == NULL)
160 aux->next = (interupt_queue *) malloc(sizeof(interupt_queue));
168 interupt_queue *aux2;
169 if (type != SPECIAL_INT)
170 while(aux->next != NULL && aux->next->count == count)
173 aux->next = (interupt_queue *) malloc(sizeof(interupt_queue));
181 void add_interupt_event_count(int type, unsigned int count)
183 add_interupt_event(type, (count - Count)/*/2*/);
186 static void remove_interupt_event(void)
188 interupt_queue *aux = q->next;
189 if(q->type == SPECIAL_INT) SPECIAL_done = 1;
192 if (q != NULL && (q->count > Count || (Count - q->count) < 0x80000000))
193 next_interupt = q->count;
198 unsigned int get_event(int type)
200 interupt_queue *aux = q;
201 if (q == NULL) return 0;
204 while (aux->next != NULL && aux->next->type != type)
206 if (aux->next != NULL)
207 return aux->next->count;
211 int get_next_event_type(void)
213 if (q == NULL) return 0;
217 void remove_event(int type)
219 interupt_queue *aux = q;
220 if (q == NULL) return;
228 while (aux->next != NULL && aux->next->type != type)
230 if (aux->next != NULL) // it's a type int
232 interupt_queue *aux2 = aux->next->next;
238 void translate_event_queue(unsigned int base)
241 remove_event(COMPARE_INT);
242 remove_event(SPECIAL_INT);
246 aux->count = (aux->count - Count)+base;
249 add_interupt_event_count(COMPARE_INT, Compare);
250 add_interupt_event_count(SPECIAL_INT, 0);
253 int save_eventqueue_infos(char *buf)
256 interupt_queue *aux = q;
259 *((unsigned int*)&buf[0]) = 0xFFFFFFFF;
264 memcpy(buf+len , &aux->type , 4);
265 memcpy(buf+len+4, &aux->count, 4);
269 *((unsigned int*)&buf[len]) = 0xFFFFFFFF;
273 void load_eventqueue_infos(char *buf)
277 while (*((unsigned int*)&buf[len]) != 0xFFFFFFFF)
279 int type = *((unsigned int*)&buf[len]);
280 unsigned int count = *((unsigned int*)&buf[len+4]);
281 add_interupt_event_count(type, count);
286 void init_interupt(void)
289 next_vi = next_interupt = 5000;
290 vi_register.vi_delay = next_vi;
293 add_interupt_event_count(VI_INT, next_vi);
294 add_interupt_event_count(SPECIAL_INT, 0);
297 void check_interupt(void)
299 if (MI_register.mi_intr_reg & MI_register.mi_intr_mask_reg)
300 Cause = (Cause | 0x400) & 0xFFFFFF83;
303 if ((Status & 7) != 1) return;
304 if (Status & Cause & 0xFF00)
308 q = (interupt_queue *) malloc(sizeof(interupt_queue));
315 interupt_queue* aux = (interupt_queue *) malloc(sizeof(interupt_queue));
318 aux->type = CHECK_INT;
321 next_interupt = Count;
325 void gen_interupt(void)
329 vi_counter = 0; // debug
333 if (!interupt_unsafe_state)
335 if (savestates_get_job() == savestates_job_load)
351 unsigned int dest = skip_jump;
354 if (q->count > Count || (Count - q->count) < 0x80000000)
355 next_interupt = q->count;
360 generic_jump_to(dest);
367 if (Count > 0x10000000) return;
368 remove_interupt_event();
369 add_interupt_event_count(SPECIAL_INT, 0);
376 cheat_apply_cheats(ENTRY_BOOT);
381 cheat_apply_cheats(ENTRY_VI);
391 // if paused, poll for input events
394 osd_render(); // draw Paused message in case gfx.updateScreen didn't do it
395 VidExt_GL_SwapBuffers();
407 if (vi_register.vi_v_sync == 0) vi_register.vi_delay = 500000;
408 else vi_register.vi_delay = ((vi_register.vi_v_sync + 1)*1500);
409 next_vi += vi_register.vi_delay;
410 if (vi_register.vi_status&0x40) vi_field=1-vi_field;
413 remove_interupt_event();
414 add_interupt_event_count(VI_INT, next_vi);
416 MI_register.mi_intr_reg |= 0x08;
417 if (MI_register.mi_intr_reg & MI_register.mi_intr_mask_reg)
418 Cause = (Cause | 0x400) & 0xFFFFFF83;
421 if ((Status & 7) != 1) return;
422 if (!(Status & Cause & 0xFF00)) return;
426 remove_interupt_event();
428 add_interupt_event_count(COMPARE_INT, Compare);
431 Cause = (Cause | 0x8000) & 0xFFFFFF83;
432 if ((Status & 7) != 1) return;
433 if (!(Status & Cause & 0xFF00)) return;
437 remove_interupt_event();
445 PIF_RAMb[0x3F] = 0x0;
446 remove_interupt_event();
447 MI_register.mi_intr_reg |= 0x02;
448 si_register.si_stat |= 0x1000;
449 if (MI_register.mi_intr_reg & MI_register.mi_intr_mask_reg)
450 Cause = (Cause | 0x400) & 0xFFFFFF83;
453 if ((Status & 7) != 1) return;
454 if (!(Status & Cause & 0xFF00)) return;
458 remove_interupt_event();
459 MI_register.mi_intr_reg |= 0x10;
460 pi_register.read_pi_status_reg &= ~3;
461 if (MI_register.mi_intr_reg & MI_register.mi_intr_mask_reg)
462 Cause = (Cause | 0x400) & 0xFFFFFF83;
465 if ((Status & 7) != 1) return;
466 if (!(Status & Cause & 0xFF00)) return;
470 if (ai_register.ai_status & 0x80000000) // full
472 unsigned int ai_event = get_event(AI_INT);
473 remove_interupt_event();
474 ai_register.ai_status &= ~0x80000000;
475 ai_register.current_delay = ai_register.next_delay;
476 ai_register.current_len = ai_register.next_len;
477 add_interupt_event_count(AI_INT, ai_event+ai_register.next_delay);
479 MI_register.mi_intr_reg |= 0x04;
480 if (MI_register.mi_intr_reg & MI_register.mi_intr_mask_reg)
481 Cause = (Cause | 0x400) & 0xFFFFFF83;
484 if ((Status & 7) != 1) return;
485 if (!(Status & Cause & 0xFF00)) return;
489 remove_interupt_event();
490 ai_register.ai_status &= ~0x40000000;
493 MI_register.mi_intr_reg |= 0x04;
494 if (MI_register.mi_intr_reg & MI_register.mi_intr_mask_reg)
495 Cause = (Cause | 0x400) & 0xFFFFFF83;
498 if ((Status & 7) != 1) return;
499 if (!(Status & Cause & 0xFF00)) return;
504 remove_interupt_event();
505 sp_register.sp_status_reg |= 0x203;
506 // sp_register.sp_status_reg |= 0x303;
508 if (!(sp_register.sp_status_reg & 0x40)) return; // !intr_on_break
509 MI_register.mi_intr_reg |= 0x01;
510 if (MI_register.mi_intr_reg & MI_register.mi_intr_mask_reg)
511 Cause = (Cause | 0x400) & 0xFFFFFF83;
514 if ((Status & 7) != 1) return;
515 if (!(Status & Cause & 0xFF00)) return;
519 remove_interupt_event();
520 dpc_register.dpc_status &= ~2;
521 dpc_register.dpc_status |= 0x81;
522 MI_register.mi_intr_reg |= 0x20;
523 if (MI_register.mi_intr_reg & MI_register.mi_intr_mask_reg)
524 Cause = (Cause | 0x400) & 0xFFFFFF83;
527 if ((Status & 7) != 1) return;
528 if (!(Status & Cause & 0xFF00)) return;
532 // Hardware Interrupt 2 -- remove interrupt event from queue
533 remove_interupt_event();
534 // setup r4300 Status flags: reset TS, and SR, set IM2
535 Status = (Status & ~0x00380000) | 0x1000;
536 Cause = (Cause | 0x1000) & 0xFFFFFF83;
537 /* the exception_general() call below will jump to the interrupt vector (0x80000180) and setup the
538 * interpreter or dynarec
543 // Non Maskable Interrupt -- remove interrupt event from queue
544 remove_interupt_event();
545 // setup r4300 Status flags: reset TS and SR, set BEV, ERL, and SR
546 Status = (Status & ~0x00380000) | 0x00500004;
548 // simulate the soft reset code which would run from the PIF ROM
550 // clear all interrupts, reset interrupt counters back to 0
554 // clear the audio status register so that subsequent write_ai() calls will work properly
555 ai_register.ai_status = 0;
556 // set ErrorEPC with the last instruction address
558 // reset the r4300 internal state
559 if (r4300emu != CORE_PURE_INTERPRETER)
561 // clear all the compiled instruction blocks and re-initialize
565 // adjust ErrorEPC if we were in a delay slot, and clear the delay_slot and dyna_interp flags
566 if(delay_slot==1 || delay_slot==3)
572 // set next instruction address to reset vector
573 last_addr = 0xa4000040;
574 generic_jump_to(0xa4000040);
578 DebugMessage(M64MSG_ERROR, "Unknown interrupt queue event type %.8X.", q->type);
579 remove_interupt_event();
584 if (r4300emu == CORE_DYNAREC) {
597 if (!interupt_unsafe_state)
599 if (savestates_get_job() == savestates_job_save)