ew.c 29.1 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
/**************************************************************************
 *                                                                        *
 *               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.  *
 *                                                                        *
 **************************************************************************/

/*
 *  Edge Walker Unit
 *
 *
 *  RJM 8/13/94
 */

#include <stdio.h>
#include <stdlib.h>
#include <bstring.h>

#include "ints.h"
#include "ew.h"


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


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

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

#define SIGN_EXTEND_27(x)	(((x) & 0x4000000) ? (x) | ~0x3ffffff : (x))
#define SIGN_EXTEND_12(x)	(((x) & 0x800) ? (x) | ~0xfff : (x))
#define SIGN_EXTEND_14(x)	(((x) & 0x2000) ? (x) | ~0x3fff : (x))


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

/* Debugging/checking */
static int ew_dump;

/* edge walker memory */
static unsigned int EwMemoryA[9]; /* attribute */
static unsigned int EwMemoryB[9]; /* da/de */

static int Trigger = 0;

/* =========================================================================
 *  An 18-word by 32-bit register file.  Two writes at the same address
 *  at the same time is not allowed.  Read and write to the same address at 
 *  the same time is ?not allowed?  The da/de memory can write data directly
 *  to the read register.
 * =========================================================================
 */
static void
  ew_reg_file( ew_t *p0, ew_t *p1, 
	       int ld_a, int attr_adrs, int attr_we, 
               int de_adrs, int de_we,
               int64 cs_ew_d)
{
  if(attr_we)
    EwMemoryA[attr_adrs] = ld_a ? cs_ew_d.word1 : p1->sum_att;

  if(de_we)
    EwMemoryB[de_adrs] = cs_ew_d.word1;

  if((p1->cnt_span_attr == de_adrs) && de_we)
    p0->rb_data = p1->ew_stall_attr ? p1->rb_data : cs_ew_d.word1;
  else
    p0->rb_data = p1->ew_stall_attr ? p1->rb_data : EwMemoryB[p1->cnt_span_attr];

  p0->ra_data = p1->ew_stall_attr ? p1->ra_data : EwMemoryA[p1->cnt_span_attr];
}

/* =========================================================================
 *  ew_attr_adder - two 16-bit adders that step attributes along attribute
 *  edge.  This function includes the sum register.
 *  
 * =========================================================================
 */
static void
  ew_attr_adder(ew_t *p0, ew_t *p1)
{
  unsigned int in_attr, in_dade;
  unsigned int sum_ls, sum_ms;
  unsigned int ms_ci, ls_ci;
  unsigned int ms_co, ls_co;

  in_attr = p1->ra_data;
  in_dade = p1->rb_data;

  /* do LS adder */
  ls_ci  = p1->ls_co && !p1->add_clear;
  sum_ls = (in_attr & 0xffff) + (in_dade & 0xffff) + ls_ci;
  ls_co  = (sum_ls & 0x10000) >> 16;
  p0->ls_co = p1->ew_stall_attr ? p1->ls_co : ls_co;
  
  /* do MS adder */
  ms_ci = p1->add32b ? ls_co : p1->ms_co && !p1->add_clear;
  sum_ms = ((in_attr >> 16) & 0xffff) + ((in_dade >> 16) & 0xffff) + ms_ci; 
  ms_co = (sum_ms & 0x10000) >> 16;
  p0->ms_co = p1->ew_stall_attr ? p1->ms_co : ms_co;

  /* save sum */
  p0->sum_att = p1->ew_stall_attr ? p1->sum_att : ((sum_ms & 0xffff) << 16) | (sum_ls & 0xffff);
}


/* =========================================================================
 *  ew_shuffle - swaps integer and fraction to make complete word (16.16)
 *   
 * =========================================================================
 */
static void
  ew_shuffle(ew_t *p0, ew_t *p1)
{
  int int_shuffle, frac_shuffle;
  int result;

  p0->inta_1delay = (p1->ra_data >> 16) & 0xffff;
  p0->fraca_1delay = p1->ra_data & 0xffff;
  p0->fraca_2delay = p1->fraca_1delay;

  p0->intd_1delay = (p1->rb_data >> 16) & 0xffff;
  p0->fracd_1delay = p1->rb_data & 0xffff;
  p0->fracd_2delay = p1->fracd_1delay;

  int_shuffle = p1->shuffle ? p1->fraca_1delay : (p1->ra_data >> 16) & 0xffff;
  frac_shuffle = p1->shuffle ? p1->fraca_2delay : p1->inta_1delay;
  result = (p1->noshuffle ? p1->inta_1delay : int_shuffle) << 16;
  result |= p1->noshuffle ? p1->fraca_1delay : frac_shuffle;
  p0->att_out_s = result;
  p0->att_out   = p1->att_out_s >> 9; /* reduce to s,15.7 */

  int_shuffle = p1->shuffle ? p1->fracd_1delay : (p1->rb_data >> 16) & 0xffff;
  frac_shuffle = p1->shuffle ? p1->fracd_2delay : p1->intd_1delay;
  result = (p1->noshuffle ? p1->intd_1delay : int_shuffle) << 16;
  result |= p1->noshuffle ? p1->fracd_1delay : frac_shuffle;
  p0->dade_out_s = result;
  p0->dade_out   = p1->dade_out_s >> 9; /* reduce to s,15.7 */
}


