tron.c 11.3 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

/*---------------------------------------------------------------------*
        Copyright (C) 1998 Nintendo. (Originated by SGI)
        
        $RCSfile: tron.c,v $
        $Revision: 1.2 $
        $Date: 2003/04/04 16:52:56 $
 *---------------------------------------------------------------------*/
#include <ultra64.h>

/*
#include <rom.h>
*/
#include <ramrom.h>
#include "controller.h"

/* local includes: */
#include "tron.h"

Dynamic dynamic[2];

static Mtx projection;

#define ANGLE_CHANGE	910		/* 5 degrees */
#define DIST_CHANGE	80

static float pos[3] = {0,0,0};
static float look[3] = {0,0,-1};
static float up[3] = {0,1,0};
static float speed = 0;

int current = 0;

u32 framestart, frameend, rdpstart, rdpend;

/*
 * Symbol genererated by "makerom" to indicate the end of the code segment
 * in virtual (and physical) memory
 */
extern char _codeSegmentEnd[];

/*
 * Symbols generated by "makerom" to tell us where the static segment is
 * in ROM.
 */
extern char _staticSegmentRomStart[], _staticSegmentRomEnd[];

/*
 * Stacks for the threads as well as message queues for synchronization
 */
u64	bootStack[STACKSIZE/8];

static void	idle(void *);
static void	main(void *);

static OSThread	idleThread;
static u64	idleThreadStack[STACKSIZE/8];

static OSThread	mainThread;
static u64	mainThreadStack[STACKSIZE/8];

static OSThread	rmonThread;
static u64	rmonThreadStack[STACKSIZE/8];

OSMesgQueue	dmaMessageQ, rspMessageQ, rdpMessageQ, retraceMessageQ;
OSMesg		dmaMessageBuf, rspMessageBuf, rdpMessageBuf, retraceMessageBuf;
OSIoMesg	dmaIoMessageBuf;
OSMesg		dummyMessage;

#define	NUM_PI_MSGS	8
static OSMesg PiMessages[NUM_PI_MSGS];
static OSMesgQueue PiMessageQ;

u32 rdp_DRAM_io=0;			/* Send through DRAM, then to RDP? */



/*
 * must be in BSS, not on the stack for this to work:
 */
OSTask    tlist[2];       /* globaltask lists */
Gfx	*glistp;	/* global for test case procs */

static int debugflag = 0;

OSPiHandle	*handler;

boot(void *arg)
{
    int i, *pr;
    char *ap;
    u32 *argp;
    u32 argbuf[16];
    
    osInitialize();

    handler = osCartRomInit();

    argp = (u32 *)RAMROM_APP_WRITE_ADDR;
    for (i=0; i<sizeof(argbuf)/4; i++, argp++) {
	osEPiReadIo(handler, (u32)argp, &argbuf[i]); /* Assume no DMA */
    }

    /* Parse the options */
    ap = (char *)argbuf;
    while (*ap == ' ')
	ap++;
    if (*ap == '-' && *(ap+1) == 'd')
	debugflag = 1;
    
    osCreateThread(&idleThread, 1, idle, arg,
		   idleThreadStack+STACKSIZE/8, (OSPri)10);
    osStartThread(&idleThread);

    /* never reached */
}

int dither=0,point=0,persp=1, fixbug=0;

static float theta = 0;
static float costheta = 1;
static float sintheta = 0;
static float verta = 0;
static float cosverta = 1;
static float sinverta = 0;

static int release_g = 1;

