/* this script saves the ASCII listing of the BASIC program inside the */ 
/* Spectrum's memory */
	
	/* test if emulator is present */
	address command
	
	if ~show(ports,ZXAM_REXX) then do
		requestchoice '>nil: 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 */

	/* locate the BASIC listing */
	
	/* start of BASIC */
	baselist=zxamdpeek(23635)
	
	/* end of BASIC */
	endlist=zxamdpeek(23627)
	
	/* length of BASIC */
	longbasic=endlist-baselist
	if longbasic=0 then do
		requestchoice '>nil: title "ZXAM Script error..." body "No BASIC program in memory!!" gadgets "AARGH!"'
		exit 0
		end
	
	/* get all the BASIC block */
	bloquebasic=zxamgetmem(baselist,endlist-baselist)
	
	
	/* ask for path&name */
	oldpath=zxamactsavepath()
	oldpattern=zxamactpattern()
	zxampattern('#?')
	nombre=zxamsaverequester('Name for BASIC listing...','ram:')
	zxamsavepath(oldpath)
	zxampattern(oldpattern)
	if nombre='' then exit 0	/* CANCEL */
	
	if ~open('fichero',nombre,'w') then exit 0
	
	/* old window status */
	oldname=zxamactname()
	oldformat=zxamactformat()
	
	ZXAMEnableAbort()		/* enables 'Abort ARexx' gadget */
	
	do forever
	
	/* process a line */
		
		/* print line number */
		numlinea=c2d(left(bloquebasic,2))
		dummy=writech('fichero','  'numlinea)
		zxamnameformat('     Converting line 'numlinea,'Wait...')
		longline=c2d(reverse(substr(bloquebasic,3,2))) /* reversed Z80 format */
		do i=5 to 4+longline	/* to process the line chars */
		if substr(bloquebasic,i,1)='0e'x then do
			i=i+5
			iterate
			end
		dummy=writech('fichero',zxambasictoken(substr(bloquebasic,i,1)))
		
		if zxamreadabort() then do
			if oldname='' then
				zxamclearnameformat()
			else
				zxamnameformat(oldname,oldformat)
			exit
			end
		
		end i
		
	dummy=writech('fichero','0a'x)
	
	bloquebasic=right(bloquebasic,length(bloquebasic)-(longline+4))
	if bloquebasic='' then break
	end
	
	dummy=close('fichero')
	
	if oldname='' then
		zxamclearnameformat()
	else
		zxamnameformat(oldname,oldformat)
	
	/* restore the status */
	if running=1 then zxamrun()

	exit
