⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 processor_ctrl_instruction.cpp

📁 浙江大学的悟空嵌入式系统模拟器
💻 CPP
字号:
/* -*- C++ -*- */

/**
*  Copyright (c) 2005 Zhejiang University, P.R.China
*
*  This program is free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  This program is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with this program; if not, write to the Free Software
*  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

///==================================================
/**
* @file        Processor_Ctrl_Instruction.cpp
* @brief      
* @author      Chenfeng Zhou <ini_autumn@163.com> 
*
* Created    : <2005-03-08 20:05:36 by Cheney Chow>
* Last update: <2005-03-08 20:32:49 by Cheney Chow>
*
* $Id: Processor_Ctrl_Instruction.cpp,v 1.1 2005/06/16 06:01:50 qilj Exp $
*/
///==================================================

#include "Instr_Flag.h"
#include "Instr_Declare.h"
#include "Parser.h"
#include "Register.h"
#include "Types.h"
#include "Reg_Utils.h"
#include "Exception.h"
#include "Utils/Debug.h"

namespace PPC
{

	PPC_INSTRUCTION_IMPL(op_mcrxr)
	{
		PPC_ERROR("mcrxr not implemented !\n");
	}

	PPC_INSTRUCTION_IMPL(op_mfcr)
	{
		
		PPC_s32 D, A, B;
		X_Form_Parser::parse_x(instr, D, A, B);

		PPC_ASSERT((A == 0) && (B == 0));

		PPC_TRACE_2("mfcr", D, "");
		REG(D).convert_from_int(REG_TO_INT(REG(CR)));
	}

	PPC_INSTRUCTION_IMPL(op_mfmsr)
	{
		PPC_TRACE("mfmsr\n");
		if (REG_BIT_IS_SET(MSR, Reg_Flag::MSR_PR)) {
			// Machine is in user mode, while we wanna execute privilaged instrucion;
			Exception_Mgr::instance()->handle(Exception::EXC_PROGRAM,
				Exception::EXC_PROGRAM_PRIV);
			return;
		}

		PPC_s32 D, A, B;
		X_Form_Parser::parse_x(instr, D, A, B);
		PPC_ASSERT((A == 0) && (B == 0));

		PPC_TRACE_2("---mfmsr", D, "");
		REG(D).convert_from_int(REG_TO_INT(REG(MSR)));
	}


	PPC_INSTRUCTION_IMPL(op_mftb)
	{
		PPC_TRACE("mftb\n");
		PPC_s32 D, spr1, spr2;
		X_Form_Parser::parse_x(instr, D, spr1, spr2);

		if (spr2 == 8) {
			switch(spr1) {
			case 12:
				REG(D).convert_from_int(REG_TO_INT(REG(TBL)));
				return;
			case 13:
				REG(D).convert_from_int(REG_TO_INT(REG(TBU)));
				return;
			default:
				PPC_ERROR("mftb spr1 error!\n");
				PPC_ASSERT(0);
			}
		}
		else {
			PPC_ERROR("mftb spr2 error!\n");
			PPC_ASSERT(0);
		}
	}

	PPC_INSTRUCTION_IMPL(op_mtcrf)
	{
		PPC_TRACE("mtcrf\n");
		PPC_s32 S;
		PPC_u32 crm;
		X_Form_Parser::parse_xfx(instr, S, crm);

		PPC_u32 mask = ((crm & 0x80)? 0xf0000000 : 0)
			| ((crm & 0x40)? 0x0f000000 : 0)
			| ((crm & 0x20)? 0x00f00000 : 0)
			| ((crm & 0x10)? 0x000f0000 : 0)
			| ((crm & 0x08)? 0x0000f000 : 0)
			| ((crm & 0x04)? 0x00000f00 : 0)
			| ((crm & 0x02)? 0x000000f0 : 0)
			| ((crm & 0x01)? 0x0000000f : 0);

		REG(CR).convert_from_int((REG_TO_INT(REG(S)) & mask) 
			| (REG_TO_INT(REG(CR)) & ~mask));
	}

	PPC_INSTRUCTION_IMPL(op_mtmsr)
	{
		PPC_TRACE("mtmsr\n");
		if (REG_BIT_IS_SET(MSR, Reg_Flag::MSR_PR)) {
			Exception_Mgr::instance()->handle(Exception::EXC_PROGRAM,
				Exception::EXC_PROGRAM_PRIV);
			return;
		}

		PPC_s32 S, A, B;

		X_Form_Parser::parse_x(instr, S, A, B);
		PPC_ASSERT( (A == 0) && (B == 0));

		REG_SET_MSR(REG_TO_INT(REG(S)));
	}


	PPC_INSTRUCTION_IMPL(op_mfspr)
	{
		PPC_s32 D, spr1, spr2;
		X_Form_Parser::parse_xo(instr, D, spr1, spr2);

		PPC_u32 spr_n = (spr2 << 5) | spr1;
		PPC_TRACE_2("mfspr", D, (PPC_u32*)spr_n);

		// For user mode;
		// UISA
		if (spr2 == 0 && (spr1 == 1 || spr1 == 8 || spr1 == 9)) {
			REG(D).convert_from_int(REG_TO_INT(SPR_REG(spr_n)));
			return;
		}

		// For Supervisor mode;
		// OEA;
		if (REG_BIT_IS_SET(MSR, Reg_Flag::MSR_PR)) {
			// Machine is in user mode, while we wanna execute privilaged instrucion;
			Exception_Mgr::instance()->handle(Exception::EXC_PROGRAM,
				Exception::EXC_PROGRAM_PRIV);
			return;
		}
		
		REG(D).convert_from_int(REG_TO_INT(SPR_REG(spr_n)));
	}

	PPC_INSTRUCTION_IMPL(op_mtspr)
	{
		PPC_s32 S, spr1, spr2;
		X_Form_Parser::parse_x(instr, S, spr1, spr2);

		PPC_u32 spr_n = (spr2 << 5) | spr1;
		PPC_TRACE_2("mtspr", S, (PPC_u32*)spr_n);

		// For user mode;
		// UISA
		if (spr2 == 0 && (spr1 == 1 || spr1 == 8 || spr1 == 9)) {
			SPR_REG(spr_n).convert_from_int(REG_TO_INT(REG(S)));
			return;
		}

		// For Supervisor mode;
		// OEA;
		if (REG_BIT_IS_SET(MSR, Reg_Flag::MSR_PR)) {
			// Machine is in user mode, while we wanna execute privilaged instrucion;
			Exception_Mgr::instance()->handle(Exception::EXC_PROGRAM,
				Exception::EXC_PROGRAM_PRIV);
			return;
		}

		SPR_REG(spr_n).convert_from_int(REG_TO_INT(REG(S)));
	}

}// namespace PPC

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -