src/memory/header.cc
/*
 * Takes in a rom file and returns the corresponding header structure.
 *
 * Assumes the rom file is open and valid.
 *
 * Returns a header structure on success and NULL otherwise.
 * Fails if the header is invalid.
 */
Takes in a rom file and returns the corresponding header structure.
Assumes the rom file is open and valid.

Returns a header structure on success and NULL otherwise.
Fails if the header is invalid.
Takes in a rom file and returns the corresponding header structure . Assumes the rom file is open and valid . Returns a header structure on success and NULL otherwise . Fails if the header is invalid .
RomHeader *DecodeHeader(FILE *rom_file) {
  fseek(rom_file, 0, SEEK_SET);
  char *file_header = new char[HEADER_SIZE];
  for (size_t i = 0; i < HEADER_SIZE; i++) { file_header[i] = fgetc(rom_file); }
  size_t rom_size = GetFileSize(rom_file);
  if (strncmp(file_header, kInesPreface, PREFACE_SIZE)) {
    fprintf(stderr, "Error: the provided file is not an NES file\n");
    return NULL;
  }
  RomHeader *header = new RomHeader();
  header->header_type = GetHeaderType(file_header, rom_size);
  switch (header->header_type) {
    case ARCHAIC_INES:
      fprintf(stderr, "Warning: Provided rom has an Archaic INES header\n");
      DecodeArchaicInes(header, file_header);
      break;
    case INES:
      DecodeInes(header, file_header);
      break;
    case NES2:
      DecodeNes2(header, file_header);
      break;
  }
  delete[] file_header;
  CONTRACT((header->prg_rom_size + header->chr_rom_size + 16) == rom_size);
  return header;
}
RomHeader * DecodeHeader ( FILE * rom_file ) { fseek ( rom_file , 0 , SEEK_SET ) ; char * file_header = new char [ HEADER_SIZE ] ; for ( size_t i = 0 ; i < HEADER_SIZE ; i ++ ) { file_header [ i ] = fgetc ( rom_file ) ; } size_t rom_size = GetFileSize ( rom_file ) ; if ( strncmp ( file_header , kInesPreface , PREFACE_SIZE ) ) { fprintf ( stderr , " \n " ) ; return NULL ; } RomHeader * header = new RomHeader ( ) ; header -> header_type = GetHeaderType ( file_header , rom_size ) ; switch ( header -> header_type ) { case ARCHAIC_INES : fprintf ( stderr , " \n " ) ; DecodeArchaicInes ( header , file_header ) ; break ; case INES : DecodeInes ( header , file_header ) ; break ; case NES2 : DecodeNes2 ( header , file_header ) ; break ; } delete [ ] file_header ; CONTRACT ( ( header -> prg_rom_size + header -> chr_rom_size + 16 ) == rom_size ) ; return header ; }
Takes in a rom file and returns the corresponding header structure.
Takes in a rom file and returns the corresponding header structure .
// Read the header in from the file. // Calculate the size of the rom file. // Verify that the header is correct. // Allocate the header structure and determine the header type. // Decode the header according to its type. // Clean up and exit