1
0
mirror of https://gitlab.com/camelot/kickc.git synced 2025-02-17 10:30:43 +00:00

improved multiply tables

This commit is contained in:
Jesper Gravgaard 2018-11-04 20:46:42 +01:00
parent 67611e548b
commit 77c0edab1a
2 changed files with 58 additions and 22 deletions

View File

@ -20,7 +20,7 @@ word* psp2 = $f7;
void main() { void main() {
asm { sei } asm { sei }
sprites_init(); sprites_init();
mulf_init(); //mulf_init();
*psp1 = (word)mulf_sqr1; *psp1 = (word)mulf_sqr1;
*psp2 = (word)mulf_sqr2; *psp2 = (word)mulf_sqr2;
anim(); anim();
@ -48,8 +48,8 @@ void anim() {
//if(*xr<xmin) xmin = *xr; //if(*xr<xmin) xmin = *xr;
//if(*xr>xmax) xmax = *xr; //if(*xr>xmax) xmax = *xr;
byte i2 = i<<1; byte i2 = i<<1;
SPRITES_XPOS[i2] = $80+(byte)(*xp); SPRITES_XPOS[i2] = ($80+(byte)(*xp))>>1;
SPRITES_YPOS[i2] = $80+(byte)(*yp); SPRITES_YPOS[i2] = ($80+(byte)(*yp))>>1;
} }
// Increment angles // Increment angles
sz++; sz++;
@ -189,8 +189,8 @@ void rotate_matrix(signed byte x, signed byte y, signed byte z) {
B2: sbc mulf_sqr2,y B2: sbc mulf_sqr2,y
sta xr sta xr
// divide x by 2 to get x below $3f for multiplication // divide x by 2 to get x below $3f for multiplication
cmp #$80 //cmp #$80
ror //ror
sta XX+1 sta XX+1
clc clc
@ -204,8 +204,8 @@ void rotate_matrix(signed byte x, signed byte y, signed byte z) {
// Calculate perspective for Y-position // Calculate perspective for Y-position
// divide y by 2 to get x below $3f for multiplication // divide y by 2 to get x below $3f for multiplication
cmp #$80 //cmp #$80
ror //ror
clc clc
tay tay
lda (psp1),y lda (psp1),y
@ -220,6 +220,29 @@ void rotate_matrix(signed byte x, signed byte y, signed byte z) {
} }
} }
// mulf_sqr tables will contain f(x)=int(x*x) and g(x) = f(1-x).
// f(x) = >(( x * x ))
byte* mulf_sqr1 = $2400;
// g(x) = >((( 1 - x ) * ( 1 - x )))
byte* mulf_sqr2 = $2600;
kickasm(pc mulf_sqr1) {{
.for(var i=0;i<$200;i++) {
.if(i<=159) { .byte round((i*i)/256) }
.if(i>159 && i<=351 ) { .byte round(((i-256)*(i-256))/256) }
.if(i>351) { .byte round(((512-i)*(512-i))/256) }
}
}}
kickasm(pc mulf_sqr2) {{
.for(var i=0;i<$200;i++) {
.if(i<=159) { .byte round((-i-1)*(-i-1)/256) }
.if(i>159 && i<=351 ) { .byte round(((255-i)*(255-i))/256) }
.if(i>351) { .byte round(((i-511)*(i-511))/256) }
}
}}
/*
// Multiplication tables for seriously fast multiplication. // Multiplication tables for seriously fast multiplication.
// This version is optimized for speed over accuracy // This version is optimized for speed over accuracy
// - It can multiply signed numbers with no extra code - but only for numbers in [-$3f;$3f] // - It can multiply signed numbers with no extra code - but only for numbers in [-$3f;$3f]
@ -252,7 +275,7 @@ void mulf_init() {
add +=2; add +=2;
} }
} }
*/
// A single sprite // A single sprite
byte* SPRITE = $3000; byte* SPRITE = $3000;
@ -264,7 +287,7 @@ kickasm(pc SPRITE, resource "balloon.png") {{
}} }}
// Perspective multiplication table containing (d/(z0-z)[z] for each z-value // Perspective multiplication table containing (d/(z0-z)[z] for each z-value
signed byte* PERSP_Z = $2400; signed byte* PERSP_Z = $2800;
kickasm(pc PERSP_Z) {{ kickasm(pc PERSP_Z) {{
{ {
@ -291,8 +314,8 @@ signed byte* COSQ = $2200;
signed byte* SINQ = COSQ+$40; // sin(x) = cos(x+PI/2) signed byte* SINQ = COSQ+$40; // sin(x) = cos(x+PI/2)
kickasm(pc COSH) {{ kickasm(pc COSH) {{
{ {
.var min = -$1fff .var min = -$2000
.var max = $1fff .var max = $2000
.var ampl = max-min; .var ampl = max-min;
.for(var i=0;i<$140;i++) { .for(var i=0;i<$140;i++) {
.var rad = i*2*PI/256; .var rad = i*2*PI/256;
@ -302,8 +325,8 @@ kickasm(pc COSH) {{
}} }}
kickasm(pc COSQ) {{ kickasm(pc COSQ) {{
{ {
.var min = -$0fff .var min = -$1000
.var max = $0fff .var max = $1000
.var ampl = max-min; .var ampl = max-min;
.for(var i=0;i<$140;i++) { .for(var i=0;i<$140;i++) {
.var rad = i*2*PI/256; .var rad = i*2*PI/256;

View File

@ -8,21 +8,21 @@
import "print.kc" import "print.kc"
signed byte[] vals = {-95, -64, 0, 64, 95}; signed byte[] vals = {-95, -64, -32, -16, 0, 16, 32, 64, 95};
void main() { void main() {
print_cls(); init_screen();
byte* at_line = $400; byte* at_line = $400;
byte* at = at_line+4; byte* at = at_line+4;
for(byte k: 0..4) { for(byte k: 0..8) {
print_sbyte_at(vals[k], at); print_sbyte_at(vals[k], at);
at += 4; at += 4;
} }
for(byte i: 0..4) { for(byte i: 0..8) {
at_line +=40; at_line +=40;
at = at_line; at = at_line;
print_sbyte_at(vals[i], at); print_sbyte_at(vals[i], at);
for(byte j: 0..4) { for(byte j: 0..8) {
at += 4; at += 4;
signed byte r = fmul8(vals[i], vals[j]); signed byte r = fmul8(vals[i], vals[j]);
print_sbyte_at(r, at); print_sbyte_at(r, at);
@ -30,6 +30,22 @@ void main() {
} }
} }
void init_screen() {
print_cls();
byte* COLS = $d800;
byte WHITE = 1;
for(byte l: 0..39) {
COLS[l] = WHITE;
}
for(byte m: 0..24) {
COLS[0] = WHITE;
COLS[1] = WHITE;
COLS[2] = WHITE;
COLS[3] = WHITE;
COLS += 40;
}
}
// Pointers to a, b and c=a*b // Pointers to a, b and c=a*b
signed byte* ap = $fd; signed byte* ap = $fd;
@ -53,8 +69,6 @@ signed byte fmul8(signed byte a, signed byte b) {
return *cp; return *cp;
} }
// mulf_sqr tables will contain f(x)=int(x*x) and g(x) = f(1-x). // mulf_sqr tables will contain f(x)=int(x*x) and g(x) = f(1-x).
// f(x) = >(( x * x )) // f(x) = >(( x * x ))
byte* mulf_sqr1 = $2000; byte* mulf_sqr1 = $2000;
@ -75,5 +89,4 @@ kickasm(pc mulf_sqr2) {{
.if(i>159 && i<=351 ) { .byte round(((255-i)*(255-i))/256) } .if(i>159 && i<=351 ) { .byte round(((255-i)*(255-i))/256) }
.if(i>351) { .byte round(((i-511)*(i-511))/256) } .if(i>351) { .byte round(((i-511)*(i-511))/256) }
} }
}} }}