; -------------------------------------------------	
;			PROJECT	"itest"
; -------------------------------------------------	

include "../../../jasm/cregs.inc.jsm"

; -------------------------------------------------	
				xmem
msg:			dc		"Hallo", 0
xram:			db		256

				cmem	0
cram:			db		256

reg_revision:	equ		0x00			
reg_testid:		equ		0x01			

; -------------------------------------------------	
			code
			org		0x000
reset:		jmp		start

; -------------------------------------------------	
			org		0x001
int_vec:	jmp		isr

; -------------------------------------------------	
			org		0x010
			
start:		call	init
			cin		R0, (cpu_revision)
			xout	(reg_revision), R0
			
			; Test 0
			call	test0
			
			; Test 1
			mov		R0, 1
			xout	(reg_testid), R0
			call	test1
			
			; Test 2
			mov		R0, 2
			xout	(reg_testid), R0
			call	test2

			; Test 3
			mov		R0, 3
			xout	(reg_testid), R0
			call	test3

			; Test 4
			mov		R0, 4
			xout	(reg_testid), R0
			call	test4

			; Test 5
			mov		R0, 5
			xout	(reg_testid), R0
			call	test5

			; Test 6
			mov		R0, 6
			xout	(reg_testid), R0
			call	test6

			; Test 7
			mov		R0, 7
			xout	(reg_testid), R0
			call	test7

			; Test 8
			mov		R0, 8
			xout	(reg_testid), R0
			call	test8

			; Test 9
			mov		R0, 9
			xout	(reg_testid), R0
			call	test9
			
			; Test 10
			mov		R0, 10
			xout	(reg_testid), R0
			call	test10

			; Test 11
			mov		R0, 11
			xout	(reg_testid), R0
			call	test11

			; Test 12
			mov		R0, 12
			xout	(reg_testid), R0
			call	test12

			; End of test
			mov		R0, 0xFF
			xout	(reg_testid), R0
			
			jmp		ok

; -------------------------------------------------	
init:		mov		R0, 0
			mov		R1, 0
			mov		R2, 0
			mov		R3, 0
			mov		R4, 0
			mov		R5, 0
			mov		R6, 0
			mov		R7, 0
			mov		R8, 0
			mov		R9, 0
			mov		R10, 0
			mov		R11, 0
			mov		R12, 0
			mov		R13, 0
			mov		R14, 0
			mov		R15, 0
			ret

; -------------------------------------------------	
; MOV|R|K
; MOV|R|R
test0:		mov		R0, 0x10
			cmp		R0, 0x10
			jne		error
			add		R0, 1

			mov		R1, R0
			cmp		R1, 0x11
			jne		error
			add		R1, 1

			mov		R2, R1
			cmp		R2, 0x12
			jne		error
			add		R2, 1

			mov		R3, R2
			cmp		R3, 0x13
			jne		error
			add		R3, 1

			mov		R4, R3
			cmp		R4, 0x14
			jne		error
			add		R4, 1

			mov		R5, R4
			cmp		R5, 0x15
			jne		error
			add		R5, 1

			mov		R6, R5
			cmp		R6, 0x16
			jne		error
			add		R6, 1

			mov		R7, R6
			cmp		R7, 0x17
			jne		error
			add		R7, 1

			mov		R8, R7
			cmp		R8, 0x18
			jne		error
			add		R8, 1

			mov		R9, R8
			cmp		R9, 0x19
			jne		error
			add		R9, 1

			mov		R10, R9
			cmp		R10, 0x1A
			jne		error
			add		R10, 1

			mov		R11, R10
			cmp		R11, 0x1B
			jne		error
			add		R11, 1

			mov		R12, R11
			cmp		R12, 0x1C
			jne		error
			add		R12, 1

			mov		R13, R12
			cmp		R13, 0x1D
			jne		error
			add		R13, 1

			mov		R14, R13
			cmp		R14, 0x1E
			jne		error
			add		R14, 1

			mov		R15, R14
			cmp		R15, 0x1F
			jne		error
			add		R15, 1
			
			ret

; -------------------------------------------------	
; Instruction test
; TST|R
; CMP|R|K
; JNZ|K
; JC|K
; JNE|K
; JLT|K
; JGT|K
; JLE|K
; JGE|K
; JMP|K
test1:		mov		R0, 0			; R0 <= 0
			tst		R0
			jnz		error	
			jc		error	
			cmp		R0, 0	
			jne		error	
			jlt		error	
			jgt		error
			jle		t1_ok10
			jmp		error

t1_ok10:	jeq		t1_ok20
			jmp		error
			
t1_ok20:	jge		t1_ok30
			jmp		error
			
