tron.c 18.4 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 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759

/*---------------------------------------------------------------------*
        Copyright (C) 1998 Nintendo. (Originated by SGI)
        
        $RCSfile: tron.c,v $
        $Revision: 1.1.1.2 $
        $Date: 2002/10/29 08:06:14 $
 *---------------------------------------------------------------------*/
#include <ultra64.h>

/* #define	DEBUGGER */

/* Define this to get to an uncaught bowtie triangle after ~5 minutes,
 * if the program is run in demo mode (no controller plugged in).
 * Leave it undefined to get a better-quality application.
 *
 * There is nothing magical about this particular name.  It's just used
 * in this program to change some of the behavior so that it just happens
 * to hit a bad triangle after a while.
 */
/* #define GOTO_BOWTIE */

/*
 * Change the definition of BOWTIE_VAL from the default.  If it is left
 * at the default of zero, and GOTO_BOWTIE is defined above, then
 * this program will hang on an uncaught bowtie triangle after several
 * minutes.
 *
 * General note on BOWTIE_VAL:
 *   BOWTIE_VAL needs to make its way to the RSP in order to do any good.
 *   The method by which it is sent to the RSP is the gSPTexture command.
 *   Don't ask why -- just believe it.
 *   You must use a gSPTexture command (with enable = OFF if desired) in
 *   order for BOWTIE_VAL to take effect.  You must execute this command
 *   every frame before any triangles are rendered that might hang.
 *   Because gSPTexture always sends BOWTIE_VAL to the RSP, you need to
 *   make sure that BOWTIE_VAL is #define'd to the desired value in
 *   ALL of your source files which include a gSPTexture or gsSPTexture
 *   command!!!!
 *   Otherwise the gSPTexture command in another file will replace your
 *   desired value of BOWTIE_VAL with the default one defined in the
 *   Ultra 64 header files (gbi.h to be specific), and all of your efforts
 *   to avoid bowtie hangs will be in vain.
 *
 *   Also:  Valid values for BOWTIE_VAL are 0, 4, 8, 12.  The default value
 *          zero does the least damage to your rendered triangles, but
 *          provides no extra protection against bowtie hangs.  12 provides
 *          maximum hang protection, but results in the most visual damage.
 */
/* #undef BOWTIE_VAL
   #define BOWTIE_VAL 4
*/

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

/*
 * 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	mainproc(void *);

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

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

OSMesgQueue	dmaMessageQ, taskMessageQ, retraceMessageQ,
                controllerMessageQ, serialMessageQ;
OSIoMesg	dmaIoMessageBuf;
OSMesg		dmaMessageBuf, taskMessageBuf, retraceMessageBuf,
                controllerMsgBuf, serialMsgBuf;
OSMesg		dummyMessage;

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

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

/*
 * Double-buffered dynamic segments (?)
 */
Dynamic	dynamicBuffer[2];

/*
 * Prototypes
 */

int initControllers(void);

/*
 * Constants
 */

#define RAND_MAX	32767

#define ZBUFFER		1
#define PI		3.1415926
#define	MAXPLAYERS	16
#define N		200

#define HEIGHT		4

#define CRASH(f) ((f) == F_WALL || ((f) >= 0 && p[f].height > HEIGHT/2.0))

#define	F_EMPTY		-2
#define F_WALL		-1

#define WEST		0
#define NORTH		1
#define EAST		2
#define SOUTH		3
#define NUMDIRS		4

#ifndef TRUE
#define FALSE		0
#define TRUE		1
#endif

/* Define number of frames per movement in grid */
/* Note that xzscale/SUBSTEP_CNT should be an integer,
 * or 1 to disable substepping */
#define SUBSTEP_CNT     4

#define V(p, x, y, z, f, s, t, r, g, b, a) {				\
	(p)->v.ob[0] = (x);						\
	(p)->v.ob[1] = (y);						\
	(p)->v.ob[2] = (z);						\
	(p)->v.flag  = (f);						\
	(p)->v.tc[0] = (s);						\
	(p)->v.tc[1] = (t);						\
	(p)->v.cn[0] = (r);						\
	(p)->v.cn[1] = (g);						\
	(p)->v.cn[2] = (b);						\
	(p)->v.cn[3] = (a);						\
}

