-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathez80.asm
192 lines (171 loc) · 3.95 KB
/
ez80.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
;
; Native eZ80 assembly routines
;
; Copyright (c) 2019 Dr. D'nar
; SPDX-License-Identifier: BSD-2-clause
.assume adl=1
.def _decompress_string
.def _lcd_dim
.def _lcd_bright
.def _get_rtc_seconds
.def _get_rtc_seconds_plus
.def _get_csc
.def _on_key_pressed
.def _clear_on_key
.ref _huffman_tree
.ref _exit_clean
_GetCSC = 002014Ch
_RestoreLCDBrightness = 0021AB8h
_DimLCDSlow = 0021AC0h
flags = 0D00080h ; location of OS Flags (+-80h)
rtc_Seconds = 0F30000h
rtc_Minutes = 0F30004h
.text
;-------------------------------------------------------------------------------
_decompress_string:
; Inputs:
; - arg0: Pointer to input Huffman bit stream
; - arg1: Pointer to target buffer
; Output:
; - HL: Pointer to byte after last written byte, can be used to check output
; string length
ld iy, 0
add iy, sp
push ix
ld ix, (iy + 6)
ld iy, (iy + 3)
; Internal registers:
; - BC: Current bit/byte of input Huffman stream
; - DE: Temporary
; - HL: Pointer to Huffman table entry
ld de, 0
ld b, 1
.loop:
ld hl, (_huffman_tree)
.innerLoop:
; Is this node a leaf?
ld e, (hl)
bit 7, e
jr nz, .deHuffmanDone
; Is it time to fetch another byte of input?
djnz .getNextBit
; Yes, get next byte
ld b, 8
ld c, (iy)
inc iy
.getNextBit:
; Get next bit of input
rrc c
jr nc, .resetBit
inc hl
ld e, (hl)
.resetBit: ; Move to next node
add hl, de
jr .innerLoop
.deHuffmanDone:
; Write decompressed code to output buffer
ld a, e
and 7Fh
jr z, .notNbsp
; TODO: This is a terrible hack to covert code 127 into a non-breaking space
cp 7Fh
jr nz, .notNbsp
add a, 160 - 7Fh ; needs to become 160 & reset Z
.notNbsp:
ld (ix), a
inc ix
jr nz, .loop
lea hl, ix + 0
pop ix
ret
;-------------------------------------------------------------------------------
_lcd_dim:
; Calls the OS's LCD idle dimming routines.
ld iy, flags
jp _DimLCDSlow
_lcd_bright:
; Restores the LCD brightness to the user's preference.
ld iy, flags
jp _RestoreLCDBrightness
;-------------------------------------------------------------------------------
_get_rtc_seconds:
; Returns the number of seconds elapsed since the start of the hour.
; Used by APD routines.
ld hl, rtc_Seconds
ld a, (hl)
ld de, (rtc_Minutes)
ld hl, (hl)
cp l
jr nz, _get_rtc_seconds
ld d, 60
mlt de
add hl, de
ret
_get_rtc_seconds_plus:
; Returns the number of seconds elapsed since the start of the hour, plus an
; offset. If the resulting time passes the end of the hour, wraps the time
; around.
; Used by APD routines.
call _get_rtc_seconds
pop bc
pop de
push de
push bc
add hl, de
ld de, 3600
or a
sbc hl, de
ret nc
add hl, de
ret
;-------------------------------------------------------------------------------
_get_csc:
; Wraps GetCSC with two differences:
; - Assumes you're using this in an input loop, so it does EI \ HALT to save a
; tiny bit of power.
; - If the ON key has been pressed and the user then presses CLEAR, aborts the
; program immediately.
ei
halt
call _GetCSC
cp 15 ; skClear
ret nz
ld hl, flags + 9 ; onFlags
bit 4, (hl) ; onInterrupt
ret z
jp _exit_clean
_on_key_pressed:
; Returns 1 if the ON key has been pressed since the last time the
; ON-key-pressed flag was checked.
; Returns 0 if the ON key has not been pressed.
ld a, (flags + 9)
and 10h
ret z
inc a
ret
_clear_on_key:
; Resets the ON-key-pressed flag.
ld hl, flags + 9 ; iy + onFlags
res 4, (hl) ; onInterrupt
ret
;-------------------------------------------------------------------------------
;_get_compressed_string:
; pop de
; pop hl
; push hl
; push de
; ld de, (_compressed_strings)
; jr indexShortArray
;indexShortArray:
; ld a, l
; or h
; ret z
; add hl, hl
; ex de, hl
; add hl, de
; ld e, (hl)
; inc hl
; ld d, (hl)
; ld hl, (_dungeon)
; add hl, de
; ret