	.TITLE	DEBUG
	.IDENT	"V1.8"		; Master version number.
	.NLIST	BEX
;
;   Author:	D. Mischler	26-OCT-87
;
;   This program is an assembly-level debugging tool
;   for the PDP-11 to run under RSX-11M-PLUS.
;
	.MCALL	EXIT$S,QIO$,QIOW$
	.MCALL	CLOSE$,FDAT$A,FDBDF$,FDOP$A,FSRSZ$,NMBLK$,PUT$
	.MCALL	CSI$,GCMLB$,GCMLD$,GCML$

	CSI$
	GCMLD$
	FSRSZ$	3
	.PAGE
	.PSECT	DATA,D,RW
;
;   Global data definitions.
;
ABOSTK::.WORD	0		; SIM11 abort stack address (0 if none).
AIDOAV::.WORD	1		; DBGAID odd address SST vector address.
AIDMPV::.WORD	1		; DBGAID memory protect SST vector address.
ATTENB::.WORD	1		; Terminal attachment enable flag.
BRKLST::.WORD	0		; Breakpoint list header.
CNMODE::.WORD	C$SYMB		; Conversion mode.
DSPTYP::.WORD	DT.INS		; Display type (default = INSTRUCTION).
EXAEND::.WORD	0		; End of EXAMINE range.
EXASTA::.WORD	0		; Start of EXAMINE range.
FREMEM::.LIMIT			; Becomes free memory listhead.
GCLBLK:	GCMLB$	2,DBG,CMDBUF,CMDLUN,,TRMLEN+2  ; GCML control block.
TRMPRM	==	GCLBLK+G.DPRM+2	; Terminal prompt text address.
HSTBUF::.WORD	0		; History buffer start address.
HSTEND::.WORD	0		; History buffer end address.
HSTPTR::.WORD	0		; History buffer pointer.
HSTSIZ::.WORD	0		; History buffer size in entries.
LOGCSI::.BLKB	C.SIZE		; CSI control block for log file.
LOGENB::.WORD	0		; Logging enable flag.
LOGNAM::.BLKB	56.		; Log file name buffer.
MCRDEB::.WORD	0		; Flag indicates MCR DEBUG command received.
NCFLGS::.WORD	6*NC.WID!8.	; Default numeric conversion mode flags.
PROLST::.WORD	0		; Profile range list.
RADIX	==	NCFLGS		; Radix in low byte of NCFLGS.
REGSYM::.WORD	R0SYM		; Register symbol list header.
SIMCNT::.WORD	MAXSIM		; Consecutive simulated instruction counter.
SIMENB::.WORD	0		; Simulation enable flag.
SIMVLD::.WORD	0		; Simulated registers valid flag.
SINGLE::.WORD	0		; Single value output mode flag.
STPCNT::.BLKW	1		; Instruction step counter.
SYMHI::	.WORD	-256.		; Highest symbolic value displayed.
SYMLOW::.WORD	256.		; Lowest symbolic value displayed.
SYMOFF::.WORD	8192.		; Maximum symbol offset displayed.
SYMTBL::.WORD	0		; Main symbol table head.
TRCLST::.WORD	0		; Tracepoint list header.
TRCTRP::.WORD	0		; Trace trap enable flag.
TRMENB::.WORD	1		; Terminal output enable flag.
TSKCTX::.WORD	0		; Task context buffer address.
USRCMD:	.WORD	0		; Flag indicates current command typed by user.
VFYENB::.WORD	0		; Command verification enable flag.
WCHLST::.WORD	0		; Watchpoint list header.
;
;   Simulated registers (must stay in this order).
;
SIM.R0::.BLKW	6		; Simulated R0 through R5.
SIM.SP::.BLKW	1		; Simulated SP.
SIM.PC::.BLKW	1		; Simulated PC.
SIM.PS::.BLKW	1		; Simulated PS.
	.PAGE
;
;   Numeric conversion format table for EVAL and SET MODE.
;
FMTTBL::.WORD	5*NC.WID!NC.SGN!10.	; Signed decimal.
DECFMT::.WORD	5*NC.WID!10.		; Unsigned decimal.
BINFMT::.WORD	16.*NC.WID!NC.LDZ!2	; Binary.
OCTFMT::.WORD	6*NC.WID!NC.LDZ!8.	; Octal.
HEXFMT::.WORD	4*NC.WID!NC.LDZ!16.	; Hexadecimal.
	.WORD	0			; Table terminator.

;
;   Log file prompt and terminal buffers.
;
CMDBUF::.BLKB	TRMLEN+10	; Command input buffer.
LOGPRM::.ASCII	"DBG>"		; Log file prompt (contiguous with TRMBUF).
TRMBUF::.BLKB	TRMLEN+10	; Terminal I/O buffer.
	.EVEN
