.include "\SrcAll\Header.asm"
.include "\SrcAll\BasicMacros.asm"
  
CursorX equ 0x100 
CursorY equ 0x101
  

	jal Cls				;Zero Graphics cursor pos
	nop
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  
  
.macro MonitorA0A1RAPCm
	push ra
		move t6,ra
		jal MonitorA0A1RAPC
		nop
	pop ra
.endmacro


	li ra,0
	MonitorA0A1RAPCm	;Show Starting point
	jal NewLine  
	nop
	
	;j TestBasicMaths
	;j TestMemory
	
	;j TestJumps
	
	j TestDelays

	nop
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;  
TestBasicMaths:	
	
    li a0,192			;Load 192 in decimal (0xC0) into A0
    li a1,0xFFEEDDCC	;Load a hexadecimal value into A1
	li a2,'A'			;load 'A' in ascii (0x41) into A2
	
	jal MonitorA0A1A2 	; Show A0,A1,A2 to screen
	nop
  

testSym equ 3

TestLabel:

	li a0,testSym		;Load a symbol
    li a1,testSym		;Load a symbol
	
	la a2,TestLabel 	;Load an address
	
	jal MonitorA0A1A2 	;Show A0,A1,A2 to screen
	nop
	
	
	li a0,0x01020304	;Load a hexadecimal value into A0
	
	move a1,a0			;Move A0->A1
	move a2,a0			;Move A0->A2
	
	jal MonitorA0A1A2 	;Show A0,A1,A2 to screen
	nop
	
	
	jal NewLine
	nop
	li a1,0
	li a2,0
	

	lui a0,0xFEDC		;Load upper immediate
	
	jal MonitorA0A1A2 	;Show A0,A1,A2 to screen
	nop
	
	
	jal NewLine
	nop
	
	
	li a0,192			;Load 192 in decimal (0xC0)
	
	addi a1,a0,1		;Add 1 to A0, store result in A1
	addi a2,a0,-1		;Subtract 1 from A0, store result in A2
	
	jal MonitorA0A1A2 	;Show A0,A1,A2 to screen
	nop
		
	
	jal NewLine
	nop
	
	
	li a0,192			;Load 192 in decimal (0xC0)
	li a1,0
	li a2,0x00000100	;Value to add or subtract
	jal MonitorA0A1A2 	;Show A0,A1,A2 to screen
	nop

	add a1,a0,a2		;A1 = A0+A2
	sub a0,a0,a2		;A0 = A0-A2
	
	jal MonitorA0A1A2 
	nop
	
	j Shutdown
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;  

TestMemory:
	li a1,0				;Clear a1
	
	la t7,TestData		;Address
	li t6,2				;Line Count
	jal MemDump			;Dump Ram to screen
	nop
	jal NewLine  
	nop
	
	li a1,0x12345678	;Test Value
	
	la a2,TestData		;Load address of TestData
	
	lw a0,(a2)			;Load Word into A0 from address in A2
	
	jal MonitorA0A1A2 	
	nop
	
	sw a1,(a2)			;Store Word from A1 into address in A2

	li t6,2				;Line Count
	jal MemDump			;Dump Ram to screen
	nop

	j Shutdown
	nop

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

TestJumps:	

	j JumpTest1			;Jump to a label
	nop					;No Operation in delay slot
ReturnFromJumpB:
	
	
	la a0,JumpTest		;Jump to address in Register
	jr a0
	nop					;No Operation in delay slot
ReturnFromJump:


	jal JumpTest3		;Jump And Link to label (PC->RA)
	nop					;No Operation in delay slot

	
	la a0,JumpTest4		
	jalr a0				;Jump And Link to address in Register
	nop					;No Operation in delay slot
	
	la a0,JumpTest4b
	jalr a0,a1			;Jump And Link to label (PC->A1)
	nop					;No Operation in delay slot
	
	
	j Shutdown
	nop
	
JumpTest:
	MonitorA0A1RAPCm
	la a0,JumpToRegister
	jal PrintString
	nop
	push ra
		jal NewLine       
		nop
		jal NewLine       
		nop
	pop ra
	j ReturnFromJump
	nop
  
JumpTest1:
	MonitorA0A1RAPCm
	la a0,JumpTo
	jal PrintString
	nop
	push ra
		jal NewLine       
		nop
		jal NewLine       
		nop
	pop ra
	j ReturnFromJumpB
	nop

	
JumpTest3:
	MonitorA0A1RAPCm
	addi sp,sp,-4		;Back up RA
	sw ra,0(sp)			;Back up RA
		la a0,JumpAndLinkTo
		jal PrintString
		nop
		jal NewLine       
		nop
		jal NewLine       
		nop
	lw ra,0(sp)			;Restore RA
	addi sp,sp,4		;Restore RA
	
	jr ra				;Return to caller 
	nop					;No Operation in delay slot
	
