-
Notifications
You must be signed in to change notification settings - Fork 13
/
z80-fixedpt.asm
236 lines (216 loc) · 4.39 KB
/
z80-fixedpt.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
; FP Registers:
; FP_A: BC
; FP_B: DE
; FP_C: HL
; FP_R: (IX)
fp_remainder:
dw 0
fp_i: ; loop index
db 0
fp_scratch:
dw 0
MACRO FP_LDA_BYTE source
ld b,source
ld c,0
ENDMACRO
MACRO FP_LDB_BYTE source
ld d,source
ld e,0
ENDMACRO
MACRO FP_LDA_BYTE_IND address
ld a,(address)
ld b,a
ld c,0
ENDMACRO
MACRO FP_LDB_BYTE_IND address
ld a,(address)
ld d,a
ld e,0
ENDMACRO
MACRO FP_LDA source
ld bc,source
ENDMACRO
MACRO FP_LDB source
ld de,source
ENDMACRO
MACRO FP_LDA_IND address
ld bc,(address)
ENDMACRO
MACRO FP_LDB_IND address
ld de,(address)
ENDMACRO
MACRO FP_STC dest
ld (dest),hl
ENDMACRO
fp_floor_byte: ; A = floor(FP_C)
ld a,h
bit 7,a
ret z
ld a,0
cp l
ld a,h
ret z
dec a
ret
fp_floor: ; FP_C = floor(FP_C)
bit 7,h
jp z,@zerofrac
ld a,0
cp l
ret z
dec h
@zerofrac:
ld l,0
ret
MACRO FP_TCA ; FP_A = FP_C
ld b,h
ld c,l
ENDMACRO
MACRO FP_TCB ; FP_B = FP_C
ld d,h
ld e,l
ENDMACRO
MACRO FP_SUBTRACT ; FP_C = FP_A - FP_B
ld h,b
ld l,c
or a
sbc hl,de
ENDMACRO
MACRO FP_ADD ; FP_C = FP_A + FP_B
ld h,b
ld l,c
add hl,de
ENDMACRO
fp_divide: ; FP_C = FP_A / FP_B; FP_REM = FP_A % FP_B
push.l de ; preserve FP_B
bit 7,b
jp nz,@abs_a ; get |FP_A| if negative
ld h,b
ld l,c ; FP_C = FP_A
jp @check_sign_b
@abs_a:
ld hl,0
or a
sbc hl,bc ; FP_C = |FP_A|
@check_sign_b:
bit 7,d
jp z,@shift_b
push.l hl ; preserve FP_C
ld hl,0
or a
sbc hl,de
ex de,hl ; FP_B = |FP_B|
pop.l hl ; restore FP_C
@shift_b:
ld e,d
ld d,0
push.l bc ; preserve FP_A
push.l de ; copy FP_B
exx ; to DE' register
pop.l de
ld hl,0 ; FP_R in HL' register
exx
ld b,16
@loop1:
add hl,hl ; Shift hi bit of FP_C into REM
exx ; switch to alternative registers set
adc hl,hl ; 16-bit left shift
ld a,l
sub e ; trial subtraction
ld c,a
ld a,h
sbc a,d
jp c,@loop2 ; Did subtraction succeed?
ld l,c ; if yes, save it
ld h,a
exx ; switch to primary registers set
inc l ; and record a 1 in the quotient
exx ; switch to alternative registers set
@loop2:
exx ; switch to primary registers set
djnz @loop1 ; decrement register B and loop while B>0
pop.l bc ; restore FP_A
pop.l de ; restore FP_B
bit 7,d
jp nz,@check_cancel
bit 7,b
ret z
jp @negative
@check_cancel:
bit 7,b
ret nz
@negative:
push.l bc
ld b,h
ld c,l
ld hl,0
or a
sbc hl,bc
pop.l bc
ret
fp_multiply: ; FP_C = FP_A * FP_B; FP_R overflow
push.l bc ; preserve FP_A
push.l de ; preserve FP_B
bit 7,b
jp z,@check_sign_b
ld hl,0
or a
sbc hl,bc
FP_TCA ; FP_A = |FP_A|
@check_sign_b:
bit 7,d
jp z,@init_c
ld hl,0
or a
sbc hl,de
FP_TCB ; FP_B = |FP_B|
@init_c:
ld hl,0 ; fp_scratch in register H'
exx ; fp_remainder in register L'
ld hl,0
exx ; switch to primary registers set
ld a,16 ; fp_i in register A
@loop1:
srl d
rr e
jp nc,@loop2
add hl,bc
@loop2:
rr h
rr l
exx ; switch to alternative registers set
rr h
rr l
exx ; switch to primary registers set
dec a
jp nz,@loop1
ld a,l
exx ; switch to alternative registers set
ld e,a ; we don't values in primary set anymore
ld d,0 ; so will use alternative set as primary
ld b,8 ; register B as loop counter
@loop3:
srl d
rr e
rr h
rr l
djnz @loop3 ; decrement and loop
pop.l de ; restore FP_B
pop.l bc ; restore FP_A
bit 7,d
jp nz,@check_cancel
bit 7,b
ret z
jp @negative
@check_cancel:
bit 7,b
ret nz
@negative:
push.l bc ; preserve FP_A
ld b,h
ld c,l
ld hl,0
or a
sbc hl,bc
pop.l bc ; restore FP_A
ret