#ifndef _USER_FUNC_H_
#define _USER_FUNC_H_

#include <xs1.h>
#include <print.h>
#include <platform.h>
#include <xclib.h>
#include <xscope.h>
#include <assert.h>
#include "user_uart.h"

// 32-bit macros for converting between host and network byte order
#define HTONL(value) ( \
    (((value) >> 24) & 0x000000FF) | \
    (((value) >>  8) & 0x0000FF00) | \
    (((value) <<  8) & 0x00FF0000) | \
    (((value) << 24) & 0xFF000000) \
)

#define NTOHL(value) HTONL(value) // Since the transformation is the same

// 16-bit macros for converting between host and network byte order
#define HTONS(value) ( \
    (((value) >> 8) & 0x00FF) | \
    (((value) << 8) & 0xFF00) \
)

#define NTOHS(value) HTONS(value) // Since the transformation is the same

#ifdef __XC__
uint32_t calculate_crc32(uint8_t *unsafe buf, int len);
unsigned char check_sum(uint8_t *unsafe pbuf, unsigned len);
void fill_boot_cmd(unsigned char *unsafe buffer);
void save_product_info(unsigned char *unsafe buffer);
void save_powerup_info(unsigned char *unsafe buffer);
void fill_finish_cmd(unsigned char *unsafe buffer);
void save_userconfig_info(unsigned char *unsafe buffer);
#else
uint32_t calculate_crc32(uint8_t *buf, int len);
unsigned char check_sum(uint8_t *pbuf, unsigned len);
void fill_boot_cmd(unsigned char *buffer);
void save_product_info(unsigned char *buffer);
void save_powerup_info(unsigned char *buffer);
void fill_finish_cmd(unsigned char *buffer);
void save_userconfig_info(unsigned char *buffer);
#endif
void load_configs(void);
void save_configs(unsigned char save);
void get_pid_vid();
#ifdef __XC__
void save_value(unsigned char *unsafe path, unsigned char value);
unsigned char load_value(unsigned char *unsafe path);
void save_value32(unsigned char *unsafe path, unsigned value);
unsigned load_value32(unsigned char *unsafe path);
#else
void save_value(unsigned char *path, unsigned char value);
unsigned char load_value(unsigned char *path);
void save_value32(unsigned char *path, unsigned value);
unsigned load_value32(unsigned char *path);
#endif

#endif

