bubble: shrink by generating a lot of the sine tables

This commit is contained in:
Vince Weaver 2024-03-03 00:58:18 -05:00
parent ebf7edec07
commit fc3ab7444c
5 changed files with 243 additions and 24 deletions

View File

@ -5,7 +5,7 @@ TOKENIZE = ../../../utils/asoft_basic-utils/tokenize_asoft
LINKER_SCRIPTS = ../../../linker_scripts
EMPTY_DISK = ../../../empty_disk
all: bubble.dsk make_table
all: bubble.dsk make_table make_table2 make_table3 compact_sine
bubble.dsk: HELLO BUBBLE.BAS BUBBLE BUBBLE_C64 \
BUBBLE_ORIG BUBBLE_ROLLED DIAMOND FAST_DOTS \
@ -103,6 +103,33 @@ make_table.o: make_table.c
###
make_table2: make_table2.o
$(CC) -o make_table2 make_table2.o -lm
make_table2.o: make_table2.c
$(CC) $(CFLAGS) -c make_table2.c
###
make_table3: make_table3.o
$(CC) -o make_table3 make_table3.o -lm
make_table3.o: make_table3.c
$(CC) $(CFLAGS) -c make_table3.c
###
compact_sine: compact_sine.o
$(CC) -o compact_sine compact_sine.o -lm
compact_sine.o: compact_sine.c
$(CC) $(CFLAGS) -c compact_sine.c
###
clean:
rm -f *~ *.o *.lst BUBBLE.BAS HELLO BUBBLE BUBBLE_C64 \
DIAMOND BUBBLE_ORIG BUBBLE_ROLLED FAST_DOTS
DIAMOND BUBBLE_ORIG BUBBLE_ROLLED FAST_DOTS \
make_table make_table2 make_table3 compact_sine

View File

