-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcalc_mant.S
271 lines (241 loc) · 7.12 KB
/
calc_mant.S
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
; ****************************************************************************
;
; Calculator mantissa operations
;
; ****************************************************************************
#include "include.inc"
.text
; ----------------------------------------------------------------------------
; Shift mantissa left (to higher bits)
; ----------------------------------------------------------------------------
; INPUT: R_M3..R_M10 mantissa
; C = input carry
; OUTPUT: R_M3..R_M10 mantissa shifted left
; C = output carry
; DESTROYS: -
; ----------------------------------------------------------------------------
.global CalcMantRol
CalcMantRol:
rol R_M10
rol R_M9
rol R_M8
rol R_M7
rol R_M6
rol R_M5
rol R_M4
rol R_M3
ret
; ----------------------------------------------------------------------------
; Shift mantissa right (to lower bits)
; ----------------------------------------------------------------------------
; INPUT: R_M3..R_M10 mantissa
; C = input carry
; OUTPUT: R_M3..R_M10 mantissa shifted right
; C = output carry
; DESTROYS: -
; ----------------------------------------------------------------------------
.global CalcMantRor
CalcMantRor:
ror R_M3
ror R_M4
ror R_M5
ror R_M6
ror R_M7
ror R_M8
ror R_M9
ror R_M10
ret
; ----------------------------------------------------------------------------
; Add mantissas
; ----------------------------------------------------------------------------
; INPUT: R_M3..R_M10 mantissa 1
; R_N3..R_N10 mantissa 2
; OUTPUT: R_M3..R_M10 result
; C = output carry
; DESTROYS: -
; ----------------------------------------------------------------------------
.global CalcMantAdd
CalcMantAdd:
add R_M10,R_N10
adc R_M9,R_N9
adc R_M8,R_N8
adc R_M7,R_N7
adc R_M6,R_N6
adc R_M5,R_N5
adc R_M4,R_N4
adc R_M3,R_N3
ret
; ----------------------------------------------------------------------------
; Subtract mantissas
; ----------------------------------------------------------------------------
; INPUT: R_M3..R_M10 mantissa 1
; R_N3..R_N10 mantissa 2
; OUTPUT: R_M3..R_M10 result
; C = output borrow
; DESTROYS: -
; ----------------------------------------------------------------------------
.global CalcMantSub
CalcMantSub:
sub R_M10,R_N10
sbc R_M9,R_N9
sbc R_M8,R_N8
sbc R_M7,R_N7
sbc R_M6,R_N6
sbc R_M5,R_N5
sbc R_M4,R_N4
sbc R_M3,R_N3
ret
; ----------------------------------------------------------------------------
; OR mantissas
; ----------------------------------------------------------------------------
; INPUT: R_M3..R_M10 mantissa 1
; R_N3..R_N10 mantissa 2
; OUTPUT: R_M3..R_M10 result
; C = output borrow
; DESTROYS: -
; ----------------------------------------------------------------------------
#if 0
.global CalcMantOr
CalcMantOr:
or R_M10,R_N10
or R_M9,R_N9
or R_M8,R_N8
or R_M7,R_N7
or R_M6,R_N6
or R_M5,R_N5
or R_M4,R_N4
or R_M3,R_N3
ret
; ----------------------------------------------------------------------------
; AND mantissas
; ----------------------------------------------------------------------------
; INPUT: R_M3..R_M10 mantissa 1
; R_N3..R_N10 mantissa 2
; OUTPUT: R_M3..R_M10 result
; C = output borrow
; DESTROYS: -
; ----------------------------------------------------------------------------
.global CalcMantAnd
CalcMantAnd:
and R_M10,R_N10
and R_M9,R_N9
and R_M8,R_N8
and R_M7,R_N7
and R_M6,R_N6
and R_M5,R_N5
and R_M4,R_N4
and R_M3,R_N3
ret
; ----------------------------------------------------------------------------
; XOR mantissas
; ----------------------------------------------------------------------------
; INPUT: R_M3..R_M10 mantissa 1
; R_N3..R_N10 mantissa 2
; OUTPUT: R_M3..R_M10 result
; C = output borrow
; DESTROYS: -
; ----------------------------------------------------------------------------
.global CalcMantXor
CalcMantXor:
eor R_M10,R_N10
eor R_M9,R_N9
eor R_M8,R_N8
eor R_M7,R_N7
eor R_M6,R_N6
eor R_M5,R_N5
eor R_M4,R_N4
eor R_M3,R_N3
ret
#endif
; ----------------------------------------------------------------------------
; SBC R_M9..R_3,R_ZERO
; ----------------------------------------------------------------------------
; INPUT: R_M3..R_M9 mantissa
; R_ZERO = subtractes register
; C = input borrow
; OUTPUT: R_M3..R_M9 result
; C = output borrow
; DESTROYS: -
; ----------------------------------------------------------------------------
.global CalcMantSbc0
CalcMantSbc0:
sbc R_M9,R_ZERO
sbc R_M8,R_ZERO
sbc R_M7,R_ZERO
sbc R_M6,R_ZERO
sbc R_M5,R_ZERO
sbc R_M4,R_ZERO
sbc R_M3,R_ZERO
ret
; ----------------------------------------------------------------------------
; Increment mantissa
; ----------------------------------------------------------------------------
; INPUT: R_M3..R_M10 = mantissa
; R_ZERO = 0
; OUTPUT: returns Z flag if result is 0 (overflow from 0xFF 0xFF 0xFF...)
; DESTROYS: -
; ----------------------------------------------------------------------------
.global CalcMantInc
CalcMantInc:
push R_ZERO
dec R_ZERO ; R_ZERO <- 0xff, subtracted number = -1 (= add +1)
sub R_M10,R_ZERO
; INPUT: R_M3..R_M9 mantissa
; R_ZERO = subtractes register
; C = input borrow
; OUTPUT: R_M3..R_M9 result
; C = output borrow
; DESTROYS: -
rcall CalcMantSbc0 ; SBC R_M9..R_3,R_ZERO
pop R_ZERO
ret
; ----------------------------------------------------------------------------
; Complement mantissa
; ----------------------------------------------------------------------------
; INPUT/OUTPUT: R_M3..(R_M9) R_M10 = mantissa
; DESTROYS: -
; ----------------------------------------------------------------------------
.global CalcMantNot
CalcMantNot:
com R_M10
.global CalcMantNot2
CalcMantNot2:
com R_M9
com R_M8
com R_M7
com R_M6
com R_M5
com R_M4
com R_M3
ret
; ----------------------------------------------------------------------------
; Negate extended mantissa
; ----------------------------------------------------------------------------
; INPUT: R_M2..R_M10 extended mantissa
; R_ZERO = 0
; OUTPUT: R_M2..R_M10 extended mantissa negated
; CY = carry set if result is not 0
; DESTROYS: -
; ----------------------------------------------------------------------------
.global CalcMantNeg
CalcMantNeg:
dec R_ZERO ; R_ZERO <- 0xff
; Complement: here we get number -1 - X, but we need 0 - X, so we must add +1 later
; complement mantissa (0xff -> 0x00, we'll get -1 - X)
com R_M2
; INPUT/OUTPUT: R_M3..(R_M9) R_M10 = mantissa
; DESTROYS: -
rcall CalcMantNot2 ; complement mantissa
; negate lowest byte of mantissa: complement R_M10 and add 1 (<>0 -> CY, 0 -> NC)
neg R_M10 ; carry will be set if number was NOT 0 (R_M10 = 0 - R_M10, CARRY <- (R_M10 != 0))
; add +1 (sub -1) with carry from R_M10
; INPUT: R_M3..R_M9 mantissa
; R_ZERO = subtractes register
; C = input borrow
; OUTPUT: R_M3..R_M9 result
; C = output borrow
; DESTROYS: -
rcall CalcMantSbc0 ; R_Mx + 1 - C
sbc R_M2,R_ZERO ; increment if lower byte was 0, set carry if result is NOT 0 (R_M2 = R_M2 - (0xFF + CARRY))
inc R_ZERO
ret