megademo: add in mode7 code

this might be ill-advised
This commit is contained in:
Vince Weaver 2018-10-02 23:04:51 -04:00
parent da861f9113
commit d208ea0cd3
16 changed files with 2264 additions and 6 deletions

View File

@ -30,6 +30,7 @@ megademo.o: megademo.s \
starring_people.s fs.inc FS_HGRC.BIN.lz4 deater.inc DEATER_HGRC.BIN.lz4\
check_email.s email_40_96.inc \
bird_mountain.s \
mode7.s \
fireworks.s fw_background.inc fw_state_machine.s fw.s \
hgr.s random16.s move_letters.s
ca65 -o megademo.o megademo.s -l megademo.lst

79
megademo/bg_scroll.s Normal file
View File

@ -0,0 +1,79 @@
;==================
; scroll background
;==================
; background already loaded
; ANGLE 0-15 sets angle
; CV is Y position to display at
; 182/220... 220/16 = 13.75
; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
; 0 11 22 34 45 56 68 79 91 102 113 125 136 147 159 170 182
scroll_offsets:
.byte 0,11,22,34,45,56,68,79,91,102,113,125,136,147,159,170
scroll_background:
ldy ANGLE ; use angle
lda scroll_offsets,Y ; lookup in table
sta OFFSET ; calculate offset
ldx #0
ldy CV ; lookup Y co-ord
lsr ; and set up self-modify code
lda gr_offsets,Y ; get position
sta bgsm1+1
lda gr_offsets+2,Y ; get position
sta bgsm2+1
lda gr_offsets+4,Y ; get position
sta bgsm3+1
lda gr_offsets+6,Y ; get position
sta bgsm4+1
iny
clc
lda gr_offsets,Y ; get position
adc DRAW_PAGE
sta bgsm1+2
lda gr_offsets+2,Y ; get position
adc DRAW_PAGE
sta bgsm2+2
lda gr_offsets+4,Y ; get position
adc DRAW_PAGE
sta bgsm3+2
lda gr_offsets+6,Y ; get position
adc DRAW_PAGE
sta bgsm4+2
ldy OFFSET
bgdraw_loop:
lda scroll_row1,Y
bgsm1:
sta $400,X
lda scroll_row2,Y
bgsm2:
sta $480,X
lda scroll_row3,Y
bgsm3:
sta $500,X
lda scroll_row4,Y
bgsm4:
sta $580,X
iny
inx
cpx #40
bne bgdraw_loop
rts

View File

@ -25,7 +25,7 @@ MARGIN = 24
; Zero page addresses
;STATE = $ED
OFFSET = $EF
;OFFSET = $EF
COLOR_GROUP = $F0
X_VELOCITY = $F1
Y_VELOCITY_H = $F2

192
megademo/gr_fast_clear.s Normal file
View File

