grphmain.c 14.5 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 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
/********************************************************************************
						   NINTENDO64 Disk Drive IPL4

							  graphics main module

								December 17, 1996
 ********************************************************************************/

#include <ultra64.h>
#include "ipl4.h"
#include "AUDIO/cpuaudio.h"
#include "graphics.h"


#define	NUM_VRT_MESGS		1
#define	NUM_RDP_MESGS		1
#define	NUM_RSP_MESGS		1
#define	NUM_DMA_MESGS		1
#define	NUM_SIM_MESGS		1

static OSMesgQueue	VRTMesgque;					/* VTRC message queue			*/
static OSMesgQueue	RSPMesgque;					/* RSP done message queue		*/
static OSMesgQueue	RDPMesgque;					/* RDP done message queue		*/
static OSMesgQueue	DMAMesgque;					/* DMA done message queue		*/
static OSMesgQueue	SIMMesgque;					/* SI manager message queue		*/

static OSMesg	VRTMesgbuf[NUM_VRT_MESGS];		/* VRTC message queue			*/
static OSMesg	RSPMesgbuf[NUM_RSP_MESGS];		/* RSP done message queue		*/
static OSMesg	RDPMesgbuf[NUM_RDP_MESGS];		/* RDP done message queue		*/
static OSMesg	DMAMesgbuf[NUM_DMA_MESGS];		/* DMA done message queue		*/
static OSMesg	SIMMesgbuf[NUM_SIM_MESGS];		/* SI manager message buffer	*/

OSContStatus	contStatus[MAXCONTROLLERS];		/* controller status			*/
OSContPad		contRdData[MAXCONTROLLERS];		/* controller data				*/
uchar			contConnection;					/* controller connection		*/


static OSTask	rsptask;						/* RSP task record				*/
static int		selector = 0;					/* double buffer selector		*/
static char *	dynamicp;						/* dynamic memory pointer		*/

Gfx *			graphicp;						/* graphic display list pointer	*/
ulong			frameCounter = 0;				/* game frame counter			*/

static OSTime startTime;						/* game frame start time		*/
static OSTime interval1 = 0;					/* CPU time						*/
static OSTime interval2 = 0;					/* RDP time						*/

static ulong clearColor = (RGBA16(0,0,0,1)<<16) + RGBA16(0,0,0,1);


//////////////////////////////////////////////////////////////////////////////////
//
//
//
//
//		Process time display routines. (for debug)
//
//
//
//
//////////////////////////////////////////////////////////////////////////////////

/********************************************************************************/
/*																				*/
/*	Display time scale bar.														*/
/*																				*/
/********************************************************************************/
static void
DisplayTime(void)
{
#if 0
	ulong length1 = (OS_CYCLES_TO_USEC(interval1) * 3) / 1000 + 30;
	ulong length2 = (OS_CYCLES_TO_USEC(interval2) * 3) / 1000 + 30;


	gDPPipeSync		(graphicp++ );
	gDPSetCycleType	(graphicp++, G_CYC_FILL);
	gDPSetRenderMode(graphicp++, G_RM_NOOP, G_RM_NOOP2);

	if (length2 > 0) {
		if (length2 > 319)  length2 = 319;
		gDPSetFillColor	(graphicp++, RGBA16( 5,15,30,1) <<16 | RGBA16( 5,15,30,1));
		gDPFillRectangle(graphicp++, 30, 216, length2, 218);
	}
	if (length1 > 0) {
		if (length1 > 319)  length1 = 319;
		gDPSetFillColor	(graphicp++, RGBA16(30,15, 5,1) <<16 | RGBA16(30,15, 5,1));
		gDPFillRectangle(graphicp++, 30, 216, length1, 218);
	}
	gDPPipeSync		(graphicp++);
	gDPSetFillColor	(graphicp++, RGBA16( 5,10,31,1) << 16 | RGBA16( 5,10,31,1));
	gDPFillRectangle(graphicp++, 30+49*0, 220, 30+49*1, 222);

	gDPPipeSync		(graphicp++);
	gDPSetFillColor	(graphicp++, RGBA16(31,15, 5,1) << 16 | RGBA16(31,15, 5,1));
	gDPFillRectangle(graphicp++, 30+49*1, 220, 30+49*2, 222);

	gDPPipeSync		(graphicp++);
	gDPSetFillColor	(graphicp++, RGBA16(31, 5, 5,1) << 16 | RGBA16(31, 5, 5,1));
	gDPFillRectangle(graphicp++, 30+49*2, 220, 30+49*3, 222);
#endif
}


