cc.c 18.6 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
/**************************************************************************
 *                                                                        *
 *               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.  *
 *                                                                        *
 **************************************************************************/

/*
 *  Color Combiner Unit
 *
 *  This unit is basically a 'paint mixer', providing various ways of
 *  combining texture, shade, and various register colors.
 *
 *  8/16/94  RJM
 */
#include <stdio.h>

#include "cc.h"

#define POSEDGE         (save_clk && !save_clk_old)
#define NEGEDGE         (!save_clk && save_clk_old)


#ifndef NULL
#define NULL (void *)0
#endif

#ifndef MIN
#define MIN(x,y)		(((x) < (y)) ? (x) : (y))
#endif

#ifndef MAX
#define MAX(x,y)		(((x) > (y)) ? (x) : (y))
#endif

#ifndef ABS
#define ABS(x)			(((x) < 0) ? -(x) : (x))
#endif

#define CLAMP(low, val, high)	(MAX(MIN(val, high), low))

#define AKE_SIGN_EXTEND_9B(x)	((((x) & 0x180) == 0x180) ? \
				((x) | ~0x1ff) : (x))

#define SIGN_EXTEND_9B(x)	(((x) & 0x100) ? ((x) | (~0x1ff)) : (x))
#define SIGN_EXTEND_12B(x)	(((x) & 0x800) ? ((x) | (~0xfff)) : (x))
#define SIGN_EXTEND_17B(x)	(((x) & 0x10000) ? ((x) | (~0x1ffff)) : (x))


/***************************************************************************
 * four_to_one(), 4:1 mux
 ***************************************************************************
 */
static int
  four_to_one( int sel, int a, int b, int c, int d )
{
  switch(sel & 3)
  {
    case 0:
      return(a);
    case 1:
      return(b);
    case 2:
      return(c);
    case 3:
      return(d);
  }
}

/***************************************************************************
 * eight_to_one(), 8:1 mux
 ***************************************************************************
 */
static int
  eight_to_one( int sel, int i0, int i1, int i2, int i3, int i4, int i5, int i6, int i7 )
{
  int out0, out1;
  int sel0, sel1;
  sel0 = sel & 3;
  sel1 = (sel & 4) >> 2;

  out0 = four_to_one(sel0, i0, i1, i2, i3);
  out1 = four_to_one(sel0, i4, i5, i6, i7);
  return(sel1 ? out1 : out0);
}

/***************************************************************************
 * sixteen_to_one(), 16:1 mux
 ***************************************************************************
 */
static int
  sixteen_to_one( int sel, int i0, int i1, int i2, int i3, int i4, int i5, int i6, int i7,
                           int i8, int i9, int ia, int ib, int ic, int id, int ie, int ig )
{
  int out0, out1;
  int sel0, sel1;
  sel0 = sel & 0x7;
  sel1 = (sel & 8) >> 3;

  out0 = eight_to_one(sel0, i0, i1, i2, i3, i4, i5, i6, i7);
  out1 = eight_to_one(sel0, i8, i9, ia, ib, ic, id, ie, ig);
  return(sel1 ? out1 : out0);
}

/***************************************************************************
 * thirtytwo_to_one(), 32:1 mux
 ***************************************************************************
 */
static int
  thirtytwo_to_one( int sel, int i0, int i1, int i2, int i3, int i4, int i5, int i6, int i7,
                             int i8, int i9, int ia, int ib, int ic, int id, int ie, int ig,
                             int i10, int i11, int i12, int i13, int i14, int i15, int i16, int i17,
                             int i18, int i19, int i1a, int i1b, int i1c, int i1d, int i1e, int i1f)
{
  int out0, out1;
  int sel0, sel1;
  sel0 = sel & 0xf;
  sel1 = (sel & 0x10) >> 4;

  out0 = sixteen_to_one(sel0, i0, i1, i2, i3, i4, i5, i6, i7,
                            i8, i9, ia, ib, ic, id, ie, ig);
  out1 = sixteen_to_one(sel0, i10, i11, i12, i13, i14, i15, i16, i17,
                            i18, i19, i1a, i1b, i1c, i1d, i1e, i1f);
  return(sel1 ? out1 : out0);
}


