//--------------------------------------------------------------------
// Created by Actel SmartDesign Mon Nov 19 10:28:00 2012
// Parameters for COREABC
//--------------------------------------------------------------------


parameter ABCCODE = "DEF IAP_IR              0
DEF IAP_DR2             4
DEF IAP_DR5             12
DEF IAP_DR16            20
DEF IAP_DR26            16
DEF IAP_DR              24
DEF IAP_DR_LENGTH       28
DEF IAP_TAP_NEW_STATE   32
    
    
// JTAG instructions
DEF ISC_ENABLE          0x80
DEF ISC_PROGRAM         0x83
DEF ISC_NOOP            0x84
DEF ISC_ERASE           0x85
DEF ISC_INCREMENT       0x87
DEF ISC_DATA_SHIFT      0x89
DEF ISC_VERIFY0         0x8D
DEF ISC_VERIFY1         0x8E
    
//Device Specific constants
//Device specific constants here and in
//do_bulk_program procedure
//Target device is AFS600
    
DEF numberofsdtiles  0x06
DEF numberofmaprows  0x0D74
    
//Time delay constants (time div  tck freq)
//Change these based on your tck frequency
   
//For 20MHz clock
// These loops are effectively 3.33MHz (clk/6)
DEF usec38   0x007F
DEF usec138  0x01CC
DEF usec165  0x0226
DEF usec256  0x0355
DEF usec1500 0x1383
DEF sec1p5h  0x004C 
DEF sec1p5l  0x37B8 
    
//These loops are 2.22MHz eff (clk/9)
DEF usec20   0x002C   
DEF usec100  0x00DE
DEF usec200  0x01BC
DEF usec264  0x024A

    
//-------------------------------------
//ACTION BULK PROGRAM
//-------------------------------------
// WAIT RESET, 5 CYCLES
    APBWRT DAT8     1 IAP_TAP_NEW_STATE 0x005F
    
    IOWRT DAT8 0x01
    CALL $initialize
    IOWRT DAT8 0x02    
    CALL $do_enc_program_from
    IOWRT DAT8 0x04   
    CALL $do_bulk_program   
    IOWRT DAT8 0x05
    CALL $do_erase_disturb
    IOWRT DAT8 0x06
    CALL $initialize
    IOWRT DAT8 0x07
    CALL $do_bulk_program   
    IOWRT DAT8 0x08
    CALL $do_enc_program_from    
    IOWRT DAT8 0x0A
    CALL $do_erase_disturb
    IOWRT DAT8 0x0B
    CALL $initialize
    IOWRT DAT8 0x0C
    CALL $do_bulk_program   
    IOWRT DAT8 0x0D
    CALL $do_enc_program_from
    
    IOWRT DAT8 0x0F
    JUMP $do_exit
    
//-------------------------------------
//do_initialize procedure
//-------------------------------------
$initialize
    APBWRT DAT8  1 IAP_IR 0xB1
    APBWRT DAT8  1 IAP_DR_LENGTH 12
    APBWRT DAT16 1 IAP_DR 0x0000 
    
    APBWRT DAT8  1 IAP_IR 0x80
    APBWRT DAT8  1 IAP_DR_LENGTH 18
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT8   1 IAP_DR 0x00
    
    APBWRT DAT8  1 IAP_TAP_NEW_STATE 0x003C
    
//wait 1500 USEC
    LOADZ DAT16 usec1500
    
$do_idly1
    
    DECZ
    JUMP IFNOT ZZERO $do_idly1
    
    APBWRT DAT8 1 IAP_IR 0x80
    APBREAD 1 IAP_DR5
    AND 0x2300
    CMP 0x0100      
    JUMP IF ZERO $busyok
//light error LED for Debug
    IOREAD
    OR DAT8 0x80
    IOWRT ACC
    
    JUMP $do_exit
    
$busyok
    APBWRT DAT8  1 IAP_DR_LENGTH 18
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT8  1 IAP_DR 0x00
    APBREAD 1 IAP_DR5
    AND 0xC000
    CMP 0xC000      
    JUMP IF ZERO $enableok
