6502-OS/util/math.h65

108 lines
1.8 KiB
Plaintext
Raw Normal View History

2023-12-08 22:56:35 +01:00
;********************************************************************************
; @module math
; @type utility
; @details
; Math library
;********************************************************************************
.ifndef INCLUDE_MATH
INCLUDE_MATH = 1
.scope math
;********************************************************************************
; @macro Divide a num (or A) by a factor
; @param num: Memory or A
; @param divider: must be power of 2
;********************************************************************************
.macro div num,divider
.if divider = 2
lsr num
.elseif divider = 4
lsr num
lsr num
.elseif divider = 8
lsr num
lsr num
lsr num
.elseif divider = 16
.repeat 4
lsr num
.endrepeat
.elseif divider = 32
.repeat 5
lsr num
.endrepeat
.elseif divider = 64
.repeat 6
lsr num
.endrepeat
.elseif divider = 128
.repeat 7
lsr num
.endrepeat
.else
.error "div macro: divider % 2 != 0"
.endif
.endmacro
; .macro Amult1 factor
; phx
; ldx factor
; AmultX
; plx
; .endmacro
; https://www.llx.com/Neil/a2/mult.html
; .macro mult num1,num2,result
; lda #0
; ldx #8
; L1:
; lsr num2
; bcc L2
; clc
; adc num1
; L2:
; ror
; ror result
; dex
; bne L1
; sta result+1
; .endmacro
; 00110101
; 10010011
; -------^
; =00110101
; 00110101
; 01001001
; -------^
; =00110101
; 00110101
; 00100100
; -------^
; =00000000
; 00110101
; 00010010
; -------^
; =00000000
; 00110101
; 00001001
; -------^
; 00110101
; 00110101
; 00000100
; -------^
; =00000000
; 00110101
; 00000010
; -------^
; =00000000
; 00110101
; 00000001
; 00110101
.endscope
.endif ; guard