game.c 14.7 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

/*
 * Main program for R4300 for a disk write program. Since this program is
 * using host io protocol and host io protocol uses Int 1 and cart int, we
 * have to do a certain tricky technique to share these interrupts between
 * host io and 64 disk drive. The method I used here is "changing" the 
 * interrupt service routine of Int 1 and the message queue that the message
 * from cartridge will be sent. See the source code of osLeoSwitch() for
 * more information.
 */
#include "hostio.h"

/*
 * Stacks for the threads as well as message queues for synchronization
 * This stack is ridiculously large, and could also be reclaimed once
 * the main thread is started.
 */
u64	bootStack[STACKSIZE/sizeof(u64)];

static void	idle(void *);
static void	mainproc(void *);

static OSThread	idleThread;
static u64	idleThreadStack[STACKSIZE/sizeof(u64)];

static OSThread	mainThread;
static u64	mainThreadStack[5 * STACKSIZE/sizeof(u64)];

/*
 * Message queues
 */
#define NUM_PI_MSGS     8
static OSMesg PiMessages[NUM_PI_MSGS];
static OSMesgQueue PiMessageQ;

static OSMesgQueue	diskQ;		/* post queue */
static OSMesg		diskQBuf;

#define NUM_CMD_MSGS	8
static OSMesg           cmdQBuf[8];

/*
 * Tables to show where the system, defect and id blocks are.
 */
s32	sysLBAs[4] 	= { -22, -21, -14, -13 };
s32	defLBAs[] 	= { -20, -19 };
s32	idLBAs[] 	= { -10, -9 };

static char	dummybuf[232 * SECPERBLOCK + 15];
static char	dummybuf2[232 * SECPERBLOCK + 15];
sysInfo	*systemData;

extern  s32 LeoSysReadWrite(LEOCmd *, s32, s32, void *,
			    u32, OSMesgQueue *);

/*
 * Prototypes
 */
static	s32   	error_handle(s32);
static 	void 	formatDisk(ctrlblk *);
static 	void 	setID(ctrlblk *);
static	s32 	readSysArea(sysInfo *sbuf);
static	s32 	readIdArea(idInfo *ibuf);
static	void 	getDiskInfo(ctrlblk *sdata);

/*
 * Definition for leoSwitch
 */
#define	DISK	0
#define	INDY	1

/*
 * Definition for error handling routine
 */
#define	RETRY	0
#define	FATAL	1

/*
 * These variables are defined in each file to align 16bytes.
 * These are for communication to Indy.
 */
extern	u64	rdramBuf[];
extern	ctrlblk	data;
extern	s32	transbyte;

extern u32 LEO_country_code;

void
boot(void)
{
    
  osInitialize();
  
  osCreateThread(&idleThread, 1, idle, (void *)0,
		 idleThreadStack+STACKSIZE/sizeof(u64), 10);
  osStartThread(&idleThread);
  
  /* never reached */
}

static void
idle(void *arg)
{
  /*
   * Create main thread
   */
  osCreateThread(&mainThread, 3, mainproc, arg,
		 mainThreadStack+STACKSIZE/sizeof(u64), 10);
  osStartThread(&mainThread);
  
  /*
   * Become the idle thread
   */
  osSetThreadPri(0, 0);
  
  for (;;);
}

/*
 * This is the main routine of the app.
 */
