tm.c 27.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 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 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983
/**************************************************************************
 *                                                                        *
 *               Copyright (C) 1994, Silicon Graphics, Inc.               *
 *                                                                        *
 *  These coded instructions, statements, and computer programs  contain  *
 *  unpublished  proprietary  information of Silicon Graphics, Inc., and  *
 *  are protected by Federal copyright law.  They  may  not be disclosed  *
 *  to  third  parties  or copied or duplicated in any form, in whole or  *
 *  in part, without the prior written consent of Silicon Graphics, Inc.  *
 *                                                                        *
 **************************************************************************/

/*
 *  Texture Memory Unit
 *
 *  Texel Formats (big endien):
 *
 *            | 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00 |
 *
 *            +------------+-----------+-----------+------------+
 * 4-bit CI,I |     0      |     1     |     2     |     3      |
 *            +------------+-----------+-----------+------------+
 *            +---------+--+--------+--+--------+--+--------+---+
 * 4-bit IA   |    I0   |A0|    I1  |A1|    I2  |A2|    I3  |A3 |
 *            +---------+--+--------+--+--------+--+--------+---+
 *
 *            +------------------------+------------------------+
 * 8-bit CI,I |            0           |           1            |
 *            +------------------------+------------------------+
 *            +------------+-----------+-----------+------------+
 * 8-bit IA   |    I0      |    A0     |    I1     |     A1     |
 *            +------------+-----------+-----------+------------+
 *
 *            +---------------+--------------+--------------+---+
 *16-bit 5551 |      R        |      G       |      B       | A |
 *            +---------------+--------------+--------------+---+
 *            +------------------------+------------------------+
 *16-bit IA   |            I           |           A            |
 *            +------------------------+------------------------+
 *            +------------------------+------------------------+
 *16-bit UV   |            U           |           V            |   (Low Half)
 *            +------------------------+------------------------+
 *            +------------------------+------------------------+
 *16-bit Y    |            Y0          |           Y1           |   (High Half)
 *            +------------------------+------------------------+
 *
 *            +------------------------+------------------------+
 *32-bit RG   |            R           |           G            |   (Low Half)
 *            +------------------------+------------------------+
 *            +------------------------+------------------------+
 *32-bit BA   |            B           |           A            |   (High Half)
 *            +------------------------+------------------------+
 *
 *            | 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00 |
 *
 *  Functions:
 *	tm_tmem(): do texture lookup from memory, do address swap, find CI adrs
 *	tm_texel_sort(): does final sort on texels and formatting
 *	load_tmem(): loads an ASCII file containing Tmem image
 *
 *
 */

#include <stdio.h>

#include "tm.h"

extern int VectorNumber;
int PrintErrorMessages;

/*
 *   M a c r o s 
 */

#define HIGH	1
#define LOW	0


/* save bits <12,3:0> of address */
#define SAVE_ADRS_BITS(a)	((((a) >> 8) & 0x10) | ((a) & 0xf))

/* get high/low bank swap bit */
#define HILO_BIT(a)		((a) & 0x10)

/* get inverted bit 1 */
#define INV_BIT_1(a)		(~(a) & 0x2)

/* invert bit 1 and clear bit 0 */
#define INV_1_CLR_0(a)		(~(a) & 2)

/* invert bit 1 and set bit 0 */
#define INV_1_SET_0(a)		((~(a) | 1) & 3)

/* invert bit 1 and bit 0 */
#define INV_BOTH(a)		(~(a) & 3)

/*  allow external access if debugging */
#ifdef DEBUG
#  define LOCAL 
#else
#  define LOCAL static
#endif /* DEBUG */


/*
 *   G l o b a l s
 */

static unsigned short TmemL0[256];  /* low-mem, 0 to (2KB-1) */
static unsigned short TmemL1[256];
static unsigned short TmemL2[256];
static unsigned short TmemL3[256];
static unsigned short TmemH0[256];  /* hi-mem, 2KB to (4KB-1) */
static unsigned short TmemH1[256];
static unsigned short TmemH2[256];
static unsigned short TmemH3[256];

/*
 *    F u n c t i o n s
 */

static int tex_sort_mux(tm_t *p1, int sel, int hi);

/*==========================================================================
  =  load_tmem() - loads Tmem from a file
  =
  ========================================================================== */
