int parse_instruction(int fd_src, t_parser *parser, t_labels **list) { char *line; char **my_tab; line = get_next_instruction(fd_src, parser); while (line != NULL) { my_tab = my_str_to_wordtab(line, SEPARATOR_CHAR); if (parse_label(parser, my_tab, list) == 1) { free_tab(my_tab); free(line); return (1); } free_tab(my_tab); free(line); line = get_next_instruction(fd_src, parser); } return (0); }
bool MIPS32_Cpu::tick() { try { last_pc = pc; execute(get_next_instruction()); } catch(const CpuException &e) { cerr << e.what() << endl; //cerr << "Press Enter to continue..." << endl; //cin.get(); pc = e.pc; } catch(const std::runtime_error &e) { cerr << e.what() << endl; //cerr << "Press Enter to continue..." << endl; //cin.get(); pc = (last_pc & 0xfffffffc) + 4; } return true; }
static void add_program_segment(UM_machine machine, char *file) { struct stat fileStats; FILE *FILE = fopen(file, "r"); /* exit if the file does not exist or if stat fails */ if(stat(file, &fileStats) < 0){ fprintf(stderr, "Stat failed. Invalid file.\n"); exit(EXIT_FAILURE); } int fileSize = fileStats.st_size; /* exit if fileSize indicates the wrong number of bytes */ if(fileSize%4 != 0){ fprintf(stderr, "Program file formatted incorrectly.\n"); exit(EXIT_FAILURE); } int num_instructions = fileSize / BYTES_PER_INSTRUCTION; UM_segment program_segment = get_new_segment_of_size(num_instructions); /* get the instructions from the input file */ for(int i=0; i<num_instructions; i++){ UM_instruction next_instruction = get_next_instruction(FILE); UM_instruction *instruction_i_p = UArray_at(program_segment->words, i); *instruction_i_p = next_instruction; } /* put the program segment in the mapped segments sequence */ Seq_addlo(machine->address_space->mapped_segments, program_segment); machine->address_space->ID_counter++; machine->num_instructions = num_instructions; fclose(FILE); }