From 92e63e3ac4ef11e01b83202069842b9da88c08e9 Mon Sep 17 00:00:00 2001
From: "XTra.KrazzY" <XTra.KrazzY@gmail.com>
Date: Tue, 7 Jul 2009 15:12:52 +0000
Subject: [PATCH] Various changes: Fixed linux build, added some zelda ucode
 RE, added HLE mail logging features

git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@3704 8ced0084-cf51-0410-be5f-012b33b47a6e
---
 Externals/WiiUseSrc/Src/SConscript            |  4 +-
 Source/Core/AudioCommon/Src/AOSoundStream.cpp |  4 +-
 Source/Core/Common/Src/Log.h                  |  1 +
 Source/Core/Common/Src/LogManager.cpp         |  1 +
 .../Core/Src/Debugger/Debugger_SymbolMap.cpp  |  2 +-
 Source/Core/DSPCore/Src/DSPHWInterface.cpp    |  8 +--
 Source/Core/DebuggerWX/Src/BreakPointDlg.cpp  |  2 +-
 .../Plugins/Plugin_DSP_HLE/Src/DSPHandler.cpp |  8 +--
 .../Plugin_DSP_HLE/Src/MailHandler.cpp        |  4 +-
 .../Plugin_DSP_HLE/Src/UCodes/UCode_Zelda.cpp |  7 +-
 .../Src/UCodes/UCode_Zelda_ADPCM.cpp          |  3 +
 Source/Plugins/Plugin_DSP_HLE/Src/main.cpp    |  3 +-
 docs/DSP/DSP_UC_Zelda.txt                     | 67 ++++++++++++-------
 13 files changed, 68 insertions(+), 46 deletions(-)

diff --git a/Externals/WiiUseSrc/Src/SConscript b/Externals/WiiUseSrc/Src/SConscript
index cb9b97617b..98e7b34c3a 100644
--- a/Externals/WiiUseSrc/Src/SConscript
+++ b/Externals/WiiUseSrc/Src/SConscript
@@ -20,10 +20,10 @@ files = [
 	]
 
 if env['HAVE_BLUEZ']:
-	files += [ "io_nix.c", ]
+	files += [ "io_nix.cpp", ]
 
 if sys.platform == 'darwin':
