calcncc.c 10.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
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <fcntl.h>

typedef unsigned char ubyte;

#ifdef MSDOS /* { */

typedef	unsigned long ulong;

#include "leotype.h"

int lba2Byte(int type, int startLBA, int nlbas)
{
  int result = 0;
  
  nlbas += startLBA;
  while(startLBA < nlbas)
    result += tab[type][startLBA++];

  return result;
}

/* Prototypes */
static void 	rev(long *data);

void rev(data)
long *data;
{
	ubyte c0,c1;
	union {
		long ld;
		ubyte ch[4];
	} d;

	d.ld=*data;
	c0=d.ch[0];		 c1=d.ch[1];
	d.ch[0]=d.ch[3]; d.ch[1]=d.ch[2];
	d.ch[3]=c0;		 d.ch[2]=c1;
	*data=d.ld;
}

#else /* }{ */

#define rev(x)

#ifdef __sgi__
# include <bstring.h>
#endif
# include <sys/types.h>
# include <unistd.h>

#endif /* } */

/*
 * Types
 */
typedef struct
{
  ubyte		*addr;
  int		infd;
  int		start;
  int		count;

} stream_s;

/* typedef ulong		unsigned long; */

/*
 * Definitions
 */
#define VERSION		"1.11"
#define LAW		65521
#define MASK1		0xb29ac743
#define C_CODE_OFS	7

#define	COUNTCODE_OFS	0
#define	DISKTYPE_OFS	5
#define	ROMEND_OFS	224
#define	RAMSTART_OFS	226
#define	RAMEND_OFS	228
#define	DISTCODE_OFS	256 + 232
#define	DEVID_OFS	256 + 8
#define	DEVID_LEN	16


/*
 * Globals
 */
ubyte   *command_name;
ubyte	*masterFileName;
ubyte	fileBuffer[20 * 1024];	/* must be more than 8K */
long	diskType;
long	romEndLBA;
long	ramStartLBA;
long	ramEndLBA;
unsigned long	romSize;
unsigned long	ramSize;

unsigned countryCode[]={
0x12777543,
0x94325546,
0xbf87e366,
0x55a94ced,
0x94325546,
0xbf87e366,
0x55a94ced,
(0xe848d316 ^ MASK1)+0x1000,
(0x2263ee56 ^ MASK1)+0x2000,
0};

/*
 * Prototypes
 */
static void 	usage(void);
static void 	printVersion(void);
static void 	help(void);
static ulong 	calcSystemNIdNcc(int ifd);
static ulong 	calcRomNcc(int ifd);
static ulong	calcRamNcc(int ifd);
static long 	getHalfWordFromFile(void *st);
static long 	getHalfWordFromRam(void *st);
static ulong 	calculate_ncc(long (*getter)(void *), void *src, int addr);
static void	checkMaster(int ifd);

extern int  	lba2Byte(int type, int startLBA, int nlbas);

static void printVersion(void)
{
  fprintf(stderr, "NCC calculator for 64DD ver.%s\n", VERSION);
  fprintf(stderr, "\n");
  
  return;
  
} /* printVersion() */


static void usage(void)
{
  fprintf(stderr, "usage: %s [masterData]\n", command_name);
  fprintf(stderr, "     calculate NCC (Nintendo Check Code) of master data\n");

  return;

} /* usage() */


static void help(void)
{
  usage();
  
  fprintf(stderr, "\n   If [masterData] is ommitted, master.d64 will be used\n");
  
  return;
  
} /* help() */


int main(int argc, ubyte **argv)
{
  ubyte          *p;
  int		ifd;
  ulong		romNcc, ramNcc;
#ifdef MSDOS /* { */
    extern int _kmc_gcc_mode;

    _kmc_gcc_mode=0;
#endif /* } */
  if ((command_name = rindex(argv[0], '/')) != NULL)
    command_name++;
  else
    command_name = argv[0];
    
  /*
   * Parse arguments
   */
  while( --argc > 0 )
  {

    if ( **(++argv) == '-' )
    {

      p = *argv;
      while ( *++p )
      {
	switch (*p)
	{
	case 'h':
	  printVersion();
	  
	  help();
	  exit(0);
	  break;
	  
	default:
	  printVersion();
	  
	  usage();
	  exit(1);
	  
	}
	
      } /* while ( *++p != 0 ) */
      
    } /* if ( **(++argv) == '-' ) */
    else
      break;

  } /* while( argc > 0 ) */
       
  if (argc == 0)
  {
    masterFileName = "master.d64";
  }
  else if (argc == 1)
  {
    masterFileName = *(argv++);
  }
  else 
  {
    printVersion();
    
    usage();
    exit(1);
  }
  
  printVersion();
  
  fprintf(stderr, "master file name = %s\n", masterFileName);

  if ( -1 == (ifd = open( masterFileName,O_RDONLY)) )
  {
    fprintf(stderr, "%s: can't open input file\n", command_name);
    exit(1);
  }
  
  /*
   * 本当にマスターデータ?
   */
  checkMaster(ifd);

  lseek(ifd, 0, 0);
  
  /*
   * Calculate NCC
   */
  romNcc = calcSystemNIdNcc(ifd);

  romNcc += calcRomNcc(ifd);
  romNcc %= 1000000;
  
  if(ramStartLBA != 0xffff)
    ramNcc = calcRamNcc(ifd);

#ifdef _DEBUG
  if (1 != read(ifd, fileBuffer, 1))
  {
    printf("O.K., we can't read the file any more\n");
  }
#endif  

  /*
   * Print out the result
   */
  printf("rom NCC is %03d-%03d\n", romNcc/1000, romNcc % 1000);

  if(ramStartLBA != 0xffff)
    printf("ram NCC is %03d-%03d\n", ramNcc/1000, ramNcc % 1000);
  else
    printf("ram area is not used\n");
    

  if ( EOF == close(ifd) )
  {
    fprintf(stderr, "%s: can't close output file\n", command_name);
    exit(1);
  }
  
  exit(0);
  
  /* NOTREACHED */
  
} /* main() */


