diff --git a/wip-hugo/routines/arithmatic/arithmatic.inc b/wip-hugo/routines/arithmatic/arithmatic.inc index 02ebd5a..474c5e2 100644 --- a/wip-hugo/routines/arithmatic/arithmatic.inc +++ b/wip-hugo/routines/arithmatic/arithmatic.inc @@ -1,4 +1,5 @@ ;;; -*- Mode: asm; indent-tabs-mode: t; tab-width: 8 -*- ;; public args -VAL_A = ARGVEC + 0 -VAL_B = ARGVEC + 1 +VAL_B = ARGVEC + 0 +VAL_A = ARGVEC + 1 +;; Note that we use VAL_A +1 as a temp variable, this is a hack to make it faster. diff --git a/wip-hugo/routines/arithmatic/div.s b/wip-hugo/routines/arithmatic/div.s new file mode 100644 index 0000000..3305f5e --- /dev/null +++ b/wip-hugo/routines/arithmatic/div.s @@ -0,0 +1,43 @@ +;;; -*- Mode: asm; indent-tabs-mode: t; tab-width: 8 -*- +.proc div; user-procedure :clobbers (A,X) :clobbers-arguments 3 + ;; https://en.wikipedia.org/wiki/Division_algorithm#Restoring_division + ;;VAL_A / VAL_B = X + ;; Remainder (after) = VAL_A + ;; only works if VAL_A > VAL_B + + ;; Because the algorithm R needs to be signed 16-bit + ;; We make it unsigned by storing the sign-bit in the branching. + .include "arithmatic.inc" + R = VAL_A + LDA #$00 + STA VAL_A + 1 + D = VAL_B + count =$E0 + + LDA #%10000000 + STA count ;; this is 2^i + LDX #$00 +for_i:; for i in 7-0 do + ;; R = R*2 -D*2^8 + ;; R*2 + Mult_16 R, R + 1 + + ;; -D* 2^8 + SEC + LDA R + 1 + SBC D + BMI R_neg + STA R + 1 +R_pos: ; R >= 0 + ;; X(i) = 1 + TXA + EOR count + TAX +R_neg: ; R < 0 +;; We need to take back R +;; We did that by not saving the arithmatic :) + LSR count + BNE for_i +for_i_end: + RTS +.endproc diff --git a/wip-hugo/routines/arithmatic/div_test.s b/wip-hugo/routines/arithmatic/div_test.s new file mode 100644 index 0000000..8787132 --- /dev/null +++ b/wip-hugo/routines/arithmatic/div_test.s @@ -0,0 +1,10 @@ +;;; -*- Mode: asm; indent-tabs-mode: t; tab-width: 8 -*- +;;Calculates $EE /$05 = $33 +.scope div_test + .include "arithmatic.inc" + LDA #$05 + STA VAL_A + LDA #$02 + STA VAL_B + JSR div; X = $02 if it works as it should +.endscope diff --git a/wip-hugo/source.s b/wip-hugo/source.s index 7f640b6..6cca178 100755 --- a/wip-hugo/source.s +++ b/wip-hugo/source.s @@ -8,7 +8,8 @@ .include "STARTUP.s" ;.include "dubbel_buffer/raster_irqs.s" -.include "routines/arithmatic/mult_test.s" +;.include "routines/arithmatic/mult_test.s" +.include "routines/arithmatic/div_test.s" ;.include "routines/line/line_test.s" ;.include "routines/text/char_draw_test.s" ;.include "routines/pixel/pixel_test.s" @@ -25,4 +26,5 @@ JMP exit .include "routines/memory/memset.s" .include "routines/memory/memcpy.s" .include "routines/arithmatic/mult.s" +.include "routines/arithmatic/div.s" .include "END.s"