-	files += [ "io_osx.c", ]
+	files += [ "io_osx.cpp", ]
 
 if sys.platform == 'darwin':
 	env_wiiuse = env.Clone(
diff --git a/Source/Core/AudioCommon/Src/AOSoundStream.cpp b/Source/Core/AudioCommon/Src/AOSoundStream.cpp
index c3cae83f74..ba2623d156 100644
--- a/Source/Core/AudioCommon/Src/AOSoundStream.cpp
+++ b/Source/Core/AudioCommon/Src/AOSoundStream.cpp
@@ -46,7 +46,7 @@ void AOSound::SoundLoop()
     while (!threadData)
 	{
         soundCriticalSection.Enter();
-		m_mixer->Mix(realtimeBuffer, numBytesToRender >> 2);
+        m_mixer->Mix(realtimeBuffer, numBytesToRender >> 2);
         ao_play(device, (char*)realtimeBuffer, numBytesToRender);
         soundCriticalSection.Leave();
 
@@ -65,7 +65,7 @@ void *soundThread(void *args)
 
 bool AOSound::Start()
 {
-	memset(realtimeBuffer, 0, sizeof(realtimeBuffer));
+    memset(realtimeBuffer, 0, sizeof(realtimeBuffer));
 
     soundSyncEvent.Init();
 	
diff --git a/Source/Core/Common/Src/Log.h b/Source/Core/Common/Src/Log.h
index dd7ce841c3..d5f7f3d725 100644
--- a/Source/Core/Common/Src/Log.h
+++ b/Source/Core/Common/Src/Log.h
@@ -38,6 +38,7 @@ enum LOG_TYPE {
 	DISCIO,
 	DSPHLE,
 	DSPLLE,
+        DSP_MAIL,
 	DSPINTERFACE,
 	DVDINTERFACE,
 	DYNA_REC,
diff --git a/Source/Core/Common/Src/LogManager.cpp b/Source/Core/Common/Src/LogManager.cpp
index da2898c9b2..0159ddc174 100644
--- a/Source/Core/Common/Src/LogManager.cpp
+++ b/Source/Core/Common/Src/LogManager.cpp
@@ -45,6 +45,7 @@ LogManager::LogManager()\
 	m_Log[LogTypes::HLE]				= new LogContainer("HLE",			"HLE");
 	m_Log[LogTypes::DSPHLE]			    = new LogContainer("DSPHLE",		"DSP HLE");
 	m_Log[LogTypes::DSPLLE]			    = new LogContainer("DSPLLE",		"DSP LLE");
+       	m_Log[LogTypes::DSP_MAIL]		    = new LogContainer("DSPMails",		"DSP Mails");
 	m_Log[LogTypes::VIDEO]			    = new LogContainer("Video",			"Video Plugin");
 	m_Log[LogTypes::AUDIO]			    = new LogContainer("Audio",			"Audio Plugin");
 	m_Log[LogTypes::DYNA_REC]			= new LogContainer("JIT",			"Dynamic Recompiler");
diff --git a/Source/Core/Core/Src/Debugger/Debugger_SymbolMap.cpp b/Source/Core/Core/Src/Debugger/Debugger_SymbolMap.cpp
index e25dcf1247..ed83e27a20 100644
--- a/Source/Core/Core/Src/Debugger/Debugger_SymbolMap.cpp
+++ b/Source/Core/Core/Src/Debugger/Debugger_SymbolMap.cpp
@@ -23,7 +23,7 @@
 #include "../PowerPC/PowerPC.h"
 #include "../PowerPC/PPCAnalyst.h"
 #include "../PowerPC/PPCSymbolDB.h"
-#include "Bochs_disasm/PowerPCDisasm.h"
+#include "PowerPCDisasm.h"
 
 namespace Dolphin_Debugger
 {
diff --git a/Source/Core/DSPCore/Src/DSPHWInterface.cpp b/Source/Core/DSPCore/Src/DSPHWInterface.cpp
index e87314bdb0..727fb8d54a 100644
--- a/Source/Core/DSPCore/Src/DSPHWInterface.cpp
+++ b/Source/Core/DSPCore/Src/DSPHWInterface.cpp
@@ -90,9 +90,9 @@ void gdsp_mbox_write_l(u8 mbx, u16 val)
 #ifdef DEBUG_EXP
 	if (mbx == GDSP_MBOX_DSP)
 	{
-		NOTICE_LOG(DSPLLE, "DSP(WM) B:%i M:0x%08x (pc=0x%04x)", mbx, gdsp_mbox_peek(GDSP_MBOX_DSP), g_dsp.pc);
+		NOTICE_LOG(DSP_MAIL, "DSP(WM) B:%i M:0x%08x (pc=0x%04x)", mbx, gdsp_mbox_peek(GDSP_MBOX_DSP), g_dsp.pc);
 	} else {
-		NOTICE_LOG(DSPLLE, "CPU(WM) B:%i M:0x%08x (pc=0x%04x)", mbx, gdsp_mbox_peek(GDSP_MBOX_CPU), g_dsp.pc);
+		NOTICE_LOG(DSP_MAIL, "CPU(WM) B:%i M:0x%08x (pc=0x%04x)", mbx, gdsp_mbox_peek(GDSP_MBOX_CPU), g_dsp.pc);
 	}
 #endif
 }
@@ -119,9 +119,9 @@ u16 gdsp_mbox_read_l(u8 mbx)
 #ifdef DEBUG_EXP
 	if (mbx == GDSP_MBOX_DSP)
 	{
-		NOTICE_LOG(DSPLLE, "DSP(RM) B:%i M:0x%08x (pc=0x%04x)", mbx, gdsp_mbox_peek(GDSP_MBOX_DSP), g_dsp.pc);
+		NOTICE_LOG(DSP_MAIL, "DSP(RM) B:%i M:0x%08x (pc=0x%04x)", mbx, gdsp_mbox_peek(GDSP_MBOX_DSP), g_dsp.pc);
 	} else {
-		NOTICE_LOG(DSPLLE, "CPU(RM) B:%i M:0x%08x (pc=0x%04x)", mbx, gdsp_mbox_peek(GDSP_MBOX_CPU), g_dsp.pc);
+		NOTICE_LOG(DSP_MAIL, "CPU(RM) B:%i M:0x%08x (pc=0x%04x)", mbx, gdsp_mbox_peek(GDSP_MBOX_CPU), g_dsp.pc);
 	}
 #endif	
 
diff --git a/Source/Core/DebuggerWX/Src/BreakPointDlg.cpp b/Source/Core/DebuggerWX/Src/BreakPointDlg.cpp
index bd01a3526d..f51eb1a9fb 100644
--- a/Source/Core/DebuggerWX/Src/BreakPointDlg.cpp
+++ b/Source/Core/DebuggerWX/Src/BreakPointDlg.cpp
@@ -20,7 +20,7 @@
 #include "Host.h"
 #include "Debugger.h"
 #include "StringUtil.h"
-#include "PowerPC.h"
+#include "PowerPC/PowerPC.h"
 
 
 BEGIN_EVENT_TABLE(BreakPointDlg,wxDialog)
diff --git a/Source/Plugins/Plugin_DSP_HLE/Src/DSPHandler.cpp b/Source/Plugins/Plugin_DSP_HLE/Src/DSPHandler.cpp
index 987ef6db41..dbe74da60b 100644
--- a/Source/Plugins/Plugin_DSP_HLE/Src/DSPHandler.cpp
+++ b/Source/Plugins/Plugin_DSP_HLE/Src/DSPHandler.cpp
@@ -15,10 +15,6 @@
 // Official SVN repository and contact information can be found at
 // http://code.google.com/p/dolphin-emu/
 
-#ifdef _WIN32
-#include "DSoundStream.h"
-#endif
-
 #include "DSPHandler.h"
 
 CDSPHandler* CDSPHandler::m_pInstance = NULL;
@@ -72,8 +68,10 @@ unsigned short CDSPHandler::ReadControlRegister()
 
 void CDSPHandler::SendMailToDSP(u32 _uMail)
 {
-	if (m_pUCode != NULL)
+	if (m_pUCode != NULL) {
+		DEBUG_LOG(DSP_MAIL, "CPU writes 0x%08x", _uMail);
 		m_pUCode->HandleMail(_uMail);
+	}
 }
 
 IUCode* CDSPHandler::GetUCode()
diff --git a/Source/Plugins/Plugin_DSP_HLE/Src/MailHandler.cpp b/Source/Plugins/Plugin_DSP_HLE/Src/MailHandler.cpp
index dd7b806599..047e61c85c 100644
--- a/Source/Plugins/Plugin_DSP_HLE/Src/MailHandler.cpp
+++ b/Source/Plugins/Plugin_DSP_HLE/Src/MailHandler.cpp
@@ -30,7 +30,7 @@ CMailHandler::~CMailHandler()
 void CMailHandler::PushMail(u32 _Mail)
 {
 	m_Mails.push(_Mail);
-
+	DEBUG_LOG(DSP_MAIL, "DSP writes 0x%08x", _Mail);
 	Update();
 }
 
@@ -53,7 +53,7 @@ u16 CMailHandler::ReadDSPMailboxLow()
 	if (!m_Mails.empty())
 	{
 		u16 result = m_Mails.front() & 0xFFFF;
-		m_Mails.pop();
+                m_Mails.pop();
 
 		Update();
 
diff --git a/Source/Plugins/Plugin_DSP_HLE/Src/UCodes/UCode_Zelda.cpp b/Source/Plugins/Plugin_DSP_HLE/Src/UCodes/UCode_Zelda.cpp
index 451c169601..6ad0b50ac1 100644
--- a/Source/Plugins/Plugin_DSP_HLE/Src/UCodes/UCode_Zelda.cpp
+++ b/Source/Plugins/Plugin_DSP_HLE/Src/UCodes/UCode_Zelda.cpp
@@ -1,4 +1,4 @@
-// Copyright (C) 2003-2008 Dolphin Project.
+// Copyright (C) 2003-2009 Dolphin Project.
 
 // 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
@@ -16,7 +16,8 @@
 // http://code.google.com/p/dolphin-emu/
 
 // Games that uses this UCode:
-// Zelda: The Windwaker, Mario Sunshine, Mario Kart, Twilight Princess
+// Zelda: The Windwaker, Mario Sunshine, Mario Kart, Twilight Princess,
+// Super Mario Galaxy
 
 #include "../Globals.h"
 #include "UCodes.h"
@@ -273,7 +274,7 @@ void CUCode_Zelda::ExecuteList()
 			// SyncFrame ... zelda ww jumps to 0x0243
 		case 0x02:
 		{
-			//	soundStream->GetMixer()->SetHLEReady(true);
+                    //soundStream->GetMixer()->SetHLEReady(true);
 			//	DEBUG_LOG(DSPHLE, "Update the SoundThread to be in sync");
 			//soundStream->Update(); //do it in this thread to avoid sync problems
 
diff --git a/Source/Plugins/Plugin_DSP_HLE/Src/UCodes/UCode_Zelda_ADPCM.cpp b/Source/Plugins/Plugin_DSP_HLE/Src/UCodes/UCode_Zelda_ADPCM.cpp
index be5c7c5cf0..64d34a2023 100644
--- a/Source/Plugins/Plugin_DSP_HLE/Src/UCodes/UCode_Zelda_ADPCM.cpp
+++ b/Source/Plugins/Plugin_DSP_HLE/Src/UCodes/UCode_Zelda_ADPCM.cpp
@@ -50,6 +50,9 @@ void CUCode_Zelda::AFCdecodebuffer(const s16 *coef, const char *input, signed sh
     else
     {
         // untested !!! i havnt seen such a sample yet :)
+
+        ERROR_LOG(DSPHLE, "Untested AFC sample");
+        
         for (int i = 0; i < 16; i += 4)
         {
             int j = (*src >> 0) & 0x02;
diff --git a/Source/Plugins/Plugin_DSP_HLE/Src/main.cpp b/Source/Plugins/Plugin_DSP_HLE/Src/main.cpp
index a3a084b21e..2a5bb7c74d 100644
--- a/Source/Plugins/Plugin_DSP_HLE/Src/main.cpp
+++ b/Source/Plugins/Plugin_DSP_HLE/Src/main.cpp
@@ -203,7 +203,8 @@ void Initialize(void *init)
 	CDSPHandler::CreateInstance();
 
 	soundStream = AudioCommon::InitSoundStream(new HLEMixer()); 
-
+        if(!soundStream)
+            PanicAlert("Error starting up sound stream");
 }
 
 void DSP_StopSoundStream()
diff --git a/docs/DSP/DSP_UC_Zelda.txt b/docs/DSP/DSP_UC_Zelda.txt
index ba79345db3..1101b7bd28 100644
--- a/docs/DSP/DSP_UC_Zelda.txt
+++ b/docs/DSP/DSP_UC_Zelda.txt
@@ -3450,7 +3450,7 @@ void 0a91_Unk() {
 	AC1.M = 0x50;
 	AR3 = 0x520;
 
-	0ab3_Decoder0x21Core(AC1.M=0x50, AR3=#0x0580);
+	0ab3_Decoder0x21Core(AC1.M=0x50, AR3=#0x0520);
 
 	// 0aa0 029f 02d8 jmp         0x02d8
 	GOTO ContinueWithBlock:  // in SyncFrame
@@ -3492,11 +3492,12 @@ void 0ab3_Decoder0x21Core(AC1.M, AR3) {
 	// 0ab8 1f1f      mrr         $AX0.L, $AC1.M
 	// 0ab9 0a00      lris        $AX0.H, #0x00
 	// 0aba 5800      subax       $ACC0, $AX0.L
-	ACC0 = [0x043a,0x043b] - AC1.M;
+	ACC0 =  [0x043a,0x043b];
+	ACC0 -= AC1.M;
 
 	
 	// 0abb 0292 0ad1 jg          0x0ad1
-	if ([0x043a,0x043b] > *0x043a) {
+	if ([0x043a,0x043b] > AC1.M) {
 		// Happens when sound has finished playing?
 
 		// 0abd 8900      clr         $ACC1
@@ -3505,7 +3506,7 @@ void 0ab3_Decoder0x21Core(AC1.M, AR3) {
 		AR0  = 0x043b;
 		
 		// 0ac0 02bf 0af6 call        0x0af6  // 0af6_Decoder0x21_MoreStuff()
-		0af6_Decoder0x21_MoreStuff(AR0=0x043b);
+		0af6_Decoder0x21_MoreStuff(AR0=0x043b, AR3);
 		
 		// 0ac2 8100      clr         $ACC0
 		// 0ac3 1fd8      mrr         $AC0.M, $AX0.L
@@ -3574,7 +3575,7 @@ void 0ab3_Decoder0x21Core(AC1.M, AR3) {
 	if (ACC0 < 0) {
 		// 0adc 00c0 0436 lr          $AR0, @0x0436
 		// 0ade 02bf 0af6 call        0x0af6
-		0af6_Decoder0x21_MoreStuff(AR0=*0x0436);
+		0af6_Decoder0x21_MoreStuff(AR0=*0x0436, AR3);
 		
 		// 0ae0 8100      clr         $ACC0
 		// 0ae1 1fd8      mrr         $AC0.M, $AX0.L
@@ -3605,7 +3606,7 @@ void 0ab3_Decoder0x21Core(AC1.M, AR3) {
 		*0x048d = *0x0489;
 
 		// 0aeb 02bf 0af6 call        0x0af6
-		0af6_Decoder0x21_MoreStuff(AR0=*0x0436);
+		0af6_Decoder0x21_MoreStuff(AR0=*0x0436, AR3);
 		
 		0aed 0092 00ff lri         $CR, #0x00ff
 		// 0aef 02df      ret  
@@ -3616,7 +3617,7 @@ void 0ab3_Decoder0x21Core(AC1.M, AR3) {
 	// 0af1 02bf 0af6 call        0x0af6
 
 	AR0 = AX0.L;
-	0af6_Decoder0x21_MoreStuff(AR0=AX0.L);
+	0af6_Decoder0x21_MoreStuff(AR0=AX0.L, AR3);
 
 
 	0af3 0092 00ff lri         $CR, #0x00ff
@@ -3627,16 +3628,13 @@ void 0ab3_Decoder0x21Core(AC1.M, AR3) {
 // CR = 0x4
 // Does strange stuff with PB[0x34] and the address PB[0x8c,d]
 // Does not touch AX0.L
-void 0af6_Decoder0x21_MoreStuff($AR0) {
+void 0af6_Decoder0x21_MoreStuff($AR0, $AR3) {
 	// 0af6 8100      clr         $ACC0
 	// 0af7 1fc0      mrr         $AC0.M, $AR0
 	// 0af8 b100      tst         $ACC0
 	// 0af9 02d5      retz        
 	if (!AR0)
 		return;
-
-	ACC0 = 0;
-	AC0.M = AR0;
 	
 	// 0afa 8900      clr         $ACC1
 	// 0afb 2734      lrs         $AC1.M, @0x0034
@@ -3656,7 +3654,7 @@ void 0af6_Decoder0x21_MoreStuff($AR0) {
 	// 0b06 5a00      subax       $ACC0, $AX1.L
 	// 0b07 5a00      subax       $ACC0, $AX1.L
 	
-	ACC0 = {8c,8d} + *0x0434 * 2 - (*0x0434 & 1) * 2
+	ACC0 = [8c,8d] + *0x0434 * 2 - ((*0x0434 & 1) * 2);
 
 	// 0b08 1c20      mrr         $AR1, $AR0
 	
@@ -3667,9 +3665,12 @@ void 0af6_Decoder0x21_MoreStuff($AR0) {
 	ACC1 = ($AR0 << 16) + 0x20000;
 	
 	// 
-	0b0b 1c1f      mrr         $AR0, $AC1.M
-	0b0c 009f 0b00 lri         $AC1.M, #0x0b00
+	// 0b0b 1c1f      mrr         $AR0, $AC1.M
+	// 0b0c 009f 0b00 lri         $AC1.M, #0x0b00
 	0b0e 0092 00ff lri         $CR, #0x00ff
+
+        AR0 = AC1.M;
+        AC1.M = 0x0b00;
 	
 	// Load more audio from RAM by DMA??
 	
@@ -3697,19 +3698,35 @@ void 0af6_Decoder0x21_MoreStuff($AR0) {
 	// 0b20 1fe1      mrr         $AC1.M, $AR1
 	// 0b21 191e      lrri        $AC0.M, @$AR0
 	// 0b22 0701      cmpis       $ACC1, #0x01
-	AC0.M = *$AR0;
+        ACC1 = 0;
+        AC1.M = AR1;
+	AC0.M = *$AR0++;
 	// 0b23 0293 0b2c jle         0x0b2c
-	if (ACC1 > 1<<16) {
-		0b25 191a      lrri        $AX0.H, @$AR0
-		0b26 05fe      addis       $ACC1, #0xfe
-		0b27 005f      loop        $AC1.M
-			0b28 64a0      movr'ls     $ACC0, $AX0.H : $AX0.H, $AC0.M
-		0b29 1b7e      srri        @$AR3, $AC0.M
-		0b2a 1b7a      srri        @$AR3, $AX0.H
-		0b2b 02df      ret    
+	if (AC1.M > 1) {
+		// 0b25 191a      lrri        $AX0.H, @$AR0
+		// 0b26 05fe      addis       $ACC1, #0xfe
+		AX0.H = *$AR0++;
+		ACC1 += 0xfe0000;
+
+		// 0b27 005f      loop        $AC1.M
+		for(int i = 0; i < AC1.M; i++) {
+			// 0b28 64a0      movr'ls     $ACC0, $AX0.H : $AX0.H, $AC0.M
+			AC0.L = 0;
+			AC0.H = AX0.H;
+			AC0.M = AX0.H;
+			AX0.H = *$AR0++;
+			*$AR3++ = AC0.M;
+		}
+		//0b29 1b7e      srri        @$AR3, $AC0.M
+		//0b2a 1b7a      srri        @$AR3, $AX0.H
+		//0b2b 02df      ret
+
+		*$AR3++ = AC0.M;
+		*$AR3++ = AX0.H;
 	} else {
-		0b2c 1b7e      srri        @$AR3, $AC0.M
-		0b2d 02df      ret         
+		//0b2c 1b7e      srri        @$AR3, $AC0.M
+		//0b2d 02df      ret
+		*$AR3++ = AC0.M;
 	}
 }