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