Mesen-X/Core/BaseApuChannel.h

106 lines
1.7 KiB
C
Raw Normal View History

#pragma once
#include "stdafx.h"
#include "IMemoryHandler.h"
2015-07-17 20:58:57 -04:00
#include "EmulationSettings.h"
2015-07-19 22:24:56 -04:00
#include "Snapshotable.h"
#include "SoundMixer.h"
2015-07-14 23:35:30 -04:00
class BaseApuChannel : public IMemoryHandler, public Snapshotable
{
private:
SoundMixer *_mixer;
int8_t _lastOutput;
uint32_t _previousCycle;
2015-07-17 20:58:57 -04:00
AudioChannel _channel;
2015-07-21 23:05:27 -04:00
NesModel _nesModel;
protected:
uint16_t _timer = 0;
uint16_t _period = 0;
2015-07-17 20:58:57 -04:00
AudioChannel GetChannel()
{
return _channel;
}
public:
virtual void Clock() = 0;
virtual bool GetStatus() = 0;
BaseApuChannel(AudioChannel channel, SoundMixer *mixer)
{
2015-07-17 20:58:57 -04:00
_channel = channel;
_mixer = mixer;
2015-07-14 23:35:30 -04:00
Reset(false);
}
virtual void Reset(bool softReset)
{
2015-07-14 23:35:30 -04:00
_timer = 0;
_period = 0;
_lastOutput = 0;
_previousCycle = 0;
_mixer->Reset();
2015-07-14 23:35:30 -04:00
}
virtual void StreamState(bool saving)
{
if(!saving) {
_previousCycle = 0;
}
Stream<int8_t>(_lastOutput);
2015-07-14 23:35:30 -04:00
Stream<uint16_t>(_timer);
Stream<uint16_t>(_period);
2015-07-21 23:05:27 -04:00
Stream<NesModel>(_nesModel);
}
2015-07-21 23:05:27 -04:00
void SetNesModel(NesModel model)
{
_nesModel = model;
}
NesModel GetNesModel()
{
return _nesModel;
}
virtual void Run(uint32_t targetCycle)
{
while(_previousCycle < targetCycle) {
if(_timer == 0) {
Clock();
_timer = _period;
_previousCycle++;
} else {
uint32_t cyclesToRun = targetCycle - _previousCycle;
uint16_t skipCount = _timer > cyclesToRun ? cyclesToRun : _timer;
_timer -= skipCount;
_previousCycle += skipCount;
if(cyclesToRun == 0) {
break;
}
}
}
}
uint8_t ReadRAM(uint16_t addr)
{
return 0;
}
void AddOutput(int8_t output)
{
if(output != _lastOutput) {
_mixer->AddDelta(_channel, _previousCycle, output - _lastOutput);
_lastOutput = output;
}
}
void EndFrame()
{
_previousCycle = 0;
}
};