-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils.c
262 lines (234 loc) · 10.9 KB
/
Utils.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
259
260
261
262
#include "Utils.h"
#include <string.h>
int MakeHSV(int h,int32_t s,int32_t v)
{
h%=360;
if(h<0) h+=360;
int h_int=h/60;
int f=ifrac(IntToFixed(h)/60);
int p=imul(v,IntToFixed(1)-s);
int q=imul(v,IntToFixed(1)-imul(f,s));
int t=imul(v,IntToFixed(1)-s+imul(f,s));
int r,g,b;
switch(h_int)
{
case 0: r=v; g=t; b=p; break;
case 1: r=q; g=v; b=p; break;
case 2: r=p; g=v; b=t; break;
case 3: r=p; g=q; b=v; break;
case 4: r=t; g=p; b=v; break;
case 5: r=v; g=p; b=q; break;
}
if(r>=IntToFixed(1)) r=0x1f;
else r>>=7;
if(g>=IntToFixed(1)) g=0x1f;
else g>>=7;
if(b>=IntToFixed(1)) b=0x1f;
else b>>=7;
return MakeRGB15(r,g,b);
}
static uint32_t state1=1234567890,state2=987654321,state3=1010101010;
uint32_t Random()
{
state1=((state1&4294967294)<<12)^(((state1<<13)^state1)>>19);
state2=((state2&4294967288)<<4)^(((state2<<2)^state2)>>25);
state3=((state3&4294967280)<<17)^(((state3<<3)^state3)>>11);
return state1^state2^state3;
}
void SaveDisplayRegisters(DisplayRegisters *regs)
{
memcpy(regs->a,(void *)&DISPCNT_A,64);
memcpy(regs->b,(void *)&DISPCNT_B,64);
/* *((uint32_t *)regs->a)=DISPCNT_A;
*((uint32_t *)regs->b)=DISPCNT_B;
for(int i=2;i<32;i++) regs->a[i]=((uint16_t *)&DISPCNT_A)[i];
for(int i=2;i<32;i++) regs->b[i]=((uint16_t *)&DISPCNT_B)[i];*/
}
void RestoreDisplayRegisters(DisplayRegisters *regs)
{
memcpy((void *)&DISPCNT_A,regs->a,64);
memcpy((void *)&DISPCNT_B,regs->b,64);
/* DISPCNT_A=*((uint32_t *)regs->a);
DISPCNT_B=*((uint32_t *)regs->b);
for(int i=2;i<32;i++) ((uint16_t *)&DISPCNT_A)[i]=regs->a[i];
for(int i=2;i<32;i++) ((uint16_t *)&DISPCNT_B)[i]=regs->b[i];*/
}
void SavePalettes(Palettes *pals)
{
memcpy(pals->a,PALRAM_A,512);
memcpy(pals->b,PALRAM_B,512);
}
void RestorePalettes(Palettes *pals)
{
memcpy(PALRAM_A,pals->a,512);
memcpy(PALRAM_B,pals->b,512);
}
uint32_t SaveAndSetMemoryBanks(int bank_a,int bank_b,int bank_c,int bank_d)
{
uint32_t saved=(VRAMCNT_A)|(VRAMCNT_B<<8)|(VRAMCNT_C<<24)|(VRAMCNT_D<<24);
VRAMCNT_A=bank_a;
VRAMCNT_B=bank_b;
VRAMCNT_C=bank_c;
VRAMCNT_D=bank_d;
}
void RestoreMemoryBanks(uint32_t savedbanks)
{
VRAMCNT_A=(savedbanks)&0xff;
VRAMCNT_B=(savedbanks>>8)&0xff;
VRAMCNT_C=(savedbanks>>16)&0xff;
VRAMCNT_D=(savedbanks>>24)&0xff;
}
void SetupEngineBSpriteScreen()
{
int n=0;
for(int y=0;y<3;y++)
for(int x=0;x<4;x++)
{
OAM_B[4*n+0]=OBJ_0_Y(y*64)|OBJ_0_BITMAP|OBJ_0_SIZE_64x64;
OAM_B[4*n+1]=OBJ_1_X(x*64)|OBJ_1_SIZE_64x64;
OAM_B[4*n+2]=OBJ_2_CHAR(x*8+y*4*64)|OBJ_2_ALPHA(0xf);
n++;
}
}
int32_t isin(int a)
{
const static uint16_t table[1025]=
{
0/8,50/8,100/8,150/8,200/8,250/8,300/8,350/8,
401/8,451/8,501/8,551/8,601/8,651/8,701/8,751/8,
801/8,852/8,902/8,952/8,1002/8,1052/8,1102/8,1152/8,
1202/8,1252/8,1302/8,1353/8,1403/8,1453/8,1503/8,1553/8,
1603/8,1653/8,1703/8,1753/8,1803/8,1853/8,1903/8,1953/8,
2003/8,2053/8,2103/8,2153/8,2203/8,2253/8,2303/8,2353/8,
2403/8,2453/8,2503/8,2553/8,2603/8,2653/8,2703/8,2753/8,
2803/8,2853/8,2903/8,2953/8,3003/8,3053/8,3103/8,3153/8,
3203/8,3252/8,3302/8,3352/8,3402/8,3452/8,3502/8,3552/8,
3601/8,3651/8,3701/8,3751/8,3801/8,3850/8,3900/8,3950/8,
4000/8,4049/8,4099/8,4149/8,4199/8,4248/8,4298/8,4348/8,
4397/8,4447/8,4497/8,4546/8,4596/8,4646/8,4695/8,4745/8,
4794/8,4844/8,4894/8,4943/8,4993/8,5042/8,5092/8,5141/8,
5191/8,5240/8,5290/8,5339/8,5389/8,5438/8,5487/8,5537/8,
5586/8,5636/8,5685/8,5734/8,5784/8,5833/8,5882/8,5932/8,
5981/8,6030/8,6079/8,6129/8,6178/8,6227/8,6276/8,6325/8,
6375/8,6424/8,6473/8,6522/8,6571/8,6620/8,6669/8,6718/8,
6767/8,6817/8,6866/8,6915/8,6964/8,7012/8,7061/8,7110/8,
7159/8,7208/8,7257/8,7306/8,7355/8,7404/8,7452/8,7501/8,
7550/8,7599/8,7648/8,7696/8,7745/8,7794/8,7842/8,7891/8,
7940/8,7988/8,8037/8,8085/8,8134/8,8182/8,8231/8,8280/8,
8328/8,8376/8,8425/8,8473/8,8522/8,8570/8,8618/8,8667/8,
8715/8,8763/8,8812/8,8860/8,8908/8,8956/8,9005/8,9053/8,
9101/8,9149/8,9197/8,9245/8,9293/8,9341/8,9389/8,9437/8,
9485/8,9533/8,9581/8,9629/8,9677/8,9725/8,9773/8,9821/8,
9868/8,9916/8,9964/8,10012/8,10059/8,10107/8,10155/8,10202/8,
10250/8,10298/8,10345/8,10393/8,10440/8,10488/8,10535/8,10583/8,
10630/8,10677/8,10725/8,10772/8,10819/8,10867/8,10914/8,10961/8,
11008/8,11056/8,11103/8,11150/8,11197/8,11244/8,11291/8,11338/8,
11385/8,11432/8,11479/8,11526/8,11573/8,11620/8,11667/8,11713/8,
11760/8,11807/8,11854/8,11900/8,11947/8,11994/8,12040/8,12087/8,
12133/8,12180/8,12226/8,12273/8,12319/8,12366/8,12412/8,12459/8,
12505/8,12551/8,12597/8,12644/8,12690/8,12736/8,12782/8,12828/8,
12874/8,12920/8,12966/8,13012/8,13058/8,13104/8,13150/8,13196/8,
13242/8,13288/8,13334/8,13379/8,13425/8,13471/8,13516/8,13562/8,
13608/8,13653/8,13699/8,13744/8,13790/8,13835/8,13880/8,13926/8,
13971/8,14016/8,14062/8,14107/8,14152/8,14197/8,14242/8,14288/8,
14333/8,14378/8,14423/8,14468/8,14513/8,14557/8,14602/8,14647/8,
14692/8,14737/8,14781/8,14826/8,14871/8,14915/8,14960/8,15004/8,
15049/8,15093/8,15138/8,15182/8,15227/8,15271/8,15315/8,15360/8,
15404/8,15448/8,15492/8,15536/8,15580/8,15624/8,15668/8,15712/8,
15756/8,15800/8,15844/8,15888/8,15932/8,15975/8,16019/8,16063/8,
16106/8,16150/8,16194/8,16237/8,16281/8,16324/8,16367/8,16411/8,
16454/8,16497/8,16541/8,16584/8,16627/8,16670/8,16713/8,16756/8,
16799/8,16842/8,16885/8,16928/8,16971/8,17014/8,17057/8,17099/8,
17142/8,17185/8,17227/8,17270/8,17312/8,17355/8,17397/8,17440/8,
17482/8,17524/8,17567/8,17609/8,17651/8,17693/8,17736/8,17778/8,
17820/8,17862/8,17904/8,17945/8,17987/8,18029/8,18071/8,18113/8,
18154/8,18196/8,18238/8,18279/8,18321/8,18362/8,18404/8,18445/8,
18486/8,18528/8,18569/8,18610/8,18651/8,18693/8,18734/8,18775/8,
18816/8,18857/8,18898/8,18939/8,18979/8,19020/8,19061/8,19102/8,
19142/8,19183/8,19223/8,19264/8,19304/8,19345/8,19385/8,19425/8,
19466/8,19506/8,19546/8,19586/8,19626/8,19667/8,19707/8,19746/8,
19786/8,19826/8,19866/8,19906/8,19946/8,19985/8,20025/8,20064/8,
20104/8,20144/8,20183/8,20222/8,20262/8,20301/8,20340/8,20379/8,
20419/8,20458/8,20497/8,20536/8,20575/8,20614/8,20653/8,20691/8,
20730/8,20769/8,20808/8,20846/8,20885/8,20923/8,20962/8,21000/8,
21039/8,21077/8,21115/8,21153/8,21192/8,21230/8,21268/8,21306/8,
21344/8,21382/8,21420/8,21458/8,21495/8,21533/8,21571/8,21608/8,
21646/8,21683/8,21721/8,21758/8,21796/8,21833/8,21870/8,21908/8,
21945/8,21982/8,22019/8,22056/8,22093/8,22130/8,22167/8,22203/8,
22240/8,22277/8,22314/8,22350/8,22387/8,22423/8,22460/8,22496/8,
22532/8,22569/8,22605/8,22641/8,22677/8,22713/8,22749/8,22785/8,
22821/8,22857/8,22893/8,22928/8,22964/8,23000/8,23035/8,23071/8,
23106/8,23142/8,23177/8,23212/8,23248/8,23283/8,23318/8,23353/8,
23388/8,23423/8,23458/8,23493/8,23528/8,23563/8,23597/8,23632/8,
23666/8,23701/8,23735/8,23770/8,23804/8,23839/8,23873/8,23907/8,
23941/8,23975/8,24009/8,24043/8,24077/8,24111/8,24145/8,24179/8,
24212/8,24246/8,24280/8,24313/8,24346/8,24380/8,24413/8,24447/8,
24480/8,24513/8,24546/8,24579/8,24612/8,24645/8,24678/8,24711/8,
24744/8,24776/8,24809/8,24842/8,24874/8,24907/8,24939/8,24971/8,
25004/8,25036/8,25068/8,25100/8,25132/8,25164/8,25196/8,25228/8,
25260/8,25292/8,25323/8,25355/8,25387/8,25418/8,25450/8,25481/8,
25512/8,25544/8,25575/8,25606/8,25637/8,25668/8,25699/8,25730/8,
25761/8,25792/8,25823/8,25853/8,25884/8,25915/8,25945/8,25975/8,
26006/8,26036/8,26066/8,26097/8,26127/8,26157/8,26187/8,26217/8,
26247/8,26277/8,26306/8,26336/8,26366/8,26395/8,26425/8,26454/8,
26484/8,26513/8,26542/8,26571/8,26601/8,26630/8,26659/8,26688/8,
26717/8,26745/8,26774/8,26803/8,26831/8,26860/8,26889/8,26917/8,
26945/8,26974/8,27002/8,27030/8,27058/8,27086/8,27114/8,27142/8,
27170/8,27198/8,27226/8,27254/8,27281/8,27309/8,27336/8,27364/8,
27391/8,27418/8,27446/8,27473/8,27500/8,27527/8,27554/8,27581/8,
27608/8,27634/8,27661/8,27688/8,27714/8,27741/8,27767/8,27794/8,
27820/8,27846/8,27873/8,27899/8,27925/8,27951/8,27977/8,28003/8,
28028/8,28054/8,28080/8,28105/8,28131/8,28156/8,28182/8,28207/8,
28232/8,28258/8,28283/8,28308/8,28333/8,28358/8,28383/8,28407/8,
28432/8,28457/8,28481/8,28506/8,28530/8,28555/8,28579/8,28604/8,
28628/8,28652/8,28676/8,28700/8,28724/8,28748/8,28772/8,28795/8,
28819/8,28843/8,28866/8,28890/8,28913/8,28936/8,28959/8,28983/8,
29006/8,29029/8,29052/8,29075/8,29098/8,29120/8,29143/8,29166/8,
29188/8,29211/8,29233/8,29256/8,29278/8,29300/8,29322/8,29344/8,
29366/8,29388/8,29410/8,29432/8,29454/8,29475/8,29497/8,29519/8,
29540/8,29561/8,29583/8,29604/8,29625/8,29646/8,29667/8,29688/8,
29709/8,29730/8,29751/8,29772/8,29792/8,29813/8,29833/8,29854/8,
29874/8,29894/8,29915/8,29935/8,29955/8,29975/8,29995/8,30015/8,
30034/8,30054/8,30074/8,30093/8,30113/8,30132/8,30152/8,30171/8,
30190/8,30209/8,30228/8,30247/8,30266/8,30285/8,30304/8,30323/8,
30341/8,30360/8,30378/8,30397/8,30415/8,30433/8,30452/8,30470/8,
30488/8,30506/8,30524/8,30542/8,30559/8,30577/8,30595/8,30612/8,
30630/8,30647/8,30665/8,30682/8,30699/8,30716/8,30733/8,30750/8,
30767/8,30784/8,30801/8,30818/8,30834/8,30851/8,30867/8,30884/8,
30900/8,30916/8,30933/8,30949/8,30965/8,30981/8,30997/8,31012/8,
31028/8,31044/8,31059/8,31075/8,31090/8,31106/8,31121/8,31136/8,
31152/8,31167/8,31182/8,31197/8,31212/8,31226/8,31241/8,31256/8,
31270/8,31285/8,31299/8,31314/8,31328/8,31342/8,31356/8,31370/8,
31384/8,31398/8,31412/8,31426/8,31440/8,31453/8,31467/8,31480/8,
31494/8,31507/8,31520/8,31534/8,31547/8,31560/8,31573/8,31586/8,
31598/8,31611/8,31624/8,31636/8,31649/8,31661/8,31674/8,31686/8,
31698/8,31710/8,31722/8,31734/8,31746/8,31758/8,31770/8,31782/8,
31793/8,31805/8,31816/8,31828/8,31839/8,31850/8,31861/8,31872/8,
31883/8,31894/8,31905/8,31916/8,31927/8,31937/8,31948/8,31959/8,
31969/8,31979/8,31990/8,32000/8,32010/8,32020/8,32030/8,32040/8,
32050/8,32059/8,32069/8,32079/8,32088/8,32098/8,32107/8,32116/8,
32125/8,32135/8,32144/8,32153/8,32162/8,32170/8,32179/8,32188/8,
32196/8,32205/8,32213/8,32222/8,32230/8,32238/8,32246/8,32255/8,
32263/8,32270/8,32278/8,32286/8,32294/8,32301/8,32309/8,32316/8,
32324/8,32331/8,32338/8,32346/8,32353/8,32360/8,32367/8,32373/8,
32380/8,32387/8,32394/8,32400/8,32407/8,32413/8,32419/8,32426/8,
32432/8,32438/8,32444/8,32450/8,32456/8,32461/8,32467/8,32473/8,
32478/8,32484/8,32489/8,32495/8,32500/8,32505/8,32510/8,32515/8,
32520/8,32525/8,32530/8,32535/8,32539/8,32544/8,32548/8,32553/8,
32557/8,32561/8,32565/8,32570/8,32574/8,32578/8,32581/8,32585/8,
32589/8,32593/8,32596/8,32600/8,32603/8,32606/8,32610/8,32613/8,
32616/8,32619/8,32622/8,32625/8,32628/8,32630/8,32633/8,32636/8,
32638/8,32641/8,32643/8,32645/8,32647/8,32649/8,32652/8,32653/8,
32655/8,32657/8,32659/8,32661/8,32662/8,32664/8,32665/8,32666/8,
32668/8,32669/8,32670/8,32671/8,32672/8,32673/8,32674/8,32674/8,
32675/8,32676/8,32676/8,32677/8,32677/8,32677/8,32677/8,32677/8,
4095
};
switch((a>>10)&3)
{
case 0: return table[a&1023];
case 1: return table[1024-(a&1023)];
case 2: return -table[a&1023];
case 3: return -table[1024-(a&1023)];
}
}