4 // This emulates the Rolling Thunder PSG (Namco CUS30)
7 // (C) 2014 Underground Software
9 // JLH = James Hammons <jlhamm@acm.org>
12 // --- ---------- -----------------------------------------------------------
13 // JLH 04/21/2014 Created this file.
18 // The emulator creates signed 16-bit samples. Make sure there's enough room
19 // in your buffer for them!
37 static Voice voice[8];
38 static uint8_t memory[0x200];
39 static uint32_t sampleRate = 44100;
40 static uint32_t divisor;
42 void InitPSG(uint32_t s/*=44100*/)
45 divisor = (uint32_t)((float)s / 0.735); // Voodoo constant
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;
53 // Note that it doesn't wipe out the buffer passed in; if you want it wiped,
54 // you have to wipe it yourself. :-)
56 void UpdatePSG(uint8_t * buffer, int count)
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.
63 // Recast buffer as int16, we're doing 16-bit sound here
64 int16_t * p = (int16_t *)buffer;
66 for(int i=0; i<8; i++)
70 if ((voice[i].leftVolume == 0) || (voice[i].frequency == 0))
73 for(int j=0; j<count; j++)
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;
94 if ((voice[i].leftVolume == 0) || ((voice[i].frequency & 0xFF) == 0))
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;
105 for(int j=0; j<count; j++)
107 p[j] += sample * noiseSign;
117 voice[i].counter += (voice[i].frequency & 0xFF) << 4;
118 int c = voice[i].counter >> 12;
119 voice[i].counter &= 0xFFF;
123 if ((voice[i].noiseSeed + 1) & 0x02)
126 if (voice[i].noiseSeed & 0x01)
127 voice[i].noiseSeed ^= 0x28000;
129 voice[i].noiseSeed >>= 1;
136 void WritePSG(uint16_t address, uint8_t data)
138 if ((address >= 0x100) && (address <= 0x13F))
140 uint8_t channel = (address - 0x100) / 8;
141 uint8_t knob = (address - 0x100) - (channel * 8);
148 voice[channel].leftVolume = data & 0x0F;
151 voice[channel].waveform = data >> 4;
152 voice[channel].frequency = ((data & 0x0F) << 16)
153 | (voice[channel].frequency & 0x0FFFF);
155 printf("PSG: Setting waveform on channel %i to %i...\n", channel, voice[channel].waveform);
159 voice[channel].frequency = (data << 8)
160 | (voice[channel].frequency & 0xF00FF);
163 voice[channel].frequency = data
164 | (voice[channel].frequency & 0xFFF00);
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);
171 if (voice[(channel + 1) & 0x07].noise)
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);
180 printf("PSG: Setting right volume on channel %i: vol=%i...\n", channel, voice[channel].rightVolume);
191 printf("PSG: Write to $%03X of $%02X...\n", address, data);
194 //if (address < 0x100)
195 // printf("PSG: Waveform byte[$%02X] = $%02X...\n", address, data);
197 memory[address & 0x01FF] = data;
200 uint8_t ReadPSG(uint16_t address)
202 return memory[address & 0x01FF];