si.c
3.54 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
/*---------------------------------------------------------------------*
Copyright (C) 1998 Nintendo.
$RCSfile: si.c,v $
$Revision: 1.1.1.1 $
$Date: 2002/05/02 03:27:21 $
*---------------------------------------------------------------------*/
/* pre-process */
#include "hglobals.h"
#include "hsi.h"
/* declarerations */
OSMesgQueue siMsgQ, siDmaMsgQ;
OSMesg siMsgBuf[SI_MSGQ_SIZE], siDmaMsgBuf[SI_DMAQ_SIZE];
static OSThread siThread;
static u64 siThreadStack[STACKSIZE/sizeof(u64)];
static void objectOfsi(void * );
static s32 si_readCont(void);
static s32 si_initCont(void);
static s32 si_interval;
static TContStat _contstat[MAXCONTROLLERS];
/* implmentations */
void
si_create(void)
{
osCreateMesgQueue(&siMsgQ, siMsgBuf, SI_MSGQ_SIZE);
osCreateMesgQueue(&siDmaMsgQ, siDmaMsgBuf, SI_DMAQ_SIZE);
osSetEventMesg(OS_EVENT_SI, &siDmaMsgQ, (OSMesg)1); /* SI */
si_initCont(); /* Initialize controllers */
osCreateThread(&siThread, ++g_thread_id,(void *)objectOfsi, 0
,siThreadStack + STACKSIZE/sizeof(u64), SI_THREAD_PRIORITY);
osStartThread(&siThread);
}
void
objectOfsi(void * arg)
{
arg = arg; /* no use */
while(1)
{
osRecvMesg(&siMsgQ, (OSMesg *)NULL, OS_MESG_BLOCK);
si_readCont();
}
}
void
si_ticker( void )
{
static s32 cnt=1;
if( cnt < 0 )
{
osSendMesg(&siMsgQ, (OSMesg)NULL, OS_MESG_BLOCK);
cnt = si_interval;
}
else
{
cnt--;
}
}
s32
si_readCont(void)
{
int i;
OSContPad contdata[MAXCONTROLLERS];
TContStat * p;
u16 bits,mask,trigmask;
u8 j;
osContStartReadData(&siDmaMsgQ); /* 関数内部 : DMA 要求後 SI_EVENT を待つ
(ブロックがかかってこの時点で読み取りは完了) */
osContGetReadData(contdata); /* RDRAM の内容を読むだけ */
for(i = 0 ; i < MAXCONTROLLERS ; i ++)
{
p = &_contstat[i];
if (!(contdata[i].errno & CONT_NO_RESPONSE_ERROR))
{
bits = contdata[i].button;
if( contdata[i].stick_x > SI_CONT_STICK_BORDER ) bits |= R_JPAD;
if( contdata[i].stick_y > SI_CONT_STICK_BORDER ) bits |= U_JPAD;
if( contdata[i].stick_x < - SI_CONT_STICK_BORDER ) bits |= L_JPAD;
if( contdata[i].stick_y < - SI_CONT_STICK_BORDER ) bits |= D_JPAD;
p->available = 1;
p->store = p->now;
p->now = bits;
p->trig = p->now & ~p->store;
trigmask=0;
mask=0;
for(j=0;j<16;j++)
{
bits = p->now & p->store; /* repeat bit */
mask = 1<<j;
if(bits & mask)
{
++p->repeat[j];
if ( (p->repeat[j] > 32) && (p->repeat[j] & 3) )
trigmask |= mask;
}
else
{
p->repeat[j]=0;
}
}
p->trig |= trigmask;
}
else
{
bzero(p,sizeof(TContStat));
}
}
return i;
}
s32
si_initCont(void)
{
int i,j=0;
u8 pattern;
OSContStatus stat[MAXCONTROLLERS];
TContStat * p;
osContInit(&siDmaMsgQ, &pattern, stat);
for (i = 0 ; i < MAXCONTROLLERS ; i ++)
{
p = &_contstat[i];
p->status = stat[i].status;
p->now = 0;
if ( ((pattern >> i) & 1) != 0 &&
((stat[i].type & CONT_TYPE_MASK) == CONT_TYPE_NORMAL)) {
p->available = 1;
j++;
} else {
p->available = 0;
}
}
si_interval = 0;
return j;
}
TContStat *
si_getCont(s32 contNo)
{
return &_contstat[contNo];
}
u16
si_getTrigger(s32 contNo)
{
u16 ret;
ret = _contstat[contNo].trig;
_contstat[contNo].trig = 0; /* trigger 呼出と同時にクリア */
return ret;
}
s32
si_setInterval(s32 i)
{
if (i<0) return 0;
si_interval = i;
return -1;
}