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;
10 import net.mograsim.machine.mi.parameters.MicroInstructionParameter;
11 import net.mograsim.machine.mi.parameters.ParameterClassification;
13 public class MicroInstructionMemoryParser
15 private final static String lineSeparator = System.getProperty("line.separator");
17 public static void parseMemory(final MicroInstructionMemory memory, String inputPath) throws IOException
19 try (InputStream input = new FileInputStream(inputPath))
21 parseMemory(memory, input);
26 * @param input The input to parse must be in csv format; The stream is closed after being consumed.
30 public static MicroInstructionMemory parseMemory(MicroInstructionMemoryDefinition memDef, InputStream input) throws IOException
34 MicroInstructionMemory memory = new StandardMicroInstructionMemory(memDef);
35 parseMemory(memory, input);
38 catch (NullPointerException e)
40 throw new MicroInstructionMemoryParseException(e);
46 * @param input The input to parse must be in csv format; The stream is closed after being consumed.
50 public static void parseMemory(final MicroInstructionMemory memory, InputStream input) throws IOException
52 try (BufferedReader reader = new BufferedReader(new InputStreamReader(input)))
54 MicroInstructionMemoryDefinition def = memory.getDefinition();
55 MicroInstructionDefinition miDef = def.getMicroInstructionDefinition();
57 long minAddr = def.getMinimalAddress();
58 long maxAddr = def.getMaximalAddress();
64 for (; i <= maxAddr && reader.ready() && !"".equals((line = reader.readLine())); i++)
66 memory.setCell(i, parse(miDef, line));
74 for (; i <= maxAddr; i++)
76 memory.setCell(i, miDef.createDefaultInstruction());
82 * must be in csv format
84 public static MicroInstruction parse(MicroInstructionDefinition definition, String input)
86 int size = definition.size();
87 String[] strings = input.split(",");
88 if (size != strings.length)
89 throw new MicroInstructionMemoryParseException("String does not match definition! The number of parameters does not match.");
90 MicroInstructionParameter[] params = new MicroInstructionParameter[size];
91 ParameterClassification[] classes = definition.getParameterClassifications();
94 for (int i = 0; i < size; i++)
96 params[i] = classes[i].parse(strings[i]);
98 return new StandardMicroInstruction(params);
102 throw new MicroInstructionMemoryParseException(e.getCause());
106 private static String toCSV(MicroInstruction inst)
108 int max = inst.getSize() - 1;
109 StringBuilder sb = new StringBuilder();
110 for (int i = 0; i < max; i++)
112 sb.append(inst.getParameter(i).toString());
115 sb.append(inst.getParameter(max).toString());
116 return sb.toString();
119 public static InputStream write(MicroInstructionMemory memory)
121 return new InputStream()
123 long instIndex = memory.getDefinition().getMinimalAddress(), maxAddress = memory.getDefinition().getMaximalAddress();
124 InputStream instStream = new ByteArrayInputStream(new byte[0]);
127 public int read() throws IOException
129 int val = instStream.read();
130 if (val == -1 && instIndex <= maxAddress)
132 instStream = new ByteArrayInputStream((toCSV(memory.getCell(instIndex++)) + lineSeparator).getBytes());
133 val = instStream.read();