/* Generated by TiZi -- UserLogic B partition (Flash @ 0x00004000) */
/* DO NOT EDIT -- regenerate via TiZi Build                         */
#include "shared_interface.h"
#include "iec_std_lib.h"
#include "config.h"

/* matiec runtime globals required by iec_std_lib */
TIME __CURRENT_TIME;
BOOL __DEBUG = 0;

/* matiec config interface (defined in config.c / resource*.c) */
extern void config_init__(void);
extern void config_run__(unsigned long tick);

/* B-zone RAM init: copy .data from Flash, zero .bss
 * Linker symbols provided by lpc824_user.ld               */
extern unsigned int _etext_b, _data_b, _edata_b, _bss_b, _ebss_b;
static void user_ram_init(void) {
    unsigned int *src = &_etext_b;
    unsigned int *dst = &_data_b;
    while (dst < &_edata_b) *dst++ = *src++;
    dst = &_bss_b;
    while (dst < &_ebss_b) *dst++ = 0u;
}

static const SystemAPI_t *g_api  = 0;
static unsigned long      s_tick = 0u;

static void setup(const SystemAPI_t *api) {
    user_ram_init();
    g_api = api;
    config_init__();
    g_api->uart_puts("UserLogic B: PLC ready\r\n");
}

static void loop(void) {
    unsigned int ms = g_api->get_tick_ms();
    __CURRENT_TIME.tv_sec  = (long)(ms / 1000u);
    __CURRENT_TIME.tv_nsec = (long)((ms % 1000u) * 1000000u);
    config_run__(s_tick++);
}

/* Interface table: MUST be first in .user_header section at 0x00004000 */
const UserLogic_t user_api __attribute__((section(".user_header"))) = {
    .magic    = USER_LOGIC_MAGIC,
    .version  = USER_LOGIC_VERSION,
    .setup    = setup,
    .loop     = loop,
    .di_count = PLC_DI_COUNT,
    .do_count = PLC_DO_COUNT,
    .scan_ms  = 0u,   /* 0 = use Runtime A default scan period */
};
