mt46v32m16.v 49.5 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 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284
/****************************************************************************************
*
*    File Name:  MT46V32M16.V  
*      Version:  2.1
*         Date:  March 19th, 2002
*        Model:  BUS Functional
*    Simulator:  Aldec, ModemSim, NCDesktop
*
* Dependencies:  None
*
*       Author:  Son P. Huynh
*        Email:  sphuynh@micron.com
*        Phone:  (208) 368-3825
*      Company:  Micron Technology, Inc.
*  Part Number:  MT46V32M16 (8Meg x 16 x 4 Banks)
*
*  Description:  Micron 512Mb SDRAM DDR (Double Data Rate)
*
*   Limitation:  - Doesn't check for 8K-cycle refresh
*
*         Note:  - Set simulator resolution to "ps" accuracy
*                - Set Debug = 0 to disable $display messages
*                - Model assume Clk and Clk# crossing at both edge
*
*   Disclaimer:  THESE DESIGNS ARE PROVIDED "AS IS" WITH NO WARRANTY 
*                WHATSOEVER AND MICRON SPECIFICALLY DISCLAIMS ANY 
*                IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
*                A PARTICULAR PURPOSE, OR AGAINST INFRINGEMENT.
*
*                Copyright  1998 Micron Semiconductor Products, Inc.
*                All rights researved
*
* Rev  Author          Phone         Date        Changes
* ---  --------------  ------------  ----------  ---------------------------------------
* 2.1  Craig Hanson	   612-884-2680	 05/15/2002	 - Updated part from 256Mb to 512Mb
*
* 2.1  Son Huynh       208-368-3825  03/19/2002  - Second Release
*      Micron Technology Inc.                    - Fix tWR and several incompatability
*                                                  between different simulators
*
****************************************************************************************/

