rdp_dl.c 22.9 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 760 761 762 763 764 765 766 767 768 769
/*
 * rdp_dl.c - rdp display list module
 */
#include <sys/types.h>
#ifdef __sgi__
#include <sys/sbd.h>
#endif
#include <sys/stat.h>
#include <sys/mman.h>
#ifdef __sgi__
#include <sys/sema.h>
#endif
#include <netinet/in.h>

#include <errno.h>
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <getopt.h>
#include <rcp.h>
#include <verify.h>
#include <gbi.h>
#include <wait.h>

/*
 * From $ROOT/usr/include/ide, which is installed from $ROOT/PR/diags/include
 */
#include "diag.h"
#include "dbg_comm.h"

/*
 * Constants
 */
#define RDP_DL_TEST_BASE	10
#define START_REG_MSK           0x00ffffff
#define END_REG_MSK             0x00ffffff

#define HIDDEN_FACTOR           8
#define MAIN_MEMORY_SIZE        0x200000
#define HIDDEN_MEMORY_SIZE      (MAIN_MEMORY_SIZE/HIDDEN_FACTOR)
#define TOTAL_MEMORY_SIZE       (MAIN_MEMORY_SIZE + HIDDEN_MEMORY_SIZE)

/*
 * Local function prototypes
 */
static int rdp_dlInit(void);
static int rdp_dlDo(TEST_REF *test_ref);

static int rdp_dl(void);

static int WriteMem(unsigned int addr, unsigned int nbytes, char *buffer);
static int ReadMem(unsigned int addr, unsigned int nbytes, char *buffer);


/*
 * Global Variables
 */
static char rdramFileName[1024];
static int  fromDmem = FALSE;
static int  verifyMode = FALSE;


/*
 * Create an array of tests, each of which corresponds to a separate menu
 * item callable from the master ide menu.
 */
static TEST_REF TestRefs[] = {
    {"RDP Display List", RDP_DL_TEST_BASE+0, rdp_dl},
    {"END OF TESTS",0,0}
};

static int NumFailures = 0;


/*
 * diagnostic entry point:
 *
 * Each separately invokable ide diagnostic command corresponds to an
 * independent ".c" module; the entry point herein must match
 * the test name as specified in the rdpcmd.awk script.  These command
 * names correspond to the names you see from the ide menu.  For this
 * module, there will be an ide command "rdp_dl".
 */
int rdp_dlEntry()
{
    int c;
    int testNum;

    int prev_commtype;

    /* run monitor out of imem */
    prev_commtype = commtype;
    commtype = DG_TINYMON_IMEM;

    /*
     * parse command line args received from ide
     */
    while ((c = getopt(pGlobalComm->argc, pGlobalComm->argv, "htn:dv")) != EOF) 
    {
	switch(c) 
     	{
	    case 'h':
		/*
		 * Print out the available subtests, then return without
		 * invoking any subtests.
		 */
		dgListSubTests(TestRefs);
	 	commtype = prev_commtype;
		return(0);
		break;
	    case 't':
                testNum = atoi(optarg);
                pGlobalComm->entryNum = -(testNum);
                break;
	    case 'n':
		strcpy(rdramFileName, optarg);
		break;
	    case 'd':
                fromDmem = TRUE;
                break;
	    case 'v':
                verifyMode = TRUE;
                break;
	    default:
		printf("weird option character %c = 0x%x\n", c, c);
		break;
        }
    }

    /*
     * IDE will call our one-time initialization function rdp_dlInit(),
     * then invoke rdp_dlDo() for as many tests as we've put into the
     * global test array "TestRefs", declared at the top of this module.
     */
    diaginit(TestRefs, rdp_dlInit, rdp_dlDo);

    commtype = prev_commtype;

    if (NumFailures) {
        errlog(INFO_END, "... test %s FAILED, %d failures.",
            ideTestName, NumFailures);
    } else {
	errlog(INFO_END, "... test %s PASSED", ideTestName);
    }
}

static int rdp_dlInit()
{

    errlog(INFO_START, "Starting test %s ... ", ideTestName);
    NumFailures = 0;

    /*
     * Setup the diagnostic communication link to the target system.
     *
     * (this uses the global ide variable "commtype", which by default
     * is set to DG_VERILOG.)
     */
    dgInitComm();

    dgInitRdram();

    return(0);
}