ulong calcSystemNIdNcc(int ifd)
{
  ubyte		distCode;
  long		countCode;
  ulong		ncc;
  stream_s	stream;
  int		i;

  /*
   * First, read in system and Id part
   */
  if (0x0200 != read(ifd, fileBuffer, 0x0200))
  {
    printf("\n******** Read ERROR *********\n");
    exit(-1);
  }
  
  /*
   * Get diskType, romEndLBA, ramStartLBA and ramStartLBA
   */
  diskType = (long)( fileBuffer[DISKTYPE_OFS] & 0x0f );

  romEndLBA = (long)( (fileBuffer[ROMEND_OFS] << 8) |
		     fileBuffer[ROMEND_OFS + 1] );
  ramStartLBA = (long)( (fileBuffer[RAMSTART_OFS] << 8) |
		       fileBuffer[RAMSTART_OFS + 1] );
  ramEndLBA = (long)( (fileBuffer[RAMEND_OFS] << 8) |
		     fileBuffer[RAMEND_OFS + 1] );

  /*
   * ここで、ramsize を計算してはいけない!
   * ram を使用していない場合、不具合が発生していた
   */

#ifdef _DEBUG
  printf("diskType = %d, rom end = %d, ram start = %d, ram end = %d\n",
	 diskType, romEndLBA, ramStartLBA, ramEndLBA);
#endif

  /*
   * Convert Dist code to Country code 
   */
  distCode = fileBuffer[DISTCODE_OFS];

  if (distCode > 0x11) 
  {
    printf("\n***** File format error *****\n");
    exit(-1);
  }

  countCode = ( countryCode[distCode + C_CODE_OFS] -
	       0x1000*(distCode + 1) ) ^ MASK1;

  rev(&countCode);
  bcopy((void *)&countCode, (void *)&(fileBuffer[COUNTCODE_OFS]), 4);
  rev(&countCode);
  
#ifdef _DEBUG
  printf("countryCode = 0x%08x\n", countCode);
#endif

  /*
   * Fill 0 into Development Id
   */
  for (i = 0; i < DEVID_LEN; i++)
    fileBuffer[DEVID_OFS + i] = 0;
  
  /*
   * Calculate NCC
   */
  /*
   * getHalfWordFromRam を指定するときは、infd には何も代入しなくてよい
   */
  stream.addr = (ubyte *)fileBuffer;
  stream.count = 0xe8;
  
  ncc = calculate_ncc(getHalfWordFromRam, (void *)&stream, 0);
  
  stream.addr = (ubyte *)fileBuffer + 0x100;
  stream.count = 0xe8;
  
  /*
   * NCC の計算
   * 2 で割るのは、要素数としてハーフワードの個数で考えるため
   */
  ncc += calculate_ncc(getHalfWordFromRam, (void *)&stream, 0xe8/2);

  return ncc;
   
} /* calcSystemNIdNcc() */


ulong calcRomNcc(int ifd)
{
  stream_s	stream;
  
  /*
   * romsize はここで計算すること
   */
  romSize = lba2Byte(diskType, 0, romEndLBA + 1);

#ifdef _DEBUG
  printf("rom size = %d\n", romSize);
#endif

  stream.infd = ifd;
  stream.start = 0x200;
  stream.count = romSize;
  
  /*
   * NCC の計算
   * 2 で割るのは、要素数としてハーフワードの個数で考えるため
   */
  return calculate_ncc(getHalfWordFromFile, (void *)&stream, 0x1d0/2);

} /* calcRomNcc() */


