#ifndef _FLASH_H
#define _FLASH_H
/**********************************************************************/
#define     UBYTE       unsigned char
#define     SBYTE                char
#define     UWORD       unsigned  int
#define     ULONG       unsigned  long int
//==============================================================================
#define        SectorsPerPage         16
#define        Flash_Sector_Size      1024
#define        Flash_Page_Min         0x38
#define        Flash_Page_Max         0x3F
#define        Flash_Blocks           1          // the number of flash blocks
#define        Flash_Pages_Per_Block  8          // the number of flash blocks
//==============================================================================
#define pg_sec(x,y)   (((((unsigned long)x)<<16) + ((((unsigned int)y)*Flash_Sector_Size)+0x8000UL)))
#define getCCR_disI() { asm TPA; asm STAA CCRbackup; asm SEI; } // save CCR disable interrupts
#define restoreCCR()  { asm LDAA CCRbackup; asm TAP;  }          // restore CCR
//==============================================================================
// flash error codes
//==============================================================================
#define FLASHerrOK           0         // OK
#define FLASHerrRANGE        1         // address is out of range
#define FLASHerrVALUE        2         // Read value is not equal to written value
#define FLASHerrNOTAVAIL     3         // Desired program/erase operation is not available
#define FLASHerrBUSY         4         // Flash is busy
#define ERR_NOTAVAIL         5         // Requested value or method not available.

//==============================================================================
// flash function definitions
//==============================================================================
#pragma CODE_SEG NON_BANKED
//==============================================================================
extern UBYTE Flash_Write_Word(ULONG addr,UWORD data);
extern UBYTE Flash_Erase_Sector(ULONG addr);
extern UBYTE Flash_Write_Block(ULONG address_source,ULONG address_destination,UWORD count);
extern UBYTE Flash_CatchError(UBYTE err, UBYTE _place);
//==============================================================================
#pragma CODE_SEG DEFAULT
//==============================================================================
extern UBYTE Flash_Init(unsigned long oscclk, unsigned long tbus); // [kHz,ns]
//==============================================================================
// Place flash routines into non banked memory because following function has 
// return code RTS
// Following wariables has to be placed in RAM
//==============================================================================
#pragma DATA_SEG DEFAULT
static UBYTE WaitForFlash[]={
 
 0xC6, 0x80,                           // LDAB #$80      // command buffer empty = 0
 0x7B, 0x01, 0x05,                     // STAB _FSTAT
 
 0xC7,                                 // CLRB           // clear return value
 
 0x1E, 0x01, 0x05, 0x20, 0x05,         // BRSET _FSTAT,#32,*+10 
 0x1F, 0x01, 0x05, 0x10, 0x03,         // BRCLR _FSTAT,#16,*+8 
 0xC6, 0x05,                           // LDAB  #5             ;ERR_NOTAVAIL
 0x3D,                                 // RTS 
 0x1F, 0x01, 0x05, 0x40, 0xFB,         // BRCLR _FSTAT,#64,*+0 
 0x3D                                  // RTS
};
//==============================================================================
// definition of sectors

#define        PG_38_SEC_00    (0x388000UL)
#define        PG_38_SEC_01    (0x388400UL)
#define        PG_38_SEC_02    (0x388800UL)
#define        PG_38_SEC_03    (0x388C00UL)
#define        PG_38_SEC_04    (0x389000UL)
#define        PG_38_SEC_05    (0x389400UL)
#define        PG_38_SEC_06    (0x389800UL)
#define        PG_38_SEC_07    (0x389C00UL)
#define        PG_38_SEC_08    (0x38A000UL)
#define        PG_38_SEC_09    (0x38A400UL)
#define        PG_38_SEC_10    (0x38A800UL)
#define        PG_38_SEC_11    (0x38AC00UL)
#define        PG_38_SEC_12    (0x38B000UL)
#define        PG_38_SEC_13    (0x38B400UL)
#define        PG_38_SEC_14    (0x38B800UL)
#define        PG_38_SEC_15    (0x38BC00UL)

#define        PG_39_SEC_00    (0x398000UL)
#define        PG_39_SEC_01    (0x398400UL)
#define        PG_39_SEC_02    (0x398800UL)
#define        PG_39_SEC_03    (0x398C00UL)
#define        PG_39_SEC_04    (0x399000UL)
#define        PG_39_SEC_05    (0x399400UL)
#define        PG_39_SEC_06    (0x399800UL)
#define        PG_39_SEC_07    (0x399C00UL)
#define        PG_39_SEC_08    (0x39A000UL)
#define        PG_39_SEC_09    (0x39A400UL)
#define        PG_39_SEC_10    (0x39A800UL)
#define        PG_39_SEC_11    (0x39AC00UL)
#define        PG_39_SEC_12    (0x39B000UL)
#define        PG_39_SEC_13    (0x39B400UL)
#define        PG_39_SEC_14    (0x39B800UL)
#define        PG_39_SEC_15    (0x39BC00UL)

#define        PG_3A_SEC_00    (0x3A8000UL)
#define        PG_3A_SEC_01    (0x3A8400UL)
#define        PG_3A_SEC_02    (0x3A8800UL)
#define        PG_3A_SEC_03    (0x3A8C00UL)
#define        PG_3A_SEC_04    (0x3A9000UL)
#define        PG_3A_SEC_05    (0x3A9400UL)
#define        PG_3A_SEC_06    (0x3A9800UL)
#define        PG_3A_SEC_07    (0x3A9C00UL)
#define        PG_3A_SEC_08    (0x3AA000UL)
#define        PG_3A_SEC_09    (0x3AA400UL)
#define        PG_3A_SEC_10    (0x3AA800UL)
#define        PG_3A_SEC_11    (0x3AAC00UL)
#define        PG_3A_SEC_12    (0x3AB000UL)
#define        PG_3A_SEC_13    (0x3AB400UL)
#define        PG_3A_SEC_14    (0x3AB800UL)
#define        PG_3A_SEC_15    (0x3ABC00UL)

