1. 程式人生 > >Rocket之新增指令

Rocket之新增指令

目錄

準備工作

  • 指令空間(找到一個未使用的)。opcode = 10101011
  • 指令功能。同或操作

Rocket Chip 原始碼

新增指令

  • 位置:rocket-chip/src/main/scala/rocket/Instructions.scala
object Instructions {
	...
	def QIHAO              = BitPat("b0000000??????????000?????1010111")
	...
}

譯碼訊號

  • 位置:rocket-chip/src/main/scala/rocket/IDecode.scala
class IDecode() {
	table {
	    ...
	    QIHAO->       List(Y,N,N,N,N,N,Y,Y,A2_RS2, A1_RS1, IMM_X, DW_XPR,FN_SOR,   	N,M_X,        MT_X,
N,N,N,N,N,N,Y,CSR.N,N,N,N,N), ... } }

ALU

  • 位置:rocket-chip/src/main/scala/rocket/ALU.scala
object ALU {
	...
	def FN_SOR  = UInt(8)
	...
}

class ALU {
	...
    val out = Mux(io.fn === FN_SOR, ~(io.in1 ^ io.in2), Mux(io.fn === FN_ADD || io.fn === FN_SUB, io.adder_out, shift_logic))
	..
. }

工具鏈

opcodes

  • 位置:rocket-chip/riscv-tools/riscv-opcodes/opcodes
qihao   rd rs1 rs2 31..25=0  14..12=0 6..2=0x15 1..0=3
  • 在opcodes下執行make install

新增指令

  • 位置:riscv-tools/riscv-gnu-toolchain/riscv-binutils-gdb/include/opcode/riscv-opc.h
#define MATCH_QIHAO 0x57
#define MASK_QIHAO  0xfe00707f
DECLARE_INSN(qihao, MATCH_QIHAO, MASK_QIHAO)

#define DECLARE_INSN(INSN_NAME, INSN_MATCH, INSN_MASK) \
static inline bool is_ ## INSN_NAME ## _insn (long insn) \
{ \
  return (insn & INSN_MASK) == INSN_MATCH; \
}

  • 位置:riscv-tools/riscv-gnu-toolchain/riscv-binutils-gdb/opcodes/riscv-opc.c
const struct riscv_opcode riscv_opcodes[] = {
/* name,      isa,   operands, match, mask, match_func, pinfo.  */
{"qihao",     "I",   "d,s,t",  MATCH_QIHAO, MASK_QIHAO, match_opcode, 0 },
}

spike支援

  • 位置:rocket-chip/riscv-tools/riscv-isa-sim/riscv/insns/qihao.h
WRITE_RD(sext_xlen(~(RS1 ^ RS2)));
  • 位置:rocket-chip/riscv-tools/riscv-isa-sim/riscv/riscv.mk.in
riscv_insn_list = \
	qihao \

編譯

cd rocket-chip/riscv-tools/
./build.sh

測試

C原始碼

#include<stdio.h>

int main() {
    int res = 0;
    __asm__ __volatile__("li a2, 0x00ff");
    __asm__ __volatile__("li a3, 0x0f0f");
    __asm__ __volatile__("qihao %0, a2, a3":"=r"(res)); // 0xfffff00f
    printf("%x\n", res);
    return 0;
}

spike 測試結果

在這裡插入圖片描述

RocketChip測試結果

在這裡插入圖片描述