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))))
}
}
+ 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();
}
}
- 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];
}
}
+ 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);
}
}