static void tronInit (void);
static void tronUpdate (void);
void sceneDraw (Dynamic *dynamicp, unsigned short *cfbp, Vtx *v);

struct player {
	int	x, y, dir, numhist, dead;
	float	ang, tipang, wantang, height;
	int	hx[MAXHIST], hy[MAXHIST];
	int	red, green, blue;
};

struct player	p[MAXPLAYERS];
int	dx[4] = { -1, 0, 1, 0 };
int	dy[4] = {  0, 1, 0, -1 };
float	ang[4] = { 0, 90, 180, 270 };

float		time; /* Time */
int		yscale, xzscale;
int		NS, numplayers, numalive, demomode;
int		field[N][N];
int		frame_count=0;
int             output_frame = 149;
static int      cfb_size = G_IM_SIZ_16b;
static int      controller;         /* Lowest active controller # */
static u16      button;             /* Controller button read     */
static u16      lastbutton;         /* Previous controller button read */
static OSContStatus statusdata[MAXCONTROLLERS];
static OSContPad    controllerdata[MAXCONTROLLERS];       

int    substep;

OSPiHandle	*handler;

boot(void *arg)
{
	osInitialize();

	handler = osCartRomInit();

	osCreateThread(&idleThread, 1, idle, (void *)0,
		idleThreadStack+STACKSIZE/8, (OSPri)10);
	osStartThread(&idleThread);
}

static void
mainproc(void *arg)
{
	int		current = 0;
	OSMesg		dummyMesg;
	OSTask		*tlistp;
	Dynamic		*dynamicp;
    	char		*staticSegment;
	unsigned short 	*cfbp;

	yscale = 20;
#ifdef GOTO_BOWTIE
	xzscale = 5;
#else
	xzscale = 20;
#endif

	NS = N * yscale;

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

	osCreateMesgQueue(&taskMessageQ, &taskMessageBuf, 1);
	osSetEventMesg(OS_EVENT_DP, &taskMessageQ, dummyMessage);

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

	/*
	 * Stick the static segment right after the code/data segment
	 */
	staticSegment = _codeSegmentEnd;

	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, &dummyMesg, OS_MESG_BLOCK);

	time = 0;

	/*
         * Init controller.  # returned is lowest # active controller,
         * or -1 if none.
         */
	demomode   = TRUE;
	controller = initControllers();
	if (controller != -1) demomode = FALSE;

	tronInit ();

	cfbp = cfb_16_b;

	for (;;) {

	        /*
                 * Get ready to read controller later
                 */
	        if (controller != -1) {
		  osContStartReadData(&controllerMessageQ);
		}
		
		/*
		 * pointers to build the display list.
		 */
		tlistp = &tlist[current];
		dynamicp = &dynamicBuffer[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);

		/*
                 * Extraneous gSPTexture call.  The sole purpose is to
                 * send BOWTIE_VAL to the RSP, which it does as a side-effect.
                 */
		gSPTexture(glistp++, 0x0, 0x0, 0x0, 0x0, G_OFF);
	
		/*
		 * 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. */

	        sceneDraw(dynamicp, cfbp, dynamicp->v);

		/*
		 * Force top-level display list to have an END.
		 */
		gDPFullSync(glistp++);
		gSPEndDisplayList(glistp++);
	
		/* build graphics task */
		tlistp->t.type = M_GFXTASK;
		tlistp->t.flags = OS_TASK_DP_WAIT;
		tlistp->t.ucode_boot = ( u64 * )rspbootTextStart;
		tlistp->t.ucode_boot_size = (u32)rspbootTextEnd - (u32)rspbootTextStart;
		tlistp->t.ucode = ( u64 * )gspF3DEX2_xbusTextStart;
		tlistp->t.ucode_data = ( u64 * )gspF3DEX2_xbusDataStart;
		tlistp->t.ucode_size = SP_UCODE_SIZE;
		tlistp->t.ucode_data_size = SP_UCODE_DATA_SIZE;

		tlistp->t.dram_stack = ( u64 * ) &dram_stack[0];
		tlistp->t.output_buff = ( u64 * ) NULL;
		tlistp->t.output_buff_size = ( u64 * ) NULL;

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

		/*
		 * Can just flush 16KB and forget about each individual pieces
		 * of data to flush.
		 */
		osWritebackDCacheAll();
	
        	osSpTaskLoad(tlistp);
        	osSpTaskStartGo(tlistp);
	
		(void)osRecvMesg(&taskMessageQ, &dummyMesg, OS_MESG_BLOCK);
	
		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;

		tronUpdate ();

		frame_count++;
		time++;
	}
}

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

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

	/*
	 * Start main thread
	 */
	osCreateThread(&mainThread, 3, mainproc, arg, mainThreadStack+STACKSIZE/8,
		(OSPri)10);
