Fleshed out Memory and Microprogramming interfaces
[Mograsim.git] / net.mograsim.machine / src / net / mograsim / machine / mi / MicroprogramMemoryParser.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.math.BigInteger;
10
11 import net.mograsim.logic.core.types.BitVector;
12 import net.mograsim.machine.MemoryDefinition;
13 import net.mograsim.machine.mi.parameters.BooleanClassification;
14 import net.mograsim.machine.mi.parameters.BooleanImmediate;
15 import net.mograsim.machine.mi.parameters.IntegerClassification;
16 import net.mograsim.machine.mi.parameters.IntegerImmediate;
17 import net.mograsim.machine.mi.parameters.MicroInstructionParameter;
18 import net.mograsim.machine.mi.parameters.MnemonicFamily;
19 import net.mograsim.machine.mi.parameters.MnemonicFamily.MnemonicPair;
20 import net.mograsim.machine.mi.parameters.ParameterClassification;
21
22 public class MicroprogramMemoryParser
23 {
24         public static void parse(MicroprogramMemory memory, long startAddress, MicroInstructionDefinition definition, String input) throws IOException
25         {
26                 try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(input))))
27                 {
28                         parse(memory, startAddress, definition, reader);
29                 }
30         }
31
32         public static void parse(MicroprogramMemory memory, long startAddress, MicroInstructionDefinition definition,
33                         BufferedReader input)
34         {
35                 MemoryDefinition def = memory.getDefinition();
36                 long minAddress = Long.max(startAddress, def.getMinimalAddress()), maxAddress = def.getMaximalAddress();
37                 try
38                 {
39                         String line;
40                         for (long i = minAddress; i < maxAddress && input.ready() && !"".equals((line = input.readLine())); i++)
41                                 memory.setCell(i, parse(definition, line));
42                 } catch (IOException e)
43                 {
44                         e.printStackTrace();
45                 }
46         }
47
48         public static MicroInstruction parse(MicroInstructionDefinition definition, String toParse)
49         {
50                 int size = definition.size();
51                 String[] strings = toParse.split(",");
52                 if (size != strings.length)
53                         throw new IllegalArgumentException(
54                                         "String does not match definition! The number of parameters does not match.");
55                 MicroInstructionParameter[] params = new MicroInstructionParameter[size];
56                 ParameterClassification[] classes = definition.getParameterClassifications();
57                 for (int i = 0; i < size; i++)
58                 {
59                         params[i] = classes[i].parse(strings[i]);
60                 }
61                 return new StandardMicroInstruction(params);
62         }
63         
64         public static void write(MicroprogramMemory memory, long startAddress, long endAddress, String output) throws IOException
65         {
66                 try(OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(output)))
67                 {
68                         write(memory, startAddress, endAddress, writer);
69                 }
70         }
71         
72         public static void write(MicroprogramMemory memory, long startAddress, long endAddress, OutputStreamWriter output) throws IOException
73         {
74                 MemoryDefinition def = memory.getDefinition();
75                 long min = Long.max(def.getMinimalAddress(), startAddress), max = Long.min(def.getMaximalAddress(), endAddress) + 1;
76                 for(long i = min; i < max; i++)
77                 {
78                         output.write(toCSV(memory.getCell(i)) + "\n");
79                 }
80         }
81         
82         private static String toCSV(MicroInstruction inst)
83         {
84                 int max = inst.getSize() - 1;
85                 StringBuilder sb = new StringBuilder();
86                 for(int i = 0; i < max; i++)
87                 {
88                         sb.append(inst.getParameter(i).toString());
89                         sb.append(",");
90                 }
91                 sb.append(inst.getParameter(max).toString());
92                 return sb.toString();
93         }
94 }