///////////////////////////////////////////////////////////////////////////////
//
// IAR C/C++ Compiler V2.10.4.157 for STM8                13/Jul/2020  10:55:02
// Copyright 2010-2015 IAR Systems AB.
//
//    Source file  =  
//        F:\2-Lora\Code\stm8l151K6T6-WLAN\FWLIB\src\stm8l15x_flash.c
//    Command line =  
//        F:\2-Lora\Code\stm8l151K6T6-WLAN\FWLIB\src\stm8l15x_flash.c -e -Ol
//        --no_cse --no_unroll --no_inline --no_code_motion --no_tbaa
//        --no_cross_call --debug --code_model small --data_model medium -o
//        F:\2-Lora\Code\stm8l151K6T6-WLAN\Debug\Obj\ --dlib_config
//        E:\IAR\stm8\LIB\dlstm8smf.h -lB
//        F:\2-Lora\Code\stm8l151K6T6-WLAN\Debug\List\ -I
//        F:\2-Lora\Code\stm8l151K6T6-WLAN\FWLIB\inc\ -I
//        F:\2-Lora\Code\stm8l151K6T6-WLAN\FWLIB\src\ -I
//        F:\2-Lora\Code\stm8l151K6T6-WLAN\USER\ -I
//        F:\2-Lora\Code\stm8l151K6T6-WLAN\BSP\ -I
//        F:\2-Lora\Code\stm8l151K6T6-WLAN\radio\ -I
//        F:\2-Lora\Code\stm8l151K6T6-WLAN\SX1278\ --vregs 16
//    List file    =  
//        F:\2-Lora\Code\stm8l151K6T6-WLAN\Debug\List\stm8l15x_flash.s
//
///////////////////////////////////////////////////////////////////////////////

        #define SHT_PROGBITS 0x1
        #define SHF_WRITE 0x1
        #define SHF_EXECINSTR 0x4

        EXTERN ?add32_l0_l0_l1
        EXTERN ?b0
        EXTERN ?b1
        EXTERN ?b10
        EXTERN ?b11
        EXTERN ?b12
        EXTERN ?b14
        EXTERN ?b15
        EXTERN ?b2
        EXTERN ?b3
        EXTERN ?b4
        EXTERN ?b5
        EXTERN ?b6
        EXTERN ?b7
        EXTERN ?b8
        EXTERN ?b9
        EXTERN ?dec32_l1_l1
        EXTERN ?epilogue_l2
        EXTERN ?mul16_x_x_w0
        EXTERN ?mul32_l0_l0_l1
        EXTERN ?push_l1
        EXTERN ?w0
        EXTERN ?w1
        EXTERN ?w2
        EXTERN ?w3
        EXTERN ?w4
        EXTERN ?w5
        EXTERN ?w7

        PUBLIC FLASH_DeInit
        PUBLIC FLASH_EraseBlock
        PUBLIC FLASH_EraseByte
        PUBLIC FLASH_EraseOptionByte
        PUBLIC FLASH_GetBootSize
        PUBLIC FLASH_GetCodeSize
        PUBLIC FLASH_GetFlagStatus
        PUBLIC FLASH_GetPowerStatus
        PUBLIC FLASH_GetProgrammingTime
        PUBLIC FLASH_GetReadOutProtectionStatus
        PUBLIC FLASH_ITConfig
        PUBLIC FLASH_Lock
        PUBLIC FLASH_PowerRunModeConfig
        PUBLIC FLASH_PowerWaitModeConfig
        PUBLIC FLASH_ProgramBlock
        PUBLIC FLASH_ProgramByte
        PUBLIC FLASH_ProgramOptionByte
        PUBLIC FLASH_ProgramWord
        PUBLIC FLASH_ReadByte
        PUBLIC FLASH_SetProgrammingTime
        PUBLIC FLASH_Unlock
        PUBLIC FLASH_WaitForLastOperation