@ -19,6 +19,7 @@
; 1443 bytes -- tail call optimization
; 1436 bytes -- call clear screen from page flip code
; 1434 bytes -- optimize HPLOT
; 1131 bytes -- generate SINE table from 65 entry lookup
; soft-switches
@ -72,6 +73,8 @@ bubble:
jsr hgr_clear_codegen
jsr setup_sine_table
;=======================
; init graphics
@ -356,50 +359,109 @@ log_lookup:
.include "hgr_clear_codegen.s"
;.align $100
sines:
.byte $13,$12,$12,$11,$10,$10,$0F,$0E,$0E,$0D,$0D,$0C,$0C,$0B,$0B,$0B
.byte $0A,$0A,$09,$09,$09,$08,$08,$08,$08,$08,$07,$07,$07,$07,$07,$07
.byte $07,$07,$07,$07,$07,$07,$07,$08,$08,$08,$08,$08,$09,$09,$09,$0A
.byte $0A,$0B,$0B,$0B,$0C,$0C,$0D,$0D,$0E,$0E,$0F,$10,$10,$11,$12,$12
.byte $13,$14,$14,$15,$16,$17,$18,$18,$19,$1A,$1B,$1C,$1D,$1E,$1E,$1F
.byte $20,$21,$22,$23,$24,$25,$26,$27,$28,$29,$2A,$2B,$2C,$2D,$2E,$2F
.align $100
; note: min=7, around 32
; max=89 ($59), around 160
; subtract 7, so 0...82? halfway = 41 = $29 + 7 = $30
; halfway= 6*16 = 96
sines = $6c00
sines2 = $6d00
;===================================
;
;
; final_sine[i]=quarter_sine[i]; // 0..64
; final_sine[128-i]=quarter_sine[i]; // 64..128
; final_sine[128+i]=0x60-quarter_sine[i]; // 128..192
; final_sine[256-i]=0x60-quarter_sine[i]; // 192..256
setup_sine_table:
ldy #64
ldx #64
setup_sine_loop:
lda sines_base,Y
sta sines,Y
sta sines2,Y
sta sines,X
sta sines2,X
lda #$60
sec
sbc sines_base,Y
sta sines+128,Y
sta sines2+128,Y
sta sines+128,X
sta sines2+128,X
inx
dey
bpl setup_sine_loop
rts
sines_base:
.byte $30,$31,$32,$33,$34,$35,$36,$37,$38,$39,$3A,$3B,$3C,$3D,$3E,$3F
.byte $40,$41,$42,$42,$43,$44,$45,$46,$47,$48,$48,$49,$4A,$4B,$4C,$4C
.byte $4D,$4E,$4E,$4F,$50,$50,$51,$52,$52,$53,$53,$54,$54,$55,$55,$55
.byte $56,$56,$57,$57,$57,$58,$58,$58,$58,$58,$59,$59,$59,$59,$59,$59
.byte $59
.if 0
sines:
.byte $30,$31,$32,$33,$34,$35,$36,$37,$38,$39,$3A,$3B,$3C,$3D,$3E,$3F
.byte $40,$41,$42,$42,$43,$44,$45,$46,$47,$48,$48,$49,$4A,$4B,$4C,$4C
.byte $4D,$4E,$4E,$4F,$50,$50,$51,$52,$52,$53,$53,$54,$54,$55,$55,$55
.byte $56,$56,$57,$57,$57,$58,$58,$58,$58,$58,$59,$59,$59,$59,$59,$59
.byte $59,$59,$59,$59,$59,$59,$59,$58,$58,$58,$58,$58,$57,$57,$57,$56
.byte $56,$55,$55,$55,$54,$54,$53,$53,$52,$52,$51,$50,$50,$4F,$4E,$4E
.byte $4D,$4C,$4C,$4B,$4A,$49,$48,$48,$47,$46,$45,$44,$43,$42,$42,$41
.byte $40,$3F,$3E,$3D,$3C,$3B,$3A,$39,$38,$37,$36,$35,$34,$33,$32,$31
.byte $30,$2F,$2E,$2D,$2C,$2B,$2A,$29,$28,$27,$26,$25,$24,$23,$22,$21
.byte $20,$1F,$1E,$1E,$1D,$1C,$1B,$1A,$19,$18,$18,$17,$16,$15,$14,$14
sines2:
; original start
.byte $13,$12,$12,$11,$10,$10,$0F,$0E,$0E,$0D,$0D,$0C,$0C,$0B,$0B,$0B
.byte $0A,$0A,$09,$09,$09,$08,$08,$08,$08,$08,$07,$07,$07,$07,$07,$07
.byte $07,$07,$07,$07,$07,$07,$07,$08,$08,$08,$08,$08,$09,$09,$09,$0A
.byte $0A,$0B,$0B,$0B,$0C,$0C,$0D,$0D,$0E,$0E,$0F,$10,$10,$11,$12,$12
.byte $13,$14,$14,$15,$16,$17,$18,$18,$19,$1A,$1B,$1C,$1D,$1E,$1E,$1F
.byte $20,$21,$22,$23,$24,$25,$26,$27,$28,$29,$2A,$2B,$2C,$2D,$2E,$2F
sines2:
.byte $30,$31,$32,$33,$34,$35,$36,$37,$38,$39,$3A,$3B,$3C,$3D,$3E,$3F
.byte $40,$41,$42,$42,$43,$44,$45,$46,$47,$48,$48,$49,$4A,$4B,$4C,$4C
.byte $4D,$4E,$4E,$4F,$50,$50,$51,$52,$52,$53,$53,$54,$54,$55,$55,$55
.byte $56,$56,$57,$57,$57,$58,$58,$58,$58,$58,$59,$59,$59,$59,$59,$59
.byte $59,$59,$59,$59,$59,$59,$59,$58,$58,$58,$58,$58,$57,$57,$57,$56
.byte $56,$55,$55,$55,$54,$54,$53,$53,$52,$52,$51,$50,$50,$4F,$4E,$4E
.byte $4D,$4C,$4C,$4B,$4A,$49,$48,$48,$47,$46,$45,$44,$43,$42,$42,$41
.byte $40,$3F,$3E,$3D,$3C,$3B,$3A,$39,$38,$37,$36,$35,$34,$33,$32,$31
.byte $30,$2F,$2E,$2D,$2C,$2B,$2A,$29,$28,$27,$26,$25,$24,$23,$22,$21
.byte $20,$1F,$1E,$1E,$1D,$1C,$1B,$1A,$19,$18,$18,$17,$16,$15,$14,$14
.byte $13,$12,$12,$11,$10,$10,$0F,$0E,$0E,$0D,$0D,$0C,$0C,$0B,$0B,$0B
.byte $0A,$0A,$09,$09,$09,$08,$08,$08,$08,$08,$07,$07,$07,$07,$07,$07
.byte $07,$07,$07,$07,$07,$07,$07,$08,$08,$08,$08,$08,$09,$09,$09,$0A
.byte $0A,$0B,$0B,$0B,$0C,$0C,$0D,$0D,$0E,$0E,$0F,$10,$10,$11,$12,$12
.byte $13,$14,$14,$15,$16,$17,$18,$18,$19,$1A,$1B,$1C,$1D,$1E,$1E,$1F
.byte $20,$21,$22,$23,$24,$25,$26,$27,$28,$29,$2A,$2B,$2C,$2D,$2E,$2F
.endif
; floor(s*cos((x-96)*PI*2/256.0)+48.5);
.align $100
cosines:
.byte $13,$14,$14,$15,$16,$17,$18,$18,$19,$1A,$1B,$1C,$1D,$1E,$1E,$1F
.byte $20,$21,$22,$23,$24,$25,$26,$27,$28,$29,$2A,$2B,$2C,$2D,$2E,$2F
.byte $30,$31,$32,$33,$34,$35,$36,$37,$38,$39,$3A,$3B,$3C,$3D,$3E,$3F
.byte $40,$41,$42,$42,$43,$44,$45,$46,$47,$48,$48,$49,$4A,$4B,$4C,$4C
.byte $4D,$4E,$4E,$4F,$50,$50,$51,$52,$52,$53,$53,$54,$54,$55,$55,$55
.byte $56,$56,$57,$57,$57,$58,$58,$58,$58,$58,$59,$59,$59,$59,$59,$59
.byte $59,$59,$59,$59,$59,$59,$59,$58,$58,$58,$58,$58,$57,$57,$57,$56
.byte $56,$55,$55,$55,$54,$54,$53,$53,$52,$52,$51,$50,$50,$4F,$4E,$4E
.byte $4D,$4C,$4C,$4B,$4A,$49,$48,$48,$47,$46,$45,$44,$43,$42,$42,$41
@ -410,13 +472,17 @@ cosines:
.byte $0A,$0A,$09,$09,$09,$08,$08,$08,$08,$08,$07,$07,$07,$07,$07,$07
.byte $07,$07,$07,$07,$07,$07,$07,$08,$08,$08,$08,$08,$09,$09,$09,$0A
.byte $0A,$0B,$0B,$0B,$0C,$0C,$0D,$0D,$0E,$0E,$0F,$10,$10,$11,$12,$12
.byte $13,$14,$14,$15,$16,$17,$18,$18,$19,$1A,$1B,$1C,$1D,$1E,$1E,$1F
.byte $20,$21,$22,$23,$24,$25,$26,$27,$28,$29,$2A,$2B,$2C,$2D,$2E,$2F
.byte $30,$31,$32,$33,$34,$35,$36,$37,$38,$39,$3A,$3B,$3C,$3D,$3E,$3F
.byte $40,$41,$42,$42,$43,$44,$45,$46,$47,$48,$48,$49,$4A,$4B,$4C,$4C
.byte $4D,$4E,$4E,$4F,$50,$50,$51,$52,$52,$53,$53,$54,$54,$55,$55,$55
.byte $56,$56,$57,$57,$57,$58,$58,$58,$58,$58,$59,$59,$59,$59,$59,$59
cosines2:
.byte $13,$14,$14,$15,$16,$17,$18,$18,$19,$1A,$1B,$1C,$1D,$1E,$1E,$1F
.byte $20,$21,$22,$23,$24,$25,$26,$27,$28,$29,$2A,$2B,$2C,$2D,$2E,$2F
.byte $30,$31,$32,$33,$34,$35,$36,$37,$38,$39,$3A,$3B,$3C,$3D,$3E,$3F
.byte $40,$41,$42,$42,$43,$44,$45,$46,$47,$48,$48,$49,$4A,$4B,$4C,$4C
.byte $4D,$4E,$4E,$4F,$50,$50,$51,$52,$52,$53,$53,$54,$54,$55,$55,$55
.byte $56,$56,$57,$57,$57,$58,$58,$58,$58,$58,$59,$59,$59,$59,$59,$59
.byte $59,$59,$59,$59,$59,$59,$59,$58,$58,$58,$58,$58,$57,$57,$57,$56
.byte $56,$55,$55,$55,$54,$54,$53,$53,$52,$52,$51,$50,$50,$4F,$4E,$4E
.byte $4D,$4C,$4C,$4B,$4A,$49,$48,$48,$47,$46,$45,$44,$43,$42,$42,$41
@ -427,3 +493,10 @@ cosines2:
.byte $0A,$0A,$09,$09,$09,$08,$08,$08,$08,$08,$07,$07,$07,$07,$07,$07
.byte $07,$07,$07,$07,$07,$07,$07,$08,$08,$08,$08,$08,$09,$09,$09,$0A
.byte $0A,$0B,$0B,$0B,$0C,$0C,$0D,$0D,$0E,$0E,$0F,$10,$10,$11,$12,$12
.byte $13,$14,$14,$15,$16,$17,$18,$18,$19,$1A,$1B,$1C,$1D,$1E,$1E,$1F
.byte $20,$21,$22,$23,$24,$25,$26,$27,$28,$29,$2A,$2B,$2C,$2D,$2E,$2F
.byte $30,$31,$32,$33,$34,$35,$36,$37,$38,$39,$3A,$3B,$3C,$3D,$3E,$3F
.byte $40,$41,$42,$42,$43,$44,$45,$46,$47,$48,$48,$49,$4A,$4B,$4C,$4C
.byte $4D,$4E,$4E,$4F,$50,$50,$51,$52,$52,$53,$53,$54,$54,$55,$55,$55
.byte $56,$56,$57,$57,$57,$58,$58,$58,$58,$58,$59,$59,$59,$59,$59,$59

