OpenCores
URL https://opencores.org/ocsvn/leros/leros/trunk

Subversion Repositories leros

[/] [leros/] [trunk/] [java/] [tools/] [src/] [leros/] [asm/] [LerosAsm.java] - Rev 4

Compare with Previous | Blame | View Log

/*
   Copyright 2011 Martin Schoeberl <masca@imm.dtu.dk>,
                  Technical University of Denmark, DTU Informatics. 
   All rights reserved.
 
   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions are met:
 
      1. Redistributions of source code must retain the above copyright notice,
         this list of conditions and the following disclaimer.
 
      2. Redistributions in binary form must reproduce the above copyright
         notice, this list of conditions and the following disclaimer in the
         documentation and/or other materials provided with the distribution.
 
   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER ``AS IS'' AND ANY EXPRESS
   OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
   NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
   DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
   The views and conclusions contained in the software and documentation are
   those of the authors and should not be interpreted as representing official
   policies, either expressed or implied, of the copyright holder.
 */
 
package leros.asm;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.List;
 
import org.antlr.runtime.ANTLRInputStream;
import org.antlr.runtime.CommonTokenStream;
 
import leros.asm.generated.*;
 
public class LerosAsm {
 
	static final int ADDRBITS = 9;
	static final int DATABITS = 16;
	static final int ROM_LEN = 1 << ADDRBITS;
 
	String fname;
	String dstDir = "./";
	String srcDir = "./";
 
	public LerosAsm(String[] args) {
		srcDir = System.getProperty("user.dir");
		dstDir = System.getProperty("user.dir");
		processOptions(args);
		if (!srcDir.endsWith(File.separator))
			srcDir += File.separator;
		if (!dstDir.endsWith(File.separator))
			dstDir += File.separator;
	}
 
	String bin(int val, int bits) {
 
		String s = "";
		for (int i = 0; i < bits; ++i) {
			s += (val & (1 << (bits - i - 1))) != 0 ? "1" : "0";
		}
		return s;
	}
 
	String getRomHeader() {
 
		String line = "--\n";
		line += "--\tleros_rom.vhd\n";
		line += "--\n";
		line += "--\tgeneric VHDL version of ROM\n";
		line += "--\n";
		line += "--\t\tDONT edit this file!\n";
		line += "--\t\tgenerated by " + this.getClass().getName() + "\n";
		line += "--\n";
		line += "\n";
		line += "library ieee;\n";
		line += "use ieee.std_logic_1164.all;\n";
		line += "\n";
		line += "entity leros_rom is\n";
		// line +=
		// "generic (width : integer; addr_width : integer);\t-- for compatibility\n";
		line += "port (\n";
		line += "    address : in std_logic_vector(" + (ADDRBITS - 1)
				+ " downto 0);\n";
		line += "    q : out std_logic_vector(" + (DATABITS - 1)
				+ " downto 0)\n";
		line += ");\n";
		line += "end leros_rom;\n";
		line += "\n";
		line += "architecture rtl of leros_rom is\n";
		line += "\n";
		line += "begin\n";
		line += "\n";
		line += "process(address) begin\n";
		line += "\n";
		line += "case address is\n";
 
		return line;
	}
 
	String getRomFeet() {
 
		String line = "\n";
		line += "    when others => q <= \"" + bin(0, DATABITS) + "\";\n";
		line += "end case;\n";
		line += "end process;\n";
		line += "\n";
		line += "end rtl;\n";
 
		return line;
	}
 
	public void dump(List list) {
 
		try {
 
			FileWriter romvhd = new FileWriter(dstDir + "leros_rom.vhd");
			PrintWriter romdat = new PrintWriter(new FileWriter("rom.txt"));
 
			romvhd.write(getRomHeader());
 
			Object o[] = list.toArray();
			for (int i = 0; i < o.length; ++i) {
				int val = ((Integer) o[i]).intValue();
				romdat.println(val);
				romvhd.write("    when \"" + bin(i, ADDRBITS) + "\" => q <= \""
						+ bin(val, DATABITS) + "\";");
//				romvhd.write(" -- " + inraw.readLine() + "\n");
				romvhd.write("\n");
 
			}
 
			romvhd.write(getRomFeet());
			romvhd.close();
			romdat.close();
 
			// PrintStream rom_mem = new PrintStream(new FileOutputStream(dstDir
			// + "mem_rom.dat"));
			// for (int i=0; i<ROM_LEN; ++i) {
			// rom_mem.println(romData[i]+" ");
			// }
			// rom_mem.close();
 
		} catch (IOException e) {
			System.out.println(e.getMessage());
			System.exit(-1);
		}
	}
 
	private boolean processOptions(String clist[]) {
		boolean success = true;
 
		for (int i = 0; i < clist.length; i++) {
			if (clist[i].equals("-s")) {
				srcDir = clist[++i];
			} else if (clist[i].equals("-d")) {
				dstDir = clist[++i];
			} else {
				fname = clist[i];
			}
		}
 
		return success;
	}
 
	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
 
		if (args.length < 1) {
			System.out
					.println("usage: java LerosAsm [-s srcDir] [-d dstDir] filename");
			System.exit(-1);
		}
		LerosAsm la = new LerosAsm(args);
 
		InputStream istr = new FileInputStream(la.srcDir + la.fname);
		ANTLRInputStream input = new ANTLRInputStream(istr);
		LerosLexer lexer = new LerosLexer(input);
		CommonTokenStream tokens = new CommonTokenStream(lexer);
		LerosParser parser = new LerosParser(tokens);
		parser.pass1();
		// parser.dump();
		parser.reset();
		List code = parser.pass2();
		System.out.println(code);
 
		la.dump(code);
	}
 
}
 

Compare with Previous | Blame | View Log

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.