libctru  v1.6.0
os.h
Go to the documentation of this file.
1 /**
2  * @file os.h
3  * @brief OS related stuff.
4  */
5 #pragma once
6 #include "svc.h"
7 
8 #define SYSCLOCK_SOC (16756991)
9 #define SYSCLOCK_ARM9 (SYSCLOCK_SOC * 8)
10 #define SYSCLOCK_ARM11 (SYSCLOCK_ARM9 * 2)
11 #define SYSCLOCK_ARM11_NEW (SYSCLOCK_ARM11 * 3)
12 
13 #define CPU_TICKS_PER_MSEC (SYSCLOCK_ARM11 / 1000.0)
14 #define CPU_TICKS_PER_USEC (SYSCLOCK_ARM11 / 1000000.0)
15 
16 /// Packs a system version from its components.
17 #define SYSTEM_VERSION(major, minor, revision) \
18  (((major)<<24)|((minor)<<16)|((revision)<<8))
19 
20 /// Retrieves the major version from a packed system version.
21 #define GET_VERSION_MAJOR(version) ((version) >>24)
22 
23 /// Retrieves the minor version from a packed system version.
24 #define GET_VERSION_MINOR(version) (((version)>>16)&0xFF)
25 
26 /// Retrieves the revision version from a packed system version.
27 #define GET_VERSION_REVISION(version) (((version)>> 8)&0xFF)
28 
29 /// Memory regions.
30 typedef enum
31 {
32  MEMREGION_ALL = 0, ///< All regions.
33  MEMREGION_APPLICATION = 1, ///< APPLICATION memory.
34  MEMREGION_SYSTEM = 2, ///< SYSTEM memory.
35  MEMREGION_BASE = 3, ///< BASE memory.
36 } MemRegion;
37 
38 /// Tick counter.
39 typedef struct
40 {
41  u64 elapsed; ///< Elapsed CPU ticks between measurements.
42  u64 reference; ///< Point in time used as reference.
43 } TickCounter;
44 
45 /// OS_VersionBin. Format of the system version: "<major>.<minor>.<build>-<nupver><region>"
46 typedef struct
47 {
48  u8 build;
49  u8 minor;
50  u8 mainver;//"major" in CVER, NUP version in NVer.
51  u8 reserved_x3;
52  char region;//"ASCII character for the system version region"
53  u8 reserved_x5[0x3];
55 
56 /**
57  * @brief Converts an address from virtual (process) memory to physical memory.
58  * @param vaddr Input virtual address.
59  * @return The corresponding physical address.
60  * It is sometimes required by services or when using the GPU command buffer.
61  */
62 u32 osConvertVirtToPhys(const void* vaddr);
63 
64 /**
65  * @brief Converts 0x14* vmem to 0x30*.
66  * @param vaddr Input virtual address.
67  * @return The corresponding address in the 0x30* range, the input address if it's already within the new vmem, or 0 if it's outside of both ranges.
68  */
69 void* osConvertOldLINEARMemToNew(const void* vaddr);
70 
71 /**
72  * @brief Retrieves basic information about a service error.
73  * @param error Error to retrieve information about.
74  * @return A string containing a summary of an error.
75  *
76  * This can be used to get some details about an error returned by a service call.
77  */
78 const char* osStrError(u32 error);
79 
80 /**
81  * @brief Gets the system's FIRM version.
82  * @return The system's FIRM version.
83  *
84  * This can be used to compare system versions easily with @ref SYSTEM_VERSION.
85  */
86 static inline u32 osGetFirmVersion(void)
87 {
88  return (*(vu32*)0x1FF80060) & ~0xFF;
89 }
90 
91 /**
92  * @brief Gets the system's kernel version.
93  * @return The system's kernel version.
94  *
95  * This can be used to compare system versions easily with @ref SYSTEM_VERSION.
96  *
97  * @code
98  * if(osGetKernelVersion() > SYSTEM_VERSION(2,46,0)) printf("You are running 9.0 or higher\n");
99  * @endcode
100  */
101 static inline u32 osGetKernelVersion(void)
102 {
103  return (*(vu32*)0x1FF80000) & ~0xFF;
104 }
105 
106 /**
107  * @brief Gets the size of the specified memory region.
108  * @param region Memory region to check.
109  * @return The size of the memory region, in bytes.
110  */
111 static inline u32 osGetMemRegionSize(MemRegion region)
112 {
113  if(region == MEMREGION_ALL) {
115  } else {
116  return *(vu32*) (0x1FF80040 + (region - 1) * 0x4);
117  }
118 }
119 
120 /**
121  * @brief Gets the number of used bytes within the specified memory region.
122  * @param region Memory region to check.
123  * @return The number of used bytes of memory.
124  */
126 
127 /**
128  * @brief Gets the number of free bytes within the specified memory region.
129  * @param region Memory region to check.
130  * @return The number of free bytes of memory.
131  */
132 static inline s64 osGetMemRegionFree(MemRegion region)
133 {
134  return (s64) osGetMemRegionSize(region) - osGetMemRegionUsed(region);
135 }
136 
137 /**
138  * @brief Gets the current time.
139  * @return The number of milliseconds since 1st Jan 1900 00:00.
140  */
141 u64 osGetTime(void);
142 
143 /**
144  * @brief Starts a tick counter.
145  * @param cnt The tick counter.
146  */
147 static inline void osTickCounterStart(TickCounter* cnt)
148 {
149  cnt->reference = svcGetSystemTick();
150 }
151 
152 /**
153  * @brief Updates the elapsed time in a tick counter.
154  * @param cnt The tick counter.
155  */
156 static inline void osTickCounterUpdate(TickCounter* cnt)
157 {
158  u64 now = svcGetSystemTick();
159  cnt->elapsed = now - cnt->reference;
160  cnt->reference = now;
161 }
162 
163 /**
164  * @brief Reads the elapsed time in a tick counter.
165  * @param cnt The tick counter.
166  * @return The number of milliseconds elapsed.
167  */
168 double osTickCounterRead(const TickCounter* cnt);
169 
170 /**
171  * @brief Gets the current Wifi signal strength.
172  * @return The current Wifi signal strength.
173  *
174  * Valid values are 0-3:
175  * - 0 means the singal strength is terrible or the 3DS is disconnected from
176  * all networks.
177  * - 1 means the signal strength is bad.
178  * - 2 means the signal strength is decent.
179  * - 3 means the signal strength is good.
180  *
181  * Values outside the range of 0-3 should never be returned.
182  *
183  * These values correspond with the number of wifi bars displayed by Home Menu.
184  */
185 static inline u8 osGetWifiStrength(void)
186 {
187  return *(vu8*)0x1FF81066;
188 }
189 
190 /**
191  * @brief Gets the state of the 3D slider.
192  * @return The state of the 3D slider (0.0~1.0)
193  */
194 static inline float osGet3DSliderState(void)
195 {
196  return *(volatile float*)0x1FF81080;
197 }
198 
199 /**
200  * @brief Configures the New 3DS speedup.
201  * @param enable Specifies whether to enable or disable the speedup.
202  */
203 void osSetSpeedupEnable(bool enable);
204 
205 /**
206  * @brief Gets the NAND system-version stored in NVer/CVer.
207  * @param nver_versionbin Output OS_VersionBin structure for the data read from NVer.
208  * @param cver_versionbin Output OS_VersionBin structure for the data read from CVer.
209  * @return The result-code. This value can be positive if opening "romfs:/version.bin" fails with stdio, since errno would be returned in that case. In some cases the error can be special negative values as well.
210  */
211 Result osGetSystemVersionData(OS_VersionBin *nver_versionbin, OS_VersionBin *cver_versionbin);
212 
213 /**
214  * @brief This is a wrapper for osGetSystemVersionData.
215  * @param nver_versionbin Optional output OS_VersionBin structure for the data read from NVer, can be NULL.
216  * @param cver_versionbin Optional output OS_VersionBin structure for the data read from CVer, can be NULL.
217  * @param sysverstr Output string where the printed system-version will be written, in the same format displayed by the System Settings title.
218  * @param sysverstr_maxsize Max size of the above string buffer, *including* NULL-terminator.
219  * @return See osGetSystemVersionData.
220  */
221 Result osGetSystemVersionDataString(OS_VersionBin *nver_versionbin, OS_VersionBin *cver_versionbin, char *sysverstr, u32 sysverstr_maxsize);
u64 svcGetSystemTick(void)
Gets the current system tick.
APPLICATION memory.
Definition: os.h:33
volatile u32 vu32
32-bit volatile unsigned integer.
Definition: types.h:33
static u32 osGetKernelVersion(void)
Gets the system's kernel version.
Definition: os.h:101
s32 Result
Function result.
Definition: types.h:42
static u32 osGetMemRegionSize(MemRegion region)
Gets the size of the specified memory region.
Definition: os.h:111
void * osConvertOldLINEARMemToNew(const void *vaddr)
Converts 0x14* vmem to 0x30*.
Tick counter.
Definition: os.h:39
u64 osGetTime(void)
Gets the current time.
volatile u8 vu8
8-bit volatile unsigned integer.
Definition: types.h:31
SYSTEM memory.
Definition: os.h:34
BASE memory.
Definition: os.h:35
u32 osConvertVirtToPhys(const void *vaddr)
Converts an address from virtual (process) memory to physical memory.
double osTickCounterRead(const TickCounter *cnt)
Reads the elapsed time in a tick counter.
uint8_t u8
would be nice if newlib had this already
Definition: types.h:21
static float osGet3DSliderState(void)
Gets the state of the 3D slider.
Definition: os.h:194
uint64_t u64
64-bit unsigned integer
Definition: types.h:24
MemRegion
Memory regions.
Definition: os.h:30
uint32_t u32
32-bit unsigned integer
Definition: types.h:23
const char * osStrError(u32 error)
Retrieves basic information about a service error.
static u32 osGetFirmVersion(void)
Gets the system's FIRM version.
Definition: os.h:86
u64 reference
Point in time used as reference.
Definition: os.h:42
Result osGetSystemVersionDataString(OS_VersionBin *nver_versionbin, OS_VersionBin *cver_versionbin, char *sysverstr, u32 sysverstr_maxsize)
This is a wrapper for osGetSystemVersionData.
void osSetSpeedupEnable(bool enable)
Configures the New 3DS speedup.
OS_VersionBin. Format of the system version: "<major>.<minor>.<build>-<nupver><region>".
Definition: os.h:46
static void osTickCounterUpdate(TickCounter *cnt)
Updates the elapsed time in a tick counter.
Definition: os.h:156
s64 osGetMemRegionUsed(MemRegion region)
Gets the number of used bytes within the specified memory region.
All regions.
Definition: os.h:32
static s64 osGetMemRegionFree(MemRegion region)
Gets the number of free bytes within the specified memory region.
Definition: os.h:132
int64_t s64
64-bit signed integer
Definition: types.h:29
Result osGetSystemVersionData(OS_VersionBin *nver_versionbin, OS_VersionBin *cver_versionbin)
Gets the NAND system-version stored in NVer/CVer.
u64 elapsed
Elapsed CPU ticks between measurements.
Definition: os.h:41
Syscall wrappers.
static u8 osGetWifiStrength(void)
Gets the current Wifi signal strength.
Definition: os.h:185
static void osTickCounterStart(TickCounter *cnt)
Starts a tick counter.
Definition: os.h:147