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

📄 misc_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        Misc_Instruction.cpp
 * @brief      
 * @author      Chenfeng Zhou <ini_autumn@163.com> 
 *
 * Created    : <2005-02-12 11:09:48 by Cheney Chow>
 * Last update: <2005-03-08 19:59:45 by Cheney Chow>
 *
 * $Id: Misc_Instruction.cpp,v 1.1 2005/06/16 06:01:47 qilj Exp $
 */
///==================================================

#include "Instruction.h"
#include "Instr_Declare.h"
#include "Utils/Debug.h"
#include "Exception.h"
#include "Register.h"
#include "Reg_Utils.h"
#include "Parser.h"
#include "MMU.h"
#include "Instr_Flag.h"
#include "Reg_Flag.h"

namespace PPC
{

	// ".long 0" => 0x00000000
	//! Data define; Space align; We ignore
	PPC_INSTRUCTION_IMPL(op_align)
	{
		PPC_TRACE("align\n");
	}

    PPC_INSTRUCTION_IMPL(op_invalid)
    {
        PPC_ERROR(("Invalid instruction!\n"));
        PPC_ASSERT(0);
    }

	//! System linkage instructions
	PPC_INSTRUCTION_IMPL(op_rfi)
	{
		PPC_TRACE("rfi\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);
			PPC_TRACE("---rfi no effect(in user mode now!)\n");
			return;
		}
#ifdef PPC7XX
		REG_SET_MSR(REG_TO_INT(REG(SRR1)) & Reg_Flag::MSR_RFI_SAVE_MASK);
#else
		REG_SET_MSR(REG_TO_INT(REG(SRR1)));
#endif

		// Round to next instruction; enable exception pending
		GET_CPU().set_npc(REG_TO_INT(REG(SRR0)) & 0xfffffffc);
	}

	PPC_INSTRUCTION_IMPL(op_sc)
	{
		PPC_TRACE("sc\n");
		Exception_Mgr::instance()->handle(Exception::EXC_SC);
	}

	//! Trap instruction;
	PPC_INSTRUCTION_IMPL(op_tw)
	{
		PPC_TRACE("tw\n");
		PPC_s32 TO, A, B;
		X_Form_Parser::parse_x(instr, TO, A, B);

		PPC_u32 a = REG_TO_INT(REG(A));
		PPC_u32 b = REG_TO_INT(REG(B));

		if (((TO & 16) && ((PPC_s32)a < (PPC_s32)b))
            || ((TO & 8) && ((PPC_s32)a > (PPC_s32)b))
            || ((TO & 4) && (a == b))
            || ((TO & 2) && (a < b))
            || ((TO & 1) && (a > b)))
            Exception_Mgr::instance()->handle(Exception::EXC_PROGRAM,
                                              Exception::EXC_PROGRAM_TRAP);
	}

	PPC_INSTRUCTION_IMPL(op_twi)
	{
		PPC_TRACE("twi\n");
		PPC_s32 TO, A;
		PPC_u32 imm;

		D_Form_Parser::parse_simm (instr, TO, A, imm);

		PPC_u32 a = REG_TO_INT(REG(A));

		if (((TO & 16) && ((PPC_s32)a < (PPC_s32)imm))
			|| ((TO & 8) && ((PPC_s32)a > (PPC_s32)imm))
			|| ((TO & 4) && (a == imm))
			|| ((TO & 2) && (a < imm))
			|| ((TO & 1) && (a > imm)))
			Exception_Mgr::instance()->handle(Exception::EXC_PROGRAM,
                                              Exception::EXC_PROGRAM_TRAP);

	}

	//! External Control Instructions
	PPC_INSTRUCTION_IMPL(op_eciwx)
	{
		PPC_ERROR(("eciwx not implemented!\n"));
	}

	PPC_INSTRUCTION_IMPL(op_ecowx)
	{
		PPC_ERROR(("ecowx not implemented!\n"));
	}


	//! Data cache block store;
	PPC_INSTRUCTION_IMPL(op_dcbst)
	{
		PPC_ERROR(("dcbst not implemented!\n"));
	}

	PPC_INSTRUCTION_IMPL(op_dcbf)
	{
		PPC_ERROR(("dcbf not implemented!\n"));
	}

	PPC_INSTRUCTION_IMPL(op_dcbt)
	{
		PPC_ERROR(("dcbt not implemented!\n"));
	}

	PPC_INSTRUCTION_IMPL(op_dcbtst)
	{
		PPC_ERROR(("dcbtst not implemented!\n"));
	}

	PPC_INSTRUCTION_IMPL(op_dcbi)
	{
		PPC_ERROR(("dcbi not implemented!\n"));
	}

	PPC_INSTRUCTION_IMPL(op_dcbz)
	{
		PPC_ERROR(("dcbz not implemented!\n"));
	}

	PPC_INSTRUCTION_IMPL(op_tlbie)
	{
		PPC_ERROR(("tlbie not implemented!\n"));
	}

	PPC_INSTRUCTION_IMPL(op_tlbia)
	{
		PPC_ERROR(("tlbie not implemented!\n"));
	}

