-
Notifications
You must be signed in to change notification settings - Fork 2
/
Register.java
95 lines (95 loc) · 2.2 KB
/
Register.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
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
import java.util.*;
/**
* Object for holding all registers and associated methods
*
*/
public class Register {
public int[] r;
public int HI, LO, pc;
/**
* Initialize all registers to 0
*/
public Register() {
r = new int[32];
for(int i = 0; i < r.length; i++)
r[i] = 0;
HI = 0;
LO = 0;
pc = 0;
}
/**
* Get value from register
* @param index register number
* @return return 4 byte value from register (int)
*/
public int get(int index) {
return r[index];
}
/**
* Given a string name of a register (hi, lo, or pc) return that register
* @param type Register name
* @return 4 byte value from register (int)
*/
public int get(String type){
if (type.toLowerCase().equals("hi"))
return HI;
else if (type.toLowerCase().equals("lo"))
return LO;
else if (type.toLowerCase().equals("pc"))
return pc;
else
return -1;
}
/**
* Set value at given register
* Register 0 must remain 0
* @param index index of register to change
* @param o int value to put in register
*/
public void set(int index, int o) {
if(index == 0) return;
r[index] = o;
}
/**
* Set value at given register
* @param type string name of register (hi,lo,pc)
* @param value int value to put in register
*/
public void set(String type, int value){
if (type.toLowerCase().equals("hi"))
HI = value;
else if (type.toLowerCase().equals("lo"))
LO = value;
else if (type.toLowerCase().equals("pc"))
pc = value;
}
/**
* Print all registers as hexadecimal values
*/
public void printAll() {
for(int i = 0; i<r.length; i++)
printReg(i);
printReg("hi");
printReg("lo");
printReg("pc");
}
/**
* Print given register
* @param i index of register to print
*/
public void printReg(int i) {
System.out.println("$"+i+" = 0x" + MIPSEmulator.formatHex(r[i]));
}
/**
* Print given register based on string (hi, lo, pc)
* @param s string name of register
*/
public void printReg(String s) {
if(s.equals("lo"))
System.out.println("$LO=0x" + MIPSEmulator.formatHex(this.LO));
else if(s.equals("hi"))
System.out.println("$HI=0x" + MIPSEmulator.formatHex(this.HI));
else if(s.equals("pc"))
System.out.println("$PC=0x" + MIPSEmulator.formatHex(this.pc));
}
}