]> git.sur5r.net Git - cc65/commitdiff
Working on the division and multiplication routines.
authoruz <uz@b7a2c559-68d2-44c3-8de9-860c34a00d81>
Wed, 4 Nov 2009 19:41:54 +0000 (19:41 +0000)
committeruz <uz@b7a2c559-68d2-44c3-8de9-860c34a00d81>
Wed, 4 Nov 2009 19:41:54 +0000 (19:41 +0000)
git-svn-id: svn://svn.cc65.org/cc65/trunk@4443 b7a2c559-68d2-44c3-8de9-860c34a00d81

libsrc/common/Makefile
libsrc/common/cc65_idiv32by16r16.s [new file with mode: 0644]
libsrc/common/cc65_imul16x16r32.s
libsrc/common/cc65_udiv32by16r16.s
libsrc/common/cc65_umul16x16r32.s
libsrc/runtime/Makefile
libsrc/runtime/idiv32by16r16.s [new file with mode: 0644]
libsrc/runtime/imul16x16r32.s
libsrc/runtime/udiv32by16r16.s
libsrc/runtime/umul16x16r32.s
libsrc/tgi/tgi_clipline.s

index 700401dd94acdccd6a7a6ae648725acdf66159c1..b92a02a144e4c19250823a70227949d39ee51e01 100644 (file)
@@ -99,6 +99,7 @@ S_OBJS =      _cwd.o                  \
                atexit.o                \
                atoi.o                  \
                calloc.o                \
+                cc65_idiv32by16r16.o    \
                 cc65_imul16x16r32.o     \
                 cc65_sincos.o           \
                 cc65_udiv32by16r16.o    \
diff --git a/libsrc/common/cc65_idiv32by16r16.s b/libsrc/common/cc65_idiv32by16r16.s
new file mode 100644 (file)
index 0000000..7439467
--- /dev/null
@@ -0,0 +1,39 @@
+;
+; Ullrich von Bassewitz, 2009-11-04
+;
+; CC65 library: 32by16 => 16 signed division
+;
+
+               .export         _cc65_idiv32by16r16
+        .import         idiv32by16r16, incsp4
+
+        .include        "zeropage.inc"
+
+
+;---------------------------------------------------------------------------
+; 32by16 division.
+
+.proc   _cc65_idiv32by16r16
+
+        pha                     ; Save rhs
+
+; Copy from stack to zeropage. This assumes ptr1 and ptr2 are adjacent.
+
+        ldy     #3
+@L1:    lda     (sp),y
+        sta     ptr1,y
+        dey
+        bpl     @L1
+
+        lda     #4
+        clc
+        adc     sp
+        sta     sp
+        bcc     @L2
+        inc     sp+1
+
+@L2:    pla                     ; Old rhs
+        jmp     idiv32by16r16
+
+.endproc
+
index 331e2189bf715c674a15e76ef3fc62b1a3408789..b4e82de109d71750790f84a70ee04a338e246bf8 100644 (file)
@@ -6,7 +6,8 @@
 
         .export         _cc65_imul16x16r32
         .import         imul16x16r32, popax
-       .importzp       ptr1
+
+        .include        "zeropage.inc"
 
 
 ;---------------------------------------------------------------------------
index 351eff85551f32c35e0b59fe6dee4907907a28ea..23fcbbe178641ff81ff688fd0d041c6b26e5e06e 100644 (file)
@@ -6,7 +6,8 @@
 
                .export         _cc65_udiv32by16r16
         .import         udiv32by16r16m, incsp4
-               .importzp       ptr1, ptr2, ptr3, sp
+
+        .include        "zeropage.inc"
 
 
 ;---------------------------------------------------------------------------
index 84c71538f8b9465b3e90036429634d3f799862b9..0e7ed76020ede256791487f61e412d6affc9ab65 100644 (file)
@@ -6,7 +6,8 @@
 
         .export         _cc65_umul16x16r32
         .import         umul16x16r32, popax
-       .importzp       ptr1
+
+        .include        "zeropage.inc"
 
 
 ;---------------------------------------------------------------------------
index c8a76beabf5b424462b9d52f6893d096fbfff95b..a7c95970041ab7df2bb1401326adf723308a4939 100644 (file)
@@ -81,6 +81,7 @@ OBJS =        add.o           \
                ge.o            \
                gt.o            \
                icmp.o          \
+        idiv32by16r16.o \
         imul16x16r32.o  \
        incax1.o        \
        incax2.o        \
