#include #include #include #include "mem.h" #include "stack.h" #include "disasm.h" #include "debug.h" uint32_t load_program(const char* path) { FILE *fp = fopen(path,"r"); fseek(fp, 0L, SEEK_END); long file_size = ftell(fp); rewind(fp); fread(mem,1,(size_t)file_size,fp); fclose(fp); return file_size/2; } void initializevm(char *program) { initialize_stack(); memset(regs,0x00,sizeof(uint16_t)*NUM_REGISTERS); memset(mem,0x00,sizeof(uint16_t)*MEMSIZE); load_program(program); } void startvm(char *program) { initializevm(program); start_execution(); free_stack(); } void disasm(char *program) { stderr = stdout; disassemble_program(load_program(program)); } void debug(char *program) { initializevm(program); debug_program(); free_stack(); } int main(int argc, char** argv) { uint8_t debug_flag = 0; uint8_t disasm_flag = 0; uint8_t err = 0; char c; char *program; while ((c = getopt(argc, argv, "s:d:")) != -1) { switch(c) { case 'd': debug_flag = 1; program = optarg; break; case 's': disasm_flag = 1; program = optarg; break; case '?': err = 1; break; } } if(err) { fprintf(stderr,"Error parsing arguments\n"); return 1; } else if(debug_flag && disasm_flag) { fprintf(stderr,"-d and -s are mutually exclusive\n"); return 1; } else if(!(debug_flag || disasm_flag)) { if(argc != 2) { fprintf(stderr,"No program specified\n"); return 1; } startvm(argv[1]); } else if(debug_flag) { debug(program); } else if(disasm_flag) { disasm(program); } return 0; }