Made MicroInstructions immutable
[Mograsim.git] / plugins / net.mograsim.machine / src / net / mograsim / machine / mi / MicroInstructionMemoryParser.java
1 package net.mograsim.machine.mi;
2
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
10 import net.mograsim.machine.MemoryDefinition;
11 import net.mograsim.machine.mi.parameters.MicroInstructionParameter;
12 import net.mograsim.machine.mi.parameters.ParameterClassification;
13
14 public class MicroInstructionMemoryParser
15 {
16         public static void parseMemory(final MicroInstructionMemory memory, String inputPath) throws IOException
17         {
18                 try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(inputPath))))
19                 {
20                         parseMemory(memory, reader);
21                 }
22         }
23
24         public static void parseMemory(final MicroInstructionMemory memory, BufferedReader input)
25         {
26                 MicroInstructionMemoryDefinition def = memory.getDefinition();
27                 MicroInstructionDefinition miDef = def.getMicroInstructionDefinition();
28
29                 long minAddr = def.getMinimalAddress();
30                 long maxAddr = def.getMaximalAddress();
31
32                 String line;
33                 long i = minAddr;
34                 try
35                 {
36                         for (; i <= maxAddr && input.ready() && !"".equals((line = input.readLine())); i++)
37                         {
38                                 memory.setCell(i, parse(miDef, line));
39                         }
40                 }
41                 catch (IOException e)
42                 {
43                         e.printStackTrace();
44                 }
45
46                 for (; i <= maxAddr; i++)
47                 {
48                         memory.setCell(i, miDef.createDefaultInstruction());
49                 }
50         }
51
52         public static MicroInstruction parse(MicroInstructionDefinition definition, String toParse)
53         {
54                 int size = definition.size();
55                 String[] strings = toParse.split(",");
56                 if (size != strings.length)
57                         throw new MicroInstructionMemoryParseException("String does not match definition! The number of parameters does not match.");
58                 MicroInstructionParameter[] params = new MicroInstructionParameter[size];
59                 ParameterClassification[] classes = definition.getParameterClassifications();
60                 try
61                 {
62                         for (int i = 0; i < size; i++)
63                         {
64                                 params[i] = classes[i].parse(strings[i]);
65                         }
66                         return new StandardMicroInstruction(params);
67                 }
68                 catch (Exception e)
69                 {
70                         throw new MicroInstructionMemoryParseException(e.getCause());
71                 }
72         }
73
74         public static void write(MicroInstructionMemory memory, String outputPath) throws IOException
75         {
76                 try (OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(outputPath)))
77                 {
78                         write(memory, writer);
79                 }
80         }
81
82         public static void write(MicroInstructionMemory memory, OutputStreamWriter output) throws IOException
83         {
84                 MemoryDefinition def = memory.getDefinition();
85                 long min = def.getMinimalAddress(), max = def.getMaximalAddress() + 1;
86                 for (long i = min; i < max; i++)
87                 {
88                         output.write(toCSV(memory.getCell(i)) + "\n");
89                 }
90         }
91
92         private static String toCSV(MicroInstruction inst)
93         {
94                 int max = inst.getSize() - 1;
95                 StringBuilder sb = new StringBuilder();
96                 for (int i = 0; i < max; i++)
97                 {
98                         sb.append(inst.getParameter(i).toString());
99                         sb.append(",");
100                 }
101                 sb.append(inst.getParameter(max).toString());
102                 return sb.toString();
103         }
104 }