dosbox-wii/src/cpu/callback.cpp

243 lines
7.2 KiB
C++
Raw Normal View History

2009-05-02 23:03:37 +02:00
/*
2009-05-02 23:53:27 +02:00
* Copyright (C) 2002-2004 The DOSBox Team
2009-05-02 23:03:37 +02:00
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2009-05-03 00:02:15 +02:00
* GNU General Public License for more details.
2009-05-02 23:03:37 +02:00
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
2009-05-03 00:02:15 +02:00
/* $Id: callback.cpp,v 1.22 2004/08/29 11:22:37 qbix79 Exp $ */
2009-05-02 23:43:00 +02:00
2009-05-02 23:03:37 +02:00
#include <stdlib.h>
#include <stdio.h>
2009-05-02 23:53:27 +02:00
#include <string.h>
2009-05-02 23:03:37 +02:00
#include "dosbox.h"
#include "callback.h"
#include "mem.h"
#include "cpu.h"
2009-05-02 23:27:47 +02:00
#include "pic.h"
2009-05-02 23:03:37 +02:00
/* CallBack are located at 0xC800:0
And they are 16 bytes each and you can define them to behave in certain ways like a
far return or and IRET
*/
CallBack_Handler CallBack_Handlers[CB_MAX];
2009-05-02 23:53:27 +02:00
char* CallBack_Description[CB_MAX];
2009-05-02 23:12:18 +02:00
static Bitu call_stop,call_idle,call_default;
2009-05-02 23:03:37 +02:00
static Bitu illegal_handler(void) {
E_Exit("Illegal CallBack Called");
return 1;
}
Bitu CALLBACK_Allocate(void) {
2009-05-02 23:43:00 +02:00
for (Bitu i=1;(i<CB_MAX);i++) {
2009-05-02 23:03:37 +02:00
if (CallBack_Handlers[i]==&illegal_handler) {
CallBack_Handlers[i]=0;
return i;
}
}
E_Exit("CALLBACK:Can't allocate handler.");
return 0;
}
void CALLBACK_Idle(void) {
/* this makes the cpu execute instructions to handle irq's and then come back */
2009-05-02 23:43:00 +02:00
Bitu oldIF=GETFLAG(IF);
SETFLAGBIT(IF,true);
2009-05-02 23:12:18 +02:00
Bit16u oldcs=SegValue(cs);
2009-05-02 23:03:37 +02:00
Bit32u oldeip=reg_eip;
2009-05-02 23:12:18 +02:00
SegSet16(cs,CB_SEG);
2009-05-02 23:03:37 +02:00
reg_eip=call_idle<<4;
DOSBOX_RunMachine();
reg_eip=oldeip;
2009-05-02 23:12:18 +02:00
SegSet16(cs,oldcs);
2009-05-02 23:43:00 +02:00
SETFLAGBIT(IF,oldIF);
2009-05-03 00:02:15 +02:00
if (CPU_Cycles>0) CPU_Cycles=0;
2009-05-02 23:03:37 +02:00
}
static Bitu default_handler(void) {
2009-05-02 23:43:00 +02:00
LOG(LOG_CPU,LOG_ERROR)("Illegal Unhandled Interrupt Called %X",lastint);
2009-05-02 23:03:37 +02:00
return CBRET_NONE;
};
static Bitu stop_handler(void) {
return CBRET_STOP;
};
void CALLBACK_RunRealFar(Bit16u seg,Bit16u off) {
2009-05-02 23:12:18 +02:00
reg_sp-=4;
mem_writew(SegPhys(ss)+reg_sp,call_stop<<4);
mem_writew(SegPhys(ss)+reg_sp+2,CB_SEG);
2009-05-02 23:03:37 +02:00
Bit32u oldeip=reg_eip;
2009-05-02 23:12:18 +02:00
Bit16u oldcs=SegValue(cs);
reg_eip=off;
SegSet16(cs,seg);
2009-05-02 23:03:37 +02:00
DOSBOX_RunMachine();
reg_eip=oldeip;
2009-05-02 23:12:18 +02:00
SegSet16(cs,oldcs);
2009-05-02 23:03:37 +02:00
}
void CALLBACK_RunRealInt(Bit8u intnum) {
Bit32u oldeip=reg_eip;
2009-05-02 23:12:18 +02:00
Bit16u oldcs=SegValue(cs);
2009-05-02 23:53:27 +02:00
reg_eip=(CB_MAX*16)+(intnum*6);
2009-05-02 23:12:18 +02:00
SegSet16(cs,CB_SEG);
2009-05-02 23:03:37 +02:00
DOSBOX_RunMachine();
reg_eip=oldeip;
2009-05-02 23:12:18 +02:00
SegSet16(cs,oldcs);
2009-05-02 23:03:37 +02:00
}
void CALLBACK_SZF(bool val) {
2009-05-02 23:12:18 +02:00
Bit16u tempf=mem_readw(SegPhys(ss)+reg_sp+4) & 0xFFBF;
2009-05-02 23:03:37 +02:00
Bit16u newZF=(val==true) << 6;
2009-05-02 23:12:18 +02:00
mem_writew(SegPhys(ss)+reg_sp+4,(tempf | newZF));
2009-05-02 23:03:37 +02:00
};
void CALLBACK_SCF(bool val) {
2009-05-02 23:12:18 +02:00
Bit16u tempf=mem_readw(SegPhys(ss)+reg_sp+4) & 0xFFFE;
2009-05-02 23:03:37 +02:00
Bit16u newCF=(val==true);
2009-05-02 23:12:18 +02:00
mem_writew(SegPhys(ss)+reg_sp+4,(tempf | newCF));
2009-05-02 23:03:37 +02:00
};
2009-05-02 23:53:27 +02:00
void CALLBACK_SetDescription(Bitu nr, const char* descr)
{
if (descr) {
CallBack_Description[nr] = new char[strlen(descr)+1];
strcpy(CallBack_Description[nr],descr);
} else
CallBack_Description[nr] = 0;
};
2009-05-02 23:03:37 +02:00
2009-05-02 23:53:27 +02:00
const char* CALLBACK_GetDescription(Bitu nr)
{
if (nr>=CB_MAX) return 0;
return CallBack_Description[nr];
};
2009-05-02 23:03:37 +02:00
2009-05-02 23:53:27 +02:00
bool CALLBACK_Setup(Bitu callback,CallBack_Handler handler,Bitu type,const char* descr) {
2009-05-02 23:03:37 +02:00
if (callback>=CB_MAX) return false;
switch (type) {
case CB_RETF:
2009-05-02 23:43:00 +02:00
phys_writeb(CB_BASE+(callback<<4)+0,(Bit8u)0xFE); //GRP 4
phys_writeb(CB_BASE+(callback<<4)+1,(Bit8u)0x38); //Extra Callback instruction
phys_writew(CB_BASE+(callback<<4)+2,callback); //The immediate word
phys_writeb(CB_BASE+(callback<<4)+4,(Bit8u)0xCB); //A RETF Instruction
2009-05-02 23:03:37 +02:00
break;
case CB_IRET:
2009-05-02 23:43:00 +02:00
phys_writeb(CB_BASE+(callback<<4)+0,(Bit8u)0xFE); //GRP 4
phys_writeb(CB_BASE+(callback<<4)+1,(Bit8u)0x38); //Extra Callback instruction
phys_writew(CB_BASE+(callback<<4)+2,callback); //The immediate word
phys_writeb(CB_BASE+(callback<<4)+4,(Bit8u)0xCF); //An IRET Instruction
2009-05-02 23:03:37 +02:00
break;
2009-05-02 23:27:47 +02:00
case CB_IRET_STI:
2009-05-02 23:43:00 +02:00
phys_writeb(CB_BASE+(callback<<4)+0,(Bit8u)0xFB); //STI
phys_writeb(CB_BASE+(callback<<4)+1,(Bit8u)0xFE); //GRP 4
phys_writeb(CB_BASE+(callback<<4)+2,(Bit8u)0x38); //Extra Callback instruction
phys_writew(CB_BASE+(callback<<4)+3,callback); //The immediate word
phys_writeb(CB_BASE+(callback<<4)+5,(Bit8u)0xCF); //An IRET Instruction
2009-05-02 23:27:47 +02:00
break;
2009-05-02 23:03:37 +02:00
default:
E_Exit("CALLBACK:Setup:Illegal type %d",type);
}
CallBack_Handlers[callback]=handler;
2009-05-02 23:53:27 +02:00
CALLBACK_SetDescription(callback,descr);
2009-05-02 23:03:37 +02:00
return true;
}
2009-05-02 23:53:27 +02:00
bool CALLBACK_SetupAt(Bitu callback,CallBack_Handler handler,Bitu type,Bitu linearAddress,const char* descr) {
2009-05-02 23:43:00 +02:00
if (callback>=CB_MAX) return false;
switch (type) {
case CB_RETF:
mem_writeb(linearAddress+0,(Bit8u)0xFE); //GRP 4
mem_writeb(linearAddress+1,(Bit8u)0x38); //Extra Callback instruction
mem_writew(linearAddress+2, callback); //The immediate word
mem_writeb(linearAddress+4,(Bit8u)0xCB); //A RETF Instruction
break;
case CB_IRET:
mem_writeb(linearAddress+0,(Bit8u)0xFE); //GRP 4
mem_writeb(linearAddress+1,(Bit8u)0x38); //Extra Callback instruction
mem_writew(linearAddress+2,callback); //The immediate word
mem_writeb(linearAddress+4,(Bit8u)0xCF); //An IRET Instruction
break;
case CB_IRET_STI:
mem_writeb(linearAddress+0,(Bit8u)0xFB); //STI
mem_writeb(linearAddress+1,(Bit8u)0xFE); //GRP 4
mem_writeb(linearAddress+2,(Bit8u)0x38); //Extra Callback instruction
mem_writew(linearAddress+3, callback); //The immediate word
mem_writeb(linearAddress+5,(Bit8u)0xCF); //An IRET Instruction
break;
default:
E_Exit("CALLBACK:Setup:Illegal type %d",type);
}
CallBack_Handlers[callback]=handler;
2009-05-02 23:53:27 +02:00
CALLBACK_SetDescription(callback,descr);
2009-05-02 23:43:00 +02:00
return true;
}
2009-05-02 23:20:05 +02:00
void CALLBACK_Init(Section* sec) {
2009-05-02 23:03:37 +02:00
Bitu i;
for (i=0;i<CB_MAX;i++) {
CallBack_Handlers[i]=&illegal_handler;
}
2009-05-02 23:12:18 +02:00
/* Setup the Stop Handler */
call_stop=CALLBACK_Allocate();
CallBack_Handlers[call_stop]=stop_handler;
2009-05-03 00:02:15 +02:00
CALLBACK_SetDescription(call_stop,"stop");
2009-05-02 23:43:00 +02:00
phys_writeb(CB_BASE+(call_stop<<4)+0,0xFE);
phys_writeb(CB_BASE+(call_stop<<4)+1,0x38);
phys_writew(CB_BASE+(call_stop<<4)+2,call_stop);
2009-05-02 23:03:37 +02:00
/* Setup the idle handler */
call_idle=CALLBACK_Allocate();
CallBack_Handlers[call_idle]=stop_handler;
2009-05-03 00:02:15 +02:00
CALLBACK_SetDescription(call_idle,"idle");
2009-05-02 23:43:00 +02:00
for (i=0;i<=11;i++) phys_writeb(CB_BASE+(call_idle<<4)+i,0x90);
phys_writeb(CB_BASE+(call_idle<<4)+12,0xFE);
phys_writeb(CB_BASE+(call_idle<<4)+13,0x38);
phys_writew(CB_BASE+(call_idle<<4)+14,call_idle);
2009-05-02 23:12:18 +02:00
2009-05-02 23:03:37 +02:00
/* Setup all Interrupt to point to the default handler */
call_default=CALLBACK_Allocate();
2009-05-03 00:02:15 +02:00
CALLBACK_Setup(call_default,&default_handler,CB_IRET,"default");
2009-05-02 23:35:44 +02:00
/* Only setup default handler for first half of interrupt table */
for (i=0;i<0x40;i++) {
2009-05-02 23:03:37 +02:00
real_writed(0,i*4,CALLBACK_RealPointer(call_default));
}
2009-05-02 23:53:27 +02:00
/* Setup block of 0xCD 0xxx instructions */
PhysPt rint_base=CB_BASE+CB_MAX*16;
for (i=0;i<=0xff;i++) {
phys_writeb(rint_base,0xCD);
phys_writeb(rint_base+1,i);
phys_writeb(rint_base+2,0xFE);
phys_writeb(rint_base+3,0x38);
phys_writew(rint_base+4,call_stop);
rint_base+=6;
}
2009-05-02 23:43:00 +02:00
real_writed(0,0x67*4,CALLBACK_RealPointer(call_default));
2009-05-02 23:53:27 +02:00
real_writed(0,0x5c*4,CALLBACK_RealPointer(call_default)); //Network stuff
2009-05-02 23:43:00 +02:00
//real_writed(0,0xf*4,0); some games don't like it
2009-05-02 23:03:37 +02:00
}