| 1 |
// Commander X16 Emulator |
| 2 |
// Copyright (c) 2020 Frank van den Hoef |
| 3 |
// All rights reserved. License: 2-clause BSD |
| 4 |
|
| 5 |
// Chip revisions |
| 6 |
// 0: V 0.3.0 |
| 7 |
// 1: V 47.0.2 (9-bit volume, phase reset on mute) |
| 8 |
// 2: V 48.0.1 (Pulse Width XOR on Saw and Triangle) |
| 9 |
|
| 10 |
#include "vera_psg.h" |
| 11 |
|
| 12 |
#include <stdlib.h> |
| 13 |
#include <string.h> |
| 14 |
|
| 15 |
enum waveform { |
| 16 |
WF_PULSE = 0, |
| 17 |
WF_SAWTOOTH, |
| 18 |
WF_TRIANGLE, |
| 19 |
WF_NOISE, |
| 20 |
}; |
| 21 |
|
| 22 |
static uint16_t volume_lut[64] = {0, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 6, 6, 7, 7, 7, 8, 8, 9, 9, 10, 11, 11, 12, 13, 14, 14, 15, 16, 17, 18, 19, 21, 22, 23, 25, 26, 28, 29, 31, 33, 35, 37, 39, 42, 44, 47, 50, 52, 56, 59, 63}; |
| 23 |
static uint16_t volume_lut_47[64] = { |
| 24 |
0, 4, 8, 12, |
| 25 |
16, 17, 18, 20, 21, 22, 23, 25, 26, 28, 30, 31, |
| 26 |
33, 35, 37, 40, 42, 45, 47, 50, 53, 56, 60, 63, |
| 27 |
67, 71, 75, 80, 85, 90, 95, 101, 107, 113, 120, 127, |
| 28 |
135, 143, 151, 160, 170, 180, 191, 202, 214, 227, 241, 255, |
| 29 |
270, 286, 303, 321, 341, 361, 382, 405, 429, 455, 482, 511 |
| 30 |
}; |
| 31 |
|
| 32 |
void |
| 33 |
psg_reset(struct VERA_PSG* psg) |
| 34 |
{ |
| 35 |
memset(psg->channels, 0, sizeof(psg->channels)); |
| 36 |
psg->noiseOut = 0; |
| 37 |
psg->noiseState = 1; |
| 38 |
} |
| 39 |
|
| 40 |
void |
| 41 |
psg_writereg(struct VERA_PSG* psg, uint8_t reg, uint8_t val) |
| 42 |
{ |
| 43 |
reg &= 0x3f; |
| 44 |
|
| 45 |
int ch = reg / 4; |
| 46 |
int idx = reg & 3; |
| 47 |
|
| 48 |
switch (idx) { |
| 49 |
case 0: psg->channels[ch].freq = (psg->channels[ch].freq & 0xFF00) | val; break; |
| 50 |
case 1: psg->channels[ch].freq = (psg->channels[ch].freq & 0x00FF) | (val << 8); break; |
| 51 |
case 2: { |
| 52 |
psg->channels[ch].right = (val & 0x80) != 0; |
| 53 |
psg->channels[ch].left = (val & 0x40) != 0; |
| 54 |
psg->channels[ch].volume = ((psg->chipType < 1) ? volume_lut : volume_lut_47)[val & 0x3F]; |
| 55 |
break; |
| 56 |
} |
| 57 |
case 3: { |
| 58 |
psg->channels[ch].pw = val & 0x3F; |
| 59 |
psg->channels[ch].waveform = val >> 6; |
| 60 |
break; |
| 61 |
} |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
static inline void |
| 66 |
render(struct VERA_PSG* psg, int16_t *left, int16_t *right) |
| 67 |
{ |
| 68 |
int l = 0; |
| 69 |
int r = 0; |
| 70 |
|
| 71 |
for (int i = 0; i < 16; i++) { |
| 72 |
// In FPGA implementation, noise values are generated every system clock and |
| 73 |
// the channel update is run sequentially. So, even if both two channels are |
| 74 |
// fetching a noise value in the same sample, they should have different values |
| 75 |
psg->noiseOut = ((psg->noiseOut << 1) | (psg->noiseState & 1)) & 63; |
| 76 |
psg->noiseState = (psg->noiseState << 1) | (((psg->noiseState >> 1) ^ (psg->noiseState >> 2) ^ (psg->noiseState >> 4) ^ (psg->noiseState >> 15)) & 1); |
| 77 |
|
| 78 |
struct VERAChannel *ch = &psg->channels[i]; |
| 79 |
|
| 80 |
unsigned new_phase = (ch->phase + ch->freq) & 0x1FFFF; |
| 81 |
if ((psg->chipType >= 1) && (!ch->left && !ch->right)) { |
| 82 |
new_phase = 0; |
| 83 |
} |
| 84 |
if ((psg->chipType < 3) ? (ch->phase & 0x10000) != (new_phase & 0x10000) : (ch->phase & 0x10000) && !(new_phase & 0x10000)) { |
| 85 |
ch->noiseval = (psg->chipType < 1) ? psg->noiseOut : (psg->noiseState >> 1) & 0x3f; |
| 86 |
} |
| 87 |
ch->phase = new_phase; |
| 88 |
|
| 89 |
uint8_t v = 0; |
| 90 |
switch (ch->waveform) { |
| 91 |
case WF_PULSE: v = (ch->phase >> 10) > ch->pw ? 0 : 63; break; |
| 92 |
case WF_SAWTOOTH: v = (ch->phase >> 11) ^ (psg->chipType < 2 ? 0 : (ch->pw ^ 0x3f) & 0x3f); break; |
| 93 |
case WF_TRIANGLE: v = ((ch->phase & 0x10000) ? (~(ch->phase >> 10) & 0x3F) : ((ch->phase >> 10) & 0x3F)) ^ (psg->chipType < 2 ? 0 : (ch->pw ^ 0x3f) & 0x3f); break; |
| 94 |
case WF_NOISE: v = ch->noiseval; break; |
| 95 |
} |
| 96 |
int8_t sv = (v ^ 0x20); |
| 97 |
if (sv & 0x20) { |
| 98 |
sv |= 0xC0; |
| 99 |
} |
| 100 |
|
| 101 |
int val = (int)sv * (int)ch->volume; |
| 102 |
|
| 103 |
if (ch->left) { |
| 104 |
l += (psg->chipType < 1) ? val : val >> 3; |
| 105 |
} |
| 106 |
if (ch->right) { |
| 107 |
r += (psg->chipType < 1) ? val : val >> 3; |
| 108 |
} |
| 109 |
|
| 110 |
if (ch->left || ch->right) { |
| 111 |
ch->lastOut = (psg->chipType < 1) ? val << 3 : val; |
| 112 |
} else { |
| 113 |
ch->lastOut = 0; |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
*left = l; |
| 118 |
*right = r; |
| 119 |
} |
| 120 |
|
| 121 |
void |
| 122 |
psg_render(struct VERA_PSG* psg, int16_t *bufL, int16_t *bufR, unsigned num_samples) |
| 123 |
{ |
| 124 |
while (num_samples--) { |
| 125 |
render(psg, bufL, bufR); |
| 126 |
bufL++; |
| 127 |
bufR++; |
| 128 |
} |
| 129 |
} |