From 787f06973828f89b7e4668d295479f33ba5bb8b2 Mon Sep 17 00:00:00 2001 From: uz Date: Thu, 5 Nov 2009 20:16:43 +0000 Subject: [PATCH] Moved fixed point multiplication and rounding into an asm module. git-svn-id: svn://svn.cc65.org/cc65/trunk@4447 b7a2c559-68d2-44c3-8de9-860c34a00d81 --- asminc/tgi-kernel.inc | 1 + include/tgi.h | 8 ++++++ include/tgi/tgi-kernel.h | 7 ++++- libsrc/tgi/Makefile | 1 + libsrc/tgi/tgi_arc.c | 21 ++++----------- libsrc/tgi/tgi_imulround.s | 54 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 75 insertions(+), 17 deletions(-) create mode 100644 libsrc/tgi/tgi_imulround.s diff --git a/asminc/tgi-kernel.inc b/asminc/tgi-kernel.inc index 8d9151dcb..f48eea6d4 100644 --- a/asminc/tgi-kernel.inc +++ b/asminc/tgi-kernel.inc @@ -170,6 +170,7 @@ TGI_TEXT_VERTICAL = 1 .global _tgi_getxres .global _tgi_getyres .global _tgi_gotoxy + .global _tgi_imulround .global _tgi_init .global _tgi_install .global _tgi_ioctl diff --git a/include/tgi.h b/include/tgi.h index ca5a5dc5f..a0cebe39f 100644 --- a/include/tgi.h +++ b/include/tgi.h @@ -218,6 +218,14 @@ void __fastcall__ tgi_ellipse (int x, int y, unsigned char rx, unsigned char ry) * drawing color. */ +void __fastcall__ tgi_arc (int x, int y, unsigned char rx, unsigned char ry, + unsigned sa, unsigned ea); +/* Draw an ellipse arc with center at x/y and radii rx/ry using the current + * drawing color. The arc covers the angle between sa and ea (startangle and + * endangle), which must be in the range 0..360 (otherwise the function may + * bevave unextectedly). + */ + void __fastcall__ tgi_bar (int x1, int y1, int x2, int y2); /* Draw a bar (a filled rectangle) using the current color. */ diff --git a/include/tgi/tgi-kernel.h b/include/tgi/tgi-kernel.h index 2fa820998..755e49b50 100644 --- a/include/tgi/tgi-kernel.h +++ b/include/tgi/tgi-kernel.h @@ -44,7 +44,7 @@ -/* TGI kernel variables */ +/* TGI kernel variables */ extern void* tgi_drv; /* Pointer to driver */ extern unsigned char tgi_error; /* Last error code */ extern unsigned char tgi_gmode; /* Flag: Graphics mode active */ @@ -68,6 +68,11 @@ extern unsigned tgi_aspectratio;/* Aspect ratio as fixed point 8.8 */ const char* __fastcall__ tgi_map_mode (unsigned char mode); /* Map a tgi mode to a driver name. Returns NULL if no driver available. */ +int __fastcall__ tgi_imulround (int rhs, int lhs); +/* Helper function for functions using sine/cosine: Multiply two values, one + * being an 8.8 fixed point one, and return the rounded and scaled result. + */ + /* End of tgi-kernel.h */ diff --git a/libsrc/tgi/Makefile b/libsrc/tgi/Makefile index 4f15e3d20..de9c3263d 100644 --- a/libsrc/tgi/Makefile +++ b/libsrc/tgi/Makefile @@ -58,6 +58,7 @@ S_OBJS = tgi-kernel.o \ tgi_getxres.o \ tgi_getyres.o \ tgi_gotoxy.o \ + tgi_imulround.o \ tgi_init.o \ tgi_ioctl.o \ tgi_line.o \ diff --git a/libsrc/tgi/tgi_arc.c b/libsrc/tgi/tgi_arc.c index 1ce239fc4..630055abb 100644 --- a/libsrc/tgi/tgi_arc.c +++ b/libsrc/tgi/tgi_arc.c @@ -34,6 +34,7 @@ #include +#include #include @@ -44,18 +45,6 @@ -static int RoundMul (int rhs, int lhs) -{ - long res = cc65_imul16x16r32 (rhs, lhs); - if ((unsigned char)res & 0x80) { - return (int)(res >> 8) + 1; - } else { - return (int)(res >> 8); - } -} - - - void __fastcall__ tgi_arc (int x, int y, unsigned char rx, unsigned char ry, unsigned sa, unsigned ea) /* Draw an ellipse arc with center at x/y and radii rx/ry using the current @@ -81,16 +70,16 @@ void __fastcall__ tgi_arc (int x, int y, unsigned char rx, unsigned char ry, } /* Calculate the start coords */ - x1 = x + RoundMul (rx, cc65_cos (sa)); - y1 = y + RoundMul (ry, cc65_sin (sa)); + x1 = x + tgi_imulround (rx, cc65_cos (sa)); + y1 = y + tgi_imulround (ry, cc65_sin (sa)); do { sa += inc; if (sa >= ea) { sa = ea; done = 1; } - x2 = x + RoundMul (rx, cc65_cos (sa)); - y2 = y - RoundMul (ry, cc65_sin (sa)); + x2 = x + tgi_imulround (rx, cc65_cos (sa)); + y2 = y - tgi_imulround (ry, cc65_sin (sa)); tgi_line (x1, y1, x2, y2); x1 = x2; y1 = y2; diff --git a/libsrc/tgi/tgi_imulround.s b/libsrc/tgi/tgi_imulround.s new file mode 100644 index 000000000..c1661d3dc --- /dev/null +++ b/libsrc/tgi/tgi_imulround.s @@ -0,0 +1,54 @@ +; +; Ullrich von Bassewitz, 2009-11-05 +; +; Helper function for functions using sine/cosine: Multiply two values, one +; being an 8.8 fixed point one, and return the rounded and scaled result. +; + + + .export _tgi_imulround + .import popax, imul16x16r32 + + .include "zeropage.inc" + + +;---------------------------------------------------------------------------- +; + +.code +.proc _tgi_imulround + +; Get arguments + + sta ptr1 + stx ptr1+1 ; Save lhs + jsr popax ; Get rhs + +; Multiplicate + + jsr imul16x16r32 + +; Round the result + + cmp #$80 ; Frac(x) >= 0.5? + txa + ldy sreg+1 ; Check sign + bmi @L1 + + adc #$00 + tay + lda sreg + adc #$00 + tax + tya + rts + +@L1: sbc #$00 + tay + lda sreg + sbc #$00 + tax + tya + rts + +.endproc -- 2.39.5