// F:\2-Lora\Code\stm8l151K6T6-WLAN\FWLIB\src\stm8l15x_flash.c
//    1 /**
//    2   ******************************************************************************
//    3   * @file    stm8l15x_flash.c
//    4   * @author  MCD Application Team
//    5   * @version V1.6.0
//    6   * @date    28-June-2013
//    7   * @brief   This file provides all the Flash firmware functions. These functions 
//    8   *          can be executed from Internal FLASH or Internal RAM memories.
//    9   *            - FLASH program and Data EEPROM memories interface configuration
//   10   *            - FLASH program and Data EEPROM memories Programming
//   11   *            - Option Bytes Programming
//   12   *            - Interrupts and flags management
//   13   *            - Functions to be executed from RAM
//   14   *               
//   15   *  @verbatim
//   16   *  
//   17   *          ===================================================================
//   18   *                                 How to use this driver
//   19   *          ===================================================================
//   20   *                           
//   21   *          This driver provides functions to configure and program the Flash 
//   22   *          memory of all STM8L15x devices
//   23   *          These functions are split in 4 groups
//   24   * 
//   25   *           1. FLASH program and Data EEPROM memories interface configuration 
//   26   *              functions: this group includes the management of the following 
//   27   *              features:
//   28   *                    - Set the fixed programming time
//   29   *                    - Configure the power state during WFI mode
//   30   *                    - Configure the power state during run, low power run and
//   31   *                      WFE modes (should be executed from internal RAM) 
//   32   *  
//   33   *           2. FLASH program and Data EEPROM memories Programming functions: 
//   34   *              this group includes all needed functions to erase and program 
//   35   *              the FLASH program or the Data EEPROM memory.
//   36   *                    - Lock and Unlock the Flash program interface.
//   37   *                    - Lock and Unlock the Data EEPROM interface.  
//   38   *                    - Erase function: Erase Byte, Erase Word and Erase Block 
//   39   *                      (should be executed from internal RAM). 
//   40   *                    - Program functions: Program Byte, Program Word, 
//   41   *                      Program Block (should be executed from internal RAM) 
//   42   *                      and Fast Program Block (should be executed from internal
//   43   *                      RAM).
//   44   *    
//   45   *           3. FLASH Option Bytes Programming functions: this group includes 
//   46   *              all needed functions to:
//   47   *                    - Program/erase the user option Bytes 
//   48   *                    - Get the Read Out Protection status (ROP option byte)
//   49   *                    - Get the User Boot Code size (UBC option byte)
//   50   *                    - Get the Protected Code size (PCODE option byte) 
//   51   *                    
//   52   *            Note: Option byte programming is very similar to data EEPROM byte
//   53   *                  programming.          
//   54   *  
//   55   *           4. FLASH Interrupts and flag management functions: this group 
//   56   *              includes all needed functions to:
//   57   *                    - Enable/Disable the flash interrupt sources
//   58   *                    - Get flags status
//   59   *                    - Wait for last flash operation(can be executed from 
//   60   *                      internal RAM)
//   61   *                        
//   62   *           5. Functions to be executed from RAM: this group includes the functions
//   63   *              that should be executed from RAM and provides description on how 
//   64   *              to handle this with the different supported toolchains
//   65   *   
//   66   *  The table below lists the functions that can be executed from RAM.
//   67   *  
//   68   *   +--------------------------------------------------------------------------------|
//   69   *   |   Functions prototypes      |    RAM execution            |     Comments       |
//   70   *   ---------------------------------------------------------------------------------|
//   71   *   |                             | Mandatory in case of block  | Can be executed    |
//   72   *   | FLASH_WaitForLastOperation  | Operation:                  | from Flash in case |
//   73   *   |                             | - Block programming         | of byte and word   |
//   74   *   |                             | - Block erase               | Operations         |
//   75   *   |--------------------------------------------------------------------------------|
//   76   *   | FLASH_PowerRunModeConfig    |       Exclusively           | useless from Flash |
//   77   *   |--------------------------------------------------------------------------------|
//   78   *   | FLASH_GetPowerStatus        |       Exclusively           | useless from Flash |
//   79   *   |--------------------------------------------------------------------------------|
//   80   *   | FLASH_ProgramBlock          |       Exclusively           | useless from Flash |
//   81   *   |--------------------------------------------------------------------------------|
//   82   *   | FLASH_EraseBlock            |       Exclusively           | useless from Flash |
//   83   *   |--------------------------------------------------------------------------------|
//   84   *  
//   85   *  To be able to execute functions from RAM several steps have to be followed.
//   86   *   These steps may differ from one toolchain to another.
//   87   *   A detailed description is available below within this driver.
//   88   *   You can also refer to the Flash_DataProgram example provided within the
//   89   *   STM8L15x_StdPeriph_Lib package.
//   90   * 
//   91   *  @endverbatim
//   92   *                      
//   93   ******************************************************************************
//   94   *
//   95   * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
//   96   * You may not use this file except in compliance with the License.
//   97   * You may obtain a copy of the License at:
//   98   *
//   99   *        http://www.st.com/software_license_agreement_liberty_v2
//  100   *
//  101   * Unless required by applicable law or agreed to in writing, software 
//  102   * distributed under the License is distributed on an "AS IS" BASIS, 
//  103   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  104   * See the License for the specific language governing permissions and
//  105   * limitations under the License.
//  106   *
//  107   ****************************************************************************** 
//  108   */
//  109 /* Includes ------------------------------------------------------------------*/
//  110 #include "stm8l15x_flash.h"
//  111 
//  112 /** @addtogroup STM8L15x_StdPeriph_Driver
//  113   * @{
//  114   */
//  115 
//  116 /** @defgroup FLASH 
//  117   * @brief FLASH driver modules
//  118   * @{
//  119   */
//  120   
//  121 /* Private typedef -----------------------------------------------------------*/
//  122 /* Private define ------------------------------------------------------------*/
//  123 /** @defgroup FLASH_Private_Define
//  124   * @{
//  125   */ 
//  126 #define FLASH_CLEAR_BYTE   ((uint8_t)0x00)
//  127 #define FLASH_SET_BYTE     ((uint8_t)0xFF)
//  128 #define OPERATION_TIMEOUT  ((uint16_t)0xFFFF)
//  129 /**
//  130   * @}
//  131   */
//  132 /* Private macro -------------------------------------------------------------*/
//  133 /* Private variables ---------------------------------------------------------*/
//  134 /* Private function prototypes -----------------------------------------------*/
//  135 /* Private functions ---------------------------------------------------------*/
//  136  
//  137 /** @defgroup FLASH_Private_Functions
//  138   * @{
//  139   */ 
//  140 
//  141 /** @defgroup FLASH_Group1 FLASH program and Data EEPROM memories Interface 
//  142   *                        configuration functions
//  143   *  @brief   FLASH Interface configuration functions 
//  144  *
//  145 @verbatim   
//  146  ===============================================================================
//  147       FLASH program and Data EEPROM memories interface configuration functions
//  148  ===============================================================================  
//  149 
//  150    The FLASH program and Data EEPROM memories interface configuration functions,
//  151     includes the following functions:
//  152 		
//  153    - FLASH_ProgramTime_TypeDef FLASH_GetProgrammingTime(void);
//  154    - void FLASH_SetProgrammingTime(FLASH_ProgramTime_TypeDef FLASH_ProgTime);
//  155 	 - void FLASH_PowerWaitModeConfig(FLASH_Power_TypeDef FLASH_Power);
//  156 
//  157    These functions don't need the unlock sequence.
//  158 
//  159 @endverbatim
//  160   * @{
//  161   */
//  162 	
//  163 /**
//  164   * @brief  Sets the fixed programming time
//  165   * @param  FLASH_ProgTime : Indicates the programming time to be fixed
//  166   *          This parameter can be one of the following values:
//  167   *            @arg FLASH_ProgramTime_Standard: Standard programming time fixed at 1/2 tprog
//  168   *            @arg FLASH_ProgramTime_TProg: Programming time fixed at tprog 
//  169   * @retval None
//  170   */

        SECTION `.near_func.text`:CODE:REORDER:NOROOT(0)
        CODE
//  171 void FLASH_SetProgrammingTime(FLASH_ProgramTime_TypeDef FLASH_ProgTime)
//  172 {
//  173   /* Check parameter */
//  174   assert_param(IS_FLASH_PROGRAM_TIME(FLASH_ProgTime));
//  175 
//  176   FLASH->CR1 &= (uint8_t)(~FLASH_CR1_FIX);
FLASH_SetProgrammingTime:
        BRES      L:0x5050, #0x0
//  177   FLASH->CR1 |= (uint8_t)FLASH_ProgTime;
        OR        A, L:0x5050
        LD        L:0x5050, A
//  178 }
        RET
//  179 
//  180 /**
//  181   * @brief  Returns the fixed programming time
//  182   * @param  None
//  183   * @retval Fixed programming time value
//  184   */

        SECTION `.near_func.text`:CODE:REORDER:NOROOT(0)
        CODE
//  185 FLASH_ProgramTime_TypeDef FLASH_GetProgrammingTime(void)
//  186 {
//  187   return((FLASH_ProgramTime_TypeDef)(FLASH->CR1 & FLASH_CR1_FIX));
FLASH_GetProgrammingTime:
        LD        A, L:0x5050
        AND       A, #0x1
        RET
//  188 }
//  189 
//  190 /**
//  191   * @brief  Configures the power state for Flash program and data EEPROM during
//  192   *          wait for interrupt mode
//  193   * @param  FLASH_Power: The power state for Flash program and data EEPROM during
//  194   *         wait for interrupt mode
//  195   *          This parameter can be one of the following values:
//  196   *            @arg FLASH_Power_IDDQ: Flash program and data EEPROM in IDDQ
//  197   *            @arg FLASH_Power_On: Flash program and data EEPROM not in IDDQ 
//  198   * @retval None
//  199   */

        SECTION `.near_func.text`:CODE:REORDER:NOROOT(0)
        CODE
//  200 void FLASH_PowerWaitModeConfig(FLASH_Power_TypeDef FLASH_Power)
//  201 {
//  202   /* Check parameter */
//  203   assert_param(IS_FLASH_POWER(FLASH_Power));
//  204 
//  205   /* Flash program and data EEPROM in IDDQ during wait for interrupt mode*/
//  206   if (FLASH_Power != FLASH_Power_On)
FLASH_PowerWaitModeConfig:
        CP        A, #0x1
        JREQ      L:??FLASH_PowerWaitModeConfig_0
//  207   {
//  208     FLASH->CR1 |= (uint8_t)FLASH_CR1_WAITM;
        BSET      L:0x5050, #0x2
        RET
//  209   }
//  210   /* Flash program and data EEPROM not in IDDQ during wait for interrupt mode*/
//  211   else
//  212   {
//  213     FLASH->CR1 &= (uint8_t)(~FLASH_CR1_WAITM);
??FLASH_PowerWaitModeConfig_0:
        BRES      L:0x5050, #0x2
//  214   }
//  215 }
        RET
