diff --git a/src/fx/fx.hgr.48boxes.common.a b/src/fx/fx.hgr.48boxes.common.a index c10d1f425..c3028937c 100644 --- a/src/fx/fx.hgr.48boxes.common.a +++ b/src/fx/fx.hgr.48boxes.common.a @@ -68,6 +68,8 @@ ; !ifndef USES_CLEAR { + ; if an effect doesn't use any clear stages, you can reduce code size + ; by setting USES_CLEAR=0 before including this file USES_CLEAR = 1 } @@ -82,7 +84,17 @@ hgrlo = $300 ; [$C0 bytes] HGR base addresses BoxesX = $200 ; [$30 bytes] starting row for each box BoxesY = $230 ; [$30 bytes] starting byte offset for each box -; high bytes of drawing routines for each stage (actual routine will be page-aligned) +; High bytes of drawing routines for each stage (actual routines will be page-aligned). +; To minimize code size, we build drawing routines in this order: +; - copy01 (STAGE1 template) +; - copy00 (STAGE0 template) +; - copy0F..copy09 (OUTER_STAGE template) +; - copy08..copy02 (MIDDLE_STAGE template) +; - change some opcodes to turn the 'copy' routines into 'clear' routines +; - clear0F..clear08 (OUTER_STAGE) +; - clear07..clear02 (MIDDLE_STAGE) +; - clear01 (STAGE1) +; - clear00 (STAGE0) clear00 = $64 clear01 = $65 clear02 = $66 @@ -117,7 +129,9 @@ copy00 = $82 copy01 = $83 ; tokens for code generation -k_rts = 0 +; used as indexes into |codegen_pieces| and |codegen_piece_lengths|, +; so keep all three in sync +k_rts = 0 ; must be 0 k_inx_and_recalc = 1 k_recalc = 2 k_recalc_and_iny = 3 @@ -180,7 +194,9 @@ k_right_mask = 17 bne - pla - jsr GenerateStages + ; drawing routines for each stage are constructed dynamically + ; and stored at copy00..copy0F and clear00..clear0F + jsr BuildDrawingRoutines ; set up zero page for drawing phase ; A=0 here @@ -194,7 +210,6 @@ k_right_mask = 17 ; X=0 here +BUILD_HGR_LOOKUP_TABLES_X_IS_ALREADY_0 hgrlo, hgrhi - ; any was initialized by the BoxStages copy loop MainLoop ldx #48 BoxLoop stx box ldy BoxStages-1, x ; for each box, get its current stage @@ -205,7 +220,7 @@ BoxLoop stx box lda BoxesX-1, x ldy BoxesY-1, x ; Y = starting byte offset for this box tax ; X = starting HGR row for this box - inc any + inc any ; was initialized by the BoxStages copy loop clc j jsr $FD00 ; [SMC] call drawing routine for this stage ldx box @@ -217,10 +232,18 @@ NextBox dex bit $C000 bpl MainLoop + + ; execution falls through here + +; These are all the pieces of code we need to construct the drawing routines. +; There are 32 drawing routines (16 if USES_CLEAR=0), which we construct from +; four templates (below). Templates use tokens to refer to these code pieces. +; Note that several pieces overlap in order to minimize code size. +; Everything from CODEGEN_COPY_START and onward is copied to zero page for +; the code generation phase on program startup. CODEGEN_COPY_START !pseudopc 0 { RTS0 ; 1 byte - rts + rts ; also terminates MainLoop RTS0_E INX_AND_RECALC ; 16 bytes inx @@ -302,7 +325,7 @@ RIGHT_MASK ; 1 byte !byte $FD RIGHT_MASK_E -codegen_piece_lengths +codegen_piece_lengths ; length of each of the pieces !byte RTS0_E-RTS0 !byte INX_AND_RECALC_E-INX_AND_RECALC !byte RECALC_E-RECALC @@ -322,7 +345,7 @@ codegen_piece_lengths !byte LEFT_MASK_E-LEFT_MASK !byte RIGHT_MASK_E-RIGHT_MASK -codegen_pieces +codegen_pieces ; address of each of the pieces (on zero page, so 1 byte) !byte