Macro factors out sine code, just like sine data, since it's used 5 times

This commit is contained in:
Rangi 2018-02-03 21:11:55 -05:00
parent 271aa20b73
commit 73ea7c6326
9 changed files with 127 additions and 411 deletions

View File

@ -461,53 +461,9 @@ PokedexShow_GetDexEntryBank:
```asm ```asm
_Sine:: ; 84d9 _Sine:: ; 84d9
; A simple sine function. ; a = d * sin(e * pi/32)
; Return d * sin(e) in hl.
; e is a signed 6-bit value.
ld a, e ld a, e
and %111111 calc_sine_wave
cp %100000
jr nc, .negative
call .ApplySineWave
ld a, h
ret
.negative
and %011111
call .ApplySineWave
ld a, h
xor $ff
inc a
ret
.ApplySineWave: ; 84ef
ld e, a
ld a, d
ld d, 0
ld hl, .sinewave
add hl, de
add hl, de
ld e, [hl]
inc hl
ld d, [hl]
ld hl, 0
; Factor amplitude
.multiply
srl a
jr nc, .even
add hl, de
.even
sla e
rl d
and a
jr nz, .multiply
ret
.sinewave ; 850b
; A 32-word table representing a sine wave.
; sin(90 degrees) is index $10 with an amplitude of $100.
sine_wave 32
``` ```
`Sprites_Cosine` and `Sprites_Sine` in [engine/sprites.asm](/engine/sprites.asm): `Sprites_Cosine` and `Sprites_Sine` in [engine/sprites.asm](/engine/sprites.asm):
@ -515,50 +471,11 @@ _Sine:: ; 84d9
```asm ```asm
Sprites_Cosine: ; 8e72a Sprites_Cosine: ; 8e72a
; a = d * cos(a * pi/32) ; a = d * cos(a * pi/32)
add %010000 add %010000 ; cos(x) = sin(x + pi/2)
; fallthrough
Sprites_Sine: ; 8e72c Sprites_Sine: ; 8e72c
; a = d * sin(a * pi/32) ; a = d * sin(a * pi/32)
and %111111 calc_sine_wave
cp %100000
jr nc, .negative
call .ApplySineWave
ld a, h
ret
.negative
and %011111
call .ApplySineWave
ld a, h
xor $ff
inc a
ret
; 8e741
.ApplySineWave: ; 8e741
ld e, a
ld a, d
ld d, 0
ld hl, .sinewave
add hl, de
add hl, de
ld e, [hl]
inc hl
ld d, [hl]
ld hl, 0
.multiply
srl a
jr nc, .even
add hl, de
.even
sla e
rl d
and a
jr nz, .multiply
ret
; 8e75d
.sinewave ; 8e75d
sine_wave 32
``` ```
`BattleAnim_Cosine` and `BattleAnim_Sine` in [engine/battle_anims/functions.asm](/engine/battle_anims/functions.asm): `BattleAnim_Cosine` and `BattleAnim_Sine` in [engine/battle_anims/functions.asm](/engine/battle_anims/functions.asm):
@ -566,50 +483,16 @@ Sprites_Sine: ; 8e72c
```asm ```asm
BattleAnim_Cosine: ; ce732 (33:6732) BattleAnim_Cosine: ; ce732 (33:6732)
; a = d * cos(a * pi/32) ; a = d * cos(a * pi/32)
add %010000 add %010000 ; cos(x) = sin(x + pi/2)
; fallthrough
BattleAnim_Sine: ; ce734 (33:6734) BattleAnim_Sine: ; ce734 (33:6734)
; a = d * sin(a * pi/32) ; a = d * sin(a * pi/32)
and %111111 calc_sine_wave BattleAnimSineWave
cp %100000
jr nc, .negative
call .ApplySineWave
ld a, h
ret
.negative
and %011111
call .ApplySineWave
ld a, h
xor $ff
inc a
ret
.ApplySineWave:
ld e, a
ld a, d
ld d, 0
ld hl, BattleAnimSineWave
add hl, de
add hl, de
ld e, [hl]
inc hl
ld d, [hl]
ld hl, 0
.multiply
srl a
jr nc, .even
add hl, de
.even
sla e
rl d
and a
jr nz, .multiply
ret
... ...
BattleAnimSineWave: ; ce77f BattleAnimSineWave: ; ce77f
sine_wave 32 sine_table 32
; ce7bf ; ce7bf
``` ```
@ -617,47 +500,7 @@ BattleAnimSineWave: ; ce77f
```asm ```asm
StartTrainerBattle_DrawSineWave: ; 8c6f7 (23:46f7) StartTrainerBattle_DrawSineWave: ; 8c6f7 (23:46f7)
; a = d * sin(a * pi/32) calc_sine_wave
and %111111
cp %100000
jr nc, .negative
call .ApplySineWave
ld a, h
ret
.negative
and %011111
call .ApplySineWave
ld a, h
xor $ff
inc a
ret
.ApplySineWave: ; 8c70c (23:470c)
ld e, a
ld a, d
ld d, 0
ld hl, .sinewave
add hl, de
add hl, de
ld e, [hl]
inc hl
ld d, [hl]
ld hl, 0
.multiply
srl a
jr nc, .even
add hl, de
.even
sla e
rl d
and a
jr nz, .multiply
ret
; 8c728 (23:4728)
.sinewave ; 8c728
sine_wave 32
; 8c768 ; 8c768
``` ```
@ -666,51 +509,76 @@ And `CelebiEvent_Cosine` in [engine/events/celebi.asm](/engine/events/celebi.asm
```asm ```asm
CelebiEvent_Cosine: ; 49b3b (12:5b3b) CelebiEvent_Cosine: ; 49b3b (12:5b3b)
; a = d * cos(a * pi/32) ; a = d * cos(a * pi/32)
add %010000 add %010000 ; cos(x) = sin(x + pi/2)
calc_sine_wave
; 49bae
```
They all rely on `calc_sine_wave` in [macros/code.asm](/macros/code.asm):
```asm
calc_sine_wave: MACRO
; input: a = a signed 6-bit value
; output: a = d * sin(a * pi/32)
and %111111 and %111111
cp %100000 cp %100000
jr nc, .negative jr nc, .negative\@
call .ApplySineWave call .apply\@
ld a, h ld a, h
ret ret
.negative\@
.negative
and %011111 and %011111
call .ApplySineWave call .apply\@
ld a, h ld a, h
xor $ff xor $ff
inc a inc a
ret ret
.apply\@
.ApplySineWave: ; 49b52 (12:5b52)
ld e, a ld e, a
ld a, d ld a, d
ld d, 0 ld d, 0
ld hl, .sinewave if _NARG == 1
ld hl, \1
else
ld hl, .sinetable\@
endc
add hl, de add hl, de
add hl, de add hl, de
ld e, [hl] ld e, [hl]
inc hl inc hl
ld d, [hl] ld d, [hl]
ld hl, 0 ld hl, 0
.multiply .multiply\@ ; factor amplitude
srl a srl a
jr nc, .even jr nc, .even\@
add hl, de add hl, de
.even .even\@
sla e sla e
rl d rl d
and a and a
jr nz, .multiply jr nz, .multiply\@
ret ret
; 49b6e (12:5b6e) if _NARG == 0
.sinetable\@
.sinewave ; 49b6e sine_table 32
sine_wave 32 endc
; 49bae ENDM
``` ```
**Fix:** Call a single shared copy of the (co)sine code in bank 0. And on `sine_table` in [macros/data.asm](/macros/data.asm):
```asm
sine_table: MACRO
; \1 samples of sin(x) from x=0 to x<32768 (pi radians)
x = 0
rept \1
dw (sin(x) + (sin(x) & $ff)) >> 8 ; round up
x = x + DIV(32768, \1) ; a circle has 65536 "degrees"
endr
ENDM
```
**Fix:** Edit [home/sine.asm](/home/sine.asm) to contain a single copy of the (co)sine code in bank 0, and call it from those five sites.
## `GetForestTreeFrame` works, but it's still bad ## `GetForestTreeFrame` works, but it's still bad

View File

@ -749,47 +749,7 @@ WipeLYOverrides: ; 8c6d8
StartTrainerBattle_DrawSineWave: ; 8c6f7 (23:46f7) StartTrainerBattle_DrawSineWave: ; 8c6f7 (23:46f7)
; a = d * sin(a * pi/32) calc_sine_wave
and %111111
cp %100000
jr nc, .negative
call .ApplySineWave
ld a, h
ret
.negative
and %011111
call .ApplySineWave
ld a, h
xor $ff
inc a
ret
.ApplySineWave: ; 8c70c (23:470c)
ld e, a
ld a, d
ld d, 0
ld hl, .sinewave
add hl, de
add hl, de
ld e, [hl]
inc hl
ld d, [hl]
ld hl, 0
.multiply
srl a
jr nc, .even
add hl, de
.even
sla e
rl d
and a
jr nz, .multiply
ret
; 8c728 (23:4728)
.sinewave ; 8c728
sine_wave 32
; 8c768 ; 8c768
StartTrainerBattle_ZoomToBlack: ; 8c768 (23:4768) StartTrainerBattle_ZoomToBlack: ; 8c768 (23:4768)

View File

@ -4125,45 +4125,11 @@ BattleAnim_IncAnonJumptableIndex: ; ce72c (33:672c)
BattleAnim_Cosine: ; ce732 (33:6732) BattleAnim_Cosine: ; ce732 (33:6732)
; a = d * cos(a * pi/32) ; a = d * cos(a * pi/32)
add %010000 add %010000 ; cos(x) = sin(x + pi/2)
; fallthrough
BattleAnim_Sine: ; ce734 (33:6734) BattleAnim_Sine: ; ce734 (33:6734)
; a = d * sin(a * pi/32) ; a = d * sin(a * pi/32)
and %111111 calc_sine_wave BattleAnimSineWave
cp %100000
jr nc, .negative
call .ApplySineWave
ld a, h
ret
.negative
and %011111
call .ApplySineWave
ld a, h
xor $ff
inc a
ret
.ApplySineWave:
ld e, a
ld a, d
ld d, 0
ld hl, BattleAnimSineWave
add hl, de
add hl, de
ld e, [hl]
inc hl
ld d, [hl]
ld hl, 0
.multiply
srl a
jr nc, .even
add hl, de
.even
sla e
rl d
and a
jr nz, .multiply
ret
BattleAnim_Sine_e: ; ce765 (33:6765) BattleAnim_Sine_e: ; ce765 (33:6765)
ld a, e ld a, e
@ -4195,5 +4161,5 @@ BattleAnim_AbsCosinePrecise: ; ce778
; ce77f ; ce77f
BattleAnimSineWave: ; ce77f BattleAnimSineWave: ; ce77f
sine_wave 32 sine_table 32
; ce7bf ; ce7bf

View File

@ -241,47 +241,8 @@ UpdateCelebiPosition: ; 49aa2 (12:5aa2)
CelebiEvent_Cosine: ; 49b3b (12:5b3b) CelebiEvent_Cosine: ; 49b3b (12:5b3b)
; a = d * cos(a * pi/32) ; a = d * cos(a * pi/32)
add %010000 add %010000 ; cos(x) = sin(x + pi/2)
and %111111 calc_sine_wave
cp %100000
jr nc, .negative
call .ApplySineWave
ld a, h
ret
.negative
and %011111
call .ApplySineWave
ld a, h
xor $ff
inc a
ret
.ApplySineWave: ; 49b52 (12:5b52)
ld e, a
ld a, d
ld d, 0
ld hl, .sinewave
add hl, de
add hl, de
ld e, [hl]
inc hl
ld d, [hl]
ld hl, 0
.multiply
srl a
jr nc, .even
add hl, de
.even
sla e
rl d
and a
jr nz, .multiply
ret
; 49b6e (12:5b6e)
.sinewave ; 49b6e
sine_wave 32
; 49bae ; 49bae
GetCelebiSpriteTile: ; 49bae GetCelebiSpriteTile: ; 49bae

View File

@ -1,48 +1,4 @@
_Sine:: ; 84d9 _Sine:: ; 84d9
; A simple sine function. ; a = d * sin(e * pi/32)
; Return d * sin(e) in hl.
; e is a signed 6-bit value.
ld a, e ld a, e
and %111111 calc_sine_wave
cp %100000
jr nc, .negative
call .ApplySineWave
ld a, h
ret
.negative
and %011111
call .ApplySineWave
ld a, h
xor $ff
inc a
ret
.ApplySineWave: ; 84ef
ld e, a
ld a, d
ld d, 0
ld hl, .sinewave
add hl, de
add hl, de
ld e, [hl]
inc hl
ld d, [hl]
ld hl, 0
; Factor amplitude
.multiply
srl a
jr nc, .even
add hl, de
.even
sla e
rl d
and a
jr nz, .multiply
ret
.sinewave ; 850b
; A 32-word table representing a sine wave.
; sin(90 degrees) is index $10 with an amplitude of $100.
sine_wave 32

View File

@ -577,50 +577,11 @@ endr
Sprites_Cosine: ; 8e72a Sprites_Cosine: ; 8e72a
; a = d * cos(a * pi/32) ; a = d * cos(a * pi/32)
add %010000 add %010000 ; cos(x) = sin(x + pi/2)
; fallthrough
Sprites_Sine: ; 8e72c Sprites_Sine: ; 8e72c
; a = d * sin(a * pi/32) ; a = d * sin(a * pi/32)
and %111111 calc_sine_wave
cp %100000
jr nc, .negative
call .ApplySineWave
ld a, h
ret
.negative
and %011111
call .ApplySineWave
ld a, h
xor $ff
inc a
ret
; 8e741
.ApplySineWave: ; 8e741
ld e, a
ld a, d
ld d, 0
ld hl, .sinewave
add hl, de
add hl, de
ld e, [hl]
inc hl
ld d, [hl]
ld hl, 0
.multiply
srl a
jr nc, .even
add hl, de
.even
sla e
rl d
and a
jr nz, .multiply
ret
; 8e75d
.sinewave ; 8e75d
sine_wave 32
AnimateEndOfExpBar: ; 8e79d AnimateEndOfExpBar: ; 8e79d

View File

@ -1,13 +1,10 @@
Cosine:: ; 1b0f Cosine:: ; 1b0f
; Return d * cos(a) in hl ; a = d * cos(a * pi/32)
add %010000 ; 90 degrees add %010000 ; cos(x) = sin(x + pi/2)
; fallthrough
Sine:: ; 1b11 Sine:: ; 1b11
; Return d * sin(a) in hl ; a = d * sin(a * pi/32)
; a is a signed 6-bit value.
ld e, a ld e, a
homecall _Sine homecall _Sine
ret ret
; 1b1e ; 1b1e

View File

@ -52,3 +52,50 @@ endc
endr endr
and x and x
ENDM ENDM
calc_sine_wave: MACRO
; input: a = a signed 6-bit value
; output: a = d * sin(a * pi/32)
and %111111
cp %100000
jr nc, .negative\@
call .apply\@
ld a, h
ret
.negative\@
and %011111
call .apply\@
ld a, h
xor $ff
inc a
ret
.apply\@
ld e, a
ld a, d
ld d, 0
if _NARG == 1
ld hl, \1
else
ld hl, .sinetable\@
endc
add hl, de
add hl, de
ld e, [hl]
inc hl
ld d, [hl]
ld hl, 0
.multiply\@ ; factor amplitude
srl a
jr nc, .even\@
add hl, de
.even\@
sla e
rl d
and a
jr nz, .multiply\@
ret
if _NARG == 0
.sinetable\@
sine_table 32
endc
ENDM

View File

@ -111,7 +111,15 @@ menu_coords: MACRO
ENDM ENDM
sine_wave: MACRO bcd: MACRO
rept _NARG
dn ((\1) % 100) / 10, (\1) % 10
shift
endr
ENDM
sine_table: MACRO
; \1 samples of sin(x) from x=0 to x<32768 (pi radians) ; \1 samples of sin(x) from x=0 to x<32768 (pi radians)
x = 0 x = 0
rept \1 rept \1
@ -119,11 +127,3 @@ rept \1
x = x + DIV(32768, \1) ; a circle has 65536 "degrees" x = x + DIV(32768, \1) ; a circle has 65536 "degrees"
endr endr
ENDM ENDM
bcd: MACRO
rept _NARG
dn ((\1) % 100) / 10, (\1) % 10
shift
endr
ENDM