vi_sync.v 35.8 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 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054
/**************************************************************************
 *                                                                        *
 *               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.  *
 *                                                                        *
 *************************************************************************/

// $Id: vi_sync.v,v 1.1.1.1 2002/05/17 06:14:58 blythe Exp $

////////////////////////////////////////////////////////////////////////
//
// Project Reality
//
// module:	vi_sync
// description:	Sync generator and rgba unpacker for video interface.
//
// designer:	Phil Gossett
// date:	1/3/95
//
////////////////////////////////////////////////////////////////////////

module vi_sync (vclk, reset_l,
	type, serrate, aa_mode, pixel_adv,
	origin, width, v_int, hsync_width,
	burst_width, vsync_width, burst_start, v_sync_period,
	h_sync_period, leap_pattern, hsync_leap_b, hsync_leap_a,
	h_video_end, h_video_start, v_video_end, v_video_start,
	v_burst_end, v_burst_start, x_scale, y_scale, x_offset, y_offset,
	data_a, data_b, block_partial, block_grant,
	block_address, block_length, block_word,
	bank_sel, block_start, block_count,
	read_addr, pre_int, horizontal_flag,
	hfrac, vfrac, v_current,
	rgb0i, rgb1i, rgb2i, rgb3i,
	cvg0i, cvg1i, cvg2i, cvg3i,
	synci);

input vclk;
input reset_l;
input [1:0] type;
input serrate;
input [1:0] aa_mode;
input [3:0] pixel_adv;
input [23:0] origin;
input [11:0] width;
input [9:0] v_int;
input [7:0] hsync_width;
input [7:0] burst_width;
input [3:0] vsync_width;
input [9:0] burst_start;
input [9:0] v_sync_period;
input [11:0] h_sync_period;
input [4:0] leap_pattern;
input [11:0] hsync_leap_b;
input [11:0] hsync_leap_a;
input [9:0] h_video_end;
input [9:0] h_video_start;
input [9:0] v_video_end;
input [9:0] v_video_start;
input [9:0] v_burst_end;
input [9:0] v_burst_start;
input [11:0] x_scale;
input [11:0] y_scale;
input [11:0] x_offset;
input [11:0] y_offset;
input [71:0] data_a;
input [71:0] data_b;
input block_partial;			// some partially covered
input block_grant;

output [23:0] block_address;		// address of requested block
output [6:0] block_length;		// length of requested block
output [3:0] block_word;		// location in buffer
output bank_sel;			// buffer bank select
output block_start;			// consecutive req's
output [1:0] block_count;		// number of consecutive req's - 1
output [3:0] read_addr;
output pre_int;
output horizontal_flag;
output [4:0] hfrac;
output [4:0] vfrac;
output [9:0] v_current;
output [7:0] rgb0i;
output [7:0] rgb1i;
output [7:0] rgb2i;
output [7:0] rgb3i;
output [2:0] cvg0i;
output [2:0] cvg1i;
output [2:0] cvg2i;
output [2:0] cvg3i;
output synci;

// outputs
reg [23:0] block_address;
reg [6:0] block_length;
reg [3:0] block_word;
reg bank_sel;
reg block_start;
reg [1:0] block_count;
reg [3:0] read_addr;
reg pre_int;
reg [7:0] rgb0i;
reg [7:0] rgb1i;
reg [7:0] rgb2i;
reg [7:0] rgb3i;
reg [2:0] cvg0i;
reg [2:0] cvg1i;
reg [2:0] cvg2i;
reg [2:0] cvg3i;
reg synci;

wire block_partial_1d;	// instanciated synchronizer delay
wire block_partial_2d;
wire block_partial_3d;
wire block_grant_1d;
wire block_grant_2d;
wire block_grant_3d;

reg block_grant_4d;
reg horizontal_flag_1d;	// delay line
reg horizontal_flag_2d;
reg horizontal_flag_3d;
reg horizontal_flag_4d;
reg [26:0] rgba_2d;
reg [26:0] rgba_3d;
reg [26:0] rgba_4d;
reg [26:3] rgba_5d;
reg [18:3] rgba_6d;
reg [10:3] rgba_7d;
reg [4:0] last_lsbs;
reg [1:0] span_sel_1d;
reg [1:0] span_sel_2d;
reg [1:0] span_sel_3d;
reg [1:0] span_sel_4d;
reg [1:0] span_sel_5d;
reg [7:0] pixel_sync_1d;
reg [7:0] pixel_sync_2d;
reg [7:0] pixel_sync_3d;
reg [15:0] block_delta;

