; program to allow user examination and setting of ($0)
; 
; allows both simulation of the $0='GDOS' phenomena on some a590s and
; 2091s ( useful for testing code for nil pointer de-references and
; incorrect use of $0 when it should be #$0
;
; also allows clearing of $0, either to remove settings from previous
; use, or due to the a590/2091 itself

forbid		equ	-$084	; offsets for various exec and int calls
getmsg		equ	-$174	; saves long includes and compile times
replymsg	equ	-$17a
waitport	equ	-$180
findtask	equ	-$126
autoreq		equ	-$15c
openlib		equ	-$228
closelib	equ	-$19e

pr_cli		equ	$0Ac	; process cli flag offset
pr_msgport	equ	$05c	; process msgport offset

startup:	move.l	$4,a6
		move.l	#0,a1
		jsr	findtask(a6)		; find ourselves

		move.l	d0,a4			; save our process pointer

		move.l	pr_cli(a4),d0
		bne	fromdos			; was a DOS startup

		; was started from WB

		lea	pr_msgport(a4),a0
		jsr	waitport(a6)		; wait for startup msg
		lea	pr_msgport(a4),a0
		jsr	getmsg(a6)		; get the wb msg

		move.l	d0,wbmsg		; save pointer to our startup msg
		move.l	#1,wb			; flag we started from wb

fromdos:	move.l	$0,d0
		cmp.l	#$0,d0
		beq	is_zero			; if $0=0 then req already setup

		lea	nzbodytext,a0
		move.l	a0,ctext		; set so req displays non-zero msg
		bra	_xx

is_zero:	lea	zbodytext,a0
		move.l	a0,ctext

_xx:		lea	intuiname,a1
		move.l	#0,d0
		move.l	$4,a6
		jsr	openlib(a6) 		; open intuition lib

		move.l	d0,a6			; save ibase
		
		move.l	#0,a0			; window to display in
		move.l	#bodyitext,a1		; body text
		move.l	#yesitext,a2 		; postive text
		move.l	#noitext,a3  		; negative text
		move.l	#0,d0			; pflags
		move.l	#0,d1			; nflags
		move.l	#640,d2			; width
		move.l	#50,d3			; height
		jsr	autoreq(a6)		; request user confirmation

		cmp.l	#0,d0
		beq	cleargdos		; user selected clear

setgdos:	move.l	#"GDOS",$0		; set $0="GDOS"		
		bra	_n1

cleargdos:	move.l	#$0,$0

_n1:		move.l	a6,a1			; close intuition lib
		move.l	$4,a6
		jsr	closelib(A6)
		
exit:		move.l	wb,d0
		cmp.l	#$0,d0			; was this a CLI invocation
		beq	exit_dos

		; workbench exit

		move.l	$4,a6
		jsr	forbid(a6)		; lock out everyone else

		move.l	wbmsg,a1
		jsr	replymsg(a6)		; reply to Wbstartup msg

exit_dos:	move.l	#0,d0			; return with return code 0
		rts			

;=============================================================
		
wb:		dc.l	0			; run mode flag 0=dos process 1=wbprocess
wbmsg:		dc.l	0			; where we save our workbench msg

intuiname:	dc.b	"intuition.library",0

; stuff for our autorequester

bodyitext:	dc.b	0	   	; front pen
		dc.b	1	   	; back pen
		dc.b	0	   	; draw mode
		dc.w	0	   	; leftedge
		dc.w	6	   	; topedge
		dc.l	0	   	; textattr = default
		dc.l	bodytext1  	; actual text
		dc.l	nbodyitext 	; next

nbodyitext:	dc.b	0	   	; front pen
		dc.b	1	   	; back pen
		dc.b	0	   	; draw mode
		dc.w	0	   	; leftedge
		dc.w	20	   	; topedge
		dc.l	0	   	; textattr = default
ctext:		dc.l	0	  	; actual text
		dc.l	0	 	; next


yesitext:	dc.b	2		; front pen
		dc.b	1		; back pen
		dc.b	0		; draw mode
		dc.w	4		; leftedge
		dc.w	4		; topedge
		dc.l	0		; textattr = default
		dc.l	yestext		; actual text
		dc.l	0		; next
		
noitext:	dc.b	2		; front pen
		dc.b	1		; back pen
		dc.b	0		; draw mode
		dc.w	4		; leftedge
		dc.w	4		; topedge
		dc.l	0		; textattr = default
		dc.l	notext		; actual text
		dc.l	0		; next		

		; actual text for our requester

bodytext1:	dc.b	"   SetGDOS by J Davis, 08-1990 v1.0 - an aid to hunting nil pointer de-refs",0
zbodytext:	dc.b	"                  Location $0 currently holds a NULL (0) value",0
nzbodytext:	dc.b	"                     Location $0 is currently NON-ZERO",0

yestext:	dc.b	"Set $0='GDOS'",0
notext:		dc.b	"Set $0=NULL",0

		END
