/*
 * $Id: efm8ub2_hwid.c 464 2019-08-01 02:15:54Z true $
 * begin 20190527 true
 */

#include "driver/efm8ub2_hwid.h"


uint8_t xdata *hwid;


void hwid_init()
{
	hwid = HWID_ADDR_BASE;
}

/*
 * EFM8UB20F32G and F64G don't have any built-in way to identify version.
 * however the memory map loops, so we can check and verify these looped
 * addresses to determine which of the two models we have.
 *
 * potential bug note: if random ID is equal to zero, a 32K model may
 * be identified as 64K model. right now I give no shits, fix it later.
 */
uint8_t hwid_get_model()
{
	uint8_t xdata *a;
	uint16_t s1 = 0;
	uint16_t s2 = 0;

	a = 0x7ff;
	while (a > 0x7f0) {
		s1 += *a--;
	}

	a = 0xfff;
	while (a > 0xff0) {
		s2 += *a--;
	}

	if (s1 == s2) {
		return HWID_MODEL_F32G;
	}

	return HWID_MODEL_F64G;
}

uint8_t hwid_get_byte(uint8_t byte)
{
	if (byte < HWID_LENGTH) {
		return hwid[byte];
	}
	return 0;
}
