2e5f0133fdc09bf79c2389aff7d13df603e2bf38
[gbajam22] / src / util.h
1 #ifndef UTIL_H_
2 #define UTIL_H_
3
4 #include <stdlib.h>
5 #include <stdint.h>
6 #include "gba.h"
7
8 #ifdef BUILD_GBA
9
10 #define wait_vblank() \
11         do { \
12                 while(REG_DISPSTAT & DISPSTAT_VBLANK); \
13                 while(!(REG_DISPSTAT & DISPSTAT_VBLANK)); \
14         } while(0)
15
16 #define present(x) \
17         do { \
18                 REG_DISPCNT = DISPCNT_BG2 | DISPCNT_OBJ | 4 | ((x) << 4); \
19         } while(0)
20
21 #define ARM_IWRAM       __attribute__((noinline, target("arm"), section(".iwram")))
22
23 #else   /* non-GBA build */
24 #define wait_vblank()
25
26 void present(int buf);          /* defined in src/pc/main.c */
27
28 #define ARM_IWRAM
29 #endif
30
31 #define set_bg_color(idx, r, g, b) \
32         gba_bgpal[idx] = (uint16_t)(r) | ((uint16_t)(g) << 5) | ((uint16_t)(b) << 10)
33
34 extern int16_t sinlut[];
35
36 #define SINLUT_BITS             8
37 #define SINLUT_SIZE             (1 << SINLUT_BITS)
38
39 #define SIN(angle) \
40         ((int32_t)sinlut[((angle) >> (16 - SINLUT_BITS)) & (SINLUT_SIZE - 1)] << 1)
41
42 #define COS(angle) \
43         ((int32_t)sinlut[(((angle) >> (16 - SINLUT_BITS)) + (SINLUT_SIZE / 4)) & (SINLUT_SIZE - 1)] << 1)
44
45 int iwram_brk(void *addr);
46 void *iwram_sbrk(intptr_t delta);
47
48 void fillblock_16byte(void *dest, uint32_t val, int count);
49
50 void *get_pc(void);
51 void *get_sp(void);
52
53 int ispow2(unsigned int x);
54
55 /* Non-failing versions of malloc/calloc/realloc. They never return 0, they call
56  * demo_abort on failure. Use the macros, don't call the *_impl functions.
57  */
58 #define malloc_nf(sz)   malloc_nf_impl(sz, __FILE__, __LINE__)
59 void *malloc_nf_impl(size_t sz, const char *file, int line);
60 #define calloc_nf(n, sz)        calloc_nf_impl(n, sz, __FILE__, __LINE__)
61 void *calloc_nf_impl(size_t num, size_t sz, const char *file, int line);
62 #define realloc_nf(p, sz)       realloc_nf_impl(p, sz, __FILE__, __LINE__)
63 void *realloc_nf_impl(void *p, size_t sz, const char *file, int line);
64 #define strdup_nf(s)    strdup_nf_impl(s, __FILE__, __LINE__)
65 char *strdup_nf_impl(const char *s, const char *file, int line);
66
67
68 #endif  /* UTIL_H_ */