@ -0,0 +1,192 @@
clear_screens:
;===================================
; Clear top/bottom of page 0
;===================================
lda #$0
sta DRAW_PAGE
jsr clear_top
jsr clear_bottom
;===================================
; Clear top/bottom of page 1
;===================================
lda #$4
sta DRAW_PAGE
jsr clear_top
jsr clear_bottom
rts
clear_bottoms:
;===================================
; Clear bottom of page 0
;===================================
lda #$0
sta DRAW_PAGE
jsr clear_bottom
;===================================
; Clear bottom of page 1
;===================================
lda #$4
sta DRAW_PAGE
jsr clear_bottom
rts
;=========================================================
; clear_top
;=========================================================
; clear DRAW_PAGE
; original = 14,558 cycles(?) 15ms, 70Hz
; OPTIMIZED MAX (page0,48rows): 45*120+4+6 = 5410 = 5.4ms 185Hz
; (pageX,40rows): 50*120+4+6 = 6010 = 6.0ms 166Hz
; 50*120+4+6+37 = 6055 = 6.0ms 166Hz
clear_top:
lda #0 ; 2
clear_top_a:
sta COLOR ; 3
clc ; 2
lda DRAW_PAGE ; 3
adc #4 ; 2
sta __ctf+2 ; 3
sta __ctf+5 ; 3
adc #1 ; 2
sta __ctf+8 ; 3
sta __ctf+11 ; 3
adc #1 ; 2
sta __ctf2+2 ; 3
sta __ctf2+5 ; 3
adc #1 ; 2
sta __ctf2+8 ; 3
sta __ctf2+11 ; 3
ldy #120 ; 2
lda COLOR ; 3
clear_top_fast_loop:
__ctf:
sta $400,Y ; 5
sta $480,Y ; 5
sta $500,Y ; 5
sta $580,Y ; 5
cpy #80 ; 2
bpl no_draw_bottom ; 2nt/3
__ctf2:
sta $600,Y ; 5
sta $680,Y ; 5
sta $700,Y ; 5
sta $780,Y ; 5
no_draw_bottom:
dey ; 2
bpl clear_top_fast_loop ; 2nt/3
rts ; 6
;=========================================================
; clear_bottom
;=========================================================
; clear bottom of draw page
clear_bottom:
clc ; 2
lda DRAW_PAGE ; 3
adc #6 ; 2
sta __cbf2+2 ; 3
sta __cbf2+5 ; 3
adc #1 ; 2
sta __cbf2+8 ; 3
sta __cbf2+11 ; 3
ldy #120 ; 2
lda #$a0 ; Normal Space ; 2
clear_bottom_fast_loop:
__cbf2:
sta $600,Y ; 5
sta $680,Y ; 5
sta $700,Y ; 5
sta $780,Y ; 5
dey ; 2
cpy #80 ; 2
bpl clear_bottom_fast_loop ; 2nt/3
rts ; 6
clear_screens_notext:
;===================================
; Clear top/bottom of page 0
;===================================
lda #$0
sta DRAW_PAGE
jsr clear_all
;===================================
; Clear top/bottom of page 1
;===================================
lda #$4
sta DRAW_PAGE
jsr clear_all
rts
;=========================================================
; clear_all
;=========================================================
; clear 48 rows
clear_all:
clc ; 2
lda DRAW_PAGE ; 3
adc #4 ; 2
sta __caf+2 ; 3
sta __caf+5 ; 3
adc #1 ; 2
sta __caf+8 ; 3
sta __caf+11 ; 3
adc #1 ; 2
sta __caf2+2 ; 3
sta __caf2+5 ; 3
adc #1 ; 2
sta __caf2+8 ; 3
sta __caf2+11 ; 3
ldy #120 ; 2
clear_all_color:
lda #0 ; 2
clear_all_fast_loop:
__caf:
sta $400,Y ; 5
sta $480,Y ; 5
sta $500,Y ; 5
sta $580,Y ; 5
__caf2:
sta $600,Y ; 5
sta $680,Y ; 5
sta $700,Y ; 5
sta $780,Y ; 5
dey ; 2
bpl clear_all_fast_loop ; 2nt/3
rts ; 6

67
megademo/gr_hlin_double.s Normal file
View File

