melonDS/src/SPU.h

111 lines
2.3 KiB
C
Raw Normal View History

/*
Copyright 2016-2017 StapleButter
This file is part of melonDS.
melonDS 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, either version 3 of the License, or (at your option)
any later version.
melonDS 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 for more details.
You should have received a copy of the GNU General Public License along
with melonDS. If not, see http://www.gnu.org/licenses/.
*/
#ifndef SPU_H
#define SPU_H
namespace SPU
{
bool Init();
void DeInit();
void Reset();
void SetBias(u16 bias);
2017-04-06 17:44:34 +00:00
void Mix(u32 samples);
void ReadOutput(s16* data, int samples);
u8 Read8(u32 addr);
u16 Read16(u32 addr);
u32 Read32(u32 addr);
void Write8(u32 addr, u8 val);
void Write16(u32 addr, u16 val);
void Write32(u32 addr, u32 val);
class Channel
{
public:
Channel(u32 num);
~Channel();
void Reset();
u32 Num;
2017-04-06 17:44:34 +00:00
u32 Cnt;
u32 SrcAddr;
u16 TimerReload;
u32 LoopPos;
u32 Length;
u8 Volume;
u8 VolumeShift;
u8 Pan;
u32 Timer;
s32 Pos;
s16 CurSample;
u16 NoiseVal;
s32 ADPCMVal;
s32 ADPCMIndex;
s32 ADPCMValLoop;
s32 ADPCMIndexLoop;
u8 ADPCMCurByte;
2017-04-06 17:44:34 +00:00
void SetCnt(u32 val)
{
u32 oldcnt = Cnt;
Cnt = val & 0xFF7F837F;
Volume = Cnt & 0x7F;
if (Volume == 127) Volume++;
const u8 volshift[4] = {4, 3, 2, 0};
VolumeShift = volshift[(Cnt >> 8) & 0x3];
Pan = (Cnt >> 16) & 0x7F;
if (Pan == 127) Pan++;
if ((val & (1<<31)) && !(oldcnt & (1<<31)))
{
Start();
}
2017-04-06 17:44:34 +00:00
}
void SetSrcAddr(u32 val) { SrcAddr = val & 0x07FFFFFC; }
void SetTimerReload(u32 val) { TimerReload = val & 0xFFFF; }
2017-04-06 17:44:34 +00:00
void SetLoopPos(u32 val) { LoopPos = (val & 0xFFFF) << 2; }
void SetLength(u32 val) { Length = (val & 0x001FFFFF) << 2; }
void Start();
void NextSample_PCM8();
void NextSample_PCM16();
void NextSample_ADPCM();
void NextSample_PSG();
void NextSample_Noise();
template<u32 type> void Run(s32* buf, u32 samples);
2017-04-06 17:44:34 +00:00
};
}
#endif // SPU_H