#define        PG_3B_SEC_00    (0x3B8000UL)
#define        PG_3B_SEC_01    (0x3B8400UL)
#define        PG_3B_SEC_02    (0x3B8800UL)
#define        PG_3B_SEC_03    (0x3B8C00UL)
#define        PG_3B_SEC_04    (0x3B9000UL)
#define        PG_3B_SEC_05    (0x3B9400UL)
#define        PG_3B_SEC_06    (0x3B9800UL)
#define        PG_3B_SEC_07    (0x3B9C00UL)
#define        PG_3B_SEC_08    (0x3BA000UL)
#define        PG_3B_SEC_09    (0x3BA400UL)
#define        PG_3B_SEC_10    (0x3BA800UL)
#define        PG_3B_SEC_11    (0x3BAC00UL)
#define        PG_3B_SEC_12    (0x3BB000UL)
#define        PG_3B_SEC_13    (0x3BB400UL)
#define        PG_3B_SEC_14    (0x3BB800UL)
#define        PG_3B_SEC_15    (0x3BBC00UL)

#define        PG_3C_SEC_00    (0x3C8000UL)
#define        PG_3C_SEC_01    (0x3C8400UL)
#define        PG_3C_SEC_02    (0x3C8800UL)
#define        PG_3C_SEC_03    (0x3C8C00UL)
#define        PG_3C_SEC_04    (0x3C9000UL)
#define        PG_3C_SEC_05    (0x3C9400UL)
#define        PG_3C_SEC_06    (0x3C9800UL)
#define        PG_3C_SEC_07    (0x3C9C00UL)
#define        PG_3C_SEC_08    (0x3CA000UL)
#define        PG_3C_SEC_09    (0x3CA400UL)
#define        PG_3C_SEC_10    (0x3CA800UL)
#define        PG_3C_SEC_11    (0x3CAC00UL)
#define        PG_3C_SEC_12    (0x3CB000UL)
#define        PG_3C_SEC_13    (0x3CB400UL)
#define        PG_3C_SEC_14    (0x3CB800UL)
#define        PG_3C_SEC_15    (0x3CBC00UL)

#define        PG_3D_SEC_00    (0x3D8000UL)
#define        PG_3D_SEC_01    (0x3D8400UL)
#define        PG_3D_SEC_02    (0x3D8800UL)
#define        PG_3D_SEC_03    (0x3D8C00UL)
#define        PG_3D_SEC_04    (0x3D9000UL)
#define        PG_3D_SEC_05    (0x3D9400UL)
#define        PG_3D_SEC_06    (0x3D9800UL)
#define        PG_3D_SEC_07    (0x3D9C00UL)
#define        PG_3D_SEC_08    (0x3DA000UL)
#define        PG_3D_SEC_09    (0x3DA400UL)
#define        PG_3D_SEC_10    (0x3DA800UL)
#define        PG_3D_SEC_11    (0x3DAC00UL)
#define        PG_3D_SEC_12    (0x3DB000UL)
#define        PG_3D_SEC_13    (0x3DB400UL)
#define        PG_3D_SEC_14    (0x3DB800UL)
#define        PG_3D_SEC_15    (0x3DBC00UL)

#define        PG_3E_SEC_00    (0x3E8000UL)
#define        PG_3E_SEC_01    (0x3E8400UL)
#define        PG_3E_SEC_02    (0x3E8800UL)
#define        PG_3E_SEC_03    (0x3E8C00UL)
#define        PG_3E_SEC_04    (0x3E9000UL)
#define        PG_3E_SEC_05    (0x3E9400UL)
#define        PG_3E_SEC_06    (0x3E9800UL)
#define        PG_3E_SEC_07    (0x3E9C00UL)
#define        PG_3E_SEC_08    (0x3EA000UL)
#define        PG_3E_SEC_09    (0x3EA400UL)
#define        PG_3E_SEC_10    (0x3EA800UL)
#define        PG_3E_SEC_11    (0x3EAC00UL)
#define        PG_3E_SEC_12    (0x3EB000UL)
#define        PG_3E_SEC_13    (0x3EB400UL)
#define        PG_3E_SEC_14    (0x3EB800UL)
#define        PG_3E_SEC_15    (0x3EBC00UL)

#define        PG_3F_SEC_00    (0x3F8000UL)
#define        PG_3F_SEC_01    (0x3F8400UL)
#define        PG_3F_SEC_02    (0x3F8800UL)
#define        PG_3F_SEC_03    (0x3F8C00UL)
#define        PG_3F_SEC_04    (0x3F9000UL)
#define        PG_3F_SEC_05    (0x3F9400UL)
#define        PG_3F_SEC_06    (0x3F9800UL)
#define        PG_3F_SEC_07    (0x3F9C00UL)
#define        PG_3F_SEC_08    (0x3FA000UL)
#define        PG_3F_SEC_09    (0x3FA400UL)
#define        PG_3F_SEC_10    (0x3FA800UL)
#define        PG_3F_SEC_11    (0x3FAC00UL)
#define        PG_3F_SEC_12    (0x3FB000UL)
#define        PG_3F_SEC_13    (0x3FB400UL)
#define        PG_3F_SEC_14    (0x3FB800UL)
#define        PG_3F_SEC_15    (0x3FBC00UL)


/**********************************************************************/
#endif