//light error LED for Debug
    IOREAD
    OR DAT8 0x80
    IOWRT ACC
    
    JUMP $do_exit
    
$enableok
    APBWRT DAT8  1 IAP_TAP_NEW_STATE 0x003C
    
//wait 1500 USEC
    LOADZ DAT16 usec1500
    
$do_idly2
    DECZ
    JUMP IFNOT ZZERO $do_idly2
    
    RETURN
    
//-------------------------------------
//do_bulk_program procedure
//-------------------------------------
$do_bulk_program
//Device specific data Selects Tiles and Rows
//tsreg values for AFS600  
//This does 'drscan tiles tseg[]' STAPL line
//ITS 
    APBWRT DAT8  1 IAP_IR 0xC2  
//AFS600 SEG 0 LSdata then MSdata
    APBWRT DAT8  1 IAP_DR_LENGTH 82
    APBWRT DAT16 1 IAP_DR 0x0000 
    APBWRT DAT16 1 IAP_DR 0x0000 
    APBWRT DAT16 1 IAP_DR 0x0000 
    APBWRT DAT16 1 IAP_DR 0x0000 
    APBWRT DAT16 1 IAP_DR 0x0000 
    APBWRT DAT16 1 IAP_DR 0x0003 
    CALL $bulk_prog
//ITS
    APBWRT DAT8  1 IAP_IR 0xC2
//  AFS600 SEG 1 LSdata then MSdata
    APBWRT DAT8  1 IAP_DR_LENGTH 82
    APBWRT DAT16 1 IAP_DR 0x0000 
    APBWRT DAT16 1 IAP_DR 0x0000 
    APBWRT DAT16 1 IAP_DR 0x0000 
    APBWRT DAT16 1 IAP_DR 0x0000 
    APBWRT DAT16 1 IAP_DR 0x3FFC 
    APBWRT DAT16 1 IAP_DR 0x0000 
    CALL $bulk_prog
//ITS
    APBWRT DAT8  1 IAP_IR 0xC2
//  AFS600 SEG 2 LSdata then MSdata
    APBWRT DAT8  1 IAP_DR_LENGTH 82
    APBWRT DAT16 1 IAP_DR 0x0000 
    APBWRT DAT16 1 IAP_DR 0x0000 
    APBWRT DAT16 1 IAP_DR 0x0000 
    APBWRT DAT16 1 IAP_DR 0xFFC0 
    APBWRT DAT16 1 IAP_DR 0x0000 
    APBWRT DAT16 1 IAP_DR 0x0000 
    CALL $bulk_prog
//ITS
    APBWRT DAT8  1 IAP_IR 0xC2
//  AFS600 SEG 3 LSdata then MSdata
    APBWRT DAT8  1 IAP_DR_LENGTH 82
    APBWRT DAT16 1 IAP_DR 0x0000 
    APBWRT DAT16 1 IAP_DR 0x0000 
    APBWRT DAT16 1 IAP_DR 0xFC00 
    APBWRT DAT16 1 IAP_DR 0x003F 
    APBWRT DAT16 1 IAP_DR 0x0000 
    APBWRT DAT16 1 IAP_DR 0x0000 
    CALL $bulk_prog
//ITS
    APBWRT DAT8  1 IAP_IR 0xC2
//  AFS600 SEG 4 LSdata then MSdata
    APBWRT DAT8  1 IAP_DR_LENGTH 82
    APBWRT DAT16 1 IAP_DR 0x0000 
    APBWRT DAT16 1 IAP_DR 0xF000 
    APBWRT DAT16 1 IAP_DR 0x03FF 
    APBWRT DAT16 1 IAP_DR 0x0000 
    APBWRT DAT16 1 IAP_DR 0x0000 
    APBWRT DAT16 1 IAP_DR 0x0000 
    CALL $bulk_prog
//ITS
    APBWRT DAT8  1 IAP_IR 0xC2