wire [1:0] span_sel;
wire [6:0] pixel_addr;
wire [23:0] pre_block_addr;
wire [23:0] block_addr;
wire [1:0] block_sel;
wire [7:0] pixel_sync;
wire h_blank_flag;
wire a_blank_flag;
wire p_blank_flag;
wire v_blank_flag;
wire [9:0] pre_hfrac;
wire [9:0] pre_vfrac;
wire vfrac_zero;
wire [71:0] data_ab;
wire [26:0] rgba;
wire [23:0] byte_width;

vi_sync_fsm vifsm  (.vclk(vclk), .reset_l(reset_l),
	.type(type), .serrate(serrate), .aa_mode(aa_mode),
	.pixel_adv(pixel_adv), .origin(origin), .width(width),
	.hsync_width(hsync_width), .burst_width(burst_width),
	.vsync_width(vsync_width), .burst_start(burst_start),
	.v_sync_period(v_sync_period), .h_sync_period(h_sync_period),
	.leap_pattern(leap_pattern),
	.hsync_leap_b(hsync_leap_b), .hsync_leap_a(hsync_leap_a),
	.h_video_end(h_video_end), .h_video_start(h_video_start),
	.v_video_end(v_video_end), .v_video_start(v_video_start),
	.v_burst_end(v_burst_end), .v_burst_start(v_burst_start),
	.x_scale(x_scale), .y_scale(y_scale),
	.x_offset(x_offset), .y_offset(y_offset),

	.block_addr(block_addr),
	.hfrac(pre_hfrac), .vfrac(pre_vfrac), .v_current(v_current),
	.span_sel(span_sel), .pixel_addr(pixel_addr),
	.pixel_sync(pixel_sync), .horizontal_flag(horizontal_flag),
	.h_blank_flag(h_blank_flag), .a_blank_flag(a_blank_flag),
	.p_blank_flag(p_blank_flag), .v_blank_flag(v_blank_flag));

assign hfrac = (aa_mode == 3) ? 0 : pre_hfrac[9:5];
assign vfrac = (aa_mode == 3) ? 0 : pre_vfrac[9:5];
assign vfrac_zero = (pre_vfrac == 0);
assign block_sel = {span_sel[1], (span_sel[1] ^~ span_sel[0])};
assign data_ab = bank_sel ? data_a : data_b;
assign rgba    = !type[1] ?
			 27'h7 :
			 (!type[0] ? (!pixel_addr[1] ? (!pixel_addr[0] ?
						{data_ab[71:67], 3'b0,
						 data_ab[66:62], 3'b0,
						 data_ab[61:57], 3'b0,
						 data_ab[56:54]} :
						{data_ab[53:49], 3'b0,
						 data_ab[48:44], 3'b0,
						 data_ab[43:39], 3'b0,
						 data_ab[38:36]}) :
						       (!pixel_addr[0] ?
						{data_ab[35:31], 3'b0,
						 data_ab[30:26], 3'b0,
						 data_ab[25:21], 3'b0,
						 data_ab[20:18]} :
						{data_ab[17:13], 3'b0,
						 data_ab[12:8],  3'b0,
						 data_ab[7:3],   3'b0,
						 data_ab[2:0]})) :
				     (!pixel_addr[0] ?
					{data_ab[71:64],
					 data_ab[63:56],
					 data_ab[53:46],
					 data_ab[45:43]} :
					{data_ab[35:28],
					 data_ab[27:20],
					 data_ab[17:10],
					 data_ab[9:7]}));
assign byte_width = !type[0] ? {width,1'b0} : {width,2'b0};
assign pre_block_addr = block_addr - 8;

reg [23:0] pre_block_address;
always @(block_address or block_delta) begin : pba_block
	/* synopsys resource pba_res:
	map_to_module = "DW01_add",
	implementation = "cla",
	ops = "ba";
	*/

	pre_block_address = block_address
	   + {{8{block_delta[15]}}, block_delta[15:0]}; // synopsys label ba
	end