static void
mainproc(void *arg)
{
  LEOError	error;
  LEOCmd	cmdBlock;
  LEODiskID	dummyDiskID;
  s32		curLBA;
  u32		transLBA;
  u32		needLBAs;
  u32		bufferLBAs;
  u32		remain;
  u32		result;
  u32		i;
  
  systemData = (sysInfo *)OS_DCACHE_ROUNDUP_ADDR((void *)dummybuf);

  osCreateMesgQueue(&diskQ, &diskQBuf, 1);

  osCreatePiManager((OSPri)OS_PRIORITY_PIMGR, &PiMessageQ, PiMessages,
		    NUM_PI_MSGS);

  error = LeoCreateLeoManager((OSPri)OS_PRIORITY_LEOMGR-1,
			      (OSPri)OS_PRIORITY_LEOMGR,
			      cmdQBuf, NUM_CMD_MSGS);

  if (error == LEO_ERROR_DEVICE_COMMUNICATION_FAILURE)
  {
    osSyncPrintf("leowrite: Drive doesn't exist\n");
    for(;;);
  }
  
  LEO_country_code = 0;
  SAVE_COUNTRY(0);		/* paranoia */

  /*
   * Clear "reset" flag.
   */
  LeoResetClear();

  /*
   * モータはあまり止まらないように設定
   */
  LeoModeSelectAsync(&cmdBlock, 10, 10, &diskQ);
  osRecvMesg(&diskQ, NULL, OS_MESG_BLOCK);

  while(1)			/* main loop */
  {
    /*
     * Send 'Ready' to Indy
     */
    osWriteHost(rdramBuf, 4);

    /*
     * First, we get the type, filesize and offset.
     */
    osReadHost(&data, sizeof(ctrlblk));

    if (data.cmdFlags & END_COMMAND)
    {
      /*
       * End of leowrite. Stop the motor of the drive
       */
      do
      {
	LeoSpdlMotor(&cmdBlock, SLEEP, &diskQ);
	osRecvMesg(&diskQ, NULL, OS_MESG_BLOCK);
    
	if ((GET_ERROR(cmdBlock) == 0) || 
	    (GET_ERROR(cmdBlock) == LEO_ERROR_MEDIUM_NOT_PRESENT))
	  break;
	if (error_handle(GET_ERROR(cmdBlock)) == FATAL)
	{
	  osSyncPrintf("Fatal error\n");
	  for(;;);
	}
    
      } while(1);
    
      /*
       * Send back the end command
       */
      osWriteHost(&data, sizeof(ctrlblk));
  
      /* Wait host to kill me */
      for(;;);

    }
  
    if (data.cmdFlags & SEND_ME_INFO)
    {
      getDiskInfo(&data);
    
      if ( !(data.cmdFlags & SYS_UNFORMATTED) ) /* if sys is unformatted, might hang */
      {
	/*
	 * Stop the motor of the drive
	 */
	do
	{
	  LeoSpdlMotor(&cmdBlock, SLEEP, &diskQ);
	  osRecvMesg(&diskQ, NULL, OS_MESG_BLOCK);
    
	  if ((GET_ERROR(cmdBlock) == 0) || 
	      (GET_ERROR(cmdBlock) == LEO_ERROR_MEDIUM_NOT_PRESENT))
	    break;
	  if (error_handle(GET_ERROR(cmdBlock)) == FATAL)
	  {
	    osSyncPrintf("Fatal error\n");
	    for(;;);
	  }
    
	} while(1);
      }
    
      osWriteHost(&data, sizeof(ctrlblk));

      continue;
      
    } /* if (SEND_ME_INFO) */
  
    /*
     * If the system area has never been written, format the disk.
     * Otherwise, change the area according as the specified parameters.
     */
    formatDisk(&data);
  
    /*
     * Set ID number
     */
    setID(&data);
  
    /*
     * Do dummy "readid" to read system info
     */
    do{
      LeoReadDiskID(&cmdBlock, &dummyDiskID, &diskQ);
      osRecvMesg(&diskQ, NULL, OS_MESG_BLOCK);

      if(GET_ERROR(cmdBlock) == 0)
	break;
      if (error_handle(GET_ERROR(cmdBlock)) == FATAL)
      {
	osSyncPrintf("Fatal error\n");
	for(;;);
      }
    } while(1);

    curLBA = data.offset;
    /*
     * If the filesize is larger than the size of the remained area,
     * we quit the operation here.
     */
    do{
      result = LeoLBAToByte(curLBA, NUM_LBAS - curLBA, (s32 *)&remain);

      if(result == 0)
	break;
      if (error_handle(result) == FATAL)
      {
	osSyncPrintf("Fatal error\n");
	for(;;);
      }
    } while(1);
  
    if (remain < data.filesize)
    {
      osSyncPrintf("file is too large: remaining disk space %d, filesize %d\n",
		   remain, data.filesize);
      transbyte = -1;
      osWriteHost(&transbyte, sizeof(transbyte));
    
      /* Wait host to kill me */
      for(;;);
    }
  
    /*
     * Calculate how many lbas are needed to write the file.
     */
    do{
      result = LeoByteToLBA(curLBA, data.filesize, (s32 *)&needLBAs);
      if (result == 0)
	break;
      if (error_handle(result) == FATAL)
      {
	osSyncPrintf("Fatal error\n");
	for(;;);
      }
    } while(1);

    osSyncPrintf("Use from LBA%d to %u", curLBA,
		 curLBA + needLBAs - 1);
  
    osSyncPrintf("(%d LBA(s))\n", needLBAs);

    /*
     * Main loop
     */
    while(data.filesize > 0)
    {
      /*
       * Calculate how many lbas can be stored in the RDRAM buffer.
       */
      if (BUFSIZE > remain)
	bufferLBAs = NUM_LBAS - curLBA;
      else
      {
	do{
	  result = LeoByteToLBA(curLBA, BUFSIZE, (s32 *)&bufferLBAs);

	  if(result == 0)
	    break;
	  if (error_handle(result) == FATAL)
	  {
	    osSyncPrintf("Fatal error\n");
	    for(;;);
	  }
	} while(1);

	/*
	 * Since BUFSIZE is smaller than the size of bufferLBAs,
	 * decrement bufferLBAs.
	 * XXX we might miss one loop (when BUFSIZE = bufferLBAs)
	 */
	bufferLBAs --;
      }

      /*
       * Calculate how many lbas and bytes should be transferred this
       * time.
       */
      transLBA = Min(needLBAs, bufferLBAs);
      do{
	result = LeoLBAToByte(curLBA, transLBA, (s32 *)&transbyte);

	if(result == 0)
	  break;
	if (error_handle(result) == FATAL)
	{
	  osSyncPrintf("Fatal error\n");
	  for(;;);
	}
      } while(1);
    
      /* XXX need to fix a bug. What if filesize = 10000, remain = 19720 and curLBA = 0? */

      /*
       * Ask host to transfer <transbyte> bytes
       */
      osWriteHost(&transbyte, sizeof(transbyte));
    
      osReadHost(rdramBuf, transbyte);
    
      /*
       * Write <transLBA> blocks to disk
       */
      do{
	LeoSysReadWrite(&cmdBlock, OS_WRITE,
			(u32)curLBA, (void *)rdramBuf, (u32)transLBA, &diskQ);
	osRecvMesg(&diskQ, NULL, OS_MESG_BLOCK);

	if(GET_ERROR(cmdBlock) == 0)
	  break;
	if (error_handle(GET_ERROR(cmdBlock)) == FATAL)
	{
	  osSyncPrintf("Fatal error\n");
	  for(;;);
	}
      } while(1);

      curLBA += transLBA;
      needLBAs -= transLBA;
      data.filesize -= transbyte;
      remain -= transbyte;
    
    } /* while */

    /*
     * Send end mark to host
     */
    transbyte = -1;
    osWriteHost(&transbyte, sizeof(transbyte));

  } /* while */
  
} /* mainproc() */

