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

📄 instruction_test.cpp

📁 浙江大学的悟空嵌入式系统模拟器
💻 CPP
字号:
#include "stdafx.h"
#include "Instruction_Test.h"

bool MU0_Load_Store::test_and_parse(instruction_unit binary)
{
	Binary_32Bit bits(binary);
	if (!(bits.test(15) == 0 && bits.test(14) == 0 && bits.test(13) == 0))
		return false;
	append_opcode(bits.convert_to_int(12, 15));
	append_operand(bits.convert_to_int(0, 11));
	return true;
}

bool MU0_Load_Store::execute(void)
{
	switch(opcodes_[0])
	{
	case BINARY4(0000):
		std::cout<<"LDA";
		std::cout<<" "<<operands_[0]<<";";
		break;
	case BINARY4(0001):
		std::cout<<"STO";
		std::cout<<" "<<operands_[0]<<";";
		break;
	}
	return true;
}

bool MU0_Add_Sub::test_and_parse(instruction_unit binary)
{
	Binary_32Bit bits(binary);
	if (!(bits.test(15) == 0 && bits.test(14) == 0 && bits.test(13) == 1))
		return false;
	append_opcode(bits.convert_to_int(12, 15));
	append_operand(bits.convert_to_int(0, 11));
	return true;
}

bool MU0_Add_Sub::execute(void)
{
	switch(opcodes_[0])
	{
	case BINARY4(0010):
		std::cout<<"ADD";
		std::cout<<" "<<operands_[0]<<";";
		break;
	case BINARY4(0011):
		std::cout<<"SUB";
		std::cout<<" "<<operands_[0]<<";";
		break;
	}
	return true;
}

void MU0_Instruction_Set::register_instructions()
{
	register_instruction(MU0_Add_Sub::instance());

	register_instruction(MU0_Load_Store::instance());
}

void Instruction_Test::setUp()
{
	instr_set_ = MU0_Instruction_Set::instance();
	instr_set_->register_instructions();
}

void Instruction_Test::tearDown()
{
	instr_set_ = MU0_Instruction_Set::instance();
	instr_set_->register_instructions();
}

void Instruction_Test::test_instruction()
{
	std::cout<<"instruction"<<std::endl;
	int a = BINARY4(0011);
	CPPUNIT_ASSERT_EQUAL(3, a);
	int b = BINARY8(1111, 0000);
	CPPUNIT_ASSERT_EQUAL(0xf0, b);
	int c = BINARY16(0001, 0010, 0011, 0100);
	CPPUNIT_ASSERT_EQUAL(0x1234, c);
	int d = BINARY32(1111, 1110, 1101, 1100, 1011, 1010, 1001, 1000);
	CPPUNIT_ASSERT(0xfedcba98 == d);

	u32 stream[] = {
		BINARY32(0000,0000,0000,0000, 0000,1111,1111,1111),
		BINARY32(0000,0000,0000,0000, 0010,1111,1111,1111),
		BINARY32(0000,0000,0000,0000, 0011,1111,1111,1111),
		BINARY32(0000,0000,0000,0000, 0001,1111,1111,1111)
	};

	for (u32 i = 0; i < sizeof(stream) / sizeof(stream[0]); ++i)
	{
		instr_set_->execute_instruction(stream[i]);
	}
	std::cout<<std::endl;
}

DEFINE_SINGLETON(MU0_Instruction_Set);

DEFINE_SINGLETON(MU0_Load_Store)

DEFINE_SINGLETON(MU0_Add_Sub)

⌨️ 快捷键说明

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