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=5babdd65b463ea8f33a950c2a8d4732417d2df18;hp=d049e37776e0ec39466d81b200727110552eb564;hpb=7d05144c25daa53e60fc9ed9fd503546a86567f8;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 d049e377..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(); @@ -35,8 +85,7 @@ public class MicroInstructionMemoryParser { for (; i <= maxAddr && input.ready() && !"".equals((line = input.readLine())); i++) { - long iFinal = i; - memory.setCell(i, parse(() -> memory.notifyObservers(iFinal), miDef, line)); + memory.setCell(i, parse(miDef, line)); } } catch (IOException e) @@ -46,15 +95,17 @@ public class MicroInstructionMemoryParser for (; i <= maxAddr; i++) { - long iFinal = i; - memory.setCell(i, miDef.createDefaultInstruction(() -> memory.notifyObservers(iFinal))); + memory.setCell(i, miDef.createDefaultInstruction()); } } - public static MicroInstruction parse(Runnable updateCallback, 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]; @@ -65,7 +116,7 @@ public class MicroInstructionMemoryParser { params[i] = classes[i].parse(strings[i]); } - return new StandardMicroInstruction(updateCallback, params); + return new StandardMicroInstruction(params); } catch (Exception e) { @@ -81,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); } }