1 #ifndef _PCEFast_PSG_H
2 #define _PCEFast_PSG_H
3 
4 #include <mednafen/sound/Blip_Buffer.h>
5 
6 namespace PCE_Fast
7 {
8 
9 class PCEFast_PSG;
10 
11 struct psg_channel
12 {
13         uint8 waveform[32];     /* Waveform data */
14         uint8 waveform_index;   /* Waveform data index */
15         uint8 dda;
16         uint8 control;          /* Channel enable, DDA, volume */
17         uint8 noisectrl;        /* Noise enable/ctrl (channels 4,5 only) */
18 
19         int32 vl[2];    //vll, vlr;
20 
21         int32 counter;
22 
23         void (PCEFast_PSG::*UpdateOutput)(const int32 timestamp, psg_channel *ch);
24 
25         uint32 freq_cache;
26         uint32 noise_freq_cache;        // Channel 4,5 only
27         int32 noisecount;
28         uint32 lfsr;
29 
30         int32 samp_accum;         // The result of adding up all the samples in the waveform buffer(part of an optimization for high-frequency playback).
31         int32 blip_prev_samp[2];
32         int32 lastts;
33 
34         uint16 frequency;       /* Channel frequency */
35         uint8 balance;          /* Channel balance */
36 };
37 
38 class PCEFast_PSG
39 {
40         public:
41 
42         PCEFast_PSG(Blip_Buffer* bbs) MDFN_COLD;
43         ~PCEFast_PSG() MDFN_COLD;
44 
45 	void StateAction(StateMem *sm, int load, int data_only) MDFN_COLD;
46 
47         void Power(const int32 timestamp) MDFN_COLD;
48         void Write(int32 timestamp, uint8 A, uint8 V);
49 
50 	void SetVolume(double new_volume) MDFN_COLD;
51 
52 	void EndFrame(int32 timestamp);
53 
54         private:
55 
56 	void Update(int32 timestamp);
57 
58 	void UpdateSubLFO(int32 timestamp);
59 	void UpdateSubNonLFO(int32 timestamp);
60 
61 	void RecalcUOFunc(int chnum);
62 	void UpdateOutput_Off(const int32 timestamp, psg_channel *ch);
63 	void UpdateOutput_Accum(const int32 timestamp, psg_channel *ch);
64         void UpdateOutput_Norm(const int32 timestamp, psg_channel *ch);
65         void UpdateOutput_Noise(const int32 timestamp, psg_channel *ch);
66 
67 	int32 GetVL(const int chnum, const int lr);
68 
69 	void RecalcFreqCache(int chnum);
70 	void RecalcNoiseFreqCache(int chnum);
71 	template<bool LFO_On>
72 	void RunChannel(int chc, int32 timestamp);
73 	double OutputVolume;
74 
75         uint8 select;               /* Selected channel (0-5) */
76         uint8 globalbalance;        /* Global sound balance */
77         uint8 lfofreq;              /* LFO frequency */
78         uint8 lfoctrl;              /* LFO control */
79 
80         int32 vol_update_counter;
81         int32 vol_update_which;
82 	int32 vol_update_vllatch;
83 	bool vol_pending;
84 
85         psg_channel channel[6];
86 
87         int32 lastts;
88 
89 	Blip_Buffer* const sbuf;
90 	Blip_Synth<blip_good_quality, 8192> Synth;
91 
92         int32 dbtable_volonly[32];
93 
94 	int32 dbtable[32][32];
95 };
96 
97 }
98 
99 #endif
100