void
  load_tmem(char *file)
{
  FILE *fp;
  int ret, i;
  int s0, s1, s2, s3;

  if((fp = fopen(file,"r")) == NULL)
  {
    fprintf(stderr,"Cannot open file %s\n", file);
    exit(1);
  }

  /* read 4K bytes */
  for(i = 0; i < 512; i++)
  {
    ret = fscanf(fp,"%*s %x %x %x %x", &s0, &s1, &s2, &s3);
    if(ret != 4)
      break;

    if(i < 256) /* low mem */
    {
      TmemL0[i] = (short)s0;
      TmemL1[i] = (short)s1;
      TmemL2[i] = (short)s2;
      TmemL3[i] = (short)s3;
    }
    else /* high mem */
    {
      TmemH0[i-256] = (short)s0;
      TmemH1[i-256] = (short)s1;
      TmemH2[i-256] = (short)s2;
      TmemH3[i-256] = (short)s3;
    }    
  }
  fclose(fp);
}

/*==========================================================================
  =  dump_tmem() - dump Tmem from a file
  =
  ========================================================================== */
void
  dump_tmem(char *file)
{
  FILE *fp;
  int ret, i;
  int s0, s1, s2, s3;

  if((fp = fopen(file,"w")) == NULL)
  {
    fprintf(stderr,"Cannot open file %s\n", file);
    exit(1);
  }

  /* write 4K bytes */
  for(i = 0; i < 512; i++)
  {
    if(i < 256) /* low mem */
    {
      s0 = TmemL0[i];
      s1 = TmemL1[i];
      s2 = TmemL2[i];
      s3 = TmemL3[i];
    }
    else /* high mem */
    {
      s0 = TmemH0[i-256];
      s1 = TmemH1[i-256];
      s2 = TmemH2[i-256];
      s3 = TmemH3[i-256];
    }    
    fprintf(fp,"%03d: %04x %04x %04x %04x\n", i, s0, s1, s2, s3);
  }
  fclose(fp);
}

/*==========================================================================
  =  tm_load() - reads in texel data from copy_load bus
  =
  ========================================================================== */