always @(posedge vclk)
begin
	read_addr <= !aa_mode[1] ? (!type[0] ?
					{block_sel,pixel_addr[3:2]} :
					{block_sel,pixel_addr[2:1]}) :
			(!aa_mode[0] ?
				   (!type[0] ?
					{span_sel[1],pixel_addr[4:2]} :
					{span_sel[1],pixel_addr[3:1]}) :
				   (!type[0] ?
					pixel_addr[5:2] :
					pixel_addr[4:1]));
	bank_sel <= (!aa_mode[1] ? (!type[0] ?
					pixel_addr[4] :
					pixel_addr[3]) :
			(!aa_mode[0] ?
				   (!type[0] ?
					pixel_addr[5] :
					pixel_addr[4]) :
				   (!type[0] ?
					pixel_addr[6] :
					pixel_addr[5]))) ^ h_blank_flag;

	span_sel_1d <= span_sel;
	span_sel_2d <= span_sel_1d;
	span_sel_3d <= span_sel_2d;
	span_sel_4d <= span_sel_3d;
	span_sel_5d <= span_sel_4d;

	rgba_2d[26:0] <= rgba;
	rgba_3d[26:0] <= rgba_2d[26:0];
	rgba_4d[26:0] <= rgba_3d[26:0];
	rgba_5d[26:3] <= rgba_4d[26:3];
	rgba_6d[18:3] <= rgba_5d[18:3];
	rgba_7d[10:3] <= rgba_6d[10:3];

        pixel_sync_1d <= pixel_sync;
        pixel_sync_2d <= pixel_sync_1d;
        pixel_sync_3d <= pixel_sync_2d;

	rgb0i <= !span_sel_5d[1] ? (!span_sel_5d[0] ?	rgba_5d[26:19] : 
							rgba_6d[18:11]) :
				   (!span_sel_5d[0] ?	rgba_7d[10:3] :
							pixel_sync_3d);
	rgb1i <= !span_sel_5d[1] ? (!span_sel_5d[0] ?	rgba_4d[26:19] : 
							rgba_5d[18:11]) :
				   (!span_sel_5d[0] ?	rgba_6d[10:3] :
							pixel_sync_3d);
	rgb2i <= !span_sel_5d[1] ? (!span_sel_5d[0] ?	rgba_3d[26:19] : 
							rgba_4d[18:11]) :
				   (!span_sel_5d[0] ?	rgba_5d[10:3] :
							pixel_sync_3d);
	rgb3i <= !span_sel_5d[1] ? (!span_sel_5d[0] ?	rgba_2d[26:19] : 
							rgba_3d[18:11]) :
				   (!span_sel_5d[0] ?	rgba_4d[10:3] :
							pixel_sync_3d);

	cvg0i <= rgba_4d[2:0];
	cvg1i <= rgba_3d[2:0];
	cvg2i <= rgba_2d[2:0];
	cvg3i <= rgba[2:0];

	synci <= (span_sel_5d == 3);
end

// instanciated synchronizer flops
dfntnb bpsy1 (.cp(vclk), .d(block_partial),    .q(block_partial_1d));
dfntnb bpsy2 (.cp(vclk), .d(block_partial_1d), .q(block_partial_2d));
dfntnb bpsy3 (.cp(vclk), .d(block_partial_2d), .q(block_partial_3d));
dfntnb dgsy1 (.cp(vclk), .d(block_grant),    .q(block_grant_1d));
dfntnb dgsy2 (.cp(vclk), .d(block_grant_1d), .q(block_grant_2d));
dfntnb dgsy3 (.cp(vclk), .d(block_grant_2d), .q(block_grant_3d));

// state machine
reg [2:0] state;
parameter
        STATE_LINE_A	     = 0,
        STATE_LINE_B         = 1,
        STATE_LINE_U         = 2,
        STATE_LINE_D         = 3,
        STATE_PRE_IDLE       = 4,
        STATE_IDLE           = 5,
        STATE_PRE_WAIT       = 6,
        STATE_WAIT           = 7;

