summaryrefslogtreecommitdiff
path: root/arm9/lib
diff options
context:
space:
mode:
Diffstat (limited to 'arm9/lib')
-rw-r--r--arm9/lib/include/OS_interrupt.h4
-rw-r--r--arm9/lib/include/OS_tick.h9
-rw-r--r--arm9/lib/include/OS_timer.h32
-rw-r--r--arm9/lib/include/registers.h6
-rw-r--r--arm9/lib/src/OS_tick.c70
5 files changed, 120 insertions, 1 deletions
diff --git a/arm9/lib/include/OS_interrupt.h b/arm9/lib/include/OS_interrupt.h
index ec58a636..481e6c6c 100644
--- a/arm9/lib/include/OS_interrupt.h
+++ b/arm9/lib/include/OS_interrupt.h
@@ -1,9 +1,11 @@
#ifndef POKEDIAMOND_ARM9_OS_INTERRUPT_H
#define POKEDIAMOND_ARM9_OS_INTERRUPT_H
-#include "nitro/types.h"
+#include "consts.h"
#include "nitro/OS_interrupt_shared.h"
+#define OS_IE_TIMER0 (1UL << REG_OS_IE_T0_SHIFT)
+
typedef void (*OSIrqFunction) (void);
typedef struct
diff --git a/arm9/lib/include/OS_tick.h b/arm9/lib/include/OS_tick.h
index f1c7145d..3dba9818 100644
--- a/arm9/lib/include/OS_tick.h
+++ b/arm9/lib/include/OS_tick.h
@@ -2,6 +2,7 @@
#define POKEDIAMOND_OS_TICK_H
#include "consts.h"
+#include "OS_timer.h"
typedef u64 OSTick;
@@ -9,4 +10,12 @@ typedef u64 OSTick;
#define OS_MilliSecondsToTicks(msec) ((OSTick)(((OS_SYSTEM_CLOCK/1000) * (u64)(msec)) / 64))
+#define OSi_TICK_TIMERCONTROL (REG_OS_TM0CNT_H_E_MASK | REG_OS_TM0CNT_H_I_MASK | OS_TIMER_PRESCALER_64)
+
+void OS_InitTick(void);
+BOOL OS_IsTickAvailable(void);
+static void OSi_CountUpTick(void);
+OSTick OS_GetTick(void);
+u16 OS_GetTickLo(void);
+
#endif //POKEDIAMOND_OS_TICK_H
diff --git a/arm9/lib/include/OS_timer.h b/arm9/lib/include/OS_timer.h
new file mode 100644
index 00000000..8b2a97a9
--- /dev/null
+++ b/arm9/lib/include/OS_timer.h
@@ -0,0 +1,32 @@
+#ifndef POKEDIAMOND_OS_TIMER_H
+#define POKEDIAMOND_OS_TIMER_H
+
+#include "consts.h"
+
+typedef enum
+{
+ OS_TIMER_PRESCALER_1 = (0UL << REG_OS_TM0CNT_H_PS_SHIFT),
+ OS_TIMER_PRESCALER_64 = (1UL << REG_OS_TM0CNT_H_PS_SHIFT),
+ OS_TIMER_PRESCALER_256 = (2UL << REG_OS_TM0CNT_H_PS_SHIFT),
+ OS_TIMER_PRESCALER_1024 = (3UL << REG_OS_TM0CNT_H_PS_SHIFT)
+} OSTimerPrescaler;
+
+typedef enum
+{
+ OS_TIMER_0 = 0,
+ OS_TIMER_1 = 1,
+ OS_TIMER_2 = 2,
+ OS_TIMER_3 = 3
+} OSTimer;
+
+static inline void OS_SetTimerCount(OSTimer id, u16 count)
+{
+ *((REGType16 *)((u32)&reg_OS_TM0CNT_L + id * 4)) = count;
+}
+
+static inline void OS_SetTimerControl(OSTimer id, u16 control)
+{
+ *((REGType16 *)((u32)&reg_OS_TM0CNT_H + id * 4)) = control;
+}
+
+#endif //POKEDIAMOND_OS_TIMER_H
diff --git a/arm9/lib/include/registers.h b/arm9/lib/include/registers.h
index 4781ba1b..06f96d5a 100644
--- a/arm9/lib/include/registers.h
+++ b/arm9/lib/include/registers.h
@@ -356,6 +356,12 @@
#define reg_MI_MCD1 (*(REGType32v *)0x4100010)
#define reg_CARD_DATA (*(REGType32v *)0x4100010) //?
+#define REG_OS_TM0CNT_H_PS_SHIFT 0
+#define REG_OS_IE_T0_SHIFT 3
+
+#define REG_OS_TM0CNT_H_I_MASK 0x0040
+#define REG_OS_TM0CNT_H_E_MASK 0x0080
+
#define REG_PAD_KEYINPUT_L_SHIFT 9
#define REG_PAD_KEYINPUT_L_SIZE 1
#define REG_PAD_KEYINPUT_L_MASK 0x0200
diff --git a/arm9/lib/src/OS_tick.c b/arm9/lib/src/OS_tick.c
new file mode 100644
index 00000000..f5f2df30
--- /dev/null
+++ b/arm9/lib/src/OS_tick.c
@@ -0,0 +1,70 @@
+#include "function_target.h"
+#include "OS_tick.h"
+#include "OS_interrupt.h"
+#include "OS_timer.h"
+#include "OS_system.h"
+
+static u16 OSi_UseTick = FALSE;
+vu64 OSi_TickCounter;
+BOOL OSi_NeedResetTimer = FALSE;
+
+extern void OSi_SetTimerReserved(u32 param1);
+
+ARM_FUNC void OS_InitTick(void)
+{
+ if (OSi_UseTick)
+ {
+ return;
+ }
+ OSi_UseTick = 1;
+ OSi_SetTimerReserved(0);
+ OSi_TickCounter = 0;
+ reg_OS_TM0CNT_H = 0;
+ reg_OS_TM0CNT_L = 0;
+ reg_OS_TM0CNT_H = 0xc1;
+ OS_SetIrqFunction(8, OSi_CountUpTick);
+ (void)OS_EnableIrqMask(8);
+ OSi_NeedResetTimer = FALSE;
+}
+
+ARM_FUNC BOOL OS_IsTickAvailable(void)
+{
+ return OSi_UseTick;
+}
+
+ARM_FUNC static void OSi_CountUpTick(void)
+{
+ OSi_TickCounter++;
+
+ if (OSi_NeedResetTimer)
+ {
+ OS_SetTimerControl(OS_TIMER_0, 0);
+ OS_SetTimerCount(OS_TIMER_0, 0);
+ OS_SetTimerControl(OS_TIMER_0, OSi_TICK_TIMERCONTROL);
+
+ OSi_NeedResetTimer = FALSE;
+ }
+
+ OSi_EnterTimerCallback(OS_TIMER_0, (void (*)(void *))OSi_CountUpTick, 0);
+}
+
+ARM_FUNC OSTick OS_GetTick(void)
+{
+ OSIntrMode prev = OS_DisableInterrupts();
+ vu16 countL = *(REGType16 *)((u32)&reg_OS_TM0CNT_L + OS_TIMER_0 * 4);
+ vu64 countH = OSi_TickCounter & 0xffffffffffffULL;
+
+ if (reg_OS_IF & OS_IE_TIMER0 && !(countL & 0x8000))
+ {
+ countH++;
+ }
+
+ (void)OS_RestoreInterrupts(prev);
+
+ return (countH << 16) | countL;
+}
+
+ARM_FUNC u16 OS_GetTickLo(void)
+{
+ return reg_OS_TM0CNT_L;
+}