TrietPhan_FinalProject.c
// Read and dump initial values for memory
Read and dump initial values for memory
Read and dump initial values for memory
void initialize_memory(int argc, char *argv[], CPU *cpu) {
  FILE *datafile = get_datafile(argc, argv);
#define BUFFER_LEN 80
  char buffer[BUFFER_LEN];
  int words_read;
  Word value_read;
  char *read_success;    
  fgets(buffer, BUFFER_LEN, datafile);
  sscanf(buffer, "%hx ", &value_read);
  int got_origin = 0;
  while(!got_origin){
    if((buffer[0] >= '0' && buffer[0] <= '9') ||
       (buffer[0] >= 'a' && buffer[0] <= 'z') ||
       (buffer[0] >= 'A' && buffer[0] <= 'Z')){
      cpu -> ORIGIN = (Address) value_read;
      got_origin = 1;
    } else {
      fgets(buffer, BUFFER_LEN, datafile);
      sscanf(buffer, "%hx ", &value_read);
    }
  }
  Address loc = cpu -> ORIGIN; 
  read_success = fgets(buffer, BUFFER_LEN, datafile);
  while (read_success != NULL) {
    words_read = sscanf(buffer, "%hx", &value_read);
    if(words_read != 1) {
      read_success = fgets(buffer, BUFFER_LEN, datafile);
      continue;
    }
    else {
      cpu -> mem[loc] = value_read;
      ++loc;
    }
    read_success = fgets(buffer, BUFFER_LEN, datafile);
  }
  while (loc != cpu -> ORIGIN) {
    cpu -> mem[loc] = 0;
    loc++;
  }
}
void initialize_memory ( int argc , char * argv [ ] , CPU * cpu ) { FILE * datafile = get_datafile ( argc , argv ) ; #define BUFFER_LEN  80 char buffer [ BUFFER_LEN ] ; int words_read ; Word value_read ; char * read_success ; fgets ( buffer , BUFFER_LEN , datafile ) ; sscanf ( buffer , " " , & value_read ) ; int got_origin = 0 ; while ( ! got_origin ) { if ( ( buffer [ 0 ] >= ' ' && buffer [ 0 ] <= ' ' ) || ( buffer [ 0 ] >= ' ' && buffer [ 0 ] <= ' ' ) || ( buffer [ 0 ] >= ' ' && buffer [ 0 ] <= ' ' ) ) { cpu -> ORIGIN = ( Address ) value_read ; got_origin = 1 ; } else { fgets ( buffer , BUFFER_LEN , datafile ) ; sscanf ( buffer , " " , & value_read ) ; } } Address loc = cpu -> ORIGIN ; read_success = fgets ( buffer , BUFFER_LEN , datafile ) ; while ( read_success != NULL ) { words_read = sscanf ( buffer , " " , & value_read ) ; if ( words_read != 1 ) { read_success = fgets ( buffer , BUFFER_LEN , datafile ) ; continue ; } else { cpu -> mem [ loc ] = value_read ; ++ loc ; } read_success = fgets ( buffer , BUFFER_LEN , datafile ) ; } while ( loc != cpu -> ORIGIN ) { cpu -> mem [ loc ] = 0 ; loc ++ ; } }
Read and dump initial values for memory
Read and dump initial values for memory
// Buffer to read next line of text into // NULL if reading in a line fails. // Getting the origin // Checking if the file start with the number or char, set the ORIGIN // If not, read the next line // Reading the file // Start from the origin