//  AFS600 SEG 5 LSdata then MSdata
    APBWRT DAT8  1 IAP_DR_LENGTH 82
    APBWRT DAT16 1 IAP_DR 0x0000 
    APBWRT DAT16 1 IAP_DR 0x0FFF 
    APBWRT DAT16 1 IAP_DR 0x0000 
    APBWRT DAT16 1 IAP_DR 0x0000 
    APBWRT DAT16 1 IAP_DR 0x0000 
    APBWRT DAT16 1 IAP_DR 0x0000 
    CALL $bulk_prog
//ITS
    APBWRT DAT8  1 IAP_IR 0xC2
//  AFS600 SEG 6 LSdata then MSdata
    APBWRT DAT8  1 IAP_DR_LENGTH 82
    APBWRT DAT16 1 IAP_DR 0xFFF0 
    APBWRT DAT16 1 IAP_DR 0x0000 
    APBWRT DAT16 1 IAP_DR 0x0000 
    APBWRT DAT16 1 IAP_DR 0x0000 
    APBWRT DAT16 1 IAP_DR 0x0000 
    APBWRT DAT16 1 IAP_DR 0x0000 
    CALL $bulk_prog
//ITS
    APBWRT DAT8  1 IAP_IR 0xC2
//  AFS600 SEG 7 LSdata then MSdata
    APBWRT DAT8  1 IAP_DR_LENGTH 82
    APBWRT DAT16 1 IAP_DR 0x000F 
    APBWRT DAT16 1 IAP_DR 0x0000 
    APBWRT DAT16 1 IAP_DR 0x0000 
    APBWRT DAT16 1 IAP_DR 0x0000 
    APBWRT DAT16 1 IAP_DR 0x0000 
    APBWRT DAT16 1 IAP_DR 0x0000 
    CALL $bulk_prog
        
//Bulk_prog procedure done so return to main
//ISC_NOOP
    APBWRT DAT8  1 IAP_IR 0x84
    APBWRT DAT8  1 IAP_TAP_NEW_STATE 0x000C
    RETURN
    
$bulk_prog
//Finish selecting tiles and rows 
//IRM
    APBWRT DAT8  1 IAP_IR 0xC3
    APBWRT DAT8  1 IAP_DR_LENGTH 60
    APBWRT DAT16 1 IAP_DR 0xFFFF 
    APBWRT DAT16 1 IAP_DR 0xFFFF
    APBWRT DAT16 1 IAP_DR 0xFFFF
    APBWRT DAT16 1 IAP_DR 0xFFFF
    
//Invoke Program
//ISC_PROGRAM
    APBWRT DAT8  1 IAP_IR 0x83
    
//Program Exec
    APBWRT DAT8  1 IAP_TAP_NEW_STATE 0x003C
    APBWRT DAT8  1 IAP_IR 0x84
    
//Program Poll    
    APBWRT DAT8  1 IAP_IR 0x84
    
//for loop of 1200
    LOADZ DAT16 0x1201
$do_bfor1
    
    DECZ
    JUMP IF ZZERO $bulk_prog_fail
//TAP=IDLE State
    APBWRT DAT8  1 IAP_TAP_NEW_STATE 0x000C
//100usec delay   
    LOAD DAT16 0x0000
$do_bdly1
    
    INC
    CMP usec100
    JUMP IFNOT ZERO $do_bdly1
    
    APBWRT DAT8  1 IAP_DR_LENGTH 5
    APBWRT DAT8  1 IAP_DR 0x00 
    APBREAD 1 IAP_DR5
    AND 0x1800
    CMP 0x0000      
    JUMP IFNOT ZERO $do_bfor1
    
$progwaitok
    APBWRT DAT8  1 IAP_TAP_NEW_STATE 0x000C
    APBWRT DAT8  1 IAP_DR_LENGTH 5
    APBWRT DAT8  1 IAP_DR 0x00  
//do next tsreg
    RETURN
    
$bulk_prog_fail
//light error LED for Debug
    IOREAD
    OR DAT8 0x80
    IOWRT ACC
    
    JUMP $do_exit  
    
