summaryrefslogtreecommitdiff
path: root/arm9/lib/NitroSDK/src/CARD_common.c
blob: fd66c7ce5196c34ca08c18ad1db472b5f4c9db94 (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
174
175
176
177
178
179
180
181
#include "CARD_common.h"
#include "CARD_request.h"
#include "consts.h"
#include "function_target.h"
#include "MI_memory.h"
#include "OS_terminate_proc.h"
#include "OS_spinLock.h"
#include "OS_cache.h"
#include "MB_mb.h"
#include "PXI_fifo.h"
#include "mmap.h"

CARDiCommon cardi_common ALIGN(32);
static CARDiCommandArg cardi_arg ALIGN(32);

u8 cardi_thread_stack[0x400] ALIGN(4);

static void CARDi_LockResource(CARDiOwner owner, CARDTargetMode target);
static void CARDi_UnlockResource(CARDiOwner owner, CARDTargetMode target);

ARM_FUNC void CARDi_SetTask(void (*task) (CARDiCommon *))
{
    CARDiCommon *const p = &cardi_common;

    (void)OS_SetThreadPriority(p->thread, p->priority);

    p->cur_th = p->thread;
    p->task_func = task;
    p->flag |= CARD_STAT_TASK;
    OS_WakeupThreadDirect(p->thread);
}

ARM_FUNC static void CARDi_LockResource(CARDiOwner owner, CARDTargetMode target)
{
    CARDiCommon *const p = &cardi_common;
    OSIntrMode bak_psr = OS_DisableInterrupts();
    if (p->lock_owner == owner)
    {
        if (p->lock_target != target)
        {
            OS_Terminate();
        }
    }
    else
    {
        while (p->lock_owner != OS_LOCK_ID_ERROR)
        {
            OS_SleepThread(p->lock_queue);
        }
        p->lock_owner = owner;
        p->lock_target = target;
    }
    ++p->lock_ref;
    p->cmd->result = CARD_RESULT_SUCCESS;
    (void)OS_RestoreInterrupts(bak_psr);
}

ARM_FUNC static void CARDi_UnlockResource(CARDiOwner owner, CARDTargetMode target)
{
    CARDiCommon *p = &cardi_common;
    OSIntrMode bak_psr = OS_DisableInterrupts();
    if ((p->lock_owner != owner) || !p->lock_ref)
    {
        OS_Terminate();
    }
    else
    {
        if (p->lock_target != target)
        {
            OS_Terminate();
        }
        if (!--p->lock_ref)
        {
            p->lock_owner = OS_LOCK_ID_ERROR;
            p->lock_target = CARD_TARGET_NONE;
            OS_WakeupThread(p->lock_queue);
        }
    }
    p->cmd->result = CARD_RESULT_SUCCESS;
    (void)OS_RestoreInterrupts(bak_psr);
}

ARM_FUNC void CARDi_InitCommon(void)
{
    CARDiCommon *p = &cardi_common;

    p->lock_owner = OS_LOCK_ID_ERROR;
    p->lock_ref = 0;
    p->lock_target = CARD_TARGET_NONE;

    p->cmd = &cardi_arg;
    MI_CpuFillFast(&cardi_arg, 0x00, sizeof(cardi_arg));
    DC_FlushRange(&cardi_arg, sizeof(cardi_arg));

    if (!MB_IsMultiBootChild())
    {
        MI_CpuCopy8((const void *)HW_ROM_HEADER_BUF, (void *)HW_CARD_ROM_HEADER, HW_CARD_ROM_HEADER_SIZE);
    }
    OS_InitThreadQueue(p->lock_queue);
    OS_InitThreadQueue(p->busy_q);
    p->priority = CARD_THREAD_PRIORITY_DEFAULT;
    OS_CreateThread(p->thread, CARDi_TaskThread, NULL, cardi_thread_stack + sizeof(cardi_thread_stack),
                    sizeof(cardi_thread_stack), p->priority);
    OS_WakeupThreadDirect(p->thread);

    PXI_SetFifoRecvCallback(PXI_FIFO_TAG_FS, CARDi_OnFifoRecv);

    if (!MB_IsMultiBootChild())
    {
        CARD_Enable(TRUE);
    }
}

static BOOL CARDi_EnableFlag = FALSE;

ARM_FUNC BOOL CARD_IsEnabled(void)
{
    return CARDi_EnableFlag;
}

ARM_FUNC void CARD_CheckEnabled(void)
{
    if (!CARD_IsEnabled())
    {
        OS_Terminate();
    }
}

ARM_FUNC void CARD_Enable(BOOL enable)
{
    CARDi_EnableFlag = enable;
}

ARM_FUNC BOOL CARDi_WaitAsync(void)
{
    CARDiCommon *const p = &cardi_common;

    OSIntrMode bak_psr = OS_DisableInterrupts();
    while ((p->flag & CARD_STAT_BUSY) != 0)
    {
        OS_SleepThread(p->busy_q);
    }
    (void)OS_RestoreInterrupts(bak_psr);
    return (p->cmd->result == CARD_RESULT_SUCCESS);
}

ARM_FUNC BOOL CARDi_TryWaitAsync(void)
{
    CARDiCommon *const p = &cardi_common;

    return !(p->flag & CARD_STAT_BUSY);
}

ARM_FUNC CARDResult CARD_GetResultCode(void)
{
    CARDiCommon *const p = &cardi_common;

    return p->cmd->result;
}

ARM_FUNC void CARD_LockRom(u16 lock_id)
{
    CARDi_LockResource(lock_id, CARD_TARGET_ROM);
    (void)OS_TryLockCard(lock_id);
}

ARM_FUNC void CARD_UnlockRom(u16 lock_id)
{
    (void)OS_UnlockCard(lock_id);
    CARDi_UnlockResource(lock_id, CARD_TARGET_ROM);
}

ARM_FUNC void CARD_LockBackup(u16 lock_id)
{
    CARDi_LockResource(lock_id, CARD_TARGET_BACKUP);
}

ARM_FUNC void CARD_UnlockBackup(u16 lock_id)
{
    CARDi_UnlockResource(lock_id, CARD_TARGET_BACKUP);
}