-
Notifications
You must be signed in to change notification settings - Fork 22
/
undte.z80
99 lines (89 loc) · 2.65 KB
/
undte.z80
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
;
; Digram tree encoding (DTE) text decompression for Game Boy
; Copyright 2018 Damian Yerrick
;
; This software is provided 'as-is', without any express or implied
; warranty. In no event will the authors be held liable for any damages
; arising from the use of this software.
;
; Permission is granted to anyone to use this software for any purpose,
; including commercial applications, and to alter it and redistribute it
; freely, subject to the following restrictions:
;
; 1. The origin of this software must not be misrepresented; you must not
; claim that you wrote the original software. If you use this software
; in a product, an acknowledgment in the product documentation would be
; appreciated but is not required.
; 2. Altered source versions must be plainly marked as such, and must not be
; misrepresented as being the original software.
; 3. This notice may not be removed or altered from any source distribution.
;
include "src/global.inc"
def DTE_STACK_SIZE equ 8
def DTE_MIN_CODEUNIT equ 128
def FIRST_PRINTABLE_CU equ 24
section "dtestack",WRAM0,align[3]
dtestack: ds DTE_STACK_SIZE
section "undte",ROM0
;;
; Decompresses the title of help page A to the line buffer.
undte_helptitle_a::
ld de,helptitles
call de_index_a
fallthrough undte_to_help_line_buffer
;; Decompresses DTE text at HL to the line buffer.
undte_to_help_line_buffer::
ld de,help_line_buffer
fallthrough undte_line
;;
; Decompresses digram tree encoded (DTE) text from HL to DE.
; Stops at the first code unit less than FIRST_PRINTABLE_CU.
; @param HL source address
; @param DE destination address
; @return HL after a low code unit in src;
; DE at a low code unit in dst;
; A = ending low code unit
undte_line::
ld bc,dtestack
.charloop:
; If there's a code on the stack, print it
ld a,c
xor low(dtestack)
jr z,.stack_empty
dec bc
ld a,[bc]
db $fe ; short for jr .print_code_a
.stack_empty:
; Otherwise, retrieve a code from the compressed text
ld a,[hl+]
.print_code_a:
; If not a pair, save it to the string
cp DTE_MIN_CODEUNIT
jr nc,.code_is_pair
ld [de],a
; If not a control, advance to the next code
cp FIRST_PRINTABLE_CU
ret c
inc de
jr .charloop
.code_is_pair:
push hl
; For a pair, we want to push the second code to dtestack
; and then decode the first. The address of the second is
; dte_replacements + 2*(code - DTE_MIN_CODEUNIT) + 1
sub DTE_MIN_CODEUNIT
ld hl,dte_replacements + 1
add a ; A = 2*(code - DTE_MIN_CODEUNIT)
add l
ld l,a
adc h
sub l
ld h, a
; Get the second code and stack it
ld a,[hl-]
ld [bc],a
inc bc
; Now get the first code and print it
ld a,[hl]
pop hl
jr .print_code_a