static void tm_load(tm_t *p0, tm_t *p1)
{
  int RGBA32, YUV;

  RGBA32 = (p1->tile_tex_type == TC_RGBA) & (p1->tile_tex_size == TC_32BIT);
  YUV    = (p1->tile_tex_type == TC_YUV);

  if (YUV)
    p0->tmem_type = 3;
  else if (RGBA32)
    p0->tmem_type = 2;
  else if (p1->odd_t)
    p0->tmem_type = 1;
  else
    p0->tmem_type = 0;

  if (p1->tmem_type == 3)      /* YUV */
    { 
      unsigned char byte0, byte1, byte2, byte3;
      unsigned int lowword;

      byte0 = (p1->copy_load_d1.word1 & (0xff << 16)) >> 16;
      byte1 = (p1->copy_load_d1.word1 & (0xff <<  0)) >>  0;
      byte2 = (p1->copy_load_d1.word0 & (0xff << 16)) >> 16;
      byte3 = (p1->copy_load_d1.word0 & (0xff <<  0)) >>  0;
      lowword     = (byte0 << 24) | (byte1 << 16) | (byte2 << 8) | byte3;

      p0->di_high.word1 = lowword;
      p0->di_high.word0 = lowword;

      byte0 = (p1->copy_load_d1.word1 & (0xff << 24)) >> 24;
      byte1 = (p1->copy_load_d1.word1 & (0xff <<  8)) >>  8;
      byte2 = (p1->copy_load_d1.word0 & (0xff << 24)) >> 24;
      byte3 = (p1->copy_load_d1.word0 & (0xff <<  8)) >>  8;
      lowword    = (byte0 << 24) | (byte1 << 16) | (byte2 << 8) | byte3;

      p0->di_low.word1 = lowword;
      p0->di_low.word0 = lowword;
    }
  else if (p1->tmem_type == 2) /* RGBA32 */
    {
      unsigned short short0, short1;
      unsigned int lowword;

      short0 = p1->copy_load_d1.word1 & 0xffff;
      short1 = p1->copy_load_d1.word0 & 0xffff;
      lowword = (short0 << 16) | short1;
      p0->di_high.word1 = lowword;
      p0->di_high.word0 = lowword;

      short0 = (p1->copy_load_d1.word1 & (0xffff << 16)) >> 16;
      short1 = (p1->copy_load_d1.word0 & (0xffff << 16)) >> 16;
      lowword = (short0 << 16) | short1;
      p0->di_low.word1 = lowword;
      p0->di_low.word0 = lowword;
    }
  else if (p1->tmem_type == 1) /* !YUV & !RGBA32 & (odd_t == 1) */
    {
      unsigned int long0, long1;
      
      long0 = p1->copy_load_d1.word0;
      long1 = p1->copy_load_d1.word1;

      p0->di_high.word1 = long0;
      p0->di_high.word0 = long1;
      p0->di_low.word1  = long0;
      p0->di_low.word0  = long1;
    }
  else                         /* !YUV & !RGBA32 & (odd_t == 0) */
    {
      unsigned int long0, long1;
      
      long0 = p1->copy_load_d1.word1;
      long1 = p1->copy_load_d1.word0;

      p0->di_high.word1 = long0;
      p0->di_high.word0 = long1;
      p0->di_low.word1  = long0;
      p0->di_low.word0  = long1;
    }

  /* Now set the write enable bits */

  p0->we01_low  = p1->load_dv & ((!p1->a_twelve & !RGBA32 & !YUV) |
				 (RGBA32 | YUV) & !(p1->a_three ^ p1->odd_t));

  p0->we23_low  = p1->load_dv & ((!p1->a_twelve & !RGBA32 & !YUV) |
				 (RGBA32 | YUV) & (p1->a_three ^ p1->odd_t));

  p0->we01_high = p1->load_dv & ((p1->a_twelve & !RGBA32 & !YUV) |
				(RGBA32 | YUV) & !(p1->a_three ^ p1->odd_t));

  p0->we23_high = p1->load_dv & ((p1->a_twelve & !RGBA32 & !YUV) |
				(RGBA32 | YUV) & (p1->a_three ^ p1->odd_t));				


  /* Now stuff di_high and di_low into memory arrays */

  if (p1->we01_low)
    {
      TmemL0[p1->adrs_bnk0l] = (p0->di_low.word1  & (0xffff << 16)) >> 16;
      TmemL1[p1->adrs_bnk1l] = (p0->di_low.word1  & (0xffff <<  0)) >>  0;
    }
  if (p1->we23_low)
    {
      TmemL2[p1->adrs_bnk2l] = (p0->di_low.word0  & (0xffff << 16)) >> 16;
      TmemL3[p1->adrs_bnk3l] = (p0->di_low.word0  & (0xffff <<  0)) >>  0;
    }
  if (p1->we01_high)
    {
      TmemH0[p1->adrs_bnk0h] = (p0->di_high.word1 & (0xffff << 16)) >> 16;
      TmemH1[p1->adrs_bnk1h] = (p0->di_high.word1 & (0xffff <<  0)) >>  0;
    }
  if (p1->we23_high)
    {
      TmemH2[p1->adrs_bnk2h] = (p0->di_high.word0 & (0xffff << 16)) >> 16;
      TmemH3[p1->adrs_bnk3h] = (p0->di_high.word0 & (0xffff <<  0)) >>  0;
    }  
}

/*==========================================================================
  =  clr_indx_mux() - muxes color index addresses
  =
  ========================================================================== */
static int
  clr_indx_mux(int adrs, int size, int pal, int tex)
{
  int lsb_sel;
  int ms_nibble, ls_nibble;

  if(size == TC_4BIT)
  {
    lsb_sel = INV_BOTH(adrs);
    ms_nibble = pal & 0xf;
  }
  else /* 8-bit */
  {
    lsb_sel = INV_1_CLR_0(adrs);
    if(INV_BIT_1(adrs))
      ms_nibble = (tex >> 12) & 0xf;
    else
      ms_nibble = (tex >> 4) & 0xf;
  }
  ls_nibble = (tex >> (lsb_sel*4)) & 0xf;

  return((ms_nibble << 4) | ls_nibble);
}


/*==========================================================================
  =  tm_tmem() - does lookup of texels from Tmem
  =
  ========================================================================== */