/***************************************************************************
 *  Linear Interpolator for color combiner function
 ***************************************************************************/

static int
  do_rgb_lerp( int suba, int subb, int mul, int add)
{
  int tsuba, tsubb, tmula, tmulb, tadda, taddb;

  /* convert to sign extended 2's comp numbers */
  tsuba = AKE_SIGN_EXTEND_9B(suba);
  tsubb = AKE_SIGN_EXTEND_9B(subb);
  tmula = tsuba - tsubb;
  tmulb = SIGN_EXTEND_9B(mul);
  tadda = (tmula * tmulb);
  taddb = AKE_SIGN_EXTEND_9B(add);
  return(tadda + (taddb << 8) + 0x80); /* round, s8.8 */
}

/***************************************************************************
 *  Linear Interpolator for alpha combiner function
 ***************************************************************************/
static int
  do_alpha_lerp( int suba, int subb, int mul, int add)
{
  int tsuba, tsubb, tmula, tmulb, tadda, taddb;

  /* convert to sign extended 2's comp numbers */
  tsuba = AKE_SIGN_EXTEND_9B(suba);
  tsubb = AKE_SIGN_EXTEND_9B(subb);
  tmula = tsuba - tsubb;
  tmulb = SIGN_EXTEND_9B(mul);
  tadda = (tmula * tmulb);
  taddb = AKE_SIGN_EXTEND_9B(add);
  return ((tadda + (taddb << 8) + 0x80) >> 8); /* s,0.8 */
}

/***************************************************************************
 *  Do Akeley method clamping:
 *
 *  Carry one extra bit of precision through all computations,
 *  and never clamp until the last computation is done (e.g. never clamp
 *  intermediate values, especially those that continue to be interpolated).
 *  When done, clamp the result if the MSB (extra bit) is one.  In this case,
 *  clamp to all zeros if the next most significant bit is 1, and to all ones
 *  if the next most significant bit is zero.  (To undo the wrap.)
 *
 ***************************************************************************/
static int
  akeley_clamp9b(int val)
{
  if(val & 0x100)
  {
    if(val & 0x80) /* clamp to zero */
	return 0;
    else /* clamp to all ones */
	return 0xff;
  }
  else /* no clamp */
    return(val);
}

/***************************************************************************
 *  Color Combiner function
 ***************************************************************************/

