diff options
author | Revo <projectrevotpp@hotmail.com> | 2020-06-25 01:30:36 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-25 01:30:36 -0400 |
commit | 40c3f6e85a2d25d3d98b2cfdaf1dd6872bbb4a6d (patch) | |
tree | a3b78fd54d27e7ead3c0ec3ff7e62ca87909a8e0 | |
parent | ce09e79eb9ca2b384fb1c64f0946661b30ae75ea (diff) | |
parent | e34791ca4a781fc37717ac0ea7c2ac9fd5d65c2e (diff) |
Merge pull request #188 from Cleverking2003/master
OS_tick
-rw-r--r-- | arm7/asm/OS_tick.s | 152 | ||||
-rw-r--r-- | arm7/global.inc | 2 | ||||
-rw-r--r-- | arm7/lib/include/OS_tick.h | 11 | ||||
-rw-r--r-- | arm7/lib/include/OS_timer.h | 6 | ||||
-rw-r--r-- | arm7/lib/src/OS_tick.c | 54 | ||||
-rw-r--r-- | arm9/lib/include/OS_timer.h | 28 | ||||
-rw-r--r-- | arm9/lib/include/registers.h | 10 | ||||
-rw-r--r-- | include/nitro/OS_timer_shared.h | 32 | ||||
-rw-r--r-- | include/nitro/registers_shared.h | 11 |
9 files changed, 116 insertions, 190 deletions
diff --git a/arm7/asm/OS_tick.s b/arm7/asm/OS_tick.s deleted file mode 100644 index f9688b9e..00000000 --- a/arm7/asm/OS_tick.s +++ /dev/null @@ -1,152 +0,0 @@ - .include "asm/macros.inc" - .include "global.inc" - - .section .bss - - .global _03807920 -_03807920: ;0x03807920 - .space 0x03807924 - 0x03807920 - - .global OSi_NeedResetTimer -OSi_NeedResetTimer: ;0x03807924 - .space 0x03807928 - 0x03807924 - - .global OSi_TickCounter -OSi_TickCounter: ;0x03807928 - .space 0x03807930 - 0x03807928 - - .section .text - - arm_func_start OS_GetTick -OS_GetTick: ; 0x037FA364 - stmfd sp!, {lr} - sub sp, sp, #12 - bl OS_DisableInterrupts - ldr r1, _037FA404 ; =0x04000100 - ldrh r1, [r1] - strh r1, [sp] - ldr r1, _037FA408 ; =OSi_TickCounter - ldr ip, [r1] - ldr r3, [r1, #4] - ldr r2, _037FA40C ; =0x0000FFFF - mvn r1, #0 - and r2, r3, r2 - and r1, ip, r1 - str r1, [sp, #4] - str r2, [sp, #8] - ldr r1, _037FA410 ; =0x04000214 - ldr r1, [r1] - ands r1, r1, #8 - beq _037FA3D8 - ldrh r1, [sp] - ands r1, r1, #32768 ; 0x8000 - bne _037FA3D8 - ldr r3, [sp, #4] - ldr r2, [sp, #8] - mov r1, #1 - adds r3, r3, r1 - adc r1, r2, #0 - str r3, [sp, #4] - str r1, [sp, #8] -_037FA3D8: - bl OS_RestoreInterrupts - ldr r2, [sp, #4] - ldr r0, [sp, #8] - mov r1, r0, lsl #16 - orr r1, r1, r2, lsr #16 - ldrh r0, [sp] - orr r1, r1, r0, asr #31 - orr r0, r0, r2, lsl #16 - add sp, sp, #12 - ldmia sp!, {lr} - bx lr -_037FA404: .word 0x04000100 -_037FA408: .word OSi_TickCounter -_037FA40C: .word 0x0000FFFF -_037FA410: .word 0x04000214 - - arm_func_start OSi_CountUpTick -OSi_CountUpTick: ; 0x037FA414 - stmfd sp!, {lr} - sub sp, sp, #4 - ldr r1, _037FA488 ; =OSi_TickCounter - ldr ip, [r1] - ldr r2, [r1, #4] - mov r3, #0 - mov r0, #1 - adds ip, ip, r0 - adc r0, r2, #0 - str ip, [r1] - str r0, [r1, #4] - ldr r0, _037FA48C ; =OSi_NeedResetTimer - ldr r1, [r0] - cmp r1, #0 - beq _037FA46C - ldr r2, _037FA490 ; =0x04000102 - strh r3, [r2] - ldr r1, _037FA494 ; =0x04000100 - strh r3, [r1] - mov r1, #193 ; 0xc1 - strh r1, [r2] - str r3, [r0] -_037FA46C: - mov r0, #0 - ldr r1, _037FA498 ; =OSi_CountUpTick - mov r2, r0 - bl OSi_EnterTimerCallback - add sp, sp, #4 - ldmia sp!, {lr} - bx lr -_037FA488: .word OSi_TickCounter -_037FA48C: .word OSi_NeedResetTimer -_037FA490: .word 0x04000102 -_037FA494: .word 0x04000100 -_037FA498: .word OSi_CountUpTick - - arm_func_start OS_IsTickAvailable -OS_IsTickAvailable: ; 0x037FA49C - ldr r0, _037FA4A8 ; =_03807920 - ldrh r0, [r0] - bx lr -_037FA4A8: .word _03807920 - - arm_func_start OS_InitTick -OS_InitTick: ; 0x037FA4AC - stmfd sp!, {lr} - sub sp, sp, #4 - ldr r0, _037FA528 ; =_03807920 - ldrh r1, [r0] - cmp r1, #0 - bne _037FA51C - mov r1, #1 - strh r1, [r0] - mov r0, #0 - bl OSi_SetTimerReserved - mov r2, #0 - ldr r0, _037FA52C ; =OSi_TickCounter - str r2, [r0] - str r2, [r0, #4] - ldr r1, _037FA530 ; =0x04000102 - strh r2, [r1] - ldr r0, _037FA534 ; =0x04000100 - strh r2, [r0] - mov r0, #193 ; 0xc1 - strh r0, [r1] - mov r0, #8 - ldr r1, _037FA538 ; =OSi_CountUpTick - bl OS_SetIrqFunction - mov r0, #8 - bl OS_EnableIrqMask - mov r1, #0 - ldr r0, _037FA53C ; =OSi_NeedResetTimer - str r1, [r0] -_037FA51C: - add sp, sp, #4 - ldmia sp!, {lr} - bx lr -_037FA528: .word _03807920 -_037FA52C: .word OSi_TickCounter -_037FA530: .word 0x04000102 -_037FA534: .word 0x04000100 -_037FA538: .word OSi_CountUpTick -_037FA53C: .word OSi_NeedResetTimer diff --git a/arm7/global.inc b/arm7/global.inc index 1b3a3fc5..b21c60fc 100644 --- a/arm7/global.inc +++ b/arm7/global.inc @@ -380,7 +380,7 @@ .extern _038078F4
.extern OSiHeapInfo
.extern _0380791C
-.extern _03807920
+.extern OSi_UseTick
.extern OSi_NeedResetTimer
.extern OSi_TickCounter
.extern _03807930
diff --git a/arm7/lib/include/OS_tick.h b/arm7/lib/include/OS_tick.h new file mode 100644 index 00000000..7ee9544a --- /dev/null +++ b/arm7/lib/include/OS_tick.h @@ -0,0 +1,11 @@ +#ifndef POKEDIAMOND_OS_TICK_H +#define POKEDIAMOND_OS_TICK_H + +#include "nitro/OS_tick_shared.h" + +void OS_InitTick(void); +u16 OS_IsTickAvailable(void); +void OSi_CountUpTick(void); +OSTick OS_GetTick(void); + +#endif diff --git a/arm7/lib/include/OS_timer.h b/arm7/lib/include/OS_timer.h new file mode 100644 index 00000000..fee42c6f --- /dev/null +++ b/arm7/lib/include/OS_timer.h @@ -0,0 +1,6 @@ +#ifndef POKEDIAMOND_OS_TIMER_H +#define POKEDIAMOND_OS_TIMER_H + +#include "nitro/OS_timer_shared.h" + +#endif diff --git a/arm7/lib/src/OS_tick.c b/arm7/lib/src/OS_tick.c new file mode 100644 index 00000000..e45b6833 --- /dev/null +++ b/arm7/lib/src/OS_tick.c @@ -0,0 +1,54 @@ +#include "OS_interrupt.h" +#include "OS_system.h" +#include "OS_tick.h" +#include "OS_timer.h" +#include "function_target.h" + +extern void OSi_SetTimerReserved(u32); + +static u16 OSi_UseTick; +static OSTick OSi_TickCounter; +static BOOL OSi_NeedResetTimer; + +ARM_FUNC void OS_InitTick(void) { + if (OSi_UseTick == 0) { + OSi_UseTick = 1; + OSi_SetTimerReserved(0); + OSi_TickCounter = 0; + OS_SetTimerControl(OS_TIMER_0, 0); + OS_SetTimerCount(OS_TIMER_0, 0); + OS_SetTimerControl(OS_TIMER_0, 0xc1); + OS_SetIrqFunction(8, OSi_CountUpTick); + (void)OS_EnableIrqMask(8); + OSi_NeedResetTimer = 0; + } +} + +ARM_FUNC u16 OS_IsTickAvailable(void) { + return OSi_UseTick; +} + +ARM_FUNC void OSi_CountUpTick(void) { + OSi_TickCounter++; + if (OSi_NeedResetTimer != 0) { + OS_SetTimerControl(OS_TIMER_0, 0); + OS_SetTimerCount(OS_TIMER_0, 0); + OS_SetTimerControl(OS_TIMER_0, 0xc1); + OSi_NeedResetTimer = 0; + } + OSi_EnterTimerCallback(0, (void(*)(void*))OSi_CountUpTick, NULL); +} + +ARM_FUNC OSTick OS_GetTick(void) { + OSIntrMode prev = OS_DisableInterrupts(); + vu16 countL = *(REGType16 *)((u32)®_OS_TM0CNT_L + OS_TIMER_0 * 4); + vu64 countH = OSi_TickCounter & 0xffffffffffffULL; + + if (reg_OS_IF & 8 && !(countL & 0x8000)) { + countH++; + } + + (void)OS_RestoreInterrupts(prev); + + return (countH << 16) | countL; +} diff --git a/arm9/lib/include/OS_timer.h b/arm9/lib/include/OS_timer.h index 8b2a97a9..effa5fc0 100644 --- a/arm9/lib/include/OS_timer.h +++ b/arm9/lib/include/OS_timer.h @@ -1,32 +1,6 @@ #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)®_OS_TM0CNT_L + id * 4)) = count; -} - -static inline void OS_SetTimerControl(OSTimer id, u16 control) -{ - *((REGType16 *)((u32)®_OS_TM0CNT_H + id * 4)) = control; -} +#include "nitro/OS_timer_shared.h" #endif //POKEDIAMOND_OS_TIMER_H diff --git a/arm9/lib/include/registers.h b/arm9/lib/include/registers.h index a5355322..75046308 100644 --- a/arm9/lib/include/registers.h +++ b/arm9/lib/include/registers.h @@ -73,15 +73,6 @@ #define reg_MI_DMA2_CLR_DATA (*(REGType32v *)0x40000e8) #define reg_MI_DMA3_CLR_DATA (*(REGType32v *)0x40000ec) -#define reg_OS_TM0CNT_L (*(REGType16v *)0x4000100) -#define reg_OS_TM0CNT_H (*(REGType16v *)0x4000102) -#define reg_OS_TM1CNT_L (*(REGType16v *)0x4000104) -#define reg_OS_TM1CNT_H (*(REGType16v *)0x4000106) -#define reg_OS_TM2CNT_L (*(REGType16v *)0x4000108) -#define reg_OS_TM2CNT_H (*(REGType16v *)0x400010a) -#define reg_OS_TM3CNT_L (*(REGType16v *)0x400010c) -#define reg_OS_TM3CNT_H (*(REGType16v *)0x400010e) - #define reg_EXI_SIODATA32 (*(REGType32v *)0x4000120) #define reg_EXI_SIOCNT (*(REGType16v *)0x4000128) #define reg_EXI_SIOSEL (*(REGType32v *)0x400012c) @@ -354,7 +345,6 @@ #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 diff --git a/include/nitro/OS_timer_shared.h b/include/nitro/OS_timer_shared.h new file mode 100644 index 00000000..db20ee08 --- /dev/null +++ b/include/nitro/OS_timer_shared.h @@ -0,0 +1,32 @@ +#ifndef POKEDIAMOND_OS_TIMER_SHARED_H +#define POKEDIAMOND_OS_TIMER_SHARED_H + +#include "nitro/consts_shared.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)®_OS_TM0CNT_L + id * 4)) = count; +} + +static inline void OS_SetTimerControl(OSTimer id, u16 control) +{ + *((REGType16 *)((u32)®_OS_TM0CNT_H + id * 4)) = control; +} + +#endif diff --git a/include/nitro/registers_shared.h b/include/nitro/registers_shared.h index 60152ab0..4962e608 100644 --- a/include/nitro/registers_shared.h +++ b/include/nitro/registers_shared.h @@ -10,8 +10,19 @@ #include "nitro/types.h" +#define reg_OS_TM0CNT_L (*(REGType16v *)0x4000100) +#define reg_OS_TM0CNT_H (*(REGType16v *)0x4000102) +#define reg_OS_TM1CNT_L (*(REGType16v *)0x4000104) +#define reg_OS_TM1CNT_H (*(REGType16v *)0x4000106) +#define reg_OS_TM2CNT_L (*(REGType16v *)0x4000108) +#define reg_OS_TM2CNT_H (*(REGType16v *)0x400010a) +#define reg_OS_TM3CNT_L (*(REGType16v *)0x400010c) +#define reg_OS_TM3CNT_H (*(REGType16v *)0x400010e) + #define reg_OS_IME (*(REGType16v *)0x4000208) #define reg_OS_IE (*(REGType32v *)0x4000210) #define reg_OS_IF (*(REGType32v *)0x4000214) +#define REG_OS_TM0CNT_H_PS_SHIFT 0 + #endif //POKEDIAMOND_REGISTERS_SHARED_H |