1 | /*--------------------------------------------------------------------------------- |
---|
2 | $Id$ |
---|
3 | |
---|
4 | BIOS functions |
---|
5 | |
---|
6 | Copyright (C) 2005 |
---|
7 | Michael Noland (joat) |
---|
8 | Jason Rogers (dovoto) |
---|
9 | Dave Murphy (WinterMute) |
---|
10 | |
---|
11 | This software is provided 'as-is', without any express or implied |
---|
12 | warranty. In no event will the authors be held liable for any |
---|
13 | damages arising from the use of this software. |
---|
14 | |
---|
15 | Permission is granted to anyone to use this software for any |
---|
16 | purpose, including commercial applications, and to alter it and |
---|
17 | redistribute it freely, subject to the following restrictions: |
---|
18 | |
---|
19 | 1. The origin of this software must not be misrepresented; you |
---|
20 | must not claim that you wrote the original software. If you use |
---|
21 | this software in a product, an acknowledgment in the product |
---|
22 | documentation would be appreciated but is not required. |
---|
23 | 2. Altered source versions must be plainly marked as such, and |
---|
24 | must not be misrepresented as being the original software. |
---|
25 | 3. This notice may not be removed or altered from any source |
---|
26 | distribution. |
---|
27 | |
---|
28 | ---------------------------------------------------------------------------------*/ |
---|
29 | |
---|
30 | #ifndef BIOS_H_INCLUDE |
---|
31 | #define BIOS_H_INCLUDE |
---|
32 | |
---|
33 | /*! \file bios.h |
---|
34 | |
---|
35 | \brief Nintendo DS Bios functions |
---|
36 | */ |
---|
37 | |
---|
38 | |
---|
39 | #ifdef __cplusplus |
---|
40 | extern "C" { |
---|
41 | #endif |
---|
42 | |
---|
43 | |
---|
44 | #include <nds/jtypes.h> |
---|
45 | |
---|
46 | |
---|
47 | typedef struct DecompressionStream { |
---|
48 | int (*getSize)(uint8 * source, uint16 * dest, uint32 r2); |
---|
49 | int (*getResult)(uint8 * source); // can be NULL |
---|
50 | uint8 (*readByte)(uint8 * source); |
---|
51 | } PACKED TDecompressionStream; |
---|
52 | |
---|
53 | |
---|
54 | typedef struct UnpackStruct { |
---|
55 | uint16 sourceSize; // in bytes |
---|
56 | uint8 sourceWidth; |
---|
57 | uint8 destWidth; |
---|
58 | uint32 dataOffset; |
---|
59 | } PACKED TUnpackStruct, * PUnpackStruct; |
---|
60 | |
---|
61 | |
---|
62 | /*! \fn swiSoftReset(void) |
---|
63 | \brief reset the DS. |
---|
64 | |
---|
65 | */ |
---|
66 | void swiSoftReset(void); |
---|
67 | |
---|
68 | /*! \fn swiDelay( uint32 duration) |
---|
69 | \brief delay |
---|
70 | |
---|
71 | Delays for for a period X + Y*duration where X is the swi overhead and Y is a cycle of |
---|
72 | <CODE><PRE> |
---|
73 | loop: |
---|
74 | sub r0, #1 |
---|
75 | bgt loop |
---|
76 | </PRE></CODE> |
---|
77 | of thumb fetches in BIOS memory |
---|
78 | \param duration |
---|
79 | length of delay |
---|
80 | \note |
---|
81 | Duration should be 1 or more, a duration of 0 is a huge delay |
---|
82 | |
---|
83 | */ |
---|
84 | void swiDelay(uint32 duration); |
---|
85 | |
---|
86 | /*! \fn swiIntrWait(int waitForSet, uint32 flags) |
---|
87 | |
---|
88 | \brief wait for interrupt(s) to occur |
---|
89 | |
---|
90 | \param waitForSet |
---|
91 | 0: Return if the interrupt has already occured |
---|
92 | 1: Wait until the interrupt has been set since the call |
---|
93 | \param flags |
---|
94 | interrupt mask to wait for |
---|
95 | |
---|
96 | */ |
---|
97 | |
---|
98 | void swiIntrWait(int waitForSet, uint32 flags); |
---|
99 | |
---|
100 | /*! \fn swiWaitForVBlank(void) |
---|
101 | \brief Wait for vblank interrupt |
---|
102 | |
---|
103 | Waits for a vertical blank interrupt |
---|
104 | |
---|
105 | \note Identical to calling swiIntrWait(1, 1) |
---|
106 | */ |
---|
107 | |
---|
108 | void swiWaitForVBlank(void); |
---|
109 | |
---|
110 | /*! \fn int swiDivide(int numerator, int divisor) |
---|
111 | \param numerator |
---|
112 | signed integer to divide |
---|
113 | \param divisor |
---|
114 | signed integer to divide by |
---|
115 | \return numerator / divisor |
---|
116 | |
---|
117 | \fn int swiRemainder(int numerator, int divisor) |
---|
118 | \param numerator |
---|
119 | signed integer to divide |
---|
120 | \param divisor |
---|
121 | signed integer to divide by |
---|
122 | \return numerator % divisor |
---|
123 | |
---|
124 | \fn void swiDivMod(int numerator, int divisor, int * result, int * remainder) |
---|
125 | \param numerator |
---|
126 | signed integer to divide |
---|
127 | \param divisor |
---|
128 | signed integer to divide by |
---|
129 | \param result |
---|
130 | pointer to integer set to numerator / divisor |
---|
131 | \param remainder |
---|
132 | pointer to integer set to numerator % divisor |
---|
133 | */ |
---|
134 | |
---|
135 | int swiDivide(int numerator, int divisor); |
---|
136 | int swiRemainder(int numerator, int divisor); |
---|
137 | void swiDivMod(int numerator, int divisor, int * result, int * remainder); |
---|
138 | |
---|
139 | /*! \fn swiCopy(const void * source, void * dest, int flags) |
---|
140 | \param source |
---|
141 | pointer to transfer source |
---|
142 | \param dest |
---|
143 | dest = pointer to transfer destination |
---|
144 | \param flags |
---|
145 | copy mode and size |
---|
146 | // flags(26) = transfer width (0: halfwords, 1: words) |
---|
147 | // flags(24) = transfer mode (0: copy, 1: fill) |
---|
148 | // flags(20..0) = transfer count (always in words) |
---|
149 | |
---|
150 | \fn swiFastCopy (const void * source, void * dest, int flags) |
---|
151 | \param source |
---|
152 | pointer to transfer source |
---|
153 | \param dest |
---|
154 | dest = pointer to transfer destination |
---|
155 | \param flags |
---|
156 | copy mode and size |
---|
157 | // flags(24) = transfer mode (0: copy, 1: fill) |
---|
158 | // flags(20..0) = transfer count (in words) |
---|
159 | |
---|
160 | \note Transfers more quickly than swiCopy, but has higher interrupt latency |
---|
161 | */ |
---|
162 | |
---|
163 | #define COPY_MODE_HWORD (0) |
---|
164 | #define COPY_MODE_WORD (1<<26) |
---|
165 | #define COPY_MODE_COPY (0) |
---|
166 | #define COPY_MODE_FILL (1<<24) |
---|
167 | |
---|
168 | void swiCopy(const void * source, void * dest, int flags); |
---|
169 | void swiFastCopy(const void * source, void * dest, int flags); |
---|
170 | |
---|
171 | // swiSqrt (swi 0x0D) |
---|
172 | |
---|
173 | int swiSqrt(int value); |
---|
174 | |
---|
175 | // swiCRC16(uint16 crc, void * data, uint32 size) (swi 0x0E) |
---|
176 | // crc - starting CRC-16 |
---|
177 | // data - pointer to data (processed nibble by nibble) |
---|
178 | // size - size in bytes |
---|
179 | // |
---|
180 | // Returns the CRC-16 after the data has been processed |
---|
181 | |
---|
182 | uint16 swiCRC16(uint16 crc, void * data, uint32 size); |
---|
183 | |
---|
184 | // int swiIsDebugger(void) |
---|
185 | // Returns 0 if running on a debugger (8 MB of ram instead of 4 MB) |
---|
186 | // |
---|
187 | // Note: swi 0x0F on both CPUs |
---|
188 | |
---|
189 | int swiIsDebugger(void); |
---|
190 | |
---|
191 | // swiUnpackBits |
---|
192 | // source - Source address |
---|
193 | // destination - destination address (word aligned) |
---|
194 | // params - Unpack structure |
---|
195 | // uint16 sourceSize (in bytes) |
---|
196 | // uint8 sourceWidth (1,2,4,8) |
---|
197 | // uint8 destWidth (1,2,4,8,16,32) |
---|
198 | // uint32 dataOffset (bits 0..30 are added to all non-zero |
---|
199 | // destination writes, unless b31 is set, |
---|
200 | // which does it for zeros too) |
---|
201 | // |
---|
202 | // Unpack data stored multiple elements to a byte into a larger space |
---|
203 | // i.e. 8 elements per byte (i.e. b/w font), into 1 element per byte |
---|
204 | // |
---|
205 | // Note: swi 0x10 on both CPUs |
---|
206 | |
---|
207 | void swiUnpackBits(uint8 * source, uint32 * destination, PUnpackStruct params); |
---|
208 | |
---|
209 | // DecompressLZSSWram (swi 0x11) |
---|
210 | // source - pointer to a header word, followed by compressed data |
---|
211 | // word(31..8) = size of uncompressed data (in bytes) |
---|
212 | // word(7..0) = ignored |
---|
213 | // destination - destination address |
---|
214 | // Writes data a byte at a time |
---|
215 | // |
---|
216 | // DecompressLZSSVram (swi 0x12) |
---|
217 | // Writes data a halfword at a time |
---|
218 | // See DecompressRLEVram for parameter details |
---|
219 | |
---|
220 | void swiDecompressLZSSWram(void * source, void * destination); |
---|
221 | int swiDecompressLZSSVram(void * source, void * destination, uint32 toGetSize, TDecompressionStream * stream); |
---|
222 | |
---|
223 | // swiDecompressHuffman (swi 0x13) |
---|
224 | // See DecompressRLE for parameter details |
---|
225 | |
---|
226 | int swiDecompressHuffman(void * source, void * destination, uint32 toGetSize, TDecompressionStream * stream); |
---|
227 | |
---|
228 | // DecompressRLEWram (swi 0x14) |
---|
229 | // r0 - pointer to a header word, followed by compressed data |
---|
230 | // word(31..8) = size of uncompressed data (in bytes) |
---|
231 | // word(7..0) = ignored |
---|
232 | // r1 - destination address |
---|
233 | // Writes data a byte at a time |
---|
234 | // |
---|
235 | // DecompressRLEVram (swi 0x15) |
---|
236 | // r0 - pointer to compressed source data, maybe! (always goes through |
---|
237 | // the function pointers, so could just be an offset) |
---|
238 | // r1 - pointer to destination (halfword) |
---|
239 | // r2 - no idea, passed to getSize function |
---|
240 | // r3 - pointer to a structure |
---|
241 | // Writes data a halfword at a time |
---|
242 | // |
---|
243 | // Result of getSize is a word, with the size of decompressed data in |
---|
244 | // bits 31..8, and bits 7..0 are ignored. This value is also returned |
---|
245 | // by the swi, unless getResult is non-NULL and returns a neg. value |
---|
246 | // |
---|
247 | // getResult is used to provide a result for the swi, given the source |
---|
248 | // pointer after all data has been read (or if getSize < 0). It can |
---|
249 | // be NULL, unlike getSize and readByte. Its value is only returned |
---|
250 | // if negative, otherwise the typical result is used, so it is likely |
---|
251 | // some sort of error-checking procedure |
---|
252 | // |
---|
253 | // readByte is used in lieu of directly accessing memory |
---|
254 | // |
---|
255 | // compressed data format: |
---|
256 | // bit(7): 0: uncompressed, 1: compressed |
---|
257 | // bit(6..0) when uncompressed: run length - 1, followed by run_length bytes of true data |
---|
258 | // bit(6..0) when compressed: run length - 3, followed by one byte of true data, to be repeated |
---|
259 | |
---|
260 | void swiDecompressRLEWram(void * source, void * destination); |
---|
261 | int swiDecompressRLEVram(void * source, void * destination, uint32 toGetSize, TDecompressionStream * stream); |
---|
262 | |
---|
263 | // swiDecodeDelta8 |
---|
264 | // source - pointer to a header word, followed by encoded data |
---|
265 | // word(31..8) = size of data (in bytes) |
---|
266 | // word(7..0) = ignored |
---|
267 | // destination - destination address |
---|
268 | // Writes data a byte at a time |
---|
269 | // |
---|
270 | // Note: ARM9 exclusive swi 0x16 |
---|
271 | |
---|
272 | #ifdef ARM9 |
---|
273 | |
---|
274 | /*! \fn swiWaitForIRQ |
---|
275 | \brief wait for any interrupt |
---|
276 | |
---|
277 | \note ARM9 exclusive |
---|
278 | */ |
---|
279 | void swiWaitForIRQ(void); |
---|
280 | |
---|
281 | void swiSetHaltCR(uint32 data); |
---|
282 | |
---|
283 | |
---|
284 | extern void swiDecodeDelta8(void * source, void * destination); |
---|
285 | |
---|
286 | // swiDecodeDelta16 |
---|
287 | // source - pointer to a header word, followed by encoded data |
---|
288 | // word(31..8) = size of data (in bytes) |
---|
289 | // word(7..0) = ignored |
---|
290 | // destination - destination address |
---|
291 | // Writes data a halfword at a time |
---|
292 | // |
---|
293 | // Note: ARM9 exclusive swi 0x18 |
---|
294 | |
---|
295 | void swiDecodeDelta16(void * source, void * destination); |
---|
296 | #endif |
---|
297 | |
---|
298 | |
---|
299 | #ifdef ARM7 |
---|
300 | |
---|
301 | // |
---|
302 | // swiHalt (swi 0x06) |
---|
303 | // Same as swiSetHaltCR(0x80) |
---|
304 | // |
---|
305 | // swiSleep (swi 0x07) |
---|
306 | // Same as swiSetHaltCR(0xC0) |
---|
307 | // |
---|
308 | // swiSwitchToGBAMode (not a SWI) |
---|
309 | // Same as swiSetHaltCR(0x40) |
---|
310 | // |
---|
311 | // swiSetHaltCR (swi 0x1F) |
---|
312 | // Writes a byte of the data to 0x04000301:8 |
---|
313 | // |
---|
314 | // Note: All of these are ARM7 exclusive |
---|
315 | // |
---|
316 | // swiSetHaltCR (swi 0x1F) |
---|
317 | // Writes a word of the data to 0x04000300:32 |
---|
318 | // |
---|
319 | // Note: This is on the ARM9, but works differently to the ARM7 function! |
---|
320 | // |
---|
321 | |
---|
322 | void swiHalt(void); |
---|
323 | void swiSleep(void); |
---|
324 | void swiSwitchToGBAMode(void); |
---|
325 | void swiSetHaltCR(uint8 data); |
---|
326 | |
---|
327 | // swiGetSineTable(int index) |
---|
328 | // Returns an entry in the sine table (index = 0..63) |
---|
329 | // |
---|
330 | // Note: ARM7 exclusive swi 0x1A |
---|
331 | |
---|
332 | uint16 swiGetSineTable(int index); |
---|
333 | // |
---|
334 | // swiGetPitchTable(int index) |
---|
335 | // Returns an entry in the pitch table (index = 0..767) |
---|
336 | // |
---|
337 | // Note: ARM7 exclusive swi 0x1B |
---|
338 | |
---|
339 | uint16 swiGetPitchTable(int index); |
---|
340 | |
---|
341 | |
---|
342 | // swiGetVolumeTable(int index) |
---|
343 | // Returns an entry in the volume table (index = 0..723) |
---|
344 | // |
---|
345 | // Note: ARM7 exclusive swi 0x1C |
---|
346 | uint8 swiGetVolumeTable(int index); |
---|
347 | // swiChangeSoundBias(int enabled, int delay) |
---|
348 | // enabled |
---|
349 | // 0: decrement SOUND_BIAS once per delay until it reaches 0x000 |
---|
350 | // 1: increment SOUND_BIAS once per delay until it reaches 0x200 |
---|
351 | // delay is in the same units of time as swiDelay |
---|
352 | // |
---|
353 | // Note: ARM7 exclusive SWI 0x08 |
---|
354 | |
---|
355 | void swiChangeSoundBias(int enabled, int delay); |
---|
356 | |
---|
357 | |
---|
358 | #endif //ARM7 |
---|
359 | |
---|
360 | |
---|
361 | #ifdef __cplusplus |
---|
362 | } |
---|
363 | #endif |
---|
364 | |
---|
365 | |
---|
366 | #endif |
---|
367 | |
---|