	.TITLE	INIT
	.IDENT	"V1.2"
	.NLIST	BEX
;
;   Author:	D. Mischler	15-JUN-87
;
;   This module contains the code to initialize the debugger.
;
; V1.1	D. Mischler  26-MAY-88	Modify DBGAID address violation vectors.
; V1.2	D. Mischler  26-JUL-88	Check startup packet type.
;
	.MCALL	DIR$,EXIT$S,GMCR$,GTSK$S,QIOW$S

	.PSECT	DATA,D,RW
;
;   Data definitions.
;
CR	=	15		; ASCII carriage return.
LF	=	12		; ASCII line feed.

GETMCR:	GMCR$			; Get MCR command line DPB.

INIMSG:	.ASCII	<LF>"DBG Assembly Level Debugger "
VRSION:	.ASCIZ	"vrsion - tsknam"

TSKPRM:	.ASCII	<CR><LF>"Target task? "
TSKPRL	=	.-TSKPRM
	.EVEN
	.PAGE
	.PSECT	CODE,I,RO
;
;   Perform one-time initialization.
;   On entry:	R3 & R4 contain task version.
;   On exit:	R0 points to startup response packet.
;		If packet type is not RT.STU then C will be set.
;
INIT::	MOV	#VRSION,R0		; Point to version number.
	MOV	R3,R1			; Get first version word.
	CALL	$C5TA			; Make it ASCII.
	MOV	R4,R1			; Get second version word.
	CALL	$C5TA			; Make it ASCII too.
	CALL	U$RMTB			; Remove trailing blanks.
	CLR	-(SP)			; Stack a null pointer word.
	MOV	R0,-(SP)		; Save message pointer for later.
	GTSK$S	#TRMBUF			; Get task parameters.
	MOV	#LOGPRM,R0		; Point to log file prompt.
	MOV	TRMBUF,R1		; Get hi word of task name.
	CALL	$C5TA			; Convert it to ASCII.
	MOV	#TRMPRM,R0		; Point to terminal prompt.
	MOV	TRMBUF,R1		; Get hi word of task name.
	CALL	$C5TA			; Convert it to ASCII.
GETSKN:	MOV	#GETMCR,R0		; Point to GMCR$ DPB.
	DIR$	R0			; Get MCR command line, OK?
	BCC	10$			; Yes, get target task name.
	MOV	#<"D >,G.MCRB(R0)	; Set up a dummy command.
	MOV	#TSKPRM,TRMOUT+Q.IOPL	; Stuff message address
	MOV	#TSKPRL,TRMOUT+Q.IOPL+2	;  and length in DPB.
	DIR$	#TRMOUT			; Prompt for task name.
	SUB	#4,SP			; Make room for an I/O status block.
	MOV	SP,R5			; Point to it.
	QIOW$S	#IO.RLB,#TI,#IOEFN,,R5,,<#GETMCR+G.MCRB+2,#20.>
	MOV	(SP)+,R0		; Get completion status.
	ADD	#2,(SP)			; Add extra characters to length.
	MOV	(SP)+,$DSW		; Make it look like GMCR$ result.
	CMPB	#IS.SUC,R0		; Did any driver errors occur?
	BEQ	10$			; No, parse result.
	CMPB	#IE.EOF,R0		; Did the user type ^Z?
	BNE	GETSKN			; No, try again.
	EXIT$S				; Terminate.
;   Parse command line for task name.
10$:	MOV	#GETMCR+G.MCRB,R0	; Point to command line buffer.
	MOV	$DSW,R1			; Get command line length.
	ADD	R0,R1			; Point to end of buffer.
	CLRB	(R1)			; Terminate buffer.
	CALL	U$CLFX			; Fix up command line.
	CALL	U$FNXT			; Point to task name, OK?
	BCS	GETSKN			; No, try again.
	CALL	U$SYMN			; Pack task name into RAD50, OK?
	BCC	20$			; Yes, go for it.
	MOV	#E.TSKN,R1		; Get error message info.
	CALL	ERROR			; Display error message.
	BR	GETSKN			; Let the poor luser try again.
	.PAGE
;
;   The task name looks OK. Try to talk to DBGAID.
;
20$:	MOV	R2,TSKNAM		; Save preliminary task name words.
	MOV	R3,TSKNAM+2
	CALL	T$STRT			; Initiate task communication.
	MOV	R0,2(SP)		; Save response packet address.
;   Target task communications have been established.
	MOV	(SP)+,R0		; Pop initialization message pointer.
	MOVB	#' ,(R0)+		; Delimit version and task name.
	MOVB	#'-,(R0)+
	MOVB	#' ,(R0)+
	MOV	TSKNAM,R1		; Get high word of target task name.
	CALL	$C5TA			; Convert it to ASCII.
	MOV	TSKNAM+2,R1		; Get low word.
	CALL	$C5TA			; Convert it too.
	CLRB	(R0)			; Terminate message.
	MOV	#INIMSG,R1		; Point to startup message.
	CALL	LINOUT			; Display it.
;   Modify DBGAID address violation SST vectors to catch illegal accesses.
	MOV	$DBG$V,AIDOAV		; Save DBGAID odd address vector.
	MOV	#OAVSST,$DBG$V		; Set up special vector.
	MOV	$DBG$V+2,AIDMPV		; Save DBGAID memory protect vector.
	MOV	#MPVSST,$DBG$V+2	; Set up special vector.
;   Initialize the free memory list.
	MOV	#FREMEM,R0	; Point to free memory list head.
	CLR	(R0)		; Empty the free memory list.
	MOV	2(R0),R2	; Get address of possible free memory.
	MOV	R2,R1		; Copy it.
	ADD	#63.,R1		; Adjust to next multiple of 64 bytes.
	BIC	#63.,R1
	MOV	R1,2(R0)	; Save next free address.
	SUB	R2,R1		; Calculate size of free memory.
	CMP	R1,#4		; Is it usable?
	BLO	100$		; No, throw it away.
	CALL	$RLCB		; Put initial free memory on free list.
100$:	MOV	(SP)+,R0	; Recover response packet address.
	CMP	#RT.STU,R.TYP(R0) ; Is it a normal startup packet?
	BEQ	110$		; Yes, exit with carry clear.
	SEC			; Indicate an abnormal startup packet.
110$:	RETURN
	.END
