| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
#include "support/harness.h" |
| 23 |
|
| 24 |
#include "vera_psg.h" |
| 25 |
|
| 26 |
#include <string.h> |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
#define R_FREQL(ch) ((ch) * 4 + 0) |
| 31 |
#define R_FREQH(ch) ((ch) * 4 + 1) |
| 32 |
#define R_VOL(ch) ((ch) * 4 + 2) |
| 33 |
#define R_WAVE(ch) ((ch) * 4 + 3) |
| 34 |
|
| 35 |
static struct psg_debug_channel |
| 36 |
chan(int ch) |
| 37 |
{ |
| 38 |
struct psg_debug_channel c; |
| 39 |
memset(&c, 0, sizeof c); |
| 40 |
psg_debug_get_channel(ch, &c); |
| 41 |
return c; |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
static void |
| 47 |
voice(int ch, uint16_t freq, uint8_t vol, uint8_t waveform, uint8_t pw) |
| 48 |
{ |
| 49 |
psg_writereg(R_FREQL(ch), freq & 0xFF); |
| 50 |
psg_writereg(R_FREQH(ch), freq >> 8); |
| 51 |
psg_writereg(R_VOL(ch), 0xC0 | (vol & 0x3F)); |
| 52 |
psg_writereg(R_WAVE(ch), (uint8_t)(waveform << 6) | (pw & 0x3F)); |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
static void |
| 57 |
test_reset_seeds_the_noise_lfsr(void) |
| 58 |
{ |
| 59 |
psg_writereg(R_FREQL(0), 0x34); |
| 60 |
psg_writereg(R_VOL(0), 0x3F); |
| 61 |
psg_reset(); |
| 62 |
|
| 63 |
check_eq(psg_debug_get_noise_state(), 1u, |
| 64 |
"reset seeds the noise LFSR to 1"); |
| 65 |
check_eq(chan(0).freq, 0u, "reset clears the frequency"); |
| 66 |
check_eq(chan(0).vol_raw, 0u, "reset clears the volume"); |
| 67 |
check_eq(chan(0).phase, 0u, "reset clears the phase accumulator"); |
| 68 |
check_eq(psg_debug_get_reg(R_FREQL(0)), 0u, "reset clears the register shadow"); |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
static void |
| 82 |
test_register_decode(void) |
| 83 |
{ |
| 84 |
psg_reset(); |
| 85 |
|
| 86 |
psg_writereg(R_FREQL(3), 0x34); |
| 87 |
psg_writereg(R_FREQH(3), 0x12); |
| 88 |
check_eq(chan(3).freq, 0x1234u, "the two frequency bytes form a 16-bit word"); |
| 89 |
|
| 90 |
psg_writereg(R_VOL(3), 0x80 | 0x2A); |
| 91 |
check_eq(chan(3).vol_raw, 0x2Au, "volume is bits 5:0"); |
| 92 |
check(chan(3).right, "bit 7 enables right"); |
| 93 |
check(!chan(3).left, "bit 6 alone controls left"); |
| 94 |
|
| 95 |
psg_writereg(R_VOL(3), 0x40 | 0x2A); |
| 96 |
check(chan(3).left, "bit 6 enables left"); |
| 97 |
check(!chan(3).right, "and bit 7 alone controls right"); |
| 98 |
|
| 99 |
psg_writereg(R_WAVE(3), 0x80 | 0x15); |
| 100 |
check_eq(chan(3).pw, 0x15u, "pulse width is bits 5:0"); |
| 101 |
check_eq(chan(3).waveform, (uint32_t)PSG_WF_TRIANGLE, "waveform is bits 7:6"); |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
static void |
| 111 |
test_register_index_wraps_at_64(void) |
| 112 |
{ |
| 113 |
psg_reset(); |
| 114 |
psg_writereg(0, 0x11); |
| 115 |
psg_writereg(64, 0x22); |
| 116 |
check_eq(chan(0).freq & 0xFF, 0x22u, "register 64 wraps onto register 0"); |
| 117 |
|
| 118 |
psg_writereg(65, 0x33); |
| 119 |
check_eq(chan(0).freq >> 8, 0x33u, "register 65 wraps onto register 1"); |
| 120 |
|
| 121 |
psg_writereg(255, 0x44); |
| 122 |
check_eq(chan(15).pw, 0x04u, "register 255 wraps onto register 63"); |
| 123 |
check_eq(chan(15).waveform, (uint32_t)PSG_WF_SAWTOOTH, "and decodes there normally"); |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
static void |
| 132 |
test_volume_curve(void) |
| 133 |
{ |
| 134 |
psg_reset(); |
| 135 |
|
| 136 |
const struct { uint8_t raw; uint16_t want; } cases[] = { |
| 137 |
{0, 0}, {1, 4}, {2, 8}, {3, 12}, |
| 138 |
{4, 16}, |
| 139 |
{63, 511}, |
| 140 |
}; |
| 141 |
for (unsigned i = 0; i < sizeof cases / sizeof cases[0]; i++) { |
| 142 |
psg_writereg(R_VOL(0), 0xC0 | cases[i].raw); |
| 143 |
check_eq(chan(0).vol_lut, cases[i].want, "volume maps through the curve"); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
static void |
| 154 |
test_phase_is_held_at_zero_while_silent(void) |
| 155 |
{ |
| 156 |
psg_reset(); |
| 157 |
int16_t buf[256]; |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
voice(0, 0x0700, 63, PSG_WF_SAWTOOTH, 0x20); |
| 163 |
psg_render(buf, 64); |
| 164 |
check(chan(0).phase != 0, "an audible voice advances its phase"); |
| 165 |
|
| 166 |
|
| 167 |
psg_writereg(R_VOL(0), 0x3F); |
| 168 |
psg_render(buf, 64); |
| 169 |
check_eq(chan(0).phase, 0u, "a voice with neither side enabled resets to phase zero"); |
| 170 |
|
| 171 |
psg_writereg(R_VOL(0), 0xC0 | 0x3F); |
| 172 |
psg_render(buf, 64); |
| 173 |
check(chan(0).phase != 0, "and runs again once a side is re-enabled"); |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
static void |
| 191 |
test_noise_lfsr_sequence(void) |
| 192 |
{ |
| 193 |
psg_reset(); |
| 194 |
check_eq(psg_debug_get_noise_state(), 1u, "seeded to 1"); |
| 195 |
|
| 196 |
int16_t buf[8]; |
| 197 |
uint16_t expect = psg_debug_get_noise_state(); |
| 198 |
for (int sample = 0; sample < 8; sample++) { |
| 199 |
for (int voice_step = 0; voice_step < PSG_NUM_CHANNELS; voice_step++) { |
| 200 |
unsigned bit = ((expect >> 1) ^ (expect >> 2) ^ (expect >> 4) ^ (expect >> 15)) & 1; |
| 201 |
expect = (uint16_t)((expect << 1) | bit); |
| 202 |
} |
| 203 |
psg_render(buf, 1); |
| 204 |
check_eq(psg_debug_get_noise_state(), expect, |
| 205 |
"the LFSR follows taps 1, 2, 4 and 15, once per voice"); |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
static void |
| 217 |
test_noise_is_latched_on_phase_wrap(void) |
| 218 |
{ |
| 219 |
psg_reset(); |
| 220 |
int16_t buf[8]; |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
voice(0, 0x0800, 63, PSG_WF_NOISE, 0); |
| 226 |
|
| 227 |
unsigned changes = 0; |
| 228 |
int changed_at = -1; |
| 229 |
uint16_t prev = chan(0).noiseval; |
| 230 |
|
| 231 |
for (int sample = 1; sample <= 64; sample++) { |
| 232 |
uint16_t lfsr_before = psg_debug_get_noise_state(); |
| 233 |
psg_render(buf, 1); |
| 234 |
check(psg_debug_get_noise_state() != lfsr_before, |
| 235 |
"the LFSR advances on every sample"); |
| 236 |
|
| 237 |
uint16_t now = chan(0).noiseval; |
| 238 |
if (now != prev) { |
| 239 |
changes++; |
| 240 |
changed_at = sample; |
| 241 |
prev = now; |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
check_eq(changes, 1u, "the voice latches noise once per phase wrap"); |
| 246 |
check_eq((uint32_t)changed_at, 64u, "and does so when the accumulator wraps"); |
| 247 |
check(chan(0).noiseval <= 0x3F, "the latched value is six bits"); |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
static void |
| 255 |
test_waveform_codes(void) |
| 256 |
{ |
| 257 |
psg_reset(); |
| 258 |
const uint8_t codes[] = {PSG_WF_PULSE, PSG_WF_SAWTOOTH, PSG_WF_TRIANGLE, PSG_WF_NOISE}; |
| 259 |
for (unsigned i = 0; i < 4; i++) { |
| 260 |
psg_writereg(R_WAVE(1), (uint8_t)(codes[i] << 6)); |
| 261 |
check_eq(chan(1).waveform, codes[i], "the waveform code round-trips"); |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
static bool |
| 267 |
renders_same(uint8_t waveform, uint8_t pw_a, uint8_t pw_b) |
| 268 |
{ |
| 269 |
int16_t a[256], b[256]; |
| 270 |
|
| 271 |
psg_reset(); |
| 272 |
voice(0, 0x0400, 63, waveform, pw_a); |
| 273 |
psg_render(a, 128); |
| 274 |
|
| 275 |
psg_reset(); |
| 276 |
voice(0, 0x0400, 63, waveform, pw_b); |
| 277 |
psg_render(b, 128); |
| 278 |
|
| 279 |
return memcmp(a, b, sizeof a) == 0; |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
static void |
| 295 |
test_pulse_width_shapes_saw_and_triangle(void) |
| 296 |
{ |
| 297 |
check(!renders_same(PSG_WF_PULSE, 0x10, 0x30), |
| 298 |
"pulse width changes the pulse waveform"); |
| 299 |
|
| 300 |
check(!renders_same(PSG_WF_SAWTOOTH, 0x00, 0x3F), |
| 301 |
"pulse width changes the sawtooth (R48; R47 ignores it)"); |
| 302 |
|
| 303 |
check(!renders_same(PSG_WF_TRIANGLE, 0x00, 0x3F), |
| 304 |
"pulse width changes the triangle (R48; R47 ignores it)"); |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
check(renders_same(PSG_WF_SAWTOOTH, 0x3F, 0x3F), |
| 309 |
"the same pulse width gives the same sawtooth"); |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
static void |
| 318 |
test_silence_at_zero_volume(void) |
| 319 |
{ |
| 320 |
psg_reset(); |
| 321 |
int16_t buf[128]; |
| 322 |
memset(buf, 0x7F, sizeof buf); |
| 323 |
|
| 324 |
voice(0, 0x0400, 0, PSG_WF_SAWTOOTH, 0x20); |
| 325 |
psg_render(buf, 64); |
| 326 |
|
| 327 |
bool silent = true; |
| 328 |
for (unsigned i = 0; i < 128; i++) { |
| 329 |
if (buf[i] != 0) { |
| 330 |
silent = false; |
| 331 |
} |
| 332 |
} |
| 333 |
check(silent, "volume zero renders exact silence"); |
| 334 |
} |
| 335 |
|
| 336 |
int |
| 337 |
main(void) |
| 338 |
{ |
| 339 |
test_reset_seeds_the_noise_lfsr(); |
| 340 |
test_register_decode(); |
| 341 |
test_register_index_wraps_at_64(); |
| 342 |
test_volume_curve(); |
| 343 |
test_phase_is_held_at_zero_while_silent(); |
| 344 |
test_noise_lfsr_sequence(); |
| 345 |
test_noise_is_latched_on_phase_wrap(); |
| 346 |
test_waveform_codes(); |
| 347 |
test_pulse_width_shapes_saw_and_triangle(); |
| 348 |
test_silence_at_zero_volume(); |
| 349 |
return x16_test_summary("vera_psg"); |
| 350 |
} |