-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruncode.c
More file actions
150 lines (121 loc) · 5.01 KB
/
runcode.c
File metadata and controls
150 lines (121 loc) · 5.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include "emu.h"
#include <stdint.h>
inst_exe_t inst_exe_arr[INST_NUM];
enum inst_list_t inst_list;
enum inst_check_parts{
C_OP, // opcode only
C_F3, // opcode + F3
C_37, // opcode + F3 + F7
C_AL, // all 32 bits
C_SP // special judge
};
typedef struct{
uint32_t instruction : 8;
uint32_t check : 4;
uint32_t opcode : 7;
uint32_t func7 : 7;
uint32_t func3 : 3;
uint32_t bit31_0 : 31;
} inst_code_t;
inst_code_t inst_code[INST_NUM] = {
{i_lui, C_OP, .opcode = 0b0110111},
{i_auipc, C_OP, .opcode = 0b0010111},
{i_jal, C_OP, .opcode = 0b1101111}, // J type
{i_jalr, C_F3, .opcode = 0b1100111, .func3 = 0},
{i_beq, C_F3, .opcode = 0b1100011, .func3 = 0},
{i_bne, C_F3, .opcode = 0b1100011, .func3 = 1},
{i_blt, C_F3, .opcode = 0b1100011, .func3 = 4},
{i_bge, C_F3, .opcode = 0b1100011, .func3 = 5},
{i_bltu, C_F3, .opcode = 0b1100011, .func3 = 6},
{i_bgeu, C_F3, .opcode = 0b1100011, .func3 = 7},
{i_lb, C_F3, .opcode = 0b0000011, .func3 = 0},
{i_lh, C_F3, .opcode = 0b0000011, .func3 = 1},
{i_lw, C_F3, .opcode = 0b0000011, .func3 = 2},
{i_lbu, C_F3, .opcode = 0b0000011, .func3 = 4},
{i_lhu, C_F3, .opcode = 0b0000011, .func3 = 5},
{i_sb, C_F3, .opcode = 0b0100011, .func3 = 0},
{i_sh, C_F3, .opcode = 0b0100011, .func3 = 1},
{i_sw, C_F3, .opcode = 0b0100011, .func3 = 2},
{i_addi, C_F3, .opcode = 0b0010011, .func3 = 0},
{i_slti, C_F3, .opcode = 0b0010011, .func3 = 2},
{i_sltiu, C_F3, .opcode = 0b0010011, .func3 = 3},
{i_xori, C_F3, .opcode = 0b0010011, .func3 = 4},
{i_ori, C_F3, .opcode = 0b0010011, .func3 = 6},
{i_andi, C_F3, .opcode = 0b0010011, .func3 = 7},
{i_slli, C_37, .opcode = 0b0010011, .func3 = 1, .func7 = 0b0000000},
{i_srli, C_37, .opcode = 0b0010011, .func3 = 5, .func7 = 0b0000000},
{i_srai, C_37, .opcode = 0b0010011, .func3 = 5, .func7 = 0b0100000},
{i_add, C_37, .opcode = 0b0110011, .func3 = 0, .func7 = 0b0000000},
{i_sub, C_37, .opcode = 0b0110011, .func3 = 0, .func7 = 0b0100000},
{i_sll, C_37, .opcode = 0b0110011, .func3 = 1, .func7 = 0b0000000},
{i_slt, C_37, .opcode = 0b0110011, .func3 = 2, .func7 = 0b0000000},
{i_sltu, C_37, .opcode = 0b0110011, .func3 = 3, .func7 = 0b0000000},
{i_xor, C_37, .opcode = 0b0110011, .func3 = 4, .func7 = 0b0000000},
{i_srl, C_37, .opcode = 0b0110011, .func3 = 5, .func7 = 0b0000000},
{i_sra, C_37, .opcode = 0b0110011, .func3 = 5, .func7 = 0b0100000},
{i_or, C_37, .opcode = 0b0110011, .func3 = 6, .func7 = 0b0000000},
{i_and, C_37, .opcode = 0b0110011, .func3 = 7, .func7 = 0b0000000},
{i_fence, C_SP},
{i_fence_i, C_AL, .bit31_0 = 0x0000100f},
{i_ecall, C_AL, .bit31_0 = 0x00000073},
{i_ebreak, C_AL, .bit31_0 = 0x00100073},
{i_csrrw, C_F3, .opcode = 0b1110011, .func3 = 1},
{i_csrrs, C_F3, .opcode = 0b1110011, .func3 = 2},
{i_csrrc, C_F3, .opcode = 0b1110011, .func3 = 3},
{i_csrrwi, C_F3, .opcode = 0b1110011, .func3 = 5},
{i_csrrsi, C_F3, .opcode = 0b1110011, .func3 = 6},
{i_csrrci, C_F3, .opcode = 0b1110011, .func3 = 7}
};
int inst_decode(inst_t inst){
uint32_t cur_opcode = inst.I.opcode;
uint32_t cur_func3 = inst.I.func3;
uint32_t cur_func7 = inst.R.func7;
for(int i = 0; i < INST_NUM; i++){
bool eqop = inst_code[i].opcode == cur_opcode;
bool eqf3 = inst_code[i].func3 == cur_func3;
bool eqf7 = inst_code[i].func7 == cur_func7;
if((inst_code[i].check == C_OP && eqop) ||
(inst_code[i].check == C_F3 && eqop && eqf3) ||
(inst_code[i].check == C_37 && eqop && eqf3 && eqf7)){
return inst_code[i].instruction;
}
}
Assert(0, "Decode Error! Unsupported instruction perhaps: %x", inst.bytes);
return -1;
}
#undef FUNC
#define FUNC(str) do{ \
const uint32_t opcode = inst.R.opcode; \
uint32_t * const rd = (inst.R.rd) ? (pms->REG + inst.R.rd) : &pms->REG_discard; \
const uint32_t rs1 = pms->REG[inst.R.rs1], rs2 = pms->REG[inst.R.rs2]; \
{str;}; \
} while(0);
void fn_lui(machine_status_t* pms, inst_t inst){
FUNC(*rd = inst.U.imm31_12 << 12);
}
void fn_jalr(machine_status_t* pms, inst_t inst){
FUNC(*rd = pms->PC + 4;
pms->PC = (rs1 + inst.I.imm11_0) & 0xfffffffe);
}
void fn_lw(machine_status_t* pms, inst_t inst){
FUNC(*rd = *(uint32_t*)&pms->MEM[rs1 + inst.I.imm11_0]);
}
void fn_lbu(machine_status_t* pms, inst_t inst){
FUNC(*rd = pms->MEM[rs1 + inst.I.imm11_0]);
}
void fn_sb(machine_status_t* pms, inst_t inst){
FUNC(pms->MEM[rs1 + (inst.S.imm11_5 << 5) + inst.S.imm4_0] = (uint8_t)rs2);
}
void fn_sw(machine_status_t* pms, inst_t inst){
FUNC(*(uint32_t*)&pms->MEM[rs1 + (inst.S.imm11_5 << 5) + inst.S.imm4_0] = rs2);
}
void fn_addi(machine_status_t* pms, inst_t inst){
FUNC(*rd = rs1 + inst.I.imm11_0);
}
void fn_add(machine_status_t* pms, inst_t inst){
FUNC(*rd = rs1 + rs2);
}
#undef FUNC
void init_inst_exe_arr(){
inst_exe_arr[i_addi] = fn_addi;
}