#include <sys/times.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <errno.h>
#include <stdlib.h>
#include "libsys.h"
#include "xcpt.h"
#include "irq.h"

static fp_xcpt_t g_xcpt_handler[MAX_NUM_XCPT] = {NULL};

char *_xcpt_code_str[MAX_NUM_XCPT] =
{
	"Int", "Mod", "TLBL", "TLBS", "AdEL", "AdES", "IBE", "DBE",
	"Sys", "Bp", "RI", "CpU", "Ov", "Tr", "NCD/VCEI", "MC/FPE",
	"Res(16)", "Res(17)", "Res(18)", "Res(19)", "Res(20)", "Res(21)", "Res(22)", "WATCH",
	"Res(24)", "Res(25)", "Res(26)", "Res(27)", "Res(28)", "Res(29)", "Res(30)", "VCED"
 };

void xcpt_register(int xcpt_num, fp_xcpt_t fp)
{
	if ((xcpt_num >= Mod) && (xcpt_num <= VCED))
		g_xcpt_handler[xcpt_num] = fp;

}

int _xcpt_dispatch(struct xcptcontext * xcp)
{
	int xcpt_code, result, i;

	xcpt_code =  ((0xFF & xcp->cr) >> 2);

	result = XCPT_ERR_NOTHANDLED;
	switch(xcpt_code)
	{
		case Int:
		result = _irq_dispatch(xcp);
		break;

		default:
		if (g_xcpt_handler[xcpt_code])
		{
			result = (g_xcpt_handler[xcpt_code])(xcp);
		}
		break;
	}
	return result;
}

int _xcpt_deliver(struct xcptcontext * _xcp)
{

	int exc_code;
	volatile int *pInstr;

	/* This function gets called by the low-level exception handler. */
	if (_xcpt_dispatch(_xcp) == XCPT_ERR_NOTHANDLED)
	{
		sputs("\n");
		PRINT_REG(" Status : ", _xcp->sr);
		PRINT_REG("  Cause : ", _xcp->cr);
		PRINT_REG("    EPC : ", _xcp->epc);
		PRINT_REG("BadAddr : ", _xcp->baddr);
		sputs("\n");
		PRINT_REG("   MDLO : ", _xcp->mdlo);
		PRINT_REG("   MDHI : ", _xcp->mdhi);

		sputs("\n");

		sputs("Registers:\n");
		PRINT_REG(" 0 (ze) : ", _xcp->regs[0]);
		PRINT_REG(" 1 (at) : ", _xcp->regs[1]);
		PRINT_REG(" 2 (v0) : ", _xcp->regs[2]);
		PRINT_REG(" 3 (v1) : ", _xcp->regs[3]);
		sputs("\n");
		PRINT_REG(" 4 (a0) : ", _xcp->regs[4]);
		PRINT_REG(" 5 (a1) : ", _xcp->regs[5]);
		PRINT_REG(" 6 (a2) : ", _xcp->regs[6]);
		PRINT_REG(" 7 (a3) : ", _xcp->regs[7]);
		sputs("\n");
		PRINT_REG(" 8 (t0) : ", _xcp->regs[8]);
		PRINT_REG(" 9 (t1) : ", _xcp->regs[9]);
		PRINT_REG("10 (t2) : ", _xcp->regs[10]);
		PRINT_REG("11 (t3) : ", _xcp->regs[11]);
		sputs("\n");
		PRINT_REG("12 (t4) : ", _xcp->regs[12]);
		PRINT_REG("13 (t5) : ", _xcp->regs[13]);
		PRINT_REG("14 (t6) : ", _xcp->regs[14]);
		PRINT_REG("15 (t7) : ", _xcp->regs[15]);
		sputs("\n");
		PRINT_REG("16 (s0) : ", _xcp->regs[16]);
		PRINT_REG("17 (s1) : ", _xcp->regs[17]);
		PRINT_REG("18 (s2) : ", _xcp->regs[18]);
		PRINT_REG("19 (s3) : ", _xcp->regs[19]);
		sputs("\n");
		PRINT_REG("20 (s4) : ", _xcp->regs[20]);
		PRINT_REG("21 (s5) : ", _xcp->regs[21]);
		PRINT_REG("22 (s6) : ", _xcp->regs[22]);
		PRINT_REG("23 (s7) : ", _xcp->regs[23]);
		sputs("\n");
		PRINT_REG("24 (t8) : ", _xcp->regs[24]);
		PRINT_REG("25 (t9) : ", _xcp->regs[25]);
		PRINT_REG("26 (k0) : ", _xcp->regs[26]);
		PRINT_REG("27 (k1) : ", _xcp->regs[27]);
		sputs("\n");
		PRINT_REG("28 (gp) : ", _xcp->regs[28]);
		PRINT_REG("29 (sp) : ", _xcp->regs[29]);
		PRINT_REG("30 (fp) : ", _xcp->regs[30]);
		PRINT_REG("31 (ra) : ", _xcp->regs[31]);
		sputs("\n");
		sputs("\n");

		exc_code = (_xcp->cr >> 2) & 0x1F;

		sputs("Unhandled exception <");
		sputs(_xcpt_code_str[exc_code]);
		sputs("> at PC : ");

		if (_xcp->cr & 0x80000000)
		{
			pInstr = (int*)(_xcp->epc + 4);
		}
		else
		{
			pInstr = (int*)(_xcp->epc);
		}
		print_word((int)pInstr);
		sputs("\n");

		sputs("Instruction at exception address : ");
		print_word(*pInstr);
		sputs("\n");

		sputs("Terminate.\n");
		_exit(exc_code);
	}

	return 0;
}
