/*---------------------------------------------------------------------------- ----------*\ Description: This core emulates the SN76489 Programmable Sound Generator (PSG), and is designed to produce a sample-accurate output. Each channel is generated and mixed at the correct internal sample rate, and all known aspects and behaviour of this device are faithfully emulated. Things to do: -Get a final answer on the conversion from attenuation to linear power, and document it within your core. -Make it possible to define the clock and noise register settings externally Output filtering: -Note that there is real "decay-like" behaviour, which comes from the audio output circuitry in the Mega Drive. The same circuit affects the output of the YM2612 in the same way. If we want to emulate this behaviour, we need to emulate it using a form of audio filter which can be applied to any sound core, just like we need to emulate analog video distortion using output filters. These filters need to be separate from the device cores, and be modular and chainable. Note that we should also use output "filters" to balance output levels between sound devices. -Measure the final audio output levels from the embedded SN76489 in the Mega Drive, both directly from the chip and after the mixing circuit. According to SN76489.txt, several of the minimum attenuation levels may in fact be clipped, which will greatly affect the overall volume of the PSG, and the relative volume levels of the different register settings on the system. Debug output modifications: -Add the calculated frequency, and attenuation in db, for each channel, on the register debug window. -Display the noise register using its actual separate register values, named correctly, on the register debug window. References: -SN76489 Notes (SN76489.txt), Maxim, 2005 -Genesis Sound Software Manual, Sega Enterprises, 1992, scans by antime -SN76489 Datasheet, Texas Instruments -SN76494 Datasheet, Texas Instruments -SN76489 sound chip details, John Kortink, http://web.inter.nl.net/users/J.Kortink/home/articles/sn76489/index.htm \*--------------------------------------------------------------------------------------------------------------------*/ #include "ISN76489.h" #ifndef __SN76489_H__ #define __SN76489_H__ #include "DeviceInterface/DeviceInterface.pkg" #include "TimedBuffers/TimedBuffers.pkg" #include #include #include #include #include "Device/Device.pkg" #include "AudioStream/AudioStream.pkg" #include "Stream/Stream.pkg" #include #include class SN76489 :public Device, public GenericAccessBase { public: // Constructors SN76489(const std::wstring& implementationName, const std::wstring& instanceName, unsigned int moduleID); // Interface version functions virtual unsigned int GetISN76489Version() const; // Initialization functions virtual bool BuildDevice(); virtual bool ValidateDevice(); virtual void Initialize(); // Clock source functions virtual unsigned int GetClockSourceID(const Marshal::In& clockSourceName) const; virtual Marshal::Ret GetClockSourceName(unsigned int clockSourceID) const; virtual void SetClockSourceRate(unsigned int clockInput, double clockRate, IDeviceContext* caller, double accessTime, unsigned int accessContext); virtual void TransparentSetClockSourceRate(unsigned int clockInput, double clockRate); // Execute functions virtual void BeginExecution(); virtual void SuspendExecution(); virtual bool SendNotifyUpcomingTimeslice() const; virtual void NotifyUpcomingTimeslice(double nanoseconds); virtual UpdateMethod GetUpdateMethod() const; virtual void ExecuteTimeslice(double nanoseconds); virtual void ExecuteRollback(); virtual void ExecuteCommit(); // Memory interface functions virtual IBusInterface::AccessResult WriteInterface(unsigned int interfaceNumber, unsigned int location, const Data& data, IDeviceContext* caller, double accessTime, unsigned int accessContext); // Savestate functions virtual void LoadState(IHierarchicalStorageNode& node); virtual void SaveState(IHierarchicalStorageNode& node) const; virtual void LoadDebuggerState(IHierarchicalStorageNode& node); virtual void SaveDebuggerState(IHierarchicalStorageNode& node) const; // Data read/write functions using IGenericAccess::ReadGenericData; using IGenericAccess::WriteGenericData; virtual bool ReadGenericData(unsigned int dataID, const DataContext* dataContext, IGenericAccessDataValue& dataValue) const; virtual bool WriteGenericData(unsigned int dataID, const DataContext* dataContext, IGenericAccessDataValue& dataValue); // Data locking functions virtual bool GetGenericDataLocked(unsigned int dataID, const DataContext* dataContext) const; virtual bool SetGenericDataLocked(unsigned int dataID, const DataContext* dataContext, bool state); private: // Enumerations enum class ClockID; // Structures struct ChannelRenderData { unsigned int initialToneCycles; unsigned int remainingToneCycles; bool polarityNegative; }; // Typedefs typedef RandomTimeAccessBuffer::AccessTarget AccessTarget; // Constants static const unsigned int maxPendingRenderOperationCount = 4; private: // Render functions void RenderThread(); void UpdateChannel(unsigned int channelNo, unsigned int outputSampleCount, std::vector& outputBuffer); // Raw register functions inline Data GetVolumeRegister(unsigned int channelNo, const AccessTarget& accessTarget) const; inline void SetVolumeRegister(unsigned int channelNo, const Data& adata, const AccessTarget& accessTarget); inline Data GetToneRegister(unsigned int channelNo, const AccessTarget& accessTarget) const; inline void SetToneRegister(unsigned int channelNo, const Data& adata, const AccessTarget& accessTarget); // Audio logging functions void SetAudioLoggingEnabled(bool state); void SetChannelAudioLoggingEnabled(unsigned int channelNo, bool state); private: // Registers mutable std::mutex _accessMutex; double _lastAccessTime; RandomTimeAccessBuffer _reg; unsigned int _latchedChannel; unsigned int _blatchedChannel; bool _latchedVolume; bool _blatchedVolume; // Render thread properties mutable std::mutex _renderThreadMutex; mutable std::mutex _timesliceMutex; std::condition_variable _renderThreadUpdate; std::condition_variable _renderThreadStopped; bool _renderThreadActive; bool _renderThreadLagging; std::condition_variable _renderThreadLaggingStateChange; unsigned int _pendingRenderOperationCount; std::list::Timeslice> _regTimesliceList; std::list::Timeslice> _regTimesliceListUncommitted; double _remainingRenderTime; unsigned int _outputSampleRate; AudioStream _outputStream; std::vector _outputBuffer; // Render data ChannelRenderData _channelRenderData[ChannelCount]; unsigned int _noiseShiftRegister; bool _noiseOutputMasked; // Device Properties double _externalClockRate; double _externalClockDivider; unsigned int _shiftRegisterBitCount; unsigned int _shiftRegisterDefaultValue; unsigned int _noiseWhiteTappedBitMask; unsigned int _noisePeriodicTappedBitMask; std::list _genericDataToUpdateOnShiftRegisterBitCountChange; // Register locking bool _channelVolumeRegisterLocked[ChannelCount]; bool _channelDataRegisterLocked[ChannelCount]; bool _noiseChannelTypeLocked; bool _noiseChannelPeriodLocked; // Wave logging mutable std::mutex _waveLoggingMutex; bool _wavLoggingEnabled; bool _wavLoggingChannelEnabled[ChannelCount]; std::wstring _wavLoggingPath; std::wstring _wavLoggingChannelPath[ChannelCount]; Stream::WAVFile _wavLog; Stream::WAVFile _wavLogChannel[ChannelCount]; }; #include "SN76489.inl" #endif