RISC-V: R-type 位元运算指令

今天一样是简单的 AND、OR、XOR指令实作,
多出来的时间就来还摸索期留下来的债,
让整个专案变得更完整和容易维护,
这次补上 JUMP/BRANCH Exception 和 LOAD/STORE Exception。

R-type

指令格式如下:

|31     25|24   20|19   15|14    12|11   7|6       0|
+---------------------------------------------------+
|  func7  |  rs2  |  rs1  | funct3 |  rd  | opcode  |
+---------------------------------------------------+

AND

rd = rs1 & rs2

|31         25|24   20|19   15|14    12|11   7|6       0|
+-------------------------------------------------------+
|   0000000   |  rs2  |  rs1  |  111   |  rd  | 0110011 |
+-------------------------------------------------------+

OR

rd = rs1 | rs2

|31         25|24   20|19   15|14    12|11   7|6       0|
+-------------------------------------------------------+
|   0000000   |  rs2  |  rs1  |  110   |  rd  | 0110011 |
+-------------------------------------------------------+

XOR

rd = rs1 ^ rs2

|31         25|24   20|19   15|14    12|11   7|6       0|
+-------------------------------------------------------+
|   0000000   |  rs2  |  rs1  |  100   |  rd  | 0110011 |
+-------------------------------------------------------+

实际程序

github 页面 Tag: ITDay23

JUMP/BRANCH Exception 的条件会取决於是否支援 Compress Extension,
详细内容可以看前面的文章
这次先在 Executor 做了一个简单的判断和处理,
未来实做 CSR 再改掉。

LOAD/STORE Exception 的条件会取决於存取的大小和 EEI 的设计,
详细内容在前面的文章有提到,
因为目前没有实做其他装置与判断机制,
这边先当作 address misalignment 处理。

//executor.cpp
...
#define INSTRUCTION_ALIGNMENT 4
#define LOAD_STORE_ALIGNMENT_W 4
#define LOAD_STORE_ALIGNMENT_HW 4

bool isAlignment(uint32_t address, unsigned int baseNumber)
{
	if(0 == address % baseNumber) {
		return true;
	} else {
		return false;
	}
}
...
void EXECUTOR::LH_E()
{
	auto rs1 = instruction_decoder->get_rs1();
	auto rd = instruction_decoder->get_rd();
	auto addr = register_file->get_value_integer(rs1) + (uint32_t) instruction_decoder->get_imm(31, 20);

	if(!isAlignment(addr, LOAD_STORE_ALIGNMENT_HW)) {
		cpu->raise_exception(CPU_INTERFACE::LOAD_ADDRESS_MISALIGNED_EXCEPTION_CAUSE);
	}
...
void EXECUTOR::JALR_E()
{
	auto offset = instruction_decoder->get_imm(31, 20);
	auto rs1 = instruction_decoder->get_rs1();
	auto rd = instruction_decoder->get_rd();
	auto addr = (register_file->get_value_integer(rs1) + offset) & ~0x1;

	if(!isAlignment(addr, INSTRUCTION_ALIGNMENT)) {
		cpu->raise_exception(CPU_INTERFACE::INSTRUCTION_ADDRESS_MISALIGNED_EXCEPTION_CAUSE);
	}
...

指令部分相对单纯,
这次用 AND 当范例,实作如下:

//executor.cpp
...
		case INSTRUCTION_DECODER_INTERFACE::AND_FN3:
			AND_E();
			//do not check FN7 for readibility, refactor in future
			break;
...
void EXECUTOR::AND_E()
{
	auto rd = instruction_decoder->get_rd();
	auto rs1 = instruction_decoder->get_rs1();
	auto rs2 = instruction_decoder->get_rs2();

	auto value = register_file->get_value_integer(rs1) & register_file->get_value_integer(rs2);
	register_file->set_value_integer(rd, value);
}
//instructionDecodetInterface.h
...
		AND_FN3 = 0b111,
...
		AND_FN7 = 0b0000000,
...

<<:  [Day 22] - React 环境建置

>>:  硬体的讯号怎麽丢给软件?

Day1对於学习Java的看法&安装程序

刚读大一的时候,最让我感到头痛的就是程序设计课了!因为我一直以来都不怎麽喜欢电脑相关的东西,更别说是...

Day 27 | SQLite资料库(二)

资料库结构与设计 //建立资料表 CREATE TABLE myTable() 表格名称後的括号内,...

Day23,替你的Gitlab pipeline 添加点搞事

正文 在前面介绍gitlab-ci的pipeline中我仅仅只用到了build stage作为con...

[Day 1] - 前言

大家好,我是转职未满一年的工程师,听到友人推荐这项IT界的盛事,今年慕名前来参加IT邦自我挑战连续发...

蓝牙威胁(Bluetooth Threats)

1.Bluesnarfing Bluesnarfing使攻击者能够利用较旧的(大约在2003年)设备...