	include "\SrcAll_SH2\BasicMacros.asm"
	include "\SrcAll_SH2\BasicHeader.asm"
		

	;bra FalseTrue
	;nop
	
	;bra Compares
	;nop
	
	;bra CompareStr
	;nop
	
	;bra Stack
	;nop
	

	bra SpecialRegisters
	nop
	
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;	
	

FalseTrue:		;T Flag

	sett 			;Set T flag
	
	;clrt			;Clear T Flag
	
	movt r0			;R0=T (1 or 0)
	
	bsr MonitorR0R1SR
	nop
	
	mov #'T',r0
	bt DoPrintChar		;Branch if true (T=1)
	
	mov #'F',r0
	bf DoPrintChar		;Branch if false (T=0)
	
	mov #'-',r0
	
DoPrintChar:
	bsr PrintChar
	nop

	
	bsr NewLine
	nop
	bsr NewLine
	nop

	
;Loop with DT

	mov #4,r0			;Loop count
	mov #0,r1			;Test Value
	
	bsr MonitorR0R1SR
	nop
	bsr NewLine
	nop
	
RepeatAgain:	
	add #2,r1			;R1=R1+2
	
	dt r0				;Decrement R0, and Test if =0
	bsr MonitorR0R1SR	
	nop
	
	bf RepeatAgain		;Reached Zero (T=1)?... False=Repeat!
				
	bsr NewLine
	nop
	
	bsr MonitorR0R1SR
	nop
	
	
	bsr NewLine
	nop
	
;BT BF with delay slot	
	
	;sett 			;Set T flag
	clrt			;Clear T Flag
	bsr MonitorR0R1SR
	nop
	
	
	bt/s DoPrintChar2	;Branch with delay /slot
	mov #'T',r0			;This is in the delay slot
	
	
	bf/s DoPrintChar2	;Branch with delay /slot
	mov #'F',r0			;This is in the delay slot
	
	mov #'-',r0
DoPrintChar2:

	bsr PrintChar
	nop
	
	
	bra ExampleDone
	nop

	
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;	
	

Compares:
	mov #0,r0
	;mov #1,r0
	;mov #1,r1
	mov #2,r1
	;mov #$FFFFFFFF,r1	;Or -1 signed
	
	bsr MonitorR0R1SR
	nop
	
	;CMP/EQ R0,R1 	;R1=R0  Compare Equal
	;bt PrintEQ
	
	;CMP/GE R0,R1	;R1>=R0 Compare Greater or Equal (signed)
	;bt PrintGE
	
	;CMP/GT R0,R1	;R1>R0  Compare Greater Than (Signed)
	;bt PrintGT
	
	;CMP/HS R0,R1	;R1>=R0  Compare Higher or Same (Unsigned)
	;bt PrintGE
	
	CMP/HI R0,R1 	;R1>R0  Compare Higher (Unsigned)
	bt PrintGT
	
	;CMP/PL R0		;R1>0 	 Compare if Plus
	;bt PrintPL
	
	;CMP/PZ R0		;R1>=0	 Compare if Plus or Zero
	;bt PrintPZ
	
	;CMP/EQ #1,R0	;R0=#	 Compare Equal Immediate (R0 only)
	;bt PrintEQ
	
	mov #'X',r0
	bsr PrintChar		;Branch if true (T=1)	
	nop
	
	bra ExampleDone
	nop

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;	
	

	
CompareStr:
	mov #$11223344,r0
	;mov #$FFEEDD11,r1	;Mot in R0 (11 not in same bytepos)
	mov #$FF22DDCC,r1	;In R0 (22)
	
	bsr MonitorR0R1SR
	nop
	
	CMP/STR R0,R1		;Byte in R1 IN R0 	Compare String
	bt PrintIN
		
	mov #'X',r0
	bsr PrintChar		;Branch if true (T=1)	
	nop
		
	bra ExampleDone
	nop
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;	
	

	
	

	
PrintGE:
	mov #'>',r0
	bsr PrintChar		
	nop
PrintEQ:
	mov #'=',r0
	bsr PrintChar
	nop
	bra ExampleDone
	nop

PrintGT:	
	mov #'>',r0
	bsr PrintChar	
	nop
	bra ExampleDone
	nop
	