//  216 
//  217 /**
//  218   * @}
//  219   */
//  220 
//  221 /** @defgroup FLASH_Group2 FLASH Memory Programming functions
//  222  *  @brief   FLASH Memory Programming functions
//  223  *
//  224 @verbatim   
//  225  ===============================================================================
//  226                       FLASH Memory Programming functions
//  227  ===============================================================================  
//  228 
//  229    The FLASH Memory Programming functions, includes the following functions:
//  230     - void FLASH_DeInit(void);
//  231     - void FLASH_Unlock(FLASH_MemType_TypeDef FLASH_MemType);
//  232     - void FLASH_Lock(FLASH_MemType_TypeDef FLASH_MemType);
//  233     - void FLASH_ProgramByte(uint32_t Address, uint8_t Data);
//  234     - void FLASH_EraseByte(uint32_t Address);
//  235     - void FLASH_ProgramWord(uint32_t Address, uint32_t Data);
//  236     - uint8_t FLASH_ReadByte(uint32_t Address);
//  237    
//  238    Any operation of erase or program should follow these steps:
//  239 
//  240    1. Call the FLASH_Unlock(FLASH_MemType) function to enable the memory access
//  241 
//  242    2. Call the desired function to erase or program data
//  243 
//  244    3. Call the FLASH_Lock() function to disable the memory access 
//  245      (it is recommended to protect the FLASH memory against possible unwanted operation)
//  246 
//  247 @endverbatim
//  248   * @{
//  249   */
//  250   
//  251 /**
//  252   * @brief  Deinitializes the FLASH registers to their default reset values.
//  253   * @param  None
//  254   * @retval None
//  255   */

        SECTION `.near_func.text`:CODE:REORDER:NOROOT(0)
        CODE
//  256 void FLASH_DeInit(void)
//  257 {
//  258   FLASH->CR1 = FLASH_CR1_RESET_VALUE;
FLASH_DeInit:
        MOV       L:0x5050, #0x0
//  259   FLASH->CR2 = FLASH_CR2_RESET_VALUE;
        MOV       L:0x5051, #0x0
//  260   FLASH->IAPSR = FLASH_IAPSR_RESET_VALUE;
        MOV       L:0x5054, #0x40
//  261   (void) FLASH->IAPSR; /* Reading of this register causes the clearing of status flags */
        LD        A, L:0x5054
//  262 }
        RET
//  263   
//  264 /**
//  265   * @brief  Unlocks the program or data EEPROM memory
//  266   * @param  FLASH_MemType : Memory type to unlock
//  267   *          This parameter can be one of the following values:
//  268   *            @arg FLASH_MemType_Program: Program memory
//  269   *            @arg FLASH_MemType_Data: Data EEPROM memory 
//  270   * @retval None
//  271   */

        SECTION `.near_func.text`:CODE:REORDER:NOROOT(0)
        CODE
//  272 void FLASH_Unlock(FLASH_MemType_TypeDef FLASH_MemType)
//  273 {
//  274   /* Check parameter */
//  275   assert_param(IS_FLASH_MEMORY_TYPE(FLASH_MemType));
//  276 
//  277   /* Unlock program memory */
//  278   if (FLASH_MemType == FLASH_MemType_Program)
FLASH_Unlock:
        CP        A, #0xfd
        JRNE      L:??FLASH_Unlock_0
//  279   {
//  280     FLASH->PUKR = FLASH_RASS_KEY1;
        MOV       L:0x5052, #0x56
//  281     FLASH->PUKR = FLASH_RASS_KEY2;
        MOV       L:0x5052, #0xae
//  282   }
//  283 
//  284   /* Unlock data memory */
//  285   if (FLASH_MemType == FLASH_MemType_Data)
??FLASH_Unlock_0:
        CP        A, #0xf7
        JRNE      L:??FLASH_Unlock_1
//  286   {
//  287     FLASH->DUKR = FLASH_RASS_KEY2; /* Warning: keys are reversed on data memory !!! */
        MOV       L:0x5053, #0xae
//  288     FLASH->DUKR = FLASH_RASS_KEY1;
        MOV       L:0x5053, #0x56
//  289   }
//  290 }
??FLASH_Unlock_1:
        RET
//  291 
//  292 /**
//  293   * @brief  Locks the program or data EEPROM memory
//  294   * @param  FLASH_MemType : Memory type
//  295   *          This parameter can be one of the following values:
//  296   *            @arg FLASH_MemType_Program: Program memory
//  297   *            @arg FLASH_MemType_Data: Data EEPROM memory 
//  298   * @retval None
//  299   */

        SECTION `.near_func.text`:CODE:REORDER:NOROOT(0)
        CODE
//  300 void FLASH_Lock(FLASH_MemType_TypeDef FLASH_MemType)
//  301 {
//  302   /* Check parameter */
//  303   assert_param(IS_FLASH_MEMORY_TYPE(FLASH_MemType));
//  304   /* Lock memory */
//  305   FLASH->IAPSR &= (uint8_t)FLASH_MemType;
FLASH_Lock:
        AND       A, L:0x5054
        LD        L:0x5054, A
//  306 }
        RET
//  307 
//  308 /**
//  309   * @brief  Programs one byte in program or data EEPROM memory
//  310   * @param  Address : Address where the byte will be programmed
//  311   * @param  Data : Value to be programmed
//  312   * @retval None
//  313   */

        SECTION `.near_func.text`:CODE:REORDER:NOROOT(0)
        CODE
//  314 void FLASH_ProgramByte(uint32_t Address, uint8_t Data)
//  315 {
//  316   /* Check parameters */
//  317   assert_param(IS_FLASH_ADDRESS(Address));
//  318 
//  319   *(PointerAttr uint8_t*) (MemoryAddressCast)Address = Data;
FLASH_ProgramByte:
        LD        [S:?w1.w], A
//  320 }
        RET
//  321 
//  322 /**
//  323   * @brief  Erases one byte in the program or data EEPROM memory
//  324   * @param  Address : Address of the byte to erase
//  325   * @retval None
//  326   */

        SECTION `.near_func.text`:CODE:REORDER:NOROOT(0)
        CODE
//  327 void FLASH_EraseByte(uint32_t Address)
//  328 {
//  329   /* Check parameter */
//  330   assert_param(IS_FLASH_ADDRESS(Address));
//  331 
//  332   *(PointerAttr uint8_t*) (MemoryAddressCast)Address = FLASH_CLEAR_BYTE; /* Erase byte */
FLASH_EraseByte:
        CLR       A
        LD        [S:?w1.w], A
//  333 }
        RET
//  334 
//  335 /**
//  336   * @brief  Programs one word (4 bytes) in program or data EEPROM memory
//  337   * @param  Address : The address where the data will be programmed
//  338   * @param  Data : Value to be programmed
//  339   * @retval None
//  340   */

        SECTION `.near_func.text`:CODE:REORDER:NOROOT(0)
        CODE
//  341 void FLASH_ProgramWord(uint32_t Address, uint32_t Data)
//  342 {
FLASH_ProgramWord:
        CALL      L:?push_l1
//  343   /* Check parameters */
//  344   assert_param(IS_FLASH_ADDRESS(Address));
//  345   /* Enable Word Write Once */
//  346   FLASH->CR2 |= FLASH_CR2_WPRG;
        BSET      L:0x5051, #0x6
//  347 
//  348   /* Write one byte - from lowest address*/
//  349   *((PointerAttr uint8_t*)(MemoryAddressCast)Address)       = *((uint8_t*)(&Data));   
        LD        A, (0x1,SP)
        LD        [S:?w1.w], A
