-
Notifications
You must be signed in to change notification settings - Fork 0
/
utility.c
149 lines (102 loc) · 2.61 KB
/
utility.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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
#include "utility.h"
// Get int enable state from cop0r12
int InCriticalSection(){
ulong returnVal;
__asm__ volatile(
"mfc0 %0,$12\n\t"
"nop\n\t"
: "=r"( returnVal )
: // no inputs
);
return !(returnVal & 0x01);
}
// Enter a critical section by disabling interrupts
int EnterCritical(){
ulong oldVal = InCriticalSection();
__asm__ volatile (
"li $9, 0x1\n\t" // li t1,0x01
"not $9\n\t" // not t1
"mfc0 $8, $12\n\t" // mfc0 t0,$12
"nop \n\t" // best opcode
"and $8,$8,$9\n\t" // t0 = t0 & t1 (mask it)
"mtc0 $8, $12\n\t" // send it back
"nop"
: // no outputs
: // no inputs
: "$8", "$9"
);
return oldVal;
}
// Exit critical by re-enabling interrupts
int ExitCritical(){
__asm__ volatile (
"mfc0 $8, $12\n\t"
"nop \n\t"
"ori $8,$8,0x01\n\t"
"mtc0 $8, $12\n\t"
"nop"
: //
: //
: "$8"
);
}
unsigned long ResetEntryInt(){
register int cmd __asm__("$9") = 0x18;
__asm__ volatile("" : "=r"(cmd) : "r"(cmd));
return ((int(*)(void))0xB0)();
}
// TODO:
void ResetGraph(){}
void InitHeap (unsigned long * a, unsigned long b){}
int StopCallback(void){}
#pragma GCC push options
#pragma GCC optimize ("-O0")
void Delay( int inLen ){
// __TEST__
int i = 0;
for( i = 0; i < inLen; i++ ){
__asm__ volatile( "" : "=r"( i ) : "r"( i ) );
}
}
#pragma GCC pop options
// E, J, or U
int IsPAL(){
return ( *(char*)0xBFC7FF52 == 'E' );
}
void UnloadMe(){
// Reset interrupt + mask
*(ulong*)0xBF801070 = 0;
*(ulong*)0xBF801074 = 0;
PadStop();
ResetGraph( 3 );
StopCallback();
}
void AddDevice( void * deviceInfo ){
register int cmd __asm__("$9") = 0x47;
__asm__ volatile("" : "=r"(cmd) : "r"(cmd));
return ((void(*)(void*))0xB0)(deviceInfo);
}
// lowercase
void RemoveDevice( char * deviceName ){
register int cmd __asm__("$9") = 0x48;
__asm__ volatile("" : "=r"(cmd) : "r"(cmd));
return ((void(*)(char*))0xB0)(deviceName);
}
void PrintDevices(){
register int cmd __asm__("$9") = 0x49;
__asm__ volatile("" : "=r"(cmd) : "r"(cmd));
return ((void(*)(void))0xB0)();
}
void CloseFile( ulong fileHandle ){
register int cmd __asm__("$9") = 0x36;
__asm__ volatile("" : "=r"(cmd) : "r"(cmd));
((void(*)(ulong))0xB0)( fileHandle );
}
ulong OpenFile( char * fileName, ulong accessMode ){
register int cmd __asm__("$9") = 0x32;
__asm__ volatile("" : "=r"(cmd) : "r"(cmd));
return ((ulong(*)(char*,ulong))0xB0)( fileName, accessMode );
}