diff --git a/src/cpu_test.c b/src/cpu_test.c index 640fe4c..416863a 100644 --- a/src/cpu_test.c +++ b/src/cpu_test.c @@ -20,9 +20,12 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. +#include #include #include #include +#include +#include #include "cpu.h" #include "mem.h" @@ -36,6 +39,12 @@ int test(int model, uint16_t start_addr, uint16_t success_addr, char *rom_path) uint16_t last_pc = cpu.state.pc; + struct timespec start; + if (clock_gettime(CLOCK_REALTIME, &start) != 0) { + perror("Cannot get time"); + exit(1); + } + while (true) { int ret = cpu_step(&cpu); if (ret < 0) { @@ -54,7 +63,23 @@ int test(int model, uint16_t start_addr, uint16_t success_addr, char *rom_path) // ideal. Is there a better way to detect this? if (cpu.state.pc == success_addr) { - fprintf(stderr, "TEST Success\n"); + struct timespec now; + if (clock_gettime(CLOCK_REALTIME, &now) != 0) { + perror("Cannot get time"); + exit(1); + } + + // Taking a shortcut here because our test will never run so + // long that it will overflow an uint64_t + + uint64_t duration_ms = (now.tv_sec * 1000 + (now.tv_nsec / 1000000)) + - (start.tv_sec * 1000 + (start.tv_nsec / 1000000)); + double duration = (double) duration_ms / 1000.0; + double mhz = (double) cpu.counter * (1.0 / duration) / 1000000.0; + + fprintf(stderr, "TEST Success; executed %" PRIu64 " cycles in %.4f at %.4f MHz\n", + cpu.counter, duration, mhz); + return 0; }