//  350   /* Write one byte*/
//  351   *(((PointerAttr uint8_t*)(MemoryAddressCast)Address) + 1) = *((uint8_t*)(&Data) + 1);
        LDW       X, S:?w1
        INCW      X
        LD        A, (0x2,SP)
        LD        (X), A
//  352   /* Write one byte*/
//  353   *(((PointerAttr uint8_t*)(MemoryAddressCast)Address) + 2) = *((uint8_t*)(&Data) + 2); 
        LDW       X, S:?w1
        ADDW      X, #0x2
        LD        A, (0x3,SP)
        LD        (X), A
//  354   /* Write one byte - from higher address*/
//  355   *(((PointerAttr uint8_t*)(MemoryAddressCast)Address) + 3) = *((uint8_t*)(&Data) + 3); 
        LDW       X, S:?w1
        ADDW      X, #0x3
        LD        A, (0x4,SP)
        LD        (X), A
//  356 }
        ADD       SP, #0x4
        RET
//  357 
//  358 /**
//  359   * @brief  Reads one byte from flash memory
//  360   * @param  Address : Address to read
//  361   * @retval Value of the byte
//  362   */

        SECTION `.near_func.text`:CODE:REORDER:NOROOT(0)
        CODE
//  363 uint8_t FLASH_ReadByte(uint32_t Address)
//  364 {
//  365   /* Read byte */
//  366   return(*(PointerAttr uint8_t *) (MemoryAddressCast)Address);
FLASH_ReadByte:
        LDW       X, S:?w1
        LD        A, (X)
        RET
//  367 }
//  368 /**
//  369   * @}
//  370   */
//  371 
//  372 /** @defgroup FLASH_Group3 Option Bytes Programming functions
//  373  *  @brief   Option Bytes Programming functions 
//  374  *
//  375 @verbatim   
//  376  ===============================================================================
//  377                         Option Bytes Programming functions
//  378  ===============================================================================  
//  379 
//  380    The FLASH_Option Bytes Programming_functions, includes the following functions:
//  381 
//  382    - void FLASH_ProgramOptionByte(uint16_t Address, uint8_t Data);
//  383    - void FLASH_EraseOptionByte(uint16_t Address);
//  384    - FunctionalState FLASH_GetReadOutProtectionStatus(void);
//  385    - uint16_t FLASH_GetBootSize(void);
//  386    - uint16_t FLASH_GetCodeSize(void);
//  387    
//  388    Any operation of erase or program should follow these steps:
//  389    
//  390    1. Call the FLASH_Unlock(FLASH_MemType_Data); function to enable the Flash 
//  391       option control register access
//  392    
//  393    2. Call the desired function to erase or program data
//  394       - void FLASH_ProgramOptionByte(uint16_t Address, uint8_t Data); => to program
//  395         the option byte Address with the desired Data value.  
//  396       - void FLASH_EraseOptionByte(uint16_t Address); => to erase the option byte
//  397         Address. 			 
//  398    
//  399    3. Once all needed option bytes to be programmed are correctly written, call the
//  400       FLASH_Lock(FLASH_MemType_Data) to disable the memory access ( It is recommended to
//  401       protect the FLASH memory against possible unwanted operation)
//  402 
//  403 @endverbatim
//  404   * @{
//  405   */
//  406   
//  407 /**
//  408   * @brief  Programs option byte
//  409   * @param  Address : option byte address to program
//  410   * @param  Data : Value to write
//  411   * @retval None
//  412   */

        SECTION `.near_func.text`:CODE:REORDER:NOROOT(0)
        CODE
//  413 void FLASH_ProgramOptionByte(uint16_t Address, uint8_t Data)
//  414 {
//  415   /* Check parameter */
//  416   assert_param(IS_OPTION_BYTE_ADDRESS(Address));
//  417 
//  418   /* Enable write access to option bytes */
//  419   FLASH->CR2 |= FLASH_CR2_OPT;
FLASH_ProgramOptionByte:
        BSET      L:0x5051, #0x7
//  420 
//  421   /* Program option byte and his complement */
//  422   *((PointerAttr uint8_t*)Address) = Data;
        LD        (X), A
//  423 
//  424   FLASH_WaitForLastOperation(FLASH_MemType_Program);
        LD        A, #0xfd
        CALL      L:FLASH_WaitForLastOperation
//  425 
//  426   /* Disable write access to option bytes */
//  427   FLASH->CR2 &= (uint8_t)(~FLASH_CR2_OPT);
        BRES      L:0x5051, #0x7
//  428 }
        RET
//  429 
//  430 /**
//  431   * @brief  Erases option byte
//  432   * @param  Address : Option byte address to erase
//  433   * @retval None
//  434   */

        SECTION `.near_func.text`:CODE:REORDER:NOROOT(0)
        CODE
//  435 void FLASH_EraseOptionByte(uint16_t Address)
//  436 {
//  437   /* Check parameter */
//  438   assert_param(IS_OPTION_BYTE_ADDRESS(Address));
//  439 
//  440   /* Enable write access to option bytes */
//  441   FLASH->CR2 |= FLASH_CR2_OPT;
FLASH_EraseOptionByte:
        BSET      L:0x5051, #0x7
//  442 
//  443   /* Erase option byte and his complement */
//  444   *((PointerAttr uint8_t*)Address) = FLASH_CLEAR_BYTE;
        CLR       A
        LD        (X), A
//  445 
//  446   FLASH_WaitForLastOperation(FLASH_MemType_Program);
        LD        A, #0xfd
        CALL      L:FLASH_WaitForLastOperation
//  447 
//  448   /* Disable write access to option bytes */
//  449   FLASH->CR2 &= (uint8_t)(~FLASH_CR2_OPT);
        BRES      L:0x5051, #0x7
//  450 }
        RET
//  451 
//  452 /**
//  453   * @brief  Returns the FLASH Read Out Protection Status.
//  454   * @param  None
//  455   * @retval FLASH Read Out Protection Status.
//  456   *         This parameter can be a ENABLE or DISABLE
//  457   */

        SECTION `.near_func.text`:CODE:REORDER:NOROOT(0)
        CODE
//  458 FunctionalState FLASH_GetReadOutProtectionStatus(void)
//  459 {
//  460   FunctionalState state = DISABLE;
FLASH_GetReadOutProtectionStatus:
        CLR       S:?b0
//  461 
//  462   if (OPT->ROP == FLASH_READOUTPROTECTION_KEY)
        LD        A, L:0x4800
        CP        A, #0xaa
        JRNE      L:??FLASH_GetReadOutProtectionStatus_0
//  463   {
//  464     /* The status of the Flash read out protection is enabled*/
//  465     state =  ENABLE;
        MOV       S:?b0, #0x1
        JRA       L:??FLASH_GetReadOutProtectionStatus_1
//  466   }
//  467   else
//  468   {
//  469     /* The status of the Flash read out protection is disabled*/
//  470     state =  DISABLE;
??FLASH_GetReadOutProtectionStatus_0:
        CLR       S:?b0
//  471   }
//  472 
//  473   return state;
??FLASH_GetReadOutProtectionStatus_1:
        LD        A, S:?b0
        RET
//  474 }
//  475 
//  476 /**
//  477   * @brief  Returns the Boot memory size in bytes
//  478   * @param  None
//  479   * @retval Boot memory size in bytes
//  480   */

        SECTION `.near_func.text`:CODE:REORDER:NOROOT(0)
        CODE
//  481 uint16_t FLASH_GetBootSize(void)
//  482 {
//  483   uint16_t temp = 0;
FLASH_GetBootSize:
        CLRW      Y
