示例#1
0
int RomLoad(const char *RomName)
{
  FILE *file=NULL;
  const char *name=NULL;
  int nameLen=0;
  int fileLen=0,space=0;

  name = RomName;

  file=fopen(name,"rb"); if (file==NULL) return 1;

  nameLen=strlen(name);

  {
    // Find out the length of the file:
    fseek(file,0,SEEK_END); fileLen=ftell(file);
    fseek(file,0,SEEK_SET);
  }

  // Allocate space for it:
  space=(fileLen+0x3fff)&~0x3fff;

  RomData=(unsigned char *)malloc(space);
  if (RomData==NULL) { fclose(file); return 1; }
  memset(RomData,0,space);

  // Read in file:
  fread(RomData,1,fileLen,file);

  fclose(file);

  RomLen=fileLen;

  // Check for SMD:
  if ((fileLen&0x3fff)==0x200)
  {
    // Decode and byteswap:
    DecodeSmd(RomData,RomLen);
    RomLen-=0x200;
  }
  else
  {
    // Just byteswap:
    _Byteswap(RomData,RomLen);
  }

  PicoCartInsert(RomData,RomLen);
  return 0;
}
示例#2
0
int emu_ReloadRom(void)
{
	unsigned int rom_size = 0;
	char *used_rom_name = romFileName;
	char ext[5];
	pm_file *rom;
	int ret, cd_state, cd_region, cfg_loaded = 0;

	lprintf("emu_ReloadRom(%s)\n", romFileName);

	get_ext(romFileName, ext);

	// detect wrong extensions
	if(!strcmp(ext, ".srm") || !strcmp(ext, "s.gz") || !strcmp(ext, ".mds")) { // s.gz ~ .mds.gz
		sprintf(menuErrorMsg, "Not a ROM selected.");
		return 0;
	}

	PicoPatchUnload();

	// check for movie file
	if(movie_data) {
		free(movie_data);
		movie_data = 0;
	}
#if 0
	if(!strcmp(ext, ".gmv")) {
		// check for both gmv and rom
		int dummy;
		FILE *movie_file = fopen(romFileName, "rb");
		if(!movie_file) {
			sprintf(menuErrorMsg, "Failed to open movie.");
			return 0;
		}
		fseek(movie_file, 0, SEEK_END);
		movie_size = ftell(movie_file);
		fseek(movie_file, 0, SEEK_SET);
		if(movie_size < 64+3) {
			sprintf(menuErrorMsg, "Invalid GMV file.");
			fclose(movie_file);
			return 0;
		}
		movie_data = malloc(movie_size);
		if(movie_data == NULL) {
			sprintf(menuErrorMsg, "low memory.");
			fclose(movie_file);
			return 0;
		}
		fread(movie_data, 1, movie_size, movie_file);
		fclose(movie_file);
		if (strncmp((char *)movie_data, "Gens Movie TEST", 15) != 0) {
			sprintf(menuErrorMsg, "Invalid GMV file.");
			return 0;
		}
		dummy = try_rfn_cut() || try_rfn_cut();
		if (!dummy) {
			sprintf(menuErrorMsg, "Could't find a ROM for movie.");
			return 0;
		}
		get_ext(romFileName, ext);
	} else 

	if (!strcmp(ext, ".pat")) {
		int dummy;
		PicoPatchLoad(romFileName);
		dummy = try_rfn_cut() || try_rfn_cut();
		if (!dummy) {
			sprintf(menuErrorMsg, "Could't find a ROM to patch.");
			return 0;
		}
		get_ext(romFileName, ext);
	}
#else
	// load GG patches list
	char ggFname[512];
	romfname_ext(ggFname, NULL, ".cht");
	PicoPatchLoad(ggFname);
#endif

	if ((PicoMCD & 1) && Pico_mcd != NULL) Stop_CD();

	// check for MegaCD image
	cd_state = emu_cdCheck(&cd_region);
	if (cd_state > 0)
	{
		// valid CD image, check for BIOS..
#if 0
		// we need to have config loaded at this point
		ret = emu_ReadConfig(READ_CONFIG_FOR_GAME, READ_CONFIG_SKIP_DEF);
		if (!ret) emu_ReadConfig(READ_CONFIG_GLOBAL, READ_CONFIG_SKIP_DEF);
#endif
		cfg_loaded = 1;

		if (PicoRegionOverride) {
			cd_region = PicoRegionOverride;
			lprintf("overrided region to %s\n", cd_region != 4 ? (cd_region == 8 ? "EU" : "JAP") : "USA");
		}
		if (!emu_findBios(cd_region, &used_rom_name)) {
			// bios_help() ?
			return 0;
		}

		PicoMCD |= 1;
		get_ext(used_rom_name, ext);
	} else
	{
		if (PicoMCD & 1) Stop_CD();
		PicoMCD &= ~1;
	}

	ips_define(ipsFileName);
	rom = pm_open(used_rom_name);
	if(!rom) {
		sprintf(menuErrorMsg, "Failed to open rom.");
		return 0;
	}

	menu_romload_prepare(used_rom_name); // also CD load

	if(rom_data) {
		free(rom_data);
		rom_data = 0;
		rom_size = 0;
	}

	if( (ret = PicoCartLoad(rom, &rom_data, &rom_size)) ) {
		sprintf(menuErrorMsg, "PicoCartLoad() failed.");
		lprintf("%s\n", menuErrorMsg);
		pm_close(rom);
		menu_romload_end();
		return 0;
	}
	pm_close(rom);

	// detect wrong files (Pico crashes on very small files), also see if ROM EP is good
	if(rom_size <= 0x200 || strncmp((char *)rom_data, "Pico", 4) == 0 ||
	  ((*(unsigned char *)(rom_data+4)<<16)|(*(unsigned short *)(rom_data+6))) >= (int)rom_size) {
		if (rom_data) free(rom_data);
		rom_data = 0;
		sprintf(menuErrorMsg, "Not a ROM selected.");
		menu_romload_end();
		return 0;
	}
#if 0
	// load config for this ROM (do this before insert to get correct region)
	if (!cfg_loaded) {
		ret = emu_ReadConfig(READ_CONFIG_FOR_GAME, READ_CONFIG_SKIP_DEF);
		if (!ret) emu_ReadConfig(READ_CONFIG_GLOBAL, READ_CONFIG_SKIP_DEF);
	}
#endif
	lprintf("PicoCartInsert(%p, %d);\n", rom_data, rom_size);
	if(PicoCartInsert(rom_data, rom_size)) {
		sprintf(menuErrorMsg, "Failed to load ROM.");
		menu_romload_end();
		return 0;
	}

	Pico.m.frame_count = 0;

	// insert CD if it was detected
	if (cd_state > 0) {
		ret = Insert_CD(romFileName, cd_state == 2);
		if (ret != 0) {
			sprintf(menuErrorMsg, "Insert_CD() failed, invalid CD image?");
			lprintf("%s\n", menuErrorMsg);
			menu_romload_end();
			return 0;
		}
	}

	menu_romload_end();
#if 0
	if (!emu_isBios(romFileName))
	{
		// emu_ReadConfig() might have messed currentConfig.lastRomFile
		strncpy(currentConfig.lastRomFile, romFileName, sizeof(currentConfig.lastRomFile)-1);
		currentConfig.lastRomFile[sizeof(currentConfig.lastRomFile)-1] = 0;
	}
#endif
	if (PicoPatches) {
		PicoPatchPrepare();
		PicoPatchApply();
	}

	// additional movie stuff
	if (movie_data) {
		if(movie_data[0x14] == '6')
		     PicoOpt |=  PicoOpt_6button_gamepad; // 6 button pad
		else PicoOpt &= ~PicoOpt_6button_gamepad;
		PicoOpt |= (PicoOpt_accurate_timing | PicoOpt_disable_vdp_fifo); // accurate timing, no VDP fifo timing
		if(movie_data[0xF] >= 'A') {
			if(movie_data[0x16] & 0x80) {
				PicoRegionOverride = 8;
			} else {
				PicoRegionOverride = 4;
			}
			PicoReset(0);
			// TODO: bits 6 & 5
		}
		movie_data[0x18+30] = 0;
		sprintf(noticeMsg, "MOVIE: %s", (char *) &movie_data[0x18]);
	}
	else
	{
		PicoOpt &= ~PicoOpt_disable_vdp_fifo;
		if(Pico.m.pal) {
			strcpy(noticeMsg, "PAL SYSTEM / 50 FPS");
		} else {
			strcpy(noticeMsg, "NTSC SYSTEM / 60 FPS");
		}
	}
	emu_noticeMsgUpdated();

	// load SRAM for this ROM
	emu_SaveLoadSRAM(1);

	return 1;
}
示例#3
0
enum media_type_e PicoLoadMedia(const char *filename,
  const char *carthw_cfg_fname,
  const char *(*get_bios_filename)(int *region, const char *cd_fname),
  void (*do_region_override)(const char *media_filename))
{
  const char *rom_fname = filename;
  enum media_type_e media_type;
  enum cd_img_type cd_img_type = CIT_NOT_CD;
  unsigned char *rom_data = NULL;
  unsigned int rom_size = 0;
  pm_file *rom = NULL;
  int cd_region = 0;
  int ret;

  media_type = detect_media(filename);
  if (media_type == PM_BAD_DETECT)
    goto out;

  if ((PicoAHW & PAHW_MCD) && Pico_mcd != NULL)
    cdd_unload();
  PicoCartUnload();
  PicoAHW = 0;
  PicoQuirks = 0;

  if (media_type == PM_CD)
  {
    // check for MegaCD image
    cd_img_type = PicoCdCheck(filename, &cd_region);
    if ((int)cd_img_type >= 0 && cd_img_type != CIT_NOT_CD)
    {
      // valid CD image, ask frontend for BIOS..
      rom_fname = NULL;
      if (get_bios_filename != NULL)
        rom_fname = get_bios_filename(&cd_region, filename);
      if (rom_fname == NULL) {
        media_type = PM_BAD_CD_NO_BIOS;
        goto out;
      }

      PicoAHW |= PAHW_MCD;
    }
    else {
      media_type = PM_BAD_CD;
      goto out;
    }
  }
  else if (media_type == PM_MARK3) {
    lprintf("detected SMS ROM\n");
    PicoAHW = PAHW_SMS;
  }

  rom = pm_open(rom_fname);
  if (rom == NULL) {
    lprintf("Failed to open ROM\n");
    media_type = PM_ERROR;
    goto out;
  }

  ret = PicoCartLoad(rom, &rom_data, &rom_size, (PicoAHW & PAHW_SMS) ? 1 : 0);
  pm_close(rom);
  if (ret != 0) {
    if      (ret == 2) lprintf("Out of memory\n");
    else if (ret == 3) lprintf("Read failed\n");
    else               lprintf("PicoCartLoad() failed.\n");
    media_type = PM_ERROR;
    goto out;
  }

  // detect wrong files
  if (strncmp((char *)rom_data, "Pico", 4) == 0) {
    lprintf("savestate selected?\n");
    media_type = PM_BAD_DETECT;
    goto out;
  }

  if (!(PicoAHW & PAHW_SMS)) {
    unsigned short *d = (unsigned short *)(rom_data + 4);
    if ((((d[0] << 16) | d[1]) & 0xffffff) >= (int)rom_size) {
      lprintf("bad reset vector\n");
      media_type = PM_BAD_DETECT;
      goto out;
    }
  }

  // load config for this ROM (do this before insert to get correct region)
  if (!(PicoAHW & PAHW_MCD)) {
    memcpy(media_id_header, rom_data + 0x100, sizeof(media_id_header));
    if (do_region_override != NULL)
      do_region_override(filename);
  }

  if (PicoCartInsert(rom_data, rom_size, carthw_cfg_fname)) {
    media_type = PM_ERROR;
    goto out;
  }
  rom_data = NULL; // now belongs to PicoCart
  Pico.m.ncart_in = 0;

  // insert CD if it was detected
  if (cd_img_type != CIT_NOT_CD) {
    ret = cdd_load(filename, cd_img_type);
    if (ret != 0) {
      PicoCartUnload();
      media_type = PM_BAD_CD;
      goto out;
    }
    Pico.m.ncart_in = 1;
  }

  if (PicoQuirks & PQUIRK_FORCE_6BTN)
    PicoSetInputDevice(0, PICO_INPUT_PAD_6BTN);

out:
  if (rom_data)
    free(rom_data);
  return media_type;
}
示例#4
0
void CPicoServSession::loadROM()
{
	TInt res;

	const TAny* pD=Message().Ptr0();

	// TInt desLen=Message().Client().GetDesLength(pD);

	if(rom_data) {
		// save SRAM for previous ROM
		if(currentConfig.iFlags & 1)
			saveLoadGame(0, 1);
	}

	RomFileName = 0;
	if(rom_data) {
		free(rom_data);
		rom_data = 0;
	}

	// read the contents of the client pointer into a TPtr.
	static TBuf8<KMaxFileName> writeBuf;
	TRAP(res,Message().ReadL(pD,writeBuf));
	if (res!=KErrNone) {
		PanicClient(EBadDescriptor);
		return;
	}

	// detect wrong extensions (.srm and .mds)
	TBuf8<5> ext;
	ext.Copy(writeBuf.Right(4));
	ext.LowerCase();
	if(!strcmp((char *)ext.PtrZ(), ".srm") || !strcmp((char *)ext.PtrZ(), "s.gz") || // .mds.gz
	   !strcmp((char *)ext.PtrZ(), ".mds")) {
		User::Leave(3);
		return;
	}

	FILE *rom = fopen((char *) writeBuf.PtrZ(), "rb");
	if(!rom) {
		DEBUGPRINT(_L("failed to open rom."));
		User::Leave(1);
		return;
	}


	unsigned int rom_size = 0;
	// zipfile support
	if(!strcmp((char *)ext.PtrZ(), ".zip")) {
		fclose(rom);
		res = CartLoadZip((const char *) writeBuf.PtrZ(), &rom_data, &rom_size);
		if(res) {
			User::Leave(res);
			return;
		}
	} else {
		if( (res = PicoCartLoad(rom, &rom_data, &rom_size)) ) {
			DEBUGPRINT(_L("PicoCartLoad() failed."));
			fclose(rom);
			User::Leave(2);
			return;
		}
		fclose(rom);
	}

	// detect wrong files (Pico crashes on very small files), also see if ROM EP is good
	if(rom_size <= 0x200 || strncmp((char *)rom_data, "Pico", 4) == 0 ||
	  ((*(TUint16 *)(rom_data+4)<<16)|(*(TUint16 *)(rom_data+6))) >= (int)rom_size) {
		free(rom_data);
		rom_data = 0;
		User::Leave(3); // not a ROM
	}

	DEBUGPRINT(_L("PicoCartInsert(0x%08X, %d);"), rom_data, rom_size);
	if(PicoCartInsert(rom_data, rom_size)) {
		User::Leave(2);
		return;
	}

	pico_was_reset = 1;

	// global ROM file name for later use
	RomFileName = (const char *) writeBuf.PtrZ();

	// load SRAM for this ROM
	if(currentConfig.iFlags & 1)
		saveLoadGame(1, 1);

	// debug
	#ifdef __DEBUG_PRINT
	TInt cells = User::CountAllocCells();
	TInt mem;
	User::AllocSize(mem);
	DEBUGPRINT(_L("comm:   cels=%d, size=%d KB"), cells, mem/1024);
	gamestate = PGS_DebugHeap;
	gamestate_prev = PGS_Running;
	#else
	gamestate = PGS_Running;
	#endif
}