// DO NOT CHANGE THE TIMESCALE
// MAKE SURE YOUR SIMULATOR USE "PS" RESOLUTION
`timescale 1ns / 1ps

module mt46v32m16 (Dq, Dqs, Addr, Ba, Clk, Clk_n, Cke, Cs_n, Ras_n, Cas_n, We_n, Dm);

    // Timing Parameters for -75Z (CL = 2)
    parameter tCK       =       7.5;
    parameter tMRD      =      15.0;
    parameter tRAS      =      40.0;
    parameter tRAP      =      20.0;
    parameter tRC       =      65.0;
    parameter tRFC      =      75.0;
    parameter tRCD      =      20.0;
    parameter tRP       =      20.0;
    parameter tRRD      =      15.0;
    parameter tWR       =      15.0;

    // Constant Parameters
    parameter addr_bits =      13;
    parameter data_bits =      16;
    parameter cols_bits =      10;
    parameter mem_sizes = 8388607;

    // Port Declarations
    inout     [data_bits - 1 : 0] Dq;
    inout                 [1 : 0] Dqs;
    input     [addr_bits - 1 : 0] Addr;
    input                 [1 : 0] Ba;
    input                         Clk;
    input                         Clk_n;
    input                         Cke;
    input                         Cs_n;
    input                         Ras_n;
    input                         Cas_n;
    input                         We_n;
    input                 [1 : 0] Dm;

    // Data pair
    reg                           Dm_temp0, Dm_temp1;
    reg                   [1 : 0] Dm_pair0, Dm_pair1;
    reg                   [7 : 0] Dq_temp0, Dq_temp1;
    reg                  [15 : 0] Dq_pair0, Dq_pair1;
    
    // Mode Register
    reg       [addr_bits - 1 : 0] Mode_reg;

    // Internal System Clock
    reg                           CkeZ, Sys_clk;

    // Internal Dqs initialize
    reg                           Dqs_int;

    // Dqs buffer
    reg                   [1 : 0] Dqs_out;

    // Dq buffer
    reg       [data_bits - 1 : 0] Dq_out, Dq_buf;

    // Read pipeline variables
    reg                           Read_cmnd [0 : 8];
    reg                   [1 : 0] Read_bank [0 : 8];
    reg       [cols_bits - 1 : 0] Read_cols [0 : 8];

    // Write pipeline variables
    reg                           Write_cmnd [0 : 2];
    reg                   [1 : 0] Write_bank [0 : 2];
    reg       [cols_bits - 1 : 0] Write_cols [0 : 2];

    // Auto precharge variables
    reg                           Read_precharge  [0 : 3];
    reg                           Write_precharge [0 : 3];
    integer                       Count_precharge [0 : 3];

    // Manual precharge variables
    reg                           A10_precharge  [0 : 8];
    reg                   [1 : 0] Bank_precharge [0 : 8];
    reg                           Cmnd_precharge [0 : 8];

    // Burst terminate variables
    reg                           Cmnd_bst [0 : 8];

    // Memory Banks
    reg       [data_bits - 1 : 0] Bank0 [0 : mem_sizes];
    reg       [data_bits - 1 : 0] Bank1 [0 : mem_sizes];
    reg       [data_bits - 1 : 0] Bank2 [0 : mem_sizes];
    reg       [data_bits - 1 : 0] Bank3 [0 : mem_sizes];

    // Burst counter
    reg       [cols_bits - 1 : 0] Burst_counter;

    // Burst Direction (only used in x32 part)
    reg                           Burst_dir;
    reg                           Burst_dir_pipe [0 : 8];

    // Precharge variables
    reg                           Pc_b0, Pc_b1, Pc_b2, Pc_b3;

    // Activate variables
    reg                           Act_b0, Act_b1, Act_b2, Act_b3;

    // Data IO variables
    reg                           Data_in_enable;
    reg                           Data_out_enable;

    // Internal address mux variables
    //reg       [cols_bits - 1 : 0] Col_brst;
    reg                   [1 : 0] Prev_bank;
    reg                   [1 : 0] Bank_addr;
    reg       [cols_bits - 1 : 0] Cols_addr, Cols_brst, Cols_temp;
    reg       [addr_bits - 1 : 0] Rows_addr;
    reg       [addr_bits - 1 : 0] B0_row_addr;
    reg       [addr_bits - 1 : 0] B1_row_addr;
    reg       [addr_bits - 1 : 0] B2_row_addr;
    reg       [addr_bits - 1 : 0] B3_row_addr;

    // DLL Reset variable
    reg                           DLL_enable;
    reg                           DLL_reset;
    reg                           DLL_done;
    integer                       DLL_count;

    // Commands Decode
    wire      Active_enable   = ~Cs_n & ~Ras_n &  Cas_n &  We_n;
    wire      Aref_enable     = ~Cs_n & ~Ras_n & ~Cas_n &  We_n;
    wire      Burst_term      = ~Cs_n &  Ras_n &  Cas_n & ~We_n;
    wire      Ext_mode_enable = ~Cs_n & ~Ras_n & ~Cas_n & ~We_n &  Ba[0] & ~Ba[1];
    wire      Mode_reg_enable = ~Cs_n & ~Ras_n & ~Cas_n & ~We_n & ~Ba[0] & ~Ba[1];
    wire      Prech_enable    = ~Cs_n & ~Ras_n &  Cas_n & ~We_n;
    wire      Read_enable     = ~Cs_n &  Ras_n & ~Cas_n &  We_n;
    wire      Write_enable    = ~Cs_n &  Ras_n & ~Cas_n & ~We_n;

    // Burst Length Decode
    wire      Burst_length_2  = ~Mode_reg[2] & ~Mode_reg[1] &  Mode_reg[0];
    wire      Burst_length_4  = ~Mode_reg[2] &  Mode_reg[1] & ~Mode_reg[0];
    wire      Burst_length_8  = ~Mode_reg[2] &  Mode_reg[1] &  Mode_reg[0];
    wire      Burst_length_f  =  Mode_reg[2] &  Mode_reg[1] &  Mode_reg[0];

    // CAS Latency Decode
    wire      Cas_latency_15  =  Mode_reg[6] & ~Mode_reg[5] &  Mode_reg[4];
    wire      Cas_latency_2   = ~Mode_reg[6] &  Mode_reg[5] & ~Mode_reg[4];
    wire      Cas_latency_25  =  Mode_reg[6] &  Mode_reg[5] & ~Mode_reg[4];
    wire      Cas_latency_3   = ~Mode_reg[6] &  Mode_reg[5] &  Mode_reg[4];
    wire      Cas_latency_4   =  Mode_reg[6] & ~Mode_reg[5] & ~Mode_reg[4];

    // DQS Buffer
    assign    Dqs = Dqs_out;

    // DQ Buffer
    assign    Dq  = Dq_out;

    // Debug message
    wire      Debug = 1'b1;

    // Timing Check
    time      MRD_chk;
    time      RFC_chk;
    time      RRD_chk;
    time      RAS_chk0, RAS_chk1, RAS_chk2, RAS_chk3;
    time      RAP_chk0, RAP_chk1, RAP_chk2, RAP_chk3;
    time      RC_chk0, RC_chk1, RC_chk2, RC_chk3;
    time      RCD_chk0, RCD_chk1, RCD_chk2, RCD_chk3;
    time      RP_chk0, RP_chk1, RP_chk2, RP_chk3;
    time      WR_chk0, WR_chk1, WR_chk2, WR_chk3;

    initial begin
        CkeZ = 1'b0;
        Sys_clk = 1'b0;
        {Pc_b0, Pc_b1, Pc_b2, Pc_b3} = 4'b0000;
        {Act_b0, Act_b1, Act_b2, Act_b3} = 4'b1111;
        Dqs_int = 1'b0;
        Dqs_out = 1'bz;
        Dq_out = {data_bits{1'bz}};
        Data_in_enable = 1'b0;
        Data_out_enable = 1'b0;
        DLL_enable = 1'b0;
        DLL_reset = 1'b0;
        DLL_done = 1'b0;
        DLL_count = 0;
        MRD_chk = 0;
        RFC_chk = 0;
        RRD_chk = 0;
        {RAS_chk0, RAS_chk1, RAS_chk2, RAS_chk3} = 0;
        {RAP_chk0, RAP_chk1, RAP_chk2, RAP_chk3} = 0;
        {RC_chk0, RC_chk1, RC_chk2, RC_chk3} = 0;
        {RCD_chk0, RCD_chk1, RCD_chk2, RCD_chk3} = 0;
        {RP_chk0, RP_chk1, RP_chk2, RP_chk3} = 0;
        {WR_chk0, WR_chk1, WR_chk2, WR_chk3} = 0;
        $timeformat (-9, 3, " ns", 12);
    end

    // System Clock
    always begin
        @ (posedge Clk) begin
            Sys_clk = CkeZ;
            CkeZ = Cke;
        end
        @ (negedge Clk) begin
            Sys_clk = 1'b0;
        end
    end

    // Burst Decode
    task Burst_Decode;
    begin
        // Advance Burst Counter
        Burst_counter = Burst_counter + 1;

        // Burst Type
        if (Mode_reg[3] === 1'b0) begin                         // Sequential Burst
            if (Burst_dir === 1'b0) begin                       // Forward Direction
                Cols_temp = Cols_addr + 1;
            end else begin                                      // Reverse Direction (x32 only)
                Cols_temp = Cols_addr - 1;
            end
        end else if (Mode_reg[3] === 1'b1) begin                // Interleaved Burst
            Cols_temp[2] =  Burst_counter[2] ^  Cols_brst[2];
            Cols_temp[1] =  Burst_counter[1] ^  Cols_brst[1];
            Cols_temp[0] =  Burst_counter[0] ^  Cols_brst[0];
        end

        // Burst Length
        if (Burst_length_2 === 1'b1) begin                      // Burst Length = 2
            Cols_addr [0] = Cols_temp [0];
        end else if (Burst_length_4 === 1'b1) begin             // Burst Length = 4
            Cols_addr [1 : 0] = Cols_temp [1 : 0];
        end else if (Burst_length_8 === 1'b1) begin             // Burst Length = 8
            Cols_addr [2 : 0] = Cols_temp [2 : 0];
        end else begin                                          // Burst Length = FULL
            Cols_addr = Cols_temp;
        end

        // Data Counter
        if (Burst_length_2 === 1'b1) begin
            if (Burst_counter >= 2) begin
                if (Data_in_enable === 1'b1) begin
                    Data_in_enable = 1'b0;
                end else if (Data_out_enable === 1'b1) begin
                    Data_out_enable = 1'b0;
                end
            end
        end else if (Burst_length_4 === 1'b1) begin
            if (Burst_counter >= 4) begin
                if (Data_in_enable === 1'b1) begin
                    Data_in_enable = 1'b0;
                end else if (Data_out_enable === 1'b1) begin
                    Data_out_enable = 1'b0;
                end
            end
        end else if (Burst_length_8 === 1'b1) begin
            if (Burst_counter >= 8) begin
                if (Data_in_enable === 1'b1) begin
                    Data_in_enable = 1'b0;
                end else if (Data_out_enable === 1'b1) begin
                    Data_out_enable = 1'b0;
                end
            end
        end
    end
    endtask

    // Manual Precharge Pipeline
    task Manual_Precharge_Pipeline;
    begin
        // A10 Precharge Pipeline
        A10_precharge[0] = A10_precharge[1];
        A10_precharge[1] = A10_precharge[2];
        A10_precharge[2] = A10_precharge[3];
        A10_precharge[3] = A10_precharge[4];
        A10_precharge[4] = A10_precharge[5];
        A10_precharge[5] = A10_precharge[6];
        A10_precharge[6] = A10_precharge[7];
        A10_precharge[7] = A10_precharge[8];
        A10_precharge[8] = 1'b0;

        // Bank Precharge Pipeline
        Bank_precharge[0] = Bank_precharge[1];
        Bank_precharge[1] = Bank_precharge[2];
        Bank_precharge[2] = Bank_precharge[3];
        Bank_precharge[3] = Bank_precharge[4];
        Bank_precharge[4] = Bank_precharge[5];
        Bank_precharge[5] = Bank_precharge[6];
        Bank_precharge[6] = Bank_precharge[7];
        Bank_precharge[7] = Bank_precharge[8];
        Bank_precharge[8] = 2'b0;

        // Command Precharge Pipeline
        Cmnd_precharge[0] = Cmnd_precharge[1];
        Cmnd_precharge[1] = Cmnd_precharge[2];
        Cmnd_precharge[2] = Cmnd_precharge[3];
        Cmnd_precharge[3] = Cmnd_precharge[4];
        Cmnd_precharge[4] = Cmnd_precharge[5];
        Cmnd_precharge[5] = Cmnd_precharge[6];
        Cmnd_precharge[6] = Cmnd_precharge[7];
        Cmnd_precharge[7] = Cmnd_precharge[8];
        Cmnd_precharge[8] = 1'b0;

        // Terminate a Read if same bank or all banks
        if (Cmnd_precharge[0] === 1'b1) begin
            if (Bank_precharge[0] === Bank_addr || A10_precharge[0] === 1'b1) begin
                if (Data_out_enable === 1'b1) begin
                    Data_out_enable = 1'b0;
                end
            end
        end
    end
    endtask

    // Burst Terminate Pipeline
    task Burst_Terminate_Pipeline;
    begin
        // Command Precharge Pipeline
        Cmnd_bst[0] = Cmnd_bst[1];
        Cmnd_bst[1] = Cmnd_bst[2];
        Cmnd_bst[2] = Cmnd_bst[3];
        Cmnd_bst[3] = Cmnd_bst[4];
        Cmnd_bst[4] = Cmnd_bst[5];
        Cmnd_bst[5] = Cmnd_bst[6];
        Cmnd_bst[6] = Cmnd_bst[7];
        Cmnd_bst[7] = Cmnd_bst[8];
        Cmnd_bst[8] = 1'b0;

        // Terminate a Read regardless of banks
        if (Cmnd_bst[0] === 1'b1 && Data_out_enable === 1'b1) begin
            Data_out_enable = 1'b0;
        end
    end
    endtask

    // Burst Direction Pipeline (x32 only)
    task Burst_Direction_Pipeline;
    begin
        // read command pipeline
        Burst_dir_pipe [0] = Burst_dir_pipe [1];
        Burst_dir_pipe [1] = Burst_dir_pipe [2];
        Burst_dir_pipe [2] = Burst_dir_pipe [3];
        Burst_dir_pipe [3] = Burst_dir_pipe [4];
        Burst_dir_pipe [4] = Burst_dir_pipe [5];
        Burst_dir_pipe [5] = Burst_dir_pipe [6];
        Burst_dir_pipe [6] = Burst_dir_pipe [7];
        Burst_dir_pipe [7] = Burst_dir_pipe [8];
        Burst_dir_pipe [8] = 1'b0;
    end
    endtask

    // Dq and Dqs Drivers
    task Dq_Dqs_Drivers;
    begin
        // read command pipeline
        Read_cmnd [0] = Read_cmnd [1];
        Read_cmnd [1] = Read_cmnd [2];
        Read_cmnd [2] = Read_cmnd [3];
        Read_cmnd [3] = Read_cmnd [4];
        Read_cmnd [4] = Read_cmnd [5];
        Read_cmnd [5] = Read_cmnd [6];
        Read_cmnd [6] = Read_cmnd [7];
        Read_cmnd [7] = Read_cmnd [8];
        Read_cmnd [8] = 1'b0;

        // read bank pipeline
        Read_bank [0] = Read_bank [1];
        Read_bank [1] = Read_bank [2];
        Read_bank [2] = Read_bank [3];
        Read_bank [3] = Read_bank [4];
        Read_bank [4] = Read_bank [5];
        Read_bank [5] = Read_bank [6];
        Read_bank [6] = Read_bank [7];
        Read_bank [7] = Read_bank [8];
        Read_bank [8] = 2'b0;

        // read column pipeline
        Read_cols [0] = Read_cols [1];
        Read_cols [1] = Read_cols [2];
        Read_cols [2] = Read_cols [3];
        Read_cols [3] = Read_cols [4];
        Read_cols [4] = Read_cols [5];
        Read_cols [5] = Read_cols [6];
        Read_cols [6] = Read_cols [7];
        Read_cols [7] = Read_cols [8];
        Read_cols [8] = 0;

        // Initialize Read command
        if (Read_cmnd [0] === 1'b1) begin
            Data_out_enable = 1'b1;
            Bank_addr = Read_bank [0];
            Cols_addr = Read_cols [0];
            Cols_brst = Cols_addr [2 : 0];
            Burst_counter = 0;
            Burst_dir = Burst_dir_pipe [0];

            // Row Address Mux
            case (Bank_addr)
                2'd0    : Rows_addr = B0_row_addr;
                2'd1    : Rows_addr = B1_row_addr;
                2'd2    : Rows_addr = B2_row_addr;
                2'd3    : Rows_addr = B3_row_addr;
                default : $display ("At time %t ERROR: Invalid Bank Address", $time);
            endcase
        end

        // Toggle Dqs during Read command
        if (Data_out_enable === 1'b1) begin
            Dqs_int = 1'b0;
            if (Dqs_out === 2'b00) begin
                Dqs_out = 2'b11;
            end else if (Dqs_out === 2'b11) begin
                Dqs_out = 2'b00;
            end else begin
                Dqs_out = 2'b00;
            end
        end else if (Data_out_enable === 1'b0 && Dqs_int === 1'b0) begin
            Dqs_out = 2'bzz;
        end

        // Initialize dqs for Read command
        if (Read_cmnd [2] === 1'b1) begin
            if (Data_out_enable === 1'b0) begin
                Dqs_int = 1'b1;
                Dqs_out = 2'b00;
            end
        end

        // Read latch
        if (Data_out_enable === 1'b1) begin
            // output data
            case (Bank_addr)
                2'd0    : Dq_out = Bank0 [{Rows_addr, Cols_addr}];
                2'd1    : Dq_out = Bank1 [{Rows_addr, Cols_addr}];
                2'd2    : Dq_out = Bank2 [{Rows_addr, Cols_addr}];
                2'd3    : Dq_out = Bank3 [{Rows_addr, Cols_addr}];
                default : $display ("At time %t ERROR: Invalid Bank Address", $time);
            endcase

            // Increase burst counter
            Burst_Decode;
        end else begin
            Dq_out = {data_bits{1'bz}};
        end
    end
    endtask

    // Write FIFO and DM Mask Logic
    task Write_FIFO_DM_Mask_Logic;
    begin
        // Write command pipeline
        Write_cmnd [0] = Write_cmnd [1];
        Write_cmnd [1] = Write_cmnd [2];
        Write_cmnd [2] = 1'b0;

        // Write command pipeline
        Write_bank [0] = Write_bank [1];
        Write_bank [1] = Write_bank [2];
        Write_bank [2] = 2'b0;

        // Write column pipeline
        Write_cols [0] = Write_cols [1];
        Write_cols [1] = Write_cols [2];
        Write_cols [2] = {cols_bits{1'b0}};

        // Initialize Write command
        if (Write_cmnd [0] === 1'b1) begin
            Data_in_enable = 1'b1;
            Bank_addr = Write_bank [0];
            Cols_addr = Write_cols [0];
            Cols_brst = Cols_addr [2 : 0];
            Burst_counter = 0;
            Burst_dir = Burst_dir_pipe [0];

            // Row address mux
            case (Bank_addr)
                2'd0    : Rows_addr = B0_row_addr;
                2'd1    : Rows_addr = B1_row_addr;
                2'd2    : Rows_addr = B2_row_addr;
                2'd3    : Rows_addr = B3_row_addr;
                default : $display ("At time %t ERROR: Invalid Row Address", $time);
            endcase
        end

        // Write data
        if (Data_in_enable === 1'b1) begin
            // Write first data
            if (Dm_pair0 [0] === 1'b0 || Dm_pair1 [0] === 1'b0) begin
                // Data Buffer
                case (Bank_addr)
                    2'd0    : Dq_buf = Bank0 [{Rows_addr, Cols_addr}];
                    2'd1    : Dq_buf = Bank1 [{Rows_addr, Cols_addr}];
                    2'd2    : Dq_buf = Bank2 [{Rows_addr, Cols_addr}];
                    2'd3    : Dq_buf = Bank3 [{Rows_addr, Cols_addr}];
                    default : $display ("At time %t ERROR: Invalid Bank Address", $time);
                endcase

                // Data mask
                if (Dm_pair0 [0] === 1'b0) begin
                    Dq_buf [7 : 0] = Dq_pair0 [7 : 0];
                end

                if (Dm_pair1 [0] === 1'b0) begin
                    Dq_buf [15 : 8] = Dq_pair1 [7 : 0];
                end

                // Write Data
                case (Bank_addr)
                    2'd0    : Bank0 [{Rows_addr, Cols_addr}] = Dq_buf;
                    2'd1    : Bank1 [{Rows_addr, Cols_addr}] = Dq_buf;
                    2'd2    : Bank2 [{Rows_addr, Cols_addr}] = Dq_buf;
                    2'd3    : Bank3 [{Rows_addr, Cols_addr}] = Dq_buf;
                    default : $display ("At time %t ERROR: Invalid Bank Address", $time);
                endcase
            end

            //  Increase Burst Counter
            Burst_Decode;

            // Write second data
            if (Dm_pair0 [1] === 1'b0 || Dm_pair1 [1] === 1'b0) begin
                // Data Buffer
                case (Bank_addr)
                    2'd0    : Dq_buf = Bank0 [{Rows_addr, Cols_addr}];
                    2'd1    : Dq_buf = Bank1 [{Rows_addr, Cols_addr}];
                    2'd2    : Dq_buf = Bank2 [{Rows_addr, Cols_addr}];
                    2'd3    : Dq_buf = Bank3 [{Rows_addr, Cols_addr}];
                    default : $display ("At time %t ERROR: Invalid Bank Address", $time);
                endcase

                // Data mask
                if (Dm_pair0 [1] === 1'b0) begin
                    Dq_buf [7 : 0] = Dq_pair0 [15 : 8];
                end
                
                if (Dm_pair1 [1] === 1'b0) begin
                    Dq_buf [15 : 8] = Dq_pair1 [15 : 8];
                end

                // Write Data
                case (Bank_addr)
                    2'd0    : Bank0 [{Rows_addr, Cols_addr}] = Dq_buf;
                    2'd1    : Bank1 [{Rows_addr, Cols_addr}] = Dq_buf;
                    2'd2    : Bank2 [{Rows_addr, Cols_addr}] = Dq_buf;
                    2'd3    : Bank3 [{Rows_addr, Cols_addr}] = Dq_buf;
                    default : $display ("At time %t ERROR: Invalid Bank Address", $time);
                endcase
            end
            
            // Increase Burst Counter
            Burst_Decode;

            // tWR start and tWTR check
            if (Dm_pair0 [0] === 1'b0 || Dm_pair1 [0] === 1'b0 || Dm_pair0 [1] === 1'b0 || Dm_pair1 [1] === 1'b0)  begin
                case (Bank_addr)
                    2'd0    : WR_chk0 = $time;
                    2'd1    : WR_chk1 = $time;
                    2'd2    : WR_chk2 = $time;
                    2'd3    : WR_chk3 = $time;
                    default : $display ("At time %t ERROR: Invalid Bank Address (tWR)", $time);
                endcase

                // tWTR check
                if (Read_enable === 1'b1 && Bank_addr === Ba) begin
                    $display ("At time %t ERROR: tWTR violation during Read", $time);
                end
            end
        end
    end
    endtask

    // Auto Precharge Calculation
    task Auto_Precharge_Calculation;
    begin
        // Precharge counter
        if (Read_precharge [0] === 1'b1 || Write_precharge [0] === 1'b1) begin
            Count_precharge [0] = Count_precharge [0] + 1;
        end
        if (Read_precharge [1] === 1'b1 || Write_precharge [1] === 1'b1) begin
            Count_precharge [1] = Count_precharge [1] + 1;
        end
        if (Read_precharge [2] === 1'b1 || Write_precharge [2] === 1'b1) begin
            Count_precharge [2] = Count_precharge [2] + 1;
        end
        if (Read_precharge [3] === 1'b1 || Write_precharge [3] === 1'b1) begin
            Count_precharge [3] = Count_precharge [3] + 1;
        end

        // Read with AutoPrecharge Calculation
        //      The device start internal precharge when:
        //          1.  Meet tRAS requirement
        //          2.  BL/2 cycles after command
        if ((Read_precharge[0] === 1'b1) && ($time - RAS_chk0 >= tRAS)) begin
            if ((Burst_length_2 === 1'b1 && Count_precharge[0] >= 1)  ||
                (Burst_length_4 === 1'b1 && Count_precharge[0] >= 2)  ||
                (Burst_length_8 === 1'b1 && Count_precharge[0] >= 4)) begin
                Pc_b0 = 1'b1;
                Act_b0 = 1'b0;
                RP_chk0 = $time;
                Read_precharge[0] = 1'b0;
            end
        end
        if ((Read_precharge[1] === 1'b1) && ($time - RAS_chk1 >= tRAS)) begin
            if ((Burst_length_2 === 1'b1 && Count_precharge[1] >= 1)  ||
                (Burst_length_4 === 1'b1 && Count_precharge[1] >= 2)  ||
                (Burst_length_8 === 1'b1 && Count_precharge[1] >= 4)) begin
                Pc_b1 = 1'b1;
                Act_b1 = 1'b0;
                RP_chk1 = $time;
                Read_precharge[1] = 1'b0;
            end
        end
        if ((Read_precharge[2] === 1'b1) && ($time - RAS_chk2 >= tRAS)) begin
            if ((Burst_length_2 === 1'b1 && Count_precharge[2] >= 1)  ||
                (Burst_length_4 === 1'b1 && Count_precharge[2] >= 2)  ||
                (Burst_length_8 === 1'b1 && Count_precharge[2] >= 4)) begin
                Pc_b2 = 1'b1;
                Act_b2 = 1'b0;
                RP_chk2 = $time;
                Read_precharge[2] = 1'b0;
            end
        end
        if ((Read_precharge[3] === 1'b1) && ($time - RAS_chk3 >= tRAS)) begin
            if ((Burst_length_2 === 1'b1 && Count_precharge[3] >= 1)  ||
                (Burst_length_4 === 1'b1 && Count_precharge[3] >= 2)  ||
                (Burst_length_8 === 1'b1 && Count_precharge[3] >= 4)) begin
                Pc_b3 = 1'b1;
                Act_b3 = 1'b0;
                RP_chk3 = $time;
                Read_precharge[3] = 1'b0;
            end
        end

        // Write with AutoPrecharge Calculation
        //      The device start internal precharge when:
        //          1.  Meet tRAS requirement
        //          2.  Two clock after last burst
        // Since tWR is time base, the model will compensate tRP
        if ((Write_precharge[0] === 1'b1) && ($time - RAS_chk0 >= tRAS)) begin
            if ((Burst_length_2 === 1'b1 && Count_precharge [0] >= 4) ||
                (Burst_length_4 === 1'b1 && Count_precharge [0] >= 5) ||
                (Burst_length_8 === 1'b1 && Count_precharge [0] >= 7)) begin
                Pc_b0 = 1'b1;
                Act_b0 = 1'b0;
                RP_chk0 = $time - ((2 * tCK) - tWR);
                Write_precharge[0] = 1'b0;
            end
        end
        if ((Write_precharge[1] === 1'b1) && ($time - RAS_chk1 >= tRAS)) begin
            if ((Burst_length_2 === 1'b1 && Count_precharge [1] >= 4) ||
                (Burst_length_4 === 1'b1 && Count_precharge [1] >= 5) ||
                (Burst_length_8 === 1'b1 && Count_precharge [1] >= 7)) begin
                Pc_b1 = 1'b1;
                Act_b1 = 1'b0;
                RP_chk1 = $time - ((2 * tCK) - tWR);
                Write_precharge[1] = 1'b0;
            end
        end
        if ((Write_precharge[2] === 1'b1) && ($time - RAS_chk2 >= tRAS)) begin
            if ((Burst_length_2 === 1'b1 && Count_precharge [2] >= 4) ||
                (Burst_length_4 === 1'b1 && Count_precharge [2] >= 5) ||
                (Burst_length_8 === 1'b1 && Count_precharge [2] >= 7)) begin
                Pc_b2 = 1'b1;
                Act_b2 = 1'b0;
                RP_chk2 = $time - ((2 * tCK) - tWR);
                Write_precharge[2] = 1'b0;
            end
        end
        if ((Write_precharge[3] === 1'b1) && ($time - RAS_chk3 >= tRAS)) begin
            if ((Burst_length_2 === 1'b1 && Count_precharge [3] >= 4) ||
                (Burst_length_4 === 1'b1 && Count_precharge [3] >= 5) ||
                (Burst_length_8 === 1'b1 && Count_precharge [3] >= 7)) begin
                Pc_b3 = 1'b1;
                Act_b3 = 1'b0;
                RP_chk3 = $time - ((2 * tCK) - tWR);
                Write_precharge[3] = 1'b0;
            end
        end
    end
    endtask

    // DLL Counter
    task DLL_Counter;
    begin
        if (DLL_reset === 1'b1 && DLL_done === 1'b0) begin
            DLL_count = DLL_count + 1;
            if (DLL_count >= 200) begin
                DLL_done = 1'b1;
            end
        end
    end
    endtask

    // Control Logic
    task Control_Logic;
    begin
        // Auto Refresh
        if (Aref_enable === 1'b1) begin
            // Display Debug Message
            if (Debug) begin
                $display ("At time %t AREF : Auto Refresh", $time);
            end
            
            // Auto Refresh to Auto Refresh
            if ($time - RFC_chk < tRFC) begin
                $display ("At time %t ERROR: tRFC violation during Auto Refresh", $time);
            end
            
            // Precharge to Auto Refresh
            if (($time - RP_chk0 < tRP) || ($time - RP_chk1 < tRP) ||
                ($time - RP_chk2 < tRP) || ($time - RP_chk3 < tRP)) begin
                $display ("At time %t ERROR: tRP violation during Auto Refresh", $time);
            end
            
            // Precharge to Auto Refresh
            if (Pc_b0 === 1'b0 || Pc_b1 === 1'b0 || Pc_b2 === 1'b0 || Pc_b3 === 1'b0) begin
                $display ("At time %t ERROR: All banks must be Precharge before Auto Refresh", $time);
            end
            
            // Record Current tRFC time
            RFC_chk = $time;
        end
    
        // Extended Mode Register
        if (Ext_mode_enable === 1'b1) begin
            if (Debug) begin
                $display ("At time %t EMR  : Extended Mode Register", $time);
            end
            if (Pc_b0 === 1'b1 && Pc_b1 === 1'b1 && Pc_b2 === 1'b1 && Pc_b3 === 1'b1) begin
                if (Addr[0] === 1'b0) begin
                    DLL_enable = 1'b1;
                    if (Debug) begin
                        $display ("At time %t EMR  : Enable DLL", $time);
                    end
                end else begin
                    DLL_enable = 1'b0;
                    if (Debug) begin
                        $display ("At time %t EMR  : Disable DLL", $time);
                    end
                end
            end else begin
                $display ("At time %t ERROR: all banks must be Precharge before Extended Mode Register", $time);
            end

            // Precharge to EMR
            if (($time - RP_chk0 < tRP) || ($time - RP_chk1 < tRP) ||
                ($time - RP_chk2 < tRP) || ($time - RP_chk3 < tRP)) begin
                $display ("At time %t ERROR: tRP violation during Extended Mode Register", $time);
            end

            // LMR/EMR to LMR/EMR
            if ($time - MRD_chk < tMRD) begin
                $display ("At time %t ERROR: tMRD violation during Extended Mode Register", $time);
            end

            // Record current tMRD time
            MRD_chk = $time;
        end
    
        // Load Mode Register
        if (Mode_reg_enable === 1'b1) begin
            if (Debug) begin
                $display ("At time %t LMR  : Load Mode Register", $time);
            end

            // Register Mode
            Mode_reg = Addr;

            // DLL Reset
            if (DLL_enable === 1'b1 && Addr [8] === 1'b1) begin
                DLL_reset = 1'b1;
                DLL_done = 1'b0;
                DLL_count = 0;
            end else if (DLL_enable === 1'b1 && DLL_reset === 1'b0 && Addr [8] === 1'b0) begin
                $display ("At time %t ERROR: DLL is ENABLE: DLL RESET is required.", $time);
            end else if (DLL_enable === 1'b0 && Addr [8] === 1'b1) begin
                $display ("At time %t ERROR: DLL is DISABLE: DLL RESET will be ignored.", $time);
            end

            // Precharge to LMR
            if (Pc_b0 === 1'b0 || Pc_b1 === 1'b0 || Pc_b2 === 1'b0 || Pc_b3 === 1'b0) begin
                $display ("At time %t ERROR: all banks must be Precharge before Load Mode Register", $time);
            end

            // Precharge to LMR
            if (($time - RP_chk0 < tRP) || ($time - RP_chk1 < tRP) ||
                ($time - RP_chk2 < tRP) || ($time - RP_chk3 < tRP)) begin
                $display ("At time %t ERROR: tRP violation during Load Mode Register", $time);
            end

            // LMR/EMR to LMR/EMR
            if ($time - MRD_chk < tMRD) begin
                $display ("At time %t ERROR: tMRD violation during Load Mode Register", $time);
            end

            // Burst Length
            if (~((Addr[2 : 0] ===  3'b001) ||
                (Addr[2 : 0] ===  3'b010) ||
                (Addr[2 : 0] ===  3'b011) ||
                (Addr[3 : 0] === 4'b0111))) begin
                $display ("At time %t ERROR: Burst Length not supported", $time);
            end

            // CAS Latency
            if (~((Addr[6 : 4] === 3'b010) ||
                (Addr[6 : 4] === 3'b110))) begin
                $display ("At time %t ERROR: CAS Latency not supported", $time);
            end

            // Record current tMRD time
            MRD_chk = $time;
        end

        // Activate Block
        if (Active_enable === 1'b1) begin
            // Display Debug Message
            if (Debug) begin
                $display ("At time %t ACT  : Bank = %d Row = %d", $time, Ba, Addr);
            end

            // Activate an open bank can corrupted.
            if ((Ba === 2'b00 && Pc_b0  === 1'b0) || (Ba === 2'b01 && Pc_b1  === 1'b0) ||
                (Ba === 2'b10 && Pc_b2  === 1'b0) || (Ba === 2'b11 && Pc_b3  === 1'b0)) begin
                $display ("At time %t ERROR: Bank = %d is already activated - data can be corrupted", $time, Ba);
            end
            
            // Activate Bank 0
            if (Ba === 2'b00 && Pc_b0 === 1'b1) begin
                // Activate to Activate (same bank)
                if ($time - RC_chk0 < tRC) begin
                    $display ("At time %t ERROR: tRC violation during Activate bank %d", $time, Ba);
                end

                // Precharge to Activate
                if ($time - RP_chk0 < tRP) begin
                    $display ("At time %t ERROR: tRP violation during Activate bank %d", $time, Ba);
                end

                // Record variables for checking violation
                Act_b0 = 1'b1;
                Pc_b0 = 1'b0;
                B0_row_addr = Addr;
                RC_chk0  = $time;
                RCD_chk0 = $time;
                RAS_chk0 = $time;
                RAP_chk0 = $time;
            end

            // Activate Bank 1
            if (Ba === 2'b01 && Pc_b1 === 1'b1) begin
                // Activate to Activate (same bank)
                if ($time - RC_chk1 < tRC) begin
                    $display ("At time %t ERROR: tRC violation during Activate bank %d", $time, Ba);
                end

                // Precharge to Activate
                if ($time - RP_chk1 < tRP) begin
                    $display ("At time %t ERROR: tRP violation during Activate bank %d", $time, Ba);
                end

                // Record variables for checking violation
                Act_b1 = 1'b1;
                Pc_b1 = 1'b0;
                B1_row_addr = Addr;
                RC_chk1  = $time;
                RCD_chk1 = $time;
                RAS_chk1 = $time;
                RAP_chk1 = $time;
            end

            // Activate Bank 2
            if (Ba === 2'b10 && Pc_b2 === 1'b1) begin
                // Activate to Activate (same bank)
                if ($time - RC_chk2 < tRC) begin
                    $display ("At time %t ERROR: tRC violation during Activate bank %d", $time, Ba);
                end

                // Precharge to Activate
                if ($time - RP_chk2 < tRP) begin
                    $display ("At time %t ERROR: tRP violation during Activate bank %d", $time, Ba);
                end

                // Record variables for checking violation
                Act_b2 = 1'b1;
                Pc_b2 = 1'b0;
                B2_row_addr = Addr;
                RC_chk2  = $time;
                RCD_chk2 = $time;
                RAS_chk2 = $time;
                RAP_chk2 = $time;
            end

            // Activate Bank 3
            if (Ba === 2'b11 && Pc_b3 === 1'b1) begin
                // Activate to Activate (same bank)
                if ($time - RC_chk3 < tRC) begin
                    $display ("At time %t ERROR: tRC violation during Activate bank %d", $time, Ba);
                end

                // Precharge to Activate
                if ($time - RP_chk3 < tRP) begin
                    $display ("At time %t ERROR: tRP violation during Activate bank %d", $time, Ba);
                end

                // Record variables for checking violation
                Act_b3 = 1'b1;
                Pc_b3 = 1'b0;
                B3_row_addr = Addr;
                RC_chk3  = $time;
                RCD_chk3 = $time;
                RAS_chk3 = $time;
                RAP_chk3 = $time;
            end

            // Activate to Activate (different bank)
            if ((Prev_bank != Ba) && ($time - RRD_chk < tRRD)) begin
                $display ("At time %t ERROR: tRRD violation during Activate bank = %d", $time, Ba);
            end
            
            // AutoRefresh to Activate
            if ($time - RFC_chk < tRFC) begin
                $display ("At time %t ERROR: tRFC violation during Activate bank %d", $time, Ba);
            end

            // Record variable for checking violation
            RRD_chk = $time;
            Prev_bank = Ba;
        end
    
        // Precharge Block - consider NOP if bank already precharged or in process of precharging
        if (Prech_enable === 1'b1) begin
            // Display Debug Message
            if (Debug) begin
                $display ("At time %t PRE  : Addr[10] = %b, Bank = %b, ", $time, Addr[10], Ba);
            end

            // EMR or LMR to Precharge
            if ($time - MRD_chk < tMRD) begin
                $display ("At time %t ERROR: tMRD violation during Precharge", $time);
            end

            // Precharge bank 0
            if ((Addr[10] === 1'b1 || (Addr[10] === 1'b0 && Ba === 2'b00)) && Act_b0 === 1'b1) begin
                Act_b0 = 1'b0;
                Pc_b0 = 1'b1;
                RP_chk0 = $time;
                
                // Activate to Precharge Bank
                if ($time - RAS_chk0 < tRAS) begin
                    $display ("At time %t ERROR: tRAS violation during Precharge", $time);
                end
                
                // tWR violation check for Write
                if ($time - WR_chk0 < tWR) begin
                    $display ("At time %t ERROR: tWR violation during Precharge", $time);
                end
            end

            // Precharge bank 1
            if ((Addr[10] === 1'b1 || (Addr[10] === 1'b0 && Ba === 2'b01)) && Act_b1 === 1'b1) begin
                Act_b1 = 1'b0;
                Pc_b1 = 1'b1;
                RP_chk1 = $time;

                // Activate to Precharge Bank 1
                if ($time - RAS_chk1 < tRAS) begin
                    $display ("At time %t ERROR: tRAS violation during Precharge", $time);
                end
                
                // tWR violation check for Write
                if ($time - WR_chk1 < tWR) begin
                    $display ("At time %t ERROR: tWR violation during Precharge", $time);
                end
            end

            // Precharge bank 2
            if ((Addr[10] === 1'b1 || (Addr[10] === 1'b0 && Ba === 2'b10)) && Act_b2 === 1'b1) begin
                Act_b2 = 1'b0;
                Pc_b2 = 1'b1;
                RP_chk2 = $time;
                
                // Activate to Precharge Bank 2
                if ($time - RAS_chk2 < tRAS) begin
                    $display ("At time %t ERROR: tRAS violation during Precharge", $time);
                end
                
                // tWR violation check for Write
                if ($time - WR_chk2 < tWR) begin
                    $display ("At time %t ERROR: tWR violation during Precharge", $time);
                end
            end

            // Precharge bank 3
            if ((Addr[10] === 1'b1 || (Addr[10] === 1'b0 && Ba === 2'b11)) && Act_b3 === 1'b1) begin
                Act_b3 = 1'b0;
                Pc_b3 = 1'b1;
                RP_chk3 = $time;
                
                // Activate to Precharge Bank 3
                if ($time - RAS_chk3 < tRAS) begin
                    $display ("At time %t ERROR: tRAS violation during Precharge", $time);
                end
                
                // tWR violation check for Write
                if ($time - WR_chk3 < tWR) begin
                    $display ("At time %t ERROR: tWR violation during Precharge", $time);
                end
            end

            // Pipeline for READ
            if (Cas_latency_15 === 1'b1) begin
                A10_precharge[3] = Addr[10];
                Bank_precharge[3] = Ba;
                Cmnd_precharge[3] = 1'b1;
            end else if (Cas_latency_2 === 1'b1) begin
                A10_precharge[4] = Addr[10];
                Bank_precharge[4] = Ba;
                Cmnd_precharge[4] = 1'b1;
            end else if (Cas_latency_25 === 1'b1) begin
                A10_precharge[5] = Addr[10];
                Bank_precharge[5] = Ba;
                Cmnd_precharge[5] = 1'b1;
            end else if (Cas_latency_3 === 1'b1) begin
                A10_precharge[6] = Addr[10];
                Bank_precharge[6] = Ba;
                Cmnd_precharge[6] = 1'b1;
            end else if (Cas_latency_4 === 1'b1) begin
                A10_precharge[8] = Addr[10];
                Bank_precharge[8] = Ba;
                Cmnd_precharge[8] = 1'b1;
            end
        end
    
        // Burst terminate
        if (Burst_term === 1'b1) begin
            // Display Debug Message
            if (Debug) begin
                $display ("At time %t BST  : Burst Terminate",$time);
            end

            // Burst Terminate Command Pipeline for Read
            if (Cas_latency_15 === 1'b1) begin
                Cmnd_bst [3] = 1'b1;
            end else if (Cas_latency_2 === 1'b1) begin
                Cmnd_bst [4] = 1'b1;
            end else if (Cas_latency_25 === 1'b1) begin
                Cmnd_bst [5] = 1'b1;
            end else if (Cas_latency_3 === 1'b1) begin
                Cmnd_bst [6] = 1'b1;
            end else if (Cas_latency_4 === 1'b1) begin
                Cmnd_bst [8] = 1'b1;
            end

            // Illegal to burst terminate a Write
            if (Data_in_enable === 1'b1) begin
                $display ("At time %t ERROR: It's illegal to burst terminate a Write", $time);
            end
            
            // Illegal to burst terminate a Read with Auto Precharge
            if (Read_precharge[0] === 1'b1 || Read_precharge[1] === 1'b1 ||
                Read_precharge[2] === 1'b1 || Read_precharge[3] === 1'b1) begin
                $display ("At time %t ERROR: It's illegal to burst terminate a Read with Auto Precharge", $time);
            end
        end
        
        // Read Command
        if (Read_enable === 1'b1) begin
            // Display Debug Message
            if (Debug) begin
                $display ("At time %t READ : Bank = %d Col = %d", $time, Ba, Addr [9 : 0]);
            end

            // CAS Latency pipeline
            if (Cas_latency_15 === 1'b1) begin
                Read_cmnd [3] = 1'b1;
                Read_bank [3] = Ba;
                Read_cols [3] = Addr [9 : 0];
                Burst_dir_pipe [3] = Burst_length_f & Addr [0];
            end else if (Cas_latency_2 === 1'b1) begin
                Read_cmnd [4] = 1'b1;
                Read_bank [4] = Ba;
                Read_cols [4] = Addr [9 : 0];
                Burst_dir_pipe [4] = Burst_length_f & Addr [0];
            end else if (Cas_latency_25 === 1'b1) begin
                Read_cmnd [5] = 1'b1;
                Read_bank [5] = Ba;
                Read_cols [5] = Addr [9 : 0];
                Burst_dir_pipe [5] = Burst_length_f & Addr [0];
            end else if (Cas_latency_3 === 1'b1) begin
                Read_cmnd [6] = 1'b1;
                Read_bank [6] = Ba;
                Read_cols [6] = Addr [9 : 0];
                Burst_dir_pipe [6] = Burst_length_f & Addr [0];
            end else if (Cas_latency_4 === 1'b1) begin
                Read_cmnd [8] = 1'b1;
                Read_bank [8] = Ba;
                Read_cols [8] = Addr [9 : 0];
                Burst_dir_pipe [8] = Burst_length_f & Addr [0];
            end

            // Terminate a Write
            if (Data_in_enable === 1'b1) begin
                Data_in_enable = 1'b0;
            end

            // Interrupt a Read with Auto Precharge (same bank only)
            if (Read_precharge [Ba] === 1'b1) begin
                $display ("At time %t ERROR: It's illegal to interrupt a Write with Auto Precharge", $time);
            end

            // Activate to Read
            if ((Ba === 2'b00 && Pc_b0 === 1'b1) || (Ba === 2'b01 && Pc_b1 === 1'b1) ||
                (Ba === 2'b10 && Pc_b2 === 1'b1) || (Ba === 2'b11 && Pc_b3 === 1'b1)) begin
                $display("At time %t ERROR: Bank is not Activated for Read", $time);
            end

            // Activate to Read without Auto Precharge
            if ((Addr [10] === 1'b0 && Ba === 2'b00 && $time - RCD_chk0 < tRCD) ||
                (Addr [10] === 1'b0 && Ba === 2'b01 && $time - RCD_chk1 < tRCD) ||
                (Addr [10] === 1'b0 && Ba === 2'b10 && $time - RCD_chk2 < tRCD) ||
                (Addr [10] === 1'b0 && Ba === 2'b11 && $time - RCD_chk3 < tRCD)) begin
                $display("At time %t ERROR: tRCD violation during Read", $time);
            end

            // Activate to Read with Auto Precharge
            if ((Addr [10] === 1'b1 && Ba === 2'b00 && $time - RAP_chk0 < tRAP) ||
                (Addr [10] === 1'b1 && Ba === 2'b01 && $time - RAP_chk1 < tRAP) ||
                (Addr [10] === 1'b1 && Ba === 2'b10 && $time - RAP_chk2 < tRAP) ||
                (Addr [10] === 1'b1 && Ba === 2'b11 && $time - RAP_chk3 < tRAP)) begin
                $display ("At time %t ERROR: tRAP violation during Read", $time);
            end

            // Auto Precharge
            if (Addr[10] === 1'b1) begin
                Read_precharge [Ba]= 1'b1;
                Count_precharge [Ba]= 0;
            end

            // Check for DLL reset before Read
            if (DLL_reset === 1'b1 && DLL_done === 1'b0) begin
                $display ("At time %t ERROR: DLL Reset not complete", $time);
            end
        end

        // Write Command
        if (Write_enable === 1'b1) begin
            // Display Debug Message
            if (Debug) begin
                $display ("At time %t WRITE: Bank = %d Col = %d", $time, Ba, Addr [9 : 0]);
            end

            // Pipeline for Write
            Write_cmnd [2] = 1'b1;
            Write_bank [2] = Ba;
            Write_cols [2] = Addr [9 : 0];
            Burst_dir_pipe [2] = Burst_length_f & Addr [0];

            // Interrupt a Write with Auto Precharge (same bank only)
            if (Write_precharge [Ba] === 1'b1) begin
                $display ("At time %t ERROR: It's illegal to interrupt a Write with Auto Precharge", $time);
            end

            // Activate to Write
            if ((Ba === 2'b00 && Pc_b0 === 1'b1) || (Ba === 2'b01 && Pc_b1 === 1'b1) ||
                (Ba === 2'b10 && Pc_b2 === 1'b1) || (Ba === 2'b11 && Pc_b3 === 1'b1)) begin
                $display("At time %t ERROR: Bank is not Activated for Write", $time);
            end

            // Activate to Write
            if ((Ba === 2'b00 && $time - RCD_chk0 < tRCD) ||
                (Ba === 2'b01 && $time - RCD_chk1 < tRCD) ||
                (Ba === 2'b10 && $time - RCD_chk2 < tRCD) ||
                (Ba === 2'b11 && $time - RCD_chk3 < tRCD)) begin
                $display("At time %t ERROR: tRCD violation during Write to Bank %d", $time, Ba);
            end

            // Auto Precharge
            if (Addr[10] === 1'b1) begin
                Write_precharge [Ba]= 1'b1;
                Count_precharge [Ba]= 0;
            end
        end
    end
    endtask

    // Main Logic
    always @ (posedge Sys_clk) begin
        Manual_Precharge_Pipeline;
        Burst_Terminate_Pipeline;
        Burst_Direction_Pipeline;
        Dq_Dqs_Drivers;
        Write_FIFO_DM_Mask_Logic;
        Auto_Precharge_Calculation;
        DLL_Counter;
        Control_Logic;
    end

    always @ (negedge Sys_clk) begin
        Manual_Precharge_Pipeline;
        Burst_Terminate_Pipeline;
        Burst_Direction_Pipeline;
        Dq_Dqs_Drivers;
    end

    // Dqs Receiver
    always @ (posedge Dqs[0]) begin
        // Latch data at posedge Dqs
        Dq_temp0 = Dq [7 : 0];
        Dm_temp0 = Dm [0];
    end

    always @ (posedge Dqs[1]) begin
        // Latch data at posedge Dqs
        Dq_temp1 = Dq [15 : 8];
        Dm_temp1 = Dm [1];
    end

    always @ (negedge Dqs[0]) begin
        // Latch data at negedge Dqs
        Dq_pair0 = {Dq [7 : 0], Dq_temp0};
        Dm_pair0 = {Dm [0], Dm_temp0};
    end

    always @ (negedge Dqs[1]) begin
        // Latch data at negedge Dqs
        Dq_pair1 = {Dq [15 : 8], Dq_temp1};
        Dm_pair1 = {Dm [1], Dm_temp1};
    end

    // Timing Check for -7 (CAS Latency = 2)
    specify
        specparam
                    tCH =  3.375,       // 0.45 * tCK
                    tCL =  3.375,       // 0.45 * tCK
                    tDH =  0.500,
                    tDS =  0.500,
                    tIH =  0.900,
                    tIS =  0.900;
        $setuphold(posedge Clk,   Cke,   tIS, tIH);
        $setuphold(posedge Clk,   Cs_n,  tIS, tIH);
        $setuphold(posedge Clk,   Cas_n, tIS, tIH);
        $setuphold(posedge Clk,   Ras_n, tIS, tIH);
        $setuphold(posedge Clk,   We_n,  tIS, tIH);
        $setuphold(posedge Clk,   Addr,  tIS, tIH);
        $setuphold(posedge Clk,   Ba,    tIS, tIH);
    endspecify

endmodule