/* this script load a snapshot from disk */
/* Uses the internal loaders of emulator (MIRAGE & PC) */

	address command

	if ~show(ports,ZXAM_REXX) then do
		requestchoice 'title "ZXAM Script error..." body "I can''t find the emulator''s port!!" gadgets "AARGH!"'
		exit
		end

	/* store the initial status of the emulator */
	running=zxamactrun()	/* 1=running */
	zxamstop()              /* stop the emulation */

/* We use the emulator's filerequester */
	
	/* old window status */
	oldname=zxamactname()
	oldformat=zxamactformat()
	
	nombre=zxamloadrequester('Select a snapshot...')
	if nombre='' then exit	/* CANCEL */
	
	bloque=zxampploadfile(nombre)	/* load the whole file */
	
	formato=zxamparseloaded(bloque)		/* parse the file if known format */
	
	if formato~='' then do			/* known format */
		zxamnameformat(zxamfilepart(nombre),formato)
		exit
		end
	
/* Yep! Unknown format. Here you can put the relevant code for loading of */
/* other formats */

/* This is an example of how to implement a format (mirage, in this case) */
	if length(bloque)=49179 then do		/* MIRAGE (I think...) */
		zxamsetreg(i,c2d(substr(bloque,1,1)))	/* register i */
		zxamsetreg(lh2,c2d(substr(bloque,2,2)))	/* register hl' */
		zxamsetreg(ed2,c2d(substr(bloque,4,2)))	/* register de' */
		zxamsetreg(cb2,c2d(substr(bloque,6,2)))	/* register bc' */
		zxamsetreg(f2,c2d(substr(bloque,8,1)))	/* register f' */
		zxamsetreg(a2,c2d(substr(bloque,9,1)))	/* register a' */
		zxamsetreg(lh,c2d(substr(bloque,10,2)))	/* register hl */
		zxamsetreg(ed,c2d(substr(bloque,12,2)))	/* register de */
		zxamsetreg(cb,c2d(substr(bloque,14,2)))	/* register bc */
		zxamsetreg(yi,c2d(substr(bloque,16,2)))	/* register iy */
		zxamsetreg(xi,c2d(substr(bloque,18,2)))	/* register ix */
		zxamsetreg(int,bittst(substr(bloque,20,1),2)) /* interrupt status */
		zxamsetreg(r,c2d(substr(bloque,21,1)))	/* register r */
		zxamsetreg(f,c2d(substr(bloque,22,1)))	/* register f */
		zxamsetreg(a,c2d(substr(bloque,23,1)))	/* register a */
		zxamsetreg(ps,c2d(substr(bloque,24,2)))	/* register sp */
		zxamsetreg(pc,zxamfindbyte(0,201))	/* address of a RET in ROM */
		zxamsetreg(im,c2d(substr(bloque,26,1)))	/* interrupt mode */
		zxamsetreg(bor,c2d(substr(bloque,27,1)))	/* border color */
		/* put the 48k of RAM */
		zxamputmem(16384,substr(bloque,28,49152))
		
		/* put name a formt in the window */
		zxamnameformat(zxamfilepart(nombre),'mirage')
		zxamnoreload()
		
		exit
		
		end


/* this examle loads a PC snapshot */
	if length(bloque)=49190 then do		/* format PC?? */
		if left(bloque,2)~='SP' then break	/* same size, but no ID */
		zxamsetreg(cb,c2d(substr(bloque,7,2)))
		zxamsetreg(ed,c2d(substr(bloque,9,2)))
		zxamsetreg(lh,c2d(substr(bloque,11,2)))
		zxamsetreg(f,c2d(substr(bloque,13,1)))
		zxamsetreg(a,c2d(substr(bloque,14,1)))
		zxamsetreg(xi,c2d(substr(bloque,15,2)))
		zxamsetreg(yi,c2d(substr(bloque,17,2)))
		zxamsetreg(cb2,c2d(substr(bloque,19,2)))
		zxamsetreg(ed2,c2d(substr(bloque,21,2)))
		zxamsetreg(lh2,c2d(substr(bloque,23,2)))
		zxamsetreg(f2,c2d(substr(bloque,25,1)))
		zxamsetreg(a2,c2d(substr(bloque,26,1)))
		zxamsetreg(r,c2d(substr(bloque,27,1)))
		zxamsetreg(i,c2d(substr(bloque,28,1)))
		zxamsetreg(ps,c2d(substr(bloque,29,2)))
		zxamsetreg(cp,c2d(substr(bloque,31,2)))
		zxamsetreg(bor,c2d(substr(bloque,35,1)))
		zxamsetreg(int,bittst(substr(bloque,37,1),0))
		zxamsetreg(im,1+bittst(substr(bloque,37,1),1))
		/* put the 48k of RAM */
		zxamputmem(16384,substr(bloque,39,49152))
		
		/* put new name and format */
		zxamnameformat(zxamfilepart(nombre),'PC')
		zxamnoreload()
		
		exit
		
		end


/* if program reached this point means that the format is totally unknown */
/* load error! */
	
	requestchoice 'title "ZXAM Script error..." body "Unknown format!!" gadgets "AARGH!"'

	if oldname='' then
		zxamclearnameformat()
	else
		zxamnameformat(oldname,oldformat)

	/* restore the status */
	if running=1 then zxamrun()

	exit
