forked from mahdihaghverdi/cpu
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request mahdihaghverdi#4 from Sayed-Hossein-Hosseini/main
Complete the project and add the Logisim CPU file
- Loading branch information
Showing
10 changed files
with
1,629 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<module type="JAVA_MODULE" version="4"> | ||
<component name="NewModuleRootManager" inherit-compiler-output="true"> | ||
<exclude-output /> | ||
<content url="file://$MODULE_DIR$"> | ||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> | ||
</content> | ||
<orderEntry type="inheritedJdk" /> | ||
<orderEntry type="sourceFolder" forTests="false" /> | ||
</component> | ||
</module> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
public class DecimalToBinary { | ||
|
||
public static String computing(String result) { // Decimal to Binary | ||
int number = Integer.parseInt(result); | ||
result = ""; | ||
|
||
while(number > 0) { | ||
result = (number % 2) + result; | ||
number /= 2; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
public static String extend(String result) { // extend to 21 bit | ||
result = computing(result); | ||
int siza = result.length(); | ||
for (int i = 0; i < 21 - siza; i++) { | ||
result = "0" + result; | ||
} | ||
|
||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Scanner; | ||
|
||
public class Instruction { | ||
private static String machineCode; | ||
private static Map<String, String> lable = new HashMap<>(); | ||
|
||
public static String main(String instructs) { | ||
instructs = lable(instructs); | ||
String result = ""; | ||
Scanner scanner = new Scanner(instructs); | ||
while (scanner.hasNextLine()) { | ||
result += instruct(scanner.nextLine()) + "\n"; | ||
} | ||
|
||
return result; | ||
} | ||
private static String instruct(String instruct) { | ||
machineCode = ""; | ||
instruct = instruct.trim(); | ||
String[] type = instruct.split(" ", 2); // Command Type Separation | ||
String typeValidation = R_type.callOrder(type[0]); // To help with validation | ||
|
||
if (typeValidation != null) { // R-type | ||
String[] register = type[1].split(", "); | ||
machineCode += "000" + // OpCode | ||
RegisterFile.callRegister(register[0]) + // rs | ||
RegisterFile.callRegister(register[1]) + // rt | ||
RegisterFile.callRegister(register[2]) + // rd | ||
"00000000000000" + // Don't Care | ||
typeValidation; // Function | ||
|
||
} else { // V-type | ||
machineCode += V_type.callOrder(type[0]); | ||
|
||
if (type[0].equals("lw") || type[0].equals("sw")) { // sw & lw commands | ||
String[] register = type[1].split(", "); | ||
machineCode += RegisterFile.callRegister(register[0].substring(0, 3)) + // rs | ||
RegisterFile.callRegister(register[1].substring(register[1].indexOf('(') + 1, register[1].indexOf(')'))) + // rt | ||
DecimalToBinary.extend(register[1].substring(0, register[1].indexOf('('))); // immediate | ||
|
||
} else { | ||
String[] register = type[1].split(", "); | ||
if (type[0].equals("bnq")) { // bnq commands | ||
machineCode += RegisterFile.callRegister(register[0]) + // rs | ||
RegisterFile.callRegister(register[1]) + // rt | ||
DecimalToBinary.extend(lable.get(register[2].trim())); // immediate | ||
|
||
} else { // other commands | ||
machineCode += RegisterFile.callRegister(register[0]) + // rs | ||
RegisterFile.callRegister(register[1]) + // rt | ||
DecimalToBinary.extend(register[2]); // immediate | ||
|
||
} | ||
} | ||
} | ||
return machineCode; | ||
} | ||
|
||
private static String lable(String instructs) { | ||
Scanner scanner = new Scanner(instructs); | ||
String instruct = ""; | ||
int count = 0; | ||
|
||
while (scanner.hasNextLine()) { | ||
String place = scanner.nextLine(); | ||
|
||
if (place.indexOf(':') != -1) { | ||
lable.put(place.substring(0, place.indexOf(':')).trim(), Integer.toString(count)); // lables | ||
instruct += place.substring(place.indexOf(':') + 1) + "\n"; | ||
} else { | ||
instruct += place + "\n"; | ||
} | ||
|
||
count++; | ||
} | ||
|
||
return instruct; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import java.util.Scanner; | ||
|
||
public class Main { | ||
public static void main(String[] args) { | ||
Scanner scanner = new Scanner(System.in); | ||
System.out.println("*** Wellcome to Program to convert assembly language to machine code ***\n" + | ||
"Please enter the assembly instructions : \n" + | ||
"(Note: At the end of the commands, enter the word 'end')"); | ||
String order = ""; | ||
while (scanner.hasNextLine()) { | ||
String line = scanner.nextLine(); | ||
if (!line.equals("end")) | ||
order += line + "\n"; | ||
else | ||
break; | ||
} | ||
System.out.print(Instruction.main(order)); | ||
} | ||
} | ||
|
||
/* | ||
You can enter the following commands as an example | ||
test : | ||
ori $t8, $t7, 15 | ||
bnq $a1, $t3, Hossein | ||
ori $t5, $t2, 15 | ||
add $t1, $t2, $a1 | ||
Hossein: ori $l1, $t2, 15 | ||
end | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class R_type { | ||
private static Map <String,String> order = new HashMap<>(); | ||
|
||
private static void addOrder() { // Specify machine code commands (Function) | ||
order.put("add", "001"); | ||
order.put("sub", "010"); | ||
order.put("mul", "011"); | ||
order.put("and", "100"); | ||
order.put("or", "101"); | ||
order.put("nor", "110"); | ||
order.put("nand", "111"); | ||
} | ||
|
||
public static String callOrder(String name) { | ||
addOrder(); | ||
return order.get(name); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class RegisterFile { | ||
private static Map<String,String> register = new HashMap<>(); | ||
|
||
private static void addRegister() { | ||
register.put("$zero", "0000"); | ||
register.put("$at", "0001"); | ||
register.put("$a1", "0010"); | ||
register.put("$a2", "0011"); | ||
register.put("$a3", "0100"); | ||
register.put("$l1", "0101"); | ||
register.put("$l2", "0110"); | ||
register.put("$l3", "0111"); | ||
register.put("$t1", "1000"); | ||
register.put("$t2", "1001"); | ||
register.put("$t3", "1010"); | ||
register.put("$t4", "1011"); | ||
register.put("$t5", "1100"); | ||
register.put("$t6", "1101"); | ||
register.put("$t7", "1110"); | ||
register.put("$t8", "1111"); | ||
} | ||
|
||
public static String callRegister(String name) { | ||
addRegister(); | ||
return register.get(name); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class V_type { | ||
private static Map <String,String> order = new HashMap<>(); | ||
|
||
private static void addOrder() { // Specify machine code commands (Opcode) | ||
order.put("bnq", "111"); | ||
order.put("lw", "001"); | ||
order.put("sw", "010"); | ||
order.put("andi", "100"); | ||
order.put("ori", "101"); | ||
} | ||
|
||
public static String callOrder(String name) { | ||
addOrder(); | ||
return order.get(name); | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.