#include <stdint.h>
#include "defines.h"

extern uint32_t _data_size, _bss_size, _data_loadaddr, _sdata, _sbss;
void main(void);
void __libc_init_array();

void reset_handler(void)
{
  // Copy .data from FLASH to SRAM
  uint32_t data_size = (uint32_t)&_data_size;
  uint8_t *flash_data = (uint8_t*) &_data_loadaddr;
  uint8_t *sram_data = (uint8_t*) &_sdata;

  for (uint32_t i = 0; i < data_size; i++)
  {
    sram_data[i] = flash_data[i];
  }

  // Zero-fill .bss section in SRAM
  uint32_t bss_size = (uint32_t)&_bss_size;
  uint8_t *bss = (uint8_t*) &_sbss;

  for (uint32_t i = 0; i < bss_size; i++)
  {
    bss[i] = 0;
  }

  __libc_init_array();
  main();
}
