typedef signed char     int8;
typedef signed short    int16;
typedef signed long     int32;
typedef unsigned char   uint8;
typedef unsigned short  uint16;
typedef unsigned long   uint32;

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include <commdlg.h>

#define MEMSIZE 0x00100000L /* default memsize 1 Mb */
#define IBS 10                  /* Ikbd input buffer size */
#define OBS 20                  /* Ikbd output buffer size */

extern uint8 compat;
extern int speedbar;
extern int checkedsample;
extern int checkedsound;
extern int nbframeskip;
extern int autoframeskip;
extern int maxframeskip;
extern int autofire;



extern int8    *membase;
extern unsigned char PathName[400];
extern int             display_mode;
//68000 vars
extern unsigned long   reg[23];
extern unsigned long   dfc, sfc, vbr;
extern unsigned short SaveWordTrc;
extern unsigned char GetTrc;
extern int             intmask, intpri;
extern char            cpu_state;
extern unsigned        cpu_type;
extern volatile unsigned cpu_count;

extern uint8    memconf;
extern uint8    mfp_gpip, mfp_aer, mfp_ddr, mfp_iera, mfp_ierb, mfp_ipra, mfp_iprb,
mfp_isra, mfp_isrb, mfp_imra, mfp_imrb, mfp_ivr, mfp_tacr,
mfp_tbcr, mfp_tcdcr, mfp_scr, mfp_ucr, mfp_rsr, mfp_tsr, mfp_udr;

//Mfp delay timer variables
extern int32 mfp_reg[12];

extern uint8    acia1_cr, acia1_sr, acia1_dr, acia2_cr, acia2_sr,
acia2_dr;

//Video shifter
extern uint32	vid_adr;
extern uint8    vid_baseh, vid_basem;
extern uint8    vid_syncmode, vid_shiftmode;
extern int16    vid_col[16];
extern int      vid_flag;


extern uint16   dma_car, dma_scr, dma_sr, dma_mode;
extern uint8    dma_adrh, dma_adrm, dma_adrl;
extern int32	mfpcycletab[16];

extern uint32	psg[26];
extern unsigned char sample[10000];

/* fdc.c */
extern unsigned char fdc_data, fdc_track, fdc_sector,
fdc_status, fdc_command, fdc_int;
extern char     fdcdir;
extern unsigned char	disk_ejected[2];
extern unsigned char	disk_changed[2];
extern int discpos[2];
struct Disk {
    FILE       *file;
    char        name[80];
    int         head;
    int         sides;
    int         tracks;
    int         sectors;
    int         secsize;
};
extern struct Disk disk[2];


extern uint16   blt_halftone[16];
extern int16    blt_src_x_inc, blt_src_y_inc;
extern uint32   blt_src_addr;
extern int16    blt_end_1, blt_end_2, blt_end_3;
extern int16    blt_dst_x_inc, blt_dst_y_inc;
extern uint32   blt_dst_addr;
extern uint16   blt_x_cnt, blt_y_cnt;
extern int8     blt_hop, blt_op, blt_status, blt_skew;



extern int exmousex,exmousey;
extern int             pause ;
extern unsigned char   inbuff[IBS];
extern unsigned char   outbuff[OBS];
extern int             inbuffi;
extern int             outbuffi;
extern int				oldjoystate;
extern int				joytick;
extern unsigned char Buttons;

extern char msabuff[1050*1024];
extern unsigned int firsttick,lasttick,tosnumber;

extern struct {
    char            buttons;    /* real buttons */
    char            stbuttons;  /* buttons as known to st */
    int             xscale, yscale;
    int             xmax, ymax;
    int             xkcm, ykcm;
    int             xth, yth;
    int             x, y;       /* real mouse position */
    int             stx, sty;   /* mouse position st */
    int             button_action;
    int             mode;
    int             yinv;
}               mouse;

