; *************************************************************************** ; *************************************************************************** ; ; hucc-math.asm ; ; Basic (i.e. very slow) 8-bit and 16-bit multiply and divide routines. ; ; Copyright John Brandwood 2021-2024. ; ; Distributed under the Boost Software License, Version 1.0. ; (See accompanying file LICENSE_1_0.txt or copy at ; http://www.boost.org/LICENSE_1_0.txt) ; ; *************************************************************************** ; *************************************************************************** ; ; This is basically a set of SDCC-compatible routines, but using Y:A for the ; primary register instead of X:A. ; ; Using Y:A makes the routines instantly usable with HuCC, and it also makes ; them usable with SDCC with just an "sxy" before and after the call. ; ; *************************************************************************** ; *************************************************************************** .code multiplier = __temp multiplicand = ___SDCC_m6502_ret0 product = multiplicand __mulint_PARM_2 = multiplier divisor = __temp dividend = ___SDCC_m6502_ret0 quotient = dividend remainder = ___SDCC_m6502_ret2 __moduint_PARM_2 = divisor __modsint_PARM_2 = divisor __divuint_PARM_2 = divisor __divsint_PARM_2 = divisor ; *************************************************************************** ; int ; _mulint (int a, int b) ; ; 1st parameter in Y:A (multiplicand) ; 2nd parameter in __mulint_PARM_2 (multiplier) ; result in Y:A ; ; N.B. signed and unsigned multiply only differ in the top 16 of the 32bits! __mulint: sta C. rol.h remainder. ; db $90 ; Turn "plp" into "bcc" to skip. ;.subtract: plp ; Discard remainder 17th bit. ; sec ; tax ; If dividend has more bits ; pla ; than the divisor, then we ; and #1 ; need to check rotated bit. ; sbc #0 ; txa bcc .skip ; CC if divisor > remainder. sta.h dividend LSB. db $F0 ; Turn "tya" into "beq" to skip. .skip: tya ; Restore remainder lo-byte. dex bne .loop ldy.h dividend LSB. rol.h C. dex beq .finished rol a ; Rotate C into remainder. rol.h remainder. ; cmp.l remainder. ; db $90 ; Turn "plp" into "bcc" to skip. ;.subtract: plp ; Discard remainder 17th bit. ; sec sta.h C. sta.l remainder. sbc dividend LSB. dey bne .loop cly ; Clear hi-byte of return. rts ; Return the 16-bit remainder. ; *************************************************************************** ; unsigned int ; _divuchar (unsigned char x, unsigned char y) ; ; 1st parameter in A (unsigned dividend) ; 2nd parameter in Y (unsigned divisor) ; result in Y:A __divuchar: jsr __moduchar lda