static int rdp_dlDo(TEST_REF *test_ref)
{
    int rc;

    errlog(INFO_START, 
      "%s: starting subtest %s (%d) ...",
      ideTestName, test_ref->name, test_ref->num);

    /*
     * Invoke the actual test from the "TEST_REF" array statically declared
     * as a global within this test module.
     */
    if (rc = test_ref->fnc()) NumFailures++;

    if (rc == 0) {
	errlog(INFO_END, "... PASSED");
    } else { 
	errlog(INFO_END, "... FAILED");
    }
    return(NumFailures);
}

int rdp_dl(void)
{
   int errcount = 0;

   unsigned read_data;
   unsigned pixelSize;
   VerifyInfo verif;
   char *displayList, *staticSegment, *frameBuffer, *zBuffer, 
        *displayListCmp, *staticSegmentCmp, *cvgBuffer, *zhBuffer;
   FILE *fp;
   char fullFileName[1024];
   char fullFileNameZ[1024];
   char InDataPath[1024];
   char archivePath[1024];
   char sysCommand[1024];
   char version_name[1024];
   struct stat statbuf;
   unsigned cvgSize, i;
   unsigned zhSize;
   unsigned nrdrams;
   unsigned hiddenAddr;
   unsigned status;
   unsigned RCP_VERSION;

#ifdef STEVE
   u32	tob, bob, curr, outp;
   u32	thisSize, cmd, *cmd_words;
   char	*datap;
   int	totDLsize;
   unsigned int curr_reg, end_reg;
#endif

   /* find /InData and /archive */
   if (!dgFindFile(InDataPath, "./InData", "\0"))
   {
     if (!dgFindFile(InDataPath, "/PR/diags/rdp/InData", "\0"))
     {
        errlog(ERR_SEVERE, "Unable to find /InData\n");
        return(1);
     }
   }

   if (!dgFindFile(archivePath, "./archive", "\0"))
   {
     if (!dgFindFile(archivePath, "/PR/diags/rdp/archive", "\0"))
     {
        errlog(ERR_SEVERE, "Unable to find /archive\n");
        return(1);
     }
   }

   sprintf(fullFileName, "%s%s%s", InDataPath, rdramFileName, ".rdram");
   sprintf(fullFileNameZ, "%s%s", fullFileName, ".Z");

   /* uncompress input rdram file and rdramgclr it */
   sprintf(sysCommand, "zcat %s > %s", fullFileNameZ, fullFileName);
   errlog(DEBUG, "%s", sysCommand);
   status = system(sysCommand); 
   if (!((status != -1) && WIFEXITED(status) && (WEXITSTATUS(status) == 0)))
   {
      errlog(ERR_SEVERE, "Unable to uncompress input rdram file\n");
      return(1);
   }

   /* rdramgclr */
   sprintf(sysCommand, "$ROOT/usr/sbin/rdramgclr -q %s%s", InDataPath, rdramFileName);
   errlog(DEBUG, "%s", sysCommand);
   status = system(sysCommand); 
   if (!((status != -1) && WIFEXITED(status) && (WEXITSTATUS(status) == 0)))
   {
      errlog(ERR_SEVERE, "rdramgclr failed\n");
      return(1);
   }

   /* make writeable */
   sprintf(sysCommand, "chmod 777 %s", fullFileName);
   errlog(DEBUG, "%s", sysCommand);
   status = system(sysCommand); 
   if (!((status != -1) && WIFEXITED(status) && (WEXITSTATUS(status) == 0)))
   {
      errlog(ERR_SEVERE, ".rdram not writeable\n");
      return(1);
   }

   /* open rdram file */
   stat(fullFileName, &statbuf);
   fp = fopen(fullFileName, "r+b");
   if (fp == NULL)
   {
      errlog(ERR_SEVERE, "Unable to open rdram file: %s\n", fullFileName);
      return(1);
   }

   /* extract the verify_info structure from .rdram file */
   fseek(fp, VERIFY_INFO_PHYSADDR, SEEK_SET);
   fread(&verif, sizeof(VerifyInfo), 1, fp);

   errlog(DEBUG, "%llx %x %x %x %x %x %x %x %x", verif.rdpListSize, verif.rdpListAddr, verif.staticSegSize, verif.staticSegAddr, verif.frameBuffer0Addr, verif.frameBufferSize, verif.width, verif.height, verif.zBufferAddr);

   /* extract displayList and staticSegment data from .rdram file */
   displayList = calloc((unsigned long) verif.rdpListSize, sizeof(char));
   fseek(fp, verif.rdpListAddr, SEEK_SET);
   fread(displayList, sizeof(char), (unsigned long) verif.rdpListSize, fp);

   staticSegment = calloc(verif.staticSegSize, sizeof(char));
   fseek(fp, verif.staticSegAddr, SEEK_SET);
   fread(staticSegment, sizeof(char), verif.staticSegSize, fp);

   /* halt RSP */
   errcount += dgTestReg(SP_STATUS_REG, SP_SET_HALT, SP_STATUS_HALT);

   /* if fromDmem flag is set, load DL into DMEM instead of RDRAM */
   if (fromDmem == TRUE)
   {
#ifndef STEVE
      if (verif.rdpListSize <= (SP_DMEM_END - SP_DMEM_START + 1))
      {
	 errcount += dgWriteWord(DPC_STATUS_REG, DPC_SET_XBUS_DMEM_DMA);
         errcount += dgRdTestWordMsk(DPC_STATUS_REG, DPC_STATUS_XBUS_DMEM_DMA, DPC_STATUS_XBUS_DMEM_DMA);
         verif.rdpListAddr = SP_DMEM_START;
      }
      else
         errlog(INFO, "Display List too large for DMEM ... Running out of RDRAM");
#endif
#ifdef STEVE
	 errcount += dgWriteWord(DPC_STATUS_REG, DPC_SET_XBUS_DMEM_DMA);
         errcount += dgRdTestWordMsk(DPC_STATUS_REG, DPC_STATUS_XBUS_DMEM_DMA, DPC_STATUS_XBUS_DMEM_DMA);
#endif
   }

   /* load display list from .rdram file */
   errlog(DEBUG, "Writing display list into dmem/rdram");
#ifdef STEVE
   if (!fromDmem)
#endif
       errcount += WriteMem(verif.rdpListAddr, (unsigned long) verif.rdpListSize, displayList);

   if (verifyMode)
   {
      /* read display list back and compare */
      displayListCmp = calloc((unsigned long) verif.rdpListSize, sizeof(char));
      errlog(DEBUG, "Reading display list from dmem/rdram");
      errcount += ReadMem(verif.rdpListAddr, (unsigned long) verif.rdpListSize, displayListCmp);
      if (memcmp(displayList, displayListCmp, (unsigned long) verif.rdpListSize))
      {
         errlog(ERR_SEVERE, "Write of DL to RDRAM Failed\n");
         return(1);
      }
      free(displayListCmp);
   }

   /* load static segment from .rdram file into rdram */
   errlog(DEBUG, "Writing static segment into rdram");
   errcount += WriteMem(verif.staticSegAddr, verif.staticSegSize, staticSegment);

   if (verifyMode)
   {
      /* read static segment back and compare */
      staticSegmentCmp = calloc(verif.staticSegSize, sizeof(char));
      errlog(DEBUG, "Reading static segment from rdram");
      errcount += ReadMem(verif.staticSegAddr, verif.staticSegSize, staticSegmentCmp);
      if (memcmp(staticSegment, staticSegmentCmp, verif.staticSegSize))
      {
         errlog(ERR_SEVERE, "Write of Static Segment to RDRAM Failed\n");
         return(1);
      }
      free(staticSegmentCmp);
   }

#ifdef STEVE
   if (fromDmem) {
       /* push through small FIFO: */
       tob = SP_DMEM_START;
       bob = SP_DMEM_START + 512;	/* 1K FIFO */
       totDLsize = verif.rdpListSize;
       datap = (char *) &(displayList[0]);

       /* init start and end pointers */
       outp = tob;
       errcount += dgTestRegMsk(DPC_START_REG, 
				outp,
				outp,
				START_REG_MSK);
       errcount += dgTestRegMsk(DPC_END_REG, 
				outp,
				outp,
				END_REG_MSK);

       fprintf(stderr,"tob = %08x bob = %08x totDLsize = %08x\n",
	       tob, bob, totDLsize);
       
       while (totDLsize > 0) {

	   cmd_words = (u32 *) datap;
	   cmd = (char) ((cmd_words[0] & 0xff000000) >> 24);

	   if (cmd > (char) -27 || cmd == 0xc0) {	/* one word */
	       thisSize = 1;
	   } else {
	       switch (cmd) {
		 case (char) G_TEXRECTFLIP:
		 case (char) G_TEXRECT:
		   thisSize = 2;
		   break;
		 case (char) G_TRI_FILL:
		   thisSize = 4;
		   break;
		 case (char) G_TRI_SHADE:
		   thisSize = 4 + 8;
		   break;
		 case (char) G_TRI_TXTR:
		   thisSize = 4 + 8;
		   break;
		 case (char) G_TRI_SHADE_TXTR:
		   thisSize = 4 + 8 + 8;
		   break;
		 case (char) G_TRI_FILL_ZBUFF:
		   thisSize = 4 + 2;
		   break;
		 case (char) G_TRI_SHADE_ZBUFF:
		   thisSize = 4 + 8 + 2;
		   break;
		 case (char) G_TRI_TXTR_ZBUFF:
		   thisSize = 4 + 8+ 2;
		   break;
		 case (char) G_TRI_SHADE_TXTR_ZBUFF:
		   thisSize = 4 + 8 + 8 + 2;
		   break;
		 default:
		   fprintf(stderr,"bad cmd [%08x %08x]\n",
			   cmd_words[0], cmd_words[1]);
		   break;
	       }
	   }

	   fprintf(stderr,"DL : %08x %08x ",cmd_words[0], cmd_words[1]);
	   if (thisSize == 1) {
	       fprintf(stderr,"\n");
	   } else {
	       fprintf(stderr,"... (%d words long)\n",thisSize);
	   }

	   cmd = (char) ((cmd_words[0] & 0xff000000) >> 24);

	   if ((outp + (thisSize*sizeof(u64))) > bob) {	/* wrap */
	       fprintf(stderr,"wrap!\n");
	       /* read current */
	       dgReadWord(DPC_CURRENT_REG, &curr_reg);
	       curr = curr_reg;

	       /* wait for (current <= outp) */
	       while (curr > (outp & 0xffffff)) {
		   fprintf(stderr,"curr : %08x outp : %08x\n",curr,outp);
		   dgReadWord(DPC_CURRENT_REG, &curr_reg);
		   curr = curr_reg;
	       }

#if 0
	       /* wait for (current > tob) */
	       while (curr != tob) {
		   fprintf(stderr,"curr : %08x tob : %08x\n",curr,tob);
		   dgReadWord(DPC_CURRENT_REG, &curr_reg);
		   curr = curr_reg;
	       }
#endif

#if 0
	       dgReadWord(DPC_CURRENT_REG, &curr_reg);
	       dgReadWord(DPC_END_REG, &end_reg);
	       while ((curr_reg & 0xffffff) != (end_reg & 0xffffff)) {
		   dgReadWord(DPC_CURRENT_REG, &curr_reg);
		   dgReadWord(DPC_END_REG, &end_reg);
	       }
#endif

	       outp = tob;

	       /* write tob to start pointer */
	       errcount += dgTestRegMsk(DPC_START_REG, outp, outp, 
					START_REG_MSK);
	   }

	   /* wait for space; (outp + thisSize*sizeof(u64)) < current */
	   dgReadWord(DPC_CURRENT_REG, &curr_reg);
	   curr = curr_reg;
	   while ((curr > (outp & 0xffffff)) && 
		  (curr < ((outp & 0xffffff) + thisSize*sizeof(u64)))) {
	       dgReadWord(DPC_CURRENT_REG, &curr_reg);
	       curr = curr_reg;
	   }

	   /* copy into buffer */
	   errcount += WriteMem(outp, thisSize*sizeof(u64), datap);
	   totDLsize -= (thisSize * sizeof(u64));
	   datap += (thisSize * sizeof(u64));
	   outp += thisSize * sizeof(u64);

	   /* update end pointer */
	   errcount += dgTestRegMsk(DPC_END_REG, outp, outp, END_REG_MSK);

	   /* done */
       }

       fprintf(stderr,"done with DMEM -> XBUS!\n");
   } else {
#endif
       /* write start/end pointers */
       errcount += dgTestRegMsk(DPC_START_REG, 
				verif.rdpListAddr, 
				verif.rdpListAddr, 
				START_REG_MSK);
       errcount += dgTestRegMsk(DPC_END_REG, 
				verif.rdpListAddr + (unsigned long) verif.rdpListSize, 
				verif.rdpListAddr + (unsigned long) verif.rdpListSize, 
				END_REG_MSK);
#ifdef STEVE
   }

   if (!fromDmem) {
#endif
       /* wait until DMA counter is at end */
       errlog(DEBUG, "Waiting for current == end pointer");
       while (!(errcount += dgReadWord(DPC_CURRENT_REG, &read_data)))
	   {
	       if ((read_data & 0x00ffffff) == ((verif.rdpListAddr + verif.rdpListSize) & 0x00ffffff)) break;

	   }
#ifdef STEVE
   }
#endif

   /* wait for full sync */
   errlog(DEBUG, "Waiting for full sync");
   while (!(errcount += dgReadWord(DPC_STATUS_REG, &read_data)))
   {
     if (!(read_data & DPC_STATUS_PIPE_BUSY)) break;
   }

   /* display frame buffer */
   dgEnableVideo(verif.frameBuffer0Addr, 0);
 
   if (verifyMode)
   {
      /* read frame buffer from rdram into .rdram file */
      pixelSize = (verif.frameBufferSize == G_IM_SIZ_16b) ? G_IM_SIZ_16b_BYTES : G_IM_SIZ_32b_BYTES;

      frameBuffer = calloc(verif.width * verif.height * pixelSize, sizeof(char));
      errlog(DEBUG, "Reading frame buffer from rdram");
      errcount += ReadMem(verif.frameBuffer0Addr, verif.width * verif.height * pixelSize, frameBuffer);
      fseek(fp, verif.frameBuffer0Addr, SEEK_SET);
      fwrite(frameBuffer, sizeof(char), verif.width * verif.height * pixelSize, fp);

      /* read Z buffer from rdram into .rdram file */
      zBuffer = calloc(verif.width * verif.height * G_IM_SIZ_16b_BYTES, sizeof(char));
      errlog(DEBUG, "Reading Z buffer from rdram");
      errcount += ReadMem(verif.zBufferAddr, verif.width * verif.height * G_IM_SIZ_16b_BYTES, zBuffer);
      fseek(fp, verif.zBufferAddr, SEEK_SET);
      fwrite(zBuffer, sizeof(char), verif.width * verif.height * G_IM_SIZ_16b_BYTES, fp);

      /* if not running tinymon out of rdram, read hidden bits from rdram into .rdram file */
      if (commtype != DG_TINYMON_RDRAM)
      {
         /* set ebus test mode in MI mode register */
         errcount += dgWriteWord(MI_MODE_REG, MI_SET_EBUS);
         errcount += dgRdTestWordMsk(MI_MODE_REG, MI_MODE_EBUS, MI_MODE_EBUS);
         
   
         /* extract hidden coverage bits if cfb is 16-bit RGBA */
         if (verif.frameBufferSize == G_IM_SIZ_16b)
         {
   	    errlog(DEBUG, "Reading hidden coverage bits from rdram");
            errcount += ReadMem(verif.frameBuffer0Addr, verif.width * verif.height * pixelSize, frameBuffer);
   
	    /* extract 4 lsb's of each 4-byte word (these are the hidden bits) */
   	    cvgSize = (verif.width * verif.height * pixelSize) / HIDDEN_FACTOR;
            cvgBuffer = calloc(cvgSize, sizeof(char));
	    
            for (i=0; i < cvgSize; i++)
     	    {
              cvgBuffer[i] = (frameBuffer[(i * 8) + 3] << 4) | (frameBuffer[(i * 8) + 7] & 0xf);
	    }
   
            /* write cvgBuffer into .rdram file */
 	    nrdrams = (statbuf.st_size > TOTAL_MEMORY_SIZE) ? 2 : 1;
            hiddenAddr = (MAIN_MEMORY_SIZE * nrdrams);
            hiddenAddr += (verif.frameBuffer0Addr / HIDDEN_FACTOR);
   
            fseek(fp, hiddenAddr, SEEK_SET);
            fwrite(cvgBuffer, sizeof(char), cvgSize, fp);
            free(cvgBuffer);
         }
   
   
         /* extract hidden z bits from RDRAM */
         errlog(DEBUG, "Reading hidden Z bits from rdram");
         errcount += ReadMem(verif.zBufferAddr, verif.width * verif.height * G_IM_SIZ_16b_BYTES, zBuffer);
   
         /* extract 4 lsb's of each 4-byte word (these are the hidden bits) */
         zhSize = (verif.width * verif.height * G_IM_SIZ_16b_BYTES) / HIDDEN_FACTOR;
         zhBuffer = calloc(zhSize, sizeof(char));
          
         for (i=0; i < zhSize; i++)
         {
           zhBuffer[i] = (zBuffer[(i * 8) + 3] << 4) | (zBuffer[(i * 8) + 7] & 0xf);
         }
   
         /* write zhBuffer into .rdram file */
         nrdrams = (statbuf.st_size > TOTAL_MEMORY_SIZE) ? 2 : 1;
         hiddenAddr = (MAIN_MEMORY_SIZE * nrdrams);
         hiddenAddr += (verif.zBufferAddr / HIDDEN_FACTOR);
   
         fseek(fp, hiddenAddr, SEEK_SET);
         fwrite(zhBuffer, sizeof(char), zhSize, fp);
         free(zhBuffer);
   
   
         /* clear ebus test mode in MI mode register */
         errcount += dgWriteWord(MI_MODE_REG, MI_CLR_EBUS);
         errcount += dgRdTestWordMsk(MI_MODE_REG, 0x0, MI_MODE_EBUS);
      }
      else
         errlog(INFO, "WARNING: Unable to read back hidden bits. (Executing out of RDRAM)");

      free(frameBuffer);
      free(zBuffer);
   }

   /* if fromDmem flag is set, point DPC_STATUS_REG back at RDRAM */
   if (fromDmem == TRUE)
   {
      if (verif.rdpListSize <= (SP_DMEM_END - SP_DMEM_START + 1))
      {
         errcount += dgWriteWord(DPC_STATUS_REG, DPC_CLR_XBUS_DMEM_DMA);
         errcount += dgRdTestWordMsk(DPC_STATUS_REG, 0x0, DPC_STATUS_XBUS_DMEM_DMA);
      }
   }

   fclose(fp);
   free(displayList);
   free(staticSegment);
   fromDmem = FALSE;


   if (verifyMode)
   {
     verifyMode = FALSE;

     /* rdram2image */
     sprintf(sysCommand, "$ROOT/usr/sbin/rdram2image -q %s%s", InDataPath, rdramFileName);
     errlog(DEBUG, "%s", sysCommand);
     status = system(sysCommand);
     if (!((status != -1) && WIFEXITED(status) && (WEXITSTATUS(status) == 0)))
     {
        errlog(ERR_SEVERE, "Unable to convert .rdram to image files\n");
        return(1);
     }

     /* backend filter */
     sprintf(sysCommand, "$ROOT/usr/sbin/filter -c OutData/%s_0.cov -i OutData/%s_0.cvg -n OutData/%s_0.rgb", rdramFileName, rdramFileName, rdramFileName);
     errlog(DEBUG, "%s", sysCommand);
     status = system(sysCommand);
     if (!((status != -1) && WIFEXITED(status) && (WEXITSTATUS(status) == 0)))
     {
        errlog(ERR_SEVERE, "Unable to run backend filter\n");
        return(1);
     }

     /* iclr */
     sprintf(sysCommand, "$ROOT/usr/sbin/iclr OutData/%s_0.rgb", rdramFileName);
     errlog(DEBUG, "%s", sysCommand);
     status = system(sysCommand);
     if (!((status != -1) && WIFEXITED(status) && (WEXITSTATUS(status) == 0)))
     {
        errlog(ERR_SEVERE, "iclr .rgb failed\n");
        return(1);
     }

     sprintf(sysCommand, "$ROOT/usr/sbin/iclr OutData/%s_0.cov", rdramFileName);
     errlog(DEBUG, "%s", sysCommand);
     status = system(sysCommand);
     if (!((status != -1) && WIFEXITED(status) && (WEXITSTATUS(status) == 0)))
     {
        errlog(ERR_SEVERE, "iclr .cov failed\n");
        return(1);
     }

     sprintf(sysCommand, "$ROOT/usr/sbin/iclr OutData/%s_0.z", rdramFileName);
     errlog(DEBUG, "%s", sysCommand);
     status = system(sysCommand);
     if (!((status != -1) && WIFEXITED(status) && (WEXITSTATUS(status) == 0)))
     {
        errlog(ERR_SEVERE, "iclr .z failed\n");
        return(1);
     }

     /* determine version of RCP */
     if (dgReadWord(MI_VERSION_REG, &RCP_VERSION)) return (1);
     if (RCP_VERSION == 0x01010101) 
       strcpy(version_name, "rcp1");
     else
       strcpy(version_name, "rcp2");

     /* cmp */
     sprintf(sysCommand, "cmp -s OutData/%s_0.rgb %s%s_io_0.rgb.%s.archive", rdramFileName, archivePath, rdramFileName, version_name);
     errlog(DEBUG, "%s", sysCommand);
     status = system(sysCommand);
     if (!((status != -1) && WIFEXITED(status) && (WEXITSTATUS(status) == 0)))
     {
        sprintf(sysCommand, "$ROOT/usr/sbin/idf -d OutData/%s_0.rgb %s%s_io_0.rgb.%s.archive OutData/%s_idf.rgb", rdramFileName, archivePath, rdramFileName, version_name, rdramFileName);
        errlog(DEBUG, "%s", sysCommand);
        status = system(sysCommand);
        errlog(ERR_SEVERE, ".rgb differ. Diff generated as OutData/%s_idf.rgb\n", rdramFileName);
        return(1);
     }

     sprintf(sysCommand, "cmp -s OutData/%s_0.cov %s%s_io_0.cov.%s.archive", rdramFileName, archivePath, rdramFileName, version_name);
     errlog(DEBUG, "%s", sysCommand);
     status = system(sysCommand);
     if (!((status != -1) && WIFEXITED(status) && (WEXITSTATUS(status) == 0)))
     {
        sprintf(sysCommand, "$ROOT/usr/sbin/idf -d OutData/%s_0.cov %s%s_io_0.cov.%s.archive OutData/%s_idf.cov", rdramFileName, archivePath, rdramFileName, version_name, rdramFileName);
        errlog(DEBUG, "%s", sysCommand);
        status = system(sysCommand);
        errlog(ERR_SEVERE, ".cov differ. Diff generated as OutData/%s_idf.cov\n", rdramFileName);
        return(1);
     }

     sprintf(sysCommand, "cmp -s OutData/%s_0.z %s%s_io_0.z.%s.archive", rdramFileName, archivePath, rdramFileName, version_name);
     errlog(DEBUG, "%s", sysCommand);
     status = system(sysCommand);
     if (!((status != -1) && WIFEXITED(status) && (WEXITSTATUS(status) == 0)))
     {
        sprintf(sysCommand, "$ROOT/usr/sbin/idf -d OutData/%s_0.z %s%s_io_0.z.%s.archive OutData/%s_idf.z", rdramFileName, archivePath, rdramFileName, version_name, rdramFileName);
        errlog(DEBUG, "%s", sysCommand);
        status = system(sysCommand);
        errlog(ERR_SEVERE, ".z differ. Diff generated as OutData/%s_idf.z\n", rdramFileName);
        return(1);
     }
   }

   if (errcount)
      errlog(ERR_SEVERE, "errcount = %d", errcount);

   return(errcount);
}


int WriteMem(unsigned int addr, unsigned int nbytes, char *buffer)
{
   int errcount = 0;   
   int byteCntr;   

#ifdef NO_MEM_UTILS
   for (byteCntr = 0; byteCntr < nbytes; byteCntr+=4)
   {
      errcount += dgWriteWord(addr + byteCntr, *((unsigned *)&buffer[byteCntr]));
   }
#else
   errcount += dgWriteMem(addr, nbytes, buffer);
#endif

   return (errcount);
}

int ReadMem(unsigned int addr, unsigned int nbytes, char *buffer)
{
   int errcount = 0;
   int byteCntr;   

#ifdef NO_MEM_UTILS
   for (byteCntr = 0; byteCntr < nbytes; byteCntr+=4)
   {
      errcount += dgReadWord(addr + byteCntr, (unsigned *)&buffer[byteCntr]);
   }
#else
   errcount += dgReadMem(addr, nbytes, buffer);
#endif

   return (errcount);
}