Beispiel #1
0
int gbcLoadState(const char* filename)
{
    loadstate(filename);

    vram_dirty();
    pal_dirty();
    sound_dirty();
    mem_updatemap();

    return 1;
}
Beispiel #2
0
int gbcLoadStateFromBuffer(const signed char * data)
{
	loadstate_from_buffer(data);

    vram_dirty();
    pal_dirty();
    sound_dirty();
    mem_updatemap();

    return 1;
}
Beispiel #3
0
void state_load(int n)
{
	FILE *f;
	char *name;

	if (n < 0) n = saveslot;
	if (n < 0) n = 0;
	name = malloc(strlen(saveprefix) + 5);
	sprintf(name, "%s.%03d", saveprefix, n);

	if ((f = fopen(name, "rb")))
	{
		loadstate(f);
		fclose(f);
		vram_dirty();
		pal_dirty();
		sound_dirty();
		mem_updatemap();
	}
	free(name);
}
Beispiel #4
0
void loadstate(int fd)
{
    int i, j;
    byte buf[4096];
    un32 (*header)[2] = (un32 (*)[2])buf;
    un32 d;
    int irl = hw.cgb ? 8 : 2;
    int vrl = hw.cgb ? 4 : 2;
    int srl = mbc.ramsize << 1;
    size_t base_offset;

    ver = hramofs = hiofs = palofs = oamofs = wavofs = 0;

    base_offset = lseek(fd, 0, SEEK_CUR);
  
    read(fd,buf, 4096);
    
    for (j = 0; header[j][0]; j++)
    {
        for (i = 0; svars[i].ptr; i++)
        {
            if (header[j][0] != svars[i].k.key)
                continue;
            d = LIL(header[j][1]);
            switch (svars[i].len)
            {
            case 1:
                *(byte *)svars[i].ptr = d;
                break;
            case 2:
                *(un16 *)svars[i].ptr = d;
                break;
            case 4:
                *(un32 *)svars[i].ptr = d;
                break;
            }
            break;
        }
    }

    /* obsolete as of version 0x104 */
    if (hramofs) memcpy(ram.hi+128, buf+hramofs, 127);
    if (wavofs) memcpy(ram.hi+48, buf+wavofs, 16);
    
    if (hiofs) memcpy(ram.hi, buf+hiofs, sizeof ram.hi);
    if (palofs) memcpy(lcd.pal, buf+palofs, sizeof lcd.pal);
    if (oamofs) memcpy(lcd.oam.mem, buf+oamofs, sizeof lcd.oam);

    lseek(fd, base_offset + (iramblock << 12), SEEK_SET);
    read(fd,ram.ibank, 4096*irl);
    
    lseek(fd, base_offset + (vramblock << 12), SEEK_SET);
    read(fd,lcd.vbank, 4096*vrl);
    
    lseek(fd, base_offset + (sramblock << 12), SEEK_SET);
    read(fd,ram.sbank, 4096*srl);
    vram_dirty();
    pal_dirty();
    sound_dirty();
    mem_updatemap();            
}
Beispiel #5
0
int menu_controls(){

	int ret=0, i=0, btna=2, btnb=1, btnx=2, btny=1, btnl=3, btnr=4;

	FILE *file;

	btna = rc_getint("button_a");
	btnb = rc_getint("button_b");
	btnx = rc_getint("button_x");
	btny = rc_getint("button_y");
	btnl = rc_getint("button_l");
	btnr = rc_getint("button_r");

	start:

	dialog_begin("Controls",NULL);

	#if defined(CAANOO)
	dialog_text("Caanoo","Gameboy",0);                       /* 1 */
	#endif
	#if defined(WIZ)
	dialog_text("Wiz","Gameboy",0);                          /* 1 */
	#endif
	#if defined(DINGOO_NATIVE)
	dialog_text("Dingoo","Gameboy",0);                       /* 1 */
	#endif
	#if defined(GP2X_ONLY)
	dialog_text("GP2X","Gameboy",0);                         /* 1 */
	#endif
	dialog_text(NULL,NULL,0);                                /* 2 */
	dialog_option("Button A",lbutton_a,&btna);               /* 3 */
	dialog_option("Button B",lbutton_b,&btnb);               /* 4 */
	dialog_option("Button X",lbutton_x,&btnx);               /* 5 */
	dialog_option("Button Y",lbutton_y,&btny);               /* 6 */
	dialog_option("Button L",lbutton_l,&btnl);               /* 7 */
	dialog_option("Button R",lbutton_r,&btnr);               /* 8 */
	dialog_text(NULL,NULL,0);                                /* 9 */
	dialog_text("Apply",NULL,FIELD_SELECTABLE);              /* 10 */
	dialog_text("Apply & Save",NULL,FIELD_SELECTABLE);       /* 11 */
	dialog_text("Cancel",NULL,FIELD_SELECTABLE);             /* 12 */

	switch(ret=dialog_end()){
		case 12: /* Cancel */
			return ret;
			break;
		case 10: /* Apply */
		case 11: /* Apply & Save */
		    sprintf(config[0],"#KEY BINDINGS#");
			sprintf(config[1],"set button_a %i",btna);
			sprintf(config[2],"set button_b %i",btnb);
			sprintf(config[3],"set button_x %i",btnx);
			sprintf(config[4],"set button_y %i",btny);
			sprintf(config[5],"set button_l %i",btnl);
			sprintf(config[6],"set button_r %i",btnr);
			#if defined(CAANOO)
			    if (btna == 0) {sprintf(config[7],"unbind joy0");}
			    if (btnb == 0) {sprintf(config[8],"unbind joy2");}
			    if (btnx == 0) {sprintf(config[9],"unbind joy1");}
			    if (btny == 0) {sprintf(config[10],"unbind joy3");}
			    if (btnl == 0) {sprintf(config[11],"unbind joy4");}
			    if (btnr == 0) {sprintf(config[12],"unbind joy5");}

			    if (btna == 1) {sprintf(config[7],"bind joy0 +a");}
			    if (btnb == 1) {sprintf(config[8],"bind joy2 +a");}
			    if (btnx == 1) {sprintf(config[9],"bind joy1 +a");}
			    if (btny == 1) {sprintf(config[10],"bind joy3 +a");}
			    if (btnl == 1) {sprintf(config[11],"bind joy4 +a");}
			    if (btnr == 1) {sprintf(config[12],"bind joy5 +a");}

			    if (btna == 2) {sprintf(config[7],"bind joy0 +b");}
			    if (btnb == 2) {sprintf(config[8],"bind joy2 +b");}
			    if (btnx == 2) {sprintf(config[9],"bind joy1 +b");}
			    if (btny == 2) {sprintf(config[10],"bind joy3 +b");}
			    if (btnl == 2) {sprintf(config[11],"bind joy4 +b");}
			    if (btnr == 2) {sprintf(config[12],"bind joy5 +b");}

			    if (btna == 3) {sprintf(config[7],"bind joy0 +select");}
			    if (btnb == 3) {sprintf(config[8],"bind joy2 +select");}
			    if (btnx == 3) {sprintf(config[9],"bind joy1 +select");}
			    if (btny == 3) {sprintf(config[10],"bind joy3 +select");}
			    if (btnl == 3) {sprintf(config[11],"bind joy4 +select");}
			    if (btnr == 3) {sprintf(config[12],"bind joy5 +select");}

			    if (btna == 4) {sprintf(config[7],"bind joy0 +start");}
			    if (btnb == 4) {sprintf(config[8],"bind joy2 +start");}
			    if (btnx == 4) {sprintf(config[9],"bind joy1 +start");}
			    if (btny == 4) {sprintf(config[10],"bind joy3 +start");}
			    if (btnl == 4) {sprintf(config[11],"bind joy4 +start");}
			    if (btnr == 4) {sprintf(config[12],"bind joy5 +start");}

			    if (btna == 5) {sprintf(config[7],"bind joy0 reset");}
			    if (btnb == 5) {sprintf(config[8],"bind joy2 reset");}
			    if (btnx == 5) {sprintf(config[9],"bind joy1 reset");}
			    if (btny == 5) {sprintf(config[10],"bind joy3 reset");}
			    if (btnl == 5) {sprintf(config[11],"bind joy4 reset");}
			    if (btnr == 5) {sprintf(config[12],"bind joy5 reset");}

			    if (btna == 6) {sprintf(config[7],"bind joy0 quit");}
			    if (btnb == 6) {sprintf(config[8],"bind joy2 quit");}
			    if (btnx == 6) {sprintf(config[9],"bind joy1 quit");}
			    if (btny == 6) {sprintf(config[10],"bind joy3 quit");}
			    if (btnl == 6) {sprintf(config[11],"bind joy4 quit");}
			    if (btnr == 6) {sprintf(config[12],"bind joy5 quit");}
			#endif
			#if defined(DINGOO_NATIVE)
			    if (btna == 0) {sprintf(config[7],"unbind ctrl");}
			    if (btnb == 0) {sprintf(config[8],"unbind alt");}
			    if (btnx == 0) {sprintf(config[9],"unbind space");}
			    if (btny == 0) {sprintf(config[10],"unbind shift");}
			    if (btnl == 0) {sprintf(config[11],"unbind tab");}
			    if (btnr == 0) {sprintf(config[12],"unbind backspace");}

			    if (btna == 1) {sprintf(config[7],"bind ctrl +a");}
			    if (btnb == 1) {sprintf(config[8],"bind alt +a");}
			    if (btnx == 1) {sprintf(config[9],"bind space +a");}
			    if (btny == 1) {sprintf(config[10],"bind shift +a");}
			    if (btnl == 1) {sprintf(config[11],"bind tab +a");}
			    if (btnr == 1) {sprintf(config[12],"bind backspace +a");}

			    if (btna == 2) {sprintf(config[7],"bind ctrl +b");}
			    if (btnb == 2) {sprintf(config[8],"bind alt +b");}
			    if (btnx == 2) {sprintf(config[9],"bind space +b");}
			    if (btny == 2) {sprintf(config[10],"bind shift +b");}
			    if (btnl == 2) {sprintf(config[11],"bind tab +b");}
			    if (btnr == 2) {sprintf(config[12],"bind backspace +b");}

			    if (btna == 3) {sprintf(config[7],"bind ctrl +select");}
			    if (btnb == 3) {sprintf(config[8],"bind alt +select");}
			    if (btnx == 3) {sprintf(config[9],"bind space +select");}
			    if (btny == 3) {sprintf(config[10],"bind shift +select");}
			    if (btnl == 3) {sprintf(config[11],"bind tab +select");}
			    if (btnr == 3) {sprintf(config[12],"bind backspace +select");}

			    if (btna == 4) {sprintf(config[7],"bind ctrl +start");}
			    if (btnb == 4) {sprintf(config[8],"bind alt +start");}
			    if (btnx == 4) {sprintf(config[9],"bind space +start");}
			    if (btny == 4) {sprintf(config[10],"bind shift +start");}
			    if (btnl == 4) {sprintf(config[11],"bind tab +start");}
			    if (btnr == 4) {sprintf(config[12],"bind backspace +start");}

			    if (btna == 5) {sprintf(config[7],"bind ctrl reset");}
			    if (btnb == 5) {sprintf(config[8],"bind alt reset");}
			    if (btnx == 5) {sprintf(config[9],"bind space reset");}
			    if (btny == 5) {sprintf(config[10],"bind shift reset");}
			    if (btnl == 5) {sprintf(config[11],"bind tab reset");}
			    if (btnr == 5) {sprintf(config[12],"bind backspace reset");}

			    if (btna == 6) {sprintf(config[7],"bind ctrl quit");}
			    if (btnb == 6) {sprintf(config[8],"bind alt quit");}
			    if (btnx == 6) {sprintf(config[9],"bind space quit");}
			    if (btny == 6) {sprintf(config[10],"bind shift quit");}
			    if (btnl == 6) {sprintf(config[11],"bind tab quit");}
			    if (btnr == 6) {sprintf(config[12],"bind backspace quit");}
			#endif
			#if defined(WIZ) || defined(GP2X_ONLY)
			    if (btna == 0) {sprintf(config[7],"unbind joy12");}
			    if (btnb == 0) {sprintf(config[8],"unbind joy13");}
			    if (btnx == 0) {sprintf(config[9],"unbind joy14");}
			    if (btny == 0) {sprintf(config[10],"unbind joy15");}
			    if (btnl == 0) {sprintf(config[11],"unbind joy10");}
			    if (btnr == 0) {sprintf(config[12],"unbind joy11");}

			    if (btna == 1) {sprintf(config[7],"bind joy12 +a");}
			    if (btnb == 1) {sprintf(config[8],"bind joy13 +a");}
			    if (btnx == 1) {sprintf(config[9],"bind joy14 +a");}
			    if (btny == 1) {sprintf(config[10],"bind joy15 +a");}
			    if (btnl == 1) {sprintf(config[11],"bind joy10 +a");}
			    if (btnr == 1) {sprintf(config[12],"bind joy11 +a");}

			    if (btna == 2) {sprintf(config[7],"bind joy12 +b");}
			    if (btnb == 2) {sprintf(config[8],"bind joy13 +b");}
			    if (btnx == 2) {sprintf(config[9],"bind joy14 +b");}
			    if (btny == 2) {sprintf(config[10],"bind joy15 +b");}
			    if (btnl == 2) {sprintf(config[11],"bind joy10 +b");}
			    if (btnr == 2) {sprintf(config[12],"bind joy11 +b");}

			    if (btna == 3) {sprintf(config[7],"bind joy12 +select");}
			    if (btnb == 3) {sprintf(config[8],"bind joy13 +select");}
			    if (btnx == 3) {sprintf(config[9],"bind joy14 +select");}
			    if (btny == 3) {sprintf(config[10],"bind joy15 +select");}
			    if (btnl == 3) {sprintf(config[11],"bind joy10 +select");}
			    if (btnr == 3) {sprintf(config[12],"bind joy11 +select");}

			    if (btna == 4) {sprintf(config[7],"bind joy12 +start");}
			    if (btnb == 4) {sprintf(config[8],"bind joy13 +start");}
			    if (btnx == 4) {sprintf(config[9],"bind joy14 +start");}
			    if (btny == 4) {sprintf(config[10],"bind joy15 +start");}
			    if (btnl == 4) {sprintf(config[11],"bind joy10 +start");}
			    if (btnr == 4) {sprintf(config[12],"bind joy11 +start");}

			    if (btna == 5) {sprintf(config[7],"bind joy12 reset");}
			    if (btnb == 5) {sprintf(config[8],"bind joy13 reset");}
			    if (btnx == 5) {sprintf(config[9],"bind joy14 reset");}
			    if (btny == 5) {sprintf(config[10],"bind joy15 reset");}
			    if (btnl == 5) {sprintf(config[11],"bind joy10 reset");}
			    if (btnr == 5) {sprintf(config[12],"bind joy11 reset");}

			    if (btna == 6) {sprintf(config[7],"bind joy12 quit");}
			    if (btnb == 6) {sprintf(config[8],"bind joy13 quit");}
			    if (btnx == 6) {sprintf(config[9],"bind joy14 quit");}
			    if (btny == 6) {sprintf(config[10],"bind joy15 quit");}
			    if (btnl == 6) {sprintf(config[11],"bind joy10 quit");}
			    if (btnr == 6) {sprintf(config[12],"bind joy11 quit");}
			#endif
			for(i=0; i<13; i++)
				rc_command(config[i]);

			pal_dirty();

			if (ret == 11){ /* Apply & Save */
				file = fopen("bindings.rc","w");
				for(i=0; i<13; i++){
					fputs(config[i],file);
					fputs("\n",file);
				}
				fclose(file);
			}
		break;
	}
	return ret;
}
Beispiel #6
0
int menu_options(){

	struct pal_s *palp=0;
	int pal=0, skip=0, ret=0, cfilter=0, sfps=0, upscale=0, speed=0, i=0;
	char *tmp=0, *romdir=0;

	FILE *file;
#ifdef DINGOO_NATIVE
    /*
    **  100Mhz once caused Dingoo A320 MIPS to hang,
    **  when 100Mhz worked BW GB (Adjustris) game was running at 32 fps (versus 60 at 200Mhz).
    **  150Mhz has never worked on my Dingoo A320.
    */
    uintptr_t dingoo_clock_speeds[] = { 200000000, 250000000, 300000000, 336000000, 360000000, 400000000 /* , 430000000 Should not be needed */ };
    /*
    ** under-under clock option is for GB games.
    ** GB games can often be ran under the already
    ** underclocked Dingoo speed of 336Mhz
    */

    bool dingoo_clock_change_result;
	uintptr_t tempCore=336000000; /* default Dingoo A320 clock speed */
	uintptr_t tempMemory=112000000; /* default Dingoo A320 memory speed */
	cpu_clock_get(&tempCore, &tempMemory);
#endif /* DINGOO_NATIVE */

	pal = findpal();
	cfilter = rc_getint("colorfilter");
	if(cfilter && !rc_getint("filterdmg")) cfilter = 2;
	upscale = rc_getint("upscaler");
	skip = rc_getint("frameskip")+1;
	sfps = rc_getint("showfps");
#ifdef DINGOO_NATIVE
	speed = 0;
#else
	speed = rc_getint("cpuspeed")/50 - 4;
#endif /* DINGOO_NATIVE */
	if(speed<0) speed = 0;
	if(speed>11) speed = 11;

	romdir = rc_getstr("romdir");
	romdir = romdir ? strdup(romdir) : strdup(".");

	start:

	dialog_begin("Options",NULL);

	dialog_option("Mono Palette",lpalettes,&pal);               /* 1 */
	dialog_option("Color Filter",lcolorfilter,&cfilter);        /* 2 */
	dialog_option("Upscaler",lupscaler,&upscale);               /* 3 */
	dialog_option("Frameskip",lframeskip,&skip);                /* 4 */
	dialog_option("Show FPS",lsdl_showfps,&sfps);               /* 5 */
#if defined(WIZ) || defined(DINGOO_NATIVE)
	dialog_option("Clock Speed",lclockspeeds,&speed);           /* 6 */
#else
	dialog_text("Clock Speed","Default",0);                     /* 6 */
#endif
	dialog_text("Rom Path",romdir,FIELD_SELECTABLE);            /* 7 */
	#ifdef GNUBOY_HARDWARE_VOLUME
	dialog_option("Volume", volume_levels, &volume_hardware);   /* 8 */ /* this is not the OSD volume.. */
	#else
	dialog_text("Volume", "Default", 0);                        /* 8 */ /* this is not the OSD volume.. */
	#endif /* GNBOY_HARDWARE_VOLUME */
	dialog_text(NULL,NULL,0);                                   /* 9 */
	dialog_text("Apply",NULL,FIELD_SELECTABLE);                 /* 10 */
	dialog_text("Apply & Save",NULL,FIELD_SELECTABLE);          /* 11 */
	dialog_text("Cancel",NULL,FIELD_SELECTABLE);                /* 12 */

	switch(ret=dialog_end()){
		case 7: /* "Rom Path" romdir */
			tmp = menu_requestdir("Select Rom Directory",romdir);
			if(tmp){
				free(romdir);
				romdir = tmp;
			}
			goto start;
		case 12: /* Cancel */
			return ret;
			break;
		case 10: /* Apply */
		case 11: /* Apply & Save */
			#ifdef GNUBOY_HARDWARE_VOLUME
			pcm_volume(volume_hardware * 10);
			#endif /* GNBOY_HARDWARE_VOLUME */
			palp = &gbpal[pal];
			if(speed)
			{
#ifdef DINGOO_NATIVE
                /*
                ** For now do NOT plug in into settings system, current
                ** (Wiz) speed system is focused on multiples of 50Mhz.
                ** Dingoo default clock speed is 336Mhz (CPU certified for
                ** 360, 433MHz is supposed to be possible).
                ** Only set clock speed if changed in options each and
                ** everytime - do not use config file
                */
                --speed;
                /* check menu response is withing the preset array range/size */
                if (speed > (sizeof(dingoo_clock_speeds)/sizeof(uintptr_t) - 1) )
                    speed = 0;
                
                tempCore = dingoo_clock_speeds[speed];
                dingoo_clock_change_result = cpu_clock_set(tempCore);
                
                tempCore=tempMemory=0;
                cpu_clock_get(&tempCore, &tempMemory); /* currently unused */
                /* TODO display clock speed next to on screen FPS indicator */
#endif /* DINGOO_NATIVE */
    
				speed = speed*50 + 200;
			}
			sprintf(config[0],"set dmg_bgp 0x%.6x 0x%.6x 0x%.6x 0x%.6x", palp->dmg_bgp[0], palp->dmg_bgp[1], palp->dmg_bgp[2], palp->dmg_bgp[3]);
			sprintf(config[1],"set dmg_wndp 0x%.6x 0x%.6x 0x%.6x 0x%.6x",palp->dmg_wndp[0],palp->dmg_wndp[1],palp->dmg_wndp[2],palp->dmg_wndp[3]);
			sprintf(config[2],"set dmg_obp0 0x%.6x 0x%.6x 0x%.6x 0x%.6x",palp->dmg_obp0[0],palp->dmg_obp0[1],palp->dmg_obp0[2],palp->dmg_obp0[3]);
			sprintf(config[3],"set dmg_obp1 0x%.6x 0x%.6x 0x%.6x 0x%.6x",palp->dmg_obp1[0],palp->dmg_obp1[1],palp->dmg_obp1[2],palp->dmg_obp1[3]);
			sprintf(config[4],"set colorfilter %i",cfilter!=0);
			sprintf(config[5],"set filterdmg %i",cfilter==1);
			sprintf(config[6],"set upscaler %i",upscale);
			sprintf(config[7],"set frameskip %i",skip-1);
			sprintf(config[8],"set showfps %i",sfps);
			sprintf(config[9],"set cpuspeed %i",speed);
			#ifdef DINGOO_NATIVE /* FIXME Windows too..... if (DIRSEP_CHAR == '\\').... */
			{
				char tmp_path[PATH_MAX];
				char *dest, *src;
				dest = &tmp_path[0];
				src = romdir;
				
				/* escape the path seperator (should escape other things too.) */
				while(*dest = *src++)
				{
					if (*dest == DIRSEP_CHAR)
					{
						dest++;
						*dest = DIRSEP_CHAR;
					}
					dest++;
				}
			
				sprintf(config[10], "set romdir \"%s\"", tmp_path);
				scaler_init(0);
			}
			#else
			sprintf(config[10],"set romdir \"%s\"",romdir);
			scaler_init(0);
			#endif /* DINGOO_NATIVE */

			for(i=0; i<11; i++)
				rc_command(config[i]);

			pal_dirty();

			if (ret == 11){ /* Apply & Save */
				file = fopen("ohboy.rc","w");
				for(i=0; i<11; i++){
					fputs(config[i],file);
					fputs("\n",file);
				}
				fclose(file);
			}

		break;
	}

	free(romdir);

	return ret;
}
Beispiel #7
0
int menu_state(int save){

	char **statebody=NULL;
	char* name;

	int i, flags,ret, del=0,l;
#ifndef OHBOY_FILE_STAT_NOT_AVAILABLE
	/* Not all platforms implement stat()/fstat() */
	struct stat fstat;
	time_t time;
	char *tstr;
#endif

	char *savedir;
	char *savename;
	char *saveprefix;
	FILE *f;
	int sizeof_slots=0;
    while (slots[sizeof_slots] != NULL)
        sizeof_slots++;
    statebody = malloc(sizeof_slots * sizeof(char*));  /* FIXME check for NULL return from malloc */

	savedir = rc_getstr("savedir");
	savename = rc_getstr("savename");
	saveprefix = malloc(strlen(savedir) + strlen(savename) + 2);
	sprintf(saveprefix, "%s%s%s", savedir, DIRSEP, savename);

	dialog_begin(save?"Save State":"Load State",rom.name);

	for(i=0; i<sizeof_slots; i++){

		name = malloc(strlen(saveprefix) + 5);
		sprintf(name, "%s.%03d", saveprefix, i);

#ifndef OHBOY_FILE_STAT_NOT_AVAILABLE
		/* if the file exists lookup the timestamp */
		if(!stat(name,&fstat)){
			time = fstat.st_mtime;
			tstr = ctime(&time);

			l = strlen(tstr);
			statebody[i] = malloc(l);
			strcpy(statebody[i],tstr);
			statebody[i][l-1]=0;
#else
		/* check if the file exists */
		if(f=fopen(name,"rb")){
			fclose(f);
			statebody[i] = (char*)not_emptyslot;
#endif /* OHBOY_FILE_STAT_NOT_AVAILABLE */
			flags = FIELD_SELECTABLE;
		} else {
			statebody[i] = (char*)emptyslot;
			flags = save ? FIELD_SELECTABLE : 0;
		}
		dialog_text(slots[i],statebody[i],flags);

		free(name);
	}

	if(ret=dialog_end()){
		name = malloc(strlen(saveprefix) + 5);
		sprintf(name, "%s.%03d", saveprefix, ret-1);
		if(save){
			if(f=fopen(name,"wb")){
				savestate(f);
				fclose(f);
			}
		}else{
			if(f=fopen(name,"rb")){
				loadstate(f);
				fclose(f);
				vram_dirty();
				pal_dirty();
				sound_dirty();
				mem_updatemap();
			}
		}
		free(name);
	}

	for(i=0; i<sizeof_slots; i++)
		if(statebody[i] != emptyslot && statebody[i] != not_emptyslot) free(statebody[i]);

	free(saveprefix);
	return ret;
}

#define GBPAL_COUNT 27
struct pal_s{
	char name[16];
	unsigned int dmg_bgp[4];
	unsigned int dmg_wndp[4];
	unsigned int dmg_obp0[4];
	unsigned int dmg_obp1[4];
}gbpal[GBPAL_COUNT] = {
	{
		.name = "Default",
		.dmg_bgp  = {0X98D0E0,0X68A0B0,0X60707C,0X2C3C3C},
		.dmg_wndp = {0X98D0E0,0X68A0B0,0X60707C,0X2C3C3C},
		.dmg_obp0 = {0X98D0E0,0X68A0B0,0X60707C,0X2C3C3C},
		.dmg_obp1 = {0X98D0E0,0X68A0B0,0X60707C,0X2C3C3C}
	},{//Grey Palette
		.name = "Grey",
		.dmg_bgp  = {  0xFFFFFF, 0xAAAAAA, 0x555555, 0x000000 }, //BG
		.dmg_wndp = {  0xFFFFFF, 0xAAAAAA, 0x555555, 0x000000 }, //WIN
		.dmg_obp0 = {  0xFFFFFF, 0xAAAAAA, 0x555555, 0x000000 }, //OB0
		.dmg_obp1 = {  0xFFFFFF, 0xAAAAAA, 0x555555, 0x000000 }  //OB1
	},{//Realistic Palette
		.name = "DMG",