extern struct {
    int             mode;
    int             rate;
    int             rx, ry, tx, ty, vx, vy;
}               joystick;
void ConvertCToT(TCHAR* pszDest, const CHAR* pszSrc)
{	int i;
for( i= 0; i < strlen(pszSrc); i++)
pszDest[i] = (TCHAR) pszSrc[i];
}
int loadstate(int numstate,unsigned short *rootapp,HWND hWnd){
	uint8 tosnum1,tosnum2;
	uint16 tosnum=0;
	char strbuf[10];
	TCHAR sbW[256];
	FILE *loadfile,*loadtos;
	char filename[400],tempname[400];
	struct Disk stdisk;
	int i;
	unsigned long  sizecompressed=0;
	unsigned long  sizeuncompressed=0;
	i=0;
	while ((char )(rootapp[i])!=0){
		tempname[i]=(char )(rootapp[i]);
		i++;
	}
	if (numstate>0){
		for (i=strlen(tempname)-1;i>0;i--){
			if (tempname[i]=='\\'){
				tempname[i]='\0';
				break;
			}
		}
		
		sprintf(filename,"%s\\save\\%02d.sst",tempname,numstate);
	}else{
		tempname[i]='\0';
		sprintf(filename,"%s",tempname,numstate);
	}
	if (NULL == (loadfile = fopen(filename,"rb"))){
		MessageBox(hWnd,L"No Savestate",L"Info",MB_OK);
		return -1;
		
	}
	
	loadtos = fopen ((const char *)PathName, "rb");
	fread (&tosnum, 1, 2, loadtos);
	fread (&tosnum, 1, 2, loadtos);
	fclose (loadtos);
	
	fread(&tempname[0],1,sizeof(unsigned char)*400,loadfile);
	fread(&disk[0],1,sizeof(stdisk)*2,loadfile);
	
	fread(&tosnumber,1,sizeof(tosnumber),loadfile);
	
	if (tosnumber!=tosnum){
		memset (&strbuf, 0, sizeof (strbuf));
		memset (&sbW, 0, sizeof (sbW));
		if (NULL == (loadtos = fopen ((const char *)tempname, "rb"))) {
			tosnum1=*((char *)(&tosnumber));
			tosnum2=*((char *)(&tosnumber)+1);
			sprintf(strbuf,"Tos. %01d.%02d is needed\nfor this savestate\nload it before...",tosnum1,tosnum2);
			ConvertCToT(sbW,strbuf);
			MessageBox(hWnd,sbW,L"Info",MB_OK);
			fclose(loadfile);
			return -1;
		}else{
			fread (&tosnum, 1, 2, loadtos);
			fread (&tosnum, 1, 2, loadtos);
			fclose(loadtos);
			if (tosnumber!=tosnum){
				tosnum1=*((char *)(&tosnumber));
				tosnum2=*((char *)(&tosnumber)+1);
				sprintf(strbuf,"Tos. %01d.%02d is needed\nfor this savestate\nload it before...",tosnum1,tosnum2);
				ConvertCToT(sbW,strbuf);
				MessageBox(hWnd,sbW,L"Info",MB_OK);
				fclose(loadfile);
				return -1;
			}
			strcpy(PathName,tempname);
		}
		
		
	}
	
	
	if (strlen(disk[0].name)>0)  FDCInit(0);
    if (strlen(disk[1].name)>0)   FDCInit(1);
	fclose(loadfile);
	loadfile = fopen(filename,"rb");
	fread(&tempname[0],1,sizeof(unsigned char)*400,loadfile);
	fread(&disk[0],1,sizeof(stdisk)*2,loadfile);
	fread(&tosnumber,1,sizeof(tosnumber),loadfile);
	Init();
	//General Vars
	fread(&compat,1,sizeof(compat),loadfile);
	fread(&speedbar,1,sizeof(speedbar),loadfile);
	fread(&checkedsample,1,sizeof(checkedsample),loadfile);
	fread(&checkedsound,1,sizeof(checkedsound),loadfile);
	fread(&nbframeskip,1,sizeof(nbframeskip),loadfile);
	fread(&autoframeskip,1,sizeof(autoframeskip),loadfile);
	fread(&maxframeskip,1,sizeof(maxframeskip),loadfile);
	fread(&autofire,1,sizeof(autofire),loadfile);
	
	
	//Save 68000 vars
	fread(&reg[0],1,sizeof(unsigned long)*23,loadfile);
	fread(&dfc,1,sizeof(dfc),loadfile);
	fread(&sfc,1,sizeof(sfc),loadfile);
	fread(&vbr,1,sizeof(vbr),loadfile);
	fread(&SaveWordTrc,1,sizeof(SaveWordTrc),loadfile);
	fread(&intmask,1,sizeof(intmask),loadfile);
	fread(&intpri,1,sizeof(intpri),loadfile);
	fread(&cpu_state,1,sizeof(cpu_state),loadfile);
	fread(&cpu_type,1,sizeof(cpu_type),loadfile);
	
	//Save FDC vars
	fread(&fdc_data,1,sizeof(fdc_data),loadfile);
	fread(&fdc_track,1,sizeof(fdc_track),loadfile);
	fread(&fdc_sector,1,sizeof(fdc_sector),loadfile);
	fread(&fdc_status,1,sizeof(fdc_status),loadfile);
	fread(&fdc_command,1,sizeof(fdc_command),loadfile);
	fread(&fdc_int,1,sizeof(fdc_int),loadfile);
	fread(&fdcdir,1,sizeof(fdcdir),loadfile);	
	fread(&disk_ejected[0],1,sizeof(unsigned char)*2,loadfile);
	fread(&disk_changed[0],1,sizeof(unsigned char)*2,loadfile);
	fread(&discpos[0],1,sizeof(int)*2,loadfile);
	//IO vars
	fread(&memconf,1,sizeof(memconf),loadfile);
	fread(&mfp_gpip,1,sizeof(mfp_gpip),loadfile);
	fread(&mfp_aer,1,sizeof(mfp_aer),loadfile);
	fread(&mfp_ddr,1,sizeof(mfp_ddr),loadfile);
	fread(&mfp_iera,1,sizeof(mfp_iera),loadfile);
	fread(&mfp_ierb,1,sizeof(mfp_ierb),loadfile);
	fread(&mfp_ipra,1,sizeof(mfp_ipra),loadfile);
	fread(&mfp_iprb,1,sizeof(mfp_iprb),loadfile);
	fread(&mfp_isra,1,sizeof(mfp_isra),loadfile);
	fread(&mfp_isrb,1,sizeof(mfp_isrb),loadfile);
	fread(&mfp_imra,1,sizeof(mfp_imra),loadfile);
	fread(&mfp_imrb,1,sizeof(mfp_imrb),loadfile);
	fread(&mfp_ivr,1,sizeof(mfp_ivr),loadfile);
	fread(&mfp_tacr,1,sizeof(mfp_tacr),loadfile);
	fread(&mfp_tbcr,1,sizeof(mfp_tbcr),loadfile);
	fread(&mfp_tcdcr,1,sizeof(mfp_tcdcr),loadfile);
	fread(&mfp_scr,1,sizeof(mfp_scr),loadfile);
	fread(&mfp_ucr,1,sizeof(mfp_ucr),loadfile);
	fread(&mfp_rsr,1,sizeof(mfp_rsr),loadfile);
	fread(&mfp_tsr,1,sizeof(mfp_tsr),loadfile);
	fread(&mfp_udr,1,sizeof(mfp_udr),loadfile);
	fread(&mfp_reg[0],1,sizeof(int32)*32,loadfile);
	
	//ACIA vars
	fread(&acia1_cr,1,sizeof(acia1_cr),loadfile);
	fread(&acia1_sr,1,sizeof(acia1_sr),loadfile);
	fread(&acia1_dr,1,sizeof(acia1_dr),loadfile);
	fread(&acia2_cr,1,sizeof(acia2_cr),loadfile);
	fread(&acia2_sr,1,sizeof(acia2_sr),loadfile);
	fread(&acia2_dr,1,sizeof(acia2_dr),loadfile);
	
	//Video vars
	fread(&vid_adr,1,sizeof(vid_adr),loadfile);
	fread(&vid_baseh,1,sizeof(vid_baseh),loadfile);
	fread(&vid_basem,1,sizeof(vid_basem),loadfile);
	fread(&vid_syncmode,1,sizeof(vid_syncmode),loadfile);
	fread(&vid_shiftmode,1,sizeof(vid_shiftmode),loadfile);
	fread(&vid_col[0],1,sizeof(int16)*16,loadfile);
	fread(&vid_flag,1,sizeof(vid_flag),loadfile);
	vid_flag=1;
	//DMA vars
	fread(&dma_car,1,sizeof(dma_car),loadfile);
	fread(&dma_scr,1,sizeof(dma_scr),loadfile);
	fread(&dma_sr,1,sizeof(dma_sr),loadfile);
	fread(&dma_mode,1,sizeof(dma_mode),loadfile);
	fread(&dma_adrh,1,sizeof(dma_adrh),loadfile);
	fread(&dma_adrm,1,sizeof(dma_adrm),loadfile);
	fread(&dma_adrl,1,sizeof(dma_adrl),loadfile);
	fread(&mfpcycletab[0],1,sizeof(int32)*16,loadfile);
	
	//Sound vars
	fread(&psg[0],1,sizeof(uint32)*26,loadfile);
	fread(&sample[0],1,sizeof(char)*10000,loadfile);
	
	fread(&display_mode,1,sizeof(display_mode),loadfile);
	
	//Blitter
	fread(&blt_halftone,1,sizeof(uint16)*16,loadfile);
	fread(&blt_src_x_inc,1,sizeof(blt_src_x_inc),loadfile);
	fread(&blt_src_y_inc,1,sizeof(blt_src_y_inc),loadfile);
	fread(&blt_src_addr,1,sizeof(blt_src_addr),loadfile);
	fread(&blt_end_1,1,sizeof(blt_end_1),loadfile);
	fread(&blt_end_2,1,sizeof(blt_end_2),loadfile);
	fread(&blt_end_3,1,sizeof(blt_end_3),loadfile);
	fread(&blt_dst_x_inc,1,sizeof(blt_dst_x_inc),loadfile);
	fread(&blt_dst_y_inc,1,sizeof(blt_dst_y_inc),loadfile);
	fread(&blt_dst_addr,1,sizeof(blt_dst_addr),loadfile);
	fread(&blt_x_cnt,1,sizeof(blt_x_cnt),loadfile);
	fread(&blt_y_cnt,1,sizeof(blt_y_cnt),loadfile);
	fread(&blt_hop,1,sizeof(blt_hop),loadfile);
	fread(&blt_op,1,sizeof(blt_op),loadfile);
	fread(&blt_status,1,sizeof(blt_status),loadfile);
	fread(&blt_skew,1,sizeof(blt_skew),loadfile);
	
	
	//Ikbd
	fread(&exmousex,1,sizeof(exmousex),loadfile);
	fread(&exmousey,1,sizeof(exmousey),loadfile);
	fread(&pause,1,sizeof(pause),loadfile);
	fread(&inbuff[0],1,sizeof(unsigned char)*IBS,loadfile);
	fread(&outbuff[0],1,sizeof(unsigned char)*OBS,loadfile);
	fread(&inbuffi,1,sizeof(inbuffi),loadfile);
	fread(&outbuffi,1,sizeof(outbuffi),loadfile);
	fread(&oldjoystate,1,sizeof(oldjoystate),loadfile);
	fread(&joytick,1,sizeof(joytick),loadfile);
	fread(&Buttons,1,sizeof(Buttons),loadfile);
	fread(&mouse,1,sizeof(mouse),loadfile);
	fread(&joystick,1,sizeof(joystick),loadfile);
	
	fread(&firsttick,1,sizeof(firsttick),loadfile);
	fread(&lasttick,1,sizeof(lasttick),loadfile);
	
	fread(&sizecompressed,1,sizeof(sizecompressed),loadfile);
	fread(&msabuff[0],1,sizecompressed,loadfile);
	sizeuncompressed=MEMSIZE+4;
	uncompress(&membase[0],&sizeuncompressed,&msabuff[0],sizecompressed);
	
	
	fclose(loadfile);
	return 0;
}

