/*
 * sys_debug.c
 *
 *  Created on: 2016/12/02
 *      Author: takayun
 */

#include "sys_debug.h"
#include <stdio.h>
#include "lcd_out.h"
#include <unistd.h>

void display_inst(struct InstRec inst, unsigned int pc) {
	char inst_name[INST_NAME_ARRAY_LEN];	// 命令名
	char reg_name[REG_NAME_ARRAY_LEN];		// レジスタ名
	char buf[17];

	// 命令の名前の取得
	convertInstName(inst_name, inst.inst);
	// レジスタの名前の取得
	convertRegName(reg_name, inst.regi);

	lcd_caret_reset();
	sprintf(buf, "PC:0x%02x -> %4s",pc,inst_name);
	lcd_print(buf);
	lcd_caret_reset2();
	sprintf(buf, "REG:%3s,MEM:0x%1x",reg_name,inst.memi);
	lcd_print(buf);
}

void display_mem(unsigned char memi, char memv) {
	char buf[17];

	lcd_caret_reset();
	sprintf(buf, "MEM:0x%1x",memi);
	lcd_print(buf);
	lcd_caret_reset2();
	sprintf(buf, "value:%d",memv);
	lcd_print(buf);
}

void convertRegName(char reg_name[REG_NAME_ARRAY_LEN], enum Register reg_code) {
	switch(reg_code) {
	case Szero:
		sprintf(reg_name, STRING_REG_ZERO);
		break;
	case Spc:
		sprintf(reg_name, STRING_REG_PC);
		break;
	case Ssp:
		sprintf(reg_name, STRING_REG_SP);
		break;
	case Sgp0:
		sprintf(reg_name, STRING_REG_GP0);
		break;
	case Sgp1:
		sprintf(reg_name, STRING_REG_GP1);
		break;
	case Sacc:
		sprintf(reg_name, STRING_REG_ACC);
		break;
	case Sflg:
		sprintf(reg_name, STRING_REG_FLG);
		break;
	default:
		sprintf(reg_name, "non");
		break;
	}
}


void convertInstName(char inst_name[INST_NAME_ARRAY_LEN], unsigned char inst_code) {
	switch(inst_code) {
	case INST_END:
		sprintf(inst_name, STRING_INST_END);
		break;
	case INST_JUMP:
		sprintf(inst_name, STRING_INST_JUMP);
		break;
	case INST_OUTPUT:
		sprintf(inst_name, STRING_INST_OUTPUT);
		break;
	case INST_LOAD:
		sprintf(inst_name, STRING_INST_LOAD);
		break;
	case INST_STORE:
		sprintf(inst_name, STRING_INST_STORE);
		break;
	case INST_DELAY:
		sprintf(inst_name, STRING_INST_DELAY);
		break;
	case INST_ADD:
		sprintf(inst_name, STRING_INST_ADD);
		break;
	case INST_SUB:
		sprintf(inst_name, STRING_INST_SUB);
		break;
	case INST_COMP:
		sprintf(inst_name, STRING_INST_COMP);
		break;
	case INST_JEQ:
		sprintf(inst_name, STRING_INST_JEQ);
		break;
	case INST_JNE:
		sprintf(inst_name, STRING_INST_JNE);
		break;
	case INST_JIEQ:
		sprintf(inst_name, STRING_INST_JIEQ);
		break;
	case INST_JINE:
		sprintf(inst_name, STRING_INST_JINE);
		break;
	default:
		sprintf(inst_name, "NoOp");
		break;
	}
}

void delay10ms(unsigned int s) {
	usleep(s*10000);
}
