implement general divition

This commit is contained in:
hugova 2025-05-17 16:11:49 +02:00
parent 3283f9a68a
commit 89dcec60ff
4 changed files with 59 additions and 3 deletions

View file

@ -1,4 +1,5 @@
;;; -*- Mode: asm; indent-tabs-mode: t; tab-width: 8 -*- ;;; -*- Mode: asm; indent-tabs-mode: t; tab-width: 8 -*-
;; public args ;; public args
VAL_A = ARGVEC + 0 VAL_B = ARGVEC + 0
VAL_B = ARGVEC + 1 VAL_A = ARGVEC + 1
;; Note that we use VAL_A +1 as a temp variable, this is a hack to make it faster.

View file

@ -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

View file

@ -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

View file

@ -8,7 +8,8 @@
.include "STARTUP.s" .include "STARTUP.s"
;.include "dubbel_buffer/raster_irqs.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/line/line_test.s"
;.include "routines/text/char_draw_test.s" ;.include "routines/text/char_draw_test.s"
;.include "routines/pixel/pixel_test.s" ;.include "routines/pixel/pixel_test.s"
@ -25,4 +26,5 @@ JMP exit
.include "routines/memory/memset.s" .include "routines/memory/memset.s"
.include "routines/memory/memcpy.s" .include "routines/memory/memcpy.s"
.include "routines/arithmatic/mult.s" .include "routines/arithmatic/mult.s"
.include "routines/arithmatic/div.s"
.include "END.s" .include "END.s"