//////////////////////////////////////////////////////////////////////////////////
//
//
//
//
//		RSP task routine
//
//
//
//
//////////////////////////////////////////////////////////////////////////////////

/********************************************************************************/
/*																				*/
/*	Open graphic task.															*/
/*																				*/
/********************************************************************************/
static void
InitRSPTask(void)
{
	OSTask_t *sptask = &rsptask.t;

	sptask->type				= M_GFXTASK;
	sptask->ucode_boot			= (uwlong *)rspbootTextStart;
	sptask->ucode_boot_size		= (int)rspbootTextEnd - (int)rspbootTextStart;

	sptask->flags				= 0;
	sptask->ucode				= (uwlong *)gspF3DEX_fifoTextStart;
	sptask->ucode_data			= (uwlong *)gspF3DEX_fifoDataStart;

	sptask->ucode_size			= SP_UCODE_SIZE;
	sptask->ucode_data_size		= SP_UCODE_DATA_SIZE;

	sptask->dram_stack			= rspDramStack;
 	sptask->dram_stack_size		= SP_DRAM_STACK_SIZE8;

	sptask->yield_data_ptr		= NULL;
	sptask->yield_data_size		= 0;

	sptask->output_buff			= &rspFifoBuffer[			  0];
	sptask->output_buff_size	= &rspFifoBuffer[SP_FIFO_SIZE64];
}
/********************************************************************************/
/*																				*/
/*	Set RSP task																*/
/*																				*/
/********************************************************************************/
static void
SetRSPTask(void)
{
	OSTask_t *sptask = &rsptask.t;
	ulong	  length = (ulong)graphicp - (ulong)&dynamicMemory[selector][0];

	sptask->data_ptr  = &dynamicMemory[selector][0];
	sptask->data_size = length;
}
/********************************************************************************/
/*																				*/
/*	Allocate dynamic memory block.												*/
/*																				*/
/********************************************************************************/
extern char *
AllocDynamic(long size)
{
	char *memory = NULL;

	size = Align64(size);
	if ((dynamicp - size) >= (char *)graphicp) {
		dynamicp -= size;
		memory	  = dynamicp;
	}
	return(memory);
}


//////////////////////////////////////////////////////////////////////////////////
//
//
//
//
//		Graphic display list oparations
//
//
//
//
//////////////////////////////////////////////////////////////////////////////////

