]> Shamusworld >> Repos - thunder/blob - src/psg.cpp
ec849c5d8d1cbed2c1f40587e71c0edcef333817
[thunder] / src / psg.cpp
1 //
2 // PSG handler
3 //
4 // This emulates the Rolling Thunder PSG (Namco CUS30)
5 //
6 // by James Hammons
7 // (C) 2014 Underground Software
8 //
9 // JLH = James Hammons <jlhamm@acm.org>
10 //
11 // Who  When        What
12 // ---  ----------  -----------------------------------------------------------
13 // JLH  04/21/2014  Created this file.
14 //
15 //
16 // Notes:
17 // ------
18 // The emulator creates signed 16-bit samples.  Make sure there's enough room
19 // in your buffer for them!
20 //
21
22 #include "psg.h"
23 #include <stdio.h>
24 #include <string.h>
25
26 struct Voice
27 {
28         uint8_t leftVolume;
29         uint8_t rightVolume;
30         uint8_t waveform;
31         uint32_t frequency;
32         uint32_t counter;
33         bool noise;
34         uint32_t noiseSeed;
35 };
36
37 static Voice voice[8];
38 static uint8_t memory[0x200];
39 static uint32_t sampleRate = 44100;
40 static uint32_t divisor;
41
42 void InitPSG(uint32_t s/*=44100*/)
43 {
44         sampleRate = s;
45         divisor = (uint32_t)((float)s / 0.735); // Voodoo constant
46
47         // Noise generation will fail if the noise seeds aren't primed...
48         for(int i=0; i<8; i++)
49                 voice[i].noiseSeed = 1;
50 }
51
52 //
53 // Note that it doesn't wipe out the buffer passed in; if you want it wiped,
54 // you have to wipe it yourself.  :-)
55 //
56 void UpdatePSG(uint8_t * buffer, int count)
57 {
58 /*
59 if F == 44100, then counter++ for each sample.
60 if F == 88200, then counter += 2 for each sample.
61 if F == 22050, then counter += 0.5 for each sample.
62 */
63         // Recast buffer as int16, we're doing 16-bit sound here
64         int16_t * p = (int16_t *)buffer;
65
66         for(int i=0; i<8; i++)
67         {
68                 if (!voice[i].noise)
69                 {
70                         if ((voice[i].leftVolume == 0) || (voice[i].frequency == 0))
71                                 continue;
72
73                         for(int j=0; j<count; j++)
74                         {
75 //                              uint8_t pos = (voice[i].counter / (sampleRate * 2)) & 0x1F;
76 // Where does 60000 come from? IDK. This would probably change depending on
77 // the playback rate (currently, 44100). N.B.: 58800 is a hair too high in pitch!
78 // 44100 / 60000 = 0.735
79 // 192000 / 60000 = 3.2
80 //                              uint8_t pos = (voice[i].counter / (60000)) & 0x1F;
81 //                              uint8_t pos = (voice[i].counter / (65536)) & 0x1F;
82                                 uint8_t pos = (voice[i].counter / divisor) & 0x1F;
83                                 uint8_t sample = ((pos & 0x01) == 0
84                                         ? memory[(voice[i].waveform * 16) + (pos / 2)] >> 4
85                                         : memory[(voice[i].waveform * 16) + (pos / 2)]) & 0x0F;
86 //                              p[j] += (((memory[(voice[i].waveform * 32) + pos] & 0x0F) - 8)
87 //                                      * voice[i].leftVolume) << 5;
88                                 p[j] += ((sample - 8) * voice[i].leftVolume) << 5;
89                                 voice[i].counter += voice[i].frequency;
90                         }
91                 }
92                 else
93                 {
94                         if ((voice[i].leftVolume == 0) || ((voice[i].frequency & 0xFF) == 0))
95                                 continue;
96
97                         // The hold stuff here is VOODOO
98                         // Need to figure out what's really going on here, what the clock
99                         // rate of this chip is. Also, some freqs can be > 255 according to
100                         // Rolling Thunder...
101                         int16_t sample = (7 * voice[i].leftVolume) << 4;
102                         int16_t noiseSign = 1;
103                         int16_t hold = 1 << 1;
104
105                         for(int j=0; j<count; j++)
106                         {
107                                 p[j] += sample * noiseSign;
108
109                                 if (hold)
110                                 {
111                                         hold--;
112                                         continue;
113                                 }
114
115                                 hold = 1 << 1;
116
117                                 voice[i].counter += (voice[i].frequency & 0xFF) << 4;
118                                 int c = voice[i].counter >> 12;
119                                 voice[i].counter &= 0xFFF;
120
121                                 for(; c>0; c--)
122                                 {
123                                         if ((voice[i].noiseSeed + 1) & 0x02)
124                                                 noiseSign *= -1;
125
126                                         if (voice[i].noiseSeed & 0x01)
127                                                 voice[i].noiseSeed ^= 0x28000;
128
129                                         voice[i].noiseSeed >>= 1;
130                                 }
131                         }
132                 }
133         }
134 }
135
136 void WritePSG(uint16_t address, uint8_t data)
137 {
138         if ((address >= 0x100) && (address <= 0x13F))
139         {
140                 uint8_t channel = (address - 0x100) / 8;
141                 uint8_t knob = (address - 0x100) - (channel * 8);
142
143                 if (channel < 8)
144                 {
145                         switch (knob)
146                         {
147                         case 0:
148                                 voice[channel].leftVolume = data & 0x0F;
149                                 break;
150                         case 1:
151                                 voice[channel].waveform = data >> 4;
152                                 voice[channel].frequency = ((data & 0x0F) << 16)
153                                         | (voice[channel].frequency & 0x0FFFF);
154 #if 0
155 printf("PSG: Setting waveform on channel %i to %i...\n", channel, voice[channel].waveform);
156 #endif
157                                 break;
158                         case 2:
159                                 voice[channel].frequency = (data << 8)
160                                         | (voice[channel].frequency & 0xF00FF);
161                                 break;
162                         case 3:
163                                 voice[channel].frequency = data
164                                         | (voice[channel].frequency & 0xFFF00);
165                                 break;
166                         case 4:
167                                 voice[channel].rightVolume = data & 0x0F;
168                                 // Noise switch is channel # + 1 (wraps to zero)
169                                 voice[(channel + 1) & 0x07].noise = (data & 0x80 ? true : false);
170 #if 0
171 if (voice[(channel + 1) & 0x07].noise)
172 {
173         uint8_t ch = (channel + 1) & 0x07;
174         printf("PSG: Setting noise on channel %i, vol=%i, freq=%i...\n", ch, voice[ch].leftVolume, voice[ch].frequency);
175 }
176 #endif
177 #if 0
178 if (data & 0x0F)
179 {
180         printf("PSG: Setting right volume on channel %i: vol=%i...\n", channel, voice[channel].rightVolume);
181 }
182 #endif
183                                 break;
184                         }
185                 }
186
187 //              return;
188         }
189 #if 0
190         else
191                 printf("PSG: Write to $%03X of $%02X...\n", address, data);
192 #endif
193
194 //if (address < 0x100)
195 //      printf("PSG: Waveform byte[$%02X] = $%02X...\n", address, data);
196
197         memory[address & 0x01FF] = data;
198 }
199
200 uint8_t ReadPSG(uint16_t address)
201 {
202         return memory[address & 0x01FF];
203 }