1 package net.mograsim.machine.mi;
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;
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;
22 public class MicroprogramMemoryParser
24 public static void parse(MicroprogramMemory memory, long startAddress, MicroInstructionDefinition definition, String input) throws IOException
26 try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(input))))
28 parse(memory, startAddress, definition, reader);
32 public static void parse(MicroprogramMemory memory, long startAddress, MicroInstructionDefinition definition,
35 MemoryDefinition def = memory.getDefinition();
36 long minAddress = Long.max(startAddress, def.getMinimalAddress()), maxAddress = def.getMaximalAddress();
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)
48 public static MicroInstruction parse(MicroInstructionDefinition definition, String toParse)
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++)
59 params[i] = classes[i].parse(strings[i]);
61 return new StandardMicroInstruction(params);
64 public static void write(MicroprogramMemory memory, long startAddress, long endAddress, String output) throws IOException
66 try(OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(output)))
68 write(memory, startAddress, endAddress, writer);
72 public static void write(MicroprogramMemory memory, long startAddress, long endAddress, OutputStreamWriter output) throws IOException
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++)
78 output.write(toCSV(memory.getCell(i)) + "\n");
82 private static String toCSV(MicroInstruction inst)
84 int max = inst.getSize() - 1;
85 StringBuilder sb = new StringBuilder();
86 for(int i = 0; i < max; i++)
88 sb.append(inst.getParameter(i).toString());
91 sb.append(inst.getParameter(max).toString());