1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
; ---
; void dzx7_turbo(void *src, void *dst)
; ---
; Dec 2012 by Einar Saukas & Urusergi
; "Turbo" version (89 bytes, 25% faster)
; ---
.def _dzx7_Turbo
.assume adl=1
; enter : hl = void *src
; de = void *dst
;
; exit : hl = & following uncompressed block
;
; uses : af, bc, de, hl
_dzx7_Turbo:
pop bc
pop hl
pop de
push de
push hl
push bc
ld a, 128
dzx7t_copy_byte_loop:
ldi ; copy literal byte
dzx7t_main_loop:
add a, a ; check next bit
call z, dzx7t_load_bits ; no more bits left?
jr nc, dzx7t_copy_byte_loop ; next bit indicates either literal or sequence
; determine number of bits used for length (Elias gamma coding)
push de
ld de, 0
ld bc, 1
dzx7t_len_size_loop:
inc d
add a, a ; check next bit
call z, dzx7t_load_bits ; no more bits left?
jr nc, dzx7t_len_size_loop
jp dzx7t_len_value_start
; determine length
dzx7t_len_value_loop:
add a, a ; check next bit
call z, dzx7t_load_bits ; no more bits left?
rl c
rl b
jr c, dzx7t_exit ; check end marker
dzx7t_len_value_start:
dec d
jr nz, dzx7t_len_value_loop
inc bc ; adjust length
; determine offset
ld e, (hl) ; load offset flag (1 bit) + offset value (7 bits)
inc hl
sla e
inc e
jr nc, dzx7t_offset_end ; if offset flag is set, load 4 extra bits
add a, a ; check next bit
call z, dzx7t_load_bits ; no more bits left?
rl d ; insert first bit into D
add a, a ; check next bit
call z, dzx7t_load_bits ; no more bits left?
rl d ; insert second bit into D
add a, a ; check next bit
call z, dzx7t_load_bits ; no more bits left?
rl d ; insert third bit into D
add a, a ; check next bit
call z, dzx7t_load_bits ; no more bits left?
ccf
jr c, dzx7t_offset_end
inc d ; equivalent to adding 128 to DE
dzx7t_offset_end:
rr e ; insert inverted fourth bit into E
; copy previous sequence
ex (sp), hl ; store source, restore destination
push hl ; store destination
sbc hl, de ; HL = destination - offset - 1
pop de ; DE = destination
ldir
dzx7t_exit:
pop hl ; restore source address (compressed data)
jp nc, dzx7t_main_loop
dzx7t_load_bits:
ld a, (hl) ; load another group of 8 bits
inc hl
rla
ret
end
|