/*
 * Error handling routine. In this version, I don't check the ID blocks.
 * XXX need to fix
 */
s32 error_handle(s32 result)
{
  LEOCmd	cmdBlock;
  LEODiskID	dummyDiskID;
  LEOStatus	test;
  s32		res;
  
  /*
   * If the error is not one of these two, consider it as fatal.
   */
  if (!( (result == LEO_ERROR_MEDIUM_NOT_PRESENT) ||
      (result == LEO_ERROR_MEDIUM_MAY_HAVE_CHANGED ) ))
    return FATAL;
  
  /*
   * Since this version doesn't check the ID, I don't care if the disk
   * might have been changed.
   */
  if (result == LEO_ERROR_MEDIUM_MAY_HAVE_CHANGED)
    return RETRY;
  
  osSyncPrintf("Insert a disk\n");

  /*
   *  Wait for the users to insert the disk.
   *  (myLeoReadDiskID command doesn't check the system area).
   */
  do
  {
    myLeoReadDiskID(&cmdBlock, &dummyDiskID, &diskQ);
    osRecvMesg(&diskQ, (OSMesg *)&res, OS_MESG_BLOCK);
    
    if ( res != LEO_ERROR_MEDIUM_NOT_PRESENT )
      break;

  } while(1);
  
  return RETRY;

} /* error_handle() */


