// Copyright (C) 2003 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
// 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_AX_STRUCTS_H
#define _UCODE_AX_STRUCTS_H

struct PBMixer
{
	u16 left;
	u16 left_delta;
	u16 right;
	u16 right_delta;

	u16 unknown3[8];
	u16 unknown4[6];
};

struct PBMixerWii
{
	// volume mixing values in .15, 0x8000 = ca. 1.0
	u16 left;
	u16 left_delta;
	u16 right;
	u16 right_delta;

	u16 auxA_left;
	u16 auxA_left_delta;
	u16 auxA_right;
	u16 auxA_right_delta;

	u16 auxB_left;
	u16 auxB_left_delta;
	u16 auxB_right;
	u16 auxB_right_delta;

	// Note: the following elements usage changes a little in DPL2 mode
	// TODO: implement and comment it in the mixer
	u16 auxC_left;
	u16 auxC_left_delta;
	u16 auxC_right;
	u16 auxC_right_delta;

	u16 surround;
	u16 surround_delta;
	u16 auxA_surround;
	u16 auxA_surround_delta;
	u16 auxB_surround;
	u16 auxB_surround_delta;
	u16 auxC_surround;
	u16 auxC_surround_delta;
};

struct PBMixerWM
{
	u16 main0;
	u16 main0_delta;
	u16 aux0;
	u16 aux0_delta;

	u16 main1;
	u16 main1_delta;
	u16 aux1;
	u16 aux1_delta;

	u16 main2;
	u16 main2_delta;
	u16 aux2;
	u16 aux2_delta;

	u16 main3;
	u16 main3_delta;
	u16 aux3;
	u16 aux3_delta;
};

struct PBInitialTimeDelay
{
	u16 on;
	u16 addrMemHigh;
	u16 addrMemLow;
	u16 offsetLeft;
	u16 offsetRight;
	u16 targetLeft;
	u16 targetRight;
};

// Update data - read these each 1ms subframe and use them!
// It seems that to provide higher time precisions for MIDI events, some games
// use this thing to update the parameter blocks per 1ms sub-block (a block is 5ms).
// Using this data should fix games that are missing MIDI notes.
struct PBUpdates
{
	u16 num_updates[5];
	u16 data_hi;  // These point to main RAM. Not sure about the structure of the data.
	u16 data_lo;
};

// The DSP stores the final sample values for each voice after every frame of processing.
// The values are then accumulated for all dropped voices, added to the next frame of audio,
// and ramped down on a per-sample basis to provide a gentle "roll off."
struct PBDpop
{
	s16 unknown[9];
};

struct PBDpopWii
{
	s16 left;
	s16 auxA_left;
	s16 auxB_left;
	s16 auxC_left;

	s16 right;
	s16 auxA_right;
	s16 auxB_right;
	s16 auxC_right;

	s16 surround;
	s16 auxA_surround;
	s16 auxB_surround;
	s16 auxC_surround;
};

struct PBDpopWM
{
	s16 aMain0;
	s16 aMain1;
	s16 aMain2;
	s16 aMain3;

	s16 aAux0;
	s16 aAux1;
	s16 aAux2;
	s16 aAux3;
};

struct PBVolumeEnvelope
{
	u16 cur_volume;			// volume at start of frame
	s16 cur_volume_delta;	// signed per sample delta (96 samples per frame)
};

struct PBUnknown2
{
	u16 unknown_reserved[3];
};

struct PBAudioAddr
{
	u16 looping;
	u16 sample_format;
	u16 loop_addr_hi;  // Start of loop (this will point to a shared "zero" buffer if one-shot mode is active)
	u16 loop_addr_lo;
	u16 end_addr_hi;   // End of sample (and loop), inclusive
	u16 end_addr_lo;
	u16 cur_addr_hi;
	u16 cur_addr_lo;
};

struct PBADPCMInfo
{
	s16 coefs[16];
	u16 gain;
	u16 pred_scale;
	s16 yn1;
	s16 yn2;
};

struct PBSampleRateConverter
{
	// ratio = (f32)ratio * 0x10000;
	// valid range is 1/512 to 4.0000
	u16 ratio_hi; // integer part of sampling ratio
	u16 ratio_lo; // fraction part of sampling ratio
	u16 cur_addr_frac;
	u16 last_samples[4];
};

struct PBSampleRateConverterWM
{
	u16 currentAddressFrac;
	u16 last_samples[4];
};

struct PBADPCMLoopInfo
{
	u16 pred_scale;
	u16 yn1;
	u16 yn2;
};

