Initial commit

This commit is contained in:
clerie 2019-01-21 21:37:46 +01:00
commit bed4736ddd
22 changed files with 543 additions and 0 deletions

4
.idea/encodings.xml Normal file
View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding" addBOMForNewFiles="with NO BOM" />
</project>

6
.idea/misc.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_10" default="true" project-jdk-name="10" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

8
.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/registermaschine.iml" filepath="$PROJECT_DIR$/registermaschine.iml" />
</modules>
</component>
</project>

124
.idea/uiDesigner.xml Normal file
View File

@ -0,0 +1,124 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Palette2">
<group name="Swing">
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
</item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item>
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.png" removable="false" auto-create-binding="false" can-attach-label="true">
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
</item>
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
<initial-values>
<property name="text" value="Button" />
</initial-values>
</item>
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="RadioButton" />
</initial-values>
</item>
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="CheckBox" />
</initial-values>
</item>
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
<initial-values>
<property name="text" value="Label" />
</initial-values>
</item>
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
</item>
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
</item>
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
<preferred-size width="-1" height="20" />
</default-constraints>
</item>
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
</item>
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
</item>
</group>
</component>
</project>

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

11
registermaschine.iml Normal file
View File

@ -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>

48
src/Configuration.java Normal file
View File

@ -0,0 +1,48 @@
public class Configuration {
public final static int NUM_REGISTERS = 10;
int ic; // Instruction Counter
int[] registers = new int[NUM_REGISTERS];
public Configuration() {
init();
}
public void init() {
ic = 0;
for(int i = 0; i < registers.length; i++) {
registers[i] = 0;
}
}
public int getIC() {
return ic;
}
public void setIC(int nic) {
ic = nic;
}
public void incIC() {
ic++;
}
public void setRegister(int i, int val) {
registers[i] = val;
}
public int getRegister(int i) {
return registers[i];
}
@Override
public String toString() {
String out = "";
for(int r : registers) {
out += r + ",";
}
return "[" + out + "...";
}
}

26
src/Instruction.java Normal file
View File

@ -0,0 +1,26 @@
public interface Instruction {
void eval(Configuration config);
String toString();
}
/*
*
* ADD
* CADD
*
* SUB
* CSUb
*
* MULT
* CMULT
*
* DIV
* CDIV
*
* GOTO
* IFGOTO
*
* LOAD
* STORE
*
*/

17
src/InstructionAdd.java Normal file
View File

@ -0,0 +1,17 @@
public class InstructionAdd implements Instruction {
private int reg;
public InstructionAdd(int i) {
reg = i;
}
public void eval(Configuration config) {
config.setRegister(0, config.getRegister(0) + config.getRegister(reg));
config.incIC();
}
@Override
public String toString() {
return "ADD " + reg;
}
}

17
src/InstructionCAdd.java Normal file
View File

@ -0,0 +1,17 @@
public class InstructionCAdd implements Instruction {
private int con;
public InstructionCAdd(int i) {
con = i;
}
public void eval(Configuration config) {
config.setRegister(0, config.getRegister(0) + con);
config.incIC();
}
@Override
public String toString() {
return "CADD " + con;
}
}

17
src/InstructionCDiv.java Normal file
View File

@ -0,0 +1,17 @@
public class InstructionCDiv implements Instruction {
private int con;
public InstructionCDiv(int i) {
con = i;
}
public void eval(Configuration config) {
config.setRegister(0, config.getRegister(0) / con);
config.incIC();
}
@Override
public String toString() {
return "CDIV " + con;
}
}

17
src/InstructionCMult.java Normal file
View File

@ -0,0 +1,17 @@
public class InstructionCMult implements Instruction {
private int con;
public InstructionCMult(int i) {
con = i;
}
public void eval(Configuration config) {
config.setRegister(0, config.getRegister(0) * con);
config.incIC();
}
@Override
public String toString() {
return "CMULT " + con;
}
}

17
src/InstructionCSub.java Normal file
View File

@ -0,0 +1,17 @@
public class InstructionCSub implements Instruction {
private int con;
public InstructionCSub(int i) {
con = i;
}
public void eval(Configuration config) {
config.setRegister(0, config.getRegister(0) - con);
config.incIC();
}
@Override
public String toString() {
return "CSUB " + con;
}
}

17
src/InstructionDiv.java Normal file
View File

@ -0,0 +1,17 @@
public class InstructionDiv implements Instruction {
private int reg;
public InstructionDiv(int i) {
reg = i;
}
public void eval(Configuration config) {
config.setRegister(0, config.getRegister(0) / config.getRegister(reg));
config.incIC();
}
@Override
public String toString() {
return "DIV " + reg;
}
}

14
src/InstructionEnd.java Normal file
View File

