(bunnymark) Optimizations

This commit is contained in:
Devine Lu Linvega 2024-01-15 08:23:10 -08:00
parent 6fbf4ff535
commit 950a5085ee
1 changed files with 118 additions and 190 deletions

View File

@ -1,239 +1,173 @@
( bunnymark.tal )
( November 2021, Kira Oakley )
( March 2022, Devine Lu Linvega )
( November 2021, Kira Oakley )
( March 2022, Devine Lu Linvega )
|00 @System &vector $2 &pad $6 &r $2 &g $2 &b $2 &debug $1 &halt $1
|20 @Screen &vector $2 &width $2 &height $2 &auto $1 &pad $1 &x $2 &y $2 &addr $2 &pixel $1 &sprite $1
|90 @Mouse &vector $2 &x $2 &y $2 &state $1 &wheel $1
|c0 @Date &year $2 &month $1 &day $1 &hour $1 &minute $1 &second $1 &dotw $1 &doty $2 &isdst $1
|c0 @DateTime &year $2 &month $1 &day $1 &hour $1 &minute $1 &second $1 &dotw $1 &doty $2 &isdst $1
|0000
@frames $2
@last $1
|0100 ( -> )
|0100
( set system colors )
@on-reset ( -> )
( | theme )
#2ce9 .System/r DEO2
#01c0 .System/g DEO2
#2ce5 .System/b DEO2
( interrupts )
( | interrupts )
;on-frame .Screen/vector DEO2
( draw "FPS:" and "BUNNIES:" and instructions labels )
.Screen/width DEI2 #0046 SUB2 #0008 ;text/fps #42 draw-str
#0004 #0008 ;text/bunnies #42 draw-str
.Screen/width DEI2 #01 SFT2 #0050 SUB2 #0008 ;text/instructions #43 draw-str
#0028 #0008 #0000 draw-dec
( seed prng )
#04 ;rand/a STA
BRK
( | draw "FPS:" and "BUNNIES:" and instructions labels )
.Screen/width DEI2 #0046 SUB2 #0008 ;text/fps #42 <draw-str>
#0004 #0008 ;text/bunnies #42 <draw-str>
.Screen/width DEI2 #01 SFT2 #0050 SUB2 #0008 ;text/instructions #43 <draw-str>
#0028 #0008 #0000 <draw-dec>
( seed prng ) prng-init BRK
@on-frame ( -> )
( frames++ )
.frames LDZ2k INC2 ROT STZ2
.Date/second DEI .last LDZ EQU ?&post-fps-update
( frames++ ) .frames LDZ2k INC2 ROT STZ2
.DateTime/second DEI .last LDZ EQU ?&post-fps-update
( fps update )
( update last-secs ) .Date/second DEI .last STZ
( update fps label ) .Screen/width DEI2 #002b SUB2 #0008 .frames LDZ2 draw-dec
( update last-secs ) .DateTime/second DEI .last STZ
( update fps label ) .Screen/width DEI2 #002b SUB2 #0008 .frames LDZ2 <draw-dec>
( reset frames counter ) #0000 .frames STZ2
&post-fps-update
( mouse input to add/remove bunnies )
&post-fps-update ( mouse input to add/remove bunnies )
.Mouse/state DEI #01 EQU ?&add
.Mouse/state DEI #01 GTH ?&remove
!&done
&add
add-bunny !&done
&remove
remove-bunny !&done
&done
( loop from 0 to ;sprite/length to make all ;draw-bunny calls )
[ ;sprite/length LDA2 ] #0000
&loop
&add add-bunny !&done
&remove remove-bunny !&done
&done ( loop from 0 to ;sprite/length to make all ;draw-bunny calls )
;sprite/length LDA2 #0000
&loop ( -- )
EQU2k ?&bail
DUP2 draw-bunny
INC2 !&loop
&bail
POP2 POP2
BRK
DUP2 draw-bunny INC2 !&loop
&bail POP2 POP2 BRK
@draw-bunny ( idx -- )
( compute the offset to the beginning of this bunny's data )
[ #30 SFT2 ;sprite/array ADD2 ]
( compute the offset to the beginning of this bunny's data ) #30 SFT2 ;sprite/array ADD2
( clear the old sprite location )
( top )
[ LDA2k ] #05 SFT2 .Screen/x DEO2
[ INC2k INC2 LDA2 ] #05 SFT2 .Screen/y DEO2
clear-sprite
( move the sprite by its velocity )
[ LDA2k ] [ OVR2 #0004 ADD2 LDA2 ] ADD2 OVR2 STA2
[ INC2k INC2 LDA2 ] [ OVR2 #0006 ADD2 LDA2 ] ADD2 OVR2 INC2 INC2 STA2
( check for right wall collision + bounce x )
[ DUP2 #0004 ADD2 LDA2 ] #0f SFT2 #0001 EQU2 ?&skip-max-x
[ LDA2k ] #05 SFT2 #0008 ADD2 [ .Screen/width DEI2 ] LTH2 ?&skip-max-x
[ DUP2 #0004 ADD2 LDA2 ] #ffff MUL2 [ OVR2 #0004 ADD2 STA2 ]
&skip-max-x
( check for left wall collision + bounce x )
[ LDA2k ] #0f SFT2 #0000 EQU2 ?&skip-min-x
[ DUP2 #0004 ADD2 LDA2 ] #ffff MUL2 [ OVR2 #0004 ADD2 STA2 ]
&skip-min-x
( check for bottom wall collision + bounce y )
[ DUP2 #0006 ADD2 LDA2 ] #0f SFT2 #0001 EQU2 ?&skip-max-y
[ INC2k INC2 LDA2 ] #05 SFT2 #0008 ADD2 [ .Screen/height DEI2 ] LTH2 ?&skip-max-y
[ DUP2 #0006 ADD2 LDA2 ] #ffff MUL2 [ OVR2 #0006 ADD2 STA2 ]
( top ) LDA2k #05 SFT2 .Screen/x DEO2
INC2k INC2 LDA2 #05 SFT2 .Screen/y DEO2
<clear-sprite>
( move the sprite by its velocity ) LDA2k OVR2 #0004 ADD2 LDA2 ADD2 OVR2 STA2
INC2k INC2 LDA2 OVR2 #0006 ADD2 LDA2 ADD2 OVR2 INC2 INC2 STA2
( check for right wall collision + bounce x ) DUP2 #0004 ADD2 LDA2 #0f SFT2 #0001 EQU2 ?&skip-max-x
LDA2k #05 SFT2 #0008 ADD2 .Screen/width DEI2 LTH2 ?&skip-max-x
DUP2 #0004 ADD2 LDA2 #ffff MUL2 OVR2 #0004 ADD2 STA2
&skip-max-x ( check for left wall collision + bounce x )
LDA2k #0f SFT2 #0000 EQU2 ?&skip-min-x
DUP2 #0004 ADD2 LDA2 #ffff MUL2 OVR2 #0004 ADD2 STA2
&skip-min-x ( check for bottom wall collision + bounce y )
DUP2 #0006 ADD2 LDA2 #0f SFT2 #0001 EQU2 ?&skip-max-y
INC2k INC2 LDA2 #05 SFT2 #0008 ADD2 .Screen/height DEI2 LTH2 ?&skip-max-y
DUP2 #0006 ADD2 LDA2 #ffff MUL2 OVR2 #0006 ADD2 STA2
!&skip-gravity
&skip-max-y
( check for top wall collision + bounce x )
[ INC2k INC2 LDA2 ] #0f SFT2 #0000 EQU2 ?&skip-min-y
[ DUP2 #0006 ADD2 LDA2 ] #ffff MUL2 [ OVR2 #0006 ADD2 STA2 ]
&skip-max-y ( check for top wall collision + bounce x )
INC2k INC2 LDA2 #0f SFT2 #0000 EQU2 ?&skip-min-y
DUP2 #0006 ADD2 LDA2 #ffff MUL2 OVR2 #0006 ADD2 STA2
!&skip-gravity
&skip-min-y
( apply gravity )
[ DUP2 #0006 ADD2 LDA2 ] #0004 ADD2 OVR2 #0006 ADD2 STA2
&skip-gravity
( draw the sprite )
( top )
[ LDA2k ] #05 SFT2 .Screen/x DEO2
[ INC2 INC2 LDA2 ] #05 SFT2 .Screen/y DEO2
( draw )
[ LIT2 15 -Screen/auto ] DEO
&skip-min-y ( apply gravity )
DUP2 #0006 ADD2 LDA2 #0004 ADD2 OVR2 #0006 ADD2 STA2
&skip-gravity ( draw the sprite )
( top ) LDA2k #05 SFT2 .Screen/x DEO2
INC2 INC2 LDA2 #05 SFT2 .Screen/y DEO2
( draw ) [ LIT2 15 -Screen/auto ] DEO
;bunny-chr .Screen/addr DEO2
#85 .Screen/sprite DEO
[ LIT2 85 -Screen/sprite ] DEO
[ LIT2 00 -Screen/auto ] DEO
JMP2r
JMP2r
@clear-sprite ( -- )
[ LIT2 11 -Screen/auto ] DEO
@<clear-sprite> ( -- )
;fill-icn .Screen/addr DEO2
#00 .Screen/sprite DEO
[ LIT2 11 -Screen/auto ] DEO
[ LIT2 00 -Screen/sprite ] DEO
[ LIT2 00 -Screen/auto ] DEO
JMP2r
JMP2r
@add-bunny ( -- )
;sprite/length LDA2
( cap bunny count at 65535 )
DUP2 #ffff EQU2 ?&bail
( compute the offset to the beginning of this new bunny's data )
DUP2 #30 SFT2 ;sprite/array ADD2
( populate the new bunny's x/y/xvel/yvel with random values )
#00 [ rand ] OVR2 STA2
( cap bunny count at 65535 ) DUP2 #ffff EQU2 ?&bail
( compute the offset to the beginning of this new bunny's data ) DUP2 #30 SFT2 ;sprite/array ADD2
( populate the new bunny's x/y/xvel/yvel with random values ) #00 [ rand ] OVR2 STA2
[ rand #1f AND ] [ rand ] OVR2 INC2 INC2 STA2
#00 [ rand #7f AND ] OVR2 #0004 ADD2 STA2
#00 [ rand #7f AND ] OVR2 #0006 ADD2 STA2
( pop ptr to bunny data ) POP2
( write new increased array length )
INC2 DUP2 ;sprite/length STA2
( update label )
STH2k #0028 #0008 STH2r draw-dec
&bail
( pop sprite/length ) POP2
JMP2r
@draw-str ( x* y* text* color -- )
( write new increased array length ) INC2 DUP2 ;sprite/length STA2
( update label ) STH2k #0028 #0008 STH2r <draw-dec>
&bail ( pop sprite/length )
POP2 JMP2r
@<draw-str> ( x* y* text* color -- )
[ LIT2 01 -Screen/auto ] DEO
STH
SWP2 .Screen/y DEO2
SWP2 .Screen/x DEO2
&loop
&loop ( -- )
LDAk #20 SUB #00 SWP #30 SFT2 ;font ADD2 .Screen/addr DEO2
STHkr .Screen/sprite DEO
INC2 LDAk ?&loop
POP2
STHr POP
[ LIT2 00 -Screen/auto ] DEO
JMP2r
@draw-dec ( x* y* num* -- )
POP2 STHr POP [ LIT2 00 -Screen/auto ] DEO
JMP2r
@<draw-dec> ( x* y* num* -- )
[ LIT2 01 -Screen/auto ] DEO
SWP2 .Screen/y DEO2
SWP2 .Screen/x DEO2
#2710 DIV2k DUP2 NIP ,&digit JSR MUL2 SUB2
#03e8 DIV2k DUP2 NIP ,&digit JSR MUL2 SUB2
#0064 DIV2k DUP2 NIP ,&digit JSR MUL2 SUB2 NIP
#0a DIVk DUP ,&digit JSR MUL SUB
,&digit JSR
#2710 DIV2k DUP2 NIP <draw-digit>
MUL2 SUB2 #03e8 DIV2k DUP2 NIP <draw-digit>
MUL2 SUB2 #0064 DIV2k DUP2 NIP <draw-digit>
MUL2 SUB2 NIP #0a DIVk DUP <draw-digit>
MUL SUB <draw-digit>
[ LIT2 00 -Screen/auto ] DEO
JMP2r
JMP2r
&digit ( num -- )
@<draw-digit> ( num -- )
#30 SFT #00 SWP ;font/num ADD2 .Screen/addr DEO2
#41 .Screen/sprite DEO
JMP2r
@remove-bunny ( -- )
;sprite/length LDA2
( don't let length go below 0 )
ORAk #00 EQU ?&bail
( clear the old sprite location )
DUP2 #0001 SUB2 #30 SFT2 ;sprite/array ADD2
( top )
[ LDA2k ] #05 SFT2 .Screen/x DEO2
[ INC2k INC2 LDA2 ] #05 SFT2 .Screen/y DEO2
( clear )
clear-sprite
POP2
#0001 SUB2 DUP2 ;sprite/length STA2
( update label )
STH2k #0028 #0008 STH2r draw-dec
&bail
POP2
( don't let length go below 0 ) ORAk #00 EQU ?&bail
( clear the old sprite location ) DUP2 #0001 SUB2 #30 SFT2 ;sprite/array ADD2
( top ) LDA2k #05 SFT2 .Screen/x DEO2
INC2k INC2 LDA2 #05 SFT2 .Screen/y DEO2
( clear ) <clear-sprite>
POP2 #0001 SUB2 DUP2 ;sprite/length STA2
( update label ) STH2k #0028 #0008 STH2r <draw-dec>
&bail POP2 JMP2r
JMP2r
(
@|random )
( PRNG code taken from flappy.tal -- thanks! )
@prng-init ( -- )
[ LIT2 00 -DateTime/second ] DEI [ LIT2 00 -DateTime/minute ] DEI #60 SFT2 EOR2 [ LIT2 00 -DateTime/hour ] DEI #c0 SFT2 EOR2 ,prng/x STR2
[ LIT2 00 -DateTime/hour ] DEI #04 SFT2 [ LIT2 00 -DateTime/day ] DEI #10 SFT2 EOR2 [ LIT2 00 -DateTime/month ] DEI #60 SFT2 EOR2 .DateTime/year DEI2 #a0 SFT2 EOR2 ,prng/y STR2
JMP2r
@prng ( -- number* )
[ LIT2 &x $2 ] DUP2 #50 SFT2 EOR2 DUP2 #03 SFT2 EOR2 [ LIT2 &y $2 ] DUP2 ,&x STR2
DUP2 #01 SFT2 EOR2 EOR2 ,&y STR2k POP JMP2r
@rand ( -- number )
prng ADD JMP2r
( static string data )
( local vars )
#04 JMP
&x $1 &y $1 &z $1 &a $1
( 8-bit PRNG https://github.com/edrosten/8bit_rng )
( t = x ^ (x << 4) )
,&x LDR DUP #40 SFT EOR
( x = y )
,&y LDR ,&x STR
( y = z )
,&z LDR ,&y STR
( z = a )
,&a LDR DUP ,&z STR
( a = z ^ t ^ (z >> 1) ^ (t << 1) )
DUPk ADD EOR SWP DUP #01 SFT EOR EOR
DUP ,&a STR
(
@|assets )
JMP2r
( static string data )
@text
&fps "FPS: 00
&bunnies "BUNS: 00
&instructions "CLICK 20 "TO 20 "ADD 20 "BUNNIES! 00
@text &fps "FPS: $1
&bunnies "BUNS: $1
&instructions "CLICK 20 "TO 20 "ADD 20 "BUNNIES! $1
@font ( atari8.uf1 )
[
0000 0000 0000 0000 6060 6060 6000 6000
6666 6600 0000 0000 006c fe6c 6cfe 6c00
183e 603c 067c 1800 0066 6c18 3066 4600
@ -241,8 +175,7 @@ JMP2r
0e1c 1818 181c 0e00 7038 1818 1838 7000
0066 3cff 3c66 0000 0018 187e 1818 0000
0000 0000 0030 3060 0000 007e 0000 0000
0000 0000 0018 1800 0206 0c18 3060 4000
&num
0000 0000 0018 1800 0206 0c18 3060 4000 ] &num [
3c66 6e76 6666 3c00 1838 1818 1818 7e00
3c66 060c 1830 7e00 7e0c 180c 0666 3c00
0c1c 3c6c 7e0c 0c00 7e60 7c06 0666 3c00
@ -264,20 +197,15 @@ JMP2r
7e18 1818 1818 1800 6666 6666 6666 3e00
6666 6666 663c 1800 c6c6 c6d6 feee c600
6666 3c18 3c66 6600 6666 663c 1818 1800
7e06 0c18 3060 7e00 7860 6060 6060 7800
7e06 0c18 3060 7e00 7860 6060 6060 7800 ]
@fill-icn
ffff ffff ffff ffff
@bunny-chr
@fill-icn [ ffff ffff ffff ffff ]
@bunny-chr [
2466 6600 2424 003c 4200 007e 7e7e 7e7e
1818 3c3c 1800 0000 ff66 4242 667e 4242
1818 3c3c 1800 0000 ff66 4242 667e 4242 ]
( beginning of sprite pixel data + array )
( beginning of sprite pixel data + array )
@sprite &length $2
&array &x 0600 &y 0500 &xvel 0060 &yvel 0010
@sprite
&length $2
&array
&x 0600
&y 0500
&xvel 0060
&yvel 0010