t1_ok30:	cmp		R0, 0x55
			jeq		error	
			jlt		t1_ok40
			jmp		error
			
t1_ok40:	jle		t1_ok50
			jmp		error
			
t1_ok50:	jge		error
			jgt		error
			
			mov		R0, 0x55		; R0 <= 0xAA
			tst		R0
			jz		error	
			jc		error	
			
			cmp		R0, 0xAA
			jeq		error	
			jne		t1_ok60
			jmp		error

t1_ok60:	jlt		t1_ok70	
			jmp		error

t1_ok70:	jle		t1_ok80
			jmp		error

t1_ok80:	jgt		error

			cmp		R0, 0x55
			jeq		t1_ok90
			jmp		error
			
t1_ok90:	jne		error
			jlt		error
			
			jle		t1_ok100
			jmp		error

t1_ok100:	jgt		error
			jge		t1_ok110
			jmp		error
			
t1_ok110:	mov		R0, 0xAA		; R0 <= 0x55
			tst		R0
			jz		error	
			jc		error	
			
			cmp		R0, 0x55
			jeq		error	
			jne		t1_ok120
			jmp		error

t1_ok120:	jgt		t1_ok130	
			jmp		error

t1_ok130:	jge		t1_ok140
			jmp		error

t1_ok140:	jlt		error
			jle		error

			ret
			
; -------------------------------------------------	
; ADD|R|K
; ADDC|R|K
; ToDo: ADD|R|R
; ToDo: ADDC|R|R
; CMP|R|R
test2:		mov		R0, 0
			mov		R1, 0
			mov		R2, R0
			mov		R3, R1

t2_loop:	add		R0, 0x01
			addc	R1, 0x00
			add		R2, 1
			cmp		R2, 0
			jne		t2_L1
			add		R3, 1
			cmp		R3, 0x02
t2_L1:		jne		t2_loop
			cmp		R0, R2
			jne		error
			cmp		R1, R3
			jne		error
			mov		R0, 0

			ret

; -------------------------------------------------	
; SUB|R|K
; SUBC|R|K
; ToDo: SUB|R|R
; ToDo: SUBC|R|R
; CMP|R|R
test3:		mov		R0, 0x00
			mov		R1, 0x03
			mov		R2, R0
			mov		R3, R1

t3_loop:	sub		R0, 0x01
			subc	R1, 0x00
			sub		R2, 1
			cmp		R2, 0xFF
			jne		t3_L1
			sub		R3, 1
			cmp		R3, 0x00
t3_L1:		jne		t3_loop
			cmp		R0, R2
			jne		error
			cmp		R1, R3
			jne		error
			mov		R0, 0

			ret
; -------------------------------------------------	
; SUB|K|R
; SUB|R|R
; SUBC|R|R
; SUBC|K|R
; CMP|R|R
test4:		mov		R0, 0x03
			mov		R1, 0x05
			sub		0x05, R0
			sub		R1, 0x03
			cmp		R0, R1
			jne		error
			mov		R0, 0x03
			mov		R1, 0x05
			sub		R0, R1
			subc	0, R0
			cmp		R0, 0x01
			jne		error
			add		R0, 0	
			subc	R1, R0
			cmp		R1, 0x04
			jne		error
			mov		R0, 0

			ret
						
; -------------------------------------------------	
; MOVC|R|Ri
; MOVC|R|Ki
; MOVC|Ri|R
; MOVC|Ri|K
; MOVC|Ki|R
test5:		mov		R0, 1
			movc	(cram+0x5A), R0
			dec		R0			
			movc	(cram+0), R0
			movc	R1, (cram+0x5A)
			movc	(R1), 0xA5
			movc	R0, (R1)
			movc	R1, (R1)
			add		R1, 0xFF-0xA5
			movc	R0, (R1)
t5_loop1:	movc	(R0), R0
			movc	R2, (R0)
			cmp		R2, R0
			jne		error
			movc	(R2), R1
			movc	R2, (R2)
			cmp		R2, R1
			jne		error
			dec		R1
			inc		R0
			jnz		t5_loop1
			
			ret

; -------------------------------------------------	
; MOVX|R|Ri
; MOVX|R|Ki
; MOVX|Ri|R
; MOVX|Ri|K
; MOVX|Ki|R
test6:		mov		R0, 1
			movx	(xram+0x5A), R0
			dec		R0			
			movx	(xram+0), R0
			movx	R1, (xram+0x5A)
			movx	(R1), 0xA5
			movx	R0, (R1)
			movx	R1, (R1)
			add		R1, 0xFF-0xA5
			movx	R0, (R1)
