                    ; 16bit x 16bit multiplication by Soji Yamakawa 2017/10/25
                    EXPORT      MUL16_NUM0
                    EXPORT      MUL16_NUM1
                    EXPORT      MUL16

MUL16_NUM0          RZB         2   ; Input i1   Output higher 16bits
MUL16_NUM1          RZB         2   ; Input i2   Output lower 16bits

MUL16               PSHS        A,B,X

                    CLR         MUL16_SIGN,PCR

                    LDD         MUL16_NUM0,PCR
                    BPL         MUL16_NUM0_POSITIVE
                    COMA
                    COMB
                    ADDD        #1
                    STD         MUL16_NUM0,PCR
                    INC         MUL16_SIGN,PCR
MUL16_NUM0_POSITIVE

                    LDD         MUL16_NUM1,PCR
                    BPL         MUL16_NUM1_POSITIVE
                    COMA
                    COMB
                    ADDD        #1
                    STD         MUL16_NUM1,PCR
                    INC         MUL16_SIGN,PCR
MUL16_NUM1_POSITIVE

                    ; (256*Hi0+Lo0)(256*Hi1+Lo1)=65536*Hi0*Hi1+256*(Hi0*Lo1+Hi1*Lo0)+Lo0*Lo1
                    LDA         MUL16_NUM0,PCR
                    LDB         MUL16_NUM1,PCR
                    MUL
                    STD         MUL16_HI16,PCR

                    LDA         MUL16_NUM0+1,PCR
                    LDB         MUL16_NUM1+1,PCR
                    MUL
                    STD         MUL16_LO16,PCR

                    LDD         MUL16_NUM0+1,PCR
                    ; LDA           MUL16_NUM0+1,PCR
                    ; LDB           MUL16_NUM1,PCR
                    MUL
                    ADDD        MUL16_HI16+1,PCR
                    STD         MUL16_HI16+1,PCR
                    LDA         MUL16_HI16,PCR
                    ADCA        #0
                    STA         MUL16_HI16,PCR

                    LDA         MUL16_NUM0,PCR
                    LDB         MUL16_NUM1+1,PCR
                    MUL
                    ADDD        MUL16_HI16+1,PCR
                    STD         MUL16_HI16+1,PCR
                    LDA         MUL16_HI16,PCR
                    ADCA        #0
                    STA         MUL16_HI16,PCR

                    LDA         MUL16_SIGN,PCR
                    LSRA
                    BCC         MUL16_PROD_POSITIVE

                    LDD         MUL16_LO16,PCR
                    COMA
                    COMB
                    ADDD        #1
                    STD         MUL16_NUM1,PCR

                    LDD         MUL16_HI16,PCR
                    SBCB        #0
                    SBCA        #0
                    COMA
                    COMB
                    STD         MUL16_NUM0,PCR

                    PULS        A,B,X
                    RTS

MUL16_PROD_POSITIVE
                    LDD         MUL16_HI16,PCR
                    STD         MUL16_NUM0,PCR
                    LDD         MUL16_LO16,PCR
                    STD         MUL16_NUM1,PCR
                    PULS        A,B,X
                    RTS
MUL16_HI16          RZB         2
MUL16_LO16          RZB         2
MUL16_SIGN          RZB         1
