From 991eb2357bcce79556afd0885364ef9c5b9c93b3 Mon Sep 17 00:00:00 2001 From: fincs Date: Thu, 16 Feb 2017 14:35:06 +0100 Subject: [PATCH] Add TickCounter for measuring performance --- libctru/include/3ds/os.h | 35 +++++++++++++++++++++++++++++++++++ libctru/source/os.c | 6 ++++++ 2 files changed, 41 insertions(+) diff --git a/libctru/include/3ds/os.h b/libctru/include/3ds/os.h index f3983ce..f4cc93f 100644 --- a/libctru/include/3ds/os.h +++ b/libctru/include/3ds/os.h @@ -3,6 +3,7 @@ * @brief OS related stuff. */ #pragma once +#include "svc.h" /// Packs a system version from its components. #define SYSTEM_VERSION(major, minor, revision) \ @@ -26,6 +27,13 @@ typedef enum MEMREGION_BASE = 3, ///< BASE memory. } MemRegion; +/// Tick counter. +typedef struct +{ + u64 elapsed; ///< Elapsed CPU ticks between measurements. + u64 reference; ///< Point in time used as reference. +} TickCounter; + /// OS_VersionBin. Format of the system version: "..-" typedef struct { @@ -124,6 +132,33 @@ static inline s64 osGetMemRegionFree(MemRegion region) */ u64 osGetTime(void); +/** + * @brief Starts a tick counter. + * @param cnt The tick counter. + */ +static inline void osTickCounterStart(TickCounter* cnt) +{ + cnt->reference = svcGetSystemTick(); +} + +/** + * @brief Updates the elapsed time in a tick counter. + * @param cnt The tick counter. + */ +static inline void osTickCounterUpdate(TickCounter* cnt) +{ + u64 now = svcGetSystemTick(); + cnt->elapsed = now - cnt->reference; + cnt->reference = now; +} + +/** + * @brief Reads the elapsed time in a tick counter. + * @param cnt The tick counter. + * @return The number of milliseconds elapsed. + */ +double osTickCounterRead(TickCounter* cnt); + /** * @brief Gets the current Wifi signal strength. * @return The current Wifi signal strength. diff --git a/libctru/source/os.c b/libctru/source/os.c index 60ef10b..5984281 100644 --- a/libctru/source/os.c +++ b/libctru/source/os.c @@ -115,6 +115,12 @@ u64 osGetTime(void) { return dt.date_time + (u32)(u64_to_double(delta)/TICKS_PER_MSEC); } +//--------------------------------------------------------------------------------- +double osTickCounterRead(TickCounter* cnt) { +//--------------------------------------------------------------------------------- + return u64_to_double(cnt->elapsed) / TICKS_PER_MSEC; +} + //--------------------------------------------------------------------------------- const char* osStrError(u32 error) { //---------------------------------------------------------------------------------