git.y1.nz

gbdk-2020

GameBoy Development Kit
download: https://git.y1.nz/archives/gbdk.tar.gz
README | Files | Log | Refs | LICENSE

gbdk-lib/libc/targets/z80/zx0_decompress.s

      1 ; -----------------------------------------------------------------------------
      2 ; ZX0 decoder by Einar Saukas & Urusergi
      3 ; "Standard" version (68 bytes only)
      4 ; -----------------------------------------------------------------------------
      5 ; Parameters:
      6 ;   HL: source address (compressed data)
      7 ;   DE: destination address (decompressing)
      8 ; -----------------------------------------------------------------------------
      9 
     10         .title  "ZX0 Decompress"
     11         .module ZX0Decompress
     12 
     13         .area _CODE
     14 
     15 _zx0_decompress::
     16 dzx0_standard:
     17         ld      bc, #0xffff             ; preserve default offset 1
     18         push    bc
     19         inc     bc
     20         ld      a, #0x80
     21 dzx0s_literals:
     22         call    dzx0s_elias             ; obtain length
     23         ldir                            ; copy literals
     24         add     a, a                    ; copy from last offset or new offset?
     25         jr      c, dzx0s_new_offset
     26         call    dzx0s_elias             ; obtain length
     27 dzx0s_copy:
     28         ex      (sp), hl                ; preserve source, restore offset
     29         push    hl                      ; preserve offset
     30         add     hl, de                  ; calculate destination - offset
     31         ldir                            ; copy from offset
     32         pop     hl                      ; restore offset
     33         ex      (sp), hl                ; preserve offset, restore source
     34         add     a, a                    ; copy from literals or new offset?
     35         jr      nc, dzx0s_literals
     36 dzx0s_new_offset:
     37         pop     bc                      ; discard last offset
     38         ld      c, #0xfe                ; prepare negative offset
     39         call    dzx0s_elias_loop        ; obtain offset MSB
     40         inc     c
     41         ret     z                       ; check end marker
     42         ld      b, c
     43         ld      c, (hl)                 ; obtain offset LSB
     44         inc     hl
     45         rr      b                       ; last offset bit becomes first length bit
     46         rr      c
     47         push    bc                      ; preserve new offset
     48         ld      bc, #1                  ; obtain length
     49         call    nc, dzx0s_elias_backtrack
     50         inc     bc
     51         jr      dzx0s_copy
     52 dzx0s_elias:
     53         inc     c                       ; interlaced Elias gamma coding
     54 dzx0s_elias_loop:
     55         add     a, a
     56         jr      nz, dzx0s_elias_skip
     57         ld      a, (hl)                 ; load another group of 8 bits
     58         inc     hl
     59         rla
     60 dzx0s_elias_skip:
     61         ret     c
     62 dzx0s_elias_backtrack:
     63         add     a, a
     64         rl      c
     65         rl      b
     66         jr      dzx0s_elias_loop
     67 ; -----------------------------------------------------------------------------

This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.