;
;   Log file FDB.
;
LOGFDB::
	FDBDF$
	FDAT$A	R.VAR,FD.CR				; File attributes.
	FDOP$A	LOGLUN,LOGCSI+C.DSDS,LOGDFB,FO.WRT	; File open data.
LOGOPN	==	LOGFDB+F.BDB		; Log file enable flag.

LOGDFB:	NMBLK$	DEBUG,LOG,0,SY,0	; Default filename block for log file.

;
;   Terminal I/O QIO DPBs.
;
ATTDET:	QIO$	IO.ATT,TI
NEWLIN:	QIOW$	IO.WVB,TI,IOEFN,,,,<CRLF,2>
TRMOUT::QIOW$	IO.WVB,TI,IOEFN,,,,<0,0>
CRLF:	.BYTE	CR,LF		; Carriage return/line feed.

;
;   Register symbol definitions.
;
R0SYM:	.WORD	1$,^RR0 ,0,TC.R0	; R0 symbol definition.
1$:	.WORD	2$,^RR1 ,0,TC.R1	; R1 symbol definition.
2$:	.WORD	3$,^RR2 ,0,TC.R2	; R2 symbol definition.
3$:	.WORD	4$,^RR3 ,0,TC.R3	; R3 symbol definition.
4$:	.WORD	5$,^RR4 ,0,TC.R4	; R4 symbol definition.
5$:	.WORD	6$,^RR5 ,0,TC.R5	; R5 symbol definition.
6$:	.WORD	7$,^RSP ,0,TC.SP	; SP symbol definition.
7$:	.WORD	8$,^RPC ,0,TC.PC	; PC symbol definition.
8$:	.WORD	9$,^RPS ,0,TC.PS	; PS symbol definition.
9$:	.WORD	0,^R$DS,^RW  ,TC.DSW	; $DSW symbol definition.
	.PAGE
	.PSECT	CODE,I,RO
;
;   Begin execution here.
;
DEBUG::	CALL	INIT		; Initialize just about everything, normal?
	BCC	10$		; Yes, skip weird stuff.
	CALL	X$STRT		; Display abnormal startup packet type.
10$:	CLR	MCRDEB		; Clear MCR DEBUG command flag.
	CALL	GETCMD		; Get a command line, EOF?
	BCS	EXIT		; Yes, terminate.
	CALL	LOGCMD		; Write command line to log file.
	CMPB	#';,(R0)	; Is the line a comment?
	BEQ	10$		; Yes, don't try to parse it.
	MOV	#CMDTBL,R1	; Point to top-level keyword (command) table.
	CALL	U$DCOD		; Decode keyword, OK?
	BCC	20$		; Yes, process the command.
	MOV	#ERROR,R2	; Point to error handling routine.
20$:	CALL	(R2)		; Process command.
	BR	10$		; Get another.

;   Terminate execution.
EXIT:	CALL	T$EXIT		; Terminate current target task.
ABORT::	CLOSE$	#LOGFDB		; Make sure the log file is closed.
	EXIT$S			; Terminate execution.
	.PAGE
;
;   Subroutine to get a command line.
;   On exit:	R0 points to command line, carry set if EOF.
;
GETCMD:	JSR	R5,.SAVR1	; Save R1 - R5.
100$:	MOV	#GCLBLK,R0	; Point to GCML command block.
	BISB	#GE.LC,G.MODE(R0); Enable lower-case input.
	BICB	#GE.COM,G.MODE(R0); Pass comment lines for logging.
	GCML$			; Get a command line, OK?
	BCC	110$		; Yes, fix it up for parsing.
	CMPB	#GE.EOF,G.ERR(R0) ; Did an EOF occur?
	BEQ	120$		; Yes, set carry and exit.
	MOV	#E.CMDF,R1	; Point to command file error message.
	CALL	ERROR		; Display error message.
	BR	100$		; Try again.
;   Regularize the acquired command line.
110$:	MOV	G.CMLD(R0),R1	; Get length of command line.
	MOV	G.CMLD+2(R0),R0	; Point to start of command line.
	ADD	R0,R1		; Point to end of data.
	CLRB	(R1)		; Terminate input buffer.
	CALL	U$CLFX		; Compress and make upper case.
	CMP	R0,R1		; Empty line?
	BEQ	100$		; Yes, try again.
	MOVB	GCLBLK+F.RCTL,R1; Get current input device characteristics.
	BIC	#^C<FD.TTY>,R1	; Leave only the terminal device bit.
	MOV	R1,USRCMD	; Save user command flag.	
	CLC			; Indicate success.
	RETURN
