#include "libsys_boot.h"

typedef struct _ssrec_t
{
	UINT32 addr;
	UINT32 size;
	UINT32 type;
	UINT8 *data;

} srec_t;

enum srec_type
{
	srec_err, srec_sob, srec_data, srec_rec, srec_eob
};

UINT8 h2i(UINT8 *c)
{
	int i;
	UINT8 t, b = 0;

	for (i=0; i < 2; i++)
	{
		t = c[i];
		if (t >= 'A')
			t = t - 'A' + 10;
		else
			t -= '0';

		b = (b << 4) | t;
	}
	return b;
}

int decode_srec(srec_t *pRec, UINT8 *pBuf, int buflen)
{
	UINT8 chksum, byte;
	int alen, i;

	if (!buflen)
		return srec_err;

	pRec->type = srec_rec;
	alen = 0;
	if ((pBuf[1] > '0') && (pBuf[1] < '4'))
	{
	  alen = pBuf[1] - '0' + 1;
	  pRec->type = srec_data;
	}
	else if ((pBuf[1] >= '7') && (pBuf[1] <= '9'))
	{
	  alen = 11 - (pBuf[1] - '0');
	  pRec->type = srec_eob;
	}
	else if (pBuf[1] == '0')
	{
	  alen = 2;
	  pRec->type = srec_sob;
	}

	byte = h2i(&pBuf[2]);
	pRec->size = byte;
	chksum = byte;

	pRec->addr = 0;
	for (i=0; i < alen; i++)
	{
		byte = h2i(&pBuf[4+2*i]);
        pRec->addr = pRec->addr << 8 | byte;
		chksum += byte;
	}
	pRec->size -= (alen+1);

	pRec->data = (UINT8*) &pBuf[4 + 2*alen];
	for (i=0; i < (int)pRec->size; i++)
	{
		byte = h2i(&pRec->data[2*i]);
		pRec->data[i] = byte;
		chksum += byte;
	}
	chksum = ~chksum;
	byte = h2i(&pRec->data[2*i]);
	if (chksum != byte)
		return 0;

	return pRec->type;
}

int srec_getline(UINT8 *pLine)
{
	char c;
	int i = 0;

	do
	{
		c = readchar();

	} while ((c != 's') && (c != 'S'));

	while(((c != 0x0D) && (c != 0x0A)))
	{
		pLine[i++] = c;
		c = readchar();
	};

	return i;
}

void Jump_to(void *pEntry)
{
	__asm
	(
		".set	noreorder\n"
	);
	__asm
	(
		"jr		%[pEntry]\n"				// jump entry
		:
		: [pEntry] "r" (pEntry)
	);
	__asm
	(
		"nop\n"
	);
	__asm
	(
		".set	reorder\n"
	);
}

void Exec_at(void *pEntry)
{
	__asm
	(
		".set	noreorder\n"
	);

	__asm
	(
		"mfc0	$26, $12\n"			// change exception vector
		"li		$27, 0xFFBFFFFF\n"
		"and	$26, $27\n"
		"mtc0	$26, $12\n"
	);

	__asm
	(
		"lw		$v0, 16($sp)\n"
		"nop\n"
		"jr		$v0\n"				// jump entry
		"rfe\n"
	);

	__asm
	(
		".set	reorder\n"
	);
}
void PrintCPUinfo(void)
{
	int result, rev_id;

	char *cpu_type_str[7] = {"invalid", "R2000", "R3000", "R6000", "R4000", "reserved", "R6000A"};

	// Print Status register
	result = CP0_SR_read();

	sputs("Status  : ");
	print_word(result);
	sputs("\n");

	// Print Revision
	result = CP0_PRID_read();
	rev_id = (result >> 8) & 0xFF;
	sputs("CPU type: ");
	if ((rev_id > 0) && (rev_id < 0x07))
	{
		sputs(cpu_type_str[rev_id]);
		sputs(" Rev.");
		rev_id = result & 0xFF;
		print_byte((char)rev_id);
	}
	else
		sputs("Unknown");

	sputs("\n");

}

#define	MAX_IMG_SIZE		(1024*1024)	// bytes

int main(int argc, char *argv[])
{
	UINT8 buf[256];
	srec_t srec;
	int result, srec_state, i;
	UINT32 haddr;
	volatile UINT8 *pMem;
	haddr = 0;

	// ----------------------------------------------------------
	sputs("\n\n");
	PrintCPUinfo();
	sputs("Booting from UART..");

	while(1)
	{
		sputs(".");
		while(1)
		{
			result = srec_getline(buf);
			srec_state = decode_srec(&srec, buf, result);
			switch (srec_state)
			{
				case srec_data:
					if ((srec.addr + srec.size) > haddr)
						haddr = srec.addr + srec.size;

					pMem = (UINT8*)srec.addr;
					for (i=0; i < srec.size; i++)
						*pMem++ = srec.data[i];

					break;

				case srec_eob:
					sputs("done\n\n");
					sputs("Execute program at ");
					print_word(srec.addr);
					sputs("\n\n");

					Exec_at((void*)srec.addr);
					break;

				default:
					result = -1;
					break;
			}
			if (result < 0)
			{
				break;
			}

		};
	}
	return 0;
}

