bubble_hgr: more size optimization

This commit is contained in:
Vince Weaver 2024-05-06 15:18:14 -04:00
parent bb6627b75a
commit 83bd798037
7 changed files with 1038 additions and 361 deletions

View File

@ -7,10 +7,12 @@ EMPTY_DISK = ../../../empty_disk
all: bubble.dsk
bubble.dsk: HELLO BUBBLE_TINY
bubble.dsk: HELLO BUBBLE_TINY BUBBLE_SQUARES BUBBLE_ROM
cp $(EMPTY_DISK)/empty.dsk bubble.dsk
$(DOS33) -y bubble.dsk SAVE A HELLO
$(DOS33) -y bubble.dsk BSAVE -a 0x0C00 BUBBLE_TINY
$(DOS33) -y bubble.dsk BSAVE -a 0x0C00 BUBBLE_SQUARES
$(DOS33) -y bubble.dsk BSAVE -a 0x0C00 BUBBLE_ROM
###
@ -27,6 +29,24 @@ bubble_tiny.o: bubble_tiny.s
###
clean:
rm -f *~ *.o *.lst HELLO BUBBLE_TINY
BUBBLE_SQUARES: bubble_squares.o
ld65 -o BUBBLE_SQUARES bubble_squares.o -C $(LINKER_SCRIPTS)/apple2_c00.inc
bubble_squares.o: bubble_squares.s hgr_clear_codegen.s
ca65 -o bubble_squares.o bubble_squares.s -l bubble_squares.lst
###
BUBBLE_ROM: bubble_rom.o
ld65 -o BUBBLE_ROM bubble_rom.o -C $(LINKER_SCRIPTS)/apple2_c00.inc
bubble_rom.o: bubble_rom.s
ca65 -o bubble_rom.o bubble_rom.s -l bubble_rom.lst
###
clean:
rm -f *~ *.o *.lst HELLO BUBBLE_TINY BUBBLE_ROM

View File