/* ======================================================================
 *  ew_offset():
 *
 *  dade_out, att_out, dx_att, dy_att - s,15.7
 *  xfrac - 0.8
 *   
 *  s,15.7 * .8 = s,15.15
 *  
 *  Offset attribute as follows: A = A + 3/4(da/de - da/dy) - dx * xfrac;
 *  
 *
 * ======================================================================
 */

#define SIGN_EXTEND_22(x)	(((x) & 0x400000) ? ((x) | ~0x7fffff) : (x))

static void
    ew_offset(ew_t *p0, ew_t *p1, int dx_att, int dy_att)
{
  int x_frac_ss, do_offset, de_offset, dy_offset;
  int att;

  p0->x_frac_s = p1->ld_x_frac ? p1->x_frac : p1->x_frac_s;

  x_frac_ss = p1->x_frac_s & (!(p1->cycle_type == 2 || p1->load_cmd_offset) ? 0xff : 0x00);
  
  do_offset = !(p1->sign_dxhdy_offset ^ p1->left_offset);
  de_offset =  do_offset ? p1->dade_out : 0x0;
  dy_offset =  do_offset ? dy_att : 0x0;

  de_offset = SIGN_EXTEND_22(de_offset);
  dy_offset = SIGN_EXTEND_22(dy_offset);
  att       = SIGN_EXTEND_22(p1->att_out);
  dx_att    = SIGN_EXTEND_22(dx_att);

  /* result is s,15.15 */
  p0->att_d_out = (att << 8) + 
	 (de_offset << 7) + (de_offset << 6) -
	 (dy_offset << 7) - (dy_offset << 6) -
	 (dx_att * x_frac_ss);
#ifdef DUMB
  if(p1->cnt_span_attr == 5 || p1->cnt_span_attr == 6 || p1->cnt_span_attr == 7
       || p1->cnt_span_attr == 8)
  {
    printf("cnt_attr: %d  ew_ep_d = %08x  att_out = %08x att = %08x, dx_att = %08x, de_off = %08x, de_y = %08x, x_frac = %02x\n", p1->cnt_span_attr, p0->ew_ep_d, p0->att_d_out, att, dx_att, de_offset, dy_offset, x_frac_ss);
  }
#endif
  p0->ew_ep_d = (p1->att_d_out >> 9) & 0x3fffff; /* s,15.6 */

}


/* ======================================================================
 *  ew_x_calc():
 *
 *  Compute xh0, xh1, xh2, xh3, xm0, xm1, xm2, xm3
 *  for each span in a triangle or rectangle.
 *
 * ======================================================================
 */

static void
    ew_x_calc(ew_t *p0, ew_t *p1, int ld_xmh, int switch_xl, int ld_dxmdy, 
           int ld_dxldy, int ld_dxhdy, int64 cs_ew_d)
{
  int dxmdy, dxhdy, xm, xh, sum;

  dxmdy = ld_xmh ? ((cs_ew_d.word0 >> 3) &  0x7ffffff) : p1->dxhdy;
  dxhdy = switch_xl ? p1->dxldy : p1->dxmdy;

  sum = SIGN_EXTEND_27(p1->dxhdy) + SIGN_EXTEND_27(p1->xh); 

  xm = ld_xmh ? (cs_ew_d.word1 >> 1) & 0x7ffffff : sum & 0x7ffffff;
  xh = switch_xl ? p1->xl : p1->xm;

  p0->dxmdy = ld_dxmdy ? dxmdy : p1->dxmdy;
  p0->dxldy = ld_dxldy ? (cs_ew_d.word0 >> 3) & 0x7ffffff : p1->dxldy;
  p0->dxhdy = ld_dxhdy ? dxhdy : p1->dxhdy;
  p0->xm = ld_dxmdy ? xm : p1->xm;
  p0->xl = ld_dxldy ? (cs_ew_d.word1 >> 1) & 0x7ffffff : p1->xl;
  p0->xh = ld_dxhdy ? xh : p1->xh;
  p0->x_sticky = (p0->xh & 0x1fff) > 0;

}


/* ======================================================================
 *  ew_scissor_y(): counts and scissors y coordinate
 *
 * ======================================================================
 */
