; divide.asm
; Description:									   
;	16 and 32 bit unsigned divide routines using the SUBCU instruction
;				
;	DIVIDE32 = 	32 bit divide routine
;		Call routine with: 	XAR0 =	Numerator
;							XAR1 = 	Denominator
;		Returns:			XAR2 =	Remainder
;							XAR3 =	Quotient
;	DIVIDE = 	Call routine with: 	AR0 =	Numerator
;							AR1 = 	Denominator
;		Returns:			AR2 =	Remainder
;							AR3 =	Quotient
;*******************************************************************************
; Begin Program:
		.global		_c_int00	
WDCR	.set 0x7029				; Watchdog Control Register

		.text			
_c_int00:				

	EALLOW 						; Allow writes to GPIO regs (and others)
		
	SETC	objmode				; Allows 32 bit operations
		
	MOV 	AR0, #WDCR 			; Disable watchdog timer
	MOV 	AR1, #0x68 			;
	MOV 	*AR0, AR1 			;
		
	MOV 	SP, #0x0400			; Initialize Stack Pointer
									
MAINLOOP:		
	; 32 bit divide: 1,000,000/503 = 1988 (0x7C4)  r.36 (0x24)
		
	MOVL	XAR0, #1000000		; Numerator32 = 1,000,000 (0xF4240)
	MOVL	XAR1, #503			; Den32	= 503 (0x1F4)
	LC  	DIVIDE32
		
	MOVL	XAR4, XAR2			; save 32bit remainder (0x24=36) in XAR4
	MOVL	XAR5, XAR3			; save 32bit quotient (0x7C4=1988) in XAR5
		
	;16bit divide:   65,000 / 503 =  129 (0x 81)  r. 113 (0x71)
		
	MOV 	AR0, #65000			; Numerator32 = 65,000 (0xFDE8)
	MOV 	AR1, #503			; Den32	= 503 (0x1F4)
	LC  	DIVIDE
								; Quotient (0x81=129) is in AR3
								; Remainder (0x71=113) is in AR2
WAIT
	B   	WAIT, UNC
;*******************************************************************************
; Call with the following
;	XAR0 = Numerator
;	XAR1 = Denominator
; Return with the following
;	XAR2 = Remainder
;	XAR3 = Quotient
DIVIDE32:
	PUSH	AL				; preserve ACC
	PUSH	AH				;
	PUSH	PL				; preserve P
	PUSH	PH				;
		
	MOVB	ACC,#0 			; Zero ACC
  	MOVL	P, XAR0			; Load P register with Num32
	RPT 	#31 			; Repeat operation 32 times
	||SUBCUL  ACC, XAR1 	; Conditional subtract with Den32
  	MOVL	XAR2,	ACC 	; Store 32 bit remainder in XAR2
  	MOVL	XAR3, P 		; Store 32 bit quotient in XAR3
	POP 	PH				; Restore P
	POP 	PL				;
	POP 	AH				; Restore ACC
	POP 	AL				;
		
	LRET
;END OF DIVIDE
;*******************************************************************************
; Call with the following
;	AR0 = Numerator
;	AR1 = Denominator
; Return with the following
;	AR2 = Remainder
;	AR3 = Quotient
DIVIDE:

	PUSH	AL				; preserve ACC
	PUSH	AH				;
	MOVU    ACC, AR0	 	; AL = 16 bit Numerator, AH = 0
	RPT     #15 			; Repeat operation 16 times
	||SUBCU   ACC, AR1 		; Conditional subtract with Denominator
  	MOV     AR2, AH 		; Store remainder in AR2
  	MOV     AR3, AL 		; Store quotient in AR3

	POP		AH				; Restore ACC
	POP		AL				;
		
	LRET
;END OF DIVIDE
;*******************************************************************************
SETUP_CPU:	
	EALLOW 					; Allow writes to GPIO regs (and others)
		
	SETC	objmode			; Allows 32 bit operations
		
	MOV 	AR0, #WDCR 		; Disable watchdog timer
	MOV 	AR1, #0x68 		;
	MOV 	*AR0, AR1 		;
		
	LRET					;Return
;END OF SETUP_CPU
