cscg22-gearboy

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

gb_decompress.s (2269B)


      1; GB-Deompress routine
      2; Compatible with GBTD
      3
      4        .include        "global.s"
      5
      6        .title  "GB Decompress"
      7        .module GBDecompress
      8
      9        .area _CODE
     10
     11_gb_decompress::
     12        lda     hl,5(SP)
     13        ld      a,(hl-)
     14        ld      d,a
     15        ld      a,(hl-)
     16        ld      e,a
     17        ld      a,(hl-)
     18        ld      l,(hl)
     19        ld      h,a
     20
     21; hl = source; de = dest
     22gb_decompress::
     23        push    bc
     24        push    de
     251$:
     26        ld      a,(hl+) ; load command
     27        or      a
     28        jr      z,9$    ; exit, if last byte
     29        bit     7,a
     30        jr      nz,5$   ; string functions
     31        bit     6,a
     32        jr      nz,3$
     33        ; RLE byte
     34        and     #63     ; calc counter
     35        inc     a
     36        ld      b,a
     37        ld      a,(hl+)
     382$:
     39        ld      (de),a
     40        inc     de
     41        dec     b
     42        jr      nz,2$
     43        jr      1$      ; next command
     443$:                     ; RLE word
     45        and     #63
     46        inc     a
     47        ld      b,(hl)  ; load word into bc
     48        inc     hl
     49        ld      c,(hl)
     50        inc     hl
     514$:
     52        push    af
     53        ld      a,b     ; store word
     54        ld      (de),a
     55        inc     de
     56        ld      a,c
     57        ld      (de),a
     58        inc     de
     59        pop     af
     60        dec     a
     61        jr      nz,4$
     62        jr      1$      ; next command
     635$:
     64        bit     6,a
     65        jr      nz,7$
     66        ; string repeat
     67        and     a,#63
     68        inc     a
     69        push    hl
     70        ld      c,(hl)
     71        inc     hl
     72        ld      b,(hl)
     73        ld      h,d
     74        ld      l,e
     75        add     hl,bc
     76        ld      b,a
     776$:
     78        ld      a,(hl+)
     79        ld      (de),a
     80        inc     de
     81        dec     b
     82        jr      nz,6$
     83        pop     hl
     84        inc     hl
     85        inc     hl
     86        jr      1$      ; next command
     877$:                     ; string copy
     88        and     #63
     89        inc     a
     90        ld      b,a
     918$:                    
     92        ld      a,(hl+)
     93        ld      (de),a
     94        inc     de
     95        dec     b
     96        jr      nz,8$
     97        jr      1$      ; next command
     989$:
     99        pop     hl
    100        ld      a, e
    101        sub     l
    102        ld      e, a
    103        ld      a, d
    104        sbc     h
    105        ld      d, a
    106        pop     bc
    107        
    108        ret