-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdzx0_turbo.asm
100 lines (99 loc) · 3.44 KB
/
dzx0_turbo.asm
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
; -----------------------------------------------------------------------------
; ZX0 decoder by Einar Saukas & introspec
; "Turbo" version (126 bytes, 21% faster)
; -----------------------------------------------------------------------------
; Parameters:
; HL: source address (compressed data)
; DE: destination address (decompressing)
; -----------------------------------------------------------------------------
dzx0_turbo:
ld bc, $ffff ; preserve default offset 1
ld (dzx0t_last_offset+1), bc
inc bc
ld a, $80
jr dzx0t_literals
dzx0t_new_offset:
ld c, $fe ; prepare negative offset
add a, a
jp nz, dzx0t_new_offset_skip
ld a, (hl) ; load another group of 8 bits
inc hl
rla
dzx0t_new_offset_skip:
call nc, dzx0t_elias ; obtain offset MSB
inc c
ret z ; check end marker
ld b, c
ld c, (hl) ; obtain offset LSB
inc hl
rr b ; last offset bit becomes first length bit
rr c
ld (dzx0t_last_offset+1), bc ; preserve new offset
ld bc, 1 ; obtain length
call nc, dzx0t_elias
inc bc
dzx0t_copy:
push hl ; preserve source
dzx0t_last_offset:
ld hl, 0 ; restore offset
add hl, de ; calculate destination - offset
ldir ; copy from offset
pop hl ; restore source
add a, a ; copy from literals or new offset?
jr c, dzx0t_new_offset
dzx0t_literals:
inc c ; obtain length
add a, a
jp nz, dzx0t_literals_skip
ld a, (hl) ; load another group of 8 bits
inc hl
rla
dzx0t_literals_skip:
call nc, dzx0t_elias
ldir ; copy literals
add a, a ; copy from last offset or new offset?
jr c, dzx0t_new_offset
inc c ; obtain length
add a, a
jp nz, dzx0t_last_offset_skip
ld a, (hl) ; load another group of 8 bits
inc hl
rla
dzx0t_last_offset_skip:
call nc, dzx0t_elias
jp dzx0t_copy
dzx0t_elias:
add a, a ; interlaced Elias gamma coding
rl c
add a, a
jr nc, dzx0t_elias
ret nz
ld a, (hl) ; load another group of 8 bits
inc hl
rla
ret c
add a, a
rl c
add a, a
ret c
add a, a
rl c
add a, a
ret c
add a, a
rl c
add a, a
ret c
dzx0t_elias_loop:
add a, a
rl c
rl b
add a, a
jr nc, dzx0t_elias_loop
ret nz
ld a, (hl) ; load another group of 8 bits
inc hl
rla
jr nc, dzx0t_elias_loop
ret
; -----------------------------------------------------------------------------