1 package net.mograsim.machine.mi;
3 import java.io.BufferedReader;
4 import java.io.FileInputStream;
5 import java.io.FileOutputStream;
6 import java.io.IOException;
7 import java.io.InputStreamReader;
8 import java.io.OutputStreamWriter;
9 import java.util.Objects;
11 import net.mograsim.machine.MachineRegistry;
12 import net.mograsim.machine.MemoryDefinition;
13 import net.mograsim.machine.mi.parameters.MicroInstructionParameter;
14 import net.mograsim.machine.mi.parameters.ParameterClassification;
16 public class MicroInstructionMemoryParser
18 private final static String lineSeparator = System.getProperty("line.separator");
20 public static void parseMemory(final MicroInstructionMemory memory, String inputPath) throws IOException
22 try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(inputPath))))
24 parseMemory(memory, reader);
28 public static MicroInstructionMemory parseMemory(String inputPath) throws IOException
30 try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(inputPath))))
32 return parseMemory(reader);
37 * First line must be the machine name, the rest must be in csv format
39 public static MicroInstructionMemory parseMemory(BufferedReader input)
43 return parseMemory(input.readLine(), input);
47 throw new MicroInstructionMemoryParseException(e);
52 * must be in csv format
54 public static MicroInstructionMemory parseMemory(String machineName, BufferedReader input)
58 MicroInstructionMemoryDefinition def = Objects
59 .requireNonNull(MachineRegistry.getinstalledMachines().get(machineName), "Unknown machine: " + machineName)
60 .getMicroInstructionMemoryDefinition();
61 MicroInstructionMemory memory = new StandardMicroInstructionMemory(def);
62 parseMemory(memory, input);
65 catch (NullPointerException e)
67 throw new MicroInstructionMemoryParseException(e);
72 * must be in csv format
74 public static void parseMemory(final MicroInstructionMemory memory, BufferedReader input)
76 MicroInstructionMemoryDefinition def = memory.getDefinition();
77 MicroInstructionDefinition miDef = def.getMicroInstructionDefinition();
79 long minAddr = def.getMinimalAddress();
80 long maxAddr = def.getMaximalAddress();
86 for (; i <= maxAddr && input.ready() && !"".equals((line = input.readLine())); i++)
88 memory.setCell(i, parse(miDef, line));
96 for (; i <= maxAddr; i++)
98 memory.setCell(i, miDef.createDefaultInstruction());
103 * must be in csv format
105 public static MicroInstruction parse(MicroInstructionDefinition definition, String path)
107 int size = definition.size();
108 String[] strings = path.split(",");
109 if (size != strings.length)
110 throw new MicroInstructionMemoryParseException("String does not match definition! The number of parameters does not match.");
111 MicroInstructionParameter[] params = new MicroInstructionParameter[size];
112 ParameterClassification[] classes = definition.getParameterClassifications();
115 for (int i = 0; i < size; i++)
117 params[i] = classes[i].parse(strings[i]);
119 return new StandardMicroInstruction(params);
123 throw new MicroInstructionMemoryParseException(e.getCause());
127 public static void write(MicroInstructionMemory memory, String outputPath) throws IOException
129 try (OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(outputPath)))
131 write(memory, writer);
135 public static void write(MicroInstructionMemory memory, String machineName, String outputPath) throws IOException
137 try (OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(outputPath)))
139 write(memory, machineName, writer);
143 public static void write(MicroInstructionMemory memory, OutputStreamWriter output) throws IOException
145 MemoryDefinition def = memory.getDefinition();
146 long min = def.getMinimalAddress(), max = def.getMaximalAddress() + 1;
147 for (long i = min; i < max; i++)
149 output.write(toCSV(memory.getCell(i)) + lineSeparator);
153 public static void write(MicroInstructionMemory memory, String machineName, OutputStreamWriter output) throws IOException
155 output.write(machineName + lineSeparator);
156 MemoryDefinition def = memory.getDefinition();
157 long min = def.getMinimalAddress(), max = def.getMaximalAddress() + 1;
158 for (long i = min; i < max; i++)
160 output.write(toCSV(memory.getCell(i)) + lineSeparator);
164 private static String toCSV(MicroInstruction inst)
166 int max = inst.getSize() - 1;
167 StringBuilder sb = new StringBuilder();
168 for (int i = 0; i < max; i++)
170 sb.append(inst.getParameter(i).toString());
173 sb.append(inst.getParameter(max).toString());
174 return sb.toString();