struct AXPB
{
	u16 next_pb_hi;
	u16 next_pb_lo;
	u16 this_pb_hi;
	u16 this_pb_lo;

	u16 src_type;     // Type of sample rate converter (none, ?, linear)
	u16 coef_select;
	u16 mixer_control;

	u16 running;       // 1=RUN 0=STOP
	u16 is_stream;     // 1 = stream, 0 = one shot

	PBMixer mixer;
	PBInitialTimeDelay initial_time_delay;  
	PBUpdates updates;
	PBDpop dpop;
	PBVolumeEnvelope vol_env;
	PBUnknown2 unknown3;
	PBAudioAddr audio_addr;
	PBADPCMInfo adpcm;
	PBSampleRateConverter src;
	PBADPCMLoopInfo adpcm_loop_info;
	u16 unknown_maybe_padding[3];
};

struct PBLowPassFilter
{
	u16 enabled;
	u16 yn1;
	u16 a0;
	u16 b0;
};

struct PBBiquadFilter
{

	u16 on;		// on = 2, off = 0
	u16 xn1;	// History data
	u16 xn2;
	u16 yn1;
	u16 yn2;
	u16 b0;		// Filter coefficients
	u16 b1;
	u16 b2;
	u16 a1;
	u16 a2;

};

union PBInfImpulseResponseWM
{
	PBLowPassFilter lpf;
	PBBiquadFilter biquad;
};

struct AXPBWii
{
	u16 next_pb_hi;
	u16 next_pb_lo;
	u16 this_pb_hi;
	u16 this_pb_lo;

	u16 src_type;		// Type of sample rate converter (none, 4-tap, linear)
	u16 coef_select;	// coef for the 4-tap src
	u32 mixer_control;

	u16 running;		// 1=RUN   0=STOP
	u16 is_stream;		// 1 = stream, 0 = one shot

	PBMixerWii mixer;
	PBInitialTimeDelay initial_time_delay;
	PBDpopWii dpop;
	PBVolumeEnvelope vol_env;
	PBAudioAddr audio_addr;
	PBADPCMInfo adpcm;
	PBSampleRateConverter src;
	PBADPCMLoopInfo adpcm_loop_info;
	PBLowPassFilter lpf;
	PBBiquadFilter biquad;

	// WIIMOTE :D
	u16 remote;
	u16 remote_mixer_control;

	PBMixerWM remote_mixer;
	PBDpopWM remote_dpop;
	PBSampleRateConverterWM remote_src;
	PBInfImpulseResponseWM remote_iir;

	u16 pad[12]; // align us, captain! (32B)
};

// Seems like nintendo used an early version of AXWii and forgot to remove the update functionality ;p
struct PBUpdatesWiiSports
{
	u16 num_updates[3];
	u16 data_hi;
	u16 data_lo;
};

struct AXPBWiiSports
{
	u16 next_pb_hi;
	u16 next_pb_lo;
	u16 this_pb_hi;
	u16 this_pb_lo;

	u16 src_type;		// Type of sample rate converter (none, 4-tap, linear)
	u16 coef_select;	// coef for the 4-tap src
	u32 mixer_control;

	u16 running;		// 1=RUN   0=STOP
	u16 is_stream;		// 1 = stream, 0 = one shot

	PBMixerWii mixer;
	PBInitialTimeDelay initial_time_delay;
	PBUpdatesWiiSports updates;
	PBDpopWii dpop;
	PBVolumeEnvelope vol_env;
	PBAudioAddr audio_addr;
	PBADPCMInfo adpcm;
	PBSampleRateConverter src;
	PBADPCMLoopInfo adpcm_loop_info;
	PBLowPassFilter lpf;
	PBBiquadFilter biquad;

	// WIIMOTE :D
	u16 remote;
	u16 remote_mixer_control;

	PBMixerWM remote_mixer;
	PBDpopWM remote_dpop;
	PBSampleRateConverterWM remote_src;
	PBInfImpulseResponseWM remote_iir;

	u16 pad[7]; // align us, captain! (32B)
};

// TODO: All these enums have changed a lot for wii
enum {
    AUDIOFORMAT_ADPCM = 0,
    AUDIOFORMAT_PCM8  = 0x19,
	AUDIOFORMAT_PCM16 = 0xA,
};

enum {
	SRCTYPE_LINEAR  = 1,
	SRCTYPE_NEAREST = 2,
	MIXCONTROL_RAMPING = 8,
};

// Both may be used at once
enum {
	FILTER_LOWPASS = 1,
	FILTER_BIQUAD = 2,
};

#endif  // _UCODE_AX_STRUCTS_H