Some work on improving BitVector<->String conversions
[Mograsim.git] / plugins / net.mograsim.machine / src / net / mograsim / machine / standard / memory / MainMemoryParser.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
10 import net.mograsim.logic.core.types.BitVector;
11 import net.mograsim.machine.MainMemory;
12 import net.mograsim.machine.MainMemoryDefinition;
13
14 public class MainMemoryParser
15 {
16         private final static String lineSeparator = System.getProperty("line.separator");
17
18         public static void parseMemory(final MainMemory memory, String inputPath) throws IOException
19         {
20                 try (InputStream input = new FileInputStream(inputPath))
21                 {
22                         parseMemory(memory, input);
23                 }
24         }
25
26         /**
27          * @param input The input to parse must be in csv format; The stream is closed after being consumed.
28          * 
29          * @throws IOException
30          */
31         public static MainMemory parseMemory(MainMemoryDefinition memDef, InputStream input) throws IOException
32         {
33                 try
34                 {
35                         MainMemory memory = new WordAddressableMemory(memDef);
36                         parseMemory(memory, input);
37                         return memory;
38                 }
39                 catch (NullPointerException e)
40                 {
41                         throw new MainMemoryParseException(e);
42                 }
43         }
44
45         /**
46          *
47          * @param input The input to parse must be in csv format; The stream is closed after being consumed.
48          * 
49          * @throws IOException
50          */
51         public static void parseMemory(final MainMemory memory, InputStream input) throws IOException
52         {
53                 try (BufferedReader reader = new BufferedReader(new InputStreamReader(input)))
54                 {
55                         MainMemoryDefinition def = memory.getDefinition();
56
57                         long minAddr = def.getMinimalAddress();
58                         long maxAddr = def.getMaximalAddress();
59
60                         String line;
61                         long i = minAddr;
62                         try
63                         {
64                                 for (; i <= maxAddr && reader.ready() && !"".equals((line = reader.readLine())); i++)
65                                 {
66                                         memory.setCell(i, BitVector.parseBitstring(line));
67                                 }
68                         }
69                         catch (IOException e)
70                         {
71                                 e.printStackTrace();
72                         }
73                 }
74         }
75
76         public static InputStream write(MainMemory memory)
77         {
78                 return new InputStream()
79                 {
80                         long instIndex = memory.getDefinition().getMinimalAddress(), maxAddress = memory.getDefinition().getMaximalAddress();
81                         InputStream instStream = new ByteArrayInputStream(new byte[0]);
82
83                         @Override
84                         public int read() throws IOException
85                         {
86                                 int val = instStream.read();
87                                 if (val == -1 && instIndex <= maxAddress)
88                                 {
89                                         instStream = new ByteArrayInputStream((memory.getCell(instIndex++).toBitstring() + lineSeparator).getBytes());
90                                         val = instStream.read();
91                                 }
92                                 return val;
93                         }
94                 };
95         }
96 }