-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathISA.java
More file actions
239 lines (217 loc) · 9.64 KB
/
ISA.java
File metadata and controls
239 lines (217 loc) · 9.64 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
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
import java.io.*;
import java.util.*;
public class ISA {
private Operations operations;
private Storage storage;
private void readOpcode(String opsFilePath_txt) throws FileNotFoundException {
Scanner sc = new Scanner(new FileInputStream(opsFilePath_txt));
ArrayList<ArrayList<String>> sup = new ArrayList<>();
ArrayList<String> sub = new ArrayList<>();
boolean found = sc.hasNextLine();
while (sc.hasNextLine()) {
String line = sc.nextLine();
if (line.length() == 0) {
sup.add(sub);
sub = new ArrayList<>();
continue;
}
String[] split = line.split(" ");
sub.addAll(Arrays.asList(split));
}
if (found) {
sup.add(sub);
}
sc.close();
operations = new Operations(sup, storage.getBit());
}
ArrayList<CodeLine> codeLines;
void readAssemblyCodeFromFile(String AssemblyFilePath_txt) throws FileNotFoundException {
Scanner sc;
try {
sc = new Scanner(new FileInputStream(AssemblyFilePath_txt));
} catch (Exception e) {
sc = new Scanner(System.in);
}
codeLines = new ArrayList<>();
boolean found = sc.hasNextLine();
while (true) {
if (!sc.hasNextLine()) break;
String line = sc.nextLine().toLowerCase();
System.out.print(line+"\t\t");
if (line.length() == 0) break;
String split[] = line.trim().split(" ");
int at = 0;
CodeLine currentLine = new CodeLine();
for (int i = 0; i < split.length; i++) {
if (split[i].length() > 0 && operations.map.containsKey(split[i])) {
currentLine.oxab[0] = split[i];
at = i + 1;
break;
}
}
if (at != 0) {
Operations.Info info = operations.map.get(currentLine.oxab[0]);
int wordcount = info.wordCount - 1, wordfound = 0;
String[] xab = new String[wordcount];
for (int i = at; i < split.length && wordfound < wordcount; i++) {
int firstBracketIndex = split[i].indexOf('(');
if (firstBracketIndex != -1) {
String outsizeBracket = split[i].substring(0, firstBracketIndex);
xab[wordfound++] = outsizeBracket;
int lastBracketIndex = split[i].indexOf(')', firstBracketIndex + 1);
String insideBracket = split[i].substring(firstBracketIndex + 1, lastBracketIndex);
xab[wordfound++] = insideBracket;
} else {
xab[wordfound++] = split[i];
}
}
if (wordfound == wordcount) {
if (wordcount == 1) {
currentLine.oxab[1] = xab[0];
} else {
currentLine.oxab[1] = xab[0];
currentLine.oxab[2] = xab[2];
currentLine.oxab[3] = xab[1];
}
}
codeLines.add(currentLine);
writeTheAssemblyCode();
}
}
sc.close();
}
private void writeTheAssemblyCode() {
CodeLine currentLine = codeLines.get(codeLines.size() - 1);
Operations.Info info = operations.map.get(currentLine.oxab[0]);
//TODO fun starts
currentLine.binaryCodeLine = new StringBuilder();
currentLine.hexCodeLine = new StringBuilder();
String lastBufferBinary = "";
String lastBufferHex = "";
int overflow = 5 - info.readInstruction.length();
int looping = 0;
for (int i = 0; i < info.readInstruction.length(); i++) {
if (looping++ > 5) {
System.out.println("You made spelling mistake maybe");
return;
}
if(currentLine.oxab[1].equals("$zero")){
System.out.println("You can't put something in zero, silly");
return;
}
if (info.readInstruction.charAt(i) == 'o') {
currentLine.binaryCodeLine.append(info.binaryCode);
} else if (info.readInstruction.charAt(i) == 'v') {
char c = info.writeInstruction.charAt(i);
if (c != 'x') {
String str = currentLine.oxab[(int) (c - '0')];
Storage.Info Info = storage.map.get(str);
if (Info == null) {
String temp = currentLine.oxab[c - '0'];
currentLine.oxab[c - '0'] = currentLine.oxab[3];
currentLine.oxab[3] = temp;
i = i - 1;
continue;
}
currentLine.binaryCodeLine.append(Info.binaryCode);
}
} else if (info.readInstruction.charAt(i) == 'n') {
char c = info.writeInstruction.charAt(i);
if (c == '-') {
lastBufferBinary = paddedStringBinary(currentLine.oxab[3], overflow);
continue;
}
String str = currentLine.oxab[(int) c - '0'];
currentLine.binaryCodeLine.append(paddedStringBinary(str, overflow));
}
}
currentLine.binaryCodeLine.append(lastBufferBinary);
currentLine.hexCodeLine.append(hexit(currentLine.binaryCodeLine.toString()));
System.out.print(currentLine.binaryCodeLine + " ");
System.out.println(currentLine.hexCodeLine);
}
private String paddedStringBinary(String str, int overflow) {
String binary = Integer.toBinaryString(Integer.parseInt(str));
String overflowStr = storage.getPaddingBinary().repeat(overflow);
String binaryStr = new String();
if (binary.length() > storage.getBit()) {
binaryStr = binary.substring(binary.length()-storage.getBit());
} else {
binaryStr = (storage.getPaddingBinary() + binary).substring(storage.getBit());
}
String paddedBinary = (overflowStr + binaryStr).substring(binaryStr.length());
return paddedBinary;
}
public String hexit(String bin) {
String hex = Integer.toString(Integer.parseInt(bin, 2), 16);
return ("0".repeat(storage.getBit()) + hex).substring(hex.length()).toUpperCase();
}
private String paddedStringHex(String str, int overflow) {
String hex = Integer.toHexString(Integer.parseInt(str));
String hexCap = ("0".repeat(overflow) + hex).substring(hex.length()).toUpperCase();
return hexCap;
}
public void printBinary() {
for (int i = 0; i < codeLines.size(); i++) {
System.out.println(codeLines.get(i).binaryCodeLine);
}
}
public void printHex() {
for (int i = 0; i < codeLines.size(); i++) {
System.out.println(codeLines.get(i).hexCodeLine);
}
}
public void printBinaryHex() {
for (int i = 0; i < codeLines.size(); i++) {
System.out.println(codeLines.get(i).binaryCodeLine + " " + codeLines.get(i).hexCodeLine);
}
}
public void printHexToFile() {
String string = "HexCode.txt";
try {
BufferedWriter out = new BufferedWriter(new FileWriter(string));
for (int i = 0; i < codeLines.size(); i++) {
out.write(String.valueOf(codeLines.get(i).hexCodeLine + ((codeLines.size() - 1 == i) ? "" : "\n")));
}
out.close();
System.out.println("Hex File created successfully" + System.getProperty("user.dir") + "\\" + string);
} catch (IOException e) {
e.printStackTrace();
}
}
public void printHexToFileLogisim() {
String string = "Load";
try {
BufferedWriter out = new BufferedWriter(new FileWriter(string));
out.write("v2.0 raw\n");
for (int i = 0; i < codeLines.size(); i++) {
out.write(String.valueOf(codeLines.get(i).hexCodeLine + ((codeLines.size() - 1 == i) ? "" : "\n")));
}
out.close();
System.out.println("Logisim File created successfully" + System.getProperty("user.dir") + "\\" + string);
} catch (IOException e) {
e.printStackTrace();
}
}
public void printBinaryToFile() {
String string = "BinaryCode.txt";
try {
BufferedWriter out = new BufferedWriter(new FileWriter(string));
for (int i = 0; i < codeLines.size(); i++) {
out.write(String.valueOf(codeLines.get(i).binaryCodeLine + ((codeLines.size() - 1 == i) ? "" : "\n")));
}
out.close();
System.out.println("Binary File created successfully " + System.getProperty("user.dir") + "\\" + string);
} catch (IOException e) {
e.printStackTrace();
}
}
private void builder(int bit, int totalTempSize, String OpcodePath_txt) throws FileNotFoundException {
storage = new Storage(bit, totalTempSize);
readOpcode(OpcodePath_txt);
}
public ISA(int bit, int totalTempSize, String OpcodePath_txt, String AssemblyCodePath_txt) throws FileNotFoundException {
builder(bit, totalTempSize, OpcodePath_txt);
readAssemblyCodeFromFile(AssemblyCodePath_txt);
}
}