Replaced S5b emu with emu2149 for better accuracy
including missing features like envelope and noise
This commit is contained in:
parent
d06a647428
commit
fdee47855a
3 changed files with 400 additions and 310 deletions
|
@ -4,8 +4,89 @@
|
|||
#include "APU.h"
|
||||
#include "BaseExpansionAudio.h"
|
||||
#include "Console.h"
|
||||
#include "emu2149.h"
|
||||
|
||||
//#define SSG_USE_OLD_EMU
|
||||
#ifndef SSG_USE_OLD_EMU
|
||||
#define SSG_USE_EMU2149
|
||||
#endif
|
||||
|
||||
#ifdef SSG_USE_EMU2149
|
||||
template<AudioChannel...channels>
|
||||
class SSGAudio : public BaseExpansionAudio
|
||||
{
|
||||
private:
|
||||
std::unique_ptr<PSG, void(*)(PSG*)> _psg;
|
||||
uint8_t _currentRegister;
|
||||
int16_t _lastOutput;
|
||||
int16_t _currentOutput;
|
||||
double _clock;
|
||||
bool _processTick;
|
||||
static constexpr uint8_t cycleCount = 1;
|
||||
|
||||
void UpdateOutputLevel()
|
||||
{
|
||||
(_console->GetApu()->AddExpansionAudioDelta(channels, _currentOutput - _lastOutput), ...);
|
||||
_lastOutput = _currentOutput;
|
||||
}
|
||||
|
||||
protected:
|
||||
void StreamState(bool saving) override
|
||||
{
|
||||
BaseExpansionAudio::StreamState(saving);
|
||||
ValueInfo<PSG> psg{ _psg.get() };
|
||||
Stream( _currentRegister, _lastOutput, _clock, psg);
|
||||
}
|
||||
|
||||
void ClockAudio() override
|
||||
{
|
||||
_clock += GetSSGClockFrequency() / (double)_console->GetCpu()->GetClockRate(_console->GetModel());
|
||||
|
||||
while (_clock >= cycleCount)
|
||||
{
|
||||
_currentOutput = 0;
|
||||
|
||||
for (uint8_t cycle = 0; cycle < cycleCount; cycle++)
|
||||
{
|
||||
_clock--;
|
||||
_currentOutput = PSG_calc(_psg.get());
|
||||
}
|
||||
|
||||
_currentOutput /= 26;
|
||||
|
||||
UpdateOutputLevel();
|
||||
}
|
||||
}
|
||||
|
||||
virtual uint32_t GetSSGClockFrequency()
|
||||
{
|
||||
return _console->GetCpu()->GetClockRate(_console->GetModel()) / 2;
|
||||
}
|
||||
|
||||
public:
|
||||
SSGAudio(shared_ptr<Console> console) : BaseExpansionAudio(console), _psg{ PSG_new(1 , 1), &PSG_delete }
|
||||
{
|
||||
_currentRegister = 0;
|
||||
_lastOutput = 0;
|
||||
_clock = 0;
|
||||
PSG_reset(_psg.get());
|
||||
}
|
||||
|
||||
void WriteRegister(uint16_t addr, uint8_t value)
|
||||
{
|
||||
switch (addr) {
|
||||
case 0xC000:
|
||||
_currentRegister = value;
|
||||
break;
|
||||
|
||||
case 0xE000:
|
||||
if (_currentRegister <= 0xF)
|
||||
PSG_writeReg(_psg.get(), _currentRegister, value);
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
#else
|
||||
template<AudioChannel...channels>
|
||||
class SSGAudio : public BaseExpansionAudio
|
||||
{
|
||||
|
@ -143,3 +224,4 @@ public:
|
|||
}
|
||||
}
|
||||
};
|
||||
#endif
|
103
Core/emu2149.cpp
103
Core/emu2149.cpp
|
@ -32,10 +32,10 @@ static uint32_t voltbl[2][32] = {
|
|||
0x0B, 0x0D, 0x0F, 0x12,
|
||||
0x16, 0x1A, 0x1F, 0x25, 0x2D, 0x35, 0x3F, 0x4C, 0x5A, 0x6A, 0x7F, 0x97,
|
||||
0xB4, 0xD6, 0xFF, 0xFF},
|
||||
{0x00, 0x00, 0x01, 0x01, 0x02, 0x02, 0x03, 0x03, 0x05, 0x05, 0x07, 0x07,
|
||||
0x0B, 0x0B, 0x0F, 0x0F,
|
||||
0x16, 0x16, 0x1F, 0x1F, 0x2D, 0x2D, 0x3F, 0x3F, 0x5A, 0x5A, 0x7F, 0x7F,
|
||||
0xB4, 0xB4, 0xFF, 0xFF}
|
||||
{0x00, 0x00, 0x03, 0x03, 0x04, 0x04, 0x06, 0x06, 0x09, 0x09, 0x0D, 0x0D,
|
||||
0x12, 0x12, 0x1D, 0x1D,
|
||||
0x22, 0x22, 0x37, 0x37, 0x4D, 0x4D, 0x62, 0x62, 0x82, 0x82, 0xA6, 0xA6,
|
||||
0xD0, 0xD0, 0xFF, 0xFF}
|
||||
};
|
||||
|
||||
static uint8_t regmsk[16] = {
|
||||
|
@ -46,55 +46,62 @@ static uint8_t regmsk[16] = {
|
|||
#define GETA_BITS 24
|
||||
|
||||
static void
|
||||
internal_refresh (PSG * psg)
|
||||
internal_refresh(PSG* psg)
|
||||
{
|
||||
if (psg->quality)
|
||||
{
|
||||
psg->base_incr = 1 << GETA_BITS;
|
||||
psg->realstep = (uint32_t) ((1 << 31) / psg->rate);
|
||||
psg->psgstep = (uint32_t) ((1 << 31) / (psg->clk / 16));
|
||||
psg->realstep = (uint32_t)((1 << 31) / psg->rate);
|
||||
psg->psgstep = (uint32_t)((1 << 31) / (psg->clk / 8));
|
||||
psg->psgtime = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
psg->base_incr =
|
||||
(uint32_t) ((double) psg->clk * (1 << GETA_BITS) / (16 * psg->rate));
|
||||
(uint32_t)((double)psg->clk * (1 << GETA_BITS) / (8 * psg->rate));
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
PSG_set_rate (PSG * psg, uint32_t r)
|
||||
PSG_set_clock(PSG* psg, uint32_t c)
|
||||
{
|
||||
psg->rate = r ? r : 44100;
|
||||
internal_refresh (psg);
|
||||
psg->clk = c;
|
||||
internal_refresh(psg);
|
||||
}
|
||||
|
||||
void
|
||||
PSG_set_quality (PSG * psg, uint32_t q)
|
||||
PSG_set_rate(PSG* psg, uint32_t r)
|
||||
{
|
||||
psg->quality = q;
|
||||
internal_refresh (psg);
|
||||
psg->rate = r ? r : 44100;
|
||||
internal_refresh(psg);
|
||||
}
|
||||
|
||||
PSG *
|
||||
PSG_new (uint32_t c, uint32_t r)
|
||||
void
|
||||
PSG_set_quality(PSG* psg, uint32_t q)
|
||||
{
|
||||
PSG *psg;
|
||||
psg->quality = q;
|
||||
internal_refresh(psg);
|
||||
}
|
||||
|
||||
psg = (PSG *) malloc (sizeof (PSG));
|
||||
PSG*
|
||||
PSG_new(uint32_t c, uint32_t r)
|
||||
{
|
||||
PSG* psg;
|
||||
|
||||
psg = (PSG*)malloc(sizeof(PSG));
|
||||
if (psg == NULL)
|
||||
return NULL;
|
||||
|
||||
PSG_setVolumeMode (psg, EMU2149_VOL_DEFAULT);
|
||||
PSG_setVolumeMode(psg, EMU2149_VOL_DEFAULT);
|
||||
psg->clk = c;
|
||||
psg->rate = r ? r : 44100;
|
||||
PSG_set_quality (psg, 0);
|
||||
PSG_set_quality(psg, 0);
|
||||
|
||||
return psg;
|
||||
}
|
||||
|
||||
void
|
||||
PSG_setVolumeMode (PSG * psg, int type)
|
||||
PSG_setVolumeMode(PSG* psg, int type)
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
|
@ -111,10 +118,10 @@ PSG_setVolumeMode (PSG * psg, int type)
|
|||
}
|
||||
|
||||
uint32_t
|
||||
PSG_setMask (PSG *psg, uint32_t mask)
|
||||
PSG_setMask(PSG* psg, uint32_t mask)
|
||||
{
|
||||
uint32_t ret = 0;
|
||||
if(psg)
|
||||
if (psg)
|
||||
{
|
||||
ret = psg->mask;
|
||||
psg->mask = mask;
|
||||
|
@ -123,10 +130,10 @@ PSG_setMask (PSG *psg, uint32_t mask)
|
|||
}
|
||||
|
||||
uint32_t
|
||||
PSG_toggleMask (PSG *psg, uint32_t mask)
|
||||
PSG_toggleMask(PSG* psg, uint32_t mask)
|
||||
{
|
||||
uint32_t ret = 0;
|
||||
if(psg)
|
||||
if (psg)
|
||||
{
|
||||
ret = psg->mask;
|
||||
psg->mask ^= mask;
|
||||
|
@ -135,7 +142,7 @@ PSG_toggleMask (PSG *psg, uint32_t mask)
|
|||
}
|
||||
|
||||
void
|
||||
PSG_reset (PSG * psg)
|
||||
PSG_reset(PSG* psg)
|
||||
{
|
||||
int i;
|
||||
|
||||
|
@ -171,35 +178,35 @@ PSG_reset (PSG * psg)
|
|||
}
|
||||
|
||||
void
|
||||
PSG_delete (PSG * psg)
|
||||
PSG_delete(PSG* psg)
|
||||
{
|
||||
free (psg);
|
||||
free(psg);
|
||||
}
|
||||
|
||||
uint8_t
|
||||
PSG_readIO (PSG * psg)
|
||||
PSG_readIO(PSG* psg)
|
||||
{
|
||||
return (uint8_t) (psg->reg[psg->adr]);
|
||||
return (uint8_t)(psg->reg[psg->adr]);
|
||||
}
|
||||
|
||||
uint8_t
|
||||
PSG_readReg (PSG * psg, uint32_t reg)
|
||||
PSG_readReg(PSG* psg, uint32_t reg)
|
||||
{
|
||||
return (uint8_t) (psg->reg[reg & 0x1f]);
|
||||
return (uint8_t)(psg->reg[reg & 0x1f]);
|
||||
|
||||
}
|
||||
|
||||
void
|
||||
PSG_writeIO (PSG * psg, uint32_t adr, uint32_t val)
|
||||
PSG_writeIO(PSG* psg, uint32_t adr, uint32_t val)
|
||||
{
|
||||
if (adr & 1)
|
||||
PSG_writeReg (psg, psg->adr, val);
|
||||
PSG_writeReg(psg, psg->adr, val);
|
||||
else
|
||||
psg->adr = val & 0x1f;
|
||||
}
|
||||
|
||||
static inline void
|
||||
update_output (PSG * psg)
|
||||
update_output(PSG* psg)
|
||||
{
|
||||
|
||||
int i, noise;
|
||||
|
@ -211,12 +218,12 @@ update_output (PSG * psg)
|
|||
|
||||
/* Envelope */
|
||||
psg->env_count += incr;
|
||||
while (psg->env_count>=0x10000 && psg->env_freq!=0)
|
||||
while (psg->env_count >= 0x10000 && psg->env_freq != 0)
|
||||
{
|
||||
if (!psg->env_pause)
|
||||
{
|
||||
if(psg->env_face)
|
||||
psg->env_ptr = (psg->env_ptr + 1) & 0x3f ;
|
||||
if (psg->env_face)
|
||||
psg->env_ptr = (psg->env_ptr + 1) & 0x3f;
|
||||
else
|
||||
psg->env_ptr = (psg->env_ptr + 0x3f) & 0x3f;
|
||||
}
|
||||
|
@ -225,9 +232,9 @@ update_output (PSG * psg)
|
|||
{
|
||||
if (psg->env_continue)
|
||||
{
|
||||
if (psg->env_alternate^psg->env_hold) psg->env_face ^= 1;
|
||||
if (psg->env_alternate ^ psg->env_hold) psg->env_face ^= 1;
|
||||
if (psg->env_hold) psg->env_pause = 1;
|
||||
psg->env_ptr = psg->env_face?0:0x1f;
|
||||
psg->env_ptr = psg->env_face ? 0 : 0x1f;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -246,7 +253,7 @@ update_output (PSG * psg)
|
|||
if (psg->noise_seed & 1)
|
||||
psg->noise_seed ^= 0x24000;
|
||||
psg->noise_seed >>= 1;
|
||||
psg->noise_count -= psg->noise_freq?psg->noise_freq:(1<<1);
|
||||
psg->noise_count -= psg->noise_freq ? psg->noise_freq : (1 << 1);
|
||||
}
|
||||
noise = psg->noise_seed & 1;
|
||||
|
||||
|
@ -267,10 +274,10 @@ update_output (PSG * psg)
|
|||
}
|
||||
}
|
||||
|
||||
if (psg->mask&PSG_MASK_CH(i))
|
||||
if (psg->mask & PSG_MASK_CH(i))
|
||||
continue;
|
||||
|
||||
if ((psg->tmask[i]||psg->edge[i]) && (psg->nmask[i]||noise))
|
||||
if ((psg->tmask[i] || psg->edge[i]) && (psg->nmask[i] || noise))
|
||||
{
|
||||
if (!(psg->volume[i] & 32))
|
||||
psg->ch_out[i] += (psg->voltbl[psg->volume[i] & 31] << 4);
|
||||
|
@ -285,12 +292,12 @@ update_output (PSG * psg)
|
|||
}
|
||||
|
||||
static inline int16_t
|
||||
mix_output(PSG *psg) {
|
||||
mix_output(PSG* psg) {
|
||||
return (int16_t)(psg->out = psg->ch_out[0] + psg->ch_out[1] + psg->ch_out[2]);
|
||||
}
|
||||
|
||||
int16_t
|
||||
PSG_calc (PSG * psg)
|
||||
PSG_calc(PSG* psg)
|
||||
{
|
||||
if (!psg->quality) {
|
||||
update_output(psg);
|
||||
|
@ -309,7 +316,7 @@ PSG_calc (PSG * psg)
|
|||
}
|
||||
|
||||
void
|
||||
PSG_writeReg (PSG * psg, uint32_t reg, uint32_t val)
|
||||
PSG_writeReg(PSG* psg, uint32_t reg, uint32_t val)
|
||||
{
|
||||
int c;
|
||||
|
||||
|
@ -317,7 +324,7 @@ PSG_writeReg (PSG * psg, uint32_t reg, uint32_t val)
|
|||
|
||||
val &= regmsk[reg];
|
||||
|
||||
psg->reg[reg] = (uint8_t) (val & 0xff);
|
||||
psg->reg[reg] = (uint8_t)(val & 0xff);
|
||||
switch (reg)
|
||||
{
|
||||
case 0:
|
||||
|
@ -362,7 +369,7 @@ PSG_writeReg (PSG * psg, uint32_t reg, uint32_t val)
|
|||
psg->env_face = psg->env_attack;
|
||||
psg->env_pause = 0;
|
||||
psg->env_count = 0x10000 - psg->env_freq;
|
||||
psg->env_ptr = psg->env_face?0:0x1f;
|
||||
psg->env_ptr = psg->env_face ? 0 : 0x1f;
|
||||
break;
|
||||
|
||||
case 14:
|
||||
|
|
|
@ -19,7 +19,7 @@ extern "C"
|
|||
{
|
||||
|
||||
/* Volume Table */
|
||||
uint32_t *voltbl;
|
||||
uint32_t* voltbl;
|
||||
|
||||
uint8_t reg[0x20];
|
||||
int32_t out;
|
||||
|
@ -67,19 +67,20 @@ extern "C"
|
|||
|
||||
} PSG;
|
||||
|
||||
void PSG_set_quality (PSG * psg, uint32_t q);
|
||||
void PSG_set_rate (PSG * psg, uint32_t r);
|
||||
PSG *PSG_new (uint32_t clk, uint32_t rate);
|
||||
void PSG_reset (PSG *);
|
||||
void PSG_delete (PSG *);
|
||||
void PSG_writeReg (PSG *, uint32_t reg, uint32_t val);
|
||||
void PSG_writeIO (PSG * psg, uint32_t adr, uint32_t val);
|
||||
uint8_t PSG_readReg (PSG * psg, uint32_t reg);
|
||||
uint8_t PSG_readIO (PSG * psg);
|
||||
int16_t PSG_calc (PSG *);
|
||||
void PSG_setVolumeMode (PSG * psg, int type);
|
||||
uint32_t PSG_setMask (PSG *, uint32_t mask);
|
||||
uint32_t PSG_toggleMask (PSG *, uint32_t mask);
|
||||
void PSG_set_quality(PSG* psg, uint32_t q);
|
||||
void PSG_set_clock(PSG* psg, uint32_t c);
|
||||
void PSG_set_rate(PSG* psg, uint32_t r);
|
||||
PSG* PSG_new(uint32_t clk, uint32_t rate);
|
||||
void PSG_reset(PSG*);
|
||||
void PSG_delete(PSG*);
|
||||
void PSG_writeReg(PSG*, uint32_t reg, uint32_t val);
|
||||
void PSG_writeIO(PSG* psg, uint32_t adr, uint32_t val);
|
||||
uint8_t PSG_readReg(PSG* psg, uint32_t reg);
|
||||
uint8_t PSG_readIO(PSG* psg);
|
||||
int16_t PSG_calc(PSG*);
|
||||
void PSG_setVolumeMode(PSG* psg, int type);
|
||||
uint32_t PSG_setMask(PSG*, uint32_t mask);
|
||||
uint32_t PSG_toggleMask(PSG*, uint32_t mask);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue