minor cleanup
[pcsx_rearmed.git] / frontend / 3ds / 3ds_utils.h
1 #ifndef _3DS_UTILS_H
2 #define _3DS_UTILS_H
3
4 #include <stdio.h>
5 #include <stdbool.h>
6 #include <3ds.h>
7
8 #ifdef OS_HEAP_AREA_BEGIN // defined in libctru 2.0+
9 #define USE_CTRULIB_2 1
10 #endif
11
12 #define MEMOP_PROT      6
13 #define MEMOP_MAP       4
14 #define MEMOP_UNMAP     5
15
16 #define DEBUG_HOLD() do{printf("%s@%s:%d.\n",__FUNCTION__, __FILE__, __LINE__);fflush(stdout);wait_for_input();}while(0)
17
18 void wait_for_input(void);
19
20 extern __attribute__((weak)) int  __ctr_svchax;
21
22 static bool has_rosalina;
23
24 static inline void check_rosalina() {
25   int64_t version;
26   uint32_t major;
27
28   has_rosalina = false;
29
30   if (!svcGetSystemInfo(&version, 0x10000, 0)) {
31      major = GET_VERSION_MAJOR(version);
32
33      if (major >= 8)
34        has_rosalina = true;
35   }
36 }
37
38 void ctr_clear_cache(void);
39
40 typedef int32_t (*ctr_callback_type)(void);
41
42 static inline void ctr_invalidate_ICache_kernel(void)
43 {
44    __asm__ volatile(
45       "cpsid aif\n\t"
46       "mov r0, #0\n\t"
47       "mcr p15, 0, r0, c7, c5, 0\n\t");
48 }
49
50 static inline void ctr_flush_DCache_kernel(void)
51 {
52    __asm__ volatile(
53       "cpsid aif\n\t"
54       "mov r0, #0\n\t"
55       "mcr p15, 0, r0, c7, c10, 0\n\t");
56 }
57
58 static inline void ctr_invalidate_ICache(void)
59 {
60    svcBackdoor((ctr_callback_type)ctr_invalidate_ICache_kernel);
61 }
62
63 static inline void ctr_flush_DCache(void)
64 {
65    svcBackdoor((ctr_callback_type)ctr_flush_DCache_kernel);
66 }
67
68 static inline void ctr_flush_invalidate_cache(void)
69 {
70    if (has_rosalina) {
71       ctr_clear_cache();
72    } else {
73       ctr_flush_DCache();
74       ctr_invalidate_ICache();
75    }
76 }
77
78 #endif // _3DS_UTILS_H