CPCWiki forum

General Category => Programming => Topic started by: Arnaud on 18:18, 11 January 21

Title: Help for asm optimization
Post by: Arnaud on 18:18, 11 January 21
Hi,
here an ugly piece of code, i am pretty sure it can be a improved a lot.
This code draws tiles into a buffer not directly on screen.

Thanks for help,
Arnaud

HL = DEST
DE = TILE SPRITE

drawTile:
ld   bc, #0x0004

;; 8 Rows of 4-Bytes
ex   de, hl            ;; [1] HL <-> DE to restore DE
ldi
ldi
ldi
ldi
ex   de, hl            ;; [1] HL <-> DE to restore DE
ld   c, #0x4C          ;; [3] DE = Distance to next row (77)
add  hl, bc            ;; [3]

... 8 times

ld   bc, #-560    ;; [3] Distance to next Tile to draw

nextTile:   
add  hl, bc       ;; [3] Compute address to draw next tile
Title: Re: Help for asm optimization
Post by: Targhan on 18:30, 11 January 21
Two well known ideas:

Both these ideas will require your sprite to be encoded in the way you display it.

If you always display from the first line of the block, this is easy. If not, this is not so easy :).

Else, use set/add to go to the next line, and use a special case for the "#c050" case.
Title: Re: Help for asm optimization
Post by: redbox on 18:52, 11 January 21
Quote from: Targhan on 18:30, 11 January 21Two well known ideas:

    Draw from left to right, then right to left.
    Use gray encoding to use set/res to pass from one line to another (2 nops!).

Both these ideas will require your sprite to be encoded in the way you display it.

And you can also use the stack to speed up pushing the bytes to the screen:


;; FnDrawTileStack

;; Description: Draw a tile using the stack
;; Conditions:  Screen address must be mutiple of 8 (character height)
;;              Screen must be 32x32 character screen (INCs are 8-bit)
;;              Tile data must be aligned to boundary (INCs are 8-bit)
;;              Tile size is set to 16x16 (4 bytes by 16 lines)
;; Notes:       320 NOPs

;; Entry:       DE - screen address, HL - tile data
;; Exit:        None
;; Corrupt:     BC, DE, HL

;; Author:      redbox (http://cpcwiki.eu/index.php/User:Redbox)
       
        di
       
        ld      (FnDrawTileStack_OldStack + 1),sp
        ld      sp,hl
        ex      hl,de
       
        pop de : ld (hl),e : inc l : ld (hl),d : inc l  ; line 1
        pop de : ld (hl),e : inc l : ld (hl),d
       
        set     3,h

        pop de : ld (hl),e : dec l : ld (hl),d : dec l  ; line 2
        pop de : ld (hl),e : dec l : ld (hl),d

        set     4,h

        pop de : ld (hl),e : inc l : ld (hl),d : inc l  ; line 4
        pop de : ld (hl),e : inc l : ld (hl),d

        res     3,h

        pop de : ld (hl),e : dec l : ld (hl),d : dec l  ; line 3
        pop de : ld (hl),e : dec l : ld (hl),d
       
        set     5,h

        pop de : ld (hl),e : inc l : ld (hl),d : inc l  ; line 7
        pop de : ld (hl),e : inc l : ld (hl),d

        set     3,h

        pop de : ld (hl),e : dec l : ld (hl),d : dec l  ; line 8
        pop de : ld (hl),e : dec l : ld (hl),d

        res     4,h

        pop de : ld (hl),e : inc l : ld (hl),d : inc l  ; line 6
        pop de : ld (hl),e : inc l : ld (hl),d

        res     3,h

        pop de : ld (hl),e : dec l : ld (hl),d : dec l  ; line 5
        pop de : ld (hl),e : dec l : ld (hl),d

        ld      bc,&e040                ; HL here is e.g. &E000, we want to...
        add     hl,bc                   ; ...get to &C040, so we add &E040

        pop de : ld (hl),e : inc l : ld (hl),d : inc l  ; line 9
        pop de : ld (hl),e : inc l : ld (hl),d

        set     3,h

        pop de : ld (hl),e : dec l : ld (hl),d : dec l  ; line 10
        pop de : ld (hl),e : dec l : ld (hl),d

        set     4,h

        pop de : ld (hl),e : inc l : ld (hl),d : inc l  ; line 12
        pop de : ld (hl),e : inc l : ld (hl),d

        res     3,h

        pop de : ld (hl),e : dec l : ld (hl),d : dec l  ; line 11
        pop de : ld (hl),e : dec l : ld (hl),d

        set     5,h

        pop de : ld (hl),e : inc l : ld (hl),d : inc l  ; line 15
        pop de : ld (hl),e : inc l : ld (hl),d

        set     3,h

        pop de : ld (hl),e : dec l : ld (hl),d : dec l  ; line 16
        pop de : ld (hl),e : dec l : ld (hl),d

        res     4,h

        pop de : ld (hl),e : inc l : ld (hl),d : inc l  ; line 14
        pop de : ld (hl),e : inc l : ld (hl),d

        res     3,h

        pop de : ld (hl),e : dec l : ld (hl),d : dec l  ; line 13
        pop de : ld (hl),e : dec l : ld (hl),d

FnDrawTileStack_OldStack:
        ld      sp,&0000
        ei
                                   
        ret
Powered by SMFPacks Menu Editor Mod