| 9120e874 Andy McCall |
1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
#include "support/harness.h" |
| 51 |
|
| 52 |
#include "vera_pcm.h" |
| 53 |
|
| 54 |
#include <string.h> |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
#define USABLE (PCM_FIFO_SIZE - 1) |
| 65 |
|
| 66 |
static struct pcm_debug_state |
| 67 |
state(void) |
| 68 |
{ |
| 69 |
struct pcm_debug_state s; |
| 70 |
pcm_debug_get_state(&s); |
| 71 |
return s; |
| 72 |
} |
| 73 |
|
| 74 |
static void |
| 75 |
fill(unsigned count, uint8_t first) |
| 76 |
{ |
| 77 |
for (unsigned i = 0; i < count; i++) { |
| 78 |
pcm_write_fifo((uint8_t)(first + i)); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
static void |
| 85 |
test_reset_clears_pointers_and_registers(void) |
| 86 |
{ |
| 87 |
pcm_reset(); |
| 88 |
fill(100, 0x10); |
| 89 |
pcm_write_ctrl(0x2F); |
| 90 |
pcm_write_rate(64); |
| 91 |
|
| 92 |
pcm_reset(); |
| 93 |
struct pcm_debug_state s = state(); |
| 94 |
check_eq(s.fifo_cnt, 0u, "reset empties the FIFO"); |
| 95 |
check_eq(s.fifo_rdidx, 0u, "reset clears the read pointer"); |
| 96 |
check_eq(s.fifo_wridx, 0u, "reset clears the write pointer"); |
| 97 |
check_eq(s.ctrl, 0u, "reset clears the control register"); |
| 98 |
check_eq(s.rate, 0u, "reset clears the rate register"); |
| 99 |
check_eq(s.phase, 0u, "reset clears the rate accumulator"); |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
static void |
| 110 |
test_fifo_depth_is_one_less_than_its_memory(void) |
| 111 |
{ |
| 112 |
pcm_reset(); |
| 113 |
fill(USABLE, 0); |
| 114 |
check_eq(state().fifo_cnt, (unsigned)USABLE, |
| 115 |
"the FIFO fills to 4095, one short of its 4096 cells"); |
| 116 |
|
| 117 |
pcm_write_fifo(0xAA); |
| 118 |
check_eq(state().fifo_cnt, (unsigned)USABLE, |
| 119 |
"a write to a full FIFO is dropped"); |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
static void |
| 129 |
test_full_and_empty_status_bits(void) |
| 130 |
{ |
| 131 |
pcm_reset(); |
| 132 |
check(pcm_read_ctrl() & 0x40, "an empty FIFO reports empty"); |
| 133 |
check(!(pcm_read_ctrl() & 0x80), "an empty FIFO does not report full"); |
| 134 |
|
| 135 |
pcm_write_fifo(0x01); |
| 136 |
check(!(pcm_read_ctrl() & 0x40), "one byte is not empty"); |
| 137 |
check(!(pcm_read_ctrl() & 0x80), "one byte is not full"); |
| 138 |
|
| 139 |
fill(USABLE - 1, 0); |
| 140 |
check(pcm_read_ctrl() & 0x80, "4095 bytes reports full"); |
| 141 |
check(!(pcm_read_ctrl() & 0x40), "a full FIFO does not report empty"); |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
static void |
| 151 |
test_almost_empty_is_strictly_below_1024(void) |
| 152 |
{ |
| 153 |
pcm_reset(); |
| 154 |
check(pcm_is_fifo_almost_empty(), "an empty FIFO is almost empty"); |
| 155 |
|
| 156 |
fill(1023, 0); |
| 157 |
check(pcm_is_fifo_almost_empty(), "1023 bytes is almost empty"); |
| 158 |
check(state().almost_empty, "and the debug view agrees"); |
| 159 |
|
| 160 |
pcm_write_fifo(0); |
| 161 |
check_eq(state().fifo_cnt, 1024u, "1024 bytes queued"); |
| 162 |
check(!pcm_is_fifo_almost_empty(), "1024 bytes is not almost empty"); |
| 163 |
check(!state().almost_empty, "and the debug view agrees"); |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
static void |
| 176 |
test_ctrl_stores_mode_and_volume_only(void) |
| 177 |
{ |
| 178 |
pcm_reset(); |
| 179 |
pcm_write_ctrl(0x3F); |
| 180 |
check_eq(state().ctrl, 0x3Fu, "16-bit, stereo and full volume are stored"); |
| 181 |
|
| 182 |
pcm_reset(); |
| 183 |
pcm_write_ctrl(0x15); |
| 184 |
check_eq(state().ctrl, 0x15u, "stereo and volume 5 are stored"); |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
static void |
| 195 |
test_ctrl_bit7_resets_the_fifo(void) |
| 196 |
{ |
| 197 |
pcm_reset(); |
| 198 |
fill(50, 0); |
| 199 |
pcm_write_ctrl(0x80); |
| 200 |
check_eq(state().fifo_cnt, 0u, "bit 7 empties the FIFO"); |
| 201 |
check_eq(state().ctrl, 0u, "bit 7 itself is not stored"); |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
static void |
| 239 |
test_rate_register_stores_the_raw_value(void) |
| 240 |
{ |
| 241 |
pcm_reset(); |
| 242 |
|
| 243 |
pcm_write_rate(0); |
| 244 |
check_eq(pcm_read_rate(), 0u, "rate 0 is stored as 0"); |
| 245 |
pcm_write_rate(64); |
| 246 |
check_eq(pcm_read_rate(), 64u, "rate 64 is stored as 64"); |
| 247 |
pcm_write_rate(128); |
| 248 |
check_eq(pcm_read_rate(), 128u, "rate 128 is stored as 128"); |
| 249 |
|
| 250 |
pcm_write_rate(129); |
| 251 |
check_divergent(pcm_read_rate() == 129, "rate 129 is stored as 129", |
| 252 |
"emulator folds to 127; top.v:480 stores write_data unchanged"); |
| 253 |
|
| 254 |
pcm_write_rate(200); |
| 255 |
check_divergent(pcm_read_rate() == 200, "rate 200 is stored as 200", |
| 256 |
"emulator folds to 56; on hardware 200 plays faster than 128, not slower"); |
| 257 |
|
| 258 |
pcm_write_rate(255); |
| 259 |
check_divergent(pcm_read_rate() == 255, "rate 255 is stored as 255", |
| 260 |
"emulator folds to 1, near-silence, where hardware runs near maximum"); |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
static void |
| 291 |
test_ctrl_bit6_is_ignored(void) |
| 292 |
{ |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
pcm_reset(); |
| 297 |
fill(100, 0x40); |
| 298 |
int16_t buf[128]; |
| 299 |
pcm_write_rate(128); |
| 300 |
pcm_render(buf, 20); |
| 301 |
|
| 302 |
unsigned rd_before = state().fifo_rdidx; |
| 303 |
unsigned cnt_before = state().fifo_cnt; |
| 304 |
check(rd_before > 0, "the read pointer has advanced"); |
| 305 |
|
| 306 |
pcm_write_ctrl(0x40); |
| 307 |
check_divergent(state().fifo_rdidx == rd_before, |
| 308 |
"bit 6 leaves the read pointer alone", |
| 309 |
"emulator rewinds it to 0; bit 6 of 0x1B is not decoded in top.v"); |
| 310 |
check_divergent(state().fifo_cnt == cnt_before, |
| 311 |
"bit 6 leaves the queued count alone", |
| 312 |
"emulator requeues everything written; there is no restart in the RTL"); |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
static void |
| 331 |
test_no_loop_mode(void) |
| 332 |
{ |
| 333 |
pcm_reset(); |
| 334 |
fill(50, 0); |
| 335 |
pcm_write_ctrl(0xC0); |
| 336 |
check_divergent(state().fifo_cnt == 0, |
| 337 |
"bit 7 resets the FIFO even with bit 6 set", |
| 338 |
"emulator reads bits 7+6 as loop mode and keeps the data; no loop in the RTL"); |
| 339 |
} |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
static void |
| 347 |
test_pointers_wrap_within_the_ring(void) |
| 348 |
{ |
| 349 |
pcm_reset(); |
| 350 |
fill(USABLE, 0); |
| 351 |
|
| 352 |
int16_t buf[4096]; |
| 353 |
pcm_write_rate(128); |
| 354 |
pcm_render(buf, 2000); |
| 355 |
|
| 356 |
unsigned before = state().fifo_cnt; |
| 357 |
check(before < (unsigned)USABLE, "rendering consumed part of the queue"); |
| 358 |
|
| 359 |
fill(100, 0x80); |
| 360 |
struct pcm_debug_state s = state(); |
| 361 |
check_eq(s.fifo_cnt, before + 100, "writes after the wrap are queued"); |
| 362 |
check(s.fifo_wridx < PCM_FIFO_SIZE, "the write pointer stays in the ring"); |
| 363 |
check(s.fifo_rdidx < PCM_FIFO_SIZE, "the read pointer stays in the ring"); |
| 364 |
} |
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
static void |
| 372 |
test_debug_peek_consumes_nothing(void) |
| 373 |
{ |
| 374 |
pcm_reset(); |
| 375 |
fill(10, 0x30); |
| 376 |
|
| 377 |
uint8_t seen[10]; |
| 378 |
unsigned got = pcm_debug_peek_fifo(seen, 0, sizeof seen); |
| 379 |
check_eq(got, 10u, "peeking returns what was queued"); |
| 380 |
check_eq(seen[0], 0x30u, "peeking starts at the read pointer"); |
| 381 |
check_eq(seen[9], 0x39u, "and reads forward in order"); |
| 382 |
check_eq(state().fifo_cnt, 10u, "peeking consumes nothing"); |
| 383 |
check_eq(state().fifo_rdidx, 0u, "and does not move the read pointer"); |
| 384 |
|
| 385 |
got = pcm_debug_peek_fifo(seen, 8, sizeof seen); |
| 386 |
check_eq(got, 2u, "peeking past the end stops at the queued data"); |
| 387 |
check_eq(seen[0], 0x38u, "the offset is applied from the read pointer"); |
| 388 |
|
| 389 |
check_eq(pcm_debug_peek_fifo(seen, 10, 4), 0u, |
| 390 |
"an offset at the end of the queue returns nothing"); |
| 391 |
} |
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
static void |
| 400 |
test_empty_fifo_renders_silence(void) |
| 401 |
{ |
| 402 |
pcm_reset(); |
| 403 |
int16_t buf[64]; |
| 404 |
memset(buf, 0x7F, sizeof buf); |
| 405 |
pcm_render(buf, 32); |
| 406 |
bool silent = true; |
| 407 |
for (int i = 0; i < 64; i++) { |
| 408 |
if (buf[i] != 0) { |
| 409 |
silent = false; |
| 410 |
} |
| 411 |
} |
| 412 |
check(silent, "rendering an empty FIFO writes silence"); |
| 413 |
} |
| 414 |
|
| 415 |
int |
| 416 |
main(void) |
| 417 |
{ |
| 418 |
test_reset_clears_pointers_and_registers(); |
| 419 |
test_fifo_depth_is_one_less_than_its_memory(); |
| 420 |
test_full_and_empty_status_bits(); |
| 421 |
test_almost_empty_is_strictly_below_1024(); |
| 422 |
test_ctrl_stores_mode_and_volume_only(); |
| 423 |
test_ctrl_bit7_resets_the_fifo(); |
| 424 |
test_rate_register_stores_the_raw_value(); |
| 425 |
test_ctrl_bit6_is_ignored(); |
| 426 |
test_no_loop_mode(); |
| 427 |
test_pointers_wrap_within_the_ring(); |
| 428 |
test_debug_peek_consumes_nothing(); |
| 429 |
test_empty_fifo_renders_silence(); |
| 430 |
return x16_test_summary("vera_pcm"); |
| 431 |
} |