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

📄 integer_instruction.cpp

📁 浙江大学的悟空嵌入式系统模拟器
💻 CPP
字号:
/**
 *  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/PPC/Integer_Instruction.cpp
 *
 *  \brief   Integer operations
 *
 *  \author  Chenfeng Zhou <ini_autumn@163.com>
 *
 *  Created    : <2005-02-10 00:24:12 by Cheney Chow>
 *  Last update: <2005-03-01 22:22:07 by Cheney Chow>
 *
 *  $Id: Integer_Instruction.cpp,v 1.1 2005/06/16 06:01:46 qilj Exp $
 */
//=============================================================================

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

namespace PPC {

    PPC_INSTRUCTION_IMPL(op_addx)
    {
        PPC_s32 D, A, B;
        X_Form_Parser::parse_xo (instr, D, A, B);

		REG_ADD (REG(D), REG(A), REG(B));

        if (instr & Op_Flag::RC) {
            REG_UPDATE_CR0 (REG(D));
        }
    }

    PPC_INSTRUCTION_IMPL(op_addcx)
    {
        PPC_s32 D, A, B;
        X_Form_Parser::parse_xo (instr, D, A, B);

        REG_ADD (REG(D), REG(A), REG(B));

        if (REG(D) < REG(A))
            REG_SET_BIT (XER, Reg_Flag::XER_CA);
        else
            REG_CLEAR_BIT (XER, Reg_Flag::XER_CA);
        
        if (instr & Op_Flag::RC)
            REG_UPDATE_CR0 (REG (D));
    }

    PPC_INSTRUCTION_IMPL(op_addex)
    {
        PPC_s32 D, A, B;
        X_Form_Parser::parse_xo (instr, D, A, B);

        PPC_Register_Int a = REG_TO_INT (REG (A));
        PPC_Register_Int b = REG_TO_INT (REG (B));
        PPC_Register_Int ca = (REG_BIT_IS_SET (XER, Reg_Flag::XER_CA)? 1 : 0);

        REG (D).convert_from_int (a + b + ca);

        if (JUDGE_CARRY_3 (a, b, ca))
            REG_SET_BIT (XER, Reg_Flag::XER_CA);
        else
            REG_CLEAR_BIT (XER, Reg_Flag::XER_CA);

        if (instr & Op_Flag::RC)
            REG_UPDATE_CR0 (REG (D));
    }

    PPC_INSTRUCTION_IMPL(op_addi)
    {
        PPC_s32 D, A;
        PPC_u32 imm;
        D_Form_Parser::parse_simm (instr, D, A, imm);

        REG (D).convert_from_int ( (A? REG (A).convert_to_int() : 0) + imm);
    }

    PPC_INSTRUCTION_IMPL(op_addic)
    {
        PPC_s32 D, A;
        PPC_u32 imm;
        D_Form_Parser::parse_simm (instr, D, A, imm);

        REG_ADD (REG (D), REG (A), imm);

        if (REG (D) < REG (A))
            REG_SET_BIT (XER, Reg_Flag::XER_CA);
        else
            REG_CLEAR_BIT (XER, Reg_Flag::XER_CA);
    }

    PPC_INSTRUCTION_IMPL(op_addic_)
    {
        PPC_s32 D, A;
        PPC_u32 imm;
        D_Form_Parser::parse_simm (instr, D, A, imm);

        REG_ADD (REG (D), REG (A), imm);

        if (REG (D) < REG (A))
            REG_SET_BIT (XER, Reg_Flag::XER_CA);
        else
            REG_CLEAR_BIT (XER, Reg_Flag::XER_CA);

        REG_UPDATE_CR0 (REG (D));
    }

    PPC_INSTRUCTION_IMPL(op_addis)
    {
        PPC_s32 D, A;
        PPC_u32 imm;
        D_Form_Parser::parse_shift16 (instr, D, A, imm);

        REG (D).convert_from_int ( (A? REG (A).convert_to_int() : 0) + imm);
    }

