power.c
6.02 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
#include "ultra64.h"
#include "os_bb.h"
#include "bcp.h"
#include "power.h"
#include "nu64sys.h"
#include "graph.h"
#define DMA_QUEUE_SIZE 200
#define PRINTF osSyncPrintf
//#define IDE_PRINT
extern u32 __osBbIsBb;
extern u32 __osBbHackFlags;
/*
* 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)));
/*
* Message queues and message buffers used by this app
*/
static OSMesg PiMessages[DMA_QUEUE_SIZE], dmaMessageBuf;
static OSMesgQueue PiMessageQ, dmaMessageQ;
static OSMesg SiMessages[DMA_QUEUE_SIZE];
static OSMesgQueue SiMessageQ;
/*
* Local variables and routines
*/
static void idleproc(char *);
static void mainproc(char *);
void
boot(void)
{
/* 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);
#if SCREEN_LOW
osViSetMode(&osViModeTable[OS_VI_NTSC_LPN1]);
#else
osViSetMode(&osViModeTable[OS_VI_NTSC_HPF1]);
#endif
/*
* Start PI Mgr for access to cartridge - start before the debugger
*/
osCreatePiManager((OSPri) OS_PRIORITY_PIMGR, &PiMessageQ, PiMessages,
DMA_QUEUE_SIZE);
osCreateMesgQueue(&dmaMessageQ, &dmaMessageBuf, 1);
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(;;); /* idle thread */
}
#define __REG(x) "$" #x
#define __REG1(x) #x
#define getcp0reg(source) \
({ int __res; \
__asm__ __volatile__( \
".set\tpush\n\t" \
".set\treorder\n\t" \
"mfc0\t%0,"__REG(source)"\n\t" \
".set\tpop" \
: "=r" (__res)); \
__res;})
static OSMesgQueue retraceMessageQ, siMessageQ;;
static OSMesg dummyMessage, retraceMessageBuf;
static u8 testbuf[1024*1024] __attribute__((aligned(16)));
static OSPiHandle* handler;
static OSIoMesg dmaIOMessageBuf;
OSMesg siMessageBuf;
static u16 cfb[SCREEN_WD*SCREEN_HT] __attribute__((aligned(64)));
static void clear(u16 bg) {
int i;
for (i = 0; i < SCREEN_WD*SCREEN_HT; ++i) {
cfb[i] = bg;
}
}
static OSBbFs fs;
static u8 blocks[BB_FL_BLOCK_SIZE];
#define ERR_ON IO_WRITE(PI_GPIO_REG, \
((PI_GPIO_ERROR_BIT | PI_GPIO_POWER_BIT) << PI_GPIO_ENABLE_SHIFT) | \
PI_GPIO_POWER_BIT)
int my_poweroff()
{
osBbPowerOff();
/* Busy loop to let CPU spin */
for (; ;);
}
static void power_test(void)
{
char buf[128];
int rv, i, fd;
u32 power_num=0;
OSContStatus sdata[MAXCONTROLLERS];
OSContPad ctrl_data[MAXCONTROLLERS];
char pattern;
int key;
osSetEventMesg(OS_EVENT_SI, &siMessageQ, (OSMesg)NULL);
osContInit(&siMessageQ, &pattern, &sdata[0]);
osBbCardInit();
if ((rv = osBbFInit(&fs)) < 0) {
ERR_ON;
for (;;);
}
if ((fd = osBbFOpen("power.cyc", "r")) >= 0) {
if ((rv = osBbFRead(fd, 0, blocks, sizeof blocks)) < 0
|| rv != sizeof blocks) {
} else {
power_num = (blocks[0] << 24) | (blocks[1] << 16);
power_num |= (blocks[2] << 8) | blocks[3];
}
osBbFClose(fd);
osBbFDelete("power.cyc");
}
recount:
printstr(white, 9, 2, "BB power cycle test");
sprintf(buf, "# of power cycles = %d", power_num);
printstr(white, 6, 5, buf);
//printstr(yellow, 4, 10, "Yellow button to power off ...");
printstr(white, 4, 12, "Button A+B to reset count ...");
power_num++;
if ((fd = osBbFCreate("power.cyc", 1, BB_FL_BLOCK_SIZE)) >= 0) {
blocks[0] = (power_num >> 24) & 0xff;
blocks[1] = (power_num >> 16) & 0xff;
blocks[2] = (power_num >> 8) & 0xff;
blocks[3] = power_num & 0xff;
osBbFWrite(fd, 0, blocks, sizeof(blocks));
osBbFClose(fd);
} else {
ERR_ON;
for (;;);
}
for (; ;) {
if (IO_READ(MI_EINTR_REG) & MI_EINTR_BUT_STATUS)
my_poweroff();
osContStartReadData(&siMessageQ);
osRecvMesg(&siMessageQ, NULL, OS_MESG_BLOCK);
osContGetReadData(ctrl_data);
for (i=key=0; i<MAXCONTROLLERS; i++) {
if (!(ctrl_data[i].errno &CONT_NO_RESPONSE_ERROR))
key |= ctrl_data[i].button;
}
//if (key & (CONT_C | CONT_D | CONT_E | CONT_F)) my_poweroff();
if ((key & (CONT_A | CONT_B)) == (CONT_A | CONT_B)) {
clear(0x0);
power_num = 0;
osBbFDelete("power.cyc");
goto recount;
}
}
return;
}
static void
mainproc(char *argv) {
__osBbHackFlags = 0;
osCreateMesgQueue(&siMessageQ, &siMessageBuf, 1);
osSetEventMesg(OS_EVENT_SI, &siMessageQ, dummyMessage);
osCreateMesgQueue(&retraceMessageQ, &retraceMessageBuf, 1);
osViSetEvent(&retraceMessageQ, dummyMessage, 1);
osViBlack(1);
osViSwapBuffer(cfb);
clear(0x0);
osViBlack(0);
osWritebackDCacheAll();
osViSwapBuffer(cfb);
IO_WRITE(PI_FLASH_CONFIG_REG, 0xa207071f);
power_test();
}