#define	FORMATTED	0
#define	UNFORMATTED	1

s32 readSysArea(sysInfo *sbuf)
{
  LEOCmd	cmdBlock;

  do{
    LeoSysReadWrite(&cmdBlock, OS_READ,
		    sysLBAs[0], (void *)sbuf, 1, 
		    &diskQ);
    osRecvMesg(&diskQ, NULL, OS_MESG_BLOCK);

    if(GET_ERROR(cmdBlock) == 0)
    {
      return FORMATTED;
    }
    if(GET_ERROR(cmdBlock) == LEO_ERROR_UNRECOVERED_READ_ERROR)
    {
      return UNFORMATTED;
    }
    if (error_handle(GET_ERROR(cmdBlock)) == FATAL)
    {
      osSyncPrintf("Fatal error\n");
      for(;;);
    }
  } while(1);

} /* readSysArea() */


s32 readIdArea(idInfo *ibuf)
{
  LEOCmd	cmdBlock;

  do{
    LeoSysReadWrite(&cmdBlock, OS_READ,
		    idLBAs[0], (void *)ibuf, 1, 
		    &diskQ);
    osRecvMesg(&diskQ, NULL, OS_MESG_BLOCK);

    if(GET_ERROR(cmdBlock) == 0)
      return FORMATTED;
    else if(GET_ERROR(cmdBlock) == LEO_ERROR_UNRECOVERED_READ_ERROR)
    {
      bzero((void *)ibuf, 232 * SECPERBLOCK);
      return UNFORMATTED;
    }
      
    if (error_handle(GET_ERROR(cmdBlock)) == FATAL)
    {
      osSyncPrintf("Fatal error\n");
      for(;;);
    }
  } while(1);

} /* readIdArea() */


void getDiskInfo(ctrlblk *sdata)
{
  sysInfo	*sbuf;
  idInfo	*ibuf;
  
  sbuf = (sysInfo *)OS_DCACHE_ROUNDUP_ADDR((void *)dummybuf2);

  if(readSysArea(sbuf) == UNFORMATTED)
  {
    sdata->cmdFlags |= SYS_UNFORMATTED;
    return;
  }

  sdata->offset 	= ((sysInfo *)sbuf)[0].IPLSize;
  sdata->type 		= ((sysInfo *)sbuf)[0].type & 0x0f;
  sdata->address 	= ((sysInfo *)sbuf)[0].loadAddr;

  ibuf = (idInfo *)sbuf;
  
  if(readIdArea(ibuf) == UNFORMATTED)
  {
    sdata->cmdFlags |= ID_UNFORMATTED;
    return;
  }

/*  bcopy((void *)&ibuf->diskID.serialNumber,
	(void *)&sdata->diskID.serialNumber, sizeof(LEOSerialNum));
*/
  bcopy((void *)&ibuf->diskID,
	(void *)&sdata->diskID, sizeof(LEODiskID));
  
  return;
  
} /* getDiskInfo */


