//#include <stdio.h>
//#include <stdlib.h>
//#include <string.h>

int faculty(int f0, int *pDst, int len)
{
	int i=0;

	pDst[i++] = f0;

	for (; i < len; i++)
	{
		pDst[i] = pDst[i-1] * i;
	}

	return i;
}

#define	NUM_ELEMENTS	20

void saus(char c)
{
	volatile char *pUART_stat = (char*)0x80000008;
	volatile char *pUART_data = (char*)0x80000004;

	while((0x02 & *pUART_stat) != 0);

	*pUART_data = c;
}

int sputs(char *pStr)
{
	char *start;
	start = pStr;

	while(*pStr)
		saus(*(pStr++));

	return pStr - start;
}

void print_word(int word)
{
	int i;
	unsigned char c, nibble;

	for (i=0; i < 8; i++)

	{
		nibble = (char)((word >> 28) & 0xF);
		word <<= 4;

		if (nibble < 10)
			c = nibble + '0';
		else
			c = nibble + 'A' - 10;

		saus(c);
	}

}

int main (void)
{
	int buf[NUM_ELEMENTS], result, i, j, cnt;
	volatile int *pReg = (int*)0x80000000;
	volatile char *pUART_data = (char*)0x80000004;
	volatile char *pUART_ctrl = (char*)0x80000008;
	volatile char *pUART_baud = (char*)0x80000009;
	char msg[] = "MIPS R3000 CPU\r\nReady\r\n";

	*pUART_baud = 0x35;
	*pUART_ctrl = 0x55;

	cnt = 0;
	memset(buf, 0, sizeof(buf));

	sputs(msg);

	buf[NUM_ELEMENTS-1] = 1;
	while (1)
	{
		result = faculty(buf[NUM_ELEMENTS-1], buf, NUM_ELEMENTS);

		for (i=0; i < NUM_ELEMENTS; i++)
		{
			print_word(buf[i]);
			saus(13);
			saus(10);
		}
		cnt++;

		while(1);
	}
}