static void
  tm_tmem(tm_t *p0, tm_t *p1)
{
  int adrs_a;
  int adrs_b_rg;
  int adrs_c;
  int adrs_d_rg;
  int adrs_b_ba;
  int adrs_d_ba;

  p0->texL0 = TmemL0[p1->adrs_bnk0l];
  p0->texL1 = TmemL1[p1->adrs_bnk1l];
  p0->texL2 = TmemL2[p1->adrs_bnk2l];
  p0->texL3 = TmemL3[p1->adrs_bnk3l];
  p0->texH0 = TmemH0[p1->adrs_bnk0h];
  p0->texH1 = TmemH1[p1->adrs_bnk1h];
  p0->texH2 = TmemH2[p1->adrs_bnk2h];
  p0->texH3 = TmemH3[p1->adrs_bnk3h];

  /* save address, only bits used (12,3:0) */
  adrs_a    = SAVE_ADRS_BITS(p1->adrs_a);
  adrs_b_ba = SAVE_ADRS_BITS(p1->adrs_b_ba);
  adrs_c    = SAVE_ADRS_BITS(p1->adrs_c);
  adrs_d_ba = SAVE_ADRS_BITS(p1->adrs_d_ba);
  adrs_b_rg = SAVE_ADRS_BITS(p1->adrs_b_rg);
  adrs_d_rg = SAVE_ADRS_BITS(p1->adrs_d_rg);

  /*
   *  save swapped addresses 
   */

  if(!p1->swap_rg) 
  {
    p0->swap_a_lo = adrs_a;
    p0->swap_b_lo = adrs_b_rg;
    p0->swap_c_lo = adrs_c;
    p0->swap_d_lo = adrs_d_rg;
  }
  else
  {
    p0->swap_a_lo = adrs_d_rg;
    p0->swap_b_lo = adrs_c;
    p0->swap_c_lo = adrs_b_rg;
    p0->swap_d_lo = adrs_a;
  }

  if(!p1->swap_ba) 
  {
    p0->swap_a_hi = adrs_a;
    p0->swap_b_hi = adrs_b_ba;
    p0->swap_c_hi = adrs_c;
    p0->swap_d_hi = adrs_d_ba;
  }
  else
  {
    p0->swap_a_hi = adrs_d_ba;
    p0->swap_b_hi = adrs_c;
    p0->swap_c_hi = adrs_b_ba;
    p0->swap_d_hi = adrs_a;
  }

  /*
   *  Find CI addresses, do this one clock ahead so return to TC unit
   *  can be order independent.  In real HW this is done in clock 11.
   */
  p0->clr_ind_a = clr_indx_mux(p0->swap_a_lo, p0->tile_tex_size, p1->palette, 
	tex_sort_mux(p0, p0->swap_a_lo, LOW));
  p0->clr_ind_b = clr_indx_mux(p0->swap_b_lo, p0->tile_tex_size, p1->palette, 
	tex_sort_mux(p0, p0->swap_b_lo, LOW));
  p0->clr_ind_c = clr_indx_mux(p0->swap_c_lo, p0->tile_tex_size, p1->palette, 
	tex_sort_mux(p0, p0->swap_c_lo, LOW));
  p0->clr_ind_d = clr_indx_mux(p0->swap_d_lo, p0->tile_tex_size, p1->palette, 
	tex_sort_mux(p0, p0->swap_d_lo, LOW));

}


/*==========================================================================
  =  tex_sort_mux() - does 4-1 mux on memory texels
  =    uses bits <3,2> of select
  ========================================================================== */
static int
  tex_sort_mux(tm_t *p1, int sel, int hi)
{
  if(hi == HIGH) /* high bank */
  {
    switch(sel & 0xc)
    {
      case 0x0:
	return(p1->texH0);
      case 0x4:
	return(p1->texH1);
      case 0x8:
	return(p1->texH2);
      case 0xc:
	return(p1->texH3);
    }
  }
  else  /* low bank */
  {
    switch(sel & 0xc)
    {
      case 0x0:
	return(p1->texL0);
      case 0x4:
	return(p1->texL1);
      case 0x8:
	return(p1->texL2);
      case 0xc:
	return(p1->texL3);
    }
  }
}

/*==========================================================================
  =  low_hi_low_mux() - determines if should swap hi/low texels
  ========================================================================== */
static int
  low_hi_lo_mux(int tlut_en_d2, int type, int size, int sel, int hi, int lo)
{
  if(tlut_en_d2)
    return(hi);
  else if(((type == TC_RGBA) && (size == TC_32BIT)) || 
          (type == TC_YUV))
    return(lo);
  else
    return(sel ? hi : lo);
}

/*==========================================================================
  =  high_hi_low_mux() - determines if should swap hi/low texels
  ========================================================================== */
static int
  high_hi_lo_mux(int tlut_en_d2, int type, int size, int sel, int hi, int lo)
{
  if(tlut_en_d2 || 
     (((type == TC_RGBA) && (size == TC_32BIT)) ||
          (type == TC_YUV)))
    return(hi);
  else
    return(sel ? hi : lo);
}

/*==========================================================================
  =  get_msb_sel_r() - grab 4/8 bit texels from 16b texel for Red
  ========================================================================== */
