From 62135d025507bee58eb20bef4bb4251b8f2a697d Mon Sep 17 00:00:00 2001 From: Daniel Kirschten Date: Mon, 30 Sep 2019 14:06:54 +0200 Subject: [PATCH] Created MainMemoryParser --- .../memory/MainMemoryParseException.java | 28 ++++++ .../standard/memory/MainMemoryParser.java | 96 +++++++++++++++++++ 2 files changed, 124 insertions(+) create mode 100644 plugins/net.mograsim.machine/src/net/mograsim/machine/standard/memory/MainMemoryParseException.java create mode 100644 plugins/net.mograsim.machine/src/net/mograsim/machine/standard/memory/MainMemoryParser.java diff --git a/plugins/net.mograsim.machine/src/net/mograsim/machine/standard/memory/MainMemoryParseException.java b/plugins/net.mograsim.machine/src/net/mograsim/machine/standard/memory/MainMemoryParseException.java new file mode 100644 index 00000000..5c6297e2 --- /dev/null +++ b/plugins/net.mograsim.machine/src/net/mograsim/machine/standard/memory/MainMemoryParseException.java @@ -0,0 +1,28 @@ +package net.mograsim.machine.standard.memory; + +import net.mograsim.machine.MachineException; + +public class MainMemoryParseException extends MachineException +{ + + /** + * + */ + private static final long serialVersionUID = 6820101808901789906L; + + public MainMemoryParseException() + { + super(); + } + + public MainMemoryParseException(String message) + { + super(message); + } + + public MainMemoryParseException(Throwable cause) + { + super(cause); + } + +} diff --git a/plugins/net.mograsim.machine/src/net/mograsim/machine/standard/memory/MainMemoryParser.java b/plugins/net.mograsim.machine/src/net/mograsim/machine/standard/memory/MainMemoryParser.java new file mode 100644 index 00000000..ea872961 --- /dev/null +++ b/plugins/net.mograsim.machine/src/net/mograsim/machine/standard/memory/MainMemoryParser.java @@ -0,0 +1,96 @@ +package net.mograsim.machine.standard.memory; + +import java.io.BufferedReader; +import java.io.ByteArrayInputStream; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; + +import net.mograsim.logic.core.types.BitVector; +import net.mograsim.machine.MainMemory; +import net.mograsim.machine.MainMemoryDefinition; + +public class MainMemoryParser +{ + private final static String lineSeparator = System.getProperty("line.separator"); + + public static void parseMemory(final MainMemory memory, String inputPath) throws IOException + { + try (InputStream input = new FileInputStream(inputPath)) + { + parseMemory(memory, input); + } + } + + /** + * @param input The input to parse must be in csv format; The stream is closed after being consumed. + * + * @throws IOException + */ + public static MainMemory parseMemory(MainMemoryDefinition memDef, InputStream input) throws IOException + { + try + { + MainMemory memory = new WordAddressableMemory(memDef); + parseMemory(memory, input); + return memory; + } + catch (NullPointerException e) + { + throw new MainMemoryParseException(e); + } + } + + /** + * + * @param input The input to parse must be in csv format; The stream is closed after being consumed. + * + * @throws IOException + */ + public static void parseMemory(final MainMemory memory, InputStream input) throws IOException + { + try (BufferedReader reader = new BufferedReader(new InputStreamReader(input))) + { + MainMemoryDefinition def = memory.getDefinition(); + + long minAddr = def.getMinimalAddress(); + long maxAddr = def.getMaximalAddress(); + + String line; + long i = minAddr; + try + { + for (; i <= maxAddr && reader.ready() && !"".equals((line = reader.readLine())); i++) + { + memory.setCell(i, BitVector.parse(line)); + } + } + catch (IOException e) + { + e.printStackTrace(); + } + } + } + + public static InputStream write(MainMemory memory) + { + return new InputStream() + { + long instIndex = memory.getDefinition().getMinimalAddress(), maxAddress = memory.getDefinition().getMaximalAddress(); + InputStream instStream = new ByteArrayInputStream(new byte[0]); + + @Override + public int read() throws IOException + { + int val = instStream.read(); + if (val == -1 && instIndex <= maxAddress) + { + instStream = new ByteArrayInputStream((memory.getCell(instIndex++).toString() + lineSeparator).getBytes()); + val = instStream.read(); + } + return val; + } + }; + } +} -- 2.17.1