NMS_sc_tb.v 32.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 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
//==================================================================
// ==========     TestBench for NOVeA Memory System      ===========                                                         QWEYTRy
//                   Version 00.20'
// 	modified to fit nms_top_16x32 & nms_sc_16x32
//==================================================================

`resetall
`timescale 1 ns / 10 ps

//  `define NMS_NAME            nms_nc15gfh_16x32     /*NMS Name*/
  `define NMS_NAME            nms_top_16x32	     /*NMS Name*/
  `define NB                  32                    /*Number of Bits*/
  `define NW                  16                    /*Number of Words*/
  `define ADR_MSB             3                     /*Address Bus MSB*/
// -------------------------------------------------------------------------
  `define WIR_MSB             50                    /*WIR MSB*/
  `define INSTR_MSB           5                     /*Instruction MSB*/
  `define PAR_I_MSB           2                     /*Parallel Instruction MSB*/
  `define CFG_MSB             44                    /*Configuration Code MSB*/
  `define NOVEA_INST          U_NMS.U_SC.I_NOVeA
  `define CHPUMP_INST         U_NMS.U_SC.I_charge_pump

// ----------------       Instructions       -----------------
  `define I_BYPASS           6'd0
  `define I_IDLE             `I_BYPASS
  `define I_SERIAL           6'd1
  `define I_FULL_STORE       6'd2
  `define I_NRECALL          6'd3
  `define I_NCOMPARE         6'd4
  `define I_KEEP             6'd5
  `define I_RECALLAM         6'd6
  
  `define I_SOFT_STORE       6'd8
  `define I_MRECALL          6'd9
  `define I_MCOMPARE         6'd10
  `define I_SER_LOOP         6'd11
  `define I_AUTO_SHIFT       6'd12
  `define I_RESET            6'd63

  `define TESTCFG_DEF        6'b010000
  `define FSTORE_DEF_CFG     45'b010101_010110_011001_011010_010000_001_00_10_0000_00_01

  `define PAR_LOAD           1'b1
  `define SER_LOAD           1'b0
  
//   --------------------       NMS_tb          -------------------
module NMS_sc_tb;


//  -----------       Regs      --------------
  reg SYS_CK;
  reg TIME_B;
  reg WRCK;
  reg WRSTN ;
  reg WSI;
  reg UpdateWR;
  reg ShiftWR;
  reg SelectWIR;
  reg ME_p;
  reg WE_p;
  reg OE_p;
  reg RA_p;
  reg [`NB-1:0] D_p;
  reg [`ADR_MSB:0] ADR_p;

  reg PAE_p ;
  reg [`PAR_I_MSB:0] PA_p;

  reg [`NB-1:0] MemArray [`NW-1:0];
  reg [`NB-1:0] TempWord;
  reg WRCK_Enable;
  reg [`CFG_MSB:0] ConfigCode;
  
  reg [16*8-1:0] TestName;
  integer TestNum;
  
  integer Fails;  // Total Fail Count
  integer i,j;
  integer IncorrCells;
  
//  -----------       Wires      --------------
  wire [`NB-1:0] Q;
  
// added by M.Honma
  wire [`NB-1:0] Q_CR;
  wire OE_NV;