diff --git a/libsrc/runtime/idiv32by16r16.s b/libsrc/runtime/idiv32by16r16.s
new file mode 100644 (file)
index 0000000..a69c2a7
--- /dev/null
@@ -0,0 +1,64 @@
+;
+; Ullrich von Bassewitz, 2009-11-04
+;
+; CC65 runtime: 32by16 => 16 signed division
+;
+
+        .export         idiv32by16r16
+               .import         negax, udiv32by16r16m
+
+        .include        "zeropage.inc"
+
+
+;---------------------------------------------------------------------------
+; 32by16 division. Divide ptr1:ptr2 by ptr3. Result is in ptr1, remainder
+; in sreg.
+;
+;   lhs         rhs           result      result also in    remainder
+; -----------------------------------------------------------------------
+;   ptr1:ptr2   ptr3          ax          ptr1              sreg
+;
+
+
+idiv32by16r16:
+        stx     tmp1
+        cpx     #0
+        bpl     @L1
+        jsr     negax
+@L1:    sta     ptr3
+        stx     ptr3+1
+
+        lda     ptr2+1
+        eor     tmp1
+        sta     tmp1
+        bit     ptr2+1
+        bpl     @L3
+
+; Negate the value in ptr1:ptr2
+
+        ldx     #0
+        ldy     #4
+        sec
+@L2:    lda     ptr1,x
+        eor     #$FF
+        adc     #$00
+        sta     ptr1,x
+        inx
+        dey
+        bne     @L2
+
+; Call the unsigned division routine
+
+@L3:    jsr     udiv32by16r16m
+
+; Check the sign of the result
+
+        bit     tmp1
+        bmi     @L4
+        rts
+
+; Negate the result. We do this here only for the result, not for the
+; remainder!
+
+@L4:    jmp     negax
+
index b9101f339f62e9ebc59441c2d28cc4e2da3e85b5..f41d13602cbad1243099414ab0ffc989bccd16bd 100644 (file)
@@ -6,7 +6,8 @@
 
         .export         imul16x16r32
         .import         negax, umul16x16r32m, negeax
-       .importzp       ptr1, ptr3, tmp1
+
+        .include        "zeropage.inc"
 
 
 ;---------------------------------------------------------------------------
index ac048bda0fa632ac31723d52389a50d6937b2d85..3925ffe813cba3cd2d11658b56c06f6f32e19154 100644 (file)
@@ -5,7 +5,8 @@
 ;
 
                .export         udiv32by16r16, udiv32by16r16m
-       .importzp       ptr1, ptr2, ptr3, sreg
+
+        .include        "zeropage.inc"
 
 
 ;---------------------------------------------------------------------------
index 75d6157feab27a8b1791a4d7623320f966dcb5a4..b51ed7343bb2dc9b0d0f4ae7d89a8f75de6405a8 100644 (file)
@@ -5,7 +5,8 @@
 ;
 
         .export         umul16x16r32, umul16x16r32m
-       .importzp       ptr1, ptr3, sreg
+
+        .include        "zeropage.inc"
 
 
 ;---------------------------------------------------------------------------
index 98bddcba2f3f4ae79fb295d9f41b4f97ed5df58c..96ef5b860ea0a9fa2e9d72d97040bb19de54690e 100644 (file)
         .export _tgi_clip_dx, _tgi_clip_dy
         .export _tgi_xmax, _tgi_ymax
 
-        .import negax, pushax, tosmulax, tosdivax
+        .import negax, pushax
+        .import imul16x16r32, idiv32by16r16
         .import return0, return1
 
         .include "tgi-kernel.inc"
+        .include "zeropage.inc"
 
         .macpack longbranch
 
@@ -65,7 +67,7 @@ CLIP_TOP        = $08
         sbc     _tgi_yres+1
         bvs     L1
         eor     #$80
-L1:     bmi     L2
+L1:     bmi     L2             
 
         ldy     #CLIP_NONE              ; No clipping actually
 
@@ -201,17 +203,26 @@ L4:     tya
 
 .proc   muldiv_dydx
 
-        tax
-        tya                             ; Move value into a/x
-
-        jsr     pushax
+        sty     ptr1                    ; lhs
+        sta     ptr1+1
         lda     _tgi_clip_dy
-        ldx     _tgi_clip_dy+1
-        jsr     tosmulax
-        jsr     pushax
+        ldx     _tgi_clip_dy+1          ; rhs
+        jsr     imul16x16r32            ; Multiplicate
+
+; Move the result of the multiplication into ptr1:ptr2
+
+        sta     ptr1
+        stx     ptr1+1
+        ldy     sreg
+        sty     ptr2
+        ldy     sreg+1
+        sty     ptr2+1
+
+; Load divisor and divide
+
         lda     _tgi_clip_dx
         ldx     _tgi_clip_dx+1
-        jmp     tosdivax
+        jmp     idiv32by16r16
 
 .endproc
 
@@ -223,17 +234,26 @@ L4:     tya
 
 .proc   muldiv_dxdy
 
-        tax
-        tya                             ; Move value into a/x
-
-        jsr     pushax
+        sty     ptr1                    ; lhs
+        sta     ptr1+1
         lda     _tgi_clip_dx
-        ldx     _tgi_clip_dx+1
-        jsr     tosmulax
-        jsr     pushax
+        ldx     _tgi_clip_dx+1          ; rhs
+        jsr     imul16x16r32            ; Multiplicate
+
+; Move the result of the multiplication into ptr1:ptr2
+
+        sta     ptr1
+        stx     ptr1+1
+        ldy     sreg
+        sty     ptr2
+        ldy     sreg+1
+        sty     ptr2+1
+
+; Load divisor and divide
+
         lda     _tgi_clip_dy
         ldx     _tgi_clip_dy+1
-        jmp     tosdivax
+        jmp     idiv32by16r16
 
 .endproc