;
; stream
;
; Debugging with VDUStream
;   VDUStream is copyright Computer Concepts.  Distribution terms appear to
;   be ambiguous.
;
;  1994-1998 Straylight
;

;----- Licensing note -------------------------------------------------------
;
; This file is part of Straylight's core library (corelib).
;
; Corelib is free software; you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation; either version 2, or (at your option)
; any later version.
;
; Corelib is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
; GNU General Public License for more details.
;
; You should have received a copy of the GNU General Public License
; along with Corelib.  If not, write to the Free Software Foundation,
; 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

;----- Overview -------------------------------------------------------------
;
; Macros provided:
;
;   SAVE
;   LOAD
;   BPT
;   S
;   REGH
;   REGD
;   REGS
;   REGC
;   RDUMP

; --- Macro: SAVE ---
;
; Arguments:	--
;
; Use:		Saves R0, R14 and the current flags so that they can be
;		restored by a subsequent LOAD.

		MACRO
$label		SAVE
$label		STMFD	R13!,{R0,R14}
		STMFD	R13!,{PC}
		MEND

; --- Macro: LOAD ---
;
; Arguments:	--
;
; Use:		Restores R0, R14 and the flags after a SAVE.

		MACRO
$label		LOAD
$label		LDMFD	R13!,{R14}
		TEQP	R14,#0
		LDMFD	R13!,{R0,R14}
		MEND

; --- Macro: BPT ---
;
; Arguments:	msg == message to print
;		cc == condition
;
; Use:		Inserts a Sledgehammer breakpoint in the code.

		MACRO
$label		BPT	$msg,$cc
$label
		[	"$cc"<>""
		ADD$cc	PC,PC,#0
		ADD	PC,PC,#(36+:LEN:"$msg"+4) :AND: -4
		]
		SAVE
		SWI	XOS_WriteI+26
		SWI	XOS_WriteI+4
		SWI	XOS_WriteI+12
		SWI	XOS_WriteS
		DCB	"$msg",0
		SWI	XOS_NewLine
		SWI	Sledgehammer_BreakPoint
		LOAD
		MEND

; --- Macro: S ---
;
; Arguments:	str == a string to display
;
; Use:		Writes a given string to VDUStream.

		MACRO
$label		S	$str
$label		SAVE
		SWI	Stream_WriteS
		DCB	"$str",13,10,0
		LOAD
		MEND

; --- Macro: REGH ---
;
; Arguments:	r == the register symbol to display
;		str == an optional name for the value
;
; Use:		Writes to VDUStream the value of the given register as
;		a hex value.

		MACRO
$label		REGH	$r,$str
$label		SAVE
		MOV	R0,$r
		SWI	Stream_WriteS
		[ "$str" <> ""
		DCB	"$str == &",0
		|
		DCB	"$r == &",0
		]
		SWI	Stream_WriteH32
		SWI	Stream_NewLine
		LOAD
		MEND

; --- Macro: REGC ---
;
; Arguments:	r == the register symbol to display
;		str == an optional name for the value
;
; Use:		Writes to VDUStream the value of the given register as
;		a character.

		MACRO
$label		REGC	$r,$str
$label		SAVE
		MOV	R0,$r
		SWI	Stream_WriteS
		[ "$str" <> ""
		DCB	"$str == `",0
		|
		DCB	"$r == `",0
		]
		SWI	Stream_WriteT8
		MOV	R0,#'''
		SWI	Stream_WriteT8
		SWI	Stream_NewLine
		LOAD
		MEND

; --- Macro: REGD ---
;
; Arguments:	r == the register symbol to display
;		str == an optional name for the value
;
; Use:		Writes to VDUStream the value of the given register as
;		a decimal value.

		MACRO
$label		REGD	$r,$str
$label		SAVE
		MOV	R0,$r
		SWI	Stream_WriteS
		[ "$str" <> ""
		DCB	"$str == ",0
		|
		DCB	"$r == ",0
		]
		SWI	Stream_WriteD32
		SWI	Stream_NewLine
		LOAD
		MEND

; --- Macro: REGS ---
;
; Arguments:	r == the register symbol to display
;		str == an optional name for the value
;
; Use:		Writes to VDUStream the null terminated string pointed to
;		by the given register.

		MACRO
$label		REGS	$r,$str
$label		SAVE
		MOV	R0,$r
		SWI	Stream_WriteS
		[ "$str" <> ""
		DCB	"$str == `",0
		|
		DCB	"$r == `",0
		]
		SWI	Stream_Write0
		MOV	R0,#'''
		SWI	Stream_WriteT8
		SWI	Stream_NewLine
		LOAD
		MEND

; --- Macro: RDUMP ---
;
; Arguments:	str == an optional heading string for the register dump
;
; Use:		Displays a total register dump.

		MACRO
$label		RDUMP	$str
		IMPORT	stream_regDump
$label		STMFD	R13!,{R0-PC}
		ADD	R0,R13,#16*4
		STR	R0,[R13,#13*4]
		[	"$str" <> ""
		SWI	Stream_WriteS
		DCB	"$str",10,13,0
		]
		MOV	R0,R13
		BL	stream_regDump
		LDMIA	R13,{R0-R14}
		MEND

;----- That's all, folks ----------------------------------------------------

		END
