pinobatch / pently

Scalable music engine for NES games
zlib License
72 stars 4 forks source link

Pack persistent zero page more tightly #21

Closed pinobatch closed 6 years ago

pinobatch commented 6 years ago

Pently uses more persistent zero page than competing engines (particularly FamiTone2), and zero page is often considered more precious than BSS. In addition, disabling features doesn't reduce the amount of zero page used. It should be possible to pack the pointers more tightly and move things that aren't pointers out.

Pently's current 36-byte zero page layout is as described in src/pentlymusic.s:

;       +0                +1                +2                +3
;  0  | Sq1 sound effect data ptr           Sq1 envelope data ptr
;  4  | Sq2 sound effect data ptr           Sq2 envelope data ptr
;  8  | Tri sound effect data ptr           Tri envelope data ptr
; 12  | Noise sound effect data ptr         Noise envelope data ptr
; 16  | Sq1 music pattern data ptr          Play/Pause        Attack channel
; 20  | Sq2 music pattern data ptr          Tempo
; 24  | Tri music pattern data ptr          Tempo counter
; 28  | Noise music pattern data ptr        Conductor segno
; 32  | Attack music pattern data ptr       Conductor track position

I propose moving music pattern data 2 bytes earlier to compact zero page to 32 bytes when attack is enabled:

;       +0                +1                +2                +3
;  0  | Sq1 sound effect data ptr           Sq1 music pattern data ptr
;  4  | Sq2 sound effect data ptr           Sq2 music pattern data ptr
;  8  | Tri sound effect data ptr           Tri music pattern data ptr
; 12  | Noise sound effect data ptr         Noise music pattern data ptr
; 16  | Sq1 envelope data ptr               Attack music pattern data ptr
; 20  | Sq2 envelope data ptr               Conductor track position
; 24  | Tri envelope data ptr               Tempo counter
; 28  | Noise envelope data ptr             Play/Pause        Attack channel

This moves Conductor segno and Tempo to BSS.

When attack envelopes are completely disabled (PENTLY_USE_ATTACK_PHASE = 0), it can be compacted further to 21 bytes:

;       +0                +1                +2                +3
;  0  | Sq1 sound effect data ptr           Sq1 music pattern data ptr
;  4  | Sq2 sound effect data ptr           Sq2 music pattern data ptr
;  8  | Tri sound effect data ptr           Tri music pattern data ptr
; 12  | Noise sound effect data ptr         Noise music pattern data ptr
; 16  | Conductor track position            Tempo counter
; 20  | Play/Pause