Finished MPROM support. Fixes #10
[Mograsim.git] / plugins / net.mograsim.machine / src / net / mograsim / machine / standard / memory / BitVectorBasedMemoryParser.java
1 package net.mograsim.machine.standard.memory;
2
3 import java.io.BufferedReader;
4 import java.io.ByteArrayInputStream;
5 import java.io.FileInputStream;
6 import java.io.IOException;
7 import java.io.InputStream;
8 import java.io.InputStreamReader;
9 import java.nio.charset.StandardCharsets;
10
11 import net.mograsim.logic.core.types.BitVector;
12 import net.mograsim.machine.BitVectorMemory;
13 import net.mograsim.machine.MainMemory;
14 import net.mograsim.machine.MainMemoryDefinition;
15 import net.mograsim.machine.MemoryDefinition;
16 import net.mograsim.machine.StandardMainMemory;
17 import net.mograsim.machine.mi.MPROM;
18 import net.mograsim.machine.mi.MPROMDefinition;
19 import net.mograsim.machine.mi.StandardMPROM;
20
21 public class BitVectorBasedMemoryParser
22 {
23         private final static String lineSeparator = System.getProperty("line.separator");
24
25         public static void parseMemory(final BitVectorMemory memory, String inputPath) throws IOException
26         {
27                 try (InputStream input = new FileInputStream(inputPath))
28                 {
29                         parseMemory(memory, input);
30                 }
31         }
32
33         /**
34          * @param input The input to parse must be in csv format; The stream is closed after being consumed.
35          * 
36          * @throws IOException
37          */
38         public static MainMemory parseMainMemory(MainMemoryDefinition memDef, InputStream input) throws IOException
39         {
40                 try
41                 {
42                         MainMemory memory = new StandardMainMemory(memDef);
43                         parseMemory(memory, input);
44                         return memory;
45                 }
46                 catch (NullPointerException e)
47                 {
48                         throw new BitVectorMemoryParseException(e);
49                 }
50         }
51
52         /**
53          * @param input The input to parse must be in csv format; The stream is closed after being consumed.
54          * 
55          * @throws IOException
56          */
57         public static MPROM parseMPROM(MPROMDefinition memDef, InputStream input) throws IOException
58         {
59                 try
60                 {
61                         MPROM memory = new StandardMPROM(memDef);
62                         parseMemory(memory, input);
63                         return memory;
64                 }
65                 catch (NullPointerException e)
66                 {
67                         throw new BitVectorMemoryParseException(e);
68                 }
69         }
70
71         /**
72          *
73          * @param input The input to parse must be in csv format; The stream is closed after being consumed.
74          * 
75          * @throws IOException
76          */
77         public static void parseMemory(final BitVectorMemory memory, InputStream input) throws IOException
78         {
79                 try (BufferedReader reader = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8)))
80                 {
81                         MemoryDefinition def = memory.getDefinition();
82
83                         long minAddr = def.getMinimalAddress();
84                         long maxAddr = def.getMaximalAddress();
85
86                         String line;
87                         long i = minAddr;
88                         try
89                         {
90                                 for (; i <= maxAddr && reader.ready() && !"".equals((line = reader.readLine())); i++)
91                                 {
92                                         memory.setCell(i, BitVector.parseBitstring(line));
93                                 }
94                         }
95                         catch (IOException e)
96                         {
97                                 e.printStackTrace();
98                         }
99                 }
100         }
101
102         public static InputStream write(BitVectorMemory memory)
103         {
104                 return new InputStream()
105                 {
106                         long instIndex = memory.getDefinition().getMinimalAddress(), maxAddress = memory.getDefinition().getMaximalAddress();
107                         InputStream instStream = new ByteArrayInputStream(new byte[0]);
108
109                         @Override
110                         public int read() throws IOException
111                         {
112                                 int val = instStream.read();
113                                 if (val == -1 && instIndex <= maxAddress)
114                                 {
115                                         instStream = new ByteArrayInputStream(
116                                                         (memory.getCell(instIndex++).toBitstring() + lineSeparator).getBytes(StandardCharsets.UTF_8));
117                                         val = instStream.read();
118                                 }
119                                 return val;
120                         }
121                 };
122         }
123 }