-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfuncs.asm
317 lines (279 loc) · 9.38 KB
/
funcs.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
;*******************************************************************************
; Copyright (C) 2023 Dave Moore
;
; This file is part of Space-Hockey.
;
; Space-Hockey is free software: you can redistribute it and/or modify it under
; the terms of the GNU General Public License as published by the Free Software
; Foundation, either version 2 of the License, or (at your option) any later
; version.
;
; Space-Hockey is distributed in the hope that it will be useful, but WITHOUT
; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
; FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
; details.
;
; You should have received a copy of the GNU General Public License along with
; Space-Hockey. If not, see <http://www.gnu.org/licenses/>.
;
; If you modify this program, or any covered work, by linking or combining it
; with the libraries referred to in README (or a modified version of said
; libraries), containing parts covered by the terms of said libraries, the
; licensors of this program grant you additional permission to convey the
; resulting work.
;*******************************************************************************
;###############################################################################
;
; Print a string
;
; Input: HL = address of string to print (terminated by 0)
; Corrupts: AF, HL
;
;###############################################################################
print_string:
ld a, (hl)
cp 0
ret z
inc hl
call TXT_OUTPUT
jr print_string
;###############################################################################
;
; Print a 3-digit decimal number
;
; Input: A = 8-bit value to display
; Corrupts: AF, BC
;
; Routine from https://tinyurl.com/mr39uep5
;
;###############################################################################
print_int:
ld b, 100 ; Divisor to obtain 100's digit value
call .digit ; Display digit
ld b, 10 ; Divisor to obtain 10's digit value
call .digit ; Display digit
ld b, 1 ; Divisor to obtain 1's digit value
.digit:
ld c, 0 ; Zeroise result
.dec_divide:
sub b ; Subtract divisor
jr c, .display ; If dividend < divisor, division ended
inc c ; Increment digit value
jr .dec_divide
.display:
add a, b ; Add divisor because dividend was
; negative, leaving remainder
push af
ld a, c ; Get digit value
add a, '0' ; Convert value into ASCII character
call TXT_OUTPUT ; Display digit
pop af
ret
;###############################################################################
;
; Internal BCD Function used to shift registers along a number of bytes so that
; commands can start from the MSB
;
; Input: B = number of bytes to shift HL and DE along
; Corrupts: BC, DE, HL
;
; Routine from https://chibiakumas.com/z80/advanced.php
;
;###############################################################################
bcd_get_end:
push bc
ld c, b ; We want to add BC, but we need to
; count the number of bytes minus one
dec c
ld b, 0
add hl, bc
ex hl, de ; We've done HL, but we also do DE
add hl, bc
ex hl, de
pop bc
ret
;###############################################################################
;
; Print BCD number
;
; Input: DE = location of BCD number array
; Input: B = number of bytes in BCD number array
; Corrupts: AF, DE
;
; Routine from https://chibiakumas.com/z80/advanced.php
;
;###############################################################################
bcd_show:
call bcd_get_end ; Need to process from the MSB not LSB
.loop:
ld a, (de)
and %11110000 ; Use the high nibble
rrca
rrca
rrca
rrca
add '0' ; Convert to a letter and print it
call TXT_OUTPUT
ld a, (de)
dec de
and %00001111 ; Now the low nibble
add '0'
call TXT_OUTPUT
djnz .loop ; Next byte
ret
;###############################################################################
;
; BCD Subtraction
;
; Input: DE = minuend
; Input: HL = subtrahend
; Input: B = number of bytes in BCD number array
; Output: DE = minuend - subtrahend
; Corrupts: AF, DE, HL
;
; Routine from https://chibiakumas.com/z80/advanced.php
;
;###############################################################################
bcd_subtract:
or a ; Clear carry flag
.loop:
ld a, (de)
sbc (hl) ; Subtract HL from DE with carry
daa ; Fix A using DAA
ld (de), a ; Store it
inc de
inc hl
djnz .loop
ret
;###############################################################################
;
; BCD Addition
;
; Input: DE = augend
; Input: HL = addend
; Input: B = number of bytes in BCD number array
; Output: DE = augend + addend
; Corrupts: AF, DE, HL
;
; Routine from https://chibiakumas.com/z80/advanced.php
;
;###############################################################################
bcd_add:
or a ; Clear carry flag
.loop:
ld a, (de)
adc (hl) ; Add HL to DE with carry
daa ; Fix A using DAA
ld (de), a ; Store it
inc de
inc hl
djnz .loop
ret
;###############################################################################
;
; BCD Comparison
;
; Input: DE = First number to compare
; Input: HL = Second number to compare
; Input: B = number of bytes in BCD number array
; Output: Z flag set if two numbers are equal
; Corrupts: AF, DE, HL
;
; Routine from https://chibiakumas.com/z80/advanced.php
;
;###############################################################################
bcd_compare:
call bcd_get_end ; Need to process from the MSB not LSB
.loop:
ld a, (de) ; Start from MSB
cp (hl)
ret c ; Smaller
ret nz ; Greater
dec de ; Equal so move onto next byte
dec hl
djnz .loop
or a ; Clear the carry flag
ret
;###############################################################################
;
; Get the Absolute Difference (ABS(A-B)) between two 8-bit numbers
;
; Input: A = first number
; Input: B = second number
; Output: A = absolute difference
; Corrupts: AF, BC
;
;###############################################################################
find_abs:
sub b
or a
ret p
neg
ret
;###############################################################################
;
; Get the Absolute value of a Signed 8-bit Number (ABS)
;
; Input: A = number
; Output: A = ABS(A)
; Corrupts: AF
;
;###############################################################################
find_abs_a:
or a
ret p
neg
ret
;###############################################################################
;
; Makes a Beep!
;
; Corrupts: AF
;
;###############################################################################
beep:
ld a, 7
call TXT_OUTPUT
ret
;###############################################################################
;
; Checks if either MSB or LSB in HL Register Pair is #FF
;
; Input: HL = Bytes to Check
; Output: A = #FF if either H or L is #FF, else A = #00
; Corrupts: AF
;
;###############################################################################
check_hl_for_ff:
ld a, h
cp #FF
jr z, .is_ff
ld a, l
cp #FF
jr z, .is_ff
ld a, #00
ret
.is_ff:
ld a, #FF
ret
;###############################################################################
;
; Checks if both MSB or LSB in HL Register Pair is #FF
;
; Input: HL = Bytes to Check
; Output: A = #FF if both H or L is #FF, else A = #00
; Corrupts: AF
;
;###############################################################################
check_hl_for_both_ff:
ld a, h
cp #FF
jr nz, .both_ff_not
ld a, l
cp #FF
jr nz, .both_ff_not
ld a, #FF
ret
.both_ff_not:
ld a, #00
ret