DCM.v 25.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 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
// $Header: /root/leakn64/depot/rf/hw/flif/xilinx/DCM.v,v 1.1 2003/08/20 23:46:45 berndt Exp $

/*

FUNCTION	: Digital Clock Manager

*/

`timescale  1 ps / 1 ps

module DCM (
	CLK0, CLK180, CLK270, CLK2X, CLK2X180, CLK90,
	CLKDV, CLKFX, CLKFX180, LOCKED, PSDONE, STATUS,
	CLKFB, CLKIN, DSSEN, PSCLK, PSEN, PSINCDEC, RST);

parameter CLK_FEEDBACK = "1X";
parameter CLKDV_DIVIDE = 2.0;
parameter CLKFX_DIVIDE = 1;
parameter CLKFX_MULTIPLY = 4;
parameter CLKIN_DIVIDE_BY_2 = "FALSE";
parameter CLKIN_PERIOD = 0.0;			// non-simulatable
parameter CLKOUT_PHASE_SHIFT = "NONE";
parameter DESKEW_ADJUST = "SYSTEM_SYNCHRONOUS";	// non-simulatable
parameter DFS_FREQUENCY_MODE = "LOW";
parameter DLL_FREQUENCY_MODE = "LOW";
parameter DSS_MODE = "NONE";			// non-simulatable
parameter DUTY_CYCLE_CORRECTION = "TRUE";
parameter FACTORY_JF = 16'hC080;		// non-simulatable
parameter MAXPERCLKIN = 1000000;
parameter MAXPERPSCLK = 100000000;
parameter PHASE_SHIFT = 0;
parameter STARTUP_WAIT = "FALSE";		// non-simulatable

input CLKFB, CLKIN, DSSEN;
input PSCLK, PSEN, PSINCDEC, RST;

output CLK0, CLK180, CLK270, CLK2X, CLK2X180, CLK90;
output CLKDV, CLKFX, CLKFX180, LOCKED, PSDONE;
output [7:0] STATUS;

reg    CLK0, CLK180, CLK270, CLK2X, CLK2X180, CLK90;
reg    CLKDV, CLKFX, CLKFX180;

reg    lock_period, lock_delay, lock_clkin, lock_clkfb;
reg    delay_found, delay_fb_found;
time   clkin_edge[0:1];
time   psclk_edge[0:1], psclk_period;
time   delay_edge[0:1];
time   period, delay, delay_fb;
time   fx_delay;

wire   clkin_buf, clkfb_buf;
reg    clkin_ps, clkin_div2;
reg    clkin_in;
reg    clkfb_in;
wire   psclk_in, psen_in, psincdec_in, rst_in;
reg    clkfx_out, clklost_out, locked_out, psdone_out, psoverflow_out, pslock;
reg    locked_period;
wire   clk0_int, clk4x_int, clkdv_int;
reg    clk2x_int;
wire   clk2x_2575, clk2x_5050;

reg    clkin_locked, clkin_locked_period, clkin_div2_r, clkin_div2_f;
reg    clkin_low1, clkin_low2, clkin_high1, clkin_high2;

reg    clkin_window, clkfb_window;
reg    clk1x, clkin_5050, clk1x_5050;
reg    clk1x_shift125, clk1x_shift250;
reg    clk2x_shift;

reg    [7:0] count3, count4, count5, count7, count9, count11, count13, count15;
reg    [32:1] divider;
reg    [8:0] divide_type;
reg    [1:0] clkfb_type;
reg    clk1x_type;
reg    clkin_type;
reg    dll_mode_type;
reg    dfs_mode_type;
reg    [3:0] DESKEW_ADJUST_mode;
reg    rst_1, rst_2;
reg    notifier;

reg    [12:0] numerator, denominator, gcd;
reg    [23:0] i, n, d, p;
reg    generate, clkfx_int;
time   period_fx;

reg    [9:0] ps_in, ps;
time   clkin_delay, clkfb_delay;

initial begin
  clk1x <= 0;
  clk1x_5050 <= 0;
  clk1x_shift125 <= 0;
  clk1x_shift250 <= 0;
  clk2x_shift <= 0;
  clkfb_delay <= 0;
  clkfb_window <= 0;
  clkfx_int <= 0;
  clkin_5050 <= 0;
  clkin_delay <= 0;
  clkin_div2 <= 0;
  clkin_div2_f <= 0;
  clkin_div2_r <= 0;
  clkin_edge[0] <= 1'bx;
  clkin_edge[1] <= 1'bx;
  clkin_high1 <= 0;
  clkin_high2 <= 0;
  clkin_low1 <= 0;
  clkin_low2 <= 0;
  clkin_window <= 0;
  count11 <= 0;
  count13 <= 0;
  count15 <= 0;
  count3 <= 0;
  count4 <= 0;
  count5 <= 0;
  count7 <= 0;
  count9 <= 0;
  d <= 2048;
  delay <= 0;
  delay_fb <= 0;
  delay_fb_found <= 0;
  delay_found <= 0;
  divider <= 0;
  generate <= 0;
  lock_delay <= 0;
  lock_period <= 0;
  locked_out <= 0;
  locked_period <= 0;
  period <= 0;
  psdone_out <= 0;
  pslock <= 0;
  psoverflow_out <= 0;

  case (CLK_FEEDBACK)
    "none" : clkfb_type <= 0;
    "NONE" : clkfb_type <= 0;
    "1x" : clkfb_type <= 1;
    "1X" : clkfb_type <= 1;
    "2x"  : clkfb_type <= 2;
    "2X"  : clkfb_type <= 2;
    default : begin
		$display("Attribute Syntax Error : The attribute CLK_FEEDBACK on DCM instance %m is set to %s.  Legal values for this attribute are NONE, 1X or 2X.", CLK_FEEDBACK);
		$finish;
	      end
  endcase

  case (CLKDV_DIVIDE)
    1.5   : divide_type <= 'd3;
    2.0   : divide_type <= 'd4;
    2.5   : divide_type <= 'd5;
    3.0   : divide_type <= 'd6;
    3.5   : divide_type <= 'd7;
    4.0   : divide_type <= 'd8;
    4.5   : divide_type <= 'd9;
    5.0   : divide_type <= 'd10;
    5.5   : divide_type <= 'd11;
    6.0   : divide_type <= 'd12;
    6.5   : divide_type <= 'd13;
    7.0   : divide_type <= 'd14;
    7.5   : divide_type <= 'd15;
    8.0   : divide_type <= 'd16;
    9.0   : divide_type <= 'd18;
    10.0  : divide_type <= 'd20;
    11.0  : divide_type <= 'd22;
    12.0  : divide_type <= 'd24;
    13.0  : divide_type <= 'd26;
    14.0  : divide_type <= 'd28;
    15.0  : divide_type <= 'd30;
    16.0  : divide_type <= 'd32;
    default : begin
		$display("Attribute Syntax Error : The attribute CLKDV_DIVIDE on DCM instance %m is set to %0.1f.  Legal values for this attribute are 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0, 5.5, 6.0, 6.5, 7.0, 7.5, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, or 16.0.", CLKDV_DIVIDE);
		$finish;
	      end
  endcase

  if ((CLKFX_DIVIDE <= 0) || (4096 < CLKFX_DIVIDE)) begin
	$display("Attribute Syntax Error : The attribute CLKFX_DIVIDE on DCM instance %m is set to %d.  Legal values for this attribute are 1 ... 32.", CLKFX_DIVIDE);
    $finish;
  end

  if ((32 < CLKFX_DIVIDE) && (CLKFX_DIVIDE <= 4096)) begin
	$display("Attribute Syntax Warning : The attribute CLKFX_DIVIDE on DCM instance %m is set to %d.  Legal values for this attribute are 1 ... 32.", CLKFX_DIVIDE);
  end

  if ((CLKFX_MULTIPLY <= 0) || (4096 < CLKFX_MULTIPLY)) begin
	$display("Attribute Syntax Error : The attribute CLKFX_MULTIPLY on DCM instance %m is set to %d.  Legal values for this attribute are 1 ... 32.", CLKFX_MULTIPLY);
    $finish;
  end

  if ((32 < CLKFX_MULTIPLY) && (CLKFX_MULTIPLY <= 4096)) begin
	$display("Attribute Syntax Warning : The attribute CLKFX_MULTIPLY on DCM instance %m is set to %d.  Legal values for this attribute are 1 ... 32.", CLKFX_MULTIPLY);
  end

  case (CLKIN_DIVIDE_BY_2)
    "false" : clkin_type <= 0;
    "FALSE" : clkin_type <= 0;
    "true"  : clkin_type <= 1;
    "TRUE"  : clkin_type <= 1;
    default : begin
		$display("Attribute Syntax Error : The attribute CLKIN_DIVIDE_BY_2 on DCM instance %m is set to %s.  Legal values for this attribute are TRUE or FALSE.", CLKIN_DIVIDE_BY_2);
		$finish;
	      end
  endcase

  case (CLKOUT_PHASE_SHIFT)
    "NONE"     : ps_in <= 0;
    "none"     : ps_in <= 0;
    "FIXED"   : ps_in <= PHASE_SHIFT + 256;
    "fixed"   : ps_in <= PHASE_SHIFT + 256;
    "VARIABLE" : ps_in <= PHASE_SHIFT + 256;
    "variable" : ps_in <= PHASE_SHIFT + 256;
    default : begin
		$display("Attribute Syntax Error : The attribute CLKOUT_PHASE_SHIFT on DCM instance %m is set to %s.  Legal values for this attribute are NONE, FIXED or VARIABLE.", CLKOUT_PHASE_SHIFT);
		$finish;
	      end
  endcase

  case (DESKEW_ADJUST)
    "source_synchronous" : DESKEW_ADJUST_mode <= 8;
    "SOURCE_SYNCHRONOUS" : DESKEW_ADJUST_mode <= 8;
    "system_synchronous"  : DESKEW_ADJUST_mode <= 11;
    "SYSTEM_SYNCHRONOUS"  : DESKEW_ADJUST_mode <= 11;
    "0"  : DESKEW_ADJUST_mode <= 0;
    "1"  : DESKEW_ADJUST_mode <= 1;
    "2"  : DESKEW_ADJUST_mode <= 2;
    "3"  : DESKEW_ADJUST_mode <= 3;
    "4"  : DESKEW_ADJUST_mode <= 4;
    "5"  : DESKEW_ADJUST_mode <= 5;
    "6"  : DESKEW_ADJUST_mode <= 6;
    "7"  : DESKEW_ADJUST_mode <= 7;
    "8"  : DESKEW_ADJUST_mode <= 8;
    "9"  : DESKEW_ADJUST_mode <= 9;
    "10"  : DESKEW_ADJUST_mode <= 10;
    "11"  : DESKEW_ADJUST_mode <= 11;
    "12"  : DESKEW_ADJUST_mode <= 12;
    "13"  : DESKEW_ADJUST_mode <= 13;
    "14"  : DESKEW_ADJUST_mode <= 14;
    "15"  : DESKEW_ADJUST_mode <= 15;
    default : begin
		$display("Attribute Syntax Error : The attribute DESKEW_ADJUST on DCM instance %m is set to %s.  Legal values for this attribute are SOURCE_SYNCHRONOUS, SYSTEM_SYNCHRONOUS or 0 ... 15.", DESKEW_ADJUST);
		$finish;
	      end
  endcase

  case (DFS_FREQUENCY_MODE)
    "high" : dfs_mode_type <= 1;
    "HIGH" : dfs_mode_type <= 1;
    "low"  : dfs_mode_type <= 0;
    "LOW"  : dfs_mode_type <= 0;
    default : begin
		$display("Attribute Syntax Error : The attribute DFS_FREQUENCY_MODE on DCM instance %m is set to %s.  Legal values for this attribute are HIGH or LOW.", DFS_FREQUENCY_MODE);
		$finish;
	      end
  endcase

  case (DLL_FREQUENCY_MODE)
    "high" : dll_mode_type <= 1;
    "HIGH" : dll_mode_type <= 1;
    "low"  : dll_mode_type <= 0;
    "LOW"  : dll_mode_type <= 0;
    default : begin
		$display("Attribute Syntax Error : The attribute DLL_FREQUENCY_MODE on DCM instance %m is set to %s.  Legal values for this attribute are HIGH or LOW.", DLL_FREQUENCY_MODE);
		$finish;
	      end
  endcase

  case (DSS_MODE)
    "none" : ;
    "NONE" : ;
    "spread_2"  : ;
    "SPREAD_2"  : ;
    "spread_4"  : ;
    "SPREAD_4"  : ;
    "spread_6"  : ;
    "SPREAD_6"  : ;
    "spread_8"  : ;
    "SPREAD_8"  : ;
    default : begin
		$display("Attribute Syntax Error : The attribute DSS_MODE on DCM instance %m is set to %s.  Legal values for this attribute are NONE, SPREAD_2, SPREAD_4, SPREAD_6 or SPREAD_8.", DSS_MODE);
		$finish;
	      end
  endcase

  case (DUTY_CYCLE_CORRECTION)
    "false" : clk1x_type <= 0;
    "FALSE" : clk1x_type <= 0;
    "true"  : clk1x_type <= 1;
    "TRUE"  : clk1x_type <= 1;
    default : begin
		$display("Attribute Syntax Error : The attribute DUTY_CYCLE_CORRECTION on DCM instance %m is set to %s.  Legal values for this attribute are TRUE or FALSE.", DUTY_CYCLE_CORRECTION);
		$finish;
	      end
  endcase

  if ((PHASE_SHIFT < -255) || (PHASE_SHIFT > 255)) begin
	$display("Attribute Syntax Error : The attribute PHASE_SHIFT on DCM instance %m is set to %d.  Legal values for this attribute are -255 ... 255.", PHASE_SHIFT);
    $display("Error : PHASE_SHIFT = %d is not -255 ... 255.", PHASE_SHIFT);
    $finish;
  end

  case (STARTUP_WAIT)
    "false" : ;
    "FALSE" : ;
    "true"  : ;
    "TRUE"  : ;
    default : begin
		$display("Attribute Syntax Error : The attribute STARTUP_WAIT on DCM instance %m is set to %s.  Legal values for this attribute are TRUE or FALSE.", STARTUP_WAIT);
		$finish;
	      end
  endcase

end

//
// phase shift parameters
//

always @(posedge psclk_in) begin
  if ((CLKOUT_PHASE_SHIFT == "VARIABLE") || (CLKOUT_PHASE_SHIFT == "variable"))
    if (psen_in)
      if (pslock)
	$display("Timing Violation Warning : Please wait for PSDONE signal before adjusting the Phase Shift.");
      else
	if (psincdec_in == 1)
	  if (ps_in == 511)
	    psoverflow_out = 1;
	  else begin
	    ps_in = ps_in + 1;
	    ps = ps_in - PHASE_SHIFT;
	    psoverflow_out = 0;
	    delay_fb = delay + period * (ps) / 256;
	    pslock = 1;
	  end
	else
	  if (ps_in == 1)
	    psoverflow_out = 1;
	  else begin
	    ps_in = ps_in - 1;
	    ps = ps_in - PHASE_SHIFT;
	    psoverflow_out = 0;
	    delay_fb = delay + period * (ps) / 256;
	    pslock = 1;
	  end
end

//
// determine phase shift
//
always @ (lock_period or ps_in) begin
  if (lock_period & (clkfb_type != 0)) begin
    clkin_delay <= period;
    clkfb_delay <= period * (512 - ps_in) / 256;
  end
end

always @ (posedge pslock) begin
  @(posedge CLKIN)
  @(posedge psclk_in)
    psdone_out <= 1;
  @(posedge psclk_in)
    psdone_out <= 0;
    pslock <= 0;
end

//
// fx parameters
//

initial begin
  generate = 1'b0;
  numerator = CLKFX_MULTIPLY;
  denominator = CLKFX_DIVIDE;
  gcd = 1;
  for (i = 2; i <= numerator; i = i + 1) begin
    if (((numerator % i) == 0) && ((denominator % i) == 0))
      gcd = i;
  end
  numerator = CLKFX_MULTIPLY / gcd;
  denominator = CLKFX_DIVIDE  / gcd;
end

//
// input wire delays
//
buf b_clkin (clkin_buf, CLKIN);
buf b_clkfb (clkfb_buf, CLKFB);

always @(clkin_buf) begin
  clkin_ps <= #clkin_delay clkin_buf;
end

always @(posedge clkin_ps) begin
  clkin_div2 <= ~clkin_div2;
end

always @(clkin_div2 or clkin_ps) begin
  if (!rst_in)
	if (clkin_type)
		clkin_in <= clkin_div2;
	else
		clkin_in <= clkin_ps;
end

always @(clkfb_buf) begin
  clkfb_in <= #clkfb_delay clkfb_buf;
end

always @(locked_out) begin
  locked_period <= #(2*period) locked_out;
end

buf b_dssen (dssen_in, DSSEN);
buf b_psclk (psclk_in, PSCLK);
buf b_psen (psen_in, PSEN);
buf b_psincdec (psincdec_in, PSINCDEC);
buf b_rst (rst_in, RST);
buf b_locked (LOCKED, locked_period);
buf b_psdone (PSDONE, psdone_out);
buf b_psoverflow (STATUS[0], psoverflow_out);
buf b_clklost (STATUS[1], clklost_out);
buf b_clkfx (STATUS[2], clkfx_out);

//
// clock lost
//

always @(CLKIN) begin
    clkin_locked <= #period CLKIN;
end

always @(clkin_locked) begin
  clkin_locked_period <= #period clkin_locked;
end

always @(posedge clkin_locked) begin
  clkin_div2_r <= ~clkin_div2_r;
end

always @(negedge clkin_locked_period) begin
  clkin_low1 <= clkin_div2_r;
end

always @(negedge clkin_locked_period) begin
  clkin_low2 <= clkin_low1;
end

always @(negedge clkin_locked) begin
  clkin_div2_f <= ~clkin_div2_f;
end

always @(posedge clkin_locked_period) begin
  clkin_high1 <= clkin_div2_f;
end

always @(posedge clkin_locked_period) begin
  clkin_high2 <= clkin_high1;
end

always @(clkin_low1 or clkin_low2 or clkin_high1 or clkin_high2) begin 
  clklost_out <= locked_out && (!(clkin_low1 ^ clkin_low2) || !(clkin_high1 ^ clkin_high2));
end

//
// determine clock period
//
always @(posedge clkin_in) begin
  clkin_edge[0] <= $time;
  clkin_edge[1] <= clkin_edge[0];
  if (rst_2)
    period <= 0;
  else if (period < clkin_edge[0] - clkin_edge[1] - 500)
    period <= clkin_edge[0] - clkin_edge[1];
  else if (period > clkin_edge[0] - clkin_edge[1] + 500)
    period <= clkin_edge[0] - clkin_edge[1];
end

always @(period) begin
  CLK0 <= 0;		    // period changed reset everything
  CLK180 <= 0;
  clk1x <= 0;
  clk1x_5050 <= 0;
  clk1x_shift125 <= 0;
  clk1x_shift250 <= 0;
  CLK270 <= 0;
  CLK2X <= 0;
  CLK2X180 <= 0;
  clk2x_shift <= 0;
  CLK90 <= 0;
  CLKDV <= 0;
  clkfb_window <= 0;
  CLKFX <= 0;
  CLKFX180 <= 0;
  clkfx_int <= 0;
  clkin_5050 <= 0;
  clkin_window <= 0;
  clklost_out <= 0;
  clkfx_out <= 1;
  d <= 2048;
  delay <= 0;
  delay_fb <= 0;
  delay_fb_found <= 0;
  delay_found <= 0;
  divider <= 0;
  generate <= 0;
  lock_clkfb <= 0;
  lock_clkin <= 0;
  lock_delay <= 0;
  lock_period <= 0;
  locked_out <= 0;
  locked_period <= 0;
  psdone_out <= 0;
  pslock <= 0;
  psoverflow_out <= 0;
  if (period < clkin_edge[0] - clkin_edge[1] - 500)
    lock_period <= 0;
  else if (period > clkin_edge[0] - clkin_edge[1] + 500)
    lock_period <= 0;
  else if ((clkin_edge[0] !== 1'bx) && (clkin_edge[1] !== 1'bx))
    lock_period <= 1;
end

always @(posedge lock_period) begin
  if (period > MAXPERCLKIN) begin
    $display("Timing Violation Error : Input clock period of, %1.3f ns, on the CLKIN port of instance %m exceeds allotted value of %1.3f ns at simulation time %1.3f ns.", period/1000.0, MAXPERCLKIN/1000.0, $time/1000.0);
  end
end

always @(posedge psclk_in) begin
  psclk_edge[0] <= $time;
  psclk_edge[1] <= psclk_edge[0];
  psclk_period <= psclk_edge[0] - psclk_edge[1];
  if (psclk_period > MAXPERPSCLK) begin
    $display("Timing Violation Error : Input clock period of, %1.3f ns, on the PSCLK port of instance %m exceeds allotted value of %1.3f ns at simulation time %1.3f ns.", period/1000.0, MAXPERPSCLK/1000.0, $time/1000.0);
  end
end

//
// determine clock delay
//
always @ (lock_period or delay_found) begin
  if (lock_period && !delay_found) begin
    assign CLK0 = 0; assign CLK2X = 0;
    #period
    assign CLK0 = 1; assign CLK2X = 1;
    #period
    assign CLK0 = 0; assign CLK2X = 0;
    #period
    assign CLK0 = 1; assign CLK2X = 1;
    delay_edge[1] = $time;
    @(posedge clkfb_in or rst_in)
    delay_edge[0] = $time;
    #period
    assign CLK0 = 0; assign CLK2X = 0;
    deassign CLK0; deassign CLK2X;
    #period
    delay = (10 * period - (delay_edge[0] - delay_edge[1])) % period;
    delay_found = 1;
  end
end

always @ (lock_period or delay_found) begin
  if (lock_period && delay_found) begin
    for (delay_fb = 0; delay_fb < delay; delay_fb = delay_fb + 1000)
      @(posedge clkin_in);
    delay_fb <= delay + period;
    @(posedge clkin_in);
    @(posedge clkin_in);
    delay_fb_found <= 1;
  end
end

always @ (posedge rst_in) begin
    CLK0 <= 0;
    CLK180 <= 0;
    clk1x <= 0;
    clk1x_5050 <= 0;
    clk1x_shift125 <= 0;
    clk1x_shift250 <= 0;
    CLK270 <= 0;
    CLK2X <= 0;
    CLK2X180 <= 0;
    clk2x_shift <= 0;
    CLK90 <= 0;
    CLKDV <= 0;
    clkfb_window <= 0;
    CLKFX <= 0;
    CLKFX180 <= 0;
    clkfx_int <= 0;
    clkin_5050 <= 0;
    clkin_edge[0] <= 1'bx;
    clkin_edge[1] <= 1'bx;
    clkin_window <= 0;
    clklost_out <= 0;
    clkfx_out <= 1;
    count11 <= 0;
    count13 <= 0;
    count15 <= 0;
    count3 <= 0;
    count4 <= 0;
    count5 <= 0;
    count7 <= 0;
    count9 <= 0;
    d <= 2048;
    delay <= 0;
    delay_fb <= 0;
    delay_fb_found <= 0;
    delay_found <= 0;
    divider <= 0;
    fx_delay <= 0;
    period_fx <= 0;
    generate <= 0;
    lock_clkfb <= 0;
    lock_clkin <= 0;
    lock_delay <= 0;
    lock_period <= 0;
    locked_out <= 0;
    locked_period <= 0;
    psdone_out <= 0;
    pslock <= 0;
    psoverflow_out <= 0;
    case (CLKOUT_PHASE_SHIFT)
      "NONE"     : ps_in <= 0;
      "none"     : ps_in <= 0;
      "FIXED"   : ps_in <= PHASE_SHIFT + 256;
      "fixed"   : ps_in <= PHASE_SHIFT + 256;
      "VARIABLE" : ps_in <= PHASE_SHIFT + 256;
      "variable" : ps_in <= PHASE_SHIFT + 256;
    endcase
end

always @ (posedge clklost_out) begin
  if (clkfb_window != 0) begin
    clkfx_out <= 1;
    locked_out <= 0;
    p <= 0;
  end
end

always @ (posedge clkfb_in) begin
  #0   clkfb_window = 1;
  #163 clkfb_window = 0;
end

always @ (posedge clkin_in) begin
  #0   clkin_window = 1;
  #163 clkin_window = 0;
end

always @ (posedge clkin_in) begin
  #1
  if (clkfb_window && delay_fb_found) begin
    lock_clkfb <= 1;
    lock_clkin <= 1;
  end
  else
    lock_clkin <= 0;
end

always @ (posedge clkfb_in) begin
  #1
  if (clkin_window && delay_fb_found) begin
    lock_clkfb <= 1;
    lock_clkin <= 1;
  end
  else
    lock_clkfb <= 0;
end

always @ (lock_clkin or lock_clkfb) begin
  if (lock_clkin || lock_clkfb)
    lock_delay <= 1;
  else
    lock_delay <= 0;
end

//
// generate master reset signal
//
always @ (posedge rst_in) begin
   rst_1 <= 1;
   rst_2 <= 1;
   locked_period <= 0;
end

always @ (posedge clkin_in) begin
  if (rst_in == 0)
    {rst_2,rst_1} <= {rst_1,rst_in};
end

//
// generate lock signal
//
always @ (posedge clkin_in) begin
  if ((clkfb_type == 0) & lock_period)
    locked_out <= 1;
  else
    locked_out <= lock_delay & lock_period & ~rst_2;
end

//
// generate the clk0_int
//
always @ (clkin_in) begin
  if (delay_fb_found)
    clk1x <= #delay_fb clkin_in;
end

always @ (posedge clkin_in) begin
    clkin_5050 <= 1;
    #(period/2)
    clkin_5050 <= 0;
end

always @ (clkin_5050) begin
  if (delay_fb_found)
    clk1x_5050 <= #delay_fb clkin_5050;
end

assign clk0_int = (clk1x_type) ? clk1x_5050 : clk1x;

//
// generate the clk2x_int
//
always @(clk1x_5050) begin
  clk1x_shift125 <= #(period/8) clk1x_5050;
  clk1x_shift250 <= #(period/4) clk1x_5050;
end

assign clk2x_2575 = clk1x_5050 & ~clk1x_shift250;
assign clk2x_5050 = clk1x_5050 ^ clk1x_shift250;

always @(clk2x_5050 or clk2x_2575) begin
  if (locked_out)
    clk2x_int = clk2x_5050;
  else
    clk2x_int = clk2x_2575;
end

//
// generate the clk4x_int
//
always @(clk2x_5050) begin
  clk2x_shift <= #(period/8) clk2x_5050;
end

assign clk4x_int = (clk2x_5050 ^ clk2x_shift) & !rst_in & !rst_1 & !rst_2;

//
// generate the clkdv_int
//

always @(posedge clk4x_int) begin
  if (dll_mode_type) begin
    if (count3 == 0)
      divider[3] <= 1;
    if (count3 == 2)
      divider[3] <= 0;
  end
  else
    if (count3 == 0 || count3 == 3)
      divider[3] <= divider[3] + 1;
  count3 <= (count3 + 1) % 6;
end

always @(posedge clk4x_int) begin
  if (count4 == 0)
    divider[4] <= divider[4] + 1;
  count4 <= (count4 + 1) % 4;
end

always @(posedge clk4x_int) begin
  if (dll_mode_type) begin
    if (count5 == 0)
      divider[5] <= 1;
    if (count5 == 4)
      divider[5] <= 0;
  end
  else
    if (count5 == 0 || count5 == 5)
      divider[5] <= divider[5] + 1;
  count5 <= (count5 + 1) % 10;
end

always @(posedge divider[3]) begin
  divider[6] <= divider[6] + 1;
end

always @(posedge clk4x_int) begin
  if (dll_mode_type) begin
    if (count7 == 0)
      divider[7] <= 1;
    if (count7 == 6)
      divider[7] <= 0;
  end
  else
    if (count7 == 0 || count7 == 7)
      divider[7] <= divider[7] + 1;
  count7 <= (count7 + 1) % 14;
end

always @(posedge divider[4]) begin
  divider[8] <= divider[8] + 1;
end

always @(posedge clk4x_int) begin
  if (dll_mode_type) begin
    if (count9 == 0)
      divider[9] <= 1;
    if (count9 == 8)
      divider[9] <= 0;
  end
  else
    if (count9 == 0 || count9 == 9)
      divider[9] <= divider[9] + 1;
  count9 <= (count9 + 1) % 18;
end

always @(posedge divider[5]) begin
  divider[10] <= divider[10] + 1;
end

always @(posedge clk4x_int) begin
  if (dll_mode_type) begin
    if (count11 == 0)
      divider[11] <= 1;
    if (count11 == 10)
      divider[11] <= 0;
  end
  else
    if (count11 == 0 || count11 == 11)
      divider[11] <= divider[11] + 1;
  count11 <= (count11 + 1) % 22;
end

always @(posedge divider[6]) begin
  divider[12] <= divider[12] + 1;
end

always @(posedge clk4x_int) begin
  if (dll_mode_type) begin
    if (count13 == 0)
      divider[13] <= 1;
    if (count13 == 12)
      divider[13] <= 0;
  end
  else
    if (count13 == 0 || count13 == 13)
      divider[13] <= divider[13] + 1;
  count13 <= (count13 + 1) % 26;
end

always @(posedge divider[7]) begin
  divider[14] <= divider[14] + 1;
end

always @(posedge clk4x_int) begin
  if (dll_mode_type) begin
    if (count15 == 0)
      divider[15] <= 1;
    if (count15 == 14)
      divider[15] <= 0;
  end
  else
    if (count15 == 0 || count15 == 15)
      divider[15] <= divider[15] + 1;
  count15 <= (count15 + 1) % 30;
end

always @(posedge divider[8]) begin
  divider[16] <= divider[16] + 1;
end

always @(posedge divider[9]) begin
  divider[18] <= divider[18] + 1;
end

always @(posedge divider[10]) begin
  divider[20] <= divider[20] + 1;
end

always @(posedge divider[11]) begin
  divider[22] <= divider[22] + 1;
end

always @(posedge divider[12]) begin
  divider[24] <= divider[24] + 1;
end

always @(posedge divider[13]) begin
  divider[26] <= divider[26] + 1;
end

always @(posedge divider[14]) begin
  divider[28] <= divider[28] + 1;
end

always @(posedge divider[15]) begin
  divider[30] <= divider[30] + 1;
end

always @(posedge divider[16]) begin
  divider[32] <= divider[32] + 1;
end

assign clkdv_int = divider[divide_type];

//
// generate fx output signal
//

always @(CLKFX) begin
  if (locked_out)
    clkfx_out = 1'b0;
end

always @(posedge locked_out) begin
  if (locked_out) begin
    while (period > 0 && rst_in == 0) begin
      period_fx = period;
      generate = !generate;
      for (p = denominator; p > 0 && rst_in == 0; p = p - 1)
        #(period);
    end
    clkfx_out = 1'b1;
  end
end

always @(generate) begin
  if (period_fx > 0) begin
    period_fx = period_fx * denominator;
    for (d = numerator; d > 1; d = d - 1) begin
      fx_delay = period_fx / d / 2;
      clkfx_int <= 1;
      #(fx_delay);
      clkfx_int <= 0;
      #(fx_delay);
      period_fx = period_fx - 2 * fx_delay;
    end
    clkfx_int <= 1;
    #(period_fx/2);
    clkfx_int <= 0;
  end
end

//
// generate all output signal
//
always @ (clk0_int) begin
    CLK0 <= clk0_int;
end

always @ (clk0_int) begin
    CLK90 <= #(period/4) clk0_int;
end

always @ (clk0_int) begin
    CLK180 <= #(period/2) clk0_int;
end

always @ (clk0_int) begin
    CLK270 <= #(3*period/4) clk0_int;
end

always @ (clk2x_int) begin
    CLK2X <= clk2x_int;
end

always @ (clk2x_int) begin
    CLK2X180 <= #(period/4) clk2x_int;
end

always @ (clkdv_int) begin
  if (locked_out)
    CLKDV <= #(period) clkdv_int;
end

always @ (clkfx_int) begin
    CLKFX <= #(delay_fb) clkfx_int;
end

always @ (clkfx_int) begin
    CLKFX180 <= #(delay_fb) ~clkfx_int;
end

specify
	specparam CLKFBDLYLH = 0:0:0, CLKFBDLYHL = 0:0:0;
	specparam CLKINDLYLH = 0:0:0, CLKINDLYHL = 0:0:0;
	specparam DSSENDLYLH = 0:0:0, DSSENDLYHL = 0:0:0;
	specparam PSCLKDLYLH = 0:0:0, PSCLKDLYHL = 0:0:0;
	specparam PSENDLYLH = 0:0:0, PSENDLYHL = 0:0:0;
	specparam PSINCDECDLYLH = 0:0:0, PSINCDECDLYHL = 0:0:0;
	specparam RSTDLYLH = 0:0:0, RSTDLYHL = 0:0:0;
	specparam LOCKEDDLYLH = 0:0:0, LOCKEDDLYHL = 0:0:0;
	specparam PSDONEDLYLH = 0:0:0, PSDONEDLYHL = 0:0:0;
	specparam STATUS0DLYLH = 0:0:0, STATUS0DLYHL = 0:0:0;
	specparam STATUS1DLYLH = 0:0:0, STATUS1DLYHL = 0:0:0;
	specparam STATUS2DLYLH = 0:0:0, STATUS2DLYHL = 0:0:0;
	specparam STATUS3DLYLH = 0:0:0, STATUS3DLYHL = 0:0:0;
	specparam STATUS4DLYLH = 0:0:0, STATUS4DLYHL = 0:0:0;
	specparam STATUS5DLYLH = 0:0:0, STATUS5DLYHL = 0:0:0;
	specparam STATUS6DLYLH = 0:0:0, STATUS6DLYHL = 0:0:0;
	specparam STATUS7DLYLH = 0:0:0, STATUS7DLYHL = 0:0:0;

	specparam PWCLKINHI = 0:0:0, PWCLKINLO = 0:0:0;
	specparam PWPSCLKHI = 0:0:0, PWPSCLKLO = 0:0:0;
	specparam PWRSTHI = 0:0:0;
	specparam MINPERCLKIN = 1111:1111:1111;
	specparam MINPERPSCLK = 1111:1111:1111;

	specparam SUPSENHIPSCLK = 0:0:0, SUPSENLOPSCLK = 0:0:0;
	specparam HOLDPSENHIPSCLK = 0:0:0, HOLDPSENLOPSCLK = 0:0:0;
	specparam SUPSINCDECHIPSCLK = 0:0:0, SUPSINCDECLOPSCLK = 0:0:0;
	specparam HOLDPSINCDECHIPSCLK = 0:0:0, HOLDPSINCDECLOPSCLK = 0:0:0;

	(CLKIN => LOCKED) = (CLKINDLYLH + LOCKEDDLYLH, CLKINDLYHL + LOCKEDDLYHL);


	$setuphold (posedge PSCLK, posedge PSEN, SUPSENHIPSCLK, HOLDPSENHIPSCLK, notifier);
	$setuphold (posedge PSCLK, negedge PSEN, SUPSENLOPSCLK, HOLDPSENLOPSCLK, notifier);
	$setuphold (posedge PSCLK, posedge PSINCDEC, SUPSINCDECHIPSCLK, HOLDPSINCDECHIPSCLK, notifier);
	$setuphold (posedge PSCLK, negedge PSINCDEC, SUPSINCDECLOPSCLK, HOLDPSINCDECLOPSCLK, notifier);

	$period (posedge CLKIN, MINPERCLKIN, notifier);
	$period (posedge PSCLK, MINPERCLKIN, notifier);
	$width (posedge CLKIN, PWCLKINHI, 0, notifier);
	$width (negedge CLKIN, PWCLKINLO, 0, notifier);
	$width (posedge PSCLK, PWPSCLKHI, 0, notifier);
	$width (negedge PSCLK, PWPSCLKLO, 0, notifier);
	$width (posedge RST, PWRSTHI, 0, notifier);

	specparam PATHPULSE$ = 0;

endspecify

endmodule