#include "psg.h"
#include <stdio.h>
#include <string.h>
+#include "fileio.h"
struct Voice
{
static uint32_t sampleRate = 44100;
static uint32_t divisor;
+void PSGSaveState(FILE * file)
+{
+ size_t ignored = fwrite(memory, 1, 0x200, file);
+ ignored = fwrite(&voice, 1, sizeof(voice), file);
+}
+
+void PSGLoadState(FILE * file)
+{
+ size_t ignored = fread(memory, 1, 0x200, file);
+ ignored = fread(&voice, 1, sizeof(voice), file);
+}
+
void InitPSG(uint32_t s/*=44100*/)
{
sampleRate = s;
//
void UpdatePSG(uint8_t * buffer, int count)
{
-/*
-if F == 44100, then counter++ for each sample.
-if F == 88200, then counter += 2 for each sample.
-if F == 22050, then counter += 0.5 for each sample.
-*/
// Recast buffer as int16, we're doing 16-bit sound here
int16_t * p = (int16_t *)buffer;
for(int j=0; j<count; j++)
{
-// uint8_t pos = (voice[i].counter / (sampleRate * 2)) & 0x1F;
-// Where does 60000 come from? IDK. This would probably change depending on
-// the playback rate (currently, 44100). N.B.: 58800 is a hair too high in pitch!
-// 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 / divisor) & 0x1F;
uint8_t sample = ((pos & 0x01) == 0
? memory[(voice[i].waveform * 16) + (pos / 2)] >> 4
: memory[(voice[i].waveform * 16) + (pos / 2)]) & 0x0F;
-// p[j] += (((memory[(voice[i].waveform * 32) + pos] & 0x0F) - 8)
-// * voice[i].leftVolume) << 5;
p[j] += ((sample - 8) * voice[i].leftVolume) << 5;
voice[i].counter += voice[i].frequency;
}
voice[channel].waveform = data >> 4;
voice[channel].frequency = ((data & 0x0F) << 16)
| (voice[channel].frequency & 0x0FFFF);
-#if 0
-printf("PSG: Setting waveform on channel %i to %i...\n", channel, voice[channel].waveform);
-#endif
break;
case 2:
voice[channel].frequency = (data << 8)
voice[channel].rightVolume = data & 0x0F;
// Noise switch is channel # + 1 (wraps to zero)
voice[(channel + 1) & 0x07].noise = (data & 0x80 ? true : false);
-#if 0
-if (voice[(channel + 1) & 0x07].noise)
-{
- uint8_t ch = (channel + 1) & 0x07;
- printf("PSG: Setting noise on channel %i, vol=%i, freq=%i...\n", ch, voice[ch].leftVolume, voice[ch].frequency);
-}
-#endif
-#if 0
-if (data & 0x0F)
-{
- printf("PSG: Setting right volume on channel %i: vol=%i...\n", channel, voice[channel].rightVolume);
-}
-#endif
break;
}
}
-
-// return;
}
-#if 0
- else
- printf("PSG: Write to $%03X of $%02X...\n", address, data);
-#endif
-
-//if (address < 0x100)
-// printf("PSG: Waveform byte[$%02X] = $%02X...\n", address, data);
memory[address & 0x01FF] = data;
}