PrintPL:
	mov #'>',r0
	bsr PrintChar		
	nop
	mov #'0',r0
	bsr PrintChar		
	nop
	bra ExampleDone
	nop	
	
PrintIN:	
	mov #'I',r0
	bsr PrintChar		
	nop
	mov #'N',r0
	bsr PrintChar		
	nop
	bra ExampleDone
	nop	
	
PrintPZ:
	mov #'>',r0
	bsr PrintChar	
	nop
	mov #'=',r0
	bsr PrintChar	
	nop
	mov #'0',r0
	bsr PrintChar		
	nop
	bra ExampleDone
	nop	
	
	align 4
	ltorg
	align 4


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;	

Stack:
	mov sp,r12			;For our MonitorStack function
	add #-16,r12

	mov #-1,r6			;TestValues
	mov #-2,r7
	mov #-3,r8
	
	bsr MonitorStack
	nop
	
	mov r6,@-sp			;Back up R6
	mov r7,@-sp			;Back up R7
	mov r8,@-sp			;Back up R8
		mov #1,r6
		mov #2,r7
		mov #3,r8
		
		bsr MonitorStack
		nop
	mov @sp+,r7			;Restore R7
	mov @sp+,r8			;Restore R8
	mov @sp+,r6			;Restore R6
	
	bsr MonitorStack
	nop
		
		
		
	mov #-32,r6
	mov #ReturnPos,r7
	bsr SubTest			;PR=PC+4
	nop
ReturnPos:
			
	bra ExampleDone
	nop
	
SubTest:
	sts.l pr,@-sp		;Back up PR (used by BSR/RTS)
		mov r6,@-sp			;Back up R0
			bsr MonitorStack
			nop		
		mov @sp+,r6			;Restore R2
	lds.l @sp+,pr		;Restore PR (used by BSR/RTS)
	rts 				;Return to the caller
	nop
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;	
	
SpecialRegisters:	
	mov #0,r0
	mov #1,r1
	mov #2,r2
	mov #3,r3
	mov #4,r4
	mov #5,r5

