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