/*
 * ram 領域の NCC の計算。ファイルは ram の先頭を指していなければいけない。
 */
ulong calcRamNcc(int ifd)
{
  stream_s	stream;
  
  /*
   * ramsize はここで計算すること
   */
  ramSize = lba2Byte(diskType, ramStartLBA, ramEndLBA - ramStartLBA + 1);
  
#ifdef _DEBUG
  printf("ram size = %d\n", ramSize);
#endif

  stream.infd = ifd;
  stream.start = 0x200 + romSize;
  stream.count = ramSize;
  
  return calculate_ncc(getHalfWordFromFile, (void *)&stream, 0);

} /* calcRomNcc() */



/*
 * get half word from the buffer
 */
long getHalfWordFromRam(void *st)
{
  int		*pCount;
  ubyte		**pAddr;
  long		data;
  
  pAddr = &(((stream_s *)st)->addr);
  pCount = &(((stream_s *)st)->count);
  
  if (*pCount == 0)
    return -1;
  
  if (*pCount < 0)
  {
    printf("\n***** File format error *****\n");
    exit(-1);
  }
  
  data = ( (**pAddr) << 8) | ( *(*pAddr + 1) );
  *pAddr += 2;
  *pCount -= 2;
  
#ifdef _DEBUG
  printf("data = 0x%04x\n", data);
#endif

  return data;

} /* getHalfWordFromRam() */




/*
 * get half word from the specified stream
 */
long getHalfWordFromFile(void *st)
{
  int		fd, *pCount, *pStart;
  static int	remainInBuffer = 0;
  static ubyte	*pointer;
  long		data;

  fd = ((stream_s *)st)->infd;
  pStart = &(((stream_s *)st)->start);
  pCount = &(((stream_s *)st)->count);
  
  if (*pCount == 0)
    return -1;

  if (*pCount < 0)
  {
    printf("\n***** File format error *****\n");
    exit(-1);
  }
  
  if (remainInBuffer == 0)
  {

    /*
     * バッファにある程度読み込んでおく
     * *pStart が -1 ならシークしない
     */
    if (*pStart != -1)
      lseek(fd, *pStart, 0);

    if ( *pCount > sizeof(fileBuffer) )
    {
      remainInBuffer = sizeof(fileBuffer);
    }
    else
    {
      remainInBuffer = *pCount;
    }
    
    if (remainInBuffer != read(fd, fileBuffer, remainInBuffer))
    {
      printf("\n******** Read ERROR **********\n");
      exit(-1);
    }

    /*
     * st の start が次の read 場所を指すようにしておく
     */
    if (*pStart != -1)
      *pStart += remainInBuffer;
    pointer = fileBuffer;
    
  } /* if (remainInBuffer == 0) */
  
  data = ((*pointer) << 8) | ( *(pointer + 1) );
  pointer += 2;
  remainInBuffer -= 2;
  
  *pCount -= 2;
  
  return data;

} /* getHalfWordFromFile() */

void checkMaster(int ifd)
{
  char      tmpbuf[10];
  
  /*
   * 本当にマスターデータかチェックする
   */
  lseek(ifd, 0, 0);
  
  /*
   * 頭の国コードは 0 のはず
   */
  if (4 != read(ifd, tmpbuf, 4))
  {
    printf("\n******** Read ERROR **********\n");
    exit(-1);
  }
  
  if ( ((int *)tmpbuf)[0] != 0 )
  {
    printf("\n******** This is not a master data!! **********\n");
    exit(-1);
  }
  
  /*
   * 8 バイト目からはディフェクトデータなので、0 のはず
   */
  lseek(ifd, 8, 0);
  if (4 != read(ifd, tmpbuf, 4))
  {
    printf("\n******** Read ERROR **********\n");
    exit(-1);
  }
  
  if ( ((int *)tmpbuf)[0] != 0 )
  {
    printf("\n******** This is not a master data!! **********\n");
    exit(-1);
  }
  
  /*
   * 28 バイト目からはロードアドレスなので、0 でないはず
   */
  lseek(ifd, 28, 0);
  if (4 != read(ifd, tmpbuf, 4))
  {
    printf("\n******** Read ERROR **********\n");
    exit(-1);
  }
  
  if ( ((int *)tmpbuf)[0] == 0 )
  {
    printf("\n******** This is not a master data!! **********\n");
    exit(-1);
  }
  

} /* checkMaster() */


ulong calculate_ncc(long (*getter)(void *), void *src, int addr)
{
  ulong		ncc = 0;
  long		a;
  
  while (1)
  {
    while (addr < LAW)
    {
      if ((a = getter(src)) == -1)
	goto quit;
      ncc += (ulong)a ^ (ulong)addr++;
    }
    addr = 0;
  }
  
 quit:
  return (ncc % 1000000);

} /* calculate_ncc() */