OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [orpsocv2/] [bench/] [verilog/] [AT26DFxxx.v] - Diff between revs 6 and 403

Go to most recent revision | Only display areas with differences | Details | Blame | View Log

Rev 6 Rev 403
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
// This is the property of PERFTRENDS TECHNOLOGIES PRIVATE LIMITED and
// This is the property of PERFTRENDS TECHNOLOGIES PRIVATE LIMITED and
// possession or use of file has to be with the written LICENCE AGGREMENT
// possession or use of file has to be with the written LICENCE AGGREMENT
// from PERFTRENDS TECHNOLOGIES PRIVATE LIMITED.
// from PERFTRENDS TECHNOLOGIES PRIVATE LIMITED.
//
//
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
//
//
// Project : ATMEL Data Flash Device
// Project : ATMEL Data Flash Device
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
// File         : $RCSfile: AT26DFxxx.v,v $
// File         : $RCSfile: AT26DFxxx.v,v $
// Path         : $Source: /home/cvs/atmel_flash_dev/design_26x/AT26DFxxx.v,v $
// Path         : $Source: /home/cvs/atmel_flash_dev/design_26x/AT26DFxxx.v,v $
// Author       : $ Devi Vasumathy N $
// Author       : $ Devi Vasumathy N $
// Created on   : $ 13-03-07 $
// Created on   : $ 13-03-07 $
// Revision     : $Revision: 1.8 $
// Revision     : $Revision: 1.8 $
// Last modified by : $Author: devivasumathy $
// Last modified by : $Author: devivasumathy $
// Last modified on : $Date: 2007/05/10 05:15:51 $
// Last modified on : $Date: 2007/05/10 05:15:51 $
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
// Module               : AT26DFxxx.v
// Module               : AT26DFxxx.v
// Description          : BFM for devices AT25DF041A, AT26DF081A, 
// Description          : BFM for devices AT25DF041A, AT26DF081A, 
//                        AT26DF161A, AT26DF321
//                        AT26DF161A, AT26DF321
//
//
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
//
//
// Design hierarchy : _top.v/top_1.v/top_2.v/...
// Design hierarchy : _top.v/top_1.v/top_2.v/...
// Instantiated Modules : top_1.v, top_2.v
// Instantiated Modules : top_1.v, top_2.v
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
// Revision history :
// Revision history :
// $Log: AT26DFxxx.v,v $
// $Log: AT26DFxxx.v,v $
// Revision 1.8  2007/05/10 05:15:51  devivasumathy
// Revision 1.8  2007/05/10 05:15:51  devivasumathy
// Card Memory preload
// Card Memory preload
//
//
// Revision 1.7  2007/04/27 09:09:40  devivasumathy
// Revision 1.7  2007/04/27 09:09:40  devivasumathy
// *** empty log message ***
// *** empty log message ***
//
//
// Revision 1.6  2007/04/17 10:50:59  devivasumathy
// Revision 1.6  2007/04/17 10:50:59  devivasumathy
// Timing verified
// Timing verified
//
//
// Revision 1.5  2007/04/16 06:24:44  devivasumathy
// Revision 1.5  2007/04/16 06:24:44  devivasumathy
// *** empty log message ***
// *** empty log message ***
//
//
// Revision 1.4  2007/04/10 09:37:36  devivasumathy
// Revision 1.4  2007/04/10 09:37:36  devivasumathy
// *** empty log message ***
// *** empty log message ***
//
//
// Revision 1.3  2007/04/09 12:06:09  devivasumathy
// Revision 1.3  2007/04/09 12:06:09  devivasumathy
// *** empty log message ***
// *** empty log message ***
//
//
// Revision 1.2  2007/04/05 14:45:12  devivasumathy
// Revision 1.2  2007/04/05 14:45:12  devivasumathy
// *** empty log message ***
// *** empty log message ***
//
//
// Revision 1.1  2007/04/05 11:32:18  devivasumathy
// Revision 1.1  2007/04/05 11:32:18  devivasumathy
// AT26DFx data flash model
// AT26DFx data flash model
//
//
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
 
 
`timescale 1ns/10ps      //ME added
`timescale 1ns/10ps      //ME added
 
 
`define def_321 1        //ME added
`define def_321 1        //ME added
 
 
 
 
module AT26DFxxx (
module AT26DFxxx (
                CSB,
                CSB,
                SCK,
                SCK,
                SI,
                SI,
                WPB,
                WPB,
`ifdef def_321
`ifdef def_321
`else
`else
                HOLDB,
                HOLDB,
`endif
`endif
                SO);
                SO);
 
 
// ******************************************************************** //
// ******************************************************************** //
//                      Port Declaration:
//                      Port Declaration:
// ******************************************************************** //
// ******************************************************************** //
 
 
input   CSB;            // Chip Select!
input   CSB;            // Chip Select!
input   SCK;            // Serial Clock
input   SCK;            // Serial Clock
input   SI;             // Serial Input
input   SI;             // Serial Input
input   WPB;            // Write Protect!
input   WPB;            // Write Protect!
`ifdef def_321
`ifdef def_321
`else
`else
input   HOLDB;          // Hold!
input   HOLDB;          // Hold!
`endif
`endif
output  SO;             // Serial Output
output  SO;             // Serial Output
 
 
`ifdef def_321
`ifdef def_321
        wire  HOLDB;
        wire  HOLDB;
`endif
`endif
 
 
parameter DEVICE = "AT25DF041A";        // Device selected
parameter DEVICE = "AT25DF041A";        // Device selected
parameter PRELOAD = 1;                  // preload memory with content in MEMORY_FILE
parameter PRELOAD = 1;                  // preload memory with content in MEMORY_FILE
parameter MEMORY_FILE = "flash.in";     // Memory pre-load
parameter MEMORY_FILE = "flash.in";     // Memory pre-load
 
   parameter VERBOSE = 1;
 
 
// ********************************************************************* //
// ********************************************************************* //
//Timing Parameters :
//Timing Parameters :
// ******************************************************************** //
// ******************************************************************** //
 
 
// Fixed parameters
// Fixed parameters
parameter fRDLF   = 33;         // SCK Frequency for read Array (Low freq - 03h opcode)
parameter fRDLF   = 33;         // SCK Frequency for read Array (Low freq - 03h opcode)
//representation in ns
//representation in ns
parameter tDIS    = 6;          // Output Disable time
parameter tDIS    = 6;          // Output Disable time
parameter tV      = 6;          // Output Valid time
parameter tV      = 6;          // Output Valid time
parameter tOH     = 0 ;          // Output Hold time
parameter tOH     = 0 ;          // Output Hold time
 
 
parameter tHLQZ   = 6 ;         // HOLD! Low to Output High-z
parameter tHLQZ   = 6 ;         // HOLD! Low to Output High-z
parameter tHHQX   = 6 ;         // HOLD! High to Output Low-z
parameter tHHQX   = 6 ;         // HOLD! High to Output Low-z
 
 
parameter tSECP   = 20;         // Sector Protect Time
parameter tSECP   = 20;         // Sector Protect Time
parameter tSECUP  = 20;         // Sector Unprotect Time
parameter tSECUP  = 20;         // Sector Unprotect Time
 
 
parameter tEDPD   = 3000;       // Chip Select high to Deep Power-down (3 us)
parameter tEDPD   = 3000;       // Chip Select high to Deep Power-down (3 us)
parameter tRDPD   = 3000;       // Chip Select high to Stand-by Mode
parameter tRDPD   = 3000;       // Chip Select high to Stand-by Mode
parameter tWRSR   = 200;        // Write Status Register Time
parameter tWRSR   = 200;        // Write Status Register Time
parameter tPP     = 5000000;    // Page Program Time
parameter tPP     = 5000000;    // Page Program Time
parameter tBLKE4  = 200000000;  // Block Erase Time 4-kB
parameter tBLKE4  = 200000000;  // Block Erase Time 4-kB
parameter tBLKE32 = 600000000;  // Block Erase Time 32-kB
parameter tBLKE32 = 600000000;  // Block Erase Time 32-kB
parameter tCHPEn  = 1000000000; // local chip erase time
parameter tCHPEn  = 1000000000; // local chip erase time
 
 
// variable parameters
// variable parameters
// ******************************************************************** //
// ******************************************************************** //
//parameter tBP     = 7000;             // Byte Program Time
//parameter tBP     = 7000;             // Byte Program Time
//parameter tBLKE64 = 950000000;        // Block Erase Time 64-kB
//parameter tBLKE64 = 950000000;        // Block Erase Time 64-kB
//parameter tCHPE   = 3000000000;       // Chip Erase Time // this is actual;
//parameter tCHPE   = 3000000000;       // Chip Erase Time // this is actual;
                                        // due to simulation warning splitted into 2 parameters as tCHPE = tmult * tCHPEn
                                        // due to simulation warning splitted into 2 parameters as tCHPE = tmult * tCHPEn
//parameter tmult   = 3;                // Multiplication factor for chip erase timing
//parameter tmult   = 3;                // Multiplication factor for chip erase timing
 
 
parameter tBP =         (DEVICE == "AT25DF041A") ? 7000 :
parameter tBP =         (DEVICE == "AT25DF041A") ? 7000 :
                        (DEVICE == "AT26DF081A") ? 7000 :
                        (DEVICE == "AT26DF081A") ? 7000 :
                        (DEVICE == "AT26DF161A") ? 7000 :
                        (DEVICE == "AT26DF161A") ? 7000 :
                        (DEVICE == "AT26DF321")  ? 6000 : 7000;
                        (DEVICE == "AT26DF321")  ? 6000 : 7000;
 
 
parameter tBLKE64 =     (DEVICE == "AT25DF041A") ? 950000000 :
parameter tBLKE64 =     (DEVICE == "AT25DF041A") ? 950000000 :
                        (DEVICE == "AT26DF081A") ? 950000000 :
                        (DEVICE == "AT26DF081A") ? 950000000 :
                        (DEVICE == "AT26DF161A") ? 950000000 :
                        (DEVICE == "AT26DF161A") ? 950000000 :
                        (DEVICE == "AT26DF321")  ? 1000000000 :950000000 ;
                        (DEVICE == "AT26DF321")  ? 1000000000 :950000000 ;
 
 
parameter tmult =       (DEVICE == "AT25DF041A") ? 3 :
parameter tmult =       (DEVICE == "AT25DF041A") ? 3 :
                        (DEVICE == "AT26DF081A") ? 6 :
                        (DEVICE == "AT26DF081A") ? 6 :
                        (DEVICE == "AT26DF161A") ? 12 :
                        (DEVICE == "AT26DF161A") ? 12 :
                        (DEVICE == "AT26DF321")  ? 36 : 3;
                        (DEVICE == "AT26DF321")  ? 36 : 3;
 
 
//parameter fSCK    = 70;       // Serial clock (SCK) Frequency in MHz
//parameter fSCK    = 70;       // Serial clock (SCK) Frequency in MHz
//parameter tSCKH   = 6.4;      // SCK High time
//parameter tSCKH   = 6.4;      // SCK High time
//parameter tSCKL   = 6.4;      // SCK Low time
//parameter tSCKL   = 6.4;      // SCK Low time
 
 
parameter fSCK =        (DEVICE == "AT25DF041A") ? 70 :
parameter fSCK =        (DEVICE == "AT25DF041A") ? 70 :
                        (DEVICE == "AT26DF081A") ? 70 :
                        (DEVICE == "AT26DF081A") ? 70 :
                        (DEVICE == "AT26DF161A") ? 70 :
                        (DEVICE == "AT26DF161A") ? 70 :
                        (DEVICE == "AT26DF321")  ? 66 : 70;
                        (DEVICE == "AT26DF321")  ? 66 : 70;
 
 
parameter tSCKH =       (DEVICE == "AT25DF041A") ? 6.4 :
parameter tSCKH =       (DEVICE == "AT25DF041A") ? 6.4 :
                        (DEVICE == "AT26DF081A") ? 6.4 :
                        (DEVICE == "AT26DF081A") ? 6.4 :
                        (DEVICE == "AT26DF161A") ? 6.4 :
                        (DEVICE == "AT26DF161A") ? 6.4 :
                        (DEVICE == "AT26DF321")  ? 6.8 : 6.4;
                        (DEVICE == "AT26DF321")  ? 6.8 : 6.4;
 
 
parameter tSCKL =       (DEVICE == "AT25DF041A") ? 6.4 :
parameter tSCKL =       (DEVICE == "AT25DF041A") ? 6.4 :
                        (DEVICE == "AT26DF081A") ? 6.4 :
                        (DEVICE == "AT26DF081A") ? 6.4 :
                        (DEVICE == "AT26DF161A") ? 6.4 :
                        (DEVICE == "AT26DF161A") ? 6.4 :
                        (DEVICE == "AT26DF321")  ? 6.8 : 6.4;
                        (DEVICE == "AT26DF321")  ? 6.8 : 6.4;
// ******************************************************************** //
// ******************************************************************** //
 
 
// ********************** Manufacturer ID ********************** //
// ********************** Manufacturer ID ********************** //
parameter [31:0] MAN_ID = (DEVICE == "AT25DF041A") ? 32'h1F_04_00_00 :
parameter [31:0] MAN_ID = (DEVICE == "AT25DF041A") ? 32'h1F_04_00_00 :
                        (DEVICE == "AT26DF081A") ? 32'h1F_45_01_00 :
                        (DEVICE == "AT26DF081A") ? 32'h1F_45_01_00 :
                        (DEVICE == "AT26DF161A") ? 32'h1F_46_01_00 :
                        (DEVICE == "AT26DF161A") ? 32'h1F_46_01_00 :
                        (DEVICE == "AT26DF321")  ? 32'h1F_47_00_00 : 32'h1F_04_00_00;
                        (DEVICE == "AT26DF321")  ? 32'h1F_47_00_00 : 32'h1F_04_00_00;
 
 
// ********* Memory And Access Related Declarations ***************** //
// ********* Memory And Access Related Declarations ***************** //
 
 
parameter BLOCK64 =     (DEVICE == "AT25DF041A") ? 8  : // no of 64kB blocks
parameter BLOCK64 =     (DEVICE == "AT25DF041A") ? 8  : // no of 64kB blocks
                        (DEVICE == "AT26DF081A") ? 16 :
                        (DEVICE == "AT26DF081A") ? 16 :
                        (DEVICE == "AT26DF161A") ? 32 :
                        (DEVICE == "AT26DF161A") ? 32 :
                        (DEVICE == "AT26DF321")  ? 64 : 8 ;
                        (DEVICE == "AT26DF321")  ? 64 : 8 ;
 
 
parameter SECTOR =      (DEVICE == "AT25DF041A") ? 11 : // no of sectors
parameter SECTOR =      (DEVICE == "AT25DF041A") ? 11 : // no of sectors
                        (DEVICE == "AT26DF081A") ? 19 :
                        (DEVICE == "AT26DF081A") ? 19 :
                        (DEVICE == "AT26DF161A") ? 32 :
                        (DEVICE == "AT26DF161A") ? 32 :
                        (DEVICE == "AT26DF321")  ? 64 : 11 ;
                        (DEVICE == "AT26DF321")  ? 64 : 11 ;
 
 
// total memory size = no. of blocks * 64 k bits (i.e each block contains 64 KB)
// total memory size = no. of blocks * 64 k bits (i.e each block contains 64 KB)
parameter MEMSIZE = BLOCK64 * 64 * 1024; // total memory size
parameter MEMSIZE = BLOCK64 * 64 * 1024; // total memory size
 
 
reg [7:0] memory [MEMSIZE-1:0]; // memory of selected device
reg [7:0] memory [MEMSIZE-1:0]; // memory of selected device
reg [7:0] status_reg;            // Status register
reg [7:0] status_reg;            // Status register
reg [63:0] sector_reg;           // Sector protection reg
reg [63:0] sector_reg;           // Sector protection reg
reg [7:0] int_buffer [255:0];     // internal buffer to store data in page programming mode
reg [7:0] int_buffer [255:0];     // internal buffer to store data in page programming mode
// ****************** ***************** ***************** //
// ****************** ***************** ***************** //
 
 
 
 
// ********* Registers to track the current operation of the device ******** //
// ********* Registers to track the current operation of the device ******** //
reg deep_power_down;            // Device in Deep Power Down Mode
reg deep_power_down;            // Device in Deep Power Down Mode
reg status_read;                // Status register is being read
reg status_read;                // Status register is being read
reg erasing_block4;             // 4kB block erase
reg erasing_block4;             // 4kB block erase
reg erasing_block32;            // 32kB block erase
reg erasing_block32;            // 32kB block erase
reg erasing_block64;            // 64kB block erase
reg erasing_block64;            // 64kB block erase
reg erasing_chip;               // chip erase
reg erasing_chip;               // chip erase
reg rd_dummy;                   // low/high freq read array
reg rd_dummy;                   // low/high freq read array
reg byte_prog;                  // Byte/page programming
reg byte_prog;                  // Byte/page programming
reg seq_byte_prog;              // sequential byte programming
reg seq_byte_prog;              // sequential byte programming
reg seq_byte_start;             // sequential byte programming start intimation
reg seq_byte_start;             // sequential byte programming start intimation
 
 
 
 
// ********* Events to trigger some task based on opcode *********** //
// ********* Events to trigger some task based on opcode *********** //
event  EDPD;            // Deep Power-down (enable)
event  EDPD;            // Deep Power-down (enable)
event  RDPD;            // Resume from Deep Power-down
event  RDPD;            // Resume from Deep Power-down
event  RA;              // Read Array
event  RA;              // Read Array
//event  RAL;           // Read Array (Low frequency)
//event  RAL;           // Read Array (Low frequency)
event  BE4;             // Block Erase 4KB
event  BE4;             // Block Erase 4KB
event  BE32;            // Block Erase 32KB
event  BE32;            // Block Erase 32KB
event  BE64;            // Block Erase 64KB
event  BE64;            // Block Erase 64KB
event  CE;              // Chip Erase
event  CE;              // Chip Erase
event  BP;              // byte /page program
event  BP;              // byte /page program
//event  SBPE;          // Sequential Byte Program Mode Erase
//event  SBPE;          // Sequential Byte Program Mode Erase
event  SBP;             // Sequential Byte Program
event  SBP;             // Sequential Byte Program
event  WE;              // Write Enable
event  WE;              // Write Enable
event  WD;              // Write Disable
event  WD;              // Write Disable
event  PS;              // Protect Sector
event  PS;              // Protect Sector
event  UPS;             // Un-Protect Sector
event  UPS;             // Un-Protect Sector
event  RSPR;            // Read Sector Protection Register
event  RSPR;            // Read Sector Protection Register
event  RSR;             // Read Status Register
event  RSR;             // Read Status Register
event  WSR;             // Write Status Register
event  WSR;             // Write Status Register
event  MIR;             // Manufacturer ID Read 
event  MIR;             // Manufacturer ID Read 
 
 
 
 
/******** Other variables/registers ******************/
/******** Other variables/registers ******************/
reg [23:0] temp_addr;            // to store mem address temporarily
reg [23:0] temp_addr;            // to store mem address temporarily
reg [23:0] current_address;      // to store mem address
reg [23:0] current_address;      // to store mem address
reg [7:0] temp_data;             // temp read data from memory
reg [7:0] temp_data;             // temp read data from memory
reg [7:0] data_in;               // data in for byte programming
reg [7:0] data_in;               // data in for byte programming
reg [7:0] read_data;             // register in which opcode/data is read-in
reg [7:0] read_data;             // register in which opcode/data is read-in
reg [7:0] read_dummy;            // register in which dont care data is stored
reg [7:0] read_dummy;            // register in which dont care data is stored
reg [3:0] stat_reg_temp; // WSR temp value for global protec/unprotect
reg [3:0] stat_reg_temp; // WSR temp value for global protec/unprotect
reg [7:0] pp_address;            // specifies the page address for BP/PP
reg [7:0] pp_address;            // specifies the page address for BP/PP
reg SO_reg;                     // Signal out reg
reg SO_reg;                     // Signal out reg
reg SO_on;                      // Signal out enable signal
reg SO_on;                      // Signal out enable signal
reg SPRL;                       // Sector Protection Register Locked
reg SPRL;                       // Sector Protection Register Locked
reg SPM;                        // Sequential Program Mode Status
reg SPM;                        // Sequential Program Mode Status
reg EPE;                        // Erase/Program Error
reg EPE;                        // Erase/Program Error
reg WPP;                        // Write Protection Pin Status
reg WPP;                        // Write Protection Pin Status
reg [1:0] SWP;                   // Software Protection Status
reg [1:0] SWP;                   // Software Protection Status
reg WEL;                        // Write Enable Latch Status
reg WEL;                        // Write Enable Latch Status
reg RDYnBSY;                    // Ready/Busy Status
reg RDYnBSY;                    // Ready/Busy Status
reg lock;                       // to lock SPRL bit of status reg
reg lock;                       // to lock SPRL bit of status reg
reg unlock;                     // to unlock SPRL bit of status reg
reg unlock;                     // to unlock SPRL bit of status reg
reg global_protect;             // global protection
reg global_protect;             // global protection
reg global_unprotect;           // global unprotect
reg global_unprotect;           // global unprotect
reg protect;                    // to protect Sector reg bits
reg protect;                    // to protect Sector reg bits
reg unprotect;                  // to unprotect Sector reg bits
reg unprotect;                  // to unprotect Sector reg bits
reg protected;                  // check protection status of sector reg
reg protected;                  // check protection status of sector reg
reg SPRL_val;                   // WSR temp value for SPRL
reg SPRL_val;                   // WSR temp value for SPRL
integer i,j,k,l;                // integers for accessing memory
integer i,j,k,l;                // integers for accessing memory
integer pp;                     // specifies no of bytes need to write in memory
integer pp;                     // specifies no of bytes need to write in memory
integer pp_j;                   // holds no of bytes received for page program
integer pp_j;                   // holds no of bytes received for page program
integer pp_i;                   // holds no of bits received for each byte in page program
integer pp_i;                   // holds no of bits received for each byte in page program
integer dummy;                  // counter for receiving 8 dummy bit from SO
integer dummy;                  // counter for receiving 8 dummy bit from SO
integer delay;                  // waits for Chip Erase to complete
integer delay;                  // waits for Chip Erase to complete
 
 
real tperiod;                   // time period - on period
real tperiod;                   // time period - on period
real tperiod1;                  // time period - off period
real tperiod1;                  // time period - off period
reg freq_error;                 // frequency limit exceeds indication
reg freq_error;                 // frequency limit exceeds indication
 
 
 
 
// ****************** Initial Block **************** //
// ****************** Initial Block **************** //
`ifdef def_321
`ifdef def_321
        assign HOLDB = 1'b1;
        assign HOLDB = 1'b1;
