From 452bbe41cfb3139ea62580e9930525d9ea664ac9 Mon Sep 17 00:00:00 2001 From: YamaArashi Date: Wed, 2 Nov 2016 14:06:41 -0700 Subject: [PATCH] port siirtc from pokeruby --- asm/librtc.s | 1160 ---------------------------------------------- asm/m4a_1.s | 30 +- asm/main.s | 2 +- asm/rtc.s | 10 +- include/siirtc.h | 54 +++ ld_script.txt | 4 +- src/siirtc.c | 432 +++++++++++++++++ sym_bss.txt | 7 +- 8 files changed, 510 insertions(+), 1189 deletions(-) delete mode 100644 asm/librtc.s create mode 100644 include/siirtc.h create mode 100644 src/siirtc.c diff --git a/asm/librtc.s b/asm/librtc.s deleted file mode 100644 index 87ce5b415..000000000 --- a/asm/librtc.s +++ /dev/null @@ -1,1160 +0,0 @@ - .include "asm/macros.inc" - - .syntax unified - - .text - - thumb_func_start RTC_SetReadWrite -@ void RTC_SetReadWrite() -RTC_SetReadWrite: @ 82E2150 - push {r7,lr} - mov r7, sp - bl RTC_SetReadWriteInternal - ldr r0, =gUnknown_03001A7E - movs r1, 0 - strb r1, [r0] - pop {r7} - pop {r0} - bx r0 - .pool - thumb_func_end RTC_SetReadWrite - - thumb_func_start RTC_SetReadOnly -@ void RTC_SetReadOnly() -RTC_SetReadOnly: @ 82E2168 - push {r7,lr} - mov r7, sp - bl RTC_SetReadOnlyInternal - ldr r0, =gUnknown_03001A7E - movs r1, 0x1 - strb r1, [r0] - pop {r7} - pop {r0} - bx r0 - .pool - thumb_func_end RTC_SetReadOnly - - thumb_func_start RTC_Init -@ u8 RTC_Init() -RTC_Init: @ 82E2180 - push {r7,lr} - sub sp, 0x10 - mov r7, sp - adds r1, r7, 0x4 - adds r0, r1, 0 - bl RTC_GetControlReg - lsls r1, r0, 24 - lsrs r0, r1, 24 - cmp r0, 0 - bne _082E219A - movs r0, 0 - b _082E224E -_082E219A: - adds r0, r7, 0 - movs r1, 0 - strb r1, [r0] - adds r0, r7, 0x4 - ldrb r1, [r0, 0x7] - movs r2, 0xC0 - adds r0, r1, 0 - ands r0, r2 - adds r2, r0, 0 - lsls r1, r2, 24 - lsrs r0, r1, 24 - cmp r0, 0x80 - beq _082E21CA - adds r0, r7, 0x4 - ldrb r1, [r0, 0x7] - movs r2, 0xC0 - adds r0, r1, 0 - ands r0, r2 - adds r2, r0, 0 - lsls r1, r2, 24 - lsrs r0, r1, 24 - cmp r0, 0 - bne _082E21E8 - b _082E21CA -_082E21CA: - bl RTC_Reset - lsls r1, r0, 24 - lsrs r0, r1, 24 - cmp r0, 0 - bne _082E21DA - movs r0, 0 - b _082E224E -_082E21DA: - adds r1, r7, 0 - adds r0, r7, 0 - adds r1, r7, 0 - ldrb r2, [r1] - adds r1, r2, 0x1 - adds r2, r1, 0 - strb r2, [r0] -_082E21E8: - adds r1, r7, 0x4 - adds r0, r1, 0 - bl RTC_GetTime - adds r0, r7, 0x4 - ldrb r1, [r0, 0x6] - movs r2, 0x80 - adds r0, r1, 0 - ands r0, r2 - adds r2, r0, 0 - lsls r1, r2, 24 - lsrs r0, r1, 24 - cmp r0, 0 - beq _082E2236 - bl RTC_Reset - lsls r1, r0, 24 - lsrs r0, r1, 24 - cmp r0, 0 - bne _082E2228 - adds r0, r7, 0 - ldrb r1, [r0] - lsls r0, r1, 4 - adds r1, r0, 0 - movs r2, 0xF - adds r0, r1, 0 - bics r0, r2 - adds r1, r0, 0 - lsls r0, r1, 24 - lsrs r1, r0, 24 - adds r0, r1, 0 - b _082E224E -_082E2228: - adds r1, r7, 0 - adds r0, r7, 0 - adds r1, r7, 0 - ldrb r2, [r1] - adds r1, r2, 0x1 - adds r2, r1, 0 - strb r2, [r0] -_082E2236: - adds r0, r7, 0 - ldrb r1, [r0] - lsls r0, r1, 4 - adds r1, r0, 0 - movs r2, 0x1 - adds r0, r1, 0 - orrs r0, r2 - adds r1, r0, 0 - lsls r0, r1, 24 - lsrs r1, r0, 24 - adds r0, r1, 0 - b _082E224E -_082E224E: - add sp, 0x10 - pop {r7} - pop {r1} - bx r1 - thumb_func_end RTC_Init - - thumb_func_start RTC_Reset -@ u8 RTC_Reset() -RTC_Reset: @ 82E2258 - push {r7,lr} - sub sp, 0x10 - mov r7, sp - ldr r0, =gUnknown_03001A7E - ldrb r1, [r0] - cmp r1, 0x1 - bne _082E2270 - movs r0, 0 - b _082E22D4 - .pool -_082E2270: - ldr r0, =gUnknown_03001A7E - movs r1, 0x1 - strb r1, [r0] - ldr r0, =GPIOPortData - movs r1, 0x1 - strh r1, [r0] - ldr r0, =GPIOPortData - movs r1, 0x5 - strh r1, [r0] - ldr r0, =GPIOPortDirection - movs r1, 0x7 - strh r1, [r0] - movs r0, 0x60 - bl RTC_WriteByte - ldr r0, =GPIOPortData - movs r1, 0x1 - strh r1, [r0] - ldr r0, =GPIOPortData - movs r1, 0x1 - strh r1, [r0] - ldr r0, =gUnknown_03001A7E - movs r1, 0 - strb r1, [r0] - adds r0, r7, 0x4 - ldrb r1, [r0, 0x7] - movs r2, 0 - ands r1, r2 - adds r2, r1, 0 - movs r3, 0x40 - adds r1, r2, 0 - orrs r1, r3 - adds r2, r1, 0 - strb r2, [r0, 0x7] - adds r1, r7, 0x4 - adds r0, r1, 0 - bl RTC_SetControlReg - adds r1, r7, 0 - strb r0, [r1] - adds r0, r7, 0 - ldrb r1, [r0] - adds r0, r1, 0 - b _082E22D4 - .pool -_082E22D4: - add sp, 0x10 - pop {r7} - pop {r1} - bx r1 - thumb_func_end RTC_Reset - - thumb_func_start RTC_GetControlReg -@ u8 RTC_GetControlReg(struct RTCInfo *rtc) -RTC_GetControlReg: @ 82E22DC - push {r4,r7,lr} - sub sp, 0x8 - mov r7, sp - str r0, [r7] - ldr r0, =gUnknown_03001A7E - ldrb r1, [r0] - cmp r1, 0x1 - bne _082E22F4 - movs r0, 0 - b _082E23A0 - .pool -_082E22F4: - ldr r0, =gUnknown_03001A7E - movs r1, 0x1 - strb r1, [r0] - ldr r0, =GPIOPortData - movs r1, 0x1 - strh r1, [r0] - ldr r0, =GPIOPortData - movs r1, 0x5 - strh r1, [r0] - ldr r0, =GPIOPortDirection - movs r1, 0x7 - strh r1, [r0] - movs r0, 0x63 - bl RTC_WriteByte - ldr r0, =GPIOPortDirection - movs r1, 0x5 - strh r1, [r0] - bl RTC_ReadByte - adds r1, r7, 0x4 - strb r0, [r1] - ldr r0, [r7] - adds r1, r7, 0x4 - ldrb r2, [r1] - movs r3, 0xC0 - adds r1, r2, 0 - ands r1, r3 - adds r2, r7, 0x4 - ldrb r3, [r2] - movs r4, 0x20 - adds r2, r3, 0 - ands r2, r4 - adds r4, r2, 0 - lsls r3, r4, 24 - lsrs r2, r3, 24 - lsrs r3, r2, 3 - adds r2, r3, 0 - orrs r1, r2 - adds r2, r7, 0x4 - ldrb r3, [r2] - movs r4, 0x8 - adds r2, r3, 0 - ands r2, r4 - adds r4, r2, 0 - lsls r3, r4, 24 - lsrs r2, r3, 24 - lsrs r3, r2, 2 - adds r2, r3, 0 - orrs r1, r2 - adds r2, r7, 0x4 - ldrb r3, [r2] - movs r4, 0x2 - adds r2, r3, 0 - ands r2, r4 - adds r4, r2, 0 - lsls r3, r4, 24 - lsrs r2, r3, 24 - lsrs r3, r2, 1 - adds r2, r3, 0 - orrs r1, r2 - ldrb r2, [r0, 0x7] - movs r3, 0 - ands r2, r3 - adds r3, r2, 0 - adds r2, r3, 0 - orrs r2, r1 - adds r1, r2, 0 - strb r1, [r0, 0x7] - ldr r0, =GPIOPortData - movs r1, 0x1 - strh r1, [r0] - ldr r0, =GPIOPortData - movs r1, 0x1 - strh r1, [r0] - ldr r0, =gUnknown_03001A7E - movs r1, 0 - strb r1, [r0] - movs r0, 0x1 - b _082E23A0 - .pool -_082E23A0: - add sp, 0x8 - pop {r4,r7} - pop {r1} - bx r1 - thumb_func_end RTC_GetControlReg - - thumb_func_start RTC_SetControlReg -@ u8 RTC_SetControlReg(struct RTCInfo *rtc) -RTC_SetControlReg: @ 82E23A8 - push {r4,r7,lr} - sub sp, 0x8 - mov r7, sp - str r0, [r7] - ldr r0, =gUnknown_03001A7E - ldrb r1, [r0] - cmp r1, 0x1 - bne _082E23C0 - movs r0, 0 - b _082E2448 - .pool -_082E23C0: - ldr r0, =gUnknown_03001A7E - movs r1, 0x1 - strb r1, [r0] - ldr r0, =GPIOPortData - movs r1, 0x1 - strh r1, [r0] - ldr r0, =GPIOPortData - movs r1, 0x5 - strh r1, [r0] - adds r0, r7, 0x4 - ldr r1, [r7] - ldrb r2, [r1, 0x7] - movs r3, 0x4 - adds r1, r2, 0 - ands r1, r3 - adds r2, r1, 0 - lsls r1, r2, 3 - ldr r2, [r7] - ldrb r3, [r2, 0x7] - movs r4, 0x2 - adds r2, r3, 0 - ands r2, r4 - adds r3, r2, 0 - lsls r2, r3, 2 - adds r3, r2, 0 - movs r4, 0x40 - adds r2, r3, 0 - orrs r2, r4 - orrs r1, r2 - ldr r2, [r7] - ldrb r3, [r2, 0x7] - movs r4, 0x1 - adds r2, r3, 0 - ands r2, r4 - adds r3, r2, 0 - lsls r2, r3, 1 - orrs r1, r2 - adds r2, r1, 0 - strb r2, [r0] - ldr r0, =GPIOPortDirection - movs r1, 0x7 - strh r1, [r0] - movs r0, 0x62 - bl RTC_WriteByte - adds r0, r7, 0x4 - ldrb r1, [r0] - adds r0, r1, 0 - bl RTC_WriteByteReversed - ldr r0, =GPIOPortData - movs r1, 0x1 - strh r1, [r0] - ldr r0, =GPIOPortData - movs r1, 0x1 - strh r1, [r0] - ldr r0, =gUnknown_03001A7E - movs r1, 0 - strb r1, [r0] - movs r0, 0x1 - b _082E2448 - .pool -_082E2448: - add sp, 0x8 - pop {r4,r7} - pop {r1} - bx r1 - thumb_func_end RTC_SetControlReg - - thumb_func_start RTC_GetDateTime -@ u8 RTC_GetDateTime(u8 *rtcDateTime) -RTC_GetDateTime: @ 82E2450 - push {r7,lr} - sub sp, 0x8 - mov r7, sp - str r0, [r7] - ldr r0, =gUnknown_03001A7E - ldrb r1, [r0] - cmp r1, 0x1 - bne _082E2468 - movs r0, 0 - b _082E24F8 - .pool -_082E2468: - ldr r0, =gUnknown_03001A7E - movs r1, 0x1 - strb r1, [r0] - ldr r0, =GPIOPortData - movs r1, 0x1 - strh r1, [r0] - ldr r0, =GPIOPortData - movs r1, 0x5 - strh r1, [r0] - ldr r0, =GPIOPortDirection - movs r1, 0x7 - strh r1, [r0] - movs r0, 0x65 - bl RTC_WriteByte - ldr r0, =GPIOPortDirection - movs r1, 0x5 - strh r1, [r0] - adds r0, r7, 0x4 - movs r1, 0 - strb r1, [r0] -_082E2492: - adds r0, r7, 0x4 - ldrb r1, [r0] - cmp r1, 0x6 - bls _082E24A8 - b _082E24C6 - .pool -_082E24A8: - bl RTC_ReadByte - adds r1, r7, 0x4 - ldrb r2, [r1] - ldr r3, [r7] - adds r1, r2, r3 - strb r0, [r1] - adds r1, r7, 0x4 - adds r0, r7, 0x4 - adds r1, r7, 0x4 - ldrb r2, [r1] - adds r1, r2, 0x1 - adds r2, r1, 0 - strb r2, [r0] - b _082E2492 -_082E24C6: - ldr r1, [r7] - adds r0, r1, 0x4 - ldr r2, [r7] - adds r1, r2, 0x4 - ldrb r2, [r1] - movs r3, 0x7F - adds r1, r2, 0 - ands r1, r3 - adds r2, r1, 0 - strb r2, [r0] - ldr r0, =GPIOPortData - movs r1, 0x1 - strh r1, [r0] - ldr r0, =GPIOPortData - movs r1, 0x1 - strh r1, [r0] - ldr r0, =gUnknown_03001A7E - movs r1, 0 - strb r1, [r0] - movs r0, 0x1 - b _082E24F8 - .pool -_082E24F8: - add sp, 0x8 - pop {r7} - pop {r1} - bx r1 - thumb_func_end RTC_GetDateTime - - thumb_func_start RTC_SetDateTime -@ u8 RTC_SetDateTime(u8 *rtcDateTime) -RTC_SetDateTime: @ 82E2500 - push {r7,lr} - sub sp, 0x8 - mov r7, sp - str r0, [r7] - ldr r0, =gUnknown_03001A7E - ldrb r1, [r0] - cmp r1, 0x1 - bne _082E2518 - movs r0, 0 - b _082E2594 - .pool -_082E2518: - ldr r0, =gUnknown_03001A7E - movs r1, 0x1 - strb r1, [r0] - ldr r0, =GPIOPortData - movs r1, 0x1 - strh r1, [r0] - ldr r0, =GPIOPortData - movs r1, 0x5 - strh r1, [r0] - ldr r0, =GPIOPortDirection - movs r1, 0x7 - strh r1, [r0] - movs r0, 0x64 - bl RTC_WriteByte - adds r0, r7, 0x4 - movs r1, 0 - strb r1, [r0] -_082E253C: - adds r0, r7, 0x4 - ldrb r1, [r0] - cmp r1, 0x6 - bls _082E2554 - b _082E2574 - .pool -_082E2554: - adds r0, r7, 0x4 - ldrb r1, [r0] - ldr r2, [r7] - adds r0, r1, r2 - ldrb r1, [r0] - adds r0, r1, 0 - bl RTC_WriteByteReversed - adds r1, r7, 0x4 - adds r0, r7, 0x4 - adds r1, r7, 0x4 - ldrb r2, [r1] - adds r1, r2, 0x1 - adds r2, r1, 0 - strb r2, [r0] - b _082E253C -_082E2574: - ldr r0, =GPIOPortData - movs r1, 0x1 - strh r1, [r0] - ldr r0, =GPIOPortData - movs r1, 0x1 - strh r1, [r0] - ldr r0, =gUnknown_03001A7E - movs r1, 0 - strb r1, [r0] - movs r0, 0x1 - b _082E2594 - .pool -_082E2594: - add sp, 0x8 - pop {r7} - pop {r1} - bx r1 - thumb_func_end RTC_SetDateTime - - thumb_func_start RTC_GetTime -@ u8 RTC_GetTime(u8 *rtcTime) -RTC_GetTime: @ 82E259C - push {r7,lr} - sub sp, 0x8 - mov r7, sp - str r0, [r7] - ldr r0, =gUnknown_03001A7E - ldrb r1, [r0] - cmp r1, 0x1 - bne _082E25B4 - movs r0, 0 - b _082E2648 - .pool -_082E25B4: - ldr r0, =gUnknown_03001A7E - movs r1, 0x1 - strb r1, [r0] - ldr r0, =GPIOPortData - movs r1, 0x1 - strh r1, [r0] - ldr r0, =GPIOPortData - movs r1, 0x5 - strh r1, [r0] - ldr r0, =GPIOPortDirection - movs r1, 0x7 - strh r1, [r0] - movs r0, 0x67 - bl RTC_WriteByte - ldr r0, =GPIOPortDirection - movs r1, 0x5 - strh r1, [r0] - adds r0, r7, 0x4 - movs r1, 0 - strb r1, [r0] -_082E25DE: - adds r0, r7, 0x4 - ldrb r1, [r0] - cmp r1, 0x2 - bls _082E25F4 - b _082E2614 - .pool -_082E25F4: - bl RTC_ReadByte - adds r1, r7, 0x4 - ldrb r2, [r1] - ldr r3, [r7] - adds r1, r2, r3 - adds r2, r1, 0x4 - strb r0, [r2] - adds r1, r7, 0x4 - adds r0, r7, 0x4 - adds r1, r7, 0x4 - ldrb r2, [r1] - adds r1, r2, 0x1 - adds r2, r1, 0 - strb r2, [r0] - b _082E25DE -_082E2614: - ldr r1, [r7] - adds r0, r1, 0x4 - ldr r2, [r7] - adds r1, r2, 0x4 - ldrb r2, [r1] - movs r3, 0x7F - adds r1, r2, 0 - ands r1, r3 - adds r2, r1, 0 - strb r2, [r0] - ldr r0, =GPIOPortData - movs r1, 0x1 - strh r1, [r0] - ldr r0, =GPIOPortData - movs r1, 0x1 - strh r1, [r0] - ldr r0, =gUnknown_03001A7E - movs r1, 0 - strb r1, [r0] - movs r0, 0x1 - b _082E2648 - .pool -_082E2648: - add sp, 0x8 - pop {r7} - pop {r1} - bx r1 - thumb_func_end RTC_GetTime - - thumb_func_start RTC_SetTime -@ u8 RTC_SetTime(u8 *rtcTime) -RTC_SetTime: @ 82E2650 - push {r7,lr} - sub sp, 0x8 - mov r7, sp - str r0, [r7] - ldr r0, =gUnknown_03001A7E - ldrb r1, [r0] - cmp r1, 0x1 - bne _082E2668 - movs r0, 0 - b _082E26E4 - .pool -_082E2668: - ldr r0, =gUnknown_03001A7E - movs r1, 0x1 - strb r1, [r0] - ldr r0, =GPIOPortData - movs r1, 0x1 - strh r1, [r0] - ldr r0, =GPIOPortData - movs r1, 0x5 - strh r1, [r0] - ldr r0, =GPIOPortDirection - movs r1, 0x7 - strh r1, [r0] - movs r0, 0x66 - bl RTC_WriteByte - adds r0, r7, 0x4 - movs r1, 0 - strb r1, [r0] -_082E268C: - adds r0, r7, 0x4 - ldrb r1, [r0] - cmp r1, 0x2 - bls _082E26A4 - b _082E26C6 - .pool -_082E26A4: - adds r0, r7, 0x4 - ldrb r1, [r0] - ldr r2, [r7] - adds r0, r1, r2 - adds r1, r0, 0x4 - ldrb r2, [r1] - adds r0, r2, 0 - bl RTC_WriteByteReversed - adds r1, r7, 0x4 - adds r0, r7, 0x4 - adds r1, r7, 0x4 - ldrb r2, [r1] - adds r1, r2, 0x1 - adds r2, r1, 0 - strb r2, [r0] - b _082E268C -_082E26C6: - ldr r0, =GPIOPortData - movs r1, 0x1 - strh r1, [r0] - ldr r0, =GPIOPortData - movs r1, 0x1 - strh r1, [r0] - ldr r0, =gUnknown_03001A7E - movs r1, 0 - strb r1, [r0] - movs r0, 0x1 - b _082E26E4 - .pool -_082E26E4: - add sp, 0x8 - pop {r7} - pop {r1} - bx r1 - thumb_func_end RTC_SetTime - - thumb_func_start RTC_SetUnknownData -@ u8 RTC_SetUnknownData(struct RTCInfo *rtc) -RTC_SetUnknownData: @ 82E26EC - push {r4,r7,lr} - sub sp, 0xC - mov r7, sp - str r0, [r7] - ldr r0, =gUnknown_03001A7E - ldrb r1, [r0] - cmp r1, 0x1 - bne _082E2704 - movs r0, 0 - b _082E2818 - .pool -_082E2704: - ldr r0, =gUnknown_03001A7E - movs r1, 0x1 - strb r1, [r0] - adds r0, r7, 0 - adds r0, 0x8 - ldr r1, [r7] - ldrb r2, [r1, 0x8] - movs r3, 0xF - adds r1, r2, 0 - ands r1, r3 - ldr r2, [r7] - ldrb r3, [r2, 0x8] - lsrs r2, r3, 4 - adds r3, r2, 0 - movs r4, 0xF - adds r2, r3, 0 - ands r2, r4 - adds r4, r2, 0 - lsls r3, r4, 24 - lsrs r2, r3, 24 - adds r4, r2, 0 - lsls r3, r4, 2 - adds r3, r2 - lsls r2, r3, 1 - adds r1, r2 - ldrb r2, [r0] - movs r3, 0 - ands r2, r3 - adds r3, r2, 0 - adds r2, r3, 0 - orrs r2, r1 - adds r1, r2, 0 - strb r1, [r0] - adds r0, r7, 0 - adds r0, 0x8 - ldrb r1, [r0] - cmp r1, 0xB - bhi _082E2770 - adds r0, r7, 0 - adds r0, 0x8 - ldr r1, [r7] - ldrb r2, [r0] - movs r3, 0 - ands r2, r3 - adds r3, r2, 0 - ldrb r1, [r1, 0x8] - adds r2, r3, 0 - orrs r2, r1 - adds r1, r2, 0 - strb r1, [r0] - b _082E278E - .pool -_082E2770: - adds r0, r7, 0 - adds r0, 0x8 - ldr r1, [r7] - ldrb r2, [r1, 0x8] - movs r3, 0x80 - adds r1, r2, 0 - orrs r1, r3 - ldrb r2, [r0] - movs r3, 0 - ands r2, r3 - adds r3, r2, 0 - adds r2, r3, 0 - orrs r2, r1 - adds r1, r2, 0 - strb r1, [r0] -_082E278E: - adds r0, r7, 0 - adds r0, 0x8 - ldr r1, [r7] - ldrb r2, [r0, 0x1] - movs r3, 0 - ands r2, r3 - adds r3, r2, 0 - ldrb r1, [r1, 0x9] - adds r2, r3, 0 - orrs r2, r1 - adds r1, r2, 0 - strb r1, [r0, 0x1] - ldr r0, =GPIOPortData - movs r1, 0x1 - strh r1, [r0] - ldr r0, =GPIOPortData - movs r1, 0x5 - strh r1, [r0] - ldr r0, =GPIOPortDirection - movs r1, 0x7 - strh r1, [r0] - movs r0, 0x68 - bl RTC_WriteByte - adds r0, r7, 0x4 - movs r1, 0 - strb r1, [r0] -_082E27C4: - adds r0, r7, 0x4 - ldrb r1, [r0] - cmp r1, 0x1 - bls _082E27D8 - b _082E27FA - .pool -_082E27D8: - adds r0, r7, 0 - adds r0, 0x8 - adds r1, r7, 0x4 - ldrb r2, [r1] - adds r0, r2 - ldrb r1, [r0] - adds r0, r1, 0 - bl RTC_WriteByteReversed - adds r1, r7, 0x4 - adds r0, r7, 0x4 - adds r1, r7, 0x4 - ldrb r2, [r1] - adds r1, r2, 0x1 - adds r2, r1, 0 - strb r2, [r0] - b _082E27C4 -_082E27FA: - ldr r0, =GPIOPortData - movs r1, 0x1 - strh r1, [r0] - ldr r0, =GPIOPortData - movs r1, 0x1 - strh r1, [r0] - ldr r0, =gUnknown_03001A7E - movs r1, 0 - strb r1, [r0] - movs r0, 0x1 - b _082E2818 - .pool -_082E2818: - add sp, 0xC - pop {r4,r7} - pop {r1} - bx r1 - thumb_func_end RTC_SetUnknownData - - thumb_func_start RTC_WriteByte -@ u8 RTC_WriteByte(u8 value) -RTC_WriteByte: @ 82E2820 - push {r4,r5,r7,lr} - sub sp, 0x4 - mov r7, sp - adds r1, r0, 0 - adds r2, r7, 0 - strb r1, [r2] - adds r1, r7, 0x1 - movs r2, 0 - strb r2, [r1] -_082E2832: - adds r1, r7, 0x1 - ldrb r2, [r1] - cmp r2, 0x7 - bls _082E283C - b _082E28BC -_082E283C: - adds r1, r7, 0x2 - adds r3, r7, 0 - ldrb r2, [r3] - adds r3, r7, 0x1 - ldrb r4, [r3] - movs r5, 0x7 - subs r3, r5, r4 - asrs r2, r3 - adds r3, r2, 0 - movs r4, 0x1 - adds r2, r3, 0 - ands r2, r4 - adds r3, r2, 0 - strb r3, [r1] - ldr r1, =GPIOPortData - adds r2, r7, 0x2 - ldrb r3, [r2] - adds r2, r3, 0 - lsls r3, r2, 1 - adds r2, r3, 0 - movs r3, 0x4 - orrs r2, r3 - adds r3, r2, 0 - strh r3, [r1] - ldr r1, =GPIOPortData - adds r2, r7, 0x2 - ldrb r3, [r2] - adds r2, r3, 0 - lsls r3, r2, 1 - adds r2, r3, 0 - movs r3, 0x4 - orrs r2, r3 - adds r3, r2, 0 - strh r3, [r1] - ldr r1, =GPIOPortData - adds r2, r7, 0x2 - ldrb r3, [r2] - adds r2, r3, 0 - lsls r3, r2, 1 - adds r2, r3, 0 - movs r3, 0x4 - orrs r2, r3 - adds r3, r2, 0 - strh r3, [r1] - ldr r1, =GPIOPortData - adds r2, r7, 0x2 - ldrb r3, [r2] - adds r2, r3, 0 - lsls r3, r2, 1 - adds r2, r3, 0 - movs r3, 0x5 - orrs r2, r3 - adds r3, r2, 0 - strh r3, [r1] - adds r2, r7, 0x1 - adds r1, r7, 0x1 - adds r2, r7, 0x1 - ldrb r3, [r2] - adds r2, r3, 0x1 - adds r3, r2, 0 - strb r3, [r1] - b _082E2832 - .pool -_082E28BC: - add sp, 0x4 - pop {r4,r5,r7} - pop {r1} - bx r1 - thumb_func_end RTC_WriteByte - - thumb_func_start RTC_WriteByteReversed -@ u8 RTC_WriteByteReversed(u8 value) -RTC_WriteByteReversed: @ 82E28C4 - push {r4,r7,lr} - sub sp, 0x4 - mov r7, sp - adds r1, r0, 0 - adds r2, r7, 0 - strb r1, [r2] - adds r1, r7, 0x1 - movs r2, 0 - strb r2, [r1] -_082E28D6: - adds r1, r7, 0x1 - ldrb r2, [r1] - cmp r2, 0x7 - bls _082E28E0 - b _082E295C -_082E28E0: - adds r1, r7, 0x2 - adds r3, r7, 0 - ldrb r2, [r3] - adds r3, r7, 0x1 - ldrb r4, [r3] - asrs r2, r4 - adds r3, r2, 0 - movs r4, 0x1 - adds r2, r3, 0 - ands r2, r4 - adds r3, r2, 0 - strb r3, [r1] - ldr r1, =GPIOPortData - adds r2, r7, 0x2 - ldrb r3, [r2] - adds r2, r3, 0 - lsls r3, r2, 1 - adds r2, r3, 0 - movs r3, 0x4 - orrs r2, r3 - adds r3, r2, 0 - strh r3, [r1] - ldr r1, =GPIOPortData - adds r2, r7, 0x2 - ldrb r3, [r2] - adds r2, r3, 0 - lsls r3, r2, 1 - adds r2, r3, 0 - movs r3, 0x4 - orrs r2, r3 - adds r3, r2, 0 - strh r3, [r1] - ldr r1, =GPIOPortData - adds r2, r7, 0x2 - ldrb r3, [r2] - adds r2, r3, 0 - lsls r3, r2, 1 - adds r2, r3, 0 - movs r3, 0x4 - orrs r2, r3 - adds r3, r2, 0 - strh r3, [r1] - ldr r1, =GPIOPortData - adds r2, r7, 0x2 - ldrb r3, [r2] - adds r2, r3, 0 - lsls r3, r2, 1 - adds r2, r3, 0 - movs r3, 0x5 - orrs r2, r3 - adds r3, r2, 0 - strh r3, [r1] - adds r2, r7, 0x1 - adds r1, r7, 0x1 - adds r2, r7, 0x1 - ldrb r3, [r2] - adds r2, r3, 0x1 - adds r3, r2, 0 - strb r3, [r1] - b _082E28D6 - .pool -_082E295C: - add sp, 0x4 - pop {r4,r7} - pop {r1} - bx r1 - thumb_func_end RTC_WriteByteReversed - - thumb_func_start RTC_ReadByte -@ u8 RTC_ReadByte() -RTC_ReadByte: @ 82E2964 - push {r7,lr} - sub sp, 0x4 - mov r7, sp - adds r0, r7, 0 - movs r1, 0 - strb r1, [r0] -_082E2970: - adds r0, r7, 0 - ldrb r1, [r0] - cmp r1, 0x7 - bls _082E297A - b _082E29E0 -_082E297A: - ldr r0, =GPIOPortData - movs r1, 0x4 - strh r1, [r0] - ldr r0, =GPIOPortData - movs r1, 0x4 - strh r1, [r0] - ldr r0, =GPIOPortData - movs r1, 0x4 - strh r1, [r0] - ldr r0, =GPIOPortData - movs r1, 0x4 - strh r1, [r0] - ldr r0, =GPIOPortData - movs r1, 0x4 - strh r1, [r0] - ldr r0, =GPIOPortData - movs r1, 0x5 - strh r1, [r0] - adds r0, r7, 0x1 - ldr r1, =GPIOPortData - ldrh r2, [r1] - movs r3, 0x2 - adds r1, r2, 0 - ands r1, r3 - adds r3, r1, 0 - lsls r2, r3, 16 - lsrs r1, r2, 16 - lsrs r2, r1, 1 - adds r1, r2, 0 - strb r1, [r0] - adds r0, r7, 0x2 - adds r1, r7, 0x2 - ldrb r2, [r1] - lsrs r1, r2, 1 - adds r2, r7, 0x1 - ldrb r3, [r2] - lsls r2, r3, 7 - orrs r1, r2 - adds r2, r1, 0 - strb r2, [r0] - adds r1, r7, 0 - adds r0, r7, 0 - adds r1, r7, 0 - ldrb r2, [r1] - adds r1, r2, 0x1 - adds r2, r1, 0 - strb r2, [r0] - b _082E2970 - .pool -_082E29E0: - adds r0, r7, 0x2 - ldrb r1, [r0] - adds r0, r1, 0 - b _082E29E8 -_082E29E8: - add sp, 0x4 - pop {r7} - pop {r1} - bx r1 - thumb_func_end RTC_ReadByte - - thumb_func_start RTC_SetReadWriteInternal -@ void RTC_SetReadWriteInternal() -RTC_SetReadWriteInternal: @ 82E29F0 - push {r7,lr} - mov r7, sp - ldr r0, =GPIOPortReadEnable - movs r1, 0x1 - strh r1, [r0] - pop {r7} - pop {r0} - bx r0 - .pool - thumb_func_end RTC_SetReadWriteInternal - - thumb_func_start RTC_SetReadOnlyInternal -@ void RTC_SetReadOnlyInternal() -RTC_SetReadOnlyInternal: @ 82E2A04 - push {r7,lr} - mov r7, sp - ldr r0, =GPIOPortReadEnable - movs r1, 0 - strh r1, [r0] - pop {r7} - pop {r0} - bx r0 - .pool - thumb_func_end RTC_SetReadOnlyInternal - - .align 2, 0 @ Don't pad with nop. diff --git a/asm/m4a_1.s b/asm/m4a_1.s index 0afc8aad4..361f66c1a 100644 --- a/asm/m4a_1.s +++ b/asm/m4a_1.s @@ -302,7 +302,7 @@ _081DD044: ldrb r0, [r4, o_SoundChannel_type] tst r0, 0x30 beq _081DD068 - bl sub_81DD264 + bl sub_82DF49C b _081DD228 _081DD068: mov r10, r10, lsl 16 @@ -465,8 +465,8 @@ _081DD25E: .pool thumb_func_end SoundMainRAM - arm_func_start sub_81DD264 -sub_81DD264: + arm_func_start sub_82DF49C +sub_82DF49C: ldr r6, [r4, o_SoundChannel_wav] ldrb r0, [r4, o_SoundChannel_status] tst r0, 0x20 @@ -505,10 +505,10 @@ _081DD2B4: ldrb r0, [r4, o_SoundChannel_type] tst r0, 0x10 bne _081DD3C0 - bl sub_81DD520 + bl sub_82DF758 mov r0, r1 add r3, r3, 0x1 - bl sub_81DD520 + bl sub_82DF758 sub r1, r1, r0 _081DD308: ldr r6, [r5] @@ -534,11 +534,11 @@ _081DD310: b _081DD364 _081DD358: add r3, r3, lr - bl sub_81DD520 + bl sub_82DF758 mov r0, r1 _081DD364: add r3, r3, 0x1 - bl sub_81DD520 + bl sub_82DF758 sub r1, r1, r0 _081DD370: adds r5, r5, 0x40000000 @@ -565,10 +565,10 @@ _081DD3B0: b _081DD3B0 _081DD3C0: sub r3, r3, 0x1 - bl sub_81DD520 + bl sub_82DF758 mov r0, r1 sub r3, r3, 0x1 - bl sub_81DD520 + bl sub_82DF758 sub r1, r1, r0 _081DD3D8: ldr r6, [r5] @@ -594,11 +594,11 @@ _081DD3E0: b _081DD434 _081DD428: sub r3, r3, lr - bl sub_81DD520 + bl sub_82DF758 mov r0, r1 _081DD434: sub r3, r3, 0x1 - bl sub_81DD520 + bl sub_82DF758 sub r1, r1, r0 _081DD440: adds r5, r5, 0x40000000 @@ -663,10 +663,10 @@ _081DD4F4: str r7, [r5, 0x630] str r6, [r5], 0x4 pop {r8,r12,pc} - arm_func_end sub_81DD264 + arm_func_end sub_82DF49C - arm_func_start sub_81DD520 -sub_81DD520: + arm_func_start sub_82DF758 +sub_82DF758: push {r0,r2,r5-r7,lr} mov r0, r3, lsr 6 ldr r1, [r4, o_SoundChannel_xpi] @@ -704,7 +704,7 @@ _081DD594: ldrsb r1, [r5, r0] pop {r0,r2,r5-r7,pc} .pool - arm_func_end sub_81DD520 + arm_func_end sub_82DF758 thumb_func_start SoundMainBTM SoundMainBTM: diff --git a/asm/main.s b/asm/main.s index d70b1ff96..9c30a3f95 100644 --- a/asm/main.s +++ b/asm/main.s @@ -693,7 +693,7 @@ Reset: @ 80008F4 ands r2, r1 strh r2, [r0, 0xA] ldrh r0, [r0, 0xA] - bl RTC_SetReadOnly + bl SiiRtcProtect movs r0, 0xFF bl SoftReset pop {r4} diff --git a/asm/rtc.s b/asm/rtc.s index 1d7c508ae..dc63514fe 100644 --- a/asm/rtc.s +++ b/asm/rtc.s @@ -202,8 +202,8 @@ GameFreakRTC_Init: @ 802F21C movs r0, 0 strh r0, [r5] bl GameFreakRTC_ClearIME - bl RTC_SetReadWrite - bl RTC_Init + bl SiiRtcUnprotect + bl SiiRtcProbe ldr r4, =gUnknown_03000DCC strb r0, [r4] bl GameFreakRTC_RestoreIME @@ -281,7 +281,7 @@ GameFreakRTC_GetRTCDateTimeInternal: @ 802F2B8 adds r4, r0, 0 bl GameFreakRTC_ClearIME adds r0, r4, 0 - bl RTC_GetDateTime + bl SiiRtcGetDateTime bl GameFreakRTC_RestoreIME pop {r4} pop {r0} @@ -295,7 +295,7 @@ GameFreakRTC_GetControlReg: @ 802F2D0 adds r4, r0, 0 bl GameFreakRTC_ClearIME adds r0, r4, 0 - bl RTC_GetControlReg + bl SiiRtcGetStatus bl GameFreakRTC_RestoreIME pop {r4} pop {r0} @@ -452,7 +452,7 @@ _0802F3E6: GameFreakRTC_Reset: @ 802F3F8 push {lr} bl GameFreakRTC_ClearIME - bl RTC_Reset + bl SiiRtcReset bl GameFreakRTC_RestoreIME pop {r0} bx r0 diff --git a/include/siirtc.h b/include/siirtc.h new file mode 100644 index 000000000..5864f95b8 --- /dev/null +++ b/include/siirtc.h @@ -0,0 +1,54 @@ +#ifndef GUARD_RTC_H +#define GUARD_RTC_H + +#include "gba/gba.h" + +#define SIIRTCINFO_INTFE 0x01 // frequency interrupt enable +#define SIIRTCINFO_INTME 0x02 // per-minute interrupt enable +#define SIIRTCINFO_INTAE 0x04 // alarm interrupt enable +#define SIIRTCINFO_24HOUR 0x40 // 0: 12-hour mode, 1: 24-hour mode +#define SIIRTCINFO_POWER 0x80 // power on or power failure occurred + +enum +{ + MONTH_JAN = 1, + MONTH_FEB, + MONTH_MAR, + MONTH_APR, + MONTH_MAY, + MONTH_JUN, + MONTH_JUL, + MONTH_AUG, + MONTH_SEP, + MONTH_OCT, + MONTH_NOV, + MONTH_DEC +}; + +struct SiiRtcInfo +{ + u8 year; + u8 month; + u8 day; + u8 dayOfWeek; + u8 hour; + u8 minute; + u8 second; + u8 status; + u8 alarmHour; + u8 alarmMinute; +}; + +void SiiRtcUnprotect(); +void SiiRtcProtect(); +u8 SiiRtcProbe(); +bool8 SiiRtcReset(); +bool8 SiiRtcGetStatus(struct SiiRtcInfo *rtc); +bool8 SiiRtcSetStatus(struct SiiRtcInfo *rtc); +bool8 SiiRtcGetDateTime(struct SiiRtcInfo *rtc); +bool8 SiiRtcSetDateTime(struct SiiRtcInfo *rtc); +bool8 SiiRtcGetTime(struct SiiRtcInfo *rtc); +bool8 SiiRtcSetTime(struct SiiRtcInfo *rtc); +bool8 SiiRtcSetAlarm(struct SiiRtcInfo *rtc); + +#endif // GUARD_RTC_H diff --git a/ld_script.txt b/ld_script.txt index d22d0ddc9..31b058496 100644 --- a/ld_script.txt +++ b/ld_script.txt @@ -215,7 +215,7 @@ SECTIONS { src/agb_flash.o(.text); src/agb_flash_1m.o(.text); src/agb_flash_mx.o(.text); - asm/librtc.o(.text); + src/siirtc.o(.text); asm/librfu.o(.text); asm/libagbsyscall.o(.text); tools/agbcc/lib/libgcc.a:_call_via_rX.o(.text); @@ -251,7 +251,7 @@ SECTIONS { src/agb_flash_1m.o(.rodata); src/agb_flash_mx.o(.rodata); src/agb_flash_le.o(.rodata); - data/librtc_rodata.o(.rodata); + src/siirtc.o(.rodata); data/librfu_rodata.o(.rodata); tools/agbcc/lib/libgcc.a:_divdi3.o(.rodata); tools/agbcc/lib/libgcc.a:_udivdi3.o(.rodata); diff --git a/src/siirtc.c b/src/siirtc.c new file mode 100644 index 000000000..965a068f1 --- /dev/null +++ b/src/siirtc.c @@ -0,0 +1,432 @@ +// Ruby/Sapphire/Emerald cartridges contain a Seiko Instruments Inc. (SII) +// S-3511A real-time clock (RTC). This library ("SIIRTC_V001") is for +// communicating with the RTC. + +#include "gba/gba.h" +#include "siirtc.h" + +#define STATUS_INTFE 0x02 // frequency interrupt enable +#define STATUS_INTME 0x08 // per-minute interrupt enable +#define STATUS_INTAE 0x20 // alarm interrupt enable +#define STATUS_24HOUR 0x40 // 0: 12-hour mode, 1: 24-hour mode +#define STATUS_POWER 0x80 // power on or power failure occurred + +#define TEST_MODE 0x80 // flag in the "second" byte + +#define ALARM_AM 0x00 +#define ALARM_PM 0x80 + +#define OFFSET_YEAR offsetof(struct SiiRtcInfo, year) +#define OFFSET_MONTH offsetof(struct SiiRtcInfo, month) +#define OFFSET_DAY offsetof(struct SiiRtcInfo, day) +#define OFFSET_DAY_OF_WEEK offsetof(struct SiiRtcInfo, dayOfWeek) +#define OFFSET_HOUR offsetof(struct SiiRtcInfo, hour) +#define OFFSET_MINUTE offsetof(struct SiiRtcInfo, minute) +#define OFFSET_SECOND offsetof(struct SiiRtcInfo, second) +#define OFFSET_STATUS offsetof(struct SiiRtcInfo, status) +#define OFFSET_ALARM_HOUR offsetof(struct SiiRtcInfo, alarmHour) +#define OFFSET_ALARM_MINUTE offsetof(struct SiiRtcInfo, alarmMinute) + +#define INFO_BUF(info, index) (*((u8 *)(info) + (index))) + +#define DATETIME_BUF(info, index) INFO_BUF(info, OFFSET_YEAR + index) +#define DATETIME_BUF_LEN (OFFSET_SECOND - OFFSET_YEAR + 1) + +#define TIME_BUF(info, index) INFO_BUF(info, OFFSET_HOUR + index) +#define TIME_BUF_LEN (OFFSET_SECOND - OFFSET_HOUR + 1) + +#define WR 0 // command for writing data +#define RD 1 // command for reading data + +#define CMD(n) (0x60 | (n << 1)) + +#define CMD_RESET CMD(0) +#define CMD_STATUS CMD(1) +#define CMD_DATETIME CMD(2) +#define CMD_TIME CMD(3) +#define CMD_ALARM CMD(4) + +#define GPIO_PORT_DATA (*(vu16 *)0x80000C4) +#define GPIO_PORT_DIRECTION (*(vu16 *)0x80000C6) +#define GPIO_PORT_READ_ENABLE (*(vu16 *)0x80000C8) + +extern vu16 GPIOPortDirection; + +static u16 sDummy; // unused variable +static bool8 sLocked; + +static int WriteCommand(u8 value); +static int WriteData(u8 value); +static u8 ReadData(); +static void EnableGpioPortRead(); +static void DisableGpioPortRead(); + +static const char AgbLibRtcVersion[] = "SIIRTC_V001"; + +void SiiRtcUnprotect() +{ + EnableGpioPortRead(); + sLocked = FALSE; +} + +void SiiRtcProtect() +{ + DisableGpioPortRead(); + sLocked = TRUE; +} + +u8 SiiRtcProbe() +{ + u8 errorCode; + struct SiiRtcInfo rtc; + + if (!SiiRtcGetStatus(&rtc)) + return 0; + + errorCode = 0; + + if ((rtc.status & (SIIRTCINFO_POWER | SIIRTCINFO_24HOUR)) == SIIRTCINFO_POWER + || (rtc.status & (SIIRTCINFO_POWER | SIIRTCINFO_24HOUR)) == 0) + { + // The RTC is in 12-hour mode. Reset it and switch to 24-hour mode. + + // Note that the conditions are redundant and equivalent to simply + // "(rtc.status & SIIRTCINFO_24HOUR) == 0". It's possible that this + // was also intended to handle resetting the clock after power failure + // but a mistake was made. + + if (!SiiRtcReset()) + return 0; + + errorCode++; + } + + SiiRtcGetTime(&rtc); + + if (rtc.second & TEST_MODE) + { + // The RTC is in test mode. Reset it to leave test mode. + + if (!SiiRtcReset()) + return (errorCode << 4) & 0xF0; + + errorCode++; + } + + return (errorCode << 4) | 1; +} + +bool8 SiiRtcReset() +{ + u8 result; + struct SiiRtcInfo rtc; + + if (sLocked == TRUE) + return FALSE; + + sLocked = TRUE; + + GPIO_PORT_DATA = 1; + GPIO_PORT_DATA = 5; + + GPIO_PORT_DIRECTION = 7; + + WriteCommand(CMD_RESET | WR); + + GPIO_PORT_DATA = 1; + GPIO_PORT_DATA = 1; + + sLocked = FALSE; + + rtc.status = SIIRTCINFO_24HOUR; + + result = SiiRtcSetStatus(&rtc); + + return result; +} + +bool8 SiiRtcGetStatus(struct SiiRtcInfo *rtc) +{ + u8 statusData; + + if (sLocked == TRUE) + return FALSE; + + sLocked = TRUE; + + GPIO_PORT_DATA = 1; + GPIO_PORT_DATA = 5; + + GPIO_PORT_DIRECTION = 7; + + WriteCommand(CMD_STATUS | RD); + + GPIO_PORT_DIRECTION = 5; + + statusData = ReadData(); + + rtc->status = (statusData & (STATUS_POWER | STATUS_24HOUR)) + | ((statusData & STATUS_INTAE) >> 3) + | ((statusData & STATUS_INTME) >> 2) + | ((statusData & STATUS_INTFE) >> 1); + + GPIO_PORT_DATA = 1; + GPIO_PORT_DATA = 1; + + sLocked = FALSE; + + return TRUE; +} + +bool8 SiiRtcSetStatus(struct SiiRtcInfo *rtc) +{ + u8 statusData; + + if (sLocked == TRUE) + return FALSE; + + sLocked = TRUE; + + GPIO_PORT_DATA = 1; + GPIO_PORT_DATA = 5; + + statusData = STATUS_24HOUR + | ((rtc->status & SIIRTCINFO_INTAE) << 3) + | ((rtc->status & SIIRTCINFO_INTME) << 2) + | ((rtc->status & SIIRTCINFO_INTFE) << 1); + + GPIO_PORT_DIRECTION = 7; + + WriteCommand(CMD_STATUS | WR); + + WriteData(statusData); + + GPIO_PORT_DATA = 1; + GPIO_PORT_DATA = 1; + + sLocked = FALSE; + + return TRUE; +} + +bool8 SiiRtcGetDateTime(struct SiiRtcInfo *rtc) +{ + u8 i; + + if (sLocked == TRUE) + return FALSE; + + sLocked = TRUE; + + GPIO_PORT_DATA = 1; + GPIO_PORT_DATA = 5; + + GPIO_PORT_DIRECTION = 7; + + WriteCommand(CMD_DATETIME | RD); + + GPIO_PORT_DIRECTION = 5; + + for (i = 0; i < DATETIME_BUF_LEN; i++) + DATETIME_BUF(rtc, i) = ReadData(); + + INFO_BUF(rtc, OFFSET_HOUR) &= 0x7F; + + GPIO_PORT_DATA = 1; + GPIO_PORT_DATA = 1; + + sLocked = FALSE; + + return TRUE; +} + +bool8 SiiRtcSetDateTime(struct SiiRtcInfo *rtc) +{ + u8 i; + + if (sLocked == TRUE) + return FALSE; + + sLocked = TRUE; + + GPIO_PORT_DATA = 1; + GPIO_PORT_DATA = 5; + + GPIO_PORT_DIRECTION = 7; + + WriteCommand(CMD_DATETIME | WR); + + for (i = 0; i < DATETIME_BUF_LEN; i++) + WriteData(DATETIME_BUF(rtc, i)); + + GPIO_PORT_DATA = 1; + GPIO_PORT_DATA = 1; + + sLocked = FALSE; + + return TRUE; +} + +bool8 SiiRtcGetTime(struct SiiRtcInfo *rtc) +{ + u8 i; + + if (sLocked == TRUE) + return FALSE; + + sLocked = TRUE; + + GPIO_PORT_DATA = 1; + GPIO_PORT_DATA = 5; + + GPIO_PORT_DIRECTION = 7; + + WriteCommand(CMD_TIME | RD); + + GPIO_PORT_DIRECTION = 5; + + for (i = 0; i < TIME_BUF_LEN; i++) + TIME_BUF(rtc, i) = ReadData(); + + INFO_BUF(rtc, OFFSET_HOUR) &= 0x7F; + + GPIO_PORT_DATA = 1; + GPIO_PORT_DATA = 1; + + sLocked = FALSE; + + return TRUE; +} + +bool8 SiiRtcSetTime(struct SiiRtcInfo *rtc) +{ + u8 i; + + if (sLocked == TRUE) + return FALSE; + + sLocked = TRUE; + + GPIO_PORT_DATA = 1; + GPIO_PORT_DATA = 5; + + GPIO_PORT_DIRECTION = 7; + + WriteCommand(CMD_TIME | WR); + + for (i = 0; i < TIME_BUF_LEN; i++) + WriteData(TIME_BUF(rtc, i)); + + GPIO_PORT_DATA = 1; + GPIO_PORT_DATA = 1; + + sLocked = FALSE; + + return TRUE; +} + +bool8 SiiRtcSetAlarm(struct SiiRtcInfo *rtc) +{ + u8 i; + u8 alarmData[2]; + + if (sLocked == TRUE) + return FALSE; + + sLocked = TRUE; + + // Decode BCD. + alarmData[0] = (rtc->alarmHour & 0xF) + 10 * ((rtc->alarmHour >> 4) & 0xF); + + // The AM/PM flag must be set correctly even in 24-hour mode. + + if (alarmData[0] < 12) + alarmData[0] = rtc->alarmHour | ALARM_AM; + else + alarmData[0] = rtc->alarmHour | ALARM_PM; + + alarmData[1] = rtc->alarmMinute; + + GPIO_PORT_DATA = 1; + GPIO_PORT_DATA = 5; + + GPIOPortDirection = 7; // Why is this the only instance that uses a symbol? + + WriteCommand(CMD_ALARM | WR); + + for (i = 0; i < 2; i++) + WriteData(alarmData[i]); + + GPIO_PORT_DATA = 1; + GPIO_PORT_DATA = 1; + + sLocked = FALSE; + + return TRUE; +} + +static int WriteCommand(u8 value) +{ + u8 i; + u8 temp; + + for (i = 0; i < 8; i++) + { + temp = ((value >> (7 - i)) & 1); + GPIO_PORT_DATA = (temp << 1) | 4; + GPIO_PORT_DATA = (temp << 1) | 4; + GPIO_PORT_DATA = (temp << 1) | 4; + GPIO_PORT_DATA = (temp << 1) | 5; + } + + // control reaches end of non-void function +} + +static int WriteData(u8 value) +{ + u8 i; + u8 temp; + + for (i = 0; i < 8; i++) + { + temp = ((value >> i) & 1); + GPIO_PORT_DATA = (temp << 1) | 4; + GPIO_PORT_DATA = (temp << 1) | 4; + GPIO_PORT_DATA = (temp << 1) | 4; + GPIO_PORT_DATA = (temp << 1) | 5; + } + + // control reaches end of non-void function +} + +static u8 ReadData() +{ + u8 i; + u8 temp; + u8 value; + + for (i = 0; i < 8; i++) + { + GPIO_PORT_DATA = 4; + GPIO_PORT_DATA = 4; + GPIO_PORT_DATA = 4; + GPIO_PORT_DATA = 4; + GPIO_PORT_DATA = 4; + GPIO_PORT_DATA = 5; + + temp = ((GPIO_PORT_DATA & 2) >> 1); + value = (value >> 1) | (temp << 7); // UB: accessing uninitialized var + } + + return value; +} + +static void EnableGpioPortRead() +{ + GPIO_PORT_READ_ENABLE = 1; +} + +static void DisableGpioPortRead() +{ + GPIO_PORT_READ_ENABLE = 0; +} diff --git a/sym_bss.txt b/sym_bss.txt index 45005362c..734c4ec4c 100644 --- a/sym_bss.txt +++ b/sym_bss.txt @@ -497,11 +497,6 @@ gUnknown_03001300: @ 3001300 .space 0x770 .include "src/agb_flash.o" - - .space 0x2 - -gUnknown_03001A7E: @ 3001A7E - .space 0x2 - + .include "src/siirtc.o" .include "tools/agbcc/lib/libgcc.a:dp-bit.o" .include "tools/agbcc/lib/libgcc.a:fp-bit.o"