vimgr.c
7.36 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
/*====================================================================
* vimgr.c
*
* Copyright 1995, Silicon Graphics, Inc.
* All Rights Reserved.
*
* This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics,
* Inc.; the contents of this file may not be disclosed to third
* parties, copied or duplicated in any form, in whole or in part,
* without the prior written permission of Silicon Graphics, Inc.
*
* RESTRICTED RIGHTS LEGEND:
* Use, duplication or disclosure by the Government is subject to
* restrictions as set forth in subdivision (c)(1)(ii) of the Rights
* in Technical Data and Computer Software clause at DFARS
* 252.227-7013, and/or in similar or successor clauses in the FAR,
* DOD or NASA FAR Supplement. Unpublished - rights reserved under the
* Copyright Laws of the United States.
*====================================================================*/
/**************************************************************************
*
* $Revision: 1.1.1.2 $
* $Date: 2002/10/29 08:06:43 $
* $Source: /root/leakn64/depot/rf/sw/n64os20l/libultra/monegi/vi/vimgr.c,v $
*
* Description:
* Video Interface Manager (VIM)
* Create a high priority thread to handle Video Interface (VI)
* requests.
*
**************************************************************************/
#include "osint.h"
#include "rcp.h"
#include "viint.h"
/**************************************************************************
*
* Definitions
*
*/
#define NUM_EV_MESG 5 /* Number of message entries in event queue */
/**************************************************************************
*
* Static variables
*
*/
static OSThread viThread; /* VIM Thread structure */
static char viThreadStack[OS_VIM_STACKSIZE];
static OSMesgQueue viEventQueue; /* Event queue structure */
static OSMesg viEventBuf[NUM_EV_MESG];
static OSIoMesg viRetraceMsg, viCounterMsg;
/**************************************************************************
*
* Global variables
*/
OSDevMgr __osViDevMgr = {0}; /* Device Manager structure */
u32 __additional_scanline = 0;
/**************************************************************************
*
* Function prototypes
*
*/
static void viMgrMain(void *);
/*
* Name: osCreateViManager
*
* Description:
* Create and start the Video Interface Manager (VIM). User specifies
* the priority at which this thread will run.
*
* Globals Referenced:
* __osViDevMgr
* viThread
* viThreadStack
* viEventQueue
* viEventBuf
*/
void
osCreateViManager(OSPri pri)
{
u32 savedMask;
OSPri oldPri, myPri;
#ifdef _DEBUG
if ((pri < OS_PRIORITY_IDLE) || (pri > OS_PRIORITY_MAX)) {
__osError(ERR_OSCREATEVIMANAGER, 1, pri);
return;
}
#endif
/* If VI Manager is already running, simply return */
if (__osViDevMgr.active)
return;
#ifdef _ULTRA64
/* Initialize timer services which use VI interrupt handler */
__osTimerServicesInit();
#endif
/* Set __additional_scanline */
__additional_scanline = 0;
/* Create the input event queue */
osCreateMesgQueue(&viEventQueue, viEventBuf, NUM_EV_MESG);
viRetraceMsg.hdr.type = OS_MESG_TYPE_VRETRACE;
viRetraceMsg.hdr.pri = OS_MESG_PRI_NORMAL;
viRetraceMsg.hdr.retQueue = NULL;
viCounterMsg.hdr.type = OS_MESG_TYPE_COUNTER;
viCounterMsg.hdr.pri = OS_MESG_PRI_NORMAL;
viCounterMsg.hdr.retQueue = NULL;
/* Take over VI interrupt */
osSetEventMesg(OS_EVENT_VI, &viEventQueue, (OSMesg)&viRetraceMsg);
/* Take over COUNTER interrupt */
osSetEventMesg(OS_EVENT_COUNTER, &viEventQueue, (OSMesg)&viCounterMsg);
/*
* Check the current thread priority against the VIM's priority.
* If current is lower, temporarily increase it to same level as VIM
*/
oldPri = -1;
myPri = osGetThreadPri((OSThread *)NULL);
if (myPri < pri) {
oldPri = myPri;
osSetThreadPri(NULL, pri);
}
/* Set global to tell that Video Interface Manager is on and active */
savedMask = __osDisableInt();
__osViDevMgr.active = 1;
__osViDevMgr.thread = &viThread;
__osViDevMgr.cmdQueue = &viEventQueue;
__osViDevMgr.evtQueue = &viEventQueue;
__osViDevMgr.acsQueue = (OSMesgQueue *)NULL;
__osViDevMgr.dma = NULL;
__osViDevMgr.edma = NULL;
/* Create VI manager thread with VI and CPU counter interrupts enabled */
osCreateThread(&viThread, 0, viMgrMain, (void *)&__osViDevMgr,
viThreadStack+OS_VIM_STACKSIZE, (OSPri)pri);
/*
* Initialize internal VI structures and set hardware registers to
* enable vertical retrace.
*/
__osViInit();
osStartThread(&viThread);
/* Restore original interrupt mask */
__osRestoreInt(savedMask);
/* Restore original thread priority */
if (oldPri != -1)
osSetThreadPri(NULL, oldPri);
} /* osCreateViManager */
/*
* Name: viMgrMain
*
* Description:
* This is the main loop for the VI manager.
*
* Note:
* As a policy, all messages sent to the command queue are pointers to
* an I/O message block.
*
* Pseudo-code:
* // The idea here is to listen on the vertical retrace message queues,
* // for event notification.
* while always {
* Listen (w/ blocking) on event queue
* Check for valid message type
* Decode message type
* Process message type (i.e., vertical retrace)
* Send reply message to registered message queue
* }
*
* Globals Referenced:
* None
*/
static void
viMgrMain(void *arg)
{
__OSViContext *vc;
OSDevMgr *dm;
OSIoMesg *mb = NULL;
static u16 retrace;
#if !_EMULATOR
s32 first = 0;
u32 Count;
#endif /* _EMULATOR */
/* Initialize retrace counter */
vc = __osViGetCurrentContext();
retrace = vc->retraceCount;
if (retrace == 0) /* to at least 1 */
retrace = 1;
dm = (OSDevMgr *)arg;
while (1) {
/*
* Listen on the Event Queue
*/
(void)osRecvMesg(dm->evtQueue, (OSMesg *)&mb, OS_MESG_BLOCK);
/*
* This signals either a vertical retrace interrupt or a CPU counter
* interrupt
*/
/*
* Decode the message: (1) vertical retrace, (2) CPU counter
*/
switch (mb->hdr.type) {
case OS_MESG_TYPE_VRETRACE: {
/* Swap the VI current and next context */
__osViSwapContext();
/* Decrement retrace counter */
retrace--;
/* Signal a thread of a vertical retrace interrupt occurrence */
if (retrace == 0) {
vc = __osViGetCurrentContext();
if (vc->msgq != NULL) {
(void)osSendMesg(vc->msgq, vc->msg, OS_MESG_NOBLOCK);
}
retrace = vc->retraceCount; /* Reset counter */
}
#if !_EMULATOR
__osViIntrCount++;
if (first) {
Count = osGetCount();
__osCurrentTime = (OSTime)Count;
first = 0;
}
Count = __osBaseCounter;
__osBaseCounter = osGetCount();
Count = __osBaseCounter - Count;
__osCurrentTime = __osCurrentTime + (OSTime)Count;
#endif /* _EMULATOR */
break;
}
case OS_MESG_TYPE_COUNTER: {
#if !_EMULATOR
__osTimerInterrupt();
#endif /* _EMULATOR */
break;
}
default: { /* ERROR: NOT REACHED */
break;
}
}
} /* while */
} /* viMgrMain */