`endif
`endif
 
 
initial
initial
begin
begin
 
 
// Memory Initialization
// Memory Initialization
//To pre-load Memory (in Hex format), use parameter MEMORY_FILE = <filename>, 
//To pre-load Memory (in Hex format), use parameter MEMORY_FILE = <filename>, 
//where <filename> is the name of the pre-load file. 
//where <filename> is the name of the pre-load file. 
//If PRELOAD = 0, the Memory is initialized to Erased state (all data = FF).
//If PRELOAD = 0, the Memory is initialized to Erased state (all data = FF).
//If PRELOAD = 1, the Memory is initialized to PreLoad state (data in pre-load file).
//If PRELOAD = 1, the Memory is initialized to PreLoad state (data in pre-load file).
 
 
        for(i=0; i<BLOCK64; i=i+1)                       // Block64K access
        for(i=0; i<BLOCK64; i=i+1)                       // Block64K access
        begin
        begin
            for(j=0; j<2; j=j+1)                 // Block32K access
            for(j=0; j<2; j=j+1)                 // Block32K access
            begin
            begin
                for(k=0; k<8; k=k+1)                     // Block4K access
                for(k=0; k<8; k=k+1)                     // Block4K access
                begin
                begin
                    for(l=0; l < 12'hfff; l=l+1)
                    for(l=0; l < 12'hfff; l=l+1)
                    begin
                    begin
                        memory[l] = 8'b11111111;
                        memory[l] = 8'b11111111;
                    end
                    end
                end
                end
            end
            end
        end
        end
 
 
// Memory Initialization for preloading condition
// Memory Initialization for preloading condition
if(PRELOAD==1)
if(PRELOAD==1)
begin
begin
        $readmemh (MEMORY_FILE, memory);
        $readmemh (MEMORY_FILE, memory);
end
end
 
 
 
 
// Status register initialization
// Status register initialization
SPRL = 1'b0;
SPRL = 1'b0;
SPM = 1'b0;
SPM = 1'b0;
EPE = 1'b0;
EPE = 1'b0;
WPP = 1'b0;
WPP = 1'b0;
SWP = 2'b11;
SWP = 2'b11;
WEL = 1'b0;
WEL = 1'b0;
RDYnBSY = 1'b0; // device is ready
RDYnBSY = 1'b0; // device is ready
status_reg[7]   = SPRL;
status_reg[7]   = SPRL;
status_reg[6]   = SPM;
status_reg[6]   = SPM;
status_reg[5]   = EPE; // reserved
status_reg[5]   = EPE; // reserved
status_reg[4]   = WPP;
status_reg[4]   = WPP;
status_reg[3:2] = SWP;
status_reg[3:2] = SWP;
status_reg[1]   = WEL;
status_reg[1]   = WEL;
status_reg[0]    = RDYnBSY;
status_reg[0]    = RDYnBSY;
//status_reg = {1'b1,1'b0,1'b0,1'b0,2'b11,2'b0};
//status_reg = {1'b1,1'b0,1'b0,1'b0,2'b11,2'b0};
 
 
sector_reg = 64'hFFFFFFFFFFFFFFFF;
sector_reg = 64'hFFFFFFFFFFFFFFFF;
 
 
// Stand-by mode initialization
// Stand-by mode initialization
current_address  = 24'b0;
current_address  = 24'b0;
data_in          = 8'b0;
data_in          = 8'b0;
stat_reg_temp    = 4'b0;
stat_reg_temp    = 4'b0;
deep_power_down  = 1'b0;
deep_power_down  = 1'b0;
status_read      = 1'b0;
status_read      = 1'b0;
rd_dummy         = 1'b0;
rd_dummy         = 1'b0;
seq_byte_start   = 1'b0;
seq_byte_start   = 1'b0;
SO_on            = 1'b0;
SO_on            = 1'b0;
global_protect   = 1'b0;
global_protect   = 1'b0;
global_unprotect = 1'b0;
global_unprotect = 1'b0;
SPRL_val         = 1'b0;
SPRL_val         = 1'b0;
lock             = 1'b0;
lock             = 1'b0;
unlock           = 1'b0;
unlock           = 1'b0;
dummy            = 0;
dummy            = 0;
 
 
deep_power_down = 1'b0;
deep_power_down = 1'b0;
status_read     = 1'b0;
status_read     = 1'b0;
erasing_block4  = 1'b0;
erasing_block4  = 1'b0;
erasing_block32 = 1'b0;
erasing_block32 = 1'b0;
erasing_block64 = 1'b0;
erasing_block64 = 1'b0;
erasing_chip    = 1'b0;
erasing_chip    = 1'b0;
rd_dummy        = 1'b0;
rd_dummy        = 1'b0;
byte_prog       = 1'b0;
byte_prog       = 1'b0;
seq_byte_prog   = 1'b0;
seq_byte_prog   = 1'b0;
seq_byte_start  = 1'b0;
seq_byte_start  = 1'b0;
 
 
end // end of initial
end // end of initial
 
 
// ********************** Drive SO ********************* //
// ********************** Drive SO ********************* //
bufif1 (SO, SO_reg, SO_on); //SO will be driven only if SO_on is High
bufif1 (SO, SO_reg, SO_on); //SO will be driven only if SO_on is High
 
 
// global protection and unprotection on sectors
// global protection and unprotection on sectors
always@(global_protect or global_unprotect)
always@(global_protect or global_unprotect)
begin
begin
        if(global_protect==1'b1)
        if(global_protect==1'b1)
                sector_reg = 64'hFFFFFFFFFFFFFFFF;
                sector_reg = 64'hFFFFFFFFFFFFFFFF;
        else if(global_unprotect==1'b1)
        else if(global_unprotect==1'b1)
                sector_reg[SECTOR-1:0] = 'b0;
                sector_reg[SECTOR-1:0] = 'b0;
end
end
 
 
// ********************* Status register ********************* //
// ********************* Status register ********************* //
always @(WPB or lock or unlock) //SPRL bit locking & Unlocking
always @(WPB or lock or unlock) //SPRL bit locking & Unlocking
begin
begin
        if(WPB==1'b1 && SPRL==1'b0 && lock==1'b1)
        if(WPB==1'b1 && SPRL==1'b0 && lock==1'b1)
                SPRL = 1'b1;
                SPRL = 1'b1;
        else if(WPB==1'b1 && SPRL==1'b1 && unlock==1'b1)
        else if(WPB==1'b1 && SPRL==1'b1 && unlock==1'b1)
                SPRL = 1'b0; // S/W Locked
                SPRL = 1'b0; // S/W Locked
        else if(WPB==1'b0 && SPRL==1'b0 && lock==1'b1)
        else if(WPB==1'b0 && SPRL==1'b0 && lock==1'b1)
                SPRL = 1'b1;
                SPRL = 1'b1;
        else if(WPB==1'b0 && SPRL==1'b1 && unlock==1'b1)
        else if(WPB==1'b0 && SPRL==1'b1 && unlock==1'b1)
                SPRL = 1'b1; // H/W Locked
                SPRL = 1'b1; // H/W Locked
end
end
 
 
always @(WPB)                   // Write Protect (WP!) Pin Status
always @(WPB)                   // Write Protect (WP!) Pin Status
begin
begin
        WPP = WPB;
        WPP = WPB;
end
end
 
 
always @(sector_reg)            // Software Protection Status
always @(sector_reg)            // Software Protection Status
begin
begin
        if(sector_reg[SECTOR-1:0]=='d0)
        if(sector_reg[SECTOR-1:0]=='d0)
                SWP = 2'b00;
                SWP = 2'b00;
        else if((|sector_reg==1'b1) && (&sector_reg==1'b0))
        else if((|sector_reg==1'b1) && (&sector_reg==1'b0))
                SWP = 2'b01;
                SWP = 2'b01;
        else if(&sector_reg==1'b1)
        else if(&sector_reg==1'b1)
                SWP = 2'b11;
                SWP = 2'b11;
end
end
 
 
always @(SPRL or SPM or EPE or WPP or SWP or WEL or RDYnBSY)
always @(SPRL or SPM or EPE or WPP or SWP or WEL or RDYnBSY)
begin
begin
        status_reg = {SPRL,SPM,EPE,WPP,SWP,WEL,RDYnBSY};
        status_reg = {SPRL,SPM,EPE,WPP,SWP,WEL,RDYnBSY};
end
end
 
 
// ******* to receive opcode and to switch to respective blocks ******* //
// ******* to receive opcode and to switch to respective blocks ******* //
always @(negedge CSB)  // the device will now become active
always @(negedge CSB)  // the device will now become active
begin : get_opcode_
begin : get_opcode_
 
 
        //@(negedge SCK);
        //@(negedge SCK);
        get_opcode;  // get opcode here
        get_opcode;  // get opcode here
 
 
        if (deep_power_down == 1'b1) // Can be only after background has been enabled
        if (deep_power_down == 1'b1) // Can be only after background has been enabled
                case (read_data)
                case (read_data)
                        8'hAB:  // Resume from Deep Power-down
                        8'hAB:  // Resume from Deep Power-down
                        begin
                        begin
                                $display("Opcode(%h) for Resume from Deep Power-down received",read_data);
                                $display("Opcode(%h) for Resume from Deep Power-down received",read_data);
                                -> RDPD;
                                -> RDPD;
                        end
                        end
                        default :
                        default :
                        begin
                        begin
                                $display ("Opcode %h is not allowed: device in Deep Power-down", read_data);
                                $display ("Opcode %h is not allowed: device in Deep Power-down", read_data);
                                disable get_opcode_;
                                disable get_opcode_;
                        end
                        end
                endcase
                endcase
        else
        else
        begin
        begin
                case (read_data)        // based on opcode, trigger an action
                case (read_data)        // based on opcode, trigger an action
                8'h0B : // Read Array
                8'h0B : // Read Array
                        begin
                        begin
                                $display("Opcode(%h) for Read Array received",read_data);
                                $display("Opcode(%h) for Read Array received",read_data);
                                rd_dummy = 1'b1;
                                rd_dummy = 1'b1;
                                -> RA;
                                -> RA;
                        end
                        end
                8'h03 : // Read Array (low freq)
                8'h03 : // Read Array (low freq)
                  begin
                  begin
                     // Comment out this to clean up sim output
                     // Comment out this to clean up sim output
                     //$display("Opcode(%h) for Read Array (Low Freq) received",read_data);
                     //$display("Opcode(%h) for Read Array (Low Freq) received",read_data);
                     rd_dummy = 1'b0;
                     rd_dummy = 1'b0;
                     -> RA;
                     -> RA;
                        end
                        end
                8'h20 : // Block erase 4 KB
                8'h20 : // Block erase 4 KB
                        begin
                        begin
                                $display("Opcode(%h) for 4 KB Block erase received",read_data);
                                $display("Opcode(%h) for 4 KB Block erase received",read_data);
                                -> BE4;
                                -> BE4;
                        end
                        end
                8'h52 : // Block erase 32 KB
                8'h52 : // Block erase 32 KB
                        begin
                        begin
                                $display("Opcode(%h) for 32 KB Block erase received",read_data);
                                $display("Opcode(%h) for 32 KB Block erase received",read_data);
                                -> BE32;
                                -> BE32;
                        end
                        end
                8'hD8 : // Block erase 64 KB
                8'hD8 : // Block erase 64 KB
                        begin
                        begin
                                $display("Opcode(%h) for 64 KB Block erase received",read_data);
                                $display("Opcode(%h) for 64 KB Block erase received",read_data);
                                -> BE64;
                                -> BE64;
                        end
                        end
                8'h60 : // Chip erase
                8'h60 : // Chip erase
                        begin
                        begin
                                $display("Opcode(%h) for Chip erase received",read_data);
                                $display("Opcode(%h) for Chip erase received",read_data);
                                -> CE;
                                -> CE;
                        end
                        end
                8'hC7 : // Chip erase
                8'hC7 : // Chip erase
                        begin
                        begin
                                $display("Opcode(%h) for Chip erase received",read_data);
                                $display("Opcode(%h) for Chip erase received",read_data);
                                -> CE;
                                -> CE;
                        end
                        end
                8'h02 : // Byte Program
                8'h02 : // Byte Program
                        begin
                        begin
                                $display("Opcode(%h) Byte Program received",read_data);
                                $display("Opcode(%h) Byte Program received",read_data);
                                -> BP;
                                -> BP;
                        end
                        end
                8'hAD : // Sequential Byte Program Mode Erase
                8'hAD : // Sequential Byte Program Mode Erase
                        begin
                        begin
                                if(DEVICE=="AT25DF041A" || DEVICE=="AT26DF081A" || DEVICE=="AT26DF161A")
                                if(DEVICE=="AT25DF041A" || DEVICE=="AT26DF081A" || DEVICE=="AT26DF161A")
                                begin
                                begin
                                        $display("Opcode(%h) Sequential Byte Program received",read_data);
                                        $display("Opcode(%h) Sequential Byte Program received",read_data);
                                        -> SBP;
                                        -> SBP;
                                end
                                end
                                else if(DEVICE=="AT26DF321")
                                else if(DEVICE=="AT26DF321")
                                        $display("Sequential Byte Program is not supported for device %s",DEVICE);
                                        $display("Sequential Byte Program is not supported for device %s",DEVICE);
                        end
                        end
                8'hAF : // Sequential Byte Program Mode
                8'hAF : // Sequential Byte Program Mode
                        begin
                        begin
                                if(DEVICE=="AT25DF041A" || DEVICE=="AT26DF081A" || DEVICE=="AT26DF161A")
                                if(DEVICE=="AT25DF041A" || DEVICE=="AT26DF081A" || DEVICE=="AT26DF161A")
                                begin
                                begin
                                        $display("Opcode(%h) Sequential Byte Program received",read_data);
                                        $display("Opcode(%h) Sequential Byte Program received",read_data);
                                        -> SBP;
                                        -> SBP;
                                end
                                end
                                else if(DEVICE=="AT26DF321")
                                else if(DEVICE=="AT26DF321")
                                        $display("Sequential Byte Program is not supported for device %s",DEVICE);
                                        $display("Sequential Byte Program is not supported for device %s",DEVICE);
                        end
                        end
                8'h06 : // Write Enable
                8'h06 : // Write Enable
                        begin
                        begin
                                $display("Opcode(%h) for Write Enable received",read_data);
                                $display("Opcode(%h) for Write Enable received",read_data);
                                -> WE;
                                -> WE;
                        end
                        end
                8'h04 : // Write Disable
                8'h04 : // Write Disable
                        begin
                        begin
                                $display("Opcode(%h) for Write Disable received",read_data);
                                $display("Opcode(%h) for Write Disable received",read_data);
                                -> WD;
                                -> WD;
                        end
                        end
                8'h36 : // Protect Sector
                8'h36 : // Protect Sector
                        begin
                        begin
                                $display("Opcode(%h) for Protect Sector received",read_data);
                                $display("Opcode(%h) for Protect Sector received",read_data);
                                -> PS;
                                -> PS;
                        end
                        end
                8'h39 : // Unprotect Sector
                8'h39 : // Unprotect Sector
                        begin
                        begin
                                $display("Opcode(%h) for Unprotect Sector received",read_data);
                                $display("Opcode(%h) for Unprotect Sector received",read_data);
                                -> UPS;
                                -> UPS;
                        end
                        end
                8'h3C : // Read Sector Protection Register
                8'h3C : // Read Sector Protection Register
                        begin
                        begin
                                $display("Opcode(%h) for Read Sector Protection Register received",read_data);
                                $display("Opcode(%h) for Read Sector Protection Register received",read_data);
                                -> RSPR;
                                -> RSPR;
                        end
                        end
                8'h05 : // Read Status Register
                8'h05 : // Read Status Register
                        begin
                        begin
                                $display("Opcode(%h) for Status Register Read received",read_data);
                                $display("Opcode(%h) for Status Register Read received",read_data);
                                -> RSR;
                                -> RSR;
                        end
                        end
                8'h01 : // Write Status Register
                8'h01 : // Write Status Register
                        begin
                        begin
                                $display("Opcode(%h) for Write Status Register received",read_data);
                                $display("Opcode(%h) for Write Status Register received",read_data);
                                -> WSR;
                                -> WSR;
                        end
                        end
                8'h9F : // Read Manufacturer and Device ID
                8'h9F : // Read Manufacturer and Device ID
                        begin
                        begin
                                $display("Opcode(%h) for Read Manufacturer and Device ID received",read_data);
                                $display("Opcode(%h) for Read Manufacturer and Device ID received",read_data);
                                -> MIR;
                                -> MIR;
                        end
                        end
                8'hB9 : // Deep Power-down
                8'hB9 : // Deep Power-down
                        begin
                        begin
                                $display("Opcode(%h) for Deep Power-down received",read_data);
                                $display("Opcode(%h) for Deep Power-down received",read_data);
                                -> EDPD;
                                -> EDPD;
                        end
                        end
                default :
                default :
                begin
                begin
                        $display ("Unrecognized opcode %h", read_data);
                        $display ("Unrecognized opcode %h", read_data);
                        disable get_opcode_;
                        disable get_opcode_;
                end
                end
                endcase
                endcase
        end
        end
end
end
 
 
// *********************** TASKS / FUNCTIONS ************************** //
// *********************** TASKS / FUNCTIONS ************************** //
 
 
// get_opcode is a task to get 8 bits of opcode.
// get_opcode is a task to get 8 bits of opcode.
// It obtains 8 bits of data obtained on SI
// It obtains 8 bits of data obtained on SI
task get_opcode;
task get_opcode;
 
 
integer i;
integer i;
begin
begin
        for (i=8; i>0; i = i-1)
        for (i=8; i>0; i = i-1)
        begin
        begin
                @(posedge SCK);
                @(posedge SCK);
                read_data[i-1] = SI;
                read_data[i-1] = SI;
                if(HOLDB==1'b0)
                if(HOLDB==1'b0)
                        wait (HOLDB);
                        wait (HOLDB);
        end
        end
        i=7;
        i=7;
end
end
endtask
endtask
 
 
// get_data is a task to get 8 bits of data. This data could be an address,
// get_data is a task to get 8 bits of data. This data could be an address,
// data or anything. It just obtains 8 bits of data obtained on SI
// data or anything. It just obtains 8 bits of data obtained on SI
task get_data;
task get_data;
 
 
integer i;
integer i;
begin
begin
        for (i=7; i>=0; i = i-1)
        for (i=7; i>=0; i = i-1)
        begin
        begin
                @(posedge SCK);
                @(posedge SCK);
                read_data[i] = SI;
                read_data[i] = SI;
                if(HOLDB==1'b0)
                if(HOLDB==1'b0)
                        wait (HOLDB);
                        wait (HOLDB);
        end
        end
        i=7;
        i=7;
end
end
endtask
endtask
 
 
// task read_out_array is to read from main Memory
// task read_out_array is to read from main Memory
task read_out_array ;
task read_out_array ;
input [23:0] read_addr;
input [23:0] read_addr;
integer i;
integer i;
 
 
begin
begin
        temp_data = memory [read_addr];
        temp_data = memory [read_addr];
        i = 7;
        i = 7;
        while (CSB == 1'b0) // continue transmitting, while, CSB is Low
        while (CSB == 1'b0) // continue transmitting, while, CSB is Low
        begin
        begin
                @(negedge SCK);
                @(negedge SCK);
                SO_reg = 1'bx;
                SO_reg = 1'bx;
                if(HOLDB==1'b0)
                if(HOLDB==1'b0)
                begin
                begin
                        #tHLQZ SO_on = 1'b0;
                        #tHLQZ SO_on = 1'b0;
                        wait (HOLDB);
                        wait (HOLDB);
                        #tHHQX;
                        #tHHQX;
                end
                end
                #tV SO_reg = temp_data[i];
                #tV SO_reg = temp_data[i];
                SO_on = 1'b1;
                SO_on = 1'b1;
                if (i == 0)
                if (i == 0)
                begin
                begin
                        //$display ("Data: %h read from memory location %h",temp_data,read_addr);
                     if (VERBOSE)
 
                        $display ("Data: %h read from memory location %h",temp_data,read_addr);
                        read_addr = read_addr + 1; // next byte
                        read_addr = read_addr + 1; // next byte
                        i = 7;
                        i = 7;
                        if (read_addr >= (MEMSIZE-1))
                        if (read_addr >= (MEMSIZE-1))
                                read_addr = 0; // Note that rollover occurs at end of memory,
                                read_addr = 0; // Note that rollover occurs at end of memory,
                        temp_data = memory [read_addr];
                        temp_data = memory [read_addr];
                end
                end
                else
                else
                        i = i - 1; // next bit
                        i = i - 1; // next bit
        end // reading over, because CSB has gone high
        end // reading over, because CSB has gone high
end
end
endtask
endtask
 
 
// Sector protection
// Sector protection
task protect_sector;
task protect_sector;
input [23:0] protect_address;
input [23:0] protect_address;
 
 
begin
begin
        $display("Entered Protect Sector task");
        $display("Entered Protect Sector task");
 
 
    if (DEVICE == "AT26DF321" && protect==1'b1)
    if (DEVICE == "AT26DF321" && protect==1'b1)
    begin
    begin
        if(protect_address[23:16]==8'b00111111)
        if(protect_address[23:16]==8'b00111111)
                sector_reg[63] = 1'b1;
                sector_reg[63] = 1'b1;
        else if(protect_address[23:16]==8'b00111110)
        else if(protect_address[23:16]==8'b00111110)
                sector_reg[62] = 1'b1;
                sector_reg[62] = 1'b1;
        else if(protect_address[23:16]==8'b00111101)
        else if(protect_address[23:16]==8'b00111101)
                sector_reg[61] = 1'b1;
                sector_reg[61] = 1'b1;
        else if(protect_address[23:16]==8'b00111100)
        else if(protect_address[23:16]==8'b00111100)
                sector_reg[60] = 1'b1;
                sector_reg[60] = 1'b1;
        else if(protect_address[23:16]==8'b00111011)
        else if(protect_address[23:16]==8'b00111011)
                sector_reg[59] = 1'b1;
                sector_reg[59] = 1'b1;
        else if(protect_address[23:16]==8'b00111010)
        else if(protect_address[23:16]==8'b00111010)
                sector_reg[58] = 1'b1;
                sector_reg[58] = 1'b1;
        else if(protect_address[23:16]==8'b00111001)
        else if(protect_address[23:16]==8'b00111001)
                sector_reg[57] = 1'b1;
                sector_reg[57] = 1'b1;
        else if(protect_address[23:16]==8'b00111000)
        else if(protect_address[23:16]==8'b00111000)
                sector_reg[56] = 1'b1;
                sector_reg[56] = 1'b1;
        else if(protect_address[23:16]==8'b00110111)
        else if(protect_address[23:16]==8'b00110111)
                sector_reg[55] = 1'b1;
                sector_reg[55] = 1'b1;
        else if(protect_address[23:16]==8'b00110110)
        else if(protect_address[23:16]==8'b00110110)
                sector_reg[54] = 1'b1;
                sector_reg[54] = 1'b1;
        else if(protect_address[23:16]==8'b00110101)
        else if(protect_address[23:16]==8'b00110101)
                sector_reg[53] = 1'b1;
                sector_reg[53] = 1'b1;
        else if(protect_address[23:16]==8'b00110100)
        else if(protect_address[23:16]==8'b00110100)
                sector_reg[52] = 1'b1;
                sector_reg[52] = 1'b1;
        else if(protect_address[23:16]==8'b00110011)
        else if(protect_address[23:16]==8'b00110011)
                sector_reg[51] = 1'b1;
                sector_reg[51] = 1'b1;
        else if(protect_address[23:16]==8'b00110010)
        else if(protect_address[23:16]==8'b00110010)
                sector_reg[50] = 1'b1;
                sector_reg[50] = 1'b1;
        else if(protect_address[23:16]==8'b00110001)
        else if(protect_address[23:16]==8'b00110001)
                sector_reg[49] = 1'b1;
                sector_reg[49] = 1'b1;
        else if(protect_address[23:16]==8'b00110000)
        else if(protect_address[23:16]==8'b00110000)
                sector_reg[48] = 1'b1;
                sector_reg[48] = 1'b1;
        else if(protect_address[23:16]==8'b00101111)
        else if(protect_address[23:16]==8'b00101111)
                sector_reg[47] = 1'b1;
                sector_reg[47] = 1'b1;
        else if(protect_address[23:16]==8'b00101110)
        else if(protect_address[23:16]==8'b00101110)
                sector_reg[46] = 1'b1;
                sector_reg[46] = 1'b1;
        else if(protect_address[23:16]==8'b00101101)
        else if(protect_address[23:16]==8'b00101101)
                sector_reg[45] = 1'b1;
                sector_reg[45] = 1'b1;
        else if(protect_address[23:16]==8'b00101100)
        else if(protect_address[23:16]==8'b00101100)
                sector_reg[44] = 1'b1;
                sector_reg[44] = 1'b1;
        else if(protect_address[23:16]==8'b00101011)
        else if(protect_address[23:16]==8'b00101011)
                sector_reg[43] = 1'b1;
                sector_reg[43] = 1'b1;
        else if(protect_address[23:16]==8'b00101010)
        else if(protect_address[23:16]==8'b00101010)
                sector_reg[42] = 1'b1;
                sector_reg[42] = 1'b1;
        else if(protect_address[23:16]==8'b00101001)
        else if(protect_address[23:16]==8'b00101001)
                sector_reg[41] = 1'b1;
                sector_reg[41] = 1'b1;
        else if(protect_address[23:16]==8'b00101000)
        else if(protect_address[23:16]==8'b00101000)
                sector_reg[40] = 1'b1;
                sector_reg[40] = 1'b1;
        else if(protect_address[23:16]==8'b00100111)
        else if(protect_address[23:16]==8'b00100111)
                sector_reg[39] = 1'b1;
                sector_reg[39] = 1'b1;
        else if(protect_address[23:16]==8'b00100110)
        else if(protect_address[23:16]==8'b00100110)
                sector_reg[38] = 1'b1;
                sector_reg[38] = 1'b1;
        else if(protect_address[23:16]==8'b00100101)
        else if(protect_address[23:16]==8'b00100101)
                sector_reg[37] = 1'b1;
                sector_reg[37] = 1'b1;
        else if(protect_address[23:16]==8'b00100100)
        else if(protect_address[23:16]==8'b00100100)
                sector_reg[36] = 1'b1;
                sector_reg[36] = 1'b1;
        else if(protect_address[23:16]==8'b00100011)
        else if(protect_address[23:16]==8'b00100011)
                sector_reg[35] = 1'b1;
                sector_reg[35] = 1'b1;
        else if(protect_address[23:16]==8'b00100010)
        else if(protect_address[23:16]==8'b00100010)
                sector_reg[34] = 1'b1;
                sector_reg[34] = 1'b1;
        else if(protect_address[23:16]==8'b00100001)
        else if(protect_address[23:16]==8'b00100001)
                sector_reg[33] = 1'b1;
                sector_reg[33] = 1'b1;
        else if(protect_address[23:16]==8'b00100000)
        else if(protect_address[23:16]==8'b00100000)
                sector_reg[32] = 1'b1;
                sector_reg[32] = 1'b1;
    end
    end
    if ((DEVICE == "AT26DF161A" || DEVICE == "AT26DF321") && protect==1'b1)
    if ((DEVICE == "AT26DF161A" || DEVICE == "AT26DF321") && protect==1'b1)
    begin
    begin
        if(protect_address[23:16]==8'b00011111)
        if(protect_address[23:16]==8'b00011111)
                sector_reg[31] = 1'b1;
                sector_reg[31] = 1'b1;
        else if(protect_address[23:16]==8'b00011110)
        else if(protect_address[23:16]==8'b00011110)
                sector_reg[30] = 1'b1;
                sector_reg[30] = 1'b1;
        else if(protect_address[23:16]==8'b00011101)
        else if(protect_address[23:16]==8'b00011101)
                sector_reg[29] = 1'b1;
                sector_reg[29] = 1'b1;
        else if(protect_address[23:16]==8'b00011100)
        else if(protect_address[23:16]==8'b00011100)
                sector_reg[28] = 1'b1;
                sector_reg[28] = 1'b1;
        else if(protect_address[23:16]==8'b00011011)
        else if(protect_address[23:16]==8'b00011011)
                sector_reg[27] = 1'b1;
                sector_reg[27] = 1'b1;
        else if(protect_address[23:16]==8'b00011010)
        else if(protect_address[23:16]==8'b00011010)
                sector_reg[26] = 1'b1;
                sector_reg[26] = 1'b1;
        else if(protect_address[23:16]==8'b00011001)
        else if(protect_address[23:16]==8'b00011001)
                sector_reg[25] = 1'b1;
                sector_reg[25] = 1'b1;
        else if(protect_address[23:16]==8'b00011000)
        else if(protect_address[23:16]==8'b00011000)
                sector_reg[24] = 1'b1;
                sector_reg[24] = 1'b1;
        else if(protect_address[23:16]==8'b00010111)
        else if(protect_address[23:16]==8'b00010111)
                sector_reg[23] = 1'b1;
                sector_reg[23] = 1'b1;
        else if(protect_address[23:16]==8'b00010110)
        else if(protect_address[23:16]==8'b00010110)
                sector_reg[22] = 1'b1;
                sector_reg[22] = 1'b1;
        else if(protect_address[23:16]==8'b00010101)
        else if(protect_address[23:16]==8'b00010101)
                sector_reg[21] = 1'b1;
                sector_reg[21] = 1'b1;
        else if(protect_address[23:16]==8'b00010100)
        else if(protect_address[23:16]==8'b00010100)
                sector_reg[20] = 1'b1;
                sector_reg[20] = 1'b1;
        else if(protect_address[23:16]==8'b00010011)
        else if(protect_address[23:16]==8'b00010011)
                sector_reg[19] = 1'b1;
                sector_reg[19] = 1'b1;
        else if(protect_address[23:16]==8'b00010010)
        else if(protect_address[23:16]==8'b00010010)
                sector_reg[18] = 1'b1;
                sector_reg[18] = 1'b1;
        else if(protect_address[23:16]==8'b00010001)
        else if(protect_address[23:16]==8'b00010001)
                sector_reg[17] = 1'b1;
                sector_reg[17] = 1'b1;
        else if(protect_address[23:16]==8'b00010000)
        else if(protect_address[23:16]==8'b00010000)
                sector_reg[16] = 1'b1;
                sector_reg[16] = 1'b1;
    end
    end
 
 
    if (DEVICE == "AT26DF081A" && protect==1'b1) // un-uniform sectors of AT26DF081A
    if (DEVICE == "AT26DF081A" && protect==1'b1) // un-uniform sectors of AT26DF081A
    begin
    begin
        if(protect_address[23:14]==10'b00001111_00)
        if(protect_address[23:14]==10'b00001111_00)
                sector_reg[15] = 1'b1;
                sector_reg[15] = 1'b1;
        else if(protect_address[23:13]==11'b00001111_010)
        else if(protect_address[23:13]==11'b00001111_010)
                sector_reg[16] = 1'b1;
                sector_reg[16] = 1'b1;
        else if(protect_address[23:13]==11'b00001111_011)
        else if(protect_address[23:13]==11'b00001111_011)
                sector_reg[17] = 1'b1;
                sector_reg[17] = 1'b1;
        else if(protect_address[23:17]==9'b00001111_1)
        else if(protect_address[23:17]==9'b00001111_1)
                sector_reg[18] = 1'b1;
                sector_reg[18] = 1'b1;
    end
    end
    else if ((DEVICE == "AT26DF161A" || DEVICE == "AT26DF321") && protect==1'b1)
    else if ((DEVICE == "AT26DF161A" || DEVICE == "AT26DF321") && protect==1'b1)
    begin
    begin
        if(protect_address[23:16]==8'b00001111)
        if(protect_address[23:16]==8'b00001111)
                sector_reg[15] = 1'b1;
                sector_reg[15] = 1'b1;
    end
    end
 
 
    if ((DEVICE == "AT26DF081A" || DEVICE == "AT26DF161A" || DEVICE == "AT26DF321") && protect==1'b1)
    if ((DEVICE == "AT26DF081A" || DEVICE == "AT26DF161A" || DEVICE == "AT26DF321") && protect==1'b1)
    begin
    begin
//      if(protect_address[23:16]==8'b00001111)
//      if(protect_address[23:16]==8'b00001111)
//              sector_reg[15] = 1'b1;
//              sector_reg[15] = 1'b1;
        if(protect_address[23:16]==8'b00001110)
        if(protect_address[23:16]==8'b00001110)
                sector_reg[14] = 1'b1;
                sector_reg[14] = 1'b1;
        else if(protect_address[23:16]==8'b00001101)
        else if(protect_address[23:16]==8'b00001101)
                sector_reg[13] = 1'b1;
                sector_reg[13] = 1'b1;
        else if(protect_address[23:16]==8'b00001100)
        else if(protect_address[23:16]==8'b00001100)
                sector_reg[12] = 1'b1;
                sector_reg[12] = 1'b1;
        else if(protect_address[23:16]==8'b00001011)
        else if(protect_address[23:16]==8'b00001011)
                sector_reg[11] = 1'b1;
                sector_reg[11] = 1'b1;
        else if(protect_address[23:16]==8'b00001010)
        else if(protect_address[23:16]==8'b00001010)
                sector_reg[10] = 1'b1;
                sector_reg[10] = 1'b1;
        else if(protect_address[23:16]==8'b00001001)
        else if(protect_address[23:16]==8'b00001001)
                sector_reg[9] = 1'b1;
                sector_reg[9] = 1'b1;
        else if(protect_address[23:16]==8'b00001000)
        else if(protect_address[23:16]==8'b00001000)
                sector_reg[8] = 1'b1;
                sector_reg[8] = 1'b1;
    end
    end
 
 
    if (DEVICE == "AT25DF041A" && protect==1'b1) // un-uniform sectors of AT25DF041A
    if (DEVICE == "AT25DF041A" && protect==1'b1) // un-uniform sectors of AT25DF041A
    begin
    begin
        if(protect_address[23:15]==9'b00000111_0)
        if(protect_address[23:15]==9'b00000111_0)
                sector_reg[7] = 1'b1;
                sector_reg[7] = 1'b1;
        else if(protect_address[23:13]==11'b00000111_100)
        else if(protect_address[23:13]==11'b00000111_100)
                sector_reg[8] = 1'b1;
                sector_reg[8] = 1'b1;
        else if(protect_address[23:13]==11'b00000111_101)
        else if(protect_address[23:13]==11'b00000111_101)
                sector_reg[9] = 1'b1;
                sector_reg[9] = 1'b1;
        else if(protect_address[23:14]==10'b00000111_11)
        else if(protect_address[23:14]==10'b00000111_11)
                sector_reg[10] = 1'b1;
                sector_reg[10] = 1'b1;
    end
    end
    else if ((DEVICE == "AT26DF081A" || DEVICE == "AT26DF161A" || DEVICE == "AT26DF321") && protect==1'b1)
    else if ((DEVICE == "AT26DF081A" || DEVICE == "AT26DF161A" || DEVICE == "AT26DF321") && protect==1'b1)
        if(protect_address[23:16]==8'b00000111)
        if(protect_address[23:16]==8'b00000111)
                sector_reg[7] = 1'b1;
                sector_reg[7] = 1'b1;
 
 
    if ((DEVICE == "AT25DF041A" || DEVICE == "AT26DF081A"
    if ((DEVICE == "AT25DF041A" || DEVICE == "AT26DF081A"
        || DEVICE == "AT26DF161A" || DEVICE == "AT26DF321") && protect==1'b1)
        || DEVICE == "AT26DF161A" || DEVICE == "AT26DF321") && protect==1'b1)
    begin
    begin
//      if(protect_address[23:16]==8'b00000111)
//      if(protect_address[23:16]==8'b00000111)
//              sector_reg[7] = 1'b1;
//              sector_reg[7] = 1'b1;
        if(protect_address[23:16]==8'b00000110)
        if(protect_address[23:16]==8'b00000110)
                sector_reg[6] = 1'b1;
                sector_reg[6] = 1'b1;
        else if(protect_address[23:16]==8'b00000101)
        else if(protect_address[23:16]==8'b00000101)
                sector_reg[5] = 1'b1;
                sector_reg[5] = 1'b1;
        else if(protect_address[23:16]==8'b00000100)
        else if(protect_address[23:16]==8'b00000100)
                sector_reg[4] = 1'b1;
                sector_reg[4] = 1'b1;
        else if(protect_address[23:16]==8'b00000011)
        else if(protect_address[23:16]==8'b00000011)
                sector_reg[3] = 1'b1;
                sector_reg[3] = 1'b1;
        else if(protect_address[23:16]==8'b00000010)
        else if(protect_address[23:16]==8'b00000010)
                sector_reg[2] = 1'b1;
                sector_reg[2] = 1'b1;
        else if(protect_address[23:16]==8'b00000001)
        else if(protect_address[23:16]==8'b00000001)
                sector_reg[1] = 1'b1;
                sector_reg[1] = 1'b1;
        else if(protect_address[23:16]==8'b00000000)
        else if(protect_address[23:16]==8'b00000000)
                sector_reg[0] = 1'b1;
                sector_reg[0] = 1'b1;
    end
    end
end
end
endtask
endtask
 
 
// Sector Unprotection
// Sector Unprotection
task unprotect_sector;
task unprotect_sector;
input [23:0] unprotect_address;
input [23:0] unprotect_address;
begin
begin
    if ((DEVICE == "AT25DF041A") && unprotect==1'b1) // sectors of AT25DF041A
    if ((DEVICE == "AT25DF041A") && unprotect==1'b1) // sectors of AT25DF041A
    begin
    begin
        $display("Entered Unprotect sector for Device: AT25DF041A");
        $display("Entered Unprotect sector for Device: AT25DF041A");
        if(unprotect_address[23:14]==10'b00000111_11)
        if(unprotect_address[23:14]==10'b00000111_11)
                sector_reg[10] = 1'b0;
                sector_reg[10] = 1'b0;
        else if(unprotect_address[23:13]==11'b00000111_101)
        else if(unprotect_address[23:13]==11'b00000111_101)
                sector_reg[9] = 1'b0;
                sector_reg[9] = 1'b0;
        else if(unprotect_address[23:13]==11'b00000111_100)
        else if(unprotect_address[23:13]==11'b00000111_100)
                sector_reg[8] = 1'b0;
                sector_reg[8] = 1'b0;
        else if(unprotect_address[23:15]==9'b00000111_0)
        else if(unprotect_address[23:15]==9'b00000111_0)
                sector_reg[7] = 1'b0;
                sector_reg[7] = 1'b0;
        else if(unprotect_address[23:16]==8'b00000110)
        else if(unprotect_address[23:16]==8'b00000110)
                sector_reg[6] = 1'b0;
                sector_reg[6] = 1'b0;
        else if(unprotect_address[23:16]==8'b00000101)
        else if(unprotect_address[23:16]==8'b00000101)
                sector_reg[5] = 1'b0;
                sector_reg[5] = 1'b0;
        else if(unprotect_address[23:16]==8'b00000100)
        else if(unprotect_address[23:16]==8'b00000100)
                sector_reg[4] = 1'b0;
                sector_reg[4] = 1'b0;
        else if(unprotect_address[23:16]==8'b00000011)
        else if(unprotect_address[23:16]==8'b00000011)
                sector_reg[3] = 1'b0;
                sector_reg[3] = 1'b0;
        else if(unprotect_address[23:16]==8'b00000010)
        else if(unprotect_address[23:16]==8'b00000010)
                sector_reg[2] = 1'b0;
                sector_reg[2] = 1'b0;
        else if(unprotect_address[23:16]==8'b00000001)
        else if(unprotect_address[23:16]==8'b00000001)
                sector_reg[1] = 1'b0;
                sector_reg[1] = 1'b0;
        else if(unprotect_address[23:16]==8'b00000000)
        else if(unprotect_address[23:16]==8'b00000000)
                sector_reg[0] = 1'b0;
                sector_reg[0] = 1'b0;
    end
    end
    else if (DEVICE == "AT26DF081A" && unprotect==1'b1) // sectors of AT26DF081A
    else if (DEVICE == "AT26DF081A" && unprotect==1'b1) // sectors of AT26DF081A
    begin
    begin
        $display("Entered Unprotect sector for Device: AT26DF081A");
        $display("Entered Unprotect sector for Device: AT26DF081A");
        if(unprotect_address[23:17]==9'b00001111_1)
        if(unprotect_address[23:17]==9'b00001111_1)
                sector_reg[18] = 1'b0;
                sector_reg[18] = 1'b0;
        else if(unprotect_address[23:13]==11'b00001111_011)
        else if(unprotect_address[23:13]==11'b00001111_011)
                sector_reg[17] = 1'b0;
                sector_reg[17] = 1'b0;
        else if(unprotect_address[23:13]==11'b00001111_010)
        else if(unprotect_address[23:13]==11'b00001111_010)
                sector_reg[16] = 1'b0;
                sector_reg[16] = 1'b0;
        else if(unprotect_address[23:14]==10'b00001111_00)
        else if(unprotect_address[23:14]==10'b00001111_00)
                sector_reg[15] = 1'b0;
                sector_reg[15] = 1'b0;
        else if(unprotect_address[23:16]==8'b00001110)
        else if(unprotect_address[23:16]==8'b00001110)
                sector_reg[14] = 1'b0;
                sector_reg[14] = 1'b0;
        else if(unprotect_address[23:16]==8'b00001101)
        else if(unprotect_address[23:16]==8'b00001101)
                sector_reg[13] = 1'b0;
                sector_reg[13] = 1'b0;
        else if(unprotect_address[23:16]==8'b00001100)
        else if(unprotect_address[23:16]==8'b00001100)
                sector_reg[12] = 1'b0;
                sector_reg[12] = 1'b0;
        else if(unprotect_address[23:16]==8'b00001011)
        else if(unprotect_address[23:16]==8'b00001011)
                sector_reg[11] = 1'b0;
                sector_reg[11] = 1'b0;
        else if(unprotect_address[23:16]==8'b00001010)
        else if(unprotect_address[23:16]==8'b00001010)
                sector_reg[10] = 1'b0;
                sector_reg[10] = 1'b0;
        else if(unprotect_address[23:16]==8'b00001001)
        else if(unprotect_address[23:16]==8'b00001001)
                sector_reg[9] = 1'b0;
                sector_reg[9] = 1'b0;
        else if(unprotect_address[23:16]==8'b00001000)
        else if(unprotect_address[23:16]==8'b00001000)
                sector_reg[8] = 1'b0;
                sector_reg[8] = 1'b0;
        else if(unprotect_address[23:16]==8'b00000111)
        else if(unprotect_address[23:16]==8'b00000111)
                sector_reg[7] = 1'b0;
                sector_reg[7] = 1'b0;
        else if(unprotect_address[23:16]==8'b00000110)
        else if(unprotect_address[23:16]==8'b00000110)
                sector_reg[6] = 1'b0;
                sector_reg[6] = 1'b0;
        else if(unprotect_address[23:16]==8'b00000101)
        else if(unprotect_address[23:16]==8'b00000101)
                sector_reg[5] = 1'b0;
                sector_reg[5] = 1'b0;
        else if(unprotect_address[23:16]==8'b00000100)
        else if(unprotect_address[23:16]==8'b00000100)
                sector_reg[4] = 1'b0;
                sector_reg[4] = 1'b0;
        else if(unprotect_address[23:16]==8'b00000011)
        else if(unprotect_address[23:16]==8'b00000011)
                sector_reg[3] = 1'b0;
                sector_reg[3] = 1'b0;
        else if(unprotect_address[23:16]==8'b00000010)
        else if(unprotect_address[23:16]==8'b00000010)
                sector_reg[2] = 1'b0;
                sector_reg[2] = 1'b0;
        else if(unprotect_address[23:16]==8'b00000001)
        else if(unprotect_address[23:16]==8'b00000001)
                sector_reg[1] = 1'b0;
                sector_reg[1] = 1'b0;
        else if(unprotect_address[23:16]==8'b00000000)
        else if(unprotect_address[23:16]==8'b00000000)
                sector_reg[0] = 1'b0;
                sector_reg[0] = 1'b0;
    end
    end
    else if ((DEVICE == "AT26DF161A") && unprotect==1'b1) // sectors of AT26DF161A
    else if ((DEVICE == "AT26DF161A") && unprotect==1'b1) // sectors of AT26DF161A
    begin
    begin
        $display("Entered Unprotect sector for Device: AT26DF161A");
        $display("Entered Unprotect sector for Device: AT26DF161A");
        if(unprotect_address[23:16]==8'b00011111)
        if(unprotect_address[23:16]==8'b00011111)
                sector_reg[31] = 1'b0;
                sector_reg[31] = 1'b0;
        else if(unprotect_address[23:16]==8'b00011110)
        else if(unprotect_address[23:16]==8'b00011110)
                sector_reg[30] = 1'b0;
                sector_reg[30] = 1'b0;
        else if(unprotect_address[23:16]==8'b00011101)
        else if(unprotect_address[23:16]==8'b00011101)
                sector_reg[29] = 1'b0;
                sector_reg[29] = 1'b0;
        else if(unprotect_address[23:16]==8'b00011100)
        else if(unprotect_address[23:16]==8'b00011100)
                sector_reg[28] = 1'b0;
                sector_reg[28] = 1'b0;
        else if(unprotect_address[23:16]==8'b00011011)
        else if(unprotect_address[23:16]==8'b00011011)
                sector_reg[27] = 1'b0;
                sector_reg[27] = 1'b0;
        else if(unprotect_address[23:16]==8'b00011010)
        else if(unprotect_address[23:16]==8'b00011010)
                sector_reg[26] = 1'b0;
                sector_reg[26] = 1'b0;
        else if(unprotect_address[23:16]==8'b00011001)
        else if(unprotect_address[23:16]==8'b00011001)
                sector_reg[25] = 1'b0;
                sector_reg[25] = 1'b0;
        else if(unprotect_address[23:16]==8'b00011000)
        else if(unprotect_address[23:16]==8'b00011000)
                sector_reg[24] = 1'b0;
                sector_reg[24] = 1'b0;
        else if(unprotect_address[23:16]==8'b00010111)
        else if(unprotect_address[23:16]==8'b00010111)
                sector_reg[23] = 1'b0;
                sector_reg[23] = 1'b0;
        else if(unprotect_address[23:16]==8'b00010110)
        else if(unprotect_address[23:16]==8'b00010110)
                sector_reg[22] = 1'b0;
                sector_reg[22] = 1'b0;
        else if(unprotect_address[23:16]==8'b00010101)
        else if(unprotect_address[23:16]==8'b00010101)
                sector_reg[21] = 1'b0;
                sector_reg[21] = 1'b0;
        else if(unprotect_address[23:16]==8'b00010100)
        else if(unprotect_address[23:16]==8'b00010100)
                sector_reg[20] = 1'b0;
                sector_reg[20] = 1'b0;
        else if(unprotect_address[23:16]==8'b00010011)
        else if(unprotect_address[23:16]==8'b00010011)
                sector_reg[19] = 1'b0;
                sector_reg[19] = 1'b0;
        else if(unprotect_address[23:16]==8'b00010010)
        else if(unprotect_address[23:16]==8'b00010010)
                sector_reg[18] = 1'b0;
                sector_reg[18] = 1'b0;
        else if(unprotect_address[23:16]==8'b00010001)
        else if(unprotect_address[23:16]==8'b00010001)
                sector_reg[17] = 1'b0;
                sector_reg[17] = 1'b0;
        else if(unprotect_address[23:16]==8'b00010000)
        else if(unprotect_address[23:16]==8'b00010000)
                sector_reg[16] = 1'b0;
                sector_reg[16] = 1'b0;
        else if(unprotect_address[23:16]==8'b00001111)
        else if(unprotect_address[23:16]==8'b00001111)
                sector_reg[15] = 1'b0;
                sector_reg[15] = 1'b0;
        else if(unprotect_address[23:16]==8'b00001110)
        else if(unprotect_address[23:16]==8'b00001110)
                sector_reg[14] = 1'b0;
                sector_reg[14] = 1'b0;
        else if(unprotect_address[23:16]==8'b00001101)
        else if(unprotect_address[23:16]==8'b00001101)
                sector_reg[13] = 1'b0;
                sector_reg[13] = 1'b0;
        else if(unprotect_address[23:16]==8'b00001100)
        else if(unprotect_address[23:16]==8'b00001100)
                sector_reg[12] = 1'b0;
                sector_reg[12] = 1'b0;
        else if(unprotect_address[23:16]==8'b00001011)
        else if(unprotect_address[23:16]==8'b00001011)
                sector_reg[11] = 1'b0;
                sector_reg[11] = 1'b0;
        else if(unprotect_address[23:16]==8'b00001010)
        else if(unprotect_address[23:16]==8'b00001010)
                sector_reg[10] = 1'b0;
                sector_reg[10] = 1'b0;
        else if(unprotect_address[23:16]==8'b00001001)
        else if(unprotect_address[23:16]==8'b00001001)
                sector_reg[9] = 1'b0;
                sector_reg[9] = 1'b0;
        else if(unprotect_address[23:16]==8'b00001000)
        else if(unprotect_address[23:16]==8'b00001000)
                sector_reg[8] = 1'b0;
                sector_reg[8] = 1'b0;
        else if(unprotect_address[23:16]==8'b00000111)
        else if(unprotect_address[23:16]==8'b00000111)
                sector_reg[7] = 1'b0;
                sector_reg[7] = 1'b0;
        else if(unprotect_address[23:16]==8'b00000110)
        else if(unprotect_address[23:16]==8'b00000110)
                sector_reg[6] = 1'b0;
                sector_reg[6] = 1'b0;
        else if(unprotect_address[23:16]==8'b00000101)
        else if(unprotect_address[23:16]==8'b00000101)
                sector_reg[5] = 1'b0;
                sector_reg[5] = 1'b0;
        else if(unprotect_address[23:16]==8'b00000100)
        else if(unprotect_address[23:16]==8'b00000100)
                sector_reg[4] = 1'b0;
                sector_reg[4] = 1'b0;
        else if(unprotect_address[23:16]==8'b00000011)
        else if(unprotect_address[23:16]==8'b00000011)
                sector_reg[3] = 1'b0;
                sector_reg[3] = 1'b0;
        else if(unprotect_address[23:16]==8'b00000010)
        else if(unprotect_address[23:16]==8'b00000010)
                sector_reg[2] = 1'b0;
                sector_reg[2] = 1'b0;
        else if(unprotect_address[23:16]==8'b00000001)
        else if(unprotect_address[23:16]==8'b00000001)
                sector_reg[1] = 1'b0;
                sector_reg[1] = 1'b0;
        else if(unprotect_address[23:16]==8'b00000000)
        else if(unprotect_address[23:16]==8'b00000000)
                sector_reg[0] = 1'b0;
                sector_reg[0] = 1'b0;
    end
    end
    else if (DEVICE == "AT26DF321" && unprotect==1'b1) // sectors of AT26DF321
    else if (DEVICE == "AT26DF321" && unprotect==1'b1) // sectors of AT26DF321
    begin
    begin
        $display("Entered Unprotect sector for Device: AT26DF321");
        $display("Entered Unprotect sector for Device: AT26DF321");
        if(unprotect_address[23:16]==8'b00111111)
        if(unprotect_address[23:16]==8'b00111111)
                sector_reg[63] = 1'b0;
                sector_reg[63] = 1'b0;
        else if(unprotect_address[23:16]==8'b00111110)
        else if(unprotect_address[23:16]==8'b00111110)
                sector_reg[62] = 1'b0;
                sector_reg[62] = 1'b0;
        else if(unprotect_address[23:16]==8'b00111101)
        else if(unprotect_address[23:16]==8'b00111101)
                sector_reg[61] = 1'b0;
                sector_reg[61] = 1'b0;
        else if(unprotect_address[23:16]==8'b00111100)
        else if(unprotect_address[23:16]==8'b00111100)
                sector_reg[60] = 1'b0;
                sector_reg[60] = 1'b0;
        else if(unprotect_address[23:16]==8'b00111011)
        else if(unprotect_address[23:16]==8'b00111011)
                sector_reg[59] = 1'b0;
                sector_reg[59] = 1'b0;
        else if(unprotect_address[23:16]==8'b00111010)
        else if(unprotect_address[23:16]==8'b00111010)
                sector_reg[58] = 1'b0;
                sector_reg[58] = 1'b0;
        else if(unprotect_address[23:16]==8'b00111001)
        else if(unprotect_address[23:16]==8'b00111001)
                sector_reg[57] = 1'b0;
                sector_reg[57] = 1'b0;
        else if(unprotect_address[23:16]==8'b00111000)
        else if(unprotect_address[23:16]==8'b00111000)
                sector_reg[56] = 1'b0;
                sector_reg[56] = 1'b0;
        else if(unprotect_address[23:16]==8'b00110111)
        else if(unprotect_address[23:16]==8'b00110111)
                sector_reg[55] = 1'b0;
                sector_reg[55] = 1'b0;
        else if(unprotect_address[23:16]==8'b00110110)
        else if(unprotect_address[23:16]==8'b00110110)
                sector_reg[54] = 1'b0;
                sector_reg[54] = 1'b0;
        else if(unprotect_address[23:16]==8'b00110101)
        else if(unprotect_address[23:16]==8'b00110101)
                sector_reg[53] = 1'b0;
                sector_reg[53] = 1'b0;
        else if(unprotect_address[23:16]==8'b00110100)
        else if(unprotect_address[23:16]==8'b00110100)
                sector_reg[52] = 1'b0;
                sector_reg[52] = 1'b0;
        else if(unprotect_address[23:16]==8'b00110011)
        else if(unprotect_address[23:16]==8'b00110011)
                sector_reg[51] = 1'b0;
                sector_reg[51] = 1'b0;
        else if(unprotect_address[23:16]==8'b00110010)
        else if(unprotect_address[23:16]==8'b00110010)
                sector_reg[50] = 1'b0;
                sector_reg[50] = 1'b0;
        else if(unprotect_address[23:16]==8'b00110001)
        else if(unprotect_address[23:16]==8'b00110001)
                sector_reg[49] = 1'b0;
                sector_reg[49] = 1'b0;
        else if(unprotect_address[23:16]==8'b00110000)
        else if(unprotect_address[23:16]==8'b00110000)
                sector_reg[48] = 1'b0;
                sector_reg[48] = 1'b0;
        else if(unprotect_address[23:16]==8'b00101111)
        else if(unprotect_address[23:16]==8'b00101111)
                sector_reg[47] = 1'b0;
                sector_reg[47] = 1'b0;
        else if(unprotect_address[23:16]==8'b00101110)
        else if(unprotect_address[23:16]==8'b00101110)
                sector_reg[46] = 1'b0;
                sector_reg[46] = 1'b0;
        else if(unprotect_address[23:16]==8'b00101101)
        else if(unprotect_address[23:16]==8'b00101101)
                sector_reg[45] = 1'b0;
                sector_reg[45] = 1'b0;
        else if(unprotect_address[23:16]==8'b00101100)
        else if(unprotect_address[23:16]==8'b00101100)
                sector_reg[44] = 1'b0;
                sector_reg[44] = 1'b0;
        else if(unprotect_address[23:16]==8'b00101011)
        else if(unprotect_address[23:16]==8'b00101011)
                sector_reg[43] = 1'b0;
                sector_reg[43] = 1'b0;
        else if(unprotect_address[23:16]==8'b00101010)
        else if(unprotect_address[23:16]==8'b00101010)
                sector_reg[42] = 1'b0;
                sector_reg[42] = 1'b0;
        else if(unprotect_address[23:16]==8'b00101001)
        else if(unprotect_address[23:16]==8'b00101001)
                sector_reg[41] = 1'b0;
                sector_reg[41] = 1'b0;
        else if(unprotect_address[23:16]==8'b00101000)
        else if(unprotect_address[23:16]==8'b00101000)
                sector_reg[40] = 1'b0;
                sector_reg[40] = 1'b0;
        else if(unprotect_address[23:16]==8'b00100111)
        else if(unprotect_address[23:16]==8'b00100111)
                sector_reg[39] = 1'b0;
                sector_reg[39] = 1'b0;
        else if(unprotect_address[23:16]==8'b00100110)
        else if(unprotect_address[23:16]==8'b00100110)
                sector_reg[38] = 1'b0;
                sector_reg[38] = 1'b0;
        else if(unprotect_address[23:16]==8'b00100101)
        else if(unprotect_address[23:16]==8'b00100101)
                sector_reg[37] = 1'b0;
                sector_reg[37] = 1'b0;
        else if(unprotect_address[23:16]==8'b00100100)
        else if(unprotect_address[23:16]==8'b00100100)
                sector_reg[36] = 1'b0;
                sector_reg[36] = 1'b0;
        else if(unprotect_address[23:16]==8'b00100011)
        else if(unprotect_address[23:16]==8'b00100011)
                sector_reg[35] = 1'b0;
                sector_reg[35] = 1'b0;
        else if(unprotect_address[23:16]==8'b00100010)
        else if(unprotect_address[23:16]==8'b00100010)
                sector_reg[34] = 1'b0;
                sector_reg[34] = 1'b0;
        else if(unprotect_address[23:16]==8'b00100001)
        else if(unprotect_address[23:16]==8'b00100001)
                sector_reg[33] = 1'b0;
                sector_reg[33] = 1'b0;
        else if(unprotect_address[23:16]==8'b00100000)
        else if(unprotect_address[23:16]==8'b00100000)
                sector_reg[32] = 1'b0;
                sector_reg[32] = 1'b0;
        else if(unprotect_address[23:16]==8'b00011111)
        else if(unprotect_address[23:16]==8'b00011111)
                sector_reg[31] = 1'b0;
                sector_reg[31] = 1'b0;
        else if(unprotect_address[23:16]==8'b00011110)
        else if(unprotect_address[23:16]==8'b00011110)
                sector_reg[30] = 1'b0;
                sector_reg[30] = 1'b0;
        else if(unprotect_address[23:16]==8'b00011101)
        else if(unprotect_address[23:16]==8'b00011101)
                sector_reg[29] = 1'b0;
                sector_reg[29] = 1'b0;
        else if(unprotect_address[23:16]==8'b00011100)
        else if(unprotect_address[23:16]==8'b00011100)
                sector_reg[28] = 1'b0;
                sector_reg[28] = 1'b0;
        else if(unprotect_address[23:16]==8'b00011011)
        else if(unprotect_address[23:16]==8'b00011011)
                sector_reg[27] = 1'b0;
                sector_reg[27] = 1'b0;
        else if(unprotect_address[23:16]==8'b00011010)
        else if(unprotect_address[23:16]==8'b00011010)
                sector_reg[26] = 1'b0;
                sector_reg[26] = 1'b0;
        else if(unprotect_address[23:16]==8'b00011001)
        else if(unprotect_address[23:16]==8'b00011001)
                sector_reg[25] = 1'b0;
                sector_reg[25] = 1'b0;
        else if(unprotect_address[23:16]==8'b00011000)
        else if(unprotect_address[23:16]==8'b00011000)
                sector_reg[24] = 1'b0;
                sector_reg[24] = 1'b0;
        else if(unprotect_address[23:16]==8'b00010111)
        else if(unprotect_address[23:16]==8'b00010111)
                sector_reg[23] = 1'b0;
                sector_reg[23] = 1'b0;
        else if(unprotect_address[23:16]==8'b00010110)
        else if(unprotect_address[23:16]==8'b00010110)
                sector_reg[22] = 1'b0;
                sector_reg[22] = 1'b0;
        else if(unprotect_address[23:16]==8'b00010101)
        else if(unprotect_address[23:16]==8'b00010101)
                sector_reg[21] = 1'b0;
                sector_reg[21] = 1'b0;
        else if(unprotect_address[23:16]==8'b00010100)
        else if(unprotect_address[23:16]==8'b00010100)
                sector_reg[20] = 1'b0;
                sector_reg[20] = 1'b0;
        else if(unprotect_address[23:16]==8'b00010011)
        else if(unprotect_address[23:16]==8'b00010011)
                sector_reg[19] = 1'b0;
                sector_reg[19] = 1'b0;
        else if(unprotect_address[23:16]==8'b00010010)
        else if(unprotect_address[23:16]==8'b00010010)
                sector_reg[18] = 1'b0;
                sector_reg[18] = 1'b0;
        else if(unprotect_address[23:16]==8'b00010001)
        else if(unprotect_address[23:16]==8'b00010001)
                sector_reg[17] = 1'b0;
                sector_reg[17] = 1'b0;
        else if(unprotect_address[23:16]==8'b00010000)
        else if(unprotect_address[23:16]==8'b00010000)
                sector_reg[16] = 1'b0;
                sector_reg[16] = 1'b0;
        else if(unprotect_address[23:16]==8'b00001111)
        else if(unprotect_address[23:16]==8'b00001111)
                sector_reg[15] = 1'b0;
                sector_reg[15] = 1'b0;
        else if(unprotect_address[23:16]==8'b00001110)
        else if(unprotect_address[23:16]==8'b00001110)
                sector_reg[14] = 1'b0;
                sector_reg[14] = 1'b0;
        else if(unprotect_address[23:16]==8'b00001101)
        else if(unprotect_address[23:16]==8'b00001101)
                sector_reg[13] = 1'b0;
                sector_reg[13] = 1'b0;
        else if(unprotect_address[23:16]==8'b00001100)
        else if(unprotect_address[23:16]==8'b00001100)
                sector_reg[12] = 1'b0;
                sector_reg[12] = 1'b0;
        else if(unprotect_address[23:16]==8'b00001011)
        else if(unprotect_address[23:16]==8'b00001011)
                sector_reg[11] = 1'b0;
                sector_reg[11] = 1'b0;
        else if(unprotect_address[23:16]==8'b00001010)
        else if(unprotect_address[23:16]==8'b00001010)
                sector_reg[10] = 1'b0;
                sector_reg[10] = 1'b0;
        else if(unprotect_address[23:16]==8'b00001001)
        else if(unprotect_address[23:16]==8'b00001001)
                sector_reg[9] = 1'b0;
                sector_reg[9] = 1'b0;
        else if(unprotect_address[23:16]==8'b00001000)
        else if(unprotect_address[23:16]==8'b00001000)
                sector_reg[8] = 1'b0;
                sector_reg[8] = 1'b0;
        else if(unprotect_address[23:16]==8'b00000111)
        else if(unprotect_address[23:16]==8'b00000111)
                sector_reg[7] = 1'b0;
                sector_reg[7] = 1'b0;
        else if(unprotect_address[23:16]==8'b00000110)
        else if(unprotect_address[23:16]==8'b00000110)
                sector_reg[6] = 1'b0;
                sector_reg[6] = 1'b0;
        else if(unprotect_address[23:16]==8'b00000101)
        else if(unprotect_address[23:16]==8'b00000101)
                sector_reg[5] = 1'b0;
                sector_reg[5] = 1'b0;
        else if(unprotect_address[23:16]==8'b00000100)
        else if(unprotect_address[23:16]==8'b00000100)
                sector_reg[4] = 1'b0;
                sector_reg[4] = 1'b0;
        else if(unprotect_address[23:16]==8'b00000011)
        else if(unprotect_address[23:16]==8'b00000011)
                sector_reg[3] = 1'b0;
                sector_reg[3] = 1'b0;
        else if(unprotect_address[23:16]==8'b00000010)
        else if(unprotect_address[23:16]==8'b00000010)
                sector_reg[2] = 1'b0;
                sector_reg[2] = 1'b0;
        else if(unprotect_address[23:16]==8'b00000001)
        else if(unprotect_address[23:16]==8'b00000001)
                sector_reg[1] = 1'b0;
                sector_reg[1] = 1'b0;
        else if(unprotect_address[23:16]==8'b00000000)
        else if(unprotect_address[23:16]==8'b00000000)
                sector_reg[0] = 1'b0;
                sector_reg[0] = 1'b0;
    end
    end
        else
        else
        $display("Unprotect Sector failed");
        $display("Unprotect Sector failed");
 
 
end
end
endtask
endtask
 
 
// Check Sector Protection
// Check Sector Protection
task check_protection;
task check_protection;
input [23:0] check_address;
input [23:0] check_address;
begin
begin
    if (DEVICE == "AT26DF321")
    if (DEVICE == "AT26DF321")
    begin
    begin
        if((check_address[23:16]==8'b00111111 && sector_reg[63]==1'b1)
        if((check_address[23:16]==8'b00111111 && sector_reg[63]==1'b1)
        || (check_address[23:16]==8'b00111110 && sector_reg[62]==1'b1)
        || (check_address[23:16]==8'b00111110 && sector_reg[62]==1'b1)
        || (check_address[23:16]==8'b00111101 && sector_reg[61]==1'b1)
        || (check_address[23:16]==8'b00111101 && sector_reg[61]==1'b1)
        || (check_address[23:16]==8'b00111100 && sector_reg[60]==1'b1)
        || (check_address[23:16]==8'b00111100 && sector_reg[60]==1'b1)
        || (check_address[23:16]==8'b00111011 && sector_reg[59]==1'b1)
        || (check_address[23:16]==8'b00111011 && sector_reg[59]==1'b1)
        || (check_address[23:16]==8'b00111010 && sector_reg[58]==1'b1)
        || (check_address[23:16]==8'b00111010 && sector_reg[58]==1'b1)
        || (check_address[23:16]==8'b00111001 && sector_reg[57]==1'b1)
        || (check_address[23:16]==8'b00111001 && sector_reg[57]==1'b1)
        || (check_address[23:16]==8'b00111000 && sector_reg[56]==1'b1)
        || (check_address[23:16]==8'b00111000 && sector_reg[56]==1'b1)
        || (check_address[23:16]==8'b00110111 && sector_reg[55]==1'b1)
        || (check_address[23:16]==8'b00110111 && sector_reg[55]==1'b1)
        || (check_address[23:16]==8'b00110110 && sector_reg[54]==1'b1)
        || (check_address[23:16]==8'b00110110 && sector_reg[54]==1'b1)
        || (check_address[23:16]==8'b00110101 && sector_reg[53]==1'b1)
        || (check_address[23:16]==8'b00110101 && sector_reg[53]==1'b1)
        || (check_address[23:16]==8'b00110100 && sector_reg[52]==1'b1)
        || (check_address[23:16]==8'b00110100 && sector_reg[52]==1'b1)
        || (check_address[23:16]==8'b00110011 && sector_reg[51]==1'b1)
        || (check_address[23:16]==8'b00110011 && sector_reg[51]==1'b1)
        || (check_address[23:16]==8'b00110010 && sector_reg[50]==1'b1)
        || (check_address[23:16]==8'b00110010 && sector_reg[50]==1'b1)
        || (check_address[23:16]==8'b00110001 && sector_reg[49]==1'b1)
        || (check_address[23:16]==8'b00110001 && sector_reg[49]==1'b1)
        || (check_address[23:16]==8'b00110000 && sector_reg[48]==1'b1)
        || (check_address[23:16]==8'b00110000 && sector_reg[48]==1'b1)
        || (check_address[23:16]==8'b00101111 && sector_reg[47]==1'b1)
        || (check_address[23:16]==8'b00101111 && sector_reg[47]==1'b1)
        || (check_address[23:16]==8'b00101110 && sector_reg[46]==1'b1)
        || (check_address[23:16]==8'b00101110 && sector_reg[46]==1'b1)
        || (check_address[23:16]==8'b00101101 && sector_reg[45]==1'b1)
        || (check_address[23:16]==8'b00101101 && sector_reg[45]==1'b1)
        || (check_address[23:16]==8'b00101100 && sector_reg[44]==1'b1)
        || (check_address[23:16]==8'b00101100 && sector_reg[44]==1'b1)
        || (check_address[23:16]==8'b00101011 && sector_reg[43]==1'b1)
        || (check_address[23:16]==8'b00101011 && sector_reg[43]==1'b1)
        || (check_address[23:16]==8'b00101010 && sector_reg[42]==1'b1)
        || (check_address[23:16]==8'b00101010 && sector_reg[42]==1'b1)
        || (check_address[23:16]==8'b00101001 && sector_reg[41]==1'b1)
        || (check_address[23:16]==8'b00101001 && sector_reg[41]==1'b1)
        || (check_address[23:16]==8'b00101000 && sector_reg[40]==1'b1)
        || (check_address[23:16]==8'b00101000 && sector_reg[40]==1'b1)
        || (check_address[23:16]==8'b00100111 && sector_reg[39]==1'b1)
        || (check_address[23:16]==8'b00100111 && sector_reg[39]==1'b1)
        || (check_address[23:16]==8'b00100110 && sector_reg[38]==1'b1)
        || (check_address[23:16]==8'b00100110 && sector_reg[38]==1'b1)
        || (check_address[23:16]==8'b00100101 && sector_reg[37]==1'b1)
        || (check_address[23:16]==8'b00100101 && sector_reg[37]==1'b1)
        || (check_address[23:16]==8'b00100100 && sector_reg[36]==1'b1)
        || (check_address[23:16]==8'b00100100 && sector_reg[36]==1'b1)
        || (check_address[23:16]==8'b00100011 && sector_reg[35]==1'b1)
        || (check_address[23:16]==8'b00100011 && sector_reg[35]==1'b1)
        || (check_address[23:16]==8'b00100010 && sector_reg[34]==1'b1)
        || (check_address[23:16]==8'b00100010 && sector_reg[34]==1'b1)
        || (check_address[23:16]==8'b00100001 && sector_reg[33]==1'b1)
        || (check_address[23:16]==8'b00100001 && sector_reg[33]==1'b1)
        || (check_address[23:16]==8'b00100000 && sector_reg[32]==1'b1)
        || (check_address[23:16]==8'b00100000 && sector_reg[32]==1'b1)
        || (check_address[23:16]==8'b00011111 && sector_reg[31]==1'b1)
        || (check_address[23:16]==8'b00011111 && sector_reg[31]==1'b1)
        || (check_address[23:16]==8'b00011110 && sector_reg[30]==1'b1)
        || (check_address[23:16]==8'b00011110 && sector_reg[30]==1'b1)
        || (check_address[23:16]==8'b00011101 && sector_reg[29]==1'b1)
        || (check_address[23:16]==8'b00011101 && sector_reg[29]==1'b1)
        || (check_address[23:16]==8'b00011100 && sector_reg[28]==1'b1)
        || (check_address[23:16]==8'b00011100 && sector_reg[28]==1'b1)
        || (check_address[23:16]==8'b00011011 && sector_reg[27]==1'b1)
        || (check_address[23:16]==8'b00011011 && sector_reg[27]==1'b1)
        || (check_address[23:16]==8'b00011010 && sector_reg[26]==1'b1)
        || (check_address[23:16]==8'b00011010 && sector_reg[26]==1'b1)
        || (check_address[23:16]==8'b00011001 && sector_reg[25]==1'b1)
        || (check_address[23:16]==8'b00011001 && sector_reg[25]==1'b1)
        || (check_address[23:16]==8'b00011000 && sector_reg[24]==1'b1)
        || (check_address[23:16]==8'b00011000 && sector_reg[24]==1'b1)
        || (check_address[23:16]==8'b00010111 && sector_reg[23]==1'b1)
        || (check_address[23:16]==8'b00010111 && sector_reg[23]==1'b1)
        || (check_address[23:16]==8'b00010110 && sector_reg[22]==1'b1)
        || (check_address[23:16]==8'b00010110 && sector_reg[22]==1'b1)
        || (check_address[23:16]==8'b00010101 && sector_reg[21]==1'b1)
        || (check_address[23:16]==8'b00010101 && sector_reg[21]==1'b1)
        || (check_address[23:16]==8'b00010100 && sector_reg[20]==1'b1)
        || (check_address[23:16]==8'b00010100 && sector_reg[20]==1'b1)
        || (check_address[23:16]==8'b00010011 && sector_reg[19]==1'b1)
        || (check_address[23:16]==8'b00010011 && sector_reg[19]==1'b1)
        || (check_address[23:16]==8'b00010010 && sector_reg[18]==1'b1)
        || (check_address[23:16]==8'b00010010 && sector_reg[18]==1'b1)
        || (check_address[23:16]==8'b00010001 && sector_reg[17]==1'b1)
        || (check_address[23:16]==8'b00010001 && sector_reg[17]==1'b1)
        || (check_address[23:16]==8'b00010000 && sector_reg[16]==1'b1)
        || (check_address[23:16]==8'b00010000 && sector_reg[16]==1'b1)
        || (check_address[23:16]==8'b00001111 && sector_reg[15]==1'b1)
        || (check_address[23:16]==8'b00001111 && sector_reg[15]==1'b1)
        || (check_address[23:16]==8'b00001110 && sector_reg[14]==1'b1)
        || (check_address[23:16]==8'b00001110 && sector_reg[14]==1'b1)
        || (check_address[23:16]==8'b00001101 && sector_reg[13]==1'b1)
        || (check_address[23:16]==8'b00001101 && sector_reg[13]==1'b1)
        || (check_address[23:16]==8'b00001100 && sector_reg[12]==1'b1)
        || (check_address[23:16]==8'b00001100 && sector_reg[12]==1'b1)
        || (check_address[23:16]==8'b00001011 && sector_reg[11]==1'b1)
        || (check_address[23:16]==8'b00001011 && sector_reg[11]==1'b1)
        || (check_address[23:16]==8'b00001010 && sector_reg[10]==1'b1)
        || (check_address[23:16]==8'b00001010 && sector_reg[10]==1'b1)
        || (check_address[23:16]==8'b00001001 && sector_reg[9] ==1'b1)
        || (check_address[23:16]==8'b00001001 && sector_reg[9] ==1'b1)
        || (check_address[23:16]==8'b00001000 && sector_reg[8] ==1'b1)
        || (check_address[23:16]==8'b00001000 && sector_reg[8] ==1'b1)
        || (check_address[23:16]==8'b00000111 && sector_reg[7] ==1'b1)
        || (check_address[23:16]==8'b00000111 && sector_reg[7] ==1'b1)
        || (check_address[23:16]==8'b00000110 && sector_reg[6] ==1'b1)
        || (check_address[23:16]==8'b00000110 && sector_reg[6] ==1'b1)
        || (check_address[23:16]==8'b00000101 && sector_reg[5] ==1'b1)
        || (check_address[23:16]==8'b00000101 && sector_reg[5] ==1'b1)
        || (check_address[23:16]==8'b00000100 && sector_reg[4] ==1'b1)
        || (check_address[23:16]==8'b00000100 && sector_reg[4] ==1'b1)
        || (check_address[23:16]==8'b00000011 && sector_reg[3] ==1'b1)
        || (check_address[23:16]==8'b00000011 && sector_reg[3] ==1'b1)
        || (check_address[23:16]==8'b00000010 && sector_reg[2] ==1'b1)
        || (check_address[23:16]==8'b00000010 && sector_reg[2] ==1'b1)
        || (check_address[23:16]==8'b00000001 && sector_reg[1] ==1'b1)
        || (check_address[23:16]==8'b00000001 && sector_reg[1] ==1'b1)
        || (check_address[23:16]==8'b00000000 && sector_reg[0] ==1'b1))
        || (check_address[23:16]==8'b00000000 && sector_reg[0] ==1'b1))
                protected = 1'b1;
                protected = 1'b1;
        else
        else
                protected = 1'b0;
                protected = 1'b0;
    end
    end
    else if (DEVICE == "AT26DF161A")
    else if (DEVICE == "AT26DF161A")
    begin
    begin
        if((check_address[23:16]==8'b00011111 && sector_reg[31]==1'b1)
        if((check_address[23:16]==8'b00011111 && sector_reg[31]==1'b1)
        || (check_address[23:16]==8'b00011110 && sector_reg[30]==1'b1)
        || (check_address[23:16]==8'b00011110 && sector_reg[30]==1'b1)
        || (check_address[23:16]==8'b00011101 && sector_reg[29]==1'b1)
        || (check_address[23:16]==8'b00011101 && sector_reg[29]==1'b1)
        || (check_address[23:16]==8'b00011100 && sector_reg[28]==1'b1)
        || (check_address[23:16]==8'b00011100 && sector_reg[28]==1'b1)
        || (check_address[23:16]==8'b00011011 && sector_reg[27]==1'b1)
        || (check_address[23:16]==8'b00011011 && sector_reg[27]==1'b1)
        || (check_address[23:16]==8'b00011010 && sector_reg[26]==1'b1)
        || (check_address[23:16]==8'b00011010 && sector_reg[26]==1'b1)
        || (check_address[23:16]==8'b00011001 && sector_reg[25]==1'b1)
        || (check_address[23:16]==8'b00011001 && sector_reg[25]==1'b1)
        || (check_address[23:16]==8'b00011000 && sector_reg[24]==1'b1)
        || (check_address[23:16]==8'b00011000 && sector_reg[24]==1'b1)
        || (check_address[23:16]==8'b00010111 && sector_reg[23]==1'b1)
        || (check_address[23:16]==8'b00010111 && sector_reg[23]==1'b1)
        || (check_address[23:16]==8'b00010110 && sector_reg[22]==1'b1)
        || (check_address[23:16]==8'b00010110 && sector_reg[22]==1'b1)
        || (check_address[23:16]==8'b00010101 && sector_reg[21]==1'b1)
        || (check_address[23:16]==8'b00010101 && sector_reg[21]==1'b1)
        || (check_address[23:16]==8'b00010100 && sector_reg[20]==1'b1)
        || (check_address[23:16]==8'b00010100 && sector_reg[20]==1'b1)
        || (check_address[23:16]==8'b00010011 && sector_reg[19]==1'b1)
        || (check_address[23:16]==8'b00010011 && sector_reg[19]==1'b1)
        || (check_address[23:16]==8'b00010010 && sector_reg[18]==1'b1)
        || (check_address[23:16]==8'b00010010 && sector_reg[18]==1'b1)
        || (check_address[23:16]==8'b00010001 && sector_reg[17]==1'b1)
        || (check_address[23:16]==8'b00010001 && sector_reg[17]==1'b1)
        || (check_address[23:16]==8'b00010000 && sector_reg[16]==1'b1)
        || (check_address[23:16]==8'b00010000 && sector_reg[16]==1'b1)
        || (check_address[23:16]==8'b00001111 && sector_reg[15]==1'b1)
        || (check_address[23:16]==8'b00001111 && sector_reg[15]==1'b1)
        || (check_address[23:16]==8'b00001110 && sector_reg[14]==1'b1)
        || (check_address[23:16]==8'b00001110 && sector_reg[14]==1'b1)
        || (check_address[23:16]==8'b00001101 && sector_reg[13]==1'b1)
        || (check_address[23:16]==8'b00001101 && sector_reg[13]==1'b1)
        || (check_address[23:16]==8'b00001100 && sector_reg[12]==1'b1)
        || (check_address[23:16]==8'b00001100 && sector_reg[12]==1'b1)
        || (check_address[23:16]==8'b00001011 && sector_reg[11]==1'b1)
        || (check_address[23:16]==8'b00001011 && sector_reg[11]==1'b1)
        || (check_address[23:16]==8'b00001010 && sector_reg[10]==1'b1)
        || (check_address[23:16]==8'b00001010 && sector_reg[10]==1'b1)
        || (check_address[23:16]==8'b00001001 && sector_reg[9] ==1'b1)
        || (check_address[23:16]==8'b00001001 && sector_reg[9] ==1'b1)
        || (check_address[23:16]==8'b00001000 && sector_reg[8] ==1'b1)
        || (check_address[23:16]==8'b00001000 && sector_reg[8] ==1'b1)
        || (check_address[23:16]==8'b00000111 && sector_reg[7] ==1'b1)
        || (check_address[23:16]==8'b00000111 && sector_reg[7] ==1'b1)
        || (check_address[23:16]==8'b00000110 && sector_reg[6] ==1'b1)
        || (check_address[23:16]==8'b00000110 && sector_reg[6] ==1'b1)
        || (check_address[23:16]==8'b00000101 && sector_reg[5] ==1'b1)
        || (check_address[23:16]==8'b00000101 && sector_reg[5] ==1'b1)
        || (check_address[23:16]==8'b00000100 && sector_reg[4] ==1'b1)
        || (check_address[23:16]==8'b00000100 && sector_reg[4] ==1'b1)
        || (check_address[23:16]==8'b00000011 && sector_reg[3] ==1'b1)
        || (check_address[23:16]==8'b00000011 && sector_reg[3] ==1'b1)
        || (check_address[23:16]==8'b00000010 && sector_reg[2] ==1'b1)
        || (check_address[23:16]==8'b00000010 && sector_reg[2] ==1'b1)
        || (check_address[23:16]==8'b00000001 && sector_reg[1] ==1'b1)
        || (check_address[23:16]==8'b00000001 && sector_reg[1] ==1'b1)
        || (check_address[23:16]==8'b00000000 && sector_reg[0] ==1'b1))
        || (check_address[23:16]==8'b00000000 && sector_reg[0] ==1'b1))
                protected = 1'b1;
                protected = 1'b1;
        else
        else
                protected = 1'b0;
                protected = 1'b0;
    end
    end
    else if (DEVICE == "AT26DF081A") // un-uniform sectors of AT26DF081A
    else if (DEVICE == "AT26DF081A") // un-uniform sectors of AT26DF081A
    begin
    begin
        if((check_address[23:17]==9'b00001111_1 && sector_reg[18]   ==1'b1)
        if((check_address[23:17]==9'b00001111_1 && sector_reg[18]   ==1'b1)
        || (check_address[23:13]==11'b00001111_011 && sector_reg[17]==1'b1)
        || (check_address[23:13]==11'b00001111_011 && sector_reg[17]==1'b1)
        || (check_address[23:13]==11'b00001111_010 && sector_reg[16]==1'b1)
        || (check_address[23:13]==11'b00001111_010 && sector_reg[16]==1'b1)
        || (check_address[23:14]==10'b00001111_00 && sector_reg[15] ==1'b1)
        || (check_address[23:14]==10'b00001111_00 && sector_reg[15] ==1'b1)
        || (check_address[23:16]==8'b00001110 && sector_reg[14]==1'b1)
        || (check_address[23:16]==8'b00001110 && sector_reg[14]==1'b1)
        || (check_address[23:16]==8'b00001101 && sector_reg[13]==1'b1)
        || (check_address[23:16]==8'b00001101 && sector_reg[13]==1'b1)
        || (check_address[23:16]==8'b00001100 && sector_reg[12]==1'b1)
        || (check_address[23:16]==8'b00001100 && sector_reg[12]==1'b1)
        || (check_address[23:16]==8'b00001011 && sector_reg[11]==1'b1)
        || (check_address[23:16]==8'b00001011 && sector_reg[11]==1'b1)
        || (check_address[23:16]==8'b00001010 && sector_reg[10]==1'b1)
        || (check_address[23:16]==8'b00001010 && sector_reg[10]==1'b1)
        || (check_address[23:16]==8'b00001001 && sector_reg[9] ==1'b1)
        || (check_address[23:16]==8'b00001001 && sector_reg[9] ==1'b1)
        || (check_address[23:16]==8'b00001000 && sector_reg[8] ==1'b1)
        || (check_address[23:16]==8'b00001000 && sector_reg[8] ==1'b1)
        || (check_address[23:16]==8'b00000111 && sector_reg[7] ==1'b1)
        || (check_address[23:16]==8'b00000111 && sector_reg[7] ==1'b1)
        || (check_address[23:16]==8'b00000110 && sector_reg[6] ==1'b1)
        || (check_address[23:16]==8'b00000110 && sector_reg[6] ==1'b1)
        || (check_address[23:16]==8'b00000101 && sector_reg[5] ==1'b1)
        || (check_address[23:16]==8'b00000101 && sector_reg[5] ==1'b1)
        || (check_address[23:16]==8'b00000100 && sector_reg[4] ==1'b1)
        || (check_address[23:16]==8'b00000100 && sector_reg[4] ==1'b1)
        || (check_address[23:16]==8'b00000011 && sector_reg[3] ==1'b1)
        || (check_address[23:16]==8'b00000011 && sector_reg[3] ==1'b1)
        || (check_address[23:16]==8'b00000010 && sector_reg[2] ==1'b1)
        || (check_address[23:16]==8'b00000010 && sector_reg[2] ==1'b1)
        || (check_address[23:16]==8'b00000001 && sector_reg[1] ==1'b1)
        || (check_address[23:16]==8'b00000001 && sector_reg[1] ==1'b1)
        || (check_address[23:16]==8'b00000000 && sector_reg[0] ==1'b1))
        || (check_address[23:16]==8'b00000000 && sector_reg[0] ==1'b1))
                protected = 1'b1;
                protected = 1'b1;
        else
        else
                protected = 1'b0;
                protected = 1'b0;
    end
    end
    else if (DEVICE == "AT25DF041A") // un-uniform sectors of AT25DF041A
    else if (DEVICE == "AT25DF041A") // un-uniform sectors of AT25DF041A
    begin
    begin
        if((check_address[23:14]==10'b00000111_11 && sector_reg[10]==1'b1)
        if((check_address[23:14]==10'b00000111_11 && sector_reg[10]==1'b1)
        || (check_address[23:13]==11'b00000111_101 && sector_reg[9]==1'b1)
        || (check_address[23:13]==11'b00000111_101 && sector_reg[9]==1'b1)
        || (check_address[23:13]==11'b00000111_100 && sector_reg[8]==1'b1)
        || (check_address[23:13]==11'b00000111_100 && sector_reg[8]==1'b1)
        || (check_address[23:15]==9'b00000111_0 && sector_reg[7]   ==1'b1)
        || (check_address[23:15]==9'b00000111_0 && sector_reg[7]   ==1'b1)
        || (check_address[23:16]==8'b00000110 && sector_reg[6]==1'b1)
        || (check_address[23:16]==8'b00000110 && sector_reg[6]==1'b1)
        || (check_address[23:16]==8'b00000101 && sector_reg[5]==1'b1)
        || (check_address[23:16]==8'b00000101 && sector_reg[5]==1'b1)
        || (check_address[23:16]==8'b00000100 && sector_reg[4]==1'b1)
        || (check_address[23:16]==8'b00000100 && sector_reg[4]==1'b1)
        || (check_address[23:16]==8'b00000011 && sector_reg[3]==1'b1)
        || (check_address[23:16]==8'b00000011 && sector_reg[3]==1'b1)
        || (check_address[23:16]==8'b00000010 && sector_reg[2]==1'b1)
        || (check_address[23:16]==8'b00000010 && sector_reg[2]==1'b1)
        || (check_address[23:16]==8'b00000001 && sector_reg[1]==1'b1)
        || (check_address[23:16]==8'b00000001 && sector_reg[1]==1'b1)
        || (check_address[23:16]==8'b00000000 && sector_reg[0]==1'b1))
        || (check_address[23:16]==8'b00000000 && sector_reg[0]==1'b1))
                protected = 1'b1;
                protected = 1'b1;
        else
        else
                protected = 1'b0;
                protected = 1'b0;
    end
    end
        else
        else
        $display("Check Sector Protection failed");
        $display("Check Sector Protection failed");
end
end
endtask
endtask
 
 
// Sending Sector Protection status
// Sending Sector Protection status
task send_protection_status;
task send_protection_status;
integer local_i;
integer local_i;
reg [7:0] send_status;   // status value, 'ff' if protected, '00' if unprotected
reg [7:0] send_status;   // status value, 'ff' if protected, '00' if unprotected
begin
begin
        if(protected==1'b1)
        if(protected==1'b1)
                send_status = 8'hff;
                send_status = 8'hff;
        else
        else
                send_status = 8'h00;
                send_status = 8'h00;
        local_i = 7;
        local_i = 7;
 
 
        while (CSB == 1'b0) // continue transmitting, while, CSB is Low
        while (CSB == 1'b0) // continue transmitting, while, CSB is Low
        begin
        begin
                @(negedge SCK);
                @(negedge SCK);
                SO_reg = 1'bx;
                SO_reg = 1'bx;
                if(HOLDB==1'b0)
                if(HOLDB==1'b0)
                begin
                begin
                        #tHLQZ SO_on = 1'b0;
                        #tHLQZ SO_on = 1'b0;
                        wait (HOLDB);
                        wait (HOLDB);
                        #tHHQX;
                        #tHHQX;
                end
                end
                #tV;
                #tV;
                SO_reg = send_status[local_i];
                SO_reg = send_status[local_i];
                SO_on = 1'b1;
                SO_on = 1'b1;
                if (local_i == 0)
                if (local_i == 0)
                        local_i = 7;
                        local_i = 7;
                else
                else
                        local_i = local_i - 1; // next bit
                        local_i = local_i - 1; // next bit
        end // sending status over, because CSB has gone high
        end // sending status over, because CSB has gone high
end
end
endtask
endtask
 
 
// receive data for byte/page programming
// receive data for byte/page programming
task page_program;
task page_program;
begin
begin
        pp_j = 0;
        pp_j = 0;
        while(CSB==1'b0)
        while(CSB==1'b0)
        begin
        begin
                for (pp_i=8; pp_i>0; pp_i = pp_i-1)
                for (pp_i=8; pp_i>0; pp_i = pp_i-1)
                begin
                begin
                        @(posedge SCK);
                        @(posedge SCK);
                        read_data[pp_i-1] = SI;
                        read_data[pp_i-1] = SI;
                        if(HOLDB==1'b0)
                        if(HOLDB==1'b0)
                                wait (HOLDB);
                                wait (HOLDB);
                end
                end
                int_buffer[pp_j] = read_data;
                int_buffer[pp_j] = read_data;
                pp_j = pp_j+1;
                pp_j = pp_j+1;
        $display("One byte of data: %h received for Page Program",read_data);
        $display("One byte of data: %h received for Page Program",read_data);
        end
        end
end
end
endtask
endtask
 
 
// Byte program for devices. also used for page program
// Byte program for devices. also used for page program
task byte_program;
task byte_program;
input [23:0] write_address;
input [23:0] write_address;
input [7:0] write_data;
input [7:0] write_data;
begin
begin
        memory [write_address] = write_data;
        memory [write_address] = write_data;
        $display ("One Byte of data %h written in memory in location %h", write_data, write_address);
        $display ("One Byte of data %h written in memory in location %h", write_data, write_address);
end
end
endtask
endtask
 
 
// Erase a 4kB block
// Erase a 4kB block
task erase_4kb;
task erase_4kb;
input [23:0] erase_address;
input [23:0] erase_address;
 
 
reg [11:0] erase4_i;
reg [11:0] erase4_i;
reg [23:0] block_addr4;
reg [23:0] block_addr4;
 
 
begin
begin
        block_addr4 = {erase_address[23:12],12'b0000_0000_0000};
        block_addr4 = {erase_address[23:12],12'b0000_0000_0000};
        $display ("4kB Block with start address %h is going to be erased", block_addr4);
        $display ("4kB Block with start address %h is going to be erased", block_addr4);
 
 
        for(erase4_i=12'b0; erase4_i < 12'b1111_1111_1111; erase4_i=erase4_i+1)
        for(erase4_i=12'b0; erase4_i < 12'b1111_1111_1111; erase4_i=erase4_i+1)
        begin
        begin
                memory [block_addr4 + erase4_i] = 8'hff;
                memory [block_addr4 + erase4_i] = 8'hff;
        end
        end
end
end
endtask
endtask
 
 
// Erase a 32kB block
// Erase a 32kB block
task erase_32kb;
task erase_32kb;
input [23:0] erase_address;
input [23:0] erase_address;
 
 
reg [14:0] erase32_i;
reg [14:0] erase32_i;
reg [23:0] block_addr32;
reg [23:0] block_addr32;
 
 
begin
begin
        block_addr32 = {erase_address[23:15],15'b0000_0000_0000_000};
        block_addr32 = {erase_address[23:15],15'b0000_0000_0000_000};
        $display ("32kB Block with start address %h is going to be erased", block_addr32);
        $display ("32kB Block with start address %h is going to be erased", block_addr32);
 
 
        for(erase32_i=15'b0; erase32_i < 15'b1111_1111_1111_111; erase32_i=erase32_i+1)
        for(erase32_i=15'b0; erase32_i < 15'b1111_1111_1111_111; erase32_i=erase32_i+1)
        begin
        begin
                memory [block_addr32 + erase32_i] = 8'hff;
                memory [block_addr32 + erase32_i] = 8'hff;
        end
        end
end
end
endtask
endtask
 
 
// Erase a 64kB block
// Erase a 64kB block
task erase_64kb;
task erase_64kb;
input [23:0] erase_address;
input [23:0] erase_address;
 
 
reg [15:0] erase64_i;
reg [15:0] erase64_i;
reg [23:0] block_addr64;
reg [23:0] block_addr64;
 
 
begin
begin
        block_addr64 = {erase_address[23:16],16'b0000_0000_0000_0000};
        block_addr64 = {erase_address[23:16],16'b0000_0000_0000_0000};
        $display ("64kB Block with start address %h is going to be erased", block_addr64);
        $display ("64kB Block with start address %h is going to be erased", block_addr64);
 
 
        for(erase64_i=0; erase64_i < 16'b1111_1111_1111_1111; erase64_i=erase64_i+1)
        for(erase64_i=0; erase64_i < 16'b1111_1111_1111_1111; erase64_i=erase64_i+1)
        begin
        begin
                memory [block_addr64 + erase64_i] = 8'hff;
                memory [block_addr64 + erase64_i] = 8'hff;
        end
        end
end
end
endtask
endtask
 
 
// Chip Erase
// Chip Erase
task erase_chip;
task erase_chip;
 
 
reg [23:0] erase_i;
reg [23:0] erase_i;
 
 
begin
begin
        $display ("Chip Erase is going to be started");
        $display ("Chip Erase is going to be started");
        for(erase_i=0; erase_i < (MEMSIZE-1); erase_i=erase_i+1)
        for(erase_i=0; erase_i < (MEMSIZE-1); erase_i=erase_i+1)
        begin
        begin
                memory [erase_i] = 8'hff;
                memory [erase_i] = 8'hff;
        end
        end
end
end
endtask
endtask
 
 
// ******************* Execution of Opcodes ********************* //
// ******************* Execution of Opcodes ********************* //
 
 
// ************* Deep Power-down ***************** //
// ************* Deep Power-down ***************** //
always @(EDPD)
always @(EDPD)
begin : EDPD_
begin : EDPD_
        if (RDYnBSY == 1'b1) // device is already busy
        if (RDYnBSY == 1'b1) // device is already busy
        begin
        begin
                $display ("Device is busy. Deep Power-down command cannot be issued");
                $display ("Device is busy. Deep Power-down command cannot be issued");
                disable EDPD_ ;
                disable EDPD_ ;
        end
        end
        // if it comes here, means, the above if was false.
        // if it comes here, means, the above if was false.
 
 
   //if (freq_error==1'b1)
   //if (freq_error==1'b1)
        //$display("WARNING: Frequency should be less than %d Mhz for SPI interface. Deep Power-down is not allowed.",fSCK);
        //$display("WARNING: Frequency should be less than %d Mhz for SPI interface. Deep Power-down is not allowed.",fSCK);
 
 
        @ (posedge CSB);
        @ (posedge CSB);
        RDYnBSY = 1'b1;
        RDYnBSY = 1'b1;
        #tEDPD deep_power_down = 1'b1;
        #tEDPD deep_power_down = 1'b1;
        RDYnBSY = 1'b0;
        RDYnBSY = 1'b0;
        $display ("Device %s enters into Deep Power-down mode. Send 'Resume from Deep Power-down' to resume", DEVICE);
        $display ("Device %s enters into Deep Power-down mode. Send 'Resume from Deep Power-down' to resume", DEVICE);
end
end
 
 
// ************* Resume from Deep Power-down ***************** //
// ************* Resume from Deep Power-down ***************** //
always @(RDPD)
always @(RDPD)
begin : RDPD_
begin : RDPD_
        if (RDYnBSY == 1'b1) // device is already busy
        if (RDYnBSY == 1'b1) // device is already busy
        begin
        begin
                $display ("Device is busy. Deep Power-down command cannot be issued");
                $display ("Device is busy. Deep Power-down command cannot be issued");
                disable EDPD_ ;
                disable EDPD_ ;
        end
        end
        // if it comes here, means, the above if was false.
        // if it comes here, means, the above if was false.
 
 
   //if (freq_error==1'b1)
   //if (freq_error==1'b1)
        //$display("WARNING: Frequency should be less than %d Mhz for SPI interface. Resume from Deep Power-down is not allowed.",fSCK);
        //$display("WARNING: Frequency should be less than %d Mhz for SPI interface. Resume from Deep Power-down is not allowed.",fSCK);
 
 
        @ (posedge CSB);
        @ (posedge CSB);
        RDYnBSY = 1'b1;
        RDYnBSY = 1'b1;
        #tRDPD deep_power_down = 1'b0;
        #tRDPD deep_power_down = 1'b0;
        RDYnBSY = 1'b0;
        RDYnBSY = 1'b0;
        $display ("Device %s Resumes from Deep Power-down mode", DEVICE);
        $display ("Device %s Resumes from Deep Power-down mode", DEVICE);
end
end
 
 
// ************* Manufacturing ID Read ******************** //
// ************* Manufacturing ID Read ******************** //
always @(MIR)
always @(MIR)
begin: MIR_
begin: MIR_
        if (RDYnBSY == 1'b1) // device is already busy
        if (RDYnBSY == 1'b1) // device is already busy
        begin
        begin
                $display ("Device is busy. Manufacturing ID Read cannot be issued");
                $display ("Device is busy. Manufacturing ID Read cannot be issued");
                disable EDPD_ ;
                disable EDPD_ ;
        end
        end
        // if it comes here, means, the above if was false.
        // if it comes here, means, the above if was false.
 
 
   //if (freq_error==1'b1)
   //if (freq_error==1'b1)
        //$display("WARNING: Frequency should be less than %d Mhz for SPI interface. Manufacturing ID Read is not allowed.",fSCK);
        //$display("WARNING: Frequency should be less than %d Mhz for SPI interface. Manufacturing ID Read is not allowed.",fSCK);
 
 
        j = 32;
        j = 32;
        while (CSB == 1'b0)
        while (CSB == 1'b0)
        begin
        begin
                @(negedge SCK);
                @(negedge SCK);
                SO_reg = 1'bx;
                SO_reg = 1'bx;
                if(HOLDB==1'b0)
                if(HOLDB==1'b0)
                begin
                begin
                        #tHLQZ SO_on = 1'b0;
                        #tHLQZ SO_on = 1'b0;
                        wait (HOLDB);
                        wait (HOLDB);
                        #tHHQX;
                        #tHHQX;
                end
                end
                #tV;
                #tV;
                if (j > 0)
                if (j > 0)
                begin
                begin
                        SO_reg = MAN_ID[j-1];
                        SO_reg = MAN_ID[j-1];
                        j = j-1;
                        j = j-1;
                        SO_on = 1'b1;
                        SO_on = 1'b1;
                        if(j == 0)
                        if(j == 0)
                        begin
                        begin
                                $display ("Manufacture ID and Device ID of Device %s sent", DEVICE);
                                $display ("Manufacture ID and Device ID of Device %s sent", DEVICE);
                                j = 32;
                                j = 32;
                        end
                        end
                end
                end
        end // output next bit on next falling edge of SCK
        end // output next bit on next falling edge of SCK
        $display ("Manufacture ID and Device ID of Device %s sent", DEVICE);
        $display ("Manufacture ID and Device ID of Device %s sent", DEVICE);
end
end
 
 
// ************ Read Status Register ******************** //
// ************ Read Status Register ******************** //
always @(RSR)
always @(RSR)
begin: RSR_
begin: RSR_
   //if (freq_error==1'b1)
   //if (freq_error==1'b1)
        //$display("WARNING: Frequency should be less than %d Mhz for SPI interface. Status Register Read is not allowed.",fSCK);
        //$display("WARNING: Frequency should be less than %d Mhz for SPI interface. Status Register Read is not allowed.",fSCK);
 
 
        status_read = 1'b1; // reading status_reg
        status_read = 1'b1; // reading status_reg
        j = 8;
        j = 8;
        while (CSB == 1'b0)
        while (CSB == 1'b0)
        begin
        begin
                @(negedge SCK);
                @(negedge SCK);
                SO_reg = 1'bx;
                SO_reg = 1'bx;
                if(HOLDB==1'b0)
                if(HOLDB==1'b0)
                begin
                begin
                        #tHLQZ SO_on = 1'b0;
                        #tHLQZ SO_on = 1'b0;
                        wait (HOLDB);
                        wait (HOLDB);
                        #tHHQX;
                        #tHHQX;
                end
                end
                #tV;
                #tV;
                if (j > 0)
                if (j > 0)
                begin
                begin
                        SO_on = 1'b1;
                        SO_on = 1'b1;
                        SO_reg = status_reg[j-1];
                        SO_reg = status_reg[j-1];
                        j = j-1;
                        j = j-1;
                        if (j == 0)
                        if (j == 0)
                        begin
                        begin
                                j = 8;
                                j = 8;
                                $display ("Status register content of Device %s transmitted", DEVICE);
                                $display ("Status register content of Device %s transmitted", DEVICE);
                        end
                        end
                end
                end
        end // output next bit on next falling edge of SCK
        end // output next bit on next falling edge of SCK
        status_read = 1'b0; // status_reg read is over
        status_read = 1'b0; // status_reg read is over
end
end
 
 
// ************ Write Status Register ******************** //
// ************ Write Status Register ******************** //
always @(WSR)
always @(WSR)
begin : WSR_
begin : WSR_
        if (RDYnBSY == 1'b1) // device is already busy
        if (RDYnBSY == 1'b1) // device is already busy
        begin
        begin
                $display ("Device %s is busy. Write Status Register is not allowed", DEVICE);
                $display ("Device %s is busy. Write Status Register is not allowed", DEVICE);
                disable WSR_;
                disable WSR_;
        end
        end
        // if it comes here, means, the above if was false.
        // if it comes here, means, the above if was false.
 
 
   if (freq_error==1'b1)
   if (freq_error==1'b1)
        $display("WARNING: Frequency should be less than %d Mhz for SPI interface. Write Status Register is not allowed.",fSCK);
        $display("WARNING: Frequency should be less than %d Mhz for SPI interface. Write Status Register is not allowed.",fSCK);
 
 
        get_data;
        get_data;
        SPRL_val = read_data [7];
        SPRL_val = read_data [7];
        stat_reg_temp = read_data [5:2];
        stat_reg_temp = read_data [5:2];
 
 
        @ (posedge CSB);
        @ (posedge CSB);
        if(WEL==1'b1)
        if(WEL==1'b1)
        begin
        begin
                if(WPB==1'b0 && SPRL==1'b0 && SPRL_val==1'b1)
                if(WPB==1'b0 && SPRL==1'b0 && SPRL_val==1'b1)
                        $display("SPRL Hardware locked");
                        $display("SPRL Hardware locked");
                else if(WPB==1'b1 && SPRL==1'b0 && SPRL_val==1'b1)
                else if(WPB==1'b1 && SPRL==1'b0 && SPRL_val==1'b1)
                        $display("SPRL Software locked");
                        $display("SPRL Software locked");
                else if(WPB==1'b0 && SPRL==1'b1 && SPRL_val==1'b1)
                else if(WPB==1'b0 && SPRL==1'b1 && SPRL_val==1'b1)
                        $display("SPRL Hardware locked. Lock cannot be done");
                        $display("SPRL Hardware locked. Lock cannot be done");
                else if(WPB==1'b1 && SPRL==1'b1 && SPRL_val==1'b1)
                else if(WPB==1'b1 && SPRL==1'b1 && SPRL_val==1'b1)
                        $display("SPRL Software locked. Lock cannot be done");
                        $display("SPRL Software locked. Lock cannot be done");
                else if(WPB==1'b0 && SPRL==1'b1 && SPRL_val==1'b0)
                else if(WPB==1'b0 && SPRL==1'b1 && SPRL_val==1'b0)
                        $display("SPRL Hardware locked. Unlock cannot be done");
                        $display("SPRL Hardware locked. Unlock cannot be done");
                else if(WPB==1'b1 && SPRL==1'b1 && SPRL_val==1'b0)
                else if(WPB==1'b1 && SPRL==1'b1 && SPRL_val==1'b0)
                        $display("Sector Protection Register UnLocked");
                        $display("Sector Protection Register UnLocked");
                else
                else
                        $display("SPRL in unlocked state");
                        $display("SPRL in unlocked state");
 
 
                if(stat_reg_temp==4'b1111 && SPRL==1'b0)
                if(stat_reg_temp==4'b1111 && SPRL==1'b0)
                        global_protect = 1'b1;
                        global_protect = 1'b1;
                else if(stat_reg_temp==4'b0000 && SPRL==1'b0)
                else if(stat_reg_temp==4'b0000 && SPRL==1'b0)
                        global_unprotect = 1'b1;
                        global_unprotect = 1'b1;
                else if(SPRL==1'b1 && (stat_reg_temp==4'b0000 || stat_reg_temp==4'b1111))
                else if(SPRL==1'b1 && (stat_reg_temp==4'b0000 || stat_reg_temp==4'b1111))
                begin
                begin
                        $display("SPRL locked. Global Protect/Unprotect cannot be done");
                        $display("SPRL locked. Global Protect/Unprotect cannot be done");
                        global_protect = 1'b0;
                        global_protect = 1'b0;
                        global_unprotect = 1'b0;
                        global_unprotect = 1'b0;
                end
                end
 
 
                if(SPRL==1'b0 && global_protect==1'b1)
                if(SPRL==1'b0 && global_protect==1'b1)
                        $display("Global Protection issued for Sector protection register");
                        $display("Global Protection issued for Sector protection register");
                else if(SPRL==1'b0 && global_unprotect==1'b1)
                else if(SPRL==1'b0 && global_unprotect==1'b1)
                        $display("Global Unprotect issued for Sector protection register");
                        $display("Global Unprotect issued for Sector protection register");
                else if(SPRL==1'b1 && (global_protect==1'b1 || global_unprotect==1'b1))
                else if(SPRL==1'b1 && (global_protect==1'b1 || global_unprotect==1'b1))
                        $display("SPRL locked. Global Protect/Unprotect cannot be done");
                        $display("SPRL locked. Global Protect/Unprotect cannot be done");
 
 
                if(SPRL==1'b0 && SPRL_val==1'b1)
                if(SPRL==1'b0 && SPRL_val==1'b1)
                begin
                begin
                        RDYnBSY = 1'b1;
                        RDYnBSY = 1'b1;
                        lock = 1'b1;
                        lock = 1'b1;
                        #tWRSR;
                        #tWRSR;
                end
                end
                else if(SPRL==1'b1 && SPRL_val==1'b0)
                else if(SPRL==1'b1 && SPRL_val==1'b0)
                begin
                begin
                        RDYnBSY = 1'b1;
                        RDYnBSY = 1'b1;
                        unlock = 1'b1;
                        unlock = 1'b1;
                        #tWRSR;
                        #tWRSR;
                end
                end
                else if(global_protect==1'b1 || global_unprotect==1'b1)
                else if(global_protect==1'b1 || global_unprotect==1'b1)
                begin
                begin
                        RDYnBSY = 1'b1;
                        RDYnBSY = 1'b1;
                        #tWRSR;
                        #tWRSR;
                end
                end
        end
        end
        else
        else
                $display ("WEL bit not set. Write Status Register is not allowed");
                $display ("WEL bit not set. Write Status Register is not allowed");
 
 
        lock = 1'b0;
        lock = 1'b0;
        unlock = 1'b0;
        unlock = 1'b0;
        RDYnBSY = 1'b0;
        RDYnBSY = 1'b0;
        global_protect = 1'b0;
        global_protect = 1'b0;
        global_unprotect = 1'b0;
        global_unprotect = 1'b0;
        WEL = 1'b0;
        WEL = 1'b0;
        $display ("Write Status Register operation completed");
        $display ("Write Status Register operation completed");
end
end
 
 
// ************ Write Enable ******************** //
// ************ Write Enable ******************** //
always @(WE)
always @(WE)
begin : WE_
begin : WE_
   if (freq_error==1'b1)
   if (freq_error==1'b1)
        $display("WARNING: Frequency should be less than %d MHz for SPI interface. Write Enable is not allowed.",fSCK);
        $display("WARNING: Frequency should be less than %d MHz for SPI interface. Write Enable is not allowed.",fSCK);
 
 
        @ (posedge CSB);
        @ (posedge CSB);
        WEL = 1'b1;
        WEL = 1'b1;
        $display ("Write Enable Latch Set");
        $display ("Write Enable Latch Set");
end
end
 
 
// ************ Write Disable ******************** //
// ************ Write Disable ******************** //
always @(WD)
always @(WD)
begin : WD_
begin : WD_
   if (freq_error==1'b1)
   if (freq_error==1'b1)
        $display("WARNING: Frequency should be less than %d MHz for SPI interface. Write Disable is not allowed.",fSCK);
        $display("WARNING: Frequency should be less than %d MHz for SPI interface. Write Disable is not allowed.",fSCK);
 
 
        @ (posedge CSB);
        @ (posedge CSB);
        WEL = 1'b0;
        WEL = 1'b0;
        seq_byte_start = 1'b0;
        seq_byte_start = 1'b0;
        SPM = 1'b0;
        SPM = 1'b0;
        $display ("Write Enable Latch Reset");
        $display ("Write Enable Latch Reset");
end
end
 
 
// ******************** Read Array ********************** //
// ******************** Read Array ********************** //
always @(RA)
always @(RA)
begin : RA_
begin : RA_
        if (RDYnBSY == 1'b1) // device is already busy
        if (RDYnBSY == 1'b1) // device is already busy
        begin
        begin
                $display ("Device %s is busy. Read Array is not allowed", DEVICE);
                $display ("Device %s is busy. Read Array is not allowed", DEVICE);
                disable RA_;
                disable RA_;
        end
        end
        // if it comes here, means, the above if was false.
        // if it comes here, means, the above if was false.
 
 
   if (freq_error==1'b1)
   if (freq_error==1'b1)
        $display ("WARNING: Frequency should be less than %d MHz for SPI interface. Read Array is not allowed",fSCK);
        $display ("WARNING: Frequency should be less than %d MHz for SPI interface. Read Array is not allowed",fSCK);
 
 
        get_data;
        get_data;
        temp_addr [23:16] = read_data [7:0];
        temp_addr [23:16] = read_data [7:0];
        get_data;
        get_data;
        temp_addr [15:8] = read_data [7:0];
        temp_addr [15:8] = read_data [7:0];
        get_data;
        get_data;
        temp_addr [7:0] = read_data [7:0];
        temp_addr [7:0] = read_data [7:0];
        current_address = temp_addr;
        current_address = temp_addr;
 
 
        if(rd_dummy==1'b1 && CSB==1'b0) // for SCK freq, receive 8 dummy bits
        if(rd_dummy==1'b1 && CSB==1'b0) // for SCK freq, receive 8 dummy bits
        begin
        begin
                for (dummy = 7; dummy >= 0; dummy = dummy - 1) // these are dont-care, so discarded
                for (dummy = 7; dummy >= 0; dummy = dummy - 1) // these are dont-care, so discarded
                begin
                begin
                        @(posedge SCK);
                        @(posedge SCK);
                        read_dummy[dummy] = SI;
                        read_dummy[dummy] = SI;
                        if(HOLDB==1'b0)
                        if(HOLDB==1'b0)
                                wait (HOLDB);
                                wait (HOLDB);
                end
                end
                read_dummy = 8'b0;
                read_dummy = 8'b0;
        end
        end
 
 
        read_out_array(current_address); // read continuously from memory untill CSB deasserted
        read_out_array(current_address); // read continuously from memory untill CSB deasserted
        current_address = 24'b0;
        current_address = 24'b0;
end
end
 
 
// ****************** Protect Sector ****************** //
// ****************** Protect Sector ****************** //
always @(PS)
always @(PS)
begin : PS_
begin : PS_
        if (RDYnBSY == 1'b1) // device is already busy
        if (RDYnBSY == 1'b1) // device is already busy
        begin
        begin
                $display ("Device %s is busy. Protect Sector is not allowed", DEVICE);
                $display ("Device %s is busy. Protect Sector is not allowed", DEVICE);
                disable PS_ ;
                disable PS_ ;
        end
        end
        // if it comes here, means, the above if was false.
        // if it comes here, means, the above if was false.
 
 
   if (freq_error==1'b1)
   if (freq_error==1'b1)
        $display ("WARNING: Frequency should be less than %d MHz for SPI interface. Protect Sector is not allowed",fSCK);
        $display ("WARNING: Frequency should be less than %d MHz for SPI interface. Protect Sector is not allowed",fSCK);
 
 
        get_data;
        get_data;
        temp_addr [23:16] = read_data [7:0];
        temp_addr [23:16] = read_data [7:0];
        get_data;
        get_data;
        temp_addr [15:8] = read_data [7:0];
        temp_addr [15:8] = read_data [7:0];
        get_data;
        get_data;
        temp_addr [7:0] = read_data [7:0];
        temp_addr [7:0] = read_data [7:0];
        current_address = temp_addr;
        current_address = temp_addr;
 
 
        @ (posedge CSB);
        @ (posedge CSB);
        if(WEL==1'b1 && SPRL==1'b0)
        if(WEL==1'b1 && SPRL==1'b0)
        begin
        begin
                RDYnBSY = 1'b1;
                RDYnBSY = 1'b1;
                protect = 1'b1;
                protect = 1'b1;
                protect_sector(current_address);
                protect_sector(current_address);
                #tSECP;
                #tSECP;
                $display ("Sector for Address %h is Protected", current_address);
                $display ("Sector for Address %h is Protected", current_address);
        end
        end
        else
        else
                $display ("WEL bit not set or Sector Protection Register Locked. Protect Sector is not allowed");
                $display ("WEL bit not set or Sector Protection Register Locked. Protect Sector is not allowed");
        WEL = 1'b0;
        WEL = 1'b0;
        RDYnBSY = 1'b0;
        RDYnBSY = 1'b0;
        protect = 1'b0;
        protect = 1'b0;
        current_address = 24'b0;
        current_address = 24'b0;
end
end
 
 
// ****************** Un-Protect Sector ****************** //
// ****************** Un-Protect Sector ****************** //
always @(UPS)
always @(UPS)
begin : UPS_
begin : UPS_
        if (RDYnBSY == 1'b1) // device is already busy
        if (RDYnBSY == 1'b1) // device is already busy
        begin
        begin
                $display ("Device %s is busy. Un-Protect Sector is not allowed", DEVICE);
                $display ("Device %s is busy. Un-Protect Sector is not allowed", DEVICE);
                disable UPS_ ;
                disable UPS_ ;
        end
        end
        // if it comes here, means, the above if was false.
        // if it comes here, means, the above if was false.
 
 
   if (freq_error==1'b1)
   if (freq_error==1'b1)
        $display ("WARNING: Frequency should be less than %d MHz for SPI interface. Un-Protect Sector is not allowed",fSCK);
        $display ("WARNING: Frequency should be less than %d MHz for SPI interface. Un-Protect Sector is not allowed",fSCK);
 
 
        get_data;
        get_data;
        temp_addr [23:16] = read_data [7:0];
        temp_addr [23:16] = read_data [7:0];
        get_data;
        get_data;
        temp_addr [15:8] = read_data [7:0];
        temp_addr [15:8] = read_data [7:0];
        get_data;
        get_data;
        temp_addr [7:0] = read_data [7:0];
        temp_addr [7:0] = read_data [7:0];
        current_address = temp_addr;
        current_address = temp_addr;
 
 
        @ (posedge CSB);
        @ (posedge CSB);
        if(WEL==1'b1 && SPRL==1'b0)
        if(WEL==1'b1 && SPRL==1'b0)
        begin
        begin
                RDYnBSY = 1'b1;
                RDYnBSY = 1'b1;
                unprotect = 1'b1;
                unprotect = 1'b1;
                unprotect_sector(current_address);
                unprotect_sector(current_address);
                #tSECUP;
                #tSECUP;
                $display ("Sector for Address %h is UnProtected", current_address);
                $display ("Sector for Address %h is UnProtected", current_address);
        end
        end
        else
        else
                $display ("WEL bit not set or Sector Protection Register Locked. Un-Protect Sector is not allowed");
                $display ("WEL bit not set or Sector Protection Register Locked. Un-Protect Sector is not allowed");
 
 
        unprotect = 1'b0;
        unprotect = 1'b0;
        WEL = 1'b0;
        WEL = 1'b0;
        RDYnBSY = 1'b0;
        RDYnBSY = 1'b0;
        current_address = 24'b0;
        current_address = 24'b0;
end
end
 
 
// ****************** Read Sector Protection Register ****************** //
// ****************** Read Sector Protection Register ****************** //
always @(RSPR)
always @(RSPR)
begin : RSPR_
begin : RSPR_
        if (RDYnBSY == 1'b1) // device is already busy
        if (RDYnBSY == 1'b1) // device is already busy
        begin
        begin
                $display ("Device %s is busy. Read Sector Protection Register is not allowed", DEVICE);
                $display ("Device %s is busy. Read Sector Protection Register is not allowed", DEVICE);
                disable RSPR_ ;
                disable RSPR_ ;
        end
        end
        // if it comes here, means, the above if was false.
        // if it comes here, means, the above if was false.
 
 
   if (freq_error==1'b1)
   if (freq_error==1'b1)
        $display ("WARNING: Frequency should be less than %d MHz for SPI interface. Read Sector Protection Register is not allowed",fSCK);
        $display ("WARNING: Frequency should be less than %d MHz for SPI interface. Read Sector Protection Register is not allowed",fSCK);
 
 
        get_data;
        get_data;
        temp_addr [23:16] = read_data [7:0];
        temp_addr [23:16] = read_data [7:0];
        get_data;
        get_data;
        temp_addr [15:8] = read_data [7:0];
        temp_addr [15:8] = read_data [7:0];
        get_data;
        get_data;
        temp_addr [7:0] = read_data [7:0];
        temp_addr [7:0] = read_data [7:0];
        current_address = temp_addr;
        current_address = temp_addr;
 
 
        check_protection(current_address);
        check_protection(current_address);
        if(protected==1'b1)
        if(protected==1'b1)
                $display ("Sector for Address %h is Protected", current_address);
                $display ("Sector for Address %h is Protected", current_address);
        else
        else
                $display ("Sector for Address %h is UnProtected", current_address);
                $display ("Sector for Address %h is UnProtected", current_address);
 
 
        send_protection_status; // sending the Sector Protection Register content in SO
        send_protection_status; // sending the Sector Protection Register content in SO
        current_address = 24'b0;
        current_address = 24'b0;
        $display ("Read Sector Protection Register completed");
        $display ("Read Sector Protection Register completed");
end
end
 
 
// ********************* Byte Program ********************* //
// ********************* Byte Program ********************* //
always @(BP)
always @(BP)
begin : BP_
begin : BP_
        if (RDYnBSY == 1'b1) // device is already busy
        if (RDYnBSY == 1'b1) // device is already busy
        begin
        begin
                $display ("Device %s is busy. Byte Program is not allowed", DEVICE);
                $display ("Device %s is busy. Byte Program is not allowed", DEVICE);
                disable BP_ ;
                disable BP_ ;
        end
        end
        // if it comes here, means, the above if was false.
        // if it comes here, means, the above if was false.
 
 
   if (freq_error==1'b1)
   if (freq_error==1'b1)
        $display ("WARNING: Frequency should be less than %d MHz for SPI interface. Byte Program is not allowed",fSCK);
        $display ("WARNING: Frequency should be less than %d MHz for SPI interface. Byte Program is not allowed",fSCK);
 
 
        // to receive 3 bytes of address
        // to receive 3 bytes of address
        get_data;
        get_data;
        temp_addr [23:16] = read_data [7:0];
        temp_addr [23:16] = read_data [7:0];
        get_data;
        get_data;
        temp_addr [15:8] = read_data [7:0];
        temp_addr [15:8] = read_data [7:0];
        get_data;
        get_data;
        temp_addr [7:0] = read_data [7:0];
        temp_addr [7:0] = read_data [7:0];
        current_address = temp_addr;
        current_address = temp_addr;
 
 
/*
/*
        // to receive 1 bytes of data
        // to receive 1 bytes of data
        get_data;
        get_data;
        data_in [7:0] = read_data [7:0];
        data_in [7:0] = read_data [7:0];
*/
*/
        page_program;   // page program - receives data
        page_program;   // page program - receives data
 
 
        if(pp_i < 8)    // page program should not proceed if CSB deasserted at intermediate points
        if(pp_i < 8)    // page program should not proceed if CSB deasserted at intermediate points
        begin
        begin
                $display ("Chip Select deasserted in non-even point. Byte/Page Program is not allowed");
                $display ("Chip Select deasserted in non-even point. Byte/Page Program is not allowed");
                WEL = 1'b0;
                WEL = 1'b0;
                EPE = 1'b1;
                EPE = 1'b1;
                disable BP_ ;
                disable BP_ ;
        end
        end
 
 
        if(WEL==1'b0)
        if(WEL==1'b0)
        begin
        begin
                $display ("WEL bit not set. Byte Program is not allowed");
                $display ("WEL bit not set. Byte Program is not allowed");
                disable BP_ ;
                disable BP_ ;
        end
        end
 
 
        check_protection(current_address);
        check_protection(current_address);
 
 
        if(protected==1'b1)
        if(protected==1'b1)
        begin
        begin
                $display("Sector for Address %h is Protected. Byte Program cannot be performed", current_address);
                $display("Sector for Address %h is Protected. Byte Program cannot be performed", current_address);
                WEL = 1'b0;
                WEL = 1'b0;
                disable BP_ ;
                disable BP_ ;
        end
        end
        else
        else
        begin
        begin
                $display ("Sector for Address %h is UnProtected, Byte Program can be performed", current_address);
                $display ("Sector for Address %h is UnProtected, Byte Program can be performed", current_address);
//              @ (posedge CSB);
//              @ (posedge CSB);
                RDYnBSY = 1'b1;
                RDYnBSY = 1'b1;
                byte_prog = 1'b1;
                byte_prog = 1'b1;
                pp_address = current_address[7:0];
                pp_address = current_address[7:0];
 
 
                for(pp = 0; pp < pp_j; pp = pp+1)
                for(pp = 0; pp < pp_j; pp = pp+1)
                begin
                begin
                        data_in = int_buffer[pp];
                        data_in = int_buffer[pp];
                        byte_program({current_address[23:8],pp_address}, data_in);
                        byte_program({current_address[23:8],pp_address}, data_in);
                        pp_address = pp_address + 1'b1;
                        pp_address = pp_address + 1'b1;
                end
                end
                $display("Byte write completed");
                $display("Byte write completed");
 
 
                #tPP;
                #tPP;
                pp               = 0;
                pp               = 0;
                pp_j             = 0;
                pp_j             = 0;
                pp_i             = 0;
                pp_i             = 0;
                WEL              = 1'b0;
                WEL              = 1'b0;
                RDYnBSY          = 1'b0;
                RDYnBSY          = 1'b0;
                byte_prog        = 1'b0;
                byte_prog        = 1'b0;
                current_address  = 24'b0;
                current_address  = 24'b0;
                pp_address       = 8'b0;
                pp_address       = 8'b0;
                data_in          = 8'b0;
                data_in          = 8'b0;
        end
        end
end
end
 
 
// ********************* Sequential Byte Program ********************* //
// ********************* Sequential Byte Program ********************* //
always @(SBP)
always @(SBP)
begin : SBP_
begin : SBP_
        if (RDYnBSY == 1'b1) // device is already busy
        if (RDYnBSY == 1'b1) // device is already busy
        begin
        begin
                $display ("Device %s is busy. Sequential Byte Program is not allowed", DEVICE);
                $display ("Device %s is busy. Sequential Byte Program is not allowed", DEVICE);
                disable SBP_ ;
                disable SBP_ ;
        end
        end
        // if it comes here, means, the above if was false.
        // if it comes here, means, the above if was false.
 
 
   if (freq_error==1'b1)
   if (freq_error==1'b1)
        $display ("WARNING: Frequency should be less than %d MHz for SPI interface. Sequential Byte Program is not allowed",fSCK);
        $display ("WARNING: Frequency should be less than %d MHz for SPI interface. Sequential Byte Program is not allowed",fSCK);
 
 
        if(seq_byte_start==1'b0)
        if(seq_byte_start==1'b0)
        begin
        begin
        // to receive 3 bytes of address
        // to receive 3 bytes of address
                get_data;
                get_data;
                temp_addr [23:16] = read_data [7:0];
                temp_addr [23:16] = read_data [7:0];
                get_data;
                get_data;
                temp_addr [15:8] = read_data [7:0];
                temp_addr [15:8] = read_data [7:0];
                get_data;
                get_data;
                temp_addr [7:0] = read_data [7:0];
                temp_addr [7:0] = read_data [7:0];
                current_address = temp_addr;
                current_address = temp_addr;
        end
        end
 
 
        SPM = 1'b1;     // Entered Sequential Byte Programing mode
        SPM = 1'b1;     // Entered Sequential Byte Programing mode
 
 
        if(seq_byte_start==1'b1)        // to increment address when entering sequential mode
        if(seq_byte_start==1'b1)        // to increment address when entering sequential mode
        begin
        begin
                if(current_address < (MEMSIZE-1))
                if(current_address < (MEMSIZE-1))
                        current_address = current_address + 1'b1;
                        current_address = current_address + 1'b1;
                else if(current_address == (MEMSIZE-1))
                else if(current_address == (MEMSIZE-1))
                begin
                begin
                        $display ("Sequential Byte Program for device: %s reaches end of memory (%h). No Wrapping allowed. Sequential Byte Program is not allowed", DEVICE, MEMSIZE-1);
                        $display ("Sequential Byte Program for device: %s reaches end of memory (%h). No Wrapping allowed. Sequential Byte Program is not allowed", DEVICE, MEMSIZE-1);
                        WEL = 1'b0;
                        WEL = 1'b0;
                        SPM = 1'b0;
                        SPM = 1'b0;
                        seq_byte_start = 1'b0;
                        seq_byte_start = 1'b0;
                        disable SBP_ ;
                        disable SBP_ ;
                end
                end
        end
        end
 
 
        // to receive 1 bytes of data
        // to receive 1 bytes of data
        get_data;
        get_data;
        data_in [7:0] = read_data [7:0];
        data_in [7:0] = read_data [7:0];
 
 
        if(WEL==1'b0)
        if(WEL==1'b0)
        begin
        begin
                $display ("WEL bit not set. Sequential Byte Program is not allowed");
                $display ("WEL bit not set. Sequential Byte Program is not allowed");
                seq_byte_start = 1'b0;
                seq_byte_start = 1'b0;
                SPM = 1'b0;
                SPM = 1'b0;
                disable SBP_ ;
                disable SBP_ ;
        end
        end
 
 
        check_protection(current_address);
        check_protection(current_address);
 
 
        if(protected==1'b1)
        if(protected==1'b1)
        begin
        begin
                $display("Sector for Address %h is Protected. Sequential Byte Program cannot be performed", current_address);
                $display("Sector for Address %h is Protected. Sequential Byte Program cannot be performed", current_address);
                seq_byte_start = 1'b0;
                seq_byte_start = 1'b0;
                WEL = 1'b0;
                WEL = 1'b0;
                SPM = 1'b0;
                SPM = 1'b0;
                disable SBP_ ;
                disable SBP_ ;
        end
        end
        else
        else
        begin
        begin
                $display ("Sector for Address %h is UnProtected, Sequential Byte Program can be performed", current_address);
                $display ("Sector for Address %h is UnProtected, Sequential Byte Program can be performed", current_address);
                @ (posedge CSB);
                @ (posedge CSB);
                RDYnBSY = 1'b1;
                RDYnBSY = 1'b1;
                seq_byte_prog = 1'b1;
                seq_byte_prog = 1'b1;
 
 
                byte_program(current_address, data_in);
                byte_program(current_address, data_in);
                $display("Sequential Byte write completed");
                $display("Sequential Byte write completed");
 
 
                #tBP;
                #tBP;
                RDYnBSY = 1'b0;
                RDYnBSY = 1'b0;
                seq_byte_prog = 1'b0;
                seq_byte_prog = 1'b0;
                data_in = 8'b0;
                data_in = 8'b0;
                seq_byte_start = 1'b1;
                seq_byte_start = 1'b1;
        end
        end
end
end
 
 
// ********************* 4kB Block Erase ********************* //
// ********************* 4kB Block Erase ********************* //
always @(BE4)
always @(BE4)
begin : BE4_
begin : BE4_
        if (RDYnBSY == 1'b1) // device is already busy
        if (RDYnBSY == 1'b1) // device is already busy
        begin
        begin
                $display ("Device %s is busy. 4KB Block Erase is not allowed", DEVICE);
                $display ("Device %s is busy. 4KB Block Erase is not allowed", DEVICE);
                disable BE4_ ;
                disable BE4_ ;
        end
        end
        // if it comes here, means, the above if was false.
        // if it comes here, means, the above if was false.
 
 
   if (freq_error==1'b1)
   if (freq_error==1'b1)
        $display ("WARNING: Frequency should be less than %d MHz for SPI interface. 4KB Block Erase is not allowed",fSCK);
        $display ("WARNING: Frequency should be less than %d MHz for SPI interface. 4KB Block Erase is not allowed",fSCK);
 
 
        get_data;
        get_data;
        temp_addr [23:16] = read_data [7:0];
        temp_addr [23:16] = read_data [7:0];
        get_data;
        get_data;
        temp_addr [15:8] = read_data [7:0];
        temp_addr [15:8] = read_data [7:0];
        get_data;
        get_data;
        temp_addr [7:0] = read_data [7:0];
        temp_addr [7:0] = read_data [7:0];
        current_address = temp_addr;
        current_address = temp_addr;
 
 
        if(WEL==1'b0)
        if(WEL==1'b0)
        begin
        begin
                $display ("WEL bit not set. 4KB Block Erase is not allowed");
                $display ("WEL bit not set. 4KB Block Erase is not allowed");
                disable BE4_ ;
                disable BE4_ ;
        end
        end
 
 
        check_protection(current_address);
        check_protection(current_address);
 
 
        if(protected==1'b1)
        if(protected==1'b1)
        begin
        begin
                $display("Sector for Address %h is Protected. 4KB Block Erase cannot be performed", current_address);
                $display("Sector for Address %h is Protected. 4KB Block Erase cannot be performed", current_address);
                WEL = 1'b0;
                WEL = 1'b0;
                disable BE4_ ;
                disable BE4_ ;
        end
        end
        else
        else
        begin
        begin
                $display ("Sector for Address %h is UnProtected, 4KB Block Erase can be performed", current_address);
                $display ("Sector for Address %h is UnProtected, 4KB Block Erase can be performed", current_address);
                @ (posedge CSB);
                @ (posedge CSB);
                RDYnBSY = 1'b1;
                RDYnBSY = 1'b1;
                erasing_block4 = 1'b1;
                erasing_block4 = 1'b1;
 
 
                erase_4kb(current_address);
                erase_4kb(current_address);
                #tBLKE4;
                #tBLKE4;
 
 
                $display ("4kB Block with start address %h erased", {current_address[23:12],12'b0});
                $display ("4kB Block with start address %h erased", {current_address[23:12],12'b0});
                WEL = 1'b0;
                WEL = 1'b0;
                RDYnBSY = 1'b0;
                RDYnBSY = 1'b0;
                current_address = 24'b0;
                current_address = 24'b0;
                erasing_block4 = 1'b0;
                erasing_block4 = 1'b0;
        end
        end
end
end
 
 
// ********************* 32kB Block Erase ********************* //
// ********************* 32kB Block Erase ********************* //
always @(BE32)
always @(BE32)
begin : BE32_
begin : BE32_
        if (RDYnBSY == 1'b1) // device is already busy
        if (RDYnBSY == 1'b1) // device is already busy
        begin
        begin
                $display ("Device %s is busy. 32KB Block Erase is not allowed", DEVICE);
                $display ("Device %s is busy. 32KB Block Erase is not allowed", DEVICE);
                disable BE32_ ;
                disable BE32_ ;
        end
        end
        // if it comes here, means, the above if was false.
        // if it comes here, means, the above if was false.
 
 
   if (freq_error==1'b1)
   if (freq_error==1'b1)
        $display ("WARNING: Frequency should be less than %d MHz for SPI interface. 32KB Block Erase is not allowed",fSCK);
        $display ("WARNING: Frequency should be less than %d MHz for SPI interface. 32KB Block Erase is not allowed",fSCK);
 
 
        get_data;
        get_data;
        temp_addr [23:16] = read_data [7:0];
        temp_addr [23:16] = read_data [7:0];
        get_data;
        get_data;
        temp_addr [15:8] = read_data [7:0];
        temp_addr [15:8] = read_data [7:0];
        get_data;
        get_data;
        temp_addr [7:0] = read_data [7:0];
        temp_addr [7:0] = read_data [7:0];
        current_address = temp_addr;
        current_address = temp_addr;
 
 
        if(WEL==1'b0)
        if(WEL==1'b0)
        begin
        begin
                $display ("WEL bit not set. 32KB Block Erase is not allowed");
                $display ("WEL bit not set. 32KB Block Erase is not allowed");
                disable BE32_ ;
                disable BE32_ ;
        end
        end
 
 
        if(DEVICE == "AT25DF041A" && current_address[23:15]==9'b0000_0111_1)
        if(DEVICE == "AT25DF041A" && current_address[23:15]==9'b0000_0111_1)
        begin
        begin
                check_protection({current_address[23:16],2'b11,14'b0});
                check_protection({current_address[23:16],2'b11,14'b0});
                if(protected==1'b1)
                if(protected==1'b1)
                begin
                begin
                        $display("Sector for Address %h is Protected. 32KB Block Erase cannot be performed", {current_address[23:16],2'b11,14'b0});
                        $display("Sector for Address %h is Protected. 32KB Block Erase cannot be performed", {current_address[23:16],2'b11,14'b0});
                        EPE = 1'b1;
                        EPE = 1'b1;
                        WEL = 1'b0;
                        WEL = 1'b0;
                        disable BE32_ ;
                        disable BE32_ ;
                end
                end
                check_protection({current_address[23:16],3'b100,13'b0});
                check_protection({current_address[23:16],3'b100,13'b0});
                if(protected==1'b1)
                if(protected==1'b1)
                begin
                begin
                        $display("Sector for Address %h is Protected. 32KB Block Erase cannot be performed", {current_address[23:16],3'b100,13'b0});
                        $display("Sector for Address %h is Protected. 32KB Block Erase cannot be performed", {current_address[23:16],3'b100,13'b0});
                        EPE = 1'b1;
                        EPE = 1'b1;
                        WEL = 1'b0;
                        WEL = 1'b0;
                        disable BE32_ ;
                        disable BE32_ ;
                end
                end
                check_protection({current_address[23:16],3'b101,13'b0});
                check_protection({current_address[23:16],3'b101,13'b0});
                if(protected==1'b1)
                if(protected==1'b1)
                begin
                begin
                        $display("Sector for Address %h is Protected. 32KB Block Erase cannot be performed", {current_address[23:16],3'b101,13'b0});
                        $display("Sector for Address %h is Protected. 32KB Block Erase cannot be performed", {current_address[23:16],3'b101,13'b0});
                        EPE = 1'b1;
                        EPE = 1'b1;
                        WEL = 1'b0;
                        WEL = 1'b0;
                        disable BE32_ ;
                        disable BE32_ ;
                end
                end
        end
        end
        else if(DEVICE == "AT26DF081A" && current_address[23:15]==9'b0000_1111_0)
        else if(DEVICE == "AT26DF081A" && current_address[23:15]==9'b0000_1111_0)
        begin
        begin
                check_protection({current_address[23:16],2'b00,15'b0});
                check_protection({current_address[23:16],2'b00,15'b0});
                if(protected==1'b1)
                if(protected==1'b1)
                begin
                begin
                        $display("Sector for Address %h is Protected. 32KB Block Erase cannot be performed", {current_address[23:16],2'b00,15'b0});
                        $display("Sector for Address %h is Protected. 32KB Block Erase cannot be performed", {current_address[23:16],2'b00,15'b0});
                        EPE = 1'b1;
                        EPE = 1'b1;
                        WEL = 1'b0;
                        WEL = 1'b0;
                        disable BE32_ ;
                        disable BE32_ ;
                end
                end
                check_protection({current_address[23:16],3'b010,13'b0});
                check_protection({current_address[23:16],3'b010,13'b0});
                if(protected==1'b1)
                if(protected==1'b1)
                begin
                begin
                        $display("Sector for Address %h is Protected. 32KB Block Erase cannot be performed", {current_address[23:16],3'b010,13'b0});
                        $display("Sector for Address %h is Protected. 32KB Block Erase cannot be performed", {current_address[23:16],3'b010,13'b0});
                        EPE = 1'b1;
                        EPE = 1'b1;
                        WEL = 1'b0;
                        WEL = 1'b0;
                        disable BE32_ ;
                        disable BE32_ ;
                end
                end
                check_protection({current_address[23:16],3'b011,13'b0});
                check_protection({current_address[23:16],3'b011,13'b0});
                if(protected==1'b1)
                if(protected==1'b1)
                begin
                begin
                        $display("Sector for Address %h is Protected. 32KB Block Erase cannot be performed", {current_address[23:16],3'b011,13'b0});
                        $display("Sector for Address %h is Protected. 32KB Block Erase cannot be performed", {current_address[23:16],3'b011,13'b0});
                        EPE = 1'b1;
                        EPE = 1'b1;
                        WEL = 1'b0;
                        WEL = 1'b0;
                        disable BE32_ ;
                        disable BE32_ ;
                end
                end
        end
        end
        else
        else
                check_protection(current_address);
                check_protection(current_address);
 
 
        if(protected==1'b1)
        if(protected==1'b1)
        begin
        begin
                $display("Sector for Address %h is Protected. 32KB Block Erase cannot be performed", current_address);
                $display("Sector for Address %h is Protected. 32KB Block Erase cannot be performed", current_address);
                WEL = 1'b0;
                WEL = 1'b0;
                disable BE32_ ;
                disable BE32_ ;
        end
        end
        else
        else
        begin
        begin
                $display ("Sector for Address %h is UnProtected, 32KB Block Erase can be performed", current_address);
                $display ("Sector for Address %h is UnProtected, 32KB Block Erase can be performed", current_address);
                @ (posedge CSB);
                @ (posedge CSB);
                RDYnBSY = 1'b1;
                RDYnBSY = 1'b1;
                erasing_block32 = 1'b1;
                erasing_block32 = 1'b1;
 
 
                erase_32kb(current_address);
                erase_32kb(current_address);
                #tBLKE32;
                #tBLKE32;
 
 
                $display ("32KB Block with start address %h erased", {current_address[23:15],15'b0});
                $display ("32KB Block with start address %h erased", {current_address[23:15],15'b0});
                WEL = 1'b0;
                WEL = 1'b0;
                RDYnBSY = 1'b0;
                RDYnBSY = 1'b0;
                current_address = 24'b0;
                current_address = 24'b0;
                erasing_block32 = 1'b0;
                erasing_block32 = 1'b0;
        end
        end
end
end
 
 
// ********************* 64kB Block Erase ********************* //
// ********************* 64kB Block Erase ********************* //
always @(BE64)
always @(BE64)
begin : BE64_
begin : BE64_
        if (RDYnBSY == 1'b1) // device is already busy
        if (RDYnBSY == 1'b1) // device is already busy
        begin
        begin
                $display ("Device %s is busy. 64KB Block Erase is not allowed", DEVICE);
                $display ("Device %s is busy. 64KB Block Erase is not allowed", DEVICE);
                disable BE64_ ;
                disable BE64_ ;
        end
        end
        // if it comes here, means, the above if was false.
        // if it comes here, means, the above if was false.
 
 
   if (freq_error==1'b1)
   if (freq_error==1'b1)
        $display ("WARNING: Frequency should be less than %d MHz for SPI interface. 64KB Block Erase is not allowed",fSCK);
        $display ("WARNING: Frequency should be less than %d MHz for SPI interface. 64KB Block Erase is not allowed",fSCK);
 
 
        get_data;
        get_data;
        temp_addr [23:16] = read_data [7:0];
        temp_addr [23:16] = read_data [7:0];
        get_data;
        get_data;
        temp_addr [15:8] = read_data [7:0];
        temp_addr [15:8] = read_data [7:0];
        get_data;
        get_data;
        temp_addr [7:0] = read_data [7:0];
        temp_addr [7:0] = read_data [7:0];
        current_address = temp_addr;
        current_address = temp_addr;
 
 
        if(WEL==1'b0)
        if(WEL==1'b0)
        begin
        begin
                $display ("WEL bit not set. 64KB Block Erase is not allowed");
                $display ("WEL bit not set. 64KB Block Erase is not allowed");
                disable BE64_ ;
                disable BE64_ ;
        end
        end
 
 
        if(DEVICE == "AT25DF041A" && current_address[23:16]==8'b0000_0111)
        if(DEVICE == "AT25DF041A" && current_address[23:16]==8'b0000_0111)
        begin
        begin
                check_protection({current_address[23:16],1'b0,15'b0});
                check_protection({current_address[23:16],1'b0,15'b0});
                if(protected==1'b1)
                if(protected==1'b1)
                begin
                begin
                        $display("Sector for Address %h is Protected. 64KB Block Erase cannot be performed", {current_address[23:16],1'b0,15'b0});
                        $display("Sector for Address %h is Protected. 64KB Block Erase cannot be performed", {current_address[23:16],1'b0,15'b0});
                        EPE = 1'b1;
                        EPE = 1'b1;
                        WEL = 1'b0;
                        WEL = 1'b0;
                        disable BE64_ ;
                        disable BE64_ ;
                end
                end
                check_protection({current_address[23:16],2'b11,14'b0});
                check_protection({current_address[23:16],2'b11,14'b0});
                if(protected==1'b1)
                if(protected==1'b1)
                begin
                begin
                        $display("Sector for Address %h is Protected. 64KB Block Erase cannot be performed", {current_address[23:16],2'b11,14'b0});
                        $display("Sector for Address %h is Protected. 64KB Block Erase cannot be performed", {current_address[23:16],2'b11,14'b0});
                        EPE = 1'b1;
                        EPE = 1'b1;
                        WEL = 1'b0;
                        WEL = 1'b0;
                        disable BE64_ ;
                        disable BE64_ ;
                end
                end
                check_protection({current_address[23:16],3'b100,13'b0});
                check_protection({current_address[23:16],3'b100,13'b0});
                if(protected==1'b1)
                if(protected==1'b1)
                begin
                begin
                        $display("Sector for Address %h is Protected. 64KB Block Erase cannot be performed", {current_address[23:16],3'b100,13'b0});
                        $display("Sector for Address %h is Protected. 64KB Block Erase cannot be performed", {current_address[23:16],3'b100,13'b0});
                        EPE = 1'b1;
                        EPE = 1'b1;
                        WEL = 1'b0;
                        WEL = 1'b0;
                        disable BE64_ ;
                        disable BE64_ ;
                end
                end
                check_protection({current_address[23:16],3'b101,13'b0});
                check_protection({current_address[23:16],3'b101,13'b0});
                if(protected==1'b1)
                if(protected==1'b1)
                begin
                begin
                        $display("Sector for Address %h is Protected. 64KB Block Erase cannot be performed", {current_address[23:16],3'b101,13'b0});
                        $display("Sector for Address %h is Protected. 64KB Block Erase cannot be performed", {current_address[23:16],3'b101,13'b0});
                        EPE = 1'b1;
                        EPE = 1'b1;
                        WEL = 1'b0;
                        WEL = 1'b0;
                        disable BE64_ ;
                        disable BE64_ ;
                end
                end
        end
        end
        else if(DEVICE == "AT26DF081A" && current_address[23:16]==8'b0000_1111)
        else if(DEVICE == "AT26DF081A" && current_address[23:16]==8'b0000_1111)
        begin
        begin
                check_protection({current_address[23:16],2'b00,15'b0});
                check_protection({current_address[23:16],2'b00,15'b0});
                if(protected==1'b1)
                if(protected==1'b1)
                begin
                begin
                        $display("Sector for Address %h is Protected. 64KB Block Erase cannot be performed", {current_address[23:16],2'b00,15'b0});
                        $display("Sector for Address %h is Protected. 64KB Block Erase cannot be performed", {current_address[23:16],2'b00,15'b0});
                        EPE = 1'b1;
                        EPE = 1'b1;
                        WEL = 1'b0;
                        WEL = 1'b0;
                        disable BE64_ ;
                        disable BE64_ ;
                end
                end
                check_protection({current_address[23:16],1'b1,14'b0});
                check_protection({current_address[23:16],1'b1,14'b0});
                if(protected==1'b1)
                if(protected==1'b1)
                begin
                begin
                        $display("Sector for Address %h is Protected. 64KB Block Erase cannot be performed", {current_address[23:16],1'b1,14'b0});
                        $display("Sector for Address %h is Protected. 64KB Block Erase cannot be performed", {current_address[23:16],1'b1,14'b0});
                        EPE = 1'b1;
                        EPE = 1'b1;
                        WEL = 1'b0;
                        WEL = 1'b0;
                        disable BE64_ ;
                        disable BE64_ ;
                end
                end
                check_protection({current_address[23:16],3'b010,13'b0});
                check_protection({current_address[23:16],3'b010,13'b0});
                if(protected==1'b1)
                if(protected==1'b1)
                begin
                begin
                        $display("Sector for Address %h is Protected. 64KB Block Erase cannot be performed", {current_address[23:16],3'b010,13'b0});
                        $display("Sector for Address %h is Protected. 64KB Block Erase cannot be performed", {current_address[23:16],3'b010,13'b0});
                        EPE = 1'b1;
                        EPE = 1'b1;
                        WEL = 1'b0;
                        WEL = 1'b0;
                        disable BE64_ ;
                        disable BE64_ ;
                end
                end
                check_protection({current_address[23:16],3'b011,13'b0});
                check_protection({current_address[23:16],3'b011,13'b0});
                if(protected==1'b1)
                if(protected==1'b1)
                begin
                begin
                        $display("Sector for Address %h is Protected. 64KB Block Erase cannot be performed", {current_address[23:16],3'b011,13'b0});
                        $display("Sector for Address %h is Protected. 64KB Block Erase cannot be performed", {current_address[23:16],3'b011,13'b0});
                        EPE = 1'b1;
                        EPE = 1'b1;
                        WEL = 1'b0;
                        WEL = 1'b0;
                        disable BE64_ ;
                        disable BE64_ ;
                end
                end
        end
        end
        else
        else
                check_protection(current_address);
                check_protection(current_address);
 
 
        if(protected==1'b1)
        if(protected==1'b1)
        begin
        begin
                $display("Sector for Address %h is Protected. 64KB Block Erase cannot be performed", current_address);
                $display("Sector for Address %h is Protected. 64KB Block Erase cannot be performed", current_address);
                WEL = 1'b0;
                WEL = 1'b0;
                disable BE64_ ;
                disable BE64_ ;
        end
        end
        else
        else
        begin
        begin
                $display ("Sector for Address %h is UnProtected, 64KB Block Erase can be performed", current_address);
                $display ("Sector for Address %h is UnProtected, 64KB Block Erase can be performed", current_address);
                @ (posedge CSB);
                @ (posedge CSB);
                RDYnBSY = 1'b1;
                RDYnBSY = 1'b1;
                erasing_block64 = 1'b1;
                erasing_block64 = 1'b1;
 
 
                erase_64kb(current_address);
                erase_64kb(current_address);
                #tBLKE64;
                #tBLKE64;
 
 
                $display ("64kB Block with start address %h erased", {current_address[23:16],16'b0});
                $display ("64kB Block with start address %h erased", {current_address[23:16],16'b0});
                WEL = 1'b0;
                WEL = 1'b0;
                RDYnBSY = 1'b0;
                RDYnBSY = 1'b0;
                current_address = 24'b0;
                current_address = 24'b0;
                erasing_block64 = 1'b0;
                erasing_block64 = 1'b0;
        end
        end
end
end
 
 
// ********************* Chip Erase ********************* //
// ********************* Chip Erase ********************* //
always @(CE)
always @(CE)
begin : CE_
begin : CE_
        if (RDYnBSY == 1'b1) // device is already busy
        if (RDYnBSY == 1'b1) // device is already busy
        begin
        begin
                $display ("Device %s is busy. Chip Erase is not allowed", DEVICE);
                $display ("Device %s is busy. Chip Erase is not allowed", DEVICE);
                disable CE_ ;
                disable CE_ ;
        end
        end
        // if it comes here, means, the above if was false.
        // if it comes here, means, the above if was false.
 
 
   if (freq_error==1'b1)
   if (freq_error==1'b1)
        $display ("WARNING: Frequency should be less than %d MHz for SPI interface. Chip Erase is not allowed",fSCK);
        $display ("WARNING: Frequency should be less than %d MHz for SPI interface. Chip Erase is not allowed",fSCK);
 
 
        if(WEL==1'b0)
        if(WEL==1'b0)
        begin
        begin
                $display ("WEL bit not set. Chip Erase is not allowed");
                $display ("WEL bit not set. Chip Erase is not allowed");
                disable CE_ ;
                disable CE_ ;
        end
        end
 
 
        if(SWP==2'b00)
        if(SWP==2'b00)
        begin
        begin
                $display("All Sectors in the chip are UnProtected. Chip Erase can be performed");
                $display("All Sectors in the chip are UnProtected. Chip Erase can be performed");
                @ (posedge CSB);
                @ (posedge CSB);
                RDYnBSY = 1'b1;
                RDYnBSY = 1'b1;
                erasing_chip = 1'b1;
                erasing_chip = 1'b1;
 
 
                erase_chip;
                erase_chip;
                for(delay =0; delay < tmult; delay = delay+1)
                for(delay =0; delay < tmult; delay = delay+1)
                        #tCHPEn;
                        #tCHPEn;
 
 
                WEL = 1'b0;
                WEL = 1'b0;
                RDYnBSY = 1'b0;
                RDYnBSY = 1'b0;
                erasing_chip = 1'b0;
                erasing_chip = 1'b0;
        end
        end
        else if(SWP!=2'b00)
        else if(SWP!=2'b00)
        begin
        begin
                $display("Some or all Sectors in the chip is Protected. Chip Erase cannot be performed");
                $display("Some or all Sectors in the chip is Protected. Chip Erase cannot be performed");
                EPE = 1'b1;
                EPE = 1'b1;
                disable CE_ ;
                disable CE_ ;
        end
        end
 
 
        $display ("Chip Erase Completed");
        $display ("Chip Erase Completed");
end
end
 
 
// ******** Posedge CSB. Stop all reading, recvng. commands/addresses etc. ********* //
// ******** Posedge CSB. Stop all reading, recvng. commands/addresses etc. ********* //
 
 
always @(posedge CSB)
always @(posedge CSB)
begin
begin
        disable RA_;    // Read Array (low freq and normal freq)
        disable RA_;    // Read Array (low freq and normal freq)
        disable MIR_;   // MIR will stop, if CSB goes high
        disable MIR_;   // MIR will stop, if CSB goes high
        disable RSR_;   // Status reading should stop.
        disable RSR_;   // Status reading should stop.
 
 
        disable get_opcode;             // Stop opcode retrieval
        disable get_opcode;             // Stop opcode retrieval
        disable get_data;               // Stop address/data retrieval
        disable get_data;               // Stop address/data retrieval
        disable read_out_array;         // send data in SO
        disable read_out_array;         // send data in SO
        disable send_protection_status; // Send Protection status
        disable send_protection_status; // Send Protection status
        disable page_program;
        disable page_program;
 
 
        temp_data = 8'b0;
        temp_data = 8'b0;
        temp_addr = 24'b0;
        temp_addr = 24'b0;
        rd_dummy = 1'b0;
        rd_dummy = 1'b0;
        status_read = 1'b0;
        status_read = 1'b0;
        protected = 1'b0;
        protected = 1'b0;
        read_data = 8'b0;
        read_data = 8'b0;
        EPE = 1'b0;
        EPE = 1'b0;
 
 
        #tDIS SO_on = 1'b0;  // SO is now in high-impedance
        #tDIS SO_on = 1'b0;  // SO is now in high-impedance
        SO_reg = 1'b0;
        SO_reg = 1'b0;
end
end
 
 
// ********************** Frequeny Checking ********************** //
// ********************** Frequeny Checking ********************** //
always @ (negedge SCK)
always @ (negedge SCK)
begin
begin
        tperiod <= $time;
        tperiod <= $time;
        tperiod1 <= tperiod;
        tperiod1 <= tperiod;
end
end
 
 
//always @(tperiod or tperiod1)
//always @(tperiod or tperiod1)
always @(negedge SCK)
always @(negedge SCK)
begin
begin
        if ((tperiod - tperiod1) < (tSCKH + tSCKH))
        if ((tperiod - tperiod1) < (tSCKH + tSCKH))
        begin
        begin
                //freq_error = 1'b1;
                //freq_error = 1'b1;
                //$display("Frequency exceeds max limit (14285 ms). Time period detected is %t", (tperiod -tperiod1)); 
                //$display("Frequency exceeds max limit (14285 ms). Time period detected is %t", (tperiod -tperiod1)); 
        end
        end
        else
        else
                freq_error = 1'b0;
                freq_error = 1'b0;
 
 
end // always
end // always
// **************************************************************** //
// **************************************************************** //
 
 
endmodule
endmodule
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.