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

/**************************************************
 *						Public
 **************************************************/

// 複数メモリのどのメモリか(0 < global_current_memory < MEMS_COUNT)
unsigned int global_current_memory = 0;

/**************************************************
 *						Private
 **************************************************/

// 複数メモリの変数
static char memory[MEMS_COUNT][MEM_SIZE];

static struct InstRec inst_memory[MEMS_COUNT][MEM_SIZE];


/**************************************************
 *						Impl
 **************************************************/

void memory_init() {
	int i, j;
	for (i = 0; i < MEMS_COUNT; i++)
	for (j = 0; j < MEM_SIZE; j++) {
		memory[i][j] = 0;
	}
}

void inst_memory_store(unsigned int mem_addr, struct InstRec inst_rec){
	inst_memory[global_current_memory][mem_addr] = inst_rec;
}
struct InstRec inst_memory_load(unsigned int mem_addr){
	return inst_memory[global_current_memory][mem_addr];
}

char memory_store(unsigned int mem_addr, enum Register reg) {
	if (!(mem_addr < MEM_SIZE)) panic();
	memory[global_current_memory][mem_addr] = global_registers[reg];
	return memory[global_current_memory][mem_addr];
}

char memory_load(unsigned int mem_addr, enum Register reg) {
	if (!(mem_addr < MEM_SIZE)) panic();
	global_registers[reg] = memory[global_current_memory][mem_addr];
	return global_registers[reg];
}