;   EOF reached.
120$:	SEC			; Indicate end of input.
	RETURN
;
;   Write command line to log file if logging is enabled.
;   Verify command if not a user command and verifying is enabled.
;
LOGCMD:	MOV	TRMENB,-(SP)	; Save terminal output enable flag.
	TST	USRCMD		; Was command typed by user?
	BNE	10$		; Yes, see if logging is enabled.
	TST	VFYENB		; Is command verification enabled?
	BEQ	40$		; No, just exit.
	BR	20$		; Verify the command line.
;   Log the command line.
10$:	CLR	TRMENB		; Disable terminal output.
;   Verify the command line.
20$:	MOV	R0,-(SP)	; Save command line address.
	MOV	#TRMBUF,R1	; Point to terminal output buffer.
30$:	MOVB	(R0)+,(R1)+	; Copy command line into place.
	BNE	30$
	MOV	(SP)+,R0	; Recover command line address.
	MOV	#LOGPRM,R1	; Point to log file "prompt".
	CALL	LINOUT		; Write command line to log file.
40$:	MOV	(SP)+,TRMENB	; Restore terminal output enable flag.
	RETURN
	.PAGE
;
;   Output a line of text to the terminal and log file.
;   On entry:	R1 points to ASCIZ text.
;
LINOUT::
	CALL	$SAVAL		; Save all registers.
	MOV	R1,R2		; Copy text pointer.
10$:	TSTB	(R2)+		; Found end of text?
	BNE	10$		; No, keep looking.
	DEC	R2		; Back up pointer.
	SUB	R1,R2		; Produce text length.
	TST	LOGENB		; Is the log file enabled?
	BEQ	20$		; No, take care of the terminal.
	PUT$	#LOGFDB,R1,R2	; Write record to the log.
20$:	TST	TRMENB		; Is terminal output enabled?
	BEQ	50$		; No, just exit.
	CMP	#TRMBUF,R1	; Is line in normal buffer?
	BNE	30$		; No, handle CR/LF the slow way.
;   Line is in TRMBUF: handle it as an optimal case.
	SUB	#2,R1		; Point back to CR/LF area.
	MOV	(R1),-(SP)	; Save current contents.
	MOV	CRLF,(R1)	; Put a CR/LF in place.
	ADD	#2,R2		; Adjust length accordingly.
	CALL	40$		; Send line to terminal.
	MOV	(SP)+,(R1)	; Repair damaged location.
	RETURN
;   Send the line to the terminal.
30$:	DIR$	#NEWLIN		; Work on a new terminal line.
40$:	MOV	R1,TRMOUT+Q.IOPL; Save parameters in DPB.
	MOV	R2,TRMOUT+Q.IOPL+2
	DIR$	#TRMOUT		; Output line to the terminal.
50$:	RETURN
	.PAGE
;
;   Subroutine to attach the terminal for output.
;
TRMATT::
	MOV	#IO.ATT,ATTDET+Q.IOFN	; Set up I/O function.
	BR	ATTCHK			; Enter common code.
;
;   Subroutine to detach the terminal.
;
TRMDET::
	MOV	#IO.DET,ATTDET+Q.IOFN	; Set up I/O function.
ATTCHK:	TST	TRMENB			; Is terminal output enabled?
	BEQ	10$			; No, just exit.
	TST	ATTENB			; Is terminal attachment enabled?
	BEQ	10$			; No, get out.
	DIR$	#ATTDET			; Attach/detach the terminal.
10$:	RETURN



	.PSECT	RODATA,D,RO
;
;   Top-level command keyword table.
;
CMDTBL:
	KEYWRD	CANCEL,CANCEL
	KEYWRD	DEFINE,DEFINE
	KEYWRD	DEPOSIT,DPOSIT
	KEYWRD	EVALUATE,EVAL
	KEYWRD	EXAMINE,EXAM
	KEYWRD	EXIT,EXIT
	KEYWRD	GO,GO
	KEYWRD	HELP,HELP
	KEYWRD	LOAD,LOAD
	KEYWRD	QUIT,EXIT
	KEYWRD	SET,SET
	KEYWRD	SHOW,SHOW
	KEYWRD	STEP,STEP
	KEYWRD	UNDEFINE,UNDEF
	.WORD	0

;
;   Master data type table associates data type codes with keywords.
;
TYPTBL::
	KEYWRD	ADDRESS,DT.ADR
	KEYWRD	ASCII,DT.ASC
	KEYWRD	BYTE,DT.BYT
	KEYWRD	INSTRUCTION,DT.INS
	KEYWRD	RAD50,DT.R50
	KEYWRD	WORD,DT.WRD
	.WORD	0

	.END	DEBUG
