;*******************************************************************************
;* Subroutine: BCD2BIN.SUB
;* Programmer: Tony Papadimitriou <tonyp@acm.org>
;* Language  : Motorola/Freescale/NXP HC08/9S08 Assembly Language (aspisys.com/ASM8)
;* Purpose   : Convert a two-digit or four-digit packed BCD number to binary
;* Status    : FREEWARE Copyright (c) 2021 by Tony Papadimitriou <tonyp@acm.org>
;* Original  : http://www.aspisys.com/code/hc08/bcd2bin.html
;* History   : 03.07.11 v1.00 Original
;*           : 10.02.07       Adapted to latest ASM8 version
;*           : 10.03.23       Added #SPAUTO
;*           : 10.10.19       Adapted to latest ASM8
;*           : 11.04.21       Moved test code at EOF (for #EXIT optimization)
;*           : 21.05.27       Separated byte and word versions into distinct files
;*******************************************************************************

#ifmain ;-----------------------------------------------------------------------
                    #ListOff
                    #Uses     mcu.inc
                    #ListOn
                    #Cycles
                    #MapOff
#endif ;------------------------------------------------------------------------
?_OBJECT_?
;*******************************************************************************
; Purpose: Convert a two-digit packed BCD number to binary
; Input  : A = packed BCD
; Output : A = binary equivalent
; Note(s): Size: 18 bytes, Cycles: 32 [HC08], 34 [S08], 35 [MMU]

                    #spauto

ByteBCD2Bin         proc
                    pshx

                    tax                           ;save original number in X
                    and       #$0F                ;isolate low nibble
                    psha      a@@                 ;save low nibble only

                    txa                           ;reload original A
                    nsa                           ;move high nibble to low
                    and       #$0F                ;isolate nibble
                    ldx       #10                 ;multiplier for decimal
                    mul

                    add       a@@,sp

                    pulx::ais
                    rtc

BCD2BIN_CYCLES      equ       :cycles

                    #sp
;*******************************************************************************
                    #Exit
;*******************************************************************************
                    #Message  ByteBCD2Bin Size: {*-ByteBCD2Bin} bytes, Cycles: {BCD2BIN_CYCLES}
                    @EndStats

                    #MapOn

Start               proc
                    @rsp

                    lda       PackedBCD
                    call      ByteBCD2Bin         ;sample call for testing
                    cmpa      Binary

Success             beq       *                   ;correct result
                    bra       *                   ;error, if here

PackedBCD           fcb       $56                 ;sample number as BCD
Binary              fcb       56                  ;sample number as binary

                    @vector   Vreset,Start