From 7ad7b65d11b0f6555709bdde0e612fce83c0df25 Mon Sep 17 00:00:00 2001 From: Aaron Culliney Date: Sat, 23 Jul 2016 17:34:59 -0700 Subject: [PATCH] Add speaker tracing API and tests --- Makefile.am | 2 +- src/audio/speaker.c | 39 ++++++++++ src/audio/speaker.h | 6 ++ src/test/testcommon.c | 3 + src/test/testspeaker.c | 172 +++++++++++++++++++++++++++++++++++++++++ src/test/testtrace.c | 90 ++++++++++++++++++++- src/timing.c | 41 +++++----- 7 files changed, 327 insertions(+), 26 deletions(-) create mode 100644 src/test/testspeaker.c diff --git a/Makefile.am b/Makefile.am index b2507083..d64459c3 100644 --- a/Makefile.am +++ b/Makefile.am @@ -132,7 +132,7 @@ EXTRA_testprefs_SOURCES = $(ASM_SRC_x86) $(VIDEO_SRC) $(AUDIO_SRC) $(META_SRC) ####################################### testtrace_SOURCES = src/test/testtrace.c $(A2_TEST_SOURCES) -testtrace_CFLAGS = $(A2_TEST_CFLAGS) -DTEST_TRACE=1 -DCPU_TRACING=1 -DDISK_TRACING=1 -DVM_TRACING=1 +testtrace_CFLAGS = $(A2_TEST_CFLAGS) -DTEST_TRACE=1 -DCPU_TRACING=1 -DDISK_TRACING=1 -DVM_TRACING=1 -DSPEAKER_TRACING=1 testtrace_CCASFLAGS = $(testtrace_CFLAGS) testtrace_LDFLAGS = $(apple2ix_LDFLAGS) testtrace_LDADD = @testtrace_ASM_O@ @testtrace_VIDEO_O@ @testtrace_AUDIO_O@ @testtrace_META_O@ @X_LIBS@ diff --git a/src/audio/speaker.c b/src/audio/speaker.c index a88ab0e9..4f6a2bc6 100644 --- a/src/audio/speaker.c +++ b/src/audio/speaker.c @@ -62,6 +62,11 @@ static int samples_adjustment_counter = 0; static AudioBuffer_s *speakerBuffer = NULL; +#if SPEAKER_TRACING +static FILE *speaker_trace_fp = NULL; +static unsigned long cycles_trace_toggled = 0; +#endif + // -------------------------------------------------------------------------------------------------------------------- static void speaker_prefsChanged(const char *domain) { @@ -511,6 +516,15 @@ GLUE_C_READ(speaker_toggle) timing_checkpoint_cycles(); +#if SPEAKER_TRACING + // output cycle count delta when toggled + if (speaker_trace_fp) { + unsigned long cycles_trace_delta = cycles_count_total - cycles_trace_toggled; + fprintf(speaker_trace_fp, "%lu\n", cycles_trace_delta); + cycles_trace_toggled = cycles_count_total; + } +#endif + #if DIRECT_SPEAKER_ACCESS #error this used to be implemented ... // AFAIK ... this requires an actual speaker device and ability to access it (usually requiring this program to be @@ -545,3 +559,28 @@ GLUE_C_READ(speaker_toggle) return floating_bus(); } +#if SPEAKER_TRACING +// -------------------------------------------------------------------------------------------------------------------- +// Speaker audio tracing (binary samples output) + +void speaker_traceBegin(const char *trace_file) { + if (trace_file) { + speaker_trace_fp = fopen(trace_file, "w"); + } +} + +void speaker_traceFlush(void) { + if (speaker_trace_fp) { + fflush(speaker_trace_fp); + } +} + +void speaker_traceEnd(void) { + if (speaker_trace_fp) { + fflush(speaker_trace_fp); + fclose(speaker_trace_fp); + speaker_trace_fp = NULL; + } +} +#endif + diff --git a/src/audio/speaker.h b/src/audio/speaker.h index 64c36acf..a9d67315 100644 --- a/src/audio/speaker.h +++ b/src/audio/speaker.h @@ -28,5 +28,11 @@ bool speaker_isActive(void); */ double speaker_cyclesPerSample(void); +#if SPEAKER_TRACING +void speaker_traceBegin(const char *trace_file); +void speaker_traceFlush(void); +void speaker_traceEnd(void); +#endif + #endif /* whole file */ diff --git a/src/test/testcommon.c b/src/test/testcommon.c index b0a11d8a..e40546a4 100644 --- a/src/test/testcommon.c +++ b/src/test/testcommon.c @@ -14,6 +14,9 @@ bool test_do_reboot = true; char mdstr[(SHA_DIGEST_LENGTH*2)+1]; +void (*testing_cyclesOverflow)(void) = NULL; +unsigned long (*testing_getCyclesCount)(void) = NULL; + // ---------------------------------------------------------------------------- void test_common_setup(void) { diff --git a/src/test/testspeaker.c b/src/test/testspeaker.c new file mode 100644 index 00000000..8681112b --- /dev/null +++ b/src/test/testspeaker.c @@ -0,0 +1,172 @@ +/* + * Apple // emulator for *ix + * + * This software package is subject to the GNU General Public License + * version 3 or later (your choice) as published by the Free Software + * Foundation. + * + * Copyright 2016 Aaron Culliney + * + */ + +#include "testcommon.h" + +#define BLANK_DSK "blank.dsk.gz" + +static bool test_thread_running = false; + +extern pthread_mutex_t interface_mutex; // TODO FIXME : raw access to CPU mutex because stepping debugger ... + +static void testspeaker_setup(void *unused) { + test_common_setup(); + apple_ii_64k[0][WATCHPOINT_ADDR] = 0x00; + apple_ii_64k[0][TESTOUT_ADDR] = 0x00; + joy_button0 = 0xff; // OpenApple + if (test_do_reboot) { + cpu65_interrupt(ResetSig); + } +} + +static void testspeaker_teardown(void *unused) { +} + +// ---------------------------------------------------------------------------- +// Spaker tests ... + +extern unsigned long (*testing_getCyclesCount)(void); +extern void (*testing_cyclesOverflow)(void); +static bool cycles_overflowed = false; + +static unsigned long testspeaker_getCyclesCount(void) { + // advance cycles count to near overflow + return ULONG_MAX - (CLK_6502_INT); +} + +static void testspeaker_cyclesOverflow(void) { + cycles_overflowed = true; +} + +// ---------------------------------------------------------------------------- +// TESTS ... + +TEST test_timing_overflow() { + + // force an almost overflow + + testing_getCyclesCount = &testspeaker_getCyclesCount; + testing_cyclesOverflow = &testspeaker_cyclesOverflow; + + ASSERT(!cycles_overflowed); + test_setup_boot_disk(BLANK_DSK, /*readonly:*/1); + BOOT_TO_DOS(); + ASSERT(cycles_overflowed); + + // appears emulator handled cycle count overflow gracefully ... + testing_getCyclesCount = NULL; + testing_cyclesOverflow = NULL; + + PASS(); +} + +#define EXPECTED_BEEP_TRACE_FILE_SIZE 770 +#define EXPECTED_BEEP_TRACE_SHA "69C728A65B5933D73F91D77694BEE7F674C9EDF7" +TEST test_boot_sound() { + + const char *homedir = HOMEDIR; + char *testout = NULL; + ASPRINTF(&testout, "%s/a2_speaker_beep_test.txt", homedir); + if (testout) { + unlink(testout); + speaker_traceBegin(testout); + } + + test_setup_boot_disk(BLANK_DSK, /*readonly:*/1); + BOOT_TO_DOS(); + + speaker_traceEnd(); + disk6_eject(0); + + do { + uint8_t md[SHA_DIGEST_LENGTH]; + char mdstr0[(SHA_DIGEST_LENGTH*2)+1]; + + FILE *fp = fopen(testout, "r"); + + fseek(fp, 0, SEEK_END); + long expectedSize = ftell(fp); + ASSERT(expectedSize == EXPECTED_BEEP_TRACE_FILE_SIZE); + fseek(fp, 0, SEEK_SET); + + unsigned char *buf = MALLOC(EXPECTED_BEEP_TRACE_FILE_SIZE); + if (fread(buf, 1, EXPECTED_BEEP_TRACE_FILE_SIZE, fp) != EXPECTED_BEEP_TRACE_FILE_SIZE) { + ASSERT(false); + } + fclose(fp); fp = NULL; + SHA1(buf, EXPECTED_BEEP_TRACE_FILE_SIZE, md); + FREE(buf); + + sha1_to_str(md, mdstr0); + ASSERT(strcmp(mdstr0, EXPECTED_BEEP_TRACE_SHA) == 0); + } while(0); + + unlink(testout); + FREE(testout); + + PASS(); +} + +// ---------------------------------------------------------------------------- +// Test Suite + +GREATEST_SUITE(test_suite_speaker) { + pthread_mutex_lock(&interface_mutex); + + test_thread_running = true; + + GREATEST_SET_SETUP_CB(testspeaker_setup, NULL); + GREATEST_SET_TEARDOWN_CB(testspeaker_teardown, NULL); + //GREATEST_SET_BREAKPOINT_CB(test_breakpoint, NULL); + + // TESTS -------------------------- + + RUN_TESTp(test_timing_overflow); + RUN_TESTp(test_boot_sound); + + // -------------------------------- + pthread_mutex_unlock(&interface_mutex); +} + +SUITE(test_suite_speaker); +GREATEST_MAIN_DEFS(); + +static char **test_argv = NULL; +static int test_argc = 0; + +static int _test_thread(void) { + int argc = test_argc; + char **argv = test_argv; + GREATEST_MAIN_BEGIN(); + RUN_SUITE(test_suite_speaker); + GREATEST_MAIN_END(); +} + +static void *test_thread(void *dummyptr) { + _test_thread(); + return NULL; +} + +void test_speaker(int _argc, char **_argv) { + test_argc = _argc; + test_argv = _argv; + + test_common_init(); + + pthread_t p; + pthread_create(&p, NULL, (void *)&test_thread, (void *)NULL); + while (!test_thread_running) { + struct timespec ts = { .tv_sec=0, .tv_nsec=33333333 }; + nanosleep(&ts, NULL); + } + pthread_detach(p); +} + diff --git a/src/test/testtrace.c b/src/test/testtrace.c index 233926d8..66197bbb 100644 --- a/src/test/testtrace.c +++ b/src/test/testtrace.c @@ -12,7 +12,6 @@ #include "testcommon.h" #define ABUSIVE_TESTS 1 -#define FINICKY_TESTS 1 #define TESTING_DISK "testvm1.dsk.gz" #define BLANK_DSK "blank.dsk.gz" @@ -39,7 +38,89 @@ static void testtrace_teardown(void *arg) { } // ---------------------------------------------------------------------------- -// Disk TESTS ... +// Cycles counter overflow test + +extern unsigned long (*testing_getCyclesCount)(void); +extern void (*testing_cyclesOverflow)(void); +static bool cycles_overflowed = false; + +static unsigned long testspeaker_getCyclesCount(void) { + // advance cycles count to near overflow + return ULONG_MAX - (CLK_6502_INT); +} + +static void testspeaker_cyclesOverflow(void) { + cycles_overflowed = true; +} + +TEST test_timing_overflow() { + + // force an almost overflow + + testing_getCyclesCount = &testspeaker_getCyclesCount; + testing_cyclesOverflow = &testspeaker_cyclesOverflow; + + ASSERT(!cycles_overflowed); + test_setup_boot_disk(BLANK_DSK, /*readonly:*/1); + BOOT_TO_DOS(); + ASSERT(cycles_overflowed); + + // appears emulator handled cycle count overflow gracefully ... + testing_getCyclesCount = NULL; + testing_cyclesOverflow = NULL; + + PASS(); +} + +// ---------------------------------------------------------------------------- +// Tracing TESTS ... + +#define EXPECTED_BEEP_TRACE_FILE_SIZE 770 +#define EXPECTED_BEEP_TRACE_SHA "69C728A65B5933D73F91D77694BEE7F674C9EDF7" +TEST test_boot_sound() { + + const char *homedir = HOMEDIR; + char *testout = NULL; + ASPRINTF(&testout, "%s/a2_speaker_beep_test.txt", homedir); + if (testout) { + unlink(testout); + speaker_traceBegin(testout); + } + + test_setup_boot_disk(BLANK_DSK, /*readonly:*/1); + BOOT_TO_DOS(); + + speaker_traceEnd(); + disk6_eject(0); + + do { + uint8_t md[SHA_DIGEST_LENGTH]; + char mdstr0[(SHA_DIGEST_LENGTH*2)+1]; + + FILE *fp = fopen(testout, "r"); + + fseek(fp, 0, SEEK_END); + long expectedSize = ftell(fp); + ASSERT(expectedSize == EXPECTED_BEEP_TRACE_FILE_SIZE); + fseek(fp, 0, SEEK_SET); + + unsigned char *buf = MALLOC(EXPECTED_BEEP_TRACE_FILE_SIZE); + if (fread(buf, 1, EXPECTED_BEEP_TRACE_FILE_SIZE, fp) != EXPECTED_BEEP_TRACE_FILE_SIZE) { + ASSERT(false); + } + fclose(fp); fp = NULL; + SHA1(buf, EXPECTED_BEEP_TRACE_FILE_SIZE, md); + FREE(buf); + + sha1_to_str(md, mdstr0); + ASSERT(strcmp(mdstr0, EXPECTED_BEEP_TRACE_SHA) == 0); + } while(0); + + unlink(testout); + FREE(testout); + + PASS(); +} // This test is majorly abusive ... it creates an ~800MB file in $HOME // ... but if it's correct, you're fairly assured the cpu/vm is working =) @@ -395,18 +476,19 @@ GREATEST_SUITE(test_suite_trace) { // TESTS -------------------------- + RUN_TESTp(test_timing_overflow); + RUN_TESTp(test_boot_sound); + #if ABUSIVE_TESTS RUN_TESTp(test_boot_disk_cputrace); #endif -#if FINICKY_TESTS RUN_TESTp(test_cputrace_hello_dsk); RUN_TESTp(test_cputrace_hello_nib); RUN_TESTp(test_cputrace_hello_po); RUN_TESTp(test_boot_disk_vmtrace); RUN_TESTp(test_boot_disk_vmtrace_nib); RUN_TESTp(test_boot_disk_vmtrace_po); -#endif // ... disk6_eject(0); diff --git a/src/timing.c b/src/timing.c index 7bf16a94..2242b17f 100644 --- a/src/timing.c +++ b/src/timing.c @@ -50,7 +50,7 @@ // cycle counting double cycles_persec_target = CLK_6502; -unsigned long cycles_count_total = 0; // Running at spec ~1MHz, this will approach overflow in ~4000secs +unsigned long cycles_count_total = 0; // Running at spec ~1MHz, this will approach overflow in ~4000secs (for 32bit architectures) int cycles_speaker_feedback = 0; int32_t cpu65_cycles_to_execute = 0; // cycles-to-execute by cpu65_run() int32_t cpu65_cycle_count = 0; // cycles currently excuted by cpu65_run() @@ -147,6 +147,12 @@ void reinitialize(void) { #endif cycles_count_total = 0; +#if TESTING + extern unsigned long (*testing_getCyclesCount)(void); + if (testing_getCyclesCount) { + cycles_count_total = testing_getCyclesCount(); + } +#endif vm_initialize(); @@ -297,11 +303,6 @@ static void *cpu_thread(void *dummyptr) { LOG("cpu_thread : begin main loop ..."); -#ifndef NDEBUG - extern void timing_testCyclesCountOverflow(void); - timing_testCyclesCountOverflow(); -#endif - clock_gettime(CLOCK_MONOTONIC, &t0); do { @@ -357,6 +358,7 @@ static void *cpu_thread(void *dummyptr) { if (is_debugging) { debugging_cycles -= cpu65_cycle_count; + timing_checkpoint_cycles(); if (c_debugger_should_break() || (debugging_cycles <= 0)) { int err = 0; if ((err = pthread_cond_signal(&dbg_thread_cond))) { @@ -366,13 +368,12 @@ static void *cpu_thread(void *dummyptr) { ERRLOG("pthread_cond_wait : %d", err); } if (debugging_cycles <= 0) { - cpu65_cycle_count = debugging_cycles0 - debugging_cycles/*<=0*/; break; } } - } - if (emul_reinitialize) { - reinitialize(); + if (emul_reinitialize) { + reinitialize(); + } } } while (is_debugging); #if DEBUG_TIMING @@ -539,23 +540,21 @@ void timing_checkpoint_cycles(void) { const int32_t d = cpu65_cycle_count - cycles_checkpoint_count; assert(d >= 0); +#if TESTING + unsigned long previous_cycles_count_total = cycles_count_total; +#endif cycles_count_total += d; -#ifndef NDEBUG - if (UNLIKELY(cycles_count_total < cycles_checkpoint_count)) { - LOG("OVERFLOWED cycles_count_total..."); +#if TESTING + if (UNLIKELY(cycles_count_total < previous_cycles_count_total)) { + extern void (*testing_cyclesOverflow)(void); + if (testing_cyclesOverflow) { + testing_cyclesOverflow(); + } } #endif cycles_checkpoint_count = cpu65_cycle_count; } -#ifndef NDEBUG -void timing_testCyclesCountOverflow(void) { - // advance cycles count to near overflow - LOG("almost overflow ..."); - cycles_count_total = ULONG_MAX - (CLK_6502_INT*10); -} -#endif - // ---------------------------------------------------------------------------- static void vm_prefsChanged(const char *domain) {