2012-12-19 09:30:18 +00:00
|
|
|
/****************************************************************************
|
|
|
|
* *
|
2015-11-10 05:21:49 +00:00
|
|
|
* Project64 - A Nintendo 64 emulator. *
|
2012-12-19 09:30:18 +00:00
|
|
|
* http://www.pj64-emu.com/ *
|
|
|
|
* Copyright (C) 2012 Project64. All rights reserved. *
|
|
|
|
* *
|
|
|
|
* License: *
|
|
|
|
* GNU/GPLv2 http://www.gnu.org/licenses/gpl-2.0.html *
|
|
|
|
* *
|
|
|
|
****************************************************************************/
|
2010-06-07 02:23:58 +00:00
|
|
|
#include "stdafx.h"
|
2015-12-06 09:59:58 +00:00
|
|
|
#include <Project64-core/N64System/SystemGlobals.h>
|
|
|
|
#include <Project64-core/N64System/N64RomClass.h>
|
2015-12-21 07:35:22 +00:00
|
|
|
#include <Project64-core/N64System/Mips/MemoryVirtualMem.h>
|
2015-12-06 09:59:58 +00:00
|
|
|
#include <Project64-core/N64System/Mips/RegisterClass.h>
|
|
|
|
#include <Project64-core/N64System/N64Class.h>
|
2016-01-18 11:26:15 +00:00
|
|
|
#include <Project64-core/Plugins/AudioPlugin.h>
|
2016-01-17 18:48:19 +00:00
|
|
|
#ifdef _WIN32
|
2015-12-06 09:59:58 +00:00
|
|
|
#include <Windows.h>
|
2016-01-17 18:48:19 +00:00
|
|
|
#endif
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2015-01-30 21:01:21 +00:00
|
|
|
CAudioPlugin::CAudioPlugin() :
|
2016-01-17 18:48:19 +00:00
|
|
|
AiLenChanged(NULL),
|
|
|
|
AiReadLength(NULL),
|
|
|
|
ProcessAList(NULL),
|
|
|
|
m_hAudioThread(NULL),
|
|
|
|
AiUpdate(NULL),
|
|
|
|
AiDacrateChanged(NULL)
|
2012-10-04 12:23:07 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-01-30 21:01:21 +00:00
|
|
|
CAudioPlugin::~CAudioPlugin()
|
2010-05-22 04:47:15 +00:00
|
|
|
{
|
2015-11-08 06:08:15 +00:00
|
|
|
Close();
|
|
|
|
UnloadPlugin();
|
2015-01-30 21:01:21 +00:00
|
|
|
}
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2015-11-08 06:08:15 +00:00
|
|
|
bool CAudioPlugin::LoadFunctions(void)
|
2015-01-30 21:01:21 +00:00
|
|
|
{
|
2015-11-08 06:08:15 +00:00
|
|
|
// Find entries for functions in DLL
|
2016-01-17 18:48:19 +00:00
|
|
|
void(CALL *InitiateAudio)(void);
|
2015-11-08 06:08:15 +00:00
|
|
|
LoadFunction(InitiateAudio);
|
|
|
|
LoadFunction(AiDacrateChanged);
|
|
|
|
LoadFunction(AiLenChanged);
|
|
|
|
LoadFunction(AiReadLength);
|
|
|
|
LoadFunction(AiUpdate);
|
|
|
|
LoadFunction(ProcessAList);
|
|
|
|
|
|
|
|
// Make sure dll has all needed functions
|
|
|
|
if (AiDacrateChanged == NULL) { UnloadPlugin(); return false; }
|
|
|
|
if (AiLenChanged == NULL) { UnloadPlugin(); return false; }
|
|
|
|
if (AiReadLength == NULL) { UnloadPlugin(); return false; }
|
|
|
|
if (InitiateAudio == NULL) { UnloadPlugin(); return false; }
|
|
|
|
if (ProcessAList == NULL) { UnloadPlugin(); return false; }
|
|
|
|
|
|
|
|
if (m_PluginInfo.Version >= 0x0102)
|
|
|
|
{
|
|
|
|
if (PluginOpened == NULL) { UnloadPlugin(); return false; }
|
|
|
|
}
|
|
|
|
return true;
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
2012-10-04 12:23:07 +00:00
|
|
|
|
2015-11-12 10:08:47 +00:00
|
|
|
bool CAudioPlugin::Initiate(CN64System * System, RenderWindow * Window)
|
2015-01-30 21:01:21 +00:00
|
|
|
{
|
2015-11-08 06:08:15 +00:00
|
|
|
struct AUDIO_INFO
|
|
|
|
{
|
2016-01-18 11:26:15 +00:00
|
|
|
void * hwnd;
|
|
|
|
void * hinst;
|
2015-11-08 06:08:15 +00:00
|
|
|
|
|
|
|
int32_t 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
|
|
|
|
uint8_t * 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.
|
|
|
|
uint8_t * RDRAM;
|
|
|
|
uint8_t * DMEM;
|
|
|
|
uint8_t * IMEM;
|
|
|
|
|
|
|
|
uint32_t * MI__INTR_REG;
|
|
|
|
|
|
|
|
uint32_t * AI__DRAM_ADDR_REG;
|
|
|
|
uint32_t * AI__LEN_REG;
|
|
|
|
uint32_t * AI__CONTROL_REG;
|
|
|
|
uint32_t * AI__STATUS_REG;
|
|
|
|
uint32_t * AI__DACRATE_REG;
|
|
|
|
uint32_t * AI__BITRATE_REG;
|
|
|
|
|
2016-01-17 18:48:19 +00:00
|
|
|
void(CALL *CheckInterrupts)(void);
|
2015-11-08 06:08:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//Get Function from DLL
|
2016-01-17 18:48:19 +00:00
|
|
|
int32_t(CALL *InitiateAudio)(AUDIO_INFO Audio_Info);
|
2015-11-08 06:08:15 +00:00
|
|
|
LoadFunction(InitiateAudio);
|
|
|
|
if (InitiateAudio == NULL) { return false; }
|
|
|
|
|
|
|
|
AUDIO_INFO Info = { 0 };
|
2015-01-30 21:01:21 +00:00
|
|
|
|
2016-01-18 11:26:15 +00:00
|
|
|
Info.hwnd = Window ? Window->GetWindowHandle() : NULL;
|
|
|
|
Info.hinst = Window ? Window->GetModuleInstance() : NULL;;
|
|
|
|
Info.MemoryBswaped = true;
|
2015-11-08 06:08:15 +00:00
|
|
|
Info.CheckInterrupts = DummyCheckInterrupts;
|
|
|
|
|
|
|
|
// We are initializing the plugin before any rom is loaded so we do not have any correct
|
|
|
|
// parameters here.. just needed to we can config the DLL.
|
|
|
|
if (System == NULL)
|
|
|
|
{
|
|
|
|
uint8_t Buffer[100];
|
|
|
|
uint32_t Value = 0;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
// Send initialization information to the DLL
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Info.HEADER = g_Rom->GetRomAddress();
|
|
|
|
Info.RDRAM = g_MMU->Rdram();
|
|
|
|
Info.DMEM = g_MMU->Dmem();
|
|
|
|
Info.IMEM = g_MMU->Imem();
|
2015-11-09 20:26:33 +00:00
|
|
|
Info.MI__INTR_REG = &g_Reg->m_AudioIntrReg;
|
|
|
|
Info.AI__DRAM_ADDR_REG = &g_Reg->AI_DRAM_ADDR_REG;
|
|
|
|
Info.AI__LEN_REG = &g_Reg->AI_LEN_REG;
|
|
|
|
Info.AI__CONTROL_REG = &g_Reg->AI_CONTROL_REG;
|
|
|
|
Info.AI__STATUS_REG = &g_Reg->AI_STATUS_REG;
|
|
|
|
Info.AI__DACRATE_REG = &g_Reg->AI_DACRATE_REG;
|
|
|
|
Info.AI__BITRATE_REG = &g_Reg->AI_BITRATE_REG;
|
2015-11-08 06:08:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m_Initialized = InitiateAudio(Info) != 0;
|
|
|
|
|
2016-01-17 18:48:19 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
//jabo had a bug so I call CreateThread so his dllmain gets called again
|
|
|
|
pjutil::DynLibCallDllMain();
|
2015-11-08 06:08:15 +00:00
|
|
|
|
|
|
|
if (System != NULL)
|
|
|
|
{
|
|
|
|
if (AiUpdate)
|
|
|
|
{
|
|
|
|
if (m_hAudioThread)
|
|
|
|
{
|
2015-12-14 10:51:33 +00:00
|
|
|
WriteTrace(TraceAudioPlugin, TraceDebug, "Terminate Audio Thread");
|
2015-11-08 06:08:15 +00:00
|
|
|
TerminateThread(m_hAudioThread, 0);
|
|
|
|
}
|
2016-01-17 18:48:19 +00:00
|
|
|
DWORD ThreadID;
|
2015-11-08 06:08:15 +00:00
|
|
|
m_hAudioThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)AudioThread, (LPVOID)this, 0, &ThreadID);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (g_Reg->AI_DACRATE_REG != 0)
|
|
|
|
{
|
|
|
|
DacrateChanged(System->SystemType());
|
|
|
|
}
|
|
|
|
}
|
2016-01-17 18:48:19 +00:00
|
|
|
#endif
|
2015-11-08 06:08:15 +00:00
|
|
|
return m_Initialized;
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
2015-01-30 21:01:21 +00:00
|
|
|
void CAudioPlugin::UnloadPluginDetails(void)
|
|
|
|
{
|
2016-01-18 11:26:15 +00:00
|
|
|
#ifdef _WIN32
|
2015-11-08 06:08:15 +00:00
|
|
|
if (m_hAudioThread)
|
|
|
|
{
|
2015-12-14 10:51:33 +00:00
|
|
|
WriteTrace(TraceAudioPlugin, TraceDebug, "Terminate Audio Thread");
|
2015-11-08 06:08:15 +00:00
|
|
|
TerminateThread(m_hAudioThread, 0);
|
|
|
|
m_hAudioThread = NULL;
|
|
|
|
}
|
2016-01-18 11:26:15 +00:00
|
|
|
#endif
|
|
|
|
AiDacrateChanged = NULL;
|
2015-11-08 06:08:15 +00:00
|
|
|
AiLenChanged = NULL;
|
|
|
|
AiReadLength = NULL;
|
|
|
|
AiUpdate = NULL;
|
|
|
|
ProcessAList = NULL;
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
2015-01-30 21:01:21 +00:00
|
|
|
void CAudioPlugin::DacrateChanged(SYSTEM_TYPE Type)
|
2010-06-14 21:14:58 +00:00
|
|
|
{
|
2015-11-08 06:08:15 +00:00
|
|
|
if (!Initialized()) { return; }
|
2015-12-14 10:51:33 +00:00
|
|
|
WriteTrace(TraceAudioPlugin, TraceDebug, "SystemType: %s", Type == SYSTEM_NTSC ? "SYSTEM_NTSC" : "SYSTEM_PAL");
|
2011-01-04 09:10:28 +00:00
|
|
|
|
2015-11-08 06:08:15 +00:00
|
|
|
//uint32_t Frequency = g_Reg->AI_DACRATE_REG * 30;
|
|
|
|
//uint32_t CountsPerSecond = (g_Reg->VI_V_SYNC_REG != 0 ? (g_Reg->VI_V_SYNC_REG + 1) * g_Settings->LoadDword(Game_ViRefreshRate) : 500000) * 60;
|
|
|
|
AiDacrateChanged(Type);
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
2016-01-18 11:26:15 +00:00
|
|
|
#ifdef _WIN32
|
2015-12-14 10:51:33 +00:00
|
|
|
void CAudioPlugin::AudioThread(CAudioPlugin * _this)
|
|
|
|
{
|
2015-11-08 06:08:15 +00:00
|
|
|
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
|
2015-12-14 10:51:33 +00:00
|
|
|
if (g_Settings->LoadBool(Setting_CN64TimeCritical))
|
|
|
|
{
|
|
|
|
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST);
|
|
|
|
}
|
|
|
|
for (;;)
|
2015-11-08 06:08:15 +00:00
|
|
|
{
|
|
|
|
_this->AiUpdate(true);
|
|
|
|
}
|
2016-01-18 11:26:15 +00:00
|
|
|
}
|
|
|
|
#endif
|