summaryrefslogtreecommitdiff
path: root/arm9/src
diff options
context:
space:
mode:
Diffstat (limited to 'arm9/src')
-rw-r--r--arm9/src/error_handling.c2
-rw-r--r--arm9/src/filesystem.c20
-rw-r--r--arm9/src/gf_rtc.c181
-rw-r--r--arm9/src/heap.c24
-rw-r--r--arm9/src/main.c2
-rw-r--r--arm9/src/poke_overlay.c16
-rw-r--r--arm9/src/scrcmd_24.c6
-rw-r--r--arm9/src/script.c2
-rw-r--r--arm9/src/script_pokemon_util.c2
-rw-r--r--arm9/src/unk_0202E29C.c2
-rw-r--r--arm9/src/unk_0202F150.c8
11 files changed, 223 insertions, 42 deletions
diff --git a/arm9/src/error_handling.c b/arm9/src/error_handling.c
index 7f006039..0b76ccff 100644
--- a/arm9/src/error_handling.c
+++ b/arm9/src/error_handling.c
@@ -5,7 +5,7 @@
-THUMB_FUNC void ErrorHandling(void)
+THUMB_FUNC void GF_AssertFail(void)
{
if (FUN_02031810())
{
diff --git a/arm9/src/filesystem.c b/arm9/src/filesystem.c
index 3775b0b8..ed82acb9 100644
--- a/arm9/src/filesystem.c
+++ b/arm9/src/filesystem.c
@@ -176,7 +176,7 @@ THUMB_FUNC void ReadFromNarcMemberByPathAndId(void * dest, const char * path, s3
FS_ReadFile(&file, &chunk_size, 4);
FS_ReadFile(&file, &num_files, 2);
if (num_files <= file_idx)
- ErrorHandling();
+ GF_AssertFail();
chunk_starts[1] = chunk_starts[0] + chunk_size;
FS_SeekFile(&file, (s32)(chunk_starts[1] + 4), FS_SEEK_SET);
FS_ReadFile(&file, &chunk_size, 4);
@@ -191,7 +191,7 @@ THUMB_FUNC void ReadFromNarcMemberByPathAndId(void * dest, const char * path, s3
else
chunk_size = size;
if (chunk_size == 0)
- ErrorHandling();
+ GF_AssertFail();
FS_ReadFile(&file, dest, (s32)chunk_size);
FS_CloseFile(&file);
}
@@ -217,7 +217,7 @@ THUMB_FUNC void * AllocAndReadFromNarcMemberByPathAndId(const char * path, s32 f
FS_ReadFile(&file, &chunk_size, 4);
FS_ReadFile(&file, &num_files, 2);
if (num_files <= file_idx)
- ErrorHandling();
+ GF_AssertFail();
chunk_starts[1] = chunk_starts[0] + chunk_size;
FS_SeekFile(&file, (s32)(chunk_starts[1] + 4), FS_SEEK_SET);
FS_ReadFile(&file, &chunk_size, 4);
@@ -232,7 +232,7 @@ THUMB_FUNC void * AllocAndReadFromNarcMemberByPathAndId(const char * path, s32 f
else
chunk_size = size;
if (chunk_size == 0)
- ErrorHandling();
+ GF_AssertFail();
switch (r4)
{
case 0:
@@ -297,7 +297,7 @@ THUMB_FUNC u32 GetNarcMemberSizeByIdPair(NarcId narc_id, s32 file_idx)
FS_ReadFile(&file, &chunk_size, 4);
FS_ReadFile(&file, &num_files, 2);
if (num_files <= file_idx)
- ErrorHandling();
+ GF_AssertFail();
chunk_starts[1] = chunk_starts[0] + chunk_size;
FS_SeekFile(&file, (s32)(chunk_starts[1] + 4), FS_SEEK_SET);
FS_ReadFile(&file, &chunk_size, 4);
@@ -309,7 +309,7 @@ THUMB_FUNC u32 GetNarcMemberSizeByIdPair(NarcId narc_id, s32 file_idx)
FS_SeekFile(&file, (s32)(chunk_starts[2] + 8 + file_start + 0), FS_SEEK_SET);
chunk_size = file_end - file_start;
if (chunk_size == 0)
- ErrorHandling();
+ GF_AssertFail();
// Bug: File is never closed
return chunk_size;
}
@@ -349,7 +349,7 @@ THUMB_FUNC void * NARC_AllocAndReadWholeMember(NARC * narc, u32 file_id, u32 hea
u32 file_end;
void * dest;
if (narc->num_files <= file_id)
- ErrorHandling();
+ GF_AssertFail();
FS_SeekFile(&narc->file, (s32)(narc->btaf_start + 12 + 8 * file_id), FS_SEEK_SET);
FS_ReadFile(&narc->file, &file_start, 4);
FS_ReadFile(&narc->file, &file_end, 4);
@@ -367,7 +367,7 @@ THUMB_FUNC void NARC_ReadWholeMember(NARC * narc, u32 file_id, void * dest)
u32 file_start;
u32 file_end;
if (narc->num_files <= file_id)
- ErrorHandling();
+ GF_AssertFail();
FS_SeekFile(&narc->file, (s32)(narc->btaf_start + 12 + 8 * file_id), FS_SEEK_SET);
FS_ReadFile(&narc->file, &file_start, 4);
FS_ReadFile(&narc->file, &file_end, 4);
@@ -380,7 +380,7 @@ THUMB_FUNC u32 NARC_GetMemberSize(NARC * narc, u32 file_id)
u32 file_start;
u32 file_end;
if (narc->num_files <= file_id)
- ErrorHandling();
+ GF_AssertFail();
FS_SeekFile(&narc->file, (s32)(narc->btaf_start + 12 + 8 * file_id), FS_SEEK_SET);
FS_ReadFile(&narc->file, &file_start, 4);
FS_ReadFile(&narc->file, &file_end, 4);
@@ -391,7 +391,7 @@ THUMB_FUNC void NARC_ReadFromMember(NARC * narc, u32 file_id, u32 pos, u32 size,
{
u32 file_start;
if (narc->num_files <= file_id)
- ErrorHandling();
+ GF_AssertFail();
FS_SeekFile(&narc->file, (s32)(narc->btaf_start + 12 + 8 * file_id), FS_SEEK_SET);
FS_ReadFile(&narc->file, &file_start, 4);
FS_SeekFile(&narc->file, (s32)(narc->gmif_start + 8 + file_start + pos), FS_SEEK_SET);
diff --git a/arm9/src/gf_rtc.c b/arm9/src/gf_rtc.c
new file mode 100644
index 00000000..0fc77efb
--- /dev/null
+++ b/arm9/src/gf_rtc.c
@@ -0,0 +1,181 @@
+#include "global.h"
+#include "MI_memory.h"
+#include "gf_rtc.h"
+#include "RTC_convert.h"
+
+typedef struct GF_RTC_Work
+{
+ BOOL getDateTimeSuccess;
+ BOOL getDateTimeLock;
+ s32 getDateTimeSleep;
+ RTCResult getDateTimeErrorCode;
+ RTCDate date; // 10
+ RTCTime time; // 20
+ RTCDate date_async; // 2C
+ RTCTime time_async; // 3C
+} GF_RTC_Work;
+
+GF_RTC_Work sGFRTCWork;
+
+void GF_RTC_GetDateTime(GF_RTC_Work * work);
+
+THUMB_FUNC void GF_InitRTCWork(void)
+{
+ RTC_Init();
+ __clear(&sGFRTCWork, sizeof(sGFRTCWork));
+ sGFRTCWork.getDateTimeSuccess = FALSE;
+ sGFRTCWork.getDateTimeLock = FALSE;
+ sGFRTCWork.getDateTimeSleep = 0;
+ GF_RTC_GetDateTime(&sGFRTCWork);
+}
+
+THUMB_FUNC void GF_RTC_UpdateOnFrame(void)
+{
+ if (!sGFRTCWork.getDateTimeLock)
+ {
+ if (++sGFRTCWork.getDateTimeSleep > 10)
+ {
+ sGFRTCWork.getDateTimeSleep = 0;
+ GF_RTC_GetDateTime(&sGFRTCWork);
+ }
+ }
+}
+
+THUMB_FUNC void GF_RTC_GetDateTime_Callback(RTCResult result, void * data)
+{
+ GF_RTC_Work * work = (GF_RTC_Work *)data;
+ work->getDateTimeErrorCode = result;
+ GF_ASSERT(result == RTC_RESULT_SUCCESS);
+ work->getDateTimeSuccess = TRUE;
+ work->date = work->date_async;
+ work->time = work->time_async;
+ work->getDateTimeLock = FALSE;
+}
+
+THUMB_FUNC void GF_RTC_GetDateTime(GF_RTC_Work * work)
+{
+ work->getDateTimeLock = TRUE;
+ RTCResult result = RTC_GetDateTimeAsync(&work->date_async, &work->time_async, GF_RTC_GetDateTime_Callback, work);
+ work->getDateTimeErrorCode = result;
+ GF_ASSERT(result == RTC_RESULT_SUCCESS);
+}
+
+THUMB_FUNC void GF_RTC_CopyDateTime(RTCDate * date, RTCTime * time)
+{
+ GF_ASSERT(sGFRTCWork.getDateTimeSuccess == TRUE);
+ *date = sGFRTCWork.date;
+ *time = sGFRTCWork.time;
+}
+
+THUMB_FUNC void GF_RTC_CopyTime(RTCTime * time)
+{
+ GF_ASSERT(sGFRTCWork.getDateTimeSuccess == TRUE);
+ *time = sGFRTCWork.time;
+}
+
+THUMB_FUNC void GF_RTC_CopyDate(RTCDate * date)
+{
+ GF_ASSERT(sGFRTCWork.getDateTimeSuccess == TRUE);
+ *date = sGFRTCWork.date;
+}
+
+THUMB_FUNC s32 GF_RTC_TimeToSec(void)
+{
+ RTCTime* time = &sGFRTCWork.time;
+ return 60 * time->minute + 3600 * time->hour + time->second;
+}
+
+THUMB_FUNC s64 GF_RTC_DateTimeToSec(void)
+{
+ return RTC_ConvertDateTimeToSecond(&sGFRTCWork.date, &sGFRTCWork.time);
+}
+
+static inline BOOL IsLeapYear(s32 year)
+{
+ return ((year % 4) == 0 && (year % 100) != 0) || ((year % 400) == 0);
+}
+
+THUMB_FUNC s32 FUN_02012710(const RTCDate * date)
+{
+ RTCDate date_stack;
+ s32 days;
+ static const u16 sGF_DaysPerMonth[] = {
+ 0, // Jan
+ 31, // Feb
+ 59, // Mar
+ 90, // Apr
+ 120, // May
+ 151, // Jun
+ 181, // Jul
+ 212, // Aug
+ 243, // Sep
+ 273, // Oct
+ 304, // Nov
+ 334, // Dec
+ };
+
+ days = date->day;
+ days += sGF_DaysPerMonth[date->month - 1];
+ if (date->month >= RTC_MONTH_MARCH && IsLeapYear(date->year))
+ days++;
+ date_stack = *date;
+ date_stack.month = RTC_MONTH_JANUARY;
+ date_stack.day = 1;
+ RTC_ConvertDateToDay(&date_stack);
+ RTC_ConvertDateToDay(date);
+ return days;
+}
+
+enum RTC_TimeOfDay GF_RTC_GetTimeOfDay(void);
+enum RTC_TimeOfDay GF_RTC_GetTimeOfDayByHour(s32 hour);
+
+THUMB_FUNC BOOL IsNighttime(void)
+{
+ switch (GF_RTC_GetTimeOfDay())
+ {
+ case RTC_TIMEOFDAY_NITE:
+ case RTC_TIMEOFDAY_LATE:
+ return TRUE;
+ default:
+ return FALSE;
+ }
+}
+
+THUMB_FUNC enum RTC_TimeOfDay GF_RTC_GetTimeOfDay(void)
+{
+ RTCTime time;
+ GF_RTC_CopyTime(&time);
+ return GF_RTC_GetTimeOfDayByHour(time.hour);
+}
+
+THUMB_FUNC enum RTC_TimeOfDay GF_RTC_GetTimeOfDayByHour(s32 hour)
+{
+ static const u8 sTimeOfDayByHour[] = {
+ // 00:00 - 03:59
+ RTC_TIMEOFDAY_LATE, RTC_TIMEOFDAY_LATE, RTC_TIMEOFDAY_LATE, RTC_TIMEOFDAY_LATE,
+ // 04:00 - 09:59
+ RTC_TIMEOFDAY_MORN, RTC_TIMEOFDAY_MORN, RTC_TIMEOFDAY_MORN, RTC_TIMEOFDAY_MORN, RTC_TIMEOFDAY_MORN, RTC_TIMEOFDAY_MORN,
+ // 10:00 - 16:59
+ RTC_TIMEOFDAY_DAY, RTC_TIMEOFDAY_DAY, RTC_TIMEOFDAY_DAY, RTC_TIMEOFDAY_DAY, RTC_TIMEOFDAY_DAY, RTC_TIMEOFDAY_DAY, RTC_TIMEOFDAY_DAY,
+ // 17:00 - 19:59
+ RTC_TIMEOFDAY_EVE, RTC_TIMEOFDAY_EVE, RTC_TIMEOFDAY_EVE,
+ // 20:00 - 23:59
+ RTC_TIMEOFDAY_NITE, RTC_TIMEOFDAY_NITE, RTC_TIMEOFDAY_NITE, RTC_TIMEOFDAY_NITE,
+ };
+
+ GF_ASSERT(hour >= 0 && hour < 24);
+ return sTimeOfDayByHour[hour];
+}
+
+THUMB_FUNC s64 FUN_020127C0(s64 r4r7, s64 r5r6)
+{
+ RTCDate maxDate = { 99, 12, 31, 0 };
+ RTCTime maxTime = { 23, 59, 59 };
+
+ s64 r2r3 = RTC_ConvertDateTimeToSecond(&maxDate, &maxTime);
+ GF_ASSERT(r2r3 == 3155759999ll);
+ if (r4r7 < r5r6)
+ return r5r6 - r4r7;
+ else
+ return r5r6 + (3155759999ll - r4r7);
+}
diff --git a/arm9/src/heap.c b/arm9/src/heap.c
index 7ae25c3b..7f35fdcc 100644
--- a/arm9/src/heap.c
+++ b/arm9/src/heap.c
@@ -73,7 +73,7 @@ THUMB_FUNC void FUN_020166C8(u32 *param0, u32 param1, u32 param2, u32 pre_size)
}
else
{
- ErrorHandling();
+ GF_AssertFail();
}
param0 += 2;
@@ -162,27 +162,27 @@ THUMB_FUNC u32 FUN_02016834(u32 param0, u32 param1, u32 param2, s32 param3)
}
else
{
- ErrorHandling();
+ GF_AssertFail();
}
}
else
{
- ErrorHandling();
+ GF_AssertFail();
}
}
else
{
- ErrorHandling();
+ GF_AssertFail();
}
}
else
{
- ErrorHandling();
+ GF_AssertFail();
}
}
else
{
- ErrorHandling();
+ GF_AssertFail();
}
return 0;
}
@@ -204,7 +204,7 @@ THUMB_FUNC void FUN_020168D0(u32 heap_id)
}
else
{
- ErrorHandling();
+ GF_AssertFail();
}
UNK_021C4D28.unk00[UNK_021C4D28.unk10[heap_id]] = 0;
@@ -306,7 +306,7 @@ void FreeToHeap(void *ptr)
return;
}
- ErrorHandling();
+ GF_AssertFail();
}
void FreeToHeapExplicit(u32 param0, void *param1)
@@ -329,7 +329,7 @@ void FreeToHeapExplicit(u32 param0, void *param1)
return;
}
- ErrorHandling();
+ GF_AssertFail();
}
THUMB_FUNC u32 FUN_02016AF8(u32 param0)
@@ -340,7 +340,7 @@ THUMB_FUNC u32 FUN_02016AF8(u32 param0)
return NNS_FndGetTotalFreeSizeForExpHeap(UNK_021C4D28.unk00[index]);
}
- ErrorHandling();
+ GF_AssertFail();
return 0;
}
@@ -354,7 +354,7 @@ THUMB_FUNC void FUN_02016B20(u32 param0, u32 param1, u32 param2)
return;
}
- ErrorHandling();
+ GF_AssertFail();
}
THUMB_FUNC void FUN_02016B44(void *ptr, u32 param1)
@@ -371,7 +371,7 @@ THUMB_FUNC void FUN_02016B44(void *ptr, u32 param1)
NNS_FndResizeForMBlockExpHeap(UNK_021C4D28.unk00[index], ptr - 16, param1);
return;
}
- ErrorHandling();
+ GF_AssertFail();
}
THUMB_FUNC u32 FUN_02016B90(u32 param0)
diff --git a/arm9/src/main.c b/arm9/src/main.c
index 314c4509..7a8a14d8 100644
--- a/arm9/src/main.c
+++ b/arm9/src/main.c
@@ -170,7 +170,7 @@ THUMB_FUNC void Main_RunOverlayManager(void)
THUMB_FUNC void RegisterMainOverlay(FSOverlayID id, const struct Unk21DBE18 * arg1)
{
if (UNK_02016FA8.unkC != NULL)
- ErrorHandling();
+ GF_AssertFail();
UNK_02016FA8.unk8 = id;
UNK_02016FA8.unkC = arg1;
}
diff --git a/arm9/src/poke_overlay.c b/arm9/src/poke_overlay.c
index 4dd98670..e11ecbb6 100644
--- a/arm9/src/poke_overlay.c
+++ b/arm9/src/poke_overlay.c
@@ -9,9 +9,9 @@ static struct LoadedOverlay gLoadedOverlays[3][8];
THUMB_FUNC void FreeOverlayAllocation(struct LoadedOverlay * loaded)
{
if (loaded->active != TRUE)
- ErrorHandling();
+ GF_AssertFail();
if (FS_UnloadOverlay(MI_PROCESSOR_ARM9, loaded->id) != TRUE)
- ErrorHandling();
+ GF_AssertFail();
loaded->active = FALSE;
}
@@ -35,7 +35,7 @@ THUMB_FUNC s32 GetOverlayLoadDestination(FSOverlayID id)
u8 *end;
u8 *start;
if (FS_LoadOverlayInfo(&info, MI_PROCESSOR_ARM9, id) != TRUE)
- ErrorHandling();
+ GF_AssertFail();
start = (u8 *)HW_ITCM_IMAGE;
end = (u8 *)HW_ITCM_END;
if (info.header.ram_address <= end && info.header.ram_address >= start)
@@ -72,7 +72,7 @@ THUMB_FUNC BOOL HandleLoadOverlay(FSOverlayID id, s32 a1)
}
if (r6 >= 8)
{
- ErrorHandling();
+ GF_AssertFail();
return FALSE;
}
if (r7 == OVERLAY_LOAD_ITCM || r7 == OVERLAY_LOAD_DTCM)
@@ -91,7 +91,7 @@ THUMB_FUNC BOOL HandleLoadOverlay(FSOverlayID id, s32 a1)
result = LoadOverlayNoInitAsync(MI_PROCESSOR_ARM9, id);
break;
default:
- ErrorHandling();
+ GF_AssertFail();
return FALSE;
}
if (r7 == OVERLAY_LOAD_ITCM || r7 == OVERLAY_LOAD_DTCM)
@@ -100,7 +100,7 @@ THUMB_FUNC BOOL HandleLoadOverlay(FSOverlayID id, s32 a1)
}
if (result == 0)
{
- ErrorHandling();
+ GF_AssertFail();
return FALSE;
}
return TRUE;
@@ -120,7 +120,7 @@ THUMB_FUNC BOOL CanOverlayBeLoaded(FSOverlayID id)
{
if ((start >= start2 && start < end2) || (end > start2 && end <= end2) || (start <= start2 && end >= end2))
{
- ErrorHandling();
+ GF_AssertFail();
return FALSE;
}
}
@@ -146,7 +146,7 @@ THUMB_FUNC BOOL GetOverlayRamBounds(FSOverlayID id, void ** start, void ** end)
{
FSOverlayInfo info;
if (!FS_LoadOverlayInfo(&info, MI_PROCESSOR_ARM9, id)) {
- ErrorHandling();
+ GF_AssertFail();
return FALSE;
}
*start = (void *)info.header.ram_address;
diff --git a/arm9/src/scrcmd_24.c b/arm9/src/scrcmd_24.c
index d3f955d0..9c8f8d4b 100644
--- a/arm9/src/scrcmd_24.c
+++ b/arm9/src/scrcmd_24.c
@@ -62,7 +62,7 @@ THUMB_FUNC asm BOOL ScrCmd_Unk01C7(struct ScriptContext* ctx)
ldr r0, [r4, #0x0]
cmp r0, #0x0
bne _02045DFC
- bl ErrorHandling
+ bl GF_AssertFail
_02045DFC:
ldr r0, [r4, #0x0]
bl FUN_02037D5C
@@ -199,7 +199,7 @@ THUMB_FUNC asm BOOL ScrCmd_Unk0223(struct ScriptContext* ctx)
ldr r4, [r0, #0x0]
cmp r4, #0x0
bne _02045FB2
- bl ErrorHandling
+ bl GF_AssertFail
_02045FB2:
ldrb r0, [r4, #0x16]
cmp r0, #0x0
@@ -257,7 +257,7 @@ THUMB_FUNC asm BOOL ScrCmd_Unk0225(struct ScriptContext* ctx)
ldr r4, [r0, #0x0]
cmp r4, #0x0
bne _02045FB2
- bl ErrorHandling
+ bl GF_AssertFail
_02045FB2:
ldrb r0, [r4, #0x16]
cmp r0, #0x0
diff --git a/arm9/src/script.c b/arm9/src/script.c
index 2fa80058..3de2ee73 100644
--- a/arm9/src/script.c
+++ b/arm9/src/script.c
@@ -76,7 +76,7 @@ THUMB_FUNC u8 RunScriptCommand(struct ScriptContext *ctx)
cmdCode = ScriptReadHalfword(ctx);
if (cmdCode >= ctx->cmdCount)
{
- ErrorHandling();
+ GF_AssertFail();
ctx->mode = 0;
return FALSE;
}
diff --git a/arm9/src/script_pokemon_util.c b/arm9/src/script_pokemon_util.c
index a3518bf4..55519ca3 100644
--- a/arm9/src/script_pokemon_util.c
+++ b/arm9/src/script_pokemon_util.c
@@ -109,7 +109,7 @@ struct Pokemon * GetFirstAliveMonInParty_CrashIfNone(struct PlayerParty * party)
if(MonNotFaintedOrEgg(mon))
return mon;
}
- ErrorHandling();
+ GF_AssertFail();
return 0;
}
diff --git a/arm9/src/unk_0202E29C.c b/arm9/src/unk_0202E29C.c
index 938107f2..2b088d6f 100644
--- a/arm9/src/unk_0202E29C.c
+++ b/arm9/src/unk_0202E29C.c
@@ -432,7 +432,7 @@ THUMB_FUNC s32 FUN_0202E844(u32 param0)
count++;
}
- ErrorHandling();
+ GF_AssertFail();
return 0;
}
diff --git a/arm9/src/unk_0202F150.c b/arm9/src/unk_0202F150.c
index 1586dae5..09b5b56f 100644
--- a/arm9/src/unk_0202F150.c
+++ b/arm9/src/unk_0202F150.c
@@ -95,7 +95,7 @@ THUMB_FUNC u32 FUN_0202F150(u32 param0, u32 param1)
r4 = 1;
if (UNK_021C59F4.unk04 == 0)
{
- ErrorHandling();
+ GF_AssertFail();
}
}
@@ -1191,7 +1191,7 @@ THUMB_FUNC void FUN_020304D4(u32 param0)
return;
}
- ErrorHandling();
+ GF_AssertFail();
}
THUMB_FUNC void FUN_020304F0(u32 param0)
@@ -1202,7 +1202,7 @@ THUMB_FUNC void FUN_020304F0(u32 param0)
return;
}
- ErrorHandling();
+ GF_AssertFail();
}
THUMB_FUNC void FUN_0203050C()
@@ -1741,7 +1741,7 @@ THUMB_FUNC u32 FUN_02030B3C(u32 param0, u8 *param1, u32 param2)
{
if (FUN_02031190() != 0)
{
- ErrorHandling();
+ GF_AssertFail();
return 0;
}