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