;**********************************************************************
;                                                                     *
;    Filename:	    testmath.asm                                      *
;    Date:          21 February 2002                                  *
;                                                                     *
;    Author:        CDR Charles B. Cameron, USN                       *
;    Organization:  United States Naval Academy                       *
;                                                                     * 
;                                                                     *
;**********************************************************************
;                                                                     *
;    Files required: p16f874.inc                                      *
;                                                                     *
;                                                                     *
;                                                                     *
;**********************************************************************
;                                                                     *
;    Notes: This program is intended to test the fixed-point          *
;           math routines provided by Microchip.  It also serves as   *
;	    an example of how to use them.                            *
;                                                                     *
;**********************************************************************


	list      p=16f874            ; list directive to define processor
	#include <p16f874.inc>        ; processor specific variable definitions

	__CONFIG _CP_OFF & _WDT_OFF & _BODEN_OFF & _PWRTE_ON & _XT_OSC & _WRT_ENABLE_OFF & _LVP_OFF & _CPD_OFF

; '__CONFIG' directive is used to embed configuration data within .asm file.
; The labels following the directive are located in the respective .inc file.
; See respective data sheet for additional information on configuration word.


;***** VARIABLE DEFINITIONS
w_temp        EQU     0x50        ; variable used for context saving 
status_temp   EQU     0x51        ; variable used for context saving

;**********************************************************************
;**********************************************************************
; The following three lines are necessary if you want to use the
; math library from Microchip.  Since the dev_fam.inc was made before
; the PIC16F874 was created, the second line specifies the memory
; map which is appropriate for this processor.  Most of what is in this
; file could probably be eliminated if I had enough time and a warm dry
; place to work on it.
;**********************************************************************
;**********************************************************************

;	This file appears to identify the PIC device which is the 
;	target processor to be used.

	#include	<dev_fam.inc>
P16_MAP2	SET	0xFF	; Use the PIC16F874 memory map

;	The next file defines the locations of all the registers used by the math
;	routines.  They could be relocated if necessary but I didn't
; 	go to the trouble of doing so.

	#include	<math16.inc>

;**********************************************************************
;**********************************************************************


;**********************************************************************
		ORG     0x000             ; processor reset vector
		clrf    PCLATH            ; Start with Page 0.
  		goto    main              ; go to beginning of program


		ORG     0x004             ; interrupt vector location
		movwf   w_temp            ; save off current W register contents
		movf	STATUS,w          ; move status register into W register
		bcf     STATUS,RP0        ; ensure file register bank set to 0
		movwf	status_temp       ; save off contents of STATUS register


; isr code can go here or be located as a call subroutine elsewhere


		bcf     STATUS,RP0        ; ensure file register bank set to 0
		movf    status_temp,w     ; retrieve copy of STATUS register
		movwf	STATUS            ; restore pre-isr STATUS register contents
		swapf   w_temp,f
		swapf   w_temp,w          ; restore pre-isr W register contents
		retfie                    ; return from interrupt



main

; Initialize the math arguments.  These all are defined in math16.inc.
	movlw	H'01'
	movwf	AARGB0		; AARGB0 is the most significant byte of the multiplicand.
	movlw	H'09'
	movwf	AARGB1		; AARGB1 is the least significant byte of the multiplicand.

	movlw	H'01'
	movwf	BARGB0		; BARGB0 is the most significant byte of the multiplier.
	movlw	H'05'
	movwf	BARGB1		; BARGB0 is the least significant byte of the multiplier

loop
; **********************
; Test the multiply and divide routines on some data.  Nothing is done with the output
; but in debug mode the simulator will show the results.
; **********************


	call	FXM1608U	; multiply
				; AARGB0:AARGB1:AARGB2 = AARGB0:AARGB1 x BARGB0
				; Corrupts BARGB0.

	call	FXD1608U	; divide
				; result = AARGB0:AARGB1 / BARGB0
				; AARGB0:AARGB1 = integer portion of result
				; REMB0 = AARGB4= remainder of result
	
	goto	loop

; **********************
; Include the desired mathematics routines here.
; **********************
	#include <Fxm68.a16>	; Fixed-multiplication routines (24 bits = 16 bits x 8 bits)
	#include <Fxd68.a16>	; Fixed-division routines (16 bits / 8 bits = 8-bit quotient
				; 				+ 8-bit remainder)

	END                       ; directive 'end of program'


