Merge branch 'development' of
[Mograsim.git] / 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                                 memory.setCell(i, parse(miDef, line));
38                 } catch (IOException e)
39                 {
40                         e.printStackTrace();
41                 }
42                 
43                 for(; i <= maxAddr; i++)
44                         memory.setCell(i, miDef.createDefaultInstruction());
45         }
46         
47         public static MicroInstruction parse(MicroInstructionDefinition definition, String toParse)
48         {
49                 int size = definition.size();
50                 String[] strings = toParse.split(",");
51                 if (size != strings.length)
52                         throw new MicroInstructionMemoryParseException(
53                                         "String does not match definition! The number of parameters does not match.");
54                 MicroInstructionParameter[] params = new MicroInstructionParameter[size];
55                 ParameterClassification[] classes = definition.getParameterClassifications();
56                 try
57                 {
58                         for (int i = 0; i < size; i++)
59                         {
60                                 params[i] = classes[i].parse(strings[i]);
61                         }
62                         return new StandardMicroInstruction(params);
63                 } catch (Exception e)
64                 {
65                         throw new MicroInstructionMemoryParseException(e.getCause());
66                 }
67         }
68
69         public static void write(MicroInstructionMemory memory, String outputPath) throws IOException
70         {
71                 try (OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(outputPath)))
72                 {
73                         write(memory, writer);
74                 }
75         }
76
77         public static void write(MicroInstructionMemory memory, OutputStreamWriter output) throws IOException
78         {
79                 MemoryDefinition def = memory.getDefinition();
80                 long min = def.getMinimalAddress(), max = def.getMaximalAddress() + 1;
81                 for (long i = min; i < max; i++)
82                 {
83                         output.write(toCSV(memory.getCell(i)) + "\n");
84                 }
85         }
86
87         private static String toCSV(MicroInstruction inst)
88         {
89                 int max = inst.getSize() - 1;
90                 StringBuilder sb = new StringBuilder();
91                 for (int i = 0; i < max; i++)
92                 {
93                         sb.append(inst.getParameter(i).toString());
94                         sb.append(",");
95                 }
96                 sb.append(inst.getParameter(max).toString());
97                 return sb.toString();
98         }
99 }