1
0
Fork 0
mirror of https://github.com/pnx/m16vm synced 2026-06-16 03:44:55 +02:00
m16vm/vm/vm.c

98 lines
2.1 KiB
C

/* vm.c
*
* Copyright (C) 2012,2014,2018-2019 Henrik Hautakoski <henrik.hautakoski@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#include <stdio.h>
#include <string.h>
#include "program.h"
#include "mm.h"
#include "cpu.h"
#include "debug.h"
static unsigned debug_mem = 0;
static unsigned debug_reg = 0;
void run(struct program *prog) {
struct cpu_state state;
int rc;
mm_init();
cpu_init(&state);
cpu_instr_load(&state, prog->instr, prog->len);
do {
rc = cpu_tick(&state);
} while(rc == 0);
if (debug_reg)
debug_print_regs(state.reg);
if (debug_mem)
debug_print_memory(mm_base_addr);
mm_exit();
}
int usage(const char* name) {
fprintf(stderr, "usage: %s [ --dmem | --dreg ] <file>\n", name);
return 1;
}
int main(int argc, char **argv) {
struct program prog = { 0 };
const char *filename = NULL;
int i;
if (argc < 2) {
return usage(argv[0]);
}
// Parse options.
for(i = 1; i < argc; i++) {
if (argv[i][0] == '-' && argv[i][1] == '-') {
if (!strcmp(argv[i] + 2, "dmem")) {
debug_mem = 1;
} else if (!strcmp(argv[i] + 2, "dreg")) {
debug_reg = 1;
} else {
fprintf(stderr, "Invalid option '%s'\n", argv[i]);
}
} else {
filename = argv[i];
break;
}
}
if (filename == NULL) {
return usage(argv[0]);
}
if (program_loadfromfile(&prog, filename) < 0)
return 1;
// Execute the program.
run(&prog);
program_free(&prog);
return 0;
}