TC58256FT.v 66.9 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 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760
////////////////////////////////////////////////////////////////////////////////
// TC58512FT 512Mbit(x8) NAND Flash Memory Verilog Simulation Model           //
// Version: 1.2                                                               //
// Date: 2001/07/18                                                           //
// Programmed By Y.Motizuki at FLASH MEMORY TECHNICAL MARKETING GROUP         //
// module : TC58512FT                                                         //
//                                                                            //
//   Copyright (c) 2001 TOSHIBA CORPORATION, All Rights Reserved.             //
////////////////////////////////////////////////////////////////////////////////
// Revision History                 Description                               //
//                         : 1.1   2001/06/10 1st Release                     //
//                         : 1.2   2001/07/18 Timing Check modify             //
////////////////////////////////////////////////////////////////////////////////
`timescale      1ns/1ns

//------------------------------------------------------------------------------
// Memory Initialize Option
//------------------------------------------------------------------------------
// If This value is defined "1", whole CHIP data "1" except for Bad Block.
// It is better to define "0" to speed up simulation.
//------------------------------------------------------------------------------

`define MemoryInitEnable     0    // Not Memory Initialize (recommended)

//------------------------------------------------------------------------------
//  Difinitions Size of Memory
//------------------------------------------------------------------------------
//  "blk_bit" is width of bits for block address in a chip 1Chip=2048Blocks, 11bits requires
//------------------------------------------------------------------------------
`define blk_bit            (11)    // Width of bits for block address

//------------------------------------------------------------------------------
//  Definition regarding AC Specification
//------------------------------------------------------------------------------
`define tCLS                (0)
`define tCLH               (10)
`define tCS                 (0)
`define tCH                (10)
`define tWP                (25)
`define tALS                (0)
`define tALH               (10)
`define tDS                (20)
`define tDH                (10)
`define tWC                (50)
`define tWH                (15)
`define tWW               (100)
`define tRR                (20)
`define tRP                (35)
`define tRC                (50)
`define tREA               (35)
`define tCEH              (100)
`define tREAID             (35)
`define tOH                (10)
`define tRHZ               (30)
`define tCHZ               (20)
`define tREH               (15)
`define tIR                 (0)
`define tRSTO              (35)
`define tCSTO              (45)
`define tRHW                (0)
`define tWHC               (30)
`define tWHR               (30)
`define tAR1              (100)
`define tCR               (100)
`define tWB               (200)
`define tAR2               (50)
`define tRB               (200)
`define tCRY             (1000)

`ifdef  FLASH_FAST
`define tRST_program    (1200)
`define tRST_erase      (1300)
`define tRST_read       (1100)
`define tR              (1000)
`define tPROG           (5000)    // Typical
`define tDBSY           (500)    // Typical
`define tMBPBSY         (5000)    // Typical
`define tBERASE         (5000)    // Typical
`else
`define tRST_program    (10000)
`define tRST_erase     (500000)
`define tRST_read        (6000)
`define tR              (25000)
`define tPROG          (200000)    // Typical
`define tDBSY            (2000)    // Typical
`define tMBPBSY        (200000)    // Typical
`define tBERASE       (2000000)    // Typical
`endif

/*
`define tRST_program   (10000)
`define tRST_erase     (20000)
`define tRST_read       (6000)
`define tR              (2000)
`define tPROG           (3000)
`define tDBSY           (1000)
`define tMBPBSY        (10000)
`define tBERASE        (10000)
*/

//------------------------------------------------------------------------------
//  tCERY : In case of sequential read, if CE gets 1 with in 30nsec, R/B will not go 0.
//          This 30nsec is defined tCERY.
//------------------------------------------------------------------------------
`define tCERY              (30)    // tCERY

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//                    TC58512FT Simulation Model
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

module TC58256FT (
                    IO,
                    CE,
                    WE,
                    RE,
                    CLE,
                    ALE,
                    WP,
                    RY_BY
                  ) ;

//------------------------------------------------------------------------------
//  Definition Fail Address & Fail bit
//------------------------------------------------------------------------------
//  Format is following;
//  ------------------------------------
//  FAIL_ADD[n] = 27'hxxxxxxx
//  FAIL_BIT[n] = 8'byyyyyyyy
//  ------------------------------------
//  xxxxxxx is address in the memory in hexadecimal number and 27bits are effective.
//  from LSB, 10bit = Column Address
//             5bit = Page   Address
//            12bit = Block  Address
//   yyyyyyyy is fail bit in Fail Address.
//   n is fail Address ID Number. (n<FAIL_NUM)
//
//   For example, in case of Block=7 , Page=8 , Column=3 , bit=2,
//   discription is
//        
//   FAIL_ADD[0] = 27'h003a003
//   FAIL_BIT[0] = 8'b00000100
//
//------------------------------------------------------------------------------
parameter FAIL_NUM = 5 ;                          // The Number of Total Fail Address
parameter ADD_BIT = 27 ;                          // Width of bits for address

reg  [ADD_BIT-1:0]  FAIL_ADD [0:FAIL_NUM-1] ;     // FAIL ADD Register
reg  [7:0]  FAIL_BIT[0:FAIL_NUM-1] ;              // FAIL BIT Register

initial begin
    FAIL_ADD[0] = 27'h0000000 ;
    FAIL_ADD[1] = 27'h0000c00 ;
    FAIL_ADD[2] = 27'h0ff7e0f ;
    FAIL_ADD[3] = 27'h0ffbe0f ;
    FAIL_ADD[4] = 27'h0fffe0f ;
    FAIL_BIT[0] = 8'b00001000 ;
    FAIL_BIT[1] = 8'b00010000 ;
    FAIL_BIT[2] = 8'b00100000 ;
    FAIL_BIT[3] = 8'b01000000 ;
    FAIL_BIT[4] = 8'b10000000 ;
end

//------------------------------------------------------------------------------
//  Parameters regarding memory size
//------------------------------------------------------------------------------
parameter D_WIDTH           = 8                ; // Data Width
parameter COL_IN_PAGE       = 528            ; // Number of columns per 1 page
parameter PAGE_IN_BLOCK     = 32             ; // Number of pages per 1 block
parameter ADD_COL           = 9              ; // Width of bits for colmn address
parameter COL_BIT           = 10             ; // Width of bits for colmn
parameter PAG_BIT           = 5              ; // Width of bits for page address
parameter BLK_BIT           = `blk_bit       ; // Width of bits for block address
parameter CHP_BIT           = BLK_BIT+PAG_BIT+COL_BIT ;
                                               // Width of bits for addressing whole chip
parameter AREA_SIZ_A        = 256            ; // Number of column in AREA-A/page
parameter AREA_SIZ_B        = 256            ; // Number of column in AREA-B/page
parameter AREA_SIZ_C        = 16             ; // Number of column in AREA-C/page
parameter AREA_BIT_A        = 8              ; // Width of bits for address in AREA-A
parameter AREA_BIT_B        = 8              ; // Width of bits for address in AREA-B
parameter AREA_BIT_C        = 4              ; // Width of bits for address in AREA-C
parameter ADD_DIV           = 4              ; // How many cycles address is devided and input
parameter ID_ADD_BIT = 3                     ; // The Number of Total Bad Block
parameter DISTRICT_NUM      = 4              ; // Number of District
parameter DISTRICT_BIT_NUM  = 2              ; // Width of bits for District

//------------------------------------------------------------------------------
//  Difinition regarding input/output signal
//------------------------------------------------------------------------------
inout [7:0] IO ;
input  CE ;
input  WE ;
input  RE ;
input  CLE ;
input  ALE ;
input  WP ;
output RY_BY  ;

//------------------------------------------------------------------------------
// Parameters regarding Command
//------------------------------------------------------------------------------
parameter CMD_SERIAL_DATA_INPUT  = 8'h80 ;
parameter CMD_READ_MODE_1        = 8'h00 ;
parameter CMD_READ_MODE_2        = 8'h01 ;
parameter CMD_READ_MODE_3        = 8'h50 ;
parameter CMD_RESET              = 8'hff ;
parameter CMD_AUTO_PROGRAM_TRUE  = 8'h10 ;
parameter CMD_AUTO_BLOCK_ERASE_1 = 8'h60 ;
parameter CMD_AUTO_BLOCK_ERASE_2 = 8'hd0 ;
parameter CMD_STATUS_READ_1      = 8'h70 ;
parameter CMD_ID_READ_1          = 8'h90 ;

parameter CMD_AUTO_PROGRAM_DUMMY = 8'h11 ;
parameter CMD_AUTO_PROGRAM_MULTI = 8'h15 ;
parameter CMD_STATUS_READ_2      = 8'h71 ;
parameter CMD_ID_READ_2          = 8'h91 ;

//------------------------------------------------------------------------------
// Parameters regarding Others
//------------------------------------------------------------------------------
//  PROGRAM,ERASE,INIT : mem_write parameter used in task
//
//    PROGRAM: indicates memory cells are written by Program
//    ERASE:   indicates memory cells are erased by Erase
//    INIT:    indicates memory cells are initialized by Erase
//    XPROGRAM: Before Program, make memory cells unknown
//    XERASE:   Before Erase, make memory cells unknown
//
//------------------------------------------------------------------------------
parameter  PROGRAM = 0 ;
parameter  ERASE   = 1 ;
parameter  INIT    = 2 ;
parameter XPROGRAM = 3 ;
parameter XERASE   = 4 ;