// --------         NOVeA Memory System Instantiation       -----
  `NMS_NAME U_NMS(
      .SYS_CK(SYS_CK),
      .TIME_B(TIME_B),
      .WRSTN(WRSTN),
      .WRCK(WRCK),
      .WSI(WSI),
      .UpdateWR(UpdateWR),
      .ShiftWR(ShiftWR),
      .SelectWIR(SelectWIR),
      .ADR_p(ADR_p),
      .D_p(D_p),
      .WE_p(WE_p),
      .ME_p(ME_p),
      .OE_p(OE_p),
      .PAE_p(PAE_p),
      .PA_p(PA_p),
      .RA_p(RA_p),
      
      .WSO(WSO),
      .Q(Q),
      .NMS_READY(NMS_READY),
      .NMS_PASS(NMS_PASS),
      .KEEP_ON(KEEP_ON),

// added by M.Honma
      .OE_NV(OE_NV),
      .Q_CR(Q_CR)

  );



// ------------------    Clock Generators    ----------------
  initial
    begin
      #7 SYS_CK = 0;
      forever #4.98 SYS_CK = ~SYS_CK;
    end
    
  initial
    begin
      #4 WRCK = 0;
      forever #99.17 if (WRCK_Enable) WRCK = ~WRCK;
    end

  initial
    begin
      TIME_B = 0;
      forever #500 TIME_B = ~TIME_B;
    end
    
// ------------------    Main initial    ----------------
  initial
    begin

      $display("$INFO    ====#=#=#=#=#        TESTING  NOVEA MEMORY SYSTEM         #=#=#=#=#====\n");
      
      Fails = 0;
      TestNum = 0;
      $readmemb("NOVeA_Contents.dat", MemArray);
      ConfigCode = `FSTORE_DEF_CFG;
      
      WRCK_Enable = 0;
      $display("$INFO    ============    P1500 Clock (WRCK) is OFF ...     ===========\n");
      
      `NOVEA_INST.uut.store_success_count <= #2 2;
      
      NMS_Reset;                  TDelay;
      
      Parall_WriteRead_Test;      TDelay;
      
      ConfigReg_Test;             TDelay;
      
      FullStore_Test(`PAR_LOAD);  TDelay;
      
      WRCK_Enable = 1;            // Start WRCK Generator
      $display("$INFO    ============    P1500 Clock (WRCK) is ON ...     ===========\n");
      
      NMS_Reset;                  TDelay;
      
      NRecall_p_Test;             TDelay;
      
      SoftStore_Test;             TDelay;
      
      NCompare_p_Test;            TDelay;

      Recall_AM_p_Test;           TDelay;
      
      MCompare_Test;              TDelay;
      
      MRecall_Test;               TDelay;
      
      Bypass_Test;                TDelay;

      Parall_WriteRead_Test;      TDelay;
      
      Serial_Test(`SER_LOAD);     TDelay;

      Serial_Loop_Test;           TDelay;
      
      AutoShift_Test;             TDelay;
      
      Keep_Test;                  TDelay;
      
      Serial_Test(`PAR_LOAD);     TDelay;
      
      Reset_Test;                 TDelay;
      
      Keep_p_Test;                TDelay;
      
      FullStore_Test(`SER_LOAD);  TDelay;
      
      if (Fails === 0)
          $display("\n$PASS    ===========     ALL TESTS PASSED SUCCESSFULLY      ===========\n");
      else
          $display("\n$FAIL    ===========      TESTS FAILED    %d     TIMES      ===========\n", Fails);
      
      #1000;
      $finish;
     
    end    
    
// ==========================       Tasks         ==============================
 
// ------------------    NMS_Reset    ----------------
 task NMS_Reset;    // Sets Default Values on the NMS inputs and Resets NMS
  begin
      TestName = "NMS_Reset";
      TestNum = TestNum + 1;
      
      ME_p   = 1'b0;
      WE_p   = 1'b0;
      OE_p   = 1'b0;
      RA_p   = 1'b0;
      PAE_p  = 1'b0;
      
      UpdateWR = 0;
      ShiftWR = 0;
      SelectWIR = 0;
      
      WRSTN  = 1'b0;
      $display("$INFO   *****     Resetting NOVeA Memory System ...     *****");
      
      if (WRCK_Enable)
            @(negedge WRCK);
      else  #100;
      WRSTN = #1 1'b1;
      
      if (NMS_READY !== 0 || NMS_PASS !== 0)
        begin
              $display("\n$FAIL    NMS_READY or NMS_PASS are not 0 during Reset:    NMS_READY = %b  , NMS_PASS = %b", NMS_READY, NMS_PASS);
              Fails = Fails + 1;
        end
      
      $display("$INFO     Waiting for NMS_READY ...\n");
      wait(NMS_READY);
      
      Display_Status;
    
      TestName = 1'bx;
      
  end
 endtask // ---   NMS_Reset
 
 
 
// ------------------    Load_WIR    ----------------
 task Load_WIR;    // Loads WIR with given instruction
    input [`WIR_MSB:0] Instr;
    parameter WAIT_READY = 0;
   
  begin
    if (NMS_READY !== 1)
      begin
        if (WAIT_READY)
          begin
            $display("$INFO     Waiting for NMS_READY ...");
            wait(NMS_READY);
          end
        else
            $display("$INFO     Warning:   NMS_READY is not 1 during Load WIR : NMS_READY = %b   !!!!", NMS_READY);
      end

    if (WRCK_Enable)
      begin
        $display("$INFO    Loading WIR with contents:  %b .\n", Instr);
        
        @(negedge WRCK);
        SelectWIR = 1;
        ShiftWR = 1;
        
        for (i = `WIR_MSB; i >= 0; i = i - 1)
          begin
            WSI = Instr[i];
            @(negedge WRCK);
          end
        
        ShiftWR = 0;
        
        @(posedge WRCK);
        UpdateWR = 1;
        
        @(posedge WRCK);
        UpdateWR = 0;
        SelectWIR = 0;
      end
    else 
      begin
        $display("$FAIL    Cannot Load WIR  because WRCK is OFF   !!!");
        Fails = Fails + 1;
      end
  end
 endtask   // ---   Load_WIR


// ------------------    Load_Instr_Par    ----------------
 task Load_Instr_Par;    // Loads Given Instruction via Parallel pins
    input [`INSTR_MSB:0] Instr;
    parameter WAIT_READY = 1;
    parameter PAE_WIDTH = 2;

    reg [`PAR_I_MSB:0] Instr_Par;
   
  begin
      if (NMS_READY !== 1)
        begin
          if (WAIT_READY)
            begin
              $display("$INFO     Waiting for NMS_READY ...");
              wait(NMS_READY);
            end
          else
              $display("$INFO     Warning:   NMS_READY is not 1 during Parallel Instruction Loading : NMS_READY = %b !!!", NMS_READY);
        end
     
      Instr_Par = Instr;
      if (Instr === Instr_Par)
          $display("$INFO    Loading Parallel Instruction:  %b\n", Instr_Par);
      else
        begin
          $display("\n$FAIL    This Instruction cannot be Loaded in Parallel way:  %b\n", Instr);
          Fails = Fails + 1;
        end
      
      @(negedge SYS_CK);
      PA_p = Instr_Par;
      PAE_p = 1;
   
      repeat (PAE_WIDTH) @(negedge SYS_CK);
      PA_p = 0;
      PAE_p = 0;
      
  end
 endtask   // ---   Load_Instr_Par

 

// ------------------    Load_Config_Par    ----------------
 task Load_Config_Par;    // Loads Given Configuration via Parallel bus
    input [7:0] CRSTO_0;
    input [6:0] CRSTO_1;
    input [7:0] CRM_0;
    input [7:0] CRM_1;
    input [7:0] CRM_2;
    input [5:0] CRM_3;
  begin
      $display("$INFO    Loading Configuration:  CRSTO_0 = %b, CRSTO_1 = %b, CRM_0 = %b, CRM_1 = %b, CRM_2 = %b, CRM_3 = %b.\n",
                CRSTO_0, CRSTO_1, CRM_0, CRM_1, CRM_2, CRM_3);
      
      @(negedge SYS_CK);
      RA_p = 1;
      ME_p = 1;
      WE_p = 1;
      
      ADR_p = 0;
      D_p = CRSTO_0;
      
      @(negedge SYS_CK);
      ADR_p = 1;
      D_p = CRSTO_1;

      @(negedge SYS_CK);
      ADR_p = 2;
      D_p = CRM_0;
      
      @(negedge SYS_CK);
      ADR_p = 3;
      D_p = CRM_1;
      
      @(negedge SYS_CK);
      ADR_p = 4;
      D_p = CRM_2;
      
      @(negedge SYS_CK);
      ADR_p = 5;
      D_p = CRM_3;
      
      @(negedge SYS_CK);
      RA_p = 0;
      ME_p = 0;
      WE_p = 0;
      
  end
 endtask   // ---   Load_Config_Par

 

// ------------------    Display_Status    ----------------
 task Display_Status;    // Displays Status of NMS
  begin
    $display("$INFO    NMS Status:     NMS_READY = %b,   NMS_PASS = %b,    KEEP_ON = %b.",
                 NMS_READY, NMS_PASS, KEEP_ON);
  end
 endtask   // ---   Display_Status

 

// ------------------    TDelay    ----------------
 task TDelay;    // Provides Delays between Tests
  begin
    #500;
  end
 endtask   // ---   TDelay

 

// ------------------    Parall_WriteRead_Test    ----------------
 task Parall_WriteRead_Test;    // Tests Write and Read of NOVeA SRAM through Parallel pins
  begin
      TestName = "Parall_WriteRead";
      TestNum = TestNum + 1;
      $display("$TEST   **********    Testing NOVeA SRAM Parallel Write/Read    ***********");
     
      @(negedge SYS_CK);
      RA_p = 1'b0;
      OE_p = 1'b1;
      
      IncorrCells = 0;
      for (i = `NW-1; i >= 0; i = i - 1)
          begin
            ADR_p = i;
            D_p   = MemArray[i];
            ME_p  = 1'b1;
            WE_p  = 1'b1;              //  Writing
          
            @(negedge SYS_CK);
            D_p   = ~MemArray[i];
            ME_p  = 1'b1;
            WE_p  = 1'b0;              //  Reading
          
            @(negedge SYS_CK);
            if ((Q ^ MemArray[i]) !== 0)
              begin
                  IncorrCells = IncorrCells + 1;
                  $display("\n$FAIL     NOVeA SRAM Parallel Write/Read Failed  !!!   Q = %b, Expected: %b \n", Q, MemArray[i]);
              end

            @(negedge SYS_CK);
          end

      if (IncorrCells === 0)
            $display("\n$PASS     NOVeA SRAM Parallel Write/Read Passed    !!! \n");
      else  Fails = Fails + 1;

      ME_p = 1'b0;
      WE_p = 1'b0;
      OE_p = 1'b0;
    
      TestName = 1'bx;
      
  end
 endtask   // ---   Parall_WriteRead_Test