@ -0,0 +1,14 @@
public class InstructionEnd implements Instruction {
public InstructionEnd() {
}
public void eval(Configuration config) {
}
@Override
public String toString() {
return "END";
}
}

11
src/InstructionGoto.java Normal file
View File

@ -0,0 +1,11 @@
public class InstructionGoto implements Instruction {
private int reg;
public InstructionGoto(int i) {
reg = i;
}
public void eval(Configuration config) {
config.setIC(reg);
}
}

View File

@ -0,0 +1,16 @@
public class InstructionIfGoto implements Instruction {
private int reg;
public InstructionIfGoto(int i) {
reg = i;
}
public void eval(Configuration config) {
if(config.getRegister(0) == 0) {
config.setIC(reg);
}
else {
config.incIC();
}
}
}

17
src/InstructionLoad.java Normal file
View File

@ -0,0 +1,17 @@
public class InstructionLoad implements Instruction {
private int reg;
public InstructionLoad(int i) {
reg = i;
}
public void eval(Configuration config) {
config.setRegister(0, config.getRegister(reg));
config.incIC();
}
@Override
public String toString() {
return "LOAD " + reg;
}
}

17
src/InstructionMult.java Normal file
View File

@ -0,0 +1,17 @@
public class InstructionMult implements Instruction {
private int reg;
public InstructionMult(int i) {
reg = i;
}
public void eval(Configuration config) {
config.setRegister(0, config.getRegister(0) * config.getRegister(reg));
config.incIC();
}
@Override
public String toString() {
return "MULT " + reg;
}
}

17
src/InstructionStore.java Normal file
View File

@ -0,0 +1,17 @@
public class InstructionStore implements Instruction {
private int reg;
public InstructionStore(int i) {
reg = i;
}
public void eval(Configuration config) {
config.setRegister(reg, config.getRegister(0));
config.incIC();
}
@Override
public String toString() {
return "STORE " + reg;
}
}

17
src/InstructionSub.java Normal file
View File

@ -0,0 +1,17 @@
public class InstructionSub implements Instruction {
private int reg;
public InstructionSub(int i) {
reg = i;
}
public void eval(Configuration config) {
config.setRegister(0, config.getRegister(0) - config.getRegister(reg));
config.incIC();
}
@Override
public String toString() {
return "SUB " + reg;
}
}

99
src/Machine.java Normal file
View File

@ -0,0 +1,99 @@
public class Machine {
private Configuration config = null;
private Instruction[] program = null;
public Machine() {
config = new Configuration();
}
public void setProgram(Instruction[] prog) {
program = prog;
}
public void setProgram(String prog) {
String[] lines = prog.split("\\n");
Instruction[] instructions = new Instruction[lines.length];
for(int i = 0; i < lines.length; i++) {
String[] words = lines[i].split(" ");
switch (words[0]) {
case "ADD":
instructions[i] = new InstructionAdd(Integer.parseInt(words[1]));
break;
case "CADD":
instructions[i] = new InstructionCAdd(Integer.parseInt(words[1]));
break;
case "SUB":
instructions[i] = new InstructionSub(Integer.parseInt(words[1]));
break;
case "CSUB":
instructions[i] = new InstructionCSub(Integer.parseInt(words[1]));
break;
case "MULT":
instructions[i] = new InstructionMult(Integer.parseInt(words[1]));
break;
case "CMULT":
instructions[i] = new InstructionCMult(Integer.parseInt(words[1]));
break;
case "DIV":
instructions[i] = new InstructionDiv(Integer.parseInt(words[1]));
break;
case "CDIV":
instructions[i] = new InstructionCDiv(Integer.parseInt(words[1]));
break;
case "LOAD":
instructions[i] = new InstructionLoad(Integer.parseInt(words[1]));
break;
case "STORE":
instructions[i] = new InstructionStore(Integer.parseInt(words[1]));
break;
case "GOTO":
instructions[i] = new InstructionGoto(Integer.parseInt(words[1]));
break;
case "IFGOTO":
instructions[i] = new InstructionIfGoto(Integer.parseInt(words[1]));
break;
case "END":
instructions[i] = new InstructionEnd();
break;
}
}
program = instructions;
}
public void run() {
int ic;
while(!program[config.getIC()].toString().equals("END")) {
ic = config.getIC();
program[ic].eval(config);
System.out.println(program[ic].toString() + " " + config.toString());
}
}
public Configuration getConfiguration() {
return config;
}
public static void main(String[] args) {
Instruction[] prog = {
new InstructionLoad(1),
new InstructionCAdd(32),
new InstructionStore(1),
new InstructionEnd()
};
String str = "LOAD 1\nCADD 32\nEND";
System.out.println(str);
Machine machine = new Machine();
machine.setProgram(str);
machine.getConfiguration().setRegister(1, 32);
machine.run();
System.out.println(machine.getConfiguration().toString());
}
}