main.c 19.2 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
/*====================================================================
 * main.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.
 *====================================================================*/

/*---------------------------------------------------------------------*
        Copyright (C) 1998 Nintendo. (Originated by SGI)
        
        $RCSfile: main.c,v $
        $Revision: 1.1.1.1 $
        $Date: 2002/05/02 03:27:22 $
 *---------------------------------------------------------------------*/

#include <ultra64.h>
#include <ultralog.h>

#include <ramrom.h>
#include <assert.h>

#include <sp.h>

#include "misc.h"
#include "spacerom.h"
#include "audio.h"
#include "gfx.h"
#include <sched.h>
#include "matrix.h"
#include "vector.h"
#include "space.h"
#include "sp_background.h"

#define LOG_SCHEDULE_GFX_TASK	    101
#define LOG_RDP_DONE		    102
#define LOG_RETRACE		    103
#define LOG_INTR		    104

#define SCHED_CONTROLLER_MSG	    (OS_SC_LAST_MSG+1)

/*
 * threads used by this file
 */
static OSThread	gameThread;
static OSThread	mainThread;
static OSThread rmonThread;

/*
 * Stack for boot code.  Space can be reused after 1st thread starts
 */
u64	        bootStack[STACKSIZE/8];

/*
 * Stacks for the threads
 */
static u64	gameThreadStack[STACKSIZE/8];
static u64	cpuThreadStack[STACKSIZE/8];
static u64	mainThreadStack[STACKSIZE/8];
static u64	rmonStack[RMON_STACKSIZE/8];


/*
 * function prototypes for private functions in this file
 */
static void	gameproc(void *);
static void	mainproc(char *);
static void     initAudio(ALHeap *hp);
static void     initGFX(void);
static int	initControllers(void);
static void     romCopy(char *src, char *dest, int len);
static void     createGfxTask(GFXInfo *i);
static void     handleGfxDone(void);
       void     spaceMain(Dynamic *dynamicp, float eyept[3]);
static void     drawScene(Dynamic *dynamicp);
static void     doStats(Dynamic *dynamicp);

/*
 * message queues and message buffers used by this app
 */
static OSMesg           PiMessages[DMA_QUEUE_SIZE];
static OSMesgQueue      PiMessageQ;
OSMesgQueue             dmaMessageQ;
OSMesg                  dmaMessageBuf[MAX_MESGS];

OSMesgQueue		gfxFrameMsgQ;
OSMesg			gfxFrameMsgBuf[MAX_MESGS];

static OSContStatus	statusdata[MAXCONTROLLERS];
static OSContPad	controllerdata[MAXCONTROLLERS];
static OSMesgQueue	timerMesgQueue;
static OSMesg		timerMesgBuf;

/*
 * audio globals
 */
#define MAX_UPDATES             64
#define MAX_EVENTS              32
/*#define AUDIO_HEAP_SIZE         272000*/
#define AUDIO_HEAP_SIZE         372000

u8 audioHeap[AUDIO_HEAP_SIZE];

ALSeqPlayer	   *seqp;
static u8          *seqPtr;
static s32         seqLen;
static ALSeq       *seq;
static ALSeqMarker seqStart;
static ALSeqMarker seqEnd;

ALHeap             hp;

/*
 * graphics globals
 */
static	int	lastx;
static	int	lasty;
static  int	lastbutton = 0;
static	int	velocity = -5;
static  float   initial_pos[3] = { 0, 1000, 0 };

Gfx             *glistp;
char            *staticSegment = 0;
char            *textureSegment = 0;
#ifdef CFB_16_BIT
int             cfb_size = G_IM_SIZ_16b;
#else
int		cfb_size = G_IM_SIZ_32b;
#endif
GFXInfo         gInfo[2];
GFXMsg		controllermsg;
static int	draw_buffer;
static int	framecount;
u64		dram_stack[SP_DRAM_STACK_SIZE64+1];

/*
 * Scheduler globals
 */
OSSched         sc;
u64	        scheduleStack[OS_SC_STACKSIZE/8];

/*
 * logging stuff
 */
#define LOG_LEN 0x8000