//-------------------------------------
//do_program_rlock procedure
//-------------------------------------
$do_program_rlock
    LOADZ DAT8  numberofsdtiles
$do_prl1
    DECZ
    LOAD  DAT8  0x01
$do_prl2
    APBWRT DAT8  1 IAP_IR 0x89
    APBWRT DAT8  1 IAP_DR_LENGTH 26
    APBWRT DAT16  1 IAP_DR 0x0000 
    APBWRT DAT16  1 IAP_DR 0x0000 
    APBWRT DAT8  1 IAP_TAP_NEW_STATE 0x003C 
    INC
    CMP 0x09
    JUMP IFNOT ZERO $do_prl2
    JUMP IFNOT ZZERO $do_prl1
    
//ISC_PROGRAM_RDLCK
    APBWRT DAT8  1 IAP_IR 0x8C
    APBWRT DAT8  1 IAP_TAP_NEW_STATE 0x003C 
    CALL $poll_program
    JUMP IF ZERO $rlock_ok
    
//light error LED for Debug
    IOREAD
    OR DAT8 0x80
    IOWRT ACC
    
    JUMP $do_exit
    
$rlock_ok
    RETURN
    
//-------------------------------------
//do_erase_disturb procedure
//-------------------------------------
$do_erase_disturb
//ISC_ERASE
    APBWRT DAT8  1 IAP_IR 0x85
    
    APBWRT DAT8  1 IAP_DR_LENGTH 23
//Use these if no UFROM
//    APBWRT DAT16 1 IAP_DR 0x4001
//    APBWRT DAT8  1 IAP_DR 0x00
//Use these if UFROM and to NOT clear UROW
//    APBWRT DAT16 1 IAP_DR 0x8001
//    APBWRT DAT8  1 IAP_DR 0x7F
//Use these if UFROM and clear UROW
    APBWRT DAT16 1 IAP_DR 0xC001
    APBWRT DAT8  1 IAP_DR 0x7F
    
    APBWRT DAT8  1 IAP_TAP_NEW_STATE 0x003C 
    
    LOADZ DAT16 sec1p5h
    LOAD  DAT16 sec1p5l
//Delay 1.5 seconds
$do_eddly1
    
    SUB 0x0001
    JUMP IFNOT ZERO $do_eddly1
    JUMP IF ZZERO $do_eddone
    DECZ
    LOAD  DAT16 0xFFFF
    JUMP $do_eddly1
$do_eddone
    APBWRT DAT8  1 IAP_TAP_NEW_STATE 0x005F
    RETURN 
//-------------------------------------
//do_enc_program_from procedure
//-------------------------------------
$do_enc_program_from
//FROM ROW1
    CALL $init_aes
//Shift in FROM data[127..0]
    APBWRT DAT8  1 IAP_IR 0xA5

    APBWRT DAT8  1 IAP_DR_LENGTH 128
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000

    CALL $epf_dly

//Shift in FROM data[255..128]
    APBWRT DAT8  1 IAP_IR 0xA6

    APBWRT DAT8  1 IAP_DR_LENGTH 128
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000

    APBWRT DAT8  1 IAP_TAP_NEW_STATE 0x005C 

    CALL $poll_program
    JUMP IFNOT ZERO $epf_error
 //FROM ROW 2
    CALL $init_aes
//Shift in FROM data[383..256]
    APBWRT DAT8  1 IAP_IR 0xA5

    APBWRT DAT8  1 IAP_DR_LENGTH 128
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000

    CALL $epf_dly

//Shift in FROM data[511..384]
    APBWRT DAT8  1 IAP_IR 0xA6

    APBWRT DAT8  1 IAP_DR_LENGTH 128
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000

    APBWRT DAT8  1 IAP_TAP_NEW_STATE 0x005C 

    CALL $poll_program
    JUMP IFNOT ZERO $epf_error

//FROM ROW 3
    CALL $init_aes
