From b92694aeb5cff949997d81e3580d4f1950f209ce Mon Sep 17 00:00:00 2001 From: "matthias@arch" Date: Fri, 8 Dec 2023 00:13:29 +0100 Subject: [PATCH] add div by pow(2) --- util/math.s65 | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 util/math.s65 diff --git a/util/math.s65 b/util/math.s65 new file mode 100644 index 0000000..3a7d2be --- /dev/null +++ b/util/math.s65 @@ -0,0 +1,106 @@ +;******************************************************************************** +; @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