-
Notifications
You must be signed in to change notification settings - Fork 72
/
OSReset.c
178 lines (142 loc) · 3.8 KB
/
OSReset.c
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
#include <placeholder.h>
#include <__mem.h>
#include <dolphin/os.h>
#include <dolphin/os/OSAudioSystem.h>
#include <dolphin/os/OSCache.h>
#include <dolphin/os/OSInterrupt.h>
#include <dolphin/os/OSReboot.h>
#include <dolphin/os/OSReset.h>
#include <dolphin/os/OSRtc.h>
#include <dolphin/os/OSThread.h>
#include <dolphin/pad/pad.h>
typedef struct OSResetQueue {
OSResetFunctionInfo* first;
OSResetFunctionInfo* last;
} OSResetQueue;
static OSResetQueue ResetFunctionQueue;
void OSRegisterResetFunction(OSResetFunctionInfo* func)
{
OSResetFunctionInfo* tmp;
OSResetFunctionInfo* iter;
for (iter = ResetFunctionQueue.first;
iter && iter->priority <= func->priority; iter = iter->next)
{
}
if (iter == NULL) {
tmp = ResetFunctionQueue.last;
if (tmp == NULL) {
ResetFunctionQueue.first = func;
} else {
tmp->next = func;
}
func->prev = tmp;
func->next = NULL;
ResetFunctionQueue.last = func;
return;
}
func->next = iter;
tmp = iter->prev;
iter->prev = func;
func->prev = tmp;
if (tmp == NULL) {
ResetFunctionQueue.first = func;
return;
}
tmp->next = func;
}
static void Reset(s32 arg0)
{
NOT_IMPLEMENTED;
}
typedef struct Unk2 {
u16 _0;
u16 _2;
} Unk2;
extern volatile Unk2 DAT_cc002000 AT_ADDRESS(0xCC002000);
extern OSSram* __OSLockSram(void);
extern OSThreadQueue __OSActiveThreadQueue AT_ADDRESS(0x800000DC);
bool __OSCallResetFunctions(bool funcs_arg)
{
OSResetFunctionInfo* iter;
bool anyReset = false;
for (iter = ResetFunctionQueue.first; iter != NULL; iter = iter->next) {
anyReset |= !iter->func(funcs_arg);
}
anyReset |= !__OSSyncSram();
return anyReset ? false : true;
}
static void KillThreads(void)
{
OSThread* thread;
OSThread* next;
for (thread = __OSActiveThreadQueue.head; thread; thread = next) {
next = thread->linkActive.next;
switch (thread->state) {
case 1:
case 4:
OSCancelThread(thread);
continue;
default:
continue;
}
}
}
void __OSDoHotReset(s32 arg0)
{
OSDisableInterrupts();
DAT_cc002000._2 = 0;
ICFlashInvalidate();
Reset(arg0 * 8);
}
void OSResetSystem(int reset, u32 resetCode, bool forceMenu)
{
u8 _[12];
// Not initialized in all branches?
bool disableRecalibration;
OSDisableScheduler();
__OSStopAudioSystem();
if (reset == OS_RESET_SHUTDOWN) {
disableRecalibration = __PADDisableRecalibration(true);
}
while (!__OSCallResetFunctions(false)) {
continue;
}
if (reset == OS_RESET_HOTRESET && forceMenu) {
OSSram* sram = __OSLockSram();
sram->flags |= (1 << 6);
__OSUnlockSram(true);
while (!__OSSyncSram()) {
continue;
}
}
OSDisableInterrupts();
__OSCallResetFunctions(true);
LCDisable();
if (reset == OS_RESET_HOTRESET) {
__OSDoHotReset(resetCode);
} else if (reset == OS_RESET_RESTART) {
KillThreads();
OSEnableScheduler();
__OSReboot(resetCode, forceMenu);
}
KillThreads();
memset(OSPhysicalToCached(0x40), 0, 0xCC - 0x40);
memset(OSPhysicalToCached(0xD4), 0, 0xE8 - 0xD4);
memset(OSPhysicalToCached(0xF4), 0, 0xF8 - 0xF4);
memset(OSPhysicalToCached(0x3000), 0, 0xC0);
memset(OSPhysicalToCached(0x30C8), 0, 0xD4 - 0xC8);
__PADDisableRecalibration(disableRecalibration);
}
extern volatile u8 DAT_800030e2 AT_ADDRESS(0x800030E2);
typedef struct Unk {
u8 pad[0x24];
u32 resetCode;
} Unk;
extern volatile Unk DAT_cc003000 AT_ADDRESS(0xCC003000);
u32 OSGetResetCode(void)
{
if (DAT_800030e2 != 0) {
return 0x80000000;
}
return ((DAT_cc003000.resetCode & ~7) >> 3);
}