aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.c
blob: 27e886aa2bc211d71c2def8757346b04c66f4eb5 (plain) (blame)
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
#include "gb.h"
#include "cpu.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char **argv) {
    if (argc < 2) {
        fprintf(stderr, "usage: %s <rom> [--test seconds]\n", argv[0]);
        return 1;
    }
    const char *rom = argv[1];
    bool test = false;
    double seconds = 30.0;
    for (int i = 2; i < argc; i++) {
        if (!strcmp(argv[i], "--test")) {
            test = true;
            if (i + 1 < argc) seconds = atof(argv[++i]);
        }
    }

    GB *gb = calloc(1, sizeof(GB));
    if (cart_load(&gb->cart, rom) != 0) return 1;
    gb_reset(gb);

    if (test) {
        gb->serial_log = true;
        u64 target = (u64)(seconds * 4194304.0);
        while (gb->cycles < target) cpu_step(gb);
        fprintf(stderr, "\n[ran %llu cycles]\n", (unsigned long long)gb->cycles);
        cart_free(&gb->cart);
        free(gb);
        return 0;
    }

    // TODO: interactive terminal rendering
    fprintf(stderr, "interactive mode not yet implemented; use --test\n");
    cart_free(&gb->cart);
    free(gb);
    return 0;
}