-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfloat.asm
More file actions
317 lines (277 loc) · 4.07 KB
/
Copy pathfloat.asm
File metadata and controls
317 lines (277 loc) · 4.07 KB
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
;;; -*- TI-Asm -*-
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Floating-point Routines
;;;
;; NewReal:
;;
;; Create a new real number.
;;
;; Input:
;; - OP1 = TI-format floating-point number (9 bytes)
;;
;; Output:
;; - HL = Logo value
;;
;; Destroys:
;; - AF, BC, DE, HL
NewReal:
call AllocNodePair
push de
ex de,hl
ld hl,OP1+8
ld a,(OP1)
call FP9ToReal
pop hl
ret
;; NewComplex:
;;
;; Create a new complex number.
;;
;; Input:
;; - OP1 = TI-format real part (9 bytes)
;; - OP2 = TI-format imaginary part (9 bytes)
;;
;; Output:
;; - HL = Logo value
;;
;; Destroys:
;; - AF, BC, DE, HL
NewComplex:
call AllocNodeQuad
push de
ex de,hl
ld hl,OP1+8
ld a,(OP1)
call FP9ToReal
ld hl,-5
add hl,de
set 7,(hl) ; mark as complex
inc de
ld hl,OP2+8
ld a,(OP2)
call FP9ToReal
pop hl
ret
;; IsReal:
;;
;; Determine if a value is a real number (i.e., floating point, not
;; integer.)
;;
;; Input:
;; - HL = value
;;
;; Output:
;; - CF set if value is not a real number
;;
;; Destroys:
;; - A
IsReal:
bit 7,h
scf
ret z
push hl
call RefToPointer
ld a,(hl)
rrca
ccf
jr c,IsReal_Fail
inc hl
inc hl
ld a,(hl)
add a,a
IsReal_Fail:
pop hl
ret
;; IsComplex:
;;
;; Determine if a value is a complex number.
;;
;; Input:
;; - HL = value
;;
;; Output:
;; - CF set if value is not a complex number
;;
;; Destroys:
;; - A
IsComplex:
bit 7,h
scf
ret z
push hl
call RefToPointer
ld a,(hl)
rrca
jr c,IsComplex_Fail
inc hl
inc hl
ld a,(hl)
add a,a
IsComplex_Fail:
ccf
pop hl
ret
;; GetRealToOP2:
;;
;; Retrieve the value of a real number in TI format.
;;
;; Input:
;; - HL = Logo value
;;
;; Output:
;; - OP2 = TI-format floating-point number (9 bytes)
;;
;; Destroys:
;; - AF, BC, DE, HL
GetRealToOP2:
call IsReal
jp c,TypeAssertionFailed
GetRealToOP2_nc:
call RefToPointer
ex de,hl
ld bc,OP2+8
jr RealToFP9
;; GetRealToOP1:
;;
;; Retrieve the value of a real number in TI format.
;;
;; Input:
;; - HL = Logo value
;;
;; Output:
;; - OP1 = TI-format floating-point number (9 bytes)
;;
;; Destroys:
;; - AF, BC, DE, HL
GetRealToOP1:
call IsReal
jp c,TypeAssertionFailed
GetRealToOP1_nc:
call RefToPointer
ex de,hl
ld bc,OP1+8
;; fall through
;; RealToFP9:
;;
;; Convert a floating-point number from Logo format into 9-byte TI
;; format.
;;
;; Input:
;; - DE = address of input
;; - BC = address of LSB of output
;;
;; Output:
;; - DE advanced by 7 bytes
;;
;; Destroys:
;; - AF, BC, HL
RealToFP9:
ld a,(de)
rrca
call RealToFP_Byte
ld a,(de)
call RealToFP_Byte
ld a,(de)
call RealToFP_Byte
ld a,(de)
push af
call RealToFP_Byte
ld a,(de)
rrca
call RealToFP_Byte
ld a,(de)
call RealToFP_Byte
ld a,(de)
call RealToFP_Byte
ld a,(de)
ld (bc),a
dec bc
pop af
and 80h
ld (bc),a
ret
RealToFP_Byte:
;; convert one byte binary->BCD
ld h,a
rrca
rrca
rrca
and 0Fh
add a,low realToFPTable
ld l,a
ld a,h
ld h,high realToFPTable
and 7
add a,(hl)
daa
ld (bc),a
inc de
dec bc
ret
realToFPTable:
db 00h, 08h, 16h, 24h, 32h, 40h, 48h, 56h
db 64h, 72h, 80h, 88h, 96h
NO_BYTE_CARRY realToFPTable
;; FP9ToReal:
;;
;; Convert a floating-point number from 9-byte TI format into Logo
;; format.
;;
;; Input:
;; - HL = address of LSB of input (e.g., OP1+8)
;; - A = sign byte of input (e.g., (OP1))
;; - DE = address to store result
;;
;; Output:
;; - DE advanced by 7 bytes
;;
;; Destroys:
;; - AF, BC, HL
FP9ToReal:
and 80h
push af
call FPToReal_FirstByte
adc a,a
ld (de),a
; ld a,(currentGCFlag)
; ld b,a
call FPToReal_Byte
; or b
ld (de),a
call FPToReal_Byte
ld (de),a
call FPToReal_Byte
ld c,a
pop af
or c
ld (de),a
call FPToReal_Byte
adc a,a
ld (de),a
call FPToReal_Byte
or b
ld (de),a
call FPToReal_Byte
ld (de),a
inc de
ld a,(hl)
ld (de),a
ret
FPToReal_Byte:
;; convert one byte BCD->binary
;; note this will always return with carry set
inc de
FPToReal_FirstByte:
ld a,(hl)
and 0F0h
rrca
rrca
ld c,a
rrca
add a,c ; A = MSB * 6
scf
sbc a,(hl) ; A = (MSB * 6) - X - 1 (this will always carry)
cpl ; A = X - (MSB * 6)
dec hl
ret