#ifndef DEBUGGER
	osStartThread(&mainThread);
#endif

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

static void tronInit (void)
{
	int	i, j;

	for (i = 0; i < N; i++) {
		for (j = 0; j < N; j++) {
			field[i][j] = F_EMPTY;
		}
	}

	for (i = 0; i < N; i++) {
		field[0][i] = F_WALL;
		field[i][0] = F_WALL;
		field[N-1][i] = F_WALL;
		field[i][N-1] = F_WALL;
	}

	p[0].x       = N/2;
	p[0].y       = N/10;
#ifdef GOTO_BOWTIE
	p[0].dir     = SOUTH;
#else
	p[0].dir     = NORTH;
#endif
	p[0].dead    = FALSE;
	p[0].numhist = 2;
	p[0].height  = HEIGHT;
	p[0].ang     = ang[p[0].dir];
	p[0].red     = 0;
	p[0].green   = 54;
	p[0].blue    = 234;
	p[0].hx[0]   = p[0].x;
	p[0].hy[0]   = p[0].y;

	numplayers   = 1;
	numalive     = numplayers;
	substep      = 0;
}

static unsigned int rand (void)
{
	/* Straight from the C library */

	static unsigned long seed = 1;

	seed = seed * 1103515245 + 12345;

	return (seed >> 16) & RAND_MAX;
}

/*
 * Wait for Start button on controller to be pressed
 */
static void waitForReset(void) {
  while(1) {
    osContStartReadData(&controllerMessageQ);
    osRecvMesg(&controllerMessageQ, NULL, OS_MESG_BLOCK);
    osContGetReadData(controllerdata);
    if (controllerdata[controller].errno != 0) continue;
    button = controllerdata[controller].button;
    if (button & CONT_START) {
      lastbutton = 0;
      return;
    }
  }
}

static void killplayer (int i)
{
	p[i].dead = TRUE;
	numalive--;
	if (numalive == 0) {
	  if (!demomode) waitForReset();
	  tronInit ();
	}
}

static void changedir (int i)
{
	int     f, cnt, turn, newdir;

	if (p[i].dead) {
		return;
	}

	cnt = 0;
	turn = (rand () % 2) * 2 - 1;
	newdir = (p[i].dir + turn + NUMDIRS) % NUMDIRS;

	f = field[p[i].x + dx[newdir]][p[i].y + dy[newdir]];
	if (CRASH (f)) {
		turn = -turn;
		newdir = (p[i].dir + turn + NUMDIRS) % NUMDIRS;
		f = field[p[i].x + dx[newdir]][p[i].y + dy[newdir]];
		if (CRASH (f)) {
			turn = 0;
			newdir = p[i].dir;
			f = field[p[i].x + dx[newdir]][p[i].y + dy[newdir]];
			if (CRASH (f)) {
				killplayer (i);
				return;
			}
		}
	}
	switch (turn) {
		case -1:
			p[i].dir = newdir;
			p[i].numhist++;
			p[i].wantang -= 90;
			p[i].tipang = 45;
			break;
		case 0:
			break;
		case 1:
			p[i].dir = newdir;
			p[i].numhist++;
			p[i].wantang += 90;
			p[i].tipang = -45;
			break;
	}
	if (p[i].numhist == MAXHIST) {
		tronInit ();
	}
}

static avoidwall (int i)
{
	int	f;

	f = field[p[i].x + dx[p[i].dir]][p[i].y + dy[p[i].dir]];
	if (CRASH (f)) {
		changedir (i);
	}
}

