cscg22-gearboy

CSCG 2022 Challenge 'Gearboy'
git clone https://git.sinitax.com/sinitax/cscg22-gearboy
Log | Files | Refs | sfeed.txt

gb_decompress_tiles.s (3326B)


      1; GB-Decompress tiledata directly to VRAM
      2; Compatible with GBTD
      3
      4        .include        "global.s"
      5
      6        .title  "GB Decompress"
      7        .module GBDecompress
      8
      9.macro WRAP_VRAM regH, ?loc
     10        bit     3, regH
     11        jr      z, loc
     12        res     4, regH
     13loc:
     14.endm
     15
     16.macro UNWRAP_VRAM regH, ?loc
     17        bit     3, regH
     18        jr      nz, loc
     19        set     4, regH
     20loc:
     21.endm
     22
     23
     24        .area _CODE
     25
     26_gb_decompress_bkg_data::
     27_gb_decompress_win_data::
     28        ld      d, #0x90
     29        ldh     a, (.LCDC)
     30        and     #LCDCF_BG8000
     31        jr      z, .load_params
     32_gb_decompress_sprite_data::
     33        ld      d, #0x80
     34
     35.load_params:
     36        ldhl    sp, #2
     37        ld      a, (hl+)
     38        
     39        ; Compute dest ptr
     40        swap    a       ; *16 (size of a tile)
     41        ld      e, a
     42        and     #0x0F   ; Get high bits
     43        add     d       ; Add base offset of target tile "block"
     44        ld      d, a
     45        ld      a, e
     46        and     #0xF0   ; Get low bits only
     47        ld      e, a
     48        WRAP_VRAM d
     49
     50        ld      a, (hl+)
     51        ld      h, (hl)
     52        ld      l, a
     53
     54; hl = source; de = dest
     55gb_decompress_vram::
     56        push    bc
     57        push    de
     581$:
     59        ld      a,(hl+) ; load command
     60        or      a
     61        jp      z,9$    ; exit, if last byte
     62        bit     7,a
     63        jr      nz,5$   ; string functions
     64        bit     6,a
     65        jr      nz,3$
     66        ; RLE byte
     67        and     #63     ; calc counter
     68        inc     a
     69        ld      c,a
     70        ld      a,(hl+)
     71        ld      b,a
     722$:
     73        WAIT_STAT
     74        ld      a,b
     75        ld      (de),a
     76        inc     de
     77        WRAP_VRAM d
     78        dec     c
     79        jr      nz,2$
     80        jr      1$      ; next command
     81        
     823$:                     ; RLE word
     83        and     #63
     84        inc     a
     85        ld      c, a
     86        ld      a,(hl+)
     87        ld      b, a
     884$:
     89        WAIT_STAT
     90        ld      a,b     ; store word
     91        ld      (de),a
     92        inc     de
     93        WRAP_VRAM d
     94        WAIT_STAT
     95        ld      a,(hl)
     96        ld      (de),a
     97        inc     de
     98        WRAP_VRAM d
     99        dec     c
    100        jr      nz,4$
    101        inc     hl      
    102        jr      1$      ; next command
    103
    1045$:
    105        bit     6,a
    106        jr      nz,7$
    107
    1086$:                     ; string repeat
    109        and     a,#63
    110        inc     a
    111        push    hl
    112
    113        ld      c,(hl)
    114        inc     hl
    115        ld      b,(hl)
    116
    117        ldhl    sp,#3
    118        bit     4,(hl)  ; check start address was above 0x9000
    119        jr      z, 11$
    120
    121        ld      h,d
    122        ld      l,e
    123        add     hl,bc
    124        UNWRAP_VRAM h
    125        jr      12$
    12611$:
    127        ld      h,d
    128        ld      l,e
    129        add     hl,bc
    13012$:
    131        ld      c,a
    132
    13314$:
    134        WAIT_STAT
    135        ld      a,(hl+)
    136        ld      (de),a
    137        WRAP_VRAM h
    138        inc     de
    139        WRAP_VRAM d
    140        dec     c
    141        jr      nz, 14$
    142
    143        pop     hl
    144        inc     hl
    145        inc     hl
    146        jp      1$      ; next command
    147
    1487$:                     ; string copy
    149        and     #63
    150        inc     a
    151        ld      c,a
    15215$:
    153        WAIT_STAT
    154        ld      a,(hl+)
    155        ld      (de),a
    156        inc     de
    157        WRAP_VRAM d
    158        dec     c
    159        jr      nz, 15$
    160
    161        jp      1$      ; next command
    1629$:
    163        pop     de
    164        pop     bc
    165        ret