;STS/LDS are for MACH,MACL,PR,DSR,A0,X0,X1,Y0,Y1
	lds r3,mach		;Load system reg MACH->R3 (Multiply And Accumulate High value)
	lds r4,macl		;Load system reg MACL->R4 (Multiply And Accumulate Low value)
	lds r5,pr		;Load systen reg PR->R3 (Procedure Register (Return address)

	sts mach,r3		;Store system reg MACH->R3 (Multiply And Accumulate High value)
	sts macl,r4		;Store system reg MACL->R4 (Multiply And Accumulate Low value)
	sts pr,r5		;Store system reg PR->R3 (Procedure Register (Return address)
		
	;PR,DSR,A0,X0,X1,Y0,Y1 are only for the SH-DSP only 

;STC/LDC is for SR,GBR,VBR,MOD,RS,RE
	ldc r0,sr		;Load control reg R0->SR (Flags)
	ldc r1,gbr		;Load control reg R1->GBR (Global Base reg)
	
	;The Saturn won't like this!
	ldc r2,vbr		;Load control reg R2->VBR (Vector Base reg)
	
	stc sr,r0		;Store control reg SR->R0 (Flags)
	stc gbr,r1		;Store control reg GBR->R1 (Global Base reg)
	stc vbr,r2		;Store control reg VBR->R2 (Vector Base reg)
			
	;MOD,RS,RE are only for the SH-DSP only

	
;WARNING! Control registers are privileged... 
;That means you probably shouldn't use them!!!!

	
;WE can backup our registers onto the stack!

	sts.l pr,@-sp	
	sts.l mach,@-sp	
	sts.l macl,@-sp
		stc.l sr,@-sp	
		stc.l gbr,@-sp	
		stc.l vbr,@-sp
		
		ldc.l @sp+,vbr
		ldc.l @sp+,gbr
		ldc.l @sp+,sr
	lds.l @sp+,macl
	lds.l @sp+,mach
	lds.l @sp+,pr
	
	bsr Monitor
	
	
				
	bra ExampleDone
	nop
		
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;	
	
	
ExampleDone:	
	bsr PageFlip			;Flip Buffer (for 32x)
	nop
	
InfLoop:	
	bra	InfLoop
	nop

	
	
;Anything too big for a 16 bit command (EG #$12345678) will be put
; at LTORG automatically by our assembler!
	
	ltorg		;Literal Org, Values too large will be stored here by the assembler 
		
	align 4		;Ensure we're still 32 bit aligned (4 bytes = 32 bit)
	

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;	
	

MonitorStack:
	pushall
		mov sp,r3
		add #60-24,r3	;Point to first pushed reg
		mov #6,r1 	;Reg label
		
		
		mov #3,r5	;Reg per line
		
		mov #3,r4	;Reg count
MonitorAgain3b:
		mov.l @r3,r2	;Get reg
		add #-4,r3		;Move to next reg
		
		bsr ShowReg
		nop
				
		add #1,r1		;Reg label
		
		dt r5
		bf MonitorNoNL3b
		
		mov #3,r5	;Reg per line
		bsr NewLine
		nop
MonitorNoNL3b:
		dt r4
		bf MonitorAgain3b
		
		
		
		mov sp,r2
		mov #'S',r0
		mov #'P',r1
		add #64,r2		;Correct SP
		bsr ShowRegSpec
		nop
		
		mov.l @r3,r2		;Return addr
		mov #'P',r0
		mov #'C',r1
		bsr ShowRegSpec
		nop
		
		mov r6,r2 ;stc SR,r2
		mov #'S',r0
		mov #'R',r1
		bsr ShowRegSpec
		nop
		
		bsr NewLine
		nop
		
		mov #16,r2	;Bytecount
		mov r12,r1	;SourceAddr
		bsr MemDump
		nop
		
		bsr NewLine
		nop
	popall
	rts
	nop
	
MonitorR0R1SR:
	pushall
		stc sr,r6
			mov sp,r3
			add #60,r3	;Point to first pushed reg
			mov #0,r1 	;Reg label
		
				
			mov #2,r4	;Reg count
MonitorAgain2:
			mov.l @r3,r2	;Get reg
			add #-4,r3		;Move to next reg
		
			bsr ShowReg
			nop
				
			add #1,r1		;Reg label
		
			dt r4
			bf MonitorAgain2
		
		
			mov r6,r2 ;stc SR,r2
			mov #'S',r0
			mov #'R',r1
			bsr ShowRegSpec
			nop
			
			mov #' ',r0
			bsr PrintChar
			nop
			
			mov #'T',r0
			ldc r6,sr
			bt MonitorSRT

			mov #'F',r0
MonitorSRT:				
			bsr PrintChar
			nop
			
			bsr NewLine
			nop
		ldc r6,sr
	popall
	rts
	nop
	

Monitor3:
	pushall
		mov sp,r3
		add #60,r3	;Point to first pushed reg
		mov #0,r1 	;Reg label
		
		
		mov #3,r5	;Reg per line
		
		mov #3,r4	;Reg count
MonitorAgain3:
		mov.l @r3,r2	;Get reg
		add #-4,r3		;Move to next reg
		
		bsr ShowReg
		nop
				
		add #1,r1		;Reg label
		
		dt r5
		bf MonitorNoNL3
		
		mov #3,r5	;Reg per line
		bsr NewLine
		nop
MonitorNoNL3:
		dt r4
		bf MonitorAgain3
		
		bsr NewLine
		nop
	popall
	rts
	nop
	
Monitor6:
	pushall
		mov sp,r3
		add #60,r3	;Point to first pushed reg
		mov #0,r1 	;Reg label
		
		mov #3,r5	;Reg per line
		
		mov #6,r4	;Reg count
MonitorAgain6:
		mov.l @r3,r2	;Get reg
		add #-4,r3		;Move to next reg
		
		bsr ShowReg
		nop
				
		add #1,r1		;Reg label
		
		dt r5
		bf MonitorNoNL6
		
		mov #3,r5	;Reg per line
		bsr NewLine
		nop
MonitorNoNL6:
		dt r4
		bf MonitorAgain6
		
		bsr NewLine
		nop
	popall
	rts
	nop
	
	include "\SrcAll_SH2\BasicFooter.asm"
	