// ------------------    ConfigReg_Test    ----------------
 task ConfigReg_Test;    // Tests Access to NMS Configuration Registers
    reg [7:0] ConfReg_Expect [7:0];
  begin
      TestName = "ConfigReg";
      TestNum = TestNum + 1;
      $display("$TEST   *******    Testing Access to NMS Configuration Registers    ******");
      
      ConfReg_Expect[3'b000] = 8'b00000001; // Initial (Default) Values
      ConfReg_Expect[3'b001] = 8'b00010010;
      ConfReg_Expect[3'b010] = 8'b10010000;
      ConfReg_Expect[3'b011] = 8'b10010110;
      ConfReg_Expect[3'b100] = 8'b01011001;
      ConfReg_Expect[3'b101] = 8'b00010101;
      ConfReg_Expect[3'b110] = 8'b01001111;
      ConfReg_Expect[3'b111] = 8'b00000001;
          
      @(negedge SYS_CK);
      OE_p = 1'b1;
      RA_p = 1'b1;
      
      IncorrCells = 0;
// -    -    -    -    -  Reading Initial values then Writing New values   -    -    - 
      for (i = 0; i < 8; i = i + 1)
          begin
            ADR_p = i;
            D_p   = 'bx;
            ME_p  = 1'b1;
            WE_p  = 1'b0;              //  Reading
            
            @(negedge SYS_CK);
            @(posedge SYS_CK);
//
// modified from Q to Q_CR
//
//            if ((Q[7:0] ^ ConfReg_Expect[ADR_p]) !== 0)
            if ((Q_CR[7:0] ^ ConfReg_Expect[ADR_p]) !== 0)
              begin
                  IncorrCells = IncorrCells + 1;
                  $display("\n$FAIL    Incorrect Initial Value of Configuration Register:  ADR_p = %b;    Q_CR = %b, Expected: %b \n",
                           ADR_p, Q_CR, ConfReg_Expect[ADR_p]);
              end

            case (ADR_p)
              3'b000:   ConfReg_Expect[ADR_p] = 8'b01000101;  // New Values 
              3'b001:   ConfReg_Expect[ADR_p] = 8'b00101011;
              3'b010:   ConfReg_Expect[ADR_p] = 8'b00111001;
              3'b011:   ConfReg_Expect[ADR_p] = 8'b00100001;
              3'b100:   ConfReg_Expect[ADR_p] = 8'b10100110;
              3'b101:   ConfReg_Expect[ADR_p] = 8'b00001000;
            endcase
          
            @(negedge SYS_CK);
            D_p   = ConfReg_Expect[ADR_p];
            ME_p  = 1'b1;
            WE_p  = 1'b1;              //  Writing
          
            @(negedge SYS_CK);
          end
          
// -    -    -    -    -  Reading previously Written values   -    -     -     -
      for (i = 0; i < 8; i = i + 1)
          begin
            ADR_p = i;
            D_p   = 'bx;
            ME_p  = 1'b1;
            WE_p  = 1'b0;              //  Reading
            
            @(negedge SYS_CK);
            @(posedge SYS_CK);
//
// modified from Q to Q_CR
//
//            if ((Q[7:0] ^ ConfReg_Expect[ADR_p]) !== 0)
            if ((Q_CR[7:0] ^ ConfReg_Expect[ADR_p]) !== 0)
              begin
                  IncorrCells = IncorrCells + 1;
                  $display("\n$FAIL    Incorrect Value of Configuration Register:  ADR_p = %b;    Q_CR = %b, Expected: %b \n",
                           ADR_p, Q_CR, ConfReg_Expect[ADR_p]);
              end
            @(negedge SYS_CK);
          end

// -    -    -    -    -    -    -    -    -     -     -     -
      ME_p   = 1'b0;
      WE_p   = 1'b0;
      OE_p   = 1'b0;
      RA_p   = 1'b0;
    
      if (IncorrCells === 0)
            $display("\n$PASS     NMS Configuration Registers Test Passed    !!! \n");
      else  Fails = Fails + 1;

      TestName = 1'bx;
      
  end
 endtask   // ---   ConfigReg_Test



// ------------------    Serial_Test    ----------------
 task Serial_Test;    // Tests Serial Access to NOVeA SRAM
    input Parall;
  begin
      TestName = "Serial";
      TestNum = TestNum + 1;
      $display("$TEST   **********    Testing Serial Access    ***********");
      
      if (Parall)
        begin
          @(negedge WRCK);
          Load_Instr_Par(`I_SERIAL);
        end
      else
          Load_WIR({`I_SERIAL, `FSTORE_DEF_CFG});

      IncorrCells = 0;
      @(negedge WRCK);
      SelectWIR = 0;
      ShiftWR = 1;
      
      for (i = `NW-1; i >= 0; i = i - 1)
        for (j = `NB-1; j >= 0; j = j - 1)
          begin
            TempWord = MemArray[i];
            WSI = ~TempWord[j];
            @(posedge WRCK);
            if (WSO !== TempWord[j])  IncorrCells = IncorrCells + 1;
            @(negedge WRCK);
          end
      
      ShiftWR = 0;
      
      @(negedge WRCK);
      ShiftWR = 1;
      
      for (i = `NW-1; i >= 0; i = i - 1)
        for (j = `NB-1; j >= 0; j = j - 1)
          begin
            TempWord = MemArray[i];
            WSI = TempWord[j];
            @(posedge WRCK);
            if (WSO !== ~TempWord[j])  IncorrCells = IncorrCells + 1;
            @(negedge WRCK);
          end
      
      ShiftWR = 0;
  
      if (IncorrCells === 0 && NMS_PASS && NMS_READY)
            $display("\n$PASS     Serial Access to NOVeA SRAM  Passed    !!! \n");
      else  
        begin
            $display("\n$FAIL     Serial Access to NOVeA SRAM  Failed    !!! \n");
            Fails = Fails + 1;
        end
    
      TestName = 1'bx;
      
  end
 endtask   // ---   Serial_Test



// ------------------    Bypass_Test    ----------------
 task Bypass_Test;    // Tests Bypass Mode
    reg [8:0] TestData;
    integer CorrectBitCnt;
  begin
      TestName = "Bypass";
      TestNum = TestNum + 1;
      $display("$TEST   **********    Testing Bypass Mode    ***********");
      
      Load_WIR({`I_BYPASS, `FSTORE_DEF_CFG});
      
      TestData = 9'b100011000;
      CorrectBitCnt = 0;

      @(negedge WRCK);
      ShiftWR = 1'b1;
      SelectWIR = 1'b0;
      
      for (i = 0; i <= 8; i = i + 1)
        begin
          WSI = TestData[8-i];
          if (i == 4)
            begin
              ShiftWR = 0;
              repeat(3) @(negedge WRCK);
              ShiftWR = 1;
            end
          @(posedge WRCK);
          if ((i > 0) && ((WSO ^ TestData[8-i+1]) === 0)) CorrectBitCnt = CorrectBitCnt + 1;
          @(negedge WRCK);
        end

      ShiftWR = 1'b0;

      Display_Status;
      if (CorrectBitCnt === 8 && NMS_PASS && NMS_READY)
            $display("\n$PASS     Bypass Mode Test  Passed    !!! \n");
      else  
        begin
            $display("\n$FAIL     Bypass Mode Test  Failed    !!! \n");
            Fails = Fails + 1;
        end
    
      TestName = 1'bx;

  end
 endtask   // ---   Bypass_Test



// ------------------    Serial_Loop_Test    ----------------
 task Serial_Loop_Test;    // Tests Serial Loop Mode of NMS
  begin
      TestName = "Serial_Loop";
      TestNum = TestNum + 1;
      $display("$TEST   **********    Testing Serial Loop Mode    ***********");
      
      Load_WIR({`I_SER_LOOP, `FSTORE_DEF_CFG});

      IncorrCells = 0;
      @(negedge WRCK);
      SelectWIR = 0;
      ShiftWR = 1;
      
      for (i = `NW-1; i >= 0; i = i - 1)
        for (j = `NB-1; j >= 0; j = j - 1)
          begin
            TempWord = MemArray[i];
            WSI = 'bx;
            @(posedge WRCK);
            if ((WSO ^ TempWord[j]) !== 0)
              begin
                IncorrCells = IncorrCells + 1;
                $display("$FAIL    Incorrect Data on Serial Output:  WSO = %b,   Expected = %b.", WSO, TempWord[j]);
              end
            @(negedge WRCK);
            
            if (i == 5)
              begin
                ShiftWR = 0;
                repeat(3) @(negedge WRCK);
                ShiftWR = 1;
              end
          end
      
      ShiftWR = 0;
      
      Display_Status;
      if (IncorrCells === 0 && NMS_PASS && NMS_READY)
            $display("\n$PASS     Test of Serial Loop Mode  Passed    !!! \n");
      else  
        begin
            $display("\n$FAIL     Test of Serial Loop Mode  Failed    !!! \n");
            Fails = Fails + 1;
        end
    
      TestName = 1'bx;
      
  end
 endtask   // ---   Serial_Loop_Test



// ------------------    AutoShift_Test    ----------------
 task AutoShift_Test;    // Tests Serial Loop Mode of NMS
  begin
      TestName = "AutoShift";
      TestNum = TestNum + 1;
      $display("$TEST   **********    Testing Cyclic AutoShift    ***********");
      
      Load_WIR({`I_SER_LOOP, `FSTORE_DEF_CFG});

      @(negedge WRCK);
      SelectWIR = 0;
      ShiftWR = 1;
      
      repeat(7 * `NB) @(negedge WRCK); // Shifting NOVeA SRAM Contents
      ShiftWR = 0;
      
      Load_WIR({`I_AUTO_SHIFT, `FSTORE_DEF_CFG});

      @(posedge NMS_READY);
      
      IncorrCells = 0;
      for (i = 0; i < `NW; i = i + 1)   // Comparing SRAM Contents with expected data
        begin
          if (`NOVEA_INST.uut.memb[i] !== MemArray[i]) IncorrCells = IncorrCells + 1;
        end
      
      Display_Status;
      if (IncorrCells === 0 && NMS_PASS && NMS_READY)
            $display("\n$PASS     Test of Cyclic AutoShift  Passed    !!! \n");
      else  
        begin
            $display("\n$FAIL     Test of Cyclic AutoShift  Failed    !!! \n");
            Fails = Fails + 1;
        end
    
      TestName = 1'bx;
      
  end
 endtask   // ---   AutoShift_Test



// ------------------    FullStore_Test    ----------------
 task FullStore_Test;    // Tests Full Store Operation
    input Parall;
  begin
      TestName = "FullStore";
      TestNum = TestNum + 1;
      $display("$TEST   ***********    Testing Full Store Operation    ***************");
      
      if (Parall)
        begin
          Load_Instr_Par(`I_FULL_STORE);
        end
      else
          Load_WIR({`I_FULL_STORE, ConfigCode});
            
      @(posedge NMS_READY);
      
      if (NMS_PASS)
              $display("\n$PASS     Full Store Operation Passed    !!! \n");
      else
        begin
              $display("\n$FAIL     Full Store Operation Failed    !!! \n");
              Fails = Fails + 1;
        end

      TestName = 1'bx;
      
  end
 endtask   // ---   FullStore_Test

 
 
// ------------------    SoftStore_Test    ----------------
 task SoftStore_Test;    // Tests Soft Store Operation
  begin
      TestName = "SoftStore";
      TestNum = TestNum + 1;
      $display("$TEST   ***********    Testing Soft Store Operation    ***************");
      
      Load_WIR({`I_SOFT_STORE, 45'b000000_000000_000000_000000_000000_000_00_00_1010_00_01});
            
      @(posedge NMS_READY);
      
      if (NMS_PASS)
              $display("\n$PASS     Soft Store Operation Passed    !!! \n");
      else
        begin
              $display("\n$FAIL     Soft Store Operation Failed    !!! \n");
              Fails = Fails + 1;
        end

      TestName = 1'bx;
      
  end
 endtask   // ---   SoftStore_Test



// ------------------    NRecall_p_Test    ----------------
 task NRecall_p_Test;    // Tests Normal Recall Operation through Parallel pins
   
  begin
      TestName = "NRecall";
      TestNum = TestNum + 1;
      $display("$TEST   ***********    Testing Normal Recall Operation    ***************");

      Load_Instr_Par(`I_NRECALL);
      
      @(posedge NMS_READY);
      
      if (NMS_PASS)
              $display("\n$PASS     Normal Recall Operation Passed    !!! \n");
      else    
        begin
              $display("\n$FAIL     Normal Recall Operation Failed    !!! \n");
              Fails = Fails + 1;
        end

      TestName = 1'bx;
      
  end
 endtask   // ---   NRecall_p_Test

 
 
// ------------------    MRecall_Test    ----------------
 task MRecall_Test;    // Tests Margin Recall Operation through P1500
   
  begin
      TestName = "MRecall";
      TestNum = TestNum + 1;
      $display("$TEST   ***********    Testing Margin Recall Operation    ***************");
      
      Load_WIR({`I_MRECALL, 45'b100000_110110_100101_001001_100001_001_00_10_0000_00_11});
      
      @(posedge NMS_READY);
      
      if (NMS_PASS)
              $display("\n$PASS     Margin Recall Operation Passed    !!! \n");
      else
        begin
              $display("\n$FAIL     Margin Recall Operation Failed    !!! \n");
              Fails = Fails + 1;
        end

      TestName = 1'bx;
      
  end
 endtask   // ---   MRecall_Test

 
 
// ------------------    NCompare_p_Test    ----------------
 task NCompare_p_Test;    // Tests Normal Compare Operation through Parallel pins
  begin
      TestName = "NCompare";
      TestNum = TestNum + 1;
      $display("$TEST   ***********    Testing Normal Compare Operation    ***************");
      
      Load_Instr_Par(`I_NCOMPARE);
      
      @(posedge NMS_READY);
      
      if (NMS_PASS)
              $display("\n$PASS     Normal Compare Operation Passed    !!! \n");
      else
        begin
              $display("\n$FAIL     Normal Compare Operation Failed    !!! \n");
              Fails = Fails + 1;
        end

      TestName = 1'bx;
      
  end
 endtask   // ---   NCompare_p_Test


 
// ------------------    MCompare_Test    ----------------
 task MCompare_Test;    // Tests Margin Compare Operation through P1500
  begin
      TestName = "MCompare";
      TestNum = TestNum + 1;
      $display("$TEST   ***********    Testing Margin Compare Operation    ***************");
      
      Load_WIR({`I_MCOMPARE, 45'b100000_110110_100101_001001_011010_001_00_10_0000_00_00});
            
      @(posedge NMS_READY);
      
      if (NMS_PASS)
              $display("\n$PASS     Margin Compare Operation Passed    !!! \n");
      else
        begin
              $display("\n$FAIL     Margin Compare Operation Failed    !!! \n");
              Fails = Fails + 1;
        end

      TestName = 1'bx;
      
  end
 endtask   // ---   MCompare_Test



// ------------------    Keep_p_Test    ----------------
 task Keep_p_Test;    // Tests Keep Mode through Parallel pins
  begin
      TestName = "KEEP_ON";
      TestNum = TestNum + 1;
      $display("$TEST   ***********    Testing Keep Mode  (Parallel pins)  ***************");
      
      Load_Instr_Par(`I_KEEP);
            
      @(posedge NMS_READY);
      if (KEEP_ON !== 1)
        begin
              $display("\n$FAIL     KEEP_ON is not 1 in Keep Mode : KEEP_ON = %b .", KEEP_ON);
              Fails = Fails + 1;
        end
      else 
        begin
              $display("\n$INFO     Keep Mode is ON    ... \n");
              
//--------------------------------------------------
              $display("$INFO     Performing Read from NOVeA SRAM in Keep Mode ...");
              @(negedge SYS_CK); 
              ME_p   = 1'b1;
              WE_p   = 1'b0;              //  Reading
            
              @(negedge SYS_CK);
              ME_p   = 1'b0;
            
//--------------------------------------------------
              #16000;
              
              TestName = "Keep_Off";
              Load_Instr_Par(`I_IDLE);

              @(posedge NMS_READY);
              if (KEEP_ON !== 0)
                begin
                    $display("\n$FAIL     KEEP_ON is not 0 after exiting Keep Mode : KEEP_ON = %b .\n", KEEP_ON);
                    Fails = Fails + 1;
                end

              else
                begin
                    $display("\n$INFO     Keep Mode is OFF    ... ");
                    if (NMS_PASS)
                          $display("\n$PASS     Keep Mode Test Passed    !!! \n");
                    else 
                      begin
                          $display("\n$FAIL     Keep Mode Test Failed    !!! \n");
                          Fails = Fails + 1;
                      end
                end
      
        end

      TestName = 1'bx;
      
  end
 endtask   // ---   Keep_p_Test


// ------------------    Keep_Test    ----------------
 task Keep_Test;    // Tests Keep Mode through P1500
  begin
      $display("$TEST   ***********    Testing Keep Mode  (P1500)  ***************");
      
      Load_WIR({`I_KEEP/*_START*/, `FSTORE_DEF_CFG});
            
      @(posedge NMS_READY);
      if (KEEP_ON !== 1)
              $display("\n$FAIL     KEEP_ON is not 1 in Keep Mode : KEEP_ON = %b .", KEEP_ON);
      else 
        begin
              $display("\n$INFO     Keep Mode is ON    ... \n");
              
              #25000;
              
              Load_WIR({`I_IDLE/*`I_KEEP_END*/, `FSTORE_DEF_CFG});

              @(posedge NMS_READY);
              if (KEEP_ON !== 0)
                      $display("\n$FAIL     KEEP_ON is not 0 after exiting Keep Mode : KEEP_ON = %b .\n", KEEP_ON);
                      
              else
                begin
                    $display("\n$INFO     Keep Mode is OFF    ... ");
                    if (NMS_PASS)  $display("\n$PASS     Keep Mode Test Passed    !!! \n");
                    else           $display("\n$FAIL     Keep Mode Test Failed    !!! \n");
                end
      
        end

  end
 endtask   // ---   Keep_Test



// ------------------    Recall_AM_p_Test    ----------------
 task Recall_AM_p_Test;    // Tests Recall Auto Margin Operation through Parallel pins
   
  begin
      TestName = "Recall_AM";
      TestNum = TestNum + 1;
      $display("$TEST   ***********    Testing Recall Auto Margin Operation    ***************");
      
      Load_Instr_Par(`I_RECALLAM);
      
      @(posedge NMS_READY);
      
      if (NMS_PASS)
              $display("\n$PASS     Recall Auto Margin Operation Passed    !!! \n");
      else
        begin
              $display("\n$FAIL     Recall Auto Margin Operation Failed    !!! \n");
              Fails = Fails + 1;
        end

      TestName = 1'bx;
      
  end
 endtask   // ---   Recall_AM_p_Test

 
 
// -----------------------------------------------------------------------------------------



// ------------------    Reset_Test    ----------------
 task Reset_Test;    // Tests Reset Instruction
   integer StepCntr;
  begin
      TestName = "Reset";
      TestNum = TestNum + 1;
      $display("$TEST   ***********    Testing Reset Instruction    ***************");
      
      Load_WIR({`I_RESET, 45'b111111111111111111111111111111111111111111111});
      
      StepCntr = 0;
      fork
        begin : RstTest_Steps
          wait(U_NMS.U_NMSC.RST_i);
          StepCntr = StepCntr + 1;
          @(negedge U_NMS.U_NMSC.RST_i);
          StepCntr = StepCntr + 1;
          @(posedge NMS_READY);
          StepCntr = StepCntr + 1;
          disable RstTest_WtchDog;
        end
        
        begin : RstTest_WtchDog
          repeat (8) @(posedge SYS_CK);
          if (StepCntr < 1)
              disable RstTest_Steps;
          else
            begin
              #100000;
              if (StepCntr < 2) disable RstTest_Steps;
            end
        end
      join

      if (StepCntr == 3)
              $display("\n$PASS     Reset Instruction Test Passed    !!! \n");
      else
        begin
              $display("\n$FAIL     Reset Instruction Test Failed    !!! \n");
              Fails = Fails + 1;
        end

      TestName = 1'bx;
      
  end
 endtask   // ---   Reset_Test

 
 
// -----------------------------------------------------------------------------------------



// ------------------------       Reset  Messages        ----------------------
  always @(posedge `CHPUMP_INST.RST)
    begin
      $display("$INFO   Resetting Charge Pump ... ");
    end

// ------------------------       Store  Messages        ----------------------
  always @(posedge `NOVEA_INST.STORE)
    begin
      $display("$INFO   Storing ...    VPPRANGE = %d , VPPSEL = %d\n",
               `CHPUMP_INST.uut.VPPRANGE_latched, `CHPUMP_INST.uut.VPPSEL_latched);
    end

// ----------------------      Compare and Recall  Messages    ------------------
  always @(posedge `NOVEA_INST.COMP)
    begin
      $display("$INFO   Comparing ...   BIAS1 = %b,  BIAS0 = %b, MRCL1 = %b,  MRCL0 = %b,  TECC1 = %b,  TECC0 = %b",
               `NOVEA_INST.BIAS1, `NOVEA_INST.BIAS0, `NOVEA_INST.MRCL1, `NOVEA_INST.MRCL0, `NOVEA_INST.TECC1, `NOVEA_INST.TECC0);
    end

  always @(posedge `NOVEA_INST.RECALL)
    begin
      $display("$INFO   Recalling ...   BIAS1 = %b,  BIAS0 = %b, MRCL1 = %b,  MRCL0 = %b,  TECC1 = %b,  TECC0 = %b",
               `NOVEA_INST.BIAS1, `NOVEA_INST.BIAS0, `NOVEA_INST.MRCL1, `NOVEA_INST.MRCL0, `NOVEA_INST.TECC1, `NOVEA_INST.TECC0);
    end

  always @(posedge `NOVEA_INST.RCREADY)
    begin
      if (`NOVEA_INST.COMP)
            $display("$INFO   Compare Completed, MATCH =   %b", `NOVEA_INST.MATCH);
      else  $display("$INFO   Recall Completed, RCREADY =  %b\n", `NOVEA_INST.RCREADY);
    end
    
`ifdef POST_SYNTH

`else
// --------------------      Timer Overflow  Check    ------------------
  always @(U_NMS.U_NMSC.U_NVC.Timer_r)
    begin
      if (&U_NMS.U_NMSC.U_NVC.Timer_r)
        begin
          $display("$FAIL   =====  Timer Overflow   =====");
          Fails = Fails + 1;
        end
    end

// --------------------------------------------
`endif  // ---  POST_SYNTH

// --------------------------------------------

endmodule  // ---   NMS_tb