Restructured Mograsim project nature and introduced project context
[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 import java.util.Objects;
10
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;
15
16 public class MicroInstructionMemoryParser
17 {
18         private final static String lineSeparator = System.getProperty("line.separator");
19
20         public static void parseMemory(final MicroInstructionMemory memory, String inputPath) throws IOException
21         {
22                 try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(inputPath))))
23                 {
24                         parseMemory(memory, reader);
25                 }
26         }
27
28         public static MicroInstructionMemory parseMemory(String inputPath) throws IOException
29         {
30                 try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(inputPath))))
31                 {
32                         return parseMemory(reader);
33                 }
34         }
35
36         /**
37          * First line must be the machine name, the rest must be in csv format
38          */
39         public static MicroInstructionMemory parseMemory(BufferedReader input)
40         {
41                 try
42                 {
43                         return parseMemory(input.readLine(), input);
44                 }
45                 catch (IOException e)
46                 {
47                         throw new MicroInstructionMemoryParseException(e);
48                 }
49         }
50
51         /**
52          * must be in csv format
53          */
54         public static MicroInstructionMemory parseMemory(String machineName, BufferedReader input)
55         {
56                 try
57                 {
58                         MicroInstructionMemoryDefinition def = Objects
59                                         .requireNonNull(MachineRegistry.getMachine(machineName), "Unknown machine: " + machineName)
60                                         .getMicroInstructionMemoryDefinition();
61                         MicroInstructionMemory memory = new StandardMicroInstructionMemory(def);
62                         parseMemory(memory, input);
63                         return memory;
64                 }
65                 catch (NullPointerException e)
66                 {
67                         throw new MicroInstructionMemoryParseException(e);
68                 }
69         }
70
71         /**
72          * must be in csv format
73          */
74         public static void parseMemory(final MicroInstructionMemory memory, BufferedReader input)
75         {
76                 MicroInstructionMemoryDefinition def = memory.getDefinition();
77                 MicroInstructionDefinition miDef = def.getMicroInstructionDefinition();
78
79                 long minAddr = def.getMinimalAddress();
80                 long maxAddr = def.getMaximalAddress();
81
82                 String line;
83                 long i = minAddr;
84                 try
85                 {
86                         for (; i <= maxAddr && input.ready() && !"".equals((line = input.readLine())); i++)
87                         {
88                                 memory.setCell(i, parse(miDef, line));
89                         }
90                 }
91                 catch (IOException e)
92                 {
93                         e.printStackTrace();
94                 }
95
96                 for (; i <= maxAddr; i++)
97                 {
98                         memory.setCell(i, miDef.createDefaultInstruction());
99                 }
100         }
101
102         /**
103          * must be in csv format
104          */
105         public static MicroInstruction parse(MicroInstructionDefinition definition, String path)
106         {
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();
113                 try
114                 {
115                         for (int i = 0; i < size; i++)
116                         {
117                                 params[i] = classes[i].parse(strings[i]);
118                         }
119                         return new StandardMicroInstruction(params);
120                 }
121                 catch (Exception e)
122                 {
123                         throw new MicroInstructionMemoryParseException(e.getCause());
124                 }
125         }
126
127         public static void write(MicroInstructionMemory memory, String outputPath) throws IOException
128         {
129                 try (OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(outputPath)))
130                 {
131                         write(memory, writer);
132                 }
133         }
134
135         public static void write(MicroInstructionMemory memory, String machineName, String outputPath) throws IOException
136         {
137                 try (OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(outputPath)))
138                 {
139                         write(memory, machineName, writer);
140                 }
141         }
142
143         public static void write(MicroInstructionMemory memory, OutputStreamWriter output) throws IOException
144         {
145                 MemoryDefinition def = memory.getDefinition();
146                 long min = def.getMinimalAddress(), max = def.getMaximalAddress() + 1;
147                 for (long i = min; i < max; i++)
148                 {
149                         output.write(toCSV(memory.getCell(i)) + lineSeparator);
150                 }
151         }
152
153         public static void write(MicroInstructionMemory memory, String machineName, OutputStreamWriter output) throws IOException
154         {
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++)
159                 {
160                         output.write(toCSV(memory.getCell(i)) + lineSeparator);
161                 }
162         }
163
164         private static String toCSV(MicroInstruction inst)
165         {
166                 int max = inst.getSize() - 1;
167                 StringBuilder sb = new StringBuilder();
168                 for (int i = 0; i < max; i++)
169                 {
170                         sb.append(inst.getParameter(i).toString());
171                         sb.append(",");
172                 }
173                 sb.append(inst.getParameter(max).toString());
174                 return sb.toString();
175         }
176 }