//  484 
//  485   /* Calculates the number of bytes */
//  486   temp = (uint16_t)((uint16_t)OPT->UBC * (uint16_t)128);
        LD        A, L:0x4802
        CLRW      X
        LD        XL, A
        LDW       S:?w1, X
        LDW       X, #0x80
        LDW       S:?w0, X
        LDW       X, S:?w1
        CALL      L:?mul16_x_x_w0
        LDW       Y, X
//  487 
//  488   /* Correction because size upper 8kb doesn't exist */
//  489   if (OPT->UBC > 0x7F)
        LD        A, L:0x4802
        CP        A, #0x80
        JRC       L:??FLASH_GetBootSize_0
//  490   {
//  491     temp = 8192;
        LDW       X, #0x2000
        LDW       Y, X
//  492   }
//  493 
//  494   /* Return value */
//  495   return(temp);
??FLASH_GetBootSize_0:
        LDW       X, Y
        RET
//  496 
//  497 }
//  498 
//  499 /**
//  500  *
//  501   * @brief  Returns the Code Area size in bytes
//  502   * @param  None
//  503   * @retval Code Area size in bytes
//  504   */

        SECTION `.near_func.text`:CODE:REORDER:NOROOT(0)
        CODE
//  505 uint16_t FLASH_GetCodeSize(void)
//  506 {
//  507   uint16_t temp = 0;
FLASH_GetCodeSize:
        CLRW      Y
//  508 
//  509   /* Calculates the number of bytes */
//  510   temp = (uint16_t)((uint16_t)OPT->PCODESIZE * (uint16_t)128);
        LD        A, L:0x4807
        CLRW      X
        LD        XL, A
        LDW       S:?w1, X
        LDW       X, #0x80
        LDW       S:?w0, X
        LDW       X, S:?w1
        CALL      L:?mul16_x_x_w0
        LDW       Y, X
//  511 
//  512   /* Correction because size upper of 8kb doesn't exist */
//  513   if (OPT->PCODESIZE > 0x7F)
        LD        A, L:0x4807
        CP        A, #0x80
        JRC       L:??FLASH_GetCodeSize_0
//  514   {
//  515     temp = 8192;
        LDW       X, #0x2000
        LDW       Y, X
//  516   }
//  517 
//  518   /* Return value */
//  519   return(temp);
??FLASH_GetCodeSize_0:
        LDW       X, Y
        RET
//  520 }
//  521 
//  522 /**
//  523   * @}
//  524   */
//  525 
//  526 /** @defgroup FLASH_Group4 Interrupts and flags management functions
//  527  *  @brief   Interrupts and flags management functions
//  528  *
//  529 @verbatim   
//  530  ===============================================================================
//  531                   Interrupts and flags management functions
//  532  ===============================================================================  
//  533 
//  534 @endverbatim
//  535   * @{
//  536   */
//  537   
//  538 /**
//  539   * @brief  Enables or Disables the Flash interrupt mode
//  540   * @param  NewState : The new state of the flash interrupt mode
//  541     *         This parameter can be: ENABLE or DISABLE.
//  542   * @retval None
//  543   */

        SECTION `.near_func.text`:CODE:REORDER:NOROOT(0)
        CODE
//  544 void FLASH_ITConfig(FunctionalState NewState)
//  545 {
//  546 
//  547   /* Check parameter */
//  548   assert_param(IS_FUNCTIONAL_STATE(NewState));
//  549 
//  550   if (NewState != DISABLE)
FLASH_ITConfig:
        TNZ       A
        JREQ      L:??FLASH_ITConfig_0
//  551   {
//  552     /* Enables the interrupt sources */
//  553     FLASH->CR1 |= FLASH_CR1_IE;
        BSET      L:0x5050, #0x1
        RET
//  554   }
//  555   else
//  556   {
//  557     /* Disables the interrupt sources */
//  558     FLASH->CR1 &= (uint8_t)(~FLASH_CR1_IE);
??FLASH_ITConfig_0:
        BRES      L:0x5050, #0x1
//  559   }
//  560 }
        RET
//  561 
//  562 /**
//  563   * @brief  Checks whether the specified FLASH flag is set or not.
//  564   * @param  FLASH_FLAG : specifies the Flash Flag to check.
//  565   *          This parameter can be one of the following values:
//  566   *            @arg FLASH_FLAG_HVOFF: End of high voltage
//  567   *            @arg FLASH_FLAG_DUL: Data EEPROM unlocked
//  568   *            @arg FLASH_FLAG_EOP: End of programming (write or erase operation)
//  569   *            @arg FLASH_FLAG_PUL: Flash Program memory unlocked
//  570   *            @arg FLASH_FLAG_WR_PG_DIS: Write attempted to protected page       
//  571   * @retval Indicates the state of the Flash_FLAG.
//  572   *         This parameter can be SET or RESET
//  573   */

        SECTION `.near_func.text`:CODE:REORDER:NOROOT(0)
        CODE
//  574 FlagStatus FLASH_GetFlagStatus(FLASH_FLAG_TypeDef FLASH_FLAG)
//  575 {
//  576   FlagStatus status = RESET;
FLASH_GetFlagStatus:
        CLR       S:?b0
//  577   assert_param(IS_FLASH_FLAGS(FLASH_FLAG));
//  578 
//  579   /* Check the status of the specified flash flag*/
//  580   if ((FLASH->IAPSR  & (uint8_t)FLASH_FLAG) != (uint8_t)RESET)
        AND       A, L:0x5054
        CP        A, #0x0
        JREQ      L:??FLASH_GetFlagStatus_0
//  581   {
//  582     status = SET; /* Flash_FLAG is set*/
        MOV       S:?b0, #0x1
        JRA       L:??FLASH_GetFlagStatus_1
//  583   }
//  584   else
//  585   {
//  586     status = RESET; /* Flash_FLAG is reset*/
??FLASH_GetFlagStatus_0:
        CLR       S:?b0
//  587   }
//  588 
//  589   /* Return the Flash_FLAG status*/
//  590   return status;
??FLASH_GetFlagStatus_1:
        LD        A, S:?b0
        RET
