MicroInstructionMemory editor can now open and save to files
[Mograsim.git] / plugins / net.mograsim.machine / src / net / mograsim / machine / mi / MicroInstructionMemoryParser.java
index 1aa518d..3c13f67 100644 (file)
@@ -6,13 +6,17 @@ import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.io.OutputStreamWriter;
+import java.util.Objects;
 
+import net.mograsim.machine.MachineRegistry;
 import net.mograsim.machine.MemoryDefinition;
 import net.mograsim.machine.mi.parameters.MicroInstructionParameter;
 import net.mograsim.machine.mi.parameters.ParameterClassification;
 
 public class MicroInstructionMemoryParser
 {
+       private final static String lineSeparator = System.getProperty("line.separator");
+
        public static void parseMemory(final MicroInstructionMemory memory, String inputPath) throws IOException
        {
                try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(inputPath))))
@@ -21,6 +25,52 @@ public class MicroInstructionMemoryParser
                }
        }
 
+       public static MicroInstructionMemory parseMemory(String inputPath) throws IOException
+       {
+               try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(inputPath))))
+               {
+                       return parseMemory(reader);
+               }
+       }
+
+       /**
+        * First line must be the machine name, the rest must be in csv format
+        */
+       public static MicroInstructionMemory parseMemory(BufferedReader input)
+       {
+               try
+               {
+                       return parseMemory(input.readLine(), input);
+               }
+               catch (IOException e)
+               {
+                       throw new MicroInstructionMemoryParseException(e);
+               }
+       }
+
+       /**
+        * must be in csv format
+        */
+       public static MicroInstructionMemory parseMemory(String machineName, BufferedReader input)
+       {
+               try
+               {
+                       MicroInstructionMemoryDefinition def = Objects
+                                       .requireNonNull(MachineRegistry.getinstalledMachines().get(machineName), "Unknown machine: " + machineName)
+                                       .getMicroInstructionMemoryDefinition();
+                       MicroInstructionMemory memory = new StandardMicroInstructionMemory(def);
+                       parseMemory(memory, input);
+                       return memory;
+               }
+               catch (NullPointerException e)
+               {
+                       throw new MicroInstructionMemoryParseException(e);
+               }
+       }
+
+       /**
+        * must be in csv format
+        */
        public static void parseMemory(final MicroInstructionMemory memory, BufferedReader input)
        {
                MicroInstructionMemoryDefinition def = memory.getDefinition();
@@ -49,10 +99,13 @@ public class MicroInstructionMemoryParser
                }
        }
 
-       public static MicroInstruction parse(MicroInstructionDefinition definition, String toParse)
+       /**
+        * must be in csv format
+        */
+       public static MicroInstruction parse(MicroInstructionDefinition definition, String path)
        {
                int size = definition.size();
-               String[] strings = toParse.split(",");
+               String[] strings = path.split(",");
                if (size != strings.length)
                        throw new MicroInstructionMemoryParseException("String does not match definition! The number of parameters does not match.");
                MicroInstructionParameter[] params = new MicroInstructionParameter[size];
@@ -79,13 +132,32 @@ public class MicroInstructionMemoryParser
                }
        }
 
+       public static void write(MicroInstructionMemory memory, String machineName, String outputPath) throws IOException
+       {
+               try (OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(outputPath)))
+               {
+                       write(memory, machineName, writer);
+               }
+       }
+
        public static void write(MicroInstructionMemory memory, OutputStreamWriter output) throws IOException
        {
                MemoryDefinition def = memory.getDefinition();
                long min = def.getMinimalAddress(), max = def.getMaximalAddress() + 1;
                for (long i = min; i < max; i++)
                {
-                       output.write(toCSV(memory.getCell(i)) + "\n");
+                       output.write(toCSV(memory.getCell(i)) + lineSeparator);
+               }
+       }
+
+       public static void write(MicroInstructionMemory memory, String machineName, OutputStreamWriter output) throws IOException
+       {
+               output.write(machineName + lineSeparator);
+               MemoryDefinition def = memory.getDefinition();
+               long min = def.getMinimalAddress(), max = def.getMaximalAddress() + 1;
+               for (long i = min; i < max; i++)
+               {
+                       output.write(toCSV(memory.getCell(i)) + lineSeparator);
                }
        }