static int
  get_msb_sel_r(int type, int size, int sel)
{
  switch(size)
  {
    case TC_4BIT:
      return INV_BOTH(sel);
    case TC_8BIT:
      return INV_1_SET_0(sel);
    default:
      return(3);
  }
}

/*==========================================================================
  =  get_lsb_sel_r() - grab 4/8 bit texels from 16b texel for Red
  ========================================================================== */
static int
  get_lsb_sel_r(int type, int size, int sel)
{
  switch(size)
  {
    case TC_4BIT:
      return INV_BOTH(sel);
    case TC_8BIT:
      if (type == TC_IA)
        return INV_1_SET_0(sel);
      else
        return INV_1_CLR_0(sel);
    default:
      return(2);
  }
}


/*==========================================================================
  =  get_msb_sel_a() - grab 4/8 bit texels from 16b texel for Alpha
  ========================================================================== */
static int
  get_msb_sel_a(int type, int size, int sel)
{
  switch(size)
  {
    case TC_4BIT:
      return INV_BOTH(sel);
    case TC_8BIT:
      return INV_1_CLR_0(sel);
    case TC_16BIT:
      if (type == TC_YUV)
        return INV_1_SET_0(sel);
      else
        return(1);
    default:
      return(1);
  }
}

/*==========================================================================
  =  tex_red_format_mux() - format red texel, find replicated texel value
  =  for other format muxes.
  ========================================================================== */
static int
  tex_red_format_mux(int msb, int lsb, int type, int size, int pal, int texr, int *rep)
{
  int ms_nibble, ls_nibble;
  int red;

  ms_nibble = (texr >> (msb * 4)) & 0xf;
  ls_nibble = (texr >> (lsb * 4)) & 0xf;

  /* prepend a palette */
  if((type == TC_CI) && (size == TC_4BIT))
    ms_nibble = pal;

  /* replicate if 4b IA */
  if((type == TC_IA) && (size == TC_4BIT))
    red = (0xe0 & (ms_nibble << 4)) | 
	  (0x1c & (ms_nibble << 1)) | 
	  (0x03 & (ms_nibble >> 2));
  else
    red = (ms_nibble << 4) | ls_nibble;

  /* save replicated output for use on green, blue, and alpha muxes */
  *rep = red;
 
  /* do UV offset (subtract 128) */
  if(type == TC_YUV)
  {
    red ^= 0x80;
    if(red & 0x80)
      red |= 0x100;  /* set sign bit */
  }

  if((type == TC_RGBA) && (size == TC_16BIT))
    red = ((texr >> 8) & 0xf8) | ((texr >> 13) & 0x7);

  return(red);
}


/*==========================================================================
  =  tex_grn_format_mux() - format green texel
  ========================================================================== */
static int
  tex_grn_format_mux(int type, int size, int texg, int rep)
{
  int green;

  if((type == TC_RGBA) && (size == TC_16BIT))
  {
    green = ((texg >> 3) & 0xf8) | ((texg >> 8) & 0x7);
    return(green);
  }

  if(((type == TC_IA) && (size == TC_16BIT)) || 
     (size == TC_4BIT)  || (size == TC_8BIT))
    green = rep;
  else
    green = texg & 0xff;

  /* subtract 128 if UV */
  if(type == TC_YUV)
  {
    green ^= 0x80;
    if(green & 0x80)
      green |= 0x100;
  }

  return(green);
}

/*==========================================================================
  =  tex_blu_format_mux() - format blue texel
  ========================================================================== */
static int
  tex_blu_format_mux(int type, int size, int swap, int texb, int rep)
{
  int blue;

  /* if 5/5/5/1 override */
  if((type == TC_RGBA) && (size == TC_16BIT))
  {
    blue = (0xf8 & (texb << 2)) | (0x7 & (texb >> 3));
    return(blue);
  }

  if(swap || type != TC_YUV)
    blue = (texb >> 8) & 0xff;
  else
    blue = texb & 0xff;

  if(((type == TC_IA) && (size == TC_16BIT)) ||
     (size == TC_4BIT) || (size == TC_8BIT))
    blue = rep;

  return(blue);
}

/*==========================================================================
  =  tex_alp_format_mux() - format alpha texel
  ========================================================================== */