//  591 }
//  592 
//  593 /**
//  594   * @}
//  595   */ 
//  596   
//  597 /** @defgroup FLASH_Group5 Functions to be executed from RAM
//  598  *  @brief  Functions to be executed from RAM
//  599  *
//  600 @verbatim   
//  601  ===============================================================================
//  602                          Functions to be executed from RAM
//  603  ===============================================================================  
//  604  
//  605  All the functions defined below must be executed from RAM exclusively, except
//  606  for the FLASH_WaitForLastOperation function which can be executed from Flash.
//  607 
//  608  Steps of the execution from RAM differs from one toolchain to another:
//  609  - For Cosmic Compiler:
//  610     1- Define a segment FLASH_CODE by the mean of " #pragma section (FLASH_CODE)".
//  611     This segment is defined in the stm8l15x_flash.c file.
//  612   2- Uncomment the "#define RAM_EXECUTION  (1)" line in the stm8l15x.h file,
//  613     or define it in Cosmic compiler preprocessor to enable the FLASH_CODE segment
//  614    definition.
//  615   3- In STVD Select Project\Settings\Linker\Category "input" and in the RAM section
//  616     add the FLASH_CODE segment with "-ic" options.
//  617   4- In main.c file call the _fctcpy() function with first segment character as 
//  618     parameter "_fctcpy('F');" to load the declared moveable code segment
//  619     (FLASH_CODE) in RAM before execution.
//  620   5- By default the _fctcpy function is packaged in the Cosmic machine library,
//  621     so the function prototype "int _fctcopy(char name);" must be added in main.c
//  622     file.
//  623 
//  624   - For Raisonance Compiler
//  625    1- Use the inram keyword in the function declaration to specify that it can be
//  626     executed from RAM.
//  627     This is done within the stm8l15x_flash.c file, and it's conditioned by 
//  628     RAM_EXECUTION definition.
//  629    2- Uncomment the "#define RAM_EXECUTION  (1)" line in the stm8l15x.h file, or 
//  630    define it in Raisonance compiler preprocessor to enable the access for the 
//  631    inram functions.
//  632    3- An inram function code is copied from Flash to RAM by the C startup code. 
//  633    In some applications, the RAM area where the code was initially stored may be
//  634    erased or corrupted, so it may be desirable to perform the copy again. 
//  635    Depending on the application memory model, the memcpy() or fmemcpy() functions
//  636    should be used to perform the copy.
//  637       • In case your project uses the SMALL memory model (code smaller than 64K),
//  638        memcpy()function is recommended to perform the copy
//  639       • In case your project uses the LARGE memory model, functions can be 
//  640       everywhere in the 24-bits address space (not limited to the first 64KB of
//  641       code), In this case, the use of memcpy() function will not be appropriate,
//  642       you need to use the specific fmemcpy() function (which copies objects with
//  643       24-bit addresses).
//  644       - The linker automatically defines 2 symbols for each inram function:
//  645            • __address__functionname is a symbol that holds the Flash address 
//  646            where the given function code is stored.
//  647            • __size__functionname is a symbol that holds the function size in bytes.
//  648      And we already have the function address (which is itself a pointer)
//  649   4- In main.c file these two steps should be performed for each inram function:
//  650      • Import the "__address__functionname" and "__size__functionname" symbols
//  651        as global variables:
//  652          extern int __address__functionname; // Symbol holding the flash address
//  653          extern int __size__functionname;    // Symbol holding the function size
//  654      • In case of SMALL memory model use, Call the memcpy() function to copy the
//  655       inram function to the RAM destination address:
//  656                 memcpy(functionname, // RAM destination address
//  657                       (void*)&__address__functionname, // Flash source address
//  658                       (int)&__size__functionname); // Code size of the function
//  659      • In case of LARGE memory model use, call the fmemcpy() function to copy 
//  660      the inram function to the RAM destination address:
//  661                  memcpy(functionname, // RAM destination address
//  662                       (void @far*)&__address__functionname, // Flash source address
//  663                       (int)&__size__functionname); // Code size of the function
//  664 
//  665  - For IAR Compiler:
//  666     1- Use the __ramfunc keyword in the function declaration to specify that it 
//  667     can be executed from RAM.
//  668     This is done within the stm8l15x_flash.c file, and it's conditioned by 
//  669     RAM_EXECUTION definition.
//  670     2- Uncomment the "#define RAM_EXECUTION  (1)" line in the stm8l15x.h file, or 
//  671    define it in IAR compiler preprocessor to enable the access for the 
//  672    __ramfunc functions.
//  673  
//  674  The Flash_DataProgram example provided within the STM8L15x_StdPeriph_Lib package
//  675  details all the steps described above.
//  676 
//  677 @endverbatim
//  678   * @{
//  679   */
//  680    
//  681 /**
//  682   * @brief
//  683   *******************************************************************************
//  684   *                         Execution from RAM enable
//  685   *******************************************************************************
//  686   *
//  687   * To enable execution from RAM you can either uncomment the following define 
//  688   * in the stm8l15x.h file or define it in your toolchain compiler preprocessor
//  689   * - #define RAM_EXECUTION  (1) 
//  690   */
//  691 
//  692 #if defined (_COSMIC_) && defined (RAM_EXECUTION)
//  693  #pragma section (FLASH_CODE)
//  694 #endif  /* _COSMIC_ && RAM_EXECUTION */
//  695 
//  696 /**
//  697   * @brief  Configures the power state for Flash program and data EEPROM during
//  698   *         run, low power run and low power wait modes
//  699   * @note   This function must be called and executed from RAM.
//  700   * @param  FLASH_Power: power state of the Flash program and data EEPROM
//  701   *          This parameter can be one of the following values:
//  702   *            @arg FLASH_Power_IDDQ: Flash program and data EEPROM in IDDQ
//  703   *            @arg FLASH_Power_On: Flash program and data EEPROM not in IDDQ 
//  704   * @retval None
//  705   */

        SECTION `.near_func.textrw`:CODE:REORDER:NOROOT(0)
        SECTION_TYPE SHT_PROGBITS, SHF_WRITE | SHF_EXECINSTR
        CODE
//  706 IN_RAM(void FLASH_PowerRunModeConfig(FLASH_Power_TypeDef FLASH_Power))
//  707 {
//  708   /* Check parameter */
//  709   assert_param(IS_FLASH_POWER(FLASH_Power));
//  710 
//  711   if (FLASH_Power != FLASH_Power_On)
FLASH_PowerRunModeConfig:
        CP        A, #0x1
        JREQ      L:??FLASH_PowerRunModeConfig_0
//  712   {
//  713   FLASH->CR1 |= (uint8_t)FLASH_CR1_EEPM;
        BSET      L:0x5050, #0x3
        RET
//  714   }
//  715   else
//  716   {
//  717     FLASH->CR1 &= (uint8_t)(~FLASH_CR1_EEPM);
??FLASH_PowerRunModeConfig_0:
        BRES      L:0x5050, #0x3
//  718   }
//  719 }
        RET
//  720 
//  721 /**
//  722   * @brief  Checks the power status for Flash program and data EEPROM
//  723   * @note   This function should be called and executed from RAM.
//  724   * @param  None
//  725   * @retval Flash program and data EEPROM power status
//  726   *         The returned value can be one of the following:
//  727   *         - FLASH_PowerStatus_IDDQDuringWaitMode: IDDQ during Wait mode
//  728   *         - FLASH_PowerStatus_IDDQDuringRunMode: IDDQ during Run mode
//  729   *         - FLASH_PowerStatus_IDDQDuringWaitAndRunModes: IDDQ during Wait/Run mode
//  730   *         - FLASH_PowerStatus_On: Powered on during Wait and Run modes
//  731   */

        SECTION `.near_func.textrw`:CODE:REORDER:NOROOT(0)
        SECTION_TYPE SHT_PROGBITS, SHF_WRITE | SHF_EXECINSTR
        CODE
//  732 IN_RAM(FLASH_PowerStatus_TypeDef FLASH_GetPowerStatus(void))
//  733 {
//  734   return((FLASH_PowerStatus_TypeDef)(FLASH->CR1 & (uint8_t)0x0C));
FLASH_GetPowerStatus:
        LD        A, L:0x5050
        AND       A, #0xc
        RET
//  735 }
//  736 
//  737 /**
//  738   * @brief  Programs a memory block
//  739   * @note   This function should be called and executed from RAM.
//  740   * @param  FLASH_MemType : The type of memory to program
//  741   *          This parameter can be one of the following values:
//  742   *            @arg FLASH_MemType_Program: Program memory
//  743   *            @arg FLASH_MemType_Data: Data EEPROM memory 
//  744   * @param  BlockNum : The block number
//  745   * @param  FLASH_ProgMode : The programming mode.
//  746   *          This parameter can be one of the following values:
//  747   *            @arg FLASH_ProgramMode_Standard: Standard programming mode
//  748   *            @arg FLASH_ProgramMode_Fast: Fast programming mode
//  749   * @param  Buffer : Pointer to buffer containing source data.
//  750   * @retval None.
//  751   */

        SECTION `.near_func.textrw`:CODE:REORDER:NOROOT(0)
        SECTION_TYPE SHT_PROGBITS, SHF_WRITE | SHF_EXECINSTR
        CODE
