This repository has been archived by the owner on Dec 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfloatmath.c
258 lines (196 loc) · 5.26 KB
/
floatmath.c
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
// This library interfaces with the full normal calc float rutines.
// This is highly accurate but not very flexible with asm programs
// floating point stack
// not much documentation exists
// but i can assume its like
// the normal stack but for ops
// only odd ops seem to exist
#define PushOP1()bcall(0x43C9)
#define PushOP5()bcall(0x43C0)
#define PushOP3()bcall(0x43C3)
#define PopOP5()bcall(0x4378)
#define PopOP3()bcall(0x437B)
#define PopOP1()bcall(0x437E)
#define OP1_TO_ANS() bcall(0x4ABF) // saves op1 to ans
#define Store_Theta() bcall(0x4AC2)
#define Store_R() bcall(0x4AC5)
#define Store_Y() bcall(0x4AC8)
#define Store_N() bcall(0x4ACB)
#define Store_T() bcall(0x4ACE)
#define Store_X() bcall(0x4AD1)
#define MATH_FLOOR() bcall(0x405D) // op1 = floor(op1)
#define MATH_CEIL() bcall(0x489A) // op1 = ceil(op1)
#define TEN_TO_THE_POWER_OF_OP1() bcall(0x40B7) // 10 ^ (op1) // not sure the use of this but its here
#define INC_OP1() bcall(0x4069) // op1 += 1
#define DEC_OP1() bcall(0x406C) // op2 -= 1
#define DOUBLE_OP1() bcall(0x4066) // op1 *= 2
#define FP_ADD() bcall(0x4072) // op1 += op2
#define FP_SUB() bcall(0x406F)// op1 -= op2
#define FP_MULT() bcall(0x4084) // op1 *= op2
#define FP_DIV() bcall(0x4099) // op1 /= op2
#define SQUARE_OP1() bcall(0x4081) // op1 = op1 * op1
#define CUBE_OP1() bcall(0x407B) // op1 = op1 * op1 * op1
#define SQ_ROOT_OP1() bcall(0x409C) // op1 = sqrt( op1 )
#define FP_Recip() bcall(0x4096) // op1 = 1/op1
#define DEG_TO_RAD() bcall(0x4075) // op1 = Pi/180 * op1 // may be bigger than 2pi
// SUPER SLOW !!!! DON'T USE UNLESS NECESSARY
// around 150,000 to 200,000 cycles, the base clock is 8 mhz and turbo is 16
#define Tan() bcall(0x40C3) // op1 = Tan( op1 )
#define Sin() bcall(0x40BD) // op1 = Sin( op1 )
#define cos() bcall(0x40C0) // op1 = cos( op1 )
// load instructions
#define OP3ToOP4() bcall(0x4114)
#define OP1ToOP4() bcall(0x4117)
#define OP2ToOP4() bcall(0x411A)
#define OP4ToOP2() bcall(0x411D)
#define OP1ToOP3() bcall(0x4123)
#define OP5ToOP2() bcall(0x4126)
#define OP5ToOP6() bcall(0x4129)
#define OP5ToOP4() bcall(0x412C)
#define OP1ToOP2() bcall(0x412F)
#define OP6ToOP2() bcall(0x4132)
#define OP6ToOP1() bcall(0x4135)
#define OP4ToOP1() bcall(0x4138)
#define OP5ToOP1() bcall(0x413B)
#define OP3ToOP1() bcall(0x413E)
#define OP6ToOP5() bcall(0x4141)
#define OP4ToOP5() bcall(0x4144)
#define OP3ToOP5() bcall(0x4147)
#define OP2ToOP5() bcall(0x414A)
#define OP2ToOP6() bcall(0x414D)
#define OP1ToOP6() bcall(0x4150)
#define OP1ToOP5() bcall(0x4153)
#define OP2ToOP1() bcall(0x4156)
// easy default values
#define OP4Set1() bcall(0x4186)
#define OP3Set1() bcall(0x4189)
#define OP2Set8() bcall(0x418C)
#define OP2Set5() bcall(0x418F)
#define OP2SetA() bcall(0x4192)
#define OP2Set4() bcall(0x4195)
#define OP2Set3() bcall(0x4198)
#define OP1Set1() bcall(0x419B)
#define OP1Set4() bcall(0x419E)
#define OP1Set3() bcall(0x41A1)
#define OP3Set2() bcall(0x41A4)
#define OP1Set2() bcall(0x41A7)
#define OP2Set2() bcall(0x41AA)
#define OP2Set1() bcall(0x41AD)
#define Zero16D() bcall(0x41B0)
#define OP5Set0() bcall(0x41B3)
#define OP4Set0() bcall(0x41B6)
#define OP3Set0() bcall(0x41B9)
#define OP2Set0() bcall(0x41BC)
#define OP1Set0() bcall(0x41BF)
int OPT1_TO_INT(){ // if greater than 9999 errors
bcall(0x4AEF);
__asm
EX DE, HL
__endasm;
}
#define PRINT_OP1 __asm \
ld a, #6 __endasm;\
bcall(0x4BF7)
// Taken and moded from Xeda112358
// https://www.cemetech.net/forum/viewtopic.php?t=1449&postdays=0&postorder=asc&start=126
// DESTORYS ALL REG
void charToOp1(char x){
__asm
ld hl, #0 + 4
add hl, sp
ld a, (hl)
ld hl,op1
ld bc,#0x0700
ld (hl),c
inc hl
ld (hl),#0x83
ld d,h
ld e,l
inc hl
ld (hl),c
$00000:djnz $00000-2
or a
jp Z, EOFFF ;If A is zero, exit early. +227cc
ld l,a ;\
ld h,c ; |
add hl,hl ; |Start converting A to BCD
add hl,hl ; |
add hl,hl ; |
add hl,hl ; |
ld a,h
daa
rl l ; |Finish converting A to BCD
adc a,a
daa
rl l ; |Number is in LA
adc a,a
daa
rl l ; |
adc a,a
daa
rl l ; |
adc a,a
daa
rl l ;/ +132cc
ex de,hl
$00001:jr nz,$00001+6
ld e,a
xor a
ld (hl),#0x81 ;+(21+4/85)cc
inc hl
ld (hl),e
inc hl
ld (hl),a
ld a,e
and #0xF0
jp nz, EOFFF ;+48cc
rld ;\ Rotate up 1 digit
dec hl ; |
rld ; |
dec hl ; |
dec (hl) ; |Decrement exponent
EOFFF:
__endasm;
}
void intToOp1(int x){
__asm
ld hl, #0 + 4
add hl, sp
ld c, (hl)
inc hl
ld b, (hl)
push bc
; loads 128 to op1
ld a, #128
push af
inc sp
call _charToOp1
inc sp
__endasm;
DOUBLE_OP1();
OP1ToOP2();
// 256 to op2
// upper byte to op1
__asm
pop bc
push bc
ld a, b
push af
inc sp
call _charToOp1
inc sp
__endasm;
// op1 now upper byte
FP_MULT();
OP1ToOP2();
// add lower byte
__asm
pop bc
ld a, c
push af
inc sp
call _charToOp1
inc sp
__endasm;
FP_ADD();
}