    PPC_INSTRUCTION_IMPL(op_addmex)
    {
        PPC_s32 D, A, B;
        X_Form_Parser::parse_xo (instr, D, A, B);

        PPC_ASSERT (B == 0);

        PPC_Register_Int ca = (REG_BIT_IS_SET (XER, Reg_Flag::XER_CA)? 1 : 0);

        REG (D).convert_from_int (REG_TO_INT (REG(A)) + ca + 0xffffffff);

        if (REG_TO_INT (REG (A)) || ca)
            REG_SET_BIT (XER, Reg_Flag::XER_CA);
        else
            REG_CLEAR_BIT (XER, Reg_Flag::XER_CA);

        if (instr & Op_Flag::RC)
            REG_UPDATE_CR0 (REG (D));
    }

    PPC_INSTRUCTION_IMPL(op_addzex)
    {
        PPC_s32 D, A, B;
        X_Form_Parser::parse_xo (instr, D, A, B);

        PPC_ASSERT (B == 0);

        PPC_Register_Int ca = (REG_BIT_IS_SET (XER, Reg_Flag::XER_CA)? 1 : 0);

        REG_ADD (REG (D), REG (A), ca);
        
		if ( (REG_TO_INT(REG(A)) == 0xffffffff) && ca )
			REG_SET_BIT (XER, Reg_Flag::XER_CA);
		else
			REG_CLEAR_BIT (XER, Reg_Flag::XER_CA);

		if (instr & Op_Flag::RC)
			REG_UPDATE_CR0 (REG (D));
    }
    
	PPC_INSTRUCTION_IMPL(op_divwx)
	{
		PPC_s32 D, A, B;
		X_Form_Parser::parse_xo (instr, D, A, B);

		if (REG_TO_INT(REG(B)) == 0)
			PPC_ERROR(("Divided by zero!\n"));

		REG_SDIV (REG(D), REG(A), REG(B));

		if (instr & Op_Flag::RC)
			REG_UPDATE_CR0 (REG (D));
	}

	PPC_INSTRUCTION_IMPL(op_divwux)
	{
		PPC_s32 D, A, B;
		X_Form_Parser::parse_xo (instr, D, A, B);

		if (REG_TO_INT(REG(B)) == 0)
			PPC_ERROR(("Divided by zero!\n"));

		REG_UDIV (REG(D), REG(A), REG(B));

		if (instr & Op_Flag::RC)
			REG_UPDATE_CR0 (REG (D));
	}

	PPC_INSTRUCTION_IMPL(op_mulhwx)
	{
		PPC_s32 D, A, B;
		X_Form_Parser::parse_xo (instr, D, A, B);

        PPC_s64 res = ((PPC_s32)REG_TO_INT (REG (A)))
            * ((PPC_s32)REG_TO_INT (REG (B)));
        
        REG (D).convert_from_int ((PPC_Register_Int)(((PPC_u64)res) >> 32));

        if (instr & Op_Flag::RC)
            REG_UPDATE_CR0 (REG (D));
	}

    PPC_INSTRUCTION_IMPL(op_mulhwux)
    {
        PPC_s32 D, A, B;
		X_Form_Parser::parse_xo (instr, D, A, B);

        PPC_u64 res = ((PPC_u64) REG_TO_INT (REG (A)))
            * ((PPC_u64)REG_TO_INT (REG (B)));

        REG (D).convert_from_int ((PPC_Register_Int)(res >> 32));

        if (instr & Op_Flag::RC)
            REG_UPDATE_CR0 (REG (D));
    }

    PPC_INSTRUCTION_IMPL(op_mulli)
    {
        PPC_s32 D, A;
        PPC_u32 imm;

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

        // ? make imm signed, different from pearpc
        REG (D).convert_from_int (REG_TO_INT (REG (A)) * (PPC_s32)imm);
    }

    PPC_INSTRUCTION_IMPL(op_mullwx)
    {
        PPC_s32 D, A, B;
		X_Form_Parser::parse_xo (instr, D, A, B);

        REG_MUL (REG (D), REG (A), REG (B));

        if (instr & Op_Flag::RC)
            REG_UPDATE_CR0 (REG (D));

        if (instr & Op_Flag::OE)
            PPC_ERROR (("OE not implemented!\n"));
    }

    PPC_INSTRUCTION_IMPL(op_negx)
    {
        PPC_s32 D, A, B;
		X_Form_Parser::parse_xo (instr, D, A, B);

        PPC_ASSERT (B == 0);

		//PPC_u32 tmp = -REG_TO_INT (REG (A));
		//? I think this is equal to the above exp
		PPC_u32 tmp = ~REG_TO_INT (REG (A)) + 1;
        REG (D).convert_from_int (tmp);

        if (instr & Op_Flag::RC)
            REG_UPDATE_CR0 (REG (D));
    }

