romload.c
/*
  Allocate a buffer and stuff the named ROM inside. If rom_size is non-NULL,
  store the ROM size there.
*/
Allocate a buffer and stuff the named ROM inside. If rom_size is non-NULL,
store the ROM size there.
Allocate a buffer and stuff the named ROM inside . If rom_size is non - NULL store the ROM size there .
uint8_t *load_rom(size_t *rom_size, const char *name)
{
	FILE *file;
	size_t size;
	uint8_t *rom;
	int error;
	void *context = NULL;
	if (name == NULL)
		return NULL;
	file = dgen_fopen(rom_path, name, (DGEN_READ | DGEN_CURRENT));
	if (file == NULL) {
		fprintf(stderr, "%s: can't open ROM file.\n", name);
		return NULL;
	}
retry:
	rom = load(&context, &size, file, (64 * 1024 * 1024));
	error = errno;
	if (rom == NULL) {
		if (error)
			fprintf(stderr, "%s: unable to load ROM: %s.\n", name,
				strerror(error));
		else
			fprintf(stderr, "%s: no valid ROM found.\n",
				name);
		load_finish(&context);
		fclose(file);
		return NULL;
	}
	if (size < 512) {
		unload(rom);
		goto retry;
	}
	if (memcmp(&rom[0x100], "SEGA", 4)) {
		uint8_t *dst = rom;
		uint8_t *src = &rom[0x200];
		size_t chunks = ((size - 0x200) / 0x4000);
		if (((size - 0x200) & (0x4000 - 1)) != 0)
			goto bad_rom;
		size -= 0x200;
		if (((rom[0] != 0x00) && (rom[0] != chunks)) ||
		    (rom[8] != 0xaa) || (rom[9] != 0xbb))
			fprintf(stderr, "%s: corrupt SMD header.\n", name);
		while (chunks) {
			size_t i;
			uint8_t tmp[0x2000];
			memcpy(tmp, src, 0x2000);
			src += 0x2000;
			for (i = 0; (i != 0x2000); ++i) {
				*(dst++) = *(src++);
				*(dst++) = tmp[i];
			}
			--chunks;
		}
		if (memcmp(&rom[0x100], "SEGA", 4)) {
		bad_rom:
			unload(rom);
			goto retry;
		}
	}
	load_finish(&context);
	fclose(file);
	if (rom_size != NULL)
		*rom_size = size;
	return rom;
}
uint8_t * load_rom ( size_t * rom_size , const char * name ) { FILE * file ; size_t size ; uint8_t * rom ; int error ; void * context = NULL ; if ( name == NULL ) return NULL ; file = dgen_fopen ( rom_path , name , ( DGEN_READ | DGEN_CURRENT ) ) ; if ( file == NULL ) { fprintf ( stderr , " \n " , name ) ; return NULL ; } retry : rom = load ( & context , & size , file , ( 64 * 1024 * 1024 ) ) ; error = errno ; if ( rom == NULL ) { if ( error ) fprintf ( stderr , " \n " , name , strerror ( error ) ) ; else fprintf ( stderr , " \n " , name ) ; load_finish ( & context ) ; fclose ( file ) ; return NULL ; } if ( size < 512 ) { unload ( rom ) ; goto retry ; } if ( memcmp ( & rom [ 0x100 ] , " " , 4 ) ) { uint8_t * dst = rom ; uint8_t * src = & rom [ 0x200 ] ; size_t chunks = ( ( size - 0x200 ) / 0x4000 ) ; if ( ( ( size - 0x200 ) & ( 0x4000 - 1 ) ) != 0 ) goto bad_rom ; size -= 0x200 ; if ( ( ( rom [ 0 ] != 0x00 ) && ( rom [ 0 ] != chunks ) ) || ( rom [ 8 ] != 0xaa ) || ( rom [ 9 ] != 0xbb ) ) fprintf ( stderr , " \n " , name ) ; while ( chunks ) { size_t i ; uint8_t tmp [ 0x2000 ] ; memcpy ( tmp , src , 0x2000 ) ; src += 0x2000 ; for ( i = 0 ; ( i != 0x2000 ) ; ++ i ) { * ( dst ++ ) = * ( src ++ ) ; * ( dst ++ ) = tmp [ i ] ; } -- chunks ; } if ( memcmp ( & rom [ 0x100 ] , " " , 4 ) ) { bad_rom : unload ( rom ) ; goto retry ; } } load_finish ( & context ) ; fclose ( file ) ; if ( rom_size != NULL ) * rom_size = size ; return rom ; }
Allocate a buffer and stuff the named ROM inside.
Allocate a buffer and stuff the named ROM inside .
/* A valid ROM will surely not be bigger than 64MB. */ /* ROM file too small */ /*
	  If "SEGA" isn't found at 0x100 and the total size minus 512 is a
	  multiple of 0x4000, it probably is a SMD.
	*/ /* Corrupt ROM? Complain and continue anyway. */ /*
		  De-interleave ROM, overwrite SMD header with the result.
		*/ /* Does it look like a valid ROM now? */ /* Invalid ROM */