//Shift in FROM data[639..512]
    APBWRT DAT8  1 IAP_IR 0xA5

    APBWRT DAT8  1 IAP_DR_LENGTH 128
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000

    CALL $epf_dly
 //Shift in FROM data[767..640]
    APBWRT DAT8  1 IAP_IR 0xA6

    APBWRT DAT8  1 IAP_DR_LENGTH 128
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000

    APBWRT DAT8  1 IAP_TAP_NEW_STATE 0x005C 

    CALL $poll_program
    JUMP IFNOT ZERO $epf_error

//FROM ROW 4
    CALL $init_aes
//Shift in FROM data[895..768]
    APBWRT DAT8  1 IAP_IR 0xA5

    APBWRT DAT8  1 IAP_DR_LENGTH 128 
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000

    CALL $epf_dly
//Shift in FROM data[1023..896]
    APBWRT DAT8  1 IAP_IR 0xA6

    APBWRT DAT8  1 IAP_DR_LENGTH 128
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000

    APBWRT DAT8  1 IAP_TAP_NEW_STATE 0x005C 

    CALL $poll_program
    JUMP IFNOT ZERO $epf_error
//FROM ROW 5
    CALL $init_aes
//Shift in FROM data[1151..1024]
    APBWRT DAT8  1 IAP_IR 0xA5

    APBWRT DAT8  1 IAP_DR_LENGTH 128    
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000

    CALL $epf_dly

//Shift in FROM data[1279..1152]
    APBWRT DAT8  1 IAP_IR 0xA6

    APBWRT DAT8  1 IAP_DR_LENGTH 128
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000

    APBWRT DAT8  1 IAP_TAP_NEW_STATE 0x005C 

    CALL $poll_program
    JUMP IFNOT ZERO $epf_error
 //FROM ROW 6
    CALL $init_aes
//Shift in FROM data[1407..1280]
    APBWRT DAT8  1 IAP_IR 0xA5

    APBWRT DAT8  1 IAP_DR_LENGTH 128    
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000

    CALL $epf_dly
//Shift in FROM data[1535..1408]
    APBWRT DAT8  1 IAP_IR 0xA6

    APBWRT DAT8  1 IAP_DR_LENGTH 128
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000

    APBWRT DAT8  1 IAP_TAP_NEW_STATE 0x005C 

    CALL $poll_program
    JUMP IFNOT ZERO $epf_error

//FROM ROW 7
    CALL $init_aes
//Shift in FROM data[1663..1536]
    APBWRT DAT8  1 IAP_IR 0xA5

    APBWRT DAT8  1 IAP_DR_LENGTH 128   
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000

    CALL $epf_dly
//Shift in FROM data[1791..1664]
    APBWRT DAT8  1 IAP_IR 0xA6

    APBWRT DAT8  1 IAP_DR_LENGTH 128
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000

    APBWRT DAT8  1 IAP_TAP_NEW_STATE 0x005C 

    CALL $poll_program
    JUMP IFNOT ZERO $epf_error
//FROM ROW 8
    CALL $init_aes
//Shift in FROM data[1919..1792]
    APBWRT DAT8  1 IAP_IR 0xA5

    APBWRT DAT8  1 IAP_DR_LENGTH 128    
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000

    CALL $epf_dly

//Shift in FROM data[2047..1920]
    APBWRT DAT8  1 IAP_IR 0xA6

    APBWRT DAT8  1 IAP_DR_LENGTH 128
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000

    APBWRT DAT8  1 IAP_TAP_NEW_STATE 0x005C 

    CALL $poll_program
    JUMP IFNOT ZERO $epf_error 
RETURN


// init_aes
$init_aes
    APBWRT DAT8  1 IAP_IR 0xDD

    APBWRT DAT8  1 IAP_DR_LENGTH 128
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    APBWRT DAT16 1 IAP_DR 0x0000
    
    APBWRT DAT8  1 IAP_TAP_NEW_STATE 0x003C 
 
//Delay 165uSec
    LOADZ DAT16 usec165
$iaes_dly
    DECZ
    JUMP IFNOT ZZERO $iaes_dly
    RETURN

