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-05-23 10:05:41 +00:00
|
|
|
#include "stdafx.h"
|
2016-01-13 11:15:30 +00:00
|
|
|
#include <Project64-core/N64System/Mips/Audio.h>
|
2015-12-06 09:59:58 +00:00
|
|
|
#include <Project64-core/N64System/SystemGlobals.h>
|
|
|
|
#include <Project64-core/N64System/N64Class.h>
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2015-04-28 22:19:02 +00:00
|
|
|
CAudio::CAudio()
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2015-11-15 00:31:56 +00:00
|
|
|
Reset();
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
2015-04-28 22:19:02 +00:00
|
|
|
CAudio::~CAudio()
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-04-28 22:19:02 +00:00
|
|
|
void CAudio::Reset()
|
2010-05-22 04:47:15 +00:00
|
|
|
{
|
2015-11-15 00:31:56 +00:00
|
|
|
m_SecondBuff = 0;
|
|
|
|
m_Status = 0;
|
|
|
|
m_BytesPerSecond = 0;
|
|
|
|
m_CountsPerByte = g_System->AiCountPerBytes(); // should be calculated ... see below, instead allow from user settings
|
|
|
|
if (m_CountsPerByte == 0) m_CountsPerByte = 500; // If the user has no defined value, grant a default and we will calculate
|
|
|
|
m_FramesPerSecond = 60;
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
2015-11-15 00:35:38 +00:00
|
|
|
uint32_t CAudio::GetLength()
|
2010-05-22 04:47:15 +00:00
|
|
|
{
|
2015-12-14 10:51:33 +00:00
|
|
|
WriteTrace(TraceAudio, TraceDebug, "Start (m_SecondBuff = %d)", m_SecondBuff);
|
2015-11-15 00:35:38 +00:00
|
|
|
uint32_t TimeLeft = g_SystemTimer->GetTimer(CSystemTimer::AiTimerInterrupt), Res = 0;
|
2015-11-15 00:31:56 +00:00
|
|
|
if (TimeLeft > 0)
|
|
|
|
{
|
|
|
|
Res = (TimeLeft / m_CountsPerByte);
|
|
|
|
}
|
2015-12-14 10:51:33 +00:00
|
|
|
WriteTrace(TraceAudio, TraceDebug, "Done (res = %d, TimeLeft = %d)", Res, TimeLeft);
|
2015-11-15 00:31:56 +00:00
|
|
|
return (Res + 3)&~3;
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
2015-11-15 00:35:38 +00:00
|
|
|
uint32_t CAudio::GetStatus()
|
2010-05-22 04:47:15 +00:00
|
|
|
{
|
2015-12-14 10:51:33 +00:00
|
|
|
WriteTrace(TraceAudio, TraceDebug, "m_Status = %X", m_Status);
|
2015-11-15 00:31:56 +00:00
|
|
|
return m_Status;
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
2015-04-28 22:19:02 +00:00
|
|
|
void CAudio::LenChanged()
|
2010-05-22 04:47:15 +00:00
|
|
|
{
|
2015-12-14 10:51:33 +00:00
|
|
|
WriteTrace(TraceAudio, TraceDebug, "Start (g_Reg->AI_LEN_REG = %d)", g_Reg->AI_LEN_REG);
|
2015-11-15 00:31:56 +00:00
|
|
|
if (g_Reg->AI_LEN_REG != 0)
|
|
|
|
{
|
|
|
|
if (g_Reg->AI_LEN_REG >= 0x40000)
|
|
|
|
{
|
2015-12-14 10:51:33 +00:00
|
|
|
WriteTrace(TraceAudio, TraceDebug, "*** Ignoring Write, To Large (%X)", g_Reg->AI_LEN_REG);
|
2015-11-15 00:31:56 +00:00
|
|
|
}
|
2015-11-15 00:35:38 +00:00
|
|
|
else
|
|
|
|
{
|
2015-11-15 00:31:56 +00:00
|
|
|
m_Status |= ai_busy;
|
2015-11-15 00:35:38 +00:00
|
|
|
uint32_t AudioLeft = g_SystemTimer->GetTimer(CSystemTimer::AiTimerInterrupt);
|
2015-11-15 00:31:56 +00:00
|
|
|
if (m_SecondBuff == 0)
|
|
|
|
{
|
|
|
|
if (AudioLeft == 0)
|
|
|
|
{
|
2015-12-14 10:51:33 +00:00
|
|
|
WriteTrace(TraceAudio, TraceDebug, "Set Timer AI_LEN_REG: %d m_CountsPerByte: %d", g_Reg->AI_LEN_REG, m_CountsPerByte);
|
2015-11-15 00:31:56 +00:00
|
|
|
g_SystemTimer->SetTimer(CSystemTimer::AiTimerInterrupt, g_Reg->AI_LEN_REG * m_CountsPerByte, false);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-12-14 10:51:33 +00:00
|
|
|
WriteTrace(TraceAudio, TraceDebug, "Increasing Second Buffer (m_SecondBuff %d Increase: %d)", m_SecondBuff, g_Reg->AI_LEN_REG);
|
2015-11-15 00:31:56 +00:00
|
|
|
m_SecondBuff += g_Reg->AI_LEN_REG;
|
|
|
|
m_Status |= ai_full;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-12-09 11:37:58 +00:00
|
|
|
g_Notify->BreakPoint(__FILE__, __LINE__);
|
2015-11-15 00:31:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-12-14 10:51:33 +00:00
|
|
|
WriteTrace(TraceAudio, TraceDebug, "*** Reset Timer to 0");
|
2015-11-15 00:31:56 +00:00
|
|
|
g_SystemTimer->StopTimer(CSystemTimer::AiTimerBusy);
|
|
|
|
g_SystemTimer->StopTimer(CSystemTimer::AiTimerInterrupt);
|
|
|
|
m_SecondBuff = 0;
|
|
|
|
m_Status = 0;
|
|
|
|
}
|
2010-07-05 11:29:46 +00:00
|
|
|
|
2015-11-15 00:31:56 +00:00
|
|
|
if (g_Plugins->Audio()->AiLenChanged != NULL)
|
|
|
|
{
|
|
|
|
g_Plugins->Audio()->AiLenChanged();
|
|
|
|
}
|
2015-12-14 10:51:33 +00:00
|
|
|
WriteTrace(TraceAudio, TraceDebug, "Done");
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
2015-04-28 22:19:02 +00:00
|
|
|
void CAudio::InterruptTimerDone()
|
2010-05-22 04:47:15 +00:00
|
|
|
{
|
2015-12-14 10:51:33 +00:00
|
|
|
WriteTrace(TraceAudio, TraceDebug, "Start (m_SecondBuff = %d)", m_SecondBuff);
|
2015-11-15 00:31:56 +00:00
|
|
|
m_Status &= ~ai_full;
|
|
|
|
g_Reg->MI_INTR_REG |= MI_INTR_AI;
|
|
|
|
g_Reg->CheckInterrupts();
|
|
|
|
if (m_SecondBuff != 0)
|
|
|
|
{
|
|
|
|
g_SystemTimer->SetTimer(CSystemTimer::AiTimerInterrupt, m_SecondBuff * m_CountsPerByte, false);
|
|
|
|
m_SecondBuff = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_Status &= ~ai_busy;
|
|
|
|
}
|
|
|
|
if (g_Reg->m_AudioIntrReg == 0)
|
|
|
|
{
|
|
|
|
g_System->SyncToAudio();
|
|
|
|
}
|
2015-12-14 10:51:33 +00:00
|
|
|
WriteTrace(TraceAudio, TraceDebug, "Done");
|
2013-01-04 22:48:25 +00:00
|
|
|
}
|
|
|
|
|
2015-04-28 22:19:02 +00:00
|
|
|
void CAudio::BusyTimerDone()
|
2013-01-04 22:48:25 +00:00
|
|
|
{
|
2015-12-14 10:51:33 +00:00
|
|
|
WriteTrace(TraceAudio, TraceDebug, "Start (m_SecondBuff = %d)", m_SecondBuff);
|
2015-12-09 11:37:58 +00:00
|
|
|
g_Notify->BreakPoint(__FILE__, __LINE__);
|
2015-11-15 00:31:56 +00:00
|
|
|
m_Status &= ~ai_busy;
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
2015-11-15 00:35:38 +00:00
|
|
|
void CAudio::SetViIntr(uint32_t VI_INTR_TIME)
|
2010-06-14 21:14:58 +00:00
|
|
|
{
|
2015-11-15 00:35:38 +00:00
|
|
|
double CountsPerSecond = (uint32_t)((double)VI_INTR_TIME * m_FramesPerSecond);
|
2015-11-15 00:31:56 +00:00
|
|
|
if (m_BytesPerSecond != 0 && (g_System->AiCountPerBytes() == 0))
|
|
|
|
{
|
2015-11-15 00:35:38 +00:00
|
|
|
m_CountsPerByte = (int32_t)((double)CountsPerSecond / (double)m_BytesPerSecond);
|
2015-11-15 00:31:56 +00:00
|
|
|
}
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
2010-06-14 21:14:58 +00:00
|
|
|
|
2015-11-15 00:35:38 +00:00
|
|
|
void CAudio::SetFrequency(uint32_t Dacrate, uint32_t System)
|
2010-06-29 02:11:22 +00:00
|
|
|
{
|
2015-12-14 10:51:33 +00:00
|
|
|
WriteTrace(TraceAudio, TraceDebug, "(Dacrate: %X System: %d): AI_BITRATE_REG = %X", Dacrate, System, g_Reg->AI_BITRATE_REG);
|
2015-11-15 00:35:38 +00:00
|
|
|
uint32_t Frequency;
|
2010-06-29 02:11:22 +00:00
|
|
|
|
2015-11-15 00:31:56 +00:00
|
|
|
switch (System)
|
|
|
|
{
|
|
|
|
case SYSTEM_PAL: Frequency = 49656530 / (Dacrate + 1); break;
|
|
|
|
case SYSTEM_MPAL: Frequency = 48628316 / (Dacrate + 1); break;
|
|
|
|
default: Frequency = 48681812 / (Dacrate + 1); break;
|
|
|
|
}
|
2010-06-29 02:11:22 +00:00
|
|
|
|
2015-11-15 00:31:56 +00:00
|
|
|
//nBlockAlign = 16 / 8 * 2;
|
|
|
|
m_BytesPerSecond = Frequency * 4;
|
|
|
|
//m_BytesPerSecond = 194532;
|
|
|
|
//m_BytesPerSecond = 128024;
|
2010-06-29 02:11:22 +00:00
|
|
|
|
2015-11-15 00:31:56 +00:00
|
|
|
m_FramesPerSecond = System == SYSTEM_PAL ? 50 : 60;
|
|
|
|
}
|