t6_loop1:	movx	(R0), R0
			movx	R2, (R0)
			cmp		R2, R0
			jne		error
			movx	(R2), R1
			movx	R2, (R2)
			cmp		R2, R1
			jne		error
			dec		R1
			inc		R0
			jnz		t6_loop1
			
			ret

; -------------------------------------------------	
; PUSH|R
; POP|R
test7:		mov		R0, 0xAA
			mov		R1, 0
t7_loop1:	push	R0
			inc		R0
			dec		R1
			jnz		t7_loop1
t7_loop2:	pop		R0
			dec		R1
			jnz		t7_loop2
			cmp		R0, 0xAA
			jne		error
			
			ret

; -------------------------------------------------	
; JMP|K
; CALL|K
; RET
test8:		mov		R0, 0x55
			push	R0
			mov		R0, 0
			mov		R1, 0xFF
			call	t8_rec
			pop		R0
			cmp		R0, 0x55
			jne		error

			; Stack manipulations 1
			mov		R0, return1'high
			mov		R1, return1'low
			cout	(cpu_stack_high), R0
			push	R1

			mov		R0, init'high
			mov		R1, init'low
			cout	(cpu_stack_high), R0
			push	R1
			ret
			
			; Stack manipulations 2
return1:	mov		R0, return2'high
			mov		R1, return2'low
			cout	(cpu_stack_high), R0
			push	R1

			mov		R0, init'high
			mov		R1, init'low
			cout	(cpu_stack_high), R0
			push	R1
return2:	ret

t8_rec:		cmp		R0, R1
			jeq		ex_t8_rec
			inc		R0
			jmp		t8_rstub
ex_t8_rec:	dec		R0
			dec		R1
			ret

t8_rstub:	call	t8_rec
			jmp		ex_t8_rec

; -------------------------------------------------	
; SWAP|R
; SHL|R  
; SHR|R  
; ROL|R  
; ROR|R  
; ROLC|R 
; RORC|R 
test9:		mov		R0, 0xA5	; SWAP test
			mov		R1, 0x5A
			swap	R0
			cmp		R0, 0x5A
			jne		error
			swap	R0
			cmp		R0, 0xA5
			jne		error
			mov		R0, 0xD2
			swap	R0
			cmp		R0, 0x2D
			jne		error
			swap	R1
			cmp		R0, 0x2D
			jne		error
			cmp		R1, 0xA5
			jne		error
			
			mov		R0, 0x2D
			shl		R0			; SHL test
			cmp		R0, 0x5A
			jne		error
			shl		R0
			cmp		R0, 0xB4
			jne		error
			shl		R0
			cmp		R0, 0x68
			jne		error
			shl		R0
			cmp		R0, 0xD0
			jne		error
			shl		R0
			cmp		R0, 0xA0
			jne		error
			shl		R0
			cmp		R0, 0x40
			jne		error
			shl		R0
			cmp		R0, 0x80
			jne		error
			shl		R0
			cmp		R0, 0x00
			jne		error

			mov		R1, 0x84
			shr		R1			; SHR test
			cmp		R1, 0x42
			jne		error
			shr		R1
			cmp		R1, 0x21
			jne		error
			shr		R1
			cmp		R1, 0x10
			jne		error
			shr		R1
			cmp		R1, 0x08
			jne		error
			shr		R1
			cmp		R1, 0x04
			jne		error
			shr		R1
			cmp		R1, 0x02
			jne		error
			shr		R1
			cmp		R1, 0x01
			jne		error
			shr		R1
			cmp		R1, 0x00
			jne		error

			mov		R0, 0xC5	; ROL test
			rol		R0			
			cmp		R0, 0x8B
			jne		error
			rol		R0			
			cmp		R0, 0x17
			jne		error
			rol		R0			
			cmp		R0, 0x2E
			jne		error
			rol		R0			
			cmp		R0, 0x5C
			jne		error
			rol		R0			
			cmp		R0, 0xB8
			jne		error
			rol		R0			
			cmp		R0, 0x71
			jne		error
			rol		R0			
			cmp		R0, 0xE2
			jne		error
			rol		R0			
			cmp		R0, 0xC5
			jne		error

			mov		R1, 0x63	; ROR test
			ror		R1
			cmp		R1, 0xB1
			jne		error
			ror		R1
			cmp		R1, 0xD8
			jne		error
			ror		R1
			cmp		R1, 0x6C
			jne		error
			ror		R1
			cmp		R1, 0x36
			jne		error
			ror		R1
			cmp		R1, 0x1B
			jne		error
			ror		R1
			cmp		R1, 0x8D
			jne		error
			ror		R1
			cmp		R1, 0xC6
			jne		error
			ror		R1
			cmp		R1, 0x63
			jne		error


			xor		R0, R0		; ROLC generate
			mov		R0, 0xC5	
			rolc	R0		
			push	R0
			rolc	R0			
			push	R0
			rolc	R0			
			push	R0
			rolc	R0			
			push	R0
			rolc	R0			
			push	R0
			rolc	R0			
			push	R0
			rolc	R0			
			push	R0
			rolc	R0			
			push	R0
			rolc	R0			

			cmp		R0, 0xC5	; ROLC verify
			jne		error
			pop		R0
			cmp		R0, 0x62
			jne		error
			pop		R0
			cmp		R0, 0xB1
			jne		error
			pop		R0
			cmp		R0, 0x58
			jne		error
			pop		R0
			cmp		R0, 0xAC
			jne		error
			pop		R0
			cmp		R0, 0x56
			jne		error
			pop		R0
			cmp		R0, 0x2B
			jne		error
			pop		R0
			cmp		R0, 0x15
			jne		error
			pop		R0
			cmp		R0, 0x8A
			jne		error

			xor		R1, R1		; RORC generate
			mov		R1, 0x63
			rorc	R1
			push	R1
			rorc	R1
			push	R1
			rorc	R1
			push	R1
			rorc	R1
			push	R1
			rorc	R1
			push	R1
			rorc	R1
			push	R1
			rorc	R1
			push	R1
			rorc	R1
			push	R1
			rorc	R1

			cmp		R1, 0x63	; RORC verify
			jne		error
			pop		R1
			cmp		R1, 0xC6
			jne		error
			pop		R1
			cmp		R1, 0x8C
			jne		error
			pop		R1
			cmp		R1, 0x19
			jne		error
			pop		R1
			cmp		R1, 0x33
			jne		error
			pop		R1
			cmp		R1, 0x66
			jne		error
			pop		R1
			cmp		R1, 0xCC
			jne		error
			pop		R1
			cmp		R1, 0x98
			jne		error
			pop		R1
			cmp		R1, 0x31
			jne		error

			ret
			