static void
main(void *arg)
{
	int		current = 0;
	OSTask		*tlistp;
	Dynamic		*dynamicp;
    	char		*staticSegment;
	unsigned short 	*cfbp;
    	/* this structure must be aligned to 64-bit boundary: */
    	u16 		perspNorm;

	theta = -50;

	pos[0] = 700;
	pos[1] = 50;
	pos[2] = 600;

	/*
	 * Setup the message queues
	 */
	osCreateMesgQueue(&dmaMessageQ, &dmaMessageBuf, 1);

	osCreateMesgQueue(&rspMessageQ, &rspMessageBuf, 1);
	osSetEventMesg(OS_EVENT_SP, &rspMessageQ, dummyMessage);
    
	osCreateMesgQueue(&rdpMessageQ, &rdpMessageBuf, 1);
	osSetEventMesg(OS_EVENT_DP, &rdpMessageQ, dummyMessage);

	osCreateMesgQueue(&retraceMessageQ, &retraceMessageBuf, 1);
	osViSetEvent(&retraceMessageQ, dummyMessage, 1);

	/*
	 * Stick the static segment right after the code/data segment
	 */
	staticSegment = _codeSegmentEnd + (16 - ((int) _codeSegmentEnd & 0xf));

	dmaIoMessageBuf.hdr.pri      = OS_MESG_PRI_NORMAL;
	dmaIoMessageBuf.hdr.retQueue = &dmaMessageQ;
	dmaIoMessageBuf.dramAddr     = staticSegment;
	dmaIoMessageBuf.devAddr      = (u32)_staticSegmentRomStart;
	dmaIoMessageBuf.size         = _staticSegmentRomEnd-_staticSegmentRomStart;

	osEPiStartDma(handler, &dmaIoMessageBuf, OS_READ);

	/*
	 * Wait for DMA to finish
	 */
	(void)osRecvMesg(&dmaMessageQ, &dummyMessage, OS_MESG_BLOCK);

	cfbp = cfb_16_b;

	initcontrollers();

	framestart = osGetCount();

	for (;;) {
		readcontrollers();
		{
			if ((pad[0].button&CONT_START) || (pad[0].button&CONT_G) || 
				(pad[0].button&CONT_R) || (pad[0].button&CONT_L)) {
			  if (pad[0].button&CONT_UP) osViSetSpecialFeatures(OS_VI_GAMMA_ON);
			  if (pad[0].button&CONT_DOWN) osViSetSpecialFeatures(OS_VI_GAMMA_OFF);
			  if (pad[0].button&CONT_LEFT) dither=0;
			  if (pad[0].button&CONT_RIGHT) dither=1;
			  if (pad[0].button&CONT_A) point=0;
			  if (pad[0].button&CONT_B) point=1;
			  if (pad[0].button&CONT_C) ;
			  if (pad[0].button&CONT_D) ;
			  if (pad[0].button&CONT_L) fixbug = 1;
			  if (pad[0].button&CONT_R) fixbug = 0;
			  if ((pad[0].button&CONT_L) && (pad[0].button&CONT_R)) ;
			  if (pad[0].button&CONT_E) persp=0;
			  if (pad[0].button&CONT_F) persp=1;

			  /* debounce g button */
  			  if (pad[0].button&CONT_G) {
				if(release_g) {
				  guParseRdpDL(rdp_output,rdp_output_len,GU_PARSERDP_PRAREA);
				  release_g = 0;
				}
			  }

			}
			else
			{
				int x,y;
				x=pad[0].stick_x;
				y=pad[0].stick_y;
#define DEAD 10
				if (x<-DEAD) x+=DEAD; else if (x>DEAD) x-=DEAD; else x=0;
				if (y<-DEAD) y+=DEAD; else if (y>DEAD) y-=DEAD; else y=0;

				pos[0]+= y*sintheta;
				pos[2]+= -y*costheta;
				if (pad[0].button&CONT_LEFT) 
				{
					pos[0]-= 20*costheta;
					pos[2]-= 20*sintheta;
				}
				if (pad[0].button&CONT_RIGHT)
				{
					pos[0]+= 20*costheta;
					pos[2]+= 20*sintheta;
				}
				theta+= x/8.0F;

				if (pad[0].button&CONT_UP) verta-=2;
				if (pad[0].button&CONT_DOWN) verta+=2;
				if ((pad[0].button&~lpad[0].button)&CONT_A);
				if ((pad[0].button&~lpad[0].button)&CONT_B);
				if (pad[0].button&CONT_E) pos[1] -= 20;
				if (pad[0].button&CONT_F) pos[1] += 20;

				release_g = 1;
			}

			costheta = cosf(theta*(2*3.14159/360));
			sintheta = sinf(theta*(2*3.14159/360));
			cosverta = cosf(verta*(2*3.14159/360));
			sinverta = sinf(verta*(2*3.14159/360));
			
			look[1] = sinverta;
			look[0] = cosverta*sintheta;
			look[2] = -cosverta*costheta;
			
			up[1] = cosverta;
			up[0] = -sinverta*sintheta;
			up[2] = sinverta*costheta;
		}

		/*
		 * pointers to build the display list.
		 */
		tlistp = &tlist[current];
		dynamicp = &dynamic[current];
		glistp = dynamicp->glist;

		/*
		 * Tell RCP where each segment is
		 */
		gSPSegment(glistp++, PHYSICAL_SEGMENT, 0);
		gSPSegment(glistp++, STATIC_SEGMENT,
			   osVirtualToPhysical(staticSegment));
		gSPSegment(glistp++, DYNAMIC_SEGMENT,
			   osVirtualToPhysical(dynamicp));
	
		/*
		 * Graphics pipeline state initialization
		 */
		gSPDisplayList(glistp++, setup_rspstate);
		gSPDisplayList(glistp++, setup_rdpstate);
	
		/*
		 * Clear framebuffer
		 */
		gDPSetColorImage(glistp++, G_IM_FMT_RGBA, G_IM_SIZ_16b,
			SCREEN_WD, OS_K0_TO_PHYSICAL(cfbp));
		gSPDisplayList(glistp++, clear_fb);
		gDPSetColorImage(glistp++, G_IM_FMT_RGBA, G_IM_SIZ_16b,
			SCREEN_WD, OS_K0_TO_PHYSICAL(cfbp));
	
		/* end of standard display list part. */


/******************************************************************************/
		guPerspective(&projection, &perspNorm, 40, (float)SCREEN_WD/SCREEN_HT, 100, 10000, 1);
		gSPPerspNormalize(glistp++, perspNorm);
		gSPMatrix(glistp++, osVirtualToPhysical(&projection), G_MTX_PROJECTION|G_MTX_LOAD|G_MTX_NOPUSH);

		guLookAt(&dynamicp->translate1, pos[0], pos[1], pos[2],
				pos[0]+look[0], pos[1]+look[1], pos[2]+look[2],
				up[0], up[1], up[2]);
		gSPMatrix(glistp++, osVirtualToPhysical(&dynamicp->translate1), G_MTX_MODELVIEW|G_MTX_LOAD|G_MTX_NOPUSH);

		gDPPipeSync(glistp++);
		gDPSetCycleType(glistp++, G_CYC_2CYCLE);
		gDPSetTextureFilter(glistp++,point?G_TF_POINT:G_TF_BILERP);
		gSPSetGeometryMode(glistp++, G_SHADE|G_ZBUFFER|G_SHADING_SMOOTH|G_CULL_BACK);
		gDPSetCombineMode(glistp++, G_CC_SHADE, G_CC_SHADE);
		gDPSetRenderMode(glistp++, G_RM_AA_ZB_OPA_SURF, G_RM_AA_ZB_OPA_SURF2);
		gDPSetRenderMode(glistp++, G_RM_PASS, G_RM_AA_ZB_OPA_SURF2);
		gDPSetTexturePersp(glistp++, persp?G_TP_PERSP:G_TP_NONE);
		gDPPipelineMode(glistp++, G_PM_1PRIMITIVE);
		gDPSetColorDither(glistp++, dither?G_CD_ENABLE:G_CD_DISABLE);

		gSPDisplayList(glistp++, objlist);
		gSPTextureL(glistp++,0xFFFF,0xFFFF,5,fixbug?0x2:0x0,G_TX_RENDERTILE,G_ON);
		gSPDisplayList(glistp++, objlist1);

		/*
		 * Force top-level display list to have an END.
		 */
		gDPFullSync(glistp++);
		gSPEndDisplayList(glistp++);
	
		/* build graphics task */
		tlistp->t.type = M_GFXTASK;
		tlistp->t.flags = 0x0;
		tlistp->t.ucode_boot = (unsigned long long *) rspbootTextStart;
		tlistp->t.ucode_boot_size = ((int)rspbootTextEnd -
					     (int)rspbootTextStart);
		if (rdp_DRAM_io) {
		    /* re-direct output to DRAM: */
			tlistp->t.ucode = (unsigned long long *)
			    gspFast3D_dramTextStart;
			tlistp->t.ucode_data = (unsigned long long *)
			    gspFast3D_dramDataStart; 
		} else {
		    /* SP output over XBUS to DP: */
			tlistp->t.ucode = (unsigned long long *)
			    gspFast3DTextStart;
			tlistp->t.ucode_data = (unsigned long long *)
			    gspFast3DDataStart;
		}
		tlistp->t.ucode_size = 4096;
		tlistp->t.ucode_data_size = 2048;

		tlistp->t.dram_stack = (unsigned long long *)
		    &(dram_stack[0]);
		tlistp->t.dram_stack_size = 1024;
		if (rdp_DRAM_io) {
		    tlistp->t.output_buff = (unsigned long long *) &(rdp_output[0]);
		    tlistp->t.output_buff_size = (unsigned long long *)
			&rdp_output_len;
		} else {
		    tlistp->t.output_buff = (unsigned long long *) 0x0;
		    tlistp->t.output_buff_size = (unsigned long long *) 0x0;
		}

		/* initial display list: */
		tlistp->t.data_ptr = (unsigned long long *)
		    dynamic[current].glist;
		tlistp->t.data_size = ((int)(glistp - dynamic[current].glist) *
				       sizeof (Gfx));
		tlistp->t.yield_data_ptr = (unsigned long long *) &(dram_yield[0]);
		tlistp->t.yield_data_size = 0xDA0;

		/*
		 * Can just flush 16KB and forget about each individual piece
		 * of data to flush.
		 */
		osWritebackDCacheAll();
	

        osSpTaskLoad(tlistp);
        osSpTaskStartGo(tlistp);
		rdpstart = osGetCount();

		(void)osRecvMesg(&rspMessageQ, &dummyMessage, OS_MESG_BLOCK);

		if (rdp_DRAM_io) {
		  	osDpSetNextBuffer(&(rdp_output[0]),(u32)rdp_output_len);
		}
		(void)osRecvMesg(&rdpMessageQ, &dummyMessage, OS_MESG_BLOCK);
	
		rdpend = osGetCount();

		osViSwapBuffer(cfbp);

		/*
		 * If graphics overruns, you will have redundant vertical
		 * retrace that need to be flushed.
		 */

		if (MQ_IS_FULL(&retraceMessageQ))
			(void)osRecvMesg(&retraceMessageQ, &dummyMessage, OS_MESG_BLOCK);
	
		(void)osRecvMesg(&retraceMessageQ, &dummyMessage, OS_MESG_BLOCK);

		current = 1 - current;
	
		if (current)    cfbp = cfb_16_a;
		else            cfbp = cfb_16_b;

		frameend = osGetCount();
		framestart = frameend;
	}

	for(;;);
}

static void
idle(void *arg)
{
	/*
	 * Initialize video
	 */
	osCreateViManager(OS_PRIORITY_VIMGR);
	osViSetMode(&osViModeTable[OS_VI_NTSC_LAN1]);

	/*
	 * Start PI Mgr for access to cartridge, rmon depends on PI Mgr
	 * to work.
	 */
	osCreatePiManager((OSPri)OS_PRIORITY_PIMGR, &PiMessageQ, PiMessages,
		NUM_PI_MSGS);

	/*
	 * Start rmon thread for rmonPrintf or gvd debugging
	 */
	osCreateThread(&rmonThread, 0, rmonMain, (void *)0,
		rmonThreadStack+STACKSIZE/8, OS_PRIORITY_RMON);
	osStartThread(&rmonThread);

	/*
	 * Start main thread
	 */
	osCreateThread(&mainThread, 3, main, arg, mainThreadStack+STACKSIZE/8,
		(OSPri)10);

	if (!debugflag)
		osStartThread(&mainThread);

	osSetThreadPri(0, 0);
	for(;;);
}