2010-06-07 02:23:58 +00:00
|
|
|
#include "stdafx.h"
|
2008-09-18 03:15:49 +00:00
|
|
|
|
|
|
|
void FixUPXIssue ( BYTE * ProgramLocation );
|
|
|
|
|
2012-10-04 12:23:07 +00:00
|
|
|
CAudioPlugin::CAudioPlugin ( const char * FileName) :
|
|
|
|
m_hDll(NULL),
|
|
|
|
m_Initilized(false),
|
|
|
|
m_RomOpen(false),
|
|
|
|
m_hAudioThread(NULL),
|
|
|
|
m_DacrateChanged(NULL),
|
|
|
|
LenChanged(NULL),
|
|
|
|
Config(NULL),
|
|
|
|
ReadLength(NULL),
|
|
|
|
RomOpen(NULL),
|
|
|
|
RomClosed(NULL),
|
|
|
|
CloseDLL(NULL),
|
|
|
|
ProcessAList(NULL),
|
|
|
|
Update(NULL),
|
|
|
|
PluginOpened(NULL),
|
|
|
|
SetSettingInfo(NULL),
|
|
|
|
SetSettingInfo2(NULL)
|
|
|
|
{
|
|
|
|
memset(&m_PluginInfo,0,sizeof(m_PluginInfo));
|
|
|
|
Init(FileName);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CAudioPlugin::Init ( const char * FileName )
|
2010-05-22 04:47:15 +00:00
|
|
|
{
|
2008-09-18 03:15:49 +00:00
|
|
|
//Make sure all parts of the class are initialized
|
|
|
|
UnloadPlugin();
|
|
|
|
|
|
|
|
//Try to load the DLL library
|
|
|
|
UINT LastErrorMode = SetErrorMode( SEM_FAILCRITICALERRORS );
|
2012-10-04 12:23:07 +00:00
|
|
|
m_hDll = LoadLibrary(FileName);
|
2008-09-18 03:15:49 +00:00
|
|
|
SetErrorMode(LastErrorMode);
|
|
|
|
|
2012-10-04 12:23:07 +00:00
|
|
|
if (m_hDll == NULL) {
|
2008-09-18 03:15:49 +00:00
|
|
|
UnloadPlugin();
|
|
|
|
return;
|
|
|
|
}
|
2012-10-04 12:23:07 +00:00
|
|
|
FixUPXIssue((BYTE *)m_hDll);
|
2008-09-18 03:15:49 +00:00
|
|
|
|
|
|
|
//Get DLL information
|
|
|
|
void (__cdecl *GetDllInfo) ( PLUGIN_INFO * PluginInfo );
|
2012-10-04 12:23:07 +00:00
|
|
|
GetDllInfo = (void (__cdecl *)(PLUGIN_INFO *))GetProcAddress( (HMODULE)m_hDll, "GetDllInfo" );
|
2008-09-18 03:15:49 +00:00
|
|
|
if (GetDllInfo == NULL) { UnloadPlugin(); return; }
|
|
|
|
|
|
|
|
GetDllInfo(&m_PluginInfo);
|
2012-10-06 13:57:18 +00:00
|
|
|
if (!CPluginList::ValidPluginVersion(m_PluginInfo)) { UnloadPlugin(); return; }
|
2008-09-18 03:15:49 +00:00
|
|
|
|
|
|
|
//Find entries for functions in DLL
|
|
|
|
void (__cdecl *InitFunc) ( void );
|
2012-10-04 12:23:07 +00:00
|
|
|
m_DacrateChanged = (void (__cdecl *)(SystemType)) GetProcAddress( (HMODULE)m_hDll, "AiDacrateChanged" );
|
|
|
|
LenChanged = (void (__cdecl *)(void)) GetProcAddress( (HMODULE)m_hDll, "AiLenChanged" );
|
|
|
|
Config = (void (__cdecl *)(DWORD))GetProcAddress( (HMODULE)m_hDll, "DllConfig" );
|
|
|
|
ReadLength = (DWORD (__cdecl *)(void))GetProcAddress( (HMODULE)m_hDll, "AiReadLength" );
|
|
|
|
InitFunc = (void (__cdecl *)(void)) GetProcAddress( (HMODULE)m_hDll, "InitiateAudio" );
|
|
|
|
RomOpen = (void (__cdecl *)(void)) GetProcAddress( (HMODULE)m_hDll, "RomOpen" );
|
|
|
|
RomClosed = (void (__cdecl *)(void)) GetProcAddress( (HMODULE)m_hDll, "RomClosed" );
|
|
|
|
CloseDLL = (void (__cdecl *)(void)) GetProcAddress( (HMODULE)m_hDll, "CloseDLL" );
|
|
|
|
ProcessAList = (void (__cdecl *)(void)) GetProcAddress( (HMODULE)m_hDll, "ProcessAList" );
|
|
|
|
|
|
|
|
Update = (void (__cdecl *)(BOOL))GetProcAddress( (HMODULE)m_hDll, "AiUpdate" );
|
2008-09-18 03:15:49 +00:00
|
|
|
|
|
|
|
//version 102 functions
|
2012-10-04 12:23:07 +00:00
|
|
|
PluginOpened = (void (__cdecl *)(void))GetProcAddress( (HMODULE)m_hDll, "PluginLoaded" );
|
2008-09-18 03:15:49 +00:00
|
|
|
|
|
|
|
//Make sure dll had all needed functions
|
|
|
|
if (m_DacrateChanged == NULL) { UnloadPlugin(); return; }
|
|
|
|
if (LenChanged == NULL) { UnloadPlugin(); return; }
|
|
|
|
if (ReadLength == NULL) { UnloadPlugin(); return; }
|
|
|
|
if (InitFunc == NULL) { UnloadPlugin(); return; }
|
|
|
|
if (RomClosed == NULL) { UnloadPlugin(); return; }
|
|
|
|
if (ProcessAList == NULL) { UnloadPlugin(); return; }
|
|
|
|
|
2012-10-04 12:23:07 +00:00
|
|
|
SetSettingInfo2 = (void (__cdecl *)(PLUGIN_SETTINGS2 *))GetProcAddress( (HMODULE)m_hDll, "SetSettingInfo2" );
|
2008-12-04 10:41:51 +00:00
|
|
|
if (SetSettingInfo2)
|
|
|
|
{
|
|
|
|
PLUGIN_SETTINGS2 info;
|
|
|
|
info.FindSystemSettingId = (unsigned int (*)( void * handle, const char * ))CSettings::FindGameSetting;
|
|
|
|
SetSettingInfo2(&info);
|
|
|
|
}
|
|
|
|
|
2012-10-04 12:23:07 +00:00
|
|
|
SetSettingInfo = (void (__cdecl *)(PLUGIN_SETTINGS *))GetProcAddress( (HMODULE)m_hDll, "SetSettingInfo" );
|
2008-12-01 18:19:49 +00:00
|
|
|
if (SetSettingInfo)
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
|
|
|
PLUGIN_SETTINGS info;
|
|
|
|
info.dwSize = sizeof(PLUGIN_SETTINGS);
|
|
|
|
info.DefaultStartRange = FirstAudioDefaultSet;
|
|
|
|
info.SettingStartRange = FirstAudioSettings;
|
|
|
|
info.MaximumSettings = MaxPluginSetting;
|
2008-11-14 20:51:06 +00:00
|
|
|
info.NoDefault = Default_None;
|
|
|
|
info.DefaultLocation = _Settings->LoadDword(Setting_UseFromRegistry) ? SettingType_Registry : SettingType_CfgFile;
|
2008-09-18 03:15:49 +00:00
|
|
|
info.handle = _Settings;
|
2008-11-14 20:51:06 +00:00
|
|
|
info.RegisterSetting = (void (*)(void *,int,int,SettingDataType,SettingType,const char *,const char *, DWORD))CSettings::RegisterSetting;
|
2008-09-18 03:15:49 +00:00
|
|
|
info.GetSetting = (unsigned int (*)( void * handle, int ID ))CSettings::GetSetting;
|
|
|
|
info.GetSettingSz = (const char * (*)( void *, int, char *, int ))CSettings::GetSettingSz;
|
|
|
|
info.SetSetting = (void (*)(void *,int,unsigned int))CSettings::SetSetting;
|
|
|
|
info.SetSettingSz = (void (*)(void *,int,const char *))CSettings::SetSettingSz;
|
|
|
|
info.UseUnregisteredSetting = NULL;
|
|
|
|
|
|
|
|
SetSettingInfo(&info);
|
|
|
|
//_Settings->UnknownSetting_AUDIO = info.UseUnregisteredSetting;
|
2008-12-01 18:19:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (m_PluginInfo.Version >= 0x0102)
|
|
|
|
{
|
|
|
|
if (PluginOpened == NULL) { UnloadPlugin(); return; }
|
2008-09-18 03:15:49 +00:00
|
|
|
PluginOpened();
|
|
|
|
}
|
|
|
|
}
|
2012-10-04 12:23:07 +00:00
|
|
|
|
2008-09-18 03:15:49 +00:00
|
|
|
CAudioPlugin::~CAudioPlugin (void) {
|
|
|
|
Close();
|
|
|
|
UnloadPlugin();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CAudioPlugin::Initiate ( CN64System * System, CMainGui * RenderWindow ) {
|
2012-10-04 12:23:07 +00:00
|
|
|
struct AUDIO_INFO {
|
2008-09-18 03:15:49 +00:00
|
|
|
HWND hwnd;
|
|
|
|
HINSTANCE hinst;
|
|
|
|
|
|
|
|
BOOL MemoryBswaped; // If this is set to TRUE, then the memory has been pre
|
|
|
|
// bswap on a dword (32 bits) boundry
|
|
|
|
// eg. the first 8 bytes are stored like this:
|
|
|
|
// 4 3 2 1 8 7 6 5
|
|
|
|
BYTE * HEADER; // This is the rom header (first 40h bytes of the rom
|
|
|
|
// This will be in the same memory format as the rest of the memory.
|
|
|
|
BYTE * RDRAM;
|
|
|
|
BYTE * DMEM;
|
|
|
|
BYTE * IMEM;
|
|
|
|
|
|
|
|
DWORD * MI__INTR_REG;
|
|
|
|
|
|
|
|
DWORD * AI__DRAM_ADDR_REG;
|
|
|
|
DWORD * AI__LEN_REG;
|
|
|
|
DWORD * AI__CONTROL_REG;
|
|
|
|
DWORD * AI__STATUS_REG;
|
|
|
|
DWORD * AI__DACRATE_REG;
|
|
|
|
DWORD * AI__BITRATE_REG;
|
|
|
|
|
|
|
|
void (__cdecl *CheckInterrupts)( void );
|
2012-10-04 12:23:07 +00:00
|
|
|
};
|
2008-09-18 03:15:49 +00:00
|
|
|
|
|
|
|
//Get Function from DLL
|
|
|
|
BOOL (__cdecl *InitiateAudio) ( AUDIO_INFO Audio_Info );
|
2012-10-04 12:23:07 +00:00
|
|
|
InitiateAudio = (BOOL (__cdecl *)(AUDIO_INFO))GetProcAddress( (HMODULE)m_hDll, "InitiateAudio" );
|
2008-09-18 03:15:49 +00:00
|
|
|
if (InitiateAudio == NULL) { return false; }
|
|
|
|
|
|
|
|
AUDIO_INFO Info;
|
|
|
|
memset(&Info,0,sizeof(Info));
|
|
|
|
//We are initilizing the plugin before any rom is loaded so we do not have any correct
|
|
|
|
//paramaters here .. just needed to we can config the DLL
|
|
|
|
if (System == NULL) {
|
|
|
|
BYTE Buffer[100];
|
|
|
|
DWORD Value = 0;
|
|
|
|
|
|
|
|
Info.hwnd = (HWND)RenderWindow->m_hMainWindow;;
|
|
|
|
Info.hinst = GetModuleHandle(NULL);
|
|
|
|
Info.MemoryBswaped = TRUE;
|
|
|
|
Info.HEADER = Buffer;
|
|
|
|
Info.RDRAM = Buffer;
|
|
|
|
Info.DMEM = Buffer;
|
|
|
|
Info.IMEM = Buffer;
|
|
|
|
Info.MI__INTR_REG = &Value;
|
|
|
|
Info.AI__DRAM_ADDR_REG = &Value;
|
|
|
|
Info.AI__LEN_REG = &Value;
|
|
|
|
Info.AI__CONTROL_REG = &Value;
|
|
|
|
Info.AI__STATUS_REG = &Value;
|
|
|
|
Info.AI__DACRATE_REG = &Value;
|
|
|
|
Info.AI__BITRATE_REG = &Value;
|
|
|
|
Info.CheckInterrupts = DummyCheckInterrupts;
|
|
|
|
|
|
|
|
m_Initilized = InitiateAudio(Info) != 0;
|
|
|
|
//jabo had a bug so I call CreateThread so his dllmain gets called again
|
|
|
|
DWORD ThreadID;
|
|
|
|
HANDLE hthread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)DummyFunction,NULL,0, &ThreadID);
|
|
|
|
CloseHandle(hthread);
|
|
|
|
Sleep(100);
|
|
|
|
return m_Initilized;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Send Initilization information to the DLL
|
|
|
|
Info.hwnd = (HWND)RenderWindow->m_hMainWindow;
|
|
|
|
Info.hinst = GetModuleHandle(NULL);
|
|
|
|
Info.MemoryBswaped = TRUE;
|
2010-05-22 04:47:15 +00:00
|
|
|
Info.HEADER = _Rom->GetRomAddress();
|
|
|
|
Info.RDRAM = _MMU->Rdram();
|
|
|
|
Info.DMEM = _MMU->Dmem();
|
|
|
|
Info.IMEM = _MMU->Imem();
|
2010-05-23 10:05:41 +00:00
|
|
|
Info.MI__INTR_REG = &_Reg->m_AudioIntrReg;
|
2008-09-18 03:15:49 +00:00
|
|
|
Info.AI__DRAM_ADDR_REG = &_Reg->AI_DRAM_ADDR_REG;
|
|
|
|
Info.AI__LEN_REG = &_Reg->AI_LEN_REG;
|
|
|
|
Info.AI__CONTROL_REG = &_Reg->AI_CONTROL_REG;
|
2012-11-02 06:10:01 +00:00
|
|
|
Info.AI__STATUS_REG = &_Reg->AI_STATUS_REG;
|
2008-09-18 03:15:49 +00:00
|
|
|
Info.AI__DACRATE_REG = &_Reg->AI_DACRATE_REG;
|
|
|
|
Info.AI__BITRATE_REG = &_Reg->AI_BITRATE_REG;
|
|
|
|
Info.CheckInterrupts = DummyCheckInterrupts;
|
|
|
|
|
|
|
|
m_Initilized = InitiateAudio(Info) != 0;
|
|
|
|
//jabo had a bug so I call CreateThread so his dllmain gets called again
|
|
|
|
DWORD ThreadID;
|
|
|
|
HANDLE hthread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)DummyFunction,NULL,0, &ThreadID);
|
|
|
|
CloseHandle(hthread);
|
|
|
|
|
|
|
|
if (Update) {
|
|
|
|
m_hAudioThread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)AudioThread, (LPVOID)this,0, &ThreadID);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_Reg->AI_DACRATE_REG != 0) {
|
|
|
|
DacrateChanged(SYSTEM_NTSC);
|
|
|
|
}
|
|
|
|
return m_Initilized;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CAudioPlugin::RomOpened ( void )
|
|
|
|
{
|
|
|
|
//Real system ... then make the file as open
|
|
|
|
if (!m_RomOpen && RomOpen)
|
|
|
|
{
|
|
|
|
RomOpen();
|
|
|
|
m_RomOpen = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CAudioPlugin::Close(void) {
|
|
|
|
if (m_RomOpen) {
|
|
|
|
RomClosed();
|
|
|
|
m_RomOpen = false;
|
|
|
|
}
|
|
|
|
if (m_Initilized) {
|
|
|
|
CloseDLL();
|
|
|
|
m_Initilized = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CAudioPlugin::GameReset(void)
|
|
|
|
{
|
|
|
|
if (m_RomOpen)
|
|
|
|
{
|
|
|
|
RomClosed();
|
|
|
|
if (RomOpen)
|
|
|
|
{
|
|
|
|
RomOpen();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CAudioPlugin::UnloadPlugin(void) {
|
2011-01-04 09:10:28 +00:00
|
|
|
if (m_hAudioThread)
|
|
|
|
{
|
|
|
|
WriteTraceF(TraceAudio,__FUNCTION__ ": Terminate Audio Thread");
|
2008-09-18 03:15:49 +00:00
|
|
|
TerminateThread(m_hAudioThread,0);
|
|
|
|
m_hAudioThread = NULL;
|
|
|
|
}
|
|
|
|
memset(&m_PluginInfo,0,sizeof(m_PluginInfo));
|
2012-10-04 12:23:07 +00:00
|
|
|
if (m_hDll != NULL ) {
|
|
|
|
FreeLibrary((HMODULE)m_hDll);
|
|
|
|
m_hDll = NULL;
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
m_DacrateChanged = NULL;
|
|
|
|
LenChanged = NULL;
|
|
|
|
Config = NULL;
|
|
|
|
ReadLength = NULL;
|
|
|
|
Update = NULL;
|
|
|
|
ProcessAList = NULL;
|
|
|
|
RomClosed = NULL;
|
|
|
|
CloseDLL = NULL;
|
|
|
|
}
|
|
|
|
|
2010-06-14 21:14:58 +00:00
|
|
|
void CAudioPlugin::DacrateChanged (SystemType Type)
|
|
|
|
{
|
2008-09-18 03:15:49 +00:00
|
|
|
if (!Initilized()) { return; }
|
2011-01-04 09:10:28 +00:00
|
|
|
WriteTraceF(TraceAudio,__FUNCTION__ ": SystemType: %s", Type == SYSTEM_NTSC ? "SYSTEM_NTSC" : "SYSTEM_PAL");
|
|
|
|
|
2012-10-06 04:09:17 +00:00
|
|
|
//DWORD Frequency = _Reg->AI_DACRATE_REG * 30;
|
|
|
|
//DWORD CountsPerSecond = (_Reg->VI_V_SYNC_REG != 0 ? (_Reg->VI_V_SYNC_REG + 1) * _Settings->LoadDword(Game_ViRefreshRate) : 500000) * 60;
|
2008-09-18 03:15:49 +00:00
|
|
|
m_DacrateChanged(Type);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CAudioPlugin::AudioThread (CAudioPlugin * _this) {
|
|
|
|
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL );
|
2011-01-04 09:10:28 +00:00
|
|
|
for (;;)
|
|
|
|
{
|
2008-09-18 03:15:49 +00:00
|
|
|
_this->Update(true);
|
|
|
|
}
|
|
|
|
}
|