exec.c 3.1 KB
Newer Older
P
Parallels 已提交
1 2 3 4
#include "cpu/exec.h"
#include "all-instr.h"

static OpcodeEntry load_table [8] = {
Z
Zihao Yu 已提交
5
  EXW(lds, 1), EXW(lds, 2), EXW(lds, 4), EXW(ld, 8), EXW(ld, 1), EXW(ld, 2), EXW(ld, 4), EMPTY
P
Parallels 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
};

static make_EHelper(load) {
  decinfo.width = load_table[decinfo.isa.instr.funct3].width;
  idex(pc, &load_table[decinfo.isa.instr.funct3]);
}

static OpcodeEntry store_table [8] = {
  EXW(st, 1), EXW(st, 2), EXW(st, 4), EXW(st, 8), EMPTY, EMPTY, EMPTY, EMPTY
};

static make_EHelper(store) {
  decinfo.width = store_table[decinfo.isa.instr.funct3].width;
  idex(pc, &store_table[decinfo.isa.instr.funct3]);
}

static OpcodeEntry op_imm_table [8] = {
  EX(add), EX(sll), EX(slt), EX(sltu), EX(xor), EX(srl), EX(or), EX(and)
};

static make_EHelper(op_imm) {
  idex(pc, &op_imm_table[decinfo.isa.instr.funct3]);
}

30 31
static OpcodeEntry op_imm_table32 [8] = {
  EX(addw), EX(sllw), EMPTY, EMPTY, EMPTY, EX(srlw), EMPTY, EMPTY
P
Parallels 已提交
32 33
};

34 35
static make_EHelper(op_imm32) {
  idex(pc, &op_imm_table32[decinfo.isa.instr.funct3]);
P
Parallels 已提交
36 37 38 39 40 41 42 43 44 45 46
}

static OpcodeEntry op_table [8] = {
  EX(add), EX(sll), EX(slt), EX(sltu), EX(xor), EX(srl), EX(or), EX(and)
};

static OpcodeEntry op2_table [8] = {
  EX(sub), EMPTY, EMPTY, EMPTY, EMPTY, EX(sra), EMPTY, EMPTY
};

static OpcodeEntry muldiv_table [8] = {
Z
Zihao Yu 已提交
47
  EX(mul), EX(mulh), EX(mulhsu), EX(mulhu), EX(div), EX(divu), EX(rem), EX(remu)
P
Parallels 已提交
48 49 50 51 52 53 54 55 56 57 58
};

static make_EHelper(op) {
  switch (decinfo.isa.instr.funct7) {
    case 0:  idex(pc, &op_table[decinfo.isa.instr.funct3]); break;
    case 1:  idex(pc, &muldiv_table[decinfo.isa.instr.funct3]); break;
    case 32: idex(pc, &op2_table[decinfo.isa.instr.funct3]); break;
    default: assert(0);
  }
}

59
static OpcodeEntry op_table32 [8] = {
P
Parallels 已提交
60
  EX(addw), EX(sllw), EMPTY, EMPTY, EMPTY, EX(srlw), EMPTY, EMPTY
P
Parallels 已提交
61 62
};

63
static OpcodeEntry op2_table32 [8] = {
P
Parallels 已提交
64
  EX(subw), EMPTY, EMPTY, EMPTY, EMPTY, EX(sraw), EMPTY, EMPTY
P
Parallels 已提交
65 66
};

67
static OpcodeEntry muldiv_table32 [8] = {
P
Parallels 已提交
68 69 70
  EX(mulw), EMPTY, EMPTY, EMPTY, EX(divw), EX(divuw), EX(remw), EX(remuw)
};

71
static make_EHelper(op32) {
P
Parallels 已提交
72
  switch (decinfo.isa.instr.funct7) {
73 74 75
    case 0:  idex(pc, &op_table32[decinfo.isa.instr.funct3]); break;
    case 1:  idex(pc, &muldiv_table32[decinfo.isa.instr.funct3]); break;
    case 32: idex(pc, &op2_table32[decinfo.isa.instr.funct3]); break;
P
Parallels 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88
    default: printf("not implemented\n"); assert(0);
  }
}

static OpcodeEntry system_table [8] = {
  EX(priv), IDEX(csr, csrrw), IDEX(csr, csrrs), EMPTY, EMPTY, EMPTY, EMPTY, EMPTY
};

static make_EHelper(system) {
  idex(pc, &system_table[decinfo.isa.instr.funct3]);
}

static OpcodeEntry opcode_table [32] = {
89 90
  /* b00 */ IDEX(ld, load), EMPTY, EMPTY, EMPTY, IDEX(I, op_imm), IDEX(U, auipc), IDEX(I, op_imm32), EMPTY,
  /* b01 */ IDEX(st, store), EMPTY, EMPTY, EMPTY, IDEX(R, op), IDEX(U, lui), IDEX(R, op32), EMPTY,
P
Parallels 已提交
91 92 93 94 95 96 97 98 99
  /* b10 */ EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
  /* b11 */ IDEX(B, branch), IDEX(I, jalr), EX(nemu_trap), IDEX(J, jal), EX(system), EMPTY, EMPTY, EMPTY,
};

void isa_exec(vaddr_t *pc) {
  decinfo.isa.instr.val = instr_fetch(pc, 4);
  assert(decinfo.isa.instr.opcode1_0 == 0x3);
  idex(pc, &opcode_table[decinfo.isa.instr.opcode6_2]);
}