-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstructions.rs
275 lines (256 loc) · 8.32 KB
/
instructions.rs
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
pub mod registers;
pub mod instruction_types;
extern crate phf;
use phf::phf_map;
pub type InstFunc = fn(instruction_types::DecodedInst);
#[allow(dead_code)]
pub static I_CODES: phf::Map<i32, InstFunc> = phf_map!{
0x0i32 => addi,
0x4i32 => xori,
0x6i32 => ori,
0x7i32 => andi,
0x1i32 => slli,
0x5i32 => srai,
0x2i32 => slti,
0x3i32 => sltiu
};
//RV32I instruction set functions sorted by opcode.
// TYPE R - OPCODE 0110011
#[allow(dead_code)]
pub fn add(instruction: instruction_types::DecodedInst){
let operand1 = registers::get(instruction.rs1);
let operand2 = registers::get(instruction.rs2);
registers::set(instruction.rd, operand1 + operand2);
}
#[allow(dead_code)]
pub fn sub(instruction: instruction_types::DecodedInst){
let operand1 = registers::get(instruction.rs1);
let operand2 = registers::get(instruction.rs2);
registers::set(instruction.rd, operand1 - operand2);
}
#[allow(dead_code)]
pub fn xor(instruction: instruction_types::DecodedInst){
let operand1 = registers::get(instruction.rs1);
let operand2 = registers::get(instruction.rs2);
registers::set(instruction.rd, operand1 ^ operand2);
}
#[allow(dead_code)]
pub fn or(instruction: instruction_types::DecodedInst){
let operand1 = registers::get(instruction.rs1);
let operand2 = registers::get(instruction.rs2);
registers::set(instruction.rd, operand1 | operand2);
}
#[allow(dead_code)]
pub fn and(instruction: instruction_types::DecodedInst){
let operand1 = registers::get(instruction.rs1);
let operand2 = registers::get(instruction.rs2);
registers::set(instruction.rd, operand1 & operand2);
}
#[allow(dead_code)]
pub fn sll(instruction: instruction_types::DecodedInst){
let operand1 = registers::get(instruction.rs1);
let operand2 = registers::get(instruction.rs2);
// Logical shift left source1 by the value stored in the lowest 5 bits of source2.
registers::set(instruction.rd, operand1 << (operand2 & 0x20));
}
#[allow(dead_code)]
pub fn srl(instruction: instruction_types::DecodedInst){
let operand1 = registers::get(instruction.rs1);
let operand2 = registers::get(instruction.rs2);
// Logical shift right source1 by the value stored in the lowest 5 bits of source2.
registers::set(instruction.rd, operand1 >> (operand2 & 0x20));
}
#[allow(dead_code)]
pub fn sra(instruction: instruction_types::DecodedInst){
let operand1 = registers::get(instruction.rs1);
let operand2 = registers::get(instruction.rs2);
// Arithmetic shift right source1 by the value stored in the lowest 5 bits of source2.
//TODO: Rust seems to chose arithmetic or logic shift automatically. So I think both source needs to be signed.
registers::set(instruction.rd, operand1 >> (operand2 & 0x20));
}
#[allow(dead_code)]
pub fn slt(instruction: instruction_types::DecodedInst){
let operand1 = registers::get(instruction.rs1);
let operand2 = registers::get(instruction.rs2);
let result = if operand1 < operand2 { 1 } else { 0};
registers::set(instruction.rd, result);
}
#[allow(dead_code)]
pub fn sltu(instruction: instruction_types::DecodedInst){
let operand1 = registers::get(instruction.rs1);
let operand2 = registers::get(instruction.rs2);
let result = if operand1 < operand2 { 1 } else { 0};
registers::set(instruction.rd, result);
}
// TYPE - I OPCODE 0010011
#[allow(dead_code)]
pub fn addi(instruction: instruction_types::DecodedInst){
let rs1 = registers::get(instruction.rs1);
registers::set(instruction.rd, rs1 + instruction.imm);
println!("ADDI");
}
#[allow(dead_code)]
pub fn xori(instruction: instruction_types::DecodedInst){
let operand1 = registers::get(instruction.rs1.try_into().unwrap());
registers::set(instruction.rd, operand1 ^ instruction.imm);
println!("XORI");
}
#[allow(dead_code)]
pub fn andi(instruction: instruction_types::DecodedInst){
let operand1 = registers::get(instruction.rs1);
registers::set(instruction.rd, operand1 & instruction.imm);
println!("ANDI");
}
#[allow(dead_code)]
pub fn ori(instruction: instruction_types::DecodedInst){
let operand1 = registers::get(instruction.rs1);
registers::set(instruction.rd, operand1 | instruction.imm);
println!("ORI");
}
#[allow(dead_code)]
pub fn slli(instruction: instruction_types::DecodedInst){
let operand1 = registers::get(instruction.rs1);
registers::set(instruction.rd, operand1 << (instruction.imm & 0x20));
println!("SLLI");
}
#[allow(dead_code)]
pub fn srli(instruction: instruction_types::DecodedInst){
let operand1 = registers::get(instruction.rs1);
registers::set(instruction.rd, operand1 >> (instruction.imm & 0x20));
println!("SRLI");
}
#[allow(dead_code)]
pub fn srai(instruction: instruction_types::DecodedInst){
let operand1 = registers::get(instruction.rs1);
registers::set(instruction.rd, operand1 >> (instruction.imm & 0x20));
println!("SRLI/SRAI");
}
#[allow(dead_code)]
pub fn slti(instruction: instruction_types::DecodedInst){
let operand1 = registers::get(instruction.rs1);
let result = if operand1 < instruction.imm { 1 } else { 0};
registers::set(instruction.rd, result);
println!("SLTI");
}
#[allow(dead_code)]
pub fn sltiu(instruction: instruction_types::DecodedInst){
let operand1 = registers::get(instruction.rs1) as u32;
let result = if operand1 < instruction.imm as u32 { 1 } else { 0};
registers::set(instruction.rd, result);
println!("SLTIU");
}
// TYPE I - OPCODE 00000011
#[allow(dead_code)]
pub fn lb(){}
#[allow(dead_code)]
pub fn lh(){}
#[allow(dead_code)]
pub fn lw(){}
#[allow(dead_code)]
pub fn lbu(){}
#[allow(dead_code)]
pub fn lhu(){}
// TYPE S - OPCODE 0100011
#[allow(dead_code)]
pub fn sb(){}
#[allow(dead_code)]
pub fn sh(){}
#[allow(dead_code)]
pub fn sw(){}
// TYPE B - OPCODE 1100011
#[allow(dead_code)]
pub fn beq(instruction: instruction_types::DecodedInst){
let operand1 = registers::get(instruction.rs1);
let operand2 = registers::get(instruction.rs2);
if operand1 == operand2
{
unsafe{
registers::PROGRAM_COUNTER += instruction.imm as u32;
}
}
}
#[allow(dead_code)]
pub fn bne(instruction: instruction_types::DecodedInst){
let operand1 = registers::get(instruction.rs1);
let operand2 = registers::get(instruction.rs2);
if operand1 != operand2
{
unsafe{
registers::PROGRAM_COUNTER += instruction.imm as u32;
}
}
}
#[allow(dead_code)]
pub fn blt(instruction: instruction_types::DecodedInst){
let operand1 = registers::get(instruction.rs1);
let operand2 = registers::get(instruction.rs2);
if operand1 < operand2
{
unsafe{
registers::PROGRAM_COUNTER += instruction.imm as u32;
}
}
}
#[allow(dead_code)]
pub fn bge(instruction: instruction_types::DecodedInst){
let operand1 = registers::get(instruction.rs1);
let operand2 = registers::get(instruction.rs2);
if operand1 >= operand2
{
unsafe{
registers::PROGRAM_COUNTER += instruction.imm as u32;
}
}
}
#[allow(dead_code)]
pub fn bltu(instruction: instruction_types::DecodedInst){
let operand1 = registers::get(instruction.rs1);
let operand2 = registers::get(instruction.rs2);
if operand1 < operand2
{
unsafe{
registers::PROGRAM_COUNTER += instruction.imm as u32;
}
}
}
#[allow(dead_code)]
pub fn bgeu(instruction: instruction_types::DecodedInst){
let operand1 = registers::get(instruction.rs1);
let operand2 = registers::get(instruction.rs2);
if operand1 == operand2
{
unsafe{
registers::PROGRAM_COUNTER += instruction.imm as u32;
}
}
}
// TYPE J/I - OPCODE 1101111
#[allow(dead_code)]
pub fn jal(instruction: instruction_types::DecodedInst){
unsafe{
registers::PROGRAM_COUNTER += instruction.imm as u32;
}
registers::set(instruction.rs1, registers::get(instruction.rs1) + 4);
}
#[allow(dead_code)]
pub fn jalr(){
//TODO
}
// TYPE U - OPCODE 0110111
#[allow(dead_code)]
pub fn lui(){}
#[allow(dead_code)]
pub fn auipc(){}
// TYPE I - OPCODE 1110011
#[allow(dead_code)]
pub fn ecall(){
//TODO: Raises EnvironmentCall
}
#[allow(dead_code)]
pub fn ebreak(){
//TODO: Raises Breakpoint
}
// OPCODE 0000111 (TODO: confirm opcode for this instruction)
#[allow(dead_code)]
pub fn fence(){}
// In the future, functions for RISC ISA extensions will be defined below.