uimain.c
7.67 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
#include <ultra64.h>
#include <PR/sched.h>
#include <PR/libaudio.h>
#include "uimain.h"
#include "audrenderer.h"
#include "gfxrenderer.h"
/**** threads used by this file ****/
static OSThread gameThread;
static OSThread initThread;
/**** Stack for boot code. Space can be reused after 1st thread starts ****/
u64 bootStack[STACKSIZEBYTES/sizeof(u64)];
/**** Stacks for the threads, divide by 8 which is the size of a u64 ****/
static u64 gameThreadStack[STACKSIZEBYTES/sizeof(u64)];
static u64 initThreadStack[STACKSIZEBYTES/sizeof(u64)];
/**** function prototypes for private functions in this file ****/
static void gameproc(void *);
static void initproc(char *);
static void initGame(void);
static void initCntrl(void);
static u16 readControllerEvent(void);
/**** message queues and message buffers used by this app ****/
static OSMesg PiMessages[DMA_QUEUE_SIZE];
static OSMesgQueue PiMessageQ;
static OSMesgQueue dmaMessageQ;
static OSMesg dmaMessageBuf;
/**** application message queue ****/
OSMesgQueue appMsgQ;
OSMesg appMsgBuf[MAX_MESGS];
OSScClient appClient;
/**** Scheduler globals ****/
OSSched sc;
u64 scheduleStack[OS_SC_STACKSIZE/8];
/**** variables used by this file for handling the controllers ****/
static u8 validcontrollers = 0;
static OSContStatus statusdata[MAXCONTROLLERS];
static OSContPad controllerdata[MAXCONTROLLERS];
static OSScMsg controllermsg;
static u16 lastBut = 0;
static OSPiHandle *handler;
/*
* Graphics renderer message queue
*/
static OSMesgQueue *gfxRenderQueue;
/*
* Sound player
*/
static ALSndPlayer soundPlayer;
static ALSndId *sndId;
static int numSounds;
/*
* UI Pages
*/
extern int startInit(void);
extern int startPage(u16 cntrl, Sprite **sp);
extern int listInit(void);
extern int listPage(u16 cntrl, Sprite **sp);
static int currentPage = 0;
typedef int (*PageHandler)(u16, Sprite **);
typedef int (*PageInit)(void);
#define NUM_PAGES 2
static PageInit pageInits[NUM_PAGES] = {listInit, startInit};
static PageHandler pageHandlers[NUM_PAGES] = {startPage, listPage};
void boot(void *arg)
{
osInitialize();
handler = osCartRomInit();
osCreateThread(&initThread, 1, (void(*)(void *))initproc, arg,
(void *)(initThreadStack+(STACKSIZEBYTES/sizeof(u64))),
(OSPri)INIT_PRIORITY);
osStartThread(&initThread);
}
/**********************************************************************
*
* initproc sets up the PI manager. It then creates
* and starts our application's game thread. After returning from that,
* it becomes the lowest priority thread, and functions as the idle thread.
*
**********************************************************************/
static void initproc(char *argv)
{
/**** Start PI Mgr for access to cartridge ****/
osCreatePiManager((OSPri) OS_PRIORITY_PIMGR, &PiMessageQ, PiMessages,
DMA_QUEUE_SIZE);
/**** Create the game thread and start it up ****/
osCreateThread(&gameThread, 6, gameproc, NULL, gameThreadStack +
(STACKSIZEBYTES/sizeof(u64)), (OSPri)GAME_PRIORITY);
osStartThread(&gameThread);
/**** Set the thread to be the idle thread ****/
osSetThreadPri(0, 0);
for(;;);
}
static void gameproc(void *argv)
{
u32 cntrlReadInProg = 0;
OSScMsg *msg = NULL;
u16 cntrlEvent;
GFXMsg m;
Sprite *sp[10];
static int id = 0;
initGame();
while (1)
{
(void) osRecvMesg(&appMsgQ, (OSMesg *) &msg, OS_MESG_BLOCK);
switch (msg->type)
{
case (OS_SC_RETRACE_MSG):
/* request latest controller information (ie poll) */
if (validcontrollers && !cntrlReadInProg) {
cntrlReadInProg = 1;
osContStartReadData(&appMsgQ);
}
break;
case APP_CONTROLLER_MSG:
cntrlEvent = readControllerEvent();
cntrlReadInProg = 0;
/*
* Make some noise
*/
if (cntrlEvent & CONT_A) {
playSound(0);
}
if (cntrlEvent & CONT_B) {
playSound(1);
}
/*
* Call the current page to create sprites
*/
currentPage = pageHandlers[currentPage](cntrlEvent, &sp[0]);
m.gfx.type = GFX_NEW_FRAME;
m.gfx.spList = &sp[0];
osSendMesg(gfxRenderQueue, &m, OS_MESG_BLOCK);
break;
case (OS_SC_PRE_NMI_MSG): /* stop creation of audio and graphics */;
break;
}
}
}
/**********************************************************************
*
* initGame sets up the message queues used, and starts the scheduler.
* After that call routines to init the graphics, init the controllers,
* and init the audio.
*
*********************************************************************/
static void initGame(void)
{
int i;
/**** set up a needed message q's ****/
osCreateMesgQueue(&dmaMessageQ, &dmaMessageBuf, 1);
osCreateMesgQueue(&appMsgQ, appMsgBuf, MAX_MESGS);
/**** Initialize the RCP task scheduler ****/
osCreateScheduler(&sc, (void *)(scheduleStack + OS_SC_STACKSIZE/8),
SCHEDULER_PRIORITY, OS_VI_NTSC_LAN1, NUM_FIELDS);
/**** Add ourselves to the scheduler to receive retrace messages ****/
osScAddClient(&sc, &appClient, &appMsgQ);
/**** Call the initialization routines ****/
initCntrl();
gfxRenderQueue = initGfxRenderer(&sc, GAME_PRIORITY);
initAudio(&sc);
/*
* Initialize all pages
*/
for (i=0; i<NUM_PAGES; i++) {
pageInits[i]();
}
}
static void initCntrl(void)
{
OSMesgQueue serialMsgQ;
OSMesg serialMsg;
s32 i;
osCreateMesgQueue(&serialMsgQ, &serialMsg, 1);
osSetEventMesg(OS_EVENT_SI, &serialMsgQ, (OSMesg)1);
if((i = osContInit(&serialMsgQ, &validcontrollers, &statusdata[0])) != 0)
#ifndef _FINALROM
PRINTF("Failure initing controllers\n");
#else
;
#endif
/**** Set up message and queue, for read completion notification ****/
controllermsg.type = APP_CONTROLLER_MSG;
osSetEventMesg(OS_EVENT_SI, &appMsgQ, (OSMesg) &controllermsg);
}
static u16 readControllerEvent(void)
{
OSContPad *pad;
u16 newbutton;
osContGetReadData(controllerdata);
/*
* Read from first controller only
*/
if(validcontrollers & 1) {
pad = &controllerdata[0];
/* ignore controllers with errors */
if (pad->errno == 0) {
/* get bits that have changed, ignore button ups */
newbutton = pad->button ^ lastBut;
newbutton &= pad->button;
lastBut = pad->button;
return newbutton;
}
}
return 0;
}
void romCopy(const char *src, const char *dest, const int len)
{
OSIoMesg dmaIoMesgBuf;
OSMesg dummyMesg;
/*
* Always invalidate cache before dma'ing data into the buffer.
* This is to prevent a flush of the cache in the future from
* potentially trashing some data that has just been dma'ed in.
* Since you don't care if old data makes it from cache out to
* memory, you can use the cheaper osInvalDCache() instead of one
* of the writeback commands
*/
osInvalDCache((void *)dest, (s32) len);
dmaIoMesgBuf.hdr.pri = OS_MESG_PRI_NORMAL;
dmaIoMesgBuf.hdr.retQueue = &dmaMessageQ;
dmaIoMesgBuf.dramAddr = (void*)dest;
dmaIoMesgBuf.devAddr = (u32)src;
dmaIoMesgBuf.size = (u32)len;
osEPiStartDma(handler, &dmaIoMesgBuf, OS_READ);
(void) osRecvMesg(&dmaMessageQ, &dummyMesg, OS_MESG_BLOCK);
}