static int
  tex_alp_format_mux(int msb, int type, int size, int swap, int texa, int rep)
{
  int ms_nibble, ls_nibble;

  /* in 5/5/5/1 override all */
  if((type == TC_RGBA) && (size == TC_16BIT))
    return(255 * (texa & 1));

  ms_nibble = (texa >> (msb*4)) & 0xf;

  if(((type == TC_YUV) || ((type == TC_IA) && (size == TC_8BIT))) 
       && swap)
    ls_nibble = (texa >> 8) & 0xf;
  else
    ls_nibble = texa & 0xf;

  if((type == TC_IA) && (size == TC_4BIT)) /* replicate alpha bit */
  {
    ms_nibble = ((texa >> (msb*4)) & 1 ? 15 : 0);
    ls_nibble = ((texa >> (msb*4)) & 1 ? 15 : 0);
  }

  if(((type != TC_IA) && (size == TC_4BIT)) ||
     ((type != TC_IA) && (size == TC_8BIT)))
    return(rep);
  else
    return((ms_nibble << 4) | ls_nibble);
}

/*==========================================================================
  =  tm_texel_sort() - sorts and formats texels
  =
  ========================================================================== */
static void
  tm_texel_sort(tm_t *p0, tm_t *p1)
{
  int 			tlut_en_d2; /* convenience variable */
  TcMwTypes 	type = TC_RGBA;
  TcMwSizes 	size = TC_16BIT;

  int swap_a_lo;
  int swap_b_lo;
  int swap_c_lo;
  int swap_d_lo;
  int swap_a_hi;
  int swap_b_hi;
  int swap_c_hi;
  int swap_d_hi;

  int sort_tex_a_lo;
  int sort_tex_b_lo;
  int sort_tex_c_lo;
  int sort_tex_d_lo;
  int sort_tex_a_hi;
  int sort_tex_b_hi;
  int sort_tex_c_hi;
  int sort_tex_d_hi;

  int tex_a_rg;
  int tex_b_rg;
  int tex_c_rg;
  int tex_d_rg;
  int tex_a_ba;
  int tex_b_ba;
  int tex_c_ba;
  int tex_d_ba;

  int msb_sel, lsb_sel;		/* format mux selects */

  /*int rep; */ 		/* replicated texel */
  int repa, repb, repc, repd;   /* replicated texels */

  /*
   *  Get Attributes
   */
  tlut_en_d2 = p1->tlut_en_d2;

  type = p1->tile_tex_type_d2;
  size = p1->tile_tex_size_d2;

  /*
   *  Overide if texture lookup table (Tlut) enabled
   */
  if(tlut_en_d2)
  {
    size = TC_16BIT;
    if(p1->tlut_type == TC_TLUT_RGBA_16b)
      type = TC_RGBA;
    else
      type = TC_IA;
  }

  /*
   *  Convenience variables
   */
  swap_a_lo = p1->swap_a_lo;
  swap_b_lo = p1->swap_b_lo;
  swap_c_lo = p1->swap_c_lo;
  swap_d_lo = p1->swap_d_lo;
  swap_a_hi = p1->swap_a_hi;
  swap_b_hi = p1->swap_b_hi;
  swap_c_hi = p1->swap_c_hi;
  swap_d_hi = p1->swap_d_hi;
  
  /*
   *  Sort texels
   */
  
  sort_tex_a_lo = tex_sort_mux(p1, swap_a_lo, LOW);
  sort_tex_b_lo = tex_sort_mux(p1, swap_b_lo, LOW);
  sort_tex_c_lo = tex_sort_mux(p1, swap_c_lo, LOW);
  sort_tex_d_lo = tex_sort_mux(p1, swap_d_lo, LOW);

#ifdef DEBUG_PRINT
  fprintf(stderr,"sort_tex: %04x %04x %04x %04x\n", 
    sort_tex_a_lo, sort_tex_b_lo,
    sort_tex_c_lo, sort_tex_d_lo);
#endif /* DEBUG_PRINT */

  if(tlut_en_d2)
  {
    sort_tex_a_hi = tex_sort_mux(p1, 0x0, HIGH);
    sort_tex_b_hi = tex_sort_mux(p1, 0x4, HIGH);
    sort_tex_c_hi = tex_sort_mux(p1, 0x8, HIGH);
    sort_tex_d_hi = tex_sort_mux(p1, 0xc, HIGH);
  }
  else
  {
    sort_tex_a_hi = tex_sort_mux(p1, swap_a_hi, HIGH);
    sort_tex_b_hi = tex_sort_mux(p1, swap_b_hi, HIGH);
    sort_tex_c_hi = tex_sort_mux(p1, swap_c_hi, HIGH);
    sort_tex_d_hi = tex_sort_mux(p1, swap_d_hi, HIGH);
  }

  /*
   *  Hi/Low Swap
   */
  tex_a_rg = low_hi_lo_mux(tlut_en_d2, type, size, HILO_BIT(swap_a_lo), 
		sort_tex_a_hi, sort_tex_a_lo);
  tex_b_rg = low_hi_lo_mux(tlut_en_d2, type, size, HILO_BIT(swap_b_lo), 
		sort_tex_b_hi, sort_tex_b_lo);
  tex_c_rg = low_hi_lo_mux(tlut_en_d2, type, size, HILO_BIT(swap_c_lo), 
		sort_tex_c_hi, sort_tex_c_lo);
  tex_d_rg = low_hi_lo_mux(tlut_en_d2, type, size, HILO_BIT(swap_d_lo), 
		sort_tex_d_hi, sort_tex_d_lo);
  tex_a_ba = high_hi_lo_mux(tlut_en_d2, type, size, HILO_BIT(swap_a_hi), 
		sort_tex_a_hi, sort_tex_a_lo);
  tex_b_ba = high_hi_lo_mux(tlut_en_d2, type, size, HILO_BIT(swap_b_hi), 
		sort_tex_b_hi, sort_tex_b_lo);
  tex_c_ba = high_hi_lo_mux(tlut_en_d2, type, size, HILO_BIT(swap_c_hi), 
		sort_tex_c_hi, sort_tex_c_lo);
  tex_d_ba = high_hi_lo_mux(tlut_en_d2, type, size, HILO_BIT(swap_d_hi), 
		sort_tex_d_hi, sort_tex_d_lo);

#ifdef DEBUG_PRINT
  fprintf(stderr,"tex_?_rg: %04x %04x %04x %04x\n", 
    tex_a_rg, tex_b_rg,
    tex_c_rg, tex_d_rg);
  fprintf(stderr,"tex_?_ba: %04x %04x %04x %04x\n", 
    tex_a_ba, tex_b_ba,
    tex_c_ba, tex_d_ba);
#endif /* DEBUG_PRINT */

  /*   Format Texel for RED/GREEN channel, A texel  */
  msb_sel = get_msb_sel_r(type, size, swap_a_lo);
  lsb_sel = get_lsb_sel_r(type, size, swap_a_lo);
  p0->red_a = tex_red_format_mux(msb_sel, lsb_sel, type, size, p1->palette_1d, tex_a_rg, &repa);
  p0->green_a = tex_grn_format_mux(type, size, tex_a_rg, repa);

#ifdef DEBUG_PRINT
  fprintf(stderr,"**** A ***** adrs_a %x \n", p1->adrs_a);
  fprintf(stderr,"type: %x swap_a_lo: %x swap_a_hi: %x msb_sel: %x lsb_sel: %x rep: %x\n", 
	type, swap_a_lo, swap_a_hi, msb_sel, lsb_sel, rep);
  fprintf(stderr,"red_a: %03x green_a: %03x\n", 
    p0->red_a, p0->green_a);
#endif /* DEBUG_PRINT */

  /*   Format Texel for BLUE/ALPHA channel, A texel  */
  p0->blue_a = tex_blu_format_mux(type, size, INV_BIT_1(swap_a_hi), tex_a_ba, repa);
  msb_sel = get_msb_sel_a(type, size, swap_a_hi);
  p0->alpha_a = tex_alp_format_mux(msb_sel, type, size, INV_BIT_1(swap_a_hi), tex_a_ba, repa);

  /*   Format Texel for RED/GREEN channel, B texel  */
  msb_sel = get_msb_sel_r(type, size, swap_b_lo);
  lsb_sel = get_lsb_sel_r(type, size, swap_b_lo);
  p0->red_b = tex_red_format_mux(msb_sel, lsb_sel, type, size, p1->palette_1d, tex_b_rg, &repb);
  p0->green_b = tex_grn_format_mux(type, size, tex_b_rg, repb);


#ifdef DEBUG_PRINT
  fprintf(stderr,"**** B *****\n");
  fprintf(stderr,"type: %x swap_b_lo: %x msb_sel: %x lsb_sel: %x rep: %x\n", 
	type, swap_b_lo, msb_sel, lsb_sel, rep);
  fprintf(stderr,"red_b: %03x green_b: %03x\n", 
    p0->red_b, p0->green_b);
#endif /* DEBUG_PRINT */

  /*   Format Texel for BLUE/ALPHA channel, B texel  */
  p0->blue_b = tex_blu_format_mux(type, size, INV_BIT_1(swap_b_hi), tex_b_ba, repb);
  msb_sel = get_msb_sel_a(type, size, swap_b_hi);
  p0->alpha_b = tex_alp_format_mux(msb_sel, type, size, INV_BIT_1(swap_b_hi), tex_b_ba, repb);

  /*   Format Texel for RED/GREEN channel, C texel  */
  msb_sel = get_msb_sel_r(type, size, swap_c_lo);
  lsb_sel = get_lsb_sel_r(type, size, swap_c_lo);
  p0->red_c = tex_red_format_mux(msb_sel, lsb_sel, type, size, p1->palette_1d, tex_c_rg, &repc);
  p0->green_c = tex_grn_format_mux(type, size, tex_c_rg, repc);

  /*   Format Texel for BLUE/ALPHA channel, C texel  */
  p0->blue_c = tex_blu_format_mux(type, size, INV_BIT_1(swap_c_hi), tex_c_ba, repc);
  msb_sel = get_msb_sel_a(type, size, swap_c_hi);
  p0->alpha_c = tex_alp_format_mux(msb_sel, type, size, INV_BIT_1(swap_c_hi), tex_c_ba, repc);

  /*   Format Texel for RED/GREEN channel, D texel  */
  msb_sel = get_msb_sel_r(type, size, swap_d_lo);
  lsb_sel = get_lsb_sel_r(type, size, swap_d_lo);
  p0->red_d = tex_red_format_mux(msb_sel, lsb_sel, type, size, p1->palette_1d, tex_d_rg, &repd);
  p0->green_d = tex_grn_format_mux(type, size, tex_d_rg, repd);

  /*   Format Texel for BLUE/ALPHA channel, D texel  */
  p0->blue_d = tex_blu_format_mux(type, size, INV_BIT_1(swap_d_hi), tex_d_ba, repd);
  msb_sel = get_msb_sel_a(type, size, swap_d_hi);
  p0->alpha_d = tex_alp_format_mux(msb_sel, type, size, INV_BIT_1(swap_d_hi), tex_d_ba, repd);


  /* set copy bus */
  if (p1->copy_load_e) {
    p0->copy_load.word0 = (tex_c_rg << 16) | tex_d_rg;

    if (size == TC_16BIT)
      p0->copy_load.word1 = (tex_a_rg << 16) | tex_b_rg;
    else
      p0->copy_load.word1 = (repa << 24) | (repb << 16) | (repc << 8) | repd;
  }

  p0->copy_load2.word0 = (tex_c_rg << 16) | tex_d_rg;

  if (size == TC_16BIT)
    p0->copy_load2.word1 = (tex_a_rg << 16) | tex_b_rg;
  else
    p0->copy_load2.word1 = (repa << 24) | (repb << 16) | (repc << 8) | repd;

}