void formatDisk(ctrlblk *sdata)
{
  LEOCmd	cmdBlock;
  u32		i;
  sysInfo	*defData;
  u8		overwrite;

  defData = (sysInfo *)OS_DCACHE_ROUNDUP_ADDR((void *)dummybuf2);

  /*
   * Write bad ecc
   */
  for (i = 0; i < 232 * 85/sizeof(u32); i++)
    ((u32 *)defData)[i] = 0x00000000;
  
  do{
    LeoSysReadWrite(&cmdBlock, OS_WRITE,
		    -12, (void *)defData, 1, &diskQ);
    osRecvMesg(&diskQ, NULL, OS_MESG_BLOCK);

    if( GET_ERROR(cmdBlock) == 0)
      break;
    if (error_handle(GET_ERROR(cmdBlock)) == FATAL)
    {
      osSyncPrintf("Fatal error\n");
      for(;;);
    }
  } while(1);
  
  if ((sdata->cmdFlags & DO_FORMAT) || (readSysArea(systemData) == UNFORMATTED))
  {
    osSyncPrintf("Formatting the disk ...\n");

    /*
     * Read the defect information block. It's readable now since the country
     * code has been set correctly.
     */
    do{
      LeoSysReadWrite(&cmdBlock, OS_READ,
		      defLBAs[0], (void *)defData, 1, &diskQ);
      osRecvMesg(&diskQ, NULL, OS_MESG_BLOCK);

      if( GET_ERROR(cmdBlock) == 0)
	break;
      if (error_handle(GET_ERROR(cmdBlock)) == FATAL)
      {
	osSyncPrintf("Fatal error\n");
	for(;;);
      }
    } while(1);
    
    /*
     * Copy the defect information block to the sys. info. buffer.
     */
    bcopy(defData, systemData, 232 * SECPERBLOCK);

    /*
     * Copy the format byte to the disk type byte.
     */
    for (i = 0; i < SECPERBLOCK; i++)
      systemData[i].type		= systemData[i].formatType;
  }
  
  /*
   * Put the specified values to the buffer
   */
  for(i = 0; i < SECPERBLOCK; i++)
  {
    systemData[i].country		= 0;
    if (sdata->type != TYPE_DONT_CHANGE)
      systemData[i].type    	= sdata->type + (systemData[i].type & 0xf0);
    if (sdata->offset == 0)
      systemData[i].IPLSize 	= (u32)((data.filesize + 19719) / 19720);
    if (sdata->address != NULL)
      systemData[i].loadAddr	= sdata->address;
  }
  osSyncPrintf("type = %d, addr = 0x%x\n",
	       systemData[0].type & 0x0f, 
	       systemData[0].loadAddr);
  
  /*
   * Write it to the system information block
   */
  for (i = 0; i < 4; i++)
    do{
      LeoSysReadWrite(&cmdBlock, OS_WRITE,
		      sysLBAs[i], (void *)systemData, 1, 
		      &diskQ);
      osRecvMesg(&diskQ, NULL, OS_MESG_BLOCK);

      if( GET_ERROR(cmdBlock) == 0)
	break;
      if (error_handle(GET_ERROR(cmdBlock)) == FATAL)
      {
	osSyncPrintf("Fatal error\n");
	for(;;);
      }
    } while(1);

  return;

} /* formatDisk() */


void setID(ctrlblk *sdata)
{
  LEOCmd	cmdBlock;
  idInfo	*idData;
  u32		i;
  
  idData = (idInfo *)OS_DCACHE_ROUNDUP_ADDR((void *)dummybuf2);
  
  /*
   * Try reading the ID area. If succeeded, simply return.
   */
  readIdArea(idData);
  
  if (sdata->cmdFlags & SPECD_ID_FILE)
    for(i = 0; i < SECPERBLOCK; i++)
    {
      bcopy((void *)&sdata->diskID,
	    (void *)&idData[i].diskID, sizeof(LEODiskID));
      bzero((void *)&idData[i].diskmanID, 200);
    }
  else
    for(i = 0; i < SECPERBLOCK; i++)
    {
      bcopy((void *)&sdata->diskID.serialNumber,
	    (void *)&idData[i].diskID.serialNumber, sizeof(LEOSerialNum));
      bzero((void *)&idData[i].diskmanID, 200);
    }
  
  for (i = 0; i < 2; i++)
    do{
      LeoSysReadWrite(&cmdBlock, OS_WRITE,
		      idLBAs[i], (void *)idData, 1, 
		      &diskQ);
      osRecvMesg(&diskQ, NULL, OS_MESG_BLOCK);

      if(GET_ERROR(cmdBlock) == 0)
	break;
      if (error_handle(GET_ERROR(cmdBlock)) == FATAL)
      {
	osSyncPrintf("Fatal error\n");
	for(;;);
      }
    } while(1);
  
  return;

} /* setID() */