gatekeep.c
6.76 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#include <PR/bcp.h>
#include <PR/R4300.h>
#include <PR/bbsim.h>
#include <PR/bbskapi.h>
#include <PR/os.h>
#include <PR/os_bb.h>
#include <PR/os_bbfs.h>
#include <PR/os_bbatb.h>
#include <PR/os_bbexec.h>
#include <PR/ramrom.h>
#include <libfb.h>
#include "gatekeep.h"
#define ALIGN_DCACHE __attribute__((aligned(DCACHE_LINESIZE)))
#define printstr fbPrintf
static u32 gMiSecModeRegEntryVal;
extern u32 __osBbHackFlags;
/*
* debug print support
*/
#undef PRINTF
#ifdef DEBUG
#define PRINTF osSyncPrintf
#else
#define PRINTF(format, args...)
#endif
#define fbhBlue GPACK_RGBA5551(0,0,80,1)
#define fbhGreen GPACK_RGBA5551(0,80,0,1)
#define fbhRed GPACK_RGBA5551(80,0,0,1)
#define fbhYellow GPACK_RGBA5551(80,80,0,1)
#define fbhMagenta GPACK_RGBA5551(80,0,80,1)
#define fbhCyan GPACK_RGBA5551(0,80,80,1)
#define fbLemonChiffon GPACK_RGBA5551(255,250,205,1)
#define fbLightSlateBlue GPACK_RGBA5551(132,112,255,1)
#define fbLightPink1 GPACK_RGBA5551(255,174,185,1)
#define fbThistle2 GPACK_RGBA5551(238,210,238,1)
#define fbSeaGreen4 GPACK_RGBA5551(105,139,105,1)
#define fbTomato1 GPACK_RGBA5551(255,99,71,1)
#define fbTurquoise2 GPACK_RGBA5551(0,229,238,1)
#define fbSienna4 GPACK_RGBA5551(139,71,38,1)
/*
* Thread and stack structures
*/
char bootStack[STACKSIZE] __attribute__ ((aligned (8)));
static OSThread idleThread;
static char idleThreadStack[STACKSIZE] __attribute__ ((aligned (8)));
static OSThread mainThread;
static char mainThreadStack[STACKSIZE] __attribute__ ((aligned (8)));
/*
* FS specific definitions
*/
static void idleproc(char *);
static void mainproc(char *);
#define DMA_QUEUE_SIZE 200
static OSMesg PiMessages[DMA_QUEUE_SIZE];
static OSMesgQueue PiMessageQ;
static OSMesg SiMessages[DMA_QUEUE_SIZE];
static OSMesgQueue SiMessageQ;
/*
* simple graphics
*/
static OSMesgQueue retraceMessageQ;
static OSMesg dummyMessage, retraceMessageBuf;
static u16 cfb[320*240] __attribute__((aligned(64)));
/*
* controller
*/
static OSMesgQueue contMessageQ;
OSContStatus statusData[MAXCONTROLLERS];
OSContPad controllerData[MAXCONTROLLERS];
static u16 lastbutton[MAXCONTROLLERS];
static int no_controller = 1;
int gBeginTest = 0;
static void
initController(void)
{
int i, c = 0;
u8 pattern;
if (__osBbIsBb<2)
__osBbHackFlags = 1;
else
__osBbHackFlags = 0;
osCreateMesgQueue(&contMessageQ, &dummyMessage, 1);
osSetEventMesg(OS_EVENT_SI, &contMessageQ, (OSMesg) 0);
osContInit(&contMessageQ, &pattern, &statusData[0]);
for (i = 0; i < MAXCONTROLLERS; i++) {
if ((pattern & (1 << i)) &&
!(statusData[i].errno & CONT_NO_RESPONSE_ERROR)) {
++c;
no_controller = 0;
}
}
}
static void
processButton(u16 button, int lastbutton)
{
if (button & D_JPAD && !(lastbutton & D_JPAD)) {
// pad down
}
if (button & U_JPAD && !(lastbutton & U_JPAD)) {
// pad up
}
// output anything changing here
if ((button & A_BUTTON && !(lastbutton & A_BUTTON)) ||
(button & START_BUTTON && !(lastbutton & START_BUTTON))){
// start or A button pressed
gBeginTest = 1;
}
}
static void
readControllers(void)
{
int i;
OSContPad *pad;
osRecvMesg(&contMessageQ, &dummyMessage, OS_MESG_BLOCK);
osContGetReadData(controllerData);
for (i = 0; i < MAXCONTROLLERS; ++i) {
pad = &controllerData[i];
if (!pad->errno && pad->button != lastbutton[i]) {
processButton(pad->button, lastbutton[i]);
lastbutton[i] = pad->button;
}
}
}
static void
delay(void) {
int k;
for(k = 0; k < 1000000; k++)
;
}
void __osBbVideoPllInit(u32);
void boot(u32 miSecModeRegVal)
{
gMiSecModeRegEntryVal = miSecModeRegVal;
/* Init the video PLL */
__osBbVideoPllInit(OS_TV_NTSC);
osInitialize();
osCreateThread(&idleThread, 1, (void(*)(void *))idleproc, (void *)0,
idleThreadStack+STACKSIZE, 8);
osStartThread(&idleThread);
}
static void
idleproc(char *argv) /* priority 8 */
{
osCreateViManager(OS_PRIORITY_VIMGR);
fbInit(FB_LOW_RES);
#ifdef DEBUG
/* should allow usb printing */
osUsbInit();
#endif
/*
* Start PI Mgr for access to cartridge - start before the debugger
*/
osCreatePiManager((OSPri) OS_PRIORITY_PIMGR, &PiMessageQ, PiMessages,
DMA_QUEUE_SIZE);
osCreateMesgQueue(&SiMessageQ, SiMessages, DMA_QUEUE_SIZE);
osSetEventMesg(OS_EVENT_SI, &SiMessageQ, (OSMesg)DMA_QUEUE_SIZE);
/*
* The main thread's priority must be the same or lower than the original
* idle's thread priority. This allows the idle thread to change its
* priority to 0 before the main thread starts execution.
*/
osCreateThread(&mainThread, 3, (void(*)(void *))mainproc, argv,
mainThreadStack+STACKSIZE/8, (OSPri)7);
osStartThread(&mainThread);
osSetThreadPri(0, OS_PRIORITY_IDLE);
for(;;){}
}
void mainproc(char *argv)
{
int ret;
u32 *p;
BbShaHash hash;
BbEccSig sign;
/* setup text output to screen */
osCreateMesgQueue(&retraceMessageQ, &retraceMessageBuf, 1);
osViSetEvent(&retraceMessageQ, dummyMessage, 1);
osViBlack(1);
osViSwapBuffer(cfb);
fbSetBg(fbBlack);
fbClear();
osViBlack(0);
osWritebackDCacheAll();
osViSwapBuffer(cfb);
fbClear();
initController();
printstr(fbWhite, 3, 2, "GateKeeper Test");
osWritebackDCacheAll();
printstr(fbWhite, 5, 4, "Press A to start");
osWritebackDCacheAll();
do {
osRecvMesg(&retraceMessageQ, NULL, OS_MESG_BLOCK);
osContStartReadData(&contMessageQ);
readControllers();
}while(!gBeginTest);
printstr(fbWhite, 5, 4, "Starting ");
osWritebackDCacheAll();
/* first check if cleared once sysapp launched */
for(p=(u32 *)PHYS_TO_K0(INTERNAL_RAM_START);p<(u32 *)PHYS_TO_K0(INTERNAL_RAM_END);p++){
if(*p!=0){
fbPrintf(fbWhite, 3, 8, "sysapp first addr: %08x",(u32)p);
break;
}
}
/* make sk call which will generate internal iram stack */
ret = skSignHash(hash,sign);
for(p=(u32 *)PHYS_TO_K0(INTERNAL_RAM_START);p<(u32 *)PHYS_TO_K0(INTERNAL_RAM_END);p++){
if(*p!=0){
fbPrintf(fbWhite, 3, 10, "skapi first addr: %08x",(u32)p);
break;
}
}
printstr(fbWhite, 5, 4, "Complete, A to power down");
osWritebackDCacheAll();
gBeginTest = 0;
do {
osRecvMesg(&retraceMessageQ, NULL, OS_MESG_BLOCK);
osContStartReadData(&contMessageQ);
readControllers();
}while(!gBeginTest);
delay();
osBbPowerOff();
return;
}