/********************************************************************************/
/*																				*/
/*	Initlaize depth buffer.														*/
/*																				*/
/********************************************************************************/
static void
InitRDP(void)
{
    gDPPipeSync			(graphicp++	);
	gDPPipelineMode		(graphicp++, G_PM_1PRIMITIVE);
	gDPSetScissor		(graphicp++, G_SC_NON_INTERLACE, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    gDPSetCombineMode	(graphicp++, G_CC_SHADE, G_CC_SHADE);
    gDPSetTextureLOD	(graphicp++, G_TL_TILE);
	gDPSetTextureLUT	(graphicp++, G_TT_NONE);
    gDPSetTextureDetail	(graphicp++, G_TD_CLAMP);
    gDPSetTexturePersp	(graphicp++, G_TP_PERSP);
    gDPSetTextureFilter	(graphicp++, G_TF_BILERP);
    gDPSetTextureConvert(graphicp++, G_TC_FILT);
    gDPSetCombineKey	(graphicp++, G_CK_NONE);
    gDPSetAlphaCompare	(graphicp++, G_AC_NONE);
	gDPSetRenderMode	(graphicp++, G_RM_OPA_SURF, G_RM_OPA_SURF2);
	gDPSetColorDither	(graphicp++, G_CD_MAGICSQ);
	gDPSetCycleType		(graphicp++, G_CYC_FILL);
    gDPPipeSync			(graphicp++	);
}
/********************************************************************************/
/*																				*/
/*	Initlaize RSP.																*/
/*																				*/
/********************************************************************************/
static void
InitRSP(void)
{
    gSPClearGeometryMode(graphicp++, G_SHADE | G_SHADING_SMOOTH | G_CULL_BOTH |
			G_FOG | G_LIGHTING | G_TEXTURE_GEN | G_TEXTURE_GEN_LINEAR | G_LOD
	);
	gSPSetGeometryMode(graphicp++, G_SHADE | G_SHADING_SMOOTH | G_CULL_BACK);
    gSPTexture(graphicp++, 0, 0, 0, 0, G_OFF);

	gSPSegment(graphicp++, 0, 0x00000000);
	gSPSegment(graphicp++, 1, _CodeSegmentEnd);
}
/********************************************************************************/
/*																				*/
/*	Initlaize depth buffer.														*/
/*																				*/
/********************************************************************************/
static void
InitDepthBuffer(void)
{
	gDPPipeSync		 (graphicp++ );
	gDPSetDepthSource(graphicp++, G_ZS_PIXEL);
	gDPSetMaskImage	 (graphicp++, depthBuffer);

	gDPSetColorImage(graphicp++, G_IM_FMT_RGBA, G_IM_SIZ_16b, SCREEN_WIDTH, K0_TO_PHYS(&depthBuffer[0]));
	gDPSetFillColor (graphicp++, GPACK_ZDZ(G_MAXFBZ, 0) << 16 | GPACK_ZDZ(G_MAXFBZ, 0));
	gDPFillRectangle(graphicp++, 0, 0, SCREEN_WIDTH-1, SCREEN_HEIGHT-1);
}
/********************************************************************************/
/*																				*/
/*	Initialize frame buffer.													*/
/*																				*/
/********************************************************************************/
static void
InitFrameBuffer(void)
{
	gDPPipeSync		(graphicp++	);
	gDPSetColorImage(graphicp++, G_IM_FMT_RGBA, G_IM_SIZ_16b, SCREEN_WIDTH, K0_TO_PHYS(&frameBuffer[selector][0]));
	gDPSetFillColor	(graphicp++, RGBA16(0,0,20,1) << 16 | RGBA16(0,0,20,1));
	gDPSetFillColor	(graphicp++, clearColor);
	gDPFillRectangle(graphicp++, 0, 0, SCREEN_WIDTH-1, SCREEN_HEIGHT-1);

	gDPPipeSync		(graphicp++);
	gDPSetCycleType	(graphicp++, G_CYC_1CYCLE);
}
/********************************************************************************/
/*																				*/
/*	Begin drawing.																*/
/*																				*/
/********************************************************************************/
static void
BeginDrawing(void)
{
	selector ^= 1;
	dynamicp  = (MemPtr)&dynamicMemory[selector][DYNAMIC_SIZE64];
	graphicp  = (GfxPtr)&dynamicMemory[selector][			  0];

	InitRDP();
	InitRSP();
	InitDepthBuffer();
	InitFrameBuffer();
}
/********************************************************************************/
/*																				*/
/*	End drawing.																*/
/*																				*/
/********************************************************************************/
static void
EndDrawing(void)
{
	DisplayTime();					/* for debug	*/
	gDPFullSync(graphicp++);
	gSPEndDisplayList(graphicp++);
}
/********************************************************************************/
/*																				*/
/*	Begin a game frame.															*/
/*																				*/
/********************************************************************************/
static void
BeginGameFrame(void)
{
	osWritebackDCacheAll();
	osSpTaskStart(&rsptask);
	BeginDrawing();
	frameCounter += 1;
}
/********************************************************************************/
/*																				*/
/*	Flush display list.															*/
/*																				*/
/********************************************************************************/
static void
EndGameFrame(void)
{
	EndDrawing();
	interval1 = osGetTime() - startTime;			/* for debug	*/
	osRecvMesg(&RSPMesgque, NULL, OS_MESG_BLOCK);
	osRecvMesg(&RDPMesgque, NULL, OS_MESG_BLOCK);
	interval2 = osGetTime() - startTime;			/* for debug	*/

	SetRSPTask();
	osRecvMesg(&VRTMesgque, NULL, OS_MESG_BLOCK);

	osViSwapBuffer((MemPtr)&frameBuffer[selector^1][0]);
	osRecvMesg(&VRTMesgque, NULL, OS_MESG_BLOCK);
	startTime = osGetTime();		/* for debug	*/
}
/********************************************************************************/
/*																				*/
/*	Initalize game frame.														*/
/*																				*/
/********************************************************************************/
static void
InitGameFrame(void)
{
	int frame;


	InitRSPTask();
	BeginDrawing();
	EndDrawing();
	SetRSPTask();

	startTime = osGetTime();		/* for debug	*/

	for (frame = 0; frame < 2; frame++) {
		BeginGameFrame();
		EndGameFrame();
	}
	osViBlack(FALSE);
}
/********************************************************************************/
/*																				*/
/*	Initalize game frame.														*/
/*																				*/
/********************************************************************************/
static void
DisposeGameFrame(void)
{
	int frame;

	SetClearColor(0, 0, 0);
	for (frame = 0; frame < 2; frame++) {
		BeginGameFrame();
		EndGameFrame();
	}
	osViBlack(TRUE);
}
/********************************************************************************/
/*																				*/
/*	Graphics thread process.													*/
/*																				*/
/********************************************************************************/
extern void
SetClearColor(uchar red, uchar green, uchar blue)
{
	clearColor = (RGBA16(red,green,blue,1)<<16) | RGBA16(red,green,blue,1);	
}








/********************************************************************************/
/*																				*/
/*	Read a data block from ROM.													*/
/*																				*/
/********************************************************************************/
static void
ReadBlock(char *ramAddr, ulong romStart, ulong romEnd)
{
	OSIoMesg IOMesgbuf;
	OSMesg	 mesg;
	uint	 size = Align128(romEnd - romStart);


	osInvalDCache(ramAddr, size);
	osPiStartDma(&IOMesgbuf, OS_MESG_PRI_NORMAL, OS_READ, romStart, ramAddr, size, &DMAMesgque);
	osRecvMesg(&DMAMesgque, &mesg, OS_MESG_BLOCK);
//	osSyncPrintf("read %08XH - %08XH\n", ramAddr, ramAddr+size);
}
/********************************************************************************/
/*																				*/
/*	Check graphics thread stop request.											*/
/*																				*/
/********************************************************************************/
static OSMesg
GetGraphicsEvent(void)
{
	OSMesg	message = NULL;

	osRecvMesg(&grphMesgque, &message, OS_MESG_NOBLOCK);
	return(message);
}
/********************************************************************************/
/*																				*/
/*	Graphics thread process.													*/
/*																				*/
/********************************************************************************/
extern void
GrphProcess(void *arg)
{
	OSMesg message;
	int	   running = TRUE;


	osCreateMesgQueue(&VRTMesgque, VRTMesgbuf, NUM_VRT_MESGS);
    osCreateMesgQueue(&RSPMesgque, RSPMesgbuf, NUM_RSP_MESGS);
    osCreateMesgQueue(&RDPMesgque, RDPMesgbuf, NUM_RDP_MESGS);
	osCreateMesgQueue(&DMAMesgque, DMAMesgbuf, NUM_DMA_MESGS);
	osCreateMesgQueue(&SIMMesgque, SIMMesgbuf, NUM_SIM_MESGS);

    osSetEventMesg(OS_EVENT_SP	  , &RSPMesgque , MESG_GRPH_RSPDONE);
    osSetEventMesg(OS_EVENT_DP	  , &RDPMesgque , MESG_GRPH_RDPDONE);
	osSetEventMesg(OS_EVENT_SI	  , &SIMMesgque , MESG_GRPH_SIMDONE);
	osSetEventMesg(OS_EVENT_PRENMI, &grphMesgque, MESG_GRPH_PRENMI );
    osViSetEvent(&VRTMesgque ,MESG_GRPH_RETRACE, 1);

	osContInit(&SIMMesgque, &contConnection, &contStatus[0]);
	ReadBlock(_CodeSegmentEnd, (ulong)_ShapeSegmentRomStart, (ulong)_ShapeSegmentRomEnd);
	Nacp_AudioMain();

	InitErrorMessage();
	InitGameFrame();

	while (running) {
		osContStartReadData(&SIMMesgque);
		BeginGameFrame();
		Nacp_AudioMain();

		osRecvMesg(&SIMMesgque, &message, OS_MESG_BLOCK);
		osContGetReadData(contRdData);

		running = DisplayGraphics(GetGraphicsEvent());
		EndGameFrame();
	}
	DisposeGameFrame();

    osSendMesg(&mainMesgque, MESG_MAIN_GRPHDONE, OS_MESG_BLOCK);
}