int savestate(int numstate,short *rootapp,HWND hWnd){
	unsigned long  sizecompressed=0;
	FILE* savefile;
	char filename[400],tempname[400];
	struct Disk stdisk;
	int i;
	
	i=0;
	while ((char )(rootapp[i])!=0){
		tempname[i]=(char )(rootapp[i]);
		i++;
	}	
	if (numstate>0){
		for (i=strlen(tempname)-1;i>0;i--){
			if (tempname[i]=='\\'){
				tempname[i]='\0';
				break;
			}
		}
		sprintf(filename,"%s\\save\\%02d.sst",tempname,numstate);
		if (NULL!=(savefile = fopen(filename,"rb"))){
			if (MessageBox(hWnd,L"SaveState Already Exist, \ndo you want to continue?",L"Info",MB_OKCANCEL)!=1) return 0;
		}
		fclose(savestate);
	}else{
		tempname[i]='\0';
		sprintf(filename,"%s",tempname);
	}
	savefile = fopen(filename,"wb");
	
	fwrite(&PathName[0],1,sizeof(unsigned char)*400,savefile);
	fwrite(&disk[0],1,sizeof(stdisk)*2,savefile);
	fwrite(&tosnumber,1,sizeof(tosnumber),savefile);
	
	//General Vars
	fwrite(&compat,1,sizeof(compat),savefile);
	fwrite(&speedbar,1,sizeof(speedbar),savefile);
	fwrite(&checkedsample,1,sizeof(checkedsample),savefile);
	fwrite(&checkedsound,1,sizeof(checkedsound),savefile);
	fwrite(&nbframeskip,1,sizeof(nbframeskip),savefile);
	fwrite(&autoframeskip,1,sizeof(autoframeskip),savefile);
	fwrite(&maxframeskip,1,sizeof(maxframeskip),savefile);
	fwrite(&autofire,1,sizeof(autofire),savefile);
	
	//Save 68000 vars
	fwrite(&reg[0],1,sizeof(unsigned long)*23,savefile);
	fwrite(&dfc,1,sizeof(dfc),savefile);
	fwrite(&sfc,1,sizeof(sfc),savefile);
	fwrite(&vbr,1,sizeof(vbr),savefile);
	fwrite(&SaveWordTrc,1,sizeof(SaveWordTrc),savefile);
	fwrite(&intmask,1,sizeof(intmask),savefile);
	fwrite(&intpri,1,sizeof(intpri),savefile);
	fwrite(&cpu_state,1,sizeof(cpu_state),savefile);
	fwrite(&cpu_type,1,sizeof(cpu_type),savefile);
	
	//Save FDC vars
	fwrite(&fdc_data,1,sizeof(fdc_data),savefile);
	fwrite(&fdc_track,1,sizeof(fdc_track),savefile);
	fwrite(&fdc_sector,1,sizeof(fdc_sector),savefile);
	fwrite(&fdc_status,1,sizeof(fdc_status),savefile);
	fwrite(&fdc_command,1,sizeof(fdc_command),savefile);
	fwrite(&fdc_int,1,sizeof(fdc_int),savefile);
	fwrite(&fdcdir,1,sizeof(fdcdir),savefile);
	fwrite(&disk_ejected[0],1,sizeof(unsigned char)*2,savefile);
	fwrite(&disk_changed[0],1,sizeof(unsigned char)*2,savefile);
	fwrite(&discpos[0],1,sizeof(unsigned int)*2,savefile);
	
	//IO vars
	fwrite(&memconf,1,sizeof(memconf),savefile);
	fwrite(&mfp_gpip,1,sizeof(mfp_gpip),savefile);
	fwrite(&mfp_aer,1,sizeof(mfp_aer),savefile);
	fwrite(&mfp_ddr,1,sizeof(mfp_ddr),savefile);
	fwrite(&mfp_iera,1,sizeof(mfp_iera),savefile);
	fwrite(&mfp_ierb,1,sizeof(mfp_ierb),savefile);
	fwrite(&mfp_ipra,1,sizeof(mfp_ipra),savefile);
	fwrite(&mfp_iprb,1,sizeof(mfp_iprb),savefile);
	fwrite(&mfp_isra,1,sizeof(mfp_isra),savefile);
	fwrite(&mfp_isrb,1,sizeof(mfp_isrb),savefile);
	fwrite(&mfp_imra,1,sizeof(mfp_imra),savefile);
	fwrite(&mfp_imrb,1,sizeof(mfp_imrb),savefile);
	fwrite(&mfp_ivr,1,sizeof(mfp_ivr),savefile);
	fwrite(&mfp_tacr,1,sizeof(mfp_tacr),savefile);
	fwrite(&mfp_tbcr,1,sizeof(mfp_tbcr),savefile);
	fwrite(&mfp_tcdcr,1,sizeof(mfp_tcdcr),savefile);
	fwrite(&mfp_scr,1,sizeof(mfp_scr),savefile);
	fwrite(&mfp_ucr,1,sizeof(mfp_ucr),savefile);
	fwrite(&mfp_rsr,1,sizeof(mfp_rsr),savefile);
	fwrite(&mfp_tsr,1,sizeof(mfp_tsr),savefile);
	fwrite(&mfp_udr,1,sizeof(mfp_udr),savefile);
	fwrite(&mfp_reg[0],1,sizeof(int32)*32,savefile);
	
	//ACIA vars
	fwrite(&acia1_cr,1,sizeof(acia1_cr),savefile);
	fwrite(&acia1_sr,1,sizeof(acia1_sr),savefile);
	fwrite(&acia1_dr,1,sizeof(acia1_dr),savefile);
	fwrite(&acia2_cr,1,sizeof(acia2_cr),savefile);
	fwrite(&acia2_sr,1,sizeof(acia2_sr),savefile);
	fwrite(&acia2_dr,1,sizeof(acia2_dr),savefile);
	
	//Video vars
	fwrite(&vid_adr,1,sizeof(vid_adr),savefile);
	fwrite(&vid_baseh,1,sizeof(vid_baseh),savefile);
	fwrite(&vid_basem,1,sizeof(vid_basem),savefile);
	fwrite(&vid_syncmode,1,sizeof(vid_syncmode),savefile);
	fwrite(&vid_shiftmode,1,sizeof(vid_shiftmode),savefile);
	fwrite(&vid_col[0],1,sizeof(int16)*16,savefile);
	fwrite(&vid_flag,1,sizeof(vid_flag),savefile);
	
	//DMA vars
	fwrite(&dma_car,1,sizeof(dma_car),savefile);
	fwrite(&dma_scr,1,sizeof(dma_scr),savefile);
	fwrite(&dma_sr,1,sizeof(dma_sr),savefile);
	fwrite(&dma_mode,1,sizeof(dma_mode),savefile);
	fwrite(&dma_adrh,1,sizeof(dma_adrh),savefile);
	fwrite(&dma_adrm,1,sizeof(dma_adrm),savefile);
	fwrite(&dma_adrl,1,sizeof(dma_adrl),savefile);
	fwrite(&mfpcycletab[0],1,sizeof(int32)*16,savefile);
	
	//Sound vars
	fwrite(&psg[0],1,sizeof(uint32)*26,savefile);
	fwrite(&sample[0],1,sizeof(char)*10000,savefile);
	
	fwrite(&display_mode,1,sizeof(display_mode),savefile);
	
	//Blitter
	fwrite(&blt_halftone,1,sizeof(uint16)*16,savefile);
	fwrite(&blt_src_x_inc,1,sizeof(blt_src_x_inc),savefile);
	fwrite(&blt_src_y_inc,1,sizeof(blt_src_y_inc),savefile);
	fwrite(&blt_src_addr,1,sizeof(blt_src_addr),savefile);
	fwrite(&blt_end_1,1,sizeof(blt_end_1),savefile);
	fwrite(&blt_end_2,1,sizeof(blt_end_2),savefile);
	fwrite(&blt_end_3,1,sizeof(blt_end_3),savefile);
	fwrite(&blt_dst_x_inc,1,sizeof(blt_dst_x_inc),savefile);
	fwrite(&blt_dst_y_inc,1,sizeof(blt_dst_y_inc),savefile);
	fwrite(&blt_dst_addr,1,sizeof(blt_dst_addr),savefile);
	fwrite(&blt_x_cnt,1,sizeof(blt_x_cnt),savefile);
	fwrite(&blt_y_cnt,1,sizeof(blt_y_cnt),savefile);
	fwrite(&blt_hop,1,sizeof(blt_hop),savefile);
	fwrite(&blt_op,1,sizeof(blt_op),savefile);
	fwrite(&blt_status,1,sizeof(blt_status),savefile);
	fwrite(&blt_skew,1,sizeof(blt_skew),savefile);
	
	//Ikbd
	fwrite(&exmousex,1,sizeof(exmousex),savefile);
	fwrite(&exmousey,1,sizeof(exmousey),savefile);
	fwrite(&pause,1,sizeof(pause),savefile);
	fwrite(&inbuff[0],1,sizeof(unsigned char)*IBS,savefile);
	fwrite(&outbuff[0],1,sizeof(unsigned char)*OBS,savefile);
	fwrite(&inbuffi,1,sizeof(inbuffi),savefile);
	fwrite(&outbuffi,1,sizeof(outbuffi),savefile);
	fwrite(&oldjoystate,1,sizeof(oldjoystate),savefile);
	fwrite(&joytick,1,sizeof(joytick),savefile);
	fwrite(&Buttons,1,sizeof(Buttons),savefile);
	fwrite(&mouse,1,sizeof(mouse),savefile);
	fwrite(&joystick,1,sizeof(joystick),savefile);
	
	fwrite(&firsttick,1,sizeof(firsttick),savefile);
	fwrite(&lasttick,1,sizeof(lasttick),savefile);
	sizecompressed=1050*1024;
	i=compress(&msabuff[0],&sizecompressed,&membase[0],MEMSIZE+4);
	
	fwrite(&sizecompressed,1,sizeof(sizecompressed),savefile);
	//Memory
	fwrite(&msabuff[0],1,sizecompressed,savefile);
	
	
	
	fclose(savefile);
	
	return 0;
}