/* This script is a mini-monitor created by Leonardo Cocaña Galán */
/* Created from some example scripts of ZXAM, and some ownmade work, */
/* of course! */
/* Will be greatly enhanced.... */

	/* check if the emulator is there */
	address command
	
	if ~show(ports,ZXAM_REXX) then do
		requestchoice 'title "ZXAM Script error..." body "I couldn'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 */

	/* open the output window */
	if ~open('fichero','con:0/11/640/256/Example of a simple monitor','W') then exit
	
	dir=zxamgetreg(pc)
	call desambla(dir,16)
	call registros()

bucle:	/* more? */
	call print ('Disas(S)emble,(N)ewAdd,Set(PC),(P)oke,P(E)ek,(R)un,')
	dummy=writech('fichero','0a'x)
	call print ('(T)race,Brea(K)Point,Re(L)oad,(B)asic,(Q)uit,(F)ind: ')
	respuesta=input()
	respuesta=upper(respuesta)	
	if (respuesta='S'|respuesta='') then do 
	call desambla(dir,16)
	call registros ()
	end
	if respuesta='Q' then signal cleanup
	if respuesta='N' then call newdir()
	if respuesta='P' then call poke()
	if respuesta='R' then do 
		zxamrun()
		dir=zxamgetreg(pc)
		call desambla(dir,16)
		call registros()
	end
	if respuesta='L' then do 
		call reload
		dir=zxamgetreg(pc)
		call desambla(dir,16)
		call registros()
		end
	if respuesta='B' then do 
		call listbasic
		signal bucle
		end
	if respuesta='PC' then 	call setpc
        if respuesta='F'  then  call Findblock
	if respuesta='E'  then  call peek
	if respuesta='T' then do 
		dummy=zxamtrace()
		dir=zxamgetreg(pc)
		call desambla(dir,16)
		call registros()
		end
	if respuesta='K' then call breakpoint
signal bucle

Desambla:

Parse arg direccion,linias
do i=1 to linias 
		linea=zxamdisassemble(direccion,'d')
		dummy=writech('fichero',substr(linea,2))
		dummy=writech('fichero','0a'x)
		direccion=(direccion+left(linea,1))
		if direccion>65535 then direccion=direccion-65536	
dir = direccion
end i

return
registros:
dummy=writech('fichero','0a'x)
dummy=writech('fichero','BC  'zxamgetreg(bc,h))
dummy=writech('fichero','     DE  'zxamgetreg(de,h))
dummy=writech('fichero','     HL  'zxamgetreg(hl,h))
dummy=writech('fichero',"     BC' "zxamgetreg(bc2,h))
dummy=writech('fichero',"     DE' "zxamgetreg(de2,h))
dummy=writech('fichero',"     HL' "zxamgetreg(hl2,h))
dummy=writech('fichero','0a'x)
		
dummy=writech('fichero','IX  'zxamgetreg(ix,h))
dummy=writech('fichero','     IY  'zxamgetreg(iy,h))
dummy=writech('fichero','     SP  'zxamgetreg(sp,h))
dummy=writech('fichero','     PC  'zxamgetreg(pc,h))
dummy=writech('fichero','      I  'zxamgetreg('i',h))
dummy=writech('fichero','        R  'zxamgetreg(r,h))
dummy=writech('fichero','0a'x)
dummy=writech('fichero'," A  "zxamgetreg(a,h))
dummy=writech('fichero',"        F  "zxamgetreg(f,h))
dummy=writech('fichero',"        A' "zxamgetreg(a2,h))
dummy=writech('fichero',"        F' "zxamgetreg(f2,h))

dummy=writech('fichero','       IM'zxamgetreg(im))
dummy=writech('fichero','          INT 'zxamgetreg(int))

dummy=writech('fichero','0a'x)
dummy=writech('fichero','0a'x)
		
dummy=writech('fichero','S   Z   -   H   -  P/V  N   C')
dummy=writech('fichero','0a'x)
		
