1 package net.mograsim.machine.mi;
3 import java.io.BufferedReader;
4 import java.io.ByteArrayInputStream;
5 import java.io.FileInputStream;
6 import java.io.IOException;
7 import java.io.InputStream;
8 import java.io.InputStreamReader;
9 import java.nio.charset.StandardCharsets;
11 import net.mograsim.machine.mi.parameters.MicroInstructionParameter;
12 import net.mograsim.machine.mi.parameters.ParameterClassification;
14 public class MicroInstructionMemoryParser
16 private final static String lineSeparator = System.getProperty("line.separator");
18 public static void parseMemory(final MicroInstructionMemory memory, String inputPath) throws IOException
20 try (InputStream input = new FileInputStream(inputPath))
22 parseMemory(memory, input);
27 * @param input The input to parse must be in csv format; The stream is closed after being consumed.
31 public static MicroInstructionMemory parseMemory(MicroInstructionMemoryDefinition memDef, InputStream input) throws IOException
35 MicroInstructionMemory memory = new StandardMicroInstructionMemory(memDef);
36 parseMemory(memory, input);
39 catch (NullPointerException e)
41 throw new MicroInstructionMemoryParseException(e);
47 * @param input The input to parse must be in csv format; The stream is closed after being consumed.
51 public static void parseMemory(final MicroInstructionMemory memory, InputStream input) throws IOException
53 try (BufferedReader reader = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8)))
55 MicroInstructionMemoryDefinition def = memory.getDefinition();
56 MicroInstructionDefinition miDef = def.getMicroInstructionDefinition();
58 long minAddr = def.getMinimalAddress();
59 long maxAddr = def.getMaximalAddress();
65 for (; i <= maxAddr && reader.ready() && !"".equals((line = reader.readLine())); i++)
67 memory.setCell(i, parse(miDef, line));
75 for (; i <= maxAddr; i++)
77 memory.setCell(i, miDef.createDefaultInstruction());
83 * must be in csv format
85 public static MicroInstruction parse(MicroInstructionDefinition definition, String input)
87 int size = definition.size();
88 String[] strings = input.split(",");
89 if (size != strings.length)
90 throw new MicroInstructionMemoryParseException("String does not match definition! The number of parameters does not match.");
91 MicroInstructionParameter[] params = new MicroInstructionParameter[size];
92 ParameterClassification[] classes = definition.getParameterClassifications();
95 for (int i = 0; i < size; i++)
97 params[i] = classes[i].parse(strings[i]);
99 return new StandardMicroInstruction(params);
103 throw new MicroInstructionMemoryParseException(e);
107 private static String toCSV(MicroInstruction inst)
109 int max = inst.getSize() - 1;
110 StringBuilder sb = new StringBuilder();
111 for (int i = 0; i < max; i++)
113 sb.append(inst.getParameter(i).toString());
116 sb.append(inst.getParameter(max).toString());
117 return sb.toString();
120 public static InputStream write(MicroInstructionMemory memory)
122 return new InputStream()
124 long instIndex = memory.getDefinition().getMinimalAddress(), maxAddress = memory.getDefinition().getMaximalAddress();
125 InputStream instStream = new ByteArrayInputStream(new byte[0]);
128 public int read() throws IOException
130 int val = instStream.read();
131 if (val == -1 && instIndex <= maxAddress)
133 instStream = new ByteArrayInputStream(
134 (toCSV(memory.getCell(instIndex++)) + lineSeparator).getBytes(StandardCharsets.UTF_8));
135 val = instStream.read();