    PPC_INSTRUCTION_IMPL(op_subfx)
    {
        PPC_s32 D, A, B;
		X_Form_Parser::parse_xo (instr, D, A, B);

        REG (D).convert_from_int (~REG_TO_INT (REG (A)) +
                                  REG_TO_INT (REG (B)) + 1);

        if (instr & Op_Flag::RC)
            REG_UPDATE_CR0 (REG (D));
    }

    PPC_INSTRUCTION_IMPL(op_subfcx)
    {
        PPC_s32 D, A, B;
		X_Form_Parser::parse_xo (instr, D, A, B);

        PPC_u32 a = REG_TO_INT (REG (A));
        PPC_u32 b = REG_TO_INT (REG (B));
        
        REG (D).convert_from_int (~a + b + 1);

        if (JUDGE_CARRY_3 (~a, b, 1))
            REG_SET_BIT (XER, Reg_Flag::XER_CA);
        else
            REG_CLEAR_BIT (XER, Reg_Flag::XER_CA);

        if (instr & Op_Flag::RC)
            REG_UPDATE_CR0 (REG (D));
    }

	//? maybe the same as subficx in the manual?
    PPC_INSTRUCTION_IMPL(op_subfic)
    {
		PPC_s32 D, A;
		PPC_u32 imm;

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

		PPC_u32 a = REG_TO_INT (REG (A));

		REG (D).convert_from_int (~a + imm + 1);

		if (JUDGE_CARRY_3 (~a, imm, 1))
			REG_SET_BIT (XER, Reg_Flag::XER_CA);
		else
			REG_CLEAR_BIT (XER, Reg_Flag::XER_CA);
    }

    PPC_INSTRUCTION_IMPL(op_subfex)
    {
        PPC_s32 D, A, B;
		X_Form_Parser::parse_xo (instr, D, A, B);

        PPC_u32 a = REG_TO_INT (REG (A));
        PPC_u32 b = REG_TO_INT (REG (B));
        PPC_u32 ca = (REG_BIT_IS_SET (XER, Reg_Flag::XER_CA)? 1 : 0);

        REG (D).convert_from_int (~a + b + ca);

        if (JUDGE_CARRY_3 (~a, b, ca))
			REG_SET_BIT (XER, Reg_Flag::XER_CA);
		else
			REG_CLEAR_BIT (XER, Reg_Flag::XER_CA);

        if (instr & Op_Flag::RC)
            REG_UPDATE_CR0 (REG (D));
    }
    
    PPC_INSTRUCTION_IMPL(op_subfmex)
    {
        PPC_s32 D, A, B;
		X_Form_Parser::parse_xo (instr, D, A, B);

        PPC_ASSERT (B == 0);

        PPC_u32 a = REG_TO_INT (REG (A));
        PPC_u32 ca = (REG_BIT_IS_SET (XER, Reg_Flag::XER_CA)? 1 : 0);

        REG (D).convert_from_int (~a + ca + 0xffffffff);

        if ( (a != 0xffffffff) || ca )
            REG_SET_BIT (XER, Reg_Flag::XER_CA);
        else
            REG_CLEAR_BIT (XER, Reg_Flag::XER_CA);

        if (instr & Op_Flag::RC)
            REG_UPDATE_CR0 (REG (D));
    }

    PPC_INSTRUCTION_IMPL(op_subfzex)
    {
        PPC_s32 D, A, B;
		X_Form_Parser::parse_xo (instr, D, A, B);
        
        PPC_ASSERT (B == 0);

        PPC_u32 a = REG_TO_INT (REG (A));
        PPC_u32 ca = (REG_BIT_IS_SET (XER, Reg_Flag::XER_CA)? 1 : 0);

        REG (D).convert_from_int (~a + ca);

        if (!a && ca)
            REG_SET_BIT (XER, Reg_Flag::XER_CA);
        else
            REG_CLEAR_BIT (XER, Reg_Flag::XER_CA);

        if (instr & Op_Flag::RC)
            REG_UPDATE_CR0 (REG (D));
    }

	
    
} // namespace PPC

⌨️ 快捷键说明

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