Fixes #110 Make cpu_test also print out the emulated cpu speed

This commit is contained in:
Stefan Arentz 2016-12-30 14:28:34 -08:00
parent 4b8df8f151
commit fee8efce69
1 changed files with 26 additions and 1 deletions

View File

@ -20,9 +20,12 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include <time.h>
#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;
}