-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIsaSim copy.java
60 lines (47 loc) · 1.48 KB
/
IsaSim copy.java
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
/**
* RISC-V Instruction Set Simulator
* <p>
* A tiny first step to get the simulator started. Can execute just a single
* RISC-V instruction.
*
* @author Martin Schoeberl ([email protected])
*/
public class IsaSim {
static int pc;
static int reg[] = new int[4];
// Here the first program hard coded as an array
static int progr[] = {
// As minimal RISC-V assembler example
0x00200093, // addi x1 x0 2
0x00300113, // addi x2 x0 3
0x002081b3, // add x3 x1 x2
};
public static void main(String[] args) {
System.out.println("Hello RISC-V World!");
pc = 0;
for (; ; ) {
int instr = progr[pc >> 2];
int opcode = instr & 0x7f;
int rd = (instr >> 7) & 0x01f;
int rs1 = (instr >> 15) & 0x01f;
int imm = (instr >> 20);
switch (opcode) {
case 0x13:
reg[rd] = reg[rs1] + imm;
break;
default:
System.out.println("Opcode " + opcode + " not yet implemented");
break;
}
pc += 4; // One instruction is four bytes
if ((pc >> 2) >= progr.length) {
break;
}
for (int i = 0; i < reg.length; ++i) {
System.out.print(reg[i] + " ");
}
System.out.println();
}
System.out.println("Program exit");
}
}