static void
    ew_scissor_y(ew_t *p0, ew_t *p1, int ld_y, int count_y, int *end_prim_y, int64 cs_ew_d)
{
  int yl_ismin_scymax;
  int min_yl_scymax;
  int yh_ismax;
  int max_yh_scmin;
  int xval0, xval1, xval2, xval3;
  int x_invalid;

  p0->ym = ld_y ? (cs_ew_d.word0 >> 16) & 0x3fff : p1->ym;
  p0->yl = ld_y ? cs_ew_d.word1 & 0x3fff : p1->yl;
  p0->yh = ld_y ? cs_ew_d.word0 & 0x3fff : p1->yh;

  /* increment y counter */
  p0->y_cur = ld_y ? cs_ew_d.word0 & 0x3ffc : p1->y_cur + count_y;

  /* min(yl, scbox_ymax) to determine end of primitive */
  if(p1->yl & 0x2000)
    yl_ismin_scymax = 1;
  else if(p1->yl & 0x1000)
    yl_ismin_scymax = 0;
  else
    yl_ismin_scymax = (p1->yl & 0xfff) < p1->scbox_ymax;

  /* Terminate primitive if current y >= min(yl, scbox_ymax) */
  min_yl_scymax = (yl_ismin_scymax || p1->load_cmd_scissor) ? p1->yl : p1->scbox_ymax & 0xfff;
  *end_prim_y = (SIGN_EXTEND_14(p1->y_cur) >= SIGN_EXTEND_14(min_yl_scymax)) || p1->flush;

  /* cancel the span if current y < max(scbox_ymin, yh) */
  if(p1->yh & 0x2000)
    yh_ismax = 0;
  else if (p1->yh & 0x1000)
    yh_ismax = 1;
  else
    yh_ismax = p1->yh >= p1->scbox_ymin;

  max_yh_scmin = (yh_ismax || p1->load_cmd_scissor) ? p1->yh : p1->scbox_ymin & 0xfff;
  x_invalid = SIGN_EXTEND_14(p1->y_cur) < SIGN_EXTEND_14(max_yh_scmin);
  p0->y_invalid = x_invalid || (*end_prim_y); /* used by ew controlto clear x_minor */

  /* get xval */
  xval0 = p1->xval & 1;
  xval1 = (p1->xval >> 1) & 1;
  xval2 = (p1->xval >> 2) & 1;
  xval3 = (p1->xval >> 3) & 1;
 
  p0->xval = (p1->cnt_span_x & 1) ? !((*end_prim_y) || x_invalid) : xval0;
  p0->xval |= ((p1->cnt_span_x & 1) ? xval0 : xval1) << 1;
  p0->xval |= ((p1->cnt_span_x & 1) ? xval1 : xval2) << 2;
  p0->xval |= ((p1->cnt_span_x & 1) ? xval2 : xval3) << 3;
}


/* ======================================================================
 *  ew_scissor_x():
 *
 *  Scissor X coordinates
 * 
 *  Scissored x, x_sc, should only use 13 LSBs outside this function.
 * ======================================================================
 */