@ -0,0 +1,336 @@
; bubble universe -- Apple II Hires
; even more size optimized version
; by Vince `deater` Weaver
; this version based on fast c64 code by serato_fig
; as posted to the sizecoding discord
; based on his TIC-80 variant
; originally was working off the BASIC code posted on the pouet forum
; original effect by yuruyrau on twitter
; 534 bytes -- original tiny version
; 529 bytes -- back out self modifying U/V code (allows more compact tables)
; 492 bytes -- hook up compact sine generation
; 445 bytes -- strip out keyboard code
; 208 bytes -- use ROM routines
; 203 bytes -- optimize page flip
; soft-switches
KEYPRESS = $C000
KEYRESET = $C010
PAGE1 = $C054
PAGE2 = $C055
; ROM routines
BKGNDZ = $F3F2 ; clear current page to 0
BKGND0 = $F3F4 ; clear current page to A
HGR2 = $F3D8 ; set hires page2 and clear $4000-$5fff
HGR = $F3E2 ; set hires page1 and clear $2000-$3fff
HPLOT0 = $F457 ; plot at (Y,X), (A)
HCOLOR1 = $F6F0 ; set HGR_COLOR to value in X
;COLORTBL = $F6F6
;WAIT = $FCA8 ; delay 1/2(26+27A+5A^2) us
; zero page
GBASL = $26
GBASH = $27
HPLOTYL = $92
I = $D0
J = $D1
T = $D7
U = $D8
V = $D9
IT = $DA
IS = $DB
HGR_PAGE = $E6
OUTL = $FC
OUTH = $FD
sines = sines_base-$1A ; overlaps some code
sines2 = sines+$100 ; duplicate so we can index cosine into it
cosines = sines+$c0
bubble:
;==========================
; setup lookup tables
;==========================
; jsr hgr_make_tables
; jsr hgr_clear_codegen
;=========================
; reconstruct sine base
;=========================
; generate the linear $30..$42 part
; and also string of $59 on end
; removes 26 bytes from table
; at expense of 16+4 bytes of code
; (4 from jsr/rts of moving over-writable table code)
ldy #$19 ; offset
ldx #$48 ; want to write $48 downto $30
; with $42 doubled
looper:
txa
sta sines,Y ; sines+12 .... sines
lda #$59 ; also write $59 off the top
sta fifty_nines,Y
cpy #$13 ; we could save more bytes if we didn't
beq skipper ; bother trying to be exact
dex
skipper:
dey
bpl looper
;==========================
; make sine/cosine tables
;==========================
; floor(s*sin((x-96)*PI*2/256.0)+48.5);
;===================================
; 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:
ldx #64
ldy #64
setup_sine_loop:
lda sines,X
; sta sines,X
sta sines,Y
lda #$60
sec
sbc sines,X
sta sines+128,X
sta sines+128,Y
iny
dex
bpl setup_sine_loop
;=======================
; init variables
; HGR leaves A at 0
; lda #0
; sta U
; sta V
; sta T
;=======================
; init variables
;=======================
; wipe all of zero page but $FF
; in theory we only need to clear/copy $00..$C0
; but not sure how to use to our advantage
inx ; X=0
ldy #0 ; Y=0
init_loop:
; sta a:$D0,X ; force 16-bit so doesn't wrap
; because I guess it's bad to wipe zero page?
; maybe it isn't?
sty $D0,X ; clear zero page
lda sines,X ; duplicate sine table for cosine use
sta sines2,X
dex
bne init_loop
;=======================
; init graphics
jsr HGR ; why both?
jsr HGR2
ldx #7
jsr HCOLOR1
;=========================
;=========================
; main loop
;=========================
;=========================
next_frame:
; reset I*T
lda T
sta IT
; reset I*S
lda #0 ; Y should be 0 here?
sta IS
i_smc:
lda #1 ; 40
sta I
i_loop:
j_smc:
lda #24 ; 200
sta J
j_loop:
; where S=41 (approximately 1/6.28)
; calc: a=i*s+v;
; calc: b=i+t+u;
; u=sines[a]+sines[b];
; v=cosines[a]+cosines[b];
clc
lda IS
adc V
tay
clc
lda IT
adc U
tax
clc ; 2
lda cosines,Y ; 4+
adc cosines,X ; 4+
sta V
; max value for both $60 so carry not set
lda sines,Y ; 4+
adc sines,X ; 4+
sta U ; 3
;===========================================================
; HPLOT U+44,V+96
; U is centered at 96, to get to center of 280 screen add 44
; U already in A
adc #44 ; 2
tax ; 2
; calculate Ypos
lda V
; HPLOT0 = $F457 ; plot at (Y,X), (A)
ldy #0
jsr HPLOT0
dec J
bne j_loop
done_j:
clc
lda IS
adc #41 ; 1/6.28 = 0.16 = 0 0 1 0 1 0 0 0 = 0x28
sta IS
dec I
bne i_loop
done_i:
inc T
end:
flip_pages:
; flip pages
; if $20 (draw PAGE1) draw PAGE2, SHOW page1
; if $40 (draw PAGE2) draw PAGE1, SHOW page2
lda HGR_PAGE
eor #$60
sta HGR_PAGE
cmp #$40
bne flip2
flip1:
sta PAGE1
beq done_flip
flip2:
sta PAGE2
done_flip:
; lda HGR_PAGE
sta OUTH
clear_loop_fix:
lda #$00
tay
; assume INL starts at 0 from clearing earlier
clear_loop:
sta (OUTL),Y
iny
bne clear_loop
inc OUTH
lda OUTH
and #$1f
bne clear_loop_fix
; jsr BKGNDZ ; clear screen to black
beq next_frame ; bra
;.include "hgr_clear_codegen.s"
.byte $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00
.byte $00,$00,$00,$00,$00,$00,$00,$00,$00,$00
; need 26 bytes of destroyable area?
; alternately, need code to copy 26 bytes
; .byte $30,$31,$32,$33,$34,$35,$36,$37,$38,$39,$3A,$3B,$3C,$3D,$3E,$3F
; .byte $40,$41,$42
;old_sines_base:
; .byte $42,$43,$44,$45,$46,$47,$48,
sines_base:
.byte $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
fifty_nines:
; .byte $59,$59,$59,$59,$59,$59
; .byte $59
; floor(s*cos((x-96)*PI*2/256.0)+48.5);

View File