JumpTest4:
	MonitorA0A1RAPCm
	push ra
		la a0,JumpAndLinkToRegister
		jal PrintString
		nop
		jal NewLine       
		nop
		jal NewLine       
		nop
	pop ra
	jr ra
	nop
	
JumpTest4b:
	MonitorA0A1RAPCm
	addi sp,sp,-4		;Back up A1
	sw a1,0(sp)			;Back up A1
		la a0,JumpAndLinkToRegister
		jal PrintString
		nop
		jal NewLine       
		nop
		jal NewLine       
		nop
	lw a1,0(sp)			;Restore A1
	addi sp,sp,4		;Restore A1
	
	jr a1 				;return to address in A1
	nop					;No Operation in delay slot
	
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
	
	
TestDelays:

; Load Delay slot test - Emulators don't emulate this - so beware!!!
	la a0,TestData		;Load a symbol
	
	lw a1,(a0)			;Load A1 from the address in A0 - 
						 ;This may take more than one command's time
	ori a1,a1,0xFF		;Load Delay slot - This may not occur correctly

;Note: The N64 R4000 processor does not have this limitation 
	; (The command will be slower, but no error will occur)

	;  The PSX R3000 processor DOES have load delays, but it is not simulated
	;  By the emulator

; Branch Delay slot test - Emulators do emulate this (if you turn it on!)
	
	jal MonitorA0A1A2 	; Show A0,A1,A2 to screen
	nop
	
	jal MonitorA0A1A2 	; Show A0,A1,A2 to screen
	ori a2,a2,0x00FF	; In delay slot, occurs BEFORE JAL
	
	jal MonitorA0A1A2 	; Show A0,A1,A2 to screen
	nop
	ori a2,a2,0xFF00	; after delay slot, occurs AFTER JAL
	
	jal NewLine         
	nop
		
	li a0,0x4
LoopTest1:	
	jal MonitorA0A1A2 	; Show A0,A1,A2 to screen
	nop
	addi a0,a0,-1
	bne a0,0,LoopTest1
	nop					;We didn't use the delay slot
	
	jal NewLine         
	nop
	
	li a0,0x4
LoopTest2:
	jal MonitorA0A1A2 	; Show A0,A1,A2 to screen
	nop
	bne a0,0,LoopTest2
	addi a0,a0,-1		;Loop DEC now in delay slot!
						;Runs AFTER the check, so a 
						;repeat occurs for Zero
	
	jal NewLine  	
	nop					; I'm putting a NOP in the delayslots for simplicity... but we can use them!
	j Shutdown
	nop
	
Shutdown:
	j Shutdown
	
MonitorA0A1RAPC:
	pushAs
	push ra
		push t6
			li t5,'a'
			li t6,'0'
			move t7,a0
			jal ShowRegTextB                ;A5 A6 : A7
			nop
			li t5,'a'
			li t6,'1'
			move t7,a1
			jal ShowRegTextB                ;A5 A6 : A7
			nop
			
			jal NewLine         
			nop			
			li t5,'r'
			li t6,'a'
		pop t7
		jal ShowRegTextB                ;A5 A6 : A7
		nop
		li t5,'p'
		li t6,'c'
	pop t7
	push t7
		jal ShowRegTextB                ;A5 A6 : A7  
		nop
		jal NewLine         
		nop
	pop ra
	popAs
	jr ra
	nop
 
MonitorA0A1A2:
	push ra
	pushAs
	pushTs
	    li t5,'a'
		li t6,'0'
		move t7,a0
		jal ShowRegTextB                ;A5 A6 : A7
		nop
		li t5,'a'
		li t6,'1'
		move t7,a1
		jal ShowRegTextB                ;A5 A6 : A7
		nop
		li t5,'a'
		li t6,'2'
		move t7,a2
		jal ShowRegTextB                ;A5 A6 : A7
		nop
		jal NewLine         
		nop
	popTs
	popAs
	pop ra
	jr ra
	nop
  
TestData:	;Little Endian - so this is F3F2F1F0
	.byte 0xF0,0xF1,0xF2,0xF3	
	.byte 0,0,0,0,0,0,0,0,0,0,0,0
	
JumpAndLinkTo: 
  .ascii "JAL "
  .byte 255 
JumpTo: 
  .ascii "J   "
  .byte 255 
JumpToRegister: 
  .ascii "JR  "
  .byte 255 
JumpAndLinkToRegister: 
  .ascii "JALR"
  .byte 255 
  
  
  
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  
MyFont:

.ifdef buildn64				;Can't use EQU to define with ifdef - use DefineLabel because reasons!
	.incbin "\ResN64\ChibiAkumas.fnt"
.endif
.ifdef buildPSX				;Can't use EQU to define with ifdef - use DefineLabel because reasons!
	.incbin "\ResPSX\ChibiAkumas.fnt"
.endif

.include "\SrcALL\graphics.asm"
	
.include "..\\SrcAll\monitor.asm"  	
.include "\SrcN64\Footer.asm"