parameter
        FRAC_ALWAYS	     = 0,
        FRAC_BYNEED          = 1,
        FRAC_NEVER           = 2,
        FRAC_PIXEL           = 3,
        INT_ALWAYS	     = 4,
        INT_BYNEED           = 5,
        INT_NEVER            = 6,
        INT_PIXEL            = 7;

always @(posedge vclk or negedge reset_l)
   begin
   if (!reset_l)
      begin
      // resetable registers
      horizontal_flag_4d <= 0;
      last_lsbs <= 0;
      block_start <= 0;
      pre_int <= 0;
      state <= STATE_IDLE;
      block_address <= 0;
      block_length <= 0;
      block_word <= 0;
      block_count <= 0;
      block_grant_4d <= 0;
      horizontal_flag_1d <= 0;
      horizontal_flag_2d <= 0;
      horizontal_flag_3d <= 0;
      block_delta <= 0;
      end
   else
      begin
      pre_int <= (v_int == v_current);
      block_grant_4d <= block_grant_3d;
      horizontal_flag_1d <= horizontal_flag;
      horizontal_flag_2d <= horizontal_flag_1d;
      horizontal_flag_3d <= horizontal_flag_2d;
      case ({vfrac_zero, aa_mode})
         FRAC_ALWAYS:
            begin
            case (state)
               STATE_WAIT,	// unused state (guarantees exit)
               STATE_IDLE :
                  begin
                  horizontal_flag_4d <= horizontal_flag_3d;
                  if (!v_blank_flag &&
                      (horizontal_flag_3d && !horizontal_flag_4d))
                     begin
                     last_lsbs <= block_addr[6:3];
                     block_start <= 1;
                     block_count <= 3;
                     block_address <= block_addr;
                     block_length <= 4*8-1;
                     block_word <= 0;
                     state <= STATE_LINE_A;
                     end
                  else
                     begin
                     if (!v_blank_flag && !p_blank_flag && !a_blank_flag &&
                         (block_addr[4:3] == last_lsbs[1:0]))
                        begin
                        block_start <= 1;
                        block_count <= 3;
                        block_word <= 0;
                        state <= STATE_LINE_A;
                        end
                     end
                  end
               STATE_LINE_A :
                  begin
                  block_delta <= byte_width;
                  if (block_grant_3d && !block_grant_4d)
                     begin
                     block_address <= pre_block_address;
                     block_word <= 8;
                     state <= STATE_LINE_B;
                     end
                  end
               STATE_LINE_B :
                  begin
                  block_delta <= byte_width;
                  if (block_grant_3d && !block_grant_4d)
                     begin
                     block_address <= pre_block_address;
                     block_word <= 12;
                     state <= STATE_LINE_D;
                     end
                  end
               STATE_LINE_D :
                  begin
                  block_delta <= -{byte_width,1'b0} - byte_width;
                  if (block_grant_3d && !block_grant_4d)
                     begin
                     block_address <= pre_block_address;
                     block_word <= 4;
                     state <= STATE_LINE_U;
                     end
                  end
               STATE_LINE_U :
                  begin
                  block_delta <= byte_width + 32;
                  if (block_grant_3d && !block_grant_4d)
                     begin
                     block_start <= 0;
                     block_address <= pre_block_address;
                     block_word <= 0;
                     if (p_blank_flag)
                        begin
                        state <= STATE_IDLE;
                        end
                     else
                        begin
                        state <= STATE_PRE_IDLE;
                        end
                     end
                  end
               STATE_PRE_WAIT,	// unused state (guarantees exit)
               STATE_PRE_IDLE :
                  begin
                  if (block_addr[4:3] != last_lsbs[1:0])
                     begin
                     state <= STATE_IDLE;
                     end
                  end
            endcase
            end
         FRAC_BYNEED:
            begin
            case (state)
               STATE_IDLE :
                  begin
                  horizontal_flag_4d <= horizontal_flag_3d;
                  if (!v_blank_flag &&
                      (horizontal_flag_3d && !horizontal_flag_4d))
                     begin
                     last_lsbs <= block_addr[6:3];
                     block_start <= 1;
                     block_count <= 1;
                     block_address <= pre_block_addr;
                     block_length <= 6*8-1;
                     block_word <= 15;
                     state <= STATE_LINE_A;
                     end
                  else
                     begin
                     if (!v_blank_flag && !p_blank_flag && !a_blank_flag &&
                         (block_addr[4:3] == last_lsbs[1:0]))
                        begin
                        block_start <= 1;
                        block_count <= 1;
                        block_length <= 6*8-1;
                        block_word <= 15;
                        state <= STATE_LINE_A;
                        end
                     end
                  end
               STATE_LINE_A :
                  begin
                  block_delta <= byte_width;
                  if (block_grant_3d && !block_grant_4d)
                     begin
                     block_address <= pre_block_address;
                     block_word <= 7;
                     state <= STATE_LINE_B;
                     end
                  end
               STATE_LINE_B :
                  begin
                  block_delta <= byte_width + 8;
                  if (block_grant_3d && !block_grant_4d)
                     begin
                     block_start <= 0;
                     block_count <= 1;
                     block_address <= pre_block_address;
                     block_length <= 4*8-1;
                     block_word <= 12;
                     if (p_blank_flag)
                        begin
                        state <= STATE_WAIT;
                        end
                     else
                        begin
                        state <= STATE_PRE_WAIT;
                        end
                     end
                  end
               STATE_PRE_WAIT :
                  begin
                  if (block_addr[4:3] != last_lsbs[1:0])
                     begin
                     state <= STATE_WAIT;
                     end
                  end
               STATE_WAIT :
                  begin
                  block_delta <= -{byte_width,1'b0} + 32 - 8;
                  if (block_partial_3d)
                     begin
                     block_start <= 1;
                     state <= STATE_LINE_D;
                     end
                  else
                     begin
                     horizontal_flag_4d <= horizontal_flag_3d;
                     if (!v_blank_flag &&
                         (horizontal_flag_3d && !horizontal_flag_4d))
                        begin
                        last_lsbs <= block_addr[6:3];
                        block_start <= 1;
                        block_count <= 1;
                        block_address <= pre_block_addr;
                        block_length <= 6*8-1;
                        block_word <= 15;
                        state <= STATE_LINE_A;
                        end
                     else
                        begin
                        if (!v_blank_flag && !p_blank_flag && !a_blank_flag &&
                            (block_addr[4:3] == last_lsbs[1:0]))
                           begin
                           block_start <= 1;
                           block_count <= 1;
                           block_address <= pre_block_address;
                           block_length <= 6*8-1;
                           block_word <= 15;
                           state <= STATE_LINE_A;
                           end
                        end
                     end
                  end
               STATE_LINE_D :
                  begin
                  block_delta <= -{byte_width,1'b0} - byte_width;
                  if (block_grant_3d && !block_grant_4d)
                     begin
                     block_address <= pre_block_address;
                     block_word <= 4;
                     state <= STATE_LINE_U;
                     end
                  end
               STATE_LINE_U :
                  begin
                  block_delta <= byte_width + 32 - 8;
                  if (block_grant_3d && !block_grant_4d)
                     begin
                     block_start <= 0;
                     block_address <= pre_block_address;
                     block_word <= 0;
                     if (p_blank_flag)
                        begin
                        state <= STATE_IDLE;
                        end
                     else
                        begin
                        state <= STATE_PRE_IDLE;
                        end
                     end
                  end
               STATE_PRE_IDLE :
                  begin
                  if (block_addr[4:3] != last_lsbs[1:0])
                     begin
                     state <= STATE_IDLE;
                     end
                  end
            endcase
            end
         FRAC_NEVER:
            begin
            case (state)
               STATE_WAIT,	// unused state (guarantees exit)
               STATE_IDLE :
                  begin
                  horizontal_flag_4d <= horizontal_flag_3d;
                  if (!v_blank_flag &&
                      (horizontal_flag_3d && !horizontal_flag_4d))
                     begin
                     last_lsbs <= block_addr[6:3];
                     block_start <= 1;
                     block_count <= 1;
                     block_address <= block_addr;
                     block_length <= 8*8-1;
                     block_word <= 0;
                     state <= STATE_LINE_A;
                     end
                  else
                     begin
                     if (!v_blank_flag && !p_blank_flag && !a_blank_flag &&
                         (block_addr[5:3] == last_lsbs[2:0]))
                        begin
                        block_start <= 1;
                        block_count <= 1;
                        block_word <= 0;
                        state <= STATE_LINE_A;
                        end
                     end
                  end
               STATE_LINE_A :
                  begin
                  block_delta <= byte_width;
                  if (block_grant_3d && !block_grant_4d)
                     begin
                     block_address <= pre_block_address;
                     block_word <= 8;
                     state <= STATE_LINE_B;
                     end
                  end
               STATE_LINE_U,	// unused state (guarantees exit)
               STATE_LINE_D,	// unused state (guarantees exit)
               STATE_LINE_B :
                  begin
                  block_delta <= -byte_width + 64;
                  if (block_grant_3d && !block_grant_4d)
                     begin
                     block_start <= 0;
                     block_address <= pre_block_address;
                     block_word <= 0;
                     if (p_blank_flag)
                        begin
                        state <= STATE_IDLE;
                        end
                     else
                        begin
                        state <= STATE_PRE_IDLE;
                        end
                     end
                  end
               STATE_PRE_WAIT,	// unused state (guarantees exit)
               STATE_PRE_IDLE :
                  begin
                  if (block_addr[5:3] != last_lsbs[2:0])
                     begin
                     state <= STATE_IDLE;
                     end
                  end
            endcase
            end
         FRAC_PIXEL:
            begin
            case (state)
               STATE_WAIT,	// unused state (guarantees exit)
               STATE_IDLE :
                  begin
                  horizontal_flag_4d <= horizontal_flag_3d;
                  if (!v_blank_flag &&
                      (horizontal_flag_3d && !horizontal_flag_4d))
                     begin
                     last_lsbs <= block_addr[6:3];
                     block_start <= 1;
                     block_count <= 0;
                     block_address <= block_addr;
                     block_length <= 16*8-1;
                     block_word <= 0;
                     state <= STATE_LINE_A;
                     end
                  else
                     begin
                     if (!v_blank_flag && !p_blank_flag && !a_blank_flag &&
                         (block_addr[6:3] == last_lsbs[3:0]))
                        begin
                        block_start <= 1;
                        block_count <= 0;
                        block_word <= 0;
                        state <= STATE_LINE_A;
                        end
                     end
                  end
               STATE_LINE_B,	// unused state (guarantees exit)
               STATE_LINE_U,	// unused state (guarantees exit)
               STATE_LINE_D,	// unused state (guarantees exit)
               STATE_LINE_A :
                  begin
                  block_delta <= 128;
                  if (block_grant_3d && !block_grant_4d)
                     begin
                     block_start <= 0;
                     block_address <= pre_block_address;
                     block_word <= 0;
                     if (p_blank_flag)
                        begin
                        state <= STATE_IDLE;
                        end
                     else
                        begin
                        state <= STATE_PRE_IDLE;
                        end
                     end
                  end
               STATE_PRE_WAIT,	// unused state (guarantees exit)
               STATE_PRE_IDLE :
                  begin
                  if (block_addr[6:3] != last_lsbs[3:0])
                     begin
                     state <= STATE_IDLE;
                     end
                  end
            endcase
            end
         INT_ALWAYS:
            begin
            case (state)
               STATE_WAIT,	// unused state (guarantees exit)
               STATE_IDLE :
                  begin
                  horizontal_flag_4d <= horizontal_flag_3d;
                  if (!v_blank_flag &&
                      (horizontal_flag_3d && !horizontal_flag_4d))
                     begin
                     last_lsbs <= block_addr[6:3];
                     block_start <= 1;
                     block_count <= 2;
                     block_address <= block_addr;
                     block_length <= 4*8-1;
                     block_word <= 0;
                     state <= STATE_LINE_A;
                     end
                  else
                     begin
                     if (!v_blank_flag && !p_blank_flag && !a_blank_flag &&
                         (block_addr[4:3] == last_lsbs[1:0]))
                        begin
                        block_start <= 1;
                        block_count <= 2;
                        block_word <= 0;
                        state <= STATE_LINE_A;
                        end
                     end
                  end
               STATE_LINE_A :
                  begin
                  block_delta <= byte_width;
                  if (block_grant_3d && !block_grant_4d)
                     begin
                     block_address <= pre_block_address;
                     block_word <= 8;
                     state <= STATE_LINE_B;
                     end
                  end
               STATE_LINE_B :
                  begin
                  block_delta <= -{byte_width,1'b0};
                  if (block_grant_3d && !block_grant_4d)
                     begin
                     block_address <= pre_block_address;
                     block_word <= 4;
                     state <= STATE_LINE_U;
                     end
                  end
               STATE_LINE_D,	// unused state (guarantees exit)
               STATE_LINE_U :
                  begin
                  block_delta <= byte_width + 32;
                  if (block_grant_3d && !block_grant_4d)
                     begin
                     block_start <= 0;
                     block_address <= pre_block_address;
                     block_word <= 0;
                     if (p_blank_flag)
                        begin
                        state <= STATE_IDLE;
                        end
                     else
                        begin
                        state <= STATE_PRE_IDLE;
                        end
                     end
                  end
               STATE_PRE_WAIT,	// unused state (guarantees exit)
               STATE_PRE_IDLE :
                  begin
                  if (block_addr[4:3] != last_lsbs[1:0])
                     begin
                     state <= STATE_IDLE;
                     end
                  end
            endcase
            end
         INT_BYNEED:
            begin
            case (state)
               STATE_IDLE :
                  begin
                  horizontal_flag_4d <= horizontal_flag_3d;
                  if (!v_blank_flag &&
                      (horizontal_flag_3d && !horizontal_flag_4d))
                     begin
                     last_lsbs <= block_addr[6:3];
                     block_start <= 1;
                     block_count <= 0;
                     block_address <= pre_block_addr;
                     block_length <= 6*8-1;
                     block_word <= 15;
                     state <= STATE_LINE_A;
                     end
                  else
                     begin
                     if (!v_blank_flag && !p_blank_flag && !a_blank_flag &&
                         (block_addr[4:3] == last_lsbs[1:0]))
                        begin
                        block_start <= 1;
                        block_count <= 0;
                        block_length <= 6*8-1;
                        block_word <= 15;
                        state <= STATE_LINE_A;
                        end
                     end
                  end
               STATE_LINE_A :
                  begin
                  block_delta <= byte_width + 8;
                  if (block_grant_3d && !block_grant_4d)
                     begin
                     block_start <= 0;
                     block_count <= 1;
                     block_address <= pre_block_address;
                     block_length <= 4*8-1;
                     block_word <= 8;
                     if (p_blank_flag)
                        begin
                        state <= STATE_WAIT;
                        end
                     else
                        begin
                        state <= STATE_PRE_WAIT;
                        end
                     end
                  end
               STATE_PRE_WAIT :
                  begin
                  if (block_addr[4:3] != last_lsbs[1:0])
                     begin
                     state <= STATE_WAIT;
                     end
                  end
               STATE_WAIT :
                  begin
                  block_delta <= -byte_width + 32 - 8;
                  if (block_partial_3d)
                     begin
                     block_start <= 1;
                     state <= STATE_LINE_B;
                     end
                  else
                     begin
                     horizontal_flag_4d <= horizontal_flag_3d;
                     if (!v_blank_flag &&
                         (horizontal_flag_3d && !horizontal_flag_4d))
                        begin
                        last_lsbs <= block_addr[6:3];
                        block_start <= 1;
                        block_count <= 0;
                        block_address <= pre_block_addr;
                        block_length <= 6*8-1;
                        block_word <= 15;
                        state <= STATE_LINE_A;
                        end
                     else
                        begin
                        if (!v_blank_flag && !p_blank_flag && !a_blank_flag &&
                            (block_addr[4:3] == last_lsbs[1:0]))
                           begin
                           block_start <= 1;
                           block_count <= 0;
                           block_address <= pre_block_address;
                           block_length <= 6*8-1;
                           block_word <= 15;
                           state <= STATE_LINE_A;
                           end
                        end
                     end
                  end
               STATE_LINE_B :
                  begin
                  block_delta <= -{byte_width,1'b0};
                  if (block_grant_3d && !block_grant_4d)
                     begin
                     block_address <= pre_block_address;
                     block_word <= 4;
                     state <= STATE_LINE_U;
                     end
                  end
               STATE_LINE_D,	// unused state (guarantees exit)
               STATE_LINE_U :
                  begin
                  block_delta <= byte_width + 32 - 8;
                  if (block_grant_3d && !block_grant_4d)
                     begin
                     block_start <= 0;
                     block_address <= pre_block_address;
                     block_word <= 0;
                     if (p_blank_flag)
                        begin
                        state <= STATE_IDLE;
                        end
                     else
                        begin
                        state <= STATE_PRE_IDLE;
                        end
                     end
                  end
               STATE_PRE_IDLE :
                  begin
                  if (block_addr[4:3] != last_lsbs[1:0])
                     begin
                     state <= STATE_IDLE;
                     end
                  end
            endcase
            end
         INT_NEVER:
            begin
            case (state)
               STATE_WAIT,	// unused state (guarantees exit)
               STATE_IDLE :
                  begin
                  horizontal_flag_4d <= horizontal_flag_3d;
                  if (!v_blank_flag &&
                      (horizontal_flag_3d && !horizontal_flag_4d))
                     begin
                     last_lsbs <= block_addr[6:3];
                     block_start <= 1;
                     block_count <= 0;
                     block_address <= block_addr;
                     block_length <= 8*8-1;
                     block_word <= 0;
                     state <= STATE_LINE_A;
                     end
                  else
                     begin
                     if (!v_blank_flag && !p_blank_flag && !a_blank_flag &&
                         (block_addr[5:3] == last_lsbs[2:0]))
                        begin
                        block_start <= 1;
                        block_count <= 0;
                        block_word <= 0;
                        state <= STATE_LINE_A;
                        end
                     end
                  end
               STATE_LINE_B,	// unused state (guarantees exit)
               STATE_LINE_U,	// unused state (guarantees exit)
               STATE_LINE_D,	// unused state (guarantees exit)
               STATE_LINE_A :
                  begin
                  block_delta <= 64;
                  if (block_grant_3d && !block_grant_4d)
                     begin
                     block_start <= 0;
                     block_address <= pre_block_address;
                     block_word <= 0;
                     if (p_blank_flag)
                        begin
                        state <= STATE_IDLE;
                        end
                     else
                        begin
                        state <= STATE_PRE_IDLE;
                        end
                     end
                  end
               STATE_PRE_WAIT,	// unused state (guarantees exit)
               STATE_PRE_IDLE :
                  begin
                  if (block_addr[5:3] != last_lsbs[2:0])
                     begin
                     state <= STATE_IDLE;
                     end
                  end
            endcase
            end
         INT_PIXEL:
            begin
            case (state)
               STATE_WAIT,	// unused state (guarantees exit)
               STATE_IDLE :
                  begin
                  horizontal_flag_4d <= horizontal_flag_3d;
                  if (!v_blank_flag &&
                      (horizontal_flag_3d && !horizontal_flag_4d))
                     begin
                     last_lsbs <= block_addr[6:3];
                     block_start <= 1;
                     block_count <= 0;
                     block_address <= block_addr;
                     block_length <= 16*8-1;
                     block_word <= 0;
                     state <= STATE_LINE_A;
                     end
                  else
                     begin
                     if (!v_blank_flag && !p_blank_flag && !a_blank_flag &&
                         (block_addr[6:3] == last_lsbs[3:0]))
                        begin
                        block_start <= 1;
                        block_count <= 0;
                        block_word <= 0;
                        state <= STATE_LINE_A;
                        end
                     end
                  end
               STATE_LINE_B,	// unused state (guarantees exit)
               STATE_LINE_U,	// unused state (guarantees exit)
               STATE_LINE_D,	// unused state (guarantees exit)
               STATE_LINE_A :
                  begin
                  block_delta <= 128;
                  if (block_grant_3d && !block_grant_4d)
                     begin
                     block_start <= 0;
                     block_address <= pre_block_address;
                     block_word <= 0;
                     if (p_blank_flag)
                        begin
                        state <= STATE_IDLE;
                        end
                     else
                        begin
                        state <= STATE_PRE_IDLE;
                        end
                     end
                  end
               STATE_PRE_WAIT,	// unused state (guarantees exit)
               STATE_PRE_IDLE :
                  begin
                  if (block_addr[6:3] != last_lsbs[3:0])
                     begin
                     state <= STATE_IDLE;
                     end
                  end
            endcase
            end
      endcase
      end
   end

endmodule // vi_sync