static void tronUpdate (void)
{
        int f;


#ifndef GOTO_BOWTIE
	/*
	 * Because the grid has a limited resolution of NxN, but we want
         * to make the front of the trail "move" every frame, we move
         * the trail on the screen a little every frame, but only mark it
	 * off in the array every fourth frame.  Without this the trail
         * would move across the grid too quickly.
         * An annoying side effect of the way this implementation works
         * is that we don't check the controllers as often as we should.
         */
	if (substep < SUBSTEP_CNT-1) {
	  substep++;
	  return;
	} else {
	  substep = 0;
	}
#endif

	field[p[0].x][p[0].y] = 0;

	if (demomode) {
		if (rand () < 500) {
			changedir (0);
		}
		avoidwall (0);
	} else {
	        /* Get value from controller and adjust dir. if necessary */
	        osRecvMesg(&controllerMessageQ, NULL, OS_MESG_BLOCK);
	        osContGetReadData(controllerdata);
		if (controllerdata[controller].errno != 0) {
		  button = 0;
		} else {
		  button = controllerdata[controller].button;
		}
		if ((button & CONT_L) && !(lastbutton & CONT_L)) {
			p[0].dir = (p[0].dir + NUMDIRS - 1) % NUMDIRS;
			p[0].wantang -= 90;
			p[0].tipang = 45;
			p[0].numhist++;
		} else if ((button & CONT_R) && !(lastbutton & CONT_R)) {
			p[0].dir = (p[0].dir + 1) % NUMDIRS;
			p[0].wantang += 90;
			p[0].tipang = -45;
			p[0].numhist++;
		}
		lastbutton = button;

		/* If hit wall, die */
		f = field[p[0].x + dx[p[0].dir]][p[0].y + dy[p[0].dir]];
		if (CRASH (f)) {
		  killplayer (0);
		}
	}

	p[0].x += dx[p[0].dir];
	p[0].y += dy[p[0].dir];
	
	p[0].hx[p[0].numhist-1] = p[0].x;
	p[0].hy[p[0].numhist-1] = p[0].y;
}