//------------------------------------------------------------------------------
// Main registers
//------------------------------------------------------------------------------
//
//  MEM   : Core of memory
//          Data Width 8bit, number of words 2^27
//
//          Because of column address 10bit, 1Page=1024words exist,
//          but only 528 words are used.
//
//
//                7 6 5 4 3 2 1 0
//              +-----------------+             ---   ---   ---
//              |                 |    0         |     |     |
//              +-----------------+    :         |     |     |
//              |                 |    :         |     |     |
//              |                 |    :       0 Page  |     |
//              |                 |    :         |     |     |
//              +-----------------+    :         |     |     |
//              |                 |   527        |     |     |
//              +-----------------+    :        ---    |     |
//              |XXXXXXXXXXXXXXXXX|    :         |   0 Block |
//              |XXXXXXXXXXXXXXXXX|    :         |     |     |
//              |XXXXXXXXXXXXXXXXX|    :      Not used |     |
//              +-----------------+    :         |     |     |
//              |XXXXXXXXXXXXXXXXX|  1023        |     |     |
//              +-----------------+             ----   |     |
//                                                     |     |
//                                            1 Page   |     |
//                                              :      |     |
//                                              :      |     |
//                                              :      |     |
//                                           31 Page   |     |
//                                                    ---    |
//                                                   1 Block |
//                                                     :     |
//                                                     :     |
//                                                     :     |
//                                                4095 Block |
//                                                          ---
//
//------------------------------------------------------------------------------

reg  [D_WIDTH-1:0] MEM [0:(1<<CHP_BIT)-1] ;    // Memory-Core

reg  [D_WIDTH-1:0] RCOD  ;                     // Command Register
reg  [ADD_BIT-1:0] RADD  ;                     // Address Register
reg  [ADD_BIT-1:0] RADD0 ;                     // Address Register (District0)
reg  [ADD_BIT-1:0] RADD1 ;                     // Address Register (District1)
reg  [ADD_BIT-1:0] RADD2 ;                     // Address Register (District2)
reg  [ADD_BIT-1:0] RADD3 ;                     // Address Register (District3)
reg  [D_WIDTH-1:0] RSTAT ;                     // Status Register
reg  [COL_BIT-1:0] DPNT  ;                     // Data Register Pointer
reg  [D_WIDTH-1:0] DATA0[0:(1<<COL_BIT)-1] ;   // Data Register (District0)
reg  [D_WIDTH-1:0] DATA1[0:(1<<COL_BIT)-1] ;   // Data Register (District1)
reg  [D_WIDTH-1:0] DATA2[0:(1<<COL_BIT)-1] ;   // Data Register (District2)
reg  [D_WIDTH-1:0] DATA3[0:(1<<COL_BIT)-1] ;   // Data Register (District3)

//------------------------------------------------------------------------------
// Declaration regarding RY_BY output signal(Busy signal)
//------------------------------------------------------------------------------
//
//    Final RY_BY signal is generated by AND calculated with the above signals
//    (except BUSY_by_read_sched)
//
//------------------------------------------------------------------------------
reg  BUSY_by_read ;         // for generating RY_BY signal in Read sequence
reg  BUSY_by_program ;      // for generating RY_BY signal in Program sequence
reg  BUSY_by_erase ;        // for generating RY_BY signal in Erase sequence
reg  BUSY_by_reset ;        // for cancellation RY_BY signal by Reset Command
reg  BUSY_by_ce ;           // for cancellation RY_BY signal by CE=1
reg  BUSY_by_read_sched ;   // for scheduling BUSY_by_read

reg bBUSY_by_read ;
reg bBUSY_by_program ;
reg bBUSY_by_erase ;
reg bBUSY_by_reset ;
reg bBUSY_by_ce ;

//==============================================================================
//  Declaration for variables
//==============================================================================
integer column ;
reg read_flg ;
reg DIN_COMPLETE ;
reg read_ok ;

time rCE, fCE, rWE, fWE ;
time rCLE, fCLE, rALE, fALE, rRE, fRE, rCEorRE, fCEorRE ;
time rfIO, rRY_BY, fRY_BY, rWP, fWP ;

time fWE2, fRE2, rCE_RST ;
time IO_hiz ;

//==============================================================================
//  From here, main functions are described
//==============================================================================

//------------------------------------------------------------------------------
// Generate Power On Signal
//------------------------------------------------------------------------------
reg  Power_on ;
initial begin
                       Power_on = 1'b0 ;
        #10            Power_on = 1'b1 ;
        #10            Power_on = 1'b0 ;
end

//------------------------------------------------------------------------------
// Set ID registers & Reset timing variables
//------------------------------------------------------------------------------
reg  [D_WIDTH-1:0] RID1[0:4] ; // ID Register (1)
reg  [D_WIDTH-1:0] RID2[0:4] ; // ID Register (2)
initial begin
  RID1[0] = 8'h98 ; RID1[1] = 8'h75 ; RID1[2] = 8'hA5 ;
  RID1[3] = 8'hC0 ; RID1[4] = 8'h01 ;
  RID2[0] = 8'h20 ; RID2[1] = 8'h46 ; RID2[2] = 8'h3A ;
  RID2[3] = 8'h00 ; RID2[4] = 8'h00 ;

  rCE   = 0   ; fCE   = 0 ;
  rWE   = 0   ; fWE   = 0 ;
  rCLE  = 0   ; fCLE   = 0 ;
  rALE  = 0   ; fALE   = 0 ;
  rRE   = 0   ; fRE   = 0 ;
  rCEorRE = 0 ; fCEorRE = 0 ;
  rRY_BY  = 0 ; fRY_BY  = 0 ;
  rfIO   = 0  ;
  rWP   = 0 ; fWP   = 0 ;
  read_flg = 1'b0 ;
  DIN_COMPLETE = 1'b0 ;
  read_ok = 1'b0 ;
end
parameter ID_NUM        = 5 ;

//------------------------------------------------------------------------------
//  Named with the rule: for input signals, attach 'i'
//                       for output signals, attach 'o'
//
//  Since regarding Output signal RY_BY, output '0' is Strong and output '1' is
//  Weak, it should be output through buffer.
//------------------------------------------------------------------------------
wire [D_WIDTH-1:0] iIO    ;
wire [D_WIDTH-1:0] oIO    ;
wire               eIO    ;
wire               iCE    ;
wire               iWE    ;
wire               iRE    ;
wire               iCLE   ;
wire               iALE   ;
wire               iWP    ;
wire               oRY_BY ;