	PPC_INSTRUCTION_IMPL(op_tlbsx)
	{
		PPC_TRACE("tlbsx\n");
		//printf("tlbsx\n");
		PPC_s32 RT, RA, RB;
		X_Form_Parser::parse_x(instr, RT, RA, RB);

		PPC_u32 base_addr;
		if( RA == 0)
			base_addr = 0;
		else
			base_addr = REG_TO_INT(REG(RA));

		PPC_u32 ea;
		ea = base_addr + REG_TO_INT(REG(RB));


		if(instr & Op_Flag::RC)
		{
			REG_CLEAR_BIT(CR, Reg_Flag::CR0_LT);
			REG_CLEAR_BIT(CR, Reg_Flag::CR0_GT);
			if( REG_BIT_IS_SET(XER, Reg_Flag::XER_SO))
				REG_SET_BIT(CR, Reg_Flag::CR0_SO);
			else
				REG_CLEAR_BIT(CR, Reg_Flag::CR0_SO);
		}

		PPC_u32 as = REG_BIT_IS_SET(MMUCR, Reg_Flag::MMUCR_STS) ? 1 : 0;
		PPC_u32 pid = REG_TO_INT(REG(PID)) & 0x000000ff;
		size_t index;
		if( ((MMU &)GET_MMU()).get_utlb().search(as, ea, pid,  &index))
		{
			REG(RT).convert_from_int(index);
			if(instr & Op_Flag::RC)
			{
				REG_SET_BIT(CR, Reg_Flag::CR0_EQ);
			}
		}
		else
		{
			if(instr & Op_Flag::RC)
			{
				REG_CLEAR_BIT(CR, Reg_Flag::CR0_EQ);
			}
		}

	}

	PPC_INSTRUCTION_IMPL(op_tlbre)
	{
		PPC_TRACE("tlbre\n");
		//printf("tlbre\n");
		PPC_s32 RT, RA, WS;
		X_Form_Parser::parse_x(instr, RT, RA, WS);

		PPC_u32 index = REG_TO_INT(REG(RA)) & 0x0000003f;
		TLB::Entry & tlbentry = ((MMU &)GET_MMU()).get_utlb().get_entry(index);

		switch( WS )
		{
		case 0:			
			if( REG_BIT_IS_SET(CCR0, Reg_Flag::CCR0_CRPE) )
				REG(RT).convert_from_int(tlbentry.ws0);
			else
				REG(RT).convert_from_int(tlbentry.ws0 & 0xfffffff0);
			REG(MMUCR).convert_from_int( REG_TO_INT(REG(MMUCR)) | (tlbentry.ws0_TID & 0x000000ff));
			break;
		case 1:
			if( REG_BIT_IS_SET(CCR0, Reg_Flag::CCR0_CRPE))
				REG(RT).convert_from_int(tlbentry.ws1);
			else
				REG(RT).convert_from_int(tlbentry.ws1 & 0xfffffc0f);
			break;
		case 2:
			if( REG_BIT_IS_SET(CCR0, Reg_Flag::CCR0_CRPE))
				REG(RT).convert_from_int(tlbentry.ws2);
			else
				REG(RT).convert_from_int(tlbentry.ws2 & 0x0000ffbf);
			break;			
		default:
			assert(0);
		}
		
	}

	PPC_INSTRUCTION_IMPL(op_tlbwe)
	{
		PPC_TRACE("tlbwe\n");
		//printf("tlbwe-----\n");

		PPC_s32 RS, RA, WS;
		X_Form_Parser::parse_x(instr, RS, RA, WS);

		PPC_u32 index = REG_TO_INT(REG(RA)) & 0x0000003f;
		TLB::Entry & tlbentry = ((MMU &)GET_MMU()).get_utlb().get_entry(index);

		//printf("old tlb#%x: %x %x %x %x\n", index, tlbentry.ws0, tlbentry.ws0_TID, tlbentry.ws1,tlbentry.ws2);

		switch( WS )
		{
		case 0:
			tlbentry.ws0 = (REG_TO_INT(REG(RS)) & 0xfffffff0) | (tlbentry.ws0 & 0x0000000f);
			tlbentry.ws0_TID = REG_TO_INT(REG(MMUCR)) & 0x000000ff;
			break;
		case 1:
			tlbentry.ws1 = (REG_TO_INT(REG(RS)) & 0xfffffc0f) | (tlbentry.ws1 & 0x000003f0);
			break;
		case 2:
			tlbentry.ws2 = (REG_TO_INT(REG(RS)) & 0x0000ffbf) | (tlbentry.ws2 & 0xffff004f);
			break;
		default:
			assert(0);
			}
		//printf("tlb#%x: %x %x %x %x\n", index, tlbentry.ws0, tlbentry.ws0_TID, tlbentry.ws1,tlbentry.ws2);
		//printf("----------\n");
	}

	PPC_INSTRUCTION_IMPL(op_tlbsync)
	{
		PPC_ERROR(("tlbsync not implemented!\n"));
	}

	PPC_INSTRUCTION_IMPL(op_icbi)
	{
		PPC_ERROR(("icbi not implemented!\n"));
	}

} // namespace PPC

⌨️ 快捷键说明

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