Example #1
0
int main(int argc, char **argv)
{
	char *base, *params, *key;
	struct mips_cpu *pcpu;
	Elf32_Sym *s_params, *s_time;
	unsigned long long TIME;
	
	if((argc != 3) && (argc != 4)) {
		fprintf(stderr, "USAGE: %s ELF PARAMS [KEY]\n", argv[0]);
		exit(1);
	}
	params = argv[2];
	key = argc == 4 ? argv[3] : NULL;

	if(!(base = malloc(MEMSZ))) {
		perror("malloc");
		exit(1);
	}

	mips_init();
	pcpu = mips_init_cpu(base, MEMSZ, STKSZ);
	prepare_cpu(pcpu, argv[1], key);

	if(!(s_params = mips_elf_find_symbol(pcpu, "PARAMS")) ||
	   !(s_time = mips_elf_find_symbol(pcpu, "TIME"))) {
		fprintf(stderr, "ERROR: 'PARAMS' and/or 'TIME' symbols not found\n");
		exit(1);
	}

	if((s_params->st_size % 4) || !s_params->st_size) {
		fprintf(stderr, "ERROR: 'PARAMS' has invalid size %u\n", s_params->st_size);
		exit(1);
	}

	if(s_time->st_size != 8) {
		fprintf(stderr, "ERROR: 'TIME' has size %u != 8\n", s_time->st_size);
		exit(1);
	}

	parse_params(pcpu, s_params, params);
	execute_loop(pcpu);

	TIME = mips_peek_uw(pcpu, s_time->st_value+4); /* high word */
	TIME = (TIME << 32) | mips_peek_uw(pcpu, s_time->st_value); /* low word */
	printf("%llu\n", TIME);

    return 0;
}
// sudo apt-get install libncurses5-dev
// http://stackoverflow.com/questions/4025891/create-a-function-to-check-for-key-press-in-unix-using-ncurses
// https://viget.com/extend/game-programming-in-c-with-the-ncurses-library
int main(int argc, char **argv) {
    char *debug_filename = NULL;
    char *input_filename = NULL;
    int c;
    opterr = 0;
    while ((c = getopt(argc, argv, "d:p:")) != -1) {
        switch (c) {
            case 'd':
                debug_filename = optarg;
                break;
            case 'p':
                input_filename = optarg;
                break;
            case '?':
                fprintf(stderr, "Unknown option: %c\n", optopt);
                print_usage();
                return 1;
            default:
                print_usage();
                return 1;
        }
    }

    if (optind < argc || input_filename == NULL || (ends_with(input_filename, required_input_ext, 0) != 1)) {
        print_usage();
        return 1;
    }

    FILE *input_file = fopen(input_filename, "rb");
    if (!input_file) {
        fprintf(stderr, "Fatal error when opening input file: '%s'\n", argv[1]);
        exit(1);
    }
    FILE *debug_file = NULL;
    if (debug_filename) {
        debug_file = fopen(debug_filename, "w");
    }
    chip_8_cpu cpu = initialize_cpu();
    initialize_memory(cpu, input_file);
    fclose(input_file);
    execute_loop(cpu, debug_file);
    free_cpu(cpu);

    return EXIT_SUCCESS;
}