@ -0,0 +1,67 @@
;=====================================================================
;= ROUTINES
;=====================================================================
;================================
; hlin_setup
;================================
; put address in GBASL/GBASH
; Ycoord in A, Xcoord in Y
hlin_setup:
sty TEMPY ; 3
tay ; y=A ; 2
lda gr_offsets,Y ; lookup low-res memory address ; 4
clc ; 2
adc TEMPY ; 3
sta GBASL ; 3
iny ; 2
lda gr_offsets,Y ; 4
adc DRAW_PAGE ; add in draw page offset ; 3
sta GBASH ; 3
rts ; 6
;===========
; 35
;================================
; hlin_double:
;================================
; HLIN Y, V2 AT A
; Y, X, A trashed
; start at Y, draw up to and including X
hlin_double:
;int hlin_double(int page, int x1, int x2, int at) {
jsr hlin_setup ; 41
sec ; 2
lda V2 ; 3
sbc TEMPY ; 3
tax ; 2
inx ; 2
;===========
; 53
; fallthrough
;=================================
; hlin_double_continue: width
;=================================
; GBASL has correct offset for row/col
; width in X
hlin_double_continue:
ldy #0 ; 2
lda COLOR ; 3
hlin_double_loop:
sta (GBASL),Y ; 6
inc GBASL ; 5
dex ; 2
bne hlin_double_loop ; 2nt/3
rts ; 6
;=============
; 53+5+X*16+5

24
megademo/gr_pageflip.s Normal file
View File

@ -0,0 +1,24 @@
;==========
; page_flip
;==========
page_flip:
lda DISP_PAGE ; 3
beq page_flip_show_1 ; 2nt/3
page_flip_show_0:
bit PAGE0 ; 4
lda #4 ; 2
sta DRAW_PAGE ; DRAW_PAGE=1 ; 3
lda #0 ; 2
sta DISP_PAGE ; DISP_PAGE=0 ; 3
rts ; 6
page_flip_show_1:
bit PAGE1 ; 4
sta DRAW_PAGE ; DRAW_PAGE=0 ; 3
lda #1 ; 2
sta DISP_PAGE ; DISP_PAGE=1 ; 3
rts ; 6
;====================
; DISP_PAGE=0 26
; DISP_PAGE=1 24

11
megademo/gr_setpage.s Normal file
View File

@ -0,0 +1,11 @@
;==========================================================
; set_gr_page0
;==========================================================
;
set_gr_page0:
bit PAGE0 ; set page 0
bit LORES ; Lo-res graphics
bit TEXTGR ; mixed gr/text mode
bit SET_GR ; set graphics
rts

63
megademo/island_lookup.s Normal file
View File

@ -0,0 +1,63 @@
;====================================
; do a full lookup, takes much longer
; used to be a separate function but we inlined it here
;====================
; lookup_map
;====================
; finds value in space_x.i,space_y.i
; returns color in A
; CLOBBERS: A,Y
lda SPACEX_I ; 3
sta spacex_label+1 ; self modifying code, LAST_SPACEX_I ; 4
and #CONST_MAP_MASK_X ; wrap at 64 ; 2
sta SPACEX_I ; store i patch out ; 3
tay ; copy to Y for later ; 2
lda SPACEY_I ; 3
sta spacey_label+1 ; self modifying code, LAST_SPACEY_I ; 4
and #CONST_MAP_MASK_Y ; wrap to 64x64 grid ; 2
sta SPACEY_I ; 3
asl ; 2
asl ; 2
asl ; multiply by 8 ; 2
clc ; 2
adc SPACEX_I ; add in X value ; 3
; only valid if x<8 and y<8
; SPACEX_I is also in y
cpy #$8 ; 2
;============
; 39
bcs @ocean_color ; bgt 8 ; 2nt/3
ldy SPACEY_I ; 3
cpy #$8 ; 2
;=============
; 7
bcs @ocean_color ; bgt 8 ; 2nt/3
tay ; 2
lda flying_map,Y ; load from array ; 4
bcc @update_cache ; 3
;============
; 11
@ocean_color:
and #$1f ; 2
tay ; 2
lda water_map,Y ; the color of the sea ; 4
;===========
; 8
@update_cache:
sta map_color_label+1 ; self-modifying ; 4
;===========
; 4
; rts ; 6

16
megademo/island_map.inc Normal file
View File

@ -0,0 +1,16 @@
flying_map:
.byte $22,$ff,$ff,$ff, $ff,$ff,$ff,$22
.byte $dd,$cc,$cc,$88, $44,$44,$00,$dd
.byte $dd,$cc,$cc,$cc, $88,$44,$44,$dd
.byte $dd,$cc,$cc,$88, $44,$44,$44,$dd
.byte $dd,$cc,$99,$99, $88,$44,$44,$dd
.byte $dd,$cc,$99,$88, $44,$44,$44,$dd
.byte $dd,$cc,$99,$99, $11,$44,$44,$dd
.byte $22,$dd,$dd,$dd, $dd,$dd,$dd,$22
water_map:
.byte $22,$22,$22,$22, $22,$22,$22,$22
.byte $ee,$22,$22,$22, $22,$22,$22,$22
.byte $22,$22,$22,$22, $22,$22,$22,$22
.byte $22,$22,$22,$22, $ee,$22,$22,$22

View File

@ -25,6 +25,8 @@ apple_iie:
;===================
jsr HOME
jsr mode7_flying
; C64 Opening Sequence
jsr c64_opener
@ -54,6 +56,7 @@ apple_iie:
; Enter ship
; mode7 (???)
jsr mode7_flying
; Fly in space
@ -61,7 +64,7 @@ apple_iie:
; Fireworks
jsr fireworks
; jsr fireworks
; Game over
game_over_man:
@ -88,9 +91,10 @@ loop_forever:
.include "wait_keypress.s"
.include "random16.s"
.align $100
.include "fireworks.s"
; .include "fireworks.s"
.include "hgr.s"
.include "bird_mountain.s"
.include "move_letters.s"
.align $100
.include "gr_putsprite.s"
.include "mode7.s"

1130
megademo/mode7.s Normal file

File diff suppressed because it is too large Load Diff

214
megademo/mode7_sprites.inc Normal file
View File

@ -0,0 +1,214 @@
;================
; Ship Sprites
;================
splash_forward:
.byte $7,$2
.byte $00,$ee,$00,$00,$00,$ee,$00
.byte $ee,$00,$00,$00,$00,$00,$ee
splash_right:
.byte $7,$2
.byte $00,$00,$00,$00,$00,$ee,$00
.byte $00,$00,$00,$00,$00,$00,$ee
splash_left:
.byte $7,$2
.byte $00,$ee,$00,$00,$00,$00,$00
.byte $ee,$00,$00,$00,$00,$00,$00
shadow_forward:
.byte $3,$2
.byte $00,$aa,$00
.byte $a0,$aa,$a0
shadow_right:
.byte $3,$2
.byte $a0,$00,$aa
.byte $00,$0a,$a0
shadow_left:
.byte $3,$2
.byte $aa,$00,$a0
.byte $a0,$0a,$00
ship_forward:
.byte $9,$5
.byte $00,$00,$00,$00,$ff,$00,$00,$00,$00
.byte $00,$00,$00,$66,$ff,$66,$00,$00,$00
.byte $00,$00,$70,$2f,$12,$2f,$70,$00,$00
.byte $f0,$f7,$f7,$f2,$d9,$f2,$f7,$f7,$f0
.byte $00,$00,$00,$00,$0d,$00,$00,$00,$00
ship_right:
.byte $9,$5
.byte $00,$00,$00,$00,$00,$60,$60,$f0,$00
.byte $00,$f0,$70,$70,$f6,$f6,$6f,$66,$00
.byte $00,$07,$ff,$2f,$12,$27,$f6,$00,$00
.byte $00,$00,$00,$dd,$d9,$f2,$77,$00,$00
.byte $00,$00,$00,$00,$00,$0f,$ff,$70,$00
ship_left:
.byte $9,$5
.byte $00,$f0,$60,$60,$00,$00,$00,$00,$00
.byte $00,$66,$6f,$f6,$f6,$70,$70,$f0,$00
.byte $00,$00,$f6,$27,$12,$2f,$ff,$07,$00
.byte $00,$00,$77,$f2,$d9,$dd,$00,$00,$00
.byte $00,$70,$ff,$0f,$00,$00,$00,$00,$00
ship_small:
.byte $5,$3
.byte $00,$00,$ff,$00,$00
.byte $00,$76,$2f,$76,$00
.byte $0f,$0f,$d9,$0f,$0f
ship_tiny:
.byte $3,$1
.byte $f0,$9f,$f0
sphere_offset:
.byte 0,27,54,81, 108,135,162,189
.if 0
sphere0:
.byte $5,$5
.byte $00,$b0,$b3,$b0,$00
.byte $b0,$bb,$bb,$bb,$30
.byte $bb,$bb,$bb,$bb,$33
.byte $bb,$bb,$bb,$bb,$33
.byte $00,$3b,$3b,$3b,$00
sphere1:
.byte $5,$5
.byte $00,$30,$bb,$b0,$00
.byte $30,$bb,$bb,$bb,$b0
.byte $bb,$bb,$bb,$bb,$bb
.byte $bb,$bb,$bb,$bb,$bb
.byte $00,$bb,$bb,$bb,$00
sphere2:
.byte $5,$5
.byte $00,$30,$b3,$b0,$00
.byte $30,$b3,$bb,$bb,$b0
.byte $33,$bb,$bb,$bb,$bb
.byte $33,$bb,$bb,$bb,$bb
.byte $00,$33,$3b,$3b,$00
sphere3:
.byte $5,$5
.byte $00,$30,$bb,$b0,$00
.byte $30,$bb,$bb,$bb,$b0
.byte $33,$3b,$bb,$bb,$bb
.byte $33,$33,$3b,$bb,$bb
.byte $00,$33,$33,$3b,$00
sphere4:
.byte $5,$5
.byte $00,$b0,$bb,$b0,$00
.byte $30,$bb,$bb,$bb,$b0
.byte $33,$33,$3b,$bb,$bb
.byte $33,$33,$33,$33,$3b
.byte $00,$33,$33,$33,$00
sphere5:
.byte $5,$5
.byte $00,$b0,$bb,$b0,$00
.byte $30,$bb,$bb,$bb,$30
.byte $33,$3b,$3b,$3b,$33
.byte $33,$33,$33,$33,$33
.byte $00,$33,$33,$33,$00
sphere6:
.byte $5,$5
.byte $00,$30,$bb,$b0,$00
.byte $30,$bb,$bb,$bb,$30
.byte $bb,$bb,$bb,$3b,$33
.byte $33,$3b,$33,$33,$33
.byte $00,$33,$33,$33,$00
sphere7:
.byte $5,$5
.byte $00,$30,$bb,$b0,$00
.byte $b0,$bb,$bb,$bb,$30
.byte $bb,$bb,$bb,$bb,$33
.byte $bb,$bb,$bb,$3b,$33
.byte $00,$33,$33,$33,$00
.endif
sphere0:
.byte $5,$5
.byte $00,$30,$bb,$b0,$00
.byte $b0,$bb,$bb,$bb,$30
.byte $bb,$bb,$bb,$bb,$33
.byte $bb,$bb,$bb,$3b,$33
.byte $00,$33,$33,$33,$00
sphere1:
.byte $5,$5
.byte $00,$30,$bb,$b0,$00
.byte $30,$bb,$bb,$bb,$30
.byte $bb,$bb,$bb,$3b,$33
.byte $33,$3b,$33,$33,$33
.byte $00,$33,$33,$33,$00
sphere2:
.byte $5,$5
.byte $00,$b0,$bb,$b0,$00
.byte $30,$bb,$bb,$bb,$30
.byte $33,$3b,$3b,$3b,$33
.byte $33,$33,$33,$33,$33
.byte $00,$33,$33,$33,$00
sphere3:
.byte $5,$5
.byte $00,$b0,$bb,$b0,$00
.byte $30,$bb,$bb,$bb,$b0
.byte $33,$33,$3b,$bb,$bb
.byte $33,$33,$33,$33,$3b
.byte $00,$33,$33,$33,$00
sphere4:
.byte $5,$5
.byte $00,$30,$bb,$b0,$00
.byte $30,$bb,$bb,$bb,$b0
.byte $33,$3b,$bb,$bb,$bb
.byte $33,$33,$3b,$bb,$bb
.byte $00,$33,$33,$3b,$00
sphere5:
.byte $5,$5
.byte $00,$30,$b3,$b0,$00
.byte $30,$b3,$bb,$bb,$b0
.byte $33,$bb,$bb,$bb,$bb
.byte $33,$bb,$bb,$bb,$bb
.byte $00,$33,$3b,$3b,$00
sphere6:
.byte $5,$5
.byte $00,$30,$bb,$b0,$00
.byte $30,$bb,$bb,$bb,$b0
.byte $bb,$bb,$bb,$bb,$bb
.byte $bb,$bb,$bb,$bb,$bb
.byte $00,$bb,$bb,$bb,$00
sphere7:
.byte $5,$5
.byte $00,$b0,$b3,$b0,$00
.byte $b0,$bb,$bb,$bb,$30
.byte $bb,$bb,$bb,$bb,$33
.byte $bb,$bb,$bb,$bb,$33
.byte $00,$3b,$3b,$3b,$00
sphere_shadow1:
.byte $5,$1
.byte $00,$a0,$a0,$a0,$00
sphere_shadow2:
.byte $5,$1
.byte $00,$00,$a0,$00,$00

87
megademo/mode7_zp.inc Normal file
View File

@ -0,0 +1,87 @@
;; Zero page monitor routines addresses
;; Flying Routine Only
TURNING = $60
;SCREEN_X = $61 ; not used?
SCREEN_Y = $62
ANGLE = $63
HORIZ_SCALE_I = $64
HORIZ_SCALE_F = $65
FACTOR_I = $66
FACTOR_F = $67
DX_I = $68
DX_F = $69
SPACEX_I = $6A
SPACEX_F = $6B
CX_I = $6C
CX_F = $6D
DY_I = $6E
DY_F = $6F
SPACEY_I = $70
SPACEY_F = $71
CY_I = $72
CY_F = $73
TEMP_I = $74
TEMP_F = $75
DISTANCE_I = $76
DISTANCE_F = $77
SPACEZ_I = $78
SPACEZ_F = $79
DRAW_SPLASH = $7A
SPEED = $7B
SPLASH_COUNT = $7C
OVER_LAND = $7D
NUM1L = $7E
NUM1H = $7F
NUM2L = $80
NUM2H = $81
RESULT = $82 ; 83,84,85
;NEGATE = $86 ; UNUSED?
LAST_SPACEX_I = $87
LAST_SPACEY_I = $88
LAST_MAP_COLOR = $89
DRAW_SKY = $8A
COLOR_MASK = $8B
KEY_COUNT = $8C
KEY_OFFSET = $8D
DRAW_BLUE_SKY = $8E
RANDOM_POINTER = $8F
MB_DETECTED = $96
WHICH_CHUNK = $97
; More zero-page addresses
; we try not to conflict with anything DOS, MONITOR or BASIC related
COLOR1 = $E0
COLOR2 = $E1
MATCH = $E2
XX = $E3
SHIPY = $E4
YADD = $E5
LOOP = $E6
MEMPTRL = $E7
MEMPTRH = $E8
NAMEL = $E9
NAMEH = $EA
NAMEX = $EB
CHAR = $EC
FIRST = $F0

351
megademo/multiply_fast.s Normal file
View File

@ -0,0 +1,351 @@
; Fast mutiply
; Note for our purposes we only care about 8.8 x 8.8 fixed point
; with 8.8 result, which means we only care about the middle two bytes
; of the 32 bit result. So we disable generation of the high and low byte
; to save some cycles.
;
; The old routine took around 700 cycles for a 16bitx16bit=32bit mutiply
; This routine, at an expense of 2kB of looku tables, takes around 250
; If you reuse a term the next time this drops closer to 200
; This routine was described by Stephen Judd and found
; in The Fridge and in the C=Hacking magazine
; http://codebase64.org/doku.php?id=base:seriously_fast_multiplication
; The key thing to note is that
; (a+b)^2 (a-b)^2
; a*b = ------- - --------
; 4 4
; So if you have tables of the squares of 0..511 you can lookup and subtract
; instead of multiplying.
; Table generation: I:0..511
; square1_lo = <((I*I)/4)
; square1_hi = >((I*I)/4)
; square2_lo = <(((I-255)*(I-255))/4)
; square2_hi = >(((I-255)*(I-255))/4)
; Note: DOS3.3 starts at $9600
.ifndef square1_lo
square1_lo = $8E00
square1_hi = $9000
square2_lo = $9200
square2_hi = $9400
.endif
; for(i=0;i<512;i++) {
; square1_lo[i]=((i*i)/4)&0xff;
; square1_hi[i]=(((i*i)/4)>>8)&0xff;
; square2_lo[i]=( ((i-255)*(i-255))/4)&0xff;
; square2_hi[i]=(( ((i-255)*(i-255))/4)>>8)&0xff;
; }
init_multiply_tables:
; Build the add tables
ldx #$00
txa
.byte $c9 ; CMP #immediate - skip TYA and clear carry flag
lb1: tya
adc #$00 ; 0
ml1: sta square1_hi,x ; square1_hi[0]=0
tay ; y=0
cmp #$40 ; subtract 64 and update flags (c=0)
txa ; a=0
ror ; rotate
ml9: adc #$00 ; add 0
sta ml9+1 ; update add value
inx ; x=1
ml0: sta square1_lo,x ; square1_lo[0]=1
bne lb1 ; if not zero, loop
inc ml0+2 ; increment values
inc ml1+2 ; increment values
clc ; c=0
iny ; y=1
bne lb1 ; loop
; Build the subtract tables based on the existing one
ldx #$00
ldy #$ff
second_table:
lda square1_hi+1,x
sta square2_hi+$100,x
lda square1_hi,x
sta square2_hi,y
lda square1_lo+1,x
sta square2_lo+$100,x
lda square1_lo,x
sta square2_lo,y
dey
inx
bne second_table
rts
; Fast 16x16 bit unsigned multiplication, 32-bit result
; Input: NUM1H:NUM1L * NUM2H:NUM2L
; Result: RESULT3:RESULT2:RESULT1:RESULT0
;
; Does self-modifying code to hard-code NUM1H:NUM1L into the code
; carry=0: re-use previous NUM1H:NUM1L
; carry=1: reload NUM1H:NUM1L (58 cycles slower)
;
; clobbered: RESULT, X, A, C
; Allocation setup: T1,T2 and RESULT preferably on Zero-page.
;
; NUM1H (x_i), NUM1L (x_f)
; NUM2H (y_i), NUM2L (y_f)
; NUM1L * NUM2L = AAaa
; NUM1L * NUM2H = BBbb
; NUM1H * NUM2L = CCcc
; NUM1H * NUM2H = DDdd
;
; AAaa
; BBbb
; CCcc
; + DDdd
; ----------
; RESULT
;fixed_16x16_mul_unsigned:
multiply:
bcc num1_same_as_last_time ; 2nt/3
;============================
; Set up self-modifying code
; this changes the code to be hard-coded to multiply by NUM1H:NUM1L
;============================
lda NUM1L ; load the low byte ; 3
sta sm1a+1 ; 3
sta sm3a+1 ; 3
sta sm5a+1 ; 3
sta sm7a+1 ; 3
eor #$ff ; invert the bits for subtracting ; 2
sta sm2a+1 ; 3
sta sm4a+1 ; 3
sta sm6a+1 ; 3
sta sm8a+1 ; 3
lda NUM1H ; load the high byte ; 3
sta sm1b+1 ; 3
sta sm3b+1 ; 3
sta sm5b+1 ; 3
; sta sm7b+1 ;
eor #$ff ; invert the bits for subtractin ; 2
sta sm2b+1 ; 3
sta sm4b+1 ; 3
sta sm6b+1 ; 3
; sta sm8b+1 ;
;===========
; 52
num1_same_as_last_time:
;==========================
; Perform NUM1L * NUM2L = AAaa
;==========================
ldx NUM2L ; (low le) ; 3
sec ; 2
sm1a:
lda square1_lo,x ; 4
sm2a:
sbc square2_lo,x ; 4
; a is _aa
; sta RESULT+0 ;
sm3a:
lda square1_hi,x ; 4
sm4a:
sbc square2_hi,x ; 4
; a is _AA
sta _AA+1 ; 3
;===========
; 24
; Perform NUM1H * NUM2L = CCcc
sec ; 2
sm1b:
lda square1_lo,x ; 4
sm2b:
sbc square2_lo,x ; 4
; a is _cc
sta _cc+1 ; 3
sm3b:
lda square1_hi,x ; 4
sm4b:
sbc square2_hi,x ; 4
; a is _CC
sta _CC+1 ; 3
;===========
; 24
;==========================
; Perform NUM1L * NUM2H = BBbb
;==========================
ldx NUM2H ; 3
sec ; 2
sm5a:
lda square1_lo,x ; 4
sm6a:
sbc square2_lo,x ; 4
; a is _bb
sta _bb+1 ; 3
sm7a:
lda square1_hi,x ; 4
sm8a:
sbc square2_hi,x ; 4
; a is _BB
sta _BB+1 ; 3
;===========
; 27
;==========================
; Perform NUM1H * NUM2H = DDdd
;==========================
sec ; 2
sm5b:
lda square1_lo,x ; 4
sm6b:
sbc square2_lo,x ; 4
; a is _dd
sta _dd+1 ; 3
;sm7b:
; lda square1_hi,x ;
;sm8b:
; sbc square2_hi,x ;
; a = _DD
; sta RESULT+3 ;
;===========
; 13
;===========================================
; Add the separate multiplications together
;===========================================
clc ; 2
_AA:
lda #0 ; loading _AA ; 2
_bb:
adc #0 ; adding in _bb ; 2
sta RESULT+1 ; 3
;==========
; 9
; product[2]=_BB+_CC+c
_BB:
lda #0 ; loading _BB ; 2
_CC:
adc #0 ; adding in _CC ; 2
sta RESULT+2 ; 3
;===========
; 7
; product[3]=_DD+c
; bcc dd_no_carry1 ;
; inc RESULT+3 ;
clc ; 2
;=============
; 2
dd_no_carry1:
; product[1]=_AA+_bb+_cc
_cc:
lda #0 ; load _cc ; 2
adc RESULT+1 ; 3
sta RESULT+1 ; 3
; product[2]=_BB+_CC+_dd+c
_dd:
lda #0 ; load _dd ; 2
adc RESULT+2 ; 3
sta RESULT+2 ; 3
;===========
; 16
; product[3]=_DD+c
; bcc dd_no_carry2 ;
; inc RESULT+3 ;
;=============
; 0
dd_no_carry2:
; *z_i=product[1];
; *z_f=product[0];
; rts ; 6
;=================
; Signed multiply
;=================
;multiply:
; jsr fixed_16x16_mul_unsigned ; 6
lda NUM1H ; x_i ; 3
;===========
; 12
bpl x_positive ;^3/2nt
sec ; 2
lda RESULT+2 ; 3
sbc NUM2L ; 3
sta RESULT+2 ; 3
; lda RESULT+3 ;
; sbc NUM2H ;
; sta RESULT+3 ;
;============
; 10
x_positive:
lda NUM2H ; y_i ; 3
;============
; ; 6
bpl y_positive ;^3/2nt
sec ; 2
lda RESULT+2 ; 3
sbc NUM1L ; 3
sta RESULT+2 ; 3
; lda RESULT+3 ;
; sbc NUM1H ;
; sta RESULT+3 ;
;===========
; 10
y_positive:
ldx RESULT+2 ; *z_i=product[2]; ; 3
lda RESULT+1 ; *z_f=product[1]; ; 3
rts ; 6
;==========
; 12

View File

@ -0,0 +1,10 @@
; Original size = 1020 bytes
sky_background:
; scroll_length:
.byte 255
.byte $A9,$00,$D0,$A9,$00,$50,$F5,$50,$AB,$00,$0F,$A7,$00,$0D,$DD,$D0,$A7,$00,$F0,$A5,$00,$11,$A7,$00,$A4,$70,$D0,$D0,$A7,$00,$70,$A9,$00,$05,$A0,$11,$00,$0F,$A0,$11,$00,$D0,$A0,$12,$00,$50,$A0,$2F,$00,$D0,$A9,$00,$50,$F5,$50,$AB,$00,$0F,$A0,$28,$00
.byte $A5,$00,$0F,$A6,$00,$F0,$A4,$00,$0F,$00,$00,$05,$A9,$00,$0F,$A4,$00,$05,$A7,$00,$DD,$A7,$00,$0F,$00,$F0,$00,$F0,$A8,$00,$07,$70,$A4,$75,$FF,$FF,$F0,$A0,$11,$00,$F0,$A5,$00,$05,$A5,$00,$07,$A0,$11,$00,$50,$A0,$1D,$00,$0F,$A0,$18,$00,$05,$AB,$00,$0F,$A6,$00,$F0,$A4,$00,$0F,$00,$00,$05,$A9,$00,$0F,$A4,$00,$05,$A0,$26,$00
.byte $00,$00,$F0,$AA,$00,$F0,$00,$00,$F0,$A7,$00,$07,$A4,$00,$70,$AB,$00,$D0,$DD,$0D,$A5,$00,$D0,$A5,$00,$0F,$A8,$00,$07,$70,$05,$75,$75,$D5,$DF,$0F,$A5,$00,$F0,$A5,$00,$50,$A0,$19,$00,$50,$A4,$00,$0D,$AE,$00,$0F,$A5,$00,$07,$A8,$00,$D0,$AB,$00,$07,$AB,$00,$0F,$AF,$00,$F0,$AA,$00,$F0,$00,$00,$F0,$A7,$00,$07,$A4,$00,$70,$A0,$2C,$00
.byte $A0,$40,$00,$07,$A0,$BF,$00
.byte $A1
; Compressed size = 231 bytes

View File

@ -1,3 +1,7 @@
;; Zero Page
FRAMEBUFFER = $00 ; $00 - $0F
;; LZ4 addresses
LZ4_SRC = $00
@ -46,7 +50,7 @@ LETTERX = $65
LETTERY = $66
LETTERD = $67
LETTER = $68
BLARGH = $69
;FACTOR_I = $66
;FACTOR_F = $67
@ -122,6 +126,7 @@ CURSOR = $9D
;MATCH = $E2
;XX = $E3
YY = $E4
HGR_COLOR = $E4
;SHIPY = $E4
;YADD = $E5
;LOOP = $E6
@ -131,12 +136,16 @@ YY = $E4
;NAMEH = $EA
;NAMEX = $EB
;CHAR = $EC
STATE = $ED
DISP_PAGE = $ED
DRAW_PAGE = $EE
OFFSET = $EF
;FIRST = $F0
;LASTKEY = $F1
;PADDLE_STATUS = $F2
LASTKEY = $F1
PADDLE_STATUS = $F2
SPRITETEMP = $F2
XPOS = $F3
YPOS = $F4