//epf_dly
$epf_dly
    APBWRT DAT8  1 IAP_TAP_NEW_STATE 0x003C 

//Delay 38uSec
    LOADZ DAT16 usec38    
$epf_dly1
    DECZ
    JUMP IFNOT ZZERO $epf_dly1
    RETURN

//On Error Exit Procedure 
//Light Error LED for debug
$epf_error
    IOREAD
    OR DAT8 0x80
    IOWRT ACC
    RETURN
    
//-------------------------------------
//poll_program subroutine
//-------------------------------------
$poll_program
    LOADZ DAT16 16381
//Test increase poll time
//    LOADZ DAT16 65000
$poll_pl1
    DECZ
    APBWRT DAT8  1 IAP_IR 0x84
    APBWRT DAT8  1 IAP_TAP_NEW_STATE 0x001C 
    LOAD DAT16 0x0000
$poll_pdly1
    
    INC
    CMP usec100
    JUMP IFNOT ZERO $poll_pdly1
    
    APBWRT DAT8  1 IAP_DR_LENGTH 5
    APBWRT DAT8  1 IAP_DR 0x00 
    APBREAD 1 IAP_DR5
    AND 0x5800
    CMP 0x0000            
    JUMP IF ZERO $poll_pdone
    JUMP IFNOT ZZERO $poll_pl1
$poll_pdone
//The calling function should 
//check  ZERO flag for pass
    RETURN
    
    
//-------------------------------------
//do_exit subroutine
//-------------------------------------
$do_exit
    APBWRT DAT8  1 IAP_IR 0xFF
    APBREAD 1 IAP_DR5
    AND 0x0400
    CMP 0x0400 
    
    APBWRT DAT8  1 IAP_IR 0x81
//Push TAP to IDLE
    APBWRT DAT8  1 IAP_TAP_NEW_STATE 0x000C 
    
    JUMP IF ZERO $do_exjmp
    APBWRT DAT8  1 IAP_IR 0x07
$do_exjmp
//delay 20usec
    LOAD DAT16 0x0000
$do_exdly1
    INC
    CMP usec20
    JUMP IFNOT ZERO $do_exdly1
//Set TAP=RESET state    
    APBWRT DAT8  1 IAP_TAP_NEW_STATE 0x000F 
//Delay 200usec    
    LOAD DAT16 0x0000
$do_exdly2
    
    INC
    CMP usec200
    JUMP IFNOT ZERO $do_exdly2
    HALT
    
    
    
//-------------------------------------
//Array erase verification support procedures
//-------------------------------------
//-------------------------------------
//do_array_verify_0_pass subroutine
//-------------------------------------
$do_array_verify_0_pass
    APBWRT DAT8 1 IAP_IR 0x87
    APBWRT DAT8 1 IAP_DR_LENGTH 2
    APBWRT DAT8 1 IAP_DR 0x2
    
    APBWRT DAT8  1 IAP_TAP_NEW_STATE 0x003C 
    
//Load ACC with numberofmaprows
    LOAD DAT16 numberofmaprows
$do_av0p_loop
//Push ACC because exe_verify and poll_program use ACC
    PUSH ACC
    CALL $exe_verify
    APBWRT DAT8 1 IAP_IR 0x87
    APBWRT DAT8 1 IAP_DR_LENGTH 2
    APBWRT DAT8 1 IAP_DR 0x3
    
    APBWRT DAT8  1 IAP_TAP_NEW_STATE 0x003C 
    
//Check result of $exe_verify
    JUMP IFNOT ZERO $do_av0p_fail
    POP
    SUB 1
    JUMP IFNOT ZERO $do_av0p_loop
    RETURN
    
$do_av0p_fail
//LIGHT ERROR LED
    IOREAD
    OR DAT8 0x80
    IOWRT ACC
    JUMP $do_exit
      