static void
    ew_scissor_x(ew_t *p0, ew_t *p1, int ld_xmajor, int clear_allxgemax)
{
  int x_unsc;			/* s,11.8 */
  int less_xmin; 
  int xsc_scmin; 		/*   11.2,sticky */
  int allxlmin;
  int ge_xmax; 
  int xsc_scmax;
  int allxgemax;
  int x_sc_maxnew; 
  int x_sc_max;
  int x_sc_minnew; 
  int x_sc_min;
  int xsc;
  int xsc_m;

  /*
   *  scissor with scbox_xmin, max(scbox_xmin, x_in)
   *  p1->xh is s,11.15
   */
  x_unsc = (p1->xh >> 7) & 0xfffff;
 
  if(x_unsc & 0x80000)  /* x negative, out of scissor box */
    less_xmin = 1;
  else if(x_unsc & 0x40000) /* x out of maximum positive scissor box */
    less_xmin = 0;
  else  /* check against scissor box min (10.2) */
    less_xmin = (((x_unsc & 0x3ffc0) >> 5) | p1->x_sticky) < (p1->scbox_xmin << 1);

  xsc_scmin = less_xmin ? (p1->scbox_xmin << 1) & 0x1ffe : 
	(((x_unsc & 0x7ffc0) >> 5) | p1->x_sticky);
  allxlmin = (p1->allxlmin || clear_allxgemax) && less_xmin && !p1->load_cmd_scissor;

  /*
   *  scissor with scbox_xmax, min(scbox_xmax, xsc_scmin)
   *  know that xsc_scmin is positive, 11.2,sticky.
   */
  if(xsc_scmin & 0x2000)
    ge_xmax = 1;
  else
    ge_xmax = (xsc_scmin & 0x1fff) >= (p1->scbox_xmax << 1);

  xsc_scmax = ge_xmax ? (p1->scbox_xmax << 1) & 0x1ffe : xsc_scmin;
  allxgemax = (p1->allxgemax || clear_allxgemax) && ge_xmax && !p1->load_cmd_scissor;

  xsc = p1->load_cmd_scissor ? ((x_unsc & 0xfffc0) >> 5) : xsc_scmax;
  /* xsc_m = (p1->cnt_span_x & 8) ? p1->xval & 0xf : xsc; */
  
  p0->allxlmin = p1->ew_stall_x ? p1->allxlmin : allxlmin;
  p0->allxgemax = p1->ew_stall_x ? p1->allxgemax : allxgemax;

  p0->x_sc = p1->ew_stall_x ? p1->x_sc : xsc;
  p0->ew_cv_d = p1->sel_xval ? p1->xval & 0xf : p1->x_sc & 0x1fff;

  if (ew_dump&0x1) {
    static int dump_num=0,dump_last=0,dump_stall=0,dump_yval;
    static int dump_x[8] = 
	{ 0xDEAD, 0xDEAD, 0xDEAD, 0xDEAD, 0xDEAD, 0xDEAD, 0xDEAD, 0xDEAD };
    dump_num++;
    if (p1->cnt_span_x >0) {
      dump_x[p1->cnt_span_x-1]=p0->ew_cv_d;
      dump_last=0;
      if (p1->cnt_span_x==1) {
        dump_stall++;
        dump_yval=p1->y_cur;
	if (dump_yval & 0x2000) dump_yval |= ~0x3FFF;
      }
    } else {
      if (!dump_last) {
        if (!p1->sel_xval) 
          printf("ew: {FUNNY:sel_xval!=0 when cnt_span==0}");
        printf("ew[%d]: ",dump_num);
	if (dump_stall>2) printf("S ");
	else printf("n ");
	dump_stall=0;
        printf("ycur=%04i.%X   ",dump_yval>>2, (dump_yval&0x3)<<2);
        printf("x0h-x1m:  ");
        printf("%04i(%i)_%04i(%i)  ",(int) (dump_x[0]>>3),(int) (dump_x[0]&7),(int) (dump_x[1]>>3),(int) (dump_x[1]&7));
        printf("%04i(%i)_%04i(%i)  ",(int) (dump_x[2]>>3),(int) (dump_x[2]&7),(int) (dump_x[3]>>3),(int) (dump_x[3]&7));
        printf("%04i(%i)_%04i(%i)  ",(int) (dump_x[4]>>3),(int) (dump_x[4]&7),(int) (dump_x[5]>>3),(int) (dump_x[5]&7));
        printf("%04i(%i)_%04i(%i)  ",(int) (dump_x[6]>>3),(int) (dump_x[6]&7),(int) (dump_x[7]>>3),(int) (dump_x[7]&7));
        printf("val=%X  ",p0->ew_cv_d);
        printf("\n");
        dump_last=1;
      }
    }
  }

  p0->x_major_1d = ld_xmajor ? x_unsc : p1->x_major_1d;
  p0->ew_cv_start_x = (p0->x_major_1d >> 8) & 0xfff; /* no frac bits needed */
  p0->x_frac_m = p1->x_major_1d & 0xff;
  p0->x_frac_mm = p1->x_frac_m;
  p0->x_frac = p1->x_frac_mm;

  /*
   *  Find the scissored x_max(x_right) x_min(x_left)
   *  These values are used in the memory span unit to calculate span address
   */
  if(p1->reset_l == 0)
  {
    p0->x_sc_max = 0;
    p0->x_sc_min = 0;
  }
  else
  {
    /* Compute x_max with no fraction bits */
    x_sc_maxnew = !p1->clear_xminor ? p1->x_sc_max : 0;
    x_sc_max = (((p1->x_sc >> 3) & 0xfff) >= x_sc_maxnew) && !p1->y_invalid ? 
        (p1->x_sc >> 3) & 0xfff : x_sc_maxnew;
    p0->x_sc_max = p1->ew_stall_x ? p1->x_sc_max : x_sc_max;

    /* compute x_min with no fraction bits */
    x_sc_minnew = p1->clear_xminor ? 0xfff : p1->x_sc_min;
    x_sc_min = (((p1->x_sc >> 3) & 0xfff) < x_sc_minnew) && !p1->y_invalid ? 
       (p1->x_sc >> 3) & 0xfff : x_sc_minnew;
    p0->x_sc_min = p1->ew_stall_x ? p1->x_sc_min : x_sc_min;
  } 

  /* mux for x_sc_major, x_minor not implemented here, were used? */ 
}


/* ======================================================================
 *  ew_ms_adrs():
 *
 *  Calculate addresses for span, to mem span unit
 * 
 *  
 * ======================================================================
 */