//  752 IN_RAM(void FLASH_ProgramBlock(uint16_t BlockNum, FLASH_MemType_TypeDef FLASH_MemType,
//  753                         FLASH_ProgramMode_TypeDef FLASH_ProgMode, uint8_t *Buffer))
//  754 {
FLASH_ProgramBlock:
        PUSH      S:?b11
        PUSH      S:?b10
        PUSH      S:?b9
        PUSH      S:?b8
        PUSH      S:?b15
        PUSH      S:?b14
        PUSH      S:?b12
        LDW       S:?w1, X
        MOV       S:?b12, S:?b0
        LDW       S:?w7, Y
//  755   uint16_t Count = 0;
        CLR       S:?b1
        CLR       S:?b0
//  756   uint32_t startaddress = 0;
        CLRW      X
        LDW       S:?w5, X
        LDW       S:?w4, X
//  757 
//  758   /* Check parameters */
//  759   assert_param(IS_FLASH_MEMORY_TYPE(FLASH_MemType));
//  760   assert_param(IS_FLASH_PROGRAM_MODE(FLASH_ProgMode));
//  761   if (FLASH_MemType == FLASH_MemType_Program)
        CP        A, #0xfd
        JRNE      L:??FLASH_ProgramBlock_0
//  762   {
//  763   assert_param(IS_FLASH_PROGRAM_BLOCK_NUMBER(BlockNum));
//  764     startaddress = FLASH_PROGRAM_START_PHYSICAL_ADDRESS;
        LDW       X, #0x8000
        LDW       S:?w5, X
        SLLW      X
        LDW       S:?w4, X
        JRA       L:??FLASH_ProgramBlock_1
//  765   }
//  766   else
//  767   {
//  768     assert_param(IS_FLASH_DATA_EEPROM_BLOCK_NUMBER(BlockNum));
//  769     startaddress = FLASH_DATA_EEPROM_START_PHYSICAL_ADDRESS;
??FLASH_ProgramBlock_0:
        LDW       X, #0x1000
        LDW       S:?w5, X
        CLRW      X
        LDW       S:?w4, X
//  770   }
//  771 
//  772   /* Point to the first block address */
//  773   startaddress = startaddress + ((uint32_t)BlockNum * FLASH_BLOCK_SIZE);
??FLASH_ProgramBlock_1:
        CLRW      X
        LDW       S:?w0, X
        LDW       X, #0x80
        LDW       S:?w3, X
        CLRW      X
        LDW       S:?w2, X
        CALL      L:?mul32_l0_l0_l1
        MOV       S:?b7, S:?b3
        MOV       S:?b6, S:?b2
        MOV       S:?b5, S:?b1
        MOV       S:?b4, S:?b0
        MOV       S:?b3, S:?b11
        MOV       S:?b2, S:?b10
        MOV       S:?b1, S:?b9
        MOV       S:?b0, S:?b8
        CALL      L:?add32_l0_l0_l1
        MOV       S:?b11, S:?b3
        MOV       S:?b10, S:?b2
        MOV       S:?b9, S:?b1
        MOV       S:?b8, S:?b0
//  774 
//  775   /* Selection of Standard or Fast programming mode */
//  776   if (FLASH_ProgMode == FLASH_ProgramMode_Standard)
        TNZ       S:?b12
        JRNE      L:??FLASH_ProgramBlock_2
//  777   {
//  778   /* Standard programming mode */
//  779   FLASH->CR2 |= FLASH_CR2_PRG;
        BSET      L:0x5051, #0x0
        JRA       L:??FLASH_ProgramBlock_3
//  780   }
//  781   else
//  782   {
//  783   /* Fast programming mode */
//  784   FLASH->CR2 |= FLASH_CR2_FPRG;
??FLASH_ProgramBlock_2:
        BSET      L:0x5051, #0x4
//  785   }
//  786 
//  787   /* Copy data bytes from RAM to FLASH memory */
//  788   for (Count = 0; Count < FLASH_BLOCK_SIZE; Count++)
??FLASH_ProgramBlock_3:
        CLR       S:?b1
        CLR       S:?b0
        JRA       L:??FLASH_ProgramBlock_4
//  789   {
//  790     *((PointerAttr uint8_t*) (MemoryAddressCast)startaddress + Count) = ((uint8_t)(Buffer[Count]));
??FLASH_ProgramBlock_5:
        LDW       X, S:?w5
        ADDW      X, S:?w0
        LDW       Y, X
        LDW       X, S:?w0
        ADDW      X, S:?w7
        LD        A, (X)
        LD        (Y), A
//  791   }
        LDW       X, S:?w0
        INCW      X
        LDW       S:?w0, X
??FLASH_ProgramBlock_4:
        LDW       X, S:?w0
        CPW       X, #0x80
        JRC       L:??FLASH_ProgramBlock_5
//  792 }
        POP       S:?b12
        POP       S:?b14
        POP       S:?b15
        JP        L:?epilogue_l2
//  793 
//  794 /**
//  795   * @brief  Erases a block in the program or data memory.
//  796   * @note   This function should be called and executed from RAM.
//  797   * @param  BlockNum : Indicates the block number to erase
//  798   * @param  FLASH_MemType :  The type of memory to erase
//  799   *          This parameter can be one of the following values:
//  800   *            @arg FLASH_MemType_Program: Program memory
//  801   *            @arg FLASH_MemType_Data: Data EEPROM memory 
//  802   * @retval None.
//  803   */

        SECTION `.near_func.textrw`:CODE:REORDER:NOROOT(0)
        SECTION_TYPE SHT_PROGBITS, SHF_WRITE | SHF_EXECINSTR
        CODE
//  804 IN_RAM(void FLASH_EraseBlock(uint16_t BlockNum, FLASH_MemType_TypeDef FLASH_MemType))
//  805 {
FLASH_EraseBlock:
        LDW       Y, X
//  806   uint32_t startaddress = 0;
        CLRW      X
        LDW       S:?w3, X
        LDW       S:?w2, X
//  807 #if defined (STM8L15X_MD) || defined (STM8L15X_MDP) || defined (STM8L15X_LD) || \ 
//  808 defined (STM8L05X_LD_VL) || defined (STM8L05X_MD_VL) || defined (STM8AL31_L_MD)
//  809   uint32_t PointerAttr  *pwFlash;
//  810   
//  811 #elif defined (STM8L15X_HD) || defined (STM8L05X_HD_VL)
//  812   uint8_t PointerAttr  *pwFlash;
//  813 #endif
//  814 
//  815   /* Check parameters */
//  816   assert_param(IS_FLASH_MEMORY_TYPE(FLASH_MemType));
//  817   if (FLASH_MemType == FLASH_MemType_Program)
        CP        A, #0xfd
        JRNE      L:??FLASH_EraseBlock_0
//  818   {
//  819   assert_param(IS_FLASH_PROGRAM_BLOCK_NUMBER(BlockNum));
//  820     startaddress = FLASH_PROGRAM_START_PHYSICAL_ADDRESS;
        LDW       X, #0x8000
        LDW       S:?w3, X
        SLLW      X
        LDW       S:?w2, X
        JRA       L:??FLASH_EraseBlock_1
