-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRAM.java
164 lines (148 loc) · 4.13 KB
/
RAM.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
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
import java.util.*;
public class RAM {
// HEX
// EX "x01010010" => 0000 0001 0000 0001 0000 0000 0001 0000 (32-bit instruction)
private ArrayList<String> instructions;
private int[] powersOfTwo = new int[] {3, 10, 20, 30, 40, 50, 60, 70, 80};
private TreeMap<Character, ArrayList<String>> validCharacters;
private String[] infoSizes = new String[] {"Byte", "KiloByte",
"MegaByte", "GigaByte", "TerraByte",
"PetaByte", "ExaByte", "ZettaByte",
"YottaByte"};
public RAM() {
validCharacters = new TreeMap<>();
instructions = new ArrayList<>();
for (int i = 0; i < infoSizes.length; i++) {
char c = infoSizes[i].charAt(0);
validCharacters.put(c, new ArrayList<String>());
}
}
// Input of the form ("x00000000") for HEX to 32-bit BINARY
public void addInstruction(String hex) {
if (hex.length() != 10 || hex.charAt(0) != '0' || hex.charAt(1) != 'x') {
System.out.println("Invalid Input " + hex);
System.out.println("Input should be of form : 0x00000000 for a 32-bit instruction");
System.out.println();
return;
} else {
String binary = "";
int i = 2;
while (i < hex.length()) {
char c = hex.charAt(i);
if (c < 48 || (c > 57 && c < 97) || c > 102) {
System.out.println("Invalid Input " + hex);
System.out.println("Input should be of form : 0x00000000 for a 32-bit instruction");
return;
}
int conversion = c;
if (c >= 48 && c <= 57) {
binary += padLeft(Integer.toBinaryString(c - 48), 4);
binary += " "; // easier read
} else {
int h = 10;
int index = 0;
char[] hexArr = new char[] {'a', 'b', 'c', 'd', 'e', 'f'};
// a b c d e f
while (index < hexArr.length) {
if (c == hexArr[index]) {
binary += padLeft(Integer.toBinaryString(h), 4);
binary += " "; // easier read
break;
} else {
h++;
index++;
}
}
}
i++;
}
instructions.add(binary);
}
}
private String padLeft(String s, int i) {
if (s.length() >= i) {
System.out.println("String length " + s.length() + " is greater than or equal to " + i);
return s;
} else {
while (s.length() < i) {
s = "0" + s;
}
return s;
}
}
public void readInstructions() {
for (String s: instructions) {
System.out.println(s);
}
}
public void addLib(String s) {
if (s.length() <= 0) {
return;
}
char c = s.charAt(0);
if (validCharacters.containsKey(c)) {
//add to adLib list
validCharacters.get(c).add(s);
}
}
public void printValues() {
System.out.println("Ad Lib Values:");
System.out.println();
for (Character c: validCharacters.keySet()) {
ArrayList<String> arr = validCharacters.get(c);
for (int i = 0; i < arr.size(); i++) {
System.out.print(arr.get(i) + " ");
}
System.out.println();
}
System.out.println();
}
public void createAdLib() {
String ret = "";
Random r = new Random();
for (int i = 0; i < infoSizes.length; i++) {
char c = infoSizes[i].charAt(0);
ArrayList<String> wordList = validCharacters.get(c);
if (wordList.size() == 0) {
System.out.println("Not Enough Characters");
System.out.println();
return;
}
int randVal = r.nextInt(wordList.size());
ret += wordList.get(randVal) + " ";
}
System.out.println(ret);
System.out.println();
}
public static void main(String[] args) {
RAM ram = new RAM();
/*ram.addLib("Betty");
ram.addLib("Keeps");
ram.addLib("Many");
ram.addLib("Goals");
ram.addLib("Together");
ram.addLib("Patiently");
ram.addLib("Expecting");
ram.addLib("Zebra");
ram.createAdLib(); // not enough values
ram.addLib("Yawns");
ram.createAdLib();
ram.addLib("Bob");
ram.addLib("Kicks");
ram.addLib("Galleries");
ram.addLib("Practically");
ram.addLib("Experiencing");
ram.addLib("Zany");
ram.addLib("Yellow");
ram.createAdLib();
ram.printValues();*/
//System.out.println(Integer.toBinaryString(""));
ram.addInstruction("0x0000000f");
ram.addInstruction("0xffffffff");
ram.addInstruction("0xa0000001");
ram.addInstruction("0x00e00011");
ram.addInstruction("0x00g00001"); // wrong
ram.addInstruction("0x001100b1");
ram.readInstructions();
}
}