@ -0,0 +1,425 @@
; bubble universe -- Apple II Hires
; even more size optimized version
; by Vince `deater` Weaver
; this version based on fast c64 code by serato_fig
; as posted to the sizecoding discord
; based on his TIC-80 variant
; originally was working off the BASIC code posted on the pouet forum
; original effect by yuruyrau on twitter
; 534 bytes -- original tiny version
; 529 bytes -- back out self modifying U/V code (allows more compact tables)
; soft-switches
KEYPRESS = $C000
KEYRESET = $C010
PAGE1 = $C054
PAGE2 = $C055
; ROM routines
BKGND0 = $F3F4 ; clear current page to A
HGR2 = $F3D8 ; set hires page2 and clear $4000-$5fff
HGR = $F3E2 ; set hires page1 and clear $2000-$3fff
HPLOT0 = $F457 ; plot at (Y,X), (A)
;HCOLOR1 = $F6F0 ; set HGR_COLOR to value in X
;COLORTBL = $F6F6
;WAIT = $FCA8 ; delay 1/2(26+27A+5A^2) us
; zero page
GBASL = $26
GBASH = $27
HPLOTYL = $92
I = $D0
J = $D1
T = $D7
U = $D8
V = $D9
IT = $DA
IS = $DB
HGR_PAGE = $E6
INL = $FC
INH = $FD
OUTL = $FE
OUTH = $FF
; const
;NUM = 32
;NUM = 24
bubble:
;========================
; setup lookup tables
jsr hgr_make_tables
jsr hgr_clear_codegen
jsr setup_sine_table
;=======================
; init graphics
jsr HGR
jsr HGR2
;=======================
; init variables
; HGR leaves A at 0
; lda #0
sta U
sta V
sta T
;=========================
;=========================
; main loop
;=========================
;=========================
next_frame:
; reset I*T
lda T
sta IT
; reset I*S
lda #0 ; Y should be 0 here?
sta IS
i_smc:
lda #24 ; 40
sta I
i_loop:
j_smc:
lda #24 ; 200
sta J
j_loop:
; where S=41 (approximately 1/6.28)
; calc: a=i*s+v;
; calc: b=i+t+u;
; u=sines[a]+sines[b];
; v=cosines[a]+cosines[b];
clc
lda IS
adc V
tay
clc
lda IT
adc U
tax
clc ; 2
lda cosines,Y ; 4+
adc cosines,X ; 4+
sta V
; max value for both $60 so carry not set
lda sines,Y ; 4+
adc sines,X ; 4+
sta U ; 3
;===========================================================
; HPLOT U+44,V+96
; U is centered at 96, to get to center of 280 screen add 44
; U already in A
adc #44 ; 2
tax ; 2
; calculate Ypos
ldy V
; "fast" hplot, Xpos in X, Ypos in A
; Apple II hi-res is more-or-less 280x192
; two consecutive pixels on are white
; single pixels are colored based on palette
; we treat things as a monochrome display, on a color
; display odd/even pixels will have different colors
; The Y memory offset is a horrible interleaved mess, so we use
; a lookup table we generated at start. We also add in
; the proper value for page-flipping
; Apple II hi-res is 7 pixels/byte, so we also pre-generate
; div and mod by 7 tables at start and use those
; instead of dividing by 7
; We cheat and don't worry about the X positions larger
; than 256 because our algorithm only goes up to 208
lda hposn_low,Y ; 4
sta GBASL ; 3
lda hposn_high,Y ; 4
ora HGR_PAGE ; 3
sta GBASH ; 3
; 21
ldy div7_table,X ; 4
lda mod7_table,X ; 4
tax ; 2
; 31
; get current 7-bit pixel range, OR in to set new pixel
lda (GBASL),Y ; 5
ora log_lookup,X ; 4
; eor log_lookup,X ; 4
sta (GBASL),Y ; 6
; 46
dec J
bne j_loop
done_j:
clc
lda IS
adc #41 ; 1/6.28 = 0.16 = 0 0 1 0 1 0 0 0 = 0x28
sta IS
dec I
bne i_loop
done_i:
inc T
end:
lda KEYPRESS
bpl flip_pages
bit KEYRESET
; 0110 -> 0100
and #$5f ; to handle lowercase too...
cmp #'A'
bne check_z
inc i_smc+1
jmp done_keys
check_z:
cmp #'Z'
bne check_j
dec i_smc+1
jmp done_keys
check_j:
cmp #'J'
bne check_m
inc j_smc+1
jmp done_keys
check_m:
cmp #'M'
bne done_keys
dec j_smc+1
done_keys:
flip_pages:
; flip pages
; if $20 (draw PAGE1) draw PAGE2, SHOW page1
; if $40 (draw PAGE2) draw PAGE1, SHOW page2
lda HGR_PAGE
eor #$60
sta HGR_PAGE
cmp #$40
bne flip2
flip1:
bit PAGE1
lda #0
jsr hgr_page2_clearscreen
jmp next_frame
flip2:
bit PAGE2
lda #0
jsr hgr_page1_clearscreen
jmp next_frame
div7_table = $6800
mod7_table = $6900
hposn_high = $6a00
hposn_low = $6b00
hgr_make_tables:
;=====================
; make /7 %7 tables
;=====================
hgr_make_7_tables:
lda #0
tax
tay
div7_loop:
sta div7_table,Y
mod7_smc:
stx mod7_table
inx
cpx #7
bne div7_not7
clc
adc #1
ldx #0
div7_not7:
inc mod7_smc+1 ; assume on page boundary
iny
bne div7_loop
; Hposn table
; hposn_low, hposn_high will each be filled with $C0 bytes
; based on routine by John Brooks
; posted on comp.sys.apple2 on 2018-07-11
; https://groups.google.com/d/msg/comp.sys.apple2/v2HOfHOmeNQ/zD76fJg_BAAJ
; clobbers A,X
; preserves Y
; vmw note: version I was using based on applesoft HPOSN was ~64 bytes
; this one is 37 bytes
build_hposn_tables:
ldx #0
btmi:
txa
and #$F8
bpl btpl1
ora #5
btpl1:
asl
bpl btpl2
ora #5
btpl2:
asl
asl
sta hposn_low, X
txa
and #7
rol
asl hposn_low, X
rol
; ora #$20
sta hposn_high, X
inx
cpx #$C0
bne btmi
rts
; which of 7 pixels to draw
; note high bit is set to pick blue/orange palette
; clear to get purple/green instead
log_lookup:
.byte $81,$82,$84,$88,$90,$A0,$C0,$80
; the current "fast" code expects to be aligned on boundary
; also have to double things up as the code can go up to 255 off
; end for speed reasons
; floor(s*sin((x-96)*PI*2/256.0)+48.5);
.include "hgr_clear_codegen.s"
; 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
cosines = $6e00
cosines2= $6f00
;===================================
;
;
; 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
ldy #0
cosine_loop:
lda sines+192,Y
sta cosines,Y
sta cosines2,Y
iny
bne cosine_loop
rts
; .byte $30,$31,$32,$33,$34,$35,$36,$37,$38,$39,$3A,$3B,$3C,$3D,$3E,$3F
; .byte $40,$41,$42
;old_sines_base:
; .byte $42,$43,$44,$45,$46,$47,$48,
sines_base:
.byte $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
fifty_nines:
; .byte $59,$59,$59,$59,$59,$59
; .byte $59
; floor(s*cos((x-96)*PI*2/256.0)+48.5);

