// --- ---------- -----------------------------------------------------------
// JLH 04/21/2014 Created this file.
//
+//
+// Notes:
+// ------
+// The emulator creates signed 16-bit samples. Make sure there's enough room in
+// your buffer for them!
+//
#include "psg.h"
#include <stdio.h>
static uint8_t memory[0x200];
//static
static uint32_t sampleRate = 44100;
-
-extern FILE * psg1;
-extern FILE * psg2;
+static uint32_t divisor;
void InitPSG(uint32_t s/*=44100*/)
{
sampleRate = s;
+ divisor = (uint32_t)((float)s / 0.735); // Voodoo constant
+ // Noise generation will fail if the noise seeds aren't primed...
for(int i=0; i<8; i++)
voice[i].noiseSeed = 1;
}
+//
+// Note that it doesn't wipe out the buffer passed in, if you want it wiped,
+// you have to wipe it yourself. :-)
+//
void UpdatePSG(uint8_t * buffer, int count)
{
/*
if F == 88200, then counter += 2 for each sample.
if F == 22050, then counter += 0.5 for each sample.
*/
-// memset(buffer, 0, count * 2);
-
// Recast buffer as int16, we're doing 16-bit sound here
int16_t * p = (int16_t *)buffer;
// 44100 / 60000 = 0.735
// 192000 / 60000 = 3.2
// uint8_t pos = (voice[i].counter / (60000)) & 0x1F;
- uint8_t pos = (voice[i].counter / (65536)) & 0x1F;
+// uint8_t pos = (voice[i].counter / (65536)) & 0x1F;
+ uint8_t pos = (voice[i].counter / divisor) & 0x1F;
uint8_t sample = ((pos & 0x01) == 0
? memory[(voice[i].waveform * 16) + (pos / 2)] >> 4
: memory[(voice[i].waveform * 16) + (pos / 2)]) & 0x0F;
if ((voice[i].leftVolume == 0) || ((voice[i].frequency & 0xFF) == 0))
continue;
+ // The hold stuff here is VOODOO
+ // Need to figure out what's really going on here, what the clock
+ // rate of this chip is. Also, some freqs can be > 255 according to
+ // Rolling Thunder...
int16_t sample = (7 * voice[i].leftVolume) << 4;
int16_t noiseSign = 1;
int16_t hold = 1 << 1;
for(int j=0; j<count; j++)
{
p[j] += sample * noiseSign;
-#if 0
-if (i == 1)
-{
- fputc(sample & 0xFF, psg1);
- fputc((sample >> 8) & 0xFF , psg1);
-}
-else if (i == 3)
-{
- fputc(sample & 0xFF, psg2);
- fputc((sample >> 8) & 0xFF , psg2);
-}
-#endif
if (hold)
{