rle_decompress.s (1897B)
1 .include "global.s" 2 3 .module RLE_DECOMPRESS 4 5 .area _DATA 6 7rle_cursor: 8 .ds 0x02 9rle_counter: 10 .ds 0x01 11rle_current: 12 .ds 0x01 13 14 .area _CODE 15 16_rle_init:: 17 ldhl sp, #2 18 ld a, (hl+) 19 ld e, (hl) 20 ld hl, #rle_cursor 21 ld (hl+), a 22 ld a, e 23 ld (hl+), a 24 xor a 25 ld (hl+), a 26 ld (hl), a 27 ld e, #1 28 ret 29 30_rle_decompress:: 31 ;; Pop the return address 32 ldhl sp, #2 33 ld a, (hl+) 34 ld c, a 35 ld a, (hl+) 36 ld d, a ; de == dest 37 ld b, (hl) ; b == count 38 39 ld hl, #rle_cursor 40 ld a, (hl+) 41 ld h, (hl) 42 ld l, a ; hl == cursor 43 44 or h 45 ld e, a 46 ret z 47 48 ld e, c 49 50 ld a, (rle_counter) 51 or a 52 ld c, a 53 jr z, 1$ 54 55 ld a, (rle_current) 56 bit 7, c 57 jr nz, 10$ 58 jr 11$ 591$: 60 ;; Fetch the run 61 ld c, (hl) 62 inc hl 63 ;; Negative means a run 648$: 65 bit 7, c 66 jr z, 2$ 67 ;; Expanding a run 68 ld a, (hl+) 693$: 70 ld (de), a 71 inc de 72 73 dec b 74 jr z, 6$ 7510$: 76 inc c 77 jr NZ, 3$ 78 jr 1$ 792$: 80 ;; Zero means end of a block 81 inc c 82 dec c 83 jr z, 4$ 84 ;; Expanding a block 855$: 86 ld a, (hl+) 87 ld (de), a 88 inc de 89 90 dec b 91 jr z, 6$ 9211$: 93 dec c 94 jr NZ, 5$ 95 jr 1$ 964$: 97 ;; save state and exit 98 ld hl, #rle_cursor 99 xor a 100 ld (hl+), a 101 ld (hl), a 102 ld e, a 103 ret 1046$: 105 ;; save state and exit 106 ld d, h 107 ld e, l 108 ld hl, #rle_current 109 ld (hl-), a 110 ld a, c 111 ld (hl-), a 112 ld a, d 113 ld (hl-), a 114 ld (hl), e 115 ld e, #1 116 ret