X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=plugins%2Fnet.mograsim.machine%2Fsrc%2Fnet%2Fmograsim%2Fmachine%2Fmi%2FMicroInstructionMemoryParser.java;h=3c13f67b8e948b46012e8ae1341152d93c2c1e0c;hb=6c32f347005a369359a68c09b7e9ac2f4a1eab0f;hp=1aa518dcd77d133ee6b61f9248d0db34ec092726;hpb=9e8e8ae5414f52ec87f9247b10a525f41768c7dc;p=Mograsim.git diff --git a/plugins/net.mograsim.machine/src/net/mograsim/machine/mi/MicroInstructionMemoryParser.java b/plugins/net.mograsim.machine/src/net/mograsim/machine/mi/MicroInstructionMemoryParser.java index 1aa518dc..3c13f67b 100644 --- a/plugins/net.mograsim.machine/src/net/mograsim/machine/mi/MicroInstructionMemoryParser.java +++ b/plugins/net.mograsim.machine/src/net/mograsim/machine/mi/MicroInstructionMemoryParser.java @@ -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); } }