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