void sceneDraw (Dynamic *dynamicp, unsigned short *cfbp, Vtx *v)
{
	/*
	 * Use either Frustum or Perspective.  The following two
	 * calls are equivalent.
	 */

	int	i;
    	u16 	perspNorm;

	guPerspective(&dynamicp->projection,&perspNorm, 33, 1, 10, 9000, 1);

/*
	guLookAt (&dynamicp->viewing,
		(p[0].y + cosf (time/2000) * 20)*20,
		20*20,
		(p[0].x + sinf (time/2000) * 20)*20,
		p[0].y * 20, 3 * 20, p[0].x * 20,
		0, 1, 0);
*/

#ifdef GOTO_BOWTIE
	guLookAt (&dynamicp->viewing,
		(p[0].y + cosf (time/2000) * 160)*xzscale,
		160*xzscale,
		(p[0].x + sinf (time/2000) * 160)*xzscale,
		p[0].y * xzscale, 3 * xzscale, p[0].x * xzscale,
		0, 1, 0);
#else
	guLookAt (&dynamicp->viewing,
		(p[0].y + cosf (time/300) * 40)*xzscale +
		   substep*dy[p[0].dir]*(xzscale/SUBSTEP_CNT),
		40*xzscale,
		(p[0].x + sinf (time/300) * 40)*xzscale +
		  substep*dx[p[0].dir]*(xzscale/SUBSTEP_CNT),
		p[0].y * xzscale + substep*dy[p[0].dir]*(xzscale/SUBSTEP_CNT),
		3 * xzscale,
		p[0].x * xzscale + substep*dx[p[0].dir]*(xzscale/SUBSTEP_CNT),
		0, 1, 0);
#endif

	gSPPerspNormalize(glistp++, perspNorm);

	gSPMatrix(glistp++, osVirtualToPhysical(&(dynamicp->projection)),
		G_MTX_PROJECTION|G_MTX_LOAD|G_MTX_NOPUSH);
	gSPMatrix(glistp++, osVirtualToPhysical(&(dynamicp->viewing)),
		G_MTX_MODELVIEW|G_MTX_LOAD|G_MTX_NOPUSH);

	/*
	 * render grid
	 */
	gDPPipeSync(glistp++);
	gDPSetCycleType(glistp++, G_CYC_1CYCLE);
	gSPSetGeometryMode(glistp++, G_SHADE | G_ZBUFFER | G_SHADING_SMOOTH);
	gDPSetCombineMode(glistp++, G_CC_SHADE, G_CC_SHADE);
	gDPSetRenderMode(glistp++, G_RM_AA_OPA_SURF, G_RM_AA_OPA_SURF2);

	gSPDisplayList (glistp++, grid);

#ifdef NOTDEF
	/*
	 * render walls
	 */
	gDPPipeSync(glistp++);
	gDPSetRenderMode(glistp++, G_RM_AA_ZB_OPA_SURF, G_RM_AA_ZB_OPA_SURF2);
	gSPDisplayList (glistp++, walls);
#endif

	/*
	 * render cycle trail
	 */
	gDPPipeSync(glistp++);
	gDPSetRenderMode(glistp++, G_RM_AA_ZB_XLU_SURF, G_RM_AA_ZB_XLU_SURF2);
	for (i = 0; i < p[0].numhist; i++) {
#ifndef GOTO_BOWTIE
	  if (i<p[0].numhist-1) {
#endif
		V (v + 3*i,
			p[0].hy[i]*xzscale, 0, p[0].hx[i]*xzscale,
			0, 0, 0, p[0].red, p[0].green, p[0].blue, 0xa0);
		V (v + 3*i + 1,
			p[0].hy[i]*xzscale, p[0].height*yscale,
                        p[0].hx[i]*xzscale,
			0, 0, 0, p[0].red, p[0].green, p[0].blue, 0xa0);
		V (v + 3*i + 2,
			p[0].hy[i]*xzscale,
		        p[0].height*yscale+(yscale*HEIGHT)/4,
		        p[0].hx[i]*xzscale,
			0, 0, 0, 0, 255, 128, 0xff);
#ifndef GOTO_BOWTIE
	   } else {
		V (v + 3*i,
			p[0].hy[i]*xzscale+
                              substep*dy[p[0].dir]*(xzscale/SUBSTEP_CNT),
		        0,
		        p[0].hx[i]*xzscale+
                              substep*dx[p[0].dir]*(xzscale/SUBSTEP_CNT),
			0, 0, 0, p[0].red, p[0].green, p[0].blue, 0xa0);
		V (v + 3*i + 1,
			p[0].hy[i]*xzscale+
		              substep*dy[p[0].dir]*(xzscale/SUBSTEP_CNT),
                        p[0].height*yscale,
                        p[0].hx[i]*xzscale+
                              substep*dx[p[0].dir]*(xzscale/SUBSTEP_CNT),
			0, 0, 0, p[0].red, p[0].green, p[0].blue, 0xa0);
		V (v + 3*i + 2,
			p[0].hy[i]*xzscale+
                              substep*dy[p[0].dir]*(xzscale/SUBSTEP_CNT),
		        p[0].height*yscale+(yscale*HEIGHT)/4,
		        p[0].hx[i]*xzscale+
		              substep*dx[p[0].dir]*(xzscale/SUBSTEP_CNT),
			0, 0, 0, 0, 255, 128, 0xff);
	   }
#endif
	}

	for (i = 0; i < p[0].numhist - 1; i++) {
		gSPVertex (glistp++, osVirtualToPhysical(v + i*3), 6, 0);
		gSP1Triangle (glistp++, 0, 1, 4, 0);
		gSP1Triangle (glistp++, 0, 4, 3, 0);
		gSP1Triangle (glistp++, 1, 2, 5, 0);
		gSP1Triangle (glistp++, 1, 5, 4, 0);
	}
}

/*
 *
 * Return the lowest number controller connected to system
 */
int initControllers()
{
    int             i;
    u8              pattern;

    osCreateMesgQueue(&serialMessageQ, &serialMsgBuf, 1);
    osSetEventMesg(OS_EVENT_SI, &serialMessageQ, (OSMesg)1);

    osContInit(&serialMessageQ, &pattern, &statusdata[0]);

    osCreateMesgQueue(&controllerMessageQ, &controllerMsgBuf, 1);
    osSetEventMesg(OS_EVENT_SI, &controllerMessageQ, (OSMesg)0);

    for (i = 0; i < MAXCONTROLLERS; i++) {
        if ((pattern & (1<<i)) &&
                !(statusdata[i].errno & CONT_NO_RESPONSE_ERROR))
            return i;
    }
    return -1;
}