2019-02-16 11:23:01 -05:00
|
|
|
#include "stdafx.h"
|
|
|
|
#include "Spc.h"
|
|
|
|
#include "SNES_SPC.h"
|
|
|
|
#include "Console.h"
|
|
|
|
#include "MemoryManager.h"
|
|
|
|
#include "SoundMixer.h"
|
|
|
|
|
2019-02-24 12:54:14 -05:00
|
|
|
Spc::Spc(shared_ptr<Console> console, vector<uint8_t> &spcRomData)
|
2019-02-16 11:23:01 -05:00
|
|
|
{
|
|
|
|
_console = console;
|
2019-02-24 12:54:14 -05:00
|
|
|
|
|
|
|
memcpy(_spcBios, spcRomData.data(), 64);
|
2019-02-16 11:23:01 -05:00
|
|
|
|
2019-02-21 16:49:19 -05:00
|
|
|
_soundBuffer = new int16_t[Spc::SampleBufferSize];
|
2019-02-16 11:23:01 -05:00
|
|
|
|
|
|
|
_spc = new SNES_SPC();
|
|
|
|
_spc->init();
|
|
|
|
_spc->init_rom(_spcBios);
|
|
|
|
_spc->reset();
|
2019-02-21 16:49:19 -05:00
|
|
|
_spc->set_output(_soundBuffer, Spc::SampleBufferSize >> 1);
|
2019-02-16 11:23:01 -05:00
|
|
|
|
|
|
|
_startFrameMasterClock = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Spc::~Spc()
|
|
|
|
{
|
|
|
|
delete _spc;
|
|
|
|
}
|
|
|
|
|
2019-02-21 16:49:19 -05:00
|
|
|
int Spc::GetSpcTime()
|
2019-02-16 11:23:01 -05:00
|
|
|
{
|
|
|
|
uint64_t currentClock = _console->GetMemoryManager()->GetMasterClock();
|
|
|
|
uint64_t elapsedClocks = currentClock - _startFrameMasterClock;
|
2019-02-21 16:49:19 -05:00
|
|
|
return (int)(elapsedClocks * 1024000 / 21477000);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t Spc::Read(uint16_t addr)
|
|
|
|
{
|
|
|
|
return _spc->read_port(GetSpcTime(), addr & 0x03);
|
2019-02-16 11:23:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void Spc::Write(uint32_t addr, uint8_t value)
|
|
|
|
{
|
2019-02-21 16:49:19 -05:00
|
|
|
_spc->write_port(GetSpcTime(), addr & 0x03, value);
|
2019-02-16 11:23:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void Spc::ProcessEndFrame()
|
|
|
|
{
|
2019-02-21 16:49:19 -05:00
|
|
|
_spc->end_frame(GetSpcTime());
|
|
|
|
|
2019-02-16 11:23:01 -05:00
|
|
|
int sampleCount = _spc->sample_count();
|
2019-02-21 16:49:19 -05:00
|
|
|
_console->GetSoundMixer()->PlayAudioBuffer(_soundBuffer, sampleCount / 2);
|
|
|
|
_spc->set_output(_soundBuffer, Spc::SampleBufferSize >> 1);
|
2019-02-16 11:23:01 -05:00
|
|
|
|
2019-02-21 16:49:19 -05:00
|
|
|
uint64_t remainder = (_console->GetMemoryManager()->GetMasterClock() - _startFrameMasterClock) * 1024000 % 21477000 / 1024000;
|
|
|
|
_startFrameMasterClock = _console->GetMemoryManager()->GetMasterClock() - remainder;
|
2019-02-16 11:23:01 -05:00
|
|
|
}
|