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