//  821   }
//  822   else
//  823   {
//  824     assert_param(IS_FLASH_DATA_EEPROM_BLOCK_NUMBER(BlockNum));
//  825     startaddress = FLASH_DATA_EEPROM_START_PHYSICAL_ADDRESS;
??FLASH_EraseBlock_0:
        LDW       X, #0x1000
        LDW       S:?w3, X
        CLRW      X
        LDW       S:?w2, X
//  826   }
//  827 
//  828   /* Point to the first block address */
//  829 #if defined (STM8L15X_MD) || defined (STM8L15X_MDP)|| defined (STM8L15X_LD) || \ 
//  830 defined (STM8L05X_LD_VL) || defined (STM8L05X_MD_VL) || defined (STM8AL31_L_MD)
//  831   pwFlash = (PointerAttr uint32_t *)(uint16_t)(startaddress + ((uint32_t)BlockNum * FLASH_BLOCK_SIZE));
??FLASH_EraseBlock_1:
        LDW       X, #0x80
        LDW       S:?w0, X
        LDW       X, Y
        CALL      L:?mul16_x_x_w0
        LDW       Y, S:?w3
        LDW       S:?w0, X
        ADDW      Y, S:?w0
//  832   
//  833 #elif defined (STM8L15X_HD) || defined (STM8L05X_HD_VL)
//  834   pwFlash = (PointerAttr uint8_t *)(uint32_t)(startaddress + ((uint32_t)BlockNum * FLASH_BLOCK_SIZE));
//  835 #endif
//  836 
//  837   /* Enable erase block mode */
//  838   FLASH->CR2 |= FLASH_CR2_ERASE;
        BSET      L:0x5051, #0x5
//  839 
//  840 #if defined (STM8L15X_MD) || defined (STM8L15X_MDP) || defined (STM8L15X_LD) || \ 
//  841 defined (STM8L05X_LD_VL) || defined (STM8L05X_MD_VL) || defined (STM8AL31_L_MD)
//  842   *pwFlash = (uint32_t)0;  
        CLRW      X
        LDW       S:?w1, X
        LDW       S:?w0, X
        LDW       X, Y
        LD        A, S:?b0
        LD        (X), A
        LD        A, S:?b1
        LD        (L:0x1,X), A
        LD        A, S:?b2
        LD        (L:0x2,X), A
        LD        A, S:?b3
        LD        (L:0x3,X), A
//  843 
//  844 #elif defined (STM8L15X_HD) || defined (STM8L05X_HD_VL)
//  845   *pwFlash = (uint8_t)0;
//  846   *(pwFlash + 1) = (uint8_t)0;
//  847   *(pwFlash + 2) = (uint8_t)0;
//  848   *(pwFlash + 3) = (uint8_t)0;
//  849 #endif
//  850 }
        RET
//  851 
//  852 /**
//  853   * @brief  Waits for a Flash operation to complete.
//  854   * @note   The call and execution of this function must be done from RAM in case
//  855   *         of Block operation, otherwise it can be executed from Flash
//  856   * @param  FLASH_MemType : Memory type
//  857   *          This parameter can be one of the following values:
//  858   *            @arg FLASH_MemType_Program: Program memory
//  859   *            @arg FLASH_MemType_Data: Data EEPROM memory 
//  860   * @retval FLASH status
//  861   */

        SECTION `.near_func.textrw`:CODE:REORDER:NOROOT(0)
        SECTION_TYPE SHT_PROGBITS, SHF_WRITE | SHF_EXECINSTR
        CODE
//  862 IN_RAM(FLASH_Status_TypeDef FLASH_WaitForLastOperation(FLASH_MemType_TypeDef FLASH_MemType))
//  863 {
//  864   uint32_t timeout = OPERATION_TIMEOUT;
FLASH_WaitForLastOperation:
        CLRW      X
        LDW       S:?w2, X
        DECW      X
        LDW       S:?w3, X
//  865   uint8_t flagstatus = 0x00;
        CLR       S:?b0
//  866   /* Wait until operation completion or write protected page occurred */
//  867   if (FLASH_MemType == FLASH_MemType_Program)
        CP        A, #0xfd
        JRNE      L:??FLASH_WaitForLastOperation_0
//  868   {
//  869   while ((flagstatus == 0x00) && (timeout != 0x00))
??FLASH_WaitForLastOperation_1:
        TNZ       S:?b0
        JRNE      L:??FLASH_WaitForLastOperation_2
        LDW       X, S:?w2
        CPW       X, #0x0
        JRNE      L:??FLASH_WaitForLastOperation_3
        LDW       X, S:?w3
        CPW       X, #0x0
??FLASH_WaitForLastOperation_3:
        JREQ      L:??FLASH_WaitForLastOperation_2
//  870     {
//  871       flagstatus = (uint8_t)(FLASH->IAPSR & (uint8_t)(FLASH_IAPSR_EOP |
//  872                              FLASH_IAPSR_WR_PG_DIS));
        LD        A, L:0x5054
        AND       A, #0x5
        LD        S:?b0, A
//  873       timeout--;
        CALL      L:?dec32_l1_l1
        JRA       L:??FLASH_WaitForLastOperation_1
//  874     }
//  875   }
//  876   else
//  877   {
//  878     while ((flagstatus == 0x00) && (timeout != 0x00))
//  879     {
//  880       flagstatus = (uint8_t)(FLASH->IAPSR & (uint8_t)(FLASH_IAPSR_HVOFF |
//  881                              FLASH_IAPSR_WR_PG_DIS));
??FLASH_WaitForLastOperation_4:
        LD        A, L:0x5054
        AND       A, #0x41
        LD        S:?b0, A
//  882       timeout--;
        CALL      L:?dec32_l1_l1
//  883     }
??FLASH_WaitForLastOperation_0:
        TNZ       S:?b0
        JRNE      L:??FLASH_WaitForLastOperation_2
        LDW       X, S:?w2
        CPW       X, #0x0
        JRNE      L:??FLASH_WaitForLastOperation_5
        LDW       X, S:?w3
        CPW       X, #0x0
??FLASH_WaitForLastOperation_5:
        JRNE      L:??FLASH_WaitForLastOperation_4
//  884   }
//  885   if (timeout == 0x00 )
??FLASH_WaitForLastOperation_2:
        LDW       X, S:?w2
        CPW       X, #0x0
        JRNE      L:??FLASH_WaitForLastOperation_6
        LDW       X, S:?w3
        CPW       X, #0x0
??FLASH_WaitForLastOperation_6:
        JRNE      L:??FLASH_WaitForLastOperation_7
//  886   {
//  887   flagstatus = FLASH_Status_TimeOut;
        MOV       S:?b0, #0x2
//  888   }
//  889 
//  890   return((FLASH_Status_TypeDef)flagstatus);
??FLASH_WaitForLastOperation_7:
        LD        A, S:?b0
        RET
//  891 }

        SECTION VREGS:DATA:REORDER:NOROOT(0)

        END
//  892 
//  893 #if defined (_COSMIC_) && defined (RAM_EXECUTION)
//  894  /* End of FLASH_CODE section */
//  895  #pragma section ()
//  896 #endif /* _COSMIC_ && RAM_EXECUTION */
//  897 
//  898 /**
//  899   * @}
//  900   */
//  901 
//  902 /**
//  903   * @}
//  904   */
//  905    
//  906   /**
//  907   * @}
//  908   */ 
//  909   
//  910   /**
//  911   * @}
//  912   */ 
//  913   
//  914 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
// 
// 283 bytes in section .near_func.text
// 361 bytes in section .near_func.textrw
// 
// 644 bytes of CODE memory
//
//Errors: none
//Warnings: 6
