|
@@ -0,0 +1,153 @@
|
|
|
+/**
|
|
|
+ * MCS Media Computer Software
|
|
|
+ * Copyright 2018 by Wilfried Klaas
|
|
|
+ * Project: SPSEmulator
|
|
|
+ * File: SPSAssembler.java
|
|
|
+ * EMail: W.Klaas@gmx.de
|
|
|
+ * Created: 25.11.2018 wklaa_000
|
|
|
+ *
|
|
|
+ * This program is free software: you can redistribute it and/or modify
|
|
|
+ * it under the terms of the GNU General Public License as published by
|
|
|
+ * the Free Software Foundation, either version 3 of the License, or
|
|
|
+ * (at your option) any later version.
|
|
|
+ *
|
|
|
+ * This program is distributed in the hope that it will be useful,
|
|
|
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
+ * GNU General Public License for more details.
|
|
|
+ *
|
|
|
+ * You should have received a copy of the GNU General Public License
|
|
|
+ * along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
+ */
|
|
|
+package de.mcs.tools.sps;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileNotFoundException;
|
|
|
+import java.io.IOException;
|
|
|
+import java.nio.charset.Charset;
|
|
|
+import java.nio.file.Files;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Iterator;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Map.Entry;
|
|
|
+
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+
|
|
|
+import de.mcs.tools.sps.mnemonic.Mnemonic;
|
|
|
+import de.mcs.tools.sps.mnemonic.MnemonicFactory;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author wklaa_000
|
|
|
+ *
|
|
|
+ */
|
|
|
+public class SPSAssembler {
|
|
|
+
|
|
|
+ private static File source;
|
|
|
+ private static File destination;
|
|
|
+ private static int lineNumber;
|
|
|
+ private static Map<String, Integer> labels = new HashMap<>();
|
|
|
+ private static boolean inBlockComment;
|
|
|
+ private static List<Mnemonic> mnemonics = new ArrayList<>();
|
|
|
+
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ */
|
|
|
+ public SPSAssembler() {
|
|
|
+ // TODO Auto-generated constructor stub
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param args
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ public static void main(String[] args) throws IOException {
|
|
|
+ if (args.length == 0) {
|
|
|
+ showHelp();
|
|
|
+ }
|
|
|
+ source = new File(args[0]);
|
|
|
+ if (!source.exists()) {
|
|
|
+ throw new FileNotFoundException(String.format("source file not found. %s", source.getAbsolutePath()));
|
|
|
+ }
|
|
|
+ System.out.printf("source file: %s \r\n", source.getName());
|
|
|
+ if (args.length > 1) {
|
|
|
+ destination = new File(args[1]);
|
|
|
+ } else {
|
|
|
+ String name = source.getName();
|
|
|
+ name = name.substring(0, source.getName().lastIndexOf("."));
|
|
|
+ destination = new File(source.getParentFile(), name + ".hex");
|
|
|
+ }
|
|
|
+ System.out.printf("destination file: %s \r\n", destination.getName());
|
|
|
+ System.out.println();
|
|
|
+
|
|
|
+ List<String> sourceFile = Files.readAllLines(source.toPath(), Charset.forName("UTF-8"));
|
|
|
+ sourceFile.forEach(line -> parseLine(line));
|
|
|
+
|
|
|
+ System.out.println("Mnemonics");
|
|
|
+ int pos = 0;
|
|
|
+ for (Iterator<Mnemonic> iterator = mnemonics.iterator(); iterator.hasNext();) {
|
|
|
+ Mnemonic mnemonic = iterator.next();
|
|
|
+ System.out.printf("%03d: %s\r\n", pos, mnemonic.toString());
|
|
|
+ pos++;
|
|
|
+ }
|
|
|
+
|
|
|
+ System.out.println();
|
|
|
+ System.out.println("labels");
|
|
|
+ for (Entry<String, Integer> entry : labels.entrySet()) {
|
|
|
+ System.out.printf("%s: %03d\r\n", entry.getKey(), entry.getValue());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void parseLine(String line) {
|
|
|
+ if (line.startsWith(":")) {
|
|
|
+ String label = getLabel(line);
|
|
|
+ labels.put(label, lineNumber);
|
|
|
+ } else {
|
|
|
+ Mnemonic mnemonic = getMnemonic(line);
|
|
|
+ if (mnemonic != null) {
|
|
|
+ lineNumber++;
|
|
|
+ mnemonics.add(mnemonic);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static Mnemonic getMnemonic(String line) {
|
|
|
+ String newLine = stripComments(line);
|
|
|
+ if (StringUtils.isNotEmpty(newLine)) {
|
|
|
+ Mnemonic mnemonic = MnemonicFactory.getMnemonic(newLine);
|
|
|
+ return mnemonic;
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String stripComments(String line) {
|
|
|
+ if (line.indexOf("*/") >= 0) {
|
|
|
+ inBlockComment = false;
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ if (inBlockComment) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ if (line.indexOf(";") >= 0) {
|
|
|
+ line = line.substring(0, line.indexOf(";")).trim();
|
|
|
+ return line;
|
|
|
+ }
|
|
|
+ if (line.startsWith("/*")) {
|
|
|
+ inBlockComment = true;
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return line.trim();
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String getLabel(String line) {
|
|
|
+ line = stripComments(line);
|
|
|
+ return line;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void showHelp() {
|
|
|
+ System.out.println("usage SPSAssembler <sorucefile> [<destination>]");
|
|
|
+ System.exit(0);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|