static void
  ew_ms_adrs( ew_t *p0, ew_t *p1)
{
  int mult;
  int add_zero;
  int sel_mult;
  int ld_addr;
  int sel_addr;
  int yh_cur;

  int a_adder;
  int b_adder_m;
  int b_adder;
  int sum;
  int a_adder_m;
  int b_adder_a;

  int posy_sel;
  int ycur;

  ycur = p1->y_cur >> 2; /* s,11.0 */

  if(p1->reset_l == 0)
  {
    p0->cnt_mult = 0;
    p0->cnt_addr = 0;
    p0->posy_state = 0;
  }
  else
  {
    p0->cnt_mult = (p1->cnt_prim == 2) || !((p1->cnt_mult == 0xa) || (p1->cnt_mult == 0x0)) ?
       p1->cnt_mult + 1 : 0;
    p0->cnt_addr = (((p1->cnt_span_x == 8)) || ((p1->cnt_addr & 0x3) != 0)) ? p1->cnt_addr + 1 : 0;

    posy_sel = ((p1->cnt_prim == 2) << 3) | ((p1->cnt_span_x == 8) << 2) | ((!((ycur & 0x800) ||
	(ycur & 0x400))) << 1) | p1->posy_state;

    switch(posy_sel)
    {
      case 0x0:
      case 0x2:
      case 0x4:
      case 0x8:
      case 0x9:
      case 0xc:
      case 0xd: p0->posy_state = 0; break;
      default:  p0->posy_state = 1; break;
    }

  }

  switch(p1->cnt_mult)
  {
     case 0x1: yh_cur = ycur & 0x200; break;
     case 0x2: yh_cur = ycur & 0x100; break;
     case 0x3: yh_cur = ycur & 0x080; break;
     case 0x4: yh_cur = ycur & 0x040; break;
     case 0x5: yh_cur = ycur & 0x020; break;
     case 0x6: yh_cur = ycur & 0x010; break;
     case 0x7: yh_cur = ycur & 0x008; break;
     case 0x8: yh_cur = ycur & 0x004; break;
     case 0x9: yh_cur = ycur & 0x002; break;
     case 0xa: yh_cur = ycur & 0x001; break;
     default:  yh_cur = ycur & 0x001; break;
  }

  mult = (p1->cnt_mult != 0);
  add_zero = (ycur & 0x800) || (ycur & 0x400) || !yh_cur;
  sel_mult = (p1->cnt_mult != 0x0) && (p1->cnt_mult != 0x1);
  ld_addr = (p1->cnt_mult != 0x0) || (p1->cnt_addr == 0x3);
  sel_addr = p1->posy_state ? p1->cnt_addr : 0;


  if(p1->reset_l == 0)
  {
    p0->addr_scanline = 0;
  }
  else
  {
    a_adder_m = add_zero ? 0 : (p1->load_cmd_image ? p1->width_timage : p1->width_cimage);
    a_adder   = mult ? a_adder_m : p1->addr_scanline;
    b_adder_m = sel_mult ? (p1->addr_scanline & 0x7ffff) << 1 : 0;
    switch (sel_addr)
    {
         case 0: b_adder_a = 0; break;
         case 1: b_adder_a = p1->left_xminor ? p1->x_sc_min : p1->x_sc_max; break;
         case 2: b_adder_a = p1->left_xminor ? p1->x_sc_max : p1->x_sc_min; break;
         case 3: b_adder_a = (p1->load_cmd_image ? p1->width_timage : p1->width_cimage); break;
    }

    b_adder = mult ? b_adder_m : b_adder_a;
    sum = a_adder + b_adder + (1 & ((mult && !add_zero) || (p1->posy_state && ((p1->cnt_addr == 3) && !mult))));

    p0->addr_scanline = ld_addr  ? sum : p1->addr_scanline;
    p0->ew_ms_addr    = sum;
  }

#ifdef DEBUG
  {
    static ew_debug_count=0;
    fprintf(stderr,"ew[%d]: addr_scanline=0x%X ew_ms_addr=0x%X sum=%X a_adder=%X b_adder=%X mult=%X add_zero=%X sel_addr=%X cnt_mult=%X cnt_addr=%X ycur=%X\n",
        ew_debug_count++,p0->addr_scanline,p0->ew_ms_addr,sum,a_adder,b_adder,mult,add_zero,sel_addr,p1->cnt_mult,p1->cnt_addr,ycur);
    fprintf(stderr,"   cnt_prim=%X  cnt_span_x=%X  posy_state%X\n",p1->cnt_prim,p1->cnt_span_x,p1->posy_state);
  }
#endif /* DEBUG */

}




/* ======================================================================
 *  ew():  main interface for the edge walker unit.
 *
 *  WARNING:  This code is order dependent.  Be careful when rearranging
 *  code sections.
 * ======================================================================
 */
