summaryrefslogtreecommitdiff
path: root/src/code/m_string.c
blob: 7b8e5b50fbb88329efc5bf1f56c1a70cef5c4175 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include "m_string.h"
#include "m_std_dma.h"
#include "m_lib.h"

#include "m_msg_main.h"
#include "6B3DC0.h"

extern u32 D_D18000[];
extern u32 D_D16000;
void mString_Get_StringDataAddressAndSize(s32 idx, uintptr_t* vramp, size_t* size) {
    s32 sized;
    uintptr_t offsetPtrs[5];

    if (idx >= 0 && idx < 0x61A) {
        uintptr_t vrom;
        if (idx & 1) {
            DmaMgr_RequestSyncDebug(offsetPtrs, (u32)&D_D18000[idx - 1], 8, "../m_string.c", 62);
            vrom = offsetPtrs[0];
            sized = offsetPtrs[1] - vrom;
        } else if (idx == 0) {
            DmaMgr_RequestSyncDebug(offsetPtrs, (u32)&D_D18000, 8, "../m_string.c", 69);
            vrom = 0;
            sized = offsetPtrs[0] - vrom;
        } else {
            DmaMgr_RequestSyncDebug(offsetPtrs, (u32)&D_D18000[idx - 2], 16, "../m_string.c", 75);
            vrom = offsetPtrs[1];
            sized = offsetPtrs[2] - vrom;
        }
        if (sized < 65) {
            *vramp = (uintptr_t)vrom + (u32)&D_D16000;
            *size = sized;
        } else {
            *vramp = NULL;
            *size = 0;
        }
    } else {
        *vramp = NULL;
        *size = 0;
    }
}

void mString_Load_StringFromRom(char* dst, s32 dstLen, s32 strIdx) {
    uintptr_t stringAddr;
    size_t stringSize;

    if (strIdx >= 0 && strIdx < 0x61A) {
        mString_Get_StringDataAddressAndSize(strIdx, &stringAddr, &stringSize);

        if (stringSize == 0) {
            mem_clear((u8*)dst, dstLen, ' ');
        } else if (stringAddr != 0) {
            char buff[80];
            u32 baseAddr = stringAddr & ~7;
            s32 ofs = stringAddr - baseAddr;
            s32 i;
            char* strSrc;
            s32 sizeCopy;
            char* strDst;
            UNUSED s32 pad;

            DmaMgr_RequestSyncDebug(buff, baseAddr, ALIGN8(ofs + stringSize), "../m_string.c", 153);
            if (dstLen < (s32)stringSize) {
                stringSize = dstLen;
            }

            strDst = dst;
            sizeCopy = stringSize;
            strSrc = &buff[ofs];
            for (i = 0; i < sizeCopy; i++) {
                *strDst++ = *strSrc++;
            }

            for (; i < dstLen; i++) {
                *strDst++ = ' ';
            }
        }
    }
}

s32 mString_Load_YearStringFromRom(char* dst, lbRTC_year_t year) {
    s32 idx;

    if (year < lbRTC_YEAR_MIN || year > lbRTC_YEAR_MAX) {
        year = GAME_YEAR_MIN;
    }

    idx = mFont_UnintToString(dst, year, 6, TRUE, FALSE);
    mString_Load_StringFromRom(&dst[idx], 6 - idx, 3);
    return mMsg_Get_Length_String(dst, 6);
}

s32 mString_Load_MonthStringFromRom(char* dst, lbRTC_month_t month) {
    s32 idx;

    if (month < lbRTC_JANUARY || month > lbRTC_DECEMBER) {
        month = lbRTC_JANUARY;
    }

    idx = mFont_UnintToString(dst, month, 4, TRUE, FALSE);
    mString_Load_StringFromRom(&dst[idx], 4 - idx, 4);
    return mMsg_Get_Length_String(dst, 4);
}

s32 mString_Load_WeekStringFromRom(char* dst, lbRTC_weekday_t weekday) {
    if (weekday > lbRTC_SATURDAY) {
        weekday = 0;
    }

    mString_Load_StringFromRom(dst, 5, weekday + 9);
    return mMsg_Get_Length_String(dst, 4);
}

s32 mString_Load_DayStringFromRom(char* dst, lbRTC_day_t day) {
    s32 idx;

    if (day < 1 || day > 31) {
        day = 1;
    }

    idx = mFont_UnintToString(dst, day, 4, TRUE, FALSE);
    mString_Load_StringFromRom(&dst[idx], 4 - idx, 5);
    return mMsg_Get_Length_String(dst, 4);
}

s32 mString_Load_HourStringFromRom(char* dst, lbRTC_hour_t hour) {
    UNUSED s32 pad;
    s32 specifier;
    s32 len;

    if (hour >= lbRTC_HOURS_PER_DAY) {
        hour = 0;
    }

    specifier = hour < 12 ? 1 : 2;

    mString_Load_StringFromRom(dst, 6, specifier);
    len = mMsg_Get_Length_String(dst, 6);
    len += mFont_UnintToString(dst + len, hour % 12, 6 - len, TRUE, FALSE);

    mString_Load_StringFromRom(dst + len, 6 - len, 6);
    return mMsg_Get_Length_String(dst, 6);
}

s32 mString_Load_MinStringFromRom(char* dst, lbRTC_min_t min) {
    s32 idx;

    if (min >= lbRTC_MINUTES_PER_HOUR) {
        min = 0;
    }

    idx = mFont_UnintToString(dst, min, 4, TRUE, FALSE);
    mString_Load_StringFromRom(&dst[idx], 4 - idx, 7);
    return mMsg_Get_Length_String(dst, 4);
}

s32 mString_Load_SecStringFromRom(char* dst, lbRTC_sec_t sec) {
    s32 idx;

    if (sec >= lbRTC_MINUTES_PER_HOUR) {
        sec = 0;
    }

    idx = mFont_UnintToString(dst, sec, 5, TRUE, FALSE);
    mString_Load_StringFromRom(&dst[idx], 5 - idx, 8);
    return mMsg_Get_Length_String(dst, 5);
}

s32 mString_Load_NumberStringAddUnitFromRom(char* dst, u16 num, s32 idx) {
    s32 len = mFont_UnintToString(dst, num, 10, TRUE, FALSE);

    mString_Load_StringFromRom(&dst[len], 10 - len, idx);
    return mMsg_Get_Length_String(dst, 10);
}