-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHumbleAssembler.java
More file actions
604 lines (535 loc) · 26.4 KB
/
HumbleAssembler.java
File metadata and controls
604 lines (535 loc) · 26.4 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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
/**
* Allows for the user to input Strings to do commands instead of manually inputting an entire collection of bits
*/
public class HumbleAssembler {
static int BranchRegister;
private static final String HALT = "0000";
private static final String MOVE = "0001";
private static final String INTERRUPT = "0010";
private static final String AND = "1000";
private static final String OR = "1001";
private static final String XOR = "1010";
private static final String NOT = "1011";
private static final String LEFTSHIFT = "1100";
private static final String RIGHTSHIFT = "1101";
private static final String ADD = "1110";
private static final String SUBTRACT = "1111";
private static final String MULTIPLY = "0111";
private static final String BRANCH = "0101";
private static final String STACK = "0110";
/**
* This will interpret all inputs from user, convert from the inputted String to the binary equivalent for each part. Then all of these binary
* Strings will be assembled together, to be used.
* @param Inputs Seperates each instruction so that the methods can act accordingly.
* @return Returns the String output for the preload to use.
* @throws IllegalStateException Throws an exception in cases where user puts in the wrong register index, or opcode string name.
*/
public static String assemble(String[] Inputs) throws IllegalStateException {
String AssembledString = "";
switch (Inputs[0]) {
case "halt":
AssembledString = AssembledString + HALT + "000000000000";
break;
case "move":
AssembledString = AssembledString + MOVE;
if (Inputs[1] != null) {
String inputtemp = Inputs[1];
AssembledString = AssembledString + Inputs1Switch(inputtemp, AssembledString);
}else{
throw new IllegalStateException("Unexpected value: " + null);
}
if (Inputs[2] != null) {
String StringValue = Inputs[2];
int IntValue = Integer.parseInt(StringValue);
HumbleLongWord LongWordValue = new HumbleLongWord();
LongWordValue.set(IntValue);
if (LongWordValue.getBit(7).getValue()) {
AssembledString = AssembledString + "1";
} else {
AssembledString = AssembledString + "0";
}
if (LongWordValue.getBit(6).getValue()) {
AssembledString = AssembledString + "1";
} else {
AssembledString = AssembledString + "0";
}
if (LongWordValue.getBit(5).getValue()) {
AssembledString = AssembledString + "1";
} else {
AssembledString = AssembledString + "0";
}
if (LongWordValue.getBit(4).getValue()) {
AssembledString = AssembledString + "1";
} else {
AssembledString = AssembledString + "0";
}
if (LongWordValue.getBit(3).getValue()) {
AssembledString = AssembledString + "1";
} else {
AssembledString = AssembledString + "0";
}
if (LongWordValue.getBit(2).getValue()) {
AssembledString = AssembledString + "1";
} else {
AssembledString = AssembledString + "0";
}
if (LongWordValue.getBit(1).getValue()) {
AssembledString = AssembledString + "1";
} else {
AssembledString = AssembledString + "0";
}
if (LongWordValue.getBit(0).getValue()) {
AssembledString = AssembledString + "1";
} else {
AssembledString = AssembledString + "0";
}
} else {
throw new IllegalStateException("Unexpected value: " + null);
}
break;
case "interrupt":
AssembledString = AssembledString + INTERRUPT;
if ("0".equals(Inputs[1])) {
AssembledString = AssembledString + "000000000000";
} else {
AssembledString = AssembledString + "000000000001";
}
break;
case "and":
AssembledString = AssembledString + AND;
if (Inputs[1] != null) {
String inputtemp = Inputs[1];
AssembledString = AssembledString + Inputs1Switch(inputtemp, AssembledString);
} else {
throw new IllegalStateException("Unexpected value: " + null);
}
if (Inputs[2] != null) {
String inputtemp = Inputs[2];
AssembledString = AssembledString + Inputs2Switch(inputtemp, AssembledString);
} else {
throw new IllegalStateException("Unexpected value: " + null);
}
AssembledString = AssembledString + "0000";
break;
case "or":
AssembledString = AssembledString + OR;
if (Inputs[1] != null) {
String inputtemp = Inputs[1];
AssembledString = AssembledString + Inputs1Switch(inputtemp, AssembledString);
} else {
throw new IllegalStateException("Unexpected value: " + null);
}
if (Inputs[2] != null) {
String inputtemp = Inputs[2];
AssembledString = AssembledString + Inputs2Switch(inputtemp, AssembledString);
} else {
throw new IllegalStateException("Unexpected value: " + null);
}
AssembledString = AssembledString + "0000";
break;
case "xor":
AssembledString = AssembledString + XOR;
if (Inputs[1] != null) {
String inputtemp = Inputs[1];
AssembledString = AssembledString + Inputs1Switch(inputtemp, AssembledString);
} else {
throw new IllegalStateException("Unexpected value: " + null);
}
if (Inputs[2] != null) {
String inputtemp = Inputs[2];
AssembledString = AssembledString + Inputs2Switch(inputtemp, AssembledString);
} else {
throw new IllegalStateException("Unexpected value: " + null);
}
AssembledString = AssembledString + "0000";
break;
case "not":
AssembledString = AssembledString + NOT;
if (Inputs[1] != null) {
String inputtemp = Inputs[1];
AssembledString = AssembledString + Inputs1Switch(inputtemp, AssembledString);
} else {
throw new IllegalStateException("Unexpected value: " + null);
}
if (Inputs[2] != null) {
String inputtemp = Inputs[2];
AssembledString = AssembledString + Inputs2Switch(inputtemp, AssembledString);
} else {
throw new IllegalStateException("Unexpected value: " + null);
}
AssembledString = AssembledString + "0000";
break;
case "leftshift":
AssembledString = AssembledString + LEFTSHIFT;
if (Inputs[1] != null) {
String inputtemp = Inputs[1];
AssembledString = AssembledString + Inputs1Switch(inputtemp, AssembledString);
} else {
throw new IllegalStateException("Unexpected value: " + null);
}
if (Inputs[2] != null) {
String inputtemp = Inputs[2];
AssembledString = AssembledString + Inputs2Switch(inputtemp, AssembledString);
} else {
throw new IllegalStateException("Unexpected value: " + null);
}
AssembledString = AssembledString + "0000";
break;
case "rightshift":
AssembledString = AssembledString + RIGHTSHIFT;
if (Inputs[1] != null) {
String inputtemp = Inputs[1];
AssembledString = AssembledString + Inputs1Switch(inputtemp, AssembledString);
} else {
throw new IllegalStateException("Unexpected value: " + null);
}
if (Inputs[2] != null) {
String inputtemp = Inputs[2];
AssembledString = AssembledString + Inputs2Switch(inputtemp, AssembledString);
} else {
throw new IllegalStateException("Unexpected value: " + null);
}
AssembledString = AssembledString + "0000";
break;
case "add":
AssembledString = AssembledString + ADD;
if (Inputs[1] != null) {
String inputtemp = Inputs[1];
AssembledString = AssembledString + Inputs1Switch(inputtemp, AssembledString);
} else {
throw new IllegalStateException("Unexpected value: " + null);
}
if (Inputs[2] != null) {
String inputtemp = Inputs[2];
AssembledString = AssembledString + Inputs2Switch(inputtemp, AssembledString);
} else {
throw new IllegalStateException("Unexpected value: " + null);
}
AssembledString = AssembledString + "0000";
break;
case "subtract":
AssembledString = AssembledString + SUBTRACT;
if (Inputs[1] != null) {
String inputtemp = Inputs[1];
AssembledString = AssembledString + Inputs1Switch(inputtemp, AssembledString);
} else {
throw new IllegalStateException("Unexpected value: " + null);
}
if (Inputs[2] != null) {
String inputtemp = Inputs[2];
AssembledString = AssembledString + Inputs2Switch(inputtemp, AssembledString);
} else {
throw new IllegalStateException("Unexpected value: " + null);
}
AssembledString = AssembledString + "0000";
break;
case "multiply":
AssembledString = AssembledString + MULTIPLY;
if (Inputs[1] != null) {
String inputtemp = Inputs[1];
AssembledString = AssembledString + Inputs1Switch(inputtemp, AssembledString);
} else {
throw new IllegalStateException("Unexpected value: " + null);
}
if (Inputs[2] != null) {
String inputtemp = Inputs[2];
AssembledString = AssembledString + Inputs2Switch(inputtemp, AssembledString);
} else {
throw new IllegalStateException("Unexpected value: " + null);
}
AssembledString = AssembledString + "0000";
break;
case "BranchLessThan":
HumbleComputer CrossOverComputerL = new HumbleComputer();
String[] ArrayOfStrings2 = new String[2];
String BranchFirstString = "0101001111111111"; //00
String BranchSecondString = "0101001111111111";
ArrayOfStrings2[0] = BranchFirstString;
ArrayOfStrings2[1] = BranchSecondString;
CrossOverComputerL.preload(ArrayOfStrings2);
CrossOverComputerL.run();
AssembledString = AssembledString + BRANCH;
AssembledString = AssembledString + CrossOverComputerL.AssembleBranchString; //auto is correct
AssembledString = AssembledString + CrossOverComputerL.AsssembleSignedString; //auto right
AssembledString = AssembledString + "000000000"; //last 9 bits
if(Inputs[1] != null){
String StringValue = Inputs[1];
BranchRegister = Integer.parseInt(StringValue);
} else {
throw new IllegalStateException("Unexpected value: " + null);
}
break;
case "BranchGreaterThan":
HumbleComputer CrossOverComputerG = new HumbleComputer();
String[] ArrayOfStrings2BranchGreaterThan = new String[2];
String GFirstString = "0101010111111111"; //01
String GSecondString = "0101010111111111";
ArrayOfStrings2BranchGreaterThan[0] = GFirstString;
ArrayOfStrings2BranchGreaterThan[1] = GSecondString;
CrossOverComputerG.preload(ArrayOfStrings2BranchGreaterThan);
CrossOverComputerG.run();
AssembledString = AssembledString + BRANCH;
AssembledString = AssembledString + CrossOverComputerG.AssembleBranchString; //auto is correct
AssembledString = AssembledString + CrossOverComputerG.AsssembleSignedString; //auto right
AssembledString = AssembledString + "000000000"; //last 9 bits
if(Inputs[1] != null){
String StringValue = Inputs[1];
BranchRegister = Integer.parseInt(StringValue);
} else {
throw new IllegalStateException("Unexpected value: " + null);
}
break;
case "BranchLessThanOrEqual":
HumbleComputer CrossOverComputerLE = new HumbleComputer();
String[] ArrayOfStrings2LE = new String[2];
String LEFirstString = "0101101111111111"; //10
String LESecondString = "0101101111111111";
ArrayOfStrings2LE[0] = LEFirstString;
ArrayOfStrings2LE[1] = LESecondString;
CrossOverComputerLE.preload(ArrayOfStrings2LE);
CrossOverComputerLE.run();
AssembledString = AssembledString + BRANCH;
AssembledString = AssembledString + CrossOverComputerLE.AssembleBranchString; //auto is correct
AssembledString = AssembledString + CrossOverComputerLE.AsssembleSignedString; //auto right
AssembledString = AssembledString + "000000000"; //last 9 bits
if(Inputs[1] != null){
String StringValue = Inputs[1];
BranchRegister = Integer.parseInt(StringValue);
} else {
throw new IllegalStateException("Unexpected value: " + null);
}
break;
case "BranchGreaterThanOrEqual":
HumbleComputer CrossOverComputerGE = new HumbleComputer();
String[] ArrayOfStrings2GE = new String[2];
String GEFirstString = "0101110111111111"; //11
String GESecondString = "0101110111111111";
ArrayOfStrings2GE[0] = GEFirstString;
ArrayOfStrings2GE[1] = GESecondString;
CrossOverComputerGE.preload(ArrayOfStrings2GE);
CrossOverComputerGE.run();
AssembledString = AssembledString + BRANCH;
AssembledString = AssembledString + CrossOverComputerGE.AssembleBranchString; //auto is correct
AssembledString = AssembledString + CrossOverComputerGE.AsssembleSignedString; //auto right
AssembledString = AssembledString + "000000000"; //last 9 bits
if(Inputs[1] != null){
String StringValue = Inputs[1];
BranchRegister = Integer.parseInt(StringValue);
} else {
throw new IllegalStateException("Unexpected value: " + null);
}
break;
case "push":
AssembledString = AssembledString + STACK;
AssembledString = AssembledString + "00000000";
if (Inputs[1] != null) {
String inputtemp = Inputs[1];
AssembledString = AssembledString + Inputs1Switch(inputtemp, AssembledString);
} else {
throw new IllegalStateException("Unexpected value: " + null);
}
break;
case "pop":
AssembledString = AssembledString + STACK;
AssembledString = AssembledString + "01000000";
if (Inputs[1] != null) {
String inputtemp = Inputs[1];
AssembledString = AssembledString + Inputs1Switch(inputtemp, AssembledString);
} else {
throw new IllegalStateException("Unexpected value: " + null);
}
break;
case "call":
AssembledString = AssembledString + STACK;
AssembledString = AssembledString + "10";
if (Inputs[1] != null) {
String StringValue = Inputs[1];
int IntValue = Integer.parseInt(StringValue);
HumbleLongWord LongWordValue = new HumbleLongWord();
LongWordValue.set(IntValue);
if (LongWordValue.getBit(9).getValue()) {
AssembledString = AssembledString + "1";
} else {
AssembledString = AssembledString + "0";
}
if (LongWordValue.getBit(8).getValue()) {
AssembledString = AssembledString + "1";
} else {
AssembledString = AssembledString + "0";
}
if (LongWordValue.getBit(7).getValue()) {
AssembledString = AssembledString + "1";
} else {
AssembledString = AssembledString + "0";
}
if (LongWordValue.getBit(6).getValue()) {
AssembledString = AssembledString + "1";
} else {
AssembledString = AssembledString + "0";
}
if (LongWordValue.getBit(5).getValue()) {
AssembledString = AssembledString + "1";
} else {
AssembledString = AssembledString + "0";
}
if (LongWordValue.getBit(4).getValue()) {
AssembledString = AssembledString + "1";
} else {
AssembledString = AssembledString + "0";
}
if (LongWordValue.getBit(3).getValue()) {
AssembledString = AssembledString + "1";
} else {
AssembledString = AssembledString + "0";
}
if (LongWordValue.getBit(2).getValue()) {
AssembledString = AssembledString + "1";
} else {
AssembledString = AssembledString + "0";
}
if (LongWordValue.getBit(1).getValue()) {
AssembledString = AssembledString + "1";
} else {
AssembledString = AssembledString + "0";
}
if (LongWordValue.getBit(0).getValue()) {
AssembledString = AssembledString + "1";
} else {
AssembledString = AssembledString + "0";
}
} else {
throw new IllegalStateException("Unexpected value: " + null);
}
break;
case "return":
AssembledString = AssembledString + STACK;
AssembledString = AssembledString + "110000000000";
break;
}
return AssembledString;
}
/**
* The Register to binary conversion is needed a lot, so to avoid rewriting the same code, a method is created that can be called.
* @param inputtemp Arbitrary temp variable used so the switch statement knows to u se the Input[1] from the user
* @param Input1AssembledString Arbitrary String value, but will contain the binary equivalent of a register number.
* @return The binary equivalent of the register number, that will be added to the AssembledString.
*/
public static String Inputs1Switch(String inputtemp, String Input1AssembledString) {
Input1AssembledString = "";
switch (inputtemp) {
case "R0":
Input1AssembledString = Input1AssembledString + "0000";
break;
case "R1":
Input1AssembledString = Input1AssembledString + "0001";
break;
case "R2":
Input1AssembledString = Input1AssembledString + "0010";
break;
case "R3":
Input1AssembledString = Input1AssembledString + "0011";
break;
case "R4":
Input1AssembledString = Input1AssembledString + "0100";
break;
case "R5":
Input1AssembledString = Input1AssembledString + "0101";
break;
case "R6":
Input1AssembledString = Input1AssembledString + "0110";
break;
case "R7":
Input1AssembledString = Input1AssembledString + "0111";
break;
case "R8":
Input1AssembledString = Input1AssembledString + "1000";
break;
case "R9":
Input1AssembledString = Input1AssembledString + "1001";
break;
case "R10":
Input1AssembledString = Input1AssembledString + "1010";
break;
case "R11":
Input1AssembledString = Input1AssembledString + "1011";
break;
case "R12":
Input1AssembledString = Input1AssembledString + "1100";
break;
case "R13":
Input1AssembledString = Input1AssembledString + "1101";
break;
case "R14":
Input1AssembledString = Input1AssembledString + "1110";
break;
case "R15":
Input1AssembledString = Input1AssembledString + "1111";
break;
default:
throw new IllegalStateException("Unexpected value: " + inputtemp);
}
return Input1AssembledString;
}
/**
* The Register to binary conversion is needed a lot, so to avoid rewriting the same code, a method is created that can be called.
* @param inputtemp Arbitrary temp variable used so the switch statement knows to u se the Input[2] from the user
* @param Input2AssembledString Arbitrary String value, but will contain the binary equivalent of a register number.
* @return The binary equivalent of the register number, that will be added to the AssembledString.
*/
public static String Inputs2Switch(String inputtemp, String Input2AssembledString) {
Input2AssembledString = "";
switch (inputtemp) {
case "R0":
Input2AssembledString = Input2AssembledString + "0000";
break;
case "R1":
Input2AssembledString = Input2AssembledString + "0001";
break;
case "R2":
Input2AssembledString = Input2AssembledString + "0010";
break;
case "R3":
Input2AssembledString = Input2AssembledString + "0011";
break;
case "R4":
Input2AssembledString = Input2AssembledString + "0100";
break;
case "R5":
Input2AssembledString = Input2AssembledString + "0101";
break;
case "R6":
Input2AssembledString = Input2AssembledString + "0110";
break;
case "R7":
Input2AssembledString = Input2AssembledString + "0111";
break;
case "R8":
Input2AssembledString = Input2AssembledString + "1000";
break;
case "R9":
Input2AssembledString = Input2AssembledString + "1001";
break;
case "R10":
Input2AssembledString = Input2AssembledString + "1010";
break;
case "R11":
Input2AssembledString = Input2AssembledString + "1011";
break;
case "R12":
Input2AssembledString = Input2AssembledString + "1100";
break;
case "R13":
Input2AssembledString = Input2AssembledString + "1101";
break;
case "R14":
Input2AssembledString = Input2AssembledString + "1110";
break;
case "R15":
Input2AssembledString = Input2AssembledString + "1111";
break;
default:
throw new IllegalStateException("Unexpected value: " + inputtemp);
}
return Input2AssembledString;
}
}