void
  ew(ew_t **pp0, ew_t **pp1)
{
  ew_t *p0, *p1;
  int save_clk, save_clk_old;

  /* intermediate variables */
  int ld_y;
  int count_y;
  int end_prim_y;

  int span_valid;
 
  int ld_xmh;
  int ld_dxmdy;
  int ld_dxhdy;
  int ld_dxldy;
  int switch_xl;
  int end_x;
 
  int ld_xmajor;
  int clear_allxgemax;

  int ld_a;
  int attr_adrs; 
  int attr_we;
  int de_adrs;
  int de_we;

  int dx_att;
  int dy_att;

  int get_ew_stall;
  int larger_x;
  int smaller_x;
  int num_shift;
  int num_pixel;
  int num_cycles_new;
  int num_cycles;
  int comp_value;
  int stall_decr;

  /* 
   *  These inputs come from pins which have logic between the output register
   *  and the pin.  So we re-grab these inputs on the non-used (negative) edge
   *  of the clock, and recalculate the module.
   */
  int64 cs_ew_d;
  int   cs_ew_newprim;

  /* get initial pointers */
  p0 = *pp0;
  p1 = *pp1;
  save_clk = p0->gclk;
  save_clk_old = p1->gclk_old;

  
  if(NEGEDGE)
  {
    cs_ew_d = p0->cs_ew_d;
    cs_ew_newprim = p0->cs_ew_newprim;
  }

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

  if(POSEDGE || NEGEDGE)
  {
    /* Update all next-clock register values */


    /* controls for Y's */
    ld_y = p1->cnt_prim == 1;
    count_y = ((p1->cnt_span_x == 1) || (p1->cnt_span_x == 3) ||
                (p1->cnt_span_x == 5) || (p1->cnt_span_x == 8)) &&
	      !( (p1->cnt_prim == 1) || (p1->cnt_prim == 2) ||
		 (p1->cnt_prim == 3) || (p1->cnt_prim == 4)) &&
              !p1->ew_stall_x && p1->span_valid_x;

    /*
     *   Scissor Y coordinates
     *
     */

    ew_scissor_y(p0, p1, ld_y, count_y, &end_prim_y, cs_ew_d);

    /*
     *   Edge Walker Control, derive control signals before
     *   calling other units.
     */
    if(p1->reset_l == 0)
    {
      p0->cnt_prim = 0;
      p0->cnt_span_x = 0;
      p0->cnt_span_attr = 0;
      p0->end_ew = 0;
      p0->span_valid_s = 0;
      p0->span_valid_x = 0;
    }
    else
    {
      if(cs_ew_newprim && (p1->cnt_prim == 0x16))
        p0->cnt_prim = 1;
      else
        p0->cnt_prim = (!cs_ew_newprim) ? 0 : (p1->cnt_prim + cs_ew_newprim);

      p0->cnt_span_x = ((p1->cnt_span_x == 0x8) || (p1->cnt_prim == 0x4)) ? 0 :
	  (p1->cnt_span_x + (!p1->ew_stall_x));
      p0->cnt_span_attr = ((p1->cnt_span_attr == 0x8) || (p1->cnt_prim == 0xd)) ? 0 :
	  (p1->cnt_span_attr + (!p1->ew_stall_attr));

      p0->end_ew = end_prim_y && (p1->cnt_span_x == 8);

      p0->span_valid_x = (p1->cnt_prim == 0x4) || (p1->span_valid_x && 
			   !(end_prim_y && (p1->cnt_span_x == 0x8)));
      p0->span_valid_x_s = p1->span_valid_x;
      p0->sc_field_s = (p1->cnt_prim == 0xc) ? p1->sc_field : p1->sc_field_s;
      p0->odd_line_s = (p1->cnt_prim == 0xc) ? p1->odd_line : p1->odd_line_s;
    }

    /* general control */
    span_valid = p1->span_valid_x && !p1->flush && (p1->xval != 0) && 
	  !p1->allxlmin && !p1->allxgemax &&
	  (!p1->sc_field_s || (p1->load_cmd_scissor & 1) || (p1->sc_field_s && 
	    !(((p1->y_cur & 0x4)>>2) ^ p1->odd_line_s)));


    if(p1->reset_l)
      p0->span_valid_s = span_valid;
    p0->ew_cv_newspan = (p1->cnt_span_x == 8) && span_valid;
    p0->ew_ep_startspan_m = p1->ew_cv_newspan;
    p0->ew_ep_startspan = p1->ew_ep_startspan_m;

    /* controls for X's */
    ld_xmh = (p1->cnt_prim == 3) || (p1->cnt_prim == 4);
    ld_dxmdy = (!(p1->cnt_span_x == 0x7) && !p1->ew_stall_x) || ld_xmh;
    ld_dxhdy = ld_dxmdy;

    /**** DEBUG ****/
    p0->ld_dxhdy = ld_dxhdy;

    ld_dxldy = (p1->cnt_prim == 2);
    switch_xl = (p1->y_cur == p1->ym) && !(p1->cnt_span_x & 1) && !(p1->cnt_span_x & 8) && !ld_xmh;
    end_x = (p1->cnt_span_x == 8);

    /*
     *   EW X coordinate Calculation.  Computes four X values, one for each
     *   subpixel Y increment, for the left and right sides of a span.
     */

    ew_x_calc(p0, p1, ld_xmh, switch_xl, ld_dxmdy, ld_dxldy, ld_dxhdy, cs_ew_d);

    /* controls for X scissor */
    ld_xmajor = (p1->left_xmajor ^ p1->sign_dxhdy_xmajor) ? (p1->cnt_span_x == 0) : (p1->cnt_span_x == 6);
    clear_allxgemax = (p1->cnt_span_x == 0);
    p0->clear_xminor = p1->ew_stall_x ? p1->clear_xminor : (p1->cnt_span_x == 0);

    /*
     *   Scissor X coordinates
     *
     */

    ew_scissor_x(p0, p1, ld_xmajor, clear_allxgemax);

    /*
     *  Register control signals 
     */
    if(p1->reset_l == 0)
    {
      /* generate ew_stall_attr */
      p0->ew_stall_attr0d = 0;
      p0->ew_stall_attr1d = 0;
      p0->ew_stall_attr2d = 0;
      p0->ew_stall_attr3d = 0;
      p0->ew_stall_attr4d = 0;
      p0->ew_stall_attr5d = 0;
      p0->ew_stall_attr6d = 0;
      p0->ew_stall_attr7d = 0;
      p0->ew_stall_attr8d = 0;
      p0->ew_stall_attr   = 0;
      p0->sel_xval = 0;
    }
    else /* no reset_l */
    {
      p0->sel_xval = (p1->cnt_span_x & 0x8) == 0x8;
      p0->wa_addr_m = p1->ew_stall_attr ? p1->wa_addr_m : p1->cnt_span_attr;
      p0->wa_addr_s = p1->ew_stall_attr ? p1->wa_addr_s : p1->wa_addr_m;
      p0->add_clear = p1->ew_stall_attr ? p1->add_clear : 
	!(p1->cnt_span_attr & 1);
      p0->add32b = p1->ew_stall_attr ? p1->add32b : (p1->cnt_span_attr & 8) == 8;
      p0->shuffle_m = p1->ew_stall_attr ? p1->shuffle_m : p1->cnt_span_attr & 1;
      p0->shuffle = p1->shuffle_m;
      p0->noshuffle_m = p1->ew_stall_attr ? p1->noshuffle_m : (p1->cnt_span_attr & 8) == 8;
      p0->noshuffle = p1->noshuffle_m;
      p0->sel_dydx_m = p1->ew_stall_attr ? p1->sel_dydx_m : p1->cnt_span_attr;
      p0->sel_dydx_mm = p1->sel_dydx_m;
      p0->sel_dydx_mmm = p1->sel_dydx_mm;
      p0->sel_dydx      = p1->sel_dydx_mmm;
      p0->ld_x_frac_m = p1->cnt_span_x == 8;
      p0->ld_x_frac_mm = p1->ld_x_frac_m;
      p0->ld_x_frac_mmm = p1->ld_x_frac_mm;
      p0->ld_x_frac      = p1->ld_x_frac_mmm;
  
      /* generate ew_stall_attr */
      p0->ew_stall_attr0d = p1->ew_stall_x;
      p0->ew_stall_attr1d = p1->ew_stall_attr0d;
      p0->ew_stall_attr2d = p1->ew_stall_attr1d;
      p0->ew_stall_attr3d = p1->ew_stall_attr2d;
      p0->ew_stall_attr4d = p1->ew_stall_attr3d;
      p0->ew_stall_attr5d = p1->ew_stall_attr4d;
      p0->ew_stall_attr6d = p1->ew_stall_attr5d;
      p0->ew_stall_attr7d = p1->ew_stall_attr6d;
      p0->ew_stall_attr8d = p1->ew_stall_attr7d;
      p0->ew_stall_attr = p1->ew_stall_attr7d;
    }

    /* ew offset control */
    ld_a = (p1->cnt_prim == 0x5) || (p1->cnt_prim == 0x6) ||
	   (p1->cnt_prim == 0x7) || (p1->cnt_prim == 0x8) ||
	   (p1->cnt_prim == 0x9) || (p1->cnt_prim == 0xa) || 
           (p1->cnt_prim == 0xb) || (p1->cnt_prim == 0xc) ||
	   (p1->cnt_prim == 0xd);


    attr_adrs = ld_a ? (p1->cnt_prim - 0x5) & 0xf : p1->wa_addr_s;
    de_adrs   = (p1->cnt_prim - 0xe) & 0xf;
    de_we     = (p1->cnt_prim == 0xe)  || (p1->cnt_prim == 0xf)  ||
		(p1->cnt_prim == 0x10) || (p1->cnt_prim == 0x11) ||
		(p1->cnt_prim == 0x12) || (p1->cnt_prim == 0x13) ||
		(p1->cnt_prim == 0x14) || (p1->cnt_prim == 0x15) ||
		(p1->cnt_prim == 0x16);
    attr_we   = !((p1->cnt_prim == 0xe) || (p1->cnt_prim == 0xf)) &&
		 (!p1->ew_stall_attr);
    
    /*
     *  EW Attribute Offset Data Path
     */
    ew_reg_file( p0, p1, ld_a, attr_adrs, attr_we, de_adrs, de_we, cs_ew_d );
    
    ew_attr_adder( p0, p1 );

    ew_shuffle( p0, p1 );


    /* select dx/dy attribute */
    switch(p1->sel_dydx)
    {
      case 0: dx_att = p1->dxw; dy_att = p1->dyw; break;
      case 1: dx_att = p1->dxl; dy_att = p1->dyl; break;
      case 2: dx_att = p1->dxs; dy_att = p1->dys; break;
      case 3: dx_att = p1->dxt; dy_att = p1->dyt; break;
      case 4: dx_att = p1->dxr; dy_att = p1->dyr; break;
      case 5: dx_att = p1->dxg; dy_att = p1->dyg; break;
      case 6: dx_att = p1->dxb; dy_att = p1->dyb; break;
      case 7: dx_att = p1->dxa; dy_att = p1->dya; break;
      case 8:
      default: dx_att = p1->dxz; dy_att = p1->dyz; break;
    }

    ew_offset(p0, p1, dx_att, dy_att);

    /*
     *  Edge Walker Stall Logic
     */
    p0->get_new_stall = ((p1->cnt_span_x & 8) == 8) && span_valid;
    get_ew_stall = p1->get_new_stall || p1->ew_stall_x;

    if(p1->reset_l == 0)
    {
      p0->ew_stall_x = 0;
      p0->cnt_stall = 0;
      p0->end_prim_state = 0;
    }
    else
    {
      larger_x = p1->left_xminor ? p1->x_sc_max : (p1->x_major_1d >> 8);
      smaller_x = p1->left_xminor ? (p1->x_major_1d >> 8) : p1->x_sc_min;
      num_pixel = SIGN_EXTEND_12(larger_x) - SIGN_EXTEND_12(smaller_x);
      p0->ew_ms_length = num_pixel;

      if(!p1->load_cmd_ewstall)
      {
	switch(p1->cycle_type)
	{
	  case 0: num_shift = 0; comp_value = 8; break;
	  case 1: num_shift = 1; comp_value = 6; break;
	  case 2: num_shift = 2; comp_value = 7; break;
	  case 3: 
            comp_value = 7;
	    switch(p1->pixel_size)
	    {
	      case 0: num_shift = 4; break;
	      case 1: num_shift = 3; break;
	      case 2: num_shift = 2; break;
	      case 3: num_shift = 1; break;
	    }
	    break;
        }
      }
      else
      {
        if(p1->load_cmd_tlut)
	{
	  comp_value = 8;
          num_shift = 0;
	}
        else
        {
          comp_value = 7;
	  switch(p1->texel_size)
	  {
	    case 0: num_shift = 4; break;
	    case 1: num_shift = 3; break;
	    case 2: num_shift = 2; break;
	    case 3: num_shift = 1; break;
          }
        }
      }

      if(!p1->load_cmd_ewstall && (p1->cycle_type == 1))
	num_cycles_new = num_pixel << num_shift;
      else
	num_cycles_new = num_pixel >> num_shift;

      num_cycles = p1->get_new_stall ?  num_cycles_new : p1->cnt_stall;
      p0->ew_stall_x = get_ew_stall ? (num_cycles > comp_value) : p1->ew_stall_x;

      stall_decr = p1->get_new_stall ? (num_cycles_new - 1) : (p1->cnt_stall - 1);


      if(p1->ew_stall_x || (p1->get_new_stall && (num_cycles != 0)))
        p0->cnt_stall = stall_decr;
      else if(num_cycles == 0)
        p1->cnt_stall = 0;
      else
        p0->cnt_stall = stall_decr;

      switch(p1->end_prim_state)
      {
        case 0:
          if(p0->cs_ew_newprim) /* negedge signal */
            p0->end_prim_state = 3;
          else
            p0->end_prim_state = 0;
          break;
        case 1:
          if(p0->cs_ew_newprim) /* negedge signal */
            p0->end_prim_state = 3;
          else
            p0->end_prim_state = 1;
          break;
        case 2:
          if(p1->cnt_stall == 0)
            p0->end_prim_state = 1;
          else
            p0->end_prim_state = 2;
          break;
        case 3:
	   /* newprim negedge signal */
          if(p0->cs_ew_newprim && p1->end_ew && p1->span_valid_s && (num_cycles > 8))
            p0->end_prim_state = 2;
          else if(p0->cs_ew_newprim)
            p0->end_prim_state = 3;
          else if(!p1->span_valid_x_s)
            p0->end_prim_state = 1;
          else if(p1->end_ew && p1->span_valid_s && (num_cycles > 0))
            p0->end_prim_state = 2;
          else if(p1->end_ew && !p1->span_valid_s)
            p0->end_prim_state = 1;
          else if(p1->end_ew && (num_cycles == 0))
            p0->end_prim_state = 1;
          else
            p0->end_prim_state = 3;
          break;
      }

      p0->ew_cs_busy = (p0->end_prim_state >> 1) & 1;
    }
    
    /* 
     *  Mem Span Address Calculation
     */
    ew_ms_adrs(p0, p1);

  }
  /*
   *  Save clock
   */
  p0->gclk_old = p1->gclk_old = save_clk;
}

/* ======================================================================
 *  ew_init():  main initialization routine for the edge walker unit.
 *
 * ======================================================================
 */
void
  ew_init(ew_t *p0, ew_t *p1)
{
  p0->gclk = p1->gclk = 0;
  p0->gclk_old = p1->gclk_old = 0;  

  {
    char *ew_dump_str;
    if (ew_dump_str=getenv("EDGEWALK_DUMP")) {
      if (!(sscanf(ew_dump_str,"%i",&ew_dump)))
        ew_dump=0;
    } else 
      ew_dump=0;
  }
}