dummy=writech('fichero',bittst(d2c(zxamgetreg(f)),7))
dummy=writech('fichero','   'bittst(d2c(zxamgetreg(f)),6))
dummy=writech('fichero','   'bittst(d2c(zxamgetreg(f)),5))
dummy=writech('fichero','   'bittst(d2c(zxamgetreg(f)),4))
dummy=writech('fichero','   'bittst(d2c(zxamgetreg(f)),3))
dummy=writech('fichero','   'bittst(d2c(zxamgetreg(f)),2))
dummy=writech('fichero','   'bittst(d2c(zxamgetreg(f)),1))
dummy=writech('fichero','   'bittst(d2c(zxamgetreg(f)),0))
dummy=writech('fichero','0a'x)
dummy=writech('fichero','0a'x)
return
cleanup:

	dummy=close('fichero')

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

	exit
newdir:
call print ('New PC: ')
dir=input()
if (dir>65535 | dir<0)  then signal newdir
call desambla(dir,16)
call registros()
return

peek:
call print ('PEEK: ')
pek=input()
if (dir>65535 | dir<0)  then signal peek
call print (zxampeek(pek))
dummy=writech('fichero','0a'x)
return
poke:
call print ('POKE ')
pok=input()
if length(pok)<7 then signal error_long
dirpoke=left(pok,(lastpos(",",pok)-1))
valor=right(pok,(length(pok)-lastpos(',',pok)))
if (dirpoke<16384 | dirpoke>65535) then signal error_dir
if (valor<0 |valor>255) then signal error_val
zxampoke(dirpoke,valor)
return

Print:
parse arg texto
dummy=writech('fichero',texto)
return

Input:
return readln('fichero')

Reload:
zxamparseloaded(zxampploadfile(zxamjoinpathname(zxamactloadpath(),zxamactname())))
return

Listbasic:
/* this script extracts the BASIC listing in the spectrum's memory... */
	
	/* start address BASIC (variable PROG) */
	baselist=zxamdpeek(23635)
	
	/* length BASIC (with variable VARS) */
	endlist=zxamdpeek(23627)
	
	/* calculate size */
	longbasic=endlist-baselist
	if longbasic=0 then do
		requestchoice '>nil: title "ZXAM Script error..." body "No BASIC program!!" gadgets "AARGH!"'
		return
		end
	
	/* get the whole BASIC area */
	bloquebasic=zxamgetmem(baselist,endlist-baselist)


	do forever	
	/* process a line */
		
		numlinea=c2d(left(bloquebasic,2))
		dummy=writech('fichero','  'numlinea)
		longline=c2d(reverse(substr(bloquebasic,3,2)))
		do i=5 to 4+longline	
		if substr(bloquebasic,i,1)='0e'x then do
			i=i+5
			iterate
			end
		dummy=writech('fichero',zxambasictoken(substr(bloquebasic,i,1)))
		
		
		end i
		
	dummy=writech('fichero','0a'x)
	
	bloquebasic=right(bloquebasic,length(bloquebasic)-(longline+4))
	if bloquebasic='' then break
	end
return

Setpc:

call print ('PC :')
pcdir=input()
if (pcdir>65535 | pcdir<0) then signal setpc
zxamsetreg(pc,pcdir)
dir=zxamgetreg(pc)
call desambla(dir,16)
call registros()
return

Error_dir:

Call Print ('ERROR: ADDRESS OUT OF RANGE (16384-65535)')
dummy=writech('fichero','0a'x)
Signal poke

Error_val:

Call print ('ERROR: VALUE OUT OF RANGE (0-255)')
dummy=writech('fichero','0a'x)
Signal poke

Error_long:

Call print ('ERROR: WRONG PARAMETERS (POKE 60000,255)')
dummy=Writech('fichero','0a'x)
signal poke

error_noblock:

call print ('DATA BLOCK NOT FOUND')
dummy=writech('fichero','0a'x)
return

Findblock:
direc=0
call print ('Enter data to search (hex):') 
block=x2c(input())
bucle1:
call busca(direc)
if direc<=-1 then do
call error_noblock
return
end
call desambla(direc,3)
direc=dir
signal bucle1
return
busca:
parse arg comienzo
direc=zxamfindblock(comienzo,block)
return

breakpoint:
call print('Break Point address: ')
break=input()
if (break<0 |break>65535) then signal breakpoint
zxambreakpoint(break)
return