cc(cc_t **pp0, cc_t **pp1)
{
  /* pointers to memory structure */
  cc_t *p0, *p1;
  int save_clk;
  int save_clk_old;

  /* input signals */
    int st_span;                /* start of span */
    int ncyc;                   /* number of cycles mode */

    int key_en;                 /* enable keying mode */
    int cvg_times_alpha;        /* modify pixel coverage with alpha */
    int alpha_cvg_select;       /* select source of pixel alpha */

    int cc_x_sel_0_r;           /* [3:0] cc mode */
    int cc_y_sel_0_r;           /* [3:0] */
    int cc_a_sel_0_r;           /* [4:0] */
    int cc_c_sel_0_r;           /* [2:0] */

    int cc_x_sel_0_a;           /* [2:0] cc mode */
    int cc_y_sel_0_a;           /* [2:0] */
    int cc_a_sel_0_a;           /* [2:0] */
    int cc_c_sel_0_a;           /* [2:0] */

    int cc_x_sel_1_r;           /* [3:0] cc mode */
    int cc_y_sel_1_r;           /* [3:0] */
    int cc_a_sel_1_r;           /* [4:0] */
    int cc_c_sel_1_r;           /* [2:0] */

    int cc_x_sel_1_a;           /* [2:0] cc mode */
    int cc_y_sel_1_a;           /* [2:0] */
    int cc_a_sel_1_a;           /* [2:0] */
    int cc_c_sel_1_a;           /* [2:0] */

    int st_r;                   /* [7:0], shade value */
    int st_g;                   /* [7:0], shade value */
    int st_b;                   /* [7:0], shade value */
    int st_a;                   /* [7:0], shade value */

    int tf_r;                   /* [8:0], texel value */
    int tf_g;                   /* [8:0], texel value */
    int tf_b;                   /* [8:0], texel value */
    int tf_a;                   /* [8:0], texel value */

    int tf_lod_frac;            /* [8:0], texel lod value */

    int prim_lod_frac;          /* [8:0] primitive LOD fraction */

    int prim_r;                 /* [7:0] primitive color */
    int prim_g;                 /* [7:0] primitive color */
    int prim_b;                 /* [7:0] primitive color */
    int prim_a;                 /* [7:0] primitive color */

    int env_r;                  /* [7:0] environment color */
    int env_g;                  /* [7:0] environment color */
    int env_b;                  /* [7:0] environment color */
    int env_a;                  /* [7:0] environment color */

    int center_r;               /* [7:0] color keying center */
    int center_g;               /* [7:0] color keying center */
    int center_b;               /* [7:0] color keying center */
    int scale_r;                /* [7:0] color keying scale */
    int scale_g;                /* [7:0] color keying scale */
    int scale_b;                /* [7:0] color keying scale */

    int width_r;                /* [11:0] color keying width */
    int width_g;                /* [11:0] color keying width */
    int width_b;                /* [11:0] color keying width */

    int k4_coeff;               /* [8:0] color conversion coefficient */
    int k5_coeff;               /* [8:0] color conversion coefficient */

    int noise;                  /* [8:0] color conversion coefficient */

    int cvg;                    /* [3:0], 1.3, pixel coverage */

  /* temporary signals */
  int sel;
  int key_mode;

  /* outputs of muxes */
  int   suba_r, suba_g, suba_b, suba_a;
  int   subb_r, subb_g, subb_b, subb_a;
  int   mul_r, mul_g, mul_b, mul_a;
  int   add_r, add_g, add_b, add_a;

  /* intermediate key values */
  int key_r, key_g, key_b, key;
  int clamp_a, key_or_a, pix_cvg;
  int a_minus_lf, lfrac, denom, cvg_x_a;
  int dither, dither_a, dither_st_a;

  /* 
   *  Get pointers, clocks
   */
  p0 = *pp0;
  p1 = *pp1;
  save_clk = p0->gclk;
  save_clk_old = p1->gclk_old;

  if(POSEDGE)
  {
    /* grab inputs from p0 */
    st_span = p0->st_span;
    ncyc = p0->ncyc;

    key_en = p0->key_en;
    cvg_times_alpha = p0->cvg_times_alpha;
    alpha_cvg_select = p0->alpha_cvg_select;

    cc_x_sel_0_r = p0->cc_x_sel_0_r;
    cc_y_sel_0_r = p0->cc_y_sel_0_r;
    cc_a_sel_0_r = p0->cc_a_sel_0_r;
    cc_c_sel_0_r = p0->cc_c_sel_0_r;

    cc_x_sel_0_a = p0->cc_x_sel_0_a;
    cc_y_sel_0_a = p0->cc_y_sel_0_a;
    cc_a_sel_0_a = p0->cc_a_sel_0_a;
    cc_c_sel_0_a = p0->cc_c_sel_0_a;

    cc_x_sel_1_r = p0->cc_x_sel_1_r;
    cc_y_sel_1_r = p0->cc_y_sel_1_r;
    cc_a_sel_1_r = p0->cc_a_sel_1_r;
    cc_c_sel_1_r = p0->cc_c_sel_1_r;

    cc_x_sel_1_a = p0->cc_x_sel_1_a;
    cc_y_sel_1_a = p0->cc_y_sel_1_a;
    cc_a_sel_1_a = p0->cc_a_sel_1_a;
    cc_c_sel_1_a = p0->cc_c_sel_1_a;

    st_r = p0->st_r;
    st_g = p0->st_g;
    st_b = p0->st_b;
    st_a = p0->st_a;


    tf_r = p0->tf_r;
    tf_g = p0->tf_g;
    tf_b = p0->tf_b;
    tf_a = p0->tf_a;


    tf_lod_frac = p0->tf_lod_frac;

    prim_lod_frac = p0->prim_lod_frac;

    prim_r = p0->prim_r;
    prim_g = p0->prim_g;
    prim_b = p0->prim_b;
    prim_a = p0->prim_a;

    env_r = p0->env_r;
    env_g = p0->env_g;
    env_b = p0->env_b;
    env_a = p0->env_a;

    center_r = p0->center_r;
    center_g = p0->center_g;
    center_b = p0->center_b;

    scale_r = p0->scale_r;
    scale_g = p0->scale_g;
    scale_b = p0->scale_b;

    width_r = p0->width_r;
    width_g = p0->width_g;
    width_b = p0->width_b;

    k4_coeff = p0->k4_coeff;
    k5_coeff = p0->k5_coeff;

    noise = p0->noise;

    cvg = p0->cvg;

  }

  if(POSEDGE)
  {
    /* transfer all next-clock register values to register outputs. */
    *pp0 = p1; /* swap */
    *pp1 = p0;
    p0 = *pp0; /* fix pointers */
    p1 = *pp1;
  }

  if(POSEDGE)
  {
    /* Update all next-clock register values */
 
    /* 
     *  generate cycle signal 
     */
    p0->cycle = !((p1->cycle || st_span) && ncyc);
    p0->cycle_d1 = p1->cycle;
    p0->cycle_d2 = p1->cycle_d1;

    /* 
     *  Select mux selects 
     */
    if(p1->cycle)
    {
      p0->cc_x_sel_r = cc_x_sel_1_r;
      p0->cc_y_sel_r = cc_y_sel_1_r;
      p0->cc_a_sel_r = cc_a_sel_1_r;
      p0->cc_c_sel_r = cc_c_sel_1_r;
      p0->cc_x_sel_a = cc_x_sel_1_a;
      p0->cc_y_sel_a = cc_y_sel_1_a;
      p0->cc_a_sel_a = cc_a_sel_1_a;
      p0->cc_c_sel_a = cc_c_sel_1_a;
    }
    else
    {
      p0->cc_x_sel_r = cc_x_sel_0_r;
      p0->cc_y_sel_r = cc_y_sel_0_r;
      p0->cc_a_sel_r = cc_a_sel_0_r;
      p0->cc_c_sel_r = cc_c_sel_0_r;
      p0->cc_x_sel_a = cc_x_sel_0_a;
      p0->cc_y_sel_a = cc_y_sel_0_a;
      p0->cc_a_sel_a = cc_a_sel_0_a;
      p0->cc_c_sel_a = cc_c_sel_0_a;
    }
  
    /* 
     *  delay shade alpha, texture and lod_frac 
     */
    p0->st_a_d1 = st_a;
    p0->tf_r_d1 = tf_r;
    p0->tf_g_d1 = tf_g;
    p0->tf_b_d1 = tf_b;
    p0->tf_a_d1 = tf_a;
    p0->tf_lod_frac_d1 = tf_lod_frac;
    
    /*
     *  two cycle delayed lod_frac values
     */

    p0->tf_lod_frac_d2 = p1->tf_lod_frac_d1;

    /*
     *  Get CC RGB mux outputs
     */

    /* subtract A input */
    suba_r = sixteen_to_one(p1->cc_x_sel_r,
		p1->cc_r >> 8,
		p1->tf_r_d1,
		tf_r,
		prim_r,
		st_r,
		env_r,
		ONE_POINT_OH,
		noise,
		ZERO, ZERO, ZERO, ZERO,
		ZERO, ZERO, ZERO, ZERO);
    suba_g = sixteen_to_one(p1->cc_x_sel_r,
		p1->cc_g >> 8,
		p1->tf_g_d1,
		tf_g,
		prim_g,
		st_g,
		env_g,
		ONE_POINT_OH,
		noise,
		ZERO, ZERO, ZERO, ZERO,
		ZERO, ZERO, ZERO, ZERO);
    suba_b = sixteen_to_one(p1->cc_x_sel_r,
		p1->cc_b >> 8,
		p1->tf_b_d1,
		tf_b,
		prim_b,
		st_b,
		env_b,
		ONE_POINT_OH,
		noise,
		ZERO, ZERO, ZERO, ZERO,
		ZERO, ZERO, ZERO, ZERO);

    /* subtract B input */
    subb_r = sixteen_to_one(p1->cc_y_sel_r,
		p1->cc_r >> 8,
		p1->tf_r_d1,
		tf_r,
		prim_r,
		st_r,
		env_r,
		center_r,
		k4_coeff,
		ZERO, ZERO, ZERO, ZERO,
		ZERO, ZERO, ZERO, ZERO);
    subb_g = sixteen_to_one(p1->cc_y_sel_r,
		p1->cc_g >> 8,
		p1->tf_g_d1,
		tf_g,
		prim_g,
		st_g,
		env_g,
		center_g,
		k4_coeff,
		ZERO, ZERO, ZERO, ZERO,
		ZERO, ZERO, ZERO, ZERO);
    subb_b = sixteen_to_one(p1->cc_y_sel_r,
		p1->cc_b >> 8,
		p1->tf_b_d1,
		tf_b,
		prim_b,
		st_b,
		env_b,
		center_b,
		k4_coeff,
		ZERO, ZERO, ZERO, ZERO,
		ZERO, ZERO, ZERO, ZERO);
		
    /* multiplier input */
    mul_r = thirtytwo_to_one(p1->cc_a_sel_r, 
		p1->cc_r >> 8,
		p1->tf_r_d1,
		tf_r,
		prim_r,
		st_r,
		env_r,
		scale_r,
		p1->cc_a,
		p1->tf_a_d1,
		tf_a,
		prim_a,
		st_a,
		env_a,
		p1->tf_lod_frac_d1,
		prim_lod_frac,
		k5_coeff,
		ZERO, ZERO, ZERO, ZERO,
		ZERO, ZERO, ZERO, ZERO,
		ZERO, ZERO, ZERO, ZERO,
		ZERO, ZERO, ZERO, ZERO);
    mul_g = thirtytwo_to_one(p1->cc_a_sel_r, 
		p1->cc_g >> 8,
		p1->tf_g_d1,
		tf_g,
		prim_g,
		st_g,
		env_g,
		scale_g,
		p1->cc_a,
		p1->tf_a_d1,
		tf_a,
		prim_a,
		st_a,
		env_a,
		p1->tf_lod_frac_d1,
		prim_lod_frac,
		k5_coeff,
		ZERO, ZERO, ZERO, ZERO,
		ZERO, ZERO, ZERO, ZERO,
		ZERO, ZERO, ZERO, ZERO,
		ZERO, ZERO, ZERO, ZERO);
    mul_b = thirtytwo_to_one(p1->cc_a_sel_r, 
		p1->cc_b >> 8,
		p1->tf_b_d1,
		tf_b,
		prim_b,
		st_b,
		env_b,
		scale_b,
		p1->cc_a,
		p1->tf_a_d1,
		tf_a,
		prim_a,
		st_a,
		env_a,
		p1->tf_lod_frac_d1,
		prim_lod_frac,
		k5_coeff,
		ZERO, ZERO, ZERO, ZERO,
		ZERO, ZERO, ZERO, ZERO,
		ZERO, ZERO, ZERO, ZERO,
		ZERO, ZERO, ZERO, ZERO);
		
    /* adder input */
    add_r = eight_to_one(p1->cc_c_sel_r,
		p1->cc_r >> 8,
		p1->tf_r_d1,
		tf_r,
		prim_r,
		st_r,
		env_r,
		ONE_POINT_OH,
		ZERO);
    add_g = eight_to_one(p1->cc_c_sel_r,
		p1->cc_g >> 8,
		p1->tf_g_d1,
		tf_g,
		prim_g,
		st_g,
		env_g,
		ONE_POINT_OH,
		ZERO);
    add_b = eight_to_one(p1->cc_c_sel_r,
		p1->cc_b >> 8,
		p1->tf_b_d1,
		tf_b,
		prim_b,
		st_b,
		env_b,
		ONE_POINT_OH,
		ZERO);

  
    /*
     *  Get CC Alpha mux outputs
     */

    suba_a = eight_to_one(p1->cc_x_sel_a,
		p1->cc_a,
		p1->tf_a_d1,
		tf_a,
		prim_a,
		st_a,
		env_a,
		ONE_POINT_OH,
		ZERO);

    subb_a = eight_to_one(p1->cc_y_sel_a,
		p1->cc_a,
		p1->tf_a_d1,
		tf_a,
		prim_a,
		st_a,
		env_a,
		ONE_POINT_OH,
		ZERO);

    mul_a = eight_to_one(p1->cc_a_sel_a,
		p1->tf_lod_frac_d1,
		p1->tf_a_d1,
		tf_a,
		prim_a,
		st_a,
		env_a,
		prim_lod_frac,
		ZERO);

    add_a = eight_to_one(p1->cc_c_sel_a,
		p1->cc_a,
		p1->tf_a_d1,
		tf_a,
		prim_a,
		st_a,
		env_a,
		ONE_POINT_OH,
		ZERO);

  
    /*
     *  Do Linear Interpolator
     */
    p0->cc_r = do_rgb_lerp( suba_r, subb_r, mul_r, add_r);
    p0->cc_g = do_rgb_lerp( suba_g, subb_g, mul_g, add_g);
    p0->cc_b = do_rgb_lerp( suba_b, subb_b, mul_b, add_b);
    p0->cc_a = do_alpha_lerp( suba_a, subb_a, mul_a, add_a);
  
    /*
     *  latch output of sub a mux
     */
    p0->suba_r = suba_r; /* bypass keying s,8.0 */
    p0->suba_g = suba_g; /* bypass keying s,8.0 */
    p0->suba_b = suba_b; /* bypass keying s,8.0 */
  	
    /*
     *  Add->-abs(key') + width for keying, select min key' as key, clamp.
     */

 
    key_r = (-ABS(SIGN_EXTEND_17B(p1->cc_r))) + (width_r<<4);
    key_g = (-ABS(SIGN_EXTEND_17B(p1->cc_g))) + (width_g<<4);
    key_b = (-ABS(SIGN_EXTEND_17B(p1->cc_b))) + (width_b<<4);
    key   = MIN(MIN(key_r, key_g), key_b);
    key   = CLAMP(0, key, 0xff);
 
    /*
     *  Do coverage * alpha, texture edge stuff
     *  ORDER DEPENDENT SECTION
     */
  	
    key_mode = (key_en && p1->cycle_d2);

    clamp_a = akeley_clamp9b(SIGN_EXTEND_9B(p1->cc_a));
    if (clamp_a == 0xFF) clamp_a = 0x100;

    if(p1->alpha_dither_sel & 2)
    {
      if(p1->alpha_dither_sel & 1)
        dither = 0;
      else
        dither = ((noise >> 6) & 7);
    }
    else
    {
      if(p1->alpha_dither_sel & 1)
        dither = (~p1->alpha_dither) & 0x7;
      else
        dither = p1->alpha_dither;
    }

    dither_a = clamp_a + dither;
    dither_st_a = p1->st_a_d1 + dither;

    key_or_a = key_mode ? key : (dither_a & 0x100) ? 0xff : dither_a;
  

    pix_cvg       = (clamp_a * cvg + 4) >> 3;
    pix_cvg	  = cvg_times_alpha ? pix_cvg : (cvg << 5);
    p0->pixel_cvg = pix_cvg >> 5; /* 1.3 fixed point */
    if (pix_cvg > 0xFF) pix_cvg = 0xFF;
    p0->pixel_a   = alpha_cvg_select ? pix_cvg : key_or_a;
    
    /*
     *  Do bypass and clamp logic
     */
  
    p0->pixel_r = key_mode ? 
  	SIGN_EXTEND_9B(p1->suba_r) : SIGN_EXTEND_17B(p1->cc_r) >> 8;
    p0->pixel_g = key_mode ? 
  	SIGN_EXTEND_9B(p1->suba_g) : SIGN_EXTEND_17B(p1->cc_g) >> 8;
    p0->pixel_b = key_mode ? 
  	SIGN_EXTEND_9B(p1->suba_b) : SIGN_EXTEND_17B(p1->cc_b) >> 8;
  
    p0->pixel_r = akeley_clamp9b(p0->pixel_r);
    p0->pixel_g = akeley_clamp9b(p0->pixel_g);
    p0->pixel_b = akeley_clamp9b(p0->pixel_b);

    p0->shade_a = (dither_st_a & 0x100) ? 0xff : dither_st_a;

  } /* posedge */

  /* save last clock state */
   p0->gclk_old = p1->gclk_old = save_clk;
}
  
  
  
  
/*
 *  cc_init():
 *
 */
cc_init(cc_t *p0, cc_t *p1)
{
  p0->gclk = p1->gclk = 0;
  p0->gclk_old = p1->gclk_old = 0;
}