View File

@ -13,6 +13,7 @@
; 534 bytes -- original tiny version
; 529 bytes -- back out self modifying U/V code (allows more compact tables)
; 492 bytes -- hook up compact sine generation
; soft-switches
@ -54,27 +55,81 @@ INH = $FD
OUTL = $FE
OUTH = $FF
; const
sines = sines_base-$1A ; overlaps some code
sines2 = sines+$100 ; duplicate so we can index cosine into it
cosines = sines+$c0
;NUM = 32
;NUM = 24
bubble:
;========================
;==========================
; setup lookup tables
;==========================
jsr hgr_make_tables
jsr hgr_clear_codegen
jsr setup_sine_table
;=======================
; init graphics
;=========================
; reconstruct sine base
;=========================
; generate the linear $30..$42 part
; and also string of $59 on end
; removes 26 bytes from table
; at expense of 16+4 bytes of code
; (4 from jsr/rts of moving over-writable table code)
ldy #$19 ; offset
ldx #$48 ; want to write $48 downto $30
; with $42 doubled
looper:
txa
sta sines,Y ; sines+12 .... sines
lda #$59 ; also write $59 off the top
sta fifty_nines,Y
cpy #$13 ; we could save more bytes if we didn't
beq skipper ; bother trying to be exact
dex
skipper:
dey
bpl looper
;==========================
; make sine/cosine tables
;==========================
; floor(s*sin((x-96)*PI*2/256.0)+48.5);
;===================================
; 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:
ldx #64
ldy #64
setup_sine_loop:
lda sines,X
; sta sines,X
sta sines,Y
lda #$60
sec
sbc sines,X
sta sines+128,X
sta sines+128,Y
iny
dex
bpl setup_sine_loop
jsr HGR
jsr HGR2
;=======================
; init variables
@ -82,9 +137,39 @@ bubble:
; HGR leaves A at 0
; lda #0
sta U
sta V
sta T
; sta U
; sta V
; sta T
;=======================
; init variables
;=======================
; wipe all of zero page but $FF
; in theory we only need to clear/copy $00..$C0
; but not sure how to use to our advantage
inx ; X=0
ldy #0 ; Y=0
init_loop:
; sta a:$D0,X ; force 16-bit so doesn't wrap
; because I guess it's bad to wipe zero page?
; maybe it isn't?
sty $D0,X ; clear zero page
lda sines,X ; duplicate sine table for cosine use
sta sines2,X
dex
bne init_loop
;=======================
; init graphics
jsr HGR
jsr HGR2
;=========================
@ -355,155 +440,16 @@ log_lookup:
.include "hgr_clear_codegen.s"
; 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
cosines = $6e00
cosines2= $6f00
;===================================
;
;
; 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
ldy #0
cosine_loop:
lda sines+192,Y
sta cosines,Y
sta cosines2,Y
iny
bne cosine_loop
rts
; .byte $30,$31,$32,$33,$34,$35,$36,$37,$38,$39,$3A,$3B,$3C,$3D,$3E,$3F
; .byte $40,$41,$42
;old_sines_base:
; .byte $42,$43,$44,$45,$46,$47,$48,
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
; 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
.byte $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
fifty_nines:
; .byte $59,$59,$59,$59,$59,$59
; .byte $59
; floor(s*cos((x-96)*PI*2/256.0)+48.5);
cosines:
; sine[64]
.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
; sine[128]
.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
; sine[192]
.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
; sine[0]
.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 $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
.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
.endif

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_TINY"
60 PRINT:PRINT CHR$(4)"BRUN BUBBLE_ROM"

