; File: test.asm
; Author: Gaspar Sinai <gaspar@yudit.org>
; Version: 2010-04-27
; Copyright: Gaspar Sinai <gaspar@yudit.org>
; Tester for frequency counter

#include "common.inc"
#include "util.inc"

    ; XT_OSC external osciallator XT,HS
    ; WDT_OFF no watchdog timer
    ; WRT_OFF no prog memmory write protection
    ; CP_OFF no code protect
    ; CPD_OFF no code protect data
    ; BODEN_ON brown out reset
    ; PWRTE_OFF power up timer disabled
    ; LVP_OFF low voltage in-circuit serial programming disabled

    __CONFIG (_XT_OSC & _WDT_OFF & _WRT_OFF & _CP_OFF & _CPD_OFF & _BODEN_ON & _PWRTE_OFF & _LVP_OFF)

    org     0
    goto    Main

    org     4
    retfie

    org 0x100

Main:
    movlw   0xA1
    movwf   AritBuffer + .3
    movlw   0xB1
    movwf   AritBuffer + .2
    movlw   0xFF
    movwf   AritBuffer + .1
    movlw   0x80
    movwf   AritBuffer + 0

    movlw   0xC2
    movwf   AritBuffer + .11
    movlw   0x32
    movwf   AritBuffer + .10
    movlw   0xFF
    movwf   AritBuffer + .9
    movlw   0x90
    movwf   AritBuffer + .8

    call    Multiply

    movlw   0x7a
    subwf   AritBuffer+7,W
    btfss   STATUS,Z
    goto    MultiplyError7

    movlw   0xa9
    subwf   AritBuffer+6,W
    btfss   STATUS,Z
    goto    MultiplyError6

    movlw   0x19
    subwf   AritBuffer+5,W
    btfss   STATUS,Z
    goto    MultiplyError5

    movlw   0xce
    subwf   AritBuffer+4,W
    btfss   STATUS,Z
    goto    MultiplyError4

    movlw   0x28
    subwf   AritBuffer+3,W
    btfss   STATUS,Z
    goto    MultiplyError3

    movlw   0xa0
    subwf   AritBuffer+2,W
    btfss   STATUS,Z
    goto    MultiplyError2

    movlw   0x38
    subwf   AritBuffer+1,W
    btfss   STATUS,Z
    goto    MultiplyError1

    movlw   0x00
    subwf   AritBuffer+0,W
    btfss   STATUS,Z
    goto    MultiplyError1

    ; Divide it back
    call    Divide;

    movlw   0xA1
    subwf   AritBuffer+3,W
    btfss   STATUS,Z
    goto    DivideError7

    movlw   0xB1
    subwf   AritBuffer+2,W
    btfss   STATUS,Z
    goto    DivideError6

    movlw   0xFF
    subwf   AritBuffer+1,W
    btfss   STATUS,Z
    goto    DivideError5

    movlw   0x80
    subwf   AritBuffer+0,W
    btfss   STATUS,Z
    goto    DivideError4

    movlw   0xF9
    movwf   AritBuffer + .7
    movlw   0xB1
    movwf   AritBuffer + .6
    movlw   0x0F
    movwf   AritBuffer + .5
    movlw   0x82
    movwf   AritBuffer + 4

    movlw   0xDC
    movwf   AritBuffer + .3
    movlw   0x76
    movwf   AritBuffer + .2
    movlw   0xFF
    movwf   AritBuffer + .1
    movlw   0xA5
    movwf   AritBuffer + .0

    call    ToBCDInteger

    ; F9B10F82DC76FFA5 ==> 17 99 21 79 04  10 40 79 35 09
    movlw   0x09
    subwf   DecNumber+0,W
    btfss   STATUS,Z
    goto    ToBCDIntError

    movlw   0x35
    subwf   DecNumber+1,W
    btfss   STATUS,Z
    goto    ToBCDIntError

    movlw   0x79
    subwf   DecNumber+2,W
    btfss   STATUS,Z
    goto    ToBCDIntError

    movlw   0x40
    subwf   DecNumber+3,W
    btfss   STATUS,Z
    goto    ToBCDIntError

    movlw   0x10
    subwf   DecNumber+4,W
    btfss   STATUS,Z
    goto    ToBCDIntError

    ; F9B10F82DC76FFA5 ==> 17 99 21 79 04  10 40 79 35 09

    movlw   0x04
    subwf   DecNumber+5,W
    btfss   STATUS,Z
    goto    ToBCDIntError

    movlw   0x79
    subwf   DecNumber+6,W
    btfss   STATUS,Z
    goto    ToBCDIntError

    movlw   0x21
    subwf   DecNumber+7,W
    btfss   STATUS,Z
    goto    ToBCDIntError

    movlw   0x99
    subwf   DecNumber+8,W
    btfss   STATUS,Z
    goto    ToBCDIntError

    movlw   0x17
    subwf   DecNumber+9,W
    btfss   STATUS,Z
    goto    ToBCDIntError

    ; Test fraction

    movlw   0x20
    movwf   AritBuffer + .7
    movlw   0x0
    movwf   AritBuffer + .6
    movlw   0x0
    movwf   AritBuffer + .5
    movlw   0x0
    movwf   AritBuffer + 4

    movlw   0x0
    movwf   AritBuffer + .3
    movlw   0x0
    movwf   AritBuffer + .2
    movlw   0x0
    movwf   AritBuffer + .1
    movlw   0x0
    movwf   AritBuffer + .0

    call    ToBCDFraction
    movlw   0x12
    subwf   DecNumber+.9,W
    btfss   STATUS,Z

    goto    ToBCDFractionErrorSimple
    movlw   0x50
    subwf   DecNumber+.8,W
    btfss   STATUS,Z
    goto    ToBCDFractionErrorSimple

    movlw   0x0
    movwf   AritBuffer + .7
    movlw   0x0
    movwf   AritBuffer + .6
    movlw   0x0
    movwf   AritBuffer + .5
    movlw   0x0
    movwf   AritBuffer + 4

    movlw   0x0
    movwf   AritBuffer + .3
    movlw   0x0
    movwf   AritBuffer + .2
    movlw   0x0
    movwf   AritBuffer + .1
    movlw   0x1
    movwf   AritBuffer + .0

    movlw   0x0
    movwf   AritBuffer + .11
    movlw   0x0
    movwf   AritBuffer + .10
    movlw   0x0
    movwf   AritBuffer + .9
    movlw   0x3
    movwf   AritBuffer + .8


    call    Divide
    call    DivideFraction  ; produce 3333333333 fraction
    call    ToBCDFraction

    movlw   0x33
    subwf   DecNumber+9,W
    btfss   STATUS,Z
    goto    ToBCDFractionError

    movlw   0x33
    subwf   DecNumber+8,W
    btfss   STATUS,Z
    goto    ToBCDFractionError

    movlw   0x33
    subwf   DecNumber+7,W
    btfss   STATUS,Z
    goto    ToBCDFractionError

    movlw   0x33
    subwf   DecNumber+6,W
    btfss   STATUS,Z
    goto    ToBCDFractionError

    movlw   0x33
    subwf   DecNumber+5,W
    btfss   STATUS,Z
    goto    ToBCDFractionError

    movlw   0x33
    subwf   DecNumber+4,W
    btfss   STATUS,Z
    goto    ToBCDFractionError

    movlw   0x33
    subwf   DecNumber+3,W
    btfss   STATUS,Z
    goto    ToBCDFractionError

    movlw   0x33
    subwf   DecNumber+2,W
    btfss   STATUS,Z
    goto    ToBCDFractionError

    movlw   0x33
    subwf   DecNumber+1,W
    btfss   STATUS,Z
    goto    ToBCDFractionError

    movlw   0x31            ; last rounding error
    subwf   DecNumber+0,W
    btfss   STATUS,Z
    goto    ToBCDFractionError

    goto    Pass

MultiplyError7:
    goto MultiplyError7

MultiplyError6:
    goto MultiplyError6

MultiplyError5:
    goto MultiplyError5

MultiplyError4:
    goto MultiplyError4

MultiplyError3:
    goto MultiplyError3

MultiplyError2:
    goto MultiplyError2

MultiplyError1:
    goto MultiplyError1

MultiplyError0:
    goto MultiplyError0

DivideError7:
    goto DivideError7

DivideError6:
    goto DivideError6

DivideError5:
    goto DivideError5

DivideError4:
    goto DivideError4

DivideError3:
    goto DivideError3

DivideError2:
    goto DivideError2

DivideError1:
    goto DivideError1

DivideError0:
    goto DivideError0

ToBCDIntError:
    goto ToBCDIntError

ToBCDFractionError:
    goto ToBCDFractionError

ToBCDFractionErrorSimple:
    goto ToBCDFractionErrorSimple

Pass:
    goto Pass

    end