View File

@ -0,0 +1,54 @@
#include <stdio.h>
static unsigned char const good_sine[256]={
0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3A,0x3B,0x3C,0x3D,0x3E,0x3F,
0x40,0x41,0x42,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x48,0x49,0x4A,0x4B,0x4C,0x4C,
0x4D,0x4E,0x4E,0x4F,0x50,0x50,0x51,0x52,0x52,0x53,0x53,0x54,0x54,0x55,0x55,0x55,
0x56,0x56,0x57,0x57,0x57,0x58,0x58,0x58,0x58,0x58,0x59,0x59,0x59,0x59,0x59,0x59,
0x59,0x59,0x59,0x59,0x59,0x59,0x59,0x58,0x58,0x58,0x58,0x58,0x57,0x57,0x57,0x56,
0x56,0x55,0x55,0x55,0x54,0x54,0x53,0x53,0x52,0x52,0x51,0x50,0x50,0x4F,0x4E,0x4E,
0x4D,0x4C,0x4C,0x4B,0x4A,0x49,0x48,0x48,0x47,0x46,0x45,0x44,0x43,0x42,0x42,0x41,
0x40,0x3F,0x3E,0x3D,0x3C,0x3B,0x3A,0x39,0x38,0x37,0x36,0x35,0x34,0x33,0x32,0x31,
0x30,0x2F,0x2E,0x2D,0x2C,0x2B,0x2A,0x29,0x28,0x27,0x26,0x25,0x24,0x23,0x22,0x21,
0x20,0x1F,0x1E,0x1E,0x1D,0x1C,0x1B,0x1A,0x19,0x18,0x18,0x17,0x16,0x15,0x14,0x14,
0x13,0x12,0x12,0x11,0x10,0x10,0x0F,0x0E,0x0E,0x0D,0x0D,0x0C,0x0C,0x0B,0x0B,0x0B,
0x0A,0x0A,0x09,0x09,0x09,0x08,0x08,0x08,0x08,0x08,0x07,0x07,0x07,0x07,0x07,0x07,
0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x08,0x08,0x08,0x08,0x08,0x09,0x09,0x09,0x0A,
0x0A,0x0B,0x0B,0x0B,0x0C,0x0C,0x0D,0x0D,0x0E,0x0E,0x0F,0x10,0x10,0x11,0x12,0x12,
0x13,0x14,0x14,0x15,0x16,0x17,0x18,0x18,0x19,0x1A,0x1B,0x1C,0x1D,0x1E,0x1E,0x1F,
0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2A,0x2B,0x2C,0x2D,0x2E,0x2F,
};
static unsigned char const quarter_sine[65]={
0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3A,0x3B,0x3C,0x3D,0x3E,0x3F,
0x40,0x41,0x42,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x48,0x49,0x4A,0x4B,0x4C,0x4C,
0x4D,0x4E,0x4E,0x4F,0x50,0x50,0x51,0x52,0x52,0x53,0x53,0x54,0x54,0x55,0x55,0x55,
0x56,0x56,0x57,0x57,0x57,0x58,0x58,0x58,0x58,0x58,0x59,0x59,0x59,0x59,0x59,0x59,0x59
};
static unsigned char final_sine[256];
int main(int argc, char **argv) {
int i,errors=0;
for(i=0;i<65;i++) {
final_sine[i]=quarter_sine[i]; // 0..64
final_sine[128-i]=quarter_sine[i]; // 64..128
final_sine[128+i]=0x60-quarter_sine[i]; // 128..192
final_sine[256-i]=0x60-quarter_sine[i]; // 192..256
}
for(i=0;i<256;i++) {
if (good_sine[i]!=final_sine[i]) {
printf("%d: %d!=%d\n",i,good_sine[i],final_sine[i]);
errors++;
}
}
printf("Total Errors=%d\n",errors);
return 0;
}