OSLog logger;
OSLog *log = &logger;
u32 logData[LOG_LEN];


OSPiHandle	*handler;

void boot(void *arg)
{
    int i;
    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_args((char *)argbuf);
    
    osCreateThread(&mainThread, 1, (void(*)(void *))mainproc, arg,
		  (void *)(mainThreadStack+STACKSIZE/8), (OSPri)MAIN_PRIORITY);

    osStartThread(&mainThread);
}

static void mainproc(char *argv) 
{
    osCreateThread(&gameThread, 6, gameproc, argv,
		   gameThreadStack+STACKSIZE/8, (OSPri)GAME_PRIORITY);

    /*
     * Start PI Mgr for access to cartridge - start before debugger?
     */
    osCreatePiManager((OSPri) OS_PRIORITY_PIMGR, &PiMessageQ, PiMessages,
			DMA_QUEUE_SIZE);

    /*
     * determine what mode to start up in.
     */
    
    osCreateThread(&rmonThread, 0, rmonMain, (void *)0,
                   (void *)(rmonStack+RMON_STACKSIZE/8),
                   (OSPri) OS_PRIORITY_RMON );
    osStartThread(&rmonThread);

    if (!debugger)
        osStartThread(&gameThread);

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

static void gameproc(void *argv)
{
    int		displaybuffer = 0;
    int         drawbuffer = 0;
    int         bufCnt = 0;
    int		controllervalid;
    int		controller;
    GFXMsg      *msg;
    OSScClient  client;
    GFXInfo	*gfxp;
    OSContPad	*pad;
    u8          mode;
    
    if (logging)
        osCreateLog(log, logData, LOG_LEN);

    osCreateMesgQueue(&dmaMessageQ, dmaMessageBuf, MAX_MESGS);
    
    /* Initialize the RCP task scheduler */
#ifdef CFB_16_BIT
    mode = OS_VI_NTSC_LAN1;
#else
    mode = OS_VI_NTSC_LAN2;
#endif
    
    osCreateScheduler(&sc, (void *)(scheduleStack + OS_SC_STACKSIZE/8),
                      SCHEDULER_PRIORITY, mode, 1);
    
    initGFX();

    if (!silent) {
	initAudio(&hp);    
	alSeqpPlay(seqp);
    }

    gInfo[0].msg.gen.type = OS_SC_DONE_MSG;
    gInfo[0].cfb = cfb_a;
    gInfo[1].msg.gen.type = OS_SC_DONE_MSG;
    gInfo[1].cfb = cfb_b;
    controllermsg.gen.type = SCHED_CONTROLLER_MSG;

    osCreateMesgQueue(&gfxFrameMsgQ, gfxFrameMsgBuf, MAX_MESGS);
    
    osScAddClient(&sc, &client, &gfxFrameMsgQ);    

    if ((controller = initControllers()) < 0)
	controllervalid = 0;
    else
	controllervalid = 1;

    osSetEventMesg(OS_EVENT_SI, &gfxFrameMsgQ, (OSMesg)&controllermsg);
    
    while (1) {
        (void) osRecvMesg(&gfxFrameMsgQ, (OSMesg *)&msg, OS_MESG_BLOCK);

        switch (msg->gen.type) {
	    /*
	     * Received every retrace
	     * Create a new gfx task unless we are overrunning
	     */
            case (OS_SC_RETRACE_MSG):
	    
		if (logging)
		    osLogEvent(log, LOG_RETRACE, 1, bufCnt);
		    
                if (bufCnt < 2) {
                    createGfxTask(&gInfo[drawbuffer]);
                    bufCnt++;
                    drawbuffer ^= 1;
                }
		
		/* request latest controller information */
		if (controllervalid) {
		    osContStartReadData(&gfxFrameMsgQ);
		    controllervalid = 0;
		}
                break;

            case (OS_SC_DONE_MSG):
	    
		gfxp = &gInfo[displaybuffer];
		
		if (logging)
		    osLogEvent(log, LOG_RDP_DONE, 3, gfxp->cfb, framecount);
	    
		displaybuffer ^= 1;
		framecount++;
                bufCnt--;
		if (framecount >= 3000)
		    framecount = 0;
                break;
		
	    case SCHED_CONTROLLER_MSG:

		osContGetReadData(controllerdata);
		controllervalid = 1;
		pad = &controllerdata[controller];

		lastbutton = pad->button;
		lastx = pad->stick_x;
		lasty = pad->stick_y;
		break;
		
            default:
		PRINTF("gameproc loop unknown intr 0x%x\n", msg->gen.type);
                break;
        }
    }
    alSeqpStop(seqp);
}

static void initAudio(ALHeap *hp) 
{
    ALBankFile  *bankPtr;
    int         bankLen;
    ALSynConfig c;
    ALSeqpConfig seqc;

    alHeapInit(hp, audioHeap, sizeof(audioHeap));    

    /*
     * Load the bank file from ROM
     */
    bankLen = _bankSegmentRomEnd - _bankSegmentRomStart;
    bankPtr = alHeapAlloc(hp, 1, bankLen);
    romCopy(_bankSegmentRomStart, (char *)bankPtr, bankLen);
    
    alBnkfNew(bankPtr, (u8 *) _tableSegmentRomStart);

    /*
     * Load the sequence file from ROM
     */
    seqLen = _seqSegmentRomEnd - _seqSegmentRomStart;
    seqPtr = alHeapAlloc(hp, 1, seqLen);
    romCopy(_seqSegmentRomStart, (char *) seqPtr, seqLen);

    /*
     * Create the Audio Manager
     */
    c.maxVVoices = MAX_VOICES;
    c.maxPVoices = MAX_VOICES;
    c.maxUpdates = MAX_UPDATES;
    c.dmaproc    = 0;                  /* audio mgr will fill this in */
    c.fxType	 = AL_FX_SMALLROOM;
    c.outputRate = osAiSetFrequency(32000);
    c.heap       = hp;

    amCreateAudioMgr(&c, AUDIO_PRIORITY);
    
    /*
     * Create the sequence and the sequence player
     */
    seqc.maxVoices      = MAX_VOICES;
    seqc.maxEvents      = MAX_EVENTS;
    seqc.maxChannels    = 16;
    seqc.heap           = hp;
    seqc.initOsc        = 0;
    seqc.updateOsc      = 0;
    seqc.stopOsc        = 0;
    
    seqp = alHeapAlloc(hp, 1, sizeof(ALSeqPlayer));
    alSeqpNew(seqp, &seqc);

    seq = alHeapAlloc(hp, 1, sizeof(ALSeq));
    alSeqNew(seq, seqPtr, seqLen);    
    alSeqNewMarker(seq, &seqStart, 0);
    alSeqNewMarker(seq, &seqEnd, -1);

    alSeqpLoop(seqp, &seqStart, &seqEnd, -1);
    alSeqpSetSeq(seqp, seq);
    alSeqpSetBank(seqp, bankPtr->bankArray[0]);
}

static void initGFX() 
{
    int len = _staticSegmentRomEnd - _staticSegmentRomStart;
    staticSegment = _yieldSegmentEnd;
    romCopy(_staticSegmentRomStart, staticSegment, len);    
}

/*
 *
 * Return the lowest number controller connected to system
 */
static int initControllers()
{
    OSMesgQueue	    serialMsgQ;
    OSMesg	    serialMsg;
    int		    i;
    u8		    pattern;

    osCreateMesgQueue(&serialMsgQ, &serialMsg, 1);
    osSetEventMesg(OS_EVENT_SI, &serialMsgQ, (OSMesg)1);

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

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

}


void createGfxTask(GFXInfo *i) 
{
    static int  count = 0;
    u32 x,y,pixel,clearcolor;
    u8  red,green,blue;
    Dynamic *dynamicp;
    static Gfx *new_background_dl = NULL;
    OSScTask *t;
    
    /*
     * pointers to build the display list.
     */
    dynamicp = &i->dp;
    glistp   = i->dp.glist;

    /*
     * Tell RCP where each segment is
     */
    gSPSegment(glistp++, PHYSICAL_SEGMENT, 0);
    gSPSegment(glistp++, STATIC_SEGMENT,  osVirtualToPhysical(staticSegment));
    gSPSegment(glistp++, DYNAMIC_SEGMENT, osVirtualToPhysical(dynamicp));
    gSPSegment(glistp++, TEXTURE_SEGMENT, osVirtualToPhysical(textureSegment));


    /*
     * Graphics pipeline state initialization
     */
    gSPDisplayList(glistp++, setup_rspstate);
    gSPDisplayList(glistp++, setup_rdpstate);
    gDPSetDepthImage(glistp++, osVirtualToPhysical(zbuffer));
    
    /*
     * clear z, z = max z, dz = 0
     */
    gDPPipeSync(glistp++);
    gDPSetCycleType(glistp++, G_CYC_FILL);

    gDPSetColorImage(glistp++, G_IM_FMT_RGBA, G_IM_SIZ_16b, SCREEN_WD,
		osVirtualToPhysical(zbuffer));
    gDPSetFillColor(glistp++, GPACK_ZDZ(G_MAXFBZ,0) << 16 |
	GPACK_ZDZ(G_MAXFBZ,0));
    gDPFillRectangle(glistp++, 0, 0, SCREEN_WD-1, SCREEN_HT-1);
	
    /*
     * Clear framebuffer cvg = FULL or 1
     */
#ifdef NO_BACKGROUND

    gDPPipeSync(glistp++);
#ifdef CFB_16_BIT
    gDPSetColorImage(glistp++, G_IM_FMT_RGBA, G_IM_SIZ_16b, SCREEN_WD,
		     osVirtualToPhysical(i->cfb));
    clearcolor = GPACK_RGBA5551(0, 0, 0, 1) << 16 | 
    		 GPACK_RGBA5551(0, 0, 0, 1);
#else
    gDPSetColorImage(glistp++, G_IM_FMT_RGBA, G_IM_SIZ_32b, SCREEN_WD,
		     osVirtualToPhysical(i->cfb));
    clearcolor = (0<<24) | (200<<16) | (0<<8) | (0xff);
#endif
    gDPSetFillColor(glistp++, clearcolor);
    gDPFillRectangle(glistp++, 0, 0, SCREEN_WD-1, SCREEN_HT-1);

#else

    /*
     * Draw background
     */
    gDPPipeSync(glistp++);
#ifdef CFB_16_BIT
    gDPSetColorImage(glistp++, G_IM_FMT_RGBA, G_IM_SIZ_16b,
                     SCREEN_WD, osVirtualToPhysical(i->cfb));
#else
    gDPSetColorImage(glistp++, G_IM_FMT_RGBA, G_IM_SIZ_32b,
                     SCREEN_WD, osVirtualToPhysical(i->cfb));
#endif

    gDPSetTexturePersp (glistp++,G_TP_PERSP);
    gDPSetTextureDetail (glistp++,G_TD_CLAMP);
    gDPSetTextureLOD (glistp++,G_TL_TILE);
    gDPSetTextureLUT (glistp++,G_TT_NONE);
    gDPSetTextureFilter (glistp++,G_TF_BILERP);
    gDPSetTextureConvert(glistp++,G_TC_FILT);

    spInit( &glistp );

    if( new_background_dl == NULL ) {
	spClearAttribute( &background_sprite, SP_FASTCOPY );
	new_background_dl = spDraw( &background_sprite );
    };

    gSPDisplayList(glistp++, new_background_dl);

    spFinish( &glistp );

    glistp--;

#endif


    /*
     * Get ready to draw scene
     */
    gDPPipeSync(glistp++);
    gDPSetCycleType(glistp++, G_CYC_1CYCLE);
    gDPPipelineMode(glistp++, G_PM_1PRIMITIVE);
    assert((void *)glistp < (void *)&i->msg);


    /*
     * Render the scene for this frame
     */
    drawScene(dynamicp);
	
#ifdef DO_STATS
    doStats(dynamicp);
#endif
    
    /*
     * Force top-level display list to have an END.
     */
    gDPFullSync(glistp++);
    gSPEndDisplayList(glistp++);

    /* Flush the dynamic segment */
    osWritebackDCache(&i->dp, (int)glistp - (int)&i->dp);

    /* build graphics task */
    t = &i->task;
    t->list.t.data_ptr = (u64 *) dynamicp->glist;
    t->list.t.data_size = (int)(glistp - dynamicp->glist) * sizeof (Gfx);
    t->list.t.type = M_GFXTASK;
    t->list.t.flags = 0x0;
    t->list.t.ucode_boot = (u64 *)rspbootTextStart;
    t->list.t.ucode_boot_size =
        ((int) rspbootTextEnd - (int) rspbootTextStart);
    t->list.t.ucode = (u64 *) gspFast3DTextStart;
    t->list.t.ucode_data = (u64 *) gspFast3DDataStart;
    t->list.t.ucode_data_size = 2048;
    t->list.t.dram_stack = (u64 *) (((int)dram_stack+15)&0xfffffff0);
    t->list.t.dram_stack_size = 1024;
    t->list.t.output_buff = (u64 *) 0x0;
    t->list.t.output_buff_size = (u64 *) 0x0;
    t->list.t.yield_data_ptr = (u64 *) gfxYieldBuf;
    t->list.t.yield_data_size = OS_YIELD_DATA_SIZE;

    t->next     = 0;                   /* paranoia */
    t->flags	= OS_SC_NEEDS_RSP | OS_SC_NEEDS_RDP | OS_SC_LAST_TASK |
        OS_SC_SWAPBUFFER;
    t->msgQ     = &gfxFrameMsgQ;       /* reply to when finished */
    t->msg      = (OSMesg)&i->msg;     /* reply with this message */
    t->framebuffer = (void *)i->cfb;
	
    osSendMesg(osScGetCmdQ(&sc), (OSMesg) t, OS_MESG_BLOCK);
}


void doStats(Dynamic *dynamicp)
{
    u16 shortcolor;

    gDPPipeSync(glistp++);
    gDPSetCycleType(glistp++, G_CYC_FILL);
    gDPSetRenderMode(glistp++, G_RM_OPA_SURF, G_RM_OPA_SURF2);
    
    shortcolor = GPACK_RGBA5551(0xff, 0, 0, 1);
    gDPSetFillColor(glistp++, shortcolor << 16 | shortcolor);
    gDPFillRectangle(glistp++, 40, 210, 40+framecount/20, 220);
    
    gDPPipeSync(glistp++);
    shortcolor = GPACK_RGBA5551(0, 0, 0, 1);
    gDPSetFillColor(glistp++, shortcolor << 16 | shortcolor);
    gDPFillRectangle(glistp++, 40, 205, 42, 225);
    gDPFillRectangle(glistp++, 40+50, 205, 42+50, 225);
    gDPFillRectangle(glistp++, 40+100, 205, 42+100, 225);
    gDPFillRectangle(glistp++, 40+150, 205, 42+150, 225);
    
}


/*
 * The slop factor is the amount of slop we allow in the joystick 
 * before registering a movement.
 */
#define SLOP_FACTOR 12

void drawScene(Dynamic *dynamicp)
{
    const float 	deltarot = 0.08f;   /* empirically determined */
    const float		deltatrans = 100.0;  /* empirically determined */
    static float	logo_theta = 0;

    vec3	eyept;			/* User's eye in world coords	*/
    float 	phi;			/* Used to generate incRot Mtx	*/
    float 	axis[3];		/* Used to generate incRot Mtx	*/
    float 	dx = 0, dy = 0, dz = 0;	/* translation increments 	*/
    float 	rx = 0, ry = 0, rz = 0;	/* rotation increments		*/
    Mtx		incRot, incTrans;	/* incremental rotation matrix	*/
    Mtx		incMat;			/* incremental xlation matrix	*/
    FMatrix	viewMat, invMat;	/* viewing matrix and inverse	*/
    u16 	perspNorm;		/* used for perspective proj.    */
    static Mtx	projection;		/* projection matrix 		*/
    static Mtx	lastViewMat;		/* last frame's viewing mat 	*/
    static int	firstcall = 1;


    guPerspective(&dynamicp->projection,&perspNorm,
                  45,320.0/240.0,10.0,2000.0,1.0);

    /*
     * The 'G' button resets the user's position
     */
    if (lastbutton & CONT_G || lastbutton & CONT_START || firstcall)
    {
	eyept[0] = initial_pos[0];
	eyept[1] = initial_pos[1];
	eyept[2] = initial_pos[2];
	guLookAt(&lastViewMat, initial_pos[0],initial_pos[1],initial_pos[2],
		 0,0,0, 0,0,1);

	firstcall = 0;
    }


    /*
     * Now map the controller to movements. The joystick is for
     * turning left, right and moving back and forth. Up and down on the
     * crosshair tilts the view up and down; left and right on the crosshair
     * 'sidestep' to the left and right. The six 'fire buttons' accomplish
     * the same suite of movements.
     */
    
    if (lastx > SLOP_FACTOR) { 
	ry = (float) (lastx-SLOP_FACTOR) / 80.0f * deltarot; 
    }
    else if (lastx < -SLOP_FACTOR) {
	ry = (float) (lastx+SLOP_FACTOR) / 80.0f * deltarot;
    }
    if (lasty > SLOP_FACTOR) {
	dz = (float) (lasty - SLOP_FACTOR) / 80.0f * 2 * deltatrans;
    }
    else if (lasty < -SLOP_FACTOR) {
	dz = (float) (lasty + SLOP_FACTOR) / 80.0f * 2 * deltatrans;
    }
    if (lastbutton & CONT_E) {
        rx = (float) 0.1f * deltarot;
    }
    if (lastbutton & CONT_F) {
        rx = (float) -0.1f * deltarot;
    }
    if ((lastbutton & CONT_UP) |lastbutton & CONT_A) {
	dy = -0.2f * deltatrans;
    }
    if ((lastbutton & CONT_DOWN) | (lastbutton & CONT_B)) {
	dy = 0.2f * deltatrans;
    }
    if (lastbutton & CONT_C) {
	rx = (float) 0.1f * deltarot;
    }
    if (lastbutton & CONT_D) {
	rx = (float) - 0.1f * deltarot;
    }
      
    if (lastbutton & (CONT_LEFT | CONT_L)) {
	dx = 0.2f * deltatrans;
    }
    if (lastbutton & (CONT_RIGHT | CONT_R)) {
	dx = - 0.2f * deltatrans;
    }
    phi = sqrtf(rx*rx + ry*ry);
    if (phi == 0)
    {	
	makeIdentMtx(&incRot);		/* paranoia; phi _shouldn't_ be 0 */
    }
    else
    {
	axis[0] = rx/phi;
	axis[1] = ry/phi;
	axis[2] = 0.0;
	NORMALIZE_VEC3(axis);
	guRotate(&incRot, phi * 360.0 / 6.283,
		 axis[0], axis[1], axis[2]);
    }

    guTranslate(&incTrans, dx, dy, dz);
    multMtx(&incMat, &incRot, &incTrans); 
    multMtx(&dynamicp->viewing, &lastViewMat, &incMat);
    copyMtx(&lastViewMat, &dynamicp->viewing);
    
    /* obtain eye point in world space */
    guMtxL2F(viewMat, &dynamicp->viewing);
    floatMatInvert(invMat, viewMat);
    eyept[0] = invMat[3][0];
    eyept[1] = invMat[3][1];
    eyept[2] = invMat[3][2];

    spaceMain(dynamicp, eyept);

}


void romCopy(char *src, char *dest, int len)
{
    OSIoMesg dmaIoMesgBuf;
    OSMesg dummyMesg;
    
    osInvalDCache(dest, len);   /* invalidate buffer */

    dmaIoMesgBuf.hdr.pri      = OS_MESG_PRI_NORMAL;
    dmaIoMesgBuf.hdr.retQueue = &dmaMessageQ;
    dmaIoMesgBuf.dramAddr     = dest;
    dmaIoMesgBuf.devAddr      = (u32)src;
    dmaIoMesgBuf.size         = len;

    osEPiStartDma(handler, &dmaIoMesgBuf, OS_READ);
    (void) osRecvMesg(&dmaMessageQ, &dummyMesg, OS_MESG_BLOCK);
}