// Read a simulator command from the keyboard ("h", "?", "d", number, // or empty line) and execute it. Return true if we hit end-of-input // or execute_command told us to quit. Otherwise return false. // int read_execute_command(int reg[], int nreg, int mem[], int memlen) { // Buffer for the command line from the keyboard, plus its size // char *cmd_buffer = NULL; size_t cmd_buffer_len = 0, bytes_read = 0; // Values read using sscanf of command line // int nbr_cycles; char cmd_char; size_t words_read; // number of items read by sscanf call int done = 0; // Should simulator stop? bytes_read = getline(&cmd_buffer, &cmd_buffer_len, stdin); if (bytes_read == -1) { done = 1; // Hit end of file } words_read = sscanf(cmd_buffer, "%d", &nbr_cycles); // If we found a number, do that many instruction cycles. // Otherwise sscanf for a character and call execute_command with it. // (Note the character might be '\n'.) // // printf("%d %d",words_read,nbr_cycles); if (words_read == 1) { many_instruction_cycles(nbr_cycles,reg,nreg,mem,memlen); } else { words_read = sscanf(cmd_buffer,"%c",&cmd_char); execute_command(cmd_char,reg,nreg,mem,memlen); } free(cmd_buffer); return done; }
int read_execute_command(CPU *cpu) { // Buffer to read next command line into #define CMD_LINE_LEN 80 char cmd_line[CMD_LINE_LEN]; char *read_success; // NULL if reading in a line fails. int nbr_cycles; // Number of instruction cycles to execute char cmd_char; // Command 'q', 'h', '?', 'd', or '\n' int done = 0; // Should simulator stop? int words_read; read_success = fgets(cmd_line, CMD_LINE_LEN, stdin); while (read_success != NULL && !done) { words_read = sscanf(cmd_line, "%d", &nbr_cycles); if (words_read==1){ many_instruction_cycles(nbr_cycles, cpu); } else { words_read = sscanf(cmd_line,"%c",&cmd_char); execute_command(cmd_char, cpu); } read_success = fgets(cmd_line, CMD_LINE_LEN, stdin); } return done; }
int read_execute_command(int reg[], int nreg, int mem[], int memlen) { char *cmd_buffer = NULL; size_t cmd_buffer_len = 0, bytes_read = 0; int nbr_cycles; char cmd_char; size_t words_read; int done = 0; /* Get user input */ bytes_read = getline(&cmd_buffer, &cmd_buffer_len, stdin); /* Did we hit the end of file? */ if (bytes_read == -1) { done = 1; } words_read = sscanf(cmd_buffer, "%d", &nbr_cycles); /* If a char was read then try to execute the according * command, else if an integer was entered, try to * execute this number of cycles */ if (words_read == 0) { sscanf(cmd_buffer, "%c", &cmd_char); done = execute_command(cmd_char, reg, nreg, mem, memlen); } else { /* Check first is the number of cycles is invalid. * If yes then execute one or more instructions */ if (nbr_cycles < 1 || nbr_cycles > memlen || nbr_cycles > (memlen - pc)) { printf("%d is an invalid number of cycles!!", nbr_cycles); } else if (nbr_cycles == 1) { one_instruction_cycle(reg, nreg, mem, memlen); } else { many_instruction_cycles(nbr_cycles, reg, nreg, mem, memlen); } } /* Command buffer not needed anymore */ free(cmd_buffer); return done; }