//-------------------------------------
//do_array_verify_0_fail subroutine
//-------------------------------------
$do_array_verify_0_fail
    APBWRT DAT8 1 IAP_IR 0x87
    APBWRT DAT8 1 IAP_DR_LENGTH 2
    APBWRT DAT8 1 IAP_DR 0x2
    
    APBWRT DAT8  1 IAP_TAP_NEW_STATE 0x003C 
    
//Load ACC with numberofmaprows
    LOAD DAT16 numberofmaprows
$do_av0f_loop
//Push ACC because exe_verify and poll_program use ACC
    PUSH ACC
    CALL $exe_verify
    APBWRT DAT8 1 IAP_IR 0x87
    APBWRT DAT8 1 IAP_DR_LENGTH 2
    APBWRT DAT8 1 IAP_DR 0x3
    
    APBWRT DAT8  1 IAP_TAP_NEW_STATE 0x003C 
    
//Check result of $exe_verify
//This is the line that differs from pass
    JUMP IF ZERO $do_av0f_fail
    POP
    SUB 1
    JUMP IFNOT ZERO $do_av0f_loop
    RETURN    
    
$do_av0f_fail
//LIGHT ERROR LED
    IOREAD
    OR DAT8 0x80
    IOWRT ACC
    JUMP $do_exit

//-------------------------------------
//exe_verify subroutine
//-------------------------------------
$exe_verify
    
    APBWRT DAT8 1 IAP_IR 0x8D
    APBWRT DAT8 1 IAP_DR_LENGTH 2
    APBWRT DAT8 1 IAP_DR 0x0
    
    APBWRT DAT8  1 IAP_TAP_NEW_STATE 0x003C 
    
//264usec delay
    LOAD DAT16 0x0000
$ev_dly
    INC
    CMP usec264
    JUMP IFNOT ZERO $ev_dly
    
    CALL $poll_program
    JUMP IF ZERO   $readverstatus
    JUMP $exe_verify_done
    
$readverstatus
    APBWRT DAT8 1 IAP_IR 0x8D
    APBWRT DAT8 1 IAP_DR_LENGTH 2
    APBWRT DAT8 1 IAP_DR 0x0
    APBREAD 1 IAP_DR2
    AND 0xC000
    CMP 0x0000
//This subroutine will return ZERO flag to 
//indicate pass or fail    
$exe_verify_done
    RETURN";
parameter ACT_CALIBRATIONDATA = 1;
parameter APB_AWIDTH = 10;
parameter APB_DWIDTH = 16;
parameter APB_SDEPTH = 3;
parameter CODEHEXDUMP = "";
parameter CODEHEXDUMP2 = "";
parameter DEBUG = 1;
parameter EN_ACM = 0;
parameter EN_ADD = 1;
parameter EN_ALURAM = 0;
parameter EN_AND = 1;
parameter EN_CALL = 1;
parameter EN_DATAM = 2;
parameter EN_INC = 1;
parameter EN_INDIRECT = 0;
parameter EN_INT = 0;
parameter EN_IOREAD = 1;
parameter EN_IOWRT = 1;
parameter EN_MULT = 0;
parameter EN_OR = 1;
parameter EN_PUSH = 1;
parameter EN_RAM = 1;
parameter EN_SHL = 1;
parameter EN_SHR = 1;
parameter EN_XOR = 1;
parameter FAMILY = 18;
parameter HDL_license = "U";
parameter ICWIDTH = 10;
parameter IFWIDTH = 0;
parameter IIWIDTH = 8;
parameter IMEM_APB_ACCESS = 0;
parameter INITWIDTH = 11;
parameter INSMODE = 0;
parameter IOWIDTH = 8;
parameter ISRADDR = 1;
parameter MAX_NVMDWIDTH = 32;
parameter STWIDTH = 4;
parameter TESTBENCH = "User";
parameter TESTMODE = 0;
parameter UNIQ_STRING = "SF_Zi1_COREABC_0";
parameter UNIQ_STRING_LENGTH = 16;
parameter VERILOGCODE = "";
parameter VERILOGVARS = "";
parameter VHDLCODE = "";
parameter VHDLVARS = "";
parameter ZRWIDTH = 16;