View File

@ -13,6 +13,8 @@
; 534 bytes -- tiny version
; 250 bytes -- strip out fast clear and hplot code and use ROM
; NOTE: see code in bubble_tiny, this can be easily taken down to 200 bytes
; with better sine table generation and optimized page flip
; 76d03 cycles = 486659 cycles = 2fps

View File

@ -1,6 +1,6 @@
; bubble universe -- Apple II Hires
; size optimized version
; even more size optimized version
; by Vince `deater` Weaver
@ -22,6 +22,8 @@
; 1131 bytes -- generate SINE table from 65 entry lookup
; 633 bytes -- generate COSINE table from SINE table
; 534 bytes -- alignment was left on for some reason
; 529 bytes -- back out self modifying U/V code (allows more compact tables)
; 492 bytes -- more compact sine table generator
; soft-switches
@ -53,6 +55,8 @@ J = $D1
T = $D7
U = $D8
V = $D9
IT = $DA
IS = $DB
HGR_PAGE = $E6
@ -61,27 +65,81 @@ INH = $FD
OUTL = $FE
OUTH = $FF
; const
sines = sines_base-$1A ; overlaps some code
sines2 = sines+$100 ; duplicate so we can index cosine into it
cosines = sines+$c0
;NUM = 32
;NUM = 24
bubble:
;========================
;==========================
; setup lookup tables
;==========================
jsr hgr_make_tables
jsr hgr_clear_codegen
jsr setup_sine_table
;=======================
; init graphics
;=========================
; reconstruct sine base
;=========================
; generate the linear $30..$42 part
; and also string of $59 on end
; removes 26 bytes from table
; at expense of 16+4 bytes of code
; (4 from jsr/rts of moving over-writable table code)
ldy #$19 ; offset
ldx #$48 ; want to write $48 downto $30
; with $42 doubled
looper:
txa
sta sines,Y ; sines+12 .... sines
lda #$59 ; also write $59 off the top
sta fifty_nines,Y
cpy #$13 ; we could save more bytes if we didn't
beq skipper ; bother trying to be exact
dex
skipper:
dey
bpl looper
;==========================
; make sine/cosine tables
;==========================
; floor(s*sin((x-96)*PI*2/256.0)+48.5);
;===================================
; 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:
ldx #64
ldy #64
setup_sine_loop:
lda sines,X
; sta sines,X
sta sines,Y
lda #$60
sec
sbc sines,X
sta sines+128,X
sta sines+128,Y
iny
dex
bpl setup_sine_loop
jsr HGR
jsr HGR2
;=======================
; init variables
@ -89,9 +147,39 @@ bubble:
; HGR leaves A at 0
; lda #0
sta U
sta V
sta T
; sta U
; sta V
; sta T
;=======================
; init variables
;=======================
; wipe all of zero page but $FF
; in theory we only need to clear/copy $00..$C0
; but not sure how to use to our advantage
inx ; X=0
ldy #0 ; Y=0
init_loop:
; sta a:$D0,X ; force 16-bit so doesn't wrap
; because I guess it's bad to wipe zero page?
; maybe it isn't?
sty $D0,X ; clear zero page
lda sines,X ; duplicate sine table for cosine use
sta sines2,X
dex
bne init_loop
;=======================
; init graphics
jsr HGR
jsr HGR2
;=========================
@ -105,48 +193,51 @@ next_frame:
; reset I*T
lda T
sta it1_smc+1
sta it2_smc+1
sta IT
; reset I*S
lda #0
sta is1_smc+1
sta is2_smc+1
lda #0 ; Y should be 0 here?
sta IS
num1_smc:
i_smc:
lda #24 ; 40
sta I
i_loop:
num2_smc:
j_smc:
lda #24 ; 200
sta J
j_loop:
ldx U
ldy V
j_loop:
; where S=41 (approximately 1/6.28)
; calc: a=i*s+v;
; calc: b=i+t+u;
; u=sines[a]+sines[b];
; v=cosines[a]+cosines[b];
clc
lda IS
adc V
tay
clc
lda IT
adc U
tax
clc ; 2
; calc: b=i+t+u;
; u=cosines[a]+cosines[b];
is2_smc:
lda cosines,Y ; 4+
it2_smc:
adc cosines,X ; 4+
sta V
; calc: a=i*s+v;
; u=sines[a]+sines[b];
is1_smc:
; max value for both $60 so carry not set
lda sines,Y ; 4+
it1_smc:
adc sines,X ; 4+
sta U ; 3
@ -204,17 +295,13 @@ it1_smc:
bne j_loop
done_j:
lda is1_smc+1
clc
lda IS
adc #41 ; 1/6.28 = 0.16 = 0 0 1 0 1 0 0 0 = 0x28
sta is1_smc+1
sta is2_smc+1
sta IS
dec I
bne i_loop
done_i:
; sty V
inc T
end:
@ -227,22 +314,22 @@ end:
cmp #'A'
bne check_z
inc num1_smc+1
inc i_smc+1
jmp done_keys
check_z:
cmp #'Z'
bne check_j
dec num1_smc+1
dec i_smc+1
jmp done_keys
check_j:
cmp #'J'
bne check_m
inc num2_smc+1
inc j_smc+1
jmp done_keys
check_m:
cmp #'M'
bne done_keys
dec num2_smc+1
dec j_smc+1
done_keys:
@ -363,155 +450,16 @@ log_lookup:
.include "hgr_clear_codegen.s"
; 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
cosines = $6e00
cosines2= $6f00
;===================================
;
;
; 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
ldy #0
cosine_loop:
lda sines+192,Y
sta cosines,Y
sta cosines2,Y
iny
bne cosine_loop
rts
; .byte $30,$31,$32,$33,$34,$35,$36,$37,$38,$39,$3A,$3B,$3C,$3D,$3E,$3F
; .byte $40,$41,$42
;old_sines_base:
; .byte $42,$43,$44,$45,$46,$47,$48,
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
; 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
.byte $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
fifty_nines:
; .byte $59,$59,$59,$59,$59,$59
; .byte $59
; floor(s*cos((x-96)*PI*2/256.0)+48.5);
cosines:
; sine[64]
.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
; sine[128]
.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
; sine[192]
.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
; sine[0]
.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 $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
.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
.endif