/*
 * input_int.c
 *
 *  Created on: 2016/11/24
 *      Author: takayun
 */
#include "input_int.h"
#include "sys_register.h"

unsigned char PUSH_EVENT = PUSH_NONE;

void in_int() {
	push_int();
}

static void update_sw_reg(sw_t s) {
	global_registers[Ssw_data] = (char)s.data.value;
	global_registers[Ssw_inst] = (char)s.splited.instruction_code;
	global_registers[Ssw_memi] = (char)s.splited.memory_index;
	global_registers[Ssw_regi] = (char)s.splited.register_index;
	global_registers[Ssw_psel] = (char)s.splited.program_selecter;
	global_registers[Ssw_rw] = (char)s.splited.rw_mode;
	global_registers[Ssw_run] = (char)s.splited.run_mode;
}

enum PushEvent push_decode(char psw) {
	int result = PUSH_NONE;
	switch(psw) {
	case 0x3:
		result += PUSH_ANY;
		result += PUSH_VALSTR;
		break;
	case 0x5:
		result += PUSH_ANY;
		result += PUSH_INSSTR;
		break;
	case 0x6:
		result += PUSH_ANY;
		result += PUSH_RUN;
		break;
	}
	return result;
}

void push_int() {
	static unsigned char status = 0;
	static enum PushEvent event_code;
	volatile sw_t s;
	s.sw = *switches;

	switch (status) {
	case 0:
		PUSH_EVENT = PUSH_NONE;
		if (*push_switches != 7) {
			event_code = push_decode(*push_switches);
			status = 1;
		}
		update_sw_reg(s);	// スイッチレジスタ更新
		break;
	case 1:
		if (*push_switches == 7) status = 2;
		break;
	case 2:
		PUSH_EVENT = event_code;
		status = 0;
		break;
	default:
		status = 0;
		break;
	}
}