; -------------------------------------------------
; AND|R|R
; AND|R|K
; OR|R|R 
; OR|R|K 
; XOR|R|R
; XOR|R|K
test10:		mov		R0, 0xA5	; AND
			swap	R0
			push	R0
			and		R0, 0x0F
			cmp		R0, 0x0A
			jne		error
			pop		R0
			push	R0
			and		R0, 0x0F
			cmp		R0, 0x0A
			pop		R1
			jne		error
			swap	R1
			mov		R2, 0x0F
			and		R1, R2
			cmp		R1, 0x05
			jne		error
			
			mov		R0, 0x05	; OR
			mov		R2, 0xA0
			swap	R0
			or		R2, R0
			cmp		R2, 0xF0
			jne		error
			swap	R0
			or		R0, 0x50
			cmp		R0, 0x55
			jne		error
			
			mov		R1, 0x01
			mov		R0, 0x01
			xor		R0, 0x01
			jnz		error
			xor		R0, R1
			cmp		R0, 0x01
			jne		error
			
			ret
			
; -------------------------------------------------	
test11:
			mov		R0, 0x03
			cout	(cpu_int_ctrl), R0
			
			xin		R0, (2)
			or		R0, 1
			xout	(2), R0
			nop
			nop
			nop
			nop
			nop
			nop
			xin		R0, (2)
			and		R0, 0xFE
			xout	(2), R0
			nop
			nop
			nop
			nop
			
			cin		R0, (cpu_int_ctrl)
			or		R0, 0x80
			cout	(cpu_int_ctrl), R0
			nop
			cin		R0, (cpu_int_ctrl)
			or		R0, 0x80
			cout	(cpu_int_ctrl), R0
			nop
			cin		R0, (cpu_int_ctrl)
			or		R0, 0x80
			cout	(cpu_int_ctrl), R0
			nop
			cin		R0, (cpu_int_ctrl)
			or		R0, 0x80
			cout	(cpu_int_ctrl), R0
			nop
			nop
			nop
			nop
			nop
			nop
			nop
			nop
			nop
			ret

; -------------------------------------------------	
; Tests clrc, setc
test12:		sub		R0, 0
			jc		error
			setc
			jnc		error
			nop
			sub		R0, 0
			jc		error
			
			mov		R0, 0
			clrc
			jc		error
			setc
			jnc		error
			nop
			clrc
			jc		error
			ret

; -------------------------------------------------	
isr:		inc		R14
			reti

; -------------------------------------------------	
			org		0x3FF
error:		jmp		error

; -------------------------------------------------	
			org		0x2FF
ok:			jmp		ok

; -------------------------------------------------	
			