/*==========================================================================
 *  tm():  main interface routine for texture memory unit
 *==========================================================================*/
void
  tm(tm_t **pp0, tm_t **pp1)
{
  /* pointers to memory structure */
  tm_t *p0, *p1;
  int save_clock;

  /* get pointers */
  p0 = *pp0;
  p1 = *pp1;
  save_clock = p0->gclk;

  if(p0->gclk && !p1->gclk_old) /* posedge(gclk) */
  {
    /* transfer all next-clock register values to register outputs. */
    *pp0 = p1; /* swap */
    *pp1 = p0;
    p0 = *pp0; /* fix pointers */
    p1 = *pp1;

    /* p0->load_dv = !p1->load_dv; */
    
    p0->copy_load_e = !p1->load_dv;

    /* if (p1->load_dv) */
      {
	/* read copy_load bus */
	tm_load(p0, p1);
      }

    /* look up texels */
    tm_tmem(p0, p1);

    /* sort and format texels */
    tm_texel_sort(p0, p1);

    /* delay signals */
    p0->tlut_en_d1   = p1->tlut_en;
    p0->tlut_en_d2   = p1->tlut_en_d1;

    p0->copy_load_d1 = p1->copy_load;

    p0->tile_tex_type_d1 = p1->tile_tex_type;
    p0->tile_tex_type_d2 = p1->tile_tex_type_d1;

    p0->tile_tex_size_d1 = p1->tile_tex_size;
    p0->tile_tex_size_d2 = p1->tile_tex_size_d1;

    p0->palette_1d = p1->palette;

  } /* posedge */

  /* save last clock state */
  p1->gclk_old = save_clock;
}

/*==========================================================================
 *  tm_init():  initialization routine for texture memory unit
 *==========================================================================*/
void
  tm_init(tm_t *p0, tm_t *p1)
{

}