From 0a05cd5f8305e79175ee162a9493123bbccac1b2 Mon Sep 17 00:00:00 2001 From: unknown <71151164+ZERICO2005@users.noreply.github.com> Date: Thu, 11 Sep 2025 10:51:11 -0600 Subject: [PATCH 1/9] optimized the SMC used for gfx_floodfill. Saves 6 bytes --- src/graphx/graphx.asm | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/graphx/graphx.asm b/src/graphx/graphx.asm index 3ab709a1b..5cfe570b0 100644 --- a/src/graphx/graphx.asm +++ b/src/graphx/graphx.asm @@ -5708,16 +5708,17 @@ gfx_FloodFill: ld bc, (ix + 6) call _GetPixel ; ov = p(x, y); - ld (.oldcolor0), a - ld (.oldcolor1), a - ld (.oldcolor2), a ld b, (ix + 12) cp a, b ; return if same color jq z, .return - ld a, b - ld (.newcolor0), a - ld (.newcolor1), a + ld iy, _FloodFill_smc_base + ld (iy + (.oldcolor0 - _FloodFill_smc_base)), a + ld (iy + (.oldcolor1 - _FloodFill_smc_base)), a + ld (.oldcolor2), a + + ld (iy + (.newcolor0 - _FloodFill_smc_base)), b + ld (iy + (.newcolor1 - _FloodFill_smc_base)), b lea iy, ix ld bc, -3224 @@ -5847,6 +5848,8 @@ smcWord _XMaxMinus1 .oldcolor1 = $-1 jr z, .forloop1 +_FloodFill_smc_base := $ + .ovat: ld (ix + 6), bc lea de, ix - 24 ; push(y, l, x-1, dy); From 90097e25d5ead375497d8d191ff260f1c9c71f66 Mon Sep 17 00:00:00 2001 From: zerico <71151164+ZERICO2005@users.noreply.github.com> Date: Mon, 9 Mar 2026 12:40:47 -0600 Subject: [PATCH 2/9] optimized floodfill while(x <= x2) --- src/graphx/graphx.asm | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/graphx/graphx.asm b/src/graphx/graphx.asm index 5cfe570b0..ede4cb324 100644 --- a/src/graphx/graphx.asm +++ b/src/graphx/graphx.asm @@ -5919,6 +5919,7 @@ smcByte _YSpan ld hl, (ix - 15) jr .whileloop + .forloop2: inc bc inc de @@ -5928,15 +5929,13 @@ smcByte _YSpan add hl, bc jr c, .done ld a, (de) - cp a, 0 + xor a, 0 .oldcolor2 = $-1 jr nz, .forloop2 - -.done: + ; Z and NC +.done: ; <-- NZ and C ld (ix + 6), bc ld (ix - 11), bc ; l = x; - or a, a - sbc hl, bc jp nc, .forloop1start ; } while ((unsigned)x<=x2); ld hl, 0 From c272d30c70c3b6894eddf856a181c1d11a423dac Mon Sep 17 00:00:00 2001 From: unknown <71151164+ZERICO2005@users.noreply.github.com> Date: Fri, 12 Sep 2025 11:53:00 -0600 Subject: [PATCH 3/9] optimized offscreen sprite clipping --- src/graphx/graphx.asm | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/graphx/graphx.asm b/src/graphx/graphx.asm index ede4cb324..cd5f7e917 100644 --- a/src/graphx/graphx.asm +++ b/src/graphx/graphx.asm @@ -2698,8 +2698,8 @@ gfx_TransparentSprite: ; Returns: ; None push ix ; save ix sp + ; returns directly to the caller of gfx_(Transparent)Sprite when offscreen call _ClipCoordinates - jr nc, .culled ; iyl = new width (next) ; iyh = new height ld (.amount), a @@ -2718,7 +2718,6 @@ smcByte _TransparentColor add ix, de dec iyh jr nz, .loop -.culled: pop ix ret @@ -2779,8 +2778,8 @@ gfx_Sprite: ; Returns: ; None push ix ; save ix sp + ; returns directly to the caller of gfx_(Transparent)Sprite when offscreen call _ClipCoordinates - jr nc, .culled ; iyl = new width (next) ; iyh = new height wait_quick @@ -2794,7 +2793,6 @@ gfx_Sprite: add hl, bc ; move to next line dec iyh jr nz, .loop -.culled: pop ix ; restore ix sp ret @@ -2941,10 +2939,15 @@ smcByte _TransparentColor add ix, de dec iyh ; loop for height jr nz, .loop +_ClipCoordinates_Restore_IX: pop ix ; restore stack pointer ret ;------------------------------------------------------------------------------- +_ClipCoordinates_Offscreen: + ; return directly to the caller of gfx_(Transparent)Sprite + pop hl ; return address for gfx_(Transparent)Sprite + jr _ClipCoordinates_Restore_IX _ClipCoordinates: ; Clipping stuff ; Arguments: @@ -2952,6 +2955,7 @@ _ClipCoordinates: ; arg1 : X coordinate ; arg2 : Y coordinate ; Returns: +; Offscreen: returns directly to the caller of gfx_(Transparent)Sprite ; A : How much to add to the sprite per iteration ; BCU: 0 ; B : 0 @@ -2959,7 +2963,6 @@ _ClipCoordinates: ; IYL: New sprite width ; HL : Sprite pixel pointer ; IX : Buffer pixel pointer -; NC : If offscreen ld ix, 6 ; get pointer to arguments add ix, sp ld hl, (ix + 3) ; hl -> sprite data @@ -2995,7 +2998,7 @@ smcByte _YSpan sub a, l ; a = negated relative y .cliptop: add hl, bc ; is partially clipped top? - ret nc + jr nc, _ClipCoordinates_Offscreen ex de, hl ; e = new height - 1 ld c, a ; c = negated relative y ld b, iyl ; b = width @@ -3040,7 +3043,7 @@ smcWord _XSpan sub a, l ; a = negated relative x .clipleft: add hl, bc ; is partially clipped left? - ret nc ; return if offscreen + jr nc, _ClipCoordinates_Offscreen ex de, hl ; e = new width - 1 ld c, a ; bc = negated relative x ld hl, (ix + 3) ; hl -> sprite data @@ -3068,7 +3071,6 @@ smcWord _XMin inc hl ld ix, (CurrentBuffer) add ix, de - scf ; set carry for success ret ;------------------------------------------------------------------------------- From 2d60624cf085598e98f0f5325d251410429cbeef Mon Sep 17 00:00:00 2001 From: zerico <71151164+ZERICO2005@users.noreply.github.com> Date: Fri, 6 Mar 2026 20:45:01 -0700 Subject: [PATCH 4/9] saved a relocation by switching _indcallHL to ti._indcall --- src/graphx/graphx.asm | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/graphx/graphx.asm b/src/graphx/graphx.asm index cd5f7e917..d1f1edc58 100644 --- a/src/graphx/graphx.asm +++ b/src/graphx/graphx.asm @@ -381,13 +381,13 @@ gfx_AllocSprite: add hl, bc ld d, (hl) ; d = height add hl, bc - ld hl, (hl) ; hl = malloc + ld iy, (hl) ; iy = malloc push de mlt de ; de = width * height inc de ; +2 to store width and height inc de ; de = width * height + 2 push de - call _indcallHL ; hl = malloc(width * height + 2) + call ti._indcall ; hl = malloc(width * height + 2) pop de pop de ; e = width, d = height, ude = unknown ; check if malloc failed (hl == 0) @@ -3400,11 +3400,6 @@ gfx_SetTextXY: ld (_TextYPos), hl push hl ; xpos=don't care, sp=&xpos ex de, hl ; hl=return address -;------------------------------------------------------------------------------- -_indcallHL: -; Calls HL -; Inputs: -; HL : Address to call jp (hl) ;------------------------------------------------------------------------------- From df8be6a69784e54f0a23f6759127af704f0abd11 Mon Sep 17 00:00:00 2001 From: zerico <71151164+ZERICO2005@users.noreply.github.com> Date: Sat, 7 Mar 2026 11:48:04 -0700 Subject: [PATCH 5/9] optimized gfx_SetCharData --- src/graphx/graphx.asm | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/graphx/graphx.asm b/src/graphx/graphx.asm index d1f1edc58..5158639ab 100644 --- a/src/graphx/graphx.asm +++ b/src/graphx/graphx.asm @@ -4001,13 +4001,17 @@ gfx_SetCharData: ; arg0 : Pointer to character data; if null returns current data ; Returns: ; Pointer to character data if null, otherwise pointer to next character - ld iy, 0 - add iy, sp + ld hl, 6 + add hl, sp + ld de, (hl) ; de -> custom_character_data + dec hl + dec hl + dec hl + ld a, (hl) sbc hl, hl ; ld hl, 0 - ld de, (iy + 6) ; de -> custom_character_data sbc hl, de ; sets z flag if NULL add hl, de - ld l, (iy + 3) ; hl = index + ld l, a ; hl = index add hl, hl add hl, hl add hl, hl From fa124e13f25783cee3dec0348492a4616e2730da Mon Sep 17 00:00:00 2001 From: zerico <71151164+ZERICO2005@users.noreply.github.com> Date: Mon, 9 Mar 2026 16:15:27 -0600 Subject: [PATCH 6/9] remove IX from gfx_TilePtr --- src/graphx/graphx.asm | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/graphx/graphx.asm b/src/graphx/graphx.asm index 5158639ab..bf34f44af 100644 --- a/src/graphx/graphx.asm +++ b/src/graphx/graphx.asm @@ -3295,18 +3295,19 @@ gfx_TilePtr: ; uint8_t *gfx_TilePtr(gfx_tilemap_t *tilemap, unsigned x_offset, unsigned y_offset) { ; return &tilemap->map[(x_offset/tilemap->tile_width)+((y_offset/tilemap->tile_height)*tilemap->width)]; ; } - push ix - ld ix, 0 - add ix, sp - ld iy, (ix + 6) - ld hl, (ix + 9) + ld hl, 3 + add hl, sp + ld iy, (hl) + inc hl + inc hl + inc hl + ld hl, (hl) ld a, (iy + t_type_width) or a, a jr nz, .fastdiv0 ld bc, 0 ld c, (iy + t_tile_width) call ti._idvrmu - ex de, hl jr .widthnotpow2 .fastdiv0: ld b, a @@ -3314,9 +3315,11 @@ gfx_TilePtr: srl h rr l djnz .div0 -.widthnotpow2: ex de, hl - ld hl, (ix + 12) +.widthnotpow2: + ld hl, 9 + add hl, sp + ld hl, (hl) ld a, (iy + t_type_height) or a, a jr nz, .fastdiv1 @@ -3329,7 +3332,8 @@ gfx_TilePtr: jr .heightnotpow2 .fastdiv1: ld b, a -.div1: srl h +.div1: + srl h rr l djnz .div1 .heightnotpow2: @@ -3338,7 +3342,6 @@ gfx_TilePtr: add hl, de ld de, (iy + t_data) add hl, de - pop ix ret ;------------------------------------------------------------------------------- From 5ed95a0e679a9aa7b07f809f4b688a89ef7a44f9 Mon Sep 17 00:00:00 2001 From: zerico <71151164+ZERICO2005@users.noreply.github.com> Date: Mon, 9 Mar 2026 16:31:13 -0600 Subject: [PATCH 7/9] use the power of djnz --- src/graphx/graphx.asm | 44 +++++++++++++++++++++---------------------- 1 file changed, 21 insertions(+), 23 deletions(-) diff --git a/src/graphx/graphx.asm b/src/graphx/graphx.asm index bf34f44af..d7ad3ccec 100644 --- a/src/graphx/graphx.asm +++ b/src/graphx/graphx.asm @@ -3143,7 +3143,7 @@ _Tilemap: ld (.tilemethod), hl push ix ld ix, 0 - lea bc, ix + inc.s bc ; clear UBC add ix, sp lea hl, ix - 12 ld sp, hl @@ -3151,16 +3151,16 @@ _Tilemap: ld hl, (ix + y_offset) ld c, (iy + t_tile_height) - ld a, (iy + t_type_height) - or a, a - jr nz, .heightpow2 + ld b, (iy + t_type_height) + inc b + djnz .heightpow2 + ; UBC and B are zero call ti._idvrmu ex de, hl push de pop bc jr .heightnotpow2 .heightpow2: ; compute as power of 2 height using shifts - ld b, a dec c ld a, l and a, c @@ -3175,16 +3175,16 @@ _Tilemap: ld c, (iy + t_tile_width) ld hl, (ix + x_offset) ; x offset - ld a, (iy + t_type_width) - or a, a - jr nz, .widthpow2 + ld b, (iy + t_type_width) + inc b + djnz .widthpow2 + ; UBC and B are zero call ti._idvrmu ex de, hl push de pop bc jr .widthnotpow2 .widthpow2: - ld b, a dec c ld a, l and a, c @@ -3295,22 +3295,21 @@ gfx_TilePtr: ; uint8_t *gfx_TilePtr(gfx_tilemap_t *tilemap, unsigned x_offset, unsigned y_offset) { ; return &tilemap->map[(x_offset/tilemap->tile_width)+((y_offset/tilemap->tile_height)*tilemap->width)]; ; } - ld hl, 3 + ld bc, 3 + push bc + pop hl add hl, sp ld iy, (hl) - inc hl - inc hl - inc hl + add hl, bc ld hl, (hl) - ld a, (iy + t_type_width) - or a, a - jr nz, .fastdiv0 - ld bc, 0 + ld b, (iy + t_type_width) + inc b + djnz .fastdiv0 + ; UBC and B are zero ld c, (iy + t_tile_width) call ti._idvrmu jr .widthnotpow2 .fastdiv0: - ld b, a .div0: srl h rr l @@ -3320,10 +3319,10 @@ gfx_TilePtr: ld hl, 9 add hl, sp ld hl, (hl) - ld a, (iy + t_type_height) - or a, a - jr nz, .fastdiv1 - ld bc, 0 + ld b, (iy + t_type_height) + inc b + djnz .fastdiv1 + ; UBC and B are zero ld c, (iy + t_tile_height) push de call ti._idvrmu @@ -3331,7 +3330,6 @@ gfx_TilePtr: pop de jr .heightnotpow2 .fastdiv1: - ld b, a .div1: srl h rr l From e1ed6731b9e0c2d928348a53ecafec0c0248dc39 Mon Sep 17 00:00:00 2001 From: zerico <71151164+ZERICO2005@users.noreply.github.com> Date: Tue, 10 Mar 2026 15:03:44 -0600 Subject: [PATCH 8/9] clear IYU trick --- src/graphx/graphx.asm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/graphx/graphx.asm b/src/graphx/graphx.asm index d7ad3ccec..dcacb43fd 100644 --- a/src/graphx/graphx.asm +++ b/src/graphx/graphx.asm @@ -4825,7 +4825,7 @@ gfx_ScaleSprite: call _UCDivA ; ca = du = (source_width*256)/target_width pop hl ; hl->src_data pop de ; de->tgt_data - ld iy, 0 + inc.s iy ; clear IYU ld iyl, a ld ixh, c ; (.du) = bc:iyl, ixl = target_height From 05a61f8b5e3cd6ea9f63d3ae863d5baf14750574 Mon Sep 17 00:00:00 2001 From: zerico <71151164+ZERICO2005@users.noreply.github.com> Date: Sat, 4 Apr 2026 12:36:47 -0600 Subject: [PATCH 9/9] optimized dy calculation in gfx_Line_NoClip --- src/graphx/graphx.asm | 50 ++++++++++++++++--------------------------- 1 file changed, 19 insertions(+), 31 deletions(-) diff --git a/src/graphx/graphx.asm b/src/graphx/graphx.asm index dcacb43fd..a53d23588 100644 --- a/src/graphx/graphx.asm +++ b/src/graphx/graphx.asm @@ -2089,10 +2089,10 @@ OutcodeOutOutcode1: gfx_Line_NoClip: ; Draws an unclipped arbitrary line ; Arguments: -; arg0 : X1 coordinate (hl) -; arg1 : Y1 coordinate (b) -; arg2 : X2 coordinate (de) -; arg3 : Y2 coordinate (c) +; arg0 : X0 coordinate (hl) +; arg1 : Y0 coordinate (b) +; arg2 : X1 coordinate (de) +; arg3 : Y1 coordinate (a) ; Returns: ; None ld iy, 0 @@ -2101,18 +2101,24 @@ _Line_NoClip: ; <-- carry is cleared ld hl, (iy + 3) ld de, (iy + 9) ld b, (iy + 6) - ld c, (iy + 12) ; line from hl, b to de, c + ld a, (iy + 12) ; line from hl, b to de, a ; or a, a sbc hl, de add hl, de jr c, _draw_left_to_right ; draw left to right ex de, hl + ld c, a ld a, b ld b, c - ld c, a _draw_left_to_right: - push bc - pop iy + + sub a, b ; dy = y1 - y0 + ld iy, 320 + jr nc, .positive_dy + ld iy, -320 + neg ; abs(dy) +.positive_dy: + push hl ld hl, (CurrentBuffer) ld c, 160 @@ -2128,29 +2134,9 @@ _draw_left_to_right: push hl pop bc ; bc = dx - xor a, a - ld h, a - ld d, a - ld e, iyl ; y0 - ex.s de, hl - ld e, iyh ; y1 - - sbc hl, de - - jr nc, .positive_dy - ex de, hl or a, a sbc hl, hl - sbc hl, de ; abs(dy) -.positive_dy: - - ld a, iyl - sub a, iyh - ld iy, -320 - jr c, .use_negative_IY - ld iy, 320 -.use_negative_IY: - or a, a + ld l, a sbc hl, bc add hl, bc ; hl = dy jr nc, dl_vertical @@ -2164,7 +2150,7 @@ dl_horizontal: ld (_smc_dl_jr_0 + 0), a ; write smc ld (_smc_dl_width_1 + 1), iy ; write smc ex de, hl -; or a, a ; or a, h clears carry +; or a, a ; or a, h clears carry sbc hl, hl sbc hl, de ld (_smc_dl_dx_1 + 1), bc ; write smc @@ -2197,13 +2183,15 @@ _smc_dl_dx_1: _smc_dl_dy_1: ld de, 0 ; dy jr dl_hloop + dl_vertical: lea de, iy ld b, c ld a, l ld iyl, a ld c, a - srl a ; a = dy / 2 + ; or a, a + rra ; a = dy / 2 inc c pop hl wait_quick