assign iIO = IO               & {D_WIDTH{1'b1}} ;
assign       IO = (eIO) ? oIO : {D_WIDTH{1'bz}} ;

assign iCE    =  CE    & 1'b1 ;
assign iWE    =  WE    & 1'b1 ;
assign iRE    =  RE    & 1'b1 ;
assign iCLE   =  CLE   & 1'b1 ;
assign iALE   =  ALE   & 1'b1 ;
assign iWP    =  WP    & 1'b1 ;
buf (strong0,weak1) (RY_BY,oRY_BY) ;

//------------------------------------------------------------------------------
// Back up initial values of input signals
//------------------------------------------------------------------------------
//  Named by addition 'b'.
//------------------------------------------------------------------------------
reg  [D_WIDTH-1:0] bIO  ;
reg                bCE  ;
reg                bWE  ;
reg                bRE  ;
reg                bCLE ;
reg                bALE ;
reg                bWP  ;

always @(iIO)      bIO  <= iIO  ;
always @(iCE)      bCE  <= iCE  ;
always @(iWE)      bWE  <= iWE  ;
always @(iRE)      bRE  <= iRE  ;
always @(iCLE)     bCLE <= iCLE ;
always @(iALE)     bALE <= iALE ;
always @(iWP)      bWP  <= iWP  ;

//------------------------------------------------------------------------------
//  Genarate Reset_on signal
//------------------------------------------------------------------------------
//  Reset_on :  has initial value 0, after issueing Reset Command, gets to 1
//              indicates if Reset Command has been issued or not.
//------------------------------------------------------------------------------
reg  Reset_on ;
always @(posedge Power_on or posedge iWE) begin
    if ( Power_on ) begin
`ifdef TOSHIBA_ORIGINAL
        Reset_on <= 1'b0 ;
`else
        Reset_on <= 1'b1 ;
        RADD <= {ADD_BIT{1'b0}};
        RADD0 <= {ADD_BIT{1'b0}} ;
        RADD1 <= {ADD_BIT{1'b0}} ;
        RADD2 <= {ADD_BIT{1'b0}} ;
        RADD3 <= {ADD_BIT{1'b0}} ;
`endif
    end
    else if ( {iCE,iCLE,iALE,iRE}===4'b0101 ) begin
        if ( iIO == CMD_RESET )
            Reset_on <= 1'b1 ;
    end
end

//------------------------------------------------------------------------------
// Write to Command Register
//------------------------------------------------------------------------------
//
//    In Command Write, write IO[8:1] to Command Register,
//    with checking the command is valid or not by the function cmd_check.
//    Only valid commands are issued into Command Register.
//
//------------------------------------------------------------------------------
always @(posedge iWE)
    if ( {iCE,iCLE,iALE,iRE}===4'b0101 )
         if ( cmd_check(RCOD,iIO) ) begin
            RCOD <= iIO ;
            read_flg = 1'b0 ;
         end

//------------------------------------------------------------------------------
// Generate mode signals by decoding Command Register
//------------------------------------------------------------------------------

wire   MOD_SERIAL_DATA_INPUT  ;
wire   MOD_READ_MODE_1        ;
wire   MOD_READ_MODE_2        ;
wire   MOD_READ_MODE_3        ;
wire   MOD_RESET              ;
wire   MOD_AUTO_PROGRAM_TRUE  ;
wire   MOD_AUTO_PROGRAM_DUMMY ;
wire   MOD_AUTO_PROGRAM_MULTI ;
wire   MOD_AUTO_BLOCK_ERASE_1 ;
wire   MOD_AUTO_BLOCK_ERASE_2 ;
wire   MOD_STATUS_READ_1      ;
wire   MOD_STATUS_READ_2      ;
wire   MOD_ID_READ_1          ;
wire   MOD_ID_READ_2          ;

wire   MOD_READ_MODE          ; // Read mode        (1),(2),(3)
wire   MOD_AUTO_PROGRAM       ; // Auto Program     (Treu),(Dummy),(Multi)
wire   MOD_AUTO_BLOCK_ERASE   ; // Auto Block Erase (1),(2)
wire   MOD_STATUS_READ        ; // Status Read      (1),(2)
wire   MOD_ID_READ            ; // ID Read          (1),(2)

assign MOD_SERIAL_DATA_INPUT  = ( RCOD === CMD_SERIAL_DATA_INPUT  ) ;
assign MOD_READ_MODE_1        = ( RCOD === CMD_READ_MODE_1        ) ||
                                ( RCOD === CMD_RESET              ) ;
assign MOD_READ_MODE_2        = ( RCOD === CMD_READ_MODE_2        ) ;
assign MOD_READ_MODE_3        = ( RCOD === CMD_READ_MODE_3        ) ;
assign MOD_RESET              = ( RCOD === CMD_RESET              ) ;
assign MOD_AUTO_PROGRAM_TRUE  = ( RCOD === CMD_AUTO_PROGRAM_TRUE  ) ;
assign MOD_AUTO_PROGRAM_DUMMY = ( RCOD === CMD_AUTO_PROGRAM_DUMMY ) ;
assign MOD_AUTO_PROGRAM_MULTI = ( RCOD === CMD_AUTO_PROGRAM_MULTI ) ;
assign MOD_AUTO_BLOCK_ERASE_1 = ( RCOD === CMD_AUTO_BLOCK_ERASE_1 ) ;
assign MOD_AUTO_BLOCK_ERASE_2 = ( RCOD === CMD_AUTO_BLOCK_ERASE_2 ) ;
assign MOD_STATUS_READ_1      = ( RCOD === CMD_STATUS_READ_1      ) ;
assign MOD_STATUS_READ_2      = ( RCOD === CMD_STATUS_READ_2      ) ;
assign MOD_ID_READ_1          = ( RCOD === CMD_ID_READ_1          ) ;
assign MOD_ID_READ_2          = ( RCOD === CMD_ID_READ_2          ) ;

assign MOD_READ_MODE          = MOD_READ_MODE_1        |
                                MOD_READ_MODE_2        |
                                MOD_READ_MODE_3        ;

assign MOD_AUTO_PROGRAM       = MOD_AUTO_PROGRAM_TRUE  |
                                MOD_AUTO_PROGRAM_DUMMY |
                                MOD_AUTO_PROGRAM_MULTI ;

assign MOD_AUTO_BLOCK_ERASE   = MOD_AUTO_BLOCK_ERASE_1 |
                                MOD_AUTO_BLOCK_ERASE_2 ;

assign MOD_STATUS_READ        = MOD_STATUS_READ_1      |
                                MOD_STATUS_READ_2      ;

assign MOD_ID_READ            = MOD_ID_READ_1          |
                                MOD_ID_READ_2          ;

//------------------------------------------------------------------------------
// Generate signals which indicate the Area in one Page.
//   Since one Page consists of Area A,B,C, the information which
//   area is selected is needed.
//------------------------------------------------------------------------------
//
//  area_a : is '1' when Area A is selected.
//  area_b : is '1' when Area B is selected.
//  area_c : is '1' when Area C is selected.
//
//  area_[abc] doesn't change in case of Serial Data Input
//  area_a = 1 , area_b = 0 , area_c = 0 in case of Read Mode (1)
//  area_a = 0 , area_b = 1 , area_c = 0 in case of Read Mode (2)
//  area_a = 0 , area_b = 0 , area_c = 1 in case of Read Mode (3)
//  area_a = 1 , area_b = 0 , area_c = 0 in case of Reset
//  area_[abc] doesn't change in case of other commands
//
//------------------------------------------------------------------------------

reg area_a ;
reg area_b ;
reg area_c ;

always @(posedge iWE)
    if ( {iCE,iCLE,iALE,oRY_BY,iRE}===5'b01011 )
        case (iIO)
            CMD_SERIAL_DATA_INPUT : {area_a,area_b,area_c} <= {area_a,area_b,area_c} ;
            CMD_READ_MODE_1       : {area_a,area_b,area_c} <= 3'b100 ;
            CMD_READ_MODE_2       : {area_a,area_b,area_c} <= 3'b010 ;
            CMD_READ_MODE_3       : {area_a,area_b,area_c} <= 3'b001 ;
            CMD_RESET             : {area_a,area_b,area_c} <= 3'b100 ;
            default               : {area_a,area_b,area_c} <= {area_a,area_b,area_c} ;
        endcase

//------------------------------------------------------------------------------
//  district : has 2bit and indicates which District is selected.
//             This signal is equivalent to RADD[15:14]
//------------------------------------------------------------------------------
wire [1:0]        district ;
assign district = RADD[(PAG_BIT+ADD_COL+DISTRICT_BIT_NUM-1):(PAG_BIT+ADD_COL)] ;

//------------------------------------------------------------------------------
// Generate position signal of Address, add_pos
//------------------------------------------------------------------------------
//
//  add_pos : counts the cycles of inputs of address
//
//    This variable has initial value 0, and counts up with address inputs.
//
//      Address input is issued into RADD[ 8:0] in case of add_pos = 0.
//      Address input is issued into RADD[16:9] in case of add_pos = 1.
//      Address input is issued into RADD[24:17] in case of add_pos = 2.
//      Address input is issued into RADD[25] in case of add_pos = 3.
//      Address input is not issued, nor counts up incase of add_pos = 4.
//
//------------------------------------------------------------------------------
integer add_pos ;
always @(posedge iWE or posedge oRY_BY ) begin
    if ( {bWE,iWE,iRE}===3'b011 ) begin
        if ( {iCE,iCLE,iALE,oRY_BY}===4'b0101 )
            if ( (iIO === CMD_AUTO_BLOCK_ERASE_1)&&(iWP===1'b1) ) add_pos <= 1 ;
            else                                                  add_pos <= 0 ;
        else if ( {iCE,iCLE,iALE,oRY_BY}===4'b0011 )
            if ( MOD_SERIAL_DATA_INPUT || MOD_READ_MODE || MOD_AUTO_BLOCK_ERASE_1 )
                add_pos <= (add_pos == ADD_DIV) ? ADD_DIV : add_pos + 1 ;
            else if ( MOD_ID_READ )
                add_pos <= 0 ;
    end
    else begin
        add_pos <= 0 ;
    end
end

//------------------------------------------------------------------------------
// Input address to Address Register
//------------------------------------------------------------------------------
//
//    At address input, write IO[8:1] to Address Register RADD.
//
//    Address Register consists of not only RADD but also RADD0,RADD1,RADD2 and RADD3
//    used in Program/Erase, for each District.
//    RADD0,RADD1,RADD2 and RADD3 are also described here.
//
//  [Operation]
//
//      Note that in Multi Block Program, same page number in blocks belonging
//      to other Distrcts is valid.
//      In case of different page nambers, the last input page is valid.
//      Therefore, RADD?[13:9] must always be input the last page address regardless
//      of the number of Districts.
//
//------------------------------------------------------------------------------

always @(posedge iWE or posedge iRE) begin

    //
    // WE signal rising
    //
    if ( {bWE,iWE,iRE}===3'b011 ) begin
        if ( {iCE,iCLE,iALE,oRY_BY}===4'b0100 ) begin
            RADD <= ( iIO == CMD_RESET ) ? {ADD_BIT{1'b0}} : RADD ;
        end
        else if ( {iCE,iCLE,iALE,oRY_BY}===4'b0101 ) begin
            RADD <= ( iIO == CMD_RESET ) ? {ADD_BIT{1'b0}} : {ADD_BIT{1'bx}} ;
            if ( iIO == CMD_RESET ) begin
                RADD0 <= {ADD_BIT{1'b0}} ;
                RADD1 <= {ADD_BIT{1'b0}} ;
                RADD2 <= {ADD_BIT{1'b0}} ;
                RADD3 <= {ADD_BIT{1'b0}} ;
            end
        end
        else if ( {iCE,iCLE,iALE,oRY_BY}===4'b0011 ) begin
            if ( MOD_SERIAL_DATA_INPUT || MOD_READ_MODE )
                case (add_pos)
                          0 : RADD <= {RADD[25:9],area_b,iIO} ;
                          1 : RADD <= {RADD[25:17],iIO,RADD[8:0]} ;
                          2 : RADD <= {RADD[25],iIO,RADD[16:0]} ;
                          3 : begin
                                    RADD <= {iIO,RADD[24:0]} ;
                                    case (district)
                                        0: RADD0[25:14] <= {iIO,RADD[24:14]} ;
                                        1: RADD1[25:14] <= {iIO,RADD[24:14]} ;
                                        2: RADD2[25:14] <= {iIO,RADD[24:14]} ;
                                        3: RADD3[25:14] <= {iIO,RADD[24:14]} ;
                                    endcase
                                    RADD0[13:9] <= RADD[13:9] ;
                                    RADD1[13:9] <= RADD[13:9] ;
                                    RADD2[13:9] <= RADD[13:9] ;
                                    RADD3[13:9] <= RADD[13:9] ;
                                    case (district)
                                        0: RADD0[8:0] <= {RADD[8:0]} ;
                                        1: RADD1[8:0] <= {RADD[8:0]} ;
                                        2: RADD2[8:0] <= {RADD[8:0]} ;
                                        3: RADD3[8:0] <= {RADD[8:0]} ;
                                    endcase
                              end
                    default : RADD <= RADD ;
                endcase
            else if ( MOD_AUTO_BLOCK_ERASE_1 )
                case (add_pos)
                          1 : RADD <= {RADD[25:17],iIO,{ADD_COL{1'b0}}} ;
                          2 : RADD <= {RADD[25],iIO,RADD[16:0]} ;
                          3 : begin
                                    RADD <= {iIO,RADD[24:0]} ;
                                    case (district)
                                        0: RADD0 <= {iIO,RADD[24:0]} ;
                                        1: RADD1 <= {iIO,RADD[24:0]} ;
                                        2: RADD2 <= {iIO,RADD[24:0]} ;
                                        3: RADD3 <= {iIO,RADD[24:0]} ;
                                    endcase
                              end
                    default : RADD <= RADD ;
                endcase
            else if ( MOD_ID_READ ) begin
                case (add_pos)
                          0 : RADD <= {iIO[2:0]} ;
                    default : RADD <= RADD ;
                endcase
           end 
        end
    end

    //
    // RE signal rising
    //
    if ( {bRE,iRE}===2'b01 ) begin
        if ( {iCE,iCLE,iALE,oRY_BY,iWE}===5'b00011 ) begin
            if ( MOD_READ_MODE ) begin
                if ( (DPNT==COL_IN_PAGE-1) ) begin
                    if ( (RADD>>ADD_COL) < {(BLK_BIT+PAG_BIT){1'b1}} ) begin
//                        RADD <= RADD + (1<<ADD_COL) ;
                    end
                end
            end
            if ( MOD_ID_READ ) begin
                if (RADD < ID_NUM-1 ) RADD <= RADD + 1 ;
           end
        end
    end
end

//------------------------------------------------------------------------------
// Flag indicating selected District(for Program)
//------------------------------------------------------------------------------
// program_stack_flag : register of 4bits(bit0 to bit3)
//                      for Program operation, a bit related to a selected District is '1'.
//
//------------------------------------------------------------------------------
reg [DISTRICT_NUM-1:0] program_stack_flag ;
always @(posedge Power_on or posedge iWE) begin
    if ( Power_on ) begin
        program_stack_flag <= {DISTRICT_NUM{1'b0}} ;
    end
    else if ( {iCE,oRY_BY,iRE}===3'b011 ) begin
        if ( {iCLE,iALE}===2'b10 ) begin
            if ( (iIO !== CMD_SERIAL_DATA_INPUT ) &&
                 (iIO !== CMD_AUTO_PROGRAM_DUMMY) &&
                 (iIO !== CMD_STATUS_READ_1 )     &&
                 (iIO !== CMD_STATUS_READ_2 )        ) begin
                program_stack_flag <= {DISTRICT_NUM{1'b0}} ;
            end
        end
        else if ( {iCLE,iALE}===2'b01 ) begin
            if ( MOD_SERIAL_DATA_INPUT ) begin
                if ( add_pos == 1 ) begin
                        program_stack_flag[iIO[6:5]] <= 1'b1 ;
                end
            end
        end
    end
end

reg [DISTRICT_NUM-1:0] program_stack_flag2 ;
always @(posedge Power_on or posedge iWE) begin
    if ( Power_on ) begin
        program_stack_flag2 <= {DISTRICT_NUM{1'b0}} ;
    end
    else if ( {iCE,oRY_BY,iRE}===3'b011 ) begin
        if ( {iCLE,iALE}===2'b10 ) begin
            if ( ((iIO === CMD_AUTO_PROGRAM_TRUE) || (iIO === CMD_AUTO_PROGRAM_MULTI))
                 && (iWP === 1'b1) ) begin
                program_stack_flag2 <= program_stack_flag ;
            end
        end
    end
end

//------------------------------------------------------------------------------
// Flag indicating selected District(for Erase)
//------------------------------------------------------------------------------
// erase_stack_flag   : register of 4bits(bit0 to bit3)
//                      for Erase operation, same definition as program_stack_flag.
//
//------------------------------------------------------------------------------
reg [DISTRICT_NUM-1:0] erase_stack_flag ;
always @(posedge Power_on or posedge iWE) begin
    if ( Power_on ) begin
        erase_stack_flag <= {DISTRICT_NUM{1'b0}} ;
    end
    else if ( {iCE,oRY_BY,iRE}===3'b011 ) begin
        if ( {iCLE,iALE}===2'b10 ) begin
            if ( (iIO !== CMD_AUTO_BLOCK_ERASE_1 ) &&
                 (iIO !== CMD_STATUS_READ_1      ) &&
                 (iIO !== CMD_STATUS_READ_2      )    ) begin
                erase_stack_flag <= {DISTRICT_NUM{1'b0}} ;
            end
        end
        else if ( {iCLE,iALE,iRE}===3'b011 ) begin
            if ( MOD_AUTO_BLOCK_ERASE_1 ) begin
                if ( add_pos == 1 ) begin
                        erase_stack_flag[iIO[6:5]] <= 1'b1 ;
                end
            end
        end
    end
end

reg [DISTRICT_NUM-1:0] erase_stack_flag2 ;
always @(posedge Power_on or posedge iWE) begin
    if ( Power_on ) begin
        erase_stack_flag2 <= {DISTRICT_NUM{1'b0}} ;
    end
    else if ( {iCE,oRY_BY,iRE}===3'b011 ) begin
        if ( {iCLE,iALE}===2'b10 ) begin
            if ( (iIO === CMD_AUTO_BLOCK_ERASE_2) && (iWP === 1'b1) ) begin
                erase_stack_flag2 <= erase_stack_flag ;
            end
        end
    end
end


//------------------------------------------------------------------------------
// Input to Data Register Pointer DPNT and increment
//------------------------------------------------------------------------------
//
//    This is a pointer for addressing Data Registers.
//    Since Data Registers have 528 words, 10bit width is necessary for this register.
//    Lower 8bits are input value of IO by address input.
//    Upper 2bits depend on Area,
//          in case of Area-A, 00
//          in case of Area-B, 01
//          in case of Area-C, 10
//
//    Data Register Pointer is set by address input basically, and is incremented by
//    Data Input and Data Read operation.
//
//------------------------------------------------------------------------------
always @(posedge iWE or posedge iRE )
    if ( {iCE,oRY_BY}===2'b01 ) begin

        //
        // WE signal rising
        //
        if ( {bWE,iWE,iRE}===3'b011 ) begin
            if ( {iCLE,iALE}===2'b01 ) begin
                if ( MOD_SERIAL_DATA_INPUT || MOD_READ_MODE ) begin
                     if (add_pos == 0 ) DPNT <= {area_c,area_b,iIO & (MOD_READ_MODE_3? 8'h0f : 8'hff)} ;
                end
                else if ( MOD_AUTO_BLOCK_ERASE_1 ) begin
                    if (add_pos == 1 ) DPNT <= {COL_BIT{1'bx}} ;
                end
                else if ( MOD_ID_READ ) begin
                    if (add_pos == 0 ) DPNT <= iIO[ID_ADD_BIT-1:0] ;
                end
            end
            else if ( {iCLE,iALE}===2'b00 ) begin
                if (MOD_SERIAL_DATA_INPUT) begin
                    if ( DPNT < COL_IN_PAGE ) DPNT <= DPNT + 1 ;
                    if ( DPNT == COL_IN_PAGE-1 ) DIN_COMPLETE <= 1'b1 ;
                    if ( ^DPNT === 1'bx     ) DPNT <= {COL_BIT{1'bx}} ;
                end
            end
        end

        //
        // RE signal rising
        //
        if ( {bRE,iRE,iWE}===3'b011 ) begin
            if ( {iCLE,iALE}===2'b00 ) begin
                if (MOD_READ_MODE && BUSY_by_read_sched) begin
                    if ( DPNT < COL_IN_PAGE - 1 ) begin
                            DPNT <= DPNT + 1 ;
                    end
                    else if ( DPNT == COL_IN_PAGE - 1 ) begin
                        if ( (RADD>>ADD_COL) < {(BLK_BIT+PAG_BIT){1'b1}} ) begin
//                            DPNT <= (area_c) ? (AREA_SIZ_A + AREA_SIZ_B) : 0 ;
                        end
                    end
                    else if ( ^DPNT === 1'bx ) begin
                        DPNT <= {COL_BIT{1'bx}} ;
                    end
                end
            end
        end
    end


//------------------------------------------------------------------------------
// Input to Data Register
//------------------------------------------------------------------------------
//
//      This function describes that Data input to Data Register in Serial Data Input and
//      Data transfer from memory cell to Data Register.
//
//      At Data input, DATA0,DATA1,DATA2 and DATA3 are used and corresponding to each
//      District.
//
//------------------------------------------------------------------------------
always @(posedge iWE or BUSY_by_read ) begin

   //
   // WE signal rising
   //
   if ( {bWE,iWE,iRE}===3'b011 ) begin
        if ( {iCE,iCLE,iALE,oRY_BY}===4'b0101 ) begin
           if ( ( iIO == CMD_SERIAL_DATA_INPUT ) || ( iIO == CMD_RESET ) ) begin
                for ( column=0 ; column < COL_IN_PAGE ; column=column+1 )
                begin
                    if(~program_stack_flag[0]) DATA0[column]<={D_WIDTH{1'b1}} ;
                    if(~program_stack_flag[1]) DATA1[column]<={D_WIDTH{1'b1}} ;
                    if(~program_stack_flag[2]) DATA2[column]<={D_WIDTH{1'b1}} ;
                    if(~program_stack_flag[3]) DATA3[column]<={D_WIDTH{1'b1}} ;
                end
           end
        end
        if ( {iCE,iCLE,iALE,oRY_BY}===4'b0001 ) begin
            if (MOD_SERIAL_DATA_INPUT) begin
                if ( DPNT < COL_IN_PAGE ) begin
                    if (district==0) DATA0[DPNT] <= iIO ;
                    if (district==1) DATA1[DPNT] <= iIO ;
                    if (district==2) DATA2[DPNT] <= iIO ;
                    if (district==3) DATA3[DPNT] <= iIO ;
                end
                if ( ^DPNT === 1'bx ) begin
                    for ( column=0 ; column < COL_IN_PAGE ; column=column+1 )
                    begin
                        if (district==0) DATA0[column] <= {D_WIDTH{1'bx}} ;
                        if (district==1) DATA1[column] <= {D_WIDTH{1'bx}} ;
                        if (district==2) DATA2[column] <= {D_WIDTH{1'bx}} ;
                        if (district==3) DATA3[column] <= {D_WIDTH{1'bx}} ;
                    end
                end
            end
        end
   end

   //
   // BUSY_by_read signal falling
   //
   if ( {bBUSY_by_read,BUSY_by_read}===2'b10 ) begin
            for ( column = 0 ; column < COL_IN_PAGE ; column=column+1 )
                DATA0[column] <= {D_WIDTH{1'b1}} ;
   end

   //
   // BUSY_by_read signal rising
   //
   if ( {bBUSY_by_read,BUSY_by_read}===2'b01 ) begin
       if ( BUSY_by_ce ) begin
            for ( column = 0 ; column < COL_IN_PAGE ; column=column+1 )
                DATA0[column] <=
                    ( ^RADD === 1'bx )
                    ? {D_WIDTH{1'bx}}
                    : MEM[(RADD[(ADD_BIT-1):(ADD_COL)]<<COL_BIT)+column] ;
       end
   end
end

//------------------------------------------------------------------------------
// Input to Memory Cell
//------------------------------------------------------------------------------
//      Describe the function of Write(Program/Erase) to memory cell.
//      mem_write task is formed as following;
//      Form:   mem_write(mode,address)
//
//------------------------------------------------------------------------------
reg RSTAT_clear ;
always @(posedge Power_on or posedge iWE)
    if (Power_on) begin
        RSTAT[4:0] = {5'b00000} ;
        RSTAT_clear <= 1'b1 ;
        mem_init ;  // initializing memory
    end
    else if ( {iCE,iCLE,iRE}===3'b011 )
        if ( iIO === CMD_RESET  ) begin
            RSTAT[4:0] = {5'b00000} ;
            RSTAT_clear <= 1'b1 ;
            if ( ~BUSY_by_program ) begin
                if (program_stack_flag2[0]) mem_write(XPROGRAM,RADD0) ;
                if (program_stack_flag2[1]) mem_write(XPROGRAM,RADD1) ;
                if (program_stack_flag2[2]) mem_write(XPROGRAM,RADD2) ;
                if (program_stack_flag2[3]) mem_write(XPROGRAM,RADD3) ;
                program_stack_flag2 <= {DISTRICT_NUM{1'b0}} ;
            end
            if ( ~BUSY_by_erase ) begin
                if (erase_stack_flag2[0]) mem_write(XERASE,RADD0) ;
                if (erase_stack_flag2[1]) mem_write(XERASE,RADD1) ;
                if (erase_stack_flag2[2]) mem_write(XERASE,RADD2) ;
                if (erase_stack_flag2[3]) mem_write(XERASE,RADD3) ;
                erase_stack_flag2 <= {DISTRICT_NUM{1'b0}} ;
            end
        end
        else if ( MOD_SERIAL_DATA_INPUT && ( (iIO === CMD_AUTO_PROGRAM_TRUE ) ||
                                             (iIO === CMD_AUTO_PROGRAM_MULTI) ) )
        begin
            if (iWP) begin
                if (RSTAT_clear) RSTAT[4:0] = {5'b00000} ;
                if (program_stack_flag[0]) mem_write(PROGRAM,RADD0) ;
                if (program_stack_flag[1]) mem_write(PROGRAM,RADD1) ;
                if (program_stack_flag[2]) mem_write(PROGRAM,RADD2) ;
                if (program_stack_flag[3]) mem_write(PROGRAM,RADD3) ;
                if ( iIO === CMD_AUTO_PROGRAM_TRUE  ) RSTAT_clear <= 1'b1 ;
                if ( iIO === CMD_AUTO_PROGRAM_MULTI ) RSTAT_clear <= 1'b0 ;
            end
        end
        else if ( MOD_AUTO_BLOCK_ERASE_1 && (iIO===CMD_AUTO_BLOCK_ERASE_2) )
        begin
            if (iWP) begin
                RSTAT[4:0] = {5'b00000} ;
                if (erase_stack_flag[0]) mem_write(ERASE,RADD0) ;
                if (erase_stack_flag[1]) mem_write(ERASE,RADD1) ;
                if (erase_stack_flag[2]) mem_write(ERASE,RADD2) ;
                if (erase_stack_flag[3]) mem_write(ERASE,RADD3) ;
            end
        end

//------------------------------------------------------------------------------
// Generate Ready/Busy signal
//------------------------------------------------------------------------------
always @(posedge Power_on or posedge iWE or posedge iRE )
    if (Power_on)
    begin
        BUSY_by_reset      <= 1'b1 ;
        BUSY_by_read       <= 1'b1 ;
        BUSY_by_read_sched <= 1'b1 ;
        BUSY_by_program    <= 1'b1 ;
        BUSY_by_erase      <= 1'b1 ;
        BUSY_by_ce         <= 1'b1 ;
    end
    //
    //
    //
    else if ( {iCE,oRY_BY}==2'b00 ) begin
        if ( {bWE,iWE,iRE}==3'b011 ) begin
            if ( {iCLE,iALE}===2'b10 ) begin
                if ( (iIO == CMD_RESET) && (BUSY_by_reset===1'b1) ) begin
                    BUSY_by_reset <= 1'b0 ;
                    if ( MOD_AUTO_BLOCK_ERASE_2 ) begin
                        BUSY_by_reset       <= #(`tRST_erase)   1'b1 ;
                        BUSY_by_erase       <= #(`tRST_erase)   1'b1 ;
                    end
                    else if ( MOD_AUTO_PROGRAM ) begin
                        BUSY_by_reset       <= #(`tRST_program) 1'b1 ;
                        BUSY_by_program     <= #(`tRST_program) 1'b1 ;
                    end
                    else begin
                        BUSY_by_reset       <= #(`tRST_read)    1'b1 ;
                        BUSY_by_read        <= #(`tRST_read)    1'b1 ;
                   end
                end
            end
        end
    end
    else if ( {iCE,oRY_BY}==2'b01 ) begin
        if ( {bWE,iWE,iRE}==3'b011 ) begin
            if ( {iCLE,iALE}===2'b10 ) begin
                if ( iIO === CMD_RESET ) begin
                    BUSY_by_reset       <= #(`tWB)       1'b0 ;
                    BUSY_by_reset       <= #(`tRST_read) 1'b1 ;
                end
            end

            if ( MOD_READ_MODE && ({iCLE,iALE}===2'b01) ) begin
                if ( add_pos == 3 ) begin
                    BUSY_by_read        <= #(`tWB)       1'b0 ;
                    BUSY_by_read        <= #(`tR)        1'b1 ;
                end
            end

            if ( MOD_SERIAL_DATA_INPUT && ({iCLE,iALE}===2'b10) ) begin
                if ( (iIO === CMD_AUTO_PROGRAM_TRUE) && (iWP === 1'b1) ) begin
                    BUSY_by_program     <= #(`tWB)       1'b0 ;
                    BUSY_by_program     <= #(`tPROG)     1'b1 ;
                end
                if ( (iIO === CMD_AUTO_PROGRAM_DUMMY) && (iWP === 1'b1) ) begin
                    BUSY_by_program     <= #(`tWB)       1'b0 ;
                    BUSY_by_program     <= #(`tDBSY)     1'b1 ;
                end
                if ( (iIO === CMD_AUTO_PROGRAM_MULTI) && (iWP === 1'b1) ) begin
                    BUSY_by_program     <= #(`tWB)       1'b0 ;
                    BUSY_by_program     <= #(`tMBPBSY)   1'b1 ;
                end
            end

            if ( MOD_AUTO_BLOCK_ERASE_1 && ({iCLE,iALE}===2'b10) ) begin
                if ( (iIO === CMD_AUTO_BLOCK_ERASE_2) && (iWP === 1'b1) ) begin
                    BUSY_by_erase       <= #(`tWB)       1'b0 ;
                    BUSY_by_erase       <= #(`tBERASE)   1'b1 ;
                end
            end
        end
        if ( {bRE,iRE,iWE}===3'b011 ) begin
            if ( MOD_READ_MODE && ({iCLE,iALE}===2'b00) ) begin
                if ( (DPNT==COL_IN_PAGE-1) ) begin
                    if ( (RADD>>ADD_COL) < {(BLK_BIT+PAG_BIT){1'b1}} ) begin
                        BUSY_by_read_sched  <=              1'b0 ;
                    end
                end
            end
        end
    end


always @( negedge BUSY_by_read ) begin
        if ( BUSY_by_read_sched  === 1'b0 ) begin
            if ( (RADD>>ADD_COL) < {(BLK_BIT+PAG_BIT){1'b1}} ) begin
                DPNT <= (area_c) ? (AREA_SIZ_A + AREA_SIZ_B) : 0 ;
                RADD <= RADD + (1<<ADD_COL) ;
            end
        end
end

always @( negedge BUSY_by_read_sched ) begin
    #(`tCERY) begin
        if ( iCE === 1'b0 ) begin
             BUSY_by_read        <= #((`tRB)-(`tCERY)) 1'b0 ;
             BUSY_by_read        <= #((`tR )-(`tCERY)) 1'b1 ;
             BUSY_by_read_sched  <= #((`tR )-(`tCERY)) 1'b1 ;
        end
        else begin
             BUSY_by_read_sched  <= #((`tRB)-(`tCERY)) 1'b1 ;
        end
    end
end

always @( posedge iCE ) begin
    if ( (BUSY_by_read===1'b0) && (BUSY_by_read_sched===1'b0) ) begin
         BUSY_by_ce         <=            1'b0 ;
         BUSY_by_ce         <= #(`tCRY)   1'b1 ;
         BUSY_by_read       <= #(`tCRY-1) 1'b1 ;
         BUSY_by_read_sched <= #(`tCRY-1) 1'b1 ;
    end
    else  if ( (BUSY_by_read===1'b0) && (BUSY_by_read_sched===1'b1) ) begin
         BUSY_by_ce         <=            1'b0 ;
         BUSY_by_ce         <= #(`tCRY)   1'b1 ;
         BUSY_by_read       <= #(`tCRY-1) 1'b1 ;
    end
    else  if ( (BUSY_by_read===1'b1) && (BUSY_by_read_sched===1'b0) ) begin
         BUSY_by_read_sched <= #(`tCRY) 1'b1 ;
    end
end

always @(BUSY_by_reset)   bBUSY_by_reset   <= BUSY_by_reset ;
always @(BUSY_by_read)    bBUSY_by_read    <= BUSY_by_read ;
always @(BUSY_by_program) bBUSY_by_program <= BUSY_by_program ;
always @(BUSY_by_erase)   bBUSY_by_erase   <= BUSY_by_erase ;
always @(BUSY_by_erase)   bBUSY_by_ce      <= BUSY_by_ce ;

assign oRY_BY = BUSY_by_reset & BUSY_by_read & BUSY_by_program & BUSY_by_erase &
                BUSY_by_ce ;

always @( MOD_SERIAL_DATA_INPUT ) begin
    DIN_COMPLETE = 1'b0 ;
end

always @( BUSY_by_read ) begin
    if ( Reset_on) begin
    	if ( BUSY_by_read==1'b0 )
    	read_ok = 1'b0 ;
    	else
    	read_ok = 1'b1 ;
	end
end

//------------------------------------------------------------------------------
// Status Register (bit8,bit7,bit6)
//------------------------------------------------------------------------------
initial            RSTAT[5] <= 1'b0 ;   // IO6
always @( oRY_BY ) RSTAT[6] <= oRY_BY ; // IO7
always @( iWP    ) RSTAT[7] <= iWP ;    // IO8

//------------------------------------------------------------------------------
// Generate output value and output enable signal
//------------------------------------------------------------------------------

reg xRE ;
always @(posedge iRE) begin
    xRE <=               iRE  ;
    xRE <= #(`tRHZ-`tOH) 1'b0 ;
end

// output enable signal oIO
assign #(`tOH) oIO = ({iCE,iRE,xRE,iWE}===4'b0001) ? (MOD_STATUS_READ_1)? RSTAT&(8'hc1) :
                                           (MOD_STATUS_READ_2)? RSTAT&(8'hdf) :
                                           (~oRY_BY          )?{D_WIDTH{1'bx}}:
                                           (MOD_READ_MODE    )? DATA0[DPNT]   :
                                           (MOD_ID_READ_1    )? RID1[RADD]    :
                                           (MOD_ID_READ_2    )? RID2[RADD]    :
                                           {D_WIDTH{1'bx}}
                                         : {D_WIDTH{1'bx}}
                                         ;

// tREA == tREAID == tRSTO

wire iCE_d ;
wire iRE_d ;

localbuf #(`tOH,`tREA) localbuf_RE (iRE_d,iRE) ;
localbuf #(`tCHZ,`tCSTO) localbuf_CE (iCE_d,iCE) ;
assign eIO = ( (iCE_d  === 1'b0) &&
               (iCLE   === 1'b0) &&
               (iALE   === 1'b0) &&
               (iRE_d  === 1'b0)
             ) ? 1'b1 : 1'b0 ;

//------------------------------------------------------------------------------
// Function checking Commands
//------------------------------------------------------------------------------
//         cmd_check returns '1' in case of valid command,
//                           '0' in case of invalid command.
//
//------------------------------------------------------------------------------
function cmd_check ;

    input [D_WIDTH-1:0] now_val ;
    input [D_WIDTH-1:0] new_val ;

    begin
        case (new_val)
            CMD_SERIAL_DATA_INPUT  : cmd_check = ( oRY_BY && iWP ) ? 1'b1 : 1'b0 ;
            CMD_READ_MODE_1        : cmd_check = ( oRY_BY )        ? 1'b1 : 1'b0 ;
            CMD_READ_MODE_2        : cmd_check = ( oRY_BY )        ? 1'b1 : 1'b0 ;
            CMD_READ_MODE_3        : cmd_check = ( oRY_BY )        ? 1'b1 : 1'b0 ;
            CMD_RESET              : cmd_check =                     1'b1        ;
            CMD_AUTO_PROGRAM_TRUE  : cmd_check = ( oRY_BY && iWP ) ? 1'b1 : 1'b0 ;
            CMD_AUTO_PROGRAM_DUMMY : cmd_check = ( oRY_BY && iWP ) ? 1'b1 : 1'b0 ;
            CMD_AUTO_PROGRAM_MULTI : cmd_check = ( oRY_BY && iWP ) ? 1'b1 : 1'b0 ;
            CMD_AUTO_BLOCK_ERASE_1 : cmd_check = ( oRY_BY && iWP ) ? 1'b1 : 1'b0 ;
            CMD_AUTO_BLOCK_ERASE_2 : cmd_check = ( oRY_BY && iWP ) ? 1'b1 : 1'b0 ;
            CMD_STATUS_READ_1      : cmd_check =                     1'b1        ;
            CMD_STATUS_READ_2      : cmd_check =                     1'b1        ;
            CMD_ID_READ_1          : cmd_check = ( oRY_BY )        ? 1'b1 : 1'b0 ;
            CMD_ID_READ_2          : cmd_check = ( oRY_BY )        ? 1'b1 : 1'b0 ;
            default                : begin cmd_check = 1'b0 ;
                                     if (!($test$plusargs("error_disable_00")||
                                           $test$plusargs("error_disable_all")) )
                                     begin
                                         $write("Error: [E-00] ") ;
                                         $write("Unkown Command ") ;
                                         $write("(%h)h ",new_val);
                                         $write("Input Ignore at %0t \n",
                                                 $time ) ;
                                     end
                                     end
        endcase
    end

endfunction

//------------------------------------------------------------------------------
// Task writing memory cells
//------------------------------------------------------------------------------
//
//  Form : mem_write(mode,address,purge)
//  Write to memory;
//         in case mode is PROGRAM, contens of selected Data Register of district are
//                                  transferred to the selected page.
//         in case of mode is ERASE, the selected block is erased.
//         in case of mode XPROGRAM, the selected page gets unknown value.
//         in case of mode XERASE, the selected block gets unknown value.
//
//------------------------------------------------------------------------------
task mem_write ;

    input [2:0]         mode ;
    input [ADD_BIT-1:0] add ;

    integer bit ;
    integer column ;
    integer page ;
    integer block ;
    integer tmp_add ;
    integer failbit ;
    integer val ;

    reg [1:0]         district ;

    begin

        district = add[15:14] ;
        block = add[(ADD_BIT-1):(PAG_BIT+ADD_COL)] ;
        scan_bad(block,val) ;

        if ( mode == XPROGRAM ) begin
            for ( column = 0 ; column < COL_IN_PAGE ; column=column+1 ) begin
                tmp_add = (add[(ADD_BIT-1):(ADD_COL)]<<COL_BIT)+column ;
                MEM[tmp_add] <= {D_WIDTH{1'bx}} ;
            end
        end

        if ( mode == XERASE   ) begin
            block_set(block,1'bx) ;
        end

        if ( (mode == PROGRAM) && (val !== 1) ) begin
            for ( column = 0 ; column < COL_IN_PAGE ; column=column+1 ) begin
                tmp_add = (add[(ADD_BIT-1):(ADD_COL)]<<COL_BIT)+column ;

                     scan_fail(tmp_add,failbit) ;
           
                if ( 8'h00 != failbit) begin 
                    RSTAT[0]          = 1'b1 ;
                    RSTAT[district+1] = 1'b1 ;
                    $display("MSG: address:%x bit:%0b is erase fail ... at %0tns",tmp_add,failbit,$time) ;
                end
                else begin
                    failbit = 8'b00000000 ;
                end

                MEM[tmp_add] <=
                    (district==0) ? (DATA0[column] & MEM[tmp_add]) ^ failbit :
                    (district==1) ? (DATA1[column] & MEM[tmp_add]) ^ failbit :
                    (district==2) ? (DATA2[column] & MEM[tmp_add]) ^ failbit :
                    (district==3) ? (DATA3[column] & MEM[tmp_add]) ^ failbit :
                    {D_WIDTH{1'bx}} ;
            end
        end

        if ( (mode == ERASE) && (val !== 1) ) begin

            $display("MSG: Block %0d Erase ...",block) ;

            for ( page=0 ; page < PAGE_IN_BLOCK ; page = page + 1 ) begin
                for ( column=0 ; column < COL_IN_PAGE ; column = column + 1 ) begin
                   tmp_add = (block<<(PAG_BIT+COL_BIT)) + (page<<(COL_BIT)) + (column) ;

                    MEM[tmp_add] <= {D_WIDTH{1'b1}} ;

                    scan_fail(tmp_add,failbit) ;
                    
                    if ( 8'h00 != failbit) begin 

                        RSTAT[0]          =  1'b1 ;
                        RSTAT[district+1] =  1'b1 ;
                        $display("MSG: address:%x bit:%0b is erase fail ... at %0tns",tmp_add,failbit,$time) ;
                        MEM[tmp_add] <= 8'hFF ^ failbit ;

                    end
                end
            end
        end
    end
endtask

//------------------------------------------------------------------------------
// Task Memory Initialize
//------------------------------------------------------------------------------
// If "MemoryInitEnable" defined  "1", whole CHIP data "1" except for Bad Block.
//------------------------------------------------------------------------------
task mem_init ;

    integer block ;
    integer val ;
    integer i ;

    if ( `MemoryInitEnable ) begin

        for ( block = 0  ; block < (1<<BLK_BIT) ; block = block + 1 ) begin
              scan_bad(block,val) ;
            if (val == 1) begin
            	block_set(block,1'b0) ;
                $display("MSG: Block %0d is Bad Block ...",block) ;
            end
            else
            	block_set(block,1'b1) ;
        end
    end
endtask

//------------------------------------------------------------------------------
// Task making whole 1 Block set
//------------------------------------------------------------------------------
task block_set ;
    input [(1<<BLK_BIT)-1:0] block ;
    input value ;

    integer column ;
    integer page ;
    integer tmp_add ;

    begin

            for ( page=0 ; page < PAGE_IN_BLOCK ; page = page + 1 ) begin
                for ( column=0 ; column < COL_IN_PAGE ; column = column + 1 ) begin
                   tmp_add = (block<<(PAG_BIT+COL_BIT)) + (page<<(COL_BIT)) + (column) ;

                MEM[tmp_add] <= {D_WIDTH{value}} ;

                end
            end
    end
endtask

//------------------------------------------------------------------------------
// Task scan fail
//------------------------------------------------------------------------------
task scan_fail ;
    input  [ADD_BIT-1:0] inadd ;
    output [7:0] fail_bit ;

    integer i ;

    begin
            fail_bit = 8'h00 ;
			// XXXXXXXX
//            for ( i=0 ; i < FAIL_NUM ; i = i + 1 ) begin
//                if( inadd == FAIL_ADD[i] )
//                     fail_bit = FAIL_BIT[i] ;
//            end
    end
endtask

//------------------------------------------------------------------------------
// Task scan block
//------------------------------------------------------------------------------
task scan_bad ;
    input  [BLK_BIT-1:0] inadd ;
    output val ;
    begin
            val = 0 ;
    end
endtask

//------------------------------------------------------------------------------
// Signal Operation time
//------------------------------------------------------------------------------
always @( WE ) begin
  if ( WE==1'b1) begin
      rWE = $time ;
      fWE2 = fWE ;
  end
  else begin
      fWE = $time ;
  end
end

always @( CE ) begin
  if ( CE==1'b1 ) begin
      rCE = $time ;
  end
  else begin
      fCE = $time ;
  end
end

always @( ALE ) begin
  if( ALE==1'b1 ) begin
      rALE = $time ;
  end
  else begin
      fALE = $time ;
  end
end

always @( CLE ) begin
  if( CLE==1'b1 ) begin
      rCLE = $time ;
  end
  else begin
      fCLE = $time ;
  end
end

always @( RE ) begin
  if ( RE==1'b1) begin
      rRE = $time ;
      fRE2 = fRE ;
  end
  else begin
      fRE = $time ;
  end
end

always @( RY_BY ) begin
  if( RY_BY==1'b1 ) begin
      rRY_BY = $time ;
  end
  else begin
      fRY_BY = $time ;
  end
end

always @( IO ) begin
      rfIO = $time ;
end

always @( WP ) begin
  if ( WP==1'b1 ) begin
      rWP = $time ;
  end
  else begin
      fWP = $time ;
  end
end


// tCH Check
always @( rCE or fCE ) begin
  if( $time > 0 ) begin
    if ( CE==1'b1 ) begin
        if( $time-rWE < `tCH ) begin
          $write("Error: %0tns : CE Hold Time (tCH) was not proper.\n",$time) ;
        end
    end
    else if($time-rWE < `tWHC && MOD_STATUS_READ ) begin
          $write("Error: %0tns : WE High to CE Low (tWHC) was not proper.\n",$time) ;
    end
  end
end

// tCLH Check
always @( rCLE or fCLE ) begin
  if( $time > 0 ) begin
      if( $time-rWE < `tCLH) begin
        $write ("Error: %0tns : CLE Hold Time (tCLH/at command) was not proper.\n",$time) ;
      end
  end
end

// tALH Check
always @( rALE or fALE ) begin
  if( $time > 0 ) begin
      if( $time-rWE < `tALH) begin
        $write ("Error: %0tns : ALE Hold Time (tALH) was not proper.\n",$time) ;
      end
  end
end

// tDS, tWP, tWH, tWC Check
always @( WE ) begin      
  if( $time > 0 ) begin
    if( WE==1'b1 ) begin
        if( $time-rfIO < `tDS) begin 
          # 1;
          if (CE === 0)
              $write ("Error: %0tns : Data Setup Time (tDS) was not proper.\n",$time) ;
        end
        if( $time-fWE < `tWP) begin  
          # 1;
          if (CE === 0)
              $write ("Error: %0tns : Write Pulse width (tWP) was not proper.\n",$time) ;
        end
    end
    else if( WE==1'b0 ) begin
        if( $time-rWE < `tWH) begin  
          $write ("Error: %0tns : WE High Hold Time (tWH) was not proper.\n",$time) ;
        end
        if( $time-fWE2 < `tWC) begin 
          $write ("Error: %0tns : Write Cycle Time (tWC) was not proper.\n",$time) ;
        end
    end
  end
end

// tWW Check
always @( fWE ) begin 
  if( $time > 0 ) begin
        if( $time-rWP < `tWW) begin        // tWW(High)
          $write ("Error: %0tns : WP High to WE Low (tWW) was not proper.\n",$time) ;
        end
        else if( $time-fWP < `tWW) begin     // tWW(Low)
          $write ("Error: %0tns : WP Low to WE Low (tWW) was not proper.\n",$time) ;
        end
  end
end

// tRP, tREH, tRC Check
always @( RE ) begin
  if( $time > 0 ) begin
    if( RE==1'b1 ) begin
        if($time-fRE < `tRP) begin
		  # 1;
		  if (CE === 0)
              $write("Error: %0tns : Read Pulse width (tRP) was not proper.\n",$time) ;
        end
    end
    else if( RE==1'b0 ) begin
        if($time-rRE < `tREH) begin
          $write("Error: %0tns : Read High Hold Time (tREH) was not proper.\n",$time) ;
        end
        if($time-fRE2 < `tRC) begin
          $write("Error: %0tns : Read Cycle time (tRC) was not proper.\n",$time) ;
        end
    end
  end
end

// tRR, tWHR, tAR1, tAR2 Check
always @( fRE ) begin 
  if( $time > 0 ) begin
    if($time-rRY_BY < `tRR) begin
      $write("Error: %0tns : Ready to RE Falling edge (tRR) was not proper.\n",$time) ;
    end
    if($time-rWE < `tWHR && MOD_STATUS_READ ) begin
      $write("Error: %0tns : WE High to RE Low (tWHR) was not proper.\n",$time) ;
    end

    if( MOD_ID_READ ) begin
        if($time-fALE < `tAR1) begin
			if (CE === 0)
	            $write("Error: %0tns : ALE Low to RE Low (tAR1) was not proper.\n",$time) ;
        end
        if($time-fCE < `tCR && (MOD_STATUS_READ==1'b0) ) begin
            $write("Error: %0tns : CE Low to RE Low (tCR) was not proper.\n",$time) ;
        end
    end
    else if( MOD_READ_MODE ) begin
        if($time-fALE < `tAR2) begin
            $write("Error: %0tns : ALE Low to RE Low (tAR2) was not proper.\n",$time) ;
        end
    end
  end
end

// tDH Check
always @( IO ) begin  
  if( $time > 0 ) begin
    if( $time-rWE < `tDH ) begin
        # 1;
        if (CE === 0)
           $write("Error: %0tns : Data Hold time (tDH) was not proper.\n",$time) ;
    end
  end
end

// Setup/Hold timing 
// tCLS Check 
always @( rCLE or fCLE) begin 
  if( $time > 0  && WE==1'b0 && $time!=fWE ) begin
    if( ALE==1'b1 || (MOD_SERIAL_DATA_INPUT && add_pos > 0) ) begin
      if( CLE==1'b0 )
        $write("Error: %0tns : CLE Setup Time (tCLS/at address or data in) was not proper.\n",$time) ; // ADD
    end
    else begin
      if( CLE==1'b1 )
        $write("Error: %0tns : CLE Setup Time (tCLS/at command) was not proper.\n",$time) ; // COM
    end
  end
end

// tCLH Check 
always @( rCLE or fCLE) begin 
  if( $time > 0  && WE==1'b0 && $time!=fWE ) begin
    if( ALE==1'b1 || (MOD_SERIAL_DATA_INPUT && add_pos > 0) ) begin
      if( $time-rWE < `tCLH )
        $write("Error: %0tns : CLE Hold Time (tCLH/at address or data in) was not proper.\n",$time) ;
    end
    else begin
      if( CLE==1'b0 )
        $write("Error: %0tns : CLE Hold Time (tCLH/at command) was not proper.\n",$time) ;
    end
  end
end

// tALS Check 
always @( rALE or fALE) begin 
  if( $time > 0  && WE==1'b0 && $time!=fWE ) begin
    if( CLE==1'b1 || (MOD_SERIAL_DATA_INPUT && add_pos > 0) ) begin
      if( ALE==1'b0 )
        $write("Error: %0tns : ALE Setup Time (tALS/at command or data in) was not proper.\n",$time) ;
    end
    else begin
      if( ALE==1'b1 )
        $write("Error: %0tns : ALE Setup Time (tALS/at address) was not proper.\n",$time) ;
    end
  end
end

// tALH Check 
always @( rALE or fALE) begin 
  if( $time > 0  && WE==1'b0 && $time!=fWE ) begin
    if( CLE==1'b1 || (MOD_SERIAL_DATA_INPUT && add_pos > 0) ) begin
      if( ALE==1'b1 )
        $write("Error: %0tns : ALE Hold Time (tALH/at command or data in) was not proper.\n",$time) ;
    end
    else begin
      if( ALE==1'b0 )
        $write("Error: %0tns : ALE Hold Time (tALH/at address) was not proper.\n",$time) ;
    end
  end
end

// tCS Check 
always @( fCE) begin
  if( $time > 0  && WE==1'b0 && $time!=fWE ) begin
          $write("Error: %0tns : CE Setup Time (tCS) was not proper.\n",$time) ;
  end
end

// tCH Check 
always @( posedge CE) begin
  if( $time > 0  && WE==1'b0 ) begin
          $write("Error: %0tns : CE Hold Time (tCH) was not proper.\n",$time) ;
  end
end

// tAR1, tAR2 Check 
always @( fALE ) begin
  if( $time > 0 && RE==1'b0 && $time!=fRE )begin
      if( MOD_ID_READ ) begin
          $write("Error: %0tns : ALE Low to RE Low (tAR1/at ID Read) was not proper.\n",$time) ;
      end
      else if( MOD_READ_MODE ) begin
          $write("Error: %0tns : ALE Low to RE Low (tAR2/at Read Mode) was not proper.\n",$time) ;
      end
  end
end

// tCR Check 
always @( fCE ) begin
  if( $time > 0 && RE==1'b0 && $time!=fRE )begin
      if( MOD_ID_READ ) begin
          $write("Error: %0tns : CE Low to RE Low (tCR/at ID Read) was not proper.\n",$time) ;
      end
  end
end

// tCLS Check 
always @( fCLE ) begin
  if( $time > 0 && RE==1'b0 && $time!=fRE )begin
      if( MOD_STATUS_READ ) begin
          $write("Error: %0tns : CLE Setup Time (tCLS/at Status Read) was not proper.\n",$time); // ST READ
      end
  end
end

//Warning Message
always @( negedge RE ) begin
  if( MOD_READ_MODE ) begin
    if( MOD_STATUS_READ==1'b0 ) begin
        if( !read_ok ) begin
          $write("Error: %0tns : RE should be input after address input.\n",$time) ;
        end
        if( RY_BY==1'b0 ) begin
          $write("Error: %0tns : RE should be fix to 'H' while the device is in busy state.\n",$time) ;
        end
    end
  end
end

// tCLS Check 
always @( posedge WE ) begin
  if( CE==1'b0 && RE==1'b1 && CLE==1'b0 && ALE==1'b0 && DIN_COMPLETE==1'b1 ) begin
    $write("Error: %0tns : When reached last column address, /WE should not be toggle.\n",$time) ;
  end
end

// tCLS Check 
always @( posedge CE ) begin
  if( MOD_READ_MODE ) begin
// frank;
// this is bogus, it does not handle terminations;
    // XXX $write("Error: %0tns : /CE was set to 'H'  while the device was in read operation.\n",$time) ;
    $write("Warning: %0tns : cmd terminated\n", $time);
  end
end

// tCLS Check 
always @( negedge RE or posedge CLE or posedge ALE ) begin
  if( CE==1'b0 && RE==1'b0 && (MOD_STATUS_READ==1'b0) ) begin
    if( CLE!=1'b0 || ALE!=1'b0 ) begin
      $write("Error: %0tns : Illegal operation. CLE=0 and ALE=0 was not selected in read mode.\n",$time) ;
      read_flg = 1'b1 ;
    end
  end
end

always @(IO) begin
    if ( {IO} === {D_WIDTH{1'bz}})
    IO_hiz = $time ;
end

`ifdef TOSHIBA_ORIGINAL
always @( fRE ) begin
  if( ($time > 0 && {IO}!=={D_WIDTH{1'bz}}) ) begin
        $write ("Error: %0tns: Timing violation tIR\n",$time) ;
    end
end
`endif

endmodule

module localbuf (z,a) ;
parameter rt = 0 ;
parameter ft = 0 ;
output z ;
input  a ;
reg    z ;
always @ (posedge a) z <= #(rt) a ;
always @ (negedge a) z <= #(ft) a ;
endmodule