1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
|
; Altirra BASIC - Utility module
; Copyright (C) 2014 Avery Lee, All Rights Reserved.
;
; Copying and distribution of this file, with or without modification,
; are permitted in any medium without royalty provided the copyright
; notice and this notice are preserved. This file is offered as-is,
; without any warranty.
;==========================================================================
; Unsigned 16x16 -> 16 multiply.
;
; Inputs:
; Multiplicand: FR0+3, FR0+2
; Multiplier: FR0+5, FR0+4
;
; Output:
; 16-bit result in [FR0+1, FR0+0]
; High byte in A
; C = 0 if OK
; C = 1 if overflow
;
; Altered:
; A, X, FR0+2, FR0+3
;
.proc umul16x16
;##TRACE "umul16x16(%u,%u)" dw(fr0) dw(fr1)
lda #0
sta fr0+0
ldx #16
bitloop:
;shift result left
asl fr0+0
rol
bcs overflow
;stash result hi
tay
;shift multiplicand left
asl fr0+2
rol fr0+3
bcc no_add
;add multiplier to result
clc
lda fr0
adc fr0+4
sta fr0
tya
adc fr0+5
bcs overflow
no_add:
dex
bne bitloop
sta fr0+1
overflow:
;##TRACE "umul16x16 result %u" dw(fr0)
rts
.endp
;==========================================================================
; Unsigned 16-bit x #6 multiply.
;
; This routine relies on the result not overflowing. We can assume this
; due to checks in the array allocate routines.
;
; Inputs:
; Multiplicand: FR0+1, FR0
;
; Output:
; 16-bit result in [FR0+1, FR0]
; High byte in A
; C = 0
;
.proc umul16_6
lda fr0
ldy fr0+1
asl
rol fr0+1
adc fr0
sta fr0
tya
adc fr0+1
asl fr0
rol
sta fr0+1
rts
.endp
|