2009-07-28 21:32:10 +00:00
// Copyright (C) 2003 Dolphin Project.
2008-12-08 04:46:09 +00: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, version 2.0.
// 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
// GNU General Public License 2.0 for more details.
// A copy of the GPL 2.0 should have been included with the program.
// If not, see http://www.gnu.org/licenses/
// Official SVN repository and contact information can be found at
// http://code.google.com/p/dolphin-emu/
# ifndef _UCODE_ZELDA_H
# define _UCODE_ZELDA_H
# include "Common.h"
# include "UCodes.h"
2009-06-28 17:58:52 +00:00
// Obviously missing things that must be in here, somewhere among the "unknown":
// * Volume
// * L/R Pan
// * (probably) choice of resampling algorithm (point, linear, cubic)
2009-06-28 14:22:51 +00:00
2009-07-11 13:57:21 +00:00
union ZeldaVoicePB
2009-06-28 14:22:51 +00:00
{
2009-07-17 22:57:56 +00:00
struct
{
2009-07-11 13:57:21 +00:00
// Read-Write part
u16 Status ; // 0x00 | 1 = play, 0 = stop
u16 KeyOff ; // 0x01 | writing 1 stops voice?
u16 RatioInt ; // 0x02 | Position delta (playback speed)
u16 Unk03 ; // 0x03 | unknown
u16 NeedsReset ; // 0x04 | indicates if some values in PB need to be reset
u16 ReachedEnd ; // 0x05 | set to 1 when end reached
u16 IsBlank ; // 0x06 | 0 = normal sound, 1 = samples are always the same
u16 Unk07 ; // 0x07 | unknown, in zelda always 0x0010. Something to do with number of saved samples (0x68)?
u16 SoundType ; // 0x08 | "Sound type": so far in zww: 0x0d00 for music (volume mode 0), 0x4861 for sfx (volume mode 1)
u16 volumeLeft1 ; // 0x09 | Left Volume 1 // There's probably two of each because they should be ramped within each frame.
u16 volumeLeft2 ; // 0x0A | Left Volume 2
u16 Unk0B ; // 0x0B | unknown
u16 SoundType2 ; // 0x0C | "Sound type" 2 (not really sound type)
u16 volumeRight1 ; // 0x0D | Right Volume 1
u16 volumeRight2 ; // 0x0E | Right Volume 2
u16 Unk0F ; // 0x0F | unknown
u16 SoundType3 ; // 0x10 | "Sound type" 3 (not really sound type)
u16 volumeUnknown1_1 ; // 0x11 | Unknown Volume 1
u16 volumeUnknown1_2 ; // 0x12 | Unknown Volume 1
u16 Unk13 ; // 0x13 | unknown
u16 SoundType4 ; // 0x14 | "Sound type" 4 (not really sound type)
u16 volumeUnknown2_1 ; // 0x15 | Unknown Volume 2
u16 volumeUnknown2_2 ; // 0x16 | Unknown Volume 2
u16 Unk17 ; // 0x17 | unknown
u16 Unk18 [ 0x10 ] ; // 0x18 | unknown
u16 Unk28 ; // 0x28 | unknown
u16 Unk29 ; // 0x29 | unknown // multiplied by 0x2a @ 0d21/ZWW
u16 Unk2a ; // 0x2A | unknown // loaded at 0d2e/ZWW
u16 Unk2b ; // 0x2B | unknown
u16 VolumeMode ; // 0x2C | unknown // See 0337/ZWW
u16 Unk2D ; // 0x2D | unknown
u16 Unk2E ; // 0x2E | unknown
u16 Unk2F ; // 0x2F | unknown
u16 CurSampleFrac ; // 0x30 | Fractional part of the current sample position
u16 Unk31 ; // 0x31 | unknown / unused
2009-07-12 19:34:59 +00:00
u16 CurBlock ; // 0x32 | current block? used by zelda's AFC decoder. we don't need it.
2009-07-11 13:57:21 +00:00
u16 FixedSample ; // 0x33 | sample value for "blank" voices
2009-07-12 19:34:59 +00:00
u32 RestartPos ; // 0x34 | restart pos / "loop start offset"
2009-07-11 13:57:21 +00:00
u16 Unk36 [ 2 ] ; // 0x36 | unknown // loaded at 0adc/ZWW in 0x21 decoder
u32 CurAddr ; // 0x38 | current address
u32 RemLength ; // 0x3A | remaining length
2009-07-12 12:22:39 +00:00
u16 ResamplerOldData [ 4 ] ; // 0x3C | The resampler stores the last 4 decoded samples here from the previous frame, so that the filter kernel has something to read before the start of the buffer.
2009-07-11 13:57:21 +00:00
u16 Unk40 [ 0x10 ] ; // 0x40 | Used as some sort of buffer by IIR
u16 Unk50 [ 0x8 ] ; // 0x50 | Used as some sort of buffer by 06ff/ZWW
u16 Unk58 [ 0x8 ] ; // 0x58 |
u16 Unk60 [ 0x6 ] ; // 0x60 |
u16 YN2 ; // 0x66 | YN2
u16 YN1 ; // 0x67 | YN1
u16 Unk68 [ 0x10 ] ; // 0x68 | Saved samples from last decode?
u16 FilterState1 ; // 0x78 | unknown // ZWW: 0c84_FilterBufferInPlace loads and stores. Simply, the filter state.
u16 FilterState2 ; // 0x79 | unknown // ZWW: same as above. these two are active if 0x04a8 != 0.
u16 Unk7A ; // 0x7A | unknown
u16 Unk7B ; // 0x7B | unknown
u16 Unk7C ; // 0x7C | unknown
u16 Unk7D ; // 0x7D | unknown
u16 Unk7E ; // 0x7E | unknown
u16 Unk7F ; // 0x7F | unknown
// Read-only part
u16 Format ; // 0x80 | audio format
u16 RepeatMode ; // 0x81 | 0 = one-shot, non zero = loop
2009-07-12 19:34:59 +00:00
u16 LoopYN1 ; // 0x82 | YN1 reload (when AFC loops)
u16 LoopYN2 ; // 0x83 | YN2 reload (when AFC loops)
2009-07-11 13:57:21 +00:00
u16 Unk84 ; // 0x84 | IIR Filter # coefs?
u16 StopOnSilence ; // 0x85 | Stop on silence? (Flag for something volume related. Decides the weird stuff at 035a/ZWW, alco 0cd3)
u16 Unk86 ; // 0x86 | unknown
u16 Unk87 ; // 0x87 | unknown
u32 LoopStartPos ; // 0x88 | loopstart pos
u32 Length ; // 0x8A | sound length
u32 StartAddr ; // 0x8C | sound start address
u32 UnkAddr ; // 0x8E | ???
u16 Padding [ 0x10 ] ; // 0x90 | padding
u16 Padding2 [ 0x8 ] ; // 0xa0 | FIR filter coefs of some sort
u16 FilterEnable ; // 0xa8 | FilterBufferInPlace enable.
u16 Padding3 [ 0x7 ] ; // 0xa9 | padding
u16 Padding4 [ 0x10 ] ; // 0xb0 | padding
} ;
2009-07-12 19:34:59 +00:00
u16 raw [ 0xc0 ] ; // WARNING-do not use on parts of the 32-bit values - they are swapped!
2009-06-28 14:22:51 +00:00
} ;
2009-07-17 22:57:56 +00:00
union ZeldaUnkPB
{
struct
{
u16 Control ; // 0x00 | control
u16 Unk01 ; // 0x01 | unknown
u32 SrcAddr ; // 0x02 | some address
u16 Unk04 [ 0xC ] ; // 0x04 | unknown
} ;
u16 raw [ 16 ] ;
} ;
2009-06-17 19:50:59 +00:00
namespace {
2009-06-28 17:58:52 +00:00
// If this miscompiles, adjust the size of ZeldaVoicePB to 0x180 bytes (0xc0 shorts).
CompileTimeAssert < sizeof ( ZeldaVoicePB ) = = 0x180 > ensure_zpb_size_correct ;
2009-06-17 19:50:59 +00:00
} // namespace
2009-06-16 21:58:49 +00:00
2008-12-08 04:46:09 +00:00
class CUCode_Zelda : public IUCode
{
2009-06-20 10:39:45 +00:00
public :
2009-06-28 14:22:51 +00:00
CUCode_Zelda ( CMailHandler & _rMailHandler , u32 _CRC ) ;
2009-06-20 10:39:45 +00:00
virtual ~ CUCode_Zelda ( ) ;
void HandleMail ( u32 _uMail ) ;
2009-07-12 23:51:18 +00:00
void HandleMail_LightVersion ( u32 _uMail ) ;
2009-07-17 22:57:56 +00:00
void HandleMail_SMSVersion ( u32 _uMail ) ;
2009-07-12 23:51:18 +00:00
void HandleMail_NormalVersion ( u32 _uMail ) ;
2009-06-20 10:39:45 +00:00
void Update ( int cycles ) ;
void MixAdd ( short * buffer , int size ) ;
void CopyPBsFromRAM ( ) ;
void CopyPBsToRAM ( ) ;
void DoState ( PointerWrap & p ) ;
int * templbuffer ;
int * temprbuffer ;
2009-07-11 13:57:21 +00:00
// Simple dump ...
2009-06-20 10:39:45 +00:00
int DumpAFC ( u8 * pIn , const int size , const int srate ) ;
2009-06-28 14:22:51 +00:00
u32 Read32 ( )
{
u32 res = * ( u32 * ) & m_Buffer [ m_readOffset ] ;
m_readOffset + = 4 ;
return res ;
}
2008-12-08 04:46:09 +00:00
private :
enum EDSP_Codes
{
2009-06-20 01:33:13 +00:00
DSP_INIT = 0xDCD10000 ,
DSP_RESUME = 0xDCD10001 ,
DSP_YIELD = 0xDCD10002 ,
DSP_DONE = 0xDCD10003 ,
DSP_SYNC = 0xDCD10004 ,
DSP_FRAME_END = 0xDCD10005 ,
2008-12-08 04:46:09 +00:00
} ;
2009-07-11 13:57:21 +00:00
// These map CRC to behaviour.
2009-07-12 23:51:18 +00:00
// DMA version
// - sound data transferred using DMA instead of accelerator
bool IsDMAVersion ( ) const
{
switch ( m_CRC )
{
case 0xD643001F : // Super Mario Galaxy
return true ;
default :
return false ;
}
}
// Light version
// - slightly different communication protocol (no list begin mail)
// - exceptions and interrupts not used
bool IsLightVersion ( ) const
{
switch ( m_CRC )
{
case 0x42f64ac4 : // Luigi
2009-07-17 16:20:55 +00:00
case 0x267fd05a : // Pikmin PAL
2009-07-12 23:51:18 +00:00
case 0x4be6a5cb : // AC, Pikmin
case 0x088e38a5 : // IPL - JAP
case 0xd73338cf : // IPL
return true ;
default :
return false ;
}
}
2009-07-11 13:57:21 +00:00
2009-07-17 22:57:56 +00:00
// SMS version
// - sync mails are sent every frame, not every 16 PBs
// (named SMS because it's used by Super Mario Sunshine
// and I couldn't find a better name)
bool IsSMSVersion ( ) const
{
switch ( m_CRC )
{
case 0x56d36052 :
return true ;
default :
return false ;
}
}
2009-06-28 14:22:51 +00:00
u32 m_CRC ;
2009-07-03 18:33:28 +00:00
// These are the only dynamically allocated things allowed in the ucode.
2009-07-12 12:22:39 +00:00
s32 * m_VoiceBuffer ;
2009-07-12 13:45:01 +00:00
s16 * m_ResampleBuffer ;
2009-06-28 14:22:51 +00:00
s32 * m_LeftBuffer ;
s32 * m_RightBuffer ;
2009-06-09 19:51:19 +00:00
2009-07-03 18:33:28 +00:00
// If you add variables, remember to keep DoState() and the constructor up to date.
2009-06-29 18:11:32 +00:00
s16 m_AFCCoefTable [ 32 ] ;
2009-07-11 13:57:21 +00:00
s16 m_MiscTable [ 0x280 ] ;
2009-06-09 19:51:19 +00:00
2009-06-28 14:22:51 +00:00
bool m_bSyncInProgress ;
u32 m_MaxVoice ;
u32 m_SyncFlags [ 16 ] ;
2009-07-17 23:26:21 +00:00
// Used by SMS version
u32 m_NumSyncMail ;
2009-06-28 14:22:51 +00:00
u32 m_NumVoices ;
bool m_bSyncCmdPending ;
u32 m_CurVoice ;
u32 m_CurBuffer ;
u32 m_NumBuffers ;
// Those are set by command 0x1 (DsetupTable)
u32 m_VoicePBsAddr ;
u32 m_UnkTableAddr ;
u32 m_AFCCoefTableAddr ;
u32 m_ReverbPBsAddr ;
u32 m_RightBuffersAddr ;
u32 m_LeftBuffersAddr ;
//u32 m_unkAddr;
u32 m_pos ;
// Only in SMG ucode
// Set by command 0xE (DsetDMABaseAddr)
u32 m_DMABaseAddr ;
// List, buffer management =====================
2008-12-08 04:46:09 +00:00
u32 m_numSteps ;
2009-06-28 14:22:51 +00:00
bool m_bListInProgress ;
2008-12-08 04:46:09 +00:00
u32 m_step ;
u8 m_Buffer [ 1024 ] ;
2009-06-20 01:33:13 +00:00
2008-12-08 04:46:09 +00:00
u32 m_readOffset ;
2009-06-20 01:33:13 +00:00
enum EMailState
{
WaitForMail ,
ReadingFrameSync ,
ReadingMessage ,
ReadingSystemMsg
} ;
2009-06-20 10:39:45 +00:00
2009-06-20 01:33:13 +00:00
EMailState m_MailState ;
u16 m_PBMask [ 0x10 ] ;
u32 m_NumPBs ;
2009-06-20 10:39:45 +00:00
u32 m_PBAddress ; // The main param block array
u32 m_PBAddress2 ; // 4 smaller param blocks
2009-07-03 18:33:28 +00:00
void ExecuteList ( ) ;
2009-06-16 21:58:49 +00:00
2009-07-12 19:34:59 +00:00
u8 * GetARAMPointer ( u32 address ) ;
2009-07-06 12:12:05 +00:00
// AFC decoder
2009-07-06 12:20:01 +00:00
static void AFCdecodebuffer ( const s16 * coef , const char * input , signed short * out , short * histp , short * hist2p , int type ) ;
2009-07-06 12:12:05 +00:00
2009-06-28 14:22:51 +00:00
void ReadVoicePB ( u32 _Addr , ZeldaVoicePB & PB ) ;
void WritebackVoicePB ( u32 _Addr , ZeldaVoicePB & PB ) ;
2009-06-16 21:58:49 +00:00
2009-07-01 20:55:43 +00:00
// Voice formats
void RenderSynth_Constant ( ZeldaVoicePB & PB , s32 * _Buffer , int _Size ) ;
2009-07-08 01:08:43 +00:00
void RenderSynth_RectWave ( ZeldaVoicePB & PB , s32 * _Buffer , int _Size ) ;
void RenderSynth_SawWave ( ZeldaVoicePB & PB , s32 * _Buffer , int _Size ) ;
2009-07-12 19:34:59 +00:00
2009-07-13 20:56:49 +00:00
void RenderVoice_PCM8 ( ZeldaVoicePB & PB , s16 * _Buffer , int _Size ) ;
2009-07-12 13:45:01 +00:00
void RenderVoice_PCM16 ( ZeldaVoicePB & PB , s16 * _Buffer , int _Size ) ;
2009-07-13 20:56:49 +00:00
2009-07-12 13:45:01 +00:00
void RenderVoice_AFC ( ZeldaVoicePB & PB , s16 * _Buffer , int _Size ) ;
2009-07-12 19:34:59 +00:00
void RenderVoice_Raw ( ZeldaVoicePB & PB , s16 * _Buffer , int _Size ) ;
2009-07-01 15:54:52 +00:00
2009-07-12 13:45:01 +00:00
void Resample ( ZeldaVoicePB & PB , int size , s16 * in , s32 * out , bool do_resample = false ) ;
int ConvertRatio ( int pb_ratio ) ;
int SizeForResampling ( ZeldaVoicePB & PB , int size , int ratio ) ;
2009-07-12 12:22:39 +00:00
2009-07-01 20:55:43 +00:00
// Renders a voice and mixes it into LeftBuffer, RightBuffer
void RenderAddVoice ( ZeldaVoicePB & PB , s32 * _LeftBuffer , s32 * _RightBuffer , int _Size ) ;
2008-12-08 04:46:09 +00:00
} ;
# endif
2009-06-20 01:33:13 +00:00