View File

@ -9,5 +9,5 @@
30 PRINT:PRINT "PRESS ANY KEY TO START"
40 GET A$
50 IF A$="Q" THEN END
60 PRINT:PRINT CHR$(4)"BRUN BUBBLE_C64"
60 PRINT:PRINT CHR$(4)"BRUN BUBBLE_TINY"

View File

@ -0,0 +1,65 @@
/* Bubble table */
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <math.h>
#define PI 3.14159265358979323846264
int main(int argc, char **argv) {
int i,t;
double d;
// in other words, scaled to 46+/-256/2pi. and shifted by 2*46
// the shift is there to remove the offset from u and v
// being sums of 2 sins and 2 coses.
// Building in the offset means my coordinates are all positive
//cos_t .char round(cos((range(256)-92)*2.0*pi/256)*256.0/2.0/pi+46)
//sin_t .char round(sin((range(256)-92)*2.0*pi/256)*256.0/2.0/pi+46)
// sine: ; floor(s*sin((x-96)*PI*2/256.0)+48.5);
int sine_table[256];
int cos_table[256];
int s=41; // approximately 1/6.28 in fixed point
for(i=0;i<256;i++) {
d=floor(s*sin((i-96)*2.0*PI/256.0)+48.5);
sine_table[i]=(int)d;
}
printf("sines:\n");
for(i=0;i<256;i++) {
if (i%16==0) printf("\t.byte\t");
t=sine_table[i];
printf("$%02X",t&0xff);
if (i%16!=15) printf(",");
else printf("\n");
}
for(i=0;i<256;i++) {
fprintf(stderr,"%d %d\n",i,sine_table[i]);
}
for(i=0;i<256;i++) {
d=floor(s*cos((i-96)*2.0*PI/256.0)+48.5);
cos_table[i]=(int)d;
}
printf("cosines:\n");
for(i=0;i<256;i++) {
if (i%16==0) printf("\t.byte\t");
t